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" |
| 51 | #include "builtin_read.h" |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 52 | #include "builtin_ulimit.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 53 | #include "math.h" |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 54 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 55 | # include "random.h" |
Denys Vlasenko | 36df048 | 2009-10-19 16:07:28 +0200 | [diff] [blame] | 56 | #else |
| 57 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 58 | #endif |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 59 | |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 60 | #define SKIP_definitions 1 |
| 61 | #include "applet_tables.h" |
| 62 | #undef SKIP_definitions |
| 63 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 64 | /* STANDALONE does not make sense, and won't compile */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 65 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 66 | # undef ENABLE_FEATURE_SH_STANDALONE |
| 67 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 68 | # undef IF_NOT_FEATURE_SH_STANDALONE |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 69 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
| 70 | # define IF_FEATURE_SH_STANDALONE(...) |
| 71 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 74 | #ifndef PIPE_BUF |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 75 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 76 | #endif |
| 77 | |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 78 | #if !BB_MMU |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 79 | # error "Do not even bother, ash will not run on NOMMU machine" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 83 | /* ============ Hash table sizes. Configurable. */ |
| 84 | |
| 85 | #define VTABSIZE 39 |
| 86 | #define ATABSIZE 39 |
| 87 | #define CMDTABLESIZE 31 /* should be prime */ |
| 88 | |
| 89 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 90 | /* ============ Shell options */ |
| 91 | |
| 92 | static const char *const optletters_optnames[] = { |
| 93 | "e" "errexit", |
| 94 | "f" "noglob", |
| 95 | "I" "ignoreeof", |
| 96 | "i" "interactive", |
| 97 | "m" "monitor", |
| 98 | "n" "noexec", |
| 99 | "s" "stdin", |
| 100 | "x" "xtrace", |
| 101 | "v" "verbose", |
| 102 | "C" "noclobber", |
| 103 | "a" "allexport", |
| 104 | "b" "notify", |
| 105 | "u" "nounset", |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 106 | "\0" "vi" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 107 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 108 | ,"\0" "pipefail" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 109 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 110 | #if DEBUG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 111 | ,"\0" "nolog" |
| 112 | ,"\0" "debug" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 113 | #endif |
| 114 | }; |
| 115 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 116 | #define optletters(n) optletters_optnames[n][0] |
| 117 | #define optnames(n) (optletters_optnames[n] + 1) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 118 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 119 | enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 120 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 121 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 122 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 123 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 124 | static const char homestr[] ALIGN1 = "HOME"; |
| 125 | static const char snlfmt[] ALIGN1 = "%s\n"; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 126 | static const char msg_illnum[] ALIGN1 = "Illegal number: %s"; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 128 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 129 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 130 | * jump locations. The global variable handler contains the location to |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 131 | * jump to when an exception occurs, and the global variable exception_type |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 132 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 133 | * exception handlers, the user should save the value of handler on entry |
| 134 | * to an inner scope, set handler to point to a jmploc structure for the |
| 135 | * inner scope, and restore handler on exit from the scope. |
| 136 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 137 | struct jmploc { |
| 138 | jmp_buf loc; |
| 139 | }; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 140 | |
| 141 | struct globals_misc { |
| 142 | /* pid of main shell */ |
| 143 | int rootpid; |
| 144 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 145 | int shlvl; |
| 146 | #define rootshell (!shlvl) |
| 147 | char *minusc; /* argument to -c option */ |
| 148 | |
| 149 | char *curdir; // = nullstr; /* current working directory */ |
| 150 | char *physdir; // = nullstr; /* physical working directory */ |
| 151 | |
| 152 | char *arg0; /* value of $0 */ |
| 153 | |
| 154 | struct jmploc *exception_handler; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 155 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 156 | volatile int suppress_int; /* counter */ |
| 157 | volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 158 | /* last pending signal */ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 159 | volatile /*sig_atomic_t*/ smallint pending_sig; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 160 | smallint exception_type; /* kind of exception (0..5) */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 161 | /* exceptions */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 162 | #define EXINT 0 /* SIGINT received */ |
| 163 | #define EXERROR 1 /* a generic error */ |
| 164 | #define EXSHELLPROC 2 /* execute a shell procedure */ |
| 165 | #define EXEXEC 3 /* command execution failed */ |
| 166 | #define EXEXIT 4 /* exit the shell */ |
| 167 | #define EXSIG 5 /* trapped signal in wait(1) */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 168 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 169 | smallint isloginsh; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 170 | char nullstr[1]; /* zero length string */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 171 | |
| 172 | char optlist[NOPTS]; |
| 173 | #define eflag optlist[0] |
| 174 | #define fflag optlist[1] |
| 175 | #define Iflag optlist[2] |
| 176 | #define iflag optlist[3] |
| 177 | #define mflag optlist[4] |
| 178 | #define nflag optlist[5] |
| 179 | #define sflag optlist[6] |
| 180 | #define xflag optlist[7] |
| 181 | #define vflag optlist[8] |
| 182 | #define Cflag optlist[9] |
| 183 | #define aflag optlist[10] |
| 184 | #define bflag optlist[11] |
| 185 | #define uflag optlist[12] |
| 186 | #define viflag optlist[13] |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 187 | #if ENABLE_ASH_BASH_COMPAT |
| 188 | # define pipefail optlist[14] |
| 189 | #else |
| 190 | # define pipefail 0 |
| 191 | #endif |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 192 | #if DEBUG |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 193 | # define nolog optlist[14 + ENABLE_ASH_BASH_COMPAT] |
| 194 | # define debug optlist[15 + ENABLE_ASH_BASH_COMPAT] |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 195 | #endif |
| 196 | |
| 197 | /* trap handler commands */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 198 | /* |
| 199 | * Sigmode records the current value of the signal handlers for the various |
| 200 | * 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] | 201 | * 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] | 202 | */ |
| 203 | char sigmode[NSIG - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 204 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 205 | #define S_CATCH 2 /* signal is caught */ |
| 206 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 207 | #define S_HARD_IGN 4 /* signal is ignored permenantly */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 208 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 209 | /* indicates specified signal received */ |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 210 | uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 211 | char *trap[NSIG]; |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 212 | char **trap_ptr; /* used only by "trap hack" */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 213 | |
| 214 | /* Rarely referenced stuff */ |
| 215 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 216 | random_t random_gen; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 217 | #endif |
| 218 | pid_t backgndpid; /* pid of last background process */ |
| 219 | 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] | 220 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 221 | extern struct globals_misc *const ash_ptr_to_globals_misc; |
| 222 | #define G_misc (*ash_ptr_to_globals_misc) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 223 | #define rootpid (G_misc.rootpid ) |
| 224 | #define shlvl (G_misc.shlvl ) |
| 225 | #define minusc (G_misc.minusc ) |
| 226 | #define curdir (G_misc.curdir ) |
| 227 | #define physdir (G_misc.physdir ) |
| 228 | #define arg0 (G_misc.arg0 ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 229 | #define exception_handler (G_misc.exception_handler) |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 230 | #define exception_type (G_misc.exception_type ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 231 | #define suppress_int (G_misc.suppress_int ) |
| 232 | #define pending_int (G_misc.pending_int ) |
| 233 | #define pending_sig (G_misc.pending_sig ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 234 | #define isloginsh (G_misc.isloginsh ) |
| 235 | #define nullstr (G_misc.nullstr ) |
| 236 | #define optlist (G_misc.optlist ) |
| 237 | #define sigmode (G_misc.sigmode ) |
| 238 | #define gotsig (G_misc.gotsig ) |
| 239 | #define trap (G_misc.trap ) |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 240 | #define trap_ptr (G_misc.trap_ptr ) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 241 | #define random_gen (G_misc.random_gen ) |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 242 | #define backgndpid (G_misc.backgndpid ) |
| 243 | #define job_warning (G_misc.job_warning) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 244 | #define INIT_G_misc() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 245 | (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ |
| 246 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 247 | curdir = nullstr; \ |
| 248 | physdir = nullstr; \ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 249 | trap_ptr = trap; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 250 | } while (0) |
| 251 | |
| 252 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 253 | /* ============ DEBUG */ |
| 254 | #if DEBUG |
| 255 | static void trace_printf(const char *fmt, ...); |
| 256 | static void trace_vprintf(const char *fmt, va_list va); |
| 257 | # define TRACE(param) trace_printf param |
| 258 | # define TRACEV(param) trace_vprintf param |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 259 | # define close(fd) do { \ |
| 260 | int dfd = (fd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 261 | if (close(dfd) < 0) \ |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 262 | bb_error_msg("bug on %d: closing %d(0x%x)", \ |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 263 | __LINE__, dfd, dfd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 264 | } while (0) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 265 | #else |
| 266 | # define TRACE(param) |
| 267 | # define TRACEV(param) |
| 268 | #endif |
| 269 | |
| 270 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 271 | /* ============ Utility functions */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 272 | #define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0) |
| 273 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 274 | static int isdigit_str9(const char *str) |
| 275 | { |
| 276 | int maxlen = 9 + 1; /* max 9 digits: 999999999 */ |
| 277 | while (--maxlen && isdigit(*str)) |
| 278 | str++; |
| 279 | return (*str == '\0'); |
| 280 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 281 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 282 | |
| 283 | /* ============ Interrupts / exceptions */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 284 | /* |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 285 | * These macros allow the user to suspend the handling of interrupt signals |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 286 | * 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] | 287 | * much more efficient and portable. (But hacking the kernel is so much |
| 288 | * more fun than worrying about efficiency and portability. :-)) |
| 289 | */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 290 | #define INT_OFF do { \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 291 | suppress_int++; \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 292 | xbarrier(); \ |
| 293 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 294 | |
| 295 | /* |
| 296 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 297 | * 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] | 298 | * stored in the global variable "exception_type". |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 299 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 300 | static void raise_exception(int) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 301 | static void |
| 302 | raise_exception(int e) |
| 303 | { |
| 304 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 305 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 306 | abort(); |
| 307 | #endif |
| 308 | INT_OFF; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 309 | exception_type = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 310 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 311 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 312 | #if DEBUG |
| 313 | #define raise_exception(e) do { \ |
| 314 | TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \ |
| 315 | raise_exception(e); \ |
| 316 | } while (0) |
| 317 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 318 | |
| 319 | /* |
| 320 | * Called from trap.c when a SIGINT is received. (If the user specifies |
| 321 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 322 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 323 | * are held using the INT_OFF macro. (The test for iflag is just |
| 324 | * defensive programming.) |
| 325 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 326 | static void raise_interrupt(void) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 327 | static void |
| 328 | raise_interrupt(void) |
| 329 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 330 | int ex_type; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 331 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 332 | pending_int = 0; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 333 | /* Signal is not automatically unmasked after it is raised, |
| 334 | * do it ourself - unmask all signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 335 | sigprocmask_allsigs(SIG_UNBLOCK); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 336 | /* pending_sig = 0; - now done in onsig() */ |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 337 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 338 | ex_type = EXSIG; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 339 | if (gotsig[SIGINT - 1] && !trap[SIGINT]) { |
| 340 | if (!(rootshell && iflag)) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 341 | /* Kill ourself with SIGINT */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 342 | signal(SIGINT, SIG_DFL); |
| 343 | raise(SIGINT); |
| 344 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 345 | ex_type = EXINT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 346 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 347 | raise_exception(ex_type); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 348 | /* NOTREACHED */ |
| 349 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 350 | #if DEBUG |
| 351 | #define raise_interrupt() do { \ |
| 352 | TRACE(("raising interrupt on line %d\n", __LINE__)); \ |
| 353 | raise_interrupt(); \ |
| 354 | } while (0) |
| 355 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 356 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 357 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 358 | int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 359 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 360 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 361 | if (--suppress_int == 0 && pending_int) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 362 | raise_interrupt(); |
| 363 | } |
| 364 | } |
| 365 | #define INT_ON int_on() |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 366 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 367 | force_int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 368 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 369 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 370 | suppress_int = 0; |
| 371 | if (pending_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 372 | raise_interrupt(); |
| 373 | } |
| 374 | #define FORCE_INT_ON force_int_on() |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 375 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 376 | #define SAVE_INT(v) ((v) = suppress_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 377 | |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 378 | #define RESTORE_INT(v) do { \ |
| 379 | xbarrier(); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 380 | suppress_int = (v); \ |
| 381 | if (suppress_int == 0 && pending_int) \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 382 | raise_interrupt(); \ |
| 383 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 384 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 385 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 386 | /* ============ Stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 387 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 388 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 389 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 390 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 391 | INT_OFF; |
| 392 | fputs(p, file); |
| 393 | INT_ON; |
| 394 | } |
| 395 | |
| 396 | static void |
| 397 | flush_stdout_stderr(void) |
| 398 | { |
| 399 | INT_OFF; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 400 | fflush_all(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 401 | INT_ON; |
| 402 | } |
| 403 | |
| 404 | static void |
| 405 | outcslow(int c, FILE *dest) |
| 406 | { |
| 407 | INT_OFF; |
| 408 | putc(c, dest); |
| 409 | fflush(dest); |
| 410 | INT_ON; |
| 411 | } |
| 412 | |
| 413 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 414 | static int |
| 415 | out1fmt(const char *fmt, ...) |
| 416 | { |
| 417 | va_list ap; |
| 418 | int r; |
| 419 | |
| 420 | INT_OFF; |
| 421 | va_start(ap, fmt); |
| 422 | r = vprintf(fmt, ap); |
| 423 | va_end(ap); |
| 424 | INT_ON; |
| 425 | return r; |
| 426 | } |
| 427 | |
| 428 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 429 | static int |
| 430 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 431 | { |
| 432 | va_list ap; |
| 433 | int ret; |
| 434 | |
| 435 | va_start(ap, fmt); |
| 436 | INT_OFF; |
| 437 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 438 | va_end(ap); |
| 439 | INT_ON; |
| 440 | return ret; |
| 441 | } |
| 442 | |
| 443 | static void |
| 444 | out1str(const char *p) |
| 445 | { |
| 446 | outstr(p, stdout); |
| 447 | } |
| 448 | |
| 449 | static void |
| 450 | out2str(const char *p) |
| 451 | { |
| 452 | outstr(p, stderr); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 453 | flush_stdout_stderr(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 457 | /* ============ Parser structures */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 458 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 459 | /* control characters in argument strings */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 460 | #define CTL_FIRST CTLESC |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 461 | #define CTLESC ((unsigned char)'\201') /* escape next character */ |
| 462 | #define CTLVAR ((unsigned char)'\202') /* variable defn */ |
| 463 | #define CTLENDVAR ((unsigned char)'\203') |
| 464 | #define CTLBACKQ ((unsigned char)'\204') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 465 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ |
| 466 | /* CTLBACKQ | CTLQUOTE == '\205' */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 467 | #define CTLARI ((unsigned char)'\206') /* arithmetic expression */ |
| 468 | #define CTLENDARI ((unsigned char)'\207') |
| 469 | #define CTLQUOTEMARK ((unsigned char)'\210') |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 470 | #define CTL_LAST CTLQUOTEMARK |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 471 | |
| 472 | /* variable substitution byte (follows CTLVAR) */ |
| 473 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 474 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
| 475 | #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */ |
| 476 | |
| 477 | /* values of VSTYPE field */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 478 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 479 | #define VSMINUS 0x2 /* ${var-text} */ |
| 480 | #define VSPLUS 0x3 /* ${var+text} */ |
| 481 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 482 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 483 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 484 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 485 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 486 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 487 | #define VSLENGTH 0xa /* ${#var} */ |
| 488 | #if ENABLE_ASH_BASH_COMPAT |
| 489 | #define VSSUBSTR 0xc /* ${var:position:length} */ |
| 490 | #define VSREPLACE 0xd /* ${var/pattern/replacement} */ |
| 491 | #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */ |
| 492 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 493 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 494 | static const char dolatstr[] ALIGN1 = { |
| 495 | CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' |
| 496 | }; |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 497 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 498 | #define NCMD 0 |
| 499 | #define NPIPE 1 |
| 500 | #define NREDIR 2 |
| 501 | #define NBACKGND 3 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 502 | #define NSUBSHELL 4 |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 503 | #define NAND 5 |
| 504 | #define NOR 6 |
| 505 | #define NSEMI 7 |
| 506 | #define NIF 8 |
| 507 | #define NWHILE 9 |
| 508 | #define NUNTIL 10 |
| 509 | #define NFOR 11 |
| 510 | #define NCASE 12 |
| 511 | #define NCLIST 13 |
| 512 | #define NDEFUN 14 |
| 513 | #define NARG 15 |
| 514 | #define NTO 16 |
| 515 | #if ENABLE_ASH_BASH_COMPAT |
| 516 | #define NTO2 17 |
| 517 | #endif |
| 518 | #define NCLOBBER 18 |
| 519 | #define NFROM 19 |
| 520 | #define NFROMTO 20 |
| 521 | #define NAPPEND 21 |
| 522 | #define NTOFD 22 |
| 523 | #define NFROMFD 23 |
| 524 | #define NHERE 24 |
| 525 | #define NXHERE 25 |
| 526 | #define NNOT 26 |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 527 | #define N_NUMBER 27 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 528 | |
| 529 | union node; |
| 530 | |
| 531 | struct ncmd { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 532 | smallint type; /* Nxxxx */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 533 | union node *assign; |
| 534 | union node *args; |
| 535 | union node *redirect; |
| 536 | }; |
| 537 | |
| 538 | struct npipe { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 539 | smallint type; |
| 540 | smallint pipe_backgnd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 541 | struct nodelist *cmdlist; |
| 542 | }; |
| 543 | |
| 544 | struct nredir { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 545 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 546 | union node *n; |
| 547 | union node *redirect; |
| 548 | }; |
| 549 | |
| 550 | struct nbinary { |
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 *ch1; |
| 553 | union node *ch2; |
| 554 | }; |
| 555 | |
| 556 | struct nif { |
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 *test; |
| 559 | union node *ifpart; |
| 560 | union node *elsepart; |
| 561 | }; |
| 562 | |
| 563 | struct nfor { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 564 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 565 | union node *args; |
| 566 | union node *body; |
| 567 | char *var; |
| 568 | }; |
| 569 | |
| 570 | struct ncase { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 571 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 572 | union node *expr; |
| 573 | union node *cases; |
| 574 | }; |
| 575 | |
| 576 | struct nclist { |
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 *next; |
| 579 | union node *pattern; |
| 580 | union node *body; |
| 581 | }; |
| 582 | |
| 583 | struct narg { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 584 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 585 | union node *next; |
| 586 | char *text; |
| 587 | struct nodelist *backquote; |
| 588 | }; |
| 589 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 590 | /* nfile and ndup layout must match! |
| 591 | * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight |
| 592 | * that it is actually NTO2 (>&file), and change its type. |
| 593 | */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 594 | struct nfile { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 595 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 596 | union node *next; |
| 597 | int fd; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 598 | int _unused_dupfd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 599 | union node *fname; |
| 600 | char *expfname; |
| 601 | }; |
| 602 | |
| 603 | struct ndup { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 604 | smallint type; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 605 | union node *next; |
| 606 | int fd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 607 | int dupfd; |
| 608 | union node *vname; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 609 | char *_unused_expfname; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 610 | }; |
| 611 | |
| 612 | struct nhere { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 613 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 614 | union node *next; |
| 615 | int fd; |
| 616 | union node *doc; |
| 617 | }; |
| 618 | |
| 619 | struct nnot { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 620 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 621 | union node *com; |
| 622 | }; |
| 623 | |
| 624 | union node { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 625 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 626 | struct ncmd ncmd; |
| 627 | struct npipe npipe; |
| 628 | struct nredir nredir; |
| 629 | struct nbinary nbinary; |
| 630 | struct nif nif; |
| 631 | struct nfor nfor; |
| 632 | struct ncase ncase; |
| 633 | struct nclist nclist; |
| 634 | struct narg narg; |
| 635 | struct nfile nfile; |
| 636 | struct ndup ndup; |
| 637 | struct nhere nhere; |
| 638 | struct nnot nnot; |
| 639 | }; |
| 640 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 641 | /* |
| 642 | * NODE_EOF is returned by parsecmd when it encounters an end of file. |
| 643 | * It must be distinct from NULL. |
| 644 | */ |
| 645 | #define NODE_EOF ((union node *) -1L) |
| 646 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 647 | struct nodelist { |
| 648 | struct nodelist *next; |
| 649 | union node *n; |
| 650 | }; |
| 651 | |
| 652 | struct funcnode { |
| 653 | int count; |
| 654 | union node n; |
| 655 | }; |
| 656 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 657 | /* |
| 658 | * Free a parse tree. |
| 659 | */ |
| 660 | static void |
| 661 | freefunc(struct funcnode *f) |
| 662 | { |
| 663 | if (f && --f->count < 0) |
| 664 | free(f); |
| 665 | } |
| 666 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 667 | |
| 668 | /* ============ Debugging output */ |
| 669 | |
| 670 | #if DEBUG |
| 671 | |
| 672 | static FILE *tracefile; |
| 673 | |
| 674 | static void |
| 675 | trace_printf(const char *fmt, ...) |
| 676 | { |
| 677 | va_list va; |
| 678 | |
| 679 | if (debug != 1) |
| 680 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 681 | if (DEBUG_TIME) |
| 682 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 683 | if (DEBUG_PID) |
| 684 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 685 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 686 | 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] | 687 | va_start(va, fmt); |
| 688 | vfprintf(tracefile, fmt, va); |
| 689 | va_end(va); |
| 690 | } |
| 691 | |
| 692 | static void |
| 693 | trace_vprintf(const char *fmt, va_list va) |
| 694 | { |
| 695 | if (debug != 1) |
| 696 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 697 | if (DEBUG_TIME) |
| 698 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 699 | if (DEBUG_PID) |
| 700 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 701 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 702 | 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] | 703 | vfprintf(tracefile, fmt, va); |
| 704 | } |
| 705 | |
| 706 | static void |
| 707 | trace_puts(const char *s) |
| 708 | { |
| 709 | if (debug != 1) |
| 710 | return; |
| 711 | fputs(s, tracefile); |
| 712 | } |
| 713 | |
| 714 | static void |
| 715 | trace_puts_quoted(char *s) |
| 716 | { |
| 717 | char *p; |
| 718 | char c; |
| 719 | |
| 720 | if (debug != 1) |
| 721 | return; |
| 722 | putc('"', tracefile); |
| 723 | for (p = s; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 724 | switch ((unsigned char)*p) { |
| 725 | case '\n': c = 'n'; goto backslash; |
| 726 | case '\t': c = 't'; goto backslash; |
| 727 | case '\r': c = 'r'; goto backslash; |
| 728 | case '\"': c = '\"'; goto backslash; |
| 729 | case '\\': c = '\\'; goto backslash; |
| 730 | case CTLESC: c = 'e'; goto backslash; |
| 731 | case CTLVAR: c = 'v'; goto backslash; |
| 732 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash; |
| 733 | case CTLBACKQ: c = 'q'; goto backslash; |
| 734 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 735 | backslash: |
| 736 | putc('\\', tracefile); |
| 737 | putc(c, tracefile); |
| 738 | break; |
| 739 | default: |
| 740 | if (*p >= ' ' && *p <= '~') |
| 741 | putc(*p, tracefile); |
| 742 | else { |
| 743 | putc('\\', tracefile); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 744 | putc((*p >> 6) & 03, tracefile); |
| 745 | putc((*p >> 3) & 07, tracefile); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 746 | putc(*p & 07, tracefile); |
| 747 | } |
| 748 | break; |
| 749 | } |
| 750 | } |
| 751 | putc('"', tracefile); |
| 752 | } |
| 753 | |
| 754 | static void |
| 755 | trace_puts_args(char **ap) |
| 756 | { |
| 757 | if (debug != 1) |
| 758 | return; |
| 759 | if (!*ap) |
| 760 | return; |
| 761 | while (1) { |
| 762 | trace_puts_quoted(*ap); |
| 763 | if (!*++ap) { |
| 764 | putc('\n', tracefile); |
| 765 | break; |
| 766 | } |
| 767 | putc(' ', tracefile); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | static void |
| 772 | opentrace(void) |
| 773 | { |
| 774 | char s[100]; |
| 775 | #ifdef O_APPEND |
| 776 | int flags; |
| 777 | #endif |
| 778 | |
| 779 | if (debug != 1) { |
| 780 | if (tracefile) |
| 781 | fflush(tracefile); |
| 782 | /* leave open because libedit might be using it */ |
| 783 | return; |
| 784 | } |
| 785 | strcpy(s, "./trace"); |
| 786 | if (tracefile) { |
| 787 | if (!freopen(s, "a", tracefile)) { |
| 788 | fprintf(stderr, "Can't re-open %s\n", s); |
| 789 | debug = 0; |
| 790 | return; |
| 791 | } |
| 792 | } else { |
| 793 | tracefile = fopen(s, "a"); |
| 794 | if (tracefile == NULL) { |
| 795 | fprintf(stderr, "Can't open %s\n", s); |
| 796 | debug = 0; |
| 797 | return; |
| 798 | } |
| 799 | } |
| 800 | #ifdef O_APPEND |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 801 | flags = fcntl(fileno(tracefile), F_GETFL); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 802 | if (flags >= 0) |
| 803 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 804 | #endif |
| 805 | setlinebuf(tracefile); |
| 806 | fputs("\nTracing started.\n", tracefile); |
| 807 | } |
| 808 | |
| 809 | static void |
| 810 | indent(int amount, char *pfx, FILE *fp) |
| 811 | { |
| 812 | int i; |
| 813 | |
| 814 | for (i = 0; i < amount; i++) { |
| 815 | if (pfx && i == amount - 1) |
| 816 | fputs(pfx, fp); |
| 817 | putc('\t', fp); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | /* little circular references here... */ |
| 822 | static void shtree(union node *n, int ind, char *pfx, FILE *fp); |
| 823 | |
| 824 | static void |
| 825 | sharg(union node *arg, FILE *fp) |
| 826 | { |
| 827 | char *p; |
| 828 | struct nodelist *bqlist; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 829 | unsigned char subtype; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 830 | |
| 831 | if (arg->type != NARG) { |
| 832 | out1fmt("<node type %d>\n", arg->type); |
| 833 | abort(); |
| 834 | } |
| 835 | bqlist = arg->narg.backquote; |
| 836 | for (p = arg->narg.text; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 837 | switch ((unsigned char)*p) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 838 | case CTLESC: |
| 839 | putc(*++p, fp); |
| 840 | break; |
| 841 | case CTLVAR: |
| 842 | putc('$', fp); |
| 843 | putc('{', fp); |
| 844 | subtype = *++p; |
| 845 | if (subtype == VSLENGTH) |
| 846 | putc('#', fp); |
| 847 | |
| 848 | while (*p != '=') |
| 849 | putc(*p++, fp); |
| 850 | |
| 851 | if (subtype & VSNUL) |
| 852 | putc(':', fp); |
| 853 | |
| 854 | switch (subtype & VSTYPE) { |
| 855 | case VSNORMAL: |
| 856 | putc('}', fp); |
| 857 | break; |
| 858 | case VSMINUS: |
| 859 | putc('-', fp); |
| 860 | break; |
| 861 | case VSPLUS: |
| 862 | putc('+', fp); |
| 863 | break; |
| 864 | case VSQUESTION: |
| 865 | putc('?', fp); |
| 866 | break; |
| 867 | case VSASSIGN: |
| 868 | putc('=', fp); |
| 869 | break; |
| 870 | case VSTRIMLEFT: |
| 871 | putc('#', fp); |
| 872 | break; |
| 873 | case VSTRIMLEFTMAX: |
| 874 | putc('#', fp); |
| 875 | putc('#', fp); |
| 876 | break; |
| 877 | case VSTRIMRIGHT: |
| 878 | putc('%', fp); |
| 879 | break; |
| 880 | case VSTRIMRIGHTMAX: |
| 881 | putc('%', fp); |
| 882 | putc('%', fp); |
| 883 | break; |
| 884 | case VSLENGTH: |
| 885 | break; |
| 886 | default: |
| 887 | out1fmt("<subtype %d>", subtype); |
| 888 | } |
| 889 | break; |
| 890 | case CTLENDVAR: |
| 891 | putc('}', fp); |
| 892 | break; |
| 893 | case CTLBACKQ: |
| 894 | case CTLBACKQ|CTLQUOTE: |
| 895 | putc('$', fp); |
| 896 | putc('(', fp); |
| 897 | shtree(bqlist->n, -1, NULL, fp); |
| 898 | putc(')', fp); |
| 899 | break; |
| 900 | default: |
| 901 | putc(*p, fp); |
| 902 | break; |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 907 | static void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 908 | shcmd(union node *cmd, FILE *fp) |
| 909 | { |
| 910 | union node *np; |
| 911 | int first; |
| 912 | const char *s; |
| 913 | int dftfd; |
| 914 | |
| 915 | first = 1; |
| 916 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 917 | if (!first) |
| 918 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 919 | sharg(np, fp); |
| 920 | first = 0; |
| 921 | } |
| 922 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 923 | if (!first) |
| 924 | putc(' ', fp); |
| 925 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 926 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 927 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 928 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 929 | case NAPPEND: s = ">>"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 930 | #if ENABLE_ASH_BASH_COMPAT |
| 931 | case NTO2: |
| 932 | #endif |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 933 | case NTOFD: s = ">&"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 934 | case NFROM: s = "<"; break; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 935 | case NFROMFD: s = "<&"; break; |
| 936 | case NFROMTO: s = "<>"; break; |
| 937 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 938 | } |
| 939 | if (np->nfile.fd != dftfd) |
| 940 | fprintf(fp, "%d", np->nfile.fd); |
| 941 | fputs(s, fp); |
| 942 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 943 | fprintf(fp, "%d", np->ndup.dupfd); |
| 944 | } else { |
| 945 | sharg(np->nfile.fname, fp); |
| 946 | } |
| 947 | first = 0; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | static void |
| 952 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 953 | { |
| 954 | struct nodelist *lp; |
| 955 | const char *s; |
| 956 | |
| 957 | if (n == NULL) |
| 958 | return; |
| 959 | |
| 960 | indent(ind, pfx, fp); |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 961 | |
| 962 | if (n == NODE_EOF) { |
| 963 | fputs("<EOF>", fp); |
| 964 | return; |
| 965 | } |
| 966 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 967 | switch (n->type) { |
| 968 | case NSEMI: |
| 969 | s = "; "; |
| 970 | goto binop; |
| 971 | case NAND: |
| 972 | s = " && "; |
| 973 | goto binop; |
| 974 | case NOR: |
| 975 | s = " || "; |
| 976 | binop: |
| 977 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 978 | /* if (ind < 0) */ |
| 979 | fputs(s, fp); |
| 980 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 981 | break; |
| 982 | case NCMD: |
| 983 | shcmd(n, fp); |
| 984 | if (ind >= 0) |
| 985 | putc('\n', fp); |
| 986 | break; |
| 987 | case NPIPE: |
| 988 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 989 | shtree(lp->n, 0, NULL, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 990 | if (lp->next) |
| 991 | fputs(" | ", fp); |
| 992 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 993 | if (n->npipe.pipe_backgnd) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 994 | fputs(" &", fp); |
| 995 | if (ind >= 0) |
| 996 | putc('\n', fp); |
| 997 | break; |
| 998 | default: |
| 999 | fprintf(fp, "<node type %d>", n->type); |
| 1000 | if (ind >= 0) |
| 1001 | putc('\n', fp); |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | static void |
| 1007 | showtree(union node *n) |
| 1008 | { |
| 1009 | trace_puts("showtree called\n"); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 1010 | shtree(n, 1, NULL, stderr); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1013 | #endif /* DEBUG */ |
| 1014 | |
| 1015 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1016 | /* ============ Parser data */ |
| 1017 | |
| 1018 | /* |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1019 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 1020 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1021 | struct strlist { |
| 1022 | struct strlist *next; |
| 1023 | char *text; |
| 1024 | }; |
| 1025 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1026 | struct alias; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1027 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1028 | struct strpush { |
| 1029 | struct strpush *prev; /* preceding string on stack */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1030 | char *prev_string; |
| 1031 | int prev_left_in_line; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1032 | #if ENABLE_ASH_ALIAS |
| 1033 | struct alias *ap; /* if push was associated with an alias */ |
| 1034 | #endif |
| 1035 | char *string; /* remember the string since it may change */ |
| 1036 | }; |
| 1037 | |
| 1038 | struct parsefile { |
| 1039 | struct parsefile *prev; /* preceding file on stack */ |
| 1040 | int linno; /* current line */ |
| 1041 | int fd; /* file descriptor (or -1 if string) */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1042 | int left_in_line; /* number of chars left in this line */ |
| 1043 | int left_in_buffer; /* number of chars left in this buffer past the line */ |
| 1044 | char *next_to_pgetc; /* next char in buffer */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1045 | char *buf; /* input buffer */ |
| 1046 | struct strpush *strpush; /* for pushing strings at this level */ |
| 1047 | struct strpush basestrpush; /* so pushing one is fast */ |
| 1048 | }; |
| 1049 | |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1050 | static struct parsefile basepf; /* top level input file */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1051 | static struct parsefile *g_parsefile = &basepf; /* current input file */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1052 | static int startlinno; /* line # where last token started */ |
| 1053 | static char *commandname; /* currently executing command */ |
| 1054 | static struct strlist *cmdenviron; /* environment for builtin command */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1055 | static uint8_t exitstatus; /* exit status of last command */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1056 | |
| 1057 | |
| 1058 | /* ============ Message printing */ |
| 1059 | |
| 1060 | static void |
| 1061 | ash_vmsg(const char *msg, va_list ap) |
| 1062 | { |
| 1063 | fprintf(stderr, "%s: ", arg0); |
| 1064 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1065 | if (strcmp(arg0, commandname)) |
| 1066 | fprintf(stderr, "%s: ", commandname); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1067 | if (!iflag || g_parsefile->fd) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1068 | fprintf(stderr, "line %d: ", startlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1069 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1070 | vfprintf(stderr, msg, ap); |
| 1071 | outcslow('\n', stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1072 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1073 | |
| 1074 | /* |
| 1075 | * Exverror is called to raise the error exception. If the second argument |
| 1076 | * is not NULL then error prints an error message using printf style |
| 1077 | * formatting. It then raises the error exception. |
| 1078 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1079 | static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1080 | static void |
| 1081 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1082 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1083 | #if DEBUG |
| 1084 | if (msg) { |
| 1085 | TRACE(("ash_vmsg_and_raise(%d, \"", cond)); |
| 1086 | TRACEV((msg, ap)); |
| 1087 | TRACE(("\") pid=%d\n", getpid())); |
| 1088 | } else |
| 1089 | TRACE(("ash_vmsg_and_raise(%d, NULL) pid=%d\n", cond, getpid())); |
| 1090 | if (msg) |
| 1091 | #endif |
| 1092 | ash_vmsg(msg, ap); |
| 1093 | |
| 1094 | flush_stdout_stderr(); |
| 1095 | raise_exception(cond); |
| 1096 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1097 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1098 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1099 | static void ash_msg_and_raise_error(const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1100 | static void |
| 1101 | ash_msg_and_raise_error(const char *msg, ...) |
| 1102 | { |
| 1103 | va_list ap; |
| 1104 | |
| 1105 | va_start(ap, msg); |
| 1106 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1107 | /* NOTREACHED */ |
| 1108 | va_end(ap); |
| 1109 | } |
| 1110 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1111 | static void raise_error_syntax(const char *) NORETURN; |
| 1112 | static void |
| 1113 | raise_error_syntax(const char *msg) |
| 1114 | { |
| 1115 | ash_msg_and_raise_error("syntax error: %s", msg); |
| 1116 | /* NOTREACHED */ |
| 1117 | } |
| 1118 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1119 | static void ash_msg_and_raise(int, const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1120 | static void |
| 1121 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1122 | { |
| 1123 | va_list ap; |
| 1124 | |
| 1125 | va_start(ap, msg); |
| 1126 | ash_vmsg_and_raise(cond, msg, ap); |
| 1127 | /* NOTREACHED */ |
| 1128 | va_end(ap); |
| 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | * error/warning routines for external builtins |
| 1133 | */ |
| 1134 | static void |
| 1135 | ash_msg(const char *fmt, ...) |
| 1136 | { |
| 1137 | va_list ap; |
| 1138 | |
| 1139 | va_start(ap, fmt); |
| 1140 | ash_vmsg(fmt, ap); |
| 1141 | va_end(ap); |
| 1142 | } |
| 1143 | |
| 1144 | /* |
| 1145 | * Return a string describing an error. The returned string may be a |
| 1146 | * pointer to a static buffer that will be overwritten on the next call. |
| 1147 | * Action describes the operation that got the error. |
| 1148 | */ |
| 1149 | static const char * |
| 1150 | errmsg(int e, const char *em) |
| 1151 | { |
| 1152 | if (e == ENOENT || e == ENOTDIR) { |
| 1153 | return em; |
| 1154 | } |
| 1155 | return strerror(e); |
| 1156 | } |
| 1157 | |
| 1158 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1159 | /* ============ Memory allocation */ |
| 1160 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1161 | #if 0 |
| 1162 | /* I consider these wrappers nearly useless: |
| 1163 | * ok, they return you to nearest exception handler, but |
| 1164 | * how much memory do you leak in the process, making |
| 1165 | * memory starvation worse? |
| 1166 | */ |
| 1167 | static void * |
| 1168 | ckrealloc(void * p, size_t nbytes) |
| 1169 | { |
| 1170 | p = realloc(p, nbytes); |
| 1171 | if (!p) |
| 1172 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1173 | return p; |
| 1174 | } |
| 1175 | |
| 1176 | static void * |
| 1177 | ckmalloc(size_t nbytes) |
| 1178 | { |
| 1179 | return ckrealloc(NULL, nbytes); |
| 1180 | } |
| 1181 | |
| 1182 | static void * |
| 1183 | ckzalloc(size_t nbytes) |
| 1184 | { |
| 1185 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1186 | } |
| 1187 | |
| 1188 | static char * |
| 1189 | ckstrdup(const char *s) |
| 1190 | { |
| 1191 | char *p = strdup(s); |
| 1192 | if (!p) |
| 1193 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1194 | return p; |
| 1195 | } |
| 1196 | #else |
| 1197 | /* Using bbox equivalents. They exit if out of memory */ |
| 1198 | # define ckrealloc xrealloc |
| 1199 | # define ckmalloc xmalloc |
| 1200 | # define ckzalloc xzalloc |
| 1201 | # define ckstrdup xstrdup |
| 1202 | #endif |
| 1203 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1204 | /* |
| 1205 | * It appears that grabstackstr() will barf with such alignments |
| 1206 | * because stalloc() will return a string allocated in a new stackblock. |
| 1207 | */ |
| 1208 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1209 | enum { |
| 1210 | /* Most machines require the value returned from malloc to be aligned |
| 1211 | * in some way. The following macro will get this right |
| 1212 | * on many machines. */ |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 1213 | SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1, |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1214 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1215 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1216 | }; |
| 1217 | |
| 1218 | struct stack_block { |
| 1219 | struct stack_block *prev; |
| 1220 | char space[MINSIZE]; |
| 1221 | }; |
| 1222 | |
| 1223 | struct stackmark { |
| 1224 | struct stack_block *stackp; |
| 1225 | char *stacknxt; |
| 1226 | size_t stacknleft; |
| 1227 | struct stackmark *marknext; |
| 1228 | }; |
| 1229 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1230 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1231 | struct globals_memstack { |
| 1232 | struct stack_block *g_stackp; // = &stackbase; |
| 1233 | struct stackmark *markp; |
| 1234 | char *g_stacknxt; // = stackbase.space; |
| 1235 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1236 | size_t g_stacknleft; // = MINSIZE; |
| 1237 | int herefd; // = -1; |
| 1238 | struct stack_block stackbase; |
| 1239 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1240 | extern struct globals_memstack *const ash_ptr_to_globals_memstack; |
| 1241 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1242 | #define g_stackp (G_memstack.g_stackp ) |
| 1243 | #define markp (G_memstack.markp ) |
| 1244 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1245 | #define sstrend (G_memstack.sstrend ) |
| 1246 | #define g_stacknleft (G_memstack.g_stacknleft) |
| 1247 | #define herefd (G_memstack.herefd ) |
| 1248 | #define stackbase (G_memstack.stackbase ) |
| 1249 | #define INIT_G_memstack() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1250 | (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \ |
| 1251 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1252 | g_stackp = &stackbase; \ |
| 1253 | g_stacknxt = stackbase.space; \ |
| 1254 | g_stacknleft = MINSIZE; \ |
| 1255 | sstrend = stackbase.space + MINSIZE; \ |
| 1256 | herefd = -1; \ |
| 1257 | } while (0) |
| 1258 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1259 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1260 | #define stackblock() ((void *)g_stacknxt) |
| 1261 | #define stackblocksize() g_stacknleft |
| 1262 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1263 | /* |
| 1264 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1265 | * to make this more efficient, and also to avoid all sorts of exception |
| 1266 | * handling code to handle interrupts in the middle of a parse. |
| 1267 | * |
| 1268 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1269 | * well. |
| 1270 | */ |
| 1271 | static void * |
| 1272 | stalloc(size_t nbytes) |
| 1273 | { |
| 1274 | char *p; |
| 1275 | size_t aligned; |
| 1276 | |
| 1277 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1278 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1279 | size_t len; |
| 1280 | size_t blocksize; |
| 1281 | struct stack_block *sp; |
| 1282 | |
| 1283 | blocksize = aligned; |
| 1284 | if (blocksize < MINSIZE) |
| 1285 | blocksize = MINSIZE; |
| 1286 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1287 | if (len < blocksize) |
| 1288 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1289 | INT_OFF; |
| 1290 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1291 | sp->prev = g_stackp; |
| 1292 | g_stacknxt = sp->space; |
| 1293 | g_stacknleft = blocksize; |
| 1294 | sstrend = g_stacknxt + blocksize; |
| 1295 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1296 | INT_ON; |
| 1297 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1298 | p = g_stacknxt; |
| 1299 | g_stacknxt += aligned; |
| 1300 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1301 | return p; |
| 1302 | } |
| 1303 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1304 | static void * |
| 1305 | stzalloc(size_t nbytes) |
| 1306 | { |
| 1307 | return memset(stalloc(nbytes), 0, nbytes); |
| 1308 | } |
| 1309 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1310 | static void |
| 1311 | stunalloc(void *p) |
| 1312 | { |
| 1313 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1314 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1315 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1316 | abort(); |
| 1317 | } |
| 1318 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1319 | g_stacknleft += g_stacknxt - (char *)p; |
| 1320 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1323 | /* |
| 1324 | * Like strdup but works with the ash stack. |
| 1325 | */ |
| 1326 | static char * |
| 1327 | ststrdup(const char *p) |
| 1328 | { |
| 1329 | size_t len = strlen(p) + 1; |
| 1330 | return memcpy(stalloc(len), p, len); |
| 1331 | } |
| 1332 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1333 | static void |
| 1334 | setstackmark(struct stackmark *mark) |
| 1335 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1336 | mark->stackp = g_stackp; |
| 1337 | mark->stacknxt = g_stacknxt; |
| 1338 | mark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1339 | mark->marknext = markp; |
| 1340 | markp = mark; |
| 1341 | } |
| 1342 | |
| 1343 | static void |
| 1344 | popstackmark(struct stackmark *mark) |
| 1345 | { |
| 1346 | struct stack_block *sp; |
| 1347 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1348 | if (!mark->stackp) |
| 1349 | return; |
| 1350 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1351 | INT_OFF; |
| 1352 | markp = mark->marknext; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1353 | while (g_stackp != mark->stackp) { |
| 1354 | sp = g_stackp; |
| 1355 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1356 | free(sp); |
| 1357 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1358 | g_stacknxt = mark->stacknxt; |
| 1359 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1360 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1361 | INT_ON; |
| 1362 | } |
| 1363 | |
| 1364 | /* |
| 1365 | * When the parser reads in a string, it wants to stick the string on the |
| 1366 | * stack and only adjust the stack pointer when it knows how big the |
| 1367 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1368 | * of space on top of the stack and stackblocklen returns the length of |
| 1369 | * this block. Growstackblock will grow this space by at least one byte, |
| 1370 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1371 | * part of the block that has been used. |
| 1372 | */ |
| 1373 | static void |
| 1374 | growstackblock(void) |
| 1375 | { |
| 1376 | size_t newlen; |
| 1377 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1378 | newlen = g_stacknleft * 2; |
| 1379 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1380 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1381 | if (newlen < 128) |
| 1382 | newlen += 128; |
| 1383 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1384 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1385 | struct stack_block *oldstackp; |
| 1386 | struct stackmark *xmark; |
| 1387 | struct stack_block *sp; |
| 1388 | struct stack_block *prevstackp; |
| 1389 | size_t grosslen; |
| 1390 | |
| 1391 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1392 | oldstackp = g_stackp; |
| 1393 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1394 | prevstackp = sp->prev; |
| 1395 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1396 | sp = ckrealloc(sp, grosslen); |
| 1397 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1398 | g_stackp = sp; |
| 1399 | g_stacknxt = sp->space; |
| 1400 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1401 | sstrend = sp->space + newlen; |
| 1402 | |
| 1403 | /* |
| 1404 | * Stack marks pointing to the start of the old block |
| 1405 | * must be relocated to point to the new block |
| 1406 | */ |
| 1407 | xmark = markp; |
| 1408 | while (xmark != NULL && xmark->stackp == oldstackp) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1409 | xmark->stackp = g_stackp; |
| 1410 | xmark->stacknxt = g_stacknxt; |
| 1411 | xmark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1412 | xmark = xmark->marknext; |
| 1413 | } |
| 1414 | INT_ON; |
| 1415 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1416 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1417 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1418 | char *p = stalloc(newlen); |
| 1419 | |
| 1420 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1421 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1422 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | static void |
| 1427 | grabstackblock(size_t len) |
| 1428 | { |
| 1429 | len = SHELL_ALIGN(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1430 | g_stacknxt += len; |
| 1431 | g_stacknleft -= len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | /* |
| 1435 | * The following routines are somewhat easier to use than the above. |
| 1436 | * The user declares a variable of type STACKSTR, which may be declared |
| 1437 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1438 | * the user uses the macro STPUTC to add characters to the string. In |
| 1439 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1440 | * grown as necessary. When the user is done, she can just leave the |
| 1441 | * string there and refer to it using stackblock(). Or she can allocate |
| 1442 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1443 | * someone else to use the stack temporarily and then continue to grow |
| 1444 | * the string, the user should use grabstack to allocate the space, and |
| 1445 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1446 | * |
| 1447 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1448 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1449 | * is space for at least one character. |
| 1450 | */ |
| 1451 | static void * |
| 1452 | growstackstr(void) |
| 1453 | { |
| 1454 | size_t len = stackblocksize(); |
| 1455 | if (herefd >= 0 && len >= 1024) { |
| 1456 | full_write(herefd, stackblock(), len); |
| 1457 | return stackblock(); |
| 1458 | } |
| 1459 | growstackblock(); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1460 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | * Called from CHECKSTRSPACE. |
| 1465 | */ |
| 1466 | static char * |
| 1467 | makestrspace(size_t newlen, char *p) |
| 1468 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1469 | size_t len = p - g_stacknxt; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1470 | size_t size = stackblocksize(); |
| 1471 | |
| 1472 | for (;;) { |
| 1473 | size_t nleft; |
| 1474 | |
| 1475 | size = stackblocksize(); |
| 1476 | nleft = size - len; |
| 1477 | if (nleft >= newlen) |
| 1478 | break; |
| 1479 | growstackblock(); |
| 1480 | } |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1481 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | static char * |
| 1485 | stack_nputstr(const char *s, size_t n, char *p) |
| 1486 | { |
| 1487 | p = makestrspace(n, p); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1488 | p = (char *)memcpy(p, s, n) + n; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1489 | return p; |
| 1490 | } |
| 1491 | |
| 1492 | static char * |
| 1493 | stack_putstr(const char *s, char *p) |
| 1494 | { |
| 1495 | return stack_nputstr(s, strlen(s), p); |
| 1496 | } |
| 1497 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1498 | static char * |
| 1499 | _STPUTC(int c, char *p) |
| 1500 | { |
| 1501 | if (p == sstrend) |
| 1502 | p = growstackstr(); |
| 1503 | *p++ = c; |
| 1504 | return p; |
| 1505 | } |
| 1506 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1507 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1508 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1509 | #define CHECKSTRSPACE(n, p) do { \ |
| 1510 | char *q = (p); \ |
| 1511 | size_t l = (n); \ |
| 1512 | size_t m = sstrend - q; \ |
| 1513 | if (l > m) \ |
| 1514 | (p) = makestrspace(l, q); \ |
| 1515 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1516 | #define USTPUTC(c, p) (*(p)++ = (c)) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1517 | #define STACKSTRNUL(p) do { \ |
| 1518 | if ((p) == sstrend) \ |
| 1519 | (p) = growstackstr(); \ |
| 1520 | *(p) = '\0'; \ |
| 1521 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1522 | #define STUNPUTC(p) (--(p)) |
| 1523 | #define STTOPC(p) ((p)[-1]) |
| 1524 | #define STADJUST(amount, p) ((p) += (amount)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1525 | |
| 1526 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1527 | #define ungrabstackstr(s, p) stunalloc(s) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1528 | #define stackstrend() ((void *)sstrend) |
| 1529 | |
| 1530 | |
| 1531 | /* ============ String helpers */ |
| 1532 | |
| 1533 | /* |
| 1534 | * prefix -- see if pfx is a prefix of string. |
| 1535 | */ |
| 1536 | static char * |
| 1537 | prefix(const char *string, const char *pfx) |
| 1538 | { |
| 1539 | while (*pfx) { |
| 1540 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1541 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1542 | } |
| 1543 | return (char *) string; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
| 1547 | * Check for a valid number. This should be elsewhere. |
| 1548 | */ |
| 1549 | static int |
| 1550 | is_number(const char *p) |
| 1551 | { |
| 1552 | do { |
| 1553 | if (!isdigit(*p)) |
| 1554 | return 0; |
| 1555 | } while (*++p != '\0'); |
| 1556 | return 1; |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * Convert a string of digits to an integer, printing an error message on |
| 1561 | * failure. |
| 1562 | */ |
| 1563 | static int |
| 1564 | number(const char *s) |
| 1565 | { |
| 1566 | if (!is_number(s)) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1567 | ash_msg_and_raise_error(msg_illnum, s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1568 | return atoi(s); |
| 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | * Produce a possibly single quoted string suitable as input to the shell. |
| 1573 | * The return string is allocated on the stack. |
| 1574 | */ |
| 1575 | static char * |
| 1576 | single_quote(const char *s) |
| 1577 | { |
| 1578 | char *p; |
| 1579 | |
| 1580 | STARTSTACKSTR(p); |
| 1581 | |
| 1582 | do { |
| 1583 | char *q; |
| 1584 | size_t len; |
| 1585 | |
| 1586 | len = strchrnul(s, '\'') - s; |
| 1587 | |
| 1588 | q = p = makestrspace(len + 3, p); |
| 1589 | |
| 1590 | *q++ = '\''; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1591 | q = (char *)memcpy(q, s, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1592 | *q++ = '\''; |
| 1593 | s += len; |
| 1594 | |
| 1595 | STADJUST(q - p, p); |
| 1596 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1597 | if (*s != '\'') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1598 | break; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1599 | len = 0; |
| 1600 | do len++; while (*++s == '\''); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1601 | |
| 1602 | q = p = makestrspace(len + 3, p); |
| 1603 | |
| 1604 | *q++ = '"'; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1605 | q = (char *)memcpy(q, s - len, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1606 | *q++ = '"'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1607 | |
| 1608 | STADJUST(q - p, p); |
| 1609 | } while (*s); |
| 1610 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1611 | USTPUTC('\0', p); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1612 | |
| 1613 | return stackblock(); |
| 1614 | } |
| 1615 | |
| 1616 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1617 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1618 | |
| 1619 | static char **argptr; /* argument list for builtin commands */ |
| 1620 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1621 | static char *optptr; /* used by nextopt */ |
| 1622 | |
| 1623 | /* |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1624 | * XXX - should get rid of. Have all builtins use getopt(3). |
| 1625 | * The library getopt must have the BSD extension static variable |
| 1626 | * "optreset", otherwise it can't be used within the shell safely. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1627 | * |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1628 | * Standard option processing (a la getopt) for builtin routines. |
| 1629 | * The only argument that is passed to nextopt is the option string; |
| 1630 | * the other arguments are unnecessary. It returns the character, |
| 1631 | * or '\0' on end of input. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1632 | */ |
| 1633 | static int |
| 1634 | nextopt(const char *optstring) |
| 1635 | { |
| 1636 | char *p; |
| 1637 | const char *q; |
| 1638 | char c; |
| 1639 | |
| 1640 | p = optptr; |
| 1641 | if (p == NULL || *p == '\0') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1642 | /* We ate entire "-param", take next one */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1643 | p = *argptr; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1644 | if (p == NULL) |
| 1645 | return '\0'; |
| 1646 | if (*p != '-') |
| 1647 | return '\0'; |
| 1648 | if (*++p == '\0') /* just "-" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1649 | return '\0'; |
| 1650 | argptr++; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1651 | if (LONE_DASH(p)) /* "--" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1652 | return '\0'; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1653 | /* p => next "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1654 | } |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1655 | /* p => some option char in the middle of a "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1656 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 1657 | for (q = optstring; *q != c;) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1658 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1659 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1660 | if (*++q == ':') |
| 1661 | q++; |
| 1662 | } |
| 1663 | if (*++q == ':') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1664 | if (*p == '\0') { |
| 1665 | p = *argptr++; |
| 1666 | if (p == NULL) |
| 1667 | ash_msg_and_raise_error("no arg for -%c option", c); |
| 1668 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1669 | optionarg = p; |
| 1670 | p = NULL; |
| 1671 | } |
| 1672 | optptr = p; |
| 1673 | return c; |
| 1674 | } |
| 1675 | |
| 1676 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1677 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1678 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1679 | /* |
| 1680 | * The parsefile structure pointed to by the global variable parsefile |
| 1681 | * contains information about the current file being read. |
| 1682 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1683 | struct shparam { |
| 1684 | int nparam; /* # of positional parameters (without $0) */ |
| 1685 | #if ENABLE_ASH_GETOPTS |
| 1686 | int optind; /* next parameter to be processed by getopts */ |
| 1687 | int optoff; /* used by getopts */ |
| 1688 | #endif |
| 1689 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 1690 | char **p; /* parameter list */ |
| 1691 | }; |
| 1692 | |
| 1693 | /* |
| 1694 | * Free the list of positional parameters. |
| 1695 | */ |
| 1696 | static void |
| 1697 | freeparam(volatile struct shparam *param) |
| 1698 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1699 | if (param->malloced) { |
Denis Vlasenko | 3177ba0 | 2008-07-13 20:39:23 +0000 | [diff] [blame] | 1700 | char **ap, **ap1; |
| 1701 | ap = ap1 = param->p; |
| 1702 | while (*ap) |
| 1703 | free(*ap++); |
| 1704 | free(ap1); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1709 | static void FAST_FUNC getoptsreset(const char *value); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1710 | #endif |
| 1711 | |
| 1712 | struct var { |
| 1713 | struct var *next; /* next entry in hash list */ |
| 1714 | int flags; /* flags are defined above */ |
| 1715 | const char *text; /* name=value */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1716 | void (*func)(const char *) FAST_FUNC; /* function to be called when */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1717 | /* the variable gets set/unset */ |
| 1718 | }; |
| 1719 | |
| 1720 | struct localvar { |
| 1721 | struct localvar *next; /* next local variable in list */ |
| 1722 | struct var *vp; /* the variable that was made local */ |
| 1723 | int flags; /* saved flags */ |
| 1724 | const char *text; /* saved text */ |
| 1725 | }; |
| 1726 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1727 | /* flags */ |
| 1728 | #define VEXPORT 0x01 /* variable is exported */ |
| 1729 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 1730 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 1731 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 1732 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 1733 | #define VUNSET 0x20 /* the variable is not set */ |
| 1734 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 1735 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 1736 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1737 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1738 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 1739 | #else |
| 1740 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1741 | #endif |
| 1742 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1743 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1744 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1745 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1746 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1747 | change_lc_all(const char *value) |
| 1748 | { |
| 1749 | if (value && *value != '\0') |
| 1750 | setlocale(LC_ALL, value); |
| 1751 | } |
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_ctype(const char *value) |
| 1754 | { |
| 1755 | if (value && *value != '\0') |
| 1756 | setlocale(LC_CTYPE, value); |
| 1757 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1758 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1759 | #if ENABLE_ASH_MAIL |
| 1760 | static void chkmail(void); |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1761 | static void changemail(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1762 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1763 | static void changepath(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1764 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1765 | static void change_random(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1766 | #endif |
| 1767 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1768 | static const struct { |
| 1769 | int flags; |
| 1770 | const char *text; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1771 | void (*func)(const char *) FAST_FUNC; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1772 | } varinit_data[] = { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1773 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1774 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1775 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL\0" , changemail }, |
| 1776 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH\0", changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1777 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1778 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 1779 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 1780 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 1781 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1782 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1783 | { VSTRFIXED|VTEXTFIXED , "OPTIND=1" , getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1784 | #endif |
| 1785 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1786 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM\0", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1787 | #endif |
| 1788 | #if ENABLE_LOCALE_SUPPORT |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1789 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL\0" , change_lc_all }, |
| 1790 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE\0", change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1791 | #endif |
| 1792 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1793 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE\0", NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1794 | #endif |
| 1795 | }; |
| 1796 | |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1797 | struct redirtab; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1798 | |
| 1799 | struct globals_var { |
| 1800 | struct shparam shellparam; /* $@ current positional parameters */ |
| 1801 | struct redirtab *redirlist; |
| 1802 | int g_nullredirs; |
| 1803 | int preverrout_fd; /* save fd2 before print debug if xflag is set. */ |
| 1804 | struct var *vartab[VTABSIZE]; |
| 1805 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
| 1806 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1807 | extern struct globals_var *const ash_ptr_to_globals_var; |
| 1808 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1809 | #define shellparam (G_var.shellparam ) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1810 | //#define redirlist (G_var.redirlist ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1811 | #define g_nullredirs (G_var.g_nullredirs ) |
| 1812 | #define preverrout_fd (G_var.preverrout_fd) |
| 1813 | #define vartab (G_var.vartab ) |
| 1814 | #define varinit (G_var.varinit ) |
| 1815 | #define INIT_G_var() do { \ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1816 | unsigned i; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1817 | (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \ |
| 1818 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1819 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
| 1820 | varinit[i].flags = varinit_data[i].flags; \ |
| 1821 | varinit[i].text = varinit_data[i].text; \ |
| 1822 | varinit[i].func = varinit_data[i].func; \ |
| 1823 | } \ |
| 1824 | } while (0) |
| 1825 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1826 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1827 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1828 | # define vmail (&vifs)[1] |
| 1829 | # define vmpath (&vmail)[1] |
| 1830 | # define vpath (&vmpath)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1831 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1832 | # define vpath (&vifs)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1833 | #endif |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1834 | #define vps1 (&vpath)[1] |
| 1835 | #define vps2 (&vps1)[1] |
| 1836 | #define vps4 (&vps2)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1837 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1838 | # define voptind (&vps4)[1] |
| 1839 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1840 | # define vrandom (&voptind)[1] |
| 1841 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1842 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1843 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1844 | # define vrandom (&vps4)[1] |
| 1845 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1846 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1847 | |
| 1848 | /* |
| 1849 | * The following macros access the values of the above variables. |
| 1850 | * They have to skip over the name. They return the null string |
| 1851 | * for unset variables. |
| 1852 | */ |
| 1853 | #define ifsval() (vifs.text + 4) |
| 1854 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1855 | #if ENABLE_ASH_MAIL |
| 1856 | # define mailval() (vmail.text + 5) |
| 1857 | # define mpathval() (vmpath.text + 9) |
| 1858 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1859 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1860 | #define pathval() (vpath.text + 5) |
| 1861 | #define ps1val() (vps1.text + 4) |
| 1862 | #define ps2val() (vps2.text + 4) |
| 1863 | #define ps4val() (vps4.text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1864 | #if ENABLE_ASH_GETOPTS |
| 1865 | # define optindval() (voptind.text + 7) |
| 1866 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1867 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1868 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1869 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1870 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1871 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1872 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1873 | static void FAST_FUNC |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1874 | getoptsreset(const char *value) |
| 1875 | { |
| 1876 | shellparam.optind = number(value); |
| 1877 | shellparam.optoff = -1; |
| 1878 | } |
| 1879 | #endif |
| 1880 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1881 | /* |
| 1882 | * Return of a legal variable name (a letter or underscore followed by zero or |
| 1883 | * more letters, underscores, and digits). |
| 1884 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1885 | static char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1886 | endofname(const char *name) |
| 1887 | { |
| 1888 | char *p; |
| 1889 | |
| 1890 | p = (char *) name; |
| 1891 | if (!is_name(*p)) |
| 1892 | return p; |
| 1893 | while (*++p) { |
| 1894 | if (!is_in_name(*p)) |
| 1895 | break; |
| 1896 | } |
| 1897 | return p; |
| 1898 | } |
| 1899 | |
| 1900 | /* |
| 1901 | * Compares two strings up to the first = or '\0'. The first |
| 1902 | * string must be terminated by '='; the second may be terminated by |
| 1903 | * either '=' or '\0'. |
| 1904 | */ |
| 1905 | static int |
| 1906 | varcmp(const char *p, const char *q) |
| 1907 | { |
| 1908 | int c, d; |
| 1909 | |
| 1910 | while ((c = *p) == (d = *q)) { |
| 1911 | if (!c || c == '=') |
| 1912 | goto out; |
| 1913 | p++; |
| 1914 | q++; |
| 1915 | } |
| 1916 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 1917 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1918 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 1919 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1920 | out: |
| 1921 | return c - d; |
| 1922 | } |
| 1923 | |
| 1924 | static int |
| 1925 | varequal(const char *a, const char *b) |
| 1926 | { |
| 1927 | return !varcmp(a, b); |
| 1928 | } |
| 1929 | |
| 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 |
| 1964 | vps1.text = "PS1=\\w \\$ "; |
| 1965 | #else |
| 1966 | if (!geteuid()) |
| 1967 | vps1.text = "PS1=# "; |
| 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 { |
| 1972 | vpp = hashvar(vp->text); |
| 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) { |
| 1982 | if (varequal((*vpp)->text, name)) { |
| 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 | */ |
| 2006 | if ((v->flags & VDYNAMIC)) |
| 2007 | (*v->func)(NULL); |
| 2008 | #endif |
| 2009 | if (!(v->flags & VUNSET)) |
| 2010 | return strchrnul(v->text, '=') + 1; |
| 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) { |
| 2024 | if (varequal(sp->text, name)) |
| 2025 | return strchrnul(sp->text, '=') + 1; |
| 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); |
| 2051 | n = vp->text; |
| 2052 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2053 | } |
| 2054 | |
| 2055 | if (flags & VNOSET) |
| 2056 | return; |
| 2057 | |
| 2058 | if (vp->func && (flags & VNOFUNC) == 0) |
| 2059 | (*vp->func)(strchrnul(s, '=') + 1); |
| 2060 | |
| 2061 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 2062 | free((char*)vp->text); |
| 2063 | |
| 2064 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
| 2065 | } else { |
| 2066 | if (flags & VNOSET) |
| 2067 | return; |
| 2068 | /* not found */ |
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); |
| 2076 | vp->text = s; |
| 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) |
| 2174 | free((char*)vp->text); |
| 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(); |
| 2226 | *ep++ = (char *) vp->text; |
| 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) |
| 2521 | dest = bltinlookup(homestr); |
| 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) |
| 2568 | out1fmt(snlfmt, curdir); |
| 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 | } |
| 2584 | out1fmt(snlfmt, dir); |
| 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 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2592 | #define IBUFSIZ COMMON_BUFSIZE |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2593 | /* buffer for top level input file */ |
| 2594 | #define basebuf bb_common_bufsiz1 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2595 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2596 | /* Syntax classes */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2597 | #define CWORD 0 /* character is nothing special */ |
| 2598 | #define CNL 1 /* newline character */ |
| 2599 | #define CBACK 2 /* a backslash character */ |
| 2600 | #define CSQUOTE 3 /* single quote */ |
| 2601 | #define CDQUOTE 4 /* double quote */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2602 | #define CENDQUOTE 5 /* a terminating quote */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2603 | #define CBQUOTE 6 /* backwards single quote */ |
| 2604 | #define CVAR 7 /* a dollar sign */ |
| 2605 | #define CENDVAR 8 /* a '}' character */ |
| 2606 | #define CLP 9 /* a left paren in arithmetic */ |
| 2607 | #define CRP 10 /* a right paren in arithmetic */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2608 | #define CENDFILE 11 /* end of file */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2609 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 2610 | #define CSPCL 13 /* these terminate a word */ |
| 2611 | #define CIGN 14 /* character should be ignored */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2612 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2613 | #define PEOF 256 |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2614 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2615 | # define PEOA 257 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2616 | #endif |
| 2617 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2618 | #define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2619 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2620 | #if ENABLE_SH_MATH_SUPPORT |
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) | (d << 12)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2622 | #else |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2623 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8)) |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2624 | #endif |
Denys Vlasenko | 068d386 | 2009-11-29 01:41:11 +0100 | [diff] [blame] | 2625 | static const uint16_t S_I_T[] = { |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2626 | #if ENABLE_ASH_ALIAS |
| 2627 | SIT_ITEM(CSPCL , CIGN , CIGN , CIGN ), /* 0, PEOA */ |
| 2628 | #endif |
| 2629 | SIT_ITEM(CSPCL , CWORD , CWORD, CWORD ), /* 1, ' ' */ |
| 2630 | SIT_ITEM(CNL , CNL , CNL , CNL ), /* 2, \n */ |
| 2631 | SIT_ITEM(CWORD , CCTL , CCTL , CWORD ), /* 3, !*-/:=?[]~ */ |
| 2632 | SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD ), /* 4, '"' */ |
| 2633 | SIT_ITEM(CVAR , CVAR , CWORD, CVAR ), /* 5, $ */ |
| 2634 | SIT_ITEM(CSQUOTE , CWORD , CENDQUOTE, CWORD), /* 6, "'" */ |
| 2635 | SIT_ITEM(CSPCL , CWORD , CWORD, CLP ), /* 7, ( */ |
| 2636 | SIT_ITEM(CSPCL , CWORD , CWORD, CRP ), /* 8, ) */ |
| 2637 | SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 9, \ */ |
| 2638 | SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 10, ` */ |
| 2639 | SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 11, } */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2640 | #if !USE_SIT_FUNCTION |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2641 | SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */ |
| 2642 | SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 13, 0-9A-Za-z */ |
| 2643 | SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 14, CTLESC ... */ |
| 2644 | #endif |
| 2645 | #undef SIT_ITEM |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2646 | }; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2647 | /* Constants below must match table above */ |
| 2648 | enum { |
| 2649 | #if ENABLE_ASH_ALIAS |
| 2650 | CSPCL_CIGN_CIGN_CIGN , /* 0 */ |
| 2651 | #endif |
| 2652 | CSPCL_CWORD_CWORD_CWORD , /* 1 */ |
| 2653 | CNL_CNL_CNL_CNL , /* 2 */ |
| 2654 | CWORD_CCTL_CCTL_CWORD , /* 3 */ |
| 2655 | CDQUOTE_CENDQUOTE_CWORD_CWORD , /* 4 */ |
| 2656 | CVAR_CVAR_CWORD_CVAR , /* 5 */ |
| 2657 | CSQUOTE_CWORD_CENDQUOTE_CWORD , /* 6 */ |
| 2658 | CSPCL_CWORD_CWORD_CLP , /* 7 */ |
| 2659 | CSPCL_CWORD_CWORD_CRP , /* 8 */ |
| 2660 | CBACK_CBACK_CCTL_CBACK , /* 9 */ |
| 2661 | CBQUOTE_CBQUOTE_CWORD_CBQUOTE , /* 10 */ |
| 2662 | CENDVAR_CENDVAR_CWORD_CENDVAR , /* 11 */ |
| 2663 | CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 12 */ |
| 2664 | CWORD_CWORD_CWORD_CWORD , /* 13 */ |
| 2665 | CCTL_CCTL_CCTL_CCTL , /* 14 */ |
| 2666 | }; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2667 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2668 | /* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF, |
| 2669 | * caller must ensure proper cast on it if c is *char_ptr! |
| 2670 | */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2671 | /* Values for syntax param */ |
| 2672 | #define BASESYNTAX 0 /* not in quotes */ |
| 2673 | #define DQSYNTAX 1 /* in double quotes */ |
| 2674 | #define SQSYNTAX 2 /* in single quotes */ |
| 2675 | #define ARISYNTAX 3 /* in arithmetic */ |
| 2676 | #define PSSYNTAX 4 /* prompt. never passed to SIT() */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2677 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2678 | #if USE_SIT_FUNCTION |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2679 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2680 | static int |
| 2681 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2682 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2683 | static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-/:;<=>?[\\]`|}~"; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2684 | # if ENABLE_ASH_ALIAS |
| 2685 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2686 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
| 2687 | 7, 8, 3, 3, 3, 3, 1, 1, /* "()*-/:;<" */ |
| 2688 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 2689 | 11, 3 /* "}~" */ |
| 2690 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2691 | # else |
| 2692 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2693 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
| 2694 | 6, 7, 2, 2, 2, 2, 0, 0, /* "()*-/:;<" */ |
| 2695 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 2696 | 10, 2 /* "}~" */ |
| 2697 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2698 | # endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2699 | const char *s; |
| 2700 | int indx; |
| 2701 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2702 | if (c == PEOF) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2703 | return CENDFILE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2704 | # if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2705 | if (c == PEOA) |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2706 | indx = 0; |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2707 | else |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2708 | # endif |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2709 | { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2710 | /* Cast is purely for paranoia here, |
| 2711 | * just in case someone passed signed char to us */ |
| 2712 | if ((unsigned char)c >= CTL_FIRST |
| 2713 | && (unsigned char)c <= CTL_LAST |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2714 | ) { |
| 2715 | return CCTL; |
| 2716 | } |
| 2717 | s = strchrnul(spec_symbls, c); |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2718 | if (*s == '\0') |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2719 | return CWORD; |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2720 | indx = syntax_index_table[s - spec_symbls]; |
| 2721 | } |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2722 | return (S_I_T[indx] >> (syntax*4)) & 0xf; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2725 | #else /* !USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2726 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2727 | static const uint8_t syntax_index_table[] = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2728 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2729 | /* 0 */ CWORD_CWORD_CWORD_CWORD, |
| 2730 | /* 1 */ CWORD_CWORD_CWORD_CWORD, |
| 2731 | /* 2 */ CWORD_CWORD_CWORD_CWORD, |
| 2732 | /* 3 */ CWORD_CWORD_CWORD_CWORD, |
| 2733 | /* 4 */ CWORD_CWORD_CWORD_CWORD, |
| 2734 | /* 5 */ CWORD_CWORD_CWORD_CWORD, |
| 2735 | /* 6 */ CWORD_CWORD_CWORD_CWORD, |
| 2736 | /* 7 */ CWORD_CWORD_CWORD_CWORD, |
| 2737 | /* 8 */ CWORD_CWORD_CWORD_CWORD, |
| 2738 | /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 2739 | /* 10 "\n" */ CNL_CNL_CNL_CNL, |
| 2740 | /* 11 */ CWORD_CWORD_CWORD_CWORD, |
| 2741 | /* 12 */ CWORD_CWORD_CWORD_CWORD, |
| 2742 | /* 13 */ CWORD_CWORD_CWORD_CWORD, |
| 2743 | /* 14 */ CWORD_CWORD_CWORD_CWORD, |
| 2744 | /* 15 */ CWORD_CWORD_CWORD_CWORD, |
| 2745 | /* 16 */ CWORD_CWORD_CWORD_CWORD, |
| 2746 | /* 17 */ CWORD_CWORD_CWORD_CWORD, |
| 2747 | /* 18 */ CWORD_CWORD_CWORD_CWORD, |
| 2748 | /* 19 */ CWORD_CWORD_CWORD_CWORD, |
| 2749 | /* 20 */ CWORD_CWORD_CWORD_CWORD, |
| 2750 | /* 21 */ CWORD_CWORD_CWORD_CWORD, |
| 2751 | /* 22 */ CWORD_CWORD_CWORD_CWORD, |
| 2752 | /* 23 */ CWORD_CWORD_CWORD_CWORD, |
| 2753 | /* 24 */ CWORD_CWORD_CWORD_CWORD, |
| 2754 | /* 25 */ CWORD_CWORD_CWORD_CWORD, |
| 2755 | /* 26 */ CWORD_CWORD_CWORD_CWORD, |
| 2756 | /* 27 */ CWORD_CWORD_CWORD_CWORD, |
| 2757 | /* 28 */ CWORD_CWORD_CWORD_CWORD, |
| 2758 | /* 29 */ CWORD_CWORD_CWORD_CWORD, |
| 2759 | /* 30 */ CWORD_CWORD_CWORD_CWORD, |
| 2760 | /* 31 */ CWORD_CWORD_CWORD_CWORD, |
| 2761 | /* 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 2762 | /* 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
| 2763 | /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
| 2764 | /* 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 2765 | /* 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 2766 | /* 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 2767 | /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
| 2768 | /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
| 2769 | /* 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 2770 | /* 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 2771 | /* 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 2772 | /* 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 2773 | /* 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 2774 | /* 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 2775 | /* 46 "." */ CWORD_CWORD_CWORD_CWORD, |
| 2776 | /* 47 "/" */ CWORD_CCTL_CCTL_CWORD, |
| 2777 | /* 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 2778 | /* 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 2779 | /* 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 2780 | /* 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 2781 | /* 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 2782 | /* 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 2783 | /* 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 2784 | /* 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 2785 | /* 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 2786 | /* 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 2787 | /* 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 2788 | /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 2789 | /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 2790 | /* 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 2791 | /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 2792 | /* 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 2793 | /* 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 2794 | /* 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 2795 | /* 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 2796 | /* 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 2797 | /* 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 2798 | /* 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 2799 | /* 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 2800 | /* 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 2801 | /* 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 2802 | /* 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 2803 | /* 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 2804 | /* 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 2805 | /* 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 2806 | /* 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 2807 | /* 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 2808 | /* 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 2809 | /* 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 2810 | /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 2811 | /* 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 2812 | /* 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 2813 | /* 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 2814 | /* 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 2815 | /* 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 2816 | /* 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 2817 | /* 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 2818 | /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 2819 | /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 2820 | /* 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 2821 | /* 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 2822 | /* 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 2823 | /* 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 2824 | /* 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 2825 | /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 2826 | /* 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 2827 | /* 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 2828 | /* 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 2829 | /* 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 2830 | /* 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 2831 | /* 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 2832 | /* 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 2833 | /* 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 2834 | /* 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 2835 | /* 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 2836 | /* 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 2837 | /* 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 2838 | /* 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 2839 | /* 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 2840 | /* 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 2841 | /* 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 2842 | /* 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 2843 | /* 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 2844 | /* 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 2845 | /* 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 2846 | /* 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 2847 | /* 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 2848 | /* 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 2849 | /* 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 2850 | /* 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 2851 | /* 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 2852 | /* 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 2853 | /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 2854 | /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 2855 | /* 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 2856 | /* 127 del */ CWORD_CWORD_CWORD_CWORD, |
| 2857 | /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 2858 | /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 2859 | /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2860 | /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2861 | /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 2862 | /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 2863 | /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 2864 | /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 2865 | /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
| 2866 | /* 137 */ CWORD_CWORD_CWORD_CWORD, |
| 2867 | /* 138 */ CWORD_CWORD_CWORD_CWORD, |
| 2868 | /* 139 */ CWORD_CWORD_CWORD_CWORD, |
| 2869 | /* 140 */ CWORD_CWORD_CWORD_CWORD, |
| 2870 | /* 141 */ CWORD_CWORD_CWORD_CWORD, |
| 2871 | /* 142 */ CWORD_CWORD_CWORD_CWORD, |
| 2872 | /* 143 */ CWORD_CWORD_CWORD_CWORD, |
| 2873 | /* 144 */ CWORD_CWORD_CWORD_CWORD, |
| 2874 | /* 145 */ CWORD_CWORD_CWORD_CWORD, |
| 2875 | /* 146 */ CWORD_CWORD_CWORD_CWORD, |
| 2876 | /* 147 */ CWORD_CWORD_CWORD_CWORD, |
| 2877 | /* 148 */ CWORD_CWORD_CWORD_CWORD, |
| 2878 | /* 149 */ CWORD_CWORD_CWORD_CWORD, |
| 2879 | /* 150 */ CWORD_CWORD_CWORD_CWORD, |
| 2880 | /* 151 */ CWORD_CWORD_CWORD_CWORD, |
| 2881 | /* 152 */ CWORD_CWORD_CWORD_CWORD, |
| 2882 | /* 153 */ CWORD_CWORD_CWORD_CWORD, |
| 2883 | /* 154 */ CWORD_CWORD_CWORD_CWORD, |
| 2884 | /* 155 */ CWORD_CWORD_CWORD_CWORD, |
| 2885 | /* 156 */ CWORD_CWORD_CWORD_CWORD, |
| 2886 | /* 157 */ CWORD_CWORD_CWORD_CWORD, |
| 2887 | /* 158 */ CWORD_CWORD_CWORD_CWORD, |
| 2888 | /* 159 */ CWORD_CWORD_CWORD_CWORD, |
| 2889 | /* 160 */ CWORD_CWORD_CWORD_CWORD, |
| 2890 | /* 161 */ CWORD_CWORD_CWORD_CWORD, |
| 2891 | /* 162 */ CWORD_CWORD_CWORD_CWORD, |
| 2892 | /* 163 */ CWORD_CWORD_CWORD_CWORD, |
| 2893 | /* 164 */ CWORD_CWORD_CWORD_CWORD, |
| 2894 | /* 165 */ CWORD_CWORD_CWORD_CWORD, |
| 2895 | /* 166 */ CWORD_CWORD_CWORD_CWORD, |
| 2896 | /* 167 */ CWORD_CWORD_CWORD_CWORD, |
| 2897 | /* 168 */ CWORD_CWORD_CWORD_CWORD, |
| 2898 | /* 169 */ CWORD_CWORD_CWORD_CWORD, |
| 2899 | /* 170 */ CWORD_CWORD_CWORD_CWORD, |
| 2900 | /* 171 */ CWORD_CWORD_CWORD_CWORD, |
| 2901 | /* 172 */ CWORD_CWORD_CWORD_CWORD, |
| 2902 | /* 173 */ CWORD_CWORD_CWORD_CWORD, |
| 2903 | /* 174 */ CWORD_CWORD_CWORD_CWORD, |
| 2904 | /* 175 */ CWORD_CWORD_CWORD_CWORD, |
| 2905 | /* 176 */ CWORD_CWORD_CWORD_CWORD, |
| 2906 | /* 177 */ CWORD_CWORD_CWORD_CWORD, |
| 2907 | /* 178 */ CWORD_CWORD_CWORD_CWORD, |
| 2908 | /* 179 */ CWORD_CWORD_CWORD_CWORD, |
| 2909 | /* 180 */ CWORD_CWORD_CWORD_CWORD, |
| 2910 | /* 181 */ CWORD_CWORD_CWORD_CWORD, |
| 2911 | /* 182 */ CWORD_CWORD_CWORD_CWORD, |
| 2912 | /* 183 */ CWORD_CWORD_CWORD_CWORD, |
| 2913 | /* 184 */ CWORD_CWORD_CWORD_CWORD, |
| 2914 | /* 185 */ CWORD_CWORD_CWORD_CWORD, |
| 2915 | /* 186 */ CWORD_CWORD_CWORD_CWORD, |
| 2916 | /* 187 */ CWORD_CWORD_CWORD_CWORD, |
| 2917 | /* 188 */ CWORD_CWORD_CWORD_CWORD, |
| 2918 | /* 189 */ CWORD_CWORD_CWORD_CWORD, |
| 2919 | /* 190 */ CWORD_CWORD_CWORD_CWORD, |
| 2920 | /* 191 */ CWORD_CWORD_CWORD_CWORD, |
| 2921 | /* 192 */ CWORD_CWORD_CWORD_CWORD, |
| 2922 | /* 193 */ CWORD_CWORD_CWORD_CWORD, |
| 2923 | /* 194 */ CWORD_CWORD_CWORD_CWORD, |
| 2924 | /* 195 */ CWORD_CWORD_CWORD_CWORD, |
| 2925 | /* 196 */ CWORD_CWORD_CWORD_CWORD, |
| 2926 | /* 197 */ CWORD_CWORD_CWORD_CWORD, |
| 2927 | /* 198 */ CWORD_CWORD_CWORD_CWORD, |
| 2928 | /* 199 */ CWORD_CWORD_CWORD_CWORD, |
| 2929 | /* 200 */ CWORD_CWORD_CWORD_CWORD, |
| 2930 | /* 201 */ CWORD_CWORD_CWORD_CWORD, |
| 2931 | /* 202 */ CWORD_CWORD_CWORD_CWORD, |
| 2932 | /* 203 */ CWORD_CWORD_CWORD_CWORD, |
| 2933 | /* 204 */ CWORD_CWORD_CWORD_CWORD, |
| 2934 | /* 205 */ CWORD_CWORD_CWORD_CWORD, |
| 2935 | /* 206 */ CWORD_CWORD_CWORD_CWORD, |
| 2936 | /* 207 */ CWORD_CWORD_CWORD_CWORD, |
| 2937 | /* 208 */ CWORD_CWORD_CWORD_CWORD, |
| 2938 | /* 209 */ CWORD_CWORD_CWORD_CWORD, |
| 2939 | /* 210 */ CWORD_CWORD_CWORD_CWORD, |
| 2940 | /* 211 */ CWORD_CWORD_CWORD_CWORD, |
| 2941 | /* 212 */ CWORD_CWORD_CWORD_CWORD, |
| 2942 | /* 213 */ CWORD_CWORD_CWORD_CWORD, |
| 2943 | /* 214 */ CWORD_CWORD_CWORD_CWORD, |
| 2944 | /* 215 */ CWORD_CWORD_CWORD_CWORD, |
| 2945 | /* 216 */ CWORD_CWORD_CWORD_CWORD, |
| 2946 | /* 217 */ CWORD_CWORD_CWORD_CWORD, |
| 2947 | /* 218 */ CWORD_CWORD_CWORD_CWORD, |
| 2948 | /* 219 */ CWORD_CWORD_CWORD_CWORD, |
| 2949 | /* 220 */ CWORD_CWORD_CWORD_CWORD, |
| 2950 | /* 221 */ CWORD_CWORD_CWORD_CWORD, |
| 2951 | /* 222 */ CWORD_CWORD_CWORD_CWORD, |
| 2952 | /* 223 */ CWORD_CWORD_CWORD_CWORD, |
| 2953 | /* 224 */ CWORD_CWORD_CWORD_CWORD, |
| 2954 | /* 225 */ CWORD_CWORD_CWORD_CWORD, |
| 2955 | /* 226 */ CWORD_CWORD_CWORD_CWORD, |
| 2956 | /* 227 */ CWORD_CWORD_CWORD_CWORD, |
| 2957 | /* 228 */ CWORD_CWORD_CWORD_CWORD, |
| 2958 | /* 229 */ CWORD_CWORD_CWORD_CWORD, |
| 2959 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 2960 | /* 231 */ CWORD_CWORD_CWORD_CWORD, |
| 2961 | /* 232 */ CWORD_CWORD_CWORD_CWORD, |
| 2962 | /* 233 */ CWORD_CWORD_CWORD_CWORD, |
| 2963 | /* 234 */ CWORD_CWORD_CWORD_CWORD, |
| 2964 | /* 235 */ CWORD_CWORD_CWORD_CWORD, |
| 2965 | /* 236 */ CWORD_CWORD_CWORD_CWORD, |
| 2966 | /* 237 */ CWORD_CWORD_CWORD_CWORD, |
| 2967 | /* 238 */ CWORD_CWORD_CWORD_CWORD, |
| 2968 | /* 239 */ CWORD_CWORD_CWORD_CWORD, |
| 2969 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 2970 | /* 241 */ CWORD_CWORD_CWORD_CWORD, |
| 2971 | /* 242 */ CWORD_CWORD_CWORD_CWORD, |
| 2972 | /* 243 */ CWORD_CWORD_CWORD_CWORD, |
| 2973 | /* 244 */ CWORD_CWORD_CWORD_CWORD, |
| 2974 | /* 245 */ CWORD_CWORD_CWORD_CWORD, |
| 2975 | /* 246 */ CWORD_CWORD_CWORD_CWORD, |
| 2976 | /* 247 */ CWORD_CWORD_CWORD_CWORD, |
| 2977 | /* 248 */ CWORD_CWORD_CWORD_CWORD, |
| 2978 | /* 249 */ CWORD_CWORD_CWORD_CWORD, |
| 2979 | /* 250 */ CWORD_CWORD_CWORD_CWORD, |
| 2980 | /* 251 */ CWORD_CWORD_CWORD_CWORD, |
| 2981 | /* 252 */ CWORD_CWORD_CWORD_CWORD, |
| 2982 | /* 253 */ CWORD_CWORD_CWORD_CWORD, |
| 2983 | /* 254 */ CWORD_CWORD_CWORD_CWORD, |
| 2984 | /* 255 */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2985 | /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2986 | # if ENABLE_ASH_ALIAS |
| 2987 | /* PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 2988 | # endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2989 | }; |
| 2990 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2991 | # 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] | 2992 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2993 | #endif /* !USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2994 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2995 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2996 | /* ============ Alias handling */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 2997 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2998 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2999 | |
| 3000 | #define ALIASINUSE 1 |
| 3001 | #define ALIASDEAD 2 |
| 3002 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3003 | struct alias { |
| 3004 | struct alias *next; |
| 3005 | char *name; |
| 3006 | char *val; |
| 3007 | int flag; |
| 3008 | }; |
| 3009 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 3010 | |
| 3011 | static struct alias **atab; // [ATABSIZE]; |
| 3012 | #define INIT_G_alias() do { \ |
| 3013 | atab = xzalloc(ATABSIZE * sizeof(atab[0])); \ |
| 3014 | } while (0) |
| 3015 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3016 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3017 | static struct alias ** |
| 3018 | __lookupalias(const char *name) { |
| 3019 | unsigned int hashval; |
| 3020 | struct alias **app; |
| 3021 | const char *p; |
| 3022 | unsigned int ch; |
| 3023 | |
| 3024 | p = name; |
| 3025 | |
| 3026 | ch = (unsigned char)*p; |
| 3027 | hashval = ch << 4; |
| 3028 | while (ch) { |
| 3029 | hashval += ch; |
| 3030 | ch = (unsigned char)*++p; |
| 3031 | } |
| 3032 | app = &atab[hashval % ATABSIZE]; |
| 3033 | |
| 3034 | for (; *app; app = &(*app)->next) { |
| 3035 | if (strcmp(name, (*app)->name) == 0) { |
| 3036 | break; |
| 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | return app; |
| 3041 | } |
| 3042 | |
| 3043 | static struct alias * |
| 3044 | lookupalias(const char *name, int check) |
| 3045 | { |
| 3046 | struct alias *ap = *__lookupalias(name); |
| 3047 | |
| 3048 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 3049 | return NULL; |
| 3050 | return ap; |
| 3051 | } |
| 3052 | |
| 3053 | static struct alias * |
| 3054 | freealias(struct alias *ap) |
| 3055 | { |
| 3056 | struct alias *next; |
| 3057 | |
| 3058 | if (ap->flag & ALIASINUSE) { |
| 3059 | ap->flag |= ALIASDEAD; |
| 3060 | return ap; |
| 3061 | } |
| 3062 | |
| 3063 | next = ap->next; |
| 3064 | free(ap->name); |
| 3065 | free(ap->val); |
| 3066 | free(ap); |
| 3067 | return next; |
| 3068 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3069 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3070 | static void |
| 3071 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3072 | { |
| 3073 | struct alias *ap, **app; |
| 3074 | |
| 3075 | app = __lookupalias(name); |
| 3076 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3077 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3078 | if (ap) { |
| 3079 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3080 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3081 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3082 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3083 | ap->flag &= ~ALIASDEAD; |
| 3084 | } else { |
| 3085 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3086 | ap = ckzalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3087 | ap->name = ckstrdup(name); |
| 3088 | ap->val = ckstrdup(val); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3089 | /*ap->flag = 0; - ckzalloc did it */ |
| 3090 | /*ap->next = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3091 | *app = ap; |
| 3092 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3093 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3096 | static int |
| 3097 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3098 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3099 | struct alias **app; |
| 3100 | |
| 3101 | app = __lookupalias(name); |
| 3102 | |
| 3103 | if (*app) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3104 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3105 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3106 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3107 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3110 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3113 | static void |
| 3114 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3115 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3116 | struct alias *ap, **app; |
| 3117 | int i; |
| 3118 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3119 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3120 | for (i = 0; i < ATABSIZE; i++) { |
| 3121 | app = &atab[i]; |
| 3122 | for (ap = *app; ap; ap = *app) { |
| 3123 | *app = freealias(*app); |
| 3124 | if (ap == *app) { |
| 3125 | app = &ap->next; |
| 3126 | } |
| 3127 | } |
| 3128 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3129 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3132 | static void |
| 3133 | printalias(const struct alias *ap) |
| 3134 | { |
| 3135 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3136 | } |
| 3137 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3138 | /* |
| 3139 | * TODO - sort output |
| 3140 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3141 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3142 | aliascmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3143 | { |
| 3144 | char *n, *v; |
| 3145 | int ret = 0; |
| 3146 | struct alias *ap; |
| 3147 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3148 | if (!argv[1]) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3149 | int i; |
| 3150 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3151 | for (i = 0; i < ATABSIZE; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3152 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3153 | printalias(ap); |
| 3154 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3155 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3156 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3157 | } |
| 3158 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3159 | v = strchr(n+1, '='); |
| 3160 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3161 | ap = *__lookupalias(n); |
| 3162 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3163 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3164 | ret = 1; |
| 3165 | } else |
| 3166 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3167 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3168 | *v++ = '\0'; |
| 3169 | setalias(n, v); |
| 3170 | } |
| 3171 | } |
| 3172 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3173 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3176 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3177 | unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3178 | { |
| 3179 | int i; |
| 3180 | |
| 3181 | while ((i = nextopt("a")) != '\0') { |
| 3182 | if (i == 'a') { |
| 3183 | rmaliases(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3184 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3185 | } |
| 3186 | } |
| 3187 | for (i = 0; *argptr; argptr++) { |
| 3188 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3189 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3190 | i = 1; |
| 3191 | } |
| 3192 | } |
| 3193 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3194 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3195 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3196 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3197 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3198 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3199 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3200 | /* ============ jobs.c */ |
| 3201 | |
| 3202 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3203 | #define FORK_FG 0 |
| 3204 | #define FORK_BG 1 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3205 | #define FORK_NOJOB 2 |
| 3206 | |
| 3207 | /* mode flags for showjob(s) */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3208 | #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */ |
| 3209 | #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */ |
| 3210 | #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3211 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3212 | /* |
| 3213 | * A job structure contains information about a job. A job is either a |
| 3214 | * single process or a set of processes contained in a pipeline. In the |
| 3215 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 3216 | * array of pids. |
| 3217 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3218 | struct procstat { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3219 | pid_t ps_pid; /* process id */ |
| 3220 | int ps_status; /* last process status from wait() */ |
| 3221 | char *ps_cmd; /* text of command being run */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3222 | }; |
| 3223 | |
| 3224 | struct job { |
| 3225 | struct procstat ps0; /* status of process */ |
| 3226 | struct procstat *ps; /* status or processes when more than one */ |
| 3227 | #if JOBS |
| 3228 | int stopstatus; /* status of a stopped job */ |
| 3229 | #endif |
| 3230 | uint32_t |
| 3231 | nprocs: 16, /* number of processes */ |
| 3232 | state: 8, |
| 3233 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3234 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3235 | #define JOBDONE 2 /* all procs are completed */ |
| 3236 | #if JOBS |
| 3237 | sigint: 1, /* job was killed by SIGINT */ |
| 3238 | jobctl: 1, /* job running under job control */ |
| 3239 | #endif |
| 3240 | waited: 1, /* true if this entry has been waited for */ |
| 3241 | used: 1, /* true if this entry is in used */ |
| 3242 | changed: 1; /* true if status has changed */ |
| 3243 | struct job *prev_job; /* previous job */ |
| 3244 | }; |
| 3245 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3246 | static struct job *makejob(/*union node *,*/ int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3247 | static int forkshell(struct job *, union node *, int); |
| 3248 | static int waitforjob(struct job *); |
| 3249 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3250 | #if !JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3251 | enum { doing_jobctl = 0 }; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3252 | #define setjobctl(on) do {} while (0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3253 | #else |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3254 | static smallint doing_jobctl; //references:8 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3255 | static void setjobctl(int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3256 | #endif |
| 3257 | |
| 3258 | /* |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3259 | * Ignore a signal. |
| 3260 | */ |
| 3261 | static void |
| 3262 | ignoresig(int signo) |
| 3263 | { |
| 3264 | /* Avoid unnecessary system calls. Is it already SIG_IGNed? */ |
| 3265 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 3266 | /* No, need to do it */ |
| 3267 | signal(signo, SIG_IGN); |
| 3268 | } |
| 3269 | sigmode[signo - 1] = S_HARD_IGN; |
| 3270 | } |
| 3271 | |
| 3272 | /* |
| 3273 | * Signal handler. Only one usage site - in setsignal() |
| 3274 | */ |
| 3275 | static void |
| 3276 | onsig(int signo) |
| 3277 | { |
| 3278 | gotsig[signo - 1] = 1; |
| 3279 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3280 | if (signo == SIGINT && !trap[SIGINT]) { |
| 3281 | if (!suppress_int) { |
| 3282 | pending_sig = 0; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3283 | raise_interrupt(); /* does not return */ |
| 3284 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3285 | pending_int = 1; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3286 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3287 | pending_sig = signo; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | /* |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3292 | * Set the signal handler for the specified signal. The routine figures |
| 3293 | * out what it should be set to. |
| 3294 | */ |
| 3295 | static void |
| 3296 | setsignal(int signo) |
| 3297 | { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3298 | char *t; |
| 3299 | char cur_act, new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3300 | struct sigaction act; |
| 3301 | |
| 3302 | t = trap[signo]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3303 | new_act = S_DFL; |
| 3304 | if (t != NULL) { /* trap for this sig is set */ |
| 3305 | new_act = S_CATCH; |
| 3306 | if (t[0] == '\0') /* trap is "": ignore this sig */ |
| 3307 | new_act = S_IGN; |
| 3308 | } |
| 3309 | |
| 3310 | if (rootshell && new_act == S_DFL) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3311 | switch (signo) { |
| 3312 | case SIGINT: |
| 3313 | if (iflag || minusc || sflag == 0) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3314 | new_act = S_CATCH; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3315 | break; |
| 3316 | case SIGQUIT: |
| 3317 | #if DEBUG |
| 3318 | if (debug) |
| 3319 | break; |
| 3320 | #endif |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3321 | /* man bash: |
| 3322 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 3323 | * commands run by bash have signal handlers |
| 3324 | * set to the values inherited by the shell |
| 3325 | * from its parent". */ |
| 3326 | new_act = S_IGN; |
| 3327 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3328 | case SIGTERM: |
| 3329 | if (iflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3330 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3331 | break; |
| 3332 | #if JOBS |
| 3333 | case SIGTSTP: |
| 3334 | case SIGTTOU: |
| 3335 | if (mflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3336 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3337 | break; |
| 3338 | #endif |
| 3339 | } |
| 3340 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3341 | //TODO: if !rootshell, we reset SIGQUIT to DFL, |
| 3342 | //whereas we have to restore it to what shell got on entry |
| 3343 | //from the parent. See comment above |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3344 | |
| 3345 | t = &sigmode[signo - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3346 | cur_act = *t; |
| 3347 | if (cur_act == 0) { |
| 3348 | /* current setting is not yet known */ |
| 3349 | if (sigaction(signo, NULL, &act)) { |
| 3350 | /* pretend it worked; maybe we should give a warning, |
| 3351 | * but other shells don't. We don't alter sigmode, |
| 3352 | * so we retry every time. |
| 3353 | * btw, in Linux it never fails. --vda */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3354 | return; |
| 3355 | } |
| 3356 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3357 | cur_act = S_HARD_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3358 | if (mflag |
| 3359 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
| 3360 | ) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3361 | cur_act = S_IGN; /* don't hard ignore these */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3362 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3363 | } |
| 3364 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3365 | if (cur_act == S_HARD_IGN || cur_act == new_act) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3366 | return; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3367 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3368 | act.sa_handler = SIG_DFL; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3369 | switch (new_act) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3370 | case S_CATCH: |
| 3371 | act.sa_handler = onsig; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3372 | act.sa_flags = 0; /* matters only if !DFL and !IGN */ |
| 3373 | sigfillset(&act.sa_mask); /* ditto */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3374 | break; |
| 3375 | case S_IGN: |
| 3376 | act.sa_handler = SIG_IGN; |
| 3377 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3378 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3379 | sigaction_set(signo, &act); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3380 | |
| 3381 | *t = new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | /* mode flags for set_curjob */ |
| 3385 | #define CUR_DELETE 2 |
| 3386 | #define CUR_RUNNING 1 |
| 3387 | #define CUR_STOPPED 0 |
| 3388 | |
| 3389 | /* mode flags for dowait */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3390 | #define DOWAIT_NONBLOCK WNOHANG |
| 3391 | #define DOWAIT_BLOCK 0 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3392 | |
| 3393 | #if JOBS |
| 3394 | /* pgrp of shell on invocation */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3395 | static int initialpgrp; //references:2 |
| 3396 | static int ttyfd = -1; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3397 | #endif |
| 3398 | /* array of jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3399 | static struct job *jobtab; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3400 | /* size of array */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3401 | static unsigned njobs; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3402 | /* current job */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3403 | static struct job *curjob; //lots |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3404 | /* number of presumed living untracked jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3405 | static int jobless; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3406 | |
| 3407 | static void |
| 3408 | set_curjob(struct job *jp, unsigned mode) |
| 3409 | { |
| 3410 | struct job *jp1; |
| 3411 | struct job **jpp, **curp; |
| 3412 | |
| 3413 | /* first remove from list */ |
| 3414 | jpp = curp = &curjob; |
| 3415 | do { |
| 3416 | jp1 = *jpp; |
| 3417 | if (jp1 == jp) |
| 3418 | break; |
| 3419 | jpp = &jp1->prev_job; |
| 3420 | } while (1); |
| 3421 | *jpp = jp1->prev_job; |
| 3422 | |
| 3423 | /* Then re-insert in correct position */ |
| 3424 | jpp = curp; |
| 3425 | switch (mode) { |
| 3426 | default: |
| 3427 | #if DEBUG |
| 3428 | abort(); |
| 3429 | #endif |
| 3430 | case CUR_DELETE: |
| 3431 | /* job being deleted */ |
| 3432 | break; |
| 3433 | case CUR_RUNNING: |
| 3434 | /* newly created job or backgrounded job, |
| 3435 | put after all stopped jobs. */ |
| 3436 | do { |
| 3437 | jp1 = *jpp; |
| 3438 | #if JOBS |
| 3439 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3440 | #endif |
| 3441 | break; |
| 3442 | jpp = &jp1->prev_job; |
| 3443 | } while (1); |
| 3444 | /* FALLTHROUGH */ |
| 3445 | #if JOBS |
| 3446 | case CUR_STOPPED: |
| 3447 | #endif |
| 3448 | /* newly stopped job - becomes curjob */ |
| 3449 | jp->prev_job = *jpp; |
| 3450 | *jpp = jp; |
| 3451 | break; |
| 3452 | } |
| 3453 | } |
| 3454 | |
| 3455 | #if JOBS || DEBUG |
| 3456 | static int |
| 3457 | jobno(const struct job *jp) |
| 3458 | { |
| 3459 | return jp - jobtab + 1; |
| 3460 | } |
| 3461 | #endif |
| 3462 | |
| 3463 | /* |
| 3464 | * Convert a job name to a job structure. |
| 3465 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3466 | #if !JOBS |
| 3467 | #define getjob(name, getctl) getjob(name) |
| 3468 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3469 | static struct job * |
| 3470 | getjob(const char *name, int getctl) |
| 3471 | { |
| 3472 | struct job *jp; |
| 3473 | struct job *found; |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3474 | const char *err_msg = "%s: no such job"; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3475 | unsigned num; |
| 3476 | int c; |
| 3477 | const char *p; |
| 3478 | char *(*match)(const char *, const char *); |
| 3479 | |
| 3480 | jp = curjob; |
| 3481 | p = name; |
| 3482 | if (!p) |
| 3483 | goto currentjob; |
| 3484 | |
| 3485 | if (*p != '%') |
| 3486 | goto err; |
| 3487 | |
| 3488 | c = *++p; |
| 3489 | if (!c) |
| 3490 | goto currentjob; |
| 3491 | |
| 3492 | if (!p[1]) { |
| 3493 | if (c == '+' || c == '%') { |
| 3494 | currentjob: |
| 3495 | err_msg = "No current job"; |
| 3496 | goto check; |
| 3497 | } |
| 3498 | if (c == '-') { |
| 3499 | if (jp) |
| 3500 | jp = jp->prev_job; |
| 3501 | err_msg = "No previous job"; |
| 3502 | check: |
| 3503 | if (!jp) |
| 3504 | goto err; |
| 3505 | goto gotit; |
| 3506 | } |
| 3507 | } |
| 3508 | |
| 3509 | if (is_number(p)) { |
| 3510 | num = atoi(p); |
| 3511 | if (num < njobs) { |
| 3512 | jp = jobtab + num - 1; |
| 3513 | if (jp->used) |
| 3514 | goto gotit; |
| 3515 | goto err; |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | match = prefix; |
| 3520 | if (*p == '?') { |
| 3521 | match = strstr; |
| 3522 | p++; |
| 3523 | } |
| 3524 | |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3525 | found = NULL; |
| 3526 | while (jp) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3527 | if (match(jp->ps[0].ps_cmd, p)) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3528 | if (found) |
| 3529 | goto err; |
| 3530 | found = jp; |
| 3531 | err_msg = "%s: ambiguous"; |
| 3532 | } |
| 3533 | jp = jp->prev_job; |
| 3534 | } |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3535 | if (!found) |
| 3536 | goto err; |
| 3537 | jp = found; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3538 | |
| 3539 | gotit: |
| 3540 | #if JOBS |
| 3541 | err_msg = "job %s not created under job control"; |
| 3542 | if (getctl && jp->jobctl == 0) |
| 3543 | goto err; |
| 3544 | #endif |
| 3545 | return jp; |
| 3546 | err: |
| 3547 | ash_msg_and_raise_error(err_msg, name); |
| 3548 | } |
| 3549 | |
| 3550 | /* |
| 3551 | * Mark a job structure as unused. |
| 3552 | */ |
| 3553 | static void |
| 3554 | freejob(struct job *jp) |
| 3555 | { |
| 3556 | struct procstat *ps; |
| 3557 | int i; |
| 3558 | |
| 3559 | INT_OFF; |
| 3560 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3561 | if (ps->ps_cmd != nullstr) |
| 3562 | free(ps->ps_cmd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3563 | } |
| 3564 | if (jp->ps != &jp->ps0) |
| 3565 | free(jp->ps); |
| 3566 | jp->used = 0; |
| 3567 | set_curjob(jp, CUR_DELETE); |
| 3568 | INT_ON; |
| 3569 | } |
| 3570 | |
| 3571 | #if JOBS |
| 3572 | static void |
| 3573 | xtcsetpgrp(int fd, pid_t pgrp) |
| 3574 | { |
| 3575 | if (tcsetpgrp(fd, pgrp)) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 3576 | ash_msg_and_raise_error("can't set tty process group (%m)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | /* |
| 3580 | * Turn job control on and off. |
| 3581 | * |
| 3582 | * Note: This code assumes that the third arg to ioctl is a character |
| 3583 | * pointer, which is true on Berkeley systems but not System V. Since |
| 3584 | * System V doesn't have job control yet, this isn't a problem now. |
| 3585 | * |
| 3586 | * Called with interrupts off. |
| 3587 | */ |
| 3588 | static void |
| 3589 | setjobctl(int on) |
| 3590 | { |
| 3591 | int fd; |
| 3592 | int pgrp; |
| 3593 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3594 | if (on == doing_jobctl || rootshell == 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3595 | return; |
| 3596 | if (on) { |
| 3597 | int ofd; |
| 3598 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 3599 | if (fd < 0) { |
| 3600 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 3601 | * That sometimes helps to acquire controlling tty. |
| 3602 | * Obviously, a workaround for bugs when someone |
| 3603 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3604 | fd = 2; |
| 3605 | while (!isatty(fd)) |
| 3606 | if (--fd < 0) |
| 3607 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3608 | } |
| 3609 | fd = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3610 | if (ofd >= 0) |
| 3611 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3612 | if (fd < 0) |
| 3613 | goto out; |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3614 | /* fd is a tty at this point */ |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 3615 | close_on_exec_on(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3616 | do { /* while we are in the background */ |
| 3617 | pgrp = tcgetpgrp(fd); |
| 3618 | if (pgrp < 0) { |
| 3619 | out: |
| 3620 | ash_msg("can't access tty; job control turned off"); |
| 3621 | mflag = on = 0; |
| 3622 | goto close; |
| 3623 | } |
| 3624 | if (pgrp == getpgrp()) |
| 3625 | break; |
| 3626 | killpg(0, SIGTTIN); |
| 3627 | } while (1); |
| 3628 | initialpgrp = pgrp; |
| 3629 | |
| 3630 | setsignal(SIGTSTP); |
| 3631 | setsignal(SIGTTOU); |
| 3632 | setsignal(SIGTTIN); |
| 3633 | pgrp = rootpid; |
| 3634 | setpgid(0, pgrp); |
| 3635 | xtcsetpgrp(fd, pgrp); |
| 3636 | } else { |
| 3637 | /* turning job control off */ |
| 3638 | fd = ttyfd; |
| 3639 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3640 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3641 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3642 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3643 | setpgid(0, pgrp); |
| 3644 | setsignal(SIGTSTP); |
| 3645 | setsignal(SIGTTOU); |
| 3646 | setsignal(SIGTTIN); |
| 3647 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3648 | if (fd >= 0) |
| 3649 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3650 | fd = -1; |
| 3651 | } |
| 3652 | ttyfd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3653 | doing_jobctl = on; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3656 | static int FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3657 | killcmd(int argc, char **argv) |
| 3658 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3659 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3660 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3661 | do { |
| 3662 | if (argv[i][0] == '%') { |
| 3663 | struct job *jp = getjob(argv[i], 0); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3664 | unsigned pid = jp->ps[0].ps_pid; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3665 | /* Enough space for ' -NNN<nul>' */ |
| 3666 | argv[i] = alloca(sizeof(int)*3 + 3); |
| 3667 | /* kill_main has matching code to expect |
| 3668 | * leading space. Needed to not confuse |
| 3669 | * negative pids with "kill -SIGNAL_NO" syntax */ |
| 3670 | sprintf(argv[i], " -%u", pid); |
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 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3673 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3674 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | static void |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3678 | showpipe(struct job *jp /*, FILE *out*/) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3679 | { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3680 | struct procstat *ps; |
| 3681 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3682 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3683 | psend = jp->ps + jp->nprocs; |
| 3684 | for (ps = jp->ps + 1; ps < psend; ps++) |
| 3685 | printf(" | %s", ps->ps_cmd); |
| 3686 | outcslow('\n', stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3687 | flush_stdout_stderr(); |
| 3688 | } |
| 3689 | |
| 3690 | |
| 3691 | static int |
| 3692 | restartjob(struct job *jp, int mode) |
| 3693 | { |
| 3694 | struct procstat *ps; |
| 3695 | int i; |
| 3696 | int status; |
| 3697 | pid_t pgid; |
| 3698 | |
| 3699 | INT_OFF; |
| 3700 | if (jp->state == JOBDONE) |
| 3701 | goto out; |
| 3702 | jp->state = JOBRUNNING; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3703 | pgid = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3704 | if (mode == FORK_FG) |
| 3705 | xtcsetpgrp(ttyfd, pgid); |
| 3706 | killpg(pgid, SIGCONT); |
| 3707 | ps = jp->ps; |
| 3708 | i = jp->nprocs; |
| 3709 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3710 | if (WIFSTOPPED(ps->ps_status)) { |
| 3711 | ps->ps_status = -1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3712 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3713 | ps++; |
| 3714 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3715 | out: |
| 3716 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 3717 | INT_ON; |
| 3718 | return status; |
| 3719 | } |
| 3720 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3721 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3722 | fg_bgcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3723 | { |
| 3724 | struct job *jp; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3725 | int mode; |
| 3726 | int retval; |
| 3727 | |
| 3728 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 3729 | nextopt(nullstr); |
| 3730 | argv = argptr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3731 | do { |
| 3732 | jp = getjob(*argv, 1); |
| 3733 | if (mode == FORK_BG) { |
| 3734 | set_curjob(jp, CUR_RUNNING); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3735 | printf("[%d] ", jobno(jp)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3736 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3737 | out1str(jp->ps[0].ps_cmd); |
| 3738 | showpipe(jp /*, stdout*/); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3739 | retval = restartjob(jp, mode); |
| 3740 | } while (*argv && *++argv); |
| 3741 | return retval; |
| 3742 | } |
| 3743 | #endif |
| 3744 | |
| 3745 | static int |
| 3746 | sprint_status(char *s, int status, int sigonly) |
| 3747 | { |
| 3748 | int col; |
| 3749 | int st; |
| 3750 | |
| 3751 | col = 0; |
| 3752 | if (!WIFEXITED(status)) { |
| 3753 | #if JOBS |
| 3754 | if (WIFSTOPPED(status)) |
| 3755 | st = WSTOPSIG(status); |
| 3756 | else |
| 3757 | #endif |
| 3758 | st = WTERMSIG(status); |
| 3759 | if (sigonly) { |
| 3760 | if (st == SIGINT || st == SIGPIPE) |
| 3761 | goto out; |
| 3762 | #if JOBS |
| 3763 | if (WIFSTOPPED(status)) |
| 3764 | goto out; |
| 3765 | #endif |
| 3766 | } |
| 3767 | st &= 0x7f; |
| 3768 | col = fmtstr(s, 32, strsignal(st)); |
| 3769 | if (WCOREDUMP(status)) { |
| 3770 | col += fmtstr(s + col, 16, " (core dumped)"); |
| 3771 | } |
| 3772 | } else if (!sigonly) { |
| 3773 | st = WEXITSTATUS(status); |
| 3774 | if (st) |
| 3775 | col = fmtstr(s, 16, "Done(%d)", st); |
| 3776 | else |
| 3777 | col = fmtstr(s, 16, "Done"); |
| 3778 | } |
| 3779 | out: |
| 3780 | return col; |
| 3781 | } |
| 3782 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3783 | static int |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3784 | dowait(int wait_flags, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3785 | { |
| 3786 | int pid; |
| 3787 | int status; |
| 3788 | struct job *jp; |
| 3789 | struct job *thisjob; |
| 3790 | int state; |
| 3791 | |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 3792 | TRACE(("dowait(0x%x) called\n", wait_flags)); |
| 3793 | |
| 3794 | /* Do a wait system call. If job control is compiled in, we accept |
| 3795 | * stopped processes. wait_flags may have WNOHANG, preventing blocking. |
| 3796 | * NB: _not_ safe_waitpid, we need to detect EINTR */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3797 | if (doing_jobctl) |
| 3798 | wait_flags |= WUNTRACED; |
| 3799 | pid = waitpid(-1, &status, wait_flags); |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 3800 | TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n", |
| 3801 | pid, status, errno, strerror(errno))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3802 | if (pid <= 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3803 | return pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3804 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3805 | INT_OFF; |
| 3806 | thisjob = NULL; |
| 3807 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3808 | struct procstat *ps; |
| 3809 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3810 | if (jp->state == JOBDONE) |
| 3811 | continue; |
| 3812 | state = JOBDONE; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3813 | ps = jp->ps; |
| 3814 | psend = ps + jp->nprocs; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3815 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3816 | if (ps->ps_pid == pid) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3817 | TRACE(("Job %d: changing status of proc %d " |
| 3818 | "from 0x%x to 0x%x\n", |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3819 | jobno(jp), pid, ps->ps_status, status)); |
| 3820 | ps->ps_status = status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3821 | thisjob = jp; |
| 3822 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3823 | if (ps->ps_status == -1) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3824 | state = JOBRUNNING; |
| 3825 | #if JOBS |
| 3826 | if (state == JOBRUNNING) |
| 3827 | continue; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3828 | if (WIFSTOPPED(ps->ps_status)) { |
| 3829 | jp->stopstatus = ps->ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3830 | state = JOBSTOPPED; |
| 3831 | } |
| 3832 | #endif |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3833 | } while (++ps < psend); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3834 | if (thisjob) |
| 3835 | goto gotjob; |
| 3836 | } |
| 3837 | #if JOBS |
| 3838 | if (!WIFSTOPPED(status)) |
| 3839 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3840 | jobless--; |
| 3841 | goto out; |
| 3842 | |
| 3843 | gotjob: |
| 3844 | if (state != JOBRUNNING) { |
| 3845 | thisjob->changed = 1; |
| 3846 | |
| 3847 | if (thisjob->state != state) { |
| 3848 | TRACE(("Job %d: changing state from %d to %d\n", |
| 3849 | jobno(thisjob), thisjob->state, state)); |
| 3850 | thisjob->state = state; |
| 3851 | #if JOBS |
| 3852 | if (state == JOBSTOPPED) { |
| 3853 | set_curjob(thisjob, CUR_STOPPED); |
| 3854 | } |
| 3855 | #endif |
| 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | out: |
| 3860 | INT_ON; |
| 3861 | |
| 3862 | if (thisjob && thisjob == job) { |
| 3863 | char s[48 + 1]; |
| 3864 | int len; |
| 3865 | |
| 3866 | len = sprint_status(s, status, 1); |
| 3867 | if (len) { |
| 3868 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3869 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3870 | out2str(s); |
| 3871 | } |
| 3872 | } |
| 3873 | return pid; |
| 3874 | } |
| 3875 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3876 | static int |
| 3877 | blocking_wait_with_raise_on_sig(struct job *job) |
| 3878 | { |
| 3879 | pid_t pid = dowait(DOWAIT_BLOCK, job); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3880 | if (pid <= 0 && pending_sig) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3881 | raise_exception(EXSIG); |
| 3882 | return pid; |
| 3883 | } |
| 3884 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3885 | #if JOBS |
| 3886 | static void |
| 3887 | showjob(FILE *out, struct job *jp, int mode) |
| 3888 | { |
| 3889 | struct procstat *ps; |
| 3890 | struct procstat *psend; |
| 3891 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3892 | int indent_col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3893 | char s[80]; |
| 3894 | |
| 3895 | ps = jp->ps; |
| 3896 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3897 | if (mode & SHOW_ONLY_PGID) { /* jobs -p */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3898 | /* just output process (group) id of pipeline */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3899 | fprintf(out, "%d\n", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3900 | return; |
| 3901 | } |
| 3902 | |
| 3903 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3904 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3905 | |
| 3906 | if (jp == curjob) |
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 | else if (curjob && jp == curjob->prev_job) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3909 | s[col - 3] = '-'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3910 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3911 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3912 | col += fmtstr(s + col, 16, "%d ", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3913 | |
| 3914 | psend = ps + jp->nprocs; |
| 3915 | |
| 3916 | if (jp->state == JOBRUNNING) { |
| 3917 | strcpy(s + col, "Running"); |
| 3918 | col += sizeof("Running") - 1; |
| 3919 | } else { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3920 | int status = psend[-1].ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3921 | if (jp->state == JOBSTOPPED) |
| 3922 | status = jp->stopstatus; |
| 3923 | col += sprint_status(s + col, status, 0); |
| 3924 | } |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3925 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3926 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3927 | /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line |
| 3928 | * or prints several "PID | <cmdN>" lines, |
| 3929 | * depending on SHOW_PIDS bit. |
| 3930 | * We do not print status of individual processes |
| 3931 | * between PID and <cmdN>. bash does it, but not very well: |
| 3932 | * first line shows overall job status, not process status, |
| 3933 | * making it impossible to know 1st process status. |
| 3934 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3935 | goto start; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3936 | do { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3937 | /* for each process */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3938 | s[0] = '\0'; |
| 3939 | col = 33; |
| 3940 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3941 | 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] | 3942 | start: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3943 | fprintf(out, "%s%*c%s%s", |
| 3944 | s, |
| 3945 | 33 - col >= 0 ? 33 - col : 0, ' ', |
| 3946 | ps == jp->ps ? "" : "| ", |
| 3947 | ps->ps_cmd |
| 3948 | ); |
| 3949 | } while (++ps != psend); |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3950 | outcslow('\n', out); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3951 | |
| 3952 | jp->changed = 0; |
| 3953 | |
| 3954 | if (jp->state == JOBDONE) { |
| 3955 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 3956 | freejob(jp); |
| 3957 | } |
| 3958 | } |
| 3959 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3960 | /* |
| 3961 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 3962 | * statuses have changed since the last call to showjobs. |
| 3963 | */ |
| 3964 | static void |
| 3965 | showjobs(FILE *out, int mode) |
| 3966 | { |
| 3967 | struct job *jp; |
| 3968 | |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 3969 | TRACE(("showjobs(0x%x) called\n", mode)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3970 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3971 | /* Handle all finished jobs */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3972 | while (dowait(DOWAIT_NONBLOCK, NULL) > 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3973 | continue; |
| 3974 | |
| 3975 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3976 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3977 | showjob(out, jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3978 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3979 | } |
| 3980 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3981 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3982 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3983 | jobscmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3984 | { |
| 3985 | int mode, m; |
| 3986 | |
| 3987 | mode = 0; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3988 | while ((m = nextopt("lp")) != '\0') { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3989 | if (m == 'l') |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3990 | mode |= SHOW_PIDS; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3991 | else |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3992 | mode |= SHOW_ONLY_PGID; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
| 3995 | argv = argptr; |
| 3996 | if (*argv) { |
| 3997 | do |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3998 | showjob(stdout, getjob(*argv, 0), mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3999 | while (*++argv); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4000 | } else { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4001 | showjobs(stdout, mode); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4002 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4003 | |
| 4004 | return 0; |
| 4005 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4006 | #endif /* JOBS */ |
| 4007 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4008 | /* Called only on finished or stopped jobs (no members are running) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4009 | static int |
| 4010 | getstatus(struct job *job) |
| 4011 | { |
| 4012 | int status; |
| 4013 | int retval; |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4014 | struct procstat *ps; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4015 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4016 | /* Fetch last member's status */ |
| 4017 | ps = job->ps + job->nprocs - 1; |
| 4018 | status = ps->ps_status; |
| 4019 | if (pipefail) { |
| 4020 | /* "set -o pipefail" mode: use last _nonzero_ status */ |
| 4021 | while (status == 0 && --ps >= job->ps) |
| 4022 | status = ps->ps_status; |
| 4023 | } |
| 4024 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4025 | retval = WEXITSTATUS(status); |
| 4026 | if (!WIFEXITED(status)) { |
| 4027 | #if JOBS |
| 4028 | retval = WSTOPSIG(status); |
| 4029 | if (!WIFSTOPPED(status)) |
| 4030 | #endif |
| 4031 | { |
| 4032 | /* XXX: limits number of signals */ |
| 4033 | retval = WTERMSIG(status); |
| 4034 | #if JOBS |
| 4035 | if (retval == SIGINT) |
| 4036 | job->sigint = 1; |
| 4037 | #endif |
| 4038 | } |
| 4039 | retval += 128; |
| 4040 | } |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4041 | 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] | 4042 | jobno(job), job->nprocs, status, retval)); |
| 4043 | return retval; |
| 4044 | } |
| 4045 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4046 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4047 | waitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4048 | { |
| 4049 | struct job *job; |
| 4050 | int retval; |
| 4051 | struct job *jp; |
| 4052 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4053 | if (pending_sig) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4054 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4055 | |
| 4056 | nextopt(nullstr); |
| 4057 | retval = 0; |
| 4058 | |
| 4059 | argv = argptr; |
| 4060 | if (!*argv) { |
| 4061 | /* wait for all jobs */ |
| 4062 | for (;;) { |
| 4063 | jp = curjob; |
| 4064 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4065 | if (!jp) /* no running procs */ |
| 4066 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4067 | if (jp->state == JOBRUNNING) |
| 4068 | break; |
| 4069 | jp->waited = 1; |
| 4070 | jp = jp->prev_job; |
| 4071 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4072 | /* man bash: |
| 4073 | * "When bash is waiting for an asynchronous command via |
| 4074 | * the wait builtin, the reception of a signal for which a trap |
| 4075 | * has been set will cause the wait builtin to return immediately |
| 4076 | * with an exit status greater than 128, immediately after which |
| 4077 | * the trap is executed." |
| 4078 | * Do we do it that way? */ |
| 4079 | blocking_wait_with_raise_on_sig(NULL); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | retval = 127; |
| 4084 | do { |
| 4085 | if (**argv != '%') { |
| 4086 | pid_t pid = number(*argv); |
| 4087 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4088 | while (1) { |
| 4089 | if (!job) |
| 4090 | goto repeat; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4091 | if (job->ps[job->nprocs - 1].ps_pid == pid) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4092 | break; |
| 4093 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4094 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4095 | } else |
| 4096 | job = getjob(*argv, 0); |
| 4097 | /* loop until process terminated or stopped */ |
| 4098 | while (job->state == JOBRUNNING) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4099 | blocking_wait_with_raise_on_sig(NULL); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4100 | job->waited = 1; |
| 4101 | retval = getstatus(job); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4102 | repeat: ; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4103 | } while (*++argv); |
| 4104 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4105 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4106 | return retval; |
| 4107 | } |
| 4108 | |
| 4109 | static struct job * |
| 4110 | growjobtab(void) |
| 4111 | { |
| 4112 | size_t len; |
| 4113 | ptrdiff_t offset; |
| 4114 | struct job *jp, *jq; |
| 4115 | |
| 4116 | len = njobs * sizeof(*jp); |
| 4117 | jq = jobtab; |
| 4118 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4119 | |
| 4120 | offset = (char *)jp - (char *)jq; |
| 4121 | if (offset) { |
| 4122 | /* Relocate pointers */ |
| 4123 | size_t l = len; |
| 4124 | |
| 4125 | jq = (struct job *)((char *)jq + l); |
| 4126 | while (l) { |
| 4127 | l -= sizeof(*jp); |
| 4128 | jq--; |
| 4129 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4130 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4131 | if (joff(jp)->ps == &jq->ps0) |
| 4132 | jmove(joff(jp)->ps); |
| 4133 | if (joff(jp)->prev_job) |
| 4134 | jmove(joff(jp)->prev_job); |
| 4135 | } |
| 4136 | if (curjob) |
| 4137 | jmove(curjob); |
| 4138 | #undef joff |
| 4139 | #undef jmove |
| 4140 | } |
| 4141 | |
| 4142 | njobs += 4; |
| 4143 | jobtab = jp; |
| 4144 | jp = (struct job *)((char *)jp + len); |
| 4145 | jq = jp + 3; |
| 4146 | do { |
| 4147 | jq->used = 0; |
| 4148 | } while (--jq >= jp); |
| 4149 | return jp; |
| 4150 | } |
| 4151 | |
| 4152 | /* |
| 4153 | * Return a new job structure. |
| 4154 | * Called with interrupts off. |
| 4155 | */ |
| 4156 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4157 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4158 | { |
| 4159 | int i; |
| 4160 | struct job *jp; |
| 4161 | |
| 4162 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4163 | if (--i < 0) { |
| 4164 | jp = growjobtab(); |
| 4165 | break; |
| 4166 | } |
| 4167 | if (jp->used == 0) |
| 4168 | break; |
| 4169 | if (jp->state != JOBDONE || !jp->waited) |
| 4170 | continue; |
| 4171 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4172 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4173 | continue; |
| 4174 | #endif |
| 4175 | freejob(jp); |
| 4176 | break; |
| 4177 | } |
| 4178 | memset(jp, 0, sizeof(*jp)); |
| 4179 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4180 | /* jp->jobctl is a bitfield. |
| 4181 | * "jp->jobctl |= jobctl" likely to give awful code */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4182 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4183 | jp->jobctl = 1; |
| 4184 | #endif |
| 4185 | jp->prev_job = curjob; |
| 4186 | curjob = jp; |
| 4187 | jp->used = 1; |
| 4188 | jp->ps = &jp->ps0; |
| 4189 | if (nprocs > 1) { |
| 4190 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4191 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4192 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4193 | jobno(jp))); |
| 4194 | return jp; |
| 4195 | } |
| 4196 | |
| 4197 | #if JOBS |
| 4198 | /* |
| 4199 | * Return a string identifying a command (to be printed by the |
| 4200 | * jobs command). |
| 4201 | */ |
| 4202 | static char *cmdnextc; |
| 4203 | |
| 4204 | static void |
| 4205 | cmdputs(const char *s) |
| 4206 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4207 | static const char vstype[VSTYPE + 1][3] = { |
| 4208 | "", "}", "-", "+", "?", "=", |
| 4209 | "%", "%%", "#", "##" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4210 | IF_ASH_BASH_COMPAT(, ":", "/", "//") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4211 | }; |
| 4212 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4213 | const char *p, *str; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4214 | char cc[2]; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4215 | char *nextc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 4216 | unsigned char c; |
| 4217 | unsigned char subtype = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4218 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4219 | |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4220 | cc[1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4221 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4222 | p = s; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4223 | while ((c = *p++) != '\0') { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 4224 | str = NULL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4225 | switch (c) { |
| 4226 | case CTLESC: |
| 4227 | c = *p++; |
| 4228 | break; |
| 4229 | case CTLVAR: |
| 4230 | subtype = *p++; |
| 4231 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4232 | str = "${#"; |
| 4233 | else |
| 4234 | str = "${"; |
| 4235 | if (!(subtype & VSQUOTE) == !(quoted & 1)) |
| 4236 | goto dostr; |
| 4237 | quoted ^= 1; |
| 4238 | c = '"'; |
| 4239 | break; |
| 4240 | case CTLENDVAR: |
| 4241 | str = "\"}" + !(quoted & 1); |
| 4242 | quoted >>= 1; |
| 4243 | subtype = 0; |
| 4244 | goto dostr; |
| 4245 | case CTLBACKQ: |
| 4246 | str = "$(...)"; |
| 4247 | goto dostr; |
| 4248 | case CTLBACKQ+CTLQUOTE: |
| 4249 | str = "\"$(...)\""; |
| 4250 | goto dostr; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4251 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4252 | case CTLARI: |
| 4253 | str = "$(("; |
| 4254 | goto dostr; |
| 4255 | case CTLENDARI: |
| 4256 | str = "))"; |
| 4257 | goto dostr; |
| 4258 | #endif |
| 4259 | case CTLQUOTEMARK: |
| 4260 | quoted ^= 1; |
| 4261 | c = '"'; |
| 4262 | break; |
| 4263 | case '=': |
| 4264 | if (subtype == 0) |
| 4265 | break; |
| 4266 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4267 | quoted <<= 1; |
| 4268 | str = vstype[subtype & VSTYPE]; |
| 4269 | if (subtype & VSNUL) |
| 4270 | c = ':'; |
| 4271 | else |
| 4272 | goto checkstr; |
| 4273 | break; |
| 4274 | case '\'': |
| 4275 | case '\\': |
| 4276 | case '"': |
| 4277 | case '$': |
| 4278 | /* These can only happen inside quotes */ |
| 4279 | cc[0] = c; |
| 4280 | str = cc; |
| 4281 | c = '\\'; |
| 4282 | break; |
| 4283 | default: |
| 4284 | break; |
| 4285 | } |
| 4286 | USTPUTC(c, nextc); |
| 4287 | checkstr: |
| 4288 | if (!str) |
| 4289 | continue; |
| 4290 | dostr: |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4291 | while ((c = *str++) != '\0') { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4292 | USTPUTC(c, nextc); |
| 4293 | } |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4294 | } /* while *p++ not NUL */ |
| 4295 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4296 | if (quoted & 1) { |
| 4297 | USTPUTC('"', nextc); |
| 4298 | } |
| 4299 | *nextc = 0; |
| 4300 | cmdnextc = nextc; |
| 4301 | } |
| 4302 | |
| 4303 | /* cmdtxt() and cmdlist() call each other */ |
| 4304 | static void cmdtxt(union node *n); |
| 4305 | |
| 4306 | static void |
| 4307 | cmdlist(union node *np, int sep) |
| 4308 | { |
| 4309 | for (; np; np = np->narg.next) { |
| 4310 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4311 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4312 | cmdtxt(np); |
| 4313 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4314 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4315 | } |
| 4316 | } |
| 4317 | |
| 4318 | static void |
| 4319 | cmdtxt(union node *n) |
| 4320 | { |
| 4321 | union node *np; |
| 4322 | struct nodelist *lp; |
| 4323 | const char *p; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4324 | |
| 4325 | if (!n) |
| 4326 | return; |
| 4327 | switch (n->type) { |
| 4328 | default: |
| 4329 | #if DEBUG |
| 4330 | abort(); |
| 4331 | #endif |
| 4332 | case NPIPE: |
| 4333 | lp = n->npipe.cmdlist; |
| 4334 | for (;;) { |
| 4335 | cmdtxt(lp->n); |
| 4336 | lp = lp->next; |
| 4337 | if (!lp) |
| 4338 | break; |
| 4339 | cmdputs(" | "); |
| 4340 | } |
| 4341 | break; |
| 4342 | case NSEMI: |
| 4343 | p = "; "; |
| 4344 | goto binop; |
| 4345 | case NAND: |
| 4346 | p = " && "; |
| 4347 | goto binop; |
| 4348 | case NOR: |
| 4349 | p = " || "; |
| 4350 | binop: |
| 4351 | cmdtxt(n->nbinary.ch1); |
| 4352 | cmdputs(p); |
| 4353 | n = n->nbinary.ch2; |
| 4354 | goto donode; |
| 4355 | case NREDIR: |
| 4356 | case NBACKGND: |
| 4357 | n = n->nredir.n; |
| 4358 | goto donode; |
| 4359 | case NNOT: |
| 4360 | cmdputs("!"); |
| 4361 | n = n->nnot.com; |
| 4362 | donode: |
| 4363 | cmdtxt(n); |
| 4364 | break; |
| 4365 | case NIF: |
| 4366 | cmdputs("if "); |
| 4367 | cmdtxt(n->nif.test); |
| 4368 | cmdputs("; then "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4369 | if (n->nif.elsepart) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4370 | cmdtxt(n->nif.ifpart); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4371 | cmdputs("; else "); |
| 4372 | n = n->nif.elsepart; |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4373 | } else { |
| 4374 | n = n->nif.ifpart; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4375 | } |
| 4376 | p = "; fi"; |
| 4377 | goto dotail; |
| 4378 | case NSUBSHELL: |
| 4379 | cmdputs("("); |
| 4380 | n = n->nredir.n; |
| 4381 | p = ")"; |
| 4382 | goto dotail; |
| 4383 | case NWHILE: |
| 4384 | p = "while "; |
| 4385 | goto until; |
| 4386 | case NUNTIL: |
| 4387 | p = "until "; |
| 4388 | until: |
| 4389 | cmdputs(p); |
| 4390 | cmdtxt(n->nbinary.ch1); |
| 4391 | n = n->nbinary.ch2; |
| 4392 | p = "; done"; |
| 4393 | dodo: |
| 4394 | cmdputs("; do "); |
| 4395 | dotail: |
| 4396 | cmdtxt(n); |
| 4397 | goto dotail2; |
| 4398 | case NFOR: |
| 4399 | cmdputs("for "); |
| 4400 | cmdputs(n->nfor.var); |
| 4401 | cmdputs(" in "); |
| 4402 | cmdlist(n->nfor.args, 1); |
| 4403 | n = n->nfor.body; |
| 4404 | p = "; done"; |
| 4405 | goto dodo; |
| 4406 | case NDEFUN: |
| 4407 | cmdputs(n->narg.text); |
| 4408 | p = "() { ... }"; |
| 4409 | goto dotail2; |
| 4410 | case NCMD: |
| 4411 | cmdlist(n->ncmd.args, 1); |
| 4412 | cmdlist(n->ncmd.redirect, 0); |
| 4413 | break; |
| 4414 | case NARG: |
| 4415 | p = n->narg.text; |
| 4416 | dotail2: |
| 4417 | cmdputs(p); |
| 4418 | break; |
| 4419 | case NHERE: |
| 4420 | case NXHERE: |
| 4421 | p = "<<..."; |
| 4422 | goto dotail2; |
| 4423 | case NCASE: |
| 4424 | cmdputs("case "); |
| 4425 | cmdputs(n->ncase.expr->narg.text); |
| 4426 | cmdputs(" in "); |
| 4427 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 4428 | cmdtxt(np->nclist.pattern); |
| 4429 | cmdputs(") "); |
| 4430 | cmdtxt(np->nclist.body); |
| 4431 | cmdputs(";; "); |
| 4432 | } |
| 4433 | p = "esac"; |
| 4434 | goto dotail2; |
| 4435 | case NTO: |
| 4436 | p = ">"; |
| 4437 | goto redir; |
| 4438 | case NCLOBBER: |
| 4439 | p = ">|"; |
| 4440 | goto redir; |
| 4441 | case NAPPEND: |
| 4442 | p = ">>"; |
| 4443 | goto redir; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4444 | #if ENABLE_ASH_BASH_COMPAT |
| 4445 | case NTO2: |
| 4446 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4447 | case NTOFD: |
| 4448 | p = ">&"; |
| 4449 | goto redir; |
| 4450 | case NFROM: |
| 4451 | p = "<"; |
| 4452 | goto redir; |
| 4453 | case NFROMFD: |
| 4454 | p = "<&"; |
| 4455 | goto redir; |
| 4456 | case NFROMTO: |
| 4457 | p = "<>"; |
| 4458 | redir: |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4459 | cmdputs(utoa(n->nfile.fd)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4460 | cmdputs(p); |
| 4461 | if (n->type == NTOFD || n->type == NFROMFD) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4462 | cmdputs(utoa(n->ndup.dupfd)); |
| 4463 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4464 | } |
| 4465 | n = n->nfile.fname; |
| 4466 | goto donode; |
| 4467 | } |
| 4468 | } |
| 4469 | |
| 4470 | static char * |
| 4471 | commandtext(union node *n) |
| 4472 | { |
| 4473 | char *name; |
| 4474 | |
| 4475 | STARTSTACKSTR(cmdnextc); |
| 4476 | cmdtxt(n); |
| 4477 | name = stackblock(); |
| 4478 | TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n", |
| 4479 | name, cmdnextc, cmdnextc)); |
| 4480 | return ckstrdup(name); |
| 4481 | } |
| 4482 | #endif /* JOBS */ |
| 4483 | |
| 4484 | /* |
| 4485 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 4486 | * own process group. Jp is a job structure that the job is to be added to. |
| 4487 | * N is the command that will be evaluated by the child. Both jp and n may |
| 4488 | * be NULL. The mode parameter can be one of the following: |
| 4489 | * FORK_FG - Fork off a foreground process. |
| 4490 | * FORK_BG - Fork off a background process. |
| 4491 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 4492 | * process group even if job control is on. |
| 4493 | * |
| 4494 | * When job control is turned off, background processes have their standard |
| 4495 | * input redirected to /dev/null (except for the second and later processes |
| 4496 | * in a pipeline). |
| 4497 | * |
| 4498 | * Called with interrupts off. |
| 4499 | */ |
| 4500 | /* |
| 4501 | * Clear traps on a fork. |
| 4502 | */ |
| 4503 | static void |
| 4504 | clear_traps(void) |
| 4505 | { |
| 4506 | char **tp; |
| 4507 | |
| 4508 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4509 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4510 | INT_OFF; |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4511 | if (trap_ptr == trap) |
| 4512 | free(*tp); |
| 4513 | /* else: it "belongs" to trap_ptr vector, don't free */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4514 | *tp = NULL; |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 4515 | if ((tp - trap) != 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4516 | setsignal(tp - trap); |
| 4517 | INT_ON; |
| 4518 | } |
| 4519 | } |
| 4520 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4521 | |
| 4522 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4523 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 4524 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4525 | /* Called after fork(), in child */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4526 | static NOINLINE void |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4527 | forkchild(struct job *jp, union node *n, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4528 | { |
| 4529 | int oldlvl; |
| 4530 | |
| 4531 | TRACE(("Child shell %d\n", getpid())); |
| 4532 | oldlvl = shlvl; |
| 4533 | shlvl++; |
| 4534 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4535 | /* man bash: "Non-builtin commands run by bash have signal handlers |
| 4536 | * set to the values inherited by the shell from its parent". |
| 4537 | * Do we do it correctly? */ |
| 4538 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4539 | closescript(); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4540 | |
| 4541 | if (mode == FORK_NOJOB /* is it `xxx` ? */ |
| 4542 | && n && n->type == NCMD /* is it single cmd? */ |
| 4543 | /* && n->ncmd.args->type == NARG - always true? */ |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4544 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4545 | && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */ |
| 4546 | /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */ |
| 4547 | ) { |
| 4548 | TRACE(("Trap hack\n")); |
| 4549 | /* Awful hack for `trap` or $(trap). |
| 4550 | * |
| 4551 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 4552 | * contains an example where "trap" is executed in a subshell: |
| 4553 | * |
| 4554 | * save_traps=$(trap) |
| 4555 | * ... |
| 4556 | * eval "$save_traps" |
| 4557 | * |
| 4558 | * Standard does not say that "trap" in subshell shall print |
| 4559 | * parent shell's traps. It only says that its output |
| 4560 | * must have suitable form, but then, in the above example |
| 4561 | * (which is not supposed to be normative), it implies that. |
| 4562 | * |
| 4563 | * bash (and probably other shell) does implement it |
| 4564 | * (traps are reset to defaults, but "trap" still shows them), |
| 4565 | * but as a result, "trap" logic is hopelessly messed up: |
| 4566 | * |
| 4567 | * # trap |
| 4568 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 4569 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 4570 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 4571 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 4572 | * trap -- 'echo Ho' SIGWINCH |
| 4573 | * # echo `(trap)` <--- in subshell in subshell - output |
| 4574 | * trap -- 'echo Ho' SIGWINCH |
| 4575 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 4576 | * trap -- 'echo Ho' SIGWINCH |
| 4577 | * |
| 4578 | * The rules when to forget and when to not forget traps |
| 4579 | * get really complex and nonsensical. |
| 4580 | * |
| 4581 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 4582 | */ |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4583 | /* Save trap handler strings for trap builtin to print */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4584 | trap_ptr = memcpy(xmalloc(sizeof(trap)), trap, sizeof(trap)); |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4585 | /* Fall through into clearing traps */ |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4586 | } |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4587 | clear_traps(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4588 | #if JOBS |
| 4589 | /* do job control only in root shell */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4590 | doing_jobctl = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4591 | if (mode != FORK_NOJOB && jp->jobctl && !oldlvl) { |
| 4592 | pid_t pgrp; |
| 4593 | |
| 4594 | if (jp->nprocs == 0) |
| 4595 | pgrp = getpid(); |
| 4596 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4597 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4598 | /* this can fail because we are doing it in the parent also */ |
| 4599 | setpgid(0, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4600 | if (mode == FORK_FG) |
| 4601 | xtcsetpgrp(ttyfd, pgrp); |
| 4602 | setsignal(SIGTSTP); |
| 4603 | setsignal(SIGTTOU); |
| 4604 | } else |
| 4605 | #endif |
| 4606 | if (mode == FORK_BG) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4607 | /* man bash: "When job control is not in effect, |
| 4608 | * asynchronous commands ignore SIGINT and SIGQUIT" */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4609 | ignoresig(SIGINT); |
| 4610 | ignoresig(SIGQUIT); |
| 4611 | if (jp->nprocs == 0) { |
| 4612 | close(0); |
| 4613 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 4614 | ash_msg_and_raise_error("can't open '%s'", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4615 | } |
| 4616 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4617 | if (!oldlvl) { |
| 4618 | if (iflag) { /* why if iflag only? */ |
| 4619 | setsignal(SIGINT); |
| 4620 | setsignal(SIGTERM); |
| 4621 | } |
| 4622 | /* man bash: |
| 4623 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 4624 | * commands run by bash have signal handlers |
| 4625 | * set to the values inherited by the shell |
| 4626 | * from its parent". |
| 4627 | * Take care of the second rule: */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4628 | setsignal(SIGQUIT); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4629 | } |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4630 | #if JOBS |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4631 | if (n && n->type == NCMD |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4632 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4633 | ) { |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4634 | TRACE(("Job hack\n")); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4635 | /* "jobs": we do not want to clear job list for it, |
| 4636 | * instead we remove only _its_ own_ job from job list. |
| 4637 | * This makes "jobs .... | cat" more useful. |
| 4638 | */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4639 | freejob(curjob); |
| 4640 | return; |
| 4641 | } |
| 4642 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4643 | for (jp = curjob; jp; jp = jp->prev_job) |
| 4644 | freejob(jp); |
| 4645 | jobless = 0; |
| 4646 | } |
| 4647 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4648 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 4649 | #if !JOBS |
| 4650 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 4651 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4652 | static void |
| 4653 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 4654 | { |
| 4655 | TRACE(("In parent shell: child = %d\n", pid)); |
| 4656 | if (!jp) { |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4657 | while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0) |
| 4658 | continue; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4659 | jobless++; |
| 4660 | return; |
| 4661 | } |
| 4662 | #if JOBS |
| 4663 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 4664 | int pgrp; |
| 4665 | |
| 4666 | if (jp->nprocs == 0) |
| 4667 | pgrp = pid; |
| 4668 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4669 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4670 | /* This can fail because we are doing it in the child also */ |
| 4671 | setpgid(pid, pgrp); |
| 4672 | } |
| 4673 | #endif |
| 4674 | if (mode == FORK_BG) { |
| 4675 | backgndpid = pid; /* set $! */ |
| 4676 | set_curjob(jp, CUR_RUNNING); |
| 4677 | } |
| 4678 | if (jp) { |
| 4679 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4680 | ps->ps_pid = pid; |
| 4681 | ps->ps_status = -1; |
| 4682 | ps->ps_cmd = nullstr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4683 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4684 | if (doing_jobctl && n) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4685 | ps->ps_cmd = commandtext(n); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4686 | #endif |
| 4687 | } |
| 4688 | } |
| 4689 | |
| 4690 | static int |
| 4691 | forkshell(struct job *jp, union node *n, int mode) |
| 4692 | { |
| 4693 | int pid; |
| 4694 | |
| 4695 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 4696 | pid = fork(); |
| 4697 | if (pid < 0) { |
| 4698 | TRACE(("Fork failed, errno=%d", errno)); |
| 4699 | if (jp) |
| 4700 | freejob(jp); |
Denis Vlasenko | fa0b56d | 2008-07-01 16:09:07 +0000 | [diff] [blame] | 4701 | ash_msg_and_raise_error("can't fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4702 | } |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4703 | if (pid == 0) { |
| 4704 | CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4705 | forkchild(jp, n, mode); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4706 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4707 | forkparent(jp, n, mode, pid); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4708 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4709 | return pid; |
| 4710 | } |
| 4711 | |
| 4712 | /* |
| 4713 | * Wait for job to finish. |
| 4714 | * |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4715 | * Under job control we have the problem that while a child process |
| 4716 | * is running interrupts generated by the user are sent to the child |
| 4717 | * but not to the shell. This means that an infinite loop started by |
| 4718 | * an interactive user may be hard to kill. With job control turned off, |
| 4719 | * an interactive user may place an interactive program inside a loop. |
| 4720 | * If the interactive program catches interrupts, the user doesn't want |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4721 | * these interrupts to also abort the loop. The approach we take here |
| 4722 | * is to have the shell ignore interrupt signals while waiting for a |
| 4723 | * foreground process to terminate, and then send itself an interrupt |
| 4724 | * signal if the child process was terminated by an interrupt signal. |
| 4725 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 4726 | * exit on interrupt; unless these processes terminate themselves by |
| 4727 | * sending a signal to themselves (instead of calling exit) they will |
| 4728 | * confuse this approach. |
| 4729 | * |
| 4730 | * Called with interrupts off. |
| 4731 | */ |
| 4732 | static int |
| 4733 | waitforjob(struct job *jp) |
| 4734 | { |
| 4735 | int st; |
| 4736 | |
| 4737 | TRACE(("waitforjob(%%%d) called\n", jobno(jp))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4738 | |
| 4739 | INT_OFF; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4740 | while (jp->state == JOBRUNNING) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4741 | /* In non-interactive shells, we _can_ get |
| 4742 | * a keyboard signal here and be EINTRed, |
| 4743 | * but we just loop back, waiting for command to complete. |
| 4744 | * |
| 4745 | * man bash: |
| 4746 | * "If bash is waiting for a command to complete and receives |
| 4747 | * a signal for which a trap has been set, the trap |
| 4748 | * will not be executed until the command completes." |
| 4749 | * |
| 4750 | * Reality is that even if trap is not set, bash |
| 4751 | * will not act on the signal until command completes. |
| 4752 | * Try this. sleep5intoff.c: |
| 4753 | * #include <signal.h> |
| 4754 | * #include <unistd.h> |
| 4755 | * int main() { |
| 4756 | * sigset_t set; |
| 4757 | * sigemptyset(&set); |
| 4758 | * sigaddset(&set, SIGINT); |
| 4759 | * sigaddset(&set, SIGQUIT); |
| 4760 | * sigprocmask(SIG_BLOCK, &set, NULL); |
| 4761 | * sleep(5); |
| 4762 | * return 0; |
| 4763 | * } |
| 4764 | * $ bash -c './sleep5intoff; echo hi' |
| 4765 | * ^C^C^C^C <--- pressing ^C once a second |
| 4766 | * $ _ |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4767 | * $ bash -c './sleep5intoff; echo hi' |
| 4768 | * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT) |
| 4769 | * $ _ |
| 4770 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4771 | dowait(DOWAIT_BLOCK, jp); |
| 4772 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4773 | INT_ON; |
| 4774 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4775 | st = getstatus(jp); |
| 4776 | #if JOBS |
| 4777 | if (jp->jobctl) { |
| 4778 | xtcsetpgrp(ttyfd, rootpid); |
| 4779 | /* |
| 4780 | * This is truly gross. |
| 4781 | * If we're doing job control, then we did a TIOCSPGRP which |
| 4782 | * caused us (the shell) to no longer be in the controlling |
| 4783 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 4784 | * intuit from the subprocess exit status whether a SIGINT |
| 4785 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 4786 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4787 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 4788 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4789 | } |
| 4790 | if (jp->state == JOBDONE) |
| 4791 | #endif |
| 4792 | freejob(jp); |
| 4793 | return st; |
| 4794 | } |
| 4795 | |
| 4796 | /* |
| 4797 | * return 1 if there are stopped jobs, otherwise 0 |
| 4798 | */ |
| 4799 | static int |
| 4800 | stoppedjobs(void) |
| 4801 | { |
| 4802 | struct job *jp; |
| 4803 | int retval; |
| 4804 | |
| 4805 | retval = 0; |
| 4806 | if (job_warning) |
| 4807 | goto out; |
| 4808 | jp = curjob; |
| 4809 | if (jp && jp->state == JOBSTOPPED) { |
| 4810 | out2str("You have stopped jobs.\n"); |
| 4811 | job_warning = 2; |
| 4812 | retval++; |
| 4813 | } |
| 4814 | out: |
| 4815 | return retval; |
| 4816 | } |
| 4817 | |
| 4818 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4819 | /* ============ redir.c |
| 4820 | * |
| 4821 | * Code for dealing with input/output redirection. |
| 4822 | */ |
| 4823 | |
| 4824 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4825 | #define CLOSED -3 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4826 | |
| 4827 | /* |
| 4828 | * Open a file in noclobber mode. |
| 4829 | * The code was copied from bash. |
| 4830 | */ |
| 4831 | static int |
| 4832 | noclobberopen(const char *fname) |
| 4833 | { |
| 4834 | int r, fd; |
| 4835 | struct stat finfo, finfo2; |
| 4836 | |
| 4837 | /* |
| 4838 | * If the file exists and is a regular file, return an error |
| 4839 | * immediately. |
| 4840 | */ |
| 4841 | r = stat(fname, &finfo); |
| 4842 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 4843 | errno = EEXIST; |
| 4844 | return -1; |
| 4845 | } |
| 4846 | |
| 4847 | /* |
| 4848 | * If the file was not present (r != 0), make sure we open it |
| 4849 | * exclusively so that if it is created before we open it, our open |
| 4850 | * will fail. Make sure that we do not truncate an existing file. |
| 4851 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 4852 | * file was not a regular file, we leave O_EXCL off. |
| 4853 | */ |
| 4854 | if (r != 0) |
| 4855 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 4856 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
| 4857 | |
| 4858 | /* If the open failed, return the file descriptor right away. */ |
| 4859 | if (fd < 0) |
| 4860 | return fd; |
| 4861 | |
| 4862 | /* |
| 4863 | * OK, the open succeeded, but the file may have been changed from a |
| 4864 | * non-regular file to a regular file between the stat and the open. |
| 4865 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 4866 | * did not exist and is symlinked to an existing file between the stat |
| 4867 | * and open. |
| 4868 | */ |
| 4869 | |
| 4870 | /* |
| 4871 | * If we can open it and fstat the file descriptor, and neither check |
| 4872 | * revealed that it was a regular file, and the file has not been |
| 4873 | * replaced, return the file descriptor. |
| 4874 | */ |
| 4875 | if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode) |
| 4876 | && finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino) |
| 4877 | return fd; |
| 4878 | |
| 4879 | /* The file has been replaced. badness. */ |
| 4880 | close(fd); |
| 4881 | errno = EEXIST; |
| 4882 | return -1; |
| 4883 | } |
| 4884 | |
| 4885 | /* |
| 4886 | * Handle here documents. Normally we fork off a process to write the |
| 4887 | * data to a pipe. If the document is short, we can stuff the data in |
| 4888 | * the pipe without forking. |
| 4889 | */ |
| 4890 | /* openhere needs this forward reference */ |
| 4891 | static void expandhere(union node *arg, int fd); |
| 4892 | static int |
| 4893 | openhere(union node *redir) |
| 4894 | { |
| 4895 | int pip[2]; |
| 4896 | size_t len = 0; |
| 4897 | |
| 4898 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 4899 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4900 | if (redir->type == NHERE) { |
| 4901 | len = strlen(redir->nhere.doc->narg.text); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4902 | if (len <= PIPE_BUF) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4903 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
| 4904 | goto out; |
| 4905 | } |
| 4906 | } |
| 4907 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4908 | /* child */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4909 | close(pip[0]); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4910 | ignoresig(SIGINT); //signal(SIGINT, SIG_IGN); |
| 4911 | ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN); |
| 4912 | ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN); |
| 4913 | ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4914 | signal(SIGPIPE, SIG_DFL); |
| 4915 | if (redir->type == NHERE) |
| 4916 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4917 | else /* NXHERE */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4918 | expandhere(redir->nhere.doc, pip[1]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 4919 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4920 | } |
| 4921 | out: |
| 4922 | close(pip[1]); |
| 4923 | return pip[0]; |
| 4924 | } |
| 4925 | |
| 4926 | static int |
| 4927 | openredirect(union node *redir) |
| 4928 | { |
| 4929 | char *fname; |
| 4930 | int f; |
| 4931 | |
| 4932 | switch (redir->nfile.type) { |
| 4933 | case NFROM: |
| 4934 | fname = redir->nfile.expfname; |
| 4935 | f = open(fname, O_RDONLY); |
| 4936 | if (f < 0) |
| 4937 | goto eopen; |
| 4938 | break; |
| 4939 | case NFROMTO: |
| 4940 | fname = redir->nfile.expfname; |
| 4941 | f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666); |
| 4942 | if (f < 0) |
| 4943 | goto ecreate; |
| 4944 | break; |
| 4945 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4946 | #if ENABLE_ASH_BASH_COMPAT |
| 4947 | case NTO2: |
| 4948 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4949 | /* Take care of noclobber mode. */ |
| 4950 | if (Cflag) { |
| 4951 | fname = redir->nfile.expfname; |
| 4952 | f = noclobberopen(fname); |
| 4953 | if (f < 0) |
| 4954 | goto ecreate; |
| 4955 | break; |
| 4956 | } |
| 4957 | /* FALLTHROUGH */ |
| 4958 | case NCLOBBER: |
| 4959 | fname = redir->nfile.expfname; |
| 4960 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 4961 | if (f < 0) |
| 4962 | goto ecreate; |
| 4963 | break; |
| 4964 | case NAPPEND: |
| 4965 | fname = redir->nfile.expfname; |
| 4966 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 4967 | if (f < 0) |
| 4968 | goto ecreate; |
| 4969 | break; |
| 4970 | default: |
| 4971 | #if DEBUG |
| 4972 | abort(); |
| 4973 | #endif |
| 4974 | /* Fall through to eliminate warning. */ |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 4975 | /* Our single caller does this itself */ |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4976 | // case NTOFD: |
| 4977 | // case NFROMFD: |
| 4978 | // f = -1; |
| 4979 | // break; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4980 | case NHERE: |
| 4981 | case NXHERE: |
| 4982 | f = openhere(redir); |
| 4983 | break; |
| 4984 | } |
| 4985 | |
| 4986 | return f; |
| 4987 | ecreate: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 4988 | 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] | 4989 | eopen: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 4990 | 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] | 4991 | } |
| 4992 | |
| 4993 | /* |
| 4994 | * Copy a file descriptor to be >= to. Returns -1 |
| 4995 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 4996 | * file descriptors left. |
| 4997 | */ |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 4998 | /* 0x800..00: bit to set in "to" to request dup2 instead of fcntl(F_DUPFD). |
| 4999 | * 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] | 5000 | enum { |
| 5001 | COPYFD_EXACT = (int)~(INT_MAX), |
| 5002 | COPYFD_RESTORE = (int)((unsigned)COPYFD_EXACT >> 1), |
| 5003 | }; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5004 | static int |
| 5005 | copyfd(int from, int to) |
| 5006 | { |
| 5007 | int newfd; |
| 5008 | |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5009 | if (to & COPYFD_EXACT) { |
| 5010 | to &= ~COPYFD_EXACT; |
| 5011 | /*if (from != to)*/ |
| 5012 | newfd = dup2(from, to); |
| 5013 | } else { |
| 5014 | newfd = fcntl(from, F_DUPFD, to); |
| 5015 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5016 | if (newfd < 0) { |
| 5017 | if (errno == EMFILE) |
| 5018 | return EMPTY; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5019 | /* Happens when source fd is not open: try "echo >&99" */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5020 | ash_msg_and_raise_error("%d: %m", from); |
| 5021 | } |
| 5022 | return newfd; |
| 5023 | } |
| 5024 | |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5025 | /* Struct def and variable are moved down to the first usage site */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5026 | struct two_fd_t { |
| 5027 | int orig, copy; |
| 5028 | }; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5029 | struct redirtab { |
| 5030 | struct redirtab *next; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5031 | int nullredirs; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5032 | int pair_count; |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 5033 | struct two_fd_t two_fd[]; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5034 | }; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5035 | #define redirlist (G_var.redirlist) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5036 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5037 | static int need_to_remember(struct redirtab *rp, int fd) |
| 5038 | { |
| 5039 | int i; |
| 5040 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5041 | if (!rp) /* remembering was not requested */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5042 | return 0; |
| 5043 | |
| 5044 | for (i = 0; i < rp->pair_count; i++) { |
| 5045 | if (rp->two_fd[i].orig == fd) { |
| 5046 | /* already remembered */ |
| 5047 | return 0; |
| 5048 | } |
| 5049 | } |
| 5050 | return 1; |
| 5051 | } |
| 5052 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5053 | /* "hidden" fd is a fd used to read scripts, or a copy of such */ |
| 5054 | static int is_hidden_fd(struct redirtab *rp, int fd) |
| 5055 | { |
| 5056 | int i; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5057 | struct parsefile *pf; |
| 5058 | |
| 5059 | if (fd == -1) |
| 5060 | return 0; |
| 5061 | pf = g_parsefile; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5062 | while (pf) { |
| 5063 | if (fd == pf->fd) { |
| 5064 | return 1; |
| 5065 | } |
| 5066 | pf = pf->prev; |
| 5067 | } |
| 5068 | if (!rp) |
| 5069 | return 0; |
| 5070 | fd |= COPYFD_RESTORE; |
| 5071 | for (i = 0; i < rp->pair_count; i++) { |
| 5072 | if (rp->two_fd[i].copy == fd) { |
| 5073 | return 1; |
| 5074 | } |
| 5075 | } |
| 5076 | return 0; |
| 5077 | } |
| 5078 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5079 | /* |
| 5080 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 5081 | * old file descriptors are stashed away so that the redirection can be |
| 5082 | * undone by calling popredir. If the REDIR_BACKQ flag is set, then the |
| 5083 | * standard output, and the standard error if it becomes a duplicate of |
| 5084 | * stdout, is saved in memory. |
| 5085 | */ |
| 5086 | /* flags passed to redirect */ |
| 5087 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
| 5088 | #define REDIR_SAVEFD2 03 /* set preverrout */ |
| 5089 | static void |
| 5090 | redirect(union node *redir, int flags) |
| 5091 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5092 | struct redirtab *sv; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5093 | int sv_pos; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5094 | int i; |
| 5095 | int fd; |
| 5096 | int newfd; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5097 | int copied_fd2 = -1; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5098 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5099 | g_nullredirs++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5100 | if (!redir) { |
| 5101 | return; |
| 5102 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5103 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5104 | sv = NULL; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5105 | sv_pos = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5106 | INT_OFF; |
| 5107 | if (flags & REDIR_PUSH) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5108 | union node *tmp = redir; |
| 5109 | do { |
| 5110 | sv_pos++; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5111 | #if ENABLE_ASH_BASH_COMPAT |
Chris Metcalf | c3c1fb6 | 2010-01-08 13:18:06 +0100 | [diff] [blame] | 5112 | if (tmp->nfile.type == NTO2) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5113 | sv_pos++; |
| 5114 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5115 | tmp = tmp->nfile.next; |
| 5116 | } while (tmp); |
| 5117 | sv = ckmalloc(sizeof(*sv) + sv_pos * sizeof(sv->two_fd[0])); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5118 | sv->next = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5119 | sv->pair_count = sv_pos; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5120 | redirlist = sv; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5121 | sv->nullredirs = g_nullredirs - 1; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5122 | g_nullredirs = 0; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5123 | while (sv_pos > 0) { |
| 5124 | sv_pos--; |
| 5125 | sv->two_fd[sv_pos].orig = sv->two_fd[sv_pos].copy = EMPTY; |
| 5126 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5127 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5128 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5129 | do { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5130 | fd = redir->nfile.fd; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5131 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5132 | int right_fd = redir->ndup.dupfd; |
| 5133 | /* redirect from/to same file descriptor? */ |
| 5134 | if (right_fd == fd) |
| 5135 | continue; |
| 5136 | /* echo >&10 and 10 is a fd opened to the sh script? */ |
| 5137 | if (is_hidden_fd(sv, right_fd)) { |
| 5138 | errno = EBADF; /* as if it is closed */ |
| 5139 | ash_msg_and_raise_error("%d: %m", right_fd); |
| 5140 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5141 | newfd = -1; |
| 5142 | } else { |
| 5143 | newfd = openredirect(redir); /* always >= 0 */ |
| 5144 | if (fd == newfd) { |
| 5145 | /* Descriptor wasn't open before redirect. |
| 5146 | * Mark it for close in the future */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5147 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5148 | goto remember_to_close; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5149 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5150 | continue; |
| 5151 | } |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5152 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5153 | #if ENABLE_ASH_BASH_COMPAT |
| 5154 | redirect_more: |
| 5155 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5156 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5157 | /* Copy old descriptor */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5158 | i = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5159 | /* You'd expect copy to be CLOEXECed. Currently these extra "saved" fds |
| 5160 | * are closed in popredir() in the child, preventing them from leaking |
| 5161 | * into child. (popredir() also cleans up the mess in case of failures) |
| 5162 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5163 | if (i == -1) { |
| 5164 | i = errno; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5165 | if (i != EBADF) { |
| 5166 | /* Strange error (e.g. "too many files" EMFILE?) */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5167 | if (newfd >= 0) |
| 5168 | close(newfd); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5169 | errno = i; |
| 5170 | ash_msg_and_raise_error("%d: %m", fd); |
| 5171 | /* NOTREACHED */ |
| 5172 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5173 | /* EBADF: it is not open - good, remember to close it */ |
| 5174 | remember_to_close: |
| 5175 | i = CLOSED; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5176 | } else { /* fd is open, save its copy */ |
| 5177 | /* "exec fd>&-" should not close fds |
| 5178 | * which point to script file(s). |
| 5179 | * Force them to be restored afterwards */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5180 | if (is_hidden_fd(sv, fd)) |
| 5181 | i |= COPYFD_RESTORE; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5182 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5183 | if (fd == 2) |
| 5184 | copied_fd2 = i; |
| 5185 | sv->two_fd[sv_pos].orig = fd; |
| 5186 | sv->two_fd[sv_pos].copy = i; |
| 5187 | sv_pos++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5188 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5189 | if (newfd < 0) { |
| 5190 | /* NTOFD/NFROMFD: copy redir->ndup.dupfd to fd */ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5191 | if (redir->ndup.dupfd < 0) { /* "fd>&-" */ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5192 | /* Don't want to trigger debugging */ |
| 5193 | if (fd != -1) |
| 5194 | close(fd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5195 | } else { |
| 5196 | copyfd(redir->ndup.dupfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5197 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5198 | } else if (fd != newfd) { /* move newfd to fd */ |
| 5199 | copyfd(newfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5200 | #if ENABLE_ASH_BASH_COMPAT |
| 5201 | if (!(redir->nfile.type == NTO2 && fd == 2)) |
| 5202 | #endif |
| 5203 | close(newfd); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5204 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5205 | #if ENABLE_ASH_BASH_COMPAT |
| 5206 | if (redir->nfile.type == NTO2 && fd == 1) { |
| 5207 | /* We already redirected it to fd 1, now copy it to 2 */ |
| 5208 | newfd = 1; |
| 5209 | fd = 2; |
| 5210 | goto redirect_more; |
| 5211 | } |
| 5212 | #endif |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5213 | } while ((redir = redir->nfile.next) != NULL); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5214 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5215 | INT_ON; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5216 | if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0) |
| 5217 | preverrout_fd = copied_fd2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | /* |
| 5221 | * Undo the effects of the last redirection. |
| 5222 | */ |
| 5223 | static void |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5224 | popredir(int drop, int restore) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5225 | { |
| 5226 | struct redirtab *rp; |
| 5227 | int i; |
| 5228 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5229 | if (--g_nullredirs >= 0) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5230 | return; |
| 5231 | INT_OFF; |
| 5232 | rp = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5233 | for (i = 0; i < rp->pair_count; i++) { |
| 5234 | int fd = rp->two_fd[i].orig; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5235 | int copy = rp->two_fd[i].copy; |
| 5236 | if (copy == CLOSED) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5237 | if (!drop) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5238 | close(fd); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5239 | continue; |
| 5240 | } |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5241 | if (copy != EMPTY) { |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5242 | if (!drop || (restore && (copy & COPYFD_RESTORE))) { |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5243 | copy &= ~COPYFD_RESTORE; |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5244 | /*close(fd);*/ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5245 | copyfd(copy, fd | COPYFD_EXACT); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5246 | } |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5247 | close(copy & ~COPYFD_RESTORE); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5248 | } |
| 5249 | } |
| 5250 | redirlist = rp->next; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5251 | g_nullredirs = rp->nullredirs; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5252 | free(rp); |
| 5253 | INT_ON; |
| 5254 | } |
| 5255 | |
| 5256 | /* |
| 5257 | * Undo all redirections. Called on error or interrupt. |
| 5258 | */ |
| 5259 | |
| 5260 | /* |
| 5261 | * Discard all saved file descriptors. |
| 5262 | */ |
| 5263 | static void |
| 5264 | clearredir(int drop) |
| 5265 | { |
| 5266 | for (;;) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5267 | g_nullredirs = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5268 | if (!redirlist) |
| 5269 | break; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5270 | popredir(drop, /*restore:*/ 0); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5271 | } |
| 5272 | } |
| 5273 | |
| 5274 | static int |
| 5275 | redirectsafe(union node *redir, int flags) |
| 5276 | { |
| 5277 | int err; |
| 5278 | volatile int saveint; |
| 5279 | struct jmploc *volatile savehandler = exception_handler; |
| 5280 | struct jmploc jmploc; |
| 5281 | |
| 5282 | SAVE_INT(saveint); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5283 | /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */ |
| 5284 | err = setjmp(jmploc.loc); // huh?? was = setjmp(jmploc.loc) * 2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5285 | if (!err) { |
| 5286 | exception_handler = &jmploc; |
| 5287 | redirect(redir, flags); |
| 5288 | } |
| 5289 | exception_handler = savehandler; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 5290 | if (err && exception_type != EXERROR) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5291 | longjmp(exception_handler->loc, 1); |
| 5292 | RESTORE_INT(saveint); |
| 5293 | return err; |
| 5294 | } |
| 5295 | |
| 5296 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5297 | /* ============ Routines to expand arguments to commands |
| 5298 | * |
| 5299 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 5300 | */ |
| 5301 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5302 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5303 | static arith_t |
| 5304 | ash_arith(const char *s) |
| 5305 | { |
| 5306 | arith_eval_hooks_t math_hooks; |
| 5307 | arith_t result; |
| 5308 | int errcode = 0; |
| 5309 | |
| 5310 | math_hooks.lookupvar = lookupvar; |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 5311 | math_hooks.setvar = setvar2; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5312 | math_hooks.endofname = endofname; |
| 5313 | |
| 5314 | INT_OFF; |
| 5315 | result = arith(s, &errcode, &math_hooks); |
| 5316 | if (errcode < 0) { |
| 5317 | if (errcode == -3) |
| 5318 | ash_msg_and_raise_error("exponent less than 0"); |
| 5319 | if (errcode == -2) |
| 5320 | ash_msg_and_raise_error("divide by zero"); |
| 5321 | if (errcode == -5) |
| 5322 | ash_msg_and_raise_error("expression recursion loop detected"); |
| 5323 | raise_error_syntax(s); |
| 5324 | } |
| 5325 | INT_ON; |
| 5326 | |
| 5327 | return result; |
| 5328 | } |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5329 | #endif |
| 5330 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5331 | /* |
| 5332 | * expandarg flags |
| 5333 | */ |
| 5334 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 5335 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 5336 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 5337 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 5338 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
| 5339 | #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */ |
| 5340 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ |
| 5341 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ |
| 5342 | #define EXP_QWORD 0x100 /* expand word in quoted parameter expansion */ |
| 5343 | /* |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5344 | * rmescape() flags |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5345 | */ |
| 5346 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 5347 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
| 5348 | #define RMESCAPE_QUOTED 0x4 /* Remove CTLESC unless in quotes */ |
| 5349 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 5350 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
| 5351 | |
| 5352 | /* |
| 5353 | * Structure specifying which parts of the string should be searched |
| 5354 | * for IFS characters. |
| 5355 | */ |
| 5356 | struct ifsregion { |
| 5357 | struct ifsregion *next; /* next region in list */ |
| 5358 | int begoff; /* offset of start of region */ |
| 5359 | int endoff; /* offset of end of region */ |
| 5360 | int nulonly; /* search for nul bytes only */ |
| 5361 | }; |
| 5362 | |
| 5363 | struct arglist { |
| 5364 | struct strlist *list; |
| 5365 | struct strlist **lastp; |
| 5366 | }; |
| 5367 | |
| 5368 | /* output of current string */ |
| 5369 | static char *expdest; |
| 5370 | /* list of back quote expressions */ |
| 5371 | static struct nodelist *argbackq; |
| 5372 | /* first struct in list of ifs regions */ |
| 5373 | static struct ifsregion ifsfirst; |
| 5374 | /* last struct in list */ |
| 5375 | static struct ifsregion *ifslastp; |
| 5376 | /* holds expanded arg list */ |
| 5377 | static struct arglist exparg; |
| 5378 | |
| 5379 | /* |
| 5380 | * Our own itoa(). |
| 5381 | */ |
| 5382 | static int |
| 5383 | cvtnum(arith_t num) |
| 5384 | { |
| 5385 | int len; |
| 5386 | |
| 5387 | expdest = makestrspace(32, expdest); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5388 | len = fmtstr(expdest, 32, arith_t_fmt, num); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5389 | STADJUST(len, expdest); |
| 5390 | return len; |
| 5391 | } |
| 5392 | |
| 5393 | static size_t |
| 5394 | esclen(const char *start, const char *p) |
| 5395 | { |
| 5396 | size_t esc = 0; |
| 5397 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5398 | while (p > start && (unsigned char)*--p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5399 | esc++; |
| 5400 | } |
| 5401 | return esc; |
| 5402 | } |
| 5403 | |
| 5404 | /* |
| 5405 | * Remove any CTLESC characters from a string. |
| 5406 | */ |
| 5407 | static char * |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5408 | rmescapes(char *str, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5409 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5410 | static const char qchars[] ALIGN1 = { CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 5411 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5412 | char *p, *q, *r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5413 | unsigned inquotes; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5414 | unsigned protect_against_glob; |
| 5415 | unsigned globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5416 | |
| 5417 | p = strpbrk(str, qchars); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5418 | if (!p) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5419 | return str; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5420 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5421 | q = p; |
| 5422 | r = str; |
| 5423 | if (flag & RMESCAPE_ALLOC) { |
| 5424 | size_t len = p - str; |
| 5425 | size_t fulllen = len + strlen(p) + 1; |
| 5426 | |
| 5427 | if (flag & RMESCAPE_GROW) { |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5428 | int strloc = str - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5429 | r = makestrspace(fulllen, expdest); |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5430 | /* p and str may be invalidated by makestrspace */ |
| 5431 | str = (char *)stackblock() + strloc; |
| 5432 | p = str + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5433 | } else if (flag & RMESCAPE_HEAP) { |
| 5434 | r = ckmalloc(fulllen); |
| 5435 | } else { |
| 5436 | r = stalloc(fulllen); |
| 5437 | } |
| 5438 | q = r; |
| 5439 | if (len > 0) { |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5440 | q = (char *)memcpy(q, str, len) + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5441 | } |
| 5442 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5443 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5444 | inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED; |
| 5445 | globbing = flag & RMESCAPE_GLOB; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5446 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5447 | while (*p) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5448 | if ((unsigned char)*p == CTLQUOTEMARK) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5449 | // TODO: if no RMESCAPE_QUOTED in flags, inquotes never becomes 0 |
| 5450 | // (alternates between RMESCAPE_QUOTED and ~RMESCAPE_QUOTED). Is it ok? |
| 5451 | // Note: both inquotes and protect_against_glob only affect whether |
| 5452 | // CTLESC,<ch> gets converted to <ch> or to \<ch> |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5453 | inquotes = ~inquotes; |
| 5454 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5455 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5456 | continue; |
| 5457 | } |
| 5458 | if (*p == '\\') { |
| 5459 | /* naked back slash */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5460 | protect_against_glob = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5461 | goto copy; |
| 5462 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5463 | if ((unsigned char)*p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5464 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5465 | if (protect_against_glob && inquotes && *p != '/') { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5466 | *q++ = '\\'; |
| 5467 | } |
| 5468 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5469 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5470 | copy: |
| 5471 | *q++ = *p++; |
| 5472 | } |
| 5473 | *q = '\0'; |
| 5474 | if (flag & RMESCAPE_GROW) { |
| 5475 | expdest = r; |
| 5476 | STADJUST(q - r + 1, expdest); |
| 5477 | } |
| 5478 | return r; |
| 5479 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5480 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 5481 | |
| 5482 | /* |
| 5483 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 5484 | * |
| 5485 | * Returns an stalloced string. |
| 5486 | */ |
| 5487 | static char * |
| 5488 | preglob(const char *pattern, int quoted, int flag) |
| 5489 | { |
| 5490 | flag |= RMESCAPE_GLOB; |
| 5491 | if (quoted) { |
| 5492 | flag |= RMESCAPE_QUOTED; |
| 5493 | } |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5494 | return rmescapes((char *)pattern, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5495 | } |
| 5496 | |
| 5497 | /* |
| 5498 | * Put a string on the stack. |
| 5499 | */ |
| 5500 | static void |
| 5501 | memtodest(const char *p, size_t len, int syntax, int quotes) |
| 5502 | { |
| 5503 | char *q = expdest; |
| 5504 | |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5505 | q = makestrspace(quotes ? len * 2 : len, q); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5506 | |
| 5507 | while (len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5508 | unsigned char c = *p++; |
| 5509 | if (c == '\0') |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5510 | continue; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5511 | if (quotes) { |
| 5512 | int n = SIT(c, syntax); |
| 5513 | if (n == CCTL || n == CBACK) |
| 5514 | USTPUTC(CTLESC, q); |
| 5515 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5516 | USTPUTC(c, q); |
| 5517 | } |
| 5518 | |
| 5519 | expdest = q; |
| 5520 | } |
| 5521 | |
| 5522 | static void |
| 5523 | strtodest(const char *p, int syntax, int quotes) |
| 5524 | { |
| 5525 | memtodest(p, strlen(p), syntax, quotes); |
| 5526 | } |
| 5527 | |
| 5528 | /* |
| 5529 | * Record the fact that we have to scan this region of the |
| 5530 | * string for IFS characters. |
| 5531 | */ |
| 5532 | static void |
| 5533 | recordregion(int start, int end, int nulonly) |
| 5534 | { |
| 5535 | struct ifsregion *ifsp; |
| 5536 | |
| 5537 | if (ifslastp == NULL) { |
| 5538 | ifsp = &ifsfirst; |
| 5539 | } else { |
| 5540 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 5541 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 5542 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5543 | ifslastp->next = ifsp; |
| 5544 | INT_ON; |
| 5545 | } |
| 5546 | ifslastp = ifsp; |
| 5547 | ifslastp->begoff = start; |
| 5548 | ifslastp->endoff = end; |
| 5549 | ifslastp->nulonly = nulonly; |
| 5550 | } |
| 5551 | |
| 5552 | static void |
| 5553 | removerecordregions(int endoff) |
| 5554 | { |
| 5555 | if (ifslastp == NULL) |
| 5556 | return; |
| 5557 | |
| 5558 | if (ifsfirst.endoff > endoff) { |
| 5559 | while (ifsfirst.next != NULL) { |
| 5560 | struct ifsregion *ifsp; |
| 5561 | INT_OFF; |
| 5562 | ifsp = ifsfirst.next->next; |
| 5563 | free(ifsfirst.next); |
| 5564 | ifsfirst.next = ifsp; |
| 5565 | INT_ON; |
| 5566 | } |
| 5567 | if (ifsfirst.begoff > endoff) |
| 5568 | ifslastp = NULL; |
| 5569 | else { |
| 5570 | ifslastp = &ifsfirst; |
| 5571 | ifsfirst.endoff = endoff; |
| 5572 | } |
| 5573 | return; |
| 5574 | } |
| 5575 | |
| 5576 | ifslastp = &ifsfirst; |
| 5577 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
| 5578 | ifslastp=ifslastp->next; |
| 5579 | while (ifslastp->next != NULL) { |
| 5580 | struct ifsregion *ifsp; |
| 5581 | INT_OFF; |
| 5582 | ifsp = ifslastp->next->next; |
| 5583 | free(ifslastp->next); |
| 5584 | ifslastp->next = ifsp; |
| 5585 | INT_ON; |
| 5586 | } |
| 5587 | if (ifslastp->endoff > endoff) |
| 5588 | ifslastp->endoff = endoff; |
| 5589 | } |
| 5590 | |
| 5591 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5592 | exptilde(char *startp, char *p, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5593 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5594 | unsigned char c; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5595 | char *name; |
| 5596 | struct passwd *pw; |
| 5597 | const char *home; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 5598 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5599 | int startloc; |
| 5600 | |
| 5601 | name = p + 1; |
| 5602 | |
| 5603 | while ((c = *++p) != '\0') { |
| 5604 | switch (c) { |
| 5605 | case CTLESC: |
| 5606 | return startp; |
| 5607 | case CTLQUOTEMARK: |
| 5608 | return startp; |
| 5609 | case ':': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5610 | if (flags & EXP_VARTILDE) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5611 | goto done; |
| 5612 | break; |
| 5613 | case '/': |
| 5614 | case CTLENDVAR: |
| 5615 | goto done; |
| 5616 | } |
| 5617 | } |
| 5618 | done: |
| 5619 | *p = '\0'; |
| 5620 | if (*name == '\0') { |
| 5621 | home = lookupvar(homestr); |
| 5622 | } else { |
| 5623 | pw = getpwnam(name); |
| 5624 | if (pw == NULL) |
| 5625 | goto lose; |
| 5626 | home = pw->pw_dir; |
| 5627 | } |
| 5628 | if (!home || !*home) |
| 5629 | goto lose; |
| 5630 | *p = c; |
| 5631 | startloc = expdest - (char *)stackblock(); |
| 5632 | strtodest(home, SQSYNTAX, quotes); |
| 5633 | recordregion(startloc, expdest - (char *)stackblock(), 0); |
| 5634 | return p; |
| 5635 | lose: |
| 5636 | *p = c; |
| 5637 | return startp; |
| 5638 | } |
| 5639 | |
| 5640 | /* |
| 5641 | * Execute a command inside back quotes. If it's a builtin command, we |
| 5642 | * want to save its output in a block obtained from malloc. Otherwise |
| 5643 | * we fork off a subprocess and get the output of the command via a pipe. |
| 5644 | * Should be called with interrupts off. |
| 5645 | */ |
| 5646 | struct backcmd { /* result of evalbackcmd */ |
| 5647 | int fd; /* file descriptor to read from */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5648 | int nleft; /* number of chars in buffer */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5649 | char *buf; /* buffer */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5650 | struct job *jp; /* job structure for command */ |
| 5651 | }; |
| 5652 | |
| 5653 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5654 | static uint8_t back_exitstatus; /* exit status of backquoted command */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5655 | #define EV_EXIT 01 /* exit after evaluating tree */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 5656 | static void evaltree(union node *, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5657 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 5658 | static void FAST_FUNC |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5659 | evalbackcmd(union node *n, struct backcmd *result) |
| 5660 | { |
| 5661 | int saveherefd; |
| 5662 | |
| 5663 | result->fd = -1; |
| 5664 | result->buf = NULL; |
| 5665 | result->nleft = 0; |
| 5666 | result->jp = NULL; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5667 | if (n == NULL) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5668 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5669 | |
| 5670 | saveherefd = herefd; |
| 5671 | herefd = -1; |
| 5672 | |
| 5673 | { |
| 5674 | int pip[2]; |
| 5675 | struct job *jp; |
| 5676 | |
| 5677 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5678 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 5679 | jp = makejob(/*n,*/ 1); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5680 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
| 5681 | FORCE_INT_ON; |
| 5682 | close(pip[0]); |
| 5683 | if (pip[1] != 1) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5684 | /*close(1);*/ |
| 5685 | copyfd(pip[1], 1 | COPYFD_EXACT); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5686 | close(pip[1]); |
| 5687 | } |
| 5688 | eflag = 0; |
| 5689 | evaltree(n, EV_EXIT); /* actually evaltreenr... */ |
| 5690 | /* NOTREACHED */ |
| 5691 | } |
| 5692 | close(pip[1]); |
| 5693 | result->fd = pip[0]; |
| 5694 | result->jp = jp; |
| 5695 | } |
| 5696 | herefd = saveherefd; |
| 5697 | out: |
| 5698 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 5699 | result->fd, result->buf, result->nleft, result->jp)); |
| 5700 | } |
| 5701 | |
| 5702 | /* |
| 5703 | * Expand stuff in backwards quotes. |
| 5704 | */ |
| 5705 | static void |
| 5706 | expbackq(union node *cmd, int quoted, int quotes) |
| 5707 | { |
| 5708 | struct backcmd in; |
| 5709 | int i; |
| 5710 | char buf[128]; |
| 5711 | char *p; |
| 5712 | char *dest; |
| 5713 | int startloc; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5714 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5715 | struct stackmark smark; |
| 5716 | |
| 5717 | INT_OFF; |
| 5718 | setstackmark(&smark); |
| 5719 | dest = expdest; |
| 5720 | startloc = dest - (char *)stackblock(); |
| 5721 | grabstackstr(dest); |
| 5722 | evalbackcmd(cmd, &in); |
| 5723 | popstackmark(&smark); |
| 5724 | |
| 5725 | p = in.buf; |
| 5726 | i = in.nleft; |
| 5727 | if (i == 0) |
| 5728 | goto read; |
| 5729 | for (;;) { |
| 5730 | memtodest(p, i, syntax, quotes); |
| 5731 | read: |
| 5732 | if (in.fd < 0) |
| 5733 | break; |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 5734 | i = nonblock_safe_read(in.fd, buf, sizeof(buf)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5735 | TRACE(("expbackq: read returns %d\n", i)); |
| 5736 | if (i <= 0) |
| 5737 | break; |
| 5738 | p = buf; |
| 5739 | } |
| 5740 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 5741 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5742 | if (in.fd >= 0) { |
| 5743 | close(in.fd); |
| 5744 | back_exitstatus = waitforjob(in.jp); |
| 5745 | } |
| 5746 | INT_ON; |
| 5747 | |
| 5748 | /* Eat all trailing newlines */ |
| 5749 | dest = expdest; |
| 5750 | for (; dest > (char *)stackblock() && dest[-1] == '\n';) |
| 5751 | STUNPUTC(dest); |
| 5752 | expdest = dest; |
| 5753 | |
| 5754 | if (quoted == 0) |
| 5755 | recordregion(startloc, dest - (char *)stackblock(), 0); |
| 5756 | TRACE(("evalbackq: size=%d: \"%.*s\"\n", |
| 5757 | (dest - (char *)stackblock()) - startloc, |
| 5758 | (dest - (char *)stackblock()) - startloc, |
| 5759 | stackblock() + startloc)); |
| 5760 | } |
| 5761 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5762 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5763 | /* |
| 5764 | * Expand arithmetic expression. Backup to start of expression, |
| 5765 | * evaluate, place result in (backed up) result, adjust string position. |
| 5766 | */ |
| 5767 | static void |
| 5768 | expari(int quotes) |
| 5769 | { |
| 5770 | char *p, *start; |
| 5771 | int begoff; |
| 5772 | int flag; |
| 5773 | int len; |
| 5774 | |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5775 | /* ifsfree(); */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5776 | |
| 5777 | /* |
| 5778 | * This routine is slightly over-complicated for |
| 5779 | * efficiency. Next we scan backwards looking for the |
| 5780 | * start of arithmetic. |
| 5781 | */ |
| 5782 | start = stackblock(); |
| 5783 | p = expdest - 1; |
| 5784 | *p = '\0'; |
| 5785 | p--; |
| 5786 | do { |
| 5787 | int esc; |
| 5788 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5789 | while ((unsigned char)*p != CTLARI) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5790 | p--; |
| 5791 | #if DEBUG |
| 5792 | if (p < start) { |
| 5793 | ash_msg_and_raise_error("missing CTLARI (shouldn't happen)"); |
| 5794 | } |
| 5795 | #endif |
| 5796 | } |
| 5797 | |
| 5798 | esc = esclen(start, p); |
| 5799 | if (!(esc % 2)) { |
| 5800 | break; |
| 5801 | } |
| 5802 | |
| 5803 | p -= esc + 1; |
| 5804 | } while (1); |
| 5805 | |
| 5806 | begoff = p - start; |
| 5807 | |
| 5808 | removerecordregions(begoff); |
| 5809 | |
| 5810 | flag = p[1]; |
| 5811 | |
| 5812 | expdest = p; |
| 5813 | |
| 5814 | if (quotes) |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5815 | rmescapes(p + 2, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5816 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5817 | len = cvtnum(ash_arith(p + 2)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5818 | |
| 5819 | if (flag != '"') |
| 5820 | recordregion(begoff, begoff + len, 0); |
| 5821 | } |
| 5822 | #endif |
| 5823 | |
| 5824 | /* argstr needs it */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5825 | static char *evalvar(char *p, int flags, struct strlist *var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5826 | |
| 5827 | /* |
| 5828 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 5829 | * characters to allow for further processing. Otherwise treat |
| 5830 | * $@ like $* since no splitting will be performed. |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5831 | * |
| 5832 | * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence |
| 5833 | * over shell varables. Needed for "A=a B=$A; echo $B" case - we use it |
| 5834 | * for correct expansion of "B=$A" word. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5835 | */ |
| 5836 | static void |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5837 | argstr(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5838 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5839 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5840 | '=', |
| 5841 | ':', |
| 5842 | CTLQUOTEMARK, |
| 5843 | CTLENDVAR, |
| 5844 | CTLESC, |
| 5845 | CTLVAR, |
| 5846 | CTLBACKQ, |
| 5847 | CTLBACKQ | CTLQUOTE, |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5848 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5849 | CTLENDARI, |
| 5850 | #endif |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5851 | '\0' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5852 | }; |
| 5853 | const char *reject = spclchars; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5854 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */ |
| 5855 | int breakall = flags & EXP_WORD; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5856 | int inquotes; |
| 5857 | size_t length; |
| 5858 | int startloc; |
| 5859 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5860 | if (!(flags & EXP_VARTILDE)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5861 | reject += 2; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5862 | } else if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5863 | reject++; |
| 5864 | } |
| 5865 | inquotes = 0; |
| 5866 | length = 0; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5867 | if (flags & EXP_TILDE) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5868 | char *q; |
| 5869 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5870 | flags &= ~EXP_TILDE; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5871 | tilde: |
| 5872 | q = p; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5873 | if (*q == CTLESC && (flags & EXP_QWORD)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5874 | q++; |
| 5875 | if (*q == '~') |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5876 | p = exptilde(p, q, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5877 | } |
| 5878 | start: |
| 5879 | startloc = expdest - (char *)stackblock(); |
| 5880 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5881 | unsigned char c; |
| 5882 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5883 | length += strcspn(p + length, reject); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5884 | c = p[length]; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5885 | if (c) { |
| 5886 | if (!(c & 0x80) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5887 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5888 | || c == CTLENDARI |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5889 | #endif |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5890 | ) { |
| 5891 | /* c == '=' || c == ':' || c == CTLENDARI */ |
| 5892 | length++; |
| 5893 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5894 | } |
| 5895 | if (length > 0) { |
| 5896 | int newloc; |
| 5897 | expdest = stack_nputstr(p, length, expdest); |
| 5898 | newloc = expdest - (char *)stackblock(); |
| 5899 | if (breakall && !inquotes && newloc > startloc) { |
| 5900 | recordregion(startloc, newloc, 0); |
| 5901 | } |
| 5902 | startloc = newloc; |
| 5903 | } |
| 5904 | p += length + 1; |
| 5905 | length = 0; |
| 5906 | |
| 5907 | switch (c) { |
| 5908 | case '\0': |
| 5909 | goto breakloop; |
| 5910 | case '=': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5911 | if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5912 | p--; |
| 5913 | continue; |
| 5914 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5915 | flags |= EXP_VARTILDE2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5916 | reject++; |
| 5917 | /* fall through */ |
| 5918 | case ':': |
| 5919 | /* |
| 5920 | * sort of a hack - expand tildes in variable |
| 5921 | * assignments (after the first '=' and after ':'s). |
| 5922 | */ |
| 5923 | if (*--p == '~') { |
| 5924 | goto tilde; |
| 5925 | } |
| 5926 | continue; |
| 5927 | } |
| 5928 | |
| 5929 | switch (c) { |
| 5930 | case CTLENDVAR: /* ??? */ |
| 5931 | goto breakloop; |
| 5932 | case CTLQUOTEMARK: |
| 5933 | /* "$@" syntax adherence hack */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5934 | if (!inquotes |
| 5935 | && memcmp(p, dolatstr, 4) == 0 |
| 5936 | && ( p[4] == CTLQUOTEMARK |
| 5937 | || (p[4] == CTLENDVAR && p[5] == CTLQUOTEMARK) |
| 5938 | ) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5939 | ) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5940 | p = evalvar(p + 1, flags, /* var_str_list: */ NULL) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5941 | goto start; |
| 5942 | } |
| 5943 | inquotes = !inquotes; |
| 5944 | addquote: |
| 5945 | if (quotes) { |
| 5946 | p--; |
| 5947 | length++; |
| 5948 | startloc++; |
| 5949 | } |
| 5950 | break; |
| 5951 | case CTLESC: |
| 5952 | startloc++; |
| 5953 | length++; |
| 5954 | goto addquote; |
| 5955 | case CTLVAR: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5956 | p = evalvar(p, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5957 | goto start; |
| 5958 | case CTLBACKQ: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5959 | c = '\0'; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5960 | case CTLBACKQ|CTLQUOTE: |
| 5961 | expbackq(argbackq->n, c, quotes); |
| 5962 | argbackq = argbackq->next; |
| 5963 | goto start; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5964 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5965 | case CTLENDARI: |
| 5966 | p--; |
| 5967 | expari(quotes); |
| 5968 | goto start; |
| 5969 | #endif |
| 5970 | } |
| 5971 | } |
| 5972 | breakloop: |
| 5973 | ; |
| 5974 | } |
| 5975 | |
| 5976 | static char * |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 5977 | 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] | 5978 | int zero) |
| 5979 | { |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5980 | // This commented out code was added by James Simmons <jsimmons@infradead.org> |
| 5981 | // as part of a larger change when he added support for ${var/a/b}. |
| 5982 | // However, it broke # and % operators: |
| 5983 | // |
| 5984 | //var=ababcdcd |
| 5985 | // ok bad |
| 5986 | //echo ${var#ab} abcdcd abcdcd |
| 5987 | //echo ${var##ab} abcdcd abcdcd |
| 5988 | //echo ${var#a*b} abcdcd ababcdcd (!) |
| 5989 | //echo ${var##a*b} cdcd cdcd |
| 5990 | //echo ${var#?} babcdcd ababcdcd (!) |
| 5991 | //echo ${var##?} babcdcd babcdcd |
| 5992 | //echo ${var#*} ababcdcd babcdcd (!) |
| 5993 | //echo ${var##*} |
| 5994 | //echo ${var%cd} ababcd ababcd |
| 5995 | //echo ${var%%cd} ababcd abab (!) |
| 5996 | //echo ${var%c*d} ababcd ababcd |
| 5997 | //echo ${var%%c*d} abab ababcdcd (!) |
| 5998 | //echo ${var%?} ababcdc ababcdc |
| 5999 | //echo ${var%%?} ababcdc ababcdcd (!) |
| 6000 | //echo ${var%*} ababcdcd ababcdcd |
| 6001 | //echo ${var%%*} |
| 6002 | // |
| 6003 | // Commenting it back out helped. Remove it completely if it really |
| 6004 | // is not needed. |
| 6005 | |
| 6006 | char *loc, *loc2; //, *full; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6007 | char c; |
| 6008 | |
| 6009 | loc = startp; |
| 6010 | loc2 = rmesc; |
| 6011 | do { |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6012 | int match; // = strlen(str); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6013 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6014 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6015 | c = *loc2; |
| 6016 | if (zero) { |
| 6017 | *loc2 = '\0'; |
| 6018 | s = rmesc; |
| 6019 | } |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6020 | match = pmatch(str, s); // this line was deleted |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6021 | |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6022 | // // chop off end if its '*' |
| 6023 | // full = strrchr(str, '*'); |
| 6024 | // if (full && full != str) |
| 6025 | // match--; |
| 6026 | // |
| 6027 | // // If str starts with '*' replace with s. |
| 6028 | // if ((*str == '*') && strlen(s) >= match) { |
| 6029 | // full = xstrdup(s); |
| 6030 | // strncpy(full+strlen(s)-match+1, str+1, match-1); |
| 6031 | // } else |
| 6032 | // full = xstrndup(str, match); |
| 6033 | // match = strncmp(s, full, strlen(full)); |
| 6034 | // free(full); |
| 6035 | // |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6036 | *loc2 = c; |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6037 | if (match) // if (!match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6038 | return loc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6039 | if (quotes && (unsigned char)*loc == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6040 | loc++; |
| 6041 | loc++; |
| 6042 | loc2++; |
| 6043 | } while (c); |
| 6044 | return 0; |
| 6045 | } |
| 6046 | |
| 6047 | static char * |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6048 | 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] | 6049 | { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6050 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6051 | int try2optimize = match_at_start; |
| 6052 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6053 | int esc = 0; |
| 6054 | char *loc; |
| 6055 | char *loc2; |
| 6056 | |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6057 | /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}": |
| 6058 | * startp="escaped_value_of_v" rmesc="raw_value_of_v" |
| 6059 | * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1 |
| 6060 | * Logic: |
| 6061 | * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc, |
| 6062 | * and on each iteration they go back two/one char until they reach the beginning. |
| 6063 | * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc. |
| 6064 | */ |
| 6065 | /* TODO: document in what other circumstances we are called. */ |
| 6066 | |
| 6067 | for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6068 | int match; |
| 6069 | char c = *loc2; |
| 6070 | const char *s = loc2; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6071 | if (match_at_start) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6072 | *loc2 = '\0'; |
| 6073 | s = rmesc; |
| 6074 | } |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6075 | match = pmatch(pattern, s); |
| 6076 | //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6077 | *loc2 = c; |
| 6078 | if (match) |
| 6079 | return loc; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6080 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6081 | if (try2optimize) { |
| 6082 | /* Maybe we can optimize this: |
| 6083 | * if pattern ends with unescaped *, we can avoid checking |
| 6084 | * shorter strings: if "foo*" doesnt match "raw_value_of_v", |
| 6085 | * it wont match truncated "raw_value_of_" strings too. |
| 6086 | */ |
| 6087 | unsigned plen = strlen(pattern); |
| 6088 | /* Does it end with "*"? */ |
| 6089 | if (plen != 0 && pattern[--plen] == '*') { |
| 6090 | /* "xxxx*" is not escaped */ |
| 6091 | /* "xxx\*" is escaped */ |
| 6092 | /* "xx\\*" is not escaped */ |
| 6093 | /* "x\\\*" is escaped */ |
| 6094 | int slashes = 0; |
| 6095 | while (plen != 0 && pattern[--plen] == '\\') |
| 6096 | slashes++; |
| 6097 | if (!(slashes & 1)) |
| 6098 | break; /* ends with unescaped "*" */ |
| 6099 | } |
| 6100 | try2optimize = 0; |
| 6101 | } |
| 6102 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6103 | loc--; |
| 6104 | if (quotes) { |
| 6105 | if (--esc < 0) { |
| 6106 | esc = esclen(startp, loc); |
| 6107 | } |
| 6108 | if (esc % 2) { |
| 6109 | esc--; |
| 6110 | loc--; |
| 6111 | } |
| 6112 | } |
| 6113 | } |
| 6114 | return 0; |
| 6115 | } |
| 6116 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6117 | static void varunset(const char *, const char *, const char *, int) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6118 | static void |
| 6119 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 6120 | { |
| 6121 | const char *msg; |
| 6122 | const char *tail; |
| 6123 | |
| 6124 | tail = nullstr; |
| 6125 | msg = "parameter not set"; |
| 6126 | if (umsg) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6127 | if ((unsigned char)*end == CTLENDVAR) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6128 | if (varflags & VSNUL) |
| 6129 | tail = " or null"; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6130 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6131 | msg = umsg; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6132 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6133 | } |
| 6134 | ash_msg_and_raise_error("%.*s: %s%s", end - var - 1, var, msg, tail); |
| 6135 | } |
| 6136 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6137 | #if ENABLE_ASH_BASH_COMPAT |
| 6138 | static char * |
| 6139 | parse_sub_pattern(char *arg, int inquotes) |
| 6140 | { |
| 6141 | char *idx, *repl = NULL; |
| 6142 | unsigned char c; |
| 6143 | |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6144 | idx = arg; |
| 6145 | while (1) { |
| 6146 | c = *arg; |
| 6147 | if (!c) |
| 6148 | break; |
| 6149 | if (c == '/') { |
| 6150 | /* Only the first '/' seen is our separator */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6151 | if (!repl) { |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6152 | repl = idx + 1; |
| 6153 | c = '\0'; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6154 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6155 | } |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6156 | *idx++ = c; |
| 6157 | if (!inquotes && c == '\\' && arg[1] == '\\') |
| 6158 | arg++; /* skip both \\, not just first one */ |
| 6159 | arg++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6160 | } |
Denis Vlasenko | 29038c0 | 2008-06-14 06:14:02 +0000 | [diff] [blame] | 6161 | *idx = c; /* NUL */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6162 | |
| 6163 | return repl; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6164 | } |
| 6165 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6166 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6167 | static const char * |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6168 | subevalvar(char *p, char *str, int strloc, int subtype, |
| 6169 | int startloc, int varflags, int quotes, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6170 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6171 | struct nodelist *saveargbackq = argbackq; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6172 | char *startp; |
| 6173 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6174 | char *rmesc, *rmescend; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6175 | IF_ASH_BASH_COMPAT(char *repl = NULL;) |
| 6176 | IF_ASH_BASH_COMPAT(char null = '\0';) |
| 6177 | IF_ASH_BASH_COMPAT(int pos, len, orig_len;) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6178 | int saveherefd = herefd; |
| 6179 | int amount, workloc, resetloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6180 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6181 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6182 | |
| 6183 | herefd = -1; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6184 | argstr(p, (subtype != VSASSIGN && subtype != VSQUESTION) ? EXP_CASE : 0, |
| 6185 | var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6186 | STPUTC('\0', expdest); |
| 6187 | herefd = saveherefd; |
| 6188 | argbackq = saveargbackq; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6189 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6190 | |
| 6191 | switch (subtype) { |
| 6192 | case VSASSIGN: |
| 6193 | setvar(str, startp, 0); |
| 6194 | amount = startp - expdest; |
| 6195 | STADJUST(amount, expdest); |
| 6196 | return startp; |
| 6197 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6198 | #if ENABLE_ASH_BASH_COMPAT |
| 6199 | case VSSUBSTR: |
| 6200 | loc = str = stackblock() + strloc; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6201 | /* Read POS in ${var:POS:LEN} */ |
| 6202 | pos = atoi(loc); /* number(loc) errors out on "1:4" */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6203 | len = str - startp - 1; |
| 6204 | |
| 6205 | /* *loc != '\0', guaranteed by parser */ |
| 6206 | if (quotes) { |
| 6207 | char *ptr; |
| 6208 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6209 | /* Adjust the length by the number of escapes */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6210 | for (ptr = startp; ptr < (str - 1); ptr++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6211 | if ((unsigned char)*ptr == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6212 | len--; |
| 6213 | ptr++; |
| 6214 | } |
| 6215 | } |
| 6216 | } |
| 6217 | orig_len = len; |
| 6218 | |
| 6219 | if (*loc++ == ':') { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6220 | /* ${var::LEN} */ |
| 6221 | len = number(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6222 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6223 | /* Skip POS in ${var:POS:LEN} */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6224 | len = orig_len; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6225 | while (*loc && *loc != ':') { |
| 6226 | /* TODO? |
| 6227 | * bash complains on: var=qwe; echo ${var:1a:123} |
| 6228 | if (!isdigit(*loc)) |
| 6229 | ash_msg_and_raise_error(msg_illnum, str); |
| 6230 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6231 | loc++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6232 | } |
| 6233 | if (*loc++ == ':') { |
| 6234 | len = number(loc); |
| 6235 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6236 | } |
| 6237 | if (pos >= orig_len) { |
| 6238 | pos = 0; |
| 6239 | len = 0; |
| 6240 | } |
| 6241 | if (len > (orig_len - pos)) |
| 6242 | len = orig_len - pos; |
| 6243 | |
| 6244 | for (str = startp; pos; str++, pos--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6245 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6246 | str++; |
| 6247 | } |
| 6248 | for (loc = startp; len; len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6249 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6250 | *loc++ = *str++; |
| 6251 | *loc++ = *str++; |
| 6252 | } |
| 6253 | *loc = '\0'; |
| 6254 | amount = loc - expdest; |
| 6255 | STADJUST(amount, expdest); |
| 6256 | return loc; |
| 6257 | #endif |
| 6258 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6259 | case VSQUESTION: |
| 6260 | varunset(p, str, startp, varflags); |
| 6261 | /* NOTREACHED */ |
| 6262 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6263 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6264 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6265 | /* We'll comeback here if we grow the stack while handling |
| 6266 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 6267 | * stack will need rebasing, and we'll need to remove our work |
| 6268 | * areas each time |
| 6269 | */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6270 | IF_ASH_BASH_COMPAT(restart:) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6271 | |
| 6272 | amount = expdest - ((char *)stackblock() + resetloc); |
| 6273 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6274 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6275 | |
| 6276 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6277 | rmescend = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6278 | if (quotes) { |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 6279 | rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6280 | if (rmesc != startp) { |
| 6281 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6282 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6283 | } |
| 6284 | } |
| 6285 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6286 | str = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6287 | preglob(str, varflags & VSQUOTE, 0); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6288 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6289 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6290 | #if ENABLE_ASH_BASH_COMPAT |
| 6291 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6292 | char *idx, *end; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6293 | |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 6294 | if (!repl) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6295 | repl = parse_sub_pattern(str, varflags & VSQUOTE); |
| 6296 | if (!repl) |
| 6297 | repl = &null; |
| 6298 | } |
| 6299 | |
| 6300 | /* If there's no pattern to match, return the expansion unmolested */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6301 | if (str[0] == '\0') |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6302 | return 0; |
| 6303 | |
| 6304 | len = 0; |
| 6305 | idx = startp; |
| 6306 | end = str - 1; |
| 6307 | while (idx < end) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6308 | try_to_match: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6309 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); |
| 6310 | if (!loc) { |
| 6311 | /* No match, advance */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6312 | char *restart_detect = stackblock(); |
| 6313 | skip_matching: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6314 | STPUTC(*idx, expdest); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6315 | if (quotes && (unsigned char)*idx == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6316 | idx++; |
| 6317 | len++; |
| 6318 | STPUTC(*idx, expdest); |
| 6319 | } |
| 6320 | if (stackblock() != restart_detect) |
| 6321 | goto restart; |
| 6322 | idx++; |
| 6323 | len++; |
| 6324 | rmesc++; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6325 | /* continue; - prone to quadratic behavior, smarter code: */ |
| 6326 | if (idx >= end) |
| 6327 | break; |
| 6328 | if (str[0] == '*') { |
| 6329 | /* Pattern is "*foo". If "*foo" does not match "long_string", |
| 6330 | * it would never match "ong_string" etc, no point in trying. |
| 6331 | */ |
| 6332 | goto skip_matching; |
| 6333 | } |
| 6334 | goto try_to_match; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6335 | } |
| 6336 | |
| 6337 | if (subtype == VSREPLACEALL) { |
| 6338 | while (idx < loc) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6339 | if (quotes && (unsigned char)*idx == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6340 | idx++; |
| 6341 | idx++; |
| 6342 | rmesc++; |
| 6343 | } |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6344 | } else { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6345 | idx = loc; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6346 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6347 | |
| 6348 | for (loc = repl; *loc; loc++) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6349 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6350 | STPUTC(*loc, expdest); |
| 6351 | if (stackblock() != restart_detect) |
| 6352 | goto restart; |
| 6353 | len++; |
| 6354 | } |
| 6355 | |
| 6356 | if (subtype == VSREPLACE) { |
| 6357 | while (*idx) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6358 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6359 | STPUTC(*idx, expdest); |
| 6360 | if (stackblock() != restart_detect) |
| 6361 | goto restart; |
| 6362 | len++; |
| 6363 | idx++; |
| 6364 | } |
| 6365 | break; |
| 6366 | } |
| 6367 | } |
| 6368 | |
| 6369 | /* We've put the replaced text into a buffer at workloc, now |
| 6370 | * move it to the right place and adjust the stack. |
| 6371 | */ |
| 6372 | startp = stackblock() + startloc; |
| 6373 | STPUTC('\0', expdest); |
| 6374 | memmove(startp, stackblock() + workloc, len); |
| 6375 | startp[len++] = '\0'; |
| 6376 | amount = expdest - ((char *)stackblock() + startloc + len - 1); |
| 6377 | STADJUST(-amount, expdest); |
| 6378 | return startp; |
| 6379 | } |
| 6380 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6381 | |
| 6382 | subtype -= VSTRIMRIGHT; |
| 6383 | #if DEBUG |
| 6384 | if (subtype < 0 || subtype > 7) |
| 6385 | abort(); |
| 6386 | #endif |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6387 | /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6388 | zero = subtype >> 1; |
| 6389 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 6390 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 6391 | |
| 6392 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 6393 | if (loc) { |
| 6394 | if (zero) { |
| 6395 | memmove(startp, loc, str - loc); |
| 6396 | loc = startp + (str - loc) - 1; |
| 6397 | } |
| 6398 | *loc = '\0'; |
| 6399 | amount = loc - expdest; |
| 6400 | STADJUST(amount, expdest); |
| 6401 | } |
| 6402 | return loc; |
| 6403 | } |
| 6404 | |
| 6405 | /* |
| 6406 | * Add the value of a specialized variable to the stack string. |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6407 | * name parameter (examples): |
| 6408 | * ash -c 'echo $1' name:'1=' |
| 6409 | * ash -c 'echo $qwe' name:'qwe=' |
| 6410 | * ash -c 'echo $$' name:'$=' |
| 6411 | * ash -c 'echo ${$}' name:'$=' |
| 6412 | * ash -c 'echo ${$##q}' name:'$=q' |
| 6413 | * ash -c 'echo ${#$}' name:'$=' |
| 6414 | * note: examples with bad shell syntax: |
| 6415 | * ash -c 'echo ${#$1}' name:'$=1' |
| 6416 | * ash -c 'echo ${#1#}' name:'1=#' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6417 | */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 6418 | static NOINLINE ssize_t |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6419 | varvalue(char *name, int varflags, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6420 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6421 | const char *p; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6422 | int num; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6423 | int i; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6424 | int sepq = 0; |
| 6425 | ssize_t len = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6426 | int subtype = varflags & VSTYPE; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6427 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6428 | int quoted = varflags & VSQUOTE; |
| 6429 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6430 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6431 | switch (*name) { |
| 6432 | case '$': |
| 6433 | num = rootpid; |
| 6434 | goto numvar; |
| 6435 | case '?': |
| 6436 | num = exitstatus; |
| 6437 | goto numvar; |
| 6438 | case '#': |
| 6439 | num = shellparam.nparam; |
| 6440 | goto numvar; |
| 6441 | case '!': |
| 6442 | num = backgndpid; |
| 6443 | if (num == 0) |
| 6444 | return -1; |
| 6445 | numvar: |
| 6446 | len = cvtnum(num); |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6447 | goto check_1char_name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6448 | case '-': |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6449 | expdest = makestrspace(NOPTS, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6450 | for (i = NOPTS - 1; i >= 0; i--) { |
| 6451 | if (optlist[i]) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6452 | USTPUTC(optletters(i), expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6453 | len++; |
| 6454 | } |
| 6455 | } |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6456 | check_1char_name: |
| 6457 | #if 0 |
| 6458 | /* handles cases similar to ${#$1} */ |
| 6459 | if (name[2] != '\0') |
| 6460 | raise_error_syntax("bad substitution"); |
| 6461 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6462 | break; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6463 | case '@': { |
| 6464 | char **ap; |
| 6465 | int sep; |
| 6466 | |
| 6467 | if (quoted && (flags & EXP_FULL)) { |
| 6468 | /* note: this is not meant as PEOF value */ |
| 6469 | sep = 1 << CHAR_BIT; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6470 | goto param; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6471 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6472 | /* fall through */ |
| 6473 | case '*': |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6474 | sep = ifsset() ? (unsigned char)(ifsval()[0]) : ' '; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6475 | i = SIT(sep, syntax); |
| 6476 | if (quotes && (i == CCTL || i == CBACK)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6477 | sepq = 1; |
| 6478 | param: |
| 6479 | ap = shellparam.p; |
| 6480 | if (!ap) |
| 6481 | return -1; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6482 | while ((p = *ap++) != NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6483 | size_t partlen; |
| 6484 | |
| 6485 | partlen = strlen(p); |
| 6486 | len += partlen; |
| 6487 | |
| 6488 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6489 | memtodest(p, partlen, syntax, quotes); |
| 6490 | |
| 6491 | if (*ap && sep) { |
| 6492 | char *q; |
| 6493 | |
| 6494 | len++; |
| 6495 | if (subtype == VSPLUS || subtype == VSLENGTH) { |
| 6496 | continue; |
| 6497 | } |
| 6498 | q = expdest; |
| 6499 | if (sepq) |
| 6500 | STPUTC(CTLESC, q); |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6501 | /* note: may put NUL despite sep != 0 |
| 6502 | * (see sep = 1 << CHAR_BIT above) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6503 | STPUTC(sep, q); |
| 6504 | expdest = q; |
| 6505 | } |
| 6506 | } |
| 6507 | return len; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6508 | } /* case '@' and '*' */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6509 | case '0': |
| 6510 | case '1': |
| 6511 | case '2': |
| 6512 | case '3': |
| 6513 | case '4': |
| 6514 | case '5': |
| 6515 | case '6': |
| 6516 | case '7': |
| 6517 | case '8': |
| 6518 | case '9': |
Denys Vlasenko | a00329c | 2009-08-30 20:05:10 +0200 | [diff] [blame] | 6519 | num = atoi(name); /* number(name) fails on ${N#str} etc */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6520 | if (num < 0 || num > shellparam.nparam) |
| 6521 | return -1; |
| 6522 | p = num ? shellparam.p[num - 1] : arg0; |
| 6523 | goto value; |
| 6524 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6525 | /* NB: name has form "VAR=..." */ |
| 6526 | |
| 6527 | /* "A=a B=$A" case: var_str_list is a list of "A=a" strings |
| 6528 | * which should be considered before we check variables. */ |
| 6529 | if (var_str_list) { |
| 6530 | unsigned name_len = (strchrnul(name, '=') - name) + 1; |
| 6531 | p = NULL; |
| 6532 | do { |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 6533 | char *str, *eq; |
| 6534 | str = var_str_list->text; |
| 6535 | eq = strchr(str, '='); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6536 | if (!eq) /* stop at first non-assignment */ |
| 6537 | break; |
| 6538 | eq++; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 6539 | if (name_len == (unsigned)(eq - str) |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6540 | && strncmp(str, name, name_len) == 0 |
| 6541 | ) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6542 | p = eq; |
| 6543 | /* goto value; - WRONG! */ |
| 6544 | /* think "A=1 A=2 B=$A" */ |
| 6545 | } |
| 6546 | var_str_list = var_str_list->next; |
| 6547 | } while (var_str_list); |
| 6548 | if (p) |
| 6549 | goto value; |
| 6550 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6551 | p = lookupvar(name); |
| 6552 | value: |
| 6553 | if (!p) |
| 6554 | return -1; |
| 6555 | |
| 6556 | len = strlen(p); |
| 6557 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6558 | memtodest(p, len, syntax, quotes); |
| 6559 | return len; |
| 6560 | } |
| 6561 | |
| 6562 | if (subtype == VSPLUS || subtype == VSLENGTH) |
| 6563 | STADJUST(-len, expdest); |
| 6564 | return len; |
| 6565 | } |
| 6566 | |
| 6567 | /* |
| 6568 | * Expand a variable, and return a pointer to the next character in the |
| 6569 | * input string. |
| 6570 | */ |
| 6571 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6572 | evalvar(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6573 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6574 | char varflags; |
| 6575 | char subtype; |
| 6576 | char quoted; |
| 6577 | char easy; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6578 | char *var; |
| 6579 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6580 | int startloc; |
| 6581 | ssize_t varlen; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6582 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6583 | varflags = (unsigned char) *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6584 | subtype = varflags & VSTYPE; |
| 6585 | quoted = varflags & VSQUOTE; |
| 6586 | var = p; |
| 6587 | easy = (!quoted || (*var == '@' && shellparam.nparam)); |
| 6588 | startloc = expdest - (char *)stackblock(); |
| 6589 | p = strchr(p, '=') + 1; |
| 6590 | |
| 6591 | again: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6592 | varlen = varvalue(var, varflags, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6593 | if (varflags & VSNUL) |
| 6594 | varlen--; |
| 6595 | |
| 6596 | if (subtype == VSPLUS) { |
| 6597 | varlen = -1 - varlen; |
| 6598 | goto vsplus; |
| 6599 | } |
| 6600 | |
| 6601 | if (subtype == VSMINUS) { |
| 6602 | vsplus: |
| 6603 | if (varlen < 0) { |
| 6604 | argstr( |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6605 | p, flags | EXP_TILDE | |
| 6606 | (quoted ? EXP_QWORD : EXP_WORD), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6607 | var_str_list |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6608 | ); |
| 6609 | goto end; |
| 6610 | } |
| 6611 | if (easy) |
| 6612 | goto record; |
| 6613 | goto end; |
| 6614 | } |
| 6615 | |
| 6616 | if (subtype == VSASSIGN || subtype == VSQUESTION) { |
| 6617 | if (varlen < 0) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6618 | if (subevalvar(p, var, /* strloc: */ 0, |
| 6619 | subtype, startloc, varflags, |
| 6620 | /* quotes: */ 0, |
| 6621 | var_str_list) |
| 6622 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6623 | varflags &= ~VSNUL; |
| 6624 | /* |
| 6625 | * Remove any recorded regions beyond |
| 6626 | * start of variable |
| 6627 | */ |
| 6628 | removerecordregions(startloc); |
| 6629 | goto again; |
| 6630 | } |
| 6631 | goto end; |
| 6632 | } |
| 6633 | if (easy) |
| 6634 | goto record; |
| 6635 | goto end; |
| 6636 | } |
| 6637 | |
| 6638 | if (varlen < 0 && uflag) |
| 6639 | varunset(p, var, 0, 0); |
| 6640 | |
| 6641 | if (subtype == VSLENGTH) { |
| 6642 | cvtnum(varlen > 0 ? varlen : 0); |
| 6643 | goto record; |
| 6644 | } |
| 6645 | |
| 6646 | if (subtype == VSNORMAL) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6647 | if (easy) |
| 6648 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6649 | goto end; |
| 6650 | } |
| 6651 | |
| 6652 | #if DEBUG |
| 6653 | switch (subtype) { |
| 6654 | case VSTRIMLEFT: |
| 6655 | case VSTRIMLEFTMAX: |
| 6656 | case VSTRIMRIGHT: |
| 6657 | case VSTRIMRIGHTMAX: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6658 | #if ENABLE_ASH_BASH_COMPAT |
| 6659 | case VSSUBSTR: |
| 6660 | case VSREPLACE: |
| 6661 | case VSREPLACEALL: |
| 6662 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6663 | break; |
| 6664 | default: |
| 6665 | abort(); |
| 6666 | } |
| 6667 | #endif |
| 6668 | |
| 6669 | if (varlen >= 0) { |
| 6670 | /* |
| 6671 | * Terminate the string and start recording the pattern |
| 6672 | * right after it |
| 6673 | */ |
| 6674 | STPUTC('\0', expdest); |
| 6675 | patloc = expdest - (char *)stackblock(); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6676 | if (0 == subevalvar(p, /* str: */ NULL, patloc, subtype, |
| 6677 | startloc, varflags, |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6678 | //TODO: | EXP_REDIR too? All other such places do it too |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6679 | /* quotes: */ flags & (EXP_FULL | EXP_CASE), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6680 | var_str_list) |
| 6681 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6682 | int amount = expdest - ( |
| 6683 | (char *)stackblock() + patloc - 1 |
| 6684 | ); |
| 6685 | STADJUST(-amount, expdest); |
| 6686 | } |
| 6687 | /* Remove any recorded regions beyond start of variable */ |
| 6688 | removerecordregions(startloc); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6689 | record: |
| 6690 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6691 | } |
| 6692 | |
| 6693 | end: |
| 6694 | if (subtype != VSNORMAL) { /* skip to end of alternative */ |
| 6695 | int nesting = 1; |
| 6696 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6697 | unsigned char c = *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6698 | if (c == CTLESC) |
| 6699 | p++; |
| 6700 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { |
| 6701 | if (varlen >= 0) |
| 6702 | argbackq = argbackq->next; |
| 6703 | } else if (c == CTLVAR) { |
| 6704 | if ((*p++ & VSTYPE) != VSNORMAL) |
| 6705 | nesting++; |
| 6706 | } else if (c == CTLENDVAR) { |
| 6707 | if (--nesting == 0) |
| 6708 | break; |
| 6709 | } |
| 6710 | } |
| 6711 | } |
| 6712 | return p; |
| 6713 | } |
| 6714 | |
| 6715 | /* |
| 6716 | * Break the argument string into pieces based upon IFS and add the |
| 6717 | * strings to the argument list. The regions of the string to be |
| 6718 | * searched for IFS characters have been stored by recordregion. |
| 6719 | */ |
| 6720 | static void |
| 6721 | ifsbreakup(char *string, struct arglist *arglist) |
| 6722 | { |
| 6723 | struct ifsregion *ifsp; |
| 6724 | struct strlist *sp; |
| 6725 | char *start; |
| 6726 | char *p; |
| 6727 | char *q; |
| 6728 | const char *ifs, *realifs; |
| 6729 | int ifsspc; |
| 6730 | int nulonly; |
| 6731 | |
| 6732 | start = string; |
| 6733 | if (ifslastp != NULL) { |
| 6734 | ifsspc = 0; |
| 6735 | nulonly = 0; |
| 6736 | realifs = ifsset() ? ifsval() : defifs; |
| 6737 | ifsp = &ifsfirst; |
| 6738 | do { |
| 6739 | p = string + ifsp->begoff; |
| 6740 | nulonly = ifsp->nulonly; |
| 6741 | ifs = nulonly ? nullstr : realifs; |
| 6742 | ifsspc = 0; |
| 6743 | while (p < string + ifsp->endoff) { |
| 6744 | q = p; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6745 | if ((unsigned char)*p == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6746 | p++; |
| 6747 | if (!strchr(ifs, *p)) { |
| 6748 | p++; |
| 6749 | continue; |
| 6750 | } |
| 6751 | if (!nulonly) |
| 6752 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6753 | /* Ignore IFS whitespace at start */ |
| 6754 | if (q == start && ifsspc) { |
| 6755 | p++; |
| 6756 | start = p; |
| 6757 | continue; |
| 6758 | } |
| 6759 | *q = '\0'; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6760 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6761 | sp->text = start; |
| 6762 | *arglist->lastp = sp; |
| 6763 | arglist->lastp = &sp->next; |
| 6764 | p++; |
| 6765 | if (!nulonly) { |
| 6766 | for (;;) { |
| 6767 | if (p >= string + ifsp->endoff) { |
| 6768 | break; |
| 6769 | } |
| 6770 | q = p; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6771 | if ((unsigned char)*p == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6772 | p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6773 | if (strchr(ifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6774 | p = q; |
| 6775 | break; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6776 | } |
| 6777 | if (strchr(defifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6778 | if (ifsspc) { |
| 6779 | p++; |
| 6780 | ifsspc = 0; |
| 6781 | } else { |
| 6782 | p = q; |
| 6783 | break; |
| 6784 | } |
| 6785 | } else |
| 6786 | p++; |
| 6787 | } |
| 6788 | } |
| 6789 | start = p; |
| 6790 | } /* while */ |
| 6791 | ifsp = ifsp->next; |
| 6792 | } while (ifsp != NULL); |
| 6793 | if (nulonly) |
| 6794 | goto add; |
| 6795 | } |
| 6796 | |
| 6797 | if (!*start) |
| 6798 | return; |
| 6799 | |
| 6800 | add: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6801 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6802 | sp->text = start; |
| 6803 | *arglist->lastp = sp; |
| 6804 | arglist->lastp = &sp->next; |
| 6805 | } |
| 6806 | |
| 6807 | static void |
| 6808 | ifsfree(void) |
| 6809 | { |
| 6810 | struct ifsregion *p; |
| 6811 | |
| 6812 | INT_OFF; |
| 6813 | p = ifsfirst.next; |
| 6814 | do { |
| 6815 | struct ifsregion *ifsp; |
| 6816 | ifsp = p->next; |
| 6817 | free(p); |
| 6818 | p = ifsp; |
| 6819 | } while (p); |
| 6820 | ifslastp = NULL; |
| 6821 | ifsfirst.next = NULL; |
| 6822 | INT_ON; |
| 6823 | } |
| 6824 | |
| 6825 | /* |
| 6826 | * Add a file name to the list. |
| 6827 | */ |
| 6828 | static void |
| 6829 | addfname(const char *name) |
| 6830 | { |
| 6831 | struct strlist *sp; |
| 6832 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6833 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6834 | sp->text = ststrdup(name); |
| 6835 | *exparg.lastp = sp; |
| 6836 | exparg.lastp = &sp->next; |
| 6837 | } |
| 6838 | |
| 6839 | static char *expdir; |
| 6840 | |
| 6841 | /* |
| 6842 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 6843 | */ |
| 6844 | static void |
| 6845 | expmeta(char *enddir, char *name) |
| 6846 | { |
| 6847 | char *p; |
| 6848 | const char *cp; |
| 6849 | char *start; |
| 6850 | char *endname; |
| 6851 | int metaflag; |
| 6852 | struct stat statb; |
| 6853 | DIR *dirp; |
| 6854 | struct dirent *dp; |
| 6855 | int atend; |
| 6856 | int matchdot; |
| 6857 | |
| 6858 | metaflag = 0; |
| 6859 | start = name; |
| 6860 | for (p = name; *p; p++) { |
| 6861 | if (*p == '*' || *p == '?') |
| 6862 | metaflag = 1; |
| 6863 | else if (*p == '[') { |
| 6864 | char *q = p + 1; |
| 6865 | if (*q == '!') |
| 6866 | q++; |
| 6867 | for (;;) { |
| 6868 | if (*q == '\\') |
| 6869 | q++; |
| 6870 | if (*q == '/' || *q == '\0') |
| 6871 | break; |
| 6872 | if (*++q == ']') { |
| 6873 | metaflag = 1; |
| 6874 | break; |
| 6875 | } |
| 6876 | } |
| 6877 | } else if (*p == '\\') |
| 6878 | p++; |
| 6879 | else if (*p == '/') { |
| 6880 | if (metaflag) |
| 6881 | goto out; |
| 6882 | start = p + 1; |
| 6883 | } |
| 6884 | } |
| 6885 | out: |
| 6886 | if (metaflag == 0) { /* we've reached the end of the file name */ |
| 6887 | if (enddir != expdir) |
| 6888 | metaflag++; |
| 6889 | p = name; |
| 6890 | do { |
| 6891 | if (*p == '\\') |
| 6892 | p++; |
| 6893 | *enddir++ = *p; |
| 6894 | } while (*p++); |
| 6895 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
| 6896 | addfname(expdir); |
| 6897 | return; |
| 6898 | } |
| 6899 | endname = p; |
| 6900 | if (name < start) { |
| 6901 | p = name; |
| 6902 | do { |
| 6903 | if (*p == '\\') |
| 6904 | p++; |
| 6905 | *enddir++ = *p++; |
| 6906 | } while (p < start); |
| 6907 | } |
| 6908 | if (enddir == expdir) { |
| 6909 | cp = "."; |
| 6910 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 6911 | cp = "/"; |
| 6912 | } else { |
| 6913 | cp = expdir; |
| 6914 | enddir[-1] = '\0'; |
| 6915 | } |
| 6916 | dirp = opendir(cp); |
| 6917 | if (dirp == NULL) |
| 6918 | return; |
| 6919 | if (enddir != expdir) |
| 6920 | enddir[-1] = '/'; |
| 6921 | if (*endname == 0) { |
| 6922 | atend = 1; |
| 6923 | } else { |
| 6924 | atend = 0; |
| 6925 | *endname++ = '\0'; |
| 6926 | } |
| 6927 | matchdot = 0; |
| 6928 | p = start; |
| 6929 | if (*p == '\\') |
| 6930 | p++; |
| 6931 | if (*p == '.') |
| 6932 | matchdot++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6933 | while (!pending_int && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 6934 | if (dp->d_name[0] == '.' && !matchdot) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6935 | continue; |
| 6936 | if (pmatch(start, dp->d_name)) { |
| 6937 | if (atend) { |
| 6938 | strcpy(enddir, dp->d_name); |
| 6939 | addfname(expdir); |
| 6940 | } else { |
| 6941 | for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';) |
| 6942 | continue; |
| 6943 | p[-1] = '/'; |
| 6944 | expmeta(p, endname); |
| 6945 | } |
| 6946 | } |
| 6947 | } |
| 6948 | closedir(dirp); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 6949 | if (!atend) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6950 | endname[-1] = '/'; |
| 6951 | } |
| 6952 | |
| 6953 | static struct strlist * |
| 6954 | msort(struct strlist *list, int len) |
| 6955 | { |
| 6956 | struct strlist *p, *q = NULL; |
| 6957 | struct strlist **lpp; |
| 6958 | int half; |
| 6959 | int n; |
| 6960 | |
| 6961 | if (len <= 1) |
| 6962 | return list; |
| 6963 | half = len >> 1; |
| 6964 | p = list; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6965 | for (n = half; --n >= 0;) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6966 | q = p; |
| 6967 | p = p->next; |
| 6968 | } |
| 6969 | q->next = NULL; /* terminate first half of list */ |
| 6970 | q = msort(list, half); /* sort first half of list */ |
| 6971 | p = msort(p, len - half); /* sort second half */ |
| 6972 | lpp = &list; |
| 6973 | for (;;) { |
| 6974 | #if ENABLE_LOCALE_SUPPORT |
| 6975 | if (strcoll(p->text, q->text) < 0) |
| 6976 | #else |
| 6977 | if (strcmp(p->text, q->text) < 0) |
| 6978 | #endif |
| 6979 | { |
| 6980 | *lpp = p; |
| 6981 | lpp = &p->next; |
| 6982 | p = *lpp; |
| 6983 | if (p == NULL) { |
| 6984 | *lpp = q; |
| 6985 | break; |
| 6986 | } |
| 6987 | } else { |
| 6988 | *lpp = q; |
| 6989 | lpp = &q->next; |
| 6990 | q = *lpp; |
| 6991 | if (q == NULL) { |
| 6992 | *lpp = p; |
| 6993 | break; |
| 6994 | } |
| 6995 | } |
| 6996 | } |
| 6997 | return list; |
| 6998 | } |
| 6999 | |
| 7000 | /* |
| 7001 | * Sort the results of file name expansion. It calculates the number of |
| 7002 | * strings to sort and then calls msort (short for merge sort) to do the |
| 7003 | * work. |
| 7004 | */ |
| 7005 | static struct strlist * |
| 7006 | expsort(struct strlist *str) |
| 7007 | { |
| 7008 | int len; |
| 7009 | struct strlist *sp; |
| 7010 | |
| 7011 | len = 0; |
| 7012 | for (sp = str; sp; sp = sp->next) |
| 7013 | len++; |
| 7014 | return msort(str, len); |
| 7015 | } |
| 7016 | |
| 7017 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7018 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7019 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 7020 | static const char metachars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7021 | '*', '?', '[', 0 |
| 7022 | }; |
| 7023 | /* TODO - EXP_REDIR */ |
| 7024 | |
| 7025 | while (str) { |
| 7026 | struct strlist **savelastp; |
| 7027 | struct strlist *sp; |
| 7028 | char *p; |
| 7029 | |
| 7030 | if (fflag) |
| 7031 | goto nometa; |
| 7032 | if (!strpbrk(str->text, metachars)) |
| 7033 | goto nometa; |
| 7034 | savelastp = exparg.lastp; |
| 7035 | |
| 7036 | INT_OFF; |
| 7037 | p = preglob(str->text, 0, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
| 7038 | { |
| 7039 | int i = strlen(str->text); |
| 7040 | expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ |
| 7041 | } |
| 7042 | |
| 7043 | expmeta(expdir, p); |
| 7044 | free(expdir); |
| 7045 | if (p != str->text) |
| 7046 | free(p); |
| 7047 | INT_ON; |
| 7048 | if (exparg.lastp == savelastp) { |
| 7049 | /* |
| 7050 | * no matches |
| 7051 | */ |
| 7052 | nometa: |
| 7053 | *exparg.lastp = str; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7054 | rmescapes(str->text, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7055 | exparg.lastp = &str->next; |
| 7056 | } else { |
| 7057 | *exparg.lastp = NULL; |
| 7058 | *savelastp = sp = expsort(*savelastp); |
| 7059 | while (sp->next != NULL) |
| 7060 | sp = sp->next; |
| 7061 | exparg.lastp = &sp->next; |
| 7062 | } |
| 7063 | str = str->next; |
| 7064 | } |
| 7065 | } |
| 7066 | |
| 7067 | /* |
| 7068 | * Perform variable substitution and command substitution on an argument, |
| 7069 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 7070 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 7071 | * here document expansion. |
| 7072 | */ |
| 7073 | static void |
| 7074 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 7075 | { |
| 7076 | struct strlist *sp; |
| 7077 | char *p; |
| 7078 | |
| 7079 | argbackq = arg->narg.backquote; |
| 7080 | STARTSTACKSTR(expdest); |
| 7081 | ifsfirst.next = NULL; |
| 7082 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7083 | argstr(arg->narg.text, flag, |
| 7084 | /* var_str_list: */ arglist ? arglist->list : NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7085 | p = _STPUTC('\0', expdest); |
| 7086 | expdest = p - 1; |
| 7087 | if (arglist == NULL) { |
| 7088 | return; /* here document expanded */ |
| 7089 | } |
| 7090 | p = grabstackstr(p); |
| 7091 | exparg.lastp = &exparg.list; |
| 7092 | /* |
| 7093 | * TODO - EXP_REDIR |
| 7094 | */ |
| 7095 | if (flag & EXP_FULL) { |
| 7096 | ifsbreakup(p, &exparg); |
| 7097 | *exparg.lastp = NULL; |
| 7098 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7099 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7100 | } else { |
| 7101 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7102 | rmescapes(p, 0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7103 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7104 | sp->text = p; |
| 7105 | *exparg.lastp = sp; |
| 7106 | exparg.lastp = &sp->next; |
| 7107 | } |
| 7108 | if (ifsfirst.next) |
| 7109 | ifsfree(); |
| 7110 | *exparg.lastp = NULL; |
| 7111 | if (exparg.list) { |
| 7112 | *arglist->lastp = exparg.list; |
| 7113 | arglist->lastp = exparg.lastp; |
| 7114 | } |
| 7115 | } |
| 7116 | |
| 7117 | /* |
| 7118 | * Expand shell variables and backquotes inside a here document. |
| 7119 | */ |
| 7120 | static void |
| 7121 | expandhere(union node *arg, int fd) |
| 7122 | { |
| 7123 | herefd = fd; |
| 7124 | expandarg(arg, (struct arglist *)NULL, 0); |
| 7125 | full_write(fd, stackblock(), expdest - (char *)stackblock()); |
| 7126 | } |
| 7127 | |
| 7128 | /* |
| 7129 | * Returns true if the pattern matches the string. |
| 7130 | */ |
| 7131 | static int |
| 7132 | patmatch(char *pattern, const char *string) |
| 7133 | { |
| 7134 | return pmatch(preglob(pattern, 0, 0), string); |
| 7135 | } |
| 7136 | |
| 7137 | /* |
| 7138 | * See if a pattern matches in a case statement. |
| 7139 | */ |
| 7140 | static int |
| 7141 | casematch(union node *pattern, char *val) |
| 7142 | { |
| 7143 | struct stackmark smark; |
| 7144 | int result; |
| 7145 | |
| 7146 | setstackmark(&smark); |
| 7147 | argbackq = pattern->narg.backquote; |
| 7148 | STARTSTACKSTR(expdest); |
| 7149 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7150 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE, |
| 7151 | /* var_str_list: */ NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7152 | STACKSTRNUL(expdest); |
| 7153 | result = patmatch(stackblock(), val); |
| 7154 | popstackmark(&smark); |
| 7155 | return result; |
| 7156 | } |
| 7157 | |
| 7158 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7159 | /* ============ find_command */ |
| 7160 | |
| 7161 | struct builtincmd { |
| 7162 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7163 | int (*builtin)(int, char **) FAST_FUNC; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7164 | /* unsigned flags; */ |
| 7165 | }; |
| 7166 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 7167 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7168 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7169 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7170 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7171 | |
| 7172 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7173 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7174 | union param { |
| 7175 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7176 | /* index >= 0 for commands without path (slashes) */ |
| 7177 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 7178 | /* index == -1 for commands with slashes */ |
| 7179 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7180 | const struct builtincmd *cmd; |
| 7181 | struct funcnode *func; |
| 7182 | } u; |
| 7183 | }; |
| 7184 | /* values of cmdtype */ |
| 7185 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 7186 | #define CMDNORMAL 0 /* command is an executable program */ |
| 7187 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 7188 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 7189 | |
| 7190 | /* action to find_command() */ |
| 7191 | #define DO_ERR 0x01 /* prints errors */ |
| 7192 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 7193 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 7194 | #define DO_ALTPATH 0x08 /* using alternate path */ |
| 7195 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ |
| 7196 | |
| 7197 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 7198 | |
| 7199 | |
| 7200 | /* ============ Hashing commands */ |
| 7201 | |
| 7202 | /* |
| 7203 | * When commands are first encountered, they are entered in a hash table. |
| 7204 | * This ensures that a full path search will not have to be done for them |
| 7205 | * on each invocation. |
| 7206 | * |
| 7207 | * We should investigate converting to a linear search, even though that |
| 7208 | * would make the command name "hash" a misnomer. |
| 7209 | */ |
| 7210 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7211 | struct tblentry { |
| 7212 | struct tblentry *next; /* next entry in hash chain */ |
| 7213 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7214 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7215 | char rehash; /* if set, cd done since entry created */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7216 | char cmdname[1]; /* name of command */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7217 | }; |
| 7218 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 7219 | static struct tblentry **cmdtable; |
| 7220 | #define INIT_G_cmdtable() do { \ |
| 7221 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 7222 | } while (0) |
| 7223 | |
| 7224 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 7225 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7226 | |
| 7227 | static void |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7228 | 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] | 7229 | { |
| 7230 | int repeated = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7231 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7232 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7233 | if (applet_no >= 0) { |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7234 | if (APPLET_IS_NOEXEC(applet_no)) { |
| 7235 | while (*envp) |
| 7236 | putenv(*envp++); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7237 | run_applet_no_and_exit(applet_no, argv); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7238 | } |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7239 | /* re-exec ourselves with the new arguments */ |
| 7240 | execve(bb_busybox_exec_path, argv, envp); |
| 7241 | /* If they called chroot or otherwise made the binary no longer |
| 7242 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7243 | } |
| 7244 | #endif |
| 7245 | |
| 7246 | repeat: |
| 7247 | #ifdef SYSV |
| 7248 | do { |
| 7249 | execve(cmd, argv, envp); |
| 7250 | } while (errno == EINTR); |
| 7251 | #else |
| 7252 | execve(cmd, argv, envp); |
| 7253 | #endif |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7254 | if (repeated) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7255 | free(argv); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7256 | return; |
| 7257 | } |
| 7258 | if (errno == ENOEXEC) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7259 | char **ap; |
| 7260 | char **new; |
| 7261 | |
| 7262 | for (ap = argv; *ap; ap++) |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7263 | continue; |
| 7264 | ap = new = ckmalloc((ap - argv + 2) * sizeof(ap[0])); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7265 | ap[1] = cmd; |
Denis Vlasenko | c44ab01 | 2007-04-09 03:11:58 +0000 | [diff] [blame] | 7266 | ap[0] = cmd = (char *)DEFAULT_SHELL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7267 | ap += 2; |
| 7268 | argv++; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7269 | while ((*ap++ = *argv++) != NULL) |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7270 | continue; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7271 | argv = new; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7272 | repeated++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7273 | goto repeat; |
| 7274 | } |
| 7275 | } |
| 7276 | |
| 7277 | /* |
| 7278 | * Exec a program. Never returns. If you change this routine, you may |
| 7279 | * have to change the find_command routine as well. |
| 7280 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7281 | static void shellexec(char **, const char *, int) NORETURN; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7282 | static void |
| 7283 | shellexec(char **argv, const char *path, int idx) |
| 7284 | { |
| 7285 | char *cmdname; |
| 7286 | int e; |
| 7287 | char **envp; |
| 7288 | int exerrno; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7289 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7290 | int applet_no = -1; |
| 7291 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7292 | |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 7293 | clearredir(/*drop:*/ 1); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7294 | envp = listvars(VEXPORT, VUNSET, 0); |
| 7295 | if (strchr(argv[0], '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7296 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7297 | || (applet_no = find_applet_by_name(argv[0])) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7298 | #endif |
| 7299 | ) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7300 | tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7301 | e = errno; |
| 7302 | } else { |
| 7303 | e = ENOENT; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7304 | while ((cmdname = path_advance(&path, argv[0])) != NULL) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7305 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7306 | tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7307 | if (errno != ENOENT && errno != ENOTDIR) |
| 7308 | e = errno; |
| 7309 | } |
| 7310 | stunalloc(cmdname); |
| 7311 | } |
| 7312 | } |
| 7313 | |
| 7314 | /* Map to POSIX errors */ |
| 7315 | switch (e) { |
| 7316 | case EACCES: |
| 7317 | exerrno = 126; |
| 7318 | break; |
| 7319 | case ENOENT: |
| 7320 | exerrno = 127; |
| 7321 | break; |
| 7322 | default: |
| 7323 | exerrno = 2; |
| 7324 | break; |
| 7325 | } |
| 7326 | exitstatus = exerrno; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7327 | TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n", |
| 7328 | argv[0], e, suppress_int)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7329 | ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found")); |
| 7330 | /* NOTREACHED */ |
| 7331 | } |
| 7332 | |
| 7333 | static void |
| 7334 | printentry(struct tblentry *cmdp) |
| 7335 | { |
| 7336 | int idx; |
| 7337 | const char *path; |
| 7338 | char *name; |
| 7339 | |
| 7340 | idx = cmdp->param.index; |
| 7341 | path = pathval(); |
| 7342 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7343 | name = path_advance(&path, cmdp->cmdname); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7344 | stunalloc(name); |
| 7345 | } while (--idx >= 0); |
| 7346 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 7347 | } |
| 7348 | |
| 7349 | /* |
| 7350 | * Clear out command entries. The argument specifies the first entry in |
| 7351 | * PATH which has changed. |
| 7352 | */ |
| 7353 | static void |
| 7354 | clearcmdentry(int firstchange) |
| 7355 | { |
| 7356 | struct tblentry **tblp; |
| 7357 | struct tblentry **pp; |
| 7358 | struct tblentry *cmdp; |
| 7359 | |
| 7360 | INT_OFF; |
| 7361 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 7362 | pp = tblp; |
| 7363 | while ((cmdp = *pp) != NULL) { |
| 7364 | if ((cmdp->cmdtype == CMDNORMAL && |
| 7365 | cmdp->param.index >= firstchange) |
| 7366 | || (cmdp->cmdtype == CMDBUILTIN && |
| 7367 | builtinloc >= firstchange) |
| 7368 | ) { |
| 7369 | *pp = cmdp->next; |
| 7370 | free(cmdp); |
| 7371 | } else { |
| 7372 | pp = &cmdp->next; |
| 7373 | } |
| 7374 | } |
| 7375 | } |
| 7376 | INT_ON; |
| 7377 | } |
| 7378 | |
| 7379 | /* |
| 7380 | * Locate a command in the command hash table. If "add" is nonzero, |
| 7381 | * add the command to the table if it is not already present. The |
| 7382 | * variable "lastcmdentry" is set to point to the address of the link |
| 7383 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 7384 | * entry. |
| 7385 | * |
| 7386 | * Interrupts must be off if called with add != 0. |
| 7387 | */ |
| 7388 | static struct tblentry **lastcmdentry; |
| 7389 | |
| 7390 | static struct tblentry * |
| 7391 | cmdlookup(const char *name, int add) |
| 7392 | { |
| 7393 | unsigned int hashval; |
| 7394 | const char *p; |
| 7395 | struct tblentry *cmdp; |
| 7396 | struct tblentry **pp; |
| 7397 | |
| 7398 | p = name; |
| 7399 | hashval = (unsigned char)*p << 4; |
| 7400 | while (*p) |
| 7401 | hashval += (unsigned char)*p++; |
| 7402 | hashval &= 0x7FFF; |
| 7403 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 7404 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7405 | if (strcmp(cmdp->cmdname, name) == 0) |
| 7406 | break; |
| 7407 | pp = &cmdp->next; |
| 7408 | } |
| 7409 | if (add && cmdp == NULL) { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7410 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) |
| 7411 | + strlen(name) |
| 7412 | /* + 1 - already done because |
| 7413 | * tblentry::cmdname is char[1] */); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7414 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7415 | cmdp->cmdtype = CMDUNKNOWN; |
| 7416 | strcpy(cmdp->cmdname, name); |
| 7417 | } |
| 7418 | lastcmdentry = pp; |
| 7419 | return cmdp; |
| 7420 | } |
| 7421 | |
| 7422 | /* |
| 7423 | * Delete the command entry returned on the last lookup. |
| 7424 | */ |
| 7425 | static void |
| 7426 | delete_cmd_entry(void) |
| 7427 | { |
| 7428 | struct tblentry *cmdp; |
| 7429 | |
| 7430 | INT_OFF; |
| 7431 | cmdp = *lastcmdentry; |
| 7432 | *lastcmdentry = cmdp->next; |
| 7433 | if (cmdp->cmdtype == CMDFUNCTION) |
| 7434 | freefunc(cmdp->param.func); |
| 7435 | free(cmdp); |
| 7436 | INT_ON; |
| 7437 | } |
| 7438 | |
| 7439 | /* |
| 7440 | * Add a new command entry, replacing any existing command entry for |
| 7441 | * the same name - except special builtins. |
| 7442 | */ |
| 7443 | static void |
| 7444 | addcmdentry(char *name, struct cmdentry *entry) |
| 7445 | { |
| 7446 | struct tblentry *cmdp; |
| 7447 | |
| 7448 | cmdp = cmdlookup(name, 1); |
| 7449 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 7450 | freefunc(cmdp->param.func); |
| 7451 | } |
| 7452 | cmdp->cmdtype = entry->cmdtype; |
| 7453 | cmdp->param = entry->u; |
| 7454 | cmdp->rehash = 0; |
| 7455 | } |
| 7456 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7457 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7458 | hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7459 | { |
| 7460 | struct tblentry **pp; |
| 7461 | struct tblentry *cmdp; |
| 7462 | int c; |
| 7463 | struct cmdentry entry; |
| 7464 | char *name; |
| 7465 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7466 | if (nextopt("r") != '\0') { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7467 | clearcmdentry(0); |
| 7468 | return 0; |
| 7469 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7470 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7471 | if (*argptr == NULL) { |
| 7472 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7473 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7474 | if (cmdp->cmdtype == CMDNORMAL) |
| 7475 | printentry(cmdp); |
| 7476 | } |
| 7477 | } |
| 7478 | return 0; |
| 7479 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7480 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7481 | c = 0; |
| 7482 | while ((name = *argptr) != NULL) { |
| 7483 | cmdp = cmdlookup(name, 0); |
| 7484 | if (cmdp != NULL |
| 7485 | && (cmdp->cmdtype == CMDNORMAL |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7486 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)) |
| 7487 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7488 | delete_cmd_entry(); |
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 | find_command(name, &entry, DO_ERR, pathval()); |
| 7491 | if (entry.cmdtype == CMDUNKNOWN) |
| 7492 | c = 1; |
| 7493 | argptr++; |
| 7494 | } |
| 7495 | return c; |
| 7496 | } |
| 7497 | |
| 7498 | /* |
| 7499 | * Called when a cd is done. Marks all commands so the next time they |
| 7500 | * are executed they will be rehashed. |
| 7501 | */ |
| 7502 | static void |
| 7503 | hashcd(void) |
| 7504 | { |
| 7505 | struct tblentry **pp; |
| 7506 | struct tblentry *cmdp; |
| 7507 | |
| 7508 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7509 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7510 | if (cmdp->cmdtype == CMDNORMAL |
| 7511 | || (cmdp->cmdtype == CMDBUILTIN |
| 7512 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 7513 | && builtinloc > 0) |
| 7514 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7515 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7516 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7517 | } |
| 7518 | } |
| 7519 | } |
| 7520 | |
| 7521 | /* |
| 7522 | * Fix command hash table when PATH changed. |
| 7523 | * Called before PATH is changed. The argument is the new value of PATH; |
| 7524 | * pathval() still returns the old value at this point. |
| 7525 | * Called with interrupts off. |
| 7526 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7527 | static void FAST_FUNC |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7528 | changepath(const char *new) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7529 | { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7530 | const char *old; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7531 | int firstchange; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7532 | int idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7533 | int idx_bltin; |
| 7534 | |
| 7535 | old = pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7536 | firstchange = 9999; /* assume no change */ |
| 7537 | idx = 0; |
| 7538 | idx_bltin = -1; |
| 7539 | for (;;) { |
| 7540 | if (*old != *new) { |
| 7541 | firstchange = idx; |
| 7542 | if ((*old == '\0' && *new == ':') |
| 7543 | || (*old == ':' && *new == '\0')) |
| 7544 | firstchange++; |
| 7545 | old = new; /* ignore subsequent differences */ |
| 7546 | } |
| 7547 | if (*new == '\0') |
| 7548 | break; |
| 7549 | if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin")) |
| 7550 | idx_bltin = idx; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7551 | if (*new == ':') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7552 | idx++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7553 | new++, old++; |
| 7554 | } |
| 7555 | if (builtinloc < 0 && idx_bltin >= 0) |
| 7556 | builtinloc = idx_bltin; /* zap builtins */ |
| 7557 | if (builtinloc >= 0 && idx_bltin < 0) |
| 7558 | firstchange = 0; |
| 7559 | clearcmdentry(firstchange); |
| 7560 | builtinloc = idx_bltin; |
| 7561 | } |
| 7562 | |
| 7563 | #define TEOF 0 |
| 7564 | #define TNL 1 |
| 7565 | #define TREDIR 2 |
| 7566 | #define TWORD 3 |
| 7567 | #define TSEMI 4 |
| 7568 | #define TBACKGND 5 |
| 7569 | #define TAND 6 |
| 7570 | #define TOR 7 |
| 7571 | #define TPIPE 8 |
| 7572 | #define TLP 9 |
| 7573 | #define TRP 10 |
| 7574 | #define TENDCASE 11 |
| 7575 | #define TENDBQUOTE 12 |
| 7576 | #define TNOT 13 |
| 7577 | #define TCASE 14 |
| 7578 | #define TDO 15 |
| 7579 | #define TDONE 16 |
| 7580 | #define TELIF 17 |
| 7581 | #define TELSE 18 |
| 7582 | #define TESAC 19 |
| 7583 | #define TFI 20 |
| 7584 | #define TFOR 21 |
| 7585 | #define TIF 22 |
| 7586 | #define TIN 23 |
| 7587 | #define TTHEN 24 |
| 7588 | #define TUNTIL 25 |
| 7589 | #define TWHILE 26 |
| 7590 | #define TBEGIN 27 |
| 7591 | #define TEND 28 |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7592 | typedef smallint token_id_t; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7593 | |
| 7594 | /* first char is indicating which tokens mark the end of a list */ |
| 7595 | static const char *const tokname_array[] = { |
| 7596 | "\1end of file", |
| 7597 | "\0newline", |
| 7598 | "\0redirection", |
| 7599 | "\0word", |
| 7600 | "\0;", |
| 7601 | "\0&", |
| 7602 | "\0&&", |
| 7603 | "\0||", |
| 7604 | "\0|", |
| 7605 | "\0(", |
| 7606 | "\1)", |
| 7607 | "\1;;", |
| 7608 | "\1`", |
| 7609 | #define KWDOFFSET 13 |
| 7610 | /* the following are keywords */ |
| 7611 | "\0!", |
| 7612 | "\0case", |
| 7613 | "\1do", |
| 7614 | "\1done", |
| 7615 | "\1elif", |
| 7616 | "\1else", |
| 7617 | "\1esac", |
| 7618 | "\1fi", |
| 7619 | "\0for", |
| 7620 | "\0if", |
| 7621 | "\0in", |
| 7622 | "\1then", |
| 7623 | "\0until", |
| 7624 | "\0while", |
| 7625 | "\0{", |
| 7626 | "\1}", |
| 7627 | }; |
| 7628 | |
| 7629 | static const char * |
| 7630 | tokname(int tok) |
| 7631 | { |
| 7632 | static char buf[16]; |
| 7633 | |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7634 | //try this: |
| 7635 | //if (tok < TSEMI) return tokname_array[tok] + 1; |
| 7636 | //sprintf(buf, "\"%s\"", tokname_array[tok] + 1); |
| 7637 | //return buf; |
| 7638 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7639 | if (tok >= TSEMI) |
| 7640 | buf[0] = '"'; |
| 7641 | sprintf(buf + (tok >= TSEMI), "%s%c", |
| 7642 | tokname_array[tok] + 1, (tok >= TSEMI ? '"' : 0)); |
| 7643 | return buf; |
| 7644 | } |
| 7645 | |
| 7646 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 7647 | static int |
| 7648 | pstrcmp(const void *a, const void *b) |
| 7649 | { |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7650 | return strcmp((char*) a, (*(char**) b) + 1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7651 | } |
| 7652 | |
| 7653 | static const char *const * |
| 7654 | findkwd(const char *s) |
| 7655 | { |
| 7656 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 7657 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 7658 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7659 | } |
| 7660 | |
| 7661 | /* |
| 7662 | * Locate and print what a word is... |
| 7663 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7664 | static int |
| 7665 | describe_command(char *command, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7666 | { |
| 7667 | struct cmdentry entry; |
| 7668 | struct tblentry *cmdp; |
| 7669 | #if ENABLE_ASH_ALIAS |
| 7670 | const struct alias *ap; |
| 7671 | #endif |
| 7672 | const char *path = pathval(); |
| 7673 | |
| 7674 | if (describe_command_verbose) { |
| 7675 | out1str(command); |
| 7676 | } |
| 7677 | |
| 7678 | /* First look at the keywords */ |
| 7679 | if (findkwd(command)) { |
| 7680 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 7681 | goto out; |
| 7682 | } |
| 7683 | |
| 7684 | #if ENABLE_ASH_ALIAS |
| 7685 | /* Then look at the aliases */ |
| 7686 | ap = lookupalias(command, 0); |
| 7687 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7688 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7689 | out1str("alias "); |
| 7690 | printalias(ap); |
| 7691 | return 0; |
| 7692 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7693 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7694 | goto out; |
| 7695 | } |
| 7696 | #endif |
| 7697 | /* Then check if it is a tracked alias */ |
| 7698 | cmdp = cmdlookup(command, 0); |
| 7699 | if (cmdp != NULL) { |
| 7700 | entry.cmdtype = cmdp->cmdtype; |
| 7701 | entry.u = cmdp->param; |
| 7702 | } else { |
| 7703 | /* Finally use brute force */ |
| 7704 | find_command(command, &entry, DO_ABS, path); |
| 7705 | } |
| 7706 | |
| 7707 | switch (entry.cmdtype) { |
| 7708 | case CMDNORMAL: { |
| 7709 | int j = entry.u.index; |
| 7710 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7711 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7712 | p = command; |
| 7713 | } else { |
| 7714 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7715 | p = path_advance(&path, command); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7716 | stunalloc(p); |
| 7717 | } while (--j >= 0); |
| 7718 | } |
| 7719 | if (describe_command_verbose) { |
| 7720 | out1fmt(" is%s %s", |
| 7721 | (cmdp ? " a tracked alias for" : nullstr), p |
| 7722 | ); |
| 7723 | } else { |
| 7724 | out1str(p); |
| 7725 | } |
| 7726 | break; |
| 7727 | } |
| 7728 | |
| 7729 | case CMDFUNCTION: |
| 7730 | if (describe_command_verbose) { |
| 7731 | out1str(" is a shell function"); |
| 7732 | } else { |
| 7733 | out1str(command); |
| 7734 | } |
| 7735 | break; |
| 7736 | |
| 7737 | case CMDBUILTIN: |
| 7738 | if (describe_command_verbose) { |
| 7739 | out1fmt(" is a %sshell builtin", |
| 7740 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 7741 | "special " : nullstr |
| 7742 | ); |
| 7743 | } else { |
| 7744 | out1str(command); |
| 7745 | } |
| 7746 | break; |
| 7747 | |
| 7748 | default: |
| 7749 | if (describe_command_verbose) { |
| 7750 | out1str(": not found\n"); |
| 7751 | } |
| 7752 | return 127; |
| 7753 | } |
| 7754 | out: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 7755 | out1str("\n"); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7756 | return 0; |
| 7757 | } |
| 7758 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7759 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7760 | typecmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7761 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7762 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7763 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7764 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7765 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7766 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 7767 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7768 | i++; |
| 7769 | verbose = 0; |
| 7770 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7771 | while (argv[i]) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7772 | err |= describe_command(argv[i++], verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7773 | } |
| 7774 | return err; |
| 7775 | } |
| 7776 | |
| 7777 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7778 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7779 | commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7780 | { |
| 7781 | int c; |
| 7782 | enum { |
| 7783 | VERIFY_BRIEF = 1, |
| 7784 | VERIFY_VERBOSE = 2, |
| 7785 | } verify = 0; |
| 7786 | |
| 7787 | while ((c = nextopt("pvV")) != '\0') |
| 7788 | if (c == 'V') |
| 7789 | verify |= VERIFY_VERBOSE; |
| 7790 | else if (c == 'v') |
| 7791 | verify |= VERIFY_BRIEF; |
| 7792 | #if DEBUG |
| 7793 | else if (c != 'p') |
| 7794 | abort(); |
| 7795 | #endif |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7796 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
| 7797 | if (verify && (*argptr != NULL)) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7798 | return describe_command(*argptr, verify - VERIFY_BRIEF); |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7799 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7800 | |
| 7801 | return 0; |
| 7802 | } |
| 7803 | #endif |
| 7804 | |
| 7805 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7806 | /* ============ eval.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7807 | |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7808 | static int funcblocksize; /* size of structures in function */ |
| 7809 | static int funcstringsize; /* size of strings in node */ |
| 7810 | static void *funcblock; /* block to allocate function from */ |
| 7811 | static char *funcstring; /* block to allocate strings from */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7812 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7813 | /* flags in argument to evaltree */ |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7814 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 7815 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7816 | #define EV_BACKCMD 04 /* command executing within back quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7817 | |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 7818 | static const uint8_t nodesize[N_NUMBER] = { |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7819 | [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), |
| 7820 | [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), |
| 7821 | [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7822 | [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7823 | [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7824 | [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7825 | [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7826 | [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7827 | [NIF ] = SHELL_ALIGN(sizeof(struct nif)), |
| 7828 | [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7829 | [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7830 | [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)), |
| 7831 | [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)), |
| 7832 | [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)), |
| 7833 | [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7834 | [NARG ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7835 | [NTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7836 | #if ENABLE_ASH_BASH_COMPAT |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7837 | [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7838 | #endif |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7839 | [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7840 | [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7841 | [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7842 | [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7843 | [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7844 | [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7845 | [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7846 | [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7847 | [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7848 | }; |
| 7849 | |
| 7850 | static void calcsize(union node *n); |
| 7851 | |
| 7852 | static void |
| 7853 | sizenodelist(struct nodelist *lp) |
| 7854 | { |
| 7855 | while (lp) { |
| 7856 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
| 7857 | calcsize(lp->n); |
| 7858 | lp = lp->next; |
| 7859 | } |
| 7860 | } |
| 7861 | |
| 7862 | static void |
| 7863 | calcsize(union node *n) |
| 7864 | { |
| 7865 | if (n == NULL) |
| 7866 | return; |
| 7867 | funcblocksize += nodesize[n->type]; |
| 7868 | switch (n->type) { |
| 7869 | case NCMD: |
| 7870 | calcsize(n->ncmd.redirect); |
| 7871 | calcsize(n->ncmd.args); |
| 7872 | calcsize(n->ncmd.assign); |
| 7873 | break; |
| 7874 | case NPIPE: |
| 7875 | sizenodelist(n->npipe.cmdlist); |
| 7876 | break; |
| 7877 | case NREDIR: |
| 7878 | case NBACKGND: |
| 7879 | case NSUBSHELL: |
| 7880 | calcsize(n->nredir.redirect); |
| 7881 | calcsize(n->nredir.n); |
| 7882 | break; |
| 7883 | case NAND: |
| 7884 | case NOR: |
| 7885 | case NSEMI: |
| 7886 | case NWHILE: |
| 7887 | case NUNTIL: |
| 7888 | calcsize(n->nbinary.ch2); |
| 7889 | calcsize(n->nbinary.ch1); |
| 7890 | break; |
| 7891 | case NIF: |
| 7892 | calcsize(n->nif.elsepart); |
| 7893 | calcsize(n->nif.ifpart); |
| 7894 | calcsize(n->nif.test); |
| 7895 | break; |
| 7896 | case NFOR: |
| 7897 | funcstringsize += strlen(n->nfor.var) + 1; |
| 7898 | calcsize(n->nfor.body); |
| 7899 | calcsize(n->nfor.args); |
| 7900 | break; |
| 7901 | case NCASE: |
| 7902 | calcsize(n->ncase.cases); |
| 7903 | calcsize(n->ncase.expr); |
| 7904 | break; |
| 7905 | case NCLIST: |
| 7906 | calcsize(n->nclist.body); |
| 7907 | calcsize(n->nclist.pattern); |
| 7908 | calcsize(n->nclist.next); |
| 7909 | break; |
| 7910 | case NDEFUN: |
| 7911 | case NARG: |
| 7912 | sizenodelist(n->narg.backquote); |
| 7913 | funcstringsize += strlen(n->narg.text) + 1; |
| 7914 | calcsize(n->narg.next); |
| 7915 | break; |
| 7916 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 7917 | #if ENABLE_ASH_BASH_COMPAT |
| 7918 | case NTO2: |
| 7919 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7920 | case NCLOBBER: |
| 7921 | case NFROM: |
| 7922 | case NFROMTO: |
| 7923 | case NAPPEND: |
| 7924 | calcsize(n->nfile.fname); |
| 7925 | calcsize(n->nfile.next); |
| 7926 | break; |
| 7927 | case NTOFD: |
| 7928 | case NFROMFD: |
| 7929 | calcsize(n->ndup.vname); |
| 7930 | calcsize(n->ndup.next); |
| 7931 | break; |
| 7932 | case NHERE: |
| 7933 | case NXHERE: |
| 7934 | calcsize(n->nhere.doc); |
| 7935 | calcsize(n->nhere.next); |
| 7936 | break; |
| 7937 | case NNOT: |
| 7938 | calcsize(n->nnot.com); |
| 7939 | break; |
| 7940 | }; |
| 7941 | } |
| 7942 | |
| 7943 | static char * |
| 7944 | nodeckstrdup(char *s) |
| 7945 | { |
| 7946 | char *rtn = funcstring; |
| 7947 | |
| 7948 | strcpy(funcstring, s); |
| 7949 | funcstring += strlen(s) + 1; |
| 7950 | return rtn; |
| 7951 | } |
| 7952 | |
| 7953 | static union node *copynode(union node *); |
| 7954 | |
| 7955 | static struct nodelist * |
| 7956 | copynodelist(struct nodelist *lp) |
| 7957 | { |
| 7958 | struct nodelist *start; |
| 7959 | struct nodelist **lpp; |
| 7960 | |
| 7961 | lpp = &start; |
| 7962 | while (lp) { |
| 7963 | *lpp = funcblock; |
| 7964 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 7965 | (*lpp)->n = copynode(lp->n); |
| 7966 | lp = lp->next; |
| 7967 | lpp = &(*lpp)->next; |
| 7968 | } |
| 7969 | *lpp = NULL; |
| 7970 | return start; |
| 7971 | } |
| 7972 | |
| 7973 | static union node * |
| 7974 | copynode(union node *n) |
| 7975 | { |
| 7976 | union node *new; |
| 7977 | |
| 7978 | if (n == NULL) |
| 7979 | return NULL; |
| 7980 | new = funcblock; |
| 7981 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 7982 | |
| 7983 | switch (n->type) { |
| 7984 | case NCMD: |
| 7985 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 7986 | new->ncmd.args = copynode(n->ncmd.args); |
| 7987 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 7988 | break; |
| 7989 | case NPIPE: |
| 7990 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7991 | new->npipe.pipe_backgnd = n->npipe.pipe_backgnd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7992 | break; |
| 7993 | case NREDIR: |
| 7994 | case NBACKGND: |
| 7995 | case NSUBSHELL: |
| 7996 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 7997 | new->nredir.n = copynode(n->nredir.n); |
| 7998 | break; |
| 7999 | case NAND: |
| 8000 | case NOR: |
| 8001 | case NSEMI: |
| 8002 | case NWHILE: |
| 8003 | case NUNTIL: |
| 8004 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 8005 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 8006 | break; |
| 8007 | case NIF: |
| 8008 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 8009 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 8010 | new->nif.test = copynode(n->nif.test); |
| 8011 | break; |
| 8012 | case NFOR: |
| 8013 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 8014 | new->nfor.body = copynode(n->nfor.body); |
| 8015 | new->nfor.args = copynode(n->nfor.args); |
| 8016 | break; |
| 8017 | case NCASE: |
| 8018 | new->ncase.cases = copynode(n->ncase.cases); |
| 8019 | new->ncase.expr = copynode(n->ncase.expr); |
| 8020 | break; |
| 8021 | case NCLIST: |
| 8022 | new->nclist.body = copynode(n->nclist.body); |
| 8023 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 8024 | new->nclist.next = copynode(n->nclist.next); |
| 8025 | break; |
| 8026 | case NDEFUN: |
| 8027 | case NARG: |
| 8028 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 8029 | new->narg.text = nodeckstrdup(n->narg.text); |
| 8030 | new->narg.next = copynode(n->narg.next); |
| 8031 | break; |
| 8032 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8033 | #if ENABLE_ASH_BASH_COMPAT |
| 8034 | case NTO2: |
| 8035 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8036 | case NCLOBBER: |
| 8037 | case NFROM: |
| 8038 | case NFROMTO: |
| 8039 | case NAPPEND: |
| 8040 | new->nfile.fname = copynode(n->nfile.fname); |
| 8041 | new->nfile.fd = n->nfile.fd; |
| 8042 | new->nfile.next = copynode(n->nfile.next); |
| 8043 | break; |
| 8044 | case NTOFD: |
| 8045 | case NFROMFD: |
| 8046 | new->ndup.vname = copynode(n->ndup.vname); |
| 8047 | new->ndup.dupfd = n->ndup.dupfd; |
| 8048 | new->ndup.fd = n->ndup.fd; |
| 8049 | new->ndup.next = copynode(n->ndup.next); |
| 8050 | break; |
| 8051 | case NHERE: |
| 8052 | case NXHERE: |
| 8053 | new->nhere.doc = copynode(n->nhere.doc); |
| 8054 | new->nhere.fd = n->nhere.fd; |
| 8055 | new->nhere.next = copynode(n->nhere.next); |
| 8056 | break; |
| 8057 | case NNOT: |
| 8058 | new->nnot.com = copynode(n->nnot.com); |
| 8059 | break; |
| 8060 | }; |
| 8061 | new->type = n->type; |
| 8062 | return new; |
| 8063 | } |
| 8064 | |
| 8065 | /* |
| 8066 | * Make a copy of a parse tree. |
| 8067 | */ |
| 8068 | static struct funcnode * |
| 8069 | copyfunc(union node *n) |
| 8070 | { |
| 8071 | struct funcnode *f; |
| 8072 | size_t blocksize; |
| 8073 | |
| 8074 | funcblocksize = offsetof(struct funcnode, n); |
| 8075 | funcstringsize = 0; |
| 8076 | calcsize(n); |
| 8077 | blocksize = funcblocksize; |
| 8078 | f = ckmalloc(blocksize + funcstringsize); |
| 8079 | funcblock = (char *) f + offsetof(struct funcnode, n); |
| 8080 | funcstring = (char *) f + blocksize; |
| 8081 | copynode(n); |
| 8082 | f->count = 0; |
| 8083 | return f; |
| 8084 | } |
| 8085 | |
| 8086 | /* |
| 8087 | * Define a shell function. |
| 8088 | */ |
| 8089 | static void |
| 8090 | defun(char *name, union node *func) |
| 8091 | { |
| 8092 | struct cmdentry entry; |
| 8093 | |
| 8094 | INT_OFF; |
| 8095 | entry.cmdtype = CMDFUNCTION; |
| 8096 | entry.u.func = copyfunc(func); |
| 8097 | addcmdentry(name, &entry); |
| 8098 | INT_ON; |
| 8099 | } |
| 8100 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8101 | /* Reasons for skipping commands (see comment on breakcmd routine) */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8102 | #define SKIPBREAK (1 << 0) |
| 8103 | #define SKIPCONT (1 << 1) |
| 8104 | #define SKIPFUNC (1 << 2) |
| 8105 | #define SKIPFILE (1 << 3) |
| 8106 | #define SKIPEVAL (1 << 4) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8107 | static smallint evalskip; /* set to SKIPxxx if we are skipping commands */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8108 | static int skipcount; /* number of levels to skip */ |
| 8109 | static int funcnest; /* depth of function calls */ |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 8110 | static int loopnest; /* current loop nesting level */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8111 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8112 | /* Forward decl way out to parsing code - dotrap needs it */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8113 | static int evalstring(char *s, int mask); |
| 8114 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8115 | /* Called to execute a trap. |
| 8116 | * Single callsite - at the end of evaltree(). |
| 8117 | * If we return non-zero, exaltree raises EXEXIT exception. |
| 8118 | * |
| 8119 | * Perhaps we should avoid entering new trap handlers |
| 8120 | * while we are executing a trap handler. [is it a TODO?] |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8121 | */ |
| 8122 | static int |
| 8123 | dotrap(void) |
| 8124 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8125 | uint8_t *g; |
| 8126 | int sig; |
| 8127 | uint8_t savestatus; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8128 | |
| 8129 | savestatus = exitstatus; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8130 | pending_sig = 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8131 | xbarrier(); |
| 8132 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8133 | TRACE(("dotrap entered\n")); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8134 | for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) { |
| 8135 | int want_exexit; |
| 8136 | char *t; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8137 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8138 | if (*g == 0) |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8139 | continue; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8140 | t = trap[sig]; |
| 8141 | /* non-trapped SIGINT is handled separately by raise_interrupt, |
| 8142 | * don't upset it by resetting gotsig[SIGINT-1] */ |
| 8143 | if (sig == SIGINT && !t) |
| 8144 | continue; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8145 | |
| 8146 | TRACE(("sig %d is active, will run handler '%s'\n", sig, t)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8147 | *g = 0; |
| 8148 | if (!t) |
| 8149 | continue; |
| 8150 | want_exexit = evalstring(t, SKIPEVAL); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8151 | exitstatus = savestatus; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8152 | if (want_exexit) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8153 | TRACE(("dotrap returns %d\n", want_exexit)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8154 | return want_exexit; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8155 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8156 | } |
| 8157 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8158 | TRACE(("dotrap returns 0\n")); |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8159 | return 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8160 | } |
| 8161 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8162 | /* forward declarations - evaluation is fairly recursive business... */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8163 | static void evalloop(union node *, int); |
| 8164 | static void evalfor(union node *, int); |
| 8165 | static void evalcase(union node *, int); |
| 8166 | static void evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8167 | static void expredir(union node *); |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8168 | static void evalpipe(union node *, int); |
| 8169 | static void evalcommand(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8170 | static int evalbltin(const struct builtincmd *, int, char **); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8171 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8172 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8173 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8174 | * Evaluate a parse tree. The value is left in the global variable |
| 8175 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8176 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8177 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8178 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8179 | { |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8180 | struct jmploc *volatile savehandler = exception_handler; |
| 8181 | struct jmploc jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8182 | int checkexit = 0; |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8183 | void (*evalfn)(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8184 | int status; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8185 | int int_level; |
| 8186 | |
| 8187 | SAVE_INT(int_level); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8188 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8189 | if (n == NULL) { |
| 8190 | TRACE(("evaltree(NULL) called\n")); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8191 | goto out1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8192 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8193 | TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8194 | |
| 8195 | exception_handler = &jmploc; |
| 8196 | { |
| 8197 | int err = setjmp(jmploc.loc); |
| 8198 | if (err) { |
| 8199 | /* if it was a signal, check for trap handlers */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8200 | if (exception_type == EXSIG) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8201 | TRACE(("exception %d (EXSIG) in evaltree, err=%d\n", |
| 8202 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8203 | goto out; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8204 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8205 | /* continue on the way out */ |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8206 | TRACE(("exception %d in evaltree, propagating err=%d\n", |
| 8207 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8208 | exception_handler = savehandler; |
| 8209 | longjmp(exception_handler->loc, err); |
| 8210 | } |
| 8211 | } |
| 8212 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8213 | switch (n->type) { |
| 8214 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 8215 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8216 | out1fmt("Node type = %d\n", n->type); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8217 | fflush_all(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8218 | break; |
| 8219 | #endif |
| 8220 | case NNOT: |
| 8221 | evaltree(n->nnot.com, EV_TESTED); |
| 8222 | status = !exitstatus; |
| 8223 | goto setstatus; |
| 8224 | case NREDIR: |
| 8225 | expredir(n->nredir.redirect); |
| 8226 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 8227 | if (!status) { |
| 8228 | evaltree(n->nredir.n, flags & EV_TESTED); |
| 8229 | status = exitstatus; |
| 8230 | } |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 8231 | popredir(/*drop:*/ 0, /*restore:*/ 0 /* not sure */); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8232 | goto setstatus; |
| 8233 | case NCMD: |
| 8234 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8235 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8236 | if (eflag && !(flags & EV_TESTED)) |
| 8237 | checkexit = ~0; |
| 8238 | goto calleval; |
| 8239 | case NFOR: |
| 8240 | evalfn = evalfor; |
| 8241 | goto calleval; |
| 8242 | case NWHILE: |
| 8243 | case NUNTIL: |
| 8244 | evalfn = evalloop; |
| 8245 | goto calleval; |
| 8246 | case NSUBSHELL: |
| 8247 | case NBACKGND: |
| 8248 | evalfn = evalsubshell; |
| 8249 | goto calleval; |
| 8250 | case NPIPE: |
| 8251 | evalfn = evalpipe; |
| 8252 | goto checkexit; |
| 8253 | case NCASE: |
| 8254 | evalfn = evalcase; |
| 8255 | goto calleval; |
| 8256 | case NAND: |
| 8257 | case NOR: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8258 | case NSEMI: { |
| 8259 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8260 | #if NAND + 1 != NOR |
| 8261 | #error NAND + 1 != NOR |
| 8262 | #endif |
| 8263 | #if NOR + 1 != NSEMI |
| 8264 | #error NOR + 1 != NSEMI |
| 8265 | #endif |
Denis Vlasenko | 87d5fd9 | 2008-07-26 13:48:35 +0000 | [diff] [blame] | 8266 | unsigned is_or = n->type - NAND; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8267 | evaltree( |
| 8268 | n->nbinary.ch1, |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8269 | (flags | ((is_or >> 1) - 1)) & EV_TESTED |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8270 | ); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8271 | if (!exitstatus == is_or) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8272 | break; |
| 8273 | if (!evalskip) { |
| 8274 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8275 | evaln: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8276 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8277 | calleval: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8278 | evalfn(n, flags); |
| 8279 | break; |
| 8280 | } |
| 8281 | break; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8282 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8283 | case NIF: |
| 8284 | evaltree(n->nif.test, EV_TESTED); |
| 8285 | if (evalskip) |
| 8286 | break; |
| 8287 | if (exitstatus == 0) { |
| 8288 | n = n->nif.ifpart; |
| 8289 | goto evaln; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8290 | } |
| 8291 | if (n->nif.elsepart) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8292 | n = n->nif.elsepart; |
| 8293 | goto evaln; |
| 8294 | } |
| 8295 | goto success; |
| 8296 | case NDEFUN: |
| 8297 | defun(n->narg.text, n->narg.next); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8298 | success: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8299 | status = 0; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8300 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8301 | exitstatus = status; |
| 8302 | break; |
| 8303 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8304 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8305 | out: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8306 | exception_handler = savehandler; |
| 8307 | out1: |
| 8308 | if (checkexit & exitstatus) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8309 | evalskip |= SKIPEVAL; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8310 | else if (pending_sig && dotrap()) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8311 | goto exexit; |
| 8312 | |
| 8313 | if (flags & EV_EXIT) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8314 | exexit: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8315 | raise_exception(EXEXIT); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8316 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8317 | |
| 8318 | RESTORE_INT(int_level); |
| 8319 | TRACE(("leaving evaltree (no interrupts)\n")); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8320 | } |
| 8321 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8322 | #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3) |
| 8323 | static |
| 8324 | #endif |
| 8325 | void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__)); |
| 8326 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8327 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8328 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8329 | { |
| 8330 | int status; |
| 8331 | |
| 8332 | loopnest++; |
| 8333 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8334 | flags &= EV_TESTED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8335 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8336 | int i; |
| 8337 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8338 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 8339 | if (evalskip) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8340 | skipping: |
| 8341 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8342 | evalskip = 0; |
| 8343 | continue; |
| 8344 | } |
| 8345 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8346 | evalskip = 0; |
| 8347 | break; |
| 8348 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8349 | i = exitstatus; |
| 8350 | if (n->type != NWHILE) |
| 8351 | i = !i; |
| 8352 | if (i != 0) |
| 8353 | break; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8354 | evaltree(n->nbinary.ch2, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8355 | status = exitstatus; |
| 8356 | if (evalskip) |
| 8357 | goto skipping; |
| 8358 | } |
| 8359 | loopnest--; |
| 8360 | exitstatus = status; |
| 8361 | } |
| 8362 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8363 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8364 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8365 | { |
| 8366 | struct arglist arglist; |
| 8367 | union node *argp; |
| 8368 | struct strlist *sp; |
| 8369 | struct stackmark smark; |
| 8370 | |
| 8371 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8372 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8373 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8374 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8375 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8376 | /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8377 | if (evalskip) |
| 8378 | goto out; |
| 8379 | } |
| 8380 | *arglist.lastp = NULL; |
| 8381 | |
| 8382 | exitstatus = 0; |
| 8383 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8384 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8385 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8386 | setvar(n->nfor.var, sp->text, 0); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8387 | evaltree(n->nfor.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8388 | if (evalskip) { |
| 8389 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 8390 | evalskip = 0; |
| 8391 | continue; |
| 8392 | } |
| 8393 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8394 | evalskip = 0; |
| 8395 | break; |
| 8396 | } |
| 8397 | } |
| 8398 | loopnest--; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8399 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8400 | popstackmark(&smark); |
| 8401 | } |
| 8402 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8403 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8404 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8405 | { |
| 8406 | union node *cp; |
| 8407 | union node *patp; |
| 8408 | struct arglist arglist; |
| 8409 | struct stackmark smark; |
| 8410 | |
| 8411 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8412 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8413 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8414 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8415 | exitstatus = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8416 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 8417 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8418 | if (casematch(patp, arglist.list->text)) { |
| 8419 | if (evalskip == 0) { |
| 8420 | evaltree(cp->nclist.body, flags); |
| 8421 | } |
| 8422 | goto out; |
| 8423 | } |
| 8424 | } |
| 8425 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8426 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8427 | popstackmark(&smark); |
| 8428 | } |
| 8429 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8430 | /* |
| 8431 | * Kick off a subshell to evaluate a tree. |
| 8432 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8433 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8434 | evalsubshell(union node *n, int flags) |
| 8435 | { |
| 8436 | struct job *jp; |
| 8437 | int backgnd = (n->type == NBACKGND); |
| 8438 | int status; |
| 8439 | |
| 8440 | expredir(n->nredir.redirect); |
| 8441 | if (!backgnd && flags & EV_EXIT && !trap[0]) |
| 8442 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8443 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8444 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8445 | if (forkshell(jp, n, backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8446 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8447 | flags |= EV_EXIT; |
| 8448 | if (backgnd) |
| 8449 | flags &=~ EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 8450 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8451 | redirect(n->nredir.redirect, 0); |
| 8452 | evaltreenr(n->nredir.n, flags); |
| 8453 | /* never returns */ |
| 8454 | } |
| 8455 | status = 0; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8456 | if (!backgnd) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8457 | status = waitforjob(jp); |
| 8458 | exitstatus = status; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8459 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8460 | } |
| 8461 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8462 | /* |
| 8463 | * Compute the names of the files in a redirection list. |
| 8464 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 8465 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8466 | static void |
| 8467 | expredir(union node *n) |
| 8468 | { |
| 8469 | union node *redir; |
| 8470 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8471 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8472 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8473 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8474 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8475 | fn.lastp = &fn.list; |
| 8476 | switch (redir->type) { |
| 8477 | case NFROMTO: |
| 8478 | case NFROM: |
| 8479 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8480 | #if ENABLE_ASH_BASH_COMPAT |
| 8481 | case NTO2: |
| 8482 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8483 | case NCLOBBER: |
| 8484 | case NAPPEND: |
| 8485 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8486 | #if ENABLE_ASH_BASH_COMPAT |
| 8487 | store_expfname: |
| 8488 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8489 | redir->nfile.expfname = fn.list->text; |
| 8490 | break; |
| 8491 | case NFROMFD: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8492 | case NTOFD: /* >& */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8493 | if (redir->ndup.vname) { |
| 8494 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8495 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8496 | ash_msg_and_raise_error("redir error"); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8497 | #if ENABLE_ASH_BASH_COMPAT |
| 8498 | //FIXME: we used expandarg with different args! |
| 8499 | if (!isdigit_str9(fn.list->text)) { |
| 8500 | /* >&file, not >&fd */ |
| 8501 | if (redir->nfile.fd != 1) /* 123>&file - BAD */ |
| 8502 | ash_msg_and_raise_error("redir error"); |
| 8503 | redir->type = NTO2; |
| 8504 | goto store_expfname; |
| 8505 | } |
| 8506 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8507 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8508 | } |
| 8509 | break; |
| 8510 | } |
| 8511 | } |
| 8512 | } |
| 8513 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8514 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8515 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 8516 | * of the process creating the pipeline. (This differs from some versions |
| 8517 | * of the shell, which make the last process in a pipeline the parent |
| 8518 | * of all the rest.) |
| 8519 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8520 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8521 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8522 | { |
| 8523 | struct job *jp; |
| 8524 | struct nodelist *lp; |
| 8525 | int pipelen; |
| 8526 | int prevfd; |
| 8527 | int pip[2]; |
| 8528 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8529 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8530 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8531 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8532 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8533 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8534 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8535 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8536 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8537 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8538 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8539 | pip[1] = -1; |
| 8540 | if (lp->next) { |
| 8541 | if (pipe(pip) < 0) { |
| 8542 | close(prevfd); |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 8543 | ash_msg_and_raise_error("pipe call failed"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8544 | } |
| 8545 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8546 | if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8547 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8548 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8549 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8550 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8551 | if (prevfd > 0) { |
| 8552 | dup2(prevfd, 0); |
| 8553 | close(prevfd); |
| 8554 | } |
| 8555 | if (pip[1] > 1) { |
| 8556 | dup2(pip[1], 1); |
| 8557 | close(pip[1]); |
| 8558 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8559 | evaltreenr(lp->n, flags); |
| 8560 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8561 | } |
| 8562 | if (prevfd >= 0) |
| 8563 | close(prevfd); |
| 8564 | prevfd = pip[0]; |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 8565 | /* Don't want to trigger debugging */ |
| 8566 | if (pip[1] != -1) |
| 8567 | close(pip[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8568 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8569 | if (n->npipe.pipe_backgnd == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8570 | exitstatus = waitforjob(jp); |
| 8571 | TRACE(("evalpipe: job done exit status %d\n", exitstatus)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8572 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8573 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8574 | } |
| 8575 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8576 | /* |
| 8577 | * Controls whether the shell is interactive or not. |
| 8578 | */ |
| 8579 | static void |
| 8580 | setinteractive(int on) |
| 8581 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8582 | static smallint is_interactive; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8583 | |
| 8584 | if (++on == is_interactive) |
| 8585 | return; |
| 8586 | is_interactive = on; |
| 8587 | setsignal(SIGINT); |
| 8588 | setsignal(SIGQUIT); |
| 8589 | setsignal(SIGTERM); |
| 8590 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8591 | if (is_interactive > 1) { |
| 8592 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8593 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8594 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8595 | if (!did_banner) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8596 | /* note: ash and hush share this string */ |
| 8597 | out1fmt("\n\n%s %s\n" |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8598 | "Enter 'help' for a list of built-in commands." |
| 8599 | "\n\n", |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8600 | bb_banner, |
| 8601 | "built-in shell (ash)" |
| 8602 | ); |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8603 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8604 | } |
| 8605 | } |
| 8606 | #endif |
| 8607 | } |
| 8608 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8609 | static void |
| 8610 | optschanged(void) |
| 8611 | { |
| 8612 | #if DEBUG |
| 8613 | opentrace(); |
| 8614 | #endif |
| 8615 | setinteractive(iflag); |
| 8616 | setjobctl(mflag); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8617 | #if ENABLE_FEATURE_EDITING_VI |
| 8618 | if (viflag) |
| 8619 | line_input_state->flags |= VI_MODE; |
| 8620 | else |
| 8621 | line_input_state->flags &= ~VI_MODE; |
| 8622 | #else |
| 8623 | viflag = 0; /* forcibly keep the option off */ |
| 8624 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8625 | } |
| 8626 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8627 | static struct localvar *localvars; |
| 8628 | |
| 8629 | /* |
| 8630 | * Called after a function returns. |
| 8631 | * Interrupts must be off. |
| 8632 | */ |
| 8633 | static void |
| 8634 | poplocalvars(void) |
| 8635 | { |
| 8636 | struct localvar *lvp; |
| 8637 | struct var *vp; |
| 8638 | |
| 8639 | while ((lvp = localvars) != NULL) { |
| 8640 | localvars = lvp->next; |
| 8641 | vp = lvp->vp; |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 8642 | TRACE(("poplocalvar %s\n", vp ? vp->text : "-")); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8643 | if (vp == NULL) { /* $- saved */ |
| 8644 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 8645 | free((char*)lvp->text); |
| 8646 | optschanged(); |
| 8647 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
| 8648 | unsetvar(vp->text); |
| 8649 | } else { |
| 8650 | if (vp->func) |
| 8651 | (*vp->func)(strchrnul(lvp->text, '=') + 1); |
| 8652 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 8653 | free((char*)vp->text); |
| 8654 | vp->flags = lvp->flags; |
| 8655 | vp->text = lvp->text; |
| 8656 | } |
| 8657 | free(lvp); |
| 8658 | } |
| 8659 | } |
| 8660 | |
| 8661 | static int |
| 8662 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 8663 | { |
| 8664 | volatile struct shparam saveparam; |
| 8665 | struct localvar *volatile savelocalvars; |
| 8666 | struct jmploc *volatile savehandler; |
| 8667 | struct jmploc jmploc; |
| 8668 | int e; |
| 8669 | |
| 8670 | saveparam = shellparam; |
| 8671 | savelocalvars = localvars; |
| 8672 | e = setjmp(jmploc.loc); |
| 8673 | if (e) { |
| 8674 | goto funcdone; |
| 8675 | } |
| 8676 | INT_OFF; |
| 8677 | savehandler = exception_handler; |
| 8678 | exception_handler = &jmploc; |
| 8679 | localvars = NULL; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8680 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8681 | func->count++; |
| 8682 | funcnest++; |
| 8683 | INT_ON; |
| 8684 | shellparam.nparam = argc - 1; |
| 8685 | shellparam.p = argv + 1; |
| 8686 | #if ENABLE_ASH_GETOPTS |
| 8687 | shellparam.optind = 1; |
| 8688 | shellparam.optoff = -1; |
| 8689 | #endif |
| 8690 | evaltree(&func->n, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8691 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8692 | INT_OFF; |
| 8693 | funcnest--; |
| 8694 | freefunc(func); |
| 8695 | poplocalvars(); |
| 8696 | localvars = savelocalvars; |
| 8697 | freeparam(&shellparam); |
| 8698 | shellparam = saveparam; |
| 8699 | exception_handler = savehandler; |
| 8700 | INT_ON; |
| 8701 | evalskip &= ~SKIPFUNC; |
| 8702 | return e; |
| 8703 | } |
| 8704 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8705 | #if ENABLE_ASH_CMDCMD |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8706 | static char ** |
| 8707 | parse_command_args(char **argv, const char **path) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8708 | { |
| 8709 | char *cp, c; |
| 8710 | |
| 8711 | for (;;) { |
| 8712 | cp = *++argv; |
| 8713 | if (!cp) |
| 8714 | return 0; |
| 8715 | if (*cp++ != '-') |
| 8716 | break; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8717 | c = *cp++; |
| 8718 | if (!c) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8719 | break; |
| 8720 | if (c == '-' && !*cp) { |
| 8721 | argv++; |
| 8722 | break; |
| 8723 | } |
| 8724 | do { |
| 8725 | switch (c) { |
| 8726 | case 'p': |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 8727 | *path = bb_default_path; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8728 | break; |
| 8729 | default: |
| 8730 | /* run 'typecmd' for other options */ |
| 8731 | return 0; |
| 8732 | } |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 8733 | c = *cp++; |
| 8734 | } while (c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8735 | } |
| 8736 | return argv; |
| 8737 | } |
| 8738 | #endif |
| 8739 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8740 | /* |
| 8741 | * Make a variable a local variable. When a variable is made local, it's |
| 8742 | * value and flags are saved in a localvar structure. The saved values |
| 8743 | * will be restored when the shell function returns. We handle the name |
| 8744 | * "-" as a special case. |
| 8745 | */ |
| 8746 | static void |
| 8747 | mklocal(char *name) |
| 8748 | { |
| 8749 | struct localvar *lvp; |
| 8750 | struct var **vpp; |
| 8751 | struct var *vp; |
| 8752 | |
| 8753 | INT_OFF; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 8754 | lvp = ckzalloc(sizeof(struct localvar)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8755 | if (LONE_DASH(name)) { |
| 8756 | char *p; |
| 8757 | p = ckmalloc(sizeof(optlist)); |
| 8758 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 8759 | vp = NULL; |
| 8760 | } else { |
| 8761 | char *eq; |
| 8762 | |
| 8763 | vpp = hashvar(name); |
| 8764 | vp = *findvar(vpp, name); |
| 8765 | eq = strchr(name, '='); |
| 8766 | if (vp == NULL) { |
| 8767 | if (eq) |
| 8768 | setvareq(name, VSTRFIXED); |
| 8769 | else |
| 8770 | setvar(name, NULL, VSTRFIXED); |
| 8771 | vp = *vpp; /* the new variable */ |
| 8772 | lvp->flags = VUNSET; |
| 8773 | } else { |
| 8774 | lvp->text = vp->text; |
| 8775 | lvp->flags = vp->flags; |
| 8776 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 8777 | if (eq) |
| 8778 | setvareq(name, 0); |
| 8779 | } |
| 8780 | } |
| 8781 | lvp->vp = vp; |
| 8782 | lvp->next = localvars; |
| 8783 | localvars = lvp; |
| 8784 | INT_ON; |
| 8785 | } |
| 8786 | |
| 8787 | /* |
| 8788 | * The "local" command. |
| 8789 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8790 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8791 | localcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8792 | { |
| 8793 | char *name; |
| 8794 | |
| 8795 | argv = argptr; |
| 8796 | while ((name = *argv++) != NULL) { |
| 8797 | mklocal(name); |
| 8798 | } |
| 8799 | return 0; |
| 8800 | } |
| 8801 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8802 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8803 | falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8804 | { |
| 8805 | return 1; |
| 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 | truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8810 | { |
| 8811 | return 0; |
| 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 | execcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8816 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8817 | if (argv[1]) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8818 | iflag = 0; /* exit on error */ |
| 8819 | mflag = 0; |
| 8820 | optschanged(); |
| 8821 | shellexec(argv + 1, pathval(), 0); |
| 8822 | } |
| 8823 | return 0; |
| 8824 | } |
| 8825 | |
| 8826 | /* |
| 8827 | * The return command. |
| 8828 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8829 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8830 | returncmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8831 | { |
| 8832 | /* |
| 8833 | * If called outside a function, do what ksh does; |
| 8834 | * skip the rest of the file. |
| 8835 | */ |
| 8836 | evalskip = funcnest ? SKIPFUNC : SKIPFILE; |
| 8837 | return argv[1] ? number(argv[1]) : exitstatus; |
| 8838 | } |
| 8839 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8840 | /* Forward declarations for builtintab[] */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8841 | static int breakcmd(int, char **) FAST_FUNC; |
| 8842 | static int dotcmd(int, char **) FAST_FUNC; |
| 8843 | static int evalcmd(int, char **) FAST_FUNC; |
| 8844 | static int exitcmd(int, char **) FAST_FUNC; |
| 8845 | static int exportcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8846 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8847 | static int getoptscmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8848 | #endif |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8849 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8850 | static int helpcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8851 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 8852 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8853 | static int letcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8854 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8855 | static int readcmd(int, char **) FAST_FUNC; |
| 8856 | static int setcmd(int, char **) FAST_FUNC; |
| 8857 | static int shiftcmd(int, char **) FAST_FUNC; |
| 8858 | static int timescmd(int, char **) FAST_FUNC; |
| 8859 | static int trapcmd(int, char **) FAST_FUNC; |
| 8860 | static int umaskcmd(int, char **) FAST_FUNC; |
| 8861 | static int unsetcmd(int, char **) FAST_FUNC; |
| 8862 | static int ulimitcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8863 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8864 | #define BUILTIN_NOSPEC "0" |
| 8865 | #define BUILTIN_SPECIAL "1" |
| 8866 | #define BUILTIN_REGULAR "2" |
| 8867 | #define BUILTIN_SPEC_REG "3" |
| 8868 | #define BUILTIN_ASSIGN "4" |
| 8869 | #define BUILTIN_SPEC_ASSG "5" |
| 8870 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8871 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 8872 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8873 | /* Stubs for calling non-FAST_FUNC's */ |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8874 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8875 | 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] | 8876 | #endif |
| 8877 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8878 | 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] | 8879 | #endif |
| 8880 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8881 | 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] | 8882 | #endif |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 8883 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 8884 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8885 | static const struct builtincmd builtintab[] = { |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8886 | { BUILTIN_SPEC_REG "." , dotcmd }, |
| 8887 | { BUILTIN_SPEC_REG ":" , truecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8888 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8889 | { BUILTIN_REGULAR "[" , testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8890 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8891 | { BUILTIN_REGULAR "[[" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8892 | #endif |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8893 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8894 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8895 | { BUILTIN_REG_ASSG "alias" , aliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8896 | #endif |
| 8897 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8898 | { BUILTIN_REGULAR "bg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8899 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8900 | { BUILTIN_SPEC_REG "break" , breakcmd }, |
| 8901 | { BUILTIN_REGULAR "cd" , cdcmd }, |
| 8902 | { BUILTIN_NOSPEC "chdir" , cdcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8903 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8904 | { BUILTIN_REGULAR "command" , commandcmd }, |
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 "continue", breakcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8907 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8908 | { BUILTIN_REGULAR "echo" , echocmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8909 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8910 | { BUILTIN_SPEC_REG "eval" , evalcmd }, |
| 8911 | { BUILTIN_SPEC_REG "exec" , execcmd }, |
| 8912 | { BUILTIN_SPEC_REG "exit" , exitcmd }, |
| 8913 | { BUILTIN_SPEC_REG_ASSG "export" , exportcmd }, |
| 8914 | { BUILTIN_REGULAR "false" , falsecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8915 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8916 | { BUILTIN_REGULAR "fg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8917 | #endif |
| 8918 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8919 | { BUILTIN_REGULAR "getopts" , getoptscmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8920 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8921 | { BUILTIN_NOSPEC "hash" , hashcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8922 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8923 | { BUILTIN_NOSPEC "help" , helpcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8924 | #endif |
| 8925 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8926 | { BUILTIN_REGULAR "jobs" , jobscmd }, |
| 8927 | { BUILTIN_REGULAR "kill" , killcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8928 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 8929 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8930 | { BUILTIN_NOSPEC "let" , letcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8931 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8932 | { BUILTIN_ASSIGN "local" , localcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8933 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8934 | { BUILTIN_REGULAR "printf" , printfcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8935 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8936 | { BUILTIN_NOSPEC "pwd" , pwdcmd }, |
| 8937 | { BUILTIN_REGULAR "read" , readcmd }, |
| 8938 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 8939 | { BUILTIN_SPEC_REG "return" , returncmd }, |
| 8940 | { BUILTIN_SPEC_REG "set" , setcmd }, |
| 8941 | { BUILTIN_SPEC_REG "shift" , shiftcmd }, |
| 8942 | { BUILTIN_SPEC_REG "source" , dotcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8943 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8944 | { BUILTIN_REGULAR "test" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8945 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8946 | { BUILTIN_SPEC_REG "times" , timescmd }, |
| 8947 | { BUILTIN_SPEC_REG "trap" , trapcmd }, |
| 8948 | { BUILTIN_REGULAR "true" , truecmd }, |
| 8949 | { BUILTIN_NOSPEC "type" , typecmd }, |
| 8950 | { BUILTIN_NOSPEC "ulimit" , ulimitcmd }, |
| 8951 | { BUILTIN_REGULAR "umask" , umaskcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8952 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8953 | { BUILTIN_REGULAR "unalias" , unaliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8954 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8955 | { BUILTIN_SPEC_REG "unset" , unsetcmd }, |
| 8956 | { BUILTIN_REGULAR "wait" , waitcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8957 | }; |
| 8958 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8959 | /* Should match the above table! */ |
| 8960 | #define COMMANDCMD (builtintab + \ |
| 8961 | 2 + \ |
| 8962 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 8963 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 8964 | 1 * ENABLE_ASH_ALIAS + \ |
| 8965 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 8966 | 3) |
| 8967 | #define EXECCMD (builtintab + \ |
| 8968 | 2 + \ |
| 8969 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 8970 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 8971 | 1 * ENABLE_ASH_ALIAS + \ |
| 8972 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 8973 | 3 + \ |
| 8974 | 1 * ENABLE_ASH_CMDCMD + \ |
| 8975 | 1 + \ |
| 8976 | ENABLE_ASH_BUILTIN_ECHO + \ |
| 8977 | 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8978 | |
| 8979 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8980 | * Search the table of builtin commands. |
| 8981 | */ |
| 8982 | static struct builtincmd * |
| 8983 | find_builtin(const char *name) |
| 8984 | { |
| 8985 | struct builtincmd *bp; |
| 8986 | |
| 8987 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 8988 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8989 | pstrcmp |
| 8990 | ); |
| 8991 | return bp; |
| 8992 | } |
| 8993 | |
| 8994 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8995 | * Execute a simple command. |
| 8996 | */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8997 | static int |
| 8998 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8999 | { |
| 9000 | const char *q = endofname(p); |
| 9001 | if (p == q) |
| 9002 | return 0; |
| 9003 | return *q == '='; |
| 9004 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9005 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9006 | bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9007 | { |
| 9008 | /* Preserve exitstatus of a previous possible redirection |
| 9009 | * as POSIX mandates */ |
| 9010 | return back_exitstatus; |
| 9011 | } |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 9012 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9013 | evalcommand(union node *cmd, int flags) |
| 9014 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9015 | static const struct builtincmd null_bltin = { |
| 9016 | "\0\0", bltincmd /* why three NULs? */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9017 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9018 | struct stackmark smark; |
| 9019 | union node *argp; |
| 9020 | struct arglist arglist; |
| 9021 | struct arglist varlist; |
| 9022 | char **argv; |
| 9023 | int argc; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9024 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9025 | struct cmdentry cmdentry; |
| 9026 | struct job *jp; |
| 9027 | char *lastarg; |
| 9028 | const char *path; |
| 9029 | int spclbltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9030 | int status; |
| 9031 | char **nargv; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9032 | struct builtincmd *bcmd; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9033 | smallint cmd_is_exec; |
| 9034 | smallint pseudovarflag = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9035 | |
| 9036 | /* First expand the arguments. */ |
| 9037 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 9038 | setstackmark(&smark); |
| 9039 | back_exitstatus = 0; |
| 9040 | |
| 9041 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9042 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9043 | varlist.lastp = &varlist.list; |
| 9044 | *varlist.lastp = NULL; |
| 9045 | arglist.lastp = &arglist.list; |
| 9046 | *arglist.lastp = NULL; |
| 9047 | |
| 9048 | argc = 0; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9049 | if (cmd->ncmd.args) { |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9050 | bcmd = find_builtin(cmd->ncmd.args->narg.text); |
| 9051 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
| 9052 | } |
| 9053 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9054 | for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) { |
| 9055 | struct strlist **spp; |
| 9056 | |
| 9057 | spp = arglist.lastp; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 9058 | if (pseudovarflag && isassignment(argp->narg.text)) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9059 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 9060 | else |
| 9061 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 9062 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9063 | for (sp = *spp; sp; sp = sp->next) |
| 9064 | argc++; |
| 9065 | } |
| 9066 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9067 | argv = nargv = stalloc(sizeof(char *) * (argc + 1)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9068 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9069 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 9070 | *nargv++ = sp->text; |
| 9071 | } |
| 9072 | *nargv = NULL; |
| 9073 | |
| 9074 | lastarg = NULL; |
| 9075 | if (iflag && funcnest == 0 && argc > 0) |
| 9076 | lastarg = nargv[-1]; |
| 9077 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9078 | preverrout_fd = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9079 | expredir(cmd->ncmd.redirect); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9080 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9081 | |
| 9082 | path = vpath.text; |
| 9083 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 9084 | struct strlist **spp; |
| 9085 | char *p; |
| 9086 | |
| 9087 | spp = varlist.lastp; |
| 9088 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 9089 | |
| 9090 | /* |
| 9091 | * Modify the command lookup path, if a PATH= assignment |
| 9092 | * is present |
| 9093 | */ |
| 9094 | p = (*spp)->text; |
| 9095 | if (varequal(p, path)) |
| 9096 | path = p; |
| 9097 | } |
| 9098 | |
| 9099 | /* Print the command if xflag is set. */ |
| 9100 | if (xflag) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9101 | int n; |
| 9102 | const char *p = " %s"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9103 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9104 | p++; |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9105 | fdprintf(preverrout_fd, p, expandstr(ps4val())); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9106 | |
| 9107 | sp = varlist.list; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9108 | for (n = 0; n < 2; n++) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9109 | while (sp) { |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9110 | fdprintf(preverrout_fd, p, sp->text); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9111 | sp = sp->next; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9112 | if (*p == '%') { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9113 | p--; |
| 9114 | } |
| 9115 | } |
| 9116 | sp = arglist.list; |
| 9117 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9118 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9119 | } |
| 9120 | |
| 9121 | cmd_is_exec = 0; |
| 9122 | spclbltin = -1; |
| 9123 | |
| 9124 | /* Now locate the command. */ |
| 9125 | if (argc) { |
| 9126 | const char *oldpath; |
| 9127 | int cmd_flag = DO_ERR; |
| 9128 | |
| 9129 | path += 5; |
| 9130 | oldpath = path; |
| 9131 | for (;;) { |
| 9132 | find_command(argv[0], &cmdentry, cmd_flag, path); |
| 9133 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9134 | flush_stdout_stderr(); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9135 | status = 127; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9136 | goto bail; |
| 9137 | } |
| 9138 | |
| 9139 | /* implement bltin and command here */ |
| 9140 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 9141 | break; |
| 9142 | if (spclbltin < 0) |
| 9143 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
| 9144 | if (cmdentry.u.cmd == EXECCMD) |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9145 | cmd_is_exec = 1; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9146 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9147 | if (cmdentry.u.cmd == COMMANDCMD) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9148 | path = oldpath; |
| 9149 | nargv = parse_command_args(argv, &path); |
| 9150 | if (!nargv) |
| 9151 | break; |
| 9152 | argc -= nargv - argv; |
| 9153 | argv = nargv; |
| 9154 | cmd_flag |= DO_NOFUNC; |
| 9155 | } else |
| 9156 | #endif |
| 9157 | break; |
| 9158 | } |
| 9159 | } |
| 9160 | |
| 9161 | if (status) { |
| 9162 | /* We have a redirection error. */ |
| 9163 | if (spclbltin > 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9164 | raise_exception(EXERROR); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9165 | bail: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9166 | exitstatus = status; |
| 9167 | goto out; |
| 9168 | } |
| 9169 | |
| 9170 | /* Execute the command. */ |
| 9171 | switch (cmdentry.cmdtype) { |
| 9172 | default: |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9173 | |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9174 | #if ENABLE_FEATURE_SH_NOFORK |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9175 | /* Hmmm... shouldn't it happen somewhere in forkshell() instead? |
| 9176 | * Why "fork off a child process if necessary" doesn't apply to NOFORK? */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9177 | { |
| 9178 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 9179 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9180 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9181 | listsetvar(varlist.list, VEXPORT|VSTACK); |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9182 | /* run <applet>_main() */ |
| 9183 | exitstatus = run_nofork_applet(applet_no, argv); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9184 | break; |
| 9185 | } |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9186 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9187 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9188 | /* Fork off a child process if necessary. */ |
| 9189 | if (!(flags & EV_EXIT) || trap[0]) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9190 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9191 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9192 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
| 9193 | exitstatus = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9194 | INT_ON; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9195 | TRACE(("forked child exited with %d\n", exitstatus)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9196 | break; |
| 9197 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9198 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9199 | } |
| 9200 | listsetvar(varlist.list, VEXPORT|VSTACK); |
| 9201 | shellexec(argv, path, cmdentry.u.index); |
| 9202 | /* NOTREACHED */ |
| 9203 | |
| 9204 | case CMDBUILTIN: |
| 9205 | cmdenviron = varlist.list; |
| 9206 | if (cmdenviron) { |
| 9207 | struct strlist *list = cmdenviron; |
| 9208 | int i = VNOSET; |
| 9209 | if (spclbltin > 0 || argc == 0) { |
| 9210 | i = 0; |
| 9211 | if (cmd_is_exec && argc > 1) |
| 9212 | i = VEXPORT; |
| 9213 | } |
| 9214 | listsetvar(list, i); |
| 9215 | } |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9216 | /* Tight loop with builtins only: |
| 9217 | * "while kill -0 $child; do true; done" |
| 9218 | * will never exit even if $child died, unless we do this |
| 9219 | * to reap the zombie and make kill detect that it's gone: */ |
| 9220 | dowait(DOWAIT_NONBLOCK, NULL); |
| 9221 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9222 | if (evalbltin(cmdentry.u.cmd, argc, argv)) { |
| 9223 | int exit_status; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 9224 | int i = exception_type; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9225 | if (i == EXEXIT) |
| 9226 | goto raise; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9227 | exit_status = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9228 | if (i == EXINT) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 9229 | exit_status = 128 + SIGINT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9230 | if (i == EXSIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9231 | exit_status = 128 + pending_sig; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9232 | exitstatus = exit_status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9233 | if (i == EXINT || spclbltin > 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9234 | raise: |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9235 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9236 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9237 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9238 | } |
| 9239 | break; |
| 9240 | |
| 9241 | case CMDFUNCTION: |
| 9242 | listsetvar(varlist.list, 0); |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9243 | /* See above for the rationale */ |
| 9244 | dowait(DOWAIT_NONBLOCK, NULL); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9245 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 9246 | goto raise; |
| 9247 | break; |
| 9248 | } |
| 9249 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9250 | out: |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9251 | popredir(/*drop:*/ cmd_is_exec, /*restore:*/ cmd_is_exec); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9252 | if (lastarg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9253 | /* dsl: I think this is intended to be used to support |
| 9254 | * '_' in 'vi' command mode during line editing... |
| 9255 | * However I implemented that within libedit itself. |
| 9256 | */ |
| 9257 | setvar("_", lastarg, 0); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9258 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9259 | popstackmark(&smark); |
| 9260 | } |
| 9261 | |
| 9262 | static int |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9263 | evalbltin(const struct builtincmd *cmd, int argc, char **argv) |
| 9264 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9265 | char *volatile savecmdname; |
| 9266 | struct jmploc *volatile savehandler; |
| 9267 | struct jmploc jmploc; |
| 9268 | int i; |
| 9269 | |
| 9270 | savecmdname = commandname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9271 | i = setjmp(jmploc.loc); |
| 9272 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9273 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9274 | savehandler = exception_handler; |
| 9275 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9276 | commandname = argv[0]; |
| 9277 | argptr = argv + 1; |
| 9278 | optptr = NULL; /* initialize nextopt */ |
| 9279 | exitstatus = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9280 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9281 | cmddone: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9282 | exitstatus |= ferror(stdout); |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 9283 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9284 | commandname = savecmdname; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9285 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9286 | |
| 9287 | return i; |
| 9288 | } |
| 9289 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9290 | static int |
| 9291 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9292 | { |
| 9293 | return !*endofname(p); |
| 9294 | } |
| 9295 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9296 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9297 | /* |
| 9298 | * Search for a command. This is called before we fork so that the |
| 9299 | * 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] | 9300 | * the child. The check for "goodname" is an overly conservative |
| 9301 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9302 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9303 | static void |
| 9304 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9305 | { |
| 9306 | struct cmdentry entry; |
| 9307 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9308 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 9309 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9310 | } |
| 9311 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9312 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9313 | /* ============ Builtin commands |
| 9314 | * |
| 9315 | * Builtin commands whose functions are closely tied to evaluation |
| 9316 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9317 | */ |
| 9318 | |
| 9319 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9320 | * Handle break and continue commands. Break, continue, and return are |
| 9321 | * all handled by setting the evalskip flag. The evaluation routines |
| 9322 | * above all check this flag, and if it is set they start skipping |
| 9323 | * commands rather than executing them. The variable skipcount is |
| 9324 | * the number of loops to break/continue, or the number of function |
| 9325 | * levels to return. (The latter is always 1.) It should probably |
| 9326 | * be an error to break out of more loops than exist, but it isn't |
| 9327 | * in the standard shell so we don't make it one here. |
| 9328 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9329 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9330 | breakcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9331 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9332 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9333 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 9334 | if (n <= 0) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9335 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9336 | if (n > loopnest) |
| 9337 | n = loopnest; |
| 9338 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9339 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9340 | skipcount = n; |
| 9341 | } |
| 9342 | return 0; |
| 9343 | } |
| 9344 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9345 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9346 | /* ============ input.c |
| 9347 | * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 9348 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9349 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9350 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9351 | enum { |
| 9352 | INPUT_PUSH_FILE = 1, |
| 9353 | INPUT_NOFILE_OK = 2, |
| 9354 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9355 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9356 | static smallint checkkwd; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9357 | /* values of checkkwd variable */ |
| 9358 | #define CHKALIAS 0x1 |
| 9359 | #define CHKKWD 0x2 |
| 9360 | #define CHKNL 0x4 |
| 9361 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9362 | /* |
| 9363 | * Push a string back onto the input at this current parsefile level. |
| 9364 | * We handle aliases this way. |
| 9365 | */ |
| 9366 | #if !ENABLE_ASH_ALIAS |
| 9367 | #define pushstring(s, ap) pushstring(s) |
| 9368 | #endif |
| 9369 | static void |
| 9370 | pushstring(char *s, struct alias *ap) |
| 9371 | { |
| 9372 | struct strpush *sp; |
| 9373 | int len; |
| 9374 | |
| 9375 | len = strlen(s); |
| 9376 | INT_OFF; |
| 9377 | if (g_parsefile->strpush) { |
| 9378 | sp = ckzalloc(sizeof(*sp)); |
| 9379 | sp->prev = g_parsefile->strpush; |
| 9380 | } else { |
| 9381 | sp = &(g_parsefile->basestrpush); |
| 9382 | } |
| 9383 | g_parsefile->strpush = sp; |
| 9384 | sp->prev_string = g_parsefile->next_to_pgetc; |
| 9385 | sp->prev_left_in_line = g_parsefile->left_in_line; |
| 9386 | #if ENABLE_ASH_ALIAS |
| 9387 | sp->ap = ap; |
| 9388 | if (ap) { |
| 9389 | ap->flag |= ALIASINUSE; |
| 9390 | sp->string = s; |
| 9391 | } |
| 9392 | #endif |
| 9393 | g_parsefile->next_to_pgetc = s; |
| 9394 | g_parsefile->left_in_line = len; |
| 9395 | INT_ON; |
| 9396 | } |
| 9397 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9398 | static void |
| 9399 | popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9400 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9401 | struct strpush *sp = g_parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9402 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9403 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9404 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9405 | if (sp->ap) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9406 | if (g_parsefile->next_to_pgetc[-1] == ' ' |
| 9407 | || g_parsefile->next_to_pgetc[-1] == '\t' |
| 9408 | ) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9409 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9410 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9411 | if (sp->string != sp->ap->val) { |
| 9412 | free(sp->string); |
| 9413 | } |
| 9414 | sp->ap->flag &= ~ALIASINUSE; |
| 9415 | if (sp->ap->flag & ALIASDEAD) { |
| 9416 | unalias(sp->ap->name); |
| 9417 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9418 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9419 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9420 | g_parsefile->next_to_pgetc = sp->prev_string; |
| 9421 | g_parsefile->left_in_line = sp->prev_left_in_line; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9422 | g_parsefile->strpush = sp->prev; |
| 9423 | if (sp != &(g_parsefile->basestrpush)) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9424 | free(sp); |
| 9425 | INT_ON; |
| 9426 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9427 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9428 | //FIXME: BASH_COMPAT with "...&" does TWO pungetc(): |
| 9429 | //it peeks whether it is &>, and then pushes back both chars. |
| 9430 | //This function needs to save last *next_to_pgetc to buf[0] |
| 9431 | //to make two pungetc() reliable. Currently, |
| 9432 | // pgetc (out of buf: does preadfd), pgetc, pungetc, pungetc won't work... |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9433 | static int |
| 9434 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9435 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9436 | int nr; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 9437 | char *buf = g_parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9438 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9439 | g_parsefile->next_to_pgetc = buf; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9440 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9441 | retry: |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9442 | if (!iflag || g_parsefile->fd != STDIN_FILENO) |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9443 | nr = nonblock_safe_read(g_parsefile->fd, buf, BUFSIZ - 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9444 | else { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9445 | #if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9446 | line_input_state->path_lookup = pathval(); |
Eric Andersen | 4a79c0e | 2004-09-08 10:01:07 +0000 | [diff] [blame] | 9447 | #endif |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9448 | nr = read_line_input(cmdedit_prompt, buf, BUFSIZ, line_input_state); |
| 9449 | if (nr == 0) { |
| 9450 | /* Ctrl+C pressed */ |
| 9451 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9452 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9453 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9454 | raise(SIGINT); |
| 9455 | return 1; |
| 9456 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9457 | goto retry; |
| 9458 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9459 | if (nr < 0 && errno == 0) { |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9460 | /* Ctrl+D pressed */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9461 | nr = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9462 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9463 | } |
| 9464 | #else |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 9465 | nr = nonblock_safe_read(g_parsefile->fd, buf, BUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9466 | #endif |
| 9467 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9468 | #if 0 |
| 9469 | /* nonblock_safe_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9470 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9471 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 9472 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9473 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 9474 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9475 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 9476 | out2str("sh: turning off NDELAY mode\n"); |
| 9477 | goto retry; |
| 9478 | } |
| 9479 | } |
| 9480 | } |
| 9481 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9482 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9483 | return nr; |
| 9484 | } |
| 9485 | |
| 9486 | /* |
| 9487 | * Refill the input buffer and return the next input character: |
| 9488 | * |
| 9489 | * 1) If a string was pushed back on the input, pop it; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9490 | * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM) |
| 9491 | * or we are reading from a string so we can't refill the buffer, |
| 9492 | * return EOF. |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 9493 | * 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] | 9494 | * 4) Process input up to the next newline, deleting nul characters. |
| 9495 | */ |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9496 | //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__) |
| 9497 | #define pgetc_debug(...) ((void)0) |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9498 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9499 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9500 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9501 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9502 | int more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9503 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9504 | while (g_parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9505 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9506 | if (g_parsefile->left_in_line == -1 |
| 9507 | && g_parsefile->strpush->ap |
| 9508 | && g_parsefile->next_to_pgetc[-1] != ' ' |
| 9509 | && g_parsefile->next_to_pgetc[-1] != '\t' |
Denis Vlasenko | 1689840 | 2008-11-25 01:34:52 +0000 | [diff] [blame] | 9510 | ) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9511 | pgetc_debug("preadbuffer PEOA"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9512 | return PEOA; |
| 9513 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9514 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9515 | popstring(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9516 | /* try "pgetc" now: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9517 | pgetc_debug("preadbuffer internal pgetc at %d:%p'%s'", |
| 9518 | g_parsefile->left_in_line, |
| 9519 | g_parsefile->next_to_pgetc, |
| 9520 | g_parsefile->next_to_pgetc); |
| 9521 | if (--g_parsefile->left_in_line >= 0) |
| 9522 | return (unsigned char)(*g_parsefile->next_to_pgetc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9523 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9524 | /* on both branches above g_parsefile->left_in_line < 0. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9525 | * "pgetc" needs refilling. |
| 9526 | */ |
| 9527 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9528 | /* -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] | 9529 | * pungetc() may increment it a few times. |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9530 | * Assuming it won't increment it to less than -90. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9531 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9532 | if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9533 | pgetc_debug("preadbuffer PEOF1"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9534 | /* even in failure keep left_in_line and next_to_pgetc |
| 9535 | * in lock step, for correct multi-layer pungetc. |
| 9536 | * left_in_line was decremented before preadbuffer(), |
| 9537 | * must inc next_to_pgetc: */ |
| 9538 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9539 | return PEOF; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9540 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9541 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9542 | more = g_parsefile->left_in_buffer; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9543 | if (more <= 0) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9544 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9545 | again: |
| 9546 | more = preadfd(); |
| 9547 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9548 | /* don't try reading again */ |
| 9549 | g_parsefile->left_in_line = -99; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9550 | pgetc_debug("preadbuffer PEOF2"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9551 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9552 | return PEOF; |
| 9553 | } |
| 9554 | } |
| 9555 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9556 | /* Find out where's the end of line. |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9557 | * Set g_parsefile->left_in_line |
| 9558 | * and g_parsefile->left_in_buffer acordingly. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9559 | * NUL chars are deleted. |
| 9560 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9561 | q = g_parsefile->next_to_pgetc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9562 | for (;;) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9563 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9564 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9565 | more--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9566 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9567 | c = *q; |
| 9568 | if (c == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9569 | memmove(q, q + 1, more); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9570 | } else { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9571 | q++; |
| 9572 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9573 | 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] | 9574 | break; |
| 9575 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9576 | } |
| 9577 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9578 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9579 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
| 9580 | if (g_parsefile->left_in_line < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9581 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9582 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9583 | } |
| 9584 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9585 | g_parsefile->left_in_buffer = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9586 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9587 | if (vflag) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9588 | char save = *q; |
| 9589 | *q = '\0'; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9590 | out2str(g_parsefile->next_to_pgetc); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9591 | *q = save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9592 | } |
| 9593 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9594 | pgetc_debug("preadbuffer at %d:%p'%s'", |
| 9595 | g_parsefile->left_in_line, |
| 9596 | g_parsefile->next_to_pgetc, |
| 9597 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9598 | return (unsigned char)*g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9599 | } |
| 9600 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9601 | #define pgetc_as_macro() \ |
| 9602 | (--g_parsefile->left_in_line >= 0 \ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9603 | ? (unsigned char)*g_parsefile->next_to_pgetc++ \ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9604 | : preadbuffer() \ |
| 9605 | ) |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9606 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9607 | static int |
| 9608 | pgetc(void) |
| 9609 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9610 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9611 | g_parsefile->left_in_line, |
| 9612 | g_parsefile->next_to_pgetc, |
| 9613 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9614 | return pgetc_as_macro(); |
| 9615 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9616 | |
| 9617 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9618 | # define pgetc_fast() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9619 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9620 | # define pgetc_fast() pgetc_as_macro() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9621 | #endif |
| 9622 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9623 | #if ENABLE_ASH_ALIAS |
| 9624 | static int |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9625 | pgetc_without_PEOA(void) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9626 | { |
| 9627 | int c; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9628 | do { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9629 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9630 | g_parsefile->left_in_line, |
| 9631 | g_parsefile->next_to_pgetc, |
| 9632 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9633 | c = pgetc_fast(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9634 | } while (c == PEOA); |
| 9635 | return c; |
| 9636 | } |
| 9637 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9638 | # define pgetc_without_PEOA() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9639 | #endif |
| 9640 | |
| 9641 | /* |
| 9642 | * Read a line from the script. |
| 9643 | */ |
| 9644 | static char * |
| 9645 | pfgets(char *line, int len) |
| 9646 | { |
| 9647 | char *p = line; |
| 9648 | int nleft = len; |
| 9649 | int c; |
| 9650 | |
| 9651 | while (--nleft > 0) { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9652 | c = pgetc_without_PEOA(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9653 | if (c == PEOF) { |
| 9654 | if (p == line) |
| 9655 | return NULL; |
| 9656 | break; |
| 9657 | } |
| 9658 | *p++ = c; |
| 9659 | if (c == '\n') |
| 9660 | break; |
| 9661 | } |
| 9662 | *p = '\0'; |
| 9663 | return line; |
| 9664 | } |
| 9665 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9666 | /* |
| 9667 | * Undo the last call to pgetc. Only one character may be pushed back. |
| 9668 | * PEOF may be pushed back. |
| 9669 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9670 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9671 | pungetc(void) |
| 9672 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9673 | g_parsefile->left_in_line++; |
| 9674 | g_parsefile->next_to_pgetc--; |
| 9675 | pgetc_debug("pushed back to %d:%p'%s'", |
| 9676 | g_parsefile->left_in_line, |
| 9677 | g_parsefile->next_to_pgetc, |
| 9678 | g_parsefile->next_to_pgetc); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9679 | } |
| 9680 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9681 | /* |
| 9682 | * To handle the "." command, a stack of input files is used. Pushfile |
| 9683 | * adds a new entry to the stack and popfile restores the previous level. |
| 9684 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9685 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9686 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9687 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9688 | struct parsefile *pf; |
| 9689 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9690 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9691 | pf->prev = g_parsefile; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9692 | pf->fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9693 | /*pf->strpush = NULL; - ckzalloc did it */ |
| 9694 | /*pf->basestrpush.prev = NULL;*/ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9695 | g_parsefile = pf; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9696 | } |
| 9697 | |
| 9698 | static void |
| 9699 | popfile(void) |
| 9700 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9701 | struct parsefile *pf = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9702 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9703 | INT_OFF; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9704 | if (pf->fd >= 0) |
| 9705 | close(pf->fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 9706 | free(pf->buf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9707 | while (pf->strpush) |
| 9708 | popstring(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9709 | g_parsefile = pf->prev; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9710 | free(pf); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9711 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9712 | } |
| 9713 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9714 | /* |
| 9715 | * Return to top level. |
| 9716 | */ |
| 9717 | static void |
| 9718 | popallfiles(void) |
| 9719 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9720 | while (g_parsefile != &basepf) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9721 | popfile(); |
| 9722 | } |
| 9723 | |
| 9724 | /* |
| 9725 | * Close the file(s) that the shell is reading commands from. Called |
| 9726 | * after a fork is done. |
| 9727 | */ |
| 9728 | static void |
| 9729 | closescript(void) |
| 9730 | { |
| 9731 | popallfiles(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9732 | if (g_parsefile->fd > 0) { |
| 9733 | close(g_parsefile->fd); |
| 9734 | g_parsefile->fd = 0; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9735 | } |
| 9736 | } |
| 9737 | |
| 9738 | /* |
| 9739 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 9740 | * interrupts off. |
| 9741 | */ |
| 9742 | static void |
| 9743 | setinputfd(int fd, int push) |
| 9744 | { |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 9745 | close_on_exec_on(fd); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9746 | if (push) { |
| 9747 | pushfile(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9748 | g_parsefile->buf = NULL; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9749 | } |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9750 | g_parsefile->fd = fd; |
| 9751 | if (g_parsefile->buf == NULL) |
| 9752 | g_parsefile->buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9753 | g_parsefile->left_in_buffer = 0; |
| 9754 | g_parsefile->left_in_line = 0; |
| 9755 | g_parsefile->linno = 1; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9756 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9757 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9758 | /* |
| 9759 | * Set the input to take input from a file. If push is set, push the |
| 9760 | * old input onto the stack first. |
| 9761 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9762 | static int |
| 9763 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9764 | { |
| 9765 | int fd; |
| 9766 | int fd2; |
| 9767 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9768 | INT_OFF; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9769 | fd = open(fname, O_RDONLY); |
| 9770 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9771 | if (flags & INPUT_NOFILE_OK) |
| 9772 | goto out; |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 9773 | ash_msg_and_raise_error("can't open '%s'", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9774 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9775 | if (fd < 10) { |
| 9776 | fd2 = copyfd(fd, 10); |
| 9777 | close(fd); |
| 9778 | if (fd2 < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 9779 | ash_msg_and_raise_error("out of file descriptors"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9780 | fd = fd2; |
| 9781 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9782 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9783 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9784 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9785 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9786 | } |
| 9787 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9788 | /* |
| 9789 | * Like setinputfile, but takes input from a string. |
| 9790 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9791 | static void |
| 9792 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9793 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9794 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9795 | pushfile(); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9796 | g_parsefile->next_to_pgetc = string; |
| 9797 | g_parsefile->left_in_line = strlen(string); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9798 | g_parsefile->buf = NULL; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9799 | g_parsefile->linno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9800 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9801 | } |
| 9802 | |
| 9803 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9804 | /* ============ mail.c |
| 9805 | * |
| 9806 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9807 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9808 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9809 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9810 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9811 | #define MAXMBOXES 10 |
| 9812 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9813 | /* times of mailboxes */ |
| 9814 | static time_t mailtime[MAXMBOXES]; |
| 9815 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9816 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9817 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9818 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9819 | * Print appropriate message(s) if mail has arrived. |
| 9820 | * If mail_var_path_changed is set, |
| 9821 | * then the value of MAIL has mail_var_path_changed, |
| 9822 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9823 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9824 | static void |
| 9825 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9826 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9827 | const char *mpath; |
| 9828 | char *p; |
| 9829 | char *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9830 | time_t *mtp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9831 | struct stackmark smark; |
| 9832 | struct stat statb; |
| 9833 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9834 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9835 | mpath = mpathset() ? mpathval() : mailval(); |
| 9836 | for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 9837 | p = path_advance(&mpath, nullstr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9838 | if (p == NULL) |
| 9839 | break; |
| 9840 | if (*p == '\0') |
| 9841 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9842 | for (q = p; *q; q++) |
| 9843 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 9844 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9845 | if (q[-1] != '/') |
| 9846 | abort(); |
| 9847 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9848 | q[-1] = '\0'; /* delete trailing '/' */ |
| 9849 | if (stat(p, &statb) < 0) { |
| 9850 | *mtp = 0; |
| 9851 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9852 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9853 | if (!mail_var_path_changed && statb.st_mtime != *mtp) { |
| 9854 | fprintf( |
| 9855 | stderr, snlfmt, |
| 9856 | pathopt ? pathopt : "you have mail" |
| 9857 | ); |
| 9858 | } |
| 9859 | *mtp = statb.st_mtime; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9860 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9861 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9862 | popstackmark(&smark); |
| 9863 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9864 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9865 | static void FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9866 | changemail(const char *val UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9867 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9868 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9869 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9870 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9871 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9872 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9873 | |
| 9874 | /* ============ ??? */ |
| 9875 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9876 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9877 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9878 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9879 | static void |
| 9880 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9881 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9882 | char **newparam; |
| 9883 | char **ap; |
| 9884 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9885 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9886 | for (nparam = 0; argv[nparam]; nparam++) |
| 9887 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9888 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 9889 | while (*argv) { |
| 9890 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9891 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9892 | *ap = NULL; |
| 9893 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9894 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9895 | shellparam.nparam = nparam; |
| 9896 | shellparam.p = newparam; |
| 9897 | #if ENABLE_ASH_GETOPTS |
| 9898 | shellparam.optind = 1; |
| 9899 | shellparam.optoff = -1; |
| 9900 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9901 | } |
| 9902 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9903 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9904 | * Process shell options. The global variable argptr contains a pointer |
| 9905 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 9906 | * |
| 9907 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 9908 | * For a non-interactive shell, an error condition encountered |
| 9909 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 9910 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 9911 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 9912 | * ... |
| 9913 | * Utility syntax error (option or operand error) Shall exit |
| 9914 | * ... |
| 9915 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 9916 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 9917 | * and shell continues), and people rely on this behavior! |
| 9918 | * Testcase: |
| 9919 | * set -o barfoo 2>/dev/null |
| 9920 | * echo $? |
| 9921 | * |
| 9922 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9923 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9924 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9925 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9926 | { |
| 9927 | int i; |
| 9928 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9929 | if (name) { |
| 9930 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 9931 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9932 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9933 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9934 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9935 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9936 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9937 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9938 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9939 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9940 | if (val) { |
| 9941 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 9942 | } else { |
| 9943 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 9944 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9945 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9946 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9947 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9948 | static void |
| 9949 | setoption(int flag, int val) |
| 9950 | { |
| 9951 | int i; |
| 9952 | |
| 9953 | for (i = 0; i < NOPTS; i++) { |
| 9954 | if (optletters(i) == flag) { |
| 9955 | optlist[i] = val; |
| 9956 | return; |
| 9957 | } |
| 9958 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9959 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9960 | /* NOTREACHED */ |
| 9961 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9962 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9963 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9964 | { |
| 9965 | char *p; |
| 9966 | int val; |
| 9967 | int c; |
| 9968 | |
| 9969 | if (cmdline) |
| 9970 | minusc = NULL; |
| 9971 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9972 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9973 | if (c != '-' && c != '+') |
| 9974 | break; |
| 9975 | argptr++; |
| 9976 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9977 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9978 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 9979 | if (p[0] == '\0' || LONE_DASH(p)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9980 | if (!cmdline) { |
| 9981 | /* "-" means turn off -x and -v */ |
| 9982 | if (p[0] == '\0') |
| 9983 | xflag = vflag = 0; |
| 9984 | /* "--" means reset params */ |
| 9985 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9986 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9987 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9988 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9989 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9990 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9991 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9992 | while ((c = *p++) != '\0') { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9993 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9994 | if (c == 'c' && cmdline) { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9995 | minusc = p; /* command is after shell args */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9996 | } else if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9997 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9998 | /* it already printed err message */ |
| 9999 | return 1; /* error */ |
| 10000 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10001 | if (*argptr) |
| 10002 | argptr++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10003 | } else if (cmdline && (c == 'l')) { /* -l or +l == --login */ |
| 10004 | isloginsh = 1; |
| 10005 | /* bash does not accept +-login, we also won't */ |
| 10006 | } else if (cmdline && val && (c == '-')) { /* long options */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10007 | if (strcmp(p, "login") == 0) |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 10008 | isloginsh = 1; |
| 10009 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10010 | } else { |
| 10011 | setoption(c, val); |
| 10012 | } |
| 10013 | } |
| 10014 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10015 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10016 | } |
| 10017 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10018 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10019 | * The shift builtin command. |
| 10020 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10021 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10022 | shiftcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10023 | { |
| 10024 | int n; |
| 10025 | char **ap1, **ap2; |
| 10026 | |
| 10027 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10028 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10029 | n = number(argv[1]); |
| 10030 | if (n > shellparam.nparam) |
Denis Vlasenko | c90e1be | 2008-07-30 15:35:05 +0000 | [diff] [blame] | 10031 | n = 0; /* bash compat, was = shellparam.nparam; */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10032 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10033 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10034 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 10035 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10036 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10037 | } |
| 10038 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10039 | while ((*ap2++ = *ap1++) != NULL) |
| 10040 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10041 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10042 | shellparam.optind = 1; |
| 10043 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10044 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10045 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10046 | return 0; |
| 10047 | } |
| 10048 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10049 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10050 | * POSIX requires that 'set' (but not export or readonly) output the |
| 10051 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 10052 | * Maybe we could keep them in an ordered balanced binary tree |
| 10053 | * instead of hashed lists. |
| 10054 | * For now just roll 'em through qsort for printing... |
| 10055 | */ |
| 10056 | static int |
| 10057 | showvars(const char *sep_prefix, int on, int off) |
| 10058 | { |
| 10059 | const char *sep; |
| 10060 | char **ep, **epend; |
| 10061 | |
| 10062 | ep = listvars(on, off, &epend); |
| 10063 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 10064 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10065 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10066 | |
| 10067 | for (; ep < epend; ep++) { |
| 10068 | const char *p; |
| 10069 | const char *q; |
| 10070 | |
| 10071 | p = strchrnul(*ep, '='); |
| 10072 | q = nullstr; |
| 10073 | if (*p) |
| 10074 | q = single_quote(++p); |
| 10075 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 10076 | } |
| 10077 | return 0; |
| 10078 | } |
| 10079 | |
| 10080 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10081 | * The set command builtin. |
| 10082 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10083 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10084 | setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10085 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10086 | int retval; |
| 10087 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10088 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10089 | return showvars(nullstr, 0, VUNSET); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10090 | INT_OFF; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10091 | retval = 1; |
| 10092 | if (!options(0)) { /* if no parse error... */ |
| 10093 | retval = 0; |
| 10094 | optschanged(); |
| 10095 | if (*argptr != NULL) { |
| 10096 | setparam(argptr); |
| 10097 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10098 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10099 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10100 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10101 | } |
| 10102 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10103 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10104 | static void FAST_FUNC |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 10105 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10106 | { |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10107 | uint32_t t; |
| 10108 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10109 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10110 | /* "get", generate */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10111 | t = next_random(&random_gen); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10112 | /* set without recursion */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 10113 | setvar(vrandom.text, utoa(t), VNOFUNC); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10114 | vrandom.flags &= ~VNOFUNC; |
| 10115 | } else { |
| 10116 | /* set/reset */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10117 | t = strtoul(value, NULL, 10); |
| 10118 | INIT_RANDOM_T(&random_gen, (t ? t : 1), t); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10119 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10120 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10121 | #endif |
| 10122 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10123 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10124 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10125 | getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10126 | { |
| 10127 | char *p, *q; |
| 10128 | char c = '?'; |
| 10129 | int done = 0; |
| 10130 | int err = 0; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10131 | char s[12]; |
| 10132 | char **optnext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10133 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10134 | if (*param_optind < 1) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10135 | return 1; |
| 10136 | optnext = optfirst + *param_optind - 1; |
| 10137 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10138 | if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10139 | p = NULL; |
| 10140 | else |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10141 | p = optnext[-1] + *optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10142 | if (p == NULL || *p == '\0') { |
| 10143 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10144 | p = *optnext; |
| 10145 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10146 | atend: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10147 | p = NULL; |
| 10148 | done = 1; |
| 10149 | goto out; |
| 10150 | } |
| 10151 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10152 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10153 | goto atend; |
| 10154 | } |
| 10155 | |
| 10156 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 10157 | for (q = optstr; *q != c;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10158 | if (*q == '\0') { |
| 10159 | if (optstr[0] == ':') { |
| 10160 | s[0] = c; |
| 10161 | s[1] = '\0'; |
| 10162 | err |= setvarsafe("OPTARG", s, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10163 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10164 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10165 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10166 | } |
| 10167 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10168 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10169 | } |
| 10170 | if (*++q == ':') |
| 10171 | q++; |
| 10172 | } |
| 10173 | |
| 10174 | if (*++q == ':') { |
| 10175 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 10176 | if (optstr[0] == ':') { |
| 10177 | s[0] = c; |
| 10178 | s[1] = '\0'; |
| 10179 | err |= setvarsafe("OPTARG", s, 0); |
| 10180 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10181 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10182 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10183 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10184 | c = '?'; |
| 10185 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10186 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10187 | } |
| 10188 | |
| 10189 | if (p == *optnext) |
| 10190 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10191 | err |= setvarsafe("OPTARG", p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10192 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10193 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10194 | err |= setvarsafe("OPTARG", nullstr, 0); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10195 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10196 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10197 | *param_optind = optnext - optfirst + 1; |
| 10198 | fmtstr(s, sizeof(s), "%d", *param_optind); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10199 | err |= setvarsafe("OPTIND", s, VNOFUNC); |
| 10200 | s[0] = c; |
| 10201 | s[1] = '\0'; |
| 10202 | err |= setvarsafe(optvar, s, 0); |
| 10203 | if (err) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10204 | *param_optind = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10205 | *optoff = -1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10206 | flush_stdout_stderr(); |
| 10207 | raise_exception(EXERROR); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10208 | } |
| 10209 | return done; |
| 10210 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10211 | |
| 10212 | /* |
| 10213 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 10214 | * to be processed. Shellparam.optptr points to the next character to |
| 10215 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 10216 | * then it's the first time getopts has been called. |
| 10217 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10218 | static int FAST_FUNC |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10219 | getoptscmd(int argc, char **argv) |
| 10220 | { |
| 10221 | char **optbase; |
| 10222 | |
| 10223 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 10224 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10225 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10226 | optbase = shellparam.p; |
| 10227 | if (shellparam.optind > shellparam.nparam + 1) { |
| 10228 | shellparam.optind = 1; |
| 10229 | shellparam.optoff = -1; |
| 10230 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10231 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10232 | optbase = &argv[3]; |
| 10233 | if (shellparam.optind > argc - 2) { |
| 10234 | shellparam.optind = 1; |
| 10235 | shellparam.optoff = -1; |
| 10236 | } |
| 10237 | } |
| 10238 | |
| 10239 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10240 | &shellparam.optoff); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10241 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10242 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10243 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10244 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10245 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10246 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10247 | struct heredoc { |
| 10248 | struct heredoc *next; /* next here document in list */ |
| 10249 | union node *here; /* redirection node */ |
| 10250 | char *eofmark; /* string indicating end of input */ |
| 10251 | smallint striptabs; /* if set, strip leading tabs */ |
| 10252 | }; |
| 10253 | |
| 10254 | static smallint tokpushback; /* last token pushed back */ |
| 10255 | static smallint parsebackquote; /* nonzero if we are inside backquotes */ |
| 10256 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
| 10257 | static token_id_t lasttoken; /* last token read (integer id Txxx) */ |
| 10258 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 10259 | static char *wordtext; /* text of last word returned by readtoken */ |
| 10260 | static struct nodelist *backquotelist; |
| 10261 | static union node *redirnode; |
| 10262 | static struct heredoc *heredoc; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10263 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10264 | /* |
| 10265 | * Called when an unexpected token is read during the parse. The argument |
| 10266 | * is the token that is expected, or -1 if more than one type of token can |
| 10267 | * occur at this point. |
| 10268 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10269 | static void raise_error_unexpected_syntax(int) NORETURN; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10270 | static void |
| 10271 | raise_error_unexpected_syntax(int token) |
| 10272 | { |
| 10273 | char msg[64]; |
| 10274 | int l; |
| 10275 | |
Denis Vlasenko | 7b2294e | 2008-11-28 03:50:46 +0000 | [diff] [blame] | 10276 | l = sprintf(msg, "unexpected %s", tokname(lasttoken)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10277 | if (token >= 0) |
| 10278 | sprintf(msg + l, " (expecting %s)", tokname(token)); |
| 10279 | raise_error_syntax(msg); |
| 10280 | /* NOTREACHED */ |
| 10281 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10282 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10283 | #define EOFMARKLEN 79 |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10284 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10285 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 10286 | static union node *andor(void); |
| 10287 | static union node *pipeline(void); |
| 10288 | static union node *parse_command(void); |
| 10289 | static void parseheredoc(void); |
| 10290 | static char peektoken(void); |
| 10291 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10292 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10293 | static union node * |
| 10294 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10295 | { |
| 10296 | union node *n1, *n2, *n3; |
| 10297 | int tok; |
| 10298 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10299 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10300 | if (nlflag == 2 && peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10301 | return NULL; |
| 10302 | n1 = NULL; |
| 10303 | for (;;) { |
| 10304 | n2 = andor(); |
| 10305 | tok = readtoken(); |
| 10306 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10307 | if (n2->type == NPIPE) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10308 | n2->npipe.pipe_backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10309 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10310 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10311 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10312 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10313 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10314 | n2 = n3; |
| 10315 | } |
| 10316 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10317 | } |
| 10318 | } |
| 10319 | if (n1 == NULL) { |
| 10320 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10321 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10322 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10323 | n3->type = NSEMI; |
| 10324 | n3->nbinary.ch1 = n1; |
| 10325 | n3->nbinary.ch2 = n2; |
| 10326 | n1 = n3; |
| 10327 | } |
| 10328 | switch (tok) { |
| 10329 | case TBACKGND: |
| 10330 | case TSEMI: |
| 10331 | tok = readtoken(); |
| 10332 | /* fall through */ |
| 10333 | case TNL: |
| 10334 | if (tok == TNL) { |
| 10335 | parseheredoc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10336 | if (nlflag == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10337 | return n1; |
| 10338 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10339 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10340 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10341 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10342 | if (peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10343 | return n1; |
| 10344 | break; |
| 10345 | case TEOF: |
| 10346 | if (heredoclist) |
| 10347 | parseheredoc(); |
| 10348 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10349 | pungetc(); /* push back EOF on input */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10350 | return n1; |
| 10351 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10352 | if (nlflag == 1) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10353 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10354 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10355 | return n1; |
| 10356 | } |
| 10357 | } |
| 10358 | } |
| 10359 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10360 | static union node * |
| 10361 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10362 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10363 | union node *n1, *n2, *n3; |
| 10364 | int t; |
| 10365 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10366 | n1 = pipeline(); |
| 10367 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10368 | t = readtoken(); |
| 10369 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10370 | t = NAND; |
| 10371 | } else if (t == TOR) { |
| 10372 | t = NOR; |
| 10373 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10374 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10375 | return n1; |
| 10376 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10377 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10378 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10379 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10380 | n3->type = t; |
| 10381 | n3->nbinary.ch1 = n1; |
| 10382 | n3->nbinary.ch2 = n2; |
| 10383 | n1 = n3; |
| 10384 | } |
| 10385 | } |
| 10386 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10387 | static union node * |
| 10388 | pipeline(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, *pipenode; |
| 10391 | struct nodelist *lp, *prev; |
| 10392 | int negate; |
| 10393 | |
| 10394 | negate = 0; |
| 10395 | TRACE(("pipeline: entered\n")); |
| 10396 | if (readtoken() == TNOT) { |
| 10397 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10398 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10399 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10400 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10401 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10402 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10403 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10404 | pipenode->type = NPIPE; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10405 | /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10406 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10407 | pipenode->npipe.cmdlist = lp; |
| 10408 | lp->n = n1; |
| 10409 | do { |
| 10410 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10411 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10412 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10413 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10414 | prev->next = lp; |
| 10415 | } while (readtoken() == TPIPE); |
| 10416 | lp->next = NULL; |
| 10417 | n1 = pipenode; |
| 10418 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10419 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10420 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10421 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10422 | n2->type = NNOT; |
| 10423 | n2->nnot.com = n1; |
| 10424 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10425 | } |
| 10426 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10427 | } |
| 10428 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10429 | static union node * |
| 10430 | makename(void) |
| 10431 | { |
| 10432 | union node *n; |
| 10433 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10434 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10435 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10436 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10437 | n->narg.text = wordtext; |
| 10438 | n->narg.backquote = backquotelist; |
| 10439 | return n; |
| 10440 | } |
| 10441 | |
| 10442 | static void |
| 10443 | fixredir(union node *n, const char *text, int err) |
| 10444 | { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10445 | int fd; |
| 10446 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10447 | TRACE(("Fix redir %s %d\n", text, err)); |
| 10448 | if (!err) |
| 10449 | n->ndup.vname = NULL; |
| 10450 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10451 | fd = bb_strtou(text, NULL, 10); |
| 10452 | if (!errno && fd >= 0) |
| 10453 | n->ndup.dupfd = fd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10454 | else if (LONE_DASH(text)) |
| 10455 | n->ndup.dupfd = -1; |
| 10456 | else { |
| 10457 | if (err) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10458 | raise_error_syntax("bad fd number"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10459 | n->ndup.vname = makename(); |
| 10460 | } |
| 10461 | } |
| 10462 | |
| 10463 | /* |
| 10464 | * Returns true if the text contains nothing to expand (no dollar signs |
| 10465 | * or backquotes). |
| 10466 | */ |
| 10467 | static int |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10468 | noexpand(const char *text) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10469 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10470 | unsigned char c; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10471 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10472 | while ((c = *text++) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10473 | if (c == CTLQUOTEMARK) |
| 10474 | continue; |
| 10475 | if (c == CTLESC) |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10476 | text++; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 10477 | else if (SIT(c, BASESYNTAX) == CCTL) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10478 | return 0; |
| 10479 | } |
| 10480 | return 1; |
| 10481 | } |
| 10482 | |
| 10483 | static void |
| 10484 | parsefname(void) |
| 10485 | { |
| 10486 | union node *n = redirnode; |
| 10487 | |
| 10488 | if (readtoken() != TWORD) |
| 10489 | raise_error_unexpected_syntax(-1); |
| 10490 | if (n->type == NHERE) { |
| 10491 | struct heredoc *here = heredoc; |
| 10492 | struct heredoc *p; |
| 10493 | int i; |
| 10494 | |
| 10495 | if (quoteflag == 0) |
| 10496 | n->type = NXHERE; |
| 10497 | TRACE(("Here document %d\n", n->type)); |
| 10498 | if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10499 | raise_error_syntax("illegal eof marker for << redirection"); |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 10500 | rmescapes(wordtext, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10501 | here->eofmark = wordtext; |
| 10502 | here->next = NULL; |
| 10503 | if (heredoclist == NULL) |
| 10504 | heredoclist = here; |
| 10505 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10506 | for (p = heredoclist; p->next; p = p->next) |
| 10507 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10508 | p->next = here; |
| 10509 | } |
| 10510 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 10511 | fixredir(n, wordtext, 0); |
| 10512 | } else { |
| 10513 | n->nfile.fname = makename(); |
| 10514 | } |
| 10515 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10516 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10517 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10518 | simplecmd(void) |
| 10519 | { |
| 10520 | union node *args, **app; |
| 10521 | union node *n = NULL; |
| 10522 | union node *vars, **vpp; |
| 10523 | union node **rpp, *redir; |
| 10524 | int savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10525 | #if ENABLE_ASH_BASH_COMPAT |
| 10526 | smallint double_brackets_flag = 0; |
| 10527 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10528 | |
| 10529 | args = NULL; |
| 10530 | app = &args; |
| 10531 | vars = NULL; |
| 10532 | vpp = &vars; |
| 10533 | redir = NULL; |
| 10534 | rpp = &redir; |
| 10535 | |
| 10536 | savecheckkwd = CHKALIAS; |
| 10537 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10538 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10539 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10540 | t = readtoken(); |
| 10541 | switch (t) { |
| 10542 | #if ENABLE_ASH_BASH_COMPAT |
| 10543 | case TAND: /* "&&" */ |
| 10544 | case TOR: /* "||" */ |
| 10545 | if (!double_brackets_flag) { |
| 10546 | tokpushback = 1; |
| 10547 | goto out; |
| 10548 | } |
| 10549 | wordtext = (char *) (t == TAND ? "-a" : "-o"); |
| 10550 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10551 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10552 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10553 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10554 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10555 | n->narg.text = wordtext; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10556 | #if ENABLE_ASH_BASH_COMPAT |
| 10557 | if (strcmp("[[", wordtext) == 0) |
| 10558 | double_brackets_flag = 1; |
| 10559 | else if (strcmp("]]", wordtext) == 0) |
| 10560 | double_brackets_flag = 0; |
| 10561 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10562 | n->narg.backquote = backquotelist; |
| 10563 | if (savecheckkwd && isassignment(wordtext)) { |
| 10564 | *vpp = n; |
| 10565 | vpp = &n->narg.next; |
| 10566 | } else { |
| 10567 | *app = n; |
| 10568 | app = &n->narg.next; |
| 10569 | savecheckkwd = 0; |
| 10570 | } |
| 10571 | break; |
| 10572 | case TREDIR: |
| 10573 | *rpp = n = redirnode; |
| 10574 | rpp = &n->nfile.next; |
| 10575 | parsefname(); /* read name of redirection file */ |
| 10576 | break; |
| 10577 | case TLP: |
| 10578 | if (args && app == &args->narg.next |
| 10579 | && !vars && !redir |
| 10580 | ) { |
| 10581 | struct builtincmd *bcmd; |
| 10582 | const char *name; |
| 10583 | |
| 10584 | /* We have a function */ |
| 10585 | if (readtoken() != TRP) |
| 10586 | raise_error_unexpected_syntax(TRP); |
| 10587 | name = n->narg.text; |
| 10588 | if (!goodname(name) |
| 10589 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 10590 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10591 | raise_error_syntax("bad function name"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10592 | } |
| 10593 | n->type = NDEFUN; |
| 10594 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10595 | n->narg.next = parse_command(); |
| 10596 | return n; |
| 10597 | } |
| 10598 | /* fall through */ |
| 10599 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10600 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10601 | goto out; |
| 10602 | } |
| 10603 | } |
| 10604 | out: |
| 10605 | *app = NULL; |
| 10606 | *vpp = NULL; |
| 10607 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10608 | n = stzalloc(sizeof(struct ncmd)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10609 | n->type = NCMD; |
| 10610 | n->ncmd.args = args; |
| 10611 | n->ncmd.assign = vars; |
| 10612 | n->ncmd.redirect = redir; |
| 10613 | return n; |
| 10614 | } |
| 10615 | |
| 10616 | static union node * |
| 10617 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10618 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10619 | union node *n1, *n2; |
| 10620 | union node *ap, **app; |
| 10621 | union node *cp, **cpp; |
| 10622 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10623 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10624 | int t; |
| 10625 | |
| 10626 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10627 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 10628 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10629 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10630 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10631 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10632 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10633 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10634 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10635 | n1->type = NIF; |
| 10636 | n1->nif.test = list(0); |
| 10637 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10638 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10639 | n1->nif.ifpart = list(0); |
| 10640 | n2 = n1; |
| 10641 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10642 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10643 | n2 = n2->nif.elsepart; |
| 10644 | n2->type = NIF; |
| 10645 | n2->nif.test = list(0); |
| 10646 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10647 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10648 | n2->nif.ifpart = list(0); |
| 10649 | } |
| 10650 | if (lasttoken == TELSE) |
| 10651 | n2->nif.elsepart = list(0); |
| 10652 | else { |
| 10653 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10654 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10655 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10656 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10657 | break; |
| 10658 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10659 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10660 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10661 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10662 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10663 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10664 | got = readtoken(); |
| 10665 | if (got != TDO) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10666 | TRACE(("expecting DO got %s %s\n", tokname(got), |
| 10667 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10668 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10669 | } |
| 10670 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10671 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10672 | break; |
| 10673 | } |
| 10674 | case TFOR: |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10675 | if (readtoken() != TWORD || quoteflag || !goodname(wordtext)) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10676 | raise_error_syntax("bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10677 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10678 | n1->type = NFOR; |
| 10679 | n1->nfor.var = wordtext; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10680 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10681 | if (readtoken() == TIN) { |
| 10682 | app = ≈ |
| 10683 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10684 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10685 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10686 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10687 | n2->narg.text = wordtext; |
| 10688 | n2->narg.backquote = backquotelist; |
| 10689 | *app = n2; |
| 10690 | app = &n2->narg.next; |
| 10691 | } |
| 10692 | *app = NULL; |
| 10693 | n1->nfor.args = ap; |
| 10694 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10695 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10696 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10697 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10698 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10699 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10700 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10701 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10702 | n1->nfor.args = n2; |
| 10703 | /* |
| 10704 | * Newline or semicolon here is optional (but note |
| 10705 | * that the original Bourne shell only allowed NL). |
| 10706 | */ |
| 10707 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10708 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10709 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10710 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10711 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10712 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10713 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10714 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10715 | break; |
| 10716 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10717 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10718 | n1->type = NCASE; |
| 10719 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10720 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10721 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10722 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10723 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10724 | n2->narg.text = wordtext; |
| 10725 | n2->narg.backquote = backquotelist; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10726 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10727 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10728 | } while (readtoken() == TNL); |
| 10729 | if (lasttoken != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10730 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10731 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10732 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10733 | checkkwd = CHKNL | CHKKWD; |
| 10734 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10735 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10736 | if (lasttoken == TLP) |
| 10737 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10738 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10739 | cp->type = NCLIST; |
| 10740 | app = &cp->nclist.pattern; |
| 10741 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10742 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10743 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10744 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10745 | ap->narg.text = wordtext; |
| 10746 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10747 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10748 | break; |
| 10749 | app = &ap->narg.next; |
| 10750 | readtoken(); |
| 10751 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10752 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10753 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10754 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10755 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10756 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10757 | cpp = &cp->nclist.next; |
| 10758 | |
| 10759 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10760 | t = readtoken(); |
| 10761 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10762 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10763 | raise_error_unexpected_syntax(TENDCASE); |
| 10764 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10765 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10766 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10767 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10768 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10769 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10770 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10771 | n1->type = NSUBSHELL; |
| 10772 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10773 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10774 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10775 | break; |
| 10776 | case TBEGIN: |
| 10777 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10778 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10779 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10780 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10781 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10782 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10783 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10784 | } |
| 10785 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10786 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10787 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10788 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10789 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10790 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10791 | checkkwd = CHKKWD | CHKALIAS; |
| 10792 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10793 | while (readtoken() == TREDIR) { |
| 10794 | *rpp = n2 = redirnode; |
| 10795 | rpp = &n2->nfile.next; |
| 10796 | parsefname(); |
| 10797 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10798 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10799 | *rpp = NULL; |
| 10800 | if (redir) { |
| 10801 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10802 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10803 | n2->type = NREDIR; |
| 10804 | n2->nredir.n = n1; |
| 10805 | n1 = n2; |
| 10806 | } |
| 10807 | n1->nredir.redirect = redir; |
| 10808 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10809 | return n1; |
| 10810 | } |
| 10811 | |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10812 | #if ENABLE_ASH_BASH_COMPAT |
| 10813 | static int decode_dollar_squote(void) |
| 10814 | { |
| 10815 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 10816 | int c, cnt; |
| 10817 | char *p; |
| 10818 | char buf[4]; |
| 10819 | |
| 10820 | c = pgetc(); |
| 10821 | p = strchr(C_escapes, c); |
| 10822 | if (p) { |
| 10823 | buf[0] = c; |
| 10824 | p = buf; |
| 10825 | cnt = 3; |
| 10826 | if ((unsigned char)(c - '0') <= 7) { /* \ooo */ |
| 10827 | do { |
| 10828 | c = pgetc(); |
| 10829 | *++p = c; |
| 10830 | } while ((unsigned char)(c - '0') <= 7 && --cnt); |
| 10831 | pungetc(); |
| 10832 | } else if (c == 'x') { /* \xHH */ |
| 10833 | do { |
| 10834 | c = pgetc(); |
| 10835 | *++p = c; |
| 10836 | } while (isxdigit(c) && --cnt); |
| 10837 | pungetc(); |
| 10838 | if (cnt == 3) { /* \x but next char is "bad" */ |
| 10839 | c = 'x'; |
| 10840 | goto unrecognized; |
| 10841 | } |
| 10842 | } else { /* simple seq like \\ or \t */ |
| 10843 | p++; |
| 10844 | } |
| 10845 | *p = '\0'; |
| 10846 | p = buf; |
| 10847 | c = bb_process_escape_sequence((void*)&p); |
| 10848 | } else { /* unrecognized "\z": print both chars unless ' or " */ |
| 10849 | if (c != '\'' && c != '"') { |
| 10850 | unrecognized: |
| 10851 | c |= 0x100; /* "please encode \, then me" */ |
| 10852 | } |
| 10853 | } |
| 10854 | return c; |
| 10855 | } |
| 10856 | #endif |
| 10857 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10858 | /* |
| 10859 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 10860 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 10861 | * 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] | 10862 | * leading tabs should be stripped from the document. The argument c |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10863 | * is the first character of the input token or document. |
| 10864 | * |
| 10865 | * Because C does not have internal subroutines, I have simulated them |
| 10866 | * using goto's to implement the subroutine linkage. The following macros |
| 10867 | * will run code that appears at the end of readtoken1. |
| 10868 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10869 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 10870 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 10871 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 10872 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 10873 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 10874 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10875 | static int |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10876 | readtoken1(int c, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10877 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10878 | /* NB: syntax parameter fits into smallint */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10879 | /* c parameter is an unsigned char or PEOF or PEOA */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10880 | char *out; |
| 10881 | int len; |
| 10882 | char line[EOFMARKLEN + 1]; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10883 | struct nodelist *bqlist; |
| 10884 | smallint quotef; |
| 10885 | smallint dblquote; |
| 10886 | smallint oldstyle; |
| 10887 | smallint prevsyntax; /* syntax before arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10888 | #if ENABLE_ASH_EXPAND_PRMT |
| 10889 | smallint pssyntax; /* we are expanding a prompt string */ |
| 10890 | #endif |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10891 | int varnest; /* levels of variables expansion */ |
| 10892 | int arinest; /* levels of arithmetic expansion */ |
| 10893 | int parenlevel; /* levels of parens in arithmetic */ |
| 10894 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 10895 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 10896 | IF_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10897 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10898 | #if __GNUC__ |
| 10899 | /* Avoid longjmp clobbering */ |
| 10900 | (void) &out; |
| 10901 | (void) "ef; |
| 10902 | (void) &dblquote; |
| 10903 | (void) &varnest; |
| 10904 | (void) &arinest; |
| 10905 | (void) &parenlevel; |
| 10906 | (void) &dqvarnest; |
| 10907 | (void) &oldstyle; |
| 10908 | (void) &prevsyntax; |
| 10909 | (void) &syntax; |
| 10910 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10911 | startlinno = g_parsefile->linno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10912 | bqlist = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10913 | quotef = 0; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10914 | oldstyle = 0; |
| 10915 | prevsyntax = 0; |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10916 | #if ENABLE_ASH_EXPAND_PRMT |
| 10917 | pssyntax = (syntax == PSSYNTAX); |
| 10918 | if (pssyntax) |
| 10919 | syntax = DQSYNTAX; |
| 10920 | #endif |
| 10921 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10922 | varnest = 0; |
| 10923 | arinest = 0; |
| 10924 | parenlevel = 0; |
| 10925 | dqvarnest = 0; |
| 10926 | |
| 10927 | STARTSTACKSTR(out); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 10928 | loop: |
| 10929 | /* For each line, until end of word */ |
| 10930 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10931 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 10932 | for (;;) { /* until end of line or end of word */ |
| 10933 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 10934 | switch (SIT(c, syntax)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10935 | case CNL: /* '\n' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10936 | if (syntax == BASESYNTAX) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10937 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10938 | USTPUTC(c, out); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10939 | g_parsefile->linno++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10940 | if (doprompt) |
| 10941 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10942 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10943 | goto loop; /* continue outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10944 | case CWORD: |
| 10945 | USTPUTC(c, out); |
| 10946 | break; |
| 10947 | case CCTL: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10948 | if (eofmark == NULL || dblquote) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10949 | USTPUTC(CTLESC, out); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10950 | #if ENABLE_ASH_BASH_COMPAT |
| 10951 | if (c == '\\' && bash_dollar_squote) { |
| 10952 | c = decode_dollar_squote(); |
| 10953 | if (c & 0x100) { |
| 10954 | USTPUTC('\\', out); |
| 10955 | c = (unsigned char)c; |
| 10956 | } |
| 10957 | } |
| 10958 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10959 | USTPUTC(c, out); |
| 10960 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10961 | case CBACK: /* backslash */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 10962 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10963 | if (c == PEOF) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10964 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10965 | USTPUTC('\\', out); |
| 10966 | pungetc(); |
| 10967 | } else if (c == '\n') { |
| 10968 | if (doprompt) |
| 10969 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10970 | } else { |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10971 | #if ENABLE_ASH_EXPAND_PRMT |
| 10972 | if (c == '$' && pssyntax) { |
| 10973 | USTPUTC(CTLESC, out); |
| 10974 | USTPUTC('\\', out); |
| 10975 | } |
| 10976 | #endif |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10977 | if (dblquote && c != '\\' |
| 10978 | && c != '`' && c != '$' |
| 10979 | && (c != '"' || eofmark != NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10980 | ) { |
| 10981 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10982 | USTPUTC('\\', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10983 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10984 | if (SIT(c, SQSYNTAX) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10985 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10986 | USTPUTC(c, out); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10987 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10988 | } |
| 10989 | break; |
| 10990 | case CSQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10991 | syntax = SQSYNTAX; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10992 | quotemark: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10993 | if (eofmark == NULL) { |
| 10994 | USTPUTC(CTLQUOTEMARK, out); |
| 10995 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10996 | break; |
| 10997 | case CDQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10998 | syntax = DQSYNTAX; |
| 10999 | dblquote = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11000 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11001 | case CENDQUOTE: |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11002 | IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11003 | if (eofmark != NULL && arinest == 0 |
| 11004 | && varnest == 0 |
| 11005 | ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11006 | USTPUTC(c, out); |
| 11007 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11008 | if (dqvarnest == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11009 | syntax = BASESYNTAX; |
| 11010 | dblquote = 0; |
| 11011 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11012 | quotef = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11013 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11014 | } |
| 11015 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11016 | case CVAR: /* '$' */ |
| 11017 | PARSESUB(); /* parse substitution */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11018 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11019 | case CENDVAR: /* '}' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11020 | if (varnest > 0) { |
| 11021 | varnest--; |
| 11022 | if (dqvarnest > 0) { |
| 11023 | dqvarnest--; |
| 11024 | } |
| 11025 | USTPUTC(CTLENDVAR, out); |
| 11026 | } else { |
| 11027 | USTPUTC(c, out); |
| 11028 | } |
| 11029 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11030 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11031 | case CLP: /* '(' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11032 | parenlevel++; |
| 11033 | USTPUTC(c, out); |
| 11034 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11035 | case CRP: /* ')' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11036 | if (parenlevel > 0) { |
| 11037 | USTPUTC(c, out); |
| 11038 | --parenlevel; |
| 11039 | } else { |
| 11040 | if (pgetc() == ')') { |
| 11041 | if (--arinest == 0) { |
| 11042 | USTPUTC(CTLENDARI, out); |
| 11043 | syntax = prevsyntax; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11044 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11045 | } else |
| 11046 | USTPUTC(')', out); |
| 11047 | } else { |
| 11048 | /* |
| 11049 | * unbalanced parens |
| 11050 | * (don't 2nd guess - no error) |
| 11051 | */ |
| 11052 | pungetc(); |
| 11053 | USTPUTC(')', out); |
| 11054 | } |
| 11055 | } |
| 11056 | break; |
| 11057 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11058 | case CBQUOTE: /* '`' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11059 | PARSEBACKQOLD(); |
| 11060 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11061 | case CENDFILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11062 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11063 | case CIGN: |
| 11064 | break; |
| 11065 | default: |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11066 | if (varnest == 0) { |
| 11067 | #if ENABLE_ASH_BASH_COMPAT |
| 11068 | if (c == '&') { |
| 11069 | if (pgetc() == '>') |
| 11070 | c = 0x100 + '>'; /* flag &> */ |
| 11071 | pungetc(); |
| 11072 | } |
| 11073 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11074 | goto endword; /* exit outer loop */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11075 | } |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11076 | IF_ASH_ALIAS(if (c != PEOA)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11077 | USTPUTC(c, out); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 11078 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11079 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11080 | c = pgetc_fast(); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11081 | } /* for (;;) */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11082 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11083 | endword: |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11084 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11085 | if (syntax == ARISYNTAX) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11086 | raise_error_syntax("missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11087 | #endif |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 11088 | if (syntax != BASESYNTAX && !parsebackquote && eofmark == NULL) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11089 | raise_error_syntax("unterminated quoted string"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11090 | if (varnest != 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11091 | startlinno = g_parsefile->linno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11092 | /* { */ |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11093 | raise_error_syntax("missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11094 | } |
| 11095 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11096 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11097 | out = stackblock(); |
| 11098 | if (eofmark == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11099 | if ((c == '>' || c == '<' IF_ASH_BASH_COMPAT( || c == 0x100 + '>')) |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11100 | && quotef == 0 |
| 11101 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11102 | if (isdigit_str9(out)) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11103 | PARSEREDIR(); /* passed as params: out, c */ |
| 11104 | lasttoken = TREDIR; |
| 11105 | return lasttoken; |
| 11106 | } |
| 11107 | /* else: non-number X seen, interpret it |
| 11108 | * as "NNNX>file" = "NNNX >file" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11109 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11110 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11111 | } |
| 11112 | quoteflag = quotef; |
| 11113 | backquotelist = bqlist; |
| 11114 | grabstackblock(len); |
| 11115 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11116 | lasttoken = TWORD; |
| 11117 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11118 | /* end of readtoken routine */ |
| 11119 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11120 | /* |
| 11121 | * Check to see whether we are at the end of the here document. When this |
| 11122 | * is called, c is set to the first character of the next input line. If |
| 11123 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 11124 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11125 | checkend: { |
| 11126 | if (eofmark) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11127 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11128 | if (c == PEOA) |
| 11129 | c = pgetc_without_PEOA(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11130 | #endif |
| 11131 | if (striptabs) { |
| 11132 | while (c == '\t') { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11133 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11134 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11135 | } |
| 11136 | if (c == *eofmark) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11137 | if (pfgets(line, sizeof(line)) != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11138 | char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11139 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11140 | p = line; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11141 | for (q = eofmark + 1; *q && *p == *q; p++, q++) |
| 11142 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11143 | if (*p == '\n' && *q == '\0') { |
| 11144 | c = PEOF; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11145 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11146 | needprompt = doprompt; |
| 11147 | } else { |
| 11148 | pushstring(line, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11149 | } |
| 11150 | } |
| 11151 | } |
| 11152 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11153 | goto checkend_return; |
| 11154 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11155 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11156 | /* |
| 11157 | * Parse a redirection operator. The variable "out" points to a string |
| 11158 | * specifying the fd to be redirected. The variable "c" contains the |
| 11159 | * first character of the redirection operator. |
| 11160 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11161 | parseredir: { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11162 | /* out is already checked to be a valid number or "" */ |
| 11163 | int fd = (*out == '\0' ? -1 : atoi(out)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11164 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11165 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11166 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11167 | if (c == '>') { |
| 11168 | np->nfile.fd = 1; |
| 11169 | c = pgetc(); |
| 11170 | if (c == '>') |
| 11171 | np->type = NAPPEND; |
| 11172 | else if (c == '|') |
| 11173 | np->type = NCLOBBER; |
| 11174 | else if (c == '&') |
| 11175 | np->type = NTOFD; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11176 | /* 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] | 11177 | else { |
| 11178 | np->type = NTO; |
| 11179 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11180 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11181 | } |
| 11182 | #if ENABLE_ASH_BASH_COMPAT |
| 11183 | else if (c == 0x100 + '>') { /* this flags &> redirection */ |
| 11184 | np->nfile.fd = 1; |
| 11185 | pgetc(); /* this is '>', no need to check */ |
| 11186 | np->type = NTO2; |
| 11187 | } |
| 11188 | #endif |
| 11189 | else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11190 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11191 | c = pgetc(); |
| 11192 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11193 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11194 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11195 | np = stzalloc(sizeof(struct nhere)); |
| 11196 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11197 | } |
| 11198 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11199 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11200 | heredoc->here = np; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11201 | c = pgetc(); |
| 11202 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11203 | heredoc->striptabs = 1; |
| 11204 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11205 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11206 | pungetc(); |
| 11207 | } |
| 11208 | break; |
| 11209 | |
| 11210 | case '&': |
| 11211 | np->type = NFROMFD; |
| 11212 | break; |
| 11213 | |
| 11214 | case '>': |
| 11215 | np->type = NFROMTO; |
| 11216 | break; |
| 11217 | |
| 11218 | default: |
| 11219 | np->type = NFROM; |
| 11220 | pungetc(); |
| 11221 | break; |
| 11222 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11223 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11224 | if (fd >= 0) |
| 11225 | np->nfile.fd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11226 | redirnode = np; |
| 11227 | goto parseredir_return; |
| 11228 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11229 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11230 | /* |
| 11231 | * Parse a substitution. At this point, we have read the dollar sign |
| 11232 | * and nothing else. |
| 11233 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11234 | |
| 11235 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 11236 | * (assuming ascii char codes, as the original implementation did) */ |
| 11237 | #define is_special(c) \ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11238 | (((unsigned)(c) - 33 < 32) \ |
| 11239 | && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11240 | parsesub: { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11241 | unsigned char subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11242 | int typeloc; |
| 11243 | int flags; |
| 11244 | char *p; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11245 | static const char types[] ALIGN1 = "}-+?="; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11246 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11247 | c = pgetc(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11248 | if (c > 255 /* PEOA or PEOF */ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11249 | || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11250 | ) { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11251 | #if ENABLE_ASH_BASH_COMPAT |
| 11252 | if (c == '\'') |
| 11253 | bash_dollar_squote = 1; |
| 11254 | else |
| 11255 | #endif |
| 11256 | USTPUTC('$', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11257 | pungetc(); |
| 11258 | } else if (c == '(') { /* $(command) or $((arith)) */ |
| 11259 | if (pgetc() == '(') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11260 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11261 | PARSEARITH(); |
| 11262 | #else |
Mike Frysinger | 98a6f56 | 2008-06-09 09:38:45 +0000 | [diff] [blame] | 11263 | raise_error_syntax("you disabled math support for $((arith)) syntax"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11264 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11265 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11266 | pungetc(); |
| 11267 | PARSEBACKQNEW(); |
| 11268 | } |
| 11269 | } else { |
| 11270 | USTPUTC(CTLVAR, out); |
| 11271 | typeloc = out - (char *)stackblock(); |
| 11272 | USTPUTC(VSNORMAL, out); |
| 11273 | subtype = VSNORMAL; |
| 11274 | if (c == '{') { |
| 11275 | c = pgetc(); |
| 11276 | if (c == '#') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11277 | c = pgetc(); |
| 11278 | if (c == '}') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11279 | c = '#'; |
| 11280 | else |
| 11281 | subtype = VSLENGTH; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11282 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11283 | subtype = 0; |
| 11284 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11285 | if (c <= 255 /* not PEOA or PEOF */ && is_name(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11286 | do { |
| 11287 | STPUTC(c, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11288 | c = pgetc(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11289 | } while (c <= 255 /* not PEOA or PEOF */ && is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11290 | } else if (isdigit(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11291 | do { |
| 11292 | STPUTC(c, out); |
| 11293 | c = pgetc(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11294 | } while (isdigit(c)); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11295 | } else if (is_special(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11296 | USTPUTC(c, out); |
| 11297 | c = pgetc(); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11298 | } else { |
| 11299 | badsub: |
| 11300 | raise_error_syntax("bad substitution"); |
| 11301 | } |
Cristian Ionescu-Idbohrn | 301f5ec | 2009-10-05 02:07:23 +0200 | [diff] [blame] | 11302 | if (c != '}' && subtype == VSLENGTH) |
| 11303 | goto badsub; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11304 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11305 | STPUTC('=', out); |
| 11306 | flags = 0; |
| 11307 | if (subtype == 0) { |
| 11308 | switch (c) { |
| 11309 | case ':': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11310 | c = pgetc(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11311 | #if ENABLE_ASH_BASH_COMPAT |
| 11312 | if (c == ':' || c == '$' || isdigit(c)) { |
| 11313 | pungetc(); |
| 11314 | subtype = VSSUBSTR; |
| 11315 | break; |
| 11316 | } |
| 11317 | #endif |
| 11318 | flags = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11319 | /*FALLTHROUGH*/ |
| 11320 | default: |
| 11321 | p = strchr(types, c); |
| 11322 | if (p == NULL) |
| 11323 | goto badsub; |
| 11324 | subtype = p - types + VSNORMAL; |
| 11325 | break; |
| 11326 | case '%': |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11327 | case '#': { |
| 11328 | int cc = c; |
| 11329 | subtype = c == '#' ? VSTRIMLEFT : VSTRIMRIGHT; |
| 11330 | c = pgetc(); |
| 11331 | if (c == cc) |
| 11332 | subtype++; |
| 11333 | else |
| 11334 | pungetc(); |
| 11335 | break; |
| 11336 | } |
| 11337 | #if ENABLE_ASH_BASH_COMPAT |
| 11338 | case '/': |
| 11339 | subtype = VSREPLACE; |
| 11340 | c = pgetc(); |
| 11341 | if (c == '/') |
| 11342 | subtype++; /* VSREPLACEALL */ |
| 11343 | else |
| 11344 | pungetc(); |
| 11345 | break; |
| 11346 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11347 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11348 | } else { |
| 11349 | pungetc(); |
| 11350 | } |
| 11351 | if (dblquote || arinest) |
| 11352 | flags |= VSQUOTE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11353 | ((unsigned char *)stackblock())[typeloc] = subtype | flags; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11354 | if (subtype != VSNORMAL) { |
| 11355 | varnest++; |
| 11356 | if (dblquote || arinest) { |
| 11357 | dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11358 | } |
| 11359 | } |
| 11360 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11361 | goto parsesub_return; |
| 11362 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11363 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11364 | /* |
| 11365 | * Called to parse command substitutions. Newstyle is set if the command |
| 11366 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 11367 | * list of commands (passed by reference), and savelen is the number of |
| 11368 | * characters on the top of the stack which must be preserved. |
| 11369 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11370 | parsebackq: { |
| 11371 | struct nodelist **nlpp; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11372 | smallint savepbq; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11373 | union node *n; |
| 11374 | char *volatile str; |
| 11375 | struct jmploc jmploc; |
| 11376 | struct jmploc *volatile savehandler; |
| 11377 | size_t savelen; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11378 | smallint saveprompt = 0; |
| 11379 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11380 | #ifdef __GNUC__ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11381 | (void) &saveprompt; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11382 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11383 | savepbq = parsebackquote; |
| 11384 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 11385 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11386 | parsebackquote = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11387 | exception_handler = savehandler; |
| 11388 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11389 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11390 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11391 | str = NULL; |
| 11392 | savelen = out - (char *)stackblock(); |
| 11393 | if (savelen > 0) { |
| 11394 | str = ckmalloc(savelen); |
| 11395 | memcpy(str, stackblock(), savelen); |
| 11396 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11397 | savehandler = exception_handler; |
| 11398 | exception_handler = &jmploc; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11399 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11400 | if (oldstyle) { |
| 11401 | /* We must read until the closing backquote, giving special |
| 11402 | treatment to some slashes, and then push the string and |
| 11403 | reread it as input, interpreting it normally. */ |
| 11404 | char *pout; |
| 11405 | int pc; |
| 11406 | size_t psavelen; |
| 11407 | char *pstr; |
| 11408 | |
| 11409 | |
| 11410 | STARTSTACKSTR(pout); |
| 11411 | for (;;) { |
| 11412 | if (needprompt) { |
| 11413 | setprompt(2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11414 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11415 | pc = pgetc(); |
| 11416 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11417 | case '`': |
| 11418 | goto done; |
| 11419 | |
| 11420 | case '\\': |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11421 | pc = pgetc(); |
| 11422 | if (pc == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11423 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11424 | if (doprompt) |
| 11425 | setprompt(2); |
| 11426 | /* |
| 11427 | * If eating a newline, avoid putting |
| 11428 | * the newline into the new character |
| 11429 | * stream (via the STPUTC after the |
| 11430 | * switch). |
| 11431 | */ |
| 11432 | continue; |
| 11433 | } |
| 11434 | if (pc != '\\' && pc != '`' && pc != '$' |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11435 | && (!dblquote || pc != '"') |
| 11436 | ) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11437 | STPUTC('\\', pout); |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11438 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11439 | if (pc <= 255 /* not PEOA or PEOF */) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11440 | break; |
| 11441 | } |
| 11442 | /* fall through */ |
| 11443 | |
| 11444 | case PEOF: |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11445 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11446 | startlinno = g_parsefile->linno; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11447 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11448 | |
| 11449 | case '\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 | needprompt = doprompt; |
| 11452 | break; |
| 11453 | |
| 11454 | default: |
| 11455 | break; |
| 11456 | } |
| 11457 | STPUTC(pc, pout); |
| 11458 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11459 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11460 | STPUTC('\0', pout); |
| 11461 | psavelen = pout - (char *)stackblock(); |
| 11462 | if (psavelen > 0) { |
| 11463 | pstr = grabstackstr(pout); |
| 11464 | setinputstring(pstr); |
| 11465 | } |
| 11466 | } |
| 11467 | nlpp = &bqlist; |
| 11468 | while (*nlpp) |
| 11469 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11470 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 11471 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11472 | parsebackquote = oldstyle; |
| 11473 | |
| 11474 | if (oldstyle) { |
| 11475 | saveprompt = doprompt; |
| 11476 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11477 | } |
| 11478 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11479 | n = list(2); |
| 11480 | |
| 11481 | if (oldstyle) |
| 11482 | doprompt = saveprompt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11483 | else if (readtoken() != TRP) |
| 11484 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11485 | |
| 11486 | (*nlpp)->n = n; |
| 11487 | if (oldstyle) { |
| 11488 | /* |
| 11489 | * Start reading from old file again, ignoring any pushed back |
| 11490 | * tokens left from the backquote parsing |
| 11491 | */ |
| 11492 | popfile(); |
| 11493 | tokpushback = 0; |
| 11494 | } |
| 11495 | while (stackblocksize() <= savelen) |
| 11496 | growstackblock(); |
| 11497 | STARTSTACKSTR(out); |
| 11498 | if (str) { |
| 11499 | memcpy(out, str, savelen); |
| 11500 | STADJUST(savelen, out); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11501 | INT_OFF; |
| 11502 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11503 | str = NULL; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11504 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11505 | } |
| 11506 | parsebackquote = savepbq; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11507 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11508 | if (arinest || dblquote) |
| 11509 | USTPUTC(CTLBACKQ | CTLQUOTE, out); |
| 11510 | else |
| 11511 | USTPUTC(CTLBACKQ, out); |
| 11512 | if (oldstyle) |
| 11513 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11514 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11515 | } |
| 11516 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11517 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11518 | /* |
| 11519 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 11520 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11521 | parsearith: { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11522 | if (++arinest == 1) { |
| 11523 | prevsyntax = syntax; |
| 11524 | syntax = ARISYNTAX; |
| 11525 | USTPUTC(CTLARI, out); |
| 11526 | if (dblquote) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11527 | USTPUTC('"', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11528 | else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11529 | USTPUTC(' ', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11530 | } else { |
| 11531 | /* |
| 11532 | * we collapse embedded arithmetic expansion to |
| 11533 | * parenthesis, which should be equivalent |
| 11534 | */ |
| 11535 | USTPUTC('(', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11536 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11537 | goto parsearith_return; |
| 11538 | } |
| 11539 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11540 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11541 | } /* end of readtoken */ |
| 11542 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11543 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11544 | * Read the next input token. |
| 11545 | * If the token is a word, we set backquotelist to the list of cmds in |
| 11546 | * backquotes. We set quoteflag to true if any part of the word was |
| 11547 | * quoted. |
| 11548 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 11549 | * the redirection. |
| 11550 | * In all cases, the variable startlinno is set to the number of the line |
| 11551 | * on which the token starts. |
| 11552 | * |
| 11553 | * [Change comment: here documents and internal procedures] |
| 11554 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 11555 | * word parsing code into a separate routine. In this case, readtoken |
| 11556 | * doesn't need to have any internal procedures, but parseword does. |
| 11557 | * We could also make parseoperator in essence the main routine, and |
| 11558 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11559 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11560 | #define NEW_xxreadtoken |
| 11561 | #ifdef NEW_xxreadtoken |
| 11562 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11563 | static const char xxreadtoken_chars[7] ALIGN1 = { |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11564 | '\n', '(', ')', /* singles */ |
| 11565 | '&', '|', ';', /* doubles */ |
| 11566 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11567 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11568 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11569 | #define xxreadtoken_singles 3 |
| 11570 | #define xxreadtoken_doubles 3 |
| 11571 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11572 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11573 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 11574 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 11575 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11576 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11577 | }; |
| 11578 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11579 | static int |
| 11580 | xxreadtoken(void) |
| 11581 | { |
| 11582 | int c; |
| 11583 | |
| 11584 | if (tokpushback) { |
| 11585 | tokpushback = 0; |
| 11586 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11587 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11588 | if (needprompt) { |
| 11589 | setprompt(2); |
| 11590 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11591 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11592 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11593 | c = pgetc_fast(); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11594 | if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA)) |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11595 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11596 | |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11597 | if (c == '#') { |
| 11598 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11599 | continue; |
| 11600 | pungetc(); |
| 11601 | } else if (c == '\\') { |
| 11602 | if (pgetc() != '\n') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11603 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11604 | break; /* return readtoken1(...) */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11605 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11606 | startlinno = ++g_parsefile->linno; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11607 | if (doprompt) |
| 11608 | setprompt(2); |
| 11609 | } else { |
| 11610 | const char *p; |
| 11611 | |
| 11612 | p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 11613 | if (c != PEOF) { |
| 11614 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11615 | g_parsefile->linno++; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11616 | needprompt = doprompt; |
| 11617 | } |
| 11618 | |
| 11619 | p = strchr(xxreadtoken_chars, c); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11620 | if (p == NULL) |
| 11621 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11622 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11623 | if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
| 11624 | int cc = pgetc(); |
| 11625 | if (cc == c) { /* double occurrence? */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11626 | p += xxreadtoken_doubles + 1; |
| 11627 | } else { |
| 11628 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11629 | #if ENABLE_ASH_BASH_COMPAT |
| 11630 | if (c == '&' && cc == '>') /* &> */ |
| 11631 | break; /* return readtoken1(...) */ |
| 11632 | #endif |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11633 | } |
| 11634 | } |
| 11635 | } |
| 11636 | lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 11637 | return lasttoken; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11638 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11639 | } /* for (;;) */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11640 | |
| 11641 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11642 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11643 | #else /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11644 | #define RETURN(token) return lasttoken = token |
| 11645 | static int |
| 11646 | xxreadtoken(void) |
| 11647 | { |
| 11648 | int c; |
| 11649 | |
| 11650 | if (tokpushback) { |
| 11651 | tokpushback = 0; |
| 11652 | return lasttoken; |
| 11653 | } |
| 11654 | if (needprompt) { |
| 11655 | setprompt(2); |
| 11656 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11657 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11658 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11659 | c = pgetc_fast(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11660 | switch (c) { |
| 11661 | case ' ': case '\t': |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11662 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11663 | continue; |
| 11664 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11665 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11666 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11667 | pungetc(); |
| 11668 | continue; |
| 11669 | case '\\': |
| 11670 | if (pgetc() == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11671 | startlinno = ++g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11672 | if (doprompt) |
| 11673 | setprompt(2); |
| 11674 | continue; |
| 11675 | } |
| 11676 | pungetc(); |
| 11677 | goto breakloop; |
| 11678 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11679 | g_parsefile->linno++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11680 | needprompt = doprompt; |
| 11681 | RETURN(TNL); |
| 11682 | case PEOF: |
| 11683 | RETURN(TEOF); |
| 11684 | case '&': |
| 11685 | if (pgetc() == '&') |
| 11686 | RETURN(TAND); |
| 11687 | pungetc(); |
| 11688 | RETURN(TBACKGND); |
| 11689 | case '|': |
| 11690 | if (pgetc() == '|') |
| 11691 | RETURN(TOR); |
| 11692 | pungetc(); |
| 11693 | RETURN(TPIPE); |
| 11694 | case ';': |
| 11695 | if (pgetc() == ';') |
| 11696 | RETURN(TENDCASE); |
| 11697 | pungetc(); |
| 11698 | RETURN(TSEMI); |
| 11699 | case '(': |
| 11700 | RETURN(TLP); |
| 11701 | case ')': |
| 11702 | RETURN(TRP); |
| 11703 | default: |
| 11704 | goto breakloop; |
| 11705 | } |
| 11706 | } |
| 11707 | breakloop: |
| 11708 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 11709 | #undef RETURN |
| 11710 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11711 | #endif /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11712 | |
| 11713 | static int |
| 11714 | readtoken(void) |
| 11715 | { |
| 11716 | int t; |
| 11717 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11718 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11719 | #endif |
| 11720 | |
| 11721 | #if ENABLE_ASH_ALIAS |
| 11722 | top: |
| 11723 | #endif |
| 11724 | |
| 11725 | t = xxreadtoken(); |
| 11726 | |
| 11727 | /* |
| 11728 | * eat newlines |
| 11729 | */ |
| 11730 | if (checkkwd & CHKNL) { |
| 11731 | while (t == TNL) { |
| 11732 | parseheredoc(); |
| 11733 | t = xxreadtoken(); |
| 11734 | } |
| 11735 | } |
| 11736 | |
| 11737 | if (t != TWORD || quoteflag) { |
| 11738 | goto out; |
| 11739 | } |
| 11740 | |
| 11741 | /* |
| 11742 | * check for keywords |
| 11743 | */ |
| 11744 | if (checkkwd & CHKKWD) { |
| 11745 | const char *const *pp; |
| 11746 | |
| 11747 | pp = findkwd(wordtext); |
| 11748 | if (pp) { |
| 11749 | lasttoken = t = pp - tokname_array; |
| 11750 | TRACE(("keyword %s recognized\n", tokname(t))); |
| 11751 | goto out; |
| 11752 | } |
| 11753 | } |
| 11754 | |
| 11755 | if (checkkwd & CHKALIAS) { |
| 11756 | #if ENABLE_ASH_ALIAS |
| 11757 | struct alias *ap; |
| 11758 | ap = lookupalias(wordtext, 1); |
| 11759 | if (ap != NULL) { |
| 11760 | if (*ap->val) { |
| 11761 | pushstring(ap->val, ap); |
| 11762 | } |
| 11763 | goto top; |
| 11764 | } |
| 11765 | #endif |
| 11766 | } |
| 11767 | out: |
| 11768 | checkkwd = 0; |
| 11769 | #if DEBUG |
| 11770 | if (!alreadyseen) |
| 11771 | TRACE(("token %s %s\n", tokname(t), t == TWORD ? wordtext : "")); |
| 11772 | else |
| 11773 | TRACE(("reread token %s %s\n", tokname(t), t == TWORD ? wordtext : "")); |
| 11774 | #endif |
| 11775 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11776 | } |
| 11777 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11778 | static char |
| 11779 | peektoken(void) |
| 11780 | { |
| 11781 | int t; |
| 11782 | |
| 11783 | t = readtoken(); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11784 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11785 | return tokname_array[t][0]; |
| 11786 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11787 | |
| 11788 | /* |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11789 | * Read and parse a command. Returns NODE_EOF on end of file. |
| 11790 | * (NULL is a valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11791 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11792 | static union node * |
| 11793 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11794 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11795 | int t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11796 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11797 | tokpushback = 0; |
| 11798 | doprompt = interact; |
| 11799 | if (doprompt) |
| 11800 | setprompt(doprompt); |
| 11801 | needprompt = 0; |
| 11802 | t = readtoken(); |
| 11803 | if (t == TEOF) |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11804 | return NODE_EOF; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11805 | if (t == TNL) |
| 11806 | return NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11807 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11808 | return list(1); |
| 11809 | } |
| 11810 | |
| 11811 | /* |
| 11812 | * Input any here documents. |
| 11813 | */ |
| 11814 | static void |
| 11815 | parseheredoc(void) |
| 11816 | { |
| 11817 | struct heredoc *here; |
| 11818 | union node *n; |
| 11819 | |
| 11820 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11821 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11822 | |
| 11823 | while (here) { |
| 11824 | if (needprompt) { |
| 11825 | setprompt(2); |
| 11826 | } |
| 11827 | readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, |
| 11828 | here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11829 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11830 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11831 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11832 | n->narg.text = wordtext; |
| 11833 | n->narg.backquote = backquotelist; |
| 11834 | here->here->nhere.doc = n; |
| 11835 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11836 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11837 | } |
| 11838 | |
| 11839 | |
| 11840 | /* |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 11841 | * called by editline -- any expansions to the prompt should be added here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11842 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11843 | #if ENABLE_ASH_EXPAND_PRMT |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11844 | static const char * |
| 11845 | expandstr(const char *ps) |
| 11846 | { |
| 11847 | union node n; |
| 11848 | |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 11849 | /* XXX Fix (char *) cast. It _is_ a bug. ps is variable's value, |
| 11850 | * and token processing _can_ alter it (delete NULs etc). */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11851 | setinputstring((char *)ps); |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11852 | readtoken1(pgetc(), PSSYNTAX, nullstr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11853 | popfile(); |
| 11854 | |
| 11855 | n.narg.type = NARG; |
| 11856 | n.narg.next = NULL; |
| 11857 | n.narg.text = wordtext; |
| 11858 | n.narg.backquote = backquotelist; |
| 11859 | |
| 11860 | expandarg(&n, NULL, 0); |
| 11861 | return stackblock(); |
| 11862 | } |
| 11863 | #endif |
| 11864 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11865 | /* |
| 11866 | * Execute a command or commands contained in a string. |
| 11867 | */ |
| 11868 | static int |
| 11869 | evalstring(char *s, int mask) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11870 | { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11871 | union node *n; |
| 11872 | struct stackmark smark; |
| 11873 | int skip; |
| 11874 | |
| 11875 | setinputstring(s); |
| 11876 | setstackmark(&smark); |
| 11877 | |
| 11878 | skip = 0; |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11879 | while ((n = parsecmd(0)) != NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11880 | evaltree(n, 0); |
| 11881 | popstackmark(&smark); |
| 11882 | skip = evalskip; |
| 11883 | if (skip) |
| 11884 | break; |
| 11885 | } |
| 11886 | popfile(); |
| 11887 | |
| 11888 | skip &= mask; |
| 11889 | evalskip = skip; |
| 11890 | return skip; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11891 | } |
| 11892 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11893 | /* |
| 11894 | * The eval command. |
| 11895 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11896 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11897 | evalcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11898 | { |
| 11899 | char *p; |
| 11900 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11901 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11902 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11903 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11904 | argv += 2; |
| 11905 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11906 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11907 | for (;;) { |
| 11908 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11909 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11910 | if (p == NULL) |
| 11911 | break; |
| 11912 | STPUTC(' ', concat); |
| 11913 | } |
| 11914 | STPUTC('\0', concat); |
| 11915 | p = grabstackstr(concat); |
| 11916 | } |
| 11917 | evalstring(p, ~SKIPEVAL); |
| 11918 | |
| 11919 | } |
| 11920 | return exitstatus; |
| 11921 | } |
| 11922 | |
| 11923 | /* |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 11924 | * Read and execute commands. |
| 11925 | * "Top" is nonzero for the top level command loop; |
| 11926 | * it turns on prompting if the shell is interactive. |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11927 | */ |
| 11928 | static int |
| 11929 | cmdloop(int top) |
| 11930 | { |
| 11931 | union node *n; |
| 11932 | struct stackmark smark; |
| 11933 | int inter; |
| 11934 | int numeof = 0; |
| 11935 | |
| 11936 | TRACE(("cmdloop(%d) called\n", top)); |
| 11937 | for (;;) { |
| 11938 | int skip; |
| 11939 | |
| 11940 | setstackmark(&smark); |
| 11941 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11942 | if (doing_jobctl) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11943 | showjobs(stderr, SHOW_CHANGED); |
| 11944 | #endif |
| 11945 | inter = 0; |
| 11946 | if (iflag && top) { |
| 11947 | inter++; |
| 11948 | #if ENABLE_ASH_MAIL |
| 11949 | chkmail(); |
| 11950 | #endif |
| 11951 | } |
| 11952 | n = parsecmd(inter); |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 11953 | #if DEBUG |
| 11954 | if (DEBUG > 2 && debug && (n != NODE_EOF)) |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 11955 | showtree(n); |
Denis Vlasenko | 135cecb | 2009-04-12 00:00:57 +0000 | [diff] [blame] | 11956 | #endif |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11957 | if (n == NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11958 | if (!top || numeof >= 50) |
| 11959 | break; |
| 11960 | if (!stoppedjobs()) { |
| 11961 | if (!Iflag) |
| 11962 | break; |
| 11963 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 11964 | } |
| 11965 | numeof++; |
| 11966 | } else if (nflag == 0) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 11967 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 11968 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11969 | numeof = 0; |
| 11970 | evaltree(n, 0); |
| 11971 | } |
| 11972 | popstackmark(&smark); |
| 11973 | skip = evalskip; |
| 11974 | |
| 11975 | if (skip) { |
| 11976 | evalskip = 0; |
| 11977 | return skip & SKIPEVAL; |
| 11978 | } |
| 11979 | } |
| 11980 | return 0; |
| 11981 | } |
| 11982 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11983 | /* |
| 11984 | * Take commands from a file. To be compatible we should do a path |
| 11985 | * search for the file, which is necessary to find sub-commands. |
| 11986 | */ |
| 11987 | static char * |
| 11988 | find_dot_file(char *name) |
| 11989 | { |
| 11990 | char *fullname; |
| 11991 | const char *path = pathval(); |
| 11992 | struct stat statb; |
| 11993 | |
| 11994 | /* don't try this for absolute or relative paths */ |
| 11995 | if (strchr(name, '/')) |
| 11996 | return name; |
| 11997 | |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 11998 | /* IIRC standards do not say whether . is to be searched. |
| 11999 | * And it is even smaller this way, making it unconditional for now: |
| 12000 | */ |
| 12001 | if (1) { /* ENABLE_ASH_BASH_COMPAT */ |
| 12002 | fullname = name; |
| 12003 | goto try_cur_dir; |
| 12004 | } |
| 12005 | |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12006 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12007 | try_cur_dir: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12008 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 12009 | /* |
| 12010 | * Don't bother freeing here, since it will |
| 12011 | * be freed by the caller. |
| 12012 | */ |
| 12013 | return fullname; |
| 12014 | } |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12015 | if (fullname != name) |
| 12016 | stunalloc(fullname); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12017 | } |
| 12018 | |
| 12019 | /* not found in the PATH */ |
| 12020 | ash_msg_and_raise_error("%s: not found", name); |
| 12021 | /* NOTREACHED */ |
| 12022 | } |
| 12023 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12024 | static int FAST_FUNC |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12025 | dotcmd(int argc, char **argv) |
| 12026 | { |
| 12027 | struct strlist *sp; |
| 12028 | volatile struct shparam saveparam; |
| 12029 | int status = 0; |
| 12030 | |
| 12031 | for (sp = cmdenviron; sp; sp = sp->next) |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 12032 | setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12033 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12034 | if (argv[1]) { /* That's what SVR2 does */ |
| 12035 | char *fullname = find_dot_file(argv[1]); |
| 12036 | argv += 2; |
| 12037 | argc -= 2; |
| 12038 | if (argc) { /* argc > 0, argv[0] != NULL */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12039 | saveparam = shellparam; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12040 | shellparam.malloced = 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12041 | shellparam.nparam = argc; |
| 12042 | shellparam.p = argv; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12043 | }; |
| 12044 | |
| 12045 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 12046 | commandname = fullname; |
| 12047 | cmdloop(0); |
| 12048 | popfile(); |
| 12049 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12050 | if (argc) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12051 | freeparam(&shellparam); |
| 12052 | shellparam = saveparam; |
| 12053 | }; |
| 12054 | status = exitstatus; |
| 12055 | } |
| 12056 | return status; |
| 12057 | } |
| 12058 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12059 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12060 | exitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12061 | { |
| 12062 | if (stoppedjobs()) |
| 12063 | return 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12064 | if (argv[1]) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12065 | exitstatus = number(argv[1]); |
| 12066 | raise_exception(EXEXIT); |
| 12067 | /* NOTREACHED */ |
| 12068 | } |
| 12069 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12070 | /* |
| 12071 | * Read a file containing shell functions. |
| 12072 | */ |
| 12073 | static void |
| 12074 | readcmdfile(char *name) |
| 12075 | { |
| 12076 | setinputfile(name, INPUT_PUSH_FILE); |
| 12077 | cmdloop(0); |
| 12078 | popfile(); |
| 12079 | } |
| 12080 | |
| 12081 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12082 | /* ============ find_command inplementation */ |
| 12083 | |
| 12084 | /* |
| 12085 | * Resolve a command name. If you change this routine, you may have to |
| 12086 | * change the shellexec routine as well. |
| 12087 | */ |
| 12088 | static void |
| 12089 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 12090 | { |
| 12091 | struct tblentry *cmdp; |
| 12092 | int idx; |
| 12093 | int prev; |
| 12094 | char *fullname; |
| 12095 | struct stat statb; |
| 12096 | int e; |
| 12097 | int updatetbl; |
| 12098 | struct builtincmd *bcmd; |
| 12099 | |
| 12100 | /* If name contains a slash, don't use PATH or hash table */ |
| 12101 | if (strchr(name, '/') != NULL) { |
| 12102 | entry->u.index = -1; |
| 12103 | if (act & DO_ABS) { |
| 12104 | while (stat(name, &statb) < 0) { |
| 12105 | #ifdef SYSV |
| 12106 | if (errno == EINTR) |
| 12107 | continue; |
| 12108 | #endif |
| 12109 | entry->cmdtype = CMDUNKNOWN; |
| 12110 | return; |
| 12111 | } |
| 12112 | } |
| 12113 | entry->cmdtype = CMDNORMAL; |
| 12114 | return; |
| 12115 | } |
| 12116 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12117 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12118 | |
| 12119 | updatetbl = (path == pathval()); |
| 12120 | if (!updatetbl) { |
| 12121 | act |= DO_ALTPATH; |
| 12122 | if (strstr(path, "%builtin") != NULL) |
| 12123 | act |= DO_ALTBLTIN; |
| 12124 | } |
| 12125 | |
| 12126 | /* If name is in the table, check answer will be ok */ |
| 12127 | cmdp = cmdlookup(name, 0); |
| 12128 | if (cmdp != NULL) { |
| 12129 | int bit; |
| 12130 | |
| 12131 | switch (cmdp->cmdtype) { |
| 12132 | default: |
| 12133 | #if DEBUG |
| 12134 | abort(); |
| 12135 | #endif |
| 12136 | case CMDNORMAL: |
| 12137 | bit = DO_ALTPATH; |
| 12138 | break; |
| 12139 | case CMDFUNCTION: |
| 12140 | bit = DO_NOFUNC; |
| 12141 | break; |
| 12142 | case CMDBUILTIN: |
| 12143 | bit = DO_ALTBLTIN; |
| 12144 | break; |
| 12145 | } |
| 12146 | if (act & bit) { |
| 12147 | updatetbl = 0; |
| 12148 | cmdp = NULL; |
| 12149 | } else if (cmdp->rehash == 0) |
| 12150 | /* if not invalidated by cd, we're done */ |
| 12151 | goto success; |
| 12152 | } |
| 12153 | |
| 12154 | /* If %builtin not in path, check for builtin next */ |
| 12155 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12156 | if (bcmd) { |
| 12157 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 12158 | goto builtin_success; |
| 12159 | if (act & DO_ALTPATH) { |
| 12160 | if (!(act & DO_ALTBLTIN)) |
| 12161 | goto builtin_success; |
| 12162 | } else if (builtinloc <= 0) { |
| 12163 | goto builtin_success; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 12164 | } |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12165 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12166 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12167 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 12168 | { |
| 12169 | int applet_no = find_applet_by_name(name); |
| 12170 | if (applet_no >= 0) { |
| 12171 | entry->cmdtype = CMDNORMAL; |
| 12172 | entry->u.index = -2 - applet_no; |
| 12173 | return; |
| 12174 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12175 | } |
| 12176 | #endif |
| 12177 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12178 | /* We have to search path. */ |
| 12179 | prev = -1; /* where to start */ |
| 12180 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 12181 | if (cmdp->cmdtype == CMDBUILTIN) |
| 12182 | prev = builtinloc; |
| 12183 | else |
| 12184 | prev = cmdp->param.index; |
| 12185 | } |
| 12186 | |
| 12187 | e = ENOENT; |
| 12188 | idx = -1; |
| 12189 | loop: |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12190 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12191 | stunalloc(fullname); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12192 | /* NB: code below will still use fullname |
| 12193 | * despite it being "unallocated" */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12194 | idx++; |
| 12195 | if (pathopt) { |
| 12196 | if (prefix(pathopt, "builtin")) { |
| 12197 | if (bcmd) |
| 12198 | goto builtin_success; |
| 12199 | continue; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 12200 | } |
| 12201 | if ((act & DO_NOFUNC) |
| 12202 | || !prefix(pathopt, "func") |
| 12203 | ) { /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12204 | continue; |
| 12205 | } |
| 12206 | } |
| 12207 | /* if rehash, don't redo absolute path names */ |
| 12208 | if (fullname[0] == '/' && idx <= prev) { |
| 12209 | if (idx < prev) |
| 12210 | continue; |
| 12211 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 12212 | goto success; |
| 12213 | } |
| 12214 | while (stat(fullname, &statb) < 0) { |
| 12215 | #ifdef SYSV |
| 12216 | if (errno == EINTR) |
| 12217 | continue; |
| 12218 | #endif |
| 12219 | if (errno != ENOENT && errno != ENOTDIR) |
| 12220 | e = errno; |
| 12221 | goto loop; |
| 12222 | } |
| 12223 | e = EACCES; /* if we fail, this will be the error */ |
| 12224 | if (!S_ISREG(statb.st_mode)) |
| 12225 | continue; |
| 12226 | if (pathopt) { /* this is a %func directory */ |
| 12227 | stalloc(strlen(fullname) + 1); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12228 | /* NB: stalloc will return space pointed by fullname |
| 12229 | * (because we don't have any intervening allocations |
| 12230 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12231 | readcmdfile(fullname); |
| 12232 | cmdp = cmdlookup(name, 0); |
| 12233 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 12234 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 12235 | stunalloc(fullname); |
| 12236 | goto success; |
| 12237 | } |
| 12238 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 12239 | if (!updatetbl) { |
| 12240 | entry->cmdtype = CMDNORMAL; |
| 12241 | entry->u.index = idx; |
| 12242 | return; |
| 12243 | } |
| 12244 | INT_OFF; |
| 12245 | cmdp = cmdlookup(name, 1); |
| 12246 | cmdp->cmdtype = CMDNORMAL; |
| 12247 | cmdp->param.index = idx; |
| 12248 | INT_ON; |
| 12249 | goto success; |
| 12250 | } |
| 12251 | |
| 12252 | /* We failed. If there was an entry for this command, delete it */ |
| 12253 | if (cmdp && updatetbl) |
| 12254 | delete_cmd_entry(); |
| 12255 | if (act & DO_ERR) |
| 12256 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
| 12257 | entry->cmdtype = CMDUNKNOWN; |
| 12258 | return; |
| 12259 | |
| 12260 | builtin_success: |
| 12261 | if (!updatetbl) { |
| 12262 | entry->cmdtype = CMDBUILTIN; |
| 12263 | entry->u.cmd = bcmd; |
| 12264 | return; |
| 12265 | } |
| 12266 | INT_OFF; |
| 12267 | cmdp = cmdlookup(name, 1); |
| 12268 | cmdp->cmdtype = CMDBUILTIN; |
| 12269 | cmdp->param.cmd = bcmd; |
| 12270 | INT_ON; |
| 12271 | success: |
| 12272 | cmdp->rehash = 0; |
| 12273 | entry->cmdtype = cmdp->cmdtype; |
| 12274 | entry->u = cmdp->param; |
| 12275 | } |
| 12276 | |
| 12277 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12278 | /* ============ trap.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12279 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12280 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12281 | * The trap builtin. |
| 12282 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12283 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12284 | trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12285 | { |
| 12286 | char *action; |
| 12287 | char **ap; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12288 | int signo, exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12289 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12290 | nextopt(nullstr); |
| 12291 | ap = argptr; |
| 12292 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12293 | for (signo = 0; signo < NSIG; signo++) { |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12294 | char *tr = trap_ptr[signo]; |
| 12295 | if (tr) { |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12296 | /* note: bash adds "SIG", but only if invoked |
| 12297 | * as "bash". If called as "sh", or if set -o posix, |
| 12298 | * then it prints short signal names. |
| 12299 | * We are printing short names: */ |
| 12300 | out1fmt("trap -- %s %s\n", |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12301 | single_quote(tr), |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12302 | get_signame(signo)); |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12303 | /* trap_ptr != trap only if we are in special-cased `trap` code. |
| 12304 | * In this case, we will exit very soon, no need to free(). */ |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12305 | /* if (trap_ptr != trap && tp[0]) */ |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12306 | /* free(tr); */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12307 | } |
| 12308 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12309 | /* |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12310 | if (trap_ptr != trap) { |
| 12311 | free(trap_ptr); |
| 12312 | trap_ptr = trap; |
| 12313 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12314 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12315 | return 0; |
| 12316 | } |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12317 | |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12318 | action = NULL; |
| 12319 | if (ap[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12320 | action = *ap++; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12321 | exitcode = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12322 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12323 | signo = get_signum(*ap); |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12324 | if (signo < 0) { |
| 12325 | /* Mimic bash message exactly */ |
| 12326 | ash_msg("%s: invalid signal specification", *ap); |
| 12327 | exitcode = 1; |
| 12328 | goto next; |
| 12329 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12330 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12331 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 12332 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12333 | action = NULL; |
| 12334 | else |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12335 | action = ckstrdup(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12336 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 12337 | free(trap[signo]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12338 | trap[signo] = action; |
| 12339 | if (signo != 0) |
| 12340 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12341 | INT_ON; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12342 | next: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12343 | ap++; |
| 12344 | } |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12345 | return exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12346 | } |
| 12347 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12348 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12349 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12350 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 12351 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12352 | /* |
| 12353 | * Lists available builtins |
| 12354 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12355 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12356 | helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12357 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 12358 | unsigned col; |
| 12359 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12360 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 12361 | out1fmt( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 12362 | "Built-in commands:\n" |
| 12363 | "------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 12364 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12365 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 12366 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12367 | if (col > 60) { |
| 12368 | out1fmt("\n"); |
| 12369 | col = 0; |
| 12370 | } |
| 12371 | } |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 12372 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 12373 | { |
| 12374 | const char *a = applet_names; |
| 12375 | while (*a) { |
| 12376 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 12377 | if (col > 60) { |
| 12378 | out1fmt("\n"); |
| 12379 | col = 0; |
| 12380 | } |
| 12381 | a += strlen(a) + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12382 | } |
| 12383 | } |
| 12384 | #endif |
| 12385 | out1fmt("\n\n"); |
| 12386 | return EXIT_SUCCESS; |
| 12387 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12388 | #endif /* FEATURE_SH_EXTRA_QUIET */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12389 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12390 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12391 | * The export and readonly commands. |
| 12392 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12393 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12394 | exportcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12395 | { |
| 12396 | struct var *vp; |
| 12397 | char *name; |
| 12398 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12399 | char **aptr; |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 12400 | int flag = argv[0][0] == 'r' ? VREADONLY : VEXPORT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12401 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12402 | if (nextopt("p") != 'p') { |
| 12403 | aptr = argptr; |
| 12404 | name = *aptr; |
| 12405 | if (name) { |
| 12406 | do { |
| 12407 | p = strchr(name, '='); |
| 12408 | if (p != NULL) { |
| 12409 | p++; |
| 12410 | } else { |
| 12411 | vp = *findvar(hashvar(name), name); |
| 12412 | if (vp) { |
| 12413 | vp->flags |= flag; |
| 12414 | continue; |
| 12415 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12416 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12417 | setvar(name, p, flag); |
| 12418 | } while ((name = *++aptr) != NULL); |
| 12419 | return 0; |
| 12420 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12421 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12422 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12423 | return 0; |
| 12424 | } |
| 12425 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12426 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12427 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12428 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12429 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12430 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12431 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12432 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12433 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12434 | cmdp = cmdlookup(name, 0); |
| 12435 | if (cmdp!= NULL && cmdp->cmdtype == CMDFUNCTION) |
| 12436 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12437 | } |
| 12438 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12439 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12440 | * The unset builtin command. We unset the function before we unset the |
| 12441 | * variable to allow a function to be unset when there is a readonly variable |
| 12442 | * with the same name. |
| 12443 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12444 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12445 | unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12446 | { |
| 12447 | char **ap; |
| 12448 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12449 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12450 | int ret = 0; |
| 12451 | |
| 12452 | while ((i = nextopt("vf")) != '\0') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12453 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12454 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12455 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12456 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12457 | if (flag != 'f') { |
| 12458 | i = unsetvar(*ap); |
| 12459 | ret |= i; |
| 12460 | if (!(i & 2)) |
| 12461 | continue; |
| 12462 | } |
| 12463 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12464 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12465 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12466 | return ret & 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12467 | } |
| 12468 | |
| 12469 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12470 | /* setmode.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12471 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12472 | #include <sys/times.h> |
| 12473 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12474 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12475 | ' ', offsetof(struct tms, tms_utime), |
| 12476 | '\n', offsetof(struct tms, tms_stime), |
| 12477 | ' ', offsetof(struct tms, tms_cutime), |
| 12478 | '\n', offsetof(struct tms, tms_cstime), |
| 12479 | 0 |
| 12480 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12481 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12482 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12483 | timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12484 | { |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12485 | long clk_tck, s, t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12486 | const unsigned char *p; |
| 12487 | struct tms buf; |
| 12488 | |
| 12489 | clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12490 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12491 | |
| 12492 | p = timescmd_str; |
| 12493 | do { |
| 12494 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 12495 | s = t / clk_tck; |
| 12496 | out1fmt("%ldm%ld.%.3lds%c", |
| 12497 | s/60, s%60, |
| 12498 | ((t - s * clk_tck) * 1000) / clk_tck, |
| 12499 | p[0]); |
| 12500 | } while (*(p += 2)); |
| 12501 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12502 | return 0; |
| 12503 | } |
| 12504 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12505 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12506 | /* |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12507 | * The let builtin. partial stolen from GNU Bash, the Bourne Again SHell. |
| 12508 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12509 | * |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12510 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12511 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12512 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12513 | letcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12514 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12515 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12516 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12517 | argv++; |
| 12518 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12519 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12520 | do { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12521 | i = ash_arith(*argv); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12522 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12523 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 12524 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12525 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12526 | #endif /* SH_MATH_SUPPORT */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12527 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12528 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12529 | /* ============ miscbltin.c |
| 12530 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12531 | * Miscellaneous builtins. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12532 | */ |
| 12533 | |
| 12534 | #undef rflag |
| 12535 | |
Denis Vlasenko | 83e5d6f | 2006-12-18 21:49:06 +0000 | [diff] [blame] | 12536 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 1 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12537 | typedef enum __rlimit_resource rlim_t; |
| 12538 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12539 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12540 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12541 | * The read builtin. Options: |
| 12542 | * -r Do not interpret '\' specially |
| 12543 | * -s Turn off echo (tty only) |
| 12544 | * -n NCHARS Read NCHARS max |
| 12545 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 12546 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 12547 | * -u FD Read from given FD instead of fd 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12548 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12549 | * TODO: bash also has: |
| 12550 | * -a ARRAY Read into array[0],[1],etc |
| 12551 | * -d DELIM End on DELIM char, not newline |
| 12552 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12553 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12554 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12555 | readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12556 | { |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12557 | char *opt_n = NULL; |
| 12558 | char *opt_p = NULL; |
| 12559 | char *opt_t = NULL; |
| 12560 | char *opt_u = NULL; |
| 12561 | int read_flags = 0; |
| 12562 | const char *r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12563 | int i; |
| 12564 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12565 | while ((i = nextopt("p:u:rt:n:s")) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 12566 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12567 | case 'p': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12568 | opt_p = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12569 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12570 | case 'n': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12571 | opt_n = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12572 | break; |
| 12573 | case 's': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12574 | read_flags |= BUILTIN_READ_SILENT; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12575 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12576 | case 't': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12577 | opt_t = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12578 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12579 | case 'r': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12580 | read_flags |= BUILTIN_READ_RAW; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12581 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12582 | case 'u': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12583 | opt_u = optionarg; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12584 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12585 | default: |
| 12586 | break; |
| 12587 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12588 | } |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12589 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 12590 | r = shell_builtin_read(setvar2, |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12591 | argptr, |
| 12592 | bltinlookup("IFS"), /* can be NULL */ |
| 12593 | read_flags, |
| 12594 | opt_n, |
| 12595 | opt_p, |
| 12596 | opt_t, |
| 12597 | opt_u |
| 12598 | ); |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12599 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12600 | if ((uintptr_t)r > 1) |
| 12601 | ash_msg_and_raise_error(r); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12602 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12603 | return (uintptr_t)r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12604 | } |
| 12605 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12606 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12607 | umaskcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12608 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12609 | static const char permuser[3] ALIGN1 = "ugo"; |
| 12610 | static const char permmode[3] ALIGN1 = "rwx"; |
| 12611 | static const short permmask[] ALIGN2 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12612 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 12613 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 12614 | S_IROTH, S_IWOTH, S_IXOTH |
| 12615 | }; |
| 12616 | |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 12617 | /* TODO: use bb_parse_mode() instead */ |
| 12618 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12619 | char *ap; |
| 12620 | mode_t mask; |
| 12621 | int i; |
| 12622 | int symbolic_mode = 0; |
| 12623 | |
| 12624 | while (nextopt("S") != '\0') { |
| 12625 | symbolic_mode = 1; |
| 12626 | } |
| 12627 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12628 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12629 | mask = umask(0); |
| 12630 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12631 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12632 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12633 | ap = *argptr; |
| 12634 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12635 | if (symbolic_mode) { |
| 12636 | char buf[18]; |
| 12637 | char *p = buf; |
| 12638 | |
| 12639 | for (i = 0; i < 3; i++) { |
| 12640 | int j; |
| 12641 | |
| 12642 | *p++ = permuser[i]; |
| 12643 | *p++ = '='; |
| 12644 | for (j = 0; j < 3; j++) { |
| 12645 | if ((mask & permmask[3 * i + j]) == 0) { |
| 12646 | *p++ = permmode[j]; |
| 12647 | } |
| 12648 | } |
| 12649 | *p++ = ','; |
| 12650 | } |
| 12651 | *--p = 0; |
| 12652 | puts(buf); |
| 12653 | } else { |
| 12654 | out1fmt("%.4o\n", mask); |
| 12655 | } |
| 12656 | } else { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12657 | if (isdigit((unsigned char) *ap)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12658 | mask = 0; |
| 12659 | do { |
| 12660 | if (*ap >= '8' || *ap < '0') |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 12661 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12662 | mask = (mask << 3) + (*ap - '0'); |
| 12663 | } while (*++ap != '\0'); |
| 12664 | umask(mask); |
| 12665 | } else { |
| 12666 | mask = ~mask & 0777; |
| 12667 | if (!bb_parse_mode(ap, &mask)) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 12668 | ash_msg_and_raise_error("illegal mode: %s", ap); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12669 | } |
| 12670 | umask(~mask & 0777); |
| 12671 | } |
| 12672 | } |
| 12673 | return 0; |
| 12674 | } |
| 12675 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12676 | static int FAST_FUNC |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 12677 | ulimitcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12678 | { |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 12679 | return shell_builtin_ulimit(argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12680 | } |
| 12681 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12682 | /* ============ main() and helpers */ |
| 12683 | |
| 12684 | /* |
| 12685 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12686 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12687 | static void exitshell(void) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12688 | static void |
| 12689 | exitshell(void) |
| 12690 | { |
| 12691 | struct jmploc loc; |
| 12692 | char *p; |
| 12693 | int status; |
| 12694 | |
| 12695 | status = exitstatus; |
| 12696 | TRACE(("pid %d, exitshell(%d)\n", getpid(), status)); |
| 12697 | if (setjmp(loc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12698 | if (exception_type == EXEXIT) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12699 | /* dash bug: it just does _exit(exitstatus) here |
| 12700 | * but we have to do setjobctl(0) first! |
| 12701 | * (bug is still not fixed in dash-0.5.3 - if you run dash |
| 12702 | * under Midnight Commander, on exit from dash MC is backgrounded) */ |
| 12703 | status = exitstatus; |
| 12704 | goto out; |
| 12705 | } |
| 12706 | exception_handler = &loc; |
| 12707 | p = trap[0]; |
| 12708 | if (p) { |
| 12709 | trap[0] = NULL; |
| 12710 | evalstring(p, 0); |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 12711 | free(p); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12712 | } |
| 12713 | flush_stdout_stderr(); |
| 12714 | out: |
| 12715 | setjobctl(0); |
| 12716 | _exit(status); |
| 12717 | /* NOTREACHED */ |
| 12718 | } |
| 12719 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12720 | static void |
| 12721 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12722 | { |
| 12723 | /* from input.c: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 12724 | basepf.next_to_pgetc = basepf.buf = basebuf; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12725 | |
| 12726 | /* from trap.c: */ |
| 12727 | signal(SIGCHLD, SIG_DFL); |
Denys Vlasenko | 7a7b034 | 2009-12-04 04:18:31 +0100 | [diff] [blame] | 12728 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 12729 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 12730 | */ |
| 12731 | signal(SIGHUP, SIG_DFL); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12732 | |
| 12733 | /* from var.c: */ |
| 12734 | { |
| 12735 | char **envp; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12736 | const char *p; |
| 12737 | struct stat st1, st2; |
| 12738 | |
| 12739 | initvar(); |
| 12740 | for (envp = environ; envp && *envp; envp++) { |
| 12741 | if (strchr(*envp, '=')) { |
| 12742 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 12743 | } |
| 12744 | } |
| 12745 | |
Denys Vlasenko | 7bb346f | 2009-10-06 22:09:50 +0200 | [diff] [blame] | 12746 | setvar("PPID", utoa(getppid()), 0); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12747 | |
| 12748 | p = lookupvar("PWD"); |
| 12749 | if (p) |
| 12750 | if (*p != '/' || stat(p, &st1) || stat(".", &st2) |
| 12751 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) |
| 12752 | p = '\0'; |
| 12753 | setpwd(p, 0); |
| 12754 | } |
| 12755 | } |
| 12756 | |
| 12757 | /* |
| 12758 | * Process the shell command line arguments. |
| 12759 | */ |
| 12760 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12761 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12762 | { |
| 12763 | int i; |
| 12764 | const char *xminusc; |
| 12765 | char **xargv; |
| 12766 | |
| 12767 | xargv = argv; |
| 12768 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12769 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12770 | xargv++; |
| 12771 | for (i = 0; i < NOPTS; i++) |
| 12772 | optlist[i] = 2; |
| 12773 | argptr = xargv; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 12774 | if (options(1)) { |
| 12775 | /* it already printed err message */ |
| 12776 | raise_exception(EXERROR); |
| 12777 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12778 | xargv = argptr; |
| 12779 | xminusc = minusc; |
| 12780 | if (*xargv == NULL) { |
| 12781 | if (xminusc) |
| 12782 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 12783 | sflag = 1; |
| 12784 | } |
| 12785 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 12786 | iflag = 1; |
| 12787 | if (mflag == 2) |
| 12788 | mflag = iflag; |
| 12789 | for (i = 0; i < NOPTS; i++) |
| 12790 | if (optlist[i] == 2) |
| 12791 | optlist[i] = 0; |
| 12792 | #if DEBUG == 2 |
| 12793 | debug = 1; |
| 12794 | #endif |
| 12795 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 12796 | if (xminusc) { |
| 12797 | minusc = *xargv++; |
| 12798 | if (*xargv) |
| 12799 | goto setarg0; |
| 12800 | } else if (!sflag) { |
| 12801 | setinputfile(*xargv, 0); |
| 12802 | setarg0: |
| 12803 | arg0 = *xargv++; |
| 12804 | commandname = arg0; |
| 12805 | } |
| 12806 | |
| 12807 | shellparam.p = xargv; |
| 12808 | #if ENABLE_ASH_GETOPTS |
| 12809 | shellparam.optind = 1; |
| 12810 | shellparam.optoff = -1; |
| 12811 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12812 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12813 | while (*xargv) { |
| 12814 | shellparam.nparam++; |
| 12815 | xargv++; |
| 12816 | } |
| 12817 | optschanged(); |
| 12818 | } |
| 12819 | |
| 12820 | /* |
| 12821 | * Read /etc/profile or .profile. |
| 12822 | */ |
| 12823 | static void |
| 12824 | read_profile(const char *name) |
| 12825 | { |
| 12826 | int skip; |
| 12827 | |
| 12828 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 12829 | return; |
| 12830 | skip = cmdloop(0); |
| 12831 | popfile(); |
| 12832 | if (skip) |
| 12833 | exitshell(); |
| 12834 | } |
| 12835 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12836 | /* |
| 12837 | * This routine is called when an error or an interrupt occurs in an |
| 12838 | * interactive shell and control is returned to the main command loop. |
| 12839 | */ |
| 12840 | static void |
| 12841 | reset(void) |
| 12842 | { |
| 12843 | /* from eval.c: */ |
| 12844 | evalskip = 0; |
| 12845 | loopnest = 0; |
| 12846 | /* from input.c: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 12847 | g_parsefile->left_in_buffer = 0; |
| 12848 | g_parsefile->left_in_line = 0; /* clear input buffer */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12849 | popallfiles(); |
| 12850 | /* from parser.c: */ |
| 12851 | tokpushback = 0; |
| 12852 | checkkwd = 0; |
| 12853 | /* from redir.c: */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 12854 | clearredir(/*drop:*/ 0); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12855 | } |
| 12856 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12857 | #if PROFILE |
| 12858 | static short profile_buf[16384]; |
| 12859 | extern int etext(); |
| 12860 | #endif |
| 12861 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12862 | /* |
| 12863 | * Main routine. We initialize things, parse the arguments, execute |
| 12864 | * profiles if we're a login shell, and then call cmdloop to execute |
| 12865 | * commands. The setjmp call sets up the location to jump to when an |
| 12866 | * exception occurs. When an exception occurs the variable "state" |
| 12867 | * is used to figure out how far we had gotten. |
| 12868 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 12869 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12870 | int ash_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12871 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12872 | const char *shinit; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 12873 | volatile smallint state; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12874 | struct jmploc jmploc; |
| 12875 | struct stackmark smark; |
| 12876 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12877 | /* Initialize global data */ |
| 12878 | INIT_G_misc(); |
| 12879 | INIT_G_memstack(); |
| 12880 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 12881 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12882 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 12883 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12884 | INIT_G_cmdtable(); |
| 12885 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12886 | #if PROFILE |
| 12887 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 12888 | #endif |
| 12889 | |
| 12890 | #if ENABLE_FEATURE_EDITING |
| 12891 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
| 12892 | #endif |
| 12893 | state = 0; |
| 12894 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12895 | smallint e; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 12896 | smallint s; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12897 | |
| 12898 | reset(); |
| 12899 | |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12900 | e = exception_type; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12901 | if (e == EXERROR) |
| 12902 | exitstatus = 2; |
| 12903 | s = state; |
| 12904 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) |
| 12905 | exitshell(); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12906 | if (e == EXINT) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12907 | outcslow('\n', stderr); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12908 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12909 | popstackmark(&smark); |
| 12910 | FORCE_INT_ON; /* enable interrupts */ |
| 12911 | if (s == 1) |
| 12912 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 12913 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12914 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 12915 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12916 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 12917 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12918 | } |
| 12919 | exception_handler = &jmploc; |
| 12920 | #if DEBUG |
| 12921 | opentrace(); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 12922 | TRACE(("Shell args: ")); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 12923 | trace_puts_args(argv); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12924 | #endif |
| 12925 | rootpid = getpid(); |
| 12926 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12927 | init(); |
| 12928 | setstackmark(&smark); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12929 | procargs(argv); |
| 12930 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12931 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
| 12932 | if (iflag) { |
| 12933 | const char *hp = lookupvar("HISTFILE"); |
| 12934 | |
| 12935 | if (hp == NULL) { |
| 12936 | hp = lookupvar("HOME"); |
| 12937 | if (hp != NULL) { |
| 12938 | char *defhp = concat_path_file(hp, ".ash_history"); |
| 12939 | setvar("HISTFILE", defhp, 0); |
| 12940 | free(defhp); |
| 12941 | } |
| 12942 | } |
| 12943 | } |
| 12944 | #endif |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 12945 | if (/* argv[0] && */ argv[0][0] == '-') |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12946 | isloginsh = 1; |
| 12947 | if (isloginsh) { |
| 12948 | state = 1; |
| 12949 | read_profile("/etc/profile"); |
| 12950 | state1: |
| 12951 | state = 2; |
| 12952 | read_profile(".profile"); |
| 12953 | } |
| 12954 | state2: |
| 12955 | state = 3; |
| 12956 | if ( |
| 12957 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12958 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12959 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12960 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12961 | ) { |
| 12962 | shinit = lookupvar("ENV"); |
| 12963 | if (shinit != NULL && *shinit != '\0') { |
| 12964 | read_profile(shinit); |
| 12965 | } |
| 12966 | } |
| 12967 | state3: |
| 12968 | state = 4; |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 12969 | if (minusc) { |
| 12970 | /* evalstring pushes parsefile stack. |
| 12971 | * Ensure we don't falsely claim that 0 (stdin) |
Denis Vlasenko | 5368ad5 | 2009-03-20 10:20:08 +0000 | [diff] [blame] | 12972 | * is one of stacked source fds. |
| 12973 | * Testcase: ash -c 'exec 1>&0' must not complain. */ |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 12974 | if (!sflag) |
| 12975 | g_parsefile->fd = -1; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12976 | evalstring(minusc, 0); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 12977 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12978 | |
| 12979 | if (sflag || minusc == NULL) { |
Denys Vlasenko | 0337e03 | 2009-11-29 00:12:30 +0100 | [diff] [blame] | 12980 | #if defined MAX_HISTORY && MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 12981 | if (iflag) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12982 | const char *hp = lookupvar("HISTFILE"); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 12983 | if (hp) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12984 | line_input_state->hist_file = hp; |
| 12985 | } |
| 12986 | #endif |
| 12987 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 12988 | cmdloop(1); |
| 12989 | } |
| 12990 | #if PROFILE |
| 12991 | monitor(0); |
| 12992 | #endif |
| 12993 | #ifdef GPROF |
| 12994 | { |
| 12995 | extern void _mcleanup(void); |
| 12996 | _mcleanup(); |
| 12997 | } |
| 12998 | #endif |
| 12999 | exitshell(); |
| 13000 | /* NOTREACHED */ |
| 13001 | } |
| 13002 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13003 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13004 | /*- |
| 13005 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 13006 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13007 | * |
| 13008 | * This code is derived from software contributed to Berkeley by |
| 13009 | * Kenneth Almquist. |
| 13010 | * |
| 13011 | * Redistribution and use in source and binary forms, with or without |
| 13012 | * modification, are permitted provided that the following conditions |
| 13013 | * are met: |
| 13014 | * 1. Redistributions of source code must retain the above copyright |
| 13015 | * notice, this list of conditions and the following disclaimer. |
| 13016 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13017 | * notice, this list of conditions and the following disclaimer in the |
| 13018 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13019 | * 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] | 13020 | * may be used to endorse or promote products derived from this software |
| 13021 | * without specific prior written permission. |
| 13022 | * |
| 13023 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 13024 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13025 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 13026 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13027 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13028 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 13029 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13030 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 13031 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 13032 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 13033 | * SUCH DAMAGE. |
| 13034 | */ |