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 | * |
| 5 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7 | * |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 8 | * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au> |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 9 | * was re-ported from NetBSD and debianized. |
| 10 | * |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11 | * This code is derived from software contributed to Berkeley by |
| 12 | * Kenneth Almquist. |
| 13 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 14 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 15 | * |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 16 | * Original BSD copyright notice is retained at the end of this file. |
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 | |
| 37 | #define IFS_BROKEN |
| 38 | |
| 39 | #define JOBS ENABLE_ASH_JOB_CONTROL |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 41 | #if DEBUG |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 42 | # ifndef _GNU_SOURCE |
| 43 | # define _GNU_SOURCE |
| 44 | # endif |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 45 | #endif |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 47 | #include "busybox.h" /* for applet_names */ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 48 | //TODO: pull in some .h and find out do we have SINGLE_APPLET_MAIN? |
| 49 | //#include "applet_tables.h" doesn't work |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 50 | #include <paths.h> |
| 51 | #include <setjmp.h> |
| 52 | #include <fnmatch.h> |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 53 | #include "math.h" |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 54 | |
| 55 | #if defined SINGLE_APPLET_MAIN |
| 56 | /* STANDALONE does not make sense, and won't compile */ |
| 57 | #undef CONFIG_FEATURE_SH_STANDALONE |
| 58 | #undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 59 | #undef IF_FEATURE_SH_STANDALONE |
| 60 | #undef IF_NOT_FEATURE_SH_STANDALONE(...) |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 61 | #define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 62 | #define IF_FEATURE_SH_STANDALONE(...) |
| 63 | #define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 66 | #ifndef PIPE_BUF |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 67 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 70 | #if defined(__uClinux__) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 71 | # error "Do not even bother, ash will not run on NOMMU machine" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 75 | /* ============ Hash table sizes. Configurable. */ |
| 76 | |
| 77 | #define VTABSIZE 39 |
| 78 | #define ATABSIZE 39 |
| 79 | #define CMDTABLESIZE 31 /* should be prime */ |
| 80 | |
| 81 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 82 | /* ============ Shell options */ |
| 83 | |
| 84 | static const char *const optletters_optnames[] = { |
| 85 | "e" "errexit", |
| 86 | "f" "noglob", |
| 87 | "I" "ignoreeof", |
| 88 | "i" "interactive", |
| 89 | "m" "monitor", |
| 90 | "n" "noexec", |
| 91 | "s" "stdin", |
| 92 | "x" "xtrace", |
| 93 | "v" "verbose", |
| 94 | "C" "noclobber", |
| 95 | "a" "allexport", |
| 96 | "b" "notify", |
| 97 | "u" "nounset", |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 98 | "\0" "vi" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 99 | #if DEBUG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 100 | ,"\0" "nolog" |
| 101 | ,"\0" "debug" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 102 | #endif |
| 103 | }; |
| 104 | |
| 105 | #define optletters(n) optletters_optnames[(n)][0] |
| 106 | #define optnames(n) (&optletters_optnames[(n)][1]) |
| 107 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 108 | enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 109 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 110 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 111 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 113 | static const char homestr[] ALIGN1 = "HOME"; |
| 114 | static const char snlfmt[] ALIGN1 = "%s\n"; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 115 | static const char msg_illnum[] ALIGN1 = "Illegal number: %s"; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 116 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 117 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 118 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 119 | * jump locations. The global variable handler contains the location to |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 120 | * jump to when an exception occurs, and the global variable exception_type |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 121 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 122 | * exception handlers, the user should save the value of handler on entry |
| 123 | * to an inner scope, set handler to point to a jmploc structure for the |
| 124 | * inner scope, and restore handler on exit from the scope. |
| 125 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 126 | struct jmploc { |
| 127 | jmp_buf loc; |
| 128 | }; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 129 | |
| 130 | struct globals_misc { |
| 131 | /* pid of main shell */ |
| 132 | int rootpid; |
| 133 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 134 | int shlvl; |
| 135 | #define rootshell (!shlvl) |
| 136 | char *minusc; /* argument to -c option */ |
| 137 | |
| 138 | char *curdir; // = nullstr; /* current working directory */ |
| 139 | char *physdir; // = nullstr; /* physical working directory */ |
| 140 | |
| 141 | char *arg0; /* value of $0 */ |
| 142 | |
| 143 | struct jmploc *exception_handler; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 144 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 145 | volatile int suppress_int; /* counter */ |
| 146 | volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 147 | /* last pending signal */ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 148 | volatile /*sig_atomic_t*/ smallint pending_sig; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 149 | smallint exception_type; /* kind of exception (0..5) */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 150 | /* exceptions */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 151 | #define EXINT 0 /* SIGINT received */ |
| 152 | #define EXERROR 1 /* a generic error */ |
| 153 | #define EXSHELLPROC 2 /* execute a shell procedure */ |
| 154 | #define EXEXEC 3 /* command execution failed */ |
| 155 | #define EXEXIT 4 /* exit the shell */ |
| 156 | #define EXSIG 5 /* trapped signal in wait(1) */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 157 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 158 | smallint isloginsh; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 159 | char nullstr[1]; /* zero length string */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 160 | |
| 161 | char optlist[NOPTS]; |
| 162 | #define eflag optlist[0] |
| 163 | #define fflag optlist[1] |
| 164 | #define Iflag optlist[2] |
| 165 | #define iflag optlist[3] |
| 166 | #define mflag optlist[4] |
| 167 | #define nflag optlist[5] |
| 168 | #define sflag optlist[6] |
| 169 | #define xflag optlist[7] |
| 170 | #define vflag optlist[8] |
| 171 | #define Cflag optlist[9] |
| 172 | #define aflag optlist[10] |
| 173 | #define bflag optlist[11] |
| 174 | #define uflag optlist[12] |
| 175 | #define viflag optlist[13] |
| 176 | #if DEBUG |
| 177 | #define nolog optlist[14] |
| 178 | #define debug optlist[15] |
| 179 | #endif |
| 180 | |
| 181 | /* trap handler commands */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 182 | /* |
| 183 | * Sigmode records the current value of the signal handlers for the various |
| 184 | * 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] | 185 | * 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] | 186 | */ |
| 187 | char sigmode[NSIG - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 188 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 189 | #define S_CATCH 2 /* signal is caught */ |
| 190 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 191 | #define S_HARD_IGN 4 /* signal is ignored permenantly */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 192 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 193 | /* indicates specified signal received */ |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 194 | uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 195 | char *trap[NSIG]; |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 196 | char **trap_ptr; /* used only by "trap hack" */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 197 | |
| 198 | /* Rarely referenced stuff */ |
| 199 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 200 | /* Random number generators */ |
| 201 | int32_t random_galois_LFSR; /* Galois LFSR (fast but weak). signed! */ |
| 202 | uint32_t random_LCG; /* LCG (fast but weak) */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 203 | #endif |
| 204 | pid_t backgndpid; /* pid of last background process */ |
| 205 | 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] | 206 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 207 | extern struct globals_misc *const ash_ptr_to_globals_misc; |
| 208 | #define G_misc (*ash_ptr_to_globals_misc) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 209 | #define rootpid (G_misc.rootpid ) |
| 210 | #define shlvl (G_misc.shlvl ) |
| 211 | #define minusc (G_misc.minusc ) |
| 212 | #define curdir (G_misc.curdir ) |
| 213 | #define physdir (G_misc.physdir ) |
| 214 | #define arg0 (G_misc.arg0 ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 215 | #define exception_handler (G_misc.exception_handler) |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 216 | #define exception_type (G_misc.exception_type ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 217 | #define suppress_int (G_misc.suppress_int ) |
| 218 | #define pending_int (G_misc.pending_int ) |
| 219 | #define pending_sig (G_misc.pending_sig ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 220 | #define isloginsh (G_misc.isloginsh ) |
| 221 | #define nullstr (G_misc.nullstr ) |
| 222 | #define optlist (G_misc.optlist ) |
| 223 | #define sigmode (G_misc.sigmode ) |
| 224 | #define gotsig (G_misc.gotsig ) |
| 225 | #define trap (G_misc.trap ) |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 226 | #define trap_ptr (G_misc.trap_ptr ) |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 227 | #define random_galois_LFSR (G_misc.random_galois_LFSR) |
| 228 | #define random_LCG (G_misc.random_LCG ) |
| 229 | #define backgndpid (G_misc.backgndpid ) |
| 230 | #define job_warning (G_misc.job_warning) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 231 | #define INIT_G_misc() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 232 | (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ |
| 233 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 234 | curdir = nullstr; \ |
| 235 | physdir = nullstr; \ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 236 | trap_ptr = trap; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 237 | } while (0) |
| 238 | |
| 239 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 240 | /* ============ DEBUG */ |
| 241 | #if DEBUG |
| 242 | static void trace_printf(const char *fmt, ...); |
| 243 | static void trace_vprintf(const char *fmt, va_list va); |
| 244 | # define TRACE(param) trace_printf param |
| 245 | # define TRACEV(param) trace_vprintf param |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 246 | # define close(fd) do { \ |
| 247 | int dfd = (fd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 248 | if (close(dfd) < 0) \ |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 249 | bb_error_msg("bug on %d: closing %d(0x%x)", \ |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 250 | __LINE__, dfd, dfd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 251 | } while (0) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 252 | #else |
| 253 | # define TRACE(param) |
| 254 | # define TRACEV(param) |
| 255 | #endif |
| 256 | |
| 257 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 258 | /* ============ Utility functions */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 259 | #define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0) |
| 260 | |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 261 | /* C99 says: "char" declaration may be signed or unsigned by default */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 262 | #define signed_char2int(sc) ((int)(signed char)(sc)) |
| 263 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 264 | static int isdigit_str9(const char *str) |
| 265 | { |
| 266 | int maxlen = 9 + 1; /* max 9 digits: 999999999 */ |
| 267 | while (--maxlen && isdigit(*str)) |
| 268 | str++; |
| 269 | return (*str == '\0'); |
| 270 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 271 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 272 | |
| 273 | /* ============ Interrupts / exceptions */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 274 | /* |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 275 | * These macros allow the user to suspend the handling of interrupt signals |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 276 | * 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] | 277 | * much more efficient and portable. (But hacking the kernel is so much |
| 278 | * more fun than worrying about efficiency and portability. :-)) |
| 279 | */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 280 | #define INT_OFF do { \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 281 | suppress_int++; \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 282 | xbarrier(); \ |
| 283 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 284 | |
| 285 | /* |
| 286 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 287 | * 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] | 288 | * stored in the global variable "exception_type". |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 289 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 290 | static void raise_exception(int) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 291 | static void |
| 292 | raise_exception(int e) |
| 293 | { |
| 294 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 295 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 296 | abort(); |
| 297 | #endif |
| 298 | INT_OFF; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 299 | exception_type = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 300 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 301 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 302 | #if DEBUG |
| 303 | #define raise_exception(e) do { \ |
| 304 | TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \ |
| 305 | raise_exception(e); \ |
| 306 | } while (0) |
| 307 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * Called from trap.c when a SIGINT is received. (If the user specifies |
| 311 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 312 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 313 | * are held using the INT_OFF macro. (The test for iflag is just |
| 314 | * defensive programming.) |
| 315 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 316 | static void raise_interrupt(void) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 317 | static void |
| 318 | raise_interrupt(void) |
| 319 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 320 | int ex_type; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 321 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 322 | pending_int = 0; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 323 | /* Signal is not automatically unmasked after it is raised, |
| 324 | * do it ourself - unmask all signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 325 | sigprocmask_allsigs(SIG_UNBLOCK); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 326 | /* pending_sig = 0; - now done in onsig() */ |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 327 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 328 | ex_type = EXSIG; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 329 | if (gotsig[SIGINT - 1] && !trap[SIGINT]) { |
| 330 | if (!(rootshell && iflag)) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 331 | /* Kill ourself with SIGINT */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 332 | signal(SIGINT, SIG_DFL); |
| 333 | raise(SIGINT); |
| 334 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 335 | ex_type = EXINT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 336 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 337 | raise_exception(ex_type); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 338 | /* NOTREACHED */ |
| 339 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 340 | #if DEBUG |
| 341 | #define raise_interrupt() do { \ |
| 342 | TRACE(("raising interrupt on line %d\n", __LINE__)); \ |
| 343 | raise_interrupt(); \ |
| 344 | } while (0) |
| 345 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 346 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 347 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 348 | int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 349 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 350 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 351 | if (--suppress_int == 0 && pending_int) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 352 | raise_interrupt(); |
| 353 | } |
| 354 | } |
| 355 | #define INT_ON int_on() |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 356 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 357 | force_int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 358 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 359 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 360 | suppress_int = 0; |
| 361 | if (pending_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 362 | raise_interrupt(); |
| 363 | } |
| 364 | #define FORCE_INT_ON force_int_on() |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 365 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 366 | #define SAVE_INT(v) ((v) = suppress_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 367 | |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 368 | #define RESTORE_INT(v) do { \ |
| 369 | xbarrier(); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 370 | suppress_int = (v); \ |
| 371 | if (suppress_int == 0 && pending_int) \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 372 | raise_interrupt(); \ |
| 373 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 374 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 375 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 376 | /* ============ Stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 377 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 378 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 379 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 380 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 381 | INT_OFF; |
| 382 | fputs(p, file); |
| 383 | INT_ON; |
| 384 | } |
| 385 | |
| 386 | static void |
| 387 | flush_stdout_stderr(void) |
| 388 | { |
| 389 | INT_OFF; |
| 390 | fflush(stdout); |
| 391 | fflush(stderr); |
| 392 | INT_ON; |
| 393 | } |
| 394 | |
| 395 | static void |
| 396 | flush_stderr(void) |
| 397 | { |
| 398 | INT_OFF; |
| 399 | fflush(stderr); |
| 400 | INT_ON; |
| 401 | } |
| 402 | |
| 403 | static void |
| 404 | outcslow(int c, FILE *dest) |
| 405 | { |
| 406 | INT_OFF; |
| 407 | putc(c, dest); |
| 408 | fflush(dest); |
| 409 | INT_ON; |
| 410 | } |
| 411 | |
| 412 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 413 | static int |
| 414 | out1fmt(const char *fmt, ...) |
| 415 | { |
| 416 | va_list ap; |
| 417 | int r; |
| 418 | |
| 419 | INT_OFF; |
| 420 | va_start(ap, fmt); |
| 421 | r = vprintf(fmt, ap); |
| 422 | va_end(ap); |
| 423 | INT_ON; |
| 424 | return r; |
| 425 | } |
| 426 | |
| 427 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 428 | static int |
| 429 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 430 | { |
| 431 | va_list ap; |
| 432 | int ret; |
| 433 | |
| 434 | va_start(ap, fmt); |
| 435 | INT_OFF; |
| 436 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 437 | va_end(ap); |
| 438 | INT_ON; |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | static void |
| 443 | out1str(const char *p) |
| 444 | { |
| 445 | outstr(p, stdout); |
| 446 | } |
| 447 | |
| 448 | static void |
| 449 | out2str(const char *p) |
| 450 | { |
| 451 | outstr(p, stderr); |
| 452 | flush_stderr(); |
| 453 | } |
| 454 | |
| 455 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 456 | /* ============ Parser structures */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 457 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 458 | /* control characters in argument strings */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 459 | #define CTLESC ((unsigned char)'\201') /* escape next character */ |
| 460 | #define CTLVAR ((unsigned char)'\202') /* variable defn */ |
| 461 | #define CTLENDVAR ((unsigned char)'\203') |
| 462 | #define CTLBACKQ ((unsigned char)'\204') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 463 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ |
| 464 | /* CTLBACKQ | CTLQUOTE == '\205' */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 465 | #define CTLARI ((unsigned char)'\206') /* arithmetic expression */ |
| 466 | #define CTLENDARI ((unsigned char)'\207') |
| 467 | #define CTLQUOTEMARK ((unsigned char)'\210') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 468 | |
| 469 | /* variable substitution byte (follows CTLVAR) */ |
| 470 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 471 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
| 472 | #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */ |
| 473 | |
| 474 | /* values of VSTYPE field */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 475 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 476 | #define VSMINUS 0x2 /* ${var-text} */ |
| 477 | #define VSPLUS 0x3 /* ${var+text} */ |
| 478 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 479 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 480 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 481 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 482 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 483 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 484 | #define VSLENGTH 0xa /* ${#var} */ |
| 485 | #if ENABLE_ASH_BASH_COMPAT |
| 486 | #define VSSUBSTR 0xc /* ${var:position:length} */ |
| 487 | #define VSREPLACE 0xd /* ${var/pattern/replacement} */ |
| 488 | #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */ |
| 489 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 490 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 491 | static const char dolatstr[] ALIGN1 = { |
| 492 | CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' |
| 493 | }; |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 494 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 495 | #define NCMD 0 |
| 496 | #define NPIPE 1 |
| 497 | #define NREDIR 2 |
| 498 | #define NBACKGND 3 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 499 | #define NSUBSHELL 4 |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 500 | #define NAND 5 |
| 501 | #define NOR 6 |
| 502 | #define NSEMI 7 |
| 503 | #define NIF 8 |
| 504 | #define NWHILE 9 |
| 505 | #define NUNTIL 10 |
| 506 | #define NFOR 11 |
| 507 | #define NCASE 12 |
| 508 | #define NCLIST 13 |
| 509 | #define NDEFUN 14 |
| 510 | #define NARG 15 |
| 511 | #define NTO 16 |
| 512 | #if ENABLE_ASH_BASH_COMPAT |
| 513 | #define NTO2 17 |
| 514 | #endif |
| 515 | #define NCLOBBER 18 |
| 516 | #define NFROM 19 |
| 517 | #define NFROMTO 20 |
| 518 | #define NAPPEND 21 |
| 519 | #define NTOFD 22 |
| 520 | #define NFROMFD 23 |
| 521 | #define NHERE 24 |
| 522 | #define NXHERE 25 |
| 523 | #define NNOT 26 |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 524 | #define N_NUMBER 27 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 525 | |
| 526 | union node; |
| 527 | |
| 528 | struct ncmd { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 529 | smallint type; /* Nxxxx */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 530 | union node *assign; |
| 531 | union node *args; |
| 532 | union node *redirect; |
| 533 | }; |
| 534 | |
| 535 | struct npipe { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 536 | smallint type; |
| 537 | smallint pipe_backgnd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 538 | struct nodelist *cmdlist; |
| 539 | }; |
| 540 | |
| 541 | struct nredir { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 542 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 543 | union node *n; |
| 544 | union node *redirect; |
| 545 | }; |
| 546 | |
| 547 | struct nbinary { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 548 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 549 | union node *ch1; |
| 550 | union node *ch2; |
| 551 | }; |
| 552 | |
| 553 | struct nif { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 554 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 555 | union node *test; |
| 556 | union node *ifpart; |
| 557 | union node *elsepart; |
| 558 | }; |
| 559 | |
| 560 | struct nfor { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 561 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 562 | union node *args; |
| 563 | union node *body; |
| 564 | char *var; |
| 565 | }; |
| 566 | |
| 567 | struct ncase { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 568 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 569 | union node *expr; |
| 570 | union node *cases; |
| 571 | }; |
| 572 | |
| 573 | struct nclist { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 574 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 575 | union node *next; |
| 576 | union node *pattern; |
| 577 | union node *body; |
| 578 | }; |
| 579 | |
| 580 | struct narg { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 581 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 582 | union node *next; |
| 583 | char *text; |
| 584 | struct nodelist *backquote; |
| 585 | }; |
| 586 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 587 | /* nfile and ndup layout must match! |
| 588 | * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight |
| 589 | * that it is actually NTO2 (>&file), and change its type. |
| 590 | */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 591 | struct nfile { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 592 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 593 | union node *next; |
| 594 | int fd; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 595 | int _unused_dupfd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 596 | union node *fname; |
| 597 | char *expfname; |
| 598 | }; |
| 599 | |
| 600 | struct ndup { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 601 | smallint type; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 602 | union node *next; |
| 603 | int fd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 604 | int dupfd; |
| 605 | union node *vname; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 606 | char *_unused_expfname; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 607 | }; |
| 608 | |
| 609 | struct nhere { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 610 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 611 | union node *next; |
| 612 | int fd; |
| 613 | union node *doc; |
| 614 | }; |
| 615 | |
| 616 | struct nnot { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 617 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 618 | union node *com; |
| 619 | }; |
| 620 | |
| 621 | union node { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 622 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 623 | struct ncmd ncmd; |
| 624 | struct npipe npipe; |
| 625 | struct nredir nredir; |
| 626 | struct nbinary nbinary; |
| 627 | struct nif nif; |
| 628 | struct nfor nfor; |
| 629 | struct ncase ncase; |
| 630 | struct nclist nclist; |
| 631 | struct narg narg; |
| 632 | struct nfile nfile; |
| 633 | struct ndup ndup; |
| 634 | struct nhere nhere; |
| 635 | struct nnot nnot; |
| 636 | }; |
| 637 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 638 | /* |
| 639 | * NODE_EOF is returned by parsecmd when it encounters an end of file. |
| 640 | * It must be distinct from NULL. |
| 641 | */ |
| 642 | #define NODE_EOF ((union node *) -1L) |
| 643 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 644 | struct nodelist { |
| 645 | struct nodelist *next; |
| 646 | union node *n; |
| 647 | }; |
| 648 | |
| 649 | struct funcnode { |
| 650 | int count; |
| 651 | union node n; |
| 652 | }; |
| 653 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 654 | /* |
| 655 | * Free a parse tree. |
| 656 | */ |
| 657 | static void |
| 658 | freefunc(struct funcnode *f) |
| 659 | { |
| 660 | if (f && --f->count < 0) |
| 661 | free(f); |
| 662 | } |
| 663 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 664 | |
| 665 | /* ============ Debugging output */ |
| 666 | |
| 667 | #if DEBUG |
| 668 | |
| 669 | static FILE *tracefile; |
| 670 | |
| 671 | static void |
| 672 | trace_printf(const char *fmt, ...) |
| 673 | { |
| 674 | va_list va; |
| 675 | |
| 676 | if (debug != 1) |
| 677 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 678 | if (DEBUG_TIME) |
| 679 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 680 | if (DEBUG_PID) |
| 681 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 682 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 683 | 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] | 684 | va_start(va, fmt); |
| 685 | vfprintf(tracefile, fmt, va); |
| 686 | va_end(va); |
| 687 | } |
| 688 | |
| 689 | static void |
| 690 | trace_vprintf(const char *fmt, va_list va) |
| 691 | { |
| 692 | if (debug != 1) |
| 693 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 694 | if (DEBUG_TIME) |
| 695 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 696 | if (DEBUG_PID) |
| 697 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 698 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 699 | 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] | 700 | vfprintf(tracefile, fmt, va); |
| 701 | } |
| 702 | |
| 703 | static void |
| 704 | trace_puts(const char *s) |
| 705 | { |
| 706 | if (debug != 1) |
| 707 | return; |
| 708 | fputs(s, tracefile); |
| 709 | } |
| 710 | |
| 711 | static void |
| 712 | trace_puts_quoted(char *s) |
| 713 | { |
| 714 | char *p; |
| 715 | char c; |
| 716 | |
| 717 | if (debug != 1) |
| 718 | return; |
| 719 | putc('"', tracefile); |
| 720 | for (p = s; *p; p++) { |
| 721 | switch (*p) { |
| 722 | case '\n': c = 'n'; goto backslash; |
| 723 | case '\t': c = 't'; goto backslash; |
| 724 | case '\r': c = 'r'; goto backslash; |
| 725 | case '"': c = '"'; goto backslash; |
| 726 | case '\\': c = '\\'; goto backslash; |
| 727 | case CTLESC: c = 'e'; goto backslash; |
| 728 | case CTLVAR: c = 'v'; goto backslash; |
| 729 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash; |
| 730 | case CTLBACKQ: c = 'q'; goto backslash; |
| 731 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash; |
| 732 | backslash: |
| 733 | putc('\\', tracefile); |
| 734 | putc(c, tracefile); |
| 735 | break; |
| 736 | default: |
| 737 | if (*p >= ' ' && *p <= '~') |
| 738 | putc(*p, tracefile); |
| 739 | else { |
| 740 | putc('\\', tracefile); |
| 741 | putc(*p >> 6 & 03, tracefile); |
| 742 | putc(*p >> 3 & 07, tracefile); |
| 743 | putc(*p & 07, tracefile); |
| 744 | } |
| 745 | break; |
| 746 | } |
| 747 | } |
| 748 | putc('"', tracefile); |
| 749 | } |
| 750 | |
| 751 | static void |
| 752 | trace_puts_args(char **ap) |
| 753 | { |
| 754 | if (debug != 1) |
| 755 | return; |
| 756 | if (!*ap) |
| 757 | return; |
| 758 | while (1) { |
| 759 | trace_puts_quoted(*ap); |
| 760 | if (!*++ap) { |
| 761 | putc('\n', tracefile); |
| 762 | break; |
| 763 | } |
| 764 | putc(' ', tracefile); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | static void |
| 769 | opentrace(void) |
| 770 | { |
| 771 | char s[100]; |
| 772 | #ifdef O_APPEND |
| 773 | int flags; |
| 774 | #endif |
| 775 | |
| 776 | if (debug != 1) { |
| 777 | if (tracefile) |
| 778 | fflush(tracefile); |
| 779 | /* leave open because libedit might be using it */ |
| 780 | return; |
| 781 | } |
| 782 | strcpy(s, "./trace"); |
| 783 | if (tracefile) { |
| 784 | if (!freopen(s, "a", tracefile)) { |
| 785 | fprintf(stderr, "Can't re-open %s\n", s); |
| 786 | debug = 0; |
| 787 | return; |
| 788 | } |
| 789 | } else { |
| 790 | tracefile = fopen(s, "a"); |
| 791 | if (tracefile == NULL) { |
| 792 | fprintf(stderr, "Can't open %s\n", s); |
| 793 | debug = 0; |
| 794 | return; |
| 795 | } |
| 796 | } |
| 797 | #ifdef O_APPEND |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 798 | flags = fcntl(fileno(tracefile), F_GETFL); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 799 | if (flags >= 0) |
| 800 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 801 | #endif |
| 802 | setlinebuf(tracefile); |
| 803 | fputs("\nTracing started.\n", tracefile); |
| 804 | } |
| 805 | |
| 806 | static void |
| 807 | indent(int amount, char *pfx, FILE *fp) |
| 808 | { |
| 809 | int i; |
| 810 | |
| 811 | for (i = 0; i < amount; i++) { |
| 812 | if (pfx && i == amount - 1) |
| 813 | fputs(pfx, fp); |
| 814 | putc('\t', fp); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | /* little circular references here... */ |
| 819 | static void shtree(union node *n, int ind, char *pfx, FILE *fp); |
| 820 | |
| 821 | static void |
| 822 | sharg(union node *arg, FILE *fp) |
| 823 | { |
| 824 | char *p; |
| 825 | struct nodelist *bqlist; |
| 826 | int subtype; |
| 827 | |
| 828 | if (arg->type != NARG) { |
| 829 | out1fmt("<node type %d>\n", arg->type); |
| 830 | abort(); |
| 831 | } |
| 832 | bqlist = arg->narg.backquote; |
| 833 | for (p = arg->narg.text; *p; p++) { |
| 834 | switch (*p) { |
| 835 | case CTLESC: |
| 836 | putc(*++p, fp); |
| 837 | break; |
| 838 | case CTLVAR: |
| 839 | putc('$', fp); |
| 840 | putc('{', fp); |
| 841 | subtype = *++p; |
| 842 | if (subtype == VSLENGTH) |
| 843 | putc('#', fp); |
| 844 | |
| 845 | while (*p != '=') |
| 846 | putc(*p++, fp); |
| 847 | |
| 848 | if (subtype & VSNUL) |
| 849 | putc(':', fp); |
| 850 | |
| 851 | switch (subtype & VSTYPE) { |
| 852 | case VSNORMAL: |
| 853 | putc('}', fp); |
| 854 | break; |
| 855 | case VSMINUS: |
| 856 | putc('-', fp); |
| 857 | break; |
| 858 | case VSPLUS: |
| 859 | putc('+', fp); |
| 860 | break; |
| 861 | case VSQUESTION: |
| 862 | putc('?', fp); |
| 863 | break; |
| 864 | case VSASSIGN: |
| 865 | putc('=', fp); |
| 866 | break; |
| 867 | case VSTRIMLEFT: |
| 868 | putc('#', fp); |
| 869 | break; |
| 870 | case VSTRIMLEFTMAX: |
| 871 | putc('#', fp); |
| 872 | putc('#', fp); |
| 873 | break; |
| 874 | case VSTRIMRIGHT: |
| 875 | putc('%', fp); |
| 876 | break; |
| 877 | case VSTRIMRIGHTMAX: |
| 878 | putc('%', fp); |
| 879 | putc('%', fp); |
| 880 | break; |
| 881 | case VSLENGTH: |
| 882 | break; |
| 883 | default: |
| 884 | out1fmt("<subtype %d>", subtype); |
| 885 | } |
| 886 | break; |
| 887 | case CTLENDVAR: |
| 888 | putc('}', fp); |
| 889 | break; |
| 890 | case CTLBACKQ: |
| 891 | case CTLBACKQ|CTLQUOTE: |
| 892 | putc('$', fp); |
| 893 | putc('(', fp); |
| 894 | shtree(bqlist->n, -1, NULL, fp); |
| 895 | putc(')', fp); |
| 896 | break; |
| 897 | default: |
| 898 | putc(*p, fp); |
| 899 | break; |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 904 | static void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 905 | shcmd(union node *cmd, FILE *fp) |
| 906 | { |
| 907 | union node *np; |
| 908 | int first; |
| 909 | const char *s; |
| 910 | int dftfd; |
| 911 | |
| 912 | first = 1; |
| 913 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 914 | if (!first) |
| 915 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 916 | sharg(np, fp); |
| 917 | first = 0; |
| 918 | } |
| 919 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 920 | if (!first) |
| 921 | putc(' ', fp); |
| 922 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 923 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 924 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 925 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 926 | case NAPPEND: s = ">>"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 927 | #if ENABLE_ASH_BASH_COMPAT |
| 928 | case NTO2: |
| 929 | #endif |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 930 | case NTOFD: s = ">&"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 931 | case NFROM: s = "<"; break; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 932 | case NFROMFD: s = "<&"; break; |
| 933 | case NFROMTO: s = "<>"; break; |
| 934 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 935 | } |
| 936 | if (np->nfile.fd != dftfd) |
| 937 | fprintf(fp, "%d", np->nfile.fd); |
| 938 | fputs(s, fp); |
| 939 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 940 | fprintf(fp, "%d", np->ndup.dupfd); |
| 941 | } else { |
| 942 | sharg(np->nfile.fname, fp); |
| 943 | } |
| 944 | first = 0; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | static void |
| 949 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 950 | { |
| 951 | struct nodelist *lp; |
| 952 | const char *s; |
| 953 | |
| 954 | if (n == NULL) |
| 955 | return; |
| 956 | |
| 957 | indent(ind, pfx, fp); |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 958 | |
| 959 | if (n == NODE_EOF) { |
| 960 | fputs("<EOF>", fp); |
| 961 | return; |
| 962 | } |
| 963 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 964 | switch (n->type) { |
| 965 | case NSEMI: |
| 966 | s = "; "; |
| 967 | goto binop; |
| 968 | case NAND: |
| 969 | s = " && "; |
| 970 | goto binop; |
| 971 | case NOR: |
| 972 | s = " || "; |
| 973 | binop: |
| 974 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 975 | /* if (ind < 0) */ |
| 976 | fputs(s, fp); |
| 977 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 978 | break; |
| 979 | case NCMD: |
| 980 | shcmd(n, fp); |
| 981 | if (ind >= 0) |
| 982 | putc('\n', fp); |
| 983 | break; |
| 984 | case NPIPE: |
| 985 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 986 | shtree(lp->n, 0, NULL, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 987 | if (lp->next) |
| 988 | fputs(" | ", fp); |
| 989 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 990 | if (n->npipe.pipe_backgnd) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 991 | fputs(" &", fp); |
| 992 | if (ind >= 0) |
| 993 | putc('\n', fp); |
| 994 | break; |
| 995 | default: |
| 996 | fprintf(fp, "<node type %d>", n->type); |
| 997 | if (ind >= 0) |
| 998 | putc('\n', fp); |
| 999 | break; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | static void |
| 1004 | showtree(union node *n) |
| 1005 | { |
| 1006 | trace_puts("showtree called\n"); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 1007 | shtree(n, 1, NULL, stderr); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1010 | #endif /* DEBUG */ |
| 1011 | |
| 1012 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1013 | /* ============ Parser data */ |
| 1014 | |
| 1015 | /* |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1016 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 1017 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1018 | struct strlist { |
| 1019 | struct strlist *next; |
| 1020 | char *text; |
| 1021 | }; |
| 1022 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1023 | struct alias; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1024 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1025 | struct strpush { |
| 1026 | struct strpush *prev; /* preceding string on stack */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1027 | char *prev_string; |
| 1028 | int prev_left_in_line; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1029 | #if ENABLE_ASH_ALIAS |
| 1030 | struct alias *ap; /* if push was associated with an alias */ |
| 1031 | #endif |
| 1032 | char *string; /* remember the string since it may change */ |
| 1033 | }; |
| 1034 | |
| 1035 | struct parsefile { |
| 1036 | struct parsefile *prev; /* preceding file on stack */ |
| 1037 | int linno; /* current line */ |
| 1038 | int fd; /* file descriptor (or -1 if string) */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1039 | int left_in_line; /* number of chars left in this line */ |
| 1040 | int left_in_buffer; /* number of chars left in this buffer past the line */ |
| 1041 | char *next_to_pgetc; /* next char in buffer */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1042 | char *buf; /* input buffer */ |
| 1043 | struct strpush *strpush; /* for pushing strings at this level */ |
| 1044 | struct strpush basestrpush; /* so pushing one is fast */ |
| 1045 | }; |
| 1046 | |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1047 | static struct parsefile basepf; /* top level input file */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1048 | static struct parsefile *g_parsefile = &basepf; /* current input file */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1049 | static int startlinno; /* line # where last token started */ |
| 1050 | static char *commandname; /* currently executing command */ |
| 1051 | static struct strlist *cmdenviron; /* environment for builtin command */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1052 | static uint8_t exitstatus; /* exit status of last command */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1053 | |
| 1054 | |
| 1055 | /* ============ Message printing */ |
| 1056 | |
| 1057 | static void |
| 1058 | ash_vmsg(const char *msg, va_list ap) |
| 1059 | { |
| 1060 | fprintf(stderr, "%s: ", arg0); |
| 1061 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1062 | if (strcmp(arg0, commandname)) |
| 1063 | fprintf(stderr, "%s: ", commandname); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1064 | if (!iflag || g_parsefile->fd) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1065 | fprintf(stderr, "line %d: ", startlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1066 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1067 | vfprintf(stderr, msg, ap); |
| 1068 | outcslow('\n', stderr); |
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 | |
| 1071 | /* |
| 1072 | * Exverror is called to raise the error exception. If the second argument |
| 1073 | * is not NULL then error prints an error message using printf style |
| 1074 | * formatting. It then raises the error exception. |
| 1075 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1076 | static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1077 | static void |
| 1078 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1079 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1080 | #if DEBUG |
| 1081 | if (msg) { |
| 1082 | TRACE(("ash_vmsg_and_raise(%d, \"", cond)); |
| 1083 | TRACEV((msg, ap)); |
| 1084 | TRACE(("\") pid=%d\n", getpid())); |
| 1085 | } else |
| 1086 | TRACE(("ash_vmsg_and_raise(%d, NULL) pid=%d\n", cond, getpid())); |
| 1087 | if (msg) |
| 1088 | #endif |
| 1089 | ash_vmsg(msg, ap); |
| 1090 | |
| 1091 | flush_stdout_stderr(); |
| 1092 | raise_exception(cond); |
| 1093 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1094 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1095 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1096 | static void ash_msg_and_raise_error(const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1097 | static void |
| 1098 | ash_msg_and_raise_error(const char *msg, ...) |
| 1099 | { |
| 1100 | va_list ap; |
| 1101 | |
| 1102 | va_start(ap, msg); |
| 1103 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1104 | /* NOTREACHED */ |
| 1105 | va_end(ap); |
| 1106 | } |
| 1107 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1108 | static void raise_error_syntax(const char *) NORETURN; |
| 1109 | static void |
| 1110 | raise_error_syntax(const char *msg) |
| 1111 | { |
| 1112 | ash_msg_and_raise_error("syntax error: %s", msg); |
| 1113 | /* NOTREACHED */ |
| 1114 | } |
| 1115 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1116 | static void ash_msg_and_raise(int, const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1117 | static void |
| 1118 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1119 | { |
| 1120 | va_list ap; |
| 1121 | |
| 1122 | va_start(ap, msg); |
| 1123 | ash_vmsg_and_raise(cond, msg, ap); |
| 1124 | /* NOTREACHED */ |
| 1125 | va_end(ap); |
| 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | * error/warning routines for external builtins |
| 1130 | */ |
| 1131 | static void |
| 1132 | ash_msg(const char *fmt, ...) |
| 1133 | { |
| 1134 | va_list ap; |
| 1135 | |
| 1136 | va_start(ap, fmt); |
| 1137 | ash_vmsg(fmt, ap); |
| 1138 | va_end(ap); |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * Return a string describing an error. The returned string may be a |
| 1143 | * pointer to a static buffer that will be overwritten on the next call. |
| 1144 | * Action describes the operation that got the error. |
| 1145 | */ |
| 1146 | static const char * |
| 1147 | errmsg(int e, const char *em) |
| 1148 | { |
| 1149 | if (e == ENOENT || e == ENOTDIR) { |
| 1150 | return em; |
| 1151 | } |
| 1152 | return strerror(e); |
| 1153 | } |
| 1154 | |
| 1155 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1156 | /* ============ Memory allocation */ |
| 1157 | |
| 1158 | /* |
| 1159 | * It appears that grabstackstr() will barf with such alignments |
| 1160 | * because stalloc() will return a string allocated in a new stackblock. |
| 1161 | */ |
| 1162 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1163 | enum { |
| 1164 | /* Most machines require the value returned from malloc to be aligned |
| 1165 | * in some way. The following macro will get this right |
| 1166 | * on many machines. */ |
| 1167 | SHELL_SIZE = sizeof(union {int i; char *cp; double d; }) - 1, |
| 1168 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1169 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1170 | }; |
| 1171 | |
| 1172 | struct stack_block { |
| 1173 | struct stack_block *prev; |
| 1174 | char space[MINSIZE]; |
| 1175 | }; |
| 1176 | |
| 1177 | struct stackmark { |
| 1178 | struct stack_block *stackp; |
| 1179 | char *stacknxt; |
| 1180 | size_t stacknleft; |
| 1181 | struct stackmark *marknext; |
| 1182 | }; |
| 1183 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1184 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1185 | struct globals_memstack { |
| 1186 | struct stack_block *g_stackp; // = &stackbase; |
| 1187 | struct stackmark *markp; |
| 1188 | char *g_stacknxt; // = stackbase.space; |
| 1189 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1190 | size_t g_stacknleft; // = MINSIZE; |
| 1191 | int herefd; // = -1; |
| 1192 | struct stack_block stackbase; |
| 1193 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1194 | extern struct globals_memstack *const ash_ptr_to_globals_memstack; |
| 1195 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1196 | #define g_stackp (G_memstack.g_stackp ) |
| 1197 | #define markp (G_memstack.markp ) |
| 1198 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1199 | #define sstrend (G_memstack.sstrend ) |
| 1200 | #define g_stacknleft (G_memstack.g_stacknleft) |
| 1201 | #define herefd (G_memstack.herefd ) |
| 1202 | #define stackbase (G_memstack.stackbase ) |
| 1203 | #define INIT_G_memstack() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1204 | (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \ |
| 1205 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1206 | g_stackp = &stackbase; \ |
| 1207 | g_stacknxt = stackbase.space; \ |
| 1208 | g_stacknleft = MINSIZE; \ |
| 1209 | sstrend = stackbase.space + MINSIZE; \ |
| 1210 | herefd = -1; \ |
| 1211 | } while (0) |
| 1212 | |
| 1213 | #define stackblock() ((void *)g_stacknxt) |
| 1214 | #define stackblocksize() g_stacknleft |
| 1215 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1216 | |
| 1217 | static void * |
| 1218 | ckrealloc(void * p, size_t nbytes) |
| 1219 | { |
| 1220 | p = realloc(p, nbytes); |
| 1221 | if (!p) |
| 1222 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1223 | return p; |
| 1224 | } |
| 1225 | |
| 1226 | static void * |
| 1227 | ckmalloc(size_t nbytes) |
| 1228 | { |
| 1229 | return ckrealloc(NULL, nbytes); |
| 1230 | } |
| 1231 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1232 | static void * |
| 1233 | ckzalloc(size_t nbytes) |
| 1234 | { |
| 1235 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1236 | } |
| 1237 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1238 | /* |
| 1239 | * Make a copy of a string in safe storage. |
| 1240 | */ |
| 1241 | static char * |
| 1242 | ckstrdup(const char *s) |
| 1243 | { |
| 1244 | char *p = strdup(s); |
| 1245 | if (!p) |
| 1246 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1247 | return p; |
| 1248 | } |
| 1249 | |
| 1250 | /* |
| 1251 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1252 | * to make this more efficient, and also to avoid all sorts of exception |
| 1253 | * handling code to handle interrupts in the middle of a parse. |
| 1254 | * |
| 1255 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1256 | * well. |
| 1257 | */ |
| 1258 | static void * |
| 1259 | stalloc(size_t nbytes) |
| 1260 | { |
| 1261 | char *p; |
| 1262 | size_t aligned; |
| 1263 | |
| 1264 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1265 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1266 | size_t len; |
| 1267 | size_t blocksize; |
| 1268 | struct stack_block *sp; |
| 1269 | |
| 1270 | blocksize = aligned; |
| 1271 | if (blocksize < MINSIZE) |
| 1272 | blocksize = MINSIZE; |
| 1273 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1274 | if (len < blocksize) |
| 1275 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1276 | INT_OFF; |
| 1277 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1278 | sp->prev = g_stackp; |
| 1279 | g_stacknxt = sp->space; |
| 1280 | g_stacknleft = blocksize; |
| 1281 | sstrend = g_stacknxt + blocksize; |
| 1282 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1283 | INT_ON; |
| 1284 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1285 | p = g_stacknxt; |
| 1286 | g_stacknxt += aligned; |
| 1287 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1288 | return p; |
| 1289 | } |
| 1290 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1291 | static void * |
| 1292 | stzalloc(size_t nbytes) |
| 1293 | { |
| 1294 | return memset(stalloc(nbytes), 0, nbytes); |
| 1295 | } |
| 1296 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1297 | static void |
| 1298 | stunalloc(void *p) |
| 1299 | { |
| 1300 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1301 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1302 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1303 | abort(); |
| 1304 | } |
| 1305 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1306 | g_stacknleft += g_stacknxt - (char *)p; |
| 1307 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1310 | /* |
| 1311 | * Like strdup but works with the ash stack. |
| 1312 | */ |
| 1313 | static char * |
| 1314 | ststrdup(const char *p) |
| 1315 | { |
| 1316 | size_t len = strlen(p) + 1; |
| 1317 | return memcpy(stalloc(len), p, len); |
| 1318 | } |
| 1319 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1320 | static void |
| 1321 | setstackmark(struct stackmark *mark) |
| 1322 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1323 | mark->stackp = g_stackp; |
| 1324 | mark->stacknxt = g_stacknxt; |
| 1325 | mark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1326 | mark->marknext = markp; |
| 1327 | markp = mark; |
| 1328 | } |
| 1329 | |
| 1330 | static void |
| 1331 | popstackmark(struct stackmark *mark) |
| 1332 | { |
| 1333 | struct stack_block *sp; |
| 1334 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1335 | if (!mark->stackp) |
| 1336 | return; |
| 1337 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1338 | INT_OFF; |
| 1339 | markp = mark->marknext; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1340 | while (g_stackp != mark->stackp) { |
| 1341 | sp = g_stackp; |
| 1342 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1343 | free(sp); |
| 1344 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1345 | g_stacknxt = mark->stacknxt; |
| 1346 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1347 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1348 | INT_ON; |
| 1349 | } |
| 1350 | |
| 1351 | /* |
| 1352 | * When the parser reads in a string, it wants to stick the string on the |
| 1353 | * stack and only adjust the stack pointer when it knows how big the |
| 1354 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1355 | * of space on top of the stack and stackblocklen returns the length of |
| 1356 | * this block. Growstackblock will grow this space by at least one byte, |
| 1357 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1358 | * part of the block that has been used. |
| 1359 | */ |
| 1360 | static void |
| 1361 | growstackblock(void) |
| 1362 | { |
| 1363 | size_t newlen; |
| 1364 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1365 | newlen = g_stacknleft * 2; |
| 1366 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1367 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1368 | if (newlen < 128) |
| 1369 | newlen += 128; |
| 1370 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1371 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1372 | struct stack_block *oldstackp; |
| 1373 | struct stackmark *xmark; |
| 1374 | struct stack_block *sp; |
| 1375 | struct stack_block *prevstackp; |
| 1376 | size_t grosslen; |
| 1377 | |
| 1378 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1379 | oldstackp = g_stackp; |
| 1380 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1381 | prevstackp = sp->prev; |
| 1382 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1383 | sp = ckrealloc(sp, grosslen); |
| 1384 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1385 | g_stackp = sp; |
| 1386 | g_stacknxt = sp->space; |
| 1387 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1388 | sstrend = sp->space + newlen; |
| 1389 | |
| 1390 | /* |
| 1391 | * Stack marks pointing to the start of the old block |
| 1392 | * must be relocated to point to the new block |
| 1393 | */ |
| 1394 | xmark = markp; |
| 1395 | while (xmark != NULL && xmark->stackp == oldstackp) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1396 | xmark->stackp = g_stackp; |
| 1397 | xmark->stacknxt = g_stacknxt; |
| 1398 | xmark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1399 | xmark = xmark->marknext; |
| 1400 | } |
| 1401 | INT_ON; |
| 1402 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1403 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1404 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1405 | char *p = stalloc(newlen); |
| 1406 | |
| 1407 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1408 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1409 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | static void |
| 1414 | grabstackblock(size_t len) |
| 1415 | { |
| 1416 | len = SHELL_ALIGN(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1417 | g_stacknxt += len; |
| 1418 | g_stacknleft -= len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | /* |
| 1422 | * The following routines are somewhat easier to use than the above. |
| 1423 | * The user declares a variable of type STACKSTR, which may be declared |
| 1424 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1425 | * the user uses the macro STPUTC to add characters to the string. In |
| 1426 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1427 | * grown as necessary. When the user is done, she can just leave the |
| 1428 | * string there and refer to it using stackblock(). Or she can allocate |
| 1429 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1430 | * someone else to use the stack temporarily and then continue to grow |
| 1431 | * the string, the user should use grabstack to allocate the space, and |
| 1432 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1433 | * |
| 1434 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1435 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1436 | * is space for at least one character. |
| 1437 | */ |
| 1438 | static void * |
| 1439 | growstackstr(void) |
| 1440 | { |
| 1441 | size_t len = stackblocksize(); |
| 1442 | if (herefd >= 0 && len >= 1024) { |
| 1443 | full_write(herefd, stackblock(), len); |
| 1444 | return stackblock(); |
| 1445 | } |
| 1446 | growstackblock(); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1447 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * Called from CHECKSTRSPACE. |
| 1452 | */ |
| 1453 | static char * |
| 1454 | makestrspace(size_t newlen, char *p) |
| 1455 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1456 | size_t len = p - g_stacknxt; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1457 | size_t size = stackblocksize(); |
| 1458 | |
| 1459 | for (;;) { |
| 1460 | size_t nleft; |
| 1461 | |
| 1462 | size = stackblocksize(); |
| 1463 | nleft = size - len; |
| 1464 | if (nleft >= newlen) |
| 1465 | break; |
| 1466 | growstackblock(); |
| 1467 | } |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1468 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | static char * |
| 1472 | stack_nputstr(const char *s, size_t n, char *p) |
| 1473 | { |
| 1474 | p = makestrspace(n, p); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1475 | p = (char *)memcpy(p, s, n) + n; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1476 | return p; |
| 1477 | } |
| 1478 | |
| 1479 | static char * |
| 1480 | stack_putstr(const char *s, char *p) |
| 1481 | { |
| 1482 | return stack_nputstr(s, strlen(s), p); |
| 1483 | } |
| 1484 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1485 | static char * |
| 1486 | _STPUTC(int c, char *p) |
| 1487 | { |
| 1488 | if (p == sstrend) |
| 1489 | p = growstackstr(); |
| 1490 | *p++ = c; |
| 1491 | return p; |
| 1492 | } |
| 1493 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1494 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1495 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1496 | #define CHECKSTRSPACE(n, p) do { \ |
| 1497 | char *q = (p); \ |
| 1498 | size_t l = (n); \ |
| 1499 | size_t m = sstrend - q; \ |
| 1500 | if (l > m) \ |
| 1501 | (p) = makestrspace(l, q); \ |
| 1502 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1503 | #define USTPUTC(c, p) (*(p)++ = (c)) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1504 | #define STACKSTRNUL(p) do { \ |
| 1505 | if ((p) == sstrend) \ |
| 1506 | (p) = growstackstr(); \ |
| 1507 | *(p) = '\0'; \ |
| 1508 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1509 | #define STUNPUTC(p) (--(p)) |
| 1510 | #define STTOPC(p) ((p)[-1]) |
| 1511 | #define STADJUST(amount, p) ((p) += (amount)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1512 | |
| 1513 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1514 | #define ungrabstackstr(s, p) stunalloc(s) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1515 | #define stackstrend() ((void *)sstrend) |
| 1516 | |
| 1517 | |
| 1518 | /* ============ String helpers */ |
| 1519 | |
| 1520 | /* |
| 1521 | * prefix -- see if pfx is a prefix of string. |
| 1522 | */ |
| 1523 | static char * |
| 1524 | prefix(const char *string, const char *pfx) |
| 1525 | { |
| 1526 | while (*pfx) { |
| 1527 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1528 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1529 | } |
| 1530 | return (char *) string; |
| 1531 | } |
| 1532 | |
| 1533 | /* |
| 1534 | * Check for a valid number. This should be elsewhere. |
| 1535 | */ |
| 1536 | static int |
| 1537 | is_number(const char *p) |
| 1538 | { |
| 1539 | do { |
| 1540 | if (!isdigit(*p)) |
| 1541 | return 0; |
| 1542 | } while (*++p != '\0'); |
| 1543 | return 1; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
| 1547 | * Convert a string of digits to an integer, printing an error message on |
| 1548 | * failure. |
| 1549 | */ |
| 1550 | static int |
| 1551 | number(const char *s) |
| 1552 | { |
| 1553 | if (!is_number(s)) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1554 | ash_msg_and_raise_error(msg_illnum, s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1555 | return atoi(s); |
| 1556 | } |
| 1557 | |
| 1558 | /* |
| 1559 | * Produce a possibly single quoted string suitable as input to the shell. |
| 1560 | * The return string is allocated on the stack. |
| 1561 | */ |
| 1562 | static char * |
| 1563 | single_quote(const char *s) |
| 1564 | { |
| 1565 | char *p; |
| 1566 | |
| 1567 | STARTSTACKSTR(p); |
| 1568 | |
| 1569 | do { |
| 1570 | char *q; |
| 1571 | size_t len; |
| 1572 | |
| 1573 | len = strchrnul(s, '\'') - s; |
| 1574 | |
| 1575 | q = p = makestrspace(len + 3, p); |
| 1576 | |
| 1577 | *q++ = '\''; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1578 | q = (char *)memcpy(q, s, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1579 | *q++ = '\''; |
| 1580 | s += len; |
| 1581 | |
| 1582 | STADJUST(q - p, p); |
| 1583 | |
| 1584 | len = strspn(s, "'"); |
| 1585 | if (!len) |
| 1586 | break; |
| 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 | } while (*s); |
| 1597 | |
| 1598 | USTPUTC(0, p); |
| 1599 | |
| 1600 | return stackblock(); |
| 1601 | } |
| 1602 | |
| 1603 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1604 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1605 | |
| 1606 | static char **argptr; /* argument list for builtin commands */ |
| 1607 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1608 | static char *optptr; /* used by nextopt */ |
| 1609 | |
| 1610 | /* |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1611 | * XXX - should get rid of. Have all builtins use getopt(3). |
| 1612 | * The library getopt must have the BSD extension static variable |
| 1613 | * "optreset", otherwise it can't be used within the shell safely. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1614 | * |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1615 | * Standard option processing (a la getopt) for builtin routines. |
| 1616 | * The only argument that is passed to nextopt is the option string; |
| 1617 | * the other arguments are unnecessary. It returns the character, |
| 1618 | * or '\0' on end of input. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1619 | */ |
| 1620 | static int |
| 1621 | nextopt(const char *optstring) |
| 1622 | { |
| 1623 | char *p; |
| 1624 | const char *q; |
| 1625 | char c; |
| 1626 | |
| 1627 | p = optptr; |
| 1628 | if (p == NULL || *p == '\0') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1629 | /* We ate entire "-param", take next one */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1630 | p = *argptr; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1631 | if (p == NULL) |
| 1632 | return '\0'; |
| 1633 | if (*p != '-') |
| 1634 | return '\0'; |
| 1635 | if (*++p == '\0') /* just "-" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1636 | return '\0'; |
| 1637 | argptr++; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1638 | if (LONE_DASH(p)) /* "--" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1639 | return '\0'; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1640 | /* p => next "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1641 | } |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1642 | /* p => some option char in the middle of a "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1643 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 1644 | for (q = optstring; *q != c;) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1645 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1646 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1647 | if (*++q == ':') |
| 1648 | q++; |
| 1649 | } |
| 1650 | if (*++q == ':') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1651 | if (*p == '\0') { |
| 1652 | p = *argptr++; |
| 1653 | if (p == NULL) |
| 1654 | ash_msg_and_raise_error("no arg for -%c option", c); |
| 1655 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1656 | optionarg = p; |
| 1657 | p = NULL; |
| 1658 | } |
| 1659 | optptr = p; |
| 1660 | return c; |
| 1661 | } |
| 1662 | |
| 1663 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1664 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1665 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1666 | /* |
| 1667 | * The parsefile structure pointed to by the global variable parsefile |
| 1668 | * contains information about the current file being read. |
| 1669 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1670 | struct shparam { |
| 1671 | int nparam; /* # of positional parameters (without $0) */ |
| 1672 | #if ENABLE_ASH_GETOPTS |
| 1673 | int optind; /* next parameter to be processed by getopts */ |
| 1674 | int optoff; /* used by getopts */ |
| 1675 | #endif |
| 1676 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 1677 | char **p; /* parameter list */ |
| 1678 | }; |
| 1679 | |
| 1680 | /* |
| 1681 | * Free the list of positional parameters. |
| 1682 | */ |
| 1683 | static void |
| 1684 | freeparam(volatile struct shparam *param) |
| 1685 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1686 | if (param->malloced) { |
Denis Vlasenko | 3177ba0 | 2008-07-13 20:39:23 +0000 | [diff] [blame] | 1687 | char **ap, **ap1; |
| 1688 | ap = ap1 = param->p; |
| 1689 | while (*ap) |
| 1690 | free(*ap++); |
| 1691 | free(ap1); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1696 | static void FAST_FUNC getoptsreset(const char *value); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1697 | #endif |
| 1698 | |
| 1699 | struct var { |
| 1700 | struct var *next; /* next entry in hash list */ |
| 1701 | int flags; /* flags are defined above */ |
| 1702 | const char *text; /* name=value */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1703 | void (*func)(const char *) FAST_FUNC; /* function to be called when */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1704 | /* the variable gets set/unset */ |
| 1705 | }; |
| 1706 | |
| 1707 | struct localvar { |
| 1708 | struct localvar *next; /* next local variable in list */ |
| 1709 | struct var *vp; /* the variable that was made local */ |
| 1710 | int flags; /* saved flags */ |
| 1711 | const char *text; /* saved text */ |
| 1712 | }; |
| 1713 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1714 | /* flags */ |
| 1715 | #define VEXPORT 0x01 /* variable is exported */ |
| 1716 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 1717 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 1718 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 1719 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 1720 | #define VUNSET 0x20 /* the variable is not set */ |
| 1721 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 1722 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 1723 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1724 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1725 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 1726 | #else |
| 1727 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1728 | #endif |
| 1729 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1730 | #ifdef IFS_BROKEN |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1731 | static const char defifsvar[] ALIGN1 = "IFS= \t\n"; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1732 | #define defifs (defifsvar + 4) |
| 1733 | #else |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1734 | static const char defifs[] ALIGN1 = " \t\n"; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1735 | #endif |
| 1736 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1737 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1738 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1739 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1740 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1741 | change_lc_all(const char *value) |
| 1742 | { |
| 1743 | if (value && *value != '\0') |
| 1744 | setlocale(LC_ALL, value); |
| 1745 | } |
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_ctype(const char *value) |
| 1748 | { |
| 1749 | if (value && *value != '\0') |
| 1750 | setlocale(LC_CTYPE, value); |
| 1751 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1752 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1753 | #if ENABLE_ASH_MAIL |
| 1754 | static void chkmail(void); |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1755 | static void changemail(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1756 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1757 | static void changepath(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1758 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1759 | static void change_random(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1760 | #endif |
| 1761 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1762 | static const struct { |
| 1763 | int flags; |
| 1764 | const char *text; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1765 | void (*func)(const char *) FAST_FUNC; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1766 | } varinit_data[] = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1767 | #ifdef IFS_BROKEN |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1768 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1769 | #else |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1770 | { VSTRFIXED|VTEXTFIXED|VUNSET, "IFS\0" , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1771 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1772 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1773 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL\0" , changemail }, |
| 1774 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH\0", changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1775 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1776 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 1777 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 1778 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 1779 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1780 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1781 | { VSTRFIXED|VTEXTFIXED , "OPTIND=1" , getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1782 | #endif |
| 1783 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1784 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM\0", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1785 | #endif |
| 1786 | #if ENABLE_LOCALE_SUPPORT |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1787 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL\0" , change_lc_all }, |
| 1788 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE\0", change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1789 | #endif |
| 1790 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1791 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE\0", NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1792 | #endif |
| 1793 | }; |
| 1794 | |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1795 | struct redirtab; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1796 | |
| 1797 | struct globals_var { |
| 1798 | struct shparam shellparam; /* $@ current positional parameters */ |
| 1799 | struct redirtab *redirlist; |
| 1800 | int g_nullredirs; |
| 1801 | int preverrout_fd; /* save fd2 before print debug if xflag is set. */ |
| 1802 | struct var *vartab[VTABSIZE]; |
| 1803 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
| 1804 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1805 | extern struct globals_var *const ash_ptr_to_globals_var; |
| 1806 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1807 | #define shellparam (G_var.shellparam ) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1808 | //#define redirlist (G_var.redirlist ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1809 | #define g_nullredirs (G_var.g_nullredirs ) |
| 1810 | #define preverrout_fd (G_var.preverrout_fd) |
| 1811 | #define vartab (G_var.vartab ) |
| 1812 | #define varinit (G_var.varinit ) |
| 1813 | #define INIT_G_var() do { \ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1814 | unsigned i; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1815 | (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \ |
| 1816 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1817 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
| 1818 | varinit[i].flags = varinit_data[i].flags; \ |
| 1819 | varinit[i].text = varinit_data[i].text; \ |
| 1820 | varinit[i].func = varinit_data[i].func; \ |
| 1821 | } \ |
| 1822 | } while (0) |
| 1823 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1824 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1825 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1826 | # define vmail (&vifs)[1] |
| 1827 | # define vmpath (&vmail)[1] |
| 1828 | # define vpath (&vmpath)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1829 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1830 | # define vpath (&vifs)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1831 | #endif |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1832 | #define vps1 (&vpath)[1] |
| 1833 | #define vps2 (&vps1)[1] |
| 1834 | #define vps4 (&vps2)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1835 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1836 | # define voptind (&vps4)[1] |
| 1837 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1838 | # define vrandom (&voptind)[1] |
| 1839 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1840 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1841 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1842 | # define vrandom (&vps4)[1] |
| 1843 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1844 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1845 | |
| 1846 | /* |
| 1847 | * The following macros access the values of the above variables. |
| 1848 | * They have to skip over the name. They return the null string |
| 1849 | * for unset variables. |
| 1850 | */ |
| 1851 | #define ifsval() (vifs.text + 4) |
| 1852 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1853 | #if ENABLE_ASH_MAIL |
| 1854 | # define mailval() (vmail.text + 5) |
| 1855 | # define mpathval() (vmpath.text + 9) |
| 1856 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1857 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1858 | #define pathval() (vpath.text + 5) |
| 1859 | #define ps1val() (vps1.text + 4) |
| 1860 | #define ps2val() (vps2.text + 4) |
| 1861 | #define ps4val() (vps4.text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1862 | #if ENABLE_ASH_GETOPTS |
| 1863 | # define optindval() (voptind.text + 7) |
| 1864 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1865 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1866 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1867 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1868 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1869 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1870 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1871 | static void FAST_FUNC |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1872 | getoptsreset(const char *value) |
| 1873 | { |
| 1874 | shellparam.optind = number(value); |
| 1875 | shellparam.optoff = -1; |
| 1876 | } |
| 1877 | #endif |
| 1878 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1879 | /* |
| 1880 | * Return of a legal variable name (a letter or underscore followed by zero or |
| 1881 | * more letters, underscores, and digits). |
| 1882 | */ |
| 1883 | static char * |
| 1884 | endofname(const char *name) |
| 1885 | { |
| 1886 | char *p; |
| 1887 | |
| 1888 | p = (char *) name; |
| 1889 | if (!is_name(*p)) |
| 1890 | return p; |
| 1891 | while (*++p) { |
| 1892 | if (!is_in_name(*p)) |
| 1893 | break; |
| 1894 | } |
| 1895 | return p; |
| 1896 | } |
| 1897 | |
| 1898 | /* |
| 1899 | * Compares two strings up to the first = or '\0'. The first |
| 1900 | * string must be terminated by '='; the second may be terminated by |
| 1901 | * either '=' or '\0'. |
| 1902 | */ |
| 1903 | static int |
| 1904 | varcmp(const char *p, const char *q) |
| 1905 | { |
| 1906 | int c, d; |
| 1907 | |
| 1908 | while ((c = *p) == (d = *q)) { |
| 1909 | if (!c || c == '=') |
| 1910 | goto out; |
| 1911 | p++; |
| 1912 | q++; |
| 1913 | } |
| 1914 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 1915 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1916 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 1917 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1918 | out: |
| 1919 | return c - d; |
| 1920 | } |
| 1921 | |
| 1922 | static int |
| 1923 | varequal(const char *a, const char *b) |
| 1924 | { |
| 1925 | return !varcmp(a, b); |
| 1926 | } |
| 1927 | |
| 1928 | /* |
| 1929 | * Find the appropriate entry in the hash table from the name. |
| 1930 | */ |
| 1931 | static struct var ** |
| 1932 | hashvar(const char *p) |
| 1933 | { |
| 1934 | unsigned hashval; |
| 1935 | |
| 1936 | hashval = ((unsigned char) *p) << 4; |
| 1937 | while (*p && *p != '=') |
| 1938 | hashval += (unsigned char) *p++; |
| 1939 | return &vartab[hashval % VTABSIZE]; |
| 1940 | } |
| 1941 | |
| 1942 | static int |
| 1943 | vpcmp(const void *a, const void *b) |
| 1944 | { |
| 1945 | return varcmp(*(const char **)a, *(const char **)b); |
| 1946 | } |
| 1947 | |
| 1948 | /* |
| 1949 | * This routine initializes the builtin variables. |
| 1950 | */ |
| 1951 | static void |
| 1952 | initvar(void) |
| 1953 | { |
| 1954 | struct var *vp; |
| 1955 | struct var *end; |
| 1956 | struct var **vpp; |
| 1957 | |
| 1958 | /* |
| 1959 | * PS1 depends on uid |
| 1960 | */ |
| 1961 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 1962 | vps1.text = "PS1=\\w \\$ "; |
| 1963 | #else |
| 1964 | if (!geteuid()) |
| 1965 | vps1.text = "PS1=# "; |
| 1966 | #endif |
| 1967 | vp = varinit; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 1968 | end = vp + ARRAY_SIZE(varinit); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1969 | do { |
| 1970 | vpp = hashvar(vp->text); |
| 1971 | vp->next = *vpp; |
| 1972 | *vpp = vp; |
| 1973 | } while (++vp < end); |
| 1974 | } |
| 1975 | |
| 1976 | static struct var ** |
| 1977 | findvar(struct var **vpp, const char *name) |
| 1978 | { |
| 1979 | for (; *vpp; vpp = &(*vpp)->next) { |
| 1980 | if (varequal((*vpp)->text, name)) { |
| 1981 | break; |
| 1982 | } |
| 1983 | } |
| 1984 | return vpp; |
| 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * Find the value of a variable. Returns NULL if not set. |
| 1989 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1990 | static const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1991 | lookupvar(const char *name) |
| 1992 | { |
| 1993 | struct var *v; |
| 1994 | |
| 1995 | v = *findvar(hashvar(name), name); |
| 1996 | if (v) { |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1997 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1998 | /* |
| 1999 | * Dynamic variables are implemented roughly the same way they are |
| 2000 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 2001 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 2002 | * lookup will no longer happen at that point. -- PFM. |
| 2003 | */ |
| 2004 | if ((v->flags & VDYNAMIC)) |
| 2005 | (*v->func)(NULL); |
| 2006 | #endif |
| 2007 | if (!(v->flags & VUNSET)) |
| 2008 | return strchrnul(v->text, '=') + 1; |
| 2009 | } |
| 2010 | return NULL; |
| 2011 | } |
| 2012 | |
| 2013 | /* |
| 2014 | * Search the environment of a builtin command. |
| 2015 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2016 | static const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2017 | bltinlookup(const char *name) |
| 2018 | { |
| 2019 | struct strlist *sp; |
| 2020 | |
| 2021 | for (sp = cmdenviron; sp; sp = sp->next) { |
| 2022 | if (varequal(sp->text, name)) |
| 2023 | return strchrnul(sp->text, '=') + 1; |
| 2024 | } |
| 2025 | return lookupvar(name); |
| 2026 | } |
| 2027 | |
| 2028 | /* |
| 2029 | * Same as setvar except that the variable and value are passed in |
| 2030 | * the first argument as name=value. Since the first argument will |
| 2031 | * be actually stored in the table, it should not be a string that |
| 2032 | * will go away. |
| 2033 | * Called with interrupts off. |
| 2034 | */ |
| 2035 | static void |
| 2036 | setvareq(char *s, int flags) |
| 2037 | { |
| 2038 | struct var *vp, **vpp; |
| 2039 | |
| 2040 | vpp = hashvar(s); |
| 2041 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
| 2042 | vp = *findvar(vpp, s); |
| 2043 | if (vp) { |
| 2044 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 2045 | const char *n; |
| 2046 | |
| 2047 | if (flags & VNOSAVE) |
| 2048 | free(s); |
| 2049 | n = vp->text; |
| 2050 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2051 | } |
| 2052 | |
| 2053 | if (flags & VNOSET) |
| 2054 | return; |
| 2055 | |
| 2056 | if (vp->func && (flags & VNOFUNC) == 0) |
| 2057 | (*vp->func)(strchrnul(s, '=') + 1); |
| 2058 | |
| 2059 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 2060 | free((char*)vp->text); |
| 2061 | |
| 2062 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
| 2063 | } else { |
| 2064 | if (flags & VNOSET) |
| 2065 | return; |
| 2066 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2067 | vp = ckzalloc(sizeof(*vp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2068 | vp->next = *vpp; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2069 | /*vp->func = NULL; - ckzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2070 | *vpp = vp; |
| 2071 | } |
| 2072 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 2073 | s = ckstrdup(s); |
| 2074 | vp->text = s; |
| 2075 | vp->flags = flags; |
| 2076 | } |
| 2077 | |
| 2078 | /* |
| 2079 | * Set the value of a variable. The flags argument is ored with the |
| 2080 | * flags of the variable. If val is NULL, the variable is unset. |
| 2081 | */ |
| 2082 | static void |
| 2083 | setvar(const char *name, const char *val, int flags) |
| 2084 | { |
| 2085 | char *p, *q; |
| 2086 | size_t namelen; |
| 2087 | char *nameeq; |
| 2088 | size_t vallen; |
| 2089 | |
| 2090 | q = endofname(name); |
| 2091 | p = strchrnul(q, '='); |
| 2092 | namelen = p - name; |
| 2093 | if (!namelen || p != q) |
| 2094 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 2095 | vallen = 0; |
| 2096 | if (val == NULL) { |
| 2097 | flags |= VUNSET; |
| 2098 | } else { |
| 2099 | vallen = strlen(val); |
| 2100 | } |
| 2101 | INT_OFF; |
| 2102 | nameeq = ckmalloc(namelen + vallen + 2); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2103 | p = (char *)memcpy(nameeq, name, namelen) + namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2104 | if (val) { |
| 2105 | *p++ = '='; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2106 | p = (char *)memcpy(p, val, vallen) + vallen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2107 | } |
| 2108 | *p = '\0'; |
| 2109 | setvareq(nameeq, flags | VNOSAVE); |
| 2110 | INT_ON; |
| 2111 | } |
| 2112 | |
| 2113 | #if ENABLE_ASH_GETOPTS |
| 2114 | /* |
| 2115 | * Safe version of setvar, returns 1 on success 0 on failure. |
| 2116 | */ |
| 2117 | static int |
| 2118 | setvarsafe(const char *name, const char *val, int flags) |
| 2119 | { |
| 2120 | int err; |
| 2121 | volatile int saveint; |
| 2122 | struct jmploc *volatile savehandler = exception_handler; |
| 2123 | struct jmploc jmploc; |
| 2124 | |
| 2125 | SAVE_INT(saveint); |
| 2126 | if (setjmp(jmploc.loc)) |
| 2127 | err = 1; |
| 2128 | else { |
| 2129 | exception_handler = &jmploc; |
| 2130 | setvar(name, val, flags); |
| 2131 | err = 0; |
| 2132 | } |
| 2133 | exception_handler = savehandler; |
| 2134 | RESTORE_INT(saveint); |
| 2135 | return err; |
| 2136 | } |
| 2137 | #endif |
| 2138 | |
| 2139 | /* |
| 2140 | * Unset the specified variable. |
| 2141 | */ |
| 2142 | static int |
| 2143 | unsetvar(const char *s) |
| 2144 | { |
| 2145 | struct var **vpp; |
| 2146 | struct var *vp; |
| 2147 | int retval; |
| 2148 | |
| 2149 | vpp = findvar(hashvar(s), s); |
| 2150 | vp = *vpp; |
| 2151 | retval = 2; |
| 2152 | if (vp) { |
| 2153 | int flags = vp->flags; |
| 2154 | |
| 2155 | retval = 1; |
| 2156 | if (flags & VREADONLY) |
| 2157 | goto out; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2158 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2159 | vp->flags &= ~VDYNAMIC; |
| 2160 | #endif |
| 2161 | if (flags & VUNSET) |
| 2162 | goto ok; |
| 2163 | if ((flags & VSTRFIXED) == 0) { |
| 2164 | INT_OFF; |
| 2165 | if ((flags & (VTEXTFIXED|VSTACK)) == 0) |
| 2166 | free((char*)vp->text); |
| 2167 | *vpp = vp->next; |
| 2168 | free(vp); |
| 2169 | INT_ON; |
| 2170 | } else { |
| 2171 | setvar(s, 0, 0); |
| 2172 | vp->flags &= ~VEXPORT; |
| 2173 | } |
| 2174 | ok: |
| 2175 | retval = 0; |
| 2176 | } |
| 2177 | out: |
| 2178 | return retval; |
| 2179 | } |
| 2180 | |
| 2181 | /* |
| 2182 | * Process a linked list of variable assignments. |
| 2183 | */ |
| 2184 | static void |
| 2185 | listsetvar(struct strlist *list_set_var, int flags) |
| 2186 | { |
| 2187 | struct strlist *lp = list_set_var; |
| 2188 | |
| 2189 | if (!lp) |
| 2190 | return; |
| 2191 | INT_OFF; |
| 2192 | do { |
| 2193 | setvareq(lp->text, flags); |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2194 | lp = lp->next; |
| 2195 | } while (lp); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2196 | INT_ON; |
| 2197 | } |
| 2198 | |
| 2199 | /* |
| 2200 | * Generate a list of variables satisfying the given conditions. |
| 2201 | */ |
| 2202 | static char ** |
| 2203 | listvars(int on, int off, char ***end) |
| 2204 | { |
| 2205 | struct var **vpp; |
| 2206 | struct var *vp; |
| 2207 | char **ep; |
| 2208 | int mask; |
| 2209 | |
| 2210 | STARTSTACKSTR(ep); |
| 2211 | vpp = vartab; |
| 2212 | mask = on | off; |
| 2213 | do { |
| 2214 | for (vp = *vpp; vp; vp = vp->next) { |
| 2215 | if ((vp->flags & mask) == on) { |
| 2216 | if (ep == stackstrend()) |
| 2217 | ep = growstackstr(); |
| 2218 | *ep++ = (char *) vp->text; |
| 2219 | } |
| 2220 | } |
| 2221 | } while (++vpp < vartab + VTABSIZE); |
| 2222 | if (ep == stackstrend()) |
| 2223 | ep = growstackstr(); |
| 2224 | if (end) |
| 2225 | *end = ep; |
| 2226 | *ep++ = NULL; |
| 2227 | return grabstackstr(ep); |
| 2228 | } |
| 2229 | |
| 2230 | |
| 2231 | /* ============ Path search helper |
| 2232 | * |
| 2233 | * The variable path (passed by reference) should be set to the start |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2234 | * of the path before the first call; path_advance will update |
| 2235 | * this value as it proceeds. Successive calls to path_advance will return |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2236 | * the possible path expansions in sequence. If an option (indicated by |
| 2237 | * a percent sign) appears in the path entry then the global variable |
| 2238 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 2239 | * NULL. |
| 2240 | */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2241 | static const char *pathopt; /* set by path_advance */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2242 | |
| 2243 | static char * |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2244 | path_advance(const char **path, const char *name) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2245 | { |
| 2246 | const char *p; |
| 2247 | char *q; |
| 2248 | const char *start; |
| 2249 | size_t len; |
| 2250 | |
| 2251 | if (*path == NULL) |
| 2252 | return NULL; |
| 2253 | start = *path; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2254 | for (p = start; *p && *p != ':' && *p != '%'; p++) |
| 2255 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2256 | len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ |
| 2257 | while (stackblocksize() < len) |
| 2258 | growstackblock(); |
| 2259 | q = stackblock(); |
| 2260 | if (p != start) { |
| 2261 | memcpy(q, start, p - start); |
| 2262 | q += p - start; |
| 2263 | *q++ = '/'; |
| 2264 | } |
| 2265 | strcpy(q, name); |
| 2266 | pathopt = NULL; |
| 2267 | if (*p == '%') { |
| 2268 | pathopt = ++p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2269 | while (*p && *p != ':') |
| 2270 | p++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2271 | } |
| 2272 | if (*p == ':') |
| 2273 | *path = p + 1; |
| 2274 | else |
| 2275 | *path = NULL; |
| 2276 | return stalloc(len); |
| 2277 | } |
| 2278 | |
| 2279 | |
| 2280 | /* ============ Prompt */ |
| 2281 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2282 | static smallint doprompt; /* if set, prompt the user */ |
| 2283 | static smallint needprompt; /* true if interactive and at start of line */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2284 | |
| 2285 | #if ENABLE_FEATURE_EDITING |
| 2286 | static line_input_t *line_input_state; |
| 2287 | static const char *cmdedit_prompt; |
| 2288 | static void |
| 2289 | putprompt(const char *s) |
| 2290 | { |
| 2291 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 2292 | free((char*)cmdedit_prompt); |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 2293 | cmdedit_prompt = ckstrdup(s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2294 | return; |
| 2295 | } |
| 2296 | cmdedit_prompt = s; |
| 2297 | } |
| 2298 | #else |
| 2299 | static void |
| 2300 | putprompt(const char *s) |
| 2301 | { |
| 2302 | out2str(s); |
| 2303 | } |
| 2304 | #endif |
| 2305 | |
| 2306 | #if ENABLE_ASH_EXPAND_PRMT |
| 2307 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
| 2308 | static const char *expandstr(const char *ps); |
| 2309 | #else |
| 2310 | #define expandstr(s) s |
| 2311 | #endif |
| 2312 | |
| 2313 | static void |
| 2314 | setprompt(int whichprompt) |
| 2315 | { |
| 2316 | const char *prompt; |
| 2317 | #if ENABLE_ASH_EXPAND_PRMT |
| 2318 | struct stackmark smark; |
| 2319 | #endif |
| 2320 | |
| 2321 | needprompt = 0; |
| 2322 | |
| 2323 | switch (whichprompt) { |
| 2324 | case 1: |
| 2325 | prompt = ps1val(); |
| 2326 | break; |
| 2327 | case 2: |
| 2328 | prompt = ps2val(); |
| 2329 | break; |
| 2330 | default: /* 0 */ |
| 2331 | prompt = nullstr; |
| 2332 | } |
| 2333 | #if ENABLE_ASH_EXPAND_PRMT |
| 2334 | setstackmark(&smark); |
| 2335 | stalloc(stackblocksize()); |
| 2336 | #endif |
| 2337 | putprompt(expandstr(prompt)); |
| 2338 | #if ENABLE_ASH_EXPAND_PRMT |
| 2339 | popstackmark(&smark); |
| 2340 | #endif |
| 2341 | } |
| 2342 | |
| 2343 | |
| 2344 | /* ============ The cd and pwd commands */ |
| 2345 | |
| 2346 | #define CD_PHYSICAL 1 |
| 2347 | #define CD_PRINT 2 |
| 2348 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2349 | static int |
| 2350 | cdopt(void) |
| 2351 | { |
| 2352 | int flags = 0; |
| 2353 | int i, j; |
| 2354 | |
| 2355 | j = 'L'; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 2356 | while ((i = nextopt("LP")) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2357 | if (i != j) { |
| 2358 | flags ^= CD_PHYSICAL; |
| 2359 | j = i; |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | return flags; |
| 2364 | } |
| 2365 | |
| 2366 | /* |
| 2367 | * Update curdir (the name of the current directory) in response to a |
| 2368 | * cd command. |
| 2369 | */ |
| 2370 | static const char * |
| 2371 | updatepwd(const char *dir) |
| 2372 | { |
| 2373 | char *new; |
| 2374 | char *p; |
| 2375 | char *cdcomppath; |
| 2376 | const char *lim; |
| 2377 | |
| 2378 | cdcomppath = ststrdup(dir); |
| 2379 | STARTSTACKSTR(new); |
| 2380 | if (*dir != '/') { |
| 2381 | if (curdir == nullstr) |
| 2382 | return 0; |
| 2383 | new = stack_putstr(curdir, new); |
| 2384 | } |
| 2385 | new = makestrspace(strlen(dir) + 2, new); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2386 | lim = (char *)stackblock() + 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2387 | if (*dir != '/') { |
| 2388 | if (new[-1] != '/') |
| 2389 | USTPUTC('/', new); |
| 2390 | if (new > lim && *lim == '/') |
| 2391 | lim++; |
| 2392 | } else { |
| 2393 | USTPUTC('/', new); |
| 2394 | cdcomppath++; |
| 2395 | if (dir[1] == '/' && dir[2] != '/') { |
| 2396 | USTPUTC('/', new); |
| 2397 | cdcomppath++; |
| 2398 | lim++; |
| 2399 | } |
| 2400 | } |
| 2401 | p = strtok(cdcomppath, "/"); |
| 2402 | while (p) { |
| 2403 | switch (*p) { |
| 2404 | case '.': |
| 2405 | if (p[1] == '.' && p[2] == '\0') { |
| 2406 | while (new > lim) { |
| 2407 | STUNPUTC(new); |
| 2408 | if (new[-1] == '/') |
| 2409 | break; |
| 2410 | } |
| 2411 | break; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 2412 | } |
| 2413 | if (p[1] == '\0') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2414 | break; |
| 2415 | /* fall through */ |
| 2416 | default: |
| 2417 | new = stack_putstr(p, new); |
| 2418 | USTPUTC('/', new); |
| 2419 | } |
| 2420 | p = strtok(0, "/"); |
| 2421 | } |
| 2422 | if (new > lim) |
| 2423 | STUNPUTC(new); |
| 2424 | *new = 0; |
| 2425 | return stackblock(); |
| 2426 | } |
| 2427 | |
| 2428 | /* |
| 2429 | * Find out what the current directory is. If we already know the current |
| 2430 | * directory, this routine returns immediately. |
| 2431 | */ |
| 2432 | static char * |
| 2433 | getpwd(void) |
| 2434 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2435 | char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2436 | return dir ? dir : nullstr; |
| 2437 | } |
| 2438 | |
| 2439 | static void |
| 2440 | setpwd(const char *val, int setold) |
| 2441 | { |
| 2442 | char *oldcur, *dir; |
| 2443 | |
| 2444 | oldcur = dir = curdir; |
| 2445 | |
| 2446 | if (setold) { |
| 2447 | setvar("OLDPWD", oldcur, VEXPORT); |
| 2448 | } |
| 2449 | INT_OFF; |
| 2450 | if (physdir != nullstr) { |
| 2451 | if (physdir != oldcur) |
| 2452 | free(physdir); |
| 2453 | physdir = nullstr; |
| 2454 | } |
| 2455 | if (oldcur == val || !val) { |
| 2456 | char *s = getpwd(); |
| 2457 | physdir = s; |
| 2458 | if (!val) |
| 2459 | dir = s; |
| 2460 | } else |
| 2461 | dir = ckstrdup(val); |
| 2462 | if (oldcur != dir && oldcur != nullstr) { |
| 2463 | free(oldcur); |
| 2464 | } |
| 2465 | curdir = dir; |
| 2466 | INT_ON; |
| 2467 | setvar("PWD", dir, VEXPORT); |
| 2468 | } |
| 2469 | |
| 2470 | static void hashcd(void); |
| 2471 | |
| 2472 | /* |
| 2473 | * Actually do the chdir. We also call hashcd to let the routines in exec.c |
| 2474 | * know that the current directory has changed. |
| 2475 | */ |
| 2476 | static int |
| 2477 | docd(const char *dest, int flags) |
| 2478 | { |
| 2479 | const char *dir = 0; |
| 2480 | int err; |
| 2481 | |
| 2482 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 2483 | |
| 2484 | INT_OFF; |
| 2485 | if (!(flags & CD_PHYSICAL)) { |
| 2486 | dir = updatepwd(dest); |
| 2487 | if (dir) |
| 2488 | dest = dir; |
| 2489 | } |
| 2490 | err = chdir(dest); |
| 2491 | if (err) |
| 2492 | goto out; |
| 2493 | setpwd(dir, 1); |
| 2494 | hashcd(); |
| 2495 | out: |
| 2496 | INT_ON; |
| 2497 | return err; |
| 2498 | } |
| 2499 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2500 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2501 | cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2502 | { |
| 2503 | const char *dest; |
| 2504 | const char *path; |
| 2505 | const char *p; |
| 2506 | char c; |
| 2507 | struct stat statb; |
| 2508 | int flags; |
| 2509 | |
| 2510 | flags = cdopt(); |
| 2511 | dest = *argptr; |
| 2512 | if (!dest) |
| 2513 | dest = bltinlookup(homestr); |
| 2514 | else if (LONE_DASH(dest)) { |
| 2515 | dest = bltinlookup("OLDPWD"); |
| 2516 | flags |= CD_PRINT; |
| 2517 | } |
| 2518 | if (!dest) |
| 2519 | dest = nullstr; |
| 2520 | if (*dest == '/') |
| 2521 | goto step7; |
| 2522 | if (*dest == '.') { |
| 2523 | c = dest[1]; |
| 2524 | dotdot: |
| 2525 | switch (c) { |
| 2526 | case '\0': |
| 2527 | case '/': |
| 2528 | goto step6; |
| 2529 | case '.': |
| 2530 | c = dest[2]; |
| 2531 | if (c != '.') |
| 2532 | goto dotdot; |
| 2533 | } |
| 2534 | } |
| 2535 | if (!*dest) |
| 2536 | dest = "."; |
| 2537 | path = bltinlookup("CDPATH"); |
| 2538 | if (!path) { |
| 2539 | step6: |
| 2540 | step7: |
| 2541 | p = dest; |
| 2542 | goto docd; |
| 2543 | } |
| 2544 | do { |
| 2545 | c = *path; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2546 | p = path_advance(&path, dest); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2547 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 2548 | if (c && c != ':') |
| 2549 | flags |= CD_PRINT; |
| 2550 | docd: |
| 2551 | if (!docd(p, flags)) |
| 2552 | goto out; |
| 2553 | break; |
| 2554 | } |
| 2555 | } while (path); |
| 2556 | ash_msg_and_raise_error("can't cd to %s", dest); |
| 2557 | /* NOTREACHED */ |
| 2558 | out: |
| 2559 | if (flags & CD_PRINT) |
| 2560 | out1fmt(snlfmt, curdir); |
| 2561 | return 0; |
| 2562 | } |
| 2563 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2564 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2565 | pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2566 | { |
| 2567 | int flags; |
| 2568 | const char *dir = curdir; |
| 2569 | |
| 2570 | flags = cdopt(); |
| 2571 | if (flags) { |
| 2572 | if (physdir == nullstr) |
| 2573 | setpwd(dir, 0); |
| 2574 | dir = physdir; |
| 2575 | } |
| 2576 | out1fmt(snlfmt, dir); |
| 2577 | return 0; |
| 2578 | } |
| 2579 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2580 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 2581 | /* ============ ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2582 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2583 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2584 | #define IBUFSIZ COMMON_BUFSIZE |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2585 | /* buffer for top level input file */ |
| 2586 | #define basebuf bb_common_bufsiz1 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2587 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2588 | /* Syntax classes */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2589 | #define CWORD 0 /* character is nothing special */ |
| 2590 | #define CNL 1 /* newline character */ |
| 2591 | #define CBACK 2 /* a backslash character */ |
| 2592 | #define CSQUOTE 3 /* single quote */ |
| 2593 | #define CDQUOTE 4 /* double quote */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2594 | #define CENDQUOTE 5 /* a terminating quote */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2595 | #define CBQUOTE 6 /* backwards single quote */ |
| 2596 | #define CVAR 7 /* a dollar sign */ |
| 2597 | #define CENDVAR 8 /* a '}' character */ |
| 2598 | #define CLP 9 /* a left paren in arithmetic */ |
| 2599 | #define CRP 10 /* a right paren in arithmetic */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2600 | #define CENDFILE 11 /* end of file */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2601 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 2602 | #define CSPCL 13 /* these terminate a word */ |
| 2603 | #define CIGN 14 /* character should be ignored */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2604 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2605 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2606 | #define SYNBASE 130 |
| 2607 | #define PEOF -130 |
| 2608 | #define PEOA -129 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2609 | #define PEOA_OR_PEOF PEOA |
| 2610 | #else |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2611 | #define SYNBASE 129 |
| 2612 | #define PEOF -129 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2613 | #define PEOA_OR_PEOF PEOF |
| 2614 | #endif |
| 2615 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2616 | /* number syntax index */ |
| 2617 | #define BASESYNTAX 0 /* not in quotes */ |
| 2618 | #define DQSYNTAX 1 /* in double quotes */ |
| 2619 | #define SQSYNTAX 2 /* in single quotes */ |
| 2620 | #define ARISYNTAX 3 /* in arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 2621 | #define PSSYNTAX 4 /* prompt */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2622 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2623 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2624 | #define USE_SIT_FUNCTION |
| 2625 | #endif |
| 2626 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2627 | #if ENABLE_SH_MATH_SUPPORT |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2628 | static const char S_I_T[][4] = { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2629 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2630 | { CSPCL, CIGN, CIGN, CIGN }, /* 0, PEOA */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2631 | #endif |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2632 | { CSPCL, CWORD, CWORD, CWORD }, /* 1, ' ' */ |
| 2633 | { CNL, CNL, CNL, CNL }, /* 2, \n */ |
| 2634 | { CWORD, CCTL, CCTL, CWORD }, /* 3, !*-/:=?[]~ */ |
| 2635 | { CDQUOTE, CENDQUOTE, CWORD, CWORD }, /* 4, '"' */ |
| 2636 | { CVAR, CVAR, CWORD, CVAR }, /* 5, $ */ |
| 2637 | { CSQUOTE, CWORD, CENDQUOTE, CWORD }, /* 6, "'" */ |
| 2638 | { CSPCL, CWORD, CWORD, CLP }, /* 7, ( */ |
| 2639 | { CSPCL, CWORD, CWORD, CRP }, /* 8, ) */ |
| 2640 | { CBACK, CBACK, CCTL, CBACK }, /* 9, \ */ |
| 2641 | { CBQUOTE, CBQUOTE, CWORD, CBQUOTE }, /* 10, ` */ |
| 2642 | { CENDVAR, CENDVAR, CWORD, CENDVAR }, /* 11, } */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2643 | #ifndef USE_SIT_FUNCTION |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2644 | { CENDFILE, CENDFILE, CENDFILE, CENDFILE }, /* 12, PEOF */ |
| 2645 | { CWORD, CWORD, CWORD, CWORD }, /* 13, 0-9A-Za-z */ |
| 2646 | { CCTL, CCTL, CCTL, CCTL } /* 14, CTLESC ... */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2647 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2648 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2649 | #else |
| 2650 | static const char S_I_T[][3] = { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2651 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2652 | { CSPCL, CIGN, CIGN }, /* 0, PEOA */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2653 | #endif |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2654 | { CSPCL, CWORD, CWORD }, /* 1, ' ' */ |
| 2655 | { CNL, CNL, CNL }, /* 2, \n */ |
| 2656 | { CWORD, CCTL, CCTL }, /* 3, !*-/:=?[]~ */ |
| 2657 | { CDQUOTE, CENDQUOTE, CWORD }, /* 4, '"' */ |
| 2658 | { CVAR, CVAR, CWORD }, /* 5, $ */ |
| 2659 | { CSQUOTE, CWORD, CENDQUOTE }, /* 6, "'" */ |
| 2660 | { CSPCL, CWORD, CWORD }, /* 7, ( */ |
| 2661 | { CSPCL, CWORD, CWORD }, /* 8, ) */ |
| 2662 | { CBACK, CBACK, CCTL }, /* 9, \ */ |
| 2663 | { CBQUOTE, CBQUOTE, CWORD }, /* 10, ` */ |
| 2664 | { CENDVAR, CENDVAR, CWORD }, /* 11, } */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2665 | #ifndef USE_SIT_FUNCTION |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2666 | { CENDFILE, CENDFILE, CENDFILE }, /* 12, PEOF */ |
| 2667 | { CWORD, CWORD, CWORD }, /* 13, 0-9A-Za-z */ |
| 2668 | { CCTL, CCTL, CCTL } /* 14, CTLESC ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2669 | #endif |
| 2670 | }; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2671 | #endif /* SH_MATH_SUPPORT */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2672 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2673 | #ifdef USE_SIT_FUNCTION |
| 2674 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2675 | static int |
| 2676 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2677 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2678 | static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-/:;<=>?[\\]`|}~"; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2679 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2680 | static const char syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2681 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
| 2682 | 7, 8, 3, 3, 3, 3, 1, 1, /* "()*-/:;<" */ |
| 2683 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 2684 | 11, 3 /* "}~" */ |
| 2685 | }; |
| 2686 | #else |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2687 | static const char syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2688 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
| 2689 | 6, 7, 2, 2, 2, 2, 0, 0, /* "()*-/:;<" */ |
| 2690 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 2691 | 10, 2 /* "}~" */ |
| 2692 | }; |
| 2693 | #endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2694 | const char *s; |
| 2695 | int indx; |
| 2696 | |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2697 | if (c == PEOF) { /* 2^8+2 */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2698 | return CENDFILE; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2699 | } |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2700 | #if ENABLE_ASH_ALIAS |
| 2701 | if (c == PEOA) { /* 2^8+1 */ |
| 2702 | indx = 0; |
| 2703 | } else |
| 2704 | #endif |
| 2705 | { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 2706 | if ((unsigned char)c >= CTLESC |
| 2707 | && (unsigned char)c <= CTLQUOTEMARK |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2708 | ) { |
| 2709 | return CCTL; |
| 2710 | } |
| 2711 | s = strchrnul(spec_symbls, c); |
| 2712 | if (*s == '\0') { |
| 2713 | return CWORD; |
| 2714 | } |
| 2715 | indx = syntax_index_table[s - spec_symbls]; |
| 2716 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2717 | return S_I_T[indx][syntax]; |
| 2718 | } |
| 2719 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2720 | #else /* !USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2721 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2722 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2723 | #define CSPCL_CIGN_CIGN_CIGN 0 |
| 2724 | #define CSPCL_CWORD_CWORD_CWORD 1 |
| 2725 | #define CNL_CNL_CNL_CNL 2 |
| 2726 | #define CWORD_CCTL_CCTL_CWORD 3 |
| 2727 | #define CDQUOTE_CENDQUOTE_CWORD_CWORD 4 |
| 2728 | #define CVAR_CVAR_CWORD_CVAR 5 |
| 2729 | #define CSQUOTE_CWORD_CENDQUOTE_CWORD 6 |
| 2730 | #define CSPCL_CWORD_CWORD_CLP 7 |
| 2731 | #define CSPCL_CWORD_CWORD_CRP 8 |
| 2732 | #define CBACK_CBACK_CCTL_CBACK 9 |
| 2733 | #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE 10 |
| 2734 | #define CENDVAR_CENDVAR_CWORD_CENDVAR 11 |
| 2735 | #define CENDFILE_CENDFILE_CENDFILE_CENDFILE 12 |
| 2736 | #define CWORD_CWORD_CWORD_CWORD 13 |
| 2737 | #define CCTL_CCTL_CCTL_CCTL 14 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2738 | #else |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2739 | #define CSPCL_CWORD_CWORD_CWORD 0 |
| 2740 | #define CNL_CNL_CNL_CNL 1 |
| 2741 | #define CWORD_CCTL_CCTL_CWORD 2 |
| 2742 | #define CDQUOTE_CENDQUOTE_CWORD_CWORD 3 |
| 2743 | #define CVAR_CVAR_CWORD_CVAR 4 |
| 2744 | #define CSQUOTE_CWORD_CENDQUOTE_CWORD 5 |
| 2745 | #define CSPCL_CWORD_CWORD_CLP 6 |
| 2746 | #define CSPCL_CWORD_CWORD_CRP 7 |
| 2747 | #define CBACK_CBACK_CCTL_CBACK 8 |
| 2748 | #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE 9 |
| 2749 | #define CENDVAR_CENDVAR_CWORD_CENDVAR 10 |
| 2750 | #define CENDFILE_CENDFILE_CENDFILE_CENDFILE 11 |
| 2751 | #define CWORD_CWORD_CWORD_CWORD 12 |
| 2752 | #define CCTL_CCTL_CCTL_CCTL 13 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2753 | #endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2754 | |
| 2755 | static const char syntax_index_table[258] = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2756 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2757 | /* 0 PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2758 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2759 | /* 1 PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 2760 | #endif |
| 2761 | /* 2 -128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 2762 | /* 3 -127 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 2763 | /* 4 -126 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2764 | /* 5 -125 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2765 | /* 6 -124 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 2766 | /* 7 -123 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 2767 | /* 8 -122 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 2768 | /* 9 -121 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 2769 | /* 10 -120 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2770 | /* 11 -119 */ CWORD_CWORD_CWORD_CWORD, |
| 2771 | /* 12 -118 */ CWORD_CWORD_CWORD_CWORD, |
| 2772 | /* 13 -117 */ CWORD_CWORD_CWORD_CWORD, |
| 2773 | /* 14 -116 */ CWORD_CWORD_CWORD_CWORD, |
| 2774 | /* 15 -115 */ CWORD_CWORD_CWORD_CWORD, |
| 2775 | /* 16 -114 */ CWORD_CWORD_CWORD_CWORD, |
| 2776 | /* 17 -113 */ CWORD_CWORD_CWORD_CWORD, |
| 2777 | /* 18 -112 */ CWORD_CWORD_CWORD_CWORD, |
| 2778 | /* 19 -111 */ CWORD_CWORD_CWORD_CWORD, |
| 2779 | /* 20 -110 */ CWORD_CWORD_CWORD_CWORD, |
| 2780 | /* 21 -109 */ CWORD_CWORD_CWORD_CWORD, |
| 2781 | /* 22 -108 */ CWORD_CWORD_CWORD_CWORD, |
| 2782 | /* 23 -107 */ CWORD_CWORD_CWORD_CWORD, |
| 2783 | /* 24 -106 */ CWORD_CWORD_CWORD_CWORD, |
| 2784 | /* 25 -105 */ CWORD_CWORD_CWORD_CWORD, |
| 2785 | /* 26 -104 */ CWORD_CWORD_CWORD_CWORD, |
| 2786 | /* 27 -103 */ CWORD_CWORD_CWORD_CWORD, |
| 2787 | /* 28 -102 */ CWORD_CWORD_CWORD_CWORD, |
| 2788 | /* 29 -101 */ CWORD_CWORD_CWORD_CWORD, |
| 2789 | /* 30 -100 */ CWORD_CWORD_CWORD_CWORD, |
| 2790 | /* 31 -99 */ CWORD_CWORD_CWORD_CWORD, |
| 2791 | /* 32 -98 */ CWORD_CWORD_CWORD_CWORD, |
| 2792 | /* 33 -97 */ CWORD_CWORD_CWORD_CWORD, |
| 2793 | /* 34 -96 */ CWORD_CWORD_CWORD_CWORD, |
| 2794 | /* 35 -95 */ CWORD_CWORD_CWORD_CWORD, |
| 2795 | /* 36 -94 */ CWORD_CWORD_CWORD_CWORD, |
| 2796 | /* 37 -93 */ CWORD_CWORD_CWORD_CWORD, |
| 2797 | /* 38 -92 */ CWORD_CWORD_CWORD_CWORD, |
| 2798 | /* 39 -91 */ CWORD_CWORD_CWORD_CWORD, |
| 2799 | /* 40 -90 */ CWORD_CWORD_CWORD_CWORD, |
| 2800 | /* 41 -89 */ CWORD_CWORD_CWORD_CWORD, |
| 2801 | /* 42 -88 */ CWORD_CWORD_CWORD_CWORD, |
| 2802 | /* 43 -87 */ CWORD_CWORD_CWORD_CWORD, |
| 2803 | /* 44 -86 */ CWORD_CWORD_CWORD_CWORD, |
| 2804 | /* 45 -85 */ CWORD_CWORD_CWORD_CWORD, |
| 2805 | /* 46 -84 */ CWORD_CWORD_CWORD_CWORD, |
| 2806 | /* 47 -83 */ CWORD_CWORD_CWORD_CWORD, |
| 2807 | /* 48 -82 */ CWORD_CWORD_CWORD_CWORD, |
| 2808 | /* 49 -81 */ CWORD_CWORD_CWORD_CWORD, |
| 2809 | /* 50 -80 */ CWORD_CWORD_CWORD_CWORD, |
| 2810 | /* 51 -79 */ CWORD_CWORD_CWORD_CWORD, |
| 2811 | /* 52 -78 */ CWORD_CWORD_CWORD_CWORD, |
| 2812 | /* 53 -77 */ CWORD_CWORD_CWORD_CWORD, |
| 2813 | /* 54 -76 */ CWORD_CWORD_CWORD_CWORD, |
| 2814 | /* 55 -75 */ CWORD_CWORD_CWORD_CWORD, |
| 2815 | /* 56 -74 */ CWORD_CWORD_CWORD_CWORD, |
| 2816 | /* 57 -73 */ CWORD_CWORD_CWORD_CWORD, |
| 2817 | /* 58 -72 */ CWORD_CWORD_CWORD_CWORD, |
| 2818 | /* 59 -71 */ CWORD_CWORD_CWORD_CWORD, |
| 2819 | /* 60 -70 */ CWORD_CWORD_CWORD_CWORD, |
| 2820 | /* 61 -69 */ CWORD_CWORD_CWORD_CWORD, |
| 2821 | /* 62 -68 */ CWORD_CWORD_CWORD_CWORD, |
| 2822 | /* 63 -67 */ CWORD_CWORD_CWORD_CWORD, |
| 2823 | /* 64 -66 */ CWORD_CWORD_CWORD_CWORD, |
| 2824 | /* 65 -65 */ CWORD_CWORD_CWORD_CWORD, |
| 2825 | /* 66 -64 */ CWORD_CWORD_CWORD_CWORD, |
| 2826 | /* 67 -63 */ CWORD_CWORD_CWORD_CWORD, |
| 2827 | /* 68 -62 */ CWORD_CWORD_CWORD_CWORD, |
| 2828 | /* 69 -61 */ CWORD_CWORD_CWORD_CWORD, |
| 2829 | /* 70 -60 */ CWORD_CWORD_CWORD_CWORD, |
| 2830 | /* 71 -59 */ CWORD_CWORD_CWORD_CWORD, |
| 2831 | /* 72 -58 */ CWORD_CWORD_CWORD_CWORD, |
| 2832 | /* 73 -57 */ CWORD_CWORD_CWORD_CWORD, |
| 2833 | /* 74 -56 */ CWORD_CWORD_CWORD_CWORD, |
| 2834 | /* 75 -55 */ CWORD_CWORD_CWORD_CWORD, |
| 2835 | /* 76 -54 */ CWORD_CWORD_CWORD_CWORD, |
| 2836 | /* 77 -53 */ CWORD_CWORD_CWORD_CWORD, |
| 2837 | /* 78 -52 */ CWORD_CWORD_CWORD_CWORD, |
| 2838 | /* 79 -51 */ CWORD_CWORD_CWORD_CWORD, |
| 2839 | /* 80 -50 */ CWORD_CWORD_CWORD_CWORD, |
| 2840 | /* 81 -49 */ CWORD_CWORD_CWORD_CWORD, |
| 2841 | /* 82 -48 */ CWORD_CWORD_CWORD_CWORD, |
| 2842 | /* 83 -47 */ CWORD_CWORD_CWORD_CWORD, |
| 2843 | /* 84 -46 */ CWORD_CWORD_CWORD_CWORD, |
| 2844 | /* 85 -45 */ CWORD_CWORD_CWORD_CWORD, |
| 2845 | /* 86 -44 */ CWORD_CWORD_CWORD_CWORD, |
| 2846 | /* 87 -43 */ CWORD_CWORD_CWORD_CWORD, |
| 2847 | /* 88 -42 */ CWORD_CWORD_CWORD_CWORD, |
| 2848 | /* 89 -41 */ CWORD_CWORD_CWORD_CWORD, |
| 2849 | /* 90 -40 */ CWORD_CWORD_CWORD_CWORD, |
| 2850 | /* 91 -39 */ CWORD_CWORD_CWORD_CWORD, |
| 2851 | /* 92 -38 */ CWORD_CWORD_CWORD_CWORD, |
| 2852 | /* 93 -37 */ CWORD_CWORD_CWORD_CWORD, |
| 2853 | /* 94 -36 */ CWORD_CWORD_CWORD_CWORD, |
| 2854 | /* 95 -35 */ CWORD_CWORD_CWORD_CWORD, |
| 2855 | /* 96 -34 */ CWORD_CWORD_CWORD_CWORD, |
| 2856 | /* 97 -33 */ CWORD_CWORD_CWORD_CWORD, |
| 2857 | /* 98 -32 */ CWORD_CWORD_CWORD_CWORD, |
| 2858 | /* 99 -31 */ CWORD_CWORD_CWORD_CWORD, |
| 2859 | /* 100 -30 */ CWORD_CWORD_CWORD_CWORD, |
| 2860 | /* 101 -29 */ CWORD_CWORD_CWORD_CWORD, |
| 2861 | /* 102 -28 */ CWORD_CWORD_CWORD_CWORD, |
| 2862 | /* 103 -27 */ CWORD_CWORD_CWORD_CWORD, |
| 2863 | /* 104 -26 */ CWORD_CWORD_CWORD_CWORD, |
| 2864 | /* 105 -25 */ CWORD_CWORD_CWORD_CWORD, |
| 2865 | /* 106 -24 */ CWORD_CWORD_CWORD_CWORD, |
| 2866 | /* 107 -23 */ CWORD_CWORD_CWORD_CWORD, |
| 2867 | /* 108 -22 */ CWORD_CWORD_CWORD_CWORD, |
| 2868 | /* 109 -21 */ CWORD_CWORD_CWORD_CWORD, |
| 2869 | /* 110 -20 */ CWORD_CWORD_CWORD_CWORD, |
| 2870 | /* 111 -19 */ CWORD_CWORD_CWORD_CWORD, |
| 2871 | /* 112 -18 */ CWORD_CWORD_CWORD_CWORD, |
| 2872 | /* 113 -17 */ CWORD_CWORD_CWORD_CWORD, |
| 2873 | /* 114 -16 */ CWORD_CWORD_CWORD_CWORD, |
| 2874 | /* 115 -15 */ CWORD_CWORD_CWORD_CWORD, |
| 2875 | /* 116 -14 */ CWORD_CWORD_CWORD_CWORD, |
| 2876 | /* 117 -13 */ CWORD_CWORD_CWORD_CWORD, |
| 2877 | /* 118 -12 */ CWORD_CWORD_CWORD_CWORD, |
| 2878 | /* 119 -11 */ CWORD_CWORD_CWORD_CWORD, |
| 2879 | /* 120 -10 */ CWORD_CWORD_CWORD_CWORD, |
| 2880 | /* 121 -9 */ CWORD_CWORD_CWORD_CWORD, |
| 2881 | /* 122 -8 */ CWORD_CWORD_CWORD_CWORD, |
| 2882 | /* 123 -7 */ CWORD_CWORD_CWORD_CWORD, |
| 2883 | /* 124 -6 */ CWORD_CWORD_CWORD_CWORD, |
| 2884 | /* 125 -5 */ CWORD_CWORD_CWORD_CWORD, |
| 2885 | /* 126 -4 */ CWORD_CWORD_CWORD_CWORD, |
| 2886 | /* 127 -3 */ CWORD_CWORD_CWORD_CWORD, |
| 2887 | /* 128 -2 */ CWORD_CWORD_CWORD_CWORD, |
| 2888 | /* 129 -1 */ CWORD_CWORD_CWORD_CWORD, |
| 2889 | /* 130 0 */ CWORD_CWORD_CWORD_CWORD, |
| 2890 | /* 131 1 */ CWORD_CWORD_CWORD_CWORD, |
| 2891 | /* 132 2 */ CWORD_CWORD_CWORD_CWORD, |
| 2892 | /* 133 3 */ CWORD_CWORD_CWORD_CWORD, |
| 2893 | /* 134 4 */ CWORD_CWORD_CWORD_CWORD, |
| 2894 | /* 135 5 */ CWORD_CWORD_CWORD_CWORD, |
| 2895 | /* 136 6 */ CWORD_CWORD_CWORD_CWORD, |
| 2896 | /* 137 7 */ CWORD_CWORD_CWORD_CWORD, |
| 2897 | /* 138 8 */ CWORD_CWORD_CWORD_CWORD, |
| 2898 | /* 139 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 2899 | /* 140 10 "\n" */ CNL_CNL_CNL_CNL, |
| 2900 | /* 141 11 */ CWORD_CWORD_CWORD_CWORD, |
| 2901 | /* 142 12 */ CWORD_CWORD_CWORD_CWORD, |
| 2902 | /* 143 13 */ CWORD_CWORD_CWORD_CWORD, |
| 2903 | /* 144 14 */ CWORD_CWORD_CWORD_CWORD, |
| 2904 | /* 145 15 */ CWORD_CWORD_CWORD_CWORD, |
| 2905 | /* 146 16 */ CWORD_CWORD_CWORD_CWORD, |
| 2906 | /* 147 17 */ CWORD_CWORD_CWORD_CWORD, |
| 2907 | /* 148 18 */ CWORD_CWORD_CWORD_CWORD, |
| 2908 | /* 149 19 */ CWORD_CWORD_CWORD_CWORD, |
| 2909 | /* 150 20 */ CWORD_CWORD_CWORD_CWORD, |
| 2910 | /* 151 21 */ CWORD_CWORD_CWORD_CWORD, |
| 2911 | /* 152 22 */ CWORD_CWORD_CWORD_CWORD, |
| 2912 | /* 153 23 */ CWORD_CWORD_CWORD_CWORD, |
| 2913 | /* 154 24 */ CWORD_CWORD_CWORD_CWORD, |
| 2914 | /* 155 25 */ CWORD_CWORD_CWORD_CWORD, |
| 2915 | /* 156 26 */ CWORD_CWORD_CWORD_CWORD, |
| 2916 | /* 157 27 */ CWORD_CWORD_CWORD_CWORD, |
| 2917 | /* 158 28 */ CWORD_CWORD_CWORD_CWORD, |
| 2918 | /* 159 29 */ CWORD_CWORD_CWORD_CWORD, |
| 2919 | /* 160 30 */ CWORD_CWORD_CWORD_CWORD, |
| 2920 | /* 161 31 */ CWORD_CWORD_CWORD_CWORD, |
| 2921 | /* 162 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 2922 | /* 163 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2923 | /* 164 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2924 | /* 165 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 2925 | /* 166 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 2926 | /* 167 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 2927 | /* 168 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2928 | /* 169 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2929 | /* 170 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 2930 | /* 171 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 2931 | /* 172 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 2932 | /* 173 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 2933 | /* 174 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 2934 | /* 175 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 2935 | /* 176 46 "." */ CWORD_CWORD_CWORD_CWORD, |
| 2936 | /* 177 47 "/" */ CWORD_CCTL_CCTL_CWORD, |
| 2937 | /* 178 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 2938 | /* 179 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 2939 | /* 180 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 2940 | /* 181 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 2941 | /* 182 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 2942 | /* 183 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 2943 | /* 184 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 2944 | /* 185 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 2945 | /* 186 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 2946 | /* 187 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 2947 | /* 188 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 2948 | /* 189 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 2949 | /* 190 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 2950 | /* 191 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 2951 | /* 192 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 2952 | /* 193 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 2953 | /* 194 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 2954 | /* 195 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 2955 | /* 196 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 2956 | /* 197 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 2957 | /* 198 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 2958 | /* 199 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 2959 | /* 200 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 2960 | /* 201 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 2961 | /* 202 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 2962 | /* 203 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 2963 | /* 204 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 2964 | /* 205 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 2965 | /* 206 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 2966 | /* 207 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 2967 | /* 208 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 2968 | /* 209 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 2969 | /* 210 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 2970 | /* 211 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 2971 | /* 212 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 2972 | /* 213 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 2973 | /* 214 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 2974 | /* 215 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 2975 | /* 216 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 2976 | /* 217 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 2977 | /* 218 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 2978 | /* 219 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 2979 | /* 220 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 2980 | /* 221 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 2981 | /* 222 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 2982 | /* 223 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 2983 | /* 224 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 2984 | /* 225 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 2985 | /* 226 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 2986 | /* 227 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 2987 | /* 228 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 2988 | /* 229 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 2989 | /* 230 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 2990 | /* 231 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 2991 | /* 232 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 2992 | /* 233 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 2993 | /* 234 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 2994 | /* 235 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 2995 | /* 236 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 2996 | /* 237 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 2997 | /* 238 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 2998 | /* 239 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 2999 | /* 240 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 3000 | /* 241 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 3001 | /* 242 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 3002 | /* 243 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 3003 | /* 244 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 3004 | /* 245 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 3005 | /* 246 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 3006 | /* 247 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 3007 | /* 248 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 3008 | /* 249 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 3009 | /* 250 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 3010 | /* 251 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 3011 | /* 252 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 3012 | /* 253 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 3013 | /* 254 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 3014 | /* 255 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 3015 | /* 256 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 3016 | /* 257 127 */ CWORD_CWORD_CWORD_CWORD, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3017 | }; |
| 3018 | |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3019 | #define SIT(c, syntax) (S_I_T[(int)syntax_index_table[(int)(c) + SYNBASE]][syntax]) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 3020 | |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 3021 | #endif /* USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3022 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3023 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3024 | /* ============ Alias handling */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3025 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3026 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3027 | |
| 3028 | #define ALIASINUSE 1 |
| 3029 | #define ALIASDEAD 2 |
| 3030 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3031 | struct alias { |
| 3032 | struct alias *next; |
| 3033 | char *name; |
| 3034 | char *val; |
| 3035 | int flag; |
| 3036 | }; |
| 3037 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 3038 | |
| 3039 | static struct alias **atab; // [ATABSIZE]; |
| 3040 | #define INIT_G_alias() do { \ |
| 3041 | atab = xzalloc(ATABSIZE * sizeof(atab[0])); \ |
| 3042 | } while (0) |
| 3043 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3044 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3045 | static struct alias ** |
| 3046 | __lookupalias(const char *name) { |
| 3047 | unsigned int hashval; |
| 3048 | struct alias **app; |
| 3049 | const char *p; |
| 3050 | unsigned int ch; |
| 3051 | |
| 3052 | p = name; |
| 3053 | |
| 3054 | ch = (unsigned char)*p; |
| 3055 | hashval = ch << 4; |
| 3056 | while (ch) { |
| 3057 | hashval += ch; |
| 3058 | ch = (unsigned char)*++p; |
| 3059 | } |
| 3060 | app = &atab[hashval % ATABSIZE]; |
| 3061 | |
| 3062 | for (; *app; app = &(*app)->next) { |
| 3063 | if (strcmp(name, (*app)->name) == 0) { |
| 3064 | break; |
| 3065 | } |
| 3066 | } |
| 3067 | |
| 3068 | return app; |
| 3069 | } |
| 3070 | |
| 3071 | static struct alias * |
| 3072 | lookupalias(const char *name, int check) |
| 3073 | { |
| 3074 | struct alias *ap = *__lookupalias(name); |
| 3075 | |
| 3076 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 3077 | return NULL; |
| 3078 | return ap; |
| 3079 | } |
| 3080 | |
| 3081 | static struct alias * |
| 3082 | freealias(struct alias *ap) |
| 3083 | { |
| 3084 | struct alias *next; |
| 3085 | |
| 3086 | if (ap->flag & ALIASINUSE) { |
| 3087 | ap->flag |= ALIASDEAD; |
| 3088 | return ap; |
| 3089 | } |
| 3090 | |
| 3091 | next = ap->next; |
| 3092 | free(ap->name); |
| 3093 | free(ap->val); |
| 3094 | free(ap); |
| 3095 | return next; |
| 3096 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3097 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3098 | static void |
| 3099 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3100 | { |
| 3101 | struct alias *ap, **app; |
| 3102 | |
| 3103 | app = __lookupalias(name); |
| 3104 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3105 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3106 | if (ap) { |
| 3107 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3108 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3109 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3110 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3111 | ap->flag &= ~ALIASDEAD; |
| 3112 | } else { |
| 3113 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3114 | ap = ckzalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3115 | ap->name = ckstrdup(name); |
| 3116 | ap->val = ckstrdup(val); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3117 | /*ap->flag = 0; - ckzalloc did it */ |
| 3118 | /*ap->next = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3119 | *app = ap; |
| 3120 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3121 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3124 | static int |
| 3125 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3126 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3127 | struct alias **app; |
| 3128 | |
| 3129 | app = __lookupalias(name); |
| 3130 | |
| 3131 | if (*app) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3132 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3133 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3134 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3135 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3138 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3141 | static void |
| 3142 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3143 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3144 | struct alias *ap, **app; |
| 3145 | int i; |
| 3146 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3147 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3148 | for (i = 0; i < ATABSIZE; i++) { |
| 3149 | app = &atab[i]; |
| 3150 | for (ap = *app; ap; ap = *app) { |
| 3151 | *app = freealias(*app); |
| 3152 | if (ap == *app) { |
| 3153 | app = &ap->next; |
| 3154 | } |
| 3155 | } |
| 3156 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3157 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3160 | static void |
| 3161 | printalias(const struct alias *ap) |
| 3162 | { |
| 3163 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3164 | } |
| 3165 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3166 | /* |
| 3167 | * TODO - sort output |
| 3168 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3169 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3170 | aliascmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3171 | { |
| 3172 | char *n, *v; |
| 3173 | int ret = 0; |
| 3174 | struct alias *ap; |
| 3175 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3176 | if (!argv[1]) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3177 | int i; |
| 3178 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3179 | for (i = 0; i < ATABSIZE; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3180 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3181 | printalias(ap); |
| 3182 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3183 | } |
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 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3187 | v = strchr(n+1, '='); |
| 3188 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3189 | ap = *__lookupalias(n); |
| 3190 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3191 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3192 | ret = 1; |
| 3193 | } else |
| 3194 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3195 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3196 | *v++ = '\0'; |
| 3197 | setalias(n, v); |
| 3198 | } |
| 3199 | } |
| 3200 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3201 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3202 | } |
| 3203 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3204 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3205 | unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3206 | { |
| 3207 | int i; |
| 3208 | |
| 3209 | while ((i = nextopt("a")) != '\0') { |
| 3210 | if (i == 'a') { |
| 3211 | rmaliases(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3212 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3213 | } |
| 3214 | } |
| 3215 | for (i = 0; *argptr; argptr++) { |
| 3216 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3217 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3218 | i = 1; |
| 3219 | } |
| 3220 | } |
| 3221 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3222 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3223 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3224 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3225 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3226 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3227 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3228 | /* ============ jobs.c */ |
| 3229 | |
| 3230 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
| 3231 | #define FORK_FG 0 |
| 3232 | #define FORK_BG 1 |
| 3233 | #define FORK_NOJOB 2 |
| 3234 | |
| 3235 | /* mode flags for showjob(s) */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3236 | #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */ |
| 3237 | #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */ |
| 3238 | #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3239 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3240 | /* |
| 3241 | * A job structure contains information about a job. A job is either a |
| 3242 | * single process or a set of processes contained in a pipeline. In the |
| 3243 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 3244 | * array of pids. |
| 3245 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3246 | struct procstat { |
| 3247 | pid_t pid; /* process id */ |
| 3248 | int status; /* last process status from wait() */ |
| 3249 | char *cmd; /* text of command being run */ |
| 3250 | }; |
| 3251 | |
| 3252 | struct job { |
| 3253 | struct procstat ps0; /* status of process */ |
| 3254 | struct procstat *ps; /* status or processes when more than one */ |
| 3255 | #if JOBS |
| 3256 | int stopstatus; /* status of a stopped job */ |
| 3257 | #endif |
| 3258 | uint32_t |
| 3259 | nprocs: 16, /* number of processes */ |
| 3260 | state: 8, |
| 3261 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3262 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3263 | #define JOBDONE 2 /* all procs are completed */ |
| 3264 | #if JOBS |
| 3265 | sigint: 1, /* job was killed by SIGINT */ |
| 3266 | jobctl: 1, /* job running under job control */ |
| 3267 | #endif |
| 3268 | waited: 1, /* true if this entry has been waited for */ |
| 3269 | used: 1, /* true if this entry is in used */ |
| 3270 | changed: 1; /* true if status has changed */ |
| 3271 | struct job *prev_job; /* previous job */ |
| 3272 | }; |
| 3273 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3274 | static struct job *makejob(/*union node *,*/ int); |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3275 | #if !JOBS |
| 3276 | #define forkshell(job, node, mode) forkshell(job, mode) |
| 3277 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3278 | static int forkshell(struct job *, union node *, int); |
| 3279 | static int waitforjob(struct job *); |
| 3280 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3281 | #if !JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3282 | enum { doing_jobctl = 0 }; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3283 | #define setjobctl(on) do {} while (0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3284 | #else |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3285 | static smallint doing_jobctl; //references:8 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3286 | static void setjobctl(int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3287 | #endif |
| 3288 | |
| 3289 | /* |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3290 | * Ignore a signal. |
| 3291 | */ |
| 3292 | static void |
| 3293 | ignoresig(int signo) |
| 3294 | { |
| 3295 | /* Avoid unnecessary system calls. Is it already SIG_IGNed? */ |
| 3296 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 3297 | /* No, need to do it */ |
| 3298 | signal(signo, SIG_IGN); |
| 3299 | } |
| 3300 | sigmode[signo - 1] = S_HARD_IGN; |
| 3301 | } |
| 3302 | |
| 3303 | /* |
| 3304 | * Signal handler. Only one usage site - in setsignal() |
| 3305 | */ |
| 3306 | static void |
| 3307 | onsig(int signo) |
| 3308 | { |
| 3309 | gotsig[signo - 1] = 1; |
| 3310 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3311 | if (signo == SIGINT && !trap[SIGINT]) { |
| 3312 | if (!suppress_int) { |
| 3313 | pending_sig = 0; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3314 | raise_interrupt(); /* does not return */ |
| 3315 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3316 | pending_int = 1; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3317 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3318 | pending_sig = signo; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | /* |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3323 | * Set the signal handler for the specified signal. The routine figures |
| 3324 | * out what it should be set to. |
| 3325 | */ |
| 3326 | static void |
| 3327 | setsignal(int signo) |
| 3328 | { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3329 | char *t; |
| 3330 | char cur_act, new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3331 | struct sigaction act; |
| 3332 | |
| 3333 | t = trap[signo]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3334 | new_act = S_DFL; |
| 3335 | if (t != NULL) { /* trap for this sig is set */ |
| 3336 | new_act = S_CATCH; |
| 3337 | if (t[0] == '\0') /* trap is "": ignore this sig */ |
| 3338 | new_act = S_IGN; |
| 3339 | } |
| 3340 | |
| 3341 | if (rootshell && new_act == S_DFL) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3342 | switch (signo) { |
| 3343 | case SIGINT: |
| 3344 | if (iflag || minusc || sflag == 0) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3345 | new_act = S_CATCH; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3346 | break; |
| 3347 | case SIGQUIT: |
| 3348 | #if DEBUG |
| 3349 | if (debug) |
| 3350 | break; |
| 3351 | #endif |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3352 | /* man bash: |
| 3353 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 3354 | * commands run by bash have signal handlers |
| 3355 | * set to the values inherited by the shell |
| 3356 | * from its parent". */ |
| 3357 | new_act = S_IGN; |
| 3358 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3359 | case SIGTERM: |
| 3360 | if (iflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3361 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3362 | break; |
| 3363 | #if JOBS |
| 3364 | case SIGTSTP: |
| 3365 | case SIGTTOU: |
| 3366 | if (mflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3367 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3368 | break; |
| 3369 | #endif |
| 3370 | } |
| 3371 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3372 | //TODO: if !rootshell, we reset SIGQUIT to DFL, |
| 3373 | //whereas we have to restore it to what shell got on entry |
| 3374 | //from the parent. See comment above |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3375 | |
| 3376 | t = &sigmode[signo - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3377 | cur_act = *t; |
| 3378 | if (cur_act == 0) { |
| 3379 | /* current setting is not yet known */ |
| 3380 | if (sigaction(signo, NULL, &act)) { |
| 3381 | /* pretend it worked; maybe we should give a warning, |
| 3382 | * but other shells don't. We don't alter sigmode, |
| 3383 | * so we retry every time. |
| 3384 | * btw, in Linux it never fails. --vda */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3385 | return; |
| 3386 | } |
| 3387 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3388 | cur_act = S_HARD_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3389 | if (mflag |
| 3390 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
| 3391 | ) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3392 | cur_act = S_IGN; /* don't hard ignore these */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3393 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3394 | } |
| 3395 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3396 | if (cur_act == S_HARD_IGN || cur_act == new_act) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3397 | return; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3398 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3399 | act.sa_handler = SIG_DFL; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3400 | switch (new_act) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3401 | case S_CATCH: |
| 3402 | act.sa_handler = onsig; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3403 | act.sa_flags = 0; /* matters only if !DFL and !IGN */ |
| 3404 | sigfillset(&act.sa_mask); /* ditto */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3405 | break; |
| 3406 | case S_IGN: |
| 3407 | act.sa_handler = SIG_IGN; |
| 3408 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3409 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3410 | sigaction_set(signo, &act); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3411 | |
| 3412 | *t = new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | /* mode flags for set_curjob */ |
| 3416 | #define CUR_DELETE 2 |
| 3417 | #define CUR_RUNNING 1 |
| 3418 | #define CUR_STOPPED 0 |
| 3419 | |
| 3420 | /* mode flags for dowait */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3421 | #define DOWAIT_NONBLOCK WNOHANG |
| 3422 | #define DOWAIT_BLOCK 0 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3423 | |
| 3424 | #if JOBS |
| 3425 | /* pgrp of shell on invocation */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3426 | static int initialpgrp; //references:2 |
| 3427 | static int ttyfd = -1; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3428 | #endif |
| 3429 | /* array of jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3430 | static struct job *jobtab; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3431 | /* size of array */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3432 | static unsigned njobs; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3433 | /* current job */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3434 | static struct job *curjob; //lots |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3435 | /* number of presumed living untracked jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3436 | static int jobless; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3437 | |
| 3438 | static void |
| 3439 | set_curjob(struct job *jp, unsigned mode) |
| 3440 | { |
| 3441 | struct job *jp1; |
| 3442 | struct job **jpp, **curp; |
| 3443 | |
| 3444 | /* first remove from list */ |
| 3445 | jpp = curp = &curjob; |
| 3446 | do { |
| 3447 | jp1 = *jpp; |
| 3448 | if (jp1 == jp) |
| 3449 | break; |
| 3450 | jpp = &jp1->prev_job; |
| 3451 | } while (1); |
| 3452 | *jpp = jp1->prev_job; |
| 3453 | |
| 3454 | /* Then re-insert in correct position */ |
| 3455 | jpp = curp; |
| 3456 | switch (mode) { |
| 3457 | default: |
| 3458 | #if DEBUG |
| 3459 | abort(); |
| 3460 | #endif |
| 3461 | case CUR_DELETE: |
| 3462 | /* job being deleted */ |
| 3463 | break; |
| 3464 | case CUR_RUNNING: |
| 3465 | /* newly created job or backgrounded job, |
| 3466 | put after all stopped jobs. */ |
| 3467 | do { |
| 3468 | jp1 = *jpp; |
| 3469 | #if JOBS |
| 3470 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3471 | #endif |
| 3472 | break; |
| 3473 | jpp = &jp1->prev_job; |
| 3474 | } while (1); |
| 3475 | /* FALLTHROUGH */ |
| 3476 | #if JOBS |
| 3477 | case CUR_STOPPED: |
| 3478 | #endif |
| 3479 | /* newly stopped job - becomes curjob */ |
| 3480 | jp->prev_job = *jpp; |
| 3481 | *jpp = jp; |
| 3482 | break; |
| 3483 | } |
| 3484 | } |
| 3485 | |
| 3486 | #if JOBS || DEBUG |
| 3487 | static int |
| 3488 | jobno(const struct job *jp) |
| 3489 | { |
| 3490 | return jp - jobtab + 1; |
| 3491 | } |
| 3492 | #endif |
| 3493 | |
| 3494 | /* |
| 3495 | * Convert a job name to a job structure. |
| 3496 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3497 | #if !JOBS |
| 3498 | #define getjob(name, getctl) getjob(name) |
| 3499 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3500 | static struct job * |
| 3501 | getjob(const char *name, int getctl) |
| 3502 | { |
| 3503 | struct job *jp; |
| 3504 | struct job *found; |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3505 | const char *err_msg = "%s: no such job"; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3506 | unsigned num; |
| 3507 | int c; |
| 3508 | const char *p; |
| 3509 | char *(*match)(const char *, const char *); |
| 3510 | |
| 3511 | jp = curjob; |
| 3512 | p = name; |
| 3513 | if (!p) |
| 3514 | goto currentjob; |
| 3515 | |
| 3516 | if (*p != '%') |
| 3517 | goto err; |
| 3518 | |
| 3519 | c = *++p; |
| 3520 | if (!c) |
| 3521 | goto currentjob; |
| 3522 | |
| 3523 | if (!p[1]) { |
| 3524 | if (c == '+' || c == '%') { |
| 3525 | currentjob: |
| 3526 | err_msg = "No current job"; |
| 3527 | goto check; |
| 3528 | } |
| 3529 | if (c == '-') { |
| 3530 | if (jp) |
| 3531 | jp = jp->prev_job; |
| 3532 | err_msg = "No previous job"; |
| 3533 | check: |
| 3534 | if (!jp) |
| 3535 | goto err; |
| 3536 | goto gotit; |
| 3537 | } |
| 3538 | } |
| 3539 | |
| 3540 | if (is_number(p)) { |
| 3541 | num = atoi(p); |
| 3542 | if (num < njobs) { |
| 3543 | jp = jobtab + num - 1; |
| 3544 | if (jp->used) |
| 3545 | goto gotit; |
| 3546 | goto err; |
| 3547 | } |
| 3548 | } |
| 3549 | |
| 3550 | match = prefix; |
| 3551 | if (*p == '?') { |
| 3552 | match = strstr; |
| 3553 | p++; |
| 3554 | } |
| 3555 | |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3556 | found = NULL; |
| 3557 | while (jp) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3558 | if (match(jp->ps[0].cmd, p)) { |
| 3559 | if (found) |
| 3560 | goto err; |
| 3561 | found = jp; |
| 3562 | err_msg = "%s: ambiguous"; |
| 3563 | } |
| 3564 | jp = jp->prev_job; |
| 3565 | } |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3566 | if (!found) |
| 3567 | goto err; |
| 3568 | jp = found; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3569 | |
| 3570 | gotit: |
| 3571 | #if JOBS |
| 3572 | err_msg = "job %s not created under job control"; |
| 3573 | if (getctl && jp->jobctl == 0) |
| 3574 | goto err; |
| 3575 | #endif |
| 3576 | return jp; |
| 3577 | err: |
| 3578 | ash_msg_and_raise_error(err_msg, name); |
| 3579 | } |
| 3580 | |
| 3581 | /* |
| 3582 | * Mark a job structure as unused. |
| 3583 | */ |
| 3584 | static void |
| 3585 | freejob(struct job *jp) |
| 3586 | { |
| 3587 | struct procstat *ps; |
| 3588 | int i; |
| 3589 | |
| 3590 | INT_OFF; |
| 3591 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
| 3592 | if (ps->cmd != nullstr) |
| 3593 | free(ps->cmd); |
| 3594 | } |
| 3595 | if (jp->ps != &jp->ps0) |
| 3596 | free(jp->ps); |
| 3597 | jp->used = 0; |
| 3598 | set_curjob(jp, CUR_DELETE); |
| 3599 | INT_ON; |
| 3600 | } |
| 3601 | |
| 3602 | #if JOBS |
| 3603 | static void |
| 3604 | xtcsetpgrp(int fd, pid_t pgrp) |
| 3605 | { |
| 3606 | if (tcsetpgrp(fd, pgrp)) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 3607 | ash_msg_and_raise_error("can't set tty process group (%m)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | /* |
| 3611 | * Turn job control on and off. |
| 3612 | * |
| 3613 | * Note: This code assumes that the third arg to ioctl is a character |
| 3614 | * pointer, which is true on Berkeley systems but not System V. Since |
| 3615 | * System V doesn't have job control yet, this isn't a problem now. |
| 3616 | * |
| 3617 | * Called with interrupts off. |
| 3618 | */ |
| 3619 | static void |
| 3620 | setjobctl(int on) |
| 3621 | { |
| 3622 | int fd; |
| 3623 | int pgrp; |
| 3624 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3625 | if (on == doing_jobctl || rootshell == 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3626 | return; |
| 3627 | if (on) { |
| 3628 | int ofd; |
| 3629 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 3630 | if (fd < 0) { |
| 3631 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 3632 | * That sometimes helps to acquire controlling tty. |
| 3633 | * Obviously, a workaround for bugs when someone |
| 3634 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3635 | fd = 2; |
| 3636 | while (!isatty(fd)) |
| 3637 | if (--fd < 0) |
| 3638 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3639 | } |
| 3640 | fd = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3641 | if (ofd >= 0) |
| 3642 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3643 | if (fd < 0) |
| 3644 | goto out; |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3645 | /* fd is a tty at this point */ |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 3646 | close_on_exec_on(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3647 | do { /* while we are in the background */ |
| 3648 | pgrp = tcgetpgrp(fd); |
| 3649 | if (pgrp < 0) { |
| 3650 | out: |
| 3651 | ash_msg("can't access tty; job control turned off"); |
| 3652 | mflag = on = 0; |
| 3653 | goto close; |
| 3654 | } |
| 3655 | if (pgrp == getpgrp()) |
| 3656 | break; |
| 3657 | killpg(0, SIGTTIN); |
| 3658 | } while (1); |
| 3659 | initialpgrp = pgrp; |
| 3660 | |
| 3661 | setsignal(SIGTSTP); |
| 3662 | setsignal(SIGTTOU); |
| 3663 | setsignal(SIGTTIN); |
| 3664 | pgrp = rootpid; |
| 3665 | setpgid(0, pgrp); |
| 3666 | xtcsetpgrp(fd, pgrp); |
| 3667 | } else { |
| 3668 | /* turning job control off */ |
| 3669 | fd = ttyfd; |
| 3670 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3671 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3672 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3673 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3674 | setpgid(0, pgrp); |
| 3675 | setsignal(SIGTSTP); |
| 3676 | setsignal(SIGTTOU); |
| 3677 | setsignal(SIGTTIN); |
| 3678 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3679 | if (fd >= 0) |
| 3680 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3681 | fd = -1; |
| 3682 | } |
| 3683 | ttyfd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3684 | doing_jobctl = on; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3687 | static int FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3688 | killcmd(int argc, char **argv) |
| 3689 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3690 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3691 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3692 | do { |
| 3693 | if (argv[i][0] == '%') { |
| 3694 | struct job *jp = getjob(argv[i], 0); |
| 3695 | unsigned pid = jp->ps[0].pid; |
| 3696 | /* Enough space for ' -NNN<nul>' */ |
| 3697 | argv[i] = alloca(sizeof(int)*3 + 3); |
| 3698 | /* kill_main has matching code to expect |
| 3699 | * leading space. Needed to not confuse |
| 3700 | * negative pids with "kill -SIGNAL_NO" syntax */ |
| 3701 | sprintf(argv[i], " -%u", pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3702 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3703 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3704 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3705 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | static void |
| 3709 | showpipe(struct job *jp, FILE *out) |
| 3710 | { |
| 3711 | struct procstat *sp; |
| 3712 | struct procstat *spend; |
| 3713 | |
| 3714 | spend = jp->ps + jp->nprocs; |
| 3715 | for (sp = jp->ps + 1; sp < spend; sp++) |
| 3716 | fprintf(out, " | %s", sp->cmd); |
| 3717 | outcslow('\n', out); |
| 3718 | flush_stdout_stderr(); |
| 3719 | } |
| 3720 | |
| 3721 | |
| 3722 | static int |
| 3723 | restartjob(struct job *jp, int mode) |
| 3724 | { |
| 3725 | struct procstat *ps; |
| 3726 | int i; |
| 3727 | int status; |
| 3728 | pid_t pgid; |
| 3729 | |
| 3730 | INT_OFF; |
| 3731 | if (jp->state == JOBDONE) |
| 3732 | goto out; |
| 3733 | jp->state = JOBRUNNING; |
| 3734 | pgid = jp->ps->pid; |
| 3735 | if (mode == FORK_FG) |
| 3736 | xtcsetpgrp(ttyfd, pgid); |
| 3737 | killpg(pgid, SIGCONT); |
| 3738 | ps = jp->ps; |
| 3739 | i = jp->nprocs; |
| 3740 | do { |
| 3741 | if (WIFSTOPPED(ps->status)) { |
| 3742 | ps->status = -1; |
| 3743 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3744 | ps++; |
| 3745 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3746 | out: |
| 3747 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 3748 | INT_ON; |
| 3749 | return status; |
| 3750 | } |
| 3751 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3752 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3753 | fg_bgcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3754 | { |
| 3755 | struct job *jp; |
| 3756 | FILE *out; |
| 3757 | int mode; |
| 3758 | int retval; |
| 3759 | |
| 3760 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 3761 | nextopt(nullstr); |
| 3762 | argv = argptr; |
| 3763 | out = stdout; |
| 3764 | do { |
| 3765 | jp = getjob(*argv, 1); |
| 3766 | if (mode == FORK_BG) { |
| 3767 | set_curjob(jp, CUR_RUNNING); |
| 3768 | fprintf(out, "[%d] ", jobno(jp)); |
| 3769 | } |
| 3770 | outstr(jp->ps->cmd, out); |
| 3771 | showpipe(jp, out); |
| 3772 | retval = restartjob(jp, mode); |
| 3773 | } while (*argv && *++argv); |
| 3774 | return retval; |
| 3775 | } |
| 3776 | #endif |
| 3777 | |
| 3778 | static int |
| 3779 | sprint_status(char *s, int status, int sigonly) |
| 3780 | { |
| 3781 | int col; |
| 3782 | int st; |
| 3783 | |
| 3784 | col = 0; |
| 3785 | if (!WIFEXITED(status)) { |
| 3786 | #if JOBS |
| 3787 | if (WIFSTOPPED(status)) |
| 3788 | st = WSTOPSIG(status); |
| 3789 | else |
| 3790 | #endif |
| 3791 | st = WTERMSIG(status); |
| 3792 | if (sigonly) { |
| 3793 | if (st == SIGINT || st == SIGPIPE) |
| 3794 | goto out; |
| 3795 | #if JOBS |
| 3796 | if (WIFSTOPPED(status)) |
| 3797 | goto out; |
| 3798 | #endif |
| 3799 | } |
| 3800 | st &= 0x7f; |
| 3801 | col = fmtstr(s, 32, strsignal(st)); |
| 3802 | if (WCOREDUMP(status)) { |
| 3803 | col += fmtstr(s + col, 16, " (core dumped)"); |
| 3804 | } |
| 3805 | } else if (!sigonly) { |
| 3806 | st = WEXITSTATUS(status); |
| 3807 | if (st) |
| 3808 | col = fmtstr(s, 16, "Done(%d)", st); |
| 3809 | else |
| 3810 | col = fmtstr(s, 16, "Done"); |
| 3811 | } |
| 3812 | out: |
| 3813 | return col; |
| 3814 | } |
| 3815 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3816 | static int |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3817 | dowait(int wait_flags, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3818 | { |
| 3819 | int pid; |
| 3820 | int status; |
| 3821 | struct job *jp; |
| 3822 | struct job *thisjob; |
| 3823 | int state; |
| 3824 | |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 3825 | TRACE(("dowait(0x%x) called\n", wait_flags)); |
| 3826 | |
| 3827 | /* Do a wait system call. If job control is compiled in, we accept |
| 3828 | * stopped processes. wait_flags may have WNOHANG, preventing blocking. |
| 3829 | * NB: _not_ safe_waitpid, we need to detect EINTR */ |
| 3830 | pid = waitpid(-1, &status, |
| 3831 | (doing_jobctl ? (wait_flags | WUNTRACED) : wait_flags)); |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 3832 | TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n", |
| 3833 | pid, status, errno, strerror(errno))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3834 | if (pid <= 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3835 | return pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3836 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3837 | INT_OFF; |
| 3838 | thisjob = NULL; |
| 3839 | for (jp = curjob; jp; jp = jp->prev_job) { |
| 3840 | struct procstat *sp; |
| 3841 | struct procstat *spend; |
| 3842 | if (jp->state == JOBDONE) |
| 3843 | continue; |
| 3844 | state = JOBDONE; |
| 3845 | spend = jp->ps + jp->nprocs; |
| 3846 | sp = jp->ps; |
| 3847 | do { |
| 3848 | if (sp->pid == pid) { |
| 3849 | TRACE(("Job %d: changing status of proc %d " |
| 3850 | "from 0x%x to 0x%x\n", |
| 3851 | jobno(jp), pid, sp->status, status)); |
| 3852 | sp->status = status; |
| 3853 | thisjob = jp; |
| 3854 | } |
| 3855 | if (sp->status == -1) |
| 3856 | state = JOBRUNNING; |
| 3857 | #if JOBS |
| 3858 | if (state == JOBRUNNING) |
| 3859 | continue; |
| 3860 | if (WIFSTOPPED(sp->status)) { |
| 3861 | jp->stopstatus = sp->status; |
| 3862 | state = JOBSTOPPED; |
| 3863 | } |
| 3864 | #endif |
| 3865 | } while (++sp < spend); |
| 3866 | if (thisjob) |
| 3867 | goto gotjob; |
| 3868 | } |
| 3869 | #if JOBS |
| 3870 | if (!WIFSTOPPED(status)) |
| 3871 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3872 | jobless--; |
| 3873 | goto out; |
| 3874 | |
| 3875 | gotjob: |
| 3876 | if (state != JOBRUNNING) { |
| 3877 | thisjob->changed = 1; |
| 3878 | |
| 3879 | if (thisjob->state != state) { |
| 3880 | TRACE(("Job %d: changing state from %d to %d\n", |
| 3881 | jobno(thisjob), thisjob->state, state)); |
| 3882 | thisjob->state = state; |
| 3883 | #if JOBS |
| 3884 | if (state == JOBSTOPPED) { |
| 3885 | set_curjob(thisjob, CUR_STOPPED); |
| 3886 | } |
| 3887 | #endif |
| 3888 | } |
| 3889 | } |
| 3890 | |
| 3891 | out: |
| 3892 | INT_ON; |
| 3893 | |
| 3894 | if (thisjob && thisjob == job) { |
| 3895 | char s[48 + 1]; |
| 3896 | int len; |
| 3897 | |
| 3898 | len = sprint_status(s, status, 1); |
| 3899 | if (len) { |
| 3900 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3901 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3902 | out2str(s); |
| 3903 | } |
| 3904 | } |
| 3905 | return pid; |
| 3906 | } |
| 3907 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3908 | static int |
| 3909 | blocking_wait_with_raise_on_sig(struct job *job) |
| 3910 | { |
| 3911 | pid_t pid = dowait(DOWAIT_BLOCK, job); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3912 | if (pid <= 0 && pending_sig) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3913 | raise_exception(EXSIG); |
| 3914 | return pid; |
| 3915 | } |
| 3916 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3917 | #if JOBS |
| 3918 | static void |
| 3919 | showjob(FILE *out, struct job *jp, int mode) |
| 3920 | { |
| 3921 | struct procstat *ps; |
| 3922 | struct procstat *psend; |
| 3923 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3924 | int indent_col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3925 | char s[80]; |
| 3926 | |
| 3927 | ps = jp->ps; |
| 3928 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3929 | if (mode & SHOW_ONLY_PGID) { /* jobs -p */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3930 | /* just output process (group) id of pipeline */ |
| 3931 | fprintf(out, "%d\n", ps->pid); |
| 3932 | return; |
| 3933 | } |
| 3934 | |
| 3935 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3936 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3937 | |
| 3938 | if (jp == curjob) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3939 | s[col - 3] = '+'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3940 | else if (curjob && jp == curjob->prev_job) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3941 | s[col - 3] = '-'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3942 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3943 | if (mode & SHOW_PIDS) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3944 | col += fmtstr(s + col, 16, "%d ", ps->pid); |
| 3945 | |
| 3946 | psend = ps + jp->nprocs; |
| 3947 | |
| 3948 | if (jp->state == JOBRUNNING) { |
| 3949 | strcpy(s + col, "Running"); |
| 3950 | col += sizeof("Running") - 1; |
| 3951 | } else { |
| 3952 | int status = psend[-1].status; |
| 3953 | if (jp->state == JOBSTOPPED) |
| 3954 | status = jp->stopstatus; |
| 3955 | col += sprint_status(s + col, status, 0); |
| 3956 | } |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3957 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3958 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3959 | /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line |
| 3960 | * or prints several "PID | <cmdN>" lines, |
| 3961 | * depending on SHOW_PIDS bit. |
| 3962 | * We do not print status of individual processes |
| 3963 | * between PID and <cmdN>. bash does it, but not very well: |
| 3964 | * first line shows overall job status, not process status, |
| 3965 | * making it impossible to know 1st process status. |
| 3966 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3967 | goto start; |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3968 | while (1) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3969 | /* for each process */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3970 | s[0] = '\0'; |
| 3971 | col = 33; |
| 3972 | if (mode & SHOW_PIDS) |
| 3973 | col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->pid) - 1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3974 | start: |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3975 | fprintf(out, "%s%*c", s, 33 - col >= 0 ? 33 - col : 0, ' '); |
| 3976 | if (ps != jp->ps) |
| 3977 | fprintf(out, "| "); |
| 3978 | fprintf(out, "%s", ps->cmd); |
| 3979 | if (++ps == psend) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3980 | break; |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3981 | } |
| 3982 | outcslow('\n', out); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3983 | |
| 3984 | jp->changed = 0; |
| 3985 | |
| 3986 | if (jp->state == JOBDONE) { |
| 3987 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 3988 | freejob(jp); |
| 3989 | } |
| 3990 | } |
| 3991 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3992 | /* |
| 3993 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 3994 | * statuses have changed since the last call to showjobs. |
| 3995 | */ |
| 3996 | static void |
| 3997 | showjobs(FILE *out, int mode) |
| 3998 | { |
| 3999 | struct job *jp; |
| 4000 | |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4001 | TRACE(("showjobs(0x%x) called\n", mode)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4002 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4003 | /* Handle all finished jobs */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4004 | while (dowait(DOWAIT_NONBLOCK, NULL) > 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4005 | continue; |
| 4006 | |
| 4007 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4008 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4009 | showjob(out, jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4010 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4011 | } |
| 4012 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4013 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4014 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4015 | jobscmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4016 | { |
| 4017 | int mode, m; |
| 4018 | |
| 4019 | mode = 0; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4020 | while ((m = nextopt("lp")) != '\0') { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4021 | if (m == 'l') |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4022 | mode |= SHOW_PIDS; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4023 | else |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4024 | mode |= SHOW_ONLY_PGID; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | argv = argptr; |
| 4028 | if (*argv) { |
| 4029 | do |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4030 | showjob(stdout, getjob(*argv, 0), mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4031 | while (*++argv); |
| 4032 | } else |
| 4033 | showjobs(stdout, mode); |
| 4034 | |
| 4035 | return 0; |
| 4036 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4037 | #endif /* JOBS */ |
| 4038 | |
| 4039 | static int |
| 4040 | getstatus(struct job *job) |
| 4041 | { |
| 4042 | int status; |
| 4043 | int retval; |
| 4044 | |
| 4045 | status = job->ps[job->nprocs - 1].status; |
| 4046 | retval = WEXITSTATUS(status); |
| 4047 | if (!WIFEXITED(status)) { |
| 4048 | #if JOBS |
| 4049 | retval = WSTOPSIG(status); |
| 4050 | if (!WIFSTOPPED(status)) |
| 4051 | #endif |
| 4052 | { |
| 4053 | /* XXX: limits number of signals */ |
| 4054 | retval = WTERMSIG(status); |
| 4055 | #if JOBS |
| 4056 | if (retval == SIGINT) |
| 4057 | job->sigint = 1; |
| 4058 | #endif |
| 4059 | } |
| 4060 | retval += 128; |
| 4061 | } |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4062 | 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] | 4063 | jobno(job), job->nprocs, status, retval)); |
| 4064 | return retval; |
| 4065 | } |
| 4066 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4067 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4068 | waitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4069 | { |
| 4070 | struct job *job; |
| 4071 | int retval; |
| 4072 | struct job *jp; |
| 4073 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4074 | if (pending_sig) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4075 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4076 | |
| 4077 | nextopt(nullstr); |
| 4078 | retval = 0; |
| 4079 | |
| 4080 | argv = argptr; |
| 4081 | if (!*argv) { |
| 4082 | /* wait for all jobs */ |
| 4083 | for (;;) { |
| 4084 | jp = curjob; |
| 4085 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4086 | if (!jp) /* no running procs */ |
| 4087 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4088 | if (jp->state == JOBRUNNING) |
| 4089 | break; |
| 4090 | jp->waited = 1; |
| 4091 | jp = jp->prev_job; |
| 4092 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4093 | /* man bash: |
| 4094 | * "When bash is waiting for an asynchronous command via |
| 4095 | * the wait builtin, the reception of a signal for which a trap |
| 4096 | * has been set will cause the wait builtin to return immediately |
| 4097 | * with an exit status greater than 128, immediately after which |
| 4098 | * the trap is executed." |
| 4099 | * Do we do it that way? */ |
| 4100 | blocking_wait_with_raise_on_sig(NULL); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4101 | } |
| 4102 | } |
| 4103 | |
| 4104 | retval = 127; |
| 4105 | do { |
| 4106 | if (**argv != '%') { |
| 4107 | pid_t pid = number(*argv); |
| 4108 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4109 | while (1) { |
| 4110 | if (!job) |
| 4111 | goto repeat; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4112 | if (job->ps[job->nprocs - 1].pid == pid) |
| 4113 | break; |
| 4114 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4115 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4116 | } else |
| 4117 | job = getjob(*argv, 0); |
| 4118 | /* loop until process terminated or stopped */ |
| 4119 | while (job->state == JOBRUNNING) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4120 | blocking_wait_with_raise_on_sig(NULL); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4121 | job->waited = 1; |
| 4122 | retval = getstatus(job); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4123 | repeat: ; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4124 | } while (*++argv); |
| 4125 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4126 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4127 | return retval; |
| 4128 | } |
| 4129 | |
| 4130 | static struct job * |
| 4131 | growjobtab(void) |
| 4132 | { |
| 4133 | size_t len; |
| 4134 | ptrdiff_t offset; |
| 4135 | struct job *jp, *jq; |
| 4136 | |
| 4137 | len = njobs * sizeof(*jp); |
| 4138 | jq = jobtab; |
| 4139 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4140 | |
| 4141 | offset = (char *)jp - (char *)jq; |
| 4142 | if (offset) { |
| 4143 | /* Relocate pointers */ |
| 4144 | size_t l = len; |
| 4145 | |
| 4146 | jq = (struct job *)((char *)jq + l); |
| 4147 | while (l) { |
| 4148 | l -= sizeof(*jp); |
| 4149 | jq--; |
| 4150 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4151 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4152 | if (joff(jp)->ps == &jq->ps0) |
| 4153 | jmove(joff(jp)->ps); |
| 4154 | if (joff(jp)->prev_job) |
| 4155 | jmove(joff(jp)->prev_job); |
| 4156 | } |
| 4157 | if (curjob) |
| 4158 | jmove(curjob); |
| 4159 | #undef joff |
| 4160 | #undef jmove |
| 4161 | } |
| 4162 | |
| 4163 | njobs += 4; |
| 4164 | jobtab = jp; |
| 4165 | jp = (struct job *)((char *)jp + len); |
| 4166 | jq = jp + 3; |
| 4167 | do { |
| 4168 | jq->used = 0; |
| 4169 | } while (--jq >= jp); |
| 4170 | return jp; |
| 4171 | } |
| 4172 | |
| 4173 | /* |
| 4174 | * Return a new job structure. |
| 4175 | * Called with interrupts off. |
| 4176 | */ |
| 4177 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4178 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4179 | { |
| 4180 | int i; |
| 4181 | struct job *jp; |
| 4182 | |
| 4183 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4184 | if (--i < 0) { |
| 4185 | jp = growjobtab(); |
| 4186 | break; |
| 4187 | } |
| 4188 | if (jp->used == 0) |
| 4189 | break; |
| 4190 | if (jp->state != JOBDONE || !jp->waited) |
| 4191 | continue; |
| 4192 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4193 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4194 | continue; |
| 4195 | #endif |
| 4196 | freejob(jp); |
| 4197 | break; |
| 4198 | } |
| 4199 | memset(jp, 0, sizeof(*jp)); |
| 4200 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4201 | /* jp->jobctl is a bitfield. |
| 4202 | * "jp->jobctl |= jobctl" likely to give awful code */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4203 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4204 | jp->jobctl = 1; |
| 4205 | #endif |
| 4206 | jp->prev_job = curjob; |
| 4207 | curjob = jp; |
| 4208 | jp->used = 1; |
| 4209 | jp->ps = &jp->ps0; |
| 4210 | if (nprocs > 1) { |
| 4211 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4212 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4213 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4214 | jobno(jp))); |
| 4215 | return jp; |
| 4216 | } |
| 4217 | |
| 4218 | #if JOBS |
| 4219 | /* |
| 4220 | * Return a string identifying a command (to be printed by the |
| 4221 | * jobs command). |
| 4222 | */ |
| 4223 | static char *cmdnextc; |
| 4224 | |
| 4225 | static void |
| 4226 | cmdputs(const char *s) |
| 4227 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4228 | static const char vstype[VSTYPE + 1][3] = { |
| 4229 | "", "}", "-", "+", "?", "=", |
| 4230 | "%", "%%", "#", "##" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4231 | IF_ASH_BASH_COMPAT(, ":", "/", "//") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4232 | }; |
| 4233 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4234 | const char *p, *str; |
| 4235 | char c, cc[2] = " "; |
| 4236 | char *nextc; |
| 4237 | int subtype = 0; |
| 4238 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4239 | |
| 4240 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4241 | p = s; |
| 4242 | while ((c = *p++) != 0) { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 4243 | str = NULL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4244 | switch (c) { |
| 4245 | case CTLESC: |
| 4246 | c = *p++; |
| 4247 | break; |
| 4248 | case CTLVAR: |
| 4249 | subtype = *p++; |
| 4250 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4251 | str = "${#"; |
| 4252 | else |
| 4253 | str = "${"; |
| 4254 | if (!(subtype & VSQUOTE) == !(quoted & 1)) |
| 4255 | goto dostr; |
| 4256 | quoted ^= 1; |
| 4257 | c = '"'; |
| 4258 | break; |
| 4259 | case CTLENDVAR: |
| 4260 | str = "\"}" + !(quoted & 1); |
| 4261 | quoted >>= 1; |
| 4262 | subtype = 0; |
| 4263 | goto dostr; |
| 4264 | case CTLBACKQ: |
| 4265 | str = "$(...)"; |
| 4266 | goto dostr; |
| 4267 | case CTLBACKQ+CTLQUOTE: |
| 4268 | str = "\"$(...)\""; |
| 4269 | goto dostr; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4270 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4271 | case CTLARI: |
| 4272 | str = "$(("; |
| 4273 | goto dostr; |
| 4274 | case CTLENDARI: |
| 4275 | str = "))"; |
| 4276 | goto dostr; |
| 4277 | #endif |
| 4278 | case CTLQUOTEMARK: |
| 4279 | quoted ^= 1; |
| 4280 | c = '"'; |
| 4281 | break; |
| 4282 | case '=': |
| 4283 | if (subtype == 0) |
| 4284 | break; |
| 4285 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4286 | quoted <<= 1; |
| 4287 | str = vstype[subtype & VSTYPE]; |
| 4288 | if (subtype & VSNUL) |
| 4289 | c = ':'; |
| 4290 | else |
| 4291 | goto checkstr; |
| 4292 | break; |
| 4293 | case '\'': |
| 4294 | case '\\': |
| 4295 | case '"': |
| 4296 | case '$': |
| 4297 | /* These can only happen inside quotes */ |
| 4298 | cc[0] = c; |
| 4299 | str = cc; |
| 4300 | c = '\\'; |
| 4301 | break; |
| 4302 | default: |
| 4303 | break; |
| 4304 | } |
| 4305 | USTPUTC(c, nextc); |
| 4306 | checkstr: |
| 4307 | if (!str) |
| 4308 | continue; |
| 4309 | dostr: |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4310 | while ((c = *str++) != '\0') { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4311 | USTPUTC(c, nextc); |
| 4312 | } |
| 4313 | } |
| 4314 | if (quoted & 1) { |
| 4315 | USTPUTC('"', nextc); |
| 4316 | } |
| 4317 | *nextc = 0; |
| 4318 | cmdnextc = nextc; |
| 4319 | } |
| 4320 | |
| 4321 | /* cmdtxt() and cmdlist() call each other */ |
| 4322 | static void cmdtxt(union node *n); |
| 4323 | |
| 4324 | static void |
| 4325 | cmdlist(union node *np, int sep) |
| 4326 | { |
| 4327 | for (; np; np = np->narg.next) { |
| 4328 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4329 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4330 | cmdtxt(np); |
| 4331 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4332 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4333 | } |
| 4334 | } |
| 4335 | |
| 4336 | static void |
| 4337 | cmdtxt(union node *n) |
| 4338 | { |
| 4339 | union node *np; |
| 4340 | struct nodelist *lp; |
| 4341 | const char *p; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4342 | |
| 4343 | if (!n) |
| 4344 | return; |
| 4345 | switch (n->type) { |
| 4346 | default: |
| 4347 | #if DEBUG |
| 4348 | abort(); |
| 4349 | #endif |
| 4350 | case NPIPE: |
| 4351 | lp = n->npipe.cmdlist; |
| 4352 | for (;;) { |
| 4353 | cmdtxt(lp->n); |
| 4354 | lp = lp->next; |
| 4355 | if (!lp) |
| 4356 | break; |
| 4357 | cmdputs(" | "); |
| 4358 | } |
| 4359 | break; |
| 4360 | case NSEMI: |
| 4361 | p = "; "; |
| 4362 | goto binop; |
| 4363 | case NAND: |
| 4364 | p = " && "; |
| 4365 | goto binop; |
| 4366 | case NOR: |
| 4367 | p = " || "; |
| 4368 | binop: |
| 4369 | cmdtxt(n->nbinary.ch1); |
| 4370 | cmdputs(p); |
| 4371 | n = n->nbinary.ch2; |
| 4372 | goto donode; |
| 4373 | case NREDIR: |
| 4374 | case NBACKGND: |
| 4375 | n = n->nredir.n; |
| 4376 | goto donode; |
| 4377 | case NNOT: |
| 4378 | cmdputs("!"); |
| 4379 | n = n->nnot.com; |
| 4380 | donode: |
| 4381 | cmdtxt(n); |
| 4382 | break; |
| 4383 | case NIF: |
| 4384 | cmdputs("if "); |
| 4385 | cmdtxt(n->nif.test); |
| 4386 | cmdputs("; then "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4387 | if (n->nif.elsepart) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4388 | cmdtxt(n->nif.ifpart); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4389 | cmdputs("; else "); |
| 4390 | n = n->nif.elsepart; |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4391 | } else { |
| 4392 | n = n->nif.ifpart; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4393 | } |
| 4394 | p = "; fi"; |
| 4395 | goto dotail; |
| 4396 | case NSUBSHELL: |
| 4397 | cmdputs("("); |
| 4398 | n = n->nredir.n; |
| 4399 | p = ")"; |
| 4400 | goto dotail; |
| 4401 | case NWHILE: |
| 4402 | p = "while "; |
| 4403 | goto until; |
| 4404 | case NUNTIL: |
| 4405 | p = "until "; |
| 4406 | until: |
| 4407 | cmdputs(p); |
| 4408 | cmdtxt(n->nbinary.ch1); |
| 4409 | n = n->nbinary.ch2; |
| 4410 | p = "; done"; |
| 4411 | dodo: |
| 4412 | cmdputs("; do "); |
| 4413 | dotail: |
| 4414 | cmdtxt(n); |
| 4415 | goto dotail2; |
| 4416 | case NFOR: |
| 4417 | cmdputs("for "); |
| 4418 | cmdputs(n->nfor.var); |
| 4419 | cmdputs(" in "); |
| 4420 | cmdlist(n->nfor.args, 1); |
| 4421 | n = n->nfor.body; |
| 4422 | p = "; done"; |
| 4423 | goto dodo; |
| 4424 | case NDEFUN: |
| 4425 | cmdputs(n->narg.text); |
| 4426 | p = "() { ... }"; |
| 4427 | goto dotail2; |
| 4428 | case NCMD: |
| 4429 | cmdlist(n->ncmd.args, 1); |
| 4430 | cmdlist(n->ncmd.redirect, 0); |
| 4431 | break; |
| 4432 | case NARG: |
| 4433 | p = n->narg.text; |
| 4434 | dotail2: |
| 4435 | cmdputs(p); |
| 4436 | break; |
| 4437 | case NHERE: |
| 4438 | case NXHERE: |
| 4439 | p = "<<..."; |
| 4440 | goto dotail2; |
| 4441 | case NCASE: |
| 4442 | cmdputs("case "); |
| 4443 | cmdputs(n->ncase.expr->narg.text); |
| 4444 | cmdputs(" in "); |
| 4445 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 4446 | cmdtxt(np->nclist.pattern); |
| 4447 | cmdputs(") "); |
| 4448 | cmdtxt(np->nclist.body); |
| 4449 | cmdputs(";; "); |
| 4450 | } |
| 4451 | p = "esac"; |
| 4452 | goto dotail2; |
| 4453 | case NTO: |
| 4454 | p = ">"; |
| 4455 | goto redir; |
| 4456 | case NCLOBBER: |
| 4457 | p = ">|"; |
| 4458 | goto redir; |
| 4459 | case NAPPEND: |
| 4460 | p = ">>"; |
| 4461 | goto redir; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4462 | #if ENABLE_ASH_BASH_COMPAT |
| 4463 | case NTO2: |
| 4464 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4465 | case NTOFD: |
| 4466 | p = ">&"; |
| 4467 | goto redir; |
| 4468 | case NFROM: |
| 4469 | p = "<"; |
| 4470 | goto redir; |
| 4471 | case NFROMFD: |
| 4472 | p = "<&"; |
| 4473 | goto redir; |
| 4474 | case NFROMTO: |
| 4475 | p = "<>"; |
| 4476 | redir: |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4477 | cmdputs(utoa(n->nfile.fd)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4478 | cmdputs(p); |
| 4479 | if (n->type == NTOFD || n->type == NFROMFD) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4480 | cmdputs(utoa(n->ndup.dupfd)); |
| 4481 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4482 | } |
| 4483 | n = n->nfile.fname; |
| 4484 | goto donode; |
| 4485 | } |
| 4486 | } |
| 4487 | |
| 4488 | static char * |
| 4489 | commandtext(union node *n) |
| 4490 | { |
| 4491 | char *name; |
| 4492 | |
| 4493 | STARTSTACKSTR(cmdnextc); |
| 4494 | cmdtxt(n); |
| 4495 | name = stackblock(); |
| 4496 | TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n", |
| 4497 | name, cmdnextc, cmdnextc)); |
| 4498 | return ckstrdup(name); |
| 4499 | } |
| 4500 | #endif /* JOBS */ |
| 4501 | |
| 4502 | /* |
| 4503 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 4504 | * own process group. Jp is a job structure that the job is to be added to. |
| 4505 | * N is the command that will be evaluated by the child. Both jp and n may |
| 4506 | * be NULL. The mode parameter can be one of the following: |
| 4507 | * FORK_FG - Fork off a foreground process. |
| 4508 | * FORK_BG - Fork off a background process. |
| 4509 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 4510 | * process group even if job control is on. |
| 4511 | * |
| 4512 | * When job control is turned off, background processes have their standard |
| 4513 | * input redirected to /dev/null (except for the second and later processes |
| 4514 | * in a pipeline). |
| 4515 | * |
| 4516 | * Called with interrupts off. |
| 4517 | */ |
| 4518 | /* |
| 4519 | * Clear traps on a fork. |
| 4520 | */ |
| 4521 | static void |
| 4522 | clear_traps(void) |
| 4523 | { |
| 4524 | char **tp; |
| 4525 | |
| 4526 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4527 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4528 | INT_OFF; |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4529 | if (trap_ptr == trap) |
| 4530 | free(*tp); |
| 4531 | /* else: it "belongs" to trap_ptr vector, don't free */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4532 | *tp = NULL; |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 4533 | if ((tp - trap) != 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4534 | setsignal(tp - trap); |
| 4535 | INT_ON; |
| 4536 | } |
| 4537 | } |
| 4538 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4539 | |
| 4540 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4541 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 4542 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4543 | /* Called after fork(), in child */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4544 | #if !JOBS |
| 4545 | # define forkchild(jp, n, mode) forkchild(jp, mode) |
| 4546 | #endif |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4547 | static NOINLINE void |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4548 | forkchild(struct job *jp, union node *n, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4549 | { |
| 4550 | int oldlvl; |
| 4551 | |
| 4552 | TRACE(("Child shell %d\n", getpid())); |
| 4553 | oldlvl = shlvl; |
| 4554 | shlvl++; |
| 4555 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4556 | /* man bash: "Non-builtin commands run by bash have signal handlers |
| 4557 | * set to the values inherited by the shell from its parent". |
| 4558 | * Do we do it correctly? */ |
| 4559 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4560 | closescript(); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4561 | |
| 4562 | if (mode == FORK_NOJOB /* is it `xxx` ? */ |
| 4563 | && n && n->type == NCMD /* is it single cmd? */ |
| 4564 | /* && n->ncmd.args->type == NARG - always true? */ |
| 4565 | && strcmp(n->ncmd.args->narg.text, "trap") == 0 |
| 4566 | && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */ |
| 4567 | /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */ |
| 4568 | ) { |
| 4569 | TRACE(("Trap hack\n")); |
| 4570 | /* Awful hack for `trap` or $(trap). |
| 4571 | * |
| 4572 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 4573 | * contains an example where "trap" is executed in a subshell: |
| 4574 | * |
| 4575 | * save_traps=$(trap) |
| 4576 | * ... |
| 4577 | * eval "$save_traps" |
| 4578 | * |
| 4579 | * Standard does not say that "trap" in subshell shall print |
| 4580 | * parent shell's traps. It only says that its output |
| 4581 | * must have suitable form, but then, in the above example |
| 4582 | * (which is not supposed to be normative), it implies that. |
| 4583 | * |
| 4584 | * bash (and probably other shell) does implement it |
| 4585 | * (traps are reset to defaults, but "trap" still shows them), |
| 4586 | * but as a result, "trap" logic is hopelessly messed up: |
| 4587 | * |
| 4588 | * # trap |
| 4589 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 4590 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 4591 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 4592 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 4593 | * trap -- 'echo Ho' SIGWINCH |
| 4594 | * # echo `(trap)` <--- in subshell in subshell - output |
| 4595 | * trap -- 'echo Ho' SIGWINCH |
| 4596 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 4597 | * trap -- 'echo Ho' SIGWINCH |
| 4598 | * |
| 4599 | * The rules when to forget and when to not forget traps |
| 4600 | * get really complex and nonsensical. |
| 4601 | * |
| 4602 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 4603 | */ |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4604 | /* Save trap handler strings for trap builtin to print */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4605 | trap_ptr = memcpy(xmalloc(sizeof(trap)), trap, sizeof(trap)); |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4606 | /* Fall through into clearing traps */ |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4607 | } |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4608 | clear_traps(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4609 | #if JOBS |
| 4610 | /* do job control only in root shell */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4611 | doing_jobctl = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4612 | if (mode != FORK_NOJOB && jp->jobctl && !oldlvl) { |
| 4613 | pid_t pgrp; |
| 4614 | |
| 4615 | if (jp->nprocs == 0) |
| 4616 | pgrp = getpid(); |
| 4617 | else |
| 4618 | pgrp = jp->ps[0].pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4619 | /* this can fail because we are doing it in the parent also */ |
| 4620 | setpgid(0, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4621 | if (mode == FORK_FG) |
| 4622 | xtcsetpgrp(ttyfd, pgrp); |
| 4623 | setsignal(SIGTSTP); |
| 4624 | setsignal(SIGTTOU); |
| 4625 | } else |
| 4626 | #endif |
| 4627 | if (mode == FORK_BG) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4628 | /* man bash: "When job control is not in effect, |
| 4629 | * asynchronous commands ignore SIGINT and SIGQUIT" */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4630 | ignoresig(SIGINT); |
| 4631 | ignoresig(SIGQUIT); |
| 4632 | if (jp->nprocs == 0) { |
| 4633 | close(0); |
| 4634 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 4635 | ash_msg_and_raise_error("can't open '%s'", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4636 | } |
| 4637 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4638 | if (!oldlvl) { |
| 4639 | if (iflag) { /* why if iflag only? */ |
| 4640 | setsignal(SIGINT); |
| 4641 | setsignal(SIGTERM); |
| 4642 | } |
| 4643 | /* man bash: |
| 4644 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 4645 | * commands run by bash have signal handlers |
| 4646 | * set to the values inherited by the shell |
| 4647 | * from its parent". |
| 4648 | * Take care of the second rule: */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4649 | setsignal(SIGQUIT); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4650 | } |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4651 | #if JOBS |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4652 | if (n && n->type == NCMD |
| 4653 | && strcmp(n->ncmd.args->narg.text, "jobs") == 0 |
| 4654 | ) { |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4655 | TRACE(("Job hack\n")); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4656 | /* "jobs": we do not want to clear job list for it, |
| 4657 | * instead we remove only _its_ own_ job from job list. |
| 4658 | * This makes "jobs .... | cat" more useful. |
| 4659 | */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4660 | freejob(curjob); |
| 4661 | return; |
| 4662 | } |
| 4663 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4664 | for (jp = curjob; jp; jp = jp->prev_job) |
| 4665 | freejob(jp); |
| 4666 | jobless = 0; |
| 4667 | } |
| 4668 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4669 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 4670 | #if !JOBS |
| 4671 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 4672 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4673 | static void |
| 4674 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 4675 | { |
| 4676 | TRACE(("In parent shell: child = %d\n", pid)); |
| 4677 | if (!jp) { |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4678 | while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0) |
| 4679 | continue; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4680 | jobless++; |
| 4681 | return; |
| 4682 | } |
| 4683 | #if JOBS |
| 4684 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 4685 | int pgrp; |
| 4686 | |
| 4687 | if (jp->nprocs == 0) |
| 4688 | pgrp = pid; |
| 4689 | else |
| 4690 | pgrp = jp->ps[0].pid; |
| 4691 | /* This can fail because we are doing it in the child also */ |
| 4692 | setpgid(pid, pgrp); |
| 4693 | } |
| 4694 | #endif |
| 4695 | if (mode == FORK_BG) { |
| 4696 | backgndpid = pid; /* set $! */ |
| 4697 | set_curjob(jp, CUR_RUNNING); |
| 4698 | } |
| 4699 | if (jp) { |
| 4700 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
| 4701 | ps->pid = pid; |
| 4702 | ps->status = -1; |
| 4703 | ps->cmd = nullstr; |
| 4704 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4705 | if (doing_jobctl && n) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4706 | ps->cmd = commandtext(n); |
| 4707 | #endif |
| 4708 | } |
| 4709 | } |
| 4710 | |
| 4711 | static int |
| 4712 | forkshell(struct job *jp, union node *n, int mode) |
| 4713 | { |
| 4714 | int pid; |
| 4715 | |
| 4716 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 4717 | pid = fork(); |
| 4718 | if (pid < 0) { |
| 4719 | TRACE(("Fork failed, errno=%d", errno)); |
| 4720 | if (jp) |
| 4721 | freejob(jp); |
Denis Vlasenko | fa0b56d | 2008-07-01 16:09:07 +0000 | [diff] [blame] | 4722 | ash_msg_and_raise_error("can't fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4723 | } |
| 4724 | if (pid == 0) |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4725 | forkchild(jp, n, mode); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4726 | else |
| 4727 | forkparent(jp, n, mode, pid); |
| 4728 | return pid; |
| 4729 | } |
| 4730 | |
| 4731 | /* |
| 4732 | * Wait for job to finish. |
| 4733 | * |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4734 | * Under job control we have the problem that while a child process |
| 4735 | * is running interrupts generated by the user are sent to the child |
| 4736 | * but not to the shell. This means that an infinite loop started by |
| 4737 | * an interactive user may be hard to kill. With job control turned off, |
| 4738 | * an interactive user may place an interactive program inside a loop. |
| 4739 | * If the interactive program catches interrupts, the user doesn't want |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4740 | * these interrupts to also abort the loop. The approach we take here |
| 4741 | * is to have the shell ignore interrupt signals while waiting for a |
| 4742 | * foreground process to terminate, and then send itself an interrupt |
| 4743 | * signal if the child process was terminated by an interrupt signal. |
| 4744 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 4745 | * exit on interrupt; unless these processes terminate themselves by |
| 4746 | * sending a signal to themselves (instead of calling exit) they will |
| 4747 | * confuse this approach. |
| 4748 | * |
| 4749 | * Called with interrupts off. |
| 4750 | */ |
| 4751 | static int |
| 4752 | waitforjob(struct job *jp) |
| 4753 | { |
| 4754 | int st; |
| 4755 | |
| 4756 | TRACE(("waitforjob(%%%d) called\n", jobno(jp))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4757 | |
| 4758 | INT_OFF; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4759 | while (jp->state == JOBRUNNING) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4760 | /* In non-interactive shells, we _can_ get |
| 4761 | * a keyboard signal here and be EINTRed, |
| 4762 | * but we just loop back, waiting for command to complete. |
| 4763 | * |
| 4764 | * man bash: |
| 4765 | * "If bash is waiting for a command to complete and receives |
| 4766 | * a signal for which a trap has been set, the trap |
| 4767 | * will not be executed until the command completes." |
| 4768 | * |
| 4769 | * Reality is that even if trap is not set, bash |
| 4770 | * will not act on the signal until command completes. |
| 4771 | * Try this. sleep5intoff.c: |
| 4772 | * #include <signal.h> |
| 4773 | * #include <unistd.h> |
| 4774 | * int main() { |
| 4775 | * sigset_t set; |
| 4776 | * sigemptyset(&set); |
| 4777 | * sigaddset(&set, SIGINT); |
| 4778 | * sigaddset(&set, SIGQUIT); |
| 4779 | * sigprocmask(SIG_BLOCK, &set, NULL); |
| 4780 | * sleep(5); |
| 4781 | * return 0; |
| 4782 | * } |
| 4783 | * $ bash -c './sleep5intoff; echo hi' |
| 4784 | * ^C^C^C^C <--- pressing ^C once a second |
| 4785 | * $ _ |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4786 | * $ bash -c './sleep5intoff; echo hi' |
| 4787 | * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT) |
| 4788 | * $ _ |
| 4789 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4790 | dowait(DOWAIT_BLOCK, jp); |
| 4791 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4792 | INT_ON; |
| 4793 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4794 | st = getstatus(jp); |
| 4795 | #if JOBS |
| 4796 | if (jp->jobctl) { |
| 4797 | xtcsetpgrp(ttyfd, rootpid); |
| 4798 | /* |
| 4799 | * This is truly gross. |
| 4800 | * If we're doing job control, then we did a TIOCSPGRP which |
| 4801 | * caused us (the shell) to no longer be in the controlling |
| 4802 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 4803 | * intuit from the subprocess exit status whether a SIGINT |
| 4804 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 4805 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4806 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 4807 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4808 | } |
| 4809 | if (jp->state == JOBDONE) |
| 4810 | #endif |
| 4811 | freejob(jp); |
| 4812 | return st; |
| 4813 | } |
| 4814 | |
| 4815 | /* |
| 4816 | * return 1 if there are stopped jobs, otherwise 0 |
| 4817 | */ |
| 4818 | static int |
| 4819 | stoppedjobs(void) |
| 4820 | { |
| 4821 | struct job *jp; |
| 4822 | int retval; |
| 4823 | |
| 4824 | retval = 0; |
| 4825 | if (job_warning) |
| 4826 | goto out; |
| 4827 | jp = curjob; |
| 4828 | if (jp && jp->state == JOBSTOPPED) { |
| 4829 | out2str("You have stopped jobs.\n"); |
| 4830 | job_warning = 2; |
| 4831 | retval++; |
| 4832 | } |
| 4833 | out: |
| 4834 | return retval; |
| 4835 | } |
| 4836 | |
| 4837 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4838 | /* ============ redir.c |
| 4839 | * |
| 4840 | * Code for dealing with input/output redirection. |
| 4841 | */ |
| 4842 | |
| 4843 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4844 | #define CLOSED -3 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4845 | |
| 4846 | /* |
| 4847 | * Open a file in noclobber mode. |
| 4848 | * The code was copied from bash. |
| 4849 | */ |
| 4850 | static int |
| 4851 | noclobberopen(const char *fname) |
| 4852 | { |
| 4853 | int r, fd; |
| 4854 | struct stat finfo, finfo2; |
| 4855 | |
| 4856 | /* |
| 4857 | * If the file exists and is a regular file, return an error |
| 4858 | * immediately. |
| 4859 | */ |
| 4860 | r = stat(fname, &finfo); |
| 4861 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 4862 | errno = EEXIST; |
| 4863 | return -1; |
| 4864 | } |
| 4865 | |
| 4866 | /* |
| 4867 | * If the file was not present (r != 0), make sure we open it |
| 4868 | * exclusively so that if it is created before we open it, our open |
| 4869 | * will fail. Make sure that we do not truncate an existing file. |
| 4870 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 4871 | * file was not a regular file, we leave O_EXCL off. |
| 4872 | */ |
| 4873 | if (r != 0) |
| 4874 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 4875 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
| 4876 | |
| 4877 | /* If the open failed, return the file descriptor right away. */ |
| 4878 | if (fd < 0) |
| 4879 | return fd; |
| 4880 | |
| 4881 | /* |
| 4882 | * OK, the open succeeded, but the file may have been changed from a |
| 4883 | * non-regular file to a regular file between the stat and the open. |
| 4884 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 4885 | * did not exist and is symlinked to an existing file between the stat |
| 4886 | * and open. |
| 4887 | */ |
| 4888 | |
| 4889 | /* |
| 4890 | * If we can open it and fstat the file descriptor, and neither check |
| 4891 | * revealed that it was a regular file, and the file has not been |
| 4892 | * replaced, return the file descriptor. |
| 4893 | */ |
| 4894 | if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode) |
| 4895 | && finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino) |
| 4896 | return fd; |
| 4897 | |
| 4898 | /* The file has been replaced. badness. */ |
| 4899 | close(fd); |
| 4900 | errno = EEXIST; |
| 4901 | return -1; |
| 4902 | } |
| 4903 | |
| 4904 | /* |
| 4905 | * Handle here documents. Normally we fork off a process to write the |
| 4906 | * data to a pipe. If the document is short, we can stuff the data in |
| 4907 | * the pipe without forking. |
| 4908 | */ |
| 4909 | /* openhere needs this forward reference */ |
| 4910 | static void expandhere(union node *arg, int fd); |
| 4911 | static int |
| 4912 | openhere(union node *redir) |
| 4913 | { |
| 4914 | int pip[2]; |
| 4915 | size_t len = 0; |
| 4916 | |
| 4917 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 4918 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4919 | if (redir->type == NHERE) { |
| 4920 | len = strlen(redir->nhere.doc->narg.text); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4921 | if (len <= PIPE_BUF) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4922 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
| 4923 | goto out; |
| 4924 | } |
| 4925 | } |
| 4926 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4927 | /* child */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4928 | close(pip[0]); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4929 | ignoresig(SIGINT); //signal(SIGINT, SIG_IGN); |
| 4930 | ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN); |
| 4931 | ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN); |
| 4932 | ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4933 | signal(SIGPIPE, SIG_DFL); |
| 4934 | if (redir->type == NHERE) |
| 4935 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4936 | else /* NXHERE */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4937 | expandhere(redir->nhere.doc, pip[1]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 4938 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4939 | } |
| 4940 | out: |
| 4941 | close(pip[1]); |
| 4942 | return pip[0]; |
| 4943 | } |
| 4944 | |
| 4945 | static int |
| 4946 | openredirect(union node *redir) |
| 4947 | { |
| 4948 | char *fname; |
| 4949 | int f; |
| 4950 | |
| 4951 | switch (redir->nfile.type) { |
| 4952 | case NFROM: |
| 4953 | fname = redir->nfile.expfname; |
| 4954 | f = open(fname, O_RDONLY); |
| 4955 | if (f < 0) |
| 4956 | goto eopen; |
| 4957 | break; |
| 4958 | case NFROMTO: |
| 4959 | fname = redir->nfile.expfname; |
| 4960 | f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666); |
| 4961 | if (f < 0) |
| 4962 | goto ecreate; |
| 4963 | break; |
| 4964 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4965 | #if ENABLE_ASH_BASH_COMPAT |
| 4966 | case NTO2: |
| 4967 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4968 | /* Take care of noclobber mode. */ |
| 4969 | if (Cflag) { |
| 4970 | fname = redir->nfile.expfname; |
| 4971 | f = noclobberopen(fname); |
| 4972 | if (f < 0) |
| 4973 | goto ecreate; |
| 4974 | break; |
| 4975 | } |
| 4976 | /* FALLTHROUGH */ |
| 4977 | case NCLOBBER: |
| 4978 | fname = redir->nfile.expfname; |
| 4979 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 4980 | if (f < 0) |
| 4981 | goto ecreate; |
| 4982 | break; |
| 4983 | case NAPPEND: |
| 4984 | fname = redir->nfile.expfname; |
| 4985 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 4986 | if (f < 0) |
| 4987 | goto ecreate; |
| 4988 | break; |
| 4989 | default: |
| 4990 | #if DEBUG |
| 4991 | abort(); |
| 4992 | #endif |
| 4993 | /* Fall through to eliminate warning. */ |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 4994 | /* Our single caller does this itself */ |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4995 | // case NTOFD: |
| 4996 | // case NFROMFD: |
| 4997 | // f = -1; |
| 4998 | // break; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4999 | case NHERE: |
| 5000 | case NXHERE: |
| 5001 | f = openhere(redir); |
| 5002 | break; |
| 5003 | } |
| 5004 | |
| 5005 | return f; |
| 5006 | ecreate: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5007 | 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] | 5008 | eopen: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5009 | 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] | 5010 | } |
| 5011 | |
| 5012 | /* |
| 5013 | * Copy a file descriptor to be >= to. Returns -1 |
| 5014 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 5015 | * file descriptors left. |
| 5016 | */ |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5017 | /* 0x800..00: bit to set in "to" to request dup2 instead of fcntl(F_DUPFD). |
| 5018 | * 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] | 5019 | enum { |
| 5020 | COPYFD_EXACT = (int)~(INT_MAX), |
| 5021 | COPYFD_RESTORE = (int)((unsigned)COPYFD_EXACT >> 1), |
| 5022 | }; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5023 | static int |
| 5024 | copyfd(int from, int to) |
| 5025 | { |
| 5026 | int newfd; |
| 5027 | |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5028 | if (to & COPYFD_EXACT) { |
| 5029 | to &= ~COPYFD_EXACT; |
| 5030 | /*if (from != to)*/ |
| 5031 | newfd = dup2(from, to); |
| 5032 | } else { |
| 5033 | newfd = fcntl(from, F_DUPFD, to); |
| 5034 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5035 | if (newfd < 0) { |
| 5036 | if (errno == EMFILE) |
| 5037 | return EMPTY; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5038 | /* Happens when source fd is not open: try "echo >&99" */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5039 | ash_msg_and_raise_error("%d: %m", from); |
| 5040 | } |
| 5041 | return newfd; |
| 5042 | } |
| 5043 | |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5044 | /* Struct def and variable are moved down to the first usage site */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5045 | struct two_fd_t { |
| 5046 | int orig, copy; |
| 5047 | }; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5048 | struct redirtab { |
| 5049 | struct redirtab *next; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5050 | int nullredirs; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5051 | int pair_count; |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 5052 | struct two_fd_t two_fd[]; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5053 | }; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5054 | #define redirlist (G_var.redirlist) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5055 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5056 | static int need_to_remember(struct redirtab *rp, int fd) |
| 5057 | { |
| 5058 | int i; |
| 5059 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5060 | if (!rp) /* remembering was not requested */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5061 | return 0; |
| 5062 | |
| 5063 | for (i = 0; i < rp->pair_count; i++) { |
| 5064 | if (rp->two_fd[i].orig == fd) { |
| 5065 | /* already remembered */ |
| 5066 | return 0; |
| 5067 | } |
| 5068 | } |
| 5069 | return 1; |
| 5070 | } |
| 5071 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5072 | /* "hidden" fd is a fd used to read scripts, or a copy of such */ |
| 5073 | static int is_hidden_fd(struct redirtab *rp, int fd) |
| 5074 | { |
| 5075 | int i; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5076 | struct parsefile *pf; |
| 5077 | |
| 5078 | if (fd == -1) |
| 5079 | return 0; |
| 5080 | pf = g_parsefile; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5081 | while (pf) { |
| 5082 | if (fd == pf->fd) { |
| 5083 | return 1; |
| 5084 | } |
| 5085 | pf = pf->prev; |
| 5086 | } |
| 5087 | if (!rp) |
| 5088 | return 0; |
| 5089 | fd |= COPYFD_RESTORE; |
| 5090 | for (i = 0; i < rp->pair_count; i++) { |
| 5091 | if (rp->two_fd[i].copy == fd) { |
| 5092 | return 1; |
| 5093 | } |
| 5094 | } |
| 5095 | return 0; |
| 5096 | } |
| 5097 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5098 | /* |
| 5099 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 5100 | * old file descriptors are stashed away so that the redirection can be |
| 5101 | * undone by calling popredir. If the REDIR_BACKQ flag is set, then the |
| 5102 | * standard output, and the standard error if it becomes a duplicate of |
| 5103 | * stdout, is saved in memory. |
| 5104 | */ |
| 5105 | /* flags passed to redirect */ |
| 5106 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
| 5107 | #define REDIR_SAVEFD2 03 /* set preverrout */ |
| 5108 | static void |
| 5109 | redirect(union node *redir, int flags) |
| 5110 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5111 | struct redirtab *sv; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5112 | int sv_pos; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5113 | int i; |
| 5114 | int fd; |
| 5115 | int newfd; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5116 | int copied_fd2 = -1; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5117 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5118 | g_nullredirs++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5119 | if (!redir) { |
| 5120 | return; |
| 5121 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5122 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5123 | sv = NULL; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5124 | sv_pos = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5125 | INT_OFF; |
| 5126 | if (flags & REDIR_PUSH) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5127 | union node *tmp = redir; |
| 5128 | do { |
| 5129 | sv_pos++; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5130 | #if ENABLE_ASH_BASH_COMPAT |
| 5131 | if (redir->nfile.type == NTO2) |
| 5132 | sv_pos++; |
| 5133 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5134 | tmp = tmp->nfile.next; |
| 5135 | } while (tmp); |
| 5136 | sv = ckmalloc(sizeof(*sv) + sv_pos * sizeof(sv->two_fd[0])); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5137 | sv->next = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5138 | sv->pair_count = sv_pos; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5139 | redirlist = sv; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5140 | sv->nullredirs = g_nullredirs - 1; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5141 | g_nullredirs = 0; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5142 | while (sv_pos > 0) { |
| 5143 | sv_pos--; |
| 5144 | sv->two_fd[sv_pos].orig = sv->two_fd[sv_pos].copy = EMPTY; |
| 5145 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5146 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5147 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5148 | do { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5149 | fd = redir->nfile.fd; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5150 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5151 | int right_fd = redir->ndup.dupfd; |
| 5152 | /* redirect from/to same file descriptor? */ |
| 5153 | if (right_fd == fd) |
| 5154 | continue; |
| 5155 | /* echo >&10 and 10 is a fd opened to the sh script? */ |
| 5156 | if (is_hidden_fd(sv, right_fd)) { |
| 5157 | errno = EBADF; /* as if it is closed */ |
| 5158 | ash_msg_and_raise_error("%d: %m", right_fd); |
| 5159 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5160 | newfd = -1; |
| 5161 | } else { |
| 5162 | newfd = openredirect(redir); /* always >= 0 */ |
| 5163 | if (fd == newfd) { |
| 5164 | /* Descriptor wasn't open before redirect. |
| 5165 | * Mark it for close in the future */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5166 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5167 | goto remember_to_close; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5168 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5169 | continue; |
| 5170 | } |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5171 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5172 | #if ENABLE_ASH_BASH_COMPAT |
| 5173 | redirect_more: |
| 5174 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5175 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5176 | /* Copy old descriptor */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5177 | i = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5178 | /* You'd expect copy to be CLOEXECed. Currently these extra "saved" fds |
| 5179 | * are closed in popredir() in the child, preventing them from leaking |
| 5180 | * into child. (popredir() also cleans up the mess in case of failures) |
| 5181 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5182 | if (i == -1) { |
| 5183 | i = errno; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5184 | if (i != EBADF) { |
| 5185 | /* Strange error (e.g. "too many files" EMFILE?) */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5186 | if (newfd >= 0) |
| 5187 | close(newfd); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5188 | errno = i; |
| 5189 | ash_msg_and_raise_error("%d: %m", fd); |
| 5190 | /* NOTREACHED */ |
| 5191 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5192 | /* EBADF: it is not open - good, remember to close it */ |
| 5193 | remember_to_close: |
| 5194 | i = CLOSED; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5195 | } else { /* fd is open, save its copy */ |
| 5196 | /* "exec fd>&-" should not close fds |
| 5197 | * which point to script file(s). |
| 5198 | * Force them to be restored afterwards */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5199 | if (is_hidden_fd(sv, fd)) |
| 5200 | i |= COPYFD_RESTORE; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5201 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5202 | if (fd == 2) |
| 5203 | copied_fd2 = i; |
| 5204 | sv->two_fd[sv_pos].orig = fd; |
| 5205 | sv->two_fd[sv_pos].copy = i; |
| 5206 | sv_pos++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5207 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5208 | if (newfd < 0) { |
| 5209 | /* NTOFD/NFROMFD: copy redir->ndup.dupfd to fd */ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5210 | if (redir->ndup.dupfd < 0) { /* "fd>&-" */ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5211 | /* Don't want to trigger debugging */ |
| 5212 | if (fd != -1) |
| 5213 | close(fd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5214 | } else { |
| 5215 | copyfd(redir->ndup.dupfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5216 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5217 | } else if (fd != newfd) { /* move newfd to fd */ |
| 5218 | copyfd(newfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5219 | #if ENABLE_ASH_BASH_COMPAT |
| 5220 | if (!(redir->nfile.type == NTO2 && fd == 2)) |
| 5221 | #endif |
| 5222 | close(newfd); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5223 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5224 | #if ENABLE_ASH_BASH_COMPAT |
| 5225 | if (redir->nfile.type == NTO2 && fd == 1) { |
| 5226 | /* We already redirected it to fd 1, now copy it to 2 */ |
| 5227 | newfd = 1; |
| 5228 | fd = 2; |
| 5229 | goto redirect_more; |
| 5230 | } |
| 5231 | #endif |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5232 | } while ((redir = redir->nfile.next) != NULL); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5233 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5234 | INT_ON; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5235 | if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0) |
| 5236 | preverrout_fd = copied_fd2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | /* |
| 5240 | * Undo the effects of the last redirection. |
| 5241 | */ |
| 5242 | static void |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5243 | popredir(int drop, int restore) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5244 | { |
| 5245 | struct redirtab *rp; |
| 5246 | int i; |
| 5247 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5248 | if (--g_nullredirs >= 0) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5249 | return; |
| 5250 | INT_OFF; |
| 5251 | rp = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5252 | for (i = 0; i < rp->pair_count; i++) { |
| 5253 | int fd = rp->two_fd[i].orig; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5254 | int copy = rp->two_fd[i].copy; |
| 5255 | if (copy == CLOSED) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5256 | if (!drop) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5257 | close(fd); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5258 | continue; |
| 5259 | } |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5260 | if (copy != EMPTY) { |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5261 | if (!drop || (restore && (copy & COPYFD_RESTORE))) { |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5262 | copy &= ~COPYFD_RESTORE; |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5263 | /*close(fd);*/ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5264 | copyfd(copy, fd | COPYFD_EXACT); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5265 | } |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5266 | close(copy & ~COPYFD_RESTORE); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5267 | } |
| 5268 | } |
| 5269 | redirlist = rp->next; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5270 | g_nullredirs = rp->nullredirs; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5271 | free(rp); |
| 5272 | INT_ON; |
| 5273 | } |
| 5274 | |
| 5275 | /* |
| 5276 | * Undo all redirections. Called on error or interrupt. |
| 5277 | */ |
| 5278 | |
| 5279 | /* |
| 5280 | * Discard all saved file descriptors. |
| 5281 | */ |
| 5282 | static void |
| 5283 | clearredir(int drop) |
| 5284 | { |
| 5285 | for (;;) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5286 | g_nullredirs = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5287 | if (!redirlist) |
| 5288 | break; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5289 | popredir(drop, /*restore:*/ 0); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5290 | } |
| 5291 | } |
| 5292 | |
| 5293 | static int |
| 5294 | redirectsafe(union node *redir, int flags) |
| 5295 | { |
| 5296 | int err; |
| 5297 | volatile int saveint; |
| 5298 | struct jmploc *volatile savehandler = exception_handler; |
| 5299 | struct jmploc jmploc; |
| 5300 | |
| 5301 | SAVE_INT(saveint); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5302 | /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */ |
| 5303 | err = setjmp(jmploc.loc); // huh?? was = setjmp(jmploc.loc) * 2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5304 | if (!err) { |
| 5305 | exception_handler = &jmploc; |
| 5306 | redirect(redir, flags); |
| 5307 | } |
| 5308 | exception_handler = savehandler; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 5309 | if (err && exception_type != EXERROR) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5310 | longjmp(exception_handler->loc, 1); |
| 5311 | RESTORE_INT(saveint); |
| 5312 | return err; |
| 5313 | } |
| 5314 | |
| 5315 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5316 | /* ============ Routines to expand arguments to commands |
| 5317 | * |
| 5318 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 5319 | */ |
| 5320 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5321 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5322 | static arith_t |
| 5323 | ash_arith(const char *s) |
| 5324 | { |
| 5325 | arith_eval_hooks_t math_hooks; |
| 5326 | arith_t result; |
| 5327 | int errcode = 0; |
| 5328 | |
| 5329 | math_hooks.lookupvar = lookupvar; |
| 5330 | math_hooks.setvar = setvar; |
| 5331 | math_hooks.endofname = endofname; |
| 5332 | |
| 5333 | INT_OFF; |
| 5334 | result = arith(s, &errcode, &math_hooks); |
| 5335 | if (errcode < 0) { |
| 5336 | if (errcode == -3) |
| 5337 | ash_msg_and_raise_error("exponent less than 0"); |
| 5338 | if (errcode == -2) |
| 5339 | ash_msg_and_raise_error("divide by zero"); |
| 5340 | if (errcode == -5) |
| 5341 | ash_msg_and_raise_error("expression recursion loop detected"); |
| 5342 | raise_error_syntax(s); |
| 5343 | } |
| 5344 | INT_ON; |
| 5345 | |
| 5346 | return result; |
| 5347 | } |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5348 | #endif |
| 5349 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5350 | /* |
| 5351 | * expandarg flags |
| 5352 | */ |
| 5353 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 5354 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 5355 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 5356 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 5357 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
| 5358 | #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */ |
| 5359 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ |
| 5360 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ |
| 5361 | #define EXP_QWORD 0x100 /* expand word in quoted parameter expansion */ |
| 5362 | /* |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5363 | * rmescape() flags |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5364 | */ |
| 5365 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 5366 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
| 5367 | #define RMESCAPE_QUOTED 0x4 /* Remove CTLESC unless in quotes */ |
| 5368 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 5369 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
| 5370 | |
| 5371 | /* |
| 5372 | * Structure specifying which parts of the string should be searched |
| 5373 | * for IFS characters. |
| 5374 | */ |
| 5375 | struct ifsregion { |
| 5376 | struct ifsregion *next; /* next region in list */ |
| 5377 | int begoff; /* offset of start of region */ |
| 5378 | int endoff; /* offset of end of region */ |
| 5379 | int nulonly; /* search for nul bytes only */ |
| 5380 | }; |
| 5381 | |
| 5382 | struct arglist { |
| 5383 | struct strlist *list; |
| 5384 | struct strlist **lastp; |
| 5385 | }; |
| 5386 | |
| 5387 | /* output of current string */ |
| 5388 | static char *expdest; |
| 5389 | /* list of back quote expressions */ |
| 5390 | static struct nodelist *argbackq; |
| 5391 | /* first struct in list of ifs regions */ |
| 5392 | static struct ifsregion ifsfirst; |
| 5393 | /* last struct in list */ |
| 5394 | static struct ifsregion *ifslastp; |
| 5395 | /* holds expanded arg list */ |
| 5396 | static struct arglist exparg; |
| 5397 | |
| 5398 | /* |
| 5399 | * Our own itoa(). |
| 5400 | */ |
| 5401 | static int |
| 5402 | cvtnum(arith_t num) |
| 5403 | { |
| 5404 | int len; |
| 5405 | |
| 5406 | expdest = makestrspace(32, expdest); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5407 | len = fmtstr(expdest, 32, arith_t_fmt, num); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5408 | STADJUST(len, expdest); |
| 5409 | return len; |
| 5410 | } |
| 5411 | |
| 5412 | static size_t |
| 5413 | esclen(const char *start, const char *p) |
| 5414 | { |
| 5415 | size_t esc = 0; |
| 5416 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5417 | while (p > start && (unsigned char)*--p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5418 | esc++; |
| 5419 | } |
| 5420 | return esc; |
| 5421 | } |
| 5422 | |
| 5423 | /* |
| 5424 | * Remove any CTLESC characters from a string. |
| 5425 | */ |
| 5426 | static char * |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5427 | rmescapes(char *str, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5428 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5429 | static const char qchars[] ALIGN1 = { CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 5430 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5431 | char *p, *q, *r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5432 | unsigned inquotes; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5433 | unsigned protect_against_glob; |
| 5434 | unsigned globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5435 | |
| 5436 | p = strpbrk(str, qchars); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5437 | if (!p) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5438 | return str; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5439 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5440 | q = p; |
| 5441 | r = str; |
| 5442 | if (flag & RMESCAPE_ALLOC) { |
| 5443 | size_t len = p - str; |
| 5444 | size_t fulllen = len + strlen(p) + 1; |
| 5445 | |
| 5446 | if (flag & RMESCAPE_GROW) { |
| 5447 | r = makestrspace(fulllen, expdest); |
| 5448 | } else if (flag & RMESCAPE_HEAP) { |
| 5449 | r = ckmalloc(fulllen); |
| 5450 | } else { |
| 5451 | r = stalloc(fulllen); |
| 5452 | } |
| 5453 | q = r; |
| 5454 | if (len > 0) { |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5455 | q = (char *)memcpy(q, str, len) + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5456 | } |
| 5457 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5458 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5459 | inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED; |
| 5460 | globbing = flag & RMESCAPE_GLOB; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5461 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5462 | while (*p) { |
| 5463 | if (*p == CTLQUOTEMARK) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5464 | // TODO: if no RMESCAPE_QUOTED in flags, inquotes never becomes 0 |
| 5465 | // (alternates between RMESCAPE_QUOTED and ~RMESCAPE_QUOTED). Is it ok? |
| 5466 | // Note: both inquotes and protect_against_glob only affect whether |
| 5467 | // CTLESC,<ch> gets converted to <ch> or to \<ch> |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5468 | inquotes = ~inquotes; |
| 5469 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5470 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5471 | continue; |
| 5472 | } |
| 5473 | if (*p == '\\') { |
| 5474 | /* naked back slash */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5475 | protect_against_glob = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5476 | goto copy; |
| 5477 | } |
| 5478 | if (*p == CTLESC) { |
| 5479 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5480 | if (protect_against_glob && inquotes && *p != '/') { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5481 | *q++ = '\\'; |
| 5482 | } |
| 5483 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5484 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5485 | copy: |
| 5486 | *q++ = *p++; |
| 5487 | } |
| 5488 | *q = '\0'; |
| 5489 | if (flag & RMESCAPE_GROW) { |
| 5490 | expdest = r; |
| 5491 | STADJUST(q - r + 1, expdest); |
| 5492 | } |
| 5493 | return r; |
| 5494 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5495 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 5496 | |
| 5497 | /* |
| 5498 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 5499 | * |
| 5500 | * Returns an stalloced string. |
| 5501 | */ |
| 5502 | static char * |
| 5503 | preglob(const char *pattern, int quoted, int flag) |
| 5504 | { |
| 5505 | flag |= RMESCAPE_GLOB; |
| 5506 | if (quoted) { |
| 5507 | flag |= RMESCAPE_QUOTED; |
| 5508 | } |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5509 | return rmescapes((char *)pattern, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5510 | } |
| 5511 | |
| 5512 | /* |
| 5513 | * Put a string on the stack. |
| 5514 | */ |
| 5515 | static void |
| 5516 | memtodest(const char *p, size_t len, int syntax, int quotes) |
| 5517 | { |
| 5518 | char *q = expdest; |
| 5519 | |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5520 | q = makestrspace(quotes ? len * 2 : len, q); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5521 | |
| 5522 | while (len--) { |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 5523 | int c = signed_char2int(*p++); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5524 | if (!c) |
| 5525 | continue; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5526 | if (quotes) { |
| 5527 | int n = SIT(c, syntax); |
| 5528 | if (n == CCTL || n == CBACK) |
| 5529 | USTPUTC(CTLESC, q); |
| 5530 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5531 | USTPUTC(c, q); |
| 5532 | } |
| 5533 | |
| 5534 | expdest = q; |
| 5535 | } |
| 5536 | |
| 5537 | static void |
| 5538 | strtodest(const char *p, int syntax, int quotes) |
| 5539 | { |
| 5540 | memtodest(p, strlen(p), syntax, quotes); |
| 5541 | } |
| 5542 | |
| 5543 | /* |
| 5544 | * Record the fact that we have to scan this region of the |
| 5545 | * string for IFS characters. |
| 5546 | */ |
| 5547 | static void |
| 5548 | recordregion(int start, int end, int nulonly) |
| 5549 | { |
| 5550 | struct ifsregion *ifsp; |
| 5551 | |
| 5552 | if (ifslastp == NULL) { |
| 5553 | ifsp = &ifsfirst; |
| 5554 | } else { |
| 5555 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 5556 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 5557 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5558 | ifslastp->next = ifsp; |
| 5559 | INT_ON; |
| 5560 | } |
| 5561 | ifslastp = ifsp; |
| 5562 | ifslastp->begoff = start; |
| 5563 | ifslastp->endoff = end; |
| 5564 | ifslastp->nulonly = nulonly; |
| 5565 | } |
| 5566 | |
| 5567 | static void |
| 5568 | removerecordregions(int endoff) |
| 5569 | { |
| 5570 | if (ifslastp == NULL) |
| 5571 | return; |
| 5572 | |
| 5573 | if (ifsfirst.endoff > endoff) { |
| 5574 | while (ifsfirst.next != NULL) { |
| 5575 | struct ifsregion *ifsp; |
| 5576 | INT_OFF; |
| 5577 | ifsp = ifsfirst.next->next; |
| 5578 | free(ifsfirst.next); |
| 5579 | ifsfirst.next = ifsp; |
| 5580 | INT_ON; |
| 5581 | } |
| 5582 | if (ifsfirst.begoff > endoff) |
| 5583 | ifslastp = NULL; |
| 5584 | else { |
| 5585 | ifslastp = &ifsfirst; |
| 5586 | ifsfirst.endoff = endoff; |
| 5587 | } |
| 5588 | return; |
| 5589 | } |
| 5590 | |
| 5591 | ifslastp = &ifsfirst; |
| 5592 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
| 5593 | ifslastp=ifslastp->next; |
| 5594 | while (ifslastp->next != NULL) { |
| 5595 | struct ifsregion *ifsp; |
| 5596 | INT_OFF; |
| 5597 | ifsp = ifslastp->next->next; |
| 5598 | free(ifslastp->next); |
| 5599 | ifslastp->next = ifsp; |
| 5600 | INT_ON; |
| 5601 | } |
| 5602 | if (ifslastp->endoff > endoff) |
| 5603 | ifslastp->endoff = endoff; |
| 5604 | } |
| 5605 | |
| 5606 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5607 | exptilde(char *startp, char *p, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5608 | { |
| 5609 | char c; |
| 5610 | char *name; |
| 5611 | struct passwd *pw; |
| 5612 | const char *home; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 5613 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5614 | int startloc; |
| 5615 | |
| 5616 | name = p + 1; |
| 5617 | |
| 5618 | while ((c = *++p) != '\0') { |
| 5619 | switch (c) { |
| 5620 | case CTLESC: |
| 5621 | return startp; |
| 5622 | case CTLQUOTEMARK: |
| 5623 | return startp; |
| 5624 | case ':': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5625 | if (flags & EXP_VARTILDE) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5626 | goto done; |
| 5627 | break; |
| 5628 | case '/': |
| 5629 | case CTLENDVAR: |
| 5630 | goto done; |
| 5631 | } |
| 5632 | } |
| 5633 | done: |
| 5634 | *p = '\0'; |
| 5635 | if (*name == '\0') { |
| 5636 | home = lookupvar(homestr); |
| 5637 | } else { |
| 5638 | pw = getpwnam(name); |
| 5639 | if (pw == NULL) |
| 5640 | goto lose; |
| 5641 | home = pw->pw_dir; |
| 5642 | } |
| 5643 | if (!home || !*home) |
| 5644 | goto lose; |
| 5645 | *p = c; |
| 5646 | startloc = expdest - (char *)stackblock(); |
| 5647 | strtodest(home, SQSYNTAX, quotes); |
| 5648 | recordregion(startloc, expdest - (char *)stackblock(), 0); |
| 5649 | return p; |
| 5650 | lose: |
| 5651 | *p = c; |
| 5652 | return startp; |
| 5653 | } |
| 5654 | |
| 5655 | /* |
| 5656 | * Execute a command inside back quotes. If it's a builtin command, we |
| 5657 | * want to save its output in a block obtained from malloc. Otherwise |
| 5658 | * we fork off a subprocess and get the output of the command via a pipe. |
| 5659 | * Should be called with interrupts off. |
| 5660 | */ |
| 5661 | struct backcmd { /* result of evalbackcmd */ |
| 5662 | int fd; /* file descriptor to read from */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5663 | int nleft; /* number of chars in buffer */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5664 | char *buf; /* buffer */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5665 | struct job *jp; /* job structure for command */ |
| 5666 | }; |
| 5667 | |
| 5668 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5669 | static uint8_t back_exitstatus; /* exit status of backquoted command */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5670 | #define EV_EXIT 01 /* exit after evaluating tree */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 5671 | static void evaltree(union node *, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5672 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 5673 | static void FAST_FUNC |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5674 | evalbackcmd(union node *n, struct backcmd *result) |
| 5675 | { |
| 5676 | int saveherefd; |
| 5677 | |
| 5678 | result->fd = -1; |
| 5679 | result->buf = NULL; |
| 5680 | result->nleft = 0; |
| 5681 | result->jp = NULL; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5682 | if (n == NULL) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5683 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5684 | |
| 5685 | saveherefd = herefd; |
| 5686 | herefd = -1; |
| 5687 | |
| 5688 | { |
| 5689 | int pip[2]; |
| 5690 | struct job *jp; |
| 5691 | |
| 5692 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5693 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 5694 | jp = makejob(/*n,*/ 1); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5695 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
| 5696 | FORCE_INT_ON; |
| 5697 | close(pip[0]); |
| 5698 | if (pip[1] != 1) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5699 | /*close(1);*/ |
| 5700 | copyfd(pip[1], 1 | COPYFD_EXACT); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5701 | close(pip[1]); |
| 5702 | } |
| 5703 | eflag = 0; |
| 5704 | evaltree(n, EV_EXIT); /* actually evaltreenr... */ |
| 5705 | /* NOTREACHED */ |
| 5706 | } |
| 5707 | close(pip[1]); |
| 5708 | result->fd = pip[0]; |
| 5709 | result->jp = jp; |
| 5710 | } |
| 5711 | herefd = saveherefd; |
| 5712 | out: |
| 5713 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 5714 | result->fd, result->buf, result->nleft, result->jp)); |
| 5715 | } |
| 5716 | |
| 5717 | /* |
| 5718 | * Expand stuff in backwards quotes. |
| 5719 | */ |
| 5720 | static void |
| 5721 | expbackq(union node *cmd, int quoted, int quotes) |
| 5722 | { |
| 5723 | struct backcmd in; |
| 5724 | int i; |
| 5725 | char buf[128]; |
| 5726 | char *p; |
| 5727 | char *dest; |
| 5728 | int startloc; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5729 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5730 | struct stackmark smark; |
| 5731 | |
| 5732 | INT_OFF; |
| 5733 | setstackmark(&smark); |
| 5734 | dest = expdest; |
| 5735 | startloc = dest - (char *)stackblock(); |
| 5736 | grabstackstr(dest); |
| 5737 | evalbackcmd(cmd, &in); |
| 5738 | popstackmark(&smark); |
| 5739 | |
| 5740 | p = in.buf; |
| 5741 | i = in.nleft; |
| 5742 | if (i == 0) |
| 5743 | goto read; |
| 5744 | for (;;) { |
| 5745 | memtodest(p, i, syntax, quotes); |
| 5746 | read: |
| 5747 | if (in.fd < 0) |
| 5748 | break; |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 5749 | i = nonblock_safe_read(in.fd, buf, sizeof(buf)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5750 | TRACE(("expbackq: read returns %d\n", i)); |
| 5751 | if (i <= 0) |
| 5752 | break; |
| 5753 | p = buf; |
| 5754 | } |
| 5755 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 5756 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5757 | if (in.fd >= 0) { |
| 5758 | close(in.fd); |
| 5759 | back_exitstatus = waitforjob(in.jp); |
| 5760 | } |
| 5761 | INT_ON; |
| 5762 | |
| 5763 | /* Eat all trailing newlines */ |
| 5764 | dest = expdest; |
| 5765 | for (; dest > (char *)stackblock() && dest[-1] == '\n';) |
| 5766 | STUNPUTC(dest); |
| 5767 | expdest = dest; |
| 5768 | |
| 5769 | if (quoted == 0) |
| 5770 | recordregion(startloc, dest - (char *)stackblock(), 0); |
| 5771 | TRACE(("evalbackq: size=%d: \"%.*s\"\n", |
| 5772 | (dest - (char *)stackblock()) - startloc, |
| 5773 | (dest - (char *)stackblock()) - startloc, |
| 5774 | stackblock() + startloc)); |
| 5775 | } |
| 5776 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5777 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5778 | /* |
| 5779 | * Expand arithmetic expression. Backup to start of expression, |
| 5780 | * evaluate, place result in (backed up) result, adjust string position. |
| 5781 | */ |
| 5782 | static void |
| 5783 | expari(int quotes) |
| 5784 | { |
| 5785 | char *p, *start; |
| 5786 | int begoff; |
| 5787 | int flag; |
| 5788 | int len; |
| 5789 | |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5790 | /* ifsfree(); */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5791 | |
| 5792 | /* |
| 5793 | * This routine is slightly over-complicated for |
| 5794 | * efficiency. Next we scan backwards looking for the |
| 5795 | * start of arithmetic. |
| 5796 | */ |
| 5797 | start = stackblock(); |
| 5798 | p = expdest - 1; |
| 5799 | *p = '\0'; |
| 5800 | p--; |
| 5801 | do { |
| 5802 | int esc; |
| 5803 | |
| 5804 | while (*p != CTLARI) { |
| 5805 | p--; |
| 5806 | #if DEBUG |
| 5807 | if (p < start) { |
| 5808 | ash_msg_and_raise_error("missing CTLARI (shouldn't happen)"); |
| 5809 | } |
| 5810 | #endif |
| 5811 | } |
| 5812 | |
| 5813 | esc = esclen(start, p); |
| 5814 | if (!(esc % 2)) { |
| 5815 | break; |
| 5816 | } |
| 5817 | |
| 5818 | p -= esc + 1; |
| 5819 | } while (1); |
| 5820 | |
| 5821 | begoff = p - start; |
| 5822 | |
| 5823 | removerecordregions(begoff); |
| 5824 | |
| 5825 | flag = p[1]; |
| 5826 | |
| 5827 | expdest = p; |
| 5828 | |
| 5829 | if (quotes) |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5830 | rmescapes(p + 2, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5831 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5832 | len = cvtnum(ash_arith(p + 2)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5833 | |
| 5834 | if (flag != '"') |
| 5835 | recordregion(begoff, begoff + len, 0); |
| 5836 | } |
| 5837 | #endif |
| 5838 | |
| 5839 | /* argstr needs it */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5840 | static char *evalvar(char *p, int flags, struct strlist *var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5841 | |
| 5842 | /* |
| 5843 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 5844 | * characters to allow for further processing. Otherwise treat |
| 5845 | * $@ like $* since no splitting will be performed. |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5846 | * |
| 5847 | * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence |
| 5848 | * over shell varables. Needed for "A=a B=$A; echo $B" case - we use it |
| 5849 | * for correct expansion of "B=$A" word. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5850 | */ |
| 5851 | static void |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5852 | argstr(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5853 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5854 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5855 | '=', |
| 5856 | ':', |
| 5857 | CTLQUOTEMARK, |
| 5858 | CTLENDVAR, |
| 5859 | CTLESC, |
| 5860 | CTLVAR, |
| 5861 | CTLBACKQ, |
| 5862 | CTLBACKQ | CTLQUOTE, |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5863 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5864 | CTLENDARI, |
| 5865 | #endif |
| 5866 | 0 |
| 5867 | }; |
| 5868 | const char *reject = spclchars; |
| 5869 | int c; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5870 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */ |
| 5871 | int breakall = flags & EXP_WORD; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5872 | int inquotes; |
| 5873 | size_t length; |
| 5874 | int startloc; |
| 5875 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5876 | if (!(flags & EXP_VARTILDE)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5877 | reject += 2; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5878 | } else if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5879 | reject++; |
| 5880 | } |
| 5881 | inquotes = 0; |
| 5882 | length = 0; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5883 | if (flags & EXP_TILDE) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5884 | char *q; |
| 5885 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5886 | flags &= ~EXP_TILDE; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5887 | tilde: |
| 5888 | q = p; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5889 | if (*q == CTLESC && (flags & EXP_QWORD)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5890 | q++; |
| 5891 | if (*q == '~') |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5892 | p = exptilde(p, q, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5893 | } |
| 5894 | start: |
| 5895 | startloc = expdest - (char *)stackblock(); |
| 5896 | for (;;) { |
| 5897 | length += strcspn(p + length, reject); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5898 | c = (unsigned char) p[length]; |
| 5899 | if (c) { |
| 5900 | if (!(c & 0x80) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5901 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5902 | || c == CTLENDARI |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5903 | #endif |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5904 | ) { |
| 5905 | /* c == '=' || c == ':' || c == CTLENDARI */ |
| 5906 | length++; |
| 5907 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5908 | } |
| 5909 | if (length > 0) { |
| 5910 | int newloc; |
| 5911 | expdest = stack_nputstr(p, length, expdest); |
| 5912 | newloc = expdest - (char *)stackblock(); |
| 5913 | if (breakall && !inquotes && newloc > startloc) { |
| 5914 | recordregion(startloc, newloc, 0); |
| 5915 | } |
| 5916 | startloc = newloc; |
| 5917 | } |
| 5918 | p += length + 1; |
| 5919 | length = 0; |
| 5920 | |
| 5921 | switch (c) { |
| 5922 | case '\0': |
| 5923 | goto breakloop; |
| 5924 | case '=': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5925 | if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5926 | p--; |
| 5927 | continue; |
| 5928 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5929 | flags |= EXP_VARTILDE2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5930 | reject++; |
| 5931 | /* fall through */ |
| 5932 | case ':': |
| 5933 | /* |
| 5934 | * sort of a hack - expand tildes in variable |
| 5935 | * assignments (after the first '=' and after ':'s). |
| 5936 | */ |
| 5937 | if (*--p == '~') { |
| 5938 | goto tilde; |
| 5939 | } |
| 5940 | continue; |
| 5941 | } |
| 5942 | |
| 5943 | switch (c) { |
| 5944 | case CTLENDVAR: /* ??? */ |
| 5945 | goto breakloop; |
| 5946 | case CTLQUOTEMARK: |
| 5947 | /* "$@" syntax adherence hack */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5948 | if (!inquotes |
| 5949 | && memcmp(p, dolatstr, 4) == 0 |
| 5950 | && ( p[4] == CTLQUOTEMARK |
| 5951 | || (p[4] == CTLENDVAR && p[5] == CTLQUOTEMARK) |
| 5952 | ) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5953 | ) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5954 | p = evalvar(p + 1, flags, /* var_str_list: */ NULL) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5955 | goto start; |
| 5956 | } |
| 5957 | inquotes = !inquotes; |
| 5958 | addquote: |
| 5959 | if (quotes) { |
| 5960 | p--; |
| 5961 | length++; |
| 5962 | startloc++; |
| 5963 | } |
| 5964 | break; |
| 5965 | case CTLESC: |
| 5966 | startloc++; |
| 5967 | length++; |
| 5968 | goto addquote; |
| 5969 | case CTLVAR: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5970 | p = evalvar(p, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5971 | goto start; |
| 5972 | case CTLBACKQ: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5973 | c = '\0'; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5974 | case CTLBACKQ|CTLQUOTE: |
| 5975 | expbackq(argbackq->n, c, quotes); |
| 5976 | argbackq = argbackq->next; |
| 5977 | goto start; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5978 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5979 | case CTLENDARI: |
| 5980 | p--; |
| 5981 | expari(quotes); |
| 5982 | goto start; |
| 5983 | #endif |
| 5984 | } |
| 5985 | } |
| 5986 | breakloop: |
| 5987 | ; |
| 5988 | } |
| 5989 | |
| 5990 | static char * |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 5991 | 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] | 5992 | int zero) |
| 5993 | { |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5994 | // This commented out code was added by James Simmons <jsimmons@infradead.org> |
| 5995 | // as part of a larger change when he added support for ${var/a/b}. |
| 5996 | // However, it broke # and % operators: |
| 5997 | // |
| 5998 | //var=ababcdcd |
| 5999 | // ok bad |
| 6000 | //echo ${var#ab} abcdcd abcdcd |
| 6001 | //echo ${var##ab} abcdcd abcdcd |
| 6002 | //echo ${var#a*b} abcdcd ababcdcd (!) |
| 6003 | //echo ${var##a*b} cdcd cdcd |
| 6004 | //echo ${var#?} babcdcd ababcdcd (!) |
| 6005 | //echo ${var##?} babcdcd babcdcd |
| 6006 | //echo ${var#*} ababcdcd babcdcd (!) |
| 6007 | //echo ${var##*} |
| 6008 | //echo ${var%cd} ababcd ababcd |
| 6009 | //echo ${var%%cd} ababcd abab (!) |
| 6010 | //echo ${var%c*d} ababcd ababcd |
| 6011 | //echo ${var%%c*d} abab ababcdcd (!) |
| 6012 | //echo ${var%?} ababcdc ababcdc |
| 6013 | //echo ${var%%?} ababcdc ababcdcd (!) |
| 6014 | //echo ${var%*} ababcdcd ababcdcd |
| 6015 | //echo ${var%%*} |
| 6016 | // |
| 6017 | // Commenting it back out helped. Remove it completely if it really |
| 6018 | // is not needed. |
| 6019 | |
| 6020 | char *loc, *loc2; //, *full; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6021 | char c; |
| 6022 | |
| 6023 | loc = startp; |
| 6024 | loc2 = rmesc; |
| 6025 | do { |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6026 | int match; // = strlen(str); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6027 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6028 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6029 | c = *loc2; |
| 6030 | if (zero) { |
| 6031 | *loc2 = '\0'; |
| 6032 | s = rmesc; |
| 6033 | } |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6034 | match = pmatch(str, s); // this line was deleted |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6035 | |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6036 | // // chop off end if its '*' |
| 6037 | // full = strrchr(str, '*'); |
| 6038 | // if (full && full != str) |
| 6039 | // match--; |
| 6040 | // |
| 6041 | // // If str starts with '*' replace with s. |
| 6042 | // if ((*str == '*') && strlen(s) >= match) { |
| 6043 | // full = xstrdup(s); |
| 6044 | // strncpy(full+strlen(s)-match+1, str+1, match-1); |
| 6045 | // } else |
| 6046 | // full = xstrndup(str, match); |
| 6047 | // match = strncmp(s, full, strlen(full)); |
| 6048 | // free(full); |
| 6049 | // |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6050 | *loc2 = c; |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6051 | if (match) // if (!match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6052 | return loc; |
| 6053 | if (quotes && *loc == CTLESC) |
| 6054 | loc++; |
| 6055 | loc++; |
| 6056 | loc2++; |
| 6057 | } while (c); |
| 6058 | return 0; |
| 6059 | } |
| 6060 | |
| 6061 | static char * |
| 6062 | scanright(char *startp, char *rmesc, char *rmescend, char *str, int quotes, |
| 6063 | int zero) |
| 6064 | { |
| 6065 | int esc = 0; |
| 6066 | char *loc; |
| 6067 | char *loc2; |
| 6068 | |
| 6069 | for (loc = str - 1, loc2 = rmescend; loc >= startp; loc2--) { |
| 6070 | int match; |
| 6071 | char c = *loc2; |
| 6072 | const char *s = loc2; |
| 6073 | if (zero) { |
| 6074 | *loc2 = '\0'; |
| 6075 | s = rmesc; |
| 6076 | } |
| 6077 | match = pmatch(str, s); |
| 6078 | *loc2 = c; |
| 6079 | if (match) |
| 6080 | return loc; |
| 6081 | loc--; |
| 6082 | if (quotes) { |
| 6083 | if (--esc < 0) { |
| 6084 | esc = esclen(startp, loc); |
| 6085 | } |
| 6086 | if (esc % 2) { |
| 6087 | esc--; |
| 6088 | loc--; |
| 6089 | } |
| 6090 | } |
| 6091 | } |
| 6092 | return 0; |
| 6093 | } |
| 6094 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6095 | static void varunset(const char *, const char *, const char *, int) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6096 | static void |
| 6097 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 6098 | { |
| 6099 | const char *msg; |
| 6100 | const char *tail; |
| 6101 | |
| 6102 | tail = nullstr; |
| 6103 | msg = "parameter not set"; |
| 6104 | if (umsg) { |
| 6105 | if (*end == CTLENDVAR) { |
| 6106 | if (varflags & VSNUL) |
| 6107 | tail = " or null"; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6108 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6109 | msg = umsg; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6110 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6111 | } |
| 6112 | ash_msg_and_raise_error("%.*s: %s%s", end - var - 1, var, msg, tail); |
| 6113 | } |
| 6114 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6115 | #if ENABLE_ASH_BASH_COMPAT |
| 6116 | static char * |
| 6117 | parse_sub_pattern(char *arg, int inquotes) |
| 6118 | { |
| 6119 | char *idx, *repl = NULL; |
| 6120 | unsigned char c; |
| 6121 | |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6122 | idx = arg; |
| 6123 | while (1) { |
| 6124 | c = *arg; |
| 6125 | if (!c) |
| 6126 | break; |
| 6127 | if (c == '/') { |
| 6128 | /* Only the first '/' seen is our separator */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6129 | if (!repl) { |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6130 | repl = idx + 1; |
| 6131 | c = '\0'; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6132 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6133 | } |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6134 | *idx++ = c; |
| 6135 | if (!inquotes && c == '\\' && arg[1] == '\\') |
| 6136 | arg++; /* skip both \\, not just first one */ |
| 6137 | arg++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6138 | } |
Denis Vlasenko | 29038c0 | 2008-06-14 06:14:02 +0000 | [diff] [blame] | 6139 | *idx = c; /* NUL */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6140 | |
| 6141 | return repl; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6142 | } |
| 6143 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6144 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6145 | static const char * |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6146 | subevalvar(char *p, char *str, int strloc, int subtype, |
| 6147 | int startloc, int varflags, int quotes, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6148 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6149 | struct nodelist *saveargbackq = argbackq; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6150 | char *startp; |
| 6151 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6152 | char *rmesc, *rmescend; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6153 | IF_ASH_BASH_COMPAT(char *repl = NULL;) |
| 6154 | IF_ASH_BASH_COMPAT(char null = '\0';) |
| 6155 | IF_ASH_BASH_COMPAT(int pos, len, orig_len;) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6156 | int saveherefd = herefd; |
| 6157 | int amount, workloc, resetloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6158 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6159 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6160 | |
| 6161 | herefd = -1; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6162 | argstr(p, (subtype != VSASSIGN && subtype != VSQUESTION) ? EXP_CASE : 0, |
| 6163 | var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6164 | STPUTC('\0', expdest); |
| 6165 | herefd = saveherefd; |
| 6166 | argbackq = saveargbackq; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6167 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6168 | |
| 6169 | switch (subtype) { |
| 6170 | case VSASSIGN: |
| 6171 | setvar(str, startp, 0); |
| 6172 | amount = startp - expdest; |
| 6173 | STADJUST(amount, expdest); |
| 6174 | return startp; |
| 6175 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6176 | #if ENABLE_ASH_BASH_COMPAT |
| 6177 | case VSSUBSTR: |
| 6178 | loc = str = stackblock() + strloc; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6179 | /* Read POS in ${var:POS:LEN} */ |
| 6180 | pos = atoi(loc); /* number(loc) errors out on "1:4" */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6181 | len = str - startp - 1; |
| 6182 | |
| 6183 | /* *loc != '\0', guaranteed by parser */ |
| 6184 | if (quotes) { |
| 6185 | char *ptr; |
| 6186 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6187 | /* Adjust the length by the number of escapes */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6188 | for (ptr = startp; ptr < (str - 1); ptr++) { |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 6189 | if (*ptr == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6190 | len--; |
| 6191 | ptr++; |
| 6192 | } |
| 6193 | } |
| 6194 | } |
| 6195 | orig_len = len; |
| 6196 | |
| 6197 | if (*loc++ == ':') { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6198 | /* ${var::LEN} */ |
| 6199 | len = number(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6200 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6201 | /* Skip POS in ${var:POS:LEN} */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6202 | len = orig_len; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6203 | while (*loc && *loc != ':') { |
| 6204 | /* TODO? |
| 6205 | * bash complains on: var=qwe; echo ${var:1a:123} |
| 6206 | if (!isdigit(*loc)) |
| 6207 | ash_msg_and_raise_error(msg_illnum, str); |
| 6208 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6209 | loc++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6210 | } |
| 6211 | if (*loc++ == ':') { |
| 6212 | len = number(loc); |
| 6213 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6214 | } |
| 6215 | if (pos >= orig_len) { |
| 6216 | pos = 0; |
| 6217 | len = 0; |
| 6218 | } |
| 6219 | if (len > (orig_len - pos)) |
| 6220 | len = orig_len - pos; |
| 6221 | |
| 6222 | for (str = startp; pos; str++, pos--) { |
| 6223 | if (quotes && *str == CTLESC) |
| 6224 | str++; |
| 6225 | } |
| 6226 | for (loc = startp; len; len--) { |
| 6227 | if (quotes && *str == CTLESC) |
| 6228 | *loc++ = *str++; |
| 6229 | *loc++ = *str++; |
| 6230 | } |
| 6231 | *loc = '\0'; |
| 6232 | amount = loc - expdest; |
| 6233 | STADJUST(amount, expdest); |
| 6234 | return loc; |
| 6235 | #endif |
| 6236 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6237 | case VSQUESTION: |
| 6238 | varunset(p, str, startp, varflags); |
| 6239 | /* NOTREACHED */ |
| 6240 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6241 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6242 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6243 | /* We'll comeback here if we grow the stack while handling |
| 6244 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 6245 | * stack will need rebasing, and we'll need to remove our work |
| 6246 | * areas each time |
| 6247 | */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6248 | IF_ASH_BASH_COMPAT(restart:) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6249 | |
| 6250 | amount = expdest - ((char *)stackblock() + resetloc); |
| 6251 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6252 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6253 | |
| 6254 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6255 | rmescend = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6256 | if (quotes) { |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 6257 | rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6258 | if (rmesc != startp) { |
| 6259 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6260 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6261 | } |
| 6262 | } |
| 6263 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6264 | str = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6265 | preglob(str, varflags & VSQUOTE, 0); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6266 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6267 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6268 | #if ENABLE_ASH_BASH_COMPAT |
| 6269 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
| 6270 | char *idx, *end, *restart_detect; |
| 6271 | |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 6272 | if (!repl) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6273 | repl = parse_sub_pattern(str, varflags & VSQUOTE); |
| 6274 | if (!repl) |
| 6275 | repl = &null; |
| 6276 | } |
| 6277 | |
| 6278 | /* If there's no pattern to match, return the expansion unmolested */ |
| 6279 | if (*str == '\0') |
| 6280 | return 0; |
| 6281 | |
| 6282 | len = 0; |
| 6283 | idx = startp; |
| 6284 | end = str - 1; |
| 6285 | while (idx < end) { |
| 6286 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); |
| 6287 | if (!loc) { |
| 6288 | /* No match, advance */ |
| 6289 | restart_detect = stackblock(); |
| 6290 | STPUTC(*idx, expdest); |
| 6291 | if (quotes && *idx == CTLESC) { |
| 6292 | idx++; |
| 6293 | len++; |
| 6294 | STPUTC(*idx, expdest); |
| 6295 | } |
| 6296 | if (stackblock() != restart_detect) |
| 6297 | goto restart; |
| 6298 | idx++; |
| 6299 | len++; |
| 6300 | rmesc++; |
| 6301 | continue; |
| 6302 | } |
| 6303 | |
| 6304 | if (subtype == VSREPLACEALL) { |
| 6305 | while (idx < loc) { |
| 6306 | if (quotes && *idx == CTLESC) |
| 6307 | idx++; |
| 6308 | idx++; |
| 6309 | rmesc++; |
| 6310 | } |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6311 | } else { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6312 | idx = loc; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6313 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6314 | |
| 6315 | for (loc = repl; *loc; loc++) { |
| 6316 | restart_detect = stackblock(); |
| 6317 | STPUTC(*loc, expdest); |
| 6318 | if (stackblock() != restart_detect) |
| 6319 | goto restart; |
| 6320 | len++; |
| 6321 | } |
| 6322 | |
| 6323 | if (subtype == VSREPLACE) { |
| 6324 | while (*idx) { |
| 6325 | restart_detect = stackblock(); |
| 6326 | STPUTC(*idx, expdest); |
| 6327 | if (stackblock() != restart_detect) |
| 6328 | goto restart; |
| 6329 | len++; |
| 6330 | idx++; |
| 6331 | } |
| 6332 | break; |
| 6333 | } |
| 6334 | } |
| 6335 | |
| 6336 | /* We've put the replaced text into a buffer at workloc, now |
| 6337 | * move it to the right place and adjust the stack. |
| 6338 | */ |
| 6339 | startp = stackblock() + startloc; |
| 6340 | STPUTC('\0', expdest); |
| 6341 | memmove(startp, stackblock() + workloc, len); |
| 6342 | startp[len++] = '\0'; |
| 6343 | amount = expdest - ((char *)stackblock() + startloc + len - 1); |
| 6344 | STADJUST(-amount, expdest); |
| 6345 | return startp; |
| 6346 | } |
| 6347 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6348 | |
| 6349 | subtype -= VSTRIMRIGHT; |
| 6350 | #if DEBUG |
| 6351 | if (subtype < 0 || subtype > 7) |
| 6352 | abort(); |
| 6353 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6354 | /* zero = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX */ |
| 6355 | zero = subtype >> 1; |
| 6356 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 6357 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 6358 | |
| 6359 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 6360 | if (loc) { |
| 6361 | if (zero) { |
| 6362 | memmove(startp, loc, str - loc); |
| 6363 | loc = startp + (str - loc) - 1; |
| 6364 | } |
| 6365 | *loc = '\0'; |
| 6366 | amount = loc - expdest; |
| 6367 | STADJUST(amount, expdest); |
| 6368 | } |
| 6369 | return loc; |
| 6370 | } |
| 6371 | |
| 6372 | /* |
| 6373 | * Add the value of a specialized variable to the stack string. |
| 6374 | */ |
| 6375 | static ssize_t |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6376 | varvalue(char *name, int varflags, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6377 | { |
| 6378 | int num; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6379 | const char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6380 | int i; |
| 6381 | int sep = 0; |
| 6382 | int sepq = 0; |
| 6383 | ssize_t len = 0; |
| 6384 | char **ap; |
| 6385 | int syntax; |
| 6386 | int quoted = varflags & VSQUOTE; |
| 6387 | int subtype = varflags & VSTYPE; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6388 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6389 | |
| 6390 | if (quoted && (flags & EXP_FULL)) |
| 6391 | sep = 1 << CHAR_BIT; |
| 6392 | |
| 6393 | syntax = quoted ? DQSYNTAX : BASESYNTAX; |
| 6394 | switch (*name) { |
| 6395 | case '$': |
| 6396 | num = rootpid; |
| 6397 | goto numvar; |
| 6398 | case '?': |
| 6399 | num = exitstatus; |
| 6400 | goto numvar; |
| 6401 | case '#': |
| 6402 | num = shellparam.nparam; |
| 6403 | goto numvar; |
| 6404 | case '!': |
| 6405 | num = backgndpid; |
| 6406 | if (num == 0) |
| 6407 | return -1; |
| 6408 | numvar: |
| 6409 | len = cvtnum(num); |
| 6410 | break; |
| 6411 | case '-': |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6412 | expdest = makestrspace(NOPTS, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6413 | for (i = NOPTS - 1; i >= 0; i--) { |
| 6414 | if (optlist[i]) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6415 | USTPUTC(optletters(i), expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6416 | len++; |
| 6417 | } |
| 6418 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6419 | break; |
| 6420 | case '@': |
| 6421 | if (sep) |
| 6422 | goto param; |
| 6423 | /* fall through */ |
| 6424 | case '*': |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 6425 | sep = ifsset() ? signed_char2int(ifsval()[0]) : ' '; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6426 | if (quotes && (SIT(sep, syntax) == CCTL || SIT(sep, syntax) == CBACK)) |
| 6427 | sepq = 1; |
| 6428 | param: |
| 6429 | ap = shellparam.p; |
| 6430 | if (!ap) |
| 6431 | return -1; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6432 | while ((p = *ap++) != NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6433 | size_t partlen; |
| 6434 | |
| 6435 | partlen = strlen(p); |
| 6436 | len += partlen; |
| 6437 | |
| 6438 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6439 | memtodest(p, partlen, syntax, quotes); |
| 6440 | |
| 6441 | if (*ap && sep) { |
| 6442 | char *q; |
| 6443 | |
| 6444 | len++; |
| 6445 | if (subtype == VSPLUS || subtype == VSLENGTH) { |
| 6446 | continue; |
| 6447 | } |
| 6448 | q = expdest; |
| 6449 | if (sepq) |
| 6450 | STPUTC(CTLESC, q); |
| 6451 | STPUTC(sep, q); |
| 6452 | expdest = q; |
| 6453 | } |
| 6454 | } |
| 6455 | return len; |
| 6456 | case '0': |
| 6457 | case '1': |
| 6458 | case '2': |
| 6459 | case '3': |
| 6460 | case '4': |
| 6461 | case '5': |
| 6462 | case '6': |
| 6463 | case '7': |
| 6464 | case '8': |
| 6465 | case '9': |
Denys Vlasenko | a00329c | 2009-08-30 20:05:10 +0200 | [diff] [blame] | 6466 | num = atoi(name); /* number(name) fails on ${N#str} etc */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6467 | if (num < 0 || num > shellparam.nparam) |
| 6468 | return -1; |
| 6469 | p = num ? shellparam.p[num - 1] : arg0; |
| 6470 | goto value; |
| 6471 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6472 | /* NB: name has form "VAR=..." */ |
| 6473 | |
| 6474 | /* "A=a B=$A" case: var_str_list is a list of "A=a" strings |
| 6475 | * which should be considered before we check variables. */ |
| 6476 | if (var_str_list) { |
| 6477 | unsigned name_len = (strchrnul(name, '=') - name) + 1; |
| 6478 | p = NULL; |
| 6479 | do { |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 6480 | char *str, *eq; |
| 6481 | str = var_str_list->text; |
| 6482 | eq = strchr(str, '='); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6483 | if (!eq) /* stop at first non-assignment */ |
| 6484 | break; |
| 6485 | eq++; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 6486 | if (name_len == (unsigned)(eq - str) |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6487 | && strncmp(str, name, name_len) == 0) { |
| 6488 | p = eq; |
| 6489 | /* goto value; - WRONG! */ |
| 6490 | /* think "A=1 A=2 B=$A" */ |
| 6491 | } |
| 6492 | var_str_list = var_str_list->next; |
| 6493 | } while (var_str_list); |
| 6494 | if (p) |
| 6495 | goto value; |
| 6496 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6497 | p = lookupvar(name); |
| 6498 | value: |
| 6499 | if (!p) |
| 6500 | return -1; |
| 6501 | |
| 6502 | len = strlen(p); |
| 6503 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6504 | memtodest(p, len, syntax, quotes); |
| 6505 | return len; |
| 6506 | } |
| 6507 | |
| 6508 | if (subtype == VSPLUS || subtype == VSLENGTH) |
| 6509 | STADJUST(-len, expdest); |
| 6510 | return len; |
| 6511 | } |
| 6512 | |
| 6513 | /* |
| 6514 | * Expand a variable, and return a pointer to the next character in the |
| 6515 | * input string. |
| 6516 | */ |
| 6517 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6518 | evalvar(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6519 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6520 | char varflags; |
| 6521 | char subtype; |
| 6522 | char quoted; |
| 6523 | char easy; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6524 | char *var; |
| 6525 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6526 | int startloc; |
| 6527 | ssize_t varlen; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6528 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6529 | varflags = (unsigned char) *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6530 | subtype = varflags & VSTYPE; |
| 6531 | quoted = varflags & VSQUOTE; |
| 6532 | var = p; |
| 6533 | easy = (!quoted || (*var == '@' && shellparam.nparam)); |
| 6534 | startloc = expdest - (char *)stackblock(); |
| 6535 | p = strchr(p, '=') + 1; |
| 6536 | |
| 6537 | again: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6538 | varlen = varvalue(var, varflags, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6539 | if (varflags & VSNUL) |
| 6540 | varlen--; |
| 6541 | |
| 6542 | if (subtype == VSPLUS) { |
| 6543 | varlen = -1 - varlen; |
| 6544 | goto vsplus; |
| 6545 | } |
| 6546 | |
| 6547 | if (subtype == VSMINUS) { |
| 6548 | vsplus: |
| 6549 | if (varlen < 0) { |
| 6550 | argstr( |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6551 | p, flags | EXP_TILDE | |
| 6552 | (quoted ? EXP_QWORD : EXP_WORD), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6553 | var_str_list |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6554 | ); |
| 6555 | goto end; |
| 6556 | } |
| 6557 | if (easy) |
| 6558 | goto record; |
| 6559 | goto end; |
| 6560 | } |
| 6561 | |
| 6562 | if (subtype == VSASSIGN || subtype == VSQUESTION) { |
| 6563 | if (varlen < 0) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6564 | if (subevalvar(p, var, /* strloc: */ 0, |
| 6565 | subtype, startloc, varflags, |
| 6566 | /* quotes: */ 0, |
| 6567 | var_str_list) |
| 6568 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6569 | varflags &= ~VSNUL; |
| 6570 | /* |
| 6571 | * Remove any recorded regions beyond |
| 6572 | * start of variable |
| 6573 | */ |
| 6574 | removerecordregions(startloc); |
| 6575 | goto again; |
| 6576 | } |
| 6577 | goto end; |
| 6578 | } |
| 6579 | if (easy) |
| 6580 | goto record; |
| 6581 | goto end; |
| 6582 | } |
| 6583 | |
| 6584 | if (varlen < 0 && uflag) |
| 6585 | varunset(p, var, 0, 0); |
| 6586 | |
| 6587 | if (subtype == VSLENGTH) { |
| 6588 | cvtnum(varlen > 0 ? varlen : 0); |
| 6589 | goto record; |
| 6590 | } |
| 6591 | |
| 6592 | if (subtype == VSNORMAL) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6593 | if (easy) |
| 6594 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6595 | goto end; |
| 6596 | } |
| 6597 | |
| 6598 | #if DEBUG |
| 6599 | switch (subtype) { |
| 6600 | case VSTRIMLEFT: |
| 6601 | case VSTRIMLEFTMAX: |
| 6602 | case VSTRIMRIGHT: |
| 6603 | case VSTRIMRIGHTMAX: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6604 | #if ENABLE_ASH_BASH_COMPAT |
| 6605 | case VSSUBSTR: |
| 6606 | case VSREPLACE: |
| 6607 | case VSREPLACEALL: |
| 6608 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6609 | break; |
| 6610 | default: |
| 6611 | abort(); |
| 6612 | } |
| 6613 | #endif |
| 6614 | |
| 6615 | if (varlen >= 0) { |
| 6616 | /* |
| 6617 | * Terminate the string and start recording the pattern |
| 6618 | * right after it |
| 6619 | */ |
| 6620 | STPUTC('\0', expdest); |
| 6621 | patloc = expdest - (char *)stackblock(); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6622 | if (0 == subevalvar(p, /* str: */ NULL, patloc, subtype, |
| 6623 | startloc, varflags, |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6624 | //TODO: | EXP_REDIR too? All other such places do it too |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6625 | /* quotes: */ flags & (EXP_FULL | EXP_CASE), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6626 | var_str_list) |
| 6627 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6628 | int amount = expdest - ( |
| 6629 | (char *)stackblock() + patloc - 1 |
| 6630 | ); |
| 6631 | STADJUST(-amount, expdest); |
| 6632 | } |
| 6633 | /* Remove any recorded regions beyond start of variable */ |
| 6634 | removerecordregions(startloc); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6635 | record: |
| 6636 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6637 | } |
| 6638 | |
| 6639 | end: |
| 6640 | if (subtype != VSNORMAL) { /* skip to end of alternative */ |
| 6641 | int nesting = 1; |
| 6642 | for (;;) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6643 | char c = *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6644 | if (c == CTLESC) |
| 6645 | p++; |
| 6646 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { |
| 6647 | if (varlen >= 0) |
| 6648 | argbackq = argbackq->next; |
| 6649 | } else if (c == CTLVAR) { |
| 6650 | if ((*p++ & VSTYPE) != VSNORMAL) |
| 6651 | nesting++; |
| 6652 | } else if (c == CTLENDVAR) { |
| 6653 | if (--nesting == 0) |
| 6654 | break; |
| 6655 | } |
| 6656 | } |
| 6657 | } |
| 6658 | return p; |
| 6659 | } |
| 6660 | |
| 6661 | /* |
| 6662 | * Break the argument string into pieces based upon IFS and add the |
| 6663 | * strings to the argument list. The regions of the string to be |
| 6664 | * searched for IFS characters have been stored by recordregion. |
| 6665 | */ |
| 6666 | static void |
| 6667 | ifsbreakup(char *string, struct arglist *arglist) |
| 6668 | { |
| 6669 | struct ifsregion *ifsp; |
| 6670 | struct strlist *sp; |
| 6671 | char *start; |
| 6672 | char *p; |
| 6673 | char *q; |
| 6674 | const char *ifs, *realifs; |
| 6675 | int ifsspc; |
| 6676 | int nulonly; |
| 6677 | |
| 6678 | start = string; |
| 6679 | if (ifslastp != NULL) { |
| 6680 | ifsspc = 0; |
| 6681 | nulonly = 0; |
| 6682 | realifs = ifsset() ? ifsval() : defifs; |
| 6683 | ifsp = &ifsfirst; |
| 6684 | do { |
| 6685 | p = string + ifsp->begoff; |
| 6686 | nulonly = ifsp->nulonly; |
| 6687 | ifs = nulonly ? nullstr : realifs; |
| 6688 | ifsspc = 0; |
| 6689 | while (p < string + ifsp->endoff) { |
| 6690 | q = p; |
| 6691 | if (*p == CTLESC) |
| 6692 | p++; |
| 6693 | if (!strchr(ifs, *p)) { |
| 6694 | p++; |
| 6695 | continue; |
| 6696 | } |
| 6697 | if (!nulonly) |
| 6698 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6699 | /* Ignore IFS whitespace at start */ |
| 6700 | if (q == start && ifsspc) { |
| 6701 | p++; |
| 6702 | start = p; |
| 6703 | continue; |
| 6704 | } |
| 6705 | *q = '\0'; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6706 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6707 | sp->text = start; |
| 6708 | *arglist->lastp = sp; |
| 6709 | arglist->lastp = &sp->next; |
| 6710 | p++; |
| 6711 | if (!nulonly) { |
| 6712 | for (;;) { |
| 6713 | if (p >= string + ifsp->endoff) { |
| 6714 | break; |
| 6715 | } |
| 6716 | q = p; |
| 6717 | if (*p == CTLESC) |
| 6718 | p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6719 | if (strchr(ifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6720 | p = q; |
| 6721 | break; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6722 | } |
| 6723 | if (strchr(defifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6724 | if (ifsspc) { |
| 6725 | p++; |
| 6726 | ifsspc = 0; |
| 6727 | } else { |
| 6728 | p = q; |
| 6729 | break; |
| 6730 | } |
| 6731 | } else |
| 6732 | p++; |
| 6733 | } |
| 6734 | } |
| 6735 | start = p; |
| 6736 | } /* while */ |
| 6737 | ifsp = ifsp->next; |
| 6738 | } while (ifsp != NULL); |
| 6739 | if (nulonly) |
| 6740 | goto add; |
| 6741 | } |
| 6742 | |
| 6743 | if (!*start) |
| 6744 | return; |
| 6745 | |
| 6746 | add: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6747 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6748 | sp->text = start; |
| 6749 | *arglist->lastp = sp; |
| 6750 | arglist->lastp = &sp->next; |
| 6751 | } |
| 6752 | |
| 6753 | static void |
| 6754 | ifsfree(void) |
| 6755 | { |
| 6756 | struct ifsregion *p; |
| 6757 | |
| 6758 | INT_OFF; |
| 6759 | p = ifsfirst.next; |
| 6760 | do { |
| 6761 | struct ifsregion *ifsp; |
| 6762 | ifsp = p->next; |
| 6763 | free(p); |
| 6764 | p = ifsp; |
| 6765 | } while (p); |
| 6766 | ifslastp = NULL; |
| 6767 | ifsfirst.next = NULL; |
| 6768 | INT_ON; |
| 6769 | } |
| 6770 | |
| 6771 | /* |
| 6772 | * Add a file name to the list. |
| 6773 | */ |
| 6774 | static void |
| 6775 | addfname(const char *name) |
| 6776 | { |
| 6777 | struct strlist *sp; |
| 6778 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6779 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6780 | sp->text = ststrdup(name); |
| 6781 | *exparg.lastp = sp; |
| 6782 | exparg.lastp = &sp->next; |
| 6783 | } |
| 6784 | |
| 6785 | static char *expdir; |
| 6786 | |
| 6787 | /* |
| 6788 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 6789 | */ |
| 6790 | static void |
| 6791 | expmeta(char *enddir, char *name) |
| 6792 | { |
| 6793 | char *p; |
| 6794 | const char *cp; |
| 6795 | char *start; |
| 6796 | char *endname; |
| 6797 | int metaflag; |
| 6798 | struct stat statb; |
| 6799 | DIR *dirp; |
| 6800 | struct dirent *dp; |
| 6801 | int atend; |
| 6802 | int matchdot; |
| 6803 | |
| 6804 | metaflag = 0; |
| 6805 | start = name; |
| 6806 | for (p = name; *p; p++) { |
| 6807 | if (*p == '*' || *p == '?') |
| 6808 | metaflag = 1; |
| 6809 | else if (*p == '[') { |
| 6810 | char *q = p + 1; |
| 6811 | if (*q == '!') |
| 6812 | q++; |
| 6813 | for (;;) { |
| 6814 | if (*q == '\\') |
| 6815 | q++; |
| 6816 | if (*q == '/' || *q == '\0') |
| 6817 | break; |
| 6818 | if (*++q == ']') { |
| 6819 | metaflag = 1; |
| 6820 | break; |
| 6821 | } |
| 6822 | } |
| 6823 | } else if (*p == '\\') |
| 6824 | p++; |
| 6825 | else if (*p == '/') { |
| 6826 | if (metaflag) |
| 6827 | goto out; |
| 6828 | start = p + 1; |
| 6829 | } |
| 6830 | } |
| 6831 | out: |
| 6832 | if (metaflag == 0) { /* we've reached the end of the file name */ |
| 6833 | if (enddir != expdir) |
| 6834 | metaflag++; |
| 6835 | p = name; |
| 6836 | do { |
| 6837 | if (*p == '\\') |
| 6838 | p++; |
| 6839 | *enddir++ = *p; |
| 6840 | } while (*p++); |
| 6841 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
| 6842 | addfname(expdir); |
| 6843 | return; |
| 6844 | } |
| 6845 | endname = p; |
| 6846 | if (name < start) { |
| 6847 | p = name; |
| 6848 | do { |
| 6849 | if (*p == '\\') |
| 6850 | p++; |
| 6851 | *enddir++ = *p++; |
| 6852 | } while (p < start); |
| 6853 | } |
| 6854 | if (enddir == expdir) { |
| 6855 | cp = "."; |
| 6856 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 6857 | cp = "/"; |
| 6858 | } else { |
| 6859 | cp = expdir; |
| 6860 | enddir[-1] = '\0'; |
| 6861 | } |
| 6862 | dirp = opendir(cp); |
| 6863 | if (dirp == NULL) |
| 6864 | return; |
| 6865 | if (enddir != expdir) |
| 6866 | enddir[-1] = '/'; |
| 6867 | if (*endname == 0) { |
| 6868 | atend = 1; |
| 6869 | } else { |
| 6870 | atend = 0; |
| 6871 | *endname++ = '\0'; |
| 6872 | } |
| 6873 | matchdot = 0; |
| 6874 | p = start; |
| 6875 | if (*p == '\\') |
| 6876 | p++; |
| 6877 | if (*p == '.') |
| 6878 | matchdot++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6879 | while (!pending_int && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 6880 | if (dp->d_name[0] == '.' && !matchdot) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6881 | continue; |
| 6882 | if (pmatch(start, dp->d_name)) { |
| 6883 | if (atend) { |
| 6884 | strcpy(enddir, dp->d_name); |
| 6885 | addfname(expdir); |
| 6886 | } else { |
| 6887 | for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';) |
| 6888 | continue; |
| 6889 | p[-1] = '/'; |
| 6890 | expmeta(p, endname); |
| 6891 | } |
| 6892 | } |
| 6893 | } |
| 6894 | closedir(dirp); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 6895 | if (!atend) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6896 | endname[-1] = '/'; |
| 6897 | } |
| 6898 | |
| 6899 | static struct strlist * |
| 6900 | msort(struct strlist *list, int len) |
| 6901 | { |
| 6902 | struct strlist *p, *q = NULL; |
| 6903 | struct strlist **lpp; |
| 6904 | int half; |
| 6905 | int n; |
| 6906 | |
| 6907 | if (len <= 1) |
| 6908 | return list; |
| 6909 | half = len >> 1; |
| 6910 | p = list; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6911 | for (n = half; --n >= 0;) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6912 | q = p; |
| 6913 | p = p->next; |
| 6914 | } |
| 6915 | q->next = NULL; /* terminate first half of list */ |
| 6916 | q = msort(list, half); /* sort first half of list */ |
| 6917 | p = msort(p, len - half); /* sort second half */ |
| 6918 | lpp = &list; |
| 6919 | for (;;) { |
| 6920 | #if ENABLE_LOCALE_SUPPORT |
| 6921 | if (strcoll(p->text, q->text) < 0) |
| 6922 | #else |
| 6923 | if (strcmp(p->text, q->text) < 0) |
| 6924 | #endif |
| 6925 | { |
| 6926 | *lpp = p; |
| 6927 | lpp = &p->next; |
| 6928 | p = *lpp; |
| 6929 | if (p == NULL) { |
| 6930 | *lpp = q; |
| 6931 | break; |
| 6932 | } |
| 6933 | } else { |
| 6934 | *lpp = q; |
| 6935 | lpp = &q->next; |
| 6936 | q = *lpp; |
| 6937 | if (q == NULL) { |
| 6938 | *lpp = p; |
| 6939 | break; |
| 6940 | } |
| 6941 | } |
| 6942 | } |
| 6943 | return list; |
| 6944 | } |
| 6945 | |
| 6946 | /* |
| 6947 | * Sort the results of file name expansion. It calculates the number of |
| 6948 | * strings to sort and then calls msort (short for merge sort) to do the |
| 6949 | * work. |
| 6950 | */ |
| 6951 | static struct strlist * |
| 6952 | expsort(struct strlist *str) |
| 6953 | { |
| 6954 | int len; |
| 6955 | struct strlist *sp; |
| 6956 | |
| 6957 | len = 0; |
| 6958 | for (sp = str; sp; sp = sp->next) |
| 6959 | len++; |
| 6960 | return msort(str, len); |
| 6961 | } |
| 6962 | |
| 6963 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 6964 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6965 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 6966 | static const char metachars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6967 | '*', '?', '[', 0 |
| 6968 | }; |
| 6969 | /* TODO - EXP_REDIR */ |
| 6970 | |
| 6971 | while (str) { |
| 6972 | struct strlist **savelastp; |
| 6973 | struct strlist *sp; |
| 6974 | char *p; |
| 6975 | |
| 6976 | if (fflag) |
| 6977 | goto nometa; |
| 6978 | if (!strpbrk(str->text, metachars)) |
| 6979 | goto nometa; |
| 6980 | savelastp = exparg.lastp; |
| 6981 | |
| 6982 | INT_OFF; |
| 6983 | p = preglob(str->text, 0, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
| 6984 | { |
| 6985 | int i = strlen(str->text); |
| 6986 | expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ |
| 6987 | } |
| 6988 | |
| 6989 | expmeta(expdir, p); |
| 6990 | free(expdir); |
| 6991 | if (p != str->text) |
| 6992 | free(p); |
| 6993 | INT_ON; |
| 6994 | if (exparg.lastp == savelastp) { |
| 6995 | /* |
| 6996 | * no matches |
| 6997 | */ |
| 6998 | nometa: |
| 6999 | *exparg.lastp = str; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7000 | rmescapes(str->text, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7001 | exparg.lastp = &str->next; |
| 7002 | } else { |
| 7003 | *exparg.lastp = NULL; |
| 7004 | *savelastp = sp = expsort(*savelastp); |
| 7005 | while (sp->next != NULL) |
| 7006 | sp = sp->next; |
| 7007 | exparg.lastp = &sp->next; |
| 7008 | } |
| 7009 | str = str->next; |
| 7010 | } |
| 7011 | } |
| 7012 | |
| 7013 | /* |
| 7014 | * Perform variable substitution and command substitution on an argument, |
| 7015 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 7016 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 7017 | * here document expansion. |
| 7018 | */ |
| 7019 | static void |
| 7020 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 7021 | { |
| 7022 | struct strlist *sp; |
| 7023 | char *p; |
| 7024 | |
| 7025 | argbackq = arg->narg.backquote; |
| 7026 | STARTSTACKSTR(expdest); |
| 7027 | ifsfirst.next = NULL; |
| 7028 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7029 | argstr(arg->narg.text, flag, |
| 7030 | /* var_str_list: */ arglist ? arglist->list : NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7031 | p = _STPUTC('\0', expdest); |
| 7032 | expdest = p - 1; |
| 7033 | if (arglist == NULL) { |
| 7034 | return; /* here document expanded */ |
| 7035 | } |
| 7036 | p = grabstackstr(p); |
| 7037 | exparg.lastp = &exparg.list; |
| 7038 | /* |
| 7039 | * TODO - EXP_REDIR |
| 7040 | */ |
| 7041 | if (flag & EXP_FULL) { |
| 7042 | ifsbreakup(p, &exparg); |
| 7043 | *exparg.lastp = NULL; |
| 7044 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7045 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7046 | } else { |
| 7047 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7048 | rmescapes(p, 0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7049 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7050 | sp->text = p; |
| 7051 | *exparg.lastp = sp; |
| 7052 | exparg.lastp = &sp->next; |
| 7053 | } |
| 7054 | if (ifsfirst.next) |
| 7055 | ifsfree(); |
| 7056 | *exparg.lastp = NULL; |
| 7057 | if (exparg.list) { |
| 7058 | *arglist->lastp = exparg.list; |
| 7059 | arglist->lastp = exparg.lastp; |
| 7060 | } |
| 7061 | } |
| 7062 | |
| 7063 | /* |
| 7064 | * Expand shell variables and backquotes inside a here document. |
| 7065 | */ |
| 7066 | static void |
| 7067 | expandhere(union node *arg, int fd) |
| 7068 | { |
| 7069 | herefd = fd; |
| 7070 | expandarg(arg, (struct arglist *)NULL, 0); |
| 7071 | full_write(fd, stackblock(), expdest - (char *)stackblock()); |
| 7072 | } |
| 7073 | |
| 7074 | /* |
| 7075 | * Returns true if the pattern matches the string. |
| 7076 | */ |
| 7077 | static int |
| 7078 | patmatch(char *pattern, const char *string) |
| 7079 | { |
| 7080 | return pmatch(preglob(pattern, 0, 0), string); |
| 7081 | } |
| 7082 | |
| 7083 | /* |
| 7084 | * See if a pattern matches in a case statement. |
| 7085 | */ |
| 7086 | static int |
| 7087 | casematch(union node *pattern, char *val) |
| 7088 | { |
| 7089 | struct stackmark smark; |
| 7090 | int result; |
| 7091 | |
| 7092 | setstackmark(&smark); |
| 7093 | argbackq = pattern->narg.backquote; |
| 7094 | STARTSTACKSTR(expdest); |
| 7095 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7096 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE, |
| 7097 | /* var_str_list: */ NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7098 | STACKSTRNUL(expdest); |
| 7099 | result = patmatch(stackblock(), val); |
| 7100 | popstackmark(&smark); |
| 7101 | return result; |
| 7102 | } |
| 7103 | |
| 7104 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7105 | /* ============ find_command */ |
| 7106 | |
| 7107 | struct builtincmd { |
| 7108 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7109 | int (*builtin)(int, char **) FAST_FUNC; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7110 | /* unsigned flags; */ |
| 7111 | }; |
| 7112 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 7113 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7114 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7115 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7116 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7117 | |
| 7118 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7119 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7120 | union param { |
| 7121 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7122 | /* index >= 0 for commands without path (slashes) */ |
| 7123 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 7124 | /* index == -1 for commands with slashes */ |
| 7125 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7126 | const struct builtincmd *cmd; |
| 7127 | struct funcnode *func; |
| 7128 | } u; |
| 7129 | }; |
| 7130 | /* values of cmdtype */ |
| 7131 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 7132 | #define CMDNORMAL 0 /* command is an executable program */ |
| 7133 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 7134 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 7135 | |
| 7136 | /* action to find_command() */ |
| 7137 | #define DO_ERR 0x01 /* prints errors */ |
| 7138 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 7139 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 7140 | #define DO_ALTPATH 0x08 /* using alternate path */ |
| 7141 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ |
| 7142 | |
| 7143 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 7144 | |
| 7145 | |
| 7146 | /* ============ Hashing commands */ |
| 7147 | |
| 7148 | /* |
| 7149 | * When commands are first encountered, they are entered in a hash table. |
| 7150 | * This ensures that a full path search will not have to be done for them |
| 7151 | * on each invocation. |
| 7152 | * |
| 7153 | * We should investigate converting to a linear search, even though that |
| 7154 | * would make the command name "hash" a misnomer. |
| 7155 | */ |
| 7156 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7157 | struct tblentry { |
| 7158 | struct tblentry *next; /* next entry in hash chain */ |
| 7159 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7160 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7161 | char rehash; /* if set, cd done since entry created */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7162 | char cmdname[1]; /* name of command */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7163 | }; |
| 7164 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 7165 | static struct tblentry **cmdtable; |
| 7166 | #define INIT_G_cmdtable() do { \ |
| 7167 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 7168 | } while (0) |
| 7169 | |
| 7170 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 7171 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7172 | |
| 7173 | static void |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7174 | 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] | 7175 | { |
| 7176 | int repeated = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7177 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7178 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7179 | if (applet_no >= 0) { |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7180 | if (APPLET_IS_NOEXEC(applet_no)) { |
| 7181 | while (*envp) |
| 7182 | putenv(*envp++); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7183 | run_applet_no_and_exit(applet_no, argv); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7184 | } |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7185 | /* re-exec ourselves with the new arguments */ |
| 7186 | execve(bb_busybox_exec_path, argv, envp); |
| 7187 | /* If they called chroot or otherwise made the binary no longer |
| 7188 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7189 | } |
| 7190 | #endif |
| 7191 | |
| 7192 | repeat: |
| 7193 | #ifdef SYSV |
| 7194 | do { |
| 7195 | execve(cmd, argv, envp); |
| 7196 | } while (errno == EINTR); |
| 7197 | #else |
| 7198 | execve(cmd, argv, envp); |
| 7199 | #endif |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7200 | if (repeated) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7201 | free(argv); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7202 | return; |
| 7203 | } |
| 7204 | if (errno == ENOEXEC) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7205 | char **ap; |
| 7206 | char **new; |
| 7207 | |
| 7208 | for (ap = argv; *ap; ap++) |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7209 | continue; |
| 7210 | ap = new = ckmalloc((ap - argv + 2) * sizeof(ap[0])); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7211 | ap[1] = cmd; |
Denis Vlasenko | c44ab01 | 2007-04-09 03:11:58 +0000 | [diff] [blame] | 7212 | ap[0] = cmd = (char *)DEFAULT_SHELL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7213 | ap += 2; |
| 7214 | argv++; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7215 | while ((*ap++ = *argv++) != NULL) |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7216 | continue; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7217 | argv = new; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7218 | repeated++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7219 | goto repeat; |
| 7220 | } |
| 7221 | } |
| 7222 | |
| 7223 | /* |
| 7224 | * Exec a program. Never returns. If you change this routine, you may |
| 7225 | * have to change the find_command routine as well. |
| 7226 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7227 | static void shellexec(char **, const char *, int) NORETURN; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7228 | static void |
| 7229 | shellexec(char **argv, const char *path, int idx) |
| 7230 | { |
| 7231 | char *cmdname; |
| 7232 | int e; |
| 7233 | char **envp; |
| 7234 | int exerrno; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7235 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7236 | int applet_no = -1; |
| 7237 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7238 | |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 7239 | clearredir(/*drop:*/ 1); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7240 | envp = listvars(VEXPORT, VUNSET, 0); |
| 7241 | if (strchr(argv[0], '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7242 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7243 | || (applet_no = find_applet_by_name(argv[0])) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7244 | #endif |
| 7245 | ) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7246 | tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7247 | e = errno; |
| 7248 | } else { |
| 7249 | e = ENOENT; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7250 | while ((cmdname = path_advance(&path, argv[0])) != NULL) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7251 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7252 | tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7253 | if (errno != ENOENT && errno != ENOTDIR) |
| 7254 | e = errno; |
| 7255 | } |
| 7256 | stunalloc(cmdname); |
| 7257 | } |
| 7258 | } |
| 7259 | |
| 7260 | /* Map to POSIX errors */ |
| 7261 | switch (e) { |
| 7262 | case EACCES: |
| 7263 | exerrno = 126; |
| 7264 | break; |
| 7265 | case ENOENT: |
| 7266 | exerrno = 127; |
| 7267 | break; |
| 7268 | default: |
| 7269 | exerrno = 2; |
| 7270 | break; |
| 7271 | } |
| 7272 | exitstatus = exerrno; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7273 | TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n", |
| 7274 | argv[0], e, suppress_int)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7275 | ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found")); |
| 7276 | /* NOTREACHED */ |
| 7277 | } |
| 7278 | |
| 7279 | static void |
| 7280 | printentry(struct tblentry *cmdp) |
| 7281 | { |
| 7282 | int idx; |
| 7283 | const char *path; |
| 7284 | char *name; |
| 7285 | |
| 7286 | idx = cmdp->param.index; |
| 7287 | path = pathval(); |
| 7288 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7289 | name = path_advance(&path, cmdp->cmdname); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7290 | stunalloc(name); |
| 7291 | } while (--idx >= 0); |
| 7292 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 7293 | } |
| 7294 | |
| 7295 | /* |
| 7296 | * Clear out command entries. The argument specifies the first entry in |
| 7297 | * PATH which has changed. |
| 7298 | */ |
| 7299 | static void |
| 7300 | clearcmdentry(int firstchange) |
| 7301 | { |
| 7302 | struct tblentry **tblp; |
| 7303 | struct tblentry **pp; |
| 7304 | struct tblentry *cmdp; |
| 7305 | |
| 7306 | INT_OFF; |
| 7307 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 7308 | pp = tblp; |
| 7309 | while ((cmdp = *pp) != NULL) { |
| 7310 | if ((cmdp->cmdtype == CMDNORMAL && |
| 7311 | cmdp->param.index >= firstchange) |
| 7312 | || (cmdp->cmdtype == CMDBUILTIN && |
| 7313 | builtinloc >= firstchange) |
| 7314 | ) { |
| 7315 | *pp = cmdp->next; |
| 7316 | free(cmdp); |
| 7317 | } else { |
| 7318 | pp = &cmdp->next; |
| 7319 | } |
| 7320 | } |
| 7321 | } |
| 7322 | INT_ON; |
| 7323 | } |
| 7324 | |
| 7325 | /* |
| 7326 | * Locate a command in the command hash table. If "add" is nonzero, |
| 7327 | * add the command to the table if it is not already present. The |
| 7328 | * variable "lastcmdentry" is set to point to the address of the link |
| 7329 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 7330 | * entry. |
| 7331 | * |
| 7332 | * Interrupts must be off if called with add != 0. |
| 7333 | */ |
| 7334 | static struct tblentry **lastcmdentry; |
| 7335 | |
| 7336 | static struct tblentry * |
| 7337 | cmdlookup(const char *name, int add) |
| 7338 | { |
| 7339 | unsigned int hashval; |
| 7340 | const char *p; |
| 7341 | struct tblentry *cmdp; |
| 7342 | struct tblentry **pp; |
| 7343 | |
| 7344 | p = name; |
| 7345 | hashval = (unsigned char)*p << 4; |
| 7346 | while (*p) |
| 7347 | hashval += (unsigned char)*p++; |
| 7348 | hashval &= 0x7FFF; |
| 7349 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 7350 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7351 | if (strcmp(cmdp->cmdname, name) == 0) |
| 7352 | break; |
| 7353 | pp = &cmdp->next; |
| 7354 | } |
| 7355 | if (add && cmdp == NULL) { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7356 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) |
| 7357 | + strlen(name) |
| 7358 | /* + 1 - already done because |
| 7359 | * tblentry::cmdname is char[1] */); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7360 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7361 | cmdp->cmdtype = CMDUNKNOWN; |
| 7362 | strcpy(cmdp->cmdname, name); |
| 7363 | } |
| 7364 | lastcmdentry = pp; |
| 7365 | return cmdp; |
| 7366 | } |
| 7367 | |
| 7368 | /* |
| 7369 | * Delete the command entry returned on the last lookup. |
| 7370 | */ |
| 7371 | static void |
| 7372 | delete_cmd_entry(void) |
| 7373 | { |
| 7374 | struct tblentry *cmdp; |
| 7375 | |
| 7376 | INT_OFF; |
| 7377 | cmdp = *lastcmdentry; |
| 7378 | *lastcmdentry = cmdp->next; |
| 7379 | if (cmdp->cmdtype == CMDFUNCTION) |
| 7380 | freefunc(cmdp->param.func); |
| 7381 | free(cmdp); |
| 7382 | INT_ON; |
| 7383 | } |
| 7384 | |
| 7385 | /* |
| 7386 | * Add a new command entry, replacing any existing command entry for |
| 7387 | * the same name - except special builtins. |
| 7388 | */ |
| 7389 | static void |
| 7390 | addcmdentry(char *name, struct cmdentry *entry) |
| 7391 | { |
| 7392 | struct tblentry *cmdp; |
| 7393 | |
| 7394 | cmdp = cmdlookup(name, 1); |
| 7395 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 7396 | freefunc(cmdp->param.func); |
| 7397 | } |
| 7398 | cmdp->cmdtype = entry->cmdtype; |
| 7399 | cmdp->param = entry->u; |
| 7400 | cmdp->rehash = 0; |
| 7401 | } |
| 7402 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7403 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7404 | hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7405 | { |
| 7406 | struct tblentry **pp; |
| 7407 | struct tblentry *cmdp; |
| 7408 | int c; |
| 7409 | struct cmdentry entry; |
| 7410 | char *name; |
| 7411 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7412 | if (nextopt("r") != '\0') { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7413 | clearcmdentry(0); |
| 7414 | return 0; |
| 7415 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7416 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7417 | if (*argptr == NULL) { |
| 7418 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7419 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7420 | if (cmdp->cmdtype == CMDNORMAL) |
| 7421 | printentry(cmdp); |
| 7422 | } |
| 7423 | } |
| 7424 | return 0; |
| 7425 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7426 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7427 | c = 0; |
| 7428 | while ((name = *argptr) != NULL) { |
| 7429 | cmdp = cmdlookup(name, 0); |
| 7430 | if (cmdp != NULL |
| 7431 | && (cmdp->cmdtype == CMDNORMAL |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7432 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)) |
| 7433 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7434 | delete_cmd_entry(); |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7435 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7436 | find_command(name, &entry, DO_ERR, pathval()); |
| 7437 | if (entry.cmdtype == CMDUNKNOWN) |
| 7438 | c = 1; |
| 7439 | argptr++; |
| 7440 | } |
| 7441 | return c; |
| 7442 | } |
| 7443 | |
| 7444 | /* |
| 7445 | * Called when a cd is done. Marks all commands so the next time they |
| 7446 | * are executed they will be rehashed. |
| 7447 | */ |
| 7448 | static void |
| 7449 | hashcd(void) |
| 7450 | { |
| 7451 | struct tblentry **pp; |
| 7452 | struct tblentry *cmdp; |
| 7453 | |
| 7454 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7455 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7456 | if (cmdp->cmdtype == CMDNORMAL |
| 7457 | || (cmdp->cmdtype == CMDBUILTIN |
| 7458 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 7459 | && builtinloc > 0) |
| 7460 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7461 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7462 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7463 | } |
| 7464 | } |
| 7465 | } |
| 7466 | |
| 7467 | /* |
| 7468 | * Fix command hash table when PATH changed. |
| 7469 | * Called before PATH is changed. The argument is the new value of PATH; |
| 7470 | * pathval() still returns the old value at this point. |
| 7471 | * Called with interrupts off. |
| 7472 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7473 | static void FAST_FUNC |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7474 | changepath(const char *new) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7475 | { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7476 | const char *old; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7477 | int firstchange; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7478 | int idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7479 | int idx_bltin; |
| 7480 | |
| 7481 | old = pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7482 | firstchange = 9999; /* assume no change */ |
| 7483 | idx = 0; |
| 7484 | idx_bltin = -1; |
| 7485 | for (;;) { |
| 7486 | if (*old != *new) { |
| 7487 | firstchange = idx; |
| 7488 | if ((*old == '\0' && *new == ':') |
| 7489 | || (*old == ':' && *new == '\0')) |
| 7490 | firstchange++; |
| 7491 | old = new; /* ignore subsequent differences */ |
| 7492 | } |
| 7493 | if (*new == '\0') |
| 7494 | break; |
| 7495 | if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin")) |
| 7496 | idx_bltin = idx; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7497 | if (*new == ':') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7498 | idx++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7499 | new++, old++; |
| 7500 | } |
| 7501 | if (builtinloc < 0 && idx_bltin >= 0) |
| 7502 | builtinloc = idx_bltin; /* zap builtins */ |
| 7503 | if (builtinloc >= 0 && idx_bltin < 0) |
| 7504 | firstchange = 0; |
| 7505 | clearcmdentry(firstchange); |
| 7506 | builtinloc = idx_bltin; |
| 7507 | } |
| 7508 | |
| 7509 | #define TEOF 0 |
| 7510 | #define TNL 1 |
| 7511 | #define TREDIR 2 |
| 7512 | #define TWORD 3 |
| 7513 | #define TSEMI 4 |
| 7514 | #define TBACKGND 5 |
| 7515 | #define TAND 6 |
| 7516 | #define TOR 7 |
| 7517 | #define TPIPE 8 |
| 7518 | #define TLP 9 |
| 7519 | #define TRP 10 |
| 7520 | #define TENDCASE 11 |
| 7521 | #define TENDBQUOTE 12 |
| 7522 | #define TNOT 13 |
| 7523 | #define TCASE 14 |
| 7524 | #define TDO 15 |
| 7525 | #define TDONE 16 |
| 7526 | #define TELIF 17 |
| 7527 | #define TELSE 18 |
| 7528 | #define TESAC 19 |
| 7529 | #define TFI 20 |
| 7530 | #define TFOR 21 |
| 7531 | #define TIF 22 |
| 7532 | #define TIN 23 |
| 7533 | #define TTHEN 24 |
| 7534 | #define TUNTIL 25 |
| 7535 | #define TWHILE 26 |
| 7536 | #define TBEGIN 27 |
| 7537 | #define TEND 28 |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7538 | typedef smallint token_id_t; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7539 | |
| 7540 | /* first char is indicating which tokens mark the end of a list */ |
| 7541 | static const char *const tokname_array[] = { |
| 7542 | "\1end of file", |
| 7543 | "\0newline", |
| 7544 | "\0redirection", |
| 7545 | "\0word", |
| 7546 | "\0;", |
| 7547 | "\0&", |
| 7548 | "\0&&", |
| 7549 | "\0||", |
| 7550 | "\0|", |
| 7551 | "\0(", |
| 7552 | "\1)", |
| 7553 | "\1;;", |
| 7554 | "\1`", |
| 7555 | #define KWDOFFSET 13 |
| 7556 | /* the following are keywords */ |
| 7557 | "\0!", |
| 7558 | "\0case", |
| 7559 | "\1do", |
| 7560 | "\1done", |
| 7561 | "\1elif", |
| 7562 | "\1else", |
| 7563 | "\1esac", |
| 7564 | "\1fi", |
| 7565 | "\0for", |
| 7566 | "\0if", |
| 7567 | "\0in", |
| 7568 | "\1then", |
| 7569 | "\0until", |
| 7570 | "\0while", |
| 7571 | "\0{", |
| 7572 | "\1}", |
| 7573 | }; |
| 7574 | |
| 7575 | static const char * |
| 7576 | tokname(int tok) |
| 7577 | { |
| 7578 | static char buf[16]; |
| 7579 | |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7580 | //try this: |
| 7581 | //if (tok < TSEMI) return tokname_array[tok] + 1; |
| 7582 | //sprintf(buf, "\"%s\"", tokname_array[tok] + 1); |
| 7583 | //return buf; |
| 7584 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7585 | if (tok >= TSEMI) |
| 7586 | buf[0] = '"'; |
| 7587 | sprintf(buf + (tok >= TSEMI), "%s%c", |
| 7588 | tokname_array[tok] + 1, (tok >= TSEMI ? '"' : 0)); |
| 7589 | return buf; |
| 7590 | } |
| 7591 | |
| 7592 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 7593 | static int |
| 7594 | pstrcmp(const void *a, const void *b) |
| 7595 | { |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7596 | return strcmp((char*) a, (*(char**) b) + 1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7597 | } |
| 7598 | |
| 7599 | static const char *const * |
| 7600 | findkwd(const char *s) |
| 7601 | { |
| 7602 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 7603 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 7604 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7605 | } |
| 7606 | |
| 7607 | /* |
| 7608 | * Locate and print what a word is... |
| 7609 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7610 | static int |
| 7611 | describe_command(char *command, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7612 | { |
| 7613 | struct cmdentry entry; |
| 7614 | struct tblentry *cmdp; |
| 7615 | #if ENABLE_ASH_ALIAS |
| 7616 | const struct alias *ap; |
| 7617 | #endif |
| 7618 | const char *path = pathval(); |
| 7619 | |
| 7620 | if (describe_command_verbose) { |
| 7621 | out1str(command); |
| 7622 | } |
| 7623 | |
| 7624 | /* First look at the keywords */ |
| 7625 | if (findkwd(command)) { |
| 7626 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 7627 | goto out; |
| 7628 | } |
| 7629 | |
| 7630 | #if ENABLE_ASH_ALIAS |
| 7631 | /* Then look at the aliases */ |
| 7632 | ap = lookupalias(command, 0); |
| 7633 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7634 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7635 | out1str("alias "); |
| 7636 | printalias(ap); |
| 7637 | return 0; |
| 7638 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7639 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7640 | goto out; |
| 7641 | } |
| 7642 | #endif |
| 7643 | /* Then check if it is a tracked alias */ |
| 7644 | cmdp = cmdlookup(command, 0); |
| 7645 | if (cmdp != NULL) { |
| 7646 | entry.cmdtype = cmdp->cmdtype; |
| 7647 | entry.u = cmdp->param; |
| 7648 | } else { |
| 7649 | /* Finally use brute force */ |
| 7650 | find_command(command, &entry, DO_ABS, path); |
| 7651 | } |
| 7652 | |
| 7653 | switch (entry.cmdtype) { |
| 7654 | case CMDNORMAL: { |
| 7655 | int j = entry.u.index; |
| 7656 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7657 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7658 | p = command; |
| 7659 | } else { |
| 7660 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7661 | p = path_advance(&path, command); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7662 | stunalloc(p); |
| 7663 | } while (--j >= 0); |
| 7664 | } |
| 7665 | if (describe_command_verbose) { |
| 7666 | out1fmt(" is%s %s", |
| 7667 | (cmdp ? " a tracked alias for" : nullstr), p |
| 7668 | ); |
| 7669 | } else { |
| 7670 | out1str(p); |
| 7671 | } |
| 7672 | break; |
| 7673 | } |
| 7674 | |
| 7675 | case CMDFUNCTION: |
| 7676 | if (describe_command_verbose) { |
| 7677 | out1str(" is a shell function"); |
| 7678 | } else { |
| 7679 | out1str(command); |
| 7680 | } |
| 7681 | break; |
| 7682 | |
| 7683 | case CMDBUILTIN: |
| 7684 | if (describe_command_verbose) { |
| 7685 | out1fmt(" is a %sshell builtin", |
| 7686 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 7687 | "special " : nullstr |
| 7688 | ); |
| 7689 | } else { |
| 7690 | out1str(command); |
| 7691 | } |
| 7692 | break; |
| 7693 | |
| 7694 | default: |
| 7695 | if (describe_command_verbose) { |
| 7696 | out1str(": not found\n"); |
| 7697 | } |
| 7698 | return 127; |
| 7699 | } |
| 7700 | out: |
| 7701 | outstr("\n", stdout); |
| 7702 | return 0; |
| 7703 | } |
| 7704 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7705 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7706 | typecmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7707 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7708 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7709 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7710 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7711 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7712 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 7713 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7714 | i++; |
| 7715 | verbose = 0; |
| 7716 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7717 | while (argv[i]) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7718 | err |= describe_command(argv[i++], verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7719 | } |
| 7720 | return err; |
| 7721 | } |
| 7722 | |
| 7723 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7724 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7725 | commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7726 | { |
| 7727 | int c; |
| 7728 | enum { |
| 7729 | VERIFY_BRIEF = 1, |
| 7730 | VERIFY_VERBOSE = 2, |
| 7731 | } verify = 0; |
| 7732 | |
| 7733 | while ((c = nextopt("pvV")) != '\0') |
| 7734 | if (c == 'V') |
| 7735 | verify |= VERIFY_VERBOSE; |
| 7736 | else if (c == 'v') |
| 7737 | verify |= VERIFY_BRIEF; |
| 7738 | #if DEBUG |
| 7739 | else if (c != 'p') |
| 7740 | abort(); |
| 7741 | #endif |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7742 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
| 7743 | if (verify && (*argptr != NULL)) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7744 | return describe_command(*argptr, verify - VERIFY_BRIEF); |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7745 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7746 | |
| 7747 | return 0; |
| 7748 | } |
| 7749 | #endif |
| 7750 | |
| 7751 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7752 | /* ============ eval.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7753 | |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7754 | static int funcblocksize; /* size of structures in function */ |
| 7755 | static int funcstringsize; /* size of strings in node */ |
| 7756 | static void *funcblock; /* block to allocate function from */ |
| 7757 | static char *funcstring; /* block to allocate strings from */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7758 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7759 | /* flags in argument to evaltree */ |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7760 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 7761 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7762 | #define EV_BACKCMD 04 /* command executing within back quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7763 | |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7764 | static const short nodesize[N_NUMBER] = { |
| 7765 | [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), |
| 7766 | [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), |
| 7767 | [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7768 | [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7769 | [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7770 | [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7771 | [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7772 | [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7773 | [NIF ] = SHELL_ALIGN(sizeof(struct nif)), |
| 7774 | [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7775 | [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7776 | [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)), |
| 7777 | [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)), |
| 7778 | [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)), |
| 7779 | [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7780 | [NARG ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7781 | [NTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7782 | #if ENABLE_ASH_BASH_COMPAT |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7783 | [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7784 | #endif |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7785 | [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7786 | [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7787 | [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7788 | [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7789 | [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7790 | [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7791 | [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7792 | [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7793 | [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7794 | }; |
| 7795 | |
| 7796 | static void calcsize(union node *n); |
| 7797 | |
| 7798 | static void |
| 7799 | sizenodelist(struct nodelist *lp) |
| 7800 | { |
| 7801 | while (lp) { |
| 7802 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
| 7803 | calcsize(lp->n); |
| 7804 | lp = lp->next; |
| 7805 | } |
| 7806 | } |
| 7807 | |
| 7808 | static void |
| 7809 | calcsize(union node *n) |
| 7810 | { |
| 7811 | if (n == NULL) |
| 7812 | return; |
| 7813 | funcblocksize += nodesize[n->type]; |
| 7814 | switch (n->type) { |
| 7815 | case NCMD: |
| 7816 | calcsize(n->ncmd.redirect); |
| 7817 | calcsize(n->ncmd.args); |
| 7818 | calcsize(n->ncmd.assign); |
| 7819 | break; |
| 7820 | case NPIPE: |
| 7821 | sizenodelist(n->npipe.cmdlist); |
| 7822 | break; |
| 7823 | case NREDIR: |
| 7824 | case NBACKGND: |
| 7825 | case NSUBSHELL: |
| 7826 | calcsize(n->nredir.redirect); |
| 7827 | calcsize(n->nredir.n); |
| 7828 | break; |
| 7829 | case NAND: |
| 7830 | case NOR: |
| 7831 | case NSEMI: |
| 7832 | case NWHILE: |
| 7833 | case NUNTIL: |
| 7834 | calcsize(n->nbinary.ch2); |
| 7835 | calcsize(n->nbinary.ch1); |
| 7836 | break; |
| 7837 | case NIF: |
| 7838 | calcsize(n->nif.elsepart); |
| 7839 | calcsize(n->nif.ifpart); |
| 7840 | calcsize(n->nif.test); |
| 7841 | break; |
| 7842 | case NFOR: |
| 7843 | funcstringsize += strlen(n->nfor.var) + 1; |
| 7844 | calcsize(n->nfor.body); |
| 7845 | calcsize(n->nfor.args); |
| 7846 | break; |
| 7847 | case NCASE: |
| 7848 | calcsize(n->ncase.cases); |
| 7849 | calcsize(n->ncase.expr); |
| 7850 | break; |
| 7851 | case NCLIST: |
| 7852 | calcsize(n->nclist.body); |
| 7853 | calcsize(n->nclist.pattern); |
| 7854 | calcsize(n->nclist.next); |
| 7855 | break; |
| 7856 | case NDEFUN: |
| 7857 | case NARG: |
| 7858 | sizenodelist(n->narg.backquote); |
| 7859 | funcstringsize += strlen(n->narg.text) + 1; |
| 7860 | calcsize(n->narg.next); |
| 7861 | break; |
| 7862 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 7863 | #if ENABLE_ASH_BASH_COMPAT |
| 7864 | case NTO2: |
| 7865 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7866 | case NCLOBBER: |
| 7867 | case NFROM: |
| 7868 | case NFROMTO: |
| 7869 | case NAPPEND: |
| 7870 | calcsize(n->nfile.fname); |
| 7871 | calcsize(n->nfile.next); |
| 7872 | break; |
| 7873 | case NTOFD: |
| 7874 | case NFROMFD: |
| 7875 | calcsize(n->ndup.vname); |
| 7876 | calcsize(n->ndup.next); |
| 7877 | break; |
| 7878 | case NHERE: |
| 7879 | case NXHERE: |
| 7880 | calcsize(n->nhere.doc); |
| 7881 | calcsize(n->nhere.next); |
| 7882 | break; |
| 7883 | case NNOT: |
| 7884 | calcsize(n->nnot.com); |
| 7885 | break; |
| 7886 | }; |
| 7887 | } |
| 7888 | |
| 7889 | static char * |
| 7890 | nodeckstrdup(char *s) |
| 7891 | { |
| 7892 | char *rtn = funcstring; |
| 7893 | |
| 7894 | strcpy(funcstring, s); |
| 7895 | funcstring += strlen(s) + 1; |
| 7896 | return rtn; |
| 7897 | } |
| 7898 | |
| 7899 | static union node *copynode(union node *); |
| 7900 | |
| 7901 | static struct nodelist * |
| 7902 | copynodelist(struct nodelist *lp) |
| 7903 | { |
| 7904 | struct nodelist *start; |
| 7905 | struct nodelist **lpp; |
| 7906 | |
| 7907 | lpp = &start; |
| 7908 | while (lp) { |
| 7909 | *lpp = funcblock; |
| 7910 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 7911 | (*lpp)->n = copynode(lp->n); |
| 7912 | lp = lp->next; |
| 7913 | lpp = &(*lpp)->next; |
| 7914 | } |
| 7915 | *lpp = NULL; |
| 7916 | return start; |
| 7917 | } |
| 7918 | |
| 7919 | static union node * |
| 7920 | copynode(union node *n) |
| 7921 | { |
| 7922 | union node *new; |
| 7923 | |
| 7924 | if (n == NULL) |
| 7925 | return NULL; |
| 7926 | new = funcblock; |
| 7927 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 7928 | |
| 7929 | switch (n->type) { |
| 7930 | case NCMD: |
| 7931 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 7932 | new->ncmd.args = copynode(n->ncmd.args); |
| 7933 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 7934 | break; |
| 7935 | case NPIPE: |
| 7936 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7937 | new->npipe.pipe_backgnd = n->npipe.pipe_backgnd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7938 | break; |
| 7939 | case NREDIR: |
| 7940 | case NBACKGND: |
| 7941 | case NSUBSHELL: |
| 7942 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 7943 | new->nredir.n = copynode(n->nredir.n); |
| 7944 | break; |
| 7945 | case NAND: |
| 7946 | case NOR: |
| 7947 | case NSEMI: |
| 7948 | case NWHILE: |
| 7949 | case NUNTIL: |
| 7950 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 7951 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 7952 | break; |
| 7953 | case NIF: |
| 7954 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 7955 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 7956 | new->nif.test = copynode(n->nif.test); |
| 7957 | break; |
| 7958 | case NFOR: |
| 7959 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 7960 | new->nfor.body = copynode(n->nfor.body); |
| 7961 | new->nfor.args = copynode(n->nfor.args); |
| 7962 | break; |
| 7963 | case NCASE: |
| 7964 | new->ncase.cases = copynode(n->ncase.cases); |
| 7965 | new->ncase.expr = copynode(n->ncase.expr); |
| 7966 | break; |
| 7967 | case NCLIST: |
| 7968 | new->nclist.body = copynode(n->nclist.body); |
| 7969 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 7970 | new->nclist.next = copynode(n->nclist.next); |
| 7971 | break; |
| 7972 | case NDEFUN: |
| 7973 | case NARG: |
| 7974 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 7975 | new->narg.text = nodeckstrdup(n->narg.text); |
| 7976 | new->narg.next = copynode(n->narg.next); |
| 7977 | break; |
| 7978 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 7979 | #if ENABLE_ASH_BASH_COMPAT |
| 7980 | case NTO2: |
| 7981 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7982 | case NCLOBBER: |
| 7983 | case NFROM: |
| 7984 | case NFROMTO: |
| 7985 | case NAPPEND: |
| 7986 | new->nfile.fname = copynode(n->nfile.fname); |
| 7987 | new->nfile.fd = n->nfile.fd; |
| 7988 | new->nfile.next = copynode(n->nfile.next); |
| 7989 | break; |
| 7990 | case NTOFD: |
| 7991 | case NFROMFD: |
| 7992 | new->ndup.vname = copynode(n->ndup.vname); |
| 7993 | new->ndup.dupfd = n->ndup.dupfd; |
| 7994 | new->ndup.fd = n->ndup.fd; |
| 7995 | new->ndup.next = copynode(n->ndup.next); |
| 7996 | break; |
| 7997 | case NHERE: |
| 7998 | case NXHERE: |
| 7999 | new->nhere.doc = copynode(n->nhere.doc); |
| 8000 | new->nhere.fd = n->nhere.fd; |
| 8001 | new->nhere.next = copynode(n->nhere.next); |
| 8002 | break; |
| 8003 | case NNOT: |
| 8004 | new->nnot.com = copynode(n->nnot.com); |
| 8005 | break; |
| 8006 | }; |
| 8007 | new->type = n->type; |
| 8008 | return new; |
| 8009 | } |
| 8010 | |
| 8011 | /* |
| 8012 | * Make a copy of a parse tree. |
| 8013 | */ |
| 8014 | static struct funcnode * |
| 8015 | copyfunc(union node *n) |
| 8016 | { |
| 8017 | struct funcnode *f; |
| 8018 | size_t blocksize; |
| 8019 | |
| 8020 | funcblocksize = offsetof(struct funcnode, n); |
| 8021 | funcstringsize = 0; |
| 8022 | calcsize(n); |
| 8023 | blocksize = funcblocksize; |
| 8024 | f = ckmalloc(blocksize + funcstringsize); |
| 8025 | funcblock = (char *) f + offsetof(struct funcnode, n); |
| 8026 | funcstring = (char *) f + blocksize; |
| 8027 | copynode(n); |
| 8028 | f->count = 0; |
| 8029 | return f; |
| 8030 | } |
| 8031 | |
| 8032 | /* |
| 8033 | * Define a shell function. |
| 8034 | */ |
| 8035 | static void |
| 8036 | defun(char *name, union node *func) |
| 8037 | { |
| 8038 | struct cmdentry entry; |
| 8039 | |
| 8040 | INT_OFF; |
| 8041 | entry.cmdtype = CMDFUNCTION; |
| 8042 | entry.u.func = copyfunc(func); |
| 8043 | addcmdentry(name, &entry); |
| 8044 | INT_ON; |
| 8045 | } |
| 8046 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8047 | /* Reasons for skipping commands (see comment on breakcmd routine) */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8048 | #define SKIPBREAK (1 << 0) |
| 8049 | #define SKIPCONT (1 << 1) |
| 8050 | #define SKIPFUNC (1 << 2) |
| 8051 | #define SKIPFILE (1 << 3) |
| 8052 | #define SKIPEVAL (1 << 4) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8053 | static smallint evalskip; /* set to SKIPxxx if we are skipping commands */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8054 | static int skipcount; /* number of levels to skip */ |
| 8055 | static int funcnest; /* depth of function calls */ |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 8056 | static int loopnest; /* current loop nesting level */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8057 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8058 | /* Forward decl way out to parsing code - dotrap needs it */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8059 | static int evalstring(char *s, int mask); |
| 8060 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8061 | /* Called to execute a trap. |
| 8062 | * Single callsite - at the end of evaltree(). |
| 8063 | * If we return non-zero, exaltree raises EXEXIT exception. |
| 8064 | * |
| 8065 | * Perhaps we should avoid entering new trap handlers |
| 8066 | * while we are executing a trap handler. [is it a TODO?] |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8067 | */ |
| 8068 | static int |
| 8069 | dotrap(void) |
| 8070 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8071 | uint8_t *g; |
| 8072 | int sig; |
| 8073 | uint8_t savestatus; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8074 | |
| 8075 | savestatus = exitstatus; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8076 | pending_sig = 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8077 | xbarrier(); |
| 8078 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8079 | TRACE(("dotrap entered\n")); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8080 | for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) { |
| 8081 | int want_exexit; |
| 8082 | char *t; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8083 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8084 | if (*g == 0) |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8085 | continue; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8086 | t = trap[sig]; |
| 8087 | /* non-trapped SIGINT is handled separately by raise_interrupt, |
| 8088 | * don't upset it by resetting gotsig[SIGINT-1] */ |
| 8089 | if (sig == SIGINT && !t) |
| 8090 | continue; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8091 | |
| 8092 | TRACE(("sig %d is active, will run handler '%s'\n", sig, t)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8093 | *g = 0; |
| 8094 | if (!t) |
| 8095 | continue; |
| 8096 | want_exexit = evalstring(t, SKIPEVAL); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8097 | exitstatus = savestatus; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8098 | if (want_exexit) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8099 | TRACE(("dotrap returns %d\n", want_exexit)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8100 | return want_exexit; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8101 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8102 | } |
| 8103 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8104 | TRACE(("dotrap returns 0\n")); |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8105 | return 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8106 | } |
| 8107 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8108 | /* forward declarations - evaluation is fairly recursive business... */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8109 | static void evalloop(union node *, int); |
| 8110 | static void evalfor(union node *, int); |
| 8111 | static void evalcase(union node *, int); |
| 8112 | static void evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8113 | static void expredir(union node *); |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8114 | static void evalpipe(union node *, int); |
| 8115 | static void evalcommand(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8116 | static int evalbltin(const struct builtincmd *, int, char **); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8117 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8118 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8119 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8120 | * Evaluate a parse tree. The value is left in the global variable |
| 8121 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8122 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8123 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8124 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8125 | { |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8126 | struct jmploc *volatile savehandler = exception_handler; |
| 8127 | struct jmploc jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8128 | int checkexit = 0; |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8129 | void (*evalfn)(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8130 | int status; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8131 | int int_level; |
| 8132 | |
| 8133 | SAVE_INT(int_level); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8134 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8135 | if (n == NULL) { |
| 8136 | TRACE(("evaltree(NULL) called\n")); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8137 | goto out1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8138 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8139 | TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8140 | |
| 8141 | exception_handler = &jmploc; |
| 8142 | { |
| 8143 | int err = setjmp(jmploc.loc); |
| 8144 | if (err) { |
| 8145 | /* if it was a signal, check for trap handlers */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8146 | if (exception_type == EXSIG) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8147 | TRACE(("exception %d (EXSIG) in evaltree, err=%d\n", |
| 8148 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8149 | goto out; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8150 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8151 | /* continue on the way out */ |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8152 | TRACE(("exception %d in evaltree, propagating err=%d\n", |
| 8153 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8154 | exception_handler = savehandler; |
| 8155 | longjmp(exception_handler->loc, err); |
| 8156 | } |
| 8157 | } |
| 8158 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8159 | switch (n->type) { |
| 8160 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 8161 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8162 | out1fmt("Node type = %d\n", n->type); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8163 | fflush(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8164 | break; |
| 8165 | #endif |
| 8166 | case NNOT: |
| 8167 | evaltree(n->nnot.com, EV_TESTED); |
| 8168 | status = !exitstatus; |
| 8169 | goto setstatus; |
| 8170 | case NREDIR: |
| 8171 | expredir(n->nredir.redirect); |
| 8172 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 8173 | if (!status) { |
| 8174 | evaltree(n->nredir.n, flags & EV_TESTED); |
| 8175 | status = exitstatus; |
| 8176 | } |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 8177 | popredir(/*drop:*/ 0, /*restore:*/ 0 /* not sure */); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8178 | goto setstatus; |
| 8179 | case NCMD: |
| 8180 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8181 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8182 | if (eflag && !(flags & EV_TESTED)) |
| 8183 | checkexit = ~0; |
| 8184 | goto calleval; |
| 8185 | case NFOR: |
| 8186 | evalfn = evalfor; |
| 8187 | goto calleval; |
| 8188 | case NWHILE: |
| 8189 | case NUNTIL: |
| 8190 | evalfn = evalloop; |
| 8191 | goto calleval; |
| 8192 | case NSUBSHELL: |
| 8193 | case NBACKGND: |
| 8194 | evalfn = evalsubshell; |
| 8195 | goto calleval; |
| 8196 | case NPIPE: |
| 8197 | evalfn = evalpipe; |
| 8198 | goto checkexit; |
| 8199 | case NCASE: |
| 8200 | evalfn = evalcase; |
| 8201 | goto calleval; |
| 8202 | case NAND: |
| 8203 | case NOR: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8204 | case NSEMI: { |
| 8205 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8206 | #if NAND + 1 != NOR |
| 8207 | #error NAND + 1 != NOR |
| 8208 | #endif |
| 8209 | #if NOR + 1 != NSEMI |
| 8210 | #error NOR + 1 != NSEMI |
| 8211 | #endif |
Denis Vlasenko | 87d5fd9 | 2008-07-26 13:48:35 +0000 | [diff] [blame] | 8212 | unsigned is_or = n->type - NAND; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8213 | evaltree( |
| 8214 | n->nbinary.ch1, |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8215 | (flags | ((is_or >> 1) - 1)) & EV_TESTED |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8216 | ); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8217 | if (!exitstatus == is_or) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8218 | break; |
| 8219 | if (!evalskip) { |
| 8220 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8221 | evaln: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8222 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8223 | calleval: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8224 | evalfn(n, flags); |
| 8225 | break; |
| 8226 | } |
| 8227 | break; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8228 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8229 | case NIF: |
| 8230 | evaltree(n->nif.test, EV_TESTED); |
| 8231 | if (evalskip) |
| 8232 | break; |
| 8233 | if (exitstatus == 0) { |
| 8234 | n = n->nif.ifpart; |
| 8235 | goto evaln; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8236 | } |
| 8237 | if (n->nif.elsepart) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8238 | n = n->nif.elsepart; |
| 8239 | goto evaln; |
| 8240 | } |
| 8241 | goto success; |
| 8242 | case NDEFUN: |
| 8243 | defun(n->narg.text, n->narg.next); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8244 | success: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8245 | status = 0; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8246 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8247 | exitstatus = status; |
| 8248 | break; |
| 8249 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8250 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8251 | out: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8252 | exception_handler = savehandler; |
| 8253 | out1: |
| 8254 | if (checkexit & exitstatus) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8255 | evalskip |= SKIPEVAL; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8256 | else if (pending_sig && dotrap()) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8257 | goto exexit; |
| 8258 | |
| 8259 | if (flags & EV_EXIT) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8260 | exexit: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8261 | raise_exception(EXEXIT); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8262 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8263 | |
| 8264 | RESTORE_INT(int_level); |
| 8265 | TRACE(("leaving evaltree (no interrupts)\n")); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8266 | } |
| 8267 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8268 | #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3) |
| 8269 | static |
| 8270 | #endif |
| 8271 | void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__)); |
| 8272 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8273 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8274 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8275 | { |
| 8276 | int status; |
| 8277 | |
| 8278 | loopnest++; |
| 8279 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8280 | flags &= EV_TESTED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8281 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8282 | int i; |
| 8283 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8284 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 8285 | if (evalskip) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8286 | skipping: |
| 8287 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8288 | evalskip = 0; |
| 8289 | continue; |
| 8290 | } |
| 8291 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8292 | evalskip = 0; |
| 8293 | break; |
| 8294 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8295 | i = exitstatus; |
| 8296 | if (n->type != NWHILE) |
| 8297 | i = !i; |
| 8298 | if (i != 0) |
| 8299 | break; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8300 | evaltree(n->nbinary.ch2, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8301 | status = exitstatus; |
| 8302 | if (evalskip) |
| 8303 | goto skipping; |
| 8304 | } |
| 8305 | loopnest--; |
| 8306 | exitstatus = status; |
| 8307 | } |
| 8308 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8309 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8310 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8311 | { |
| 8312 | struct arglist arglist; |
| 8313 | union node *argp; |
| 8314 | struct strlist *sp; |
| 8315 | struct stackmark smark; |
| 8316 | |
| 8317 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8318 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8319 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8320 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8321 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8322 | /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8323 | if (evalskip) |
| 8324 | goto out; |
| 8325 | } |
| 8326 | *arglist.lastp = NULL; |
| 8327 | |
| 8328 | exitstatus = 0; |
| 8329 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8330 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8331 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8332 | setvar(n->nfor.var, sp->text, 0); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8333 | evaltree(n->nfor.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8334 | if (evalskip) { |
| 8335 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 8336 | evalskip = 0; |
| 8337 | continue; |
| 8338 | } |
| 8339 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8340 | evalskip = 0; |
| 8341 | break; |
| 8342 | } |
| 8343 | } |
| 8344 | loopnest--; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8345 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8346 | popstackmark(&smark); |
| 8347 | } |
| 8348 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8349 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8350 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8351 | { |
| 8352 | union node *cp; |
| 8353 | union node *patp; |
| 8354 | struct arglist arglist; |
| 8355 | struct stackmark smark; |
| 8356 | |
| 8357 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8358 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8359 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8360 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8361 | exitstatus = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8362 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 8363 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8364 | if (casematch(patp, arglist.list->text)) { |
| 8365 | if (evalskip == 0) { |
| 8366 | evaltree(cp->nclist.body, flags); |
| 8367 | } |
| 8368 | goto out; |
| 8369 | } |
| 8370 | } |
| 8371 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8372 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8373 | popstackmark(&smark); |
| 8374 | } |
| 8375 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8376 | /* |
| 8377 | * Kick off a subshell to evaluate a tree. |
| 8378 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8379 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8380 | evalsubshell(union node *n, int flags) |
| 8381 | { |
| 8382 | struct job *jp; |
| 8383 | int backgnd = (n->type == NBACKGND); |
| 8384 | int status; |
| 8385 | |
| 8386 | expredir(n->nredir.redirect); |
| 8387 | if (!backgnd && flags & EV_EXIT && !trap[0]) |
| 8388 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8389 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8390 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8391 | if (forkshell(jp, n, backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8392 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8393 | flags |= EV_EXIT; |
| 8394 | if (backgnd) |
| 8395 | flags &=~ EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 8396 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8397 | redirect(n->nredir.redirect, 0); |
| 8398 | evaltreenr(n->nredir.n, flags); |
| 8399 | /* never returns */ |
| 8400 | } |
| 8401 | status = 0; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8402 | if (!backgnd) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8403 | status = waitforjob(jp); |
| 8404 | exitstatus = status; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8405 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8406 | } |
| 8407 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8408 | /* |
| 8409 | * Compute the names of the files in a redirection list. |
| 8410 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 8411 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8412 | static void |
| 8413 | expredir(union node *n) |
| 8414 | { |
| 8415 | union node *redir; |
| 8416 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8417 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8418 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8419 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8420 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8421 | fn.lastp = &fn.list; |
| 8422 | switch (redir->type) { |
| 8423 | case NFROMTO: |
| 8424 | case NFROM: |
| 8425 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8426 | #if ENABLE_ASH_BASH_COMPAT |
| 8427 | case NTO2: |
| 8428 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8429 | case NCLOBBER: |
| 8430 | case NAPPEND: |
| 8431 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8432 | #if ENABLE_ASH_BASH_COMPAT |
| 8433 | store_expfname: |
| 8434 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8435 | redir->nfile.expfname = fn.list->text; |
| 8436 | break; |
| 8437 | case NFROMFD: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8438 | case NTOFD: /* >& */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8439 | if (redir->ndup.vname) { |
| 8440 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8441 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8442 | ash_msg_and_raise_error("redir error"); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8443 | #if ENABLE_ASH_BASH_COMPAT |
| 8444 | //FIXME: we used expandarg with different args! |
| 8445 | if (!isdigit_str9(fn.list->text)) { |
| 8446 | /* >&file, not >&fd */ |
| 8447 | if (redir->nfile.fd != 1) /* 123>&file - BAD */ |
| 8448 | ash_msg_and_raise_error("redir error"); |
| 8449 | redir->type = NTO2; |
| 8450 | goto store_expfname; |
| 8451 | } |
| 8452 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8453 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8454 | } |
| 8455 | break; |
| 8456 | } |
| 8457 | } |
| 8458 | } |
| 8459 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8460 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8461 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 8462 | * of the process creating the pipeline. (This differs from some versions |
| 8463 | * of the shell, which make the last process in a pipeline the parent |
| 8464 | * of all the rest.) |
| 8465 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8466 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8467 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8468 | { |
| 8469 | struct job *jp; |
| 8470 | struct nodelist *lp; |
| 8471 | int pipelen; |
| 8472 | int prevfd; |
| 8473 | int pip[2]; |
| 8474 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8475 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8476 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8477 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8478 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8479 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8480 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8481 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8482 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8483 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8484 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8485 | pip[1] = -1; |
| 8486 | if (lp->next) { |
| 8487 | if (pipe(pip) < 0) { |
| 8488 | close(prevfd); |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 8489 | ash_msg_and_raise_error("pipe call failed"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8490 | } |
| 8491 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8492 | if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8493 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8494 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8495 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8496 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8497 | if (prevfd > 0) { |
| 8498 | dup2(prevfd, 0); |
| 8499 | close(prevfd); |
| 8500 | } |
| 8501 | if (pip[1] > 1) { |
| 8502 | dup2(pip[1], 1); |
| 8503 | close(pip[1]); |
| 8504 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8505 | evaltreenr(lp->n, flags); |
| 8506 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8507 | } |
| 8508 | if (prevfd >= 0) |
| 8509 | close(prevfd); |
| 8510 | prevfd = pip[0]; |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 8511 | /* Don't want to trigger debugging */ |
| 8512 | if (pip[1] != -1) |
| 8513 | close(pip[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8514 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8515 | if (n->npipe.pipe_backgnd == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8516 | exitstatus = waitforjob(jp); |
| 8517 | TRACE(("evalpipe: job done exit status %d\n", exitstatus)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8518 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8519 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8520 | } |
| 8521 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8522 | /* |
| 8523 | * Controls whether the shell is interactive or not. |
| 8524 | */ |
| 8525 | static void |
| 8526 | setinteractive(int on) |
| 8527 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8528 | static smallint is_interactive; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8529 | |
| 8530 | if (++on == is_interactive) |
| 8531 | return; |
| 8532 | is_interactive = on; |
| 8533 | setsignal(SIGINT); |
| 8534 | setsignal(SIGQUIT); |
| 8535 | setsignal(SIGTERM); |
| 8536 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8537 | if (is_interactive > 1) { |
| 8538 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8539 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8540 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8541 | if (!did_banner) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8542 | /* note: ash and hush share this string */ |
| 8543 | out1fmt("\n\n%s %s\n" |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8544 | "Enter 'help' for a list of built-in commands." |
| 8545 | "\n\n", |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8546 | bb_banner, |
| 8547 | "built-in shell (ash)" |
| 8548 | ); |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8549 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8550 | } |
| 8551 | } |
| 8552 | #endif |
| 8553 | } |
| 8554 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8555 | static void |
| 8556 | optschanged(void) |
| 8557 | { |
| 8558 | #if DEBUG |
| 8559 | opentrace(); |
| 8560 | #endif |
| 8561 | setinteractive(iflag); |
| 8562 | setjobctl(mflag); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8563 | #if ENABLE_FEATURE_EDITING_VI |
| 8564 | if (viflag) |
| 8565 | line_input_state->flags |= VI_MODE; |
| 8566 | else |
| 8567 | line_input_state->flags &= ~VI_MODE; |
| 8568 | #else |
| 8569 | viflag = 0; /* forcibly keep the option off */ |
| 8570 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8571 | } |
| 8572 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8573 | static struct localvar *localvars; |
| 8574 | |
| 8575 | /* |
| 8576 | * Called after a function returns. |
| 8577 | * Interrupts must be off. |
| 8578 | */ |
| 8579 | static void |
| 8580 | poplocalvars(void) |
| 8581 | { |
| 8582 | struct localvar *lvp; |
| 8583 | struct var *vp; |
| 8584 | |
| 8585 | while ((lvp = localvars) != NULL) { |
| 8586 | localvars = lvp->next; |
| 8587 | vp = lvp->vp; |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 8588 | TRACE(("poplocalvar %s\n", vp ? vp->text : "-")); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8589 | if (vp == NULL) { /* $- saved */ |
| 8590 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 8591 | free((char*)lvp->text); |
| 8592 | optschanged(); |
| 8593 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
| 8594 | unsetvar(vp->text); |
| 8595 | } else { |
| 8596 | if (vp->func) |
| 8597 | (*vp->func)(strchrnul(lvp->text, '=') + 1); |
| 8598 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 8599 | free((char*)vp->text); |
| 8600 | vp->flags = lvp->flags; |
| 8601 | vp->text = lvp->text; |
| 8602 | } |
| 8603 | free(lvp); |
| 8604 | } |
| 8605 | } |
| 8606 | |
| 8607 | static int |
| 8608 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 8609 | { |
| 8610 | volatile struct shparam saveparam; |
| 8611 | struct localvar *volatile savelocalvars; |
| 8612 | struct jmploc *volatile savehandler; |
| 8613 | struct jmploc jmploc; |
| 8614 | int e; |
| 8615 | |
| 8616 | saveparam = shellparam; |
| 8617 | savelocalvars = localvars; |
| 8618 | e = setjmp(jmploc.loc); |
| 8619 | if (e) { |
| 8620 | goto funcdone; |
| 8621 | } |
| 8622 | INT_OFF; |
| 8623 | savehandler = exception_handler; |
| 8624 | exception_handler = &jmploc; |
| 8625 | localvars = NULL; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8626 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8627 | func->count++; |
| 8628 | funcnest++; |
| 8629 | INT_ON; |
| 8630 | shellparam.nparam = argc - 1; |
| 8631 | shellparam.p = argv + 1; |
| 8632 | #if ENABLE_ASH_GETOPTS |
| 8633 | shellparam.optind = 1; |
| 8634 | shellparam.optoff = -1; |
| 8635 | #endif |
| 8636 | evaltree(&func->n, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8637 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8638 | INT_OFF; |
| 8639 | funcnest--; |
| 8640 | freefunc(func); |
| 8641 | poplocalvars(); |
| 8642 | localvars = savelocalvars; |
| 8643 | freeparam(&shellparam); |
| 8644 | shellparam = saveparam; |
| 8645 | exception_handler = savehandler; |
| 8646 | INT_ON; |
| 8647 | evalskip &= ~SKIPFUNC; |
| 8648 | return e; |
| 8649 | } |
| 8650 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8651 | #if ENABLE_ASH_CMDCMD |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8652 | static char ** |
| 8653 | parse_command_args(char **argv, const char **path) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8654 | { |
| 8655 | char *cp, c; |
| 8656 | |
| 8657 | for (;;) { |
| 8658 | cp = *++argv; |
| 8659 | if (!cp) |
| 8660 | return 0; |
| 8661 | if (*cp++ != '-') |
| 8662 | break; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8663 | c = *cp++; |
| 8664 | if (!c) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8665 | break; |
| 8666 | if (c == '-' && !*cp) { |
| 8667 | argv++; |
| 8668 | break; |
| 8669 | } |
| 8670 | do { |
| 8671 | switch (c) { |
| 8672 | case 'p': |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 8673 | *path = bb_default_path; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8674 | break; |
| 8675 | default: |
| 8676 | /* run 'typecmd' for other options */ |
| 8677 | return 0; |
| 8678 | } |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 8679 | c = *cp++; |
| 8680 | } while (c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8681 | } |
| 8682 | return argv; |
| 8683 | } |
| 8684 | #endif |
| 8685 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8686 | /* |
| 8687 | * Make a variable a local variable. When a variable is made local, it's |
| 8688 | * value and flags are saved in a localvar structure. The saved values |
| 8689 | * will be restored when the shell function returns. We handle the name |
| 8690 | * "-" as a special case. |
| 8691 | */ |
| 8692 | static void |
| 8693 | mklocal(char *name) |
| 8694 | { |
| 8695 | struct localvar *lvp; |
| 8696 | struct var **vpp; |
| 8697 | struct var *vp; |
| 8698 | |
| 8699 | INT_OFF; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 8700 | lvp = ckzalloc(sizeof(struct localvar)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8701 | if (LONE_DASH(name)) { |
| 8702 | char *p; |
| 8703 | p = ckmalloc(sizeof(optlist)); |
| 8704 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 8705 | vp = NULL; |
| 8706 | } else { |
| 8707 | char *eq; |
| 8708 | |
| 8709 | vpp = hashvar(name); |
| 8710 | vp = *findvar(vpp, name); |
| 8711 | eq = strchr(name, '='); |
| 8712 | if (vp == NULL) { |
| 8713 | if (eq) |
| 8714 | setvareq(name, VSTRFIXED); |
| 8715 | else |
| 8716 | setvar(name, NULL, VSTRFIXED); |
| 8717 | vp = *vpp; /* the new variable */ |
| 8718 | lvp->flags = VUNSET; |
| 8719 | } else { |
| 8720 | lvp->text = vp->text; |
| 8721 | lvp->flags = vp->flags; |
| 8722 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 8723 | if (eq) |
| 8724 | setvareq(name, 0); |
| 8725 | } |
| 8726 | } |
| 8727 | lvp->vp = vp; |
| 8728 | lvp->next = localvars; |
| 8729 | localvars = lvp; |
| 8730 | INT_ON; |
| 8731 | } |
| 8732 | |
| 8733 | /* |
| 8734 | * The "local" command. |
| 8735 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8736 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8737 | localcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8738 | { |
| 8739 | char *name; |
| 8740 | |
| 8741 | argv = argptr; |
| 8742 | while ((name = *argv++) != NULL) { |
| 8743 | mklocal(name); |
| 8744 | } |
| 8745 | return 0; |
| 8746 | } |
| 8747 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8748 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8749 | falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8750 | { |
| 8751 | return 1; |
| 8752 | } |
| 8753 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8754 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8755 | truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8756 | { |
| 8757 | return 0; |
| 8758 | } |
| 8759 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8760 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8761 | execcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8762 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8763 | if (argv[1]) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8764 | iflag = 0; /* exit on error */ |
| 8765 | mflag = 0; |
| 8766 | optschanged(); |
| 8767 | shellexec(argv + 1, pathval(), 0); |
| 8768 | } |
| 8769 | return 0; |
| 8770 | } |
| 8771 | |
| 8772 | /* |
| 8773 | * The return command. |
| 8774 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8775 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8776 | returncmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8777 | { |
| 8778 | /* |
| 8779 | * If called outside a function, do what ksh does; |
| 8780 | * skip the rest of the file. |
| 8781 | */ |
| 8782 | evalskip = funcnest ? SKIPFUNC : SKIPFILE; |
| 8783 | return argv[1] ? number(argv[1]) : exitstatus; |
| 8784 | } |
| 8785 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8786 | /* Forward declarations for builtintab[] */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8787 | static int breakcmd(int, char **) FAST_FUNC; |
| 8788 | static int dotcmd(int, char **) FAST_FUNC; |
| 8789 | static int evalcmd(int, char **) FAST_FUNC; |
| 8790 | static int exitcmd(int, char **) FAST_FUNC; |
| 8791 | static int exportcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8792 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8793 | static int getoptscmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8794 | #endif |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8795 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8796 | static int helpcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8797 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 8798 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8799 | static int letcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8800 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8801 | static int readcmd(int, char **) FAST_FUNC; |
| 8802 | static int setcmd(int, char **) FAST_FUNC; |
| 8803 | static int shiftcmd(int, char **) FAST_FUNC; |
| 8804 | static int timescmd(int, char **) FAST_FUNC; |
| 8805 | static int trapcmd(int, char **) FAST_FUNC; |
| 8806 | static int umaskcmd(int, char **) FAST_FUNC; |
| 8807 | static int unsetcmd(int, char **) FAST_FUNC; |
| 8808 | static int ulimitcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8809 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8810 | #define BUILTIN_NOSPEC "0" |
| 8811 | #define BUILTIN_SPECIAL "1" |
| 8812 | #define BUILTIN_REGULAR "2" |
| 8813 | #define BUILTIN_SPEC_REG "3" |
| 8814 | #define BUILTIN_ASSIGN "4" |
| 8815 | #define BUILTIN_SPEC_ASSG "5" |
| 8816 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8817 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 8818 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8819 | /* Stubs for calling non-FAST_FUNC's */ |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8820 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8821 | 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] | 8822 | #endif |
| 8823 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8824 | 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] | 8825 | #endif |
| 8826 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8827 | 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] | 8828 | #endif |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 8829 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 8830 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8831 | static const struct builtincmd builtintab[] = { |
| 8832 | { BUILTIN_SPEC_REG ".", dotcmd }, |
| 8833 | { BUILTIN_SPEC_REG ":", truecmd }, |
| 8834 | #if ENABLE_ASH_BUILTIN_TEST |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8835 | { BUILTIN_REGULAR "[", testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8836 | #if ENABLE_ASH_BASH_COMPAT |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8837 | { BUILTIN_REGULAR "[[", testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8838 | #endif |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8839 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8840 | #if ENABLE_ASH_ALIAS |
| 8841 | { BUILTIN_REG_ASSG "alias", aliascmd }, |
| 8842 | #endif |
| 8843 | #if JOBS |
| 8844 | { BUILTIN_REGULAR "bg", fg_bgcmd }, |
| 8845 | #endif |
| 8846 | { BUILTIN_SPEC_REG "break", breakcmd }, |
| 8847 | { BUILTIN_REGULAR "cd", cdcmd }, |
| 8848 | { BUILTIN_NOSPEC "chdir", cdcmd }, |
| 8849 | #if ENABLE_ASH_CMDCMD |
| 8850 | { BUILTIN_REGULAR "command", commandcmd }, |
| 8851 | #endif |
| 8852 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
| 8853 | #if ENABLE_ASH_BUILTIN_ECHO |
| 8854 | { BUILTIN_REGULAR "echo", echocmd }, |
| 8855 | #endif |
| 8856 | { BUILTIN_SPEC_REG "eval", evalcmd }, |
| 8857 | { BUILTIN_SPEC_REG "exec", execcmd }, |
| 8858 | { BUILTIN_SPEC_REG "exit", exitcmd }, |
| 8859 | { BUILTIN_SPEC_REG_ASSG "export", exportcmd }, |
| 8860 | { BUILTIN_REGULAR "false", falsecmd }, |
| 8861 | #if JOBS |
| 8862 | { BUILTIN_REGULAR "fg", fg_bgcmd }, |
| 8863 | #endif |
| 8864 | #if ENABLE_ASH_GETOPTS |
| 8865 | { BUILTIN_REGULAR "getopts", getoptscmd }, |
| 8866 | #endif |
| 8867 | { BUILTIN_NOSPEC "hash", hashcmd }, |
| 8868 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8869 | { BUILTIN_NOSPEC "help", helpcmd }, |
| 8870 | #endif |
| 8871 | #if JOBS |
| 8872 | { BUILTIN_REGULAR "jobs", jobscmd }, |
| 8873 | { BUILTIN_REGULAR "kill", killcmd }, |
| 8874 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 8875 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8876 | { BUILTIN_NOSPEC "let", letcmd }, |
| 8877 | #endif |
| 8878 | { BUILTIN_ASSIGN "local", localcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8879 | #if ENABLE_ASH_BUILTIN_PRINTF |
| 8880 | { BUILTIN_REGULAR "printf", printfcmd }, |
| 8881 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8882 | { BUILTIN_NOSPEC "pwd", pwdcmd }, |
| 8883 | { BUILTIN_REGULAR "read", readcmd }, |
| 8884 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 8885 | { BUILTIN_SPEC_REG "return", returncmd }, |
| 8886 | { BUILTIN_SPEC_REG "set", setcmd }, |
| 8887 | { BUILTIN_SPEC_REG "shift", shiftcmd }, |
| 8888 | { BUILTIN_SPEC_REG "source", dotcmd }, |
| 8889 | #if ENABLE_ASH_BUILTIN_TEST |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8890 | { BUILTIN_REGULAR "test", testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8891 | #endif |
| 8892 | { BUILTIN_SPEC_REG "times", timescmd }, |
| 8893 | { BUILTIN_SPEC_REG "trap", trapcmd }, |
| 8894 | { BUILTIN_REGULAR "true", truecmd }, |
| 8895 | { BUILTIN_NOSPEC "type", typecmd }, |
| 8896 | { BUILTIN_NOSPEC "ulimit", ulimitcmd }, |
| 8897 | { BUILTIN_REGULAR "umask", umaskcmd }, |
| 8898 | #if ENABLE_ASH_ALIAS |
| 8899 | { BUILTIN_REGULAR "unalias", unaliascmd }, |
| 8900 | #endif |
| 8901 | { BUILTIN_SPEC_REG "unset", unsetcmd }, |
| 8902 | { BUILTIN_REGULAR "wait", waitcmd }, |
| 8903 | }; |
| 8904 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8905 | /* Should match the above table! */ |
| 8906 | #define COMMANDCMD (builtintab + \ |
| 8907 | 2 + \ |
| 8908 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 8909 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 8910 | 1 * ENABLE_ASH_ALIAS + \ |
| 8911 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 8912 | 3) |
| 8913 | #define EXECCMD (builtintab + \ |
| 8914 | 2 + \ |
| 8915 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 8916 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 8917 | 1 * ENABLE_ASH_ALIAS + \ |
| 8918 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 8919 | 3 + \ |
| 8920 | 1 * ENABLE_ASH_CMDCMD + \ |
| 8921 | 1 + \ |
| 8922 | ENABLE_ASH_BUILTIN_ECHO + \ |
| 8923 | 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8924 | |
| 8925 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8926 | * Search the table of builtin commands. |
| 8927 | */ |
| 8928 | static struct builtincmd * |
| 8929 | find_builtin(const char *name) |
| 8930 | { |
| 8931 | struct builtincmd *bp; |
| 8932 | |
| 8933 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 8934 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8935 | pstrcmp |
| 8936 | ); |
| 8937 | return bp; |
| 8938 | } |
| 8939 | |
| 8940 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8941 | * Execute a simple command. |
| 8942 | */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8943 | static int |
| 8944 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8945 | { |
| 8946 | const char *q = endofname(p); |
| 8947 | if (p == q) |
| 8948 | return 0; |
| 8949 | return *q == '='; |
| 8950 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8951 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8952 | bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8953 | { |
| 8954 | /* Preserve exitstatus of a previous possible redirection |
| 8955 | * as POSIX mandates */ |
| 8956 | return back_exitstatus; |
| 8957 | } |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8958 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8959 | evalcommand(union node *cmd, int flags) |
| 8960 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 8961 | static const struct builtincmd null_bltin = { |
| 8962 | "\0\0", bltincmd /* why three NULs? */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8963 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8964 | struct stackmark smark; |
| 8965 | union node *argp; |
| 8966 | struct arglist arglist; |
| 8967 | struct arglist varlist; |
| 8968 | char **argv; |
| 8969 | int argc; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8970 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8971 | struct cmdentry cmdentry; |
| 8972 | struct job *jp; |
| 8973 | char *lastarg; |
| 8974 | const char *path; |
| 8975 | int spclbltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8976 | int status; |
| 8977 | char **nargv; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8978 | struct builtincmd *bcmd; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 8979 | smallint cmd_is_exec; |
| 8980 | smallint pseudovarflag = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8981 | |
| 8982 | /* First expand the arguments. */ |
| 8983 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 8984 | setstackmark(&smark); |
| 8985 | back_exitstatus = 0; |
| 8986 | |
| 8987 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 8988 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8989 | varlist.lastp = &varlist.list; |
| 8990 | *varlist.lastp = NULL; |
| 8991 | arglist.lastp = &arglist.list; |
| 8992 | *arglist.lastp = NULL; |
| 8993 | |
| 8994 | argc = 0; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8995 | if (cmd->ncmd.args) { |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8996 | bcmd = find_builtin(cmd->ncmd.args->narg.text); |
| 8997 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
| 8998 | } |
| 8999 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9000 | for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) { |
| 9001 | struct strlist **spp; |
| 9002 | |
| 9003 | spp = arglist.lastp; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 9004 | if (pseudovarflag && isassignment(argp->narg.text)) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9005 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 9006 | else |
| 9007 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 9008 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9009 | for (sp = *spp; sp; sp = sp->next) |
| 9010 | argc++; |
| 9011 | } |
| 9012 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9013 | argv = nargv = stalloc(sizeof(char *) * (argc + 1)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9014 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9015 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 9016 | *nargv++ = sp->text; |
| 9017 | } |
| 9018 | *nargv = NULL; |
| 9019 | |
| 9020 | lastarg = NULL; |
| 9021 | if (iflag && funcnest == 0 && argc > 0) |
| 9022 | lastarg = nargv[-1]; |
| 9023 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9024 | preverrout_fd = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9025 | expredir(cmd->ncmd.redirect); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9026 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9027 | |
| 9028 | path = vpath.text; |
| 9029 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 9030 | struct strlist **spp; |
| 9031 | char *p; |
| 9032 | |
| 9033 | spp = varlist.lastp; |
| 9034 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 9035 | |
| 9036 | /* |
| 9037 | * Modify the command lookup path, if a PATH= assignment |
| 9038 | * is present |
| 9039 | */ |
| 9040 | p = (*spp)->text; |
| 9041 | if (varequal(p, path)) |
| 9042 | path = p; |
| 9043 | } |
| 9044 | |
| 9045 | /* Print the command if xflag is set. */ |
| 9046 | if (xflag) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9047 | int n; |
| 9048 | const char *p = " %s"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9049 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9050 | p++; |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9051 | fdprintf(preverrout_fd, p, expandstr(ps4val())); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9052 | |
| 9053 | sp = varlist.list; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9054 | for (n = 0; n < 2; n++) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9055 | while (sp) { |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9056 | fdprintf(preverrout_fd, p, sp->text); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9057 | sp = sp->next; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9058 | if (*p == '%') { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9059 | p--; |
| 9060 | } |
| 9061 | } |
| 9062 | sp = arglist.list; |
| 9063 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9064 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9065 | } |
| 9066 | |
| 9067 | cmd_is_exec = 0; |
| 9068 | spclbltin = -1; |
| 9069 | |
| 9070 | /* Now locate the command. */ |
| 9071 | if (argc) { |
| 9072 | const char *oldpath; |
| 9073 | int cmd_flag = DO_ERR; |
| 9074 | |
| 9075 | path += 5; |
| 9076 | oldpath = path; |
| 9077 | for (;;) { |
| 9078 | find_command(argv[0], &cmdentry, cmd_flag, path); |
| 9079 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9080 | flush_stderr(); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9081 | status = 127; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9082 | goto bail; |
| 9083 | } |
| 9084 | |
| 9085 | /* implement bltin and command here */ |
| 9086 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 9087 | break; |
| 9088 | if (spclbltin < 0) |
| 9089 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
| 9090 | if (cmdentry.u.cmd == EXECCMD) |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9091 | cmd_is_exec = 1; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9092 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9093 | if (cmdentry.u.cmd == COMMANDCMD) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9094 | path = oldpath; |
| 9095 | nargv = parse_command_args(argv, &path); |
| 9096 | if (!nargv) |
| 9097 | break; |
| 9098 | argc -= nargv - argv; |
| 9099 | argv = nargv; |
| 9100 | cmd_flag |= DO_NOFUNC; |
| 9101 | } else |
| 9102 | #endif |
| 9103 | break; |
| 9104 | } |
| 9105 | } |
| 9106 | |
| 9107 | if (status) { |
| 9108 | /* We have a redirection error. */ |
| 9109 | if (spclbltin > 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9110 | raise_exception(EXERROR); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9111 | bail: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9112 | exitstatus = status; |
| 9113 | goto out; |
| 9114 | } |
| 9115 | |
| 9116 | /* Execute the command. */ |
| 9117 | switch (cmdentry.cmdtype) { |
| 9118 | default: |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9119 | |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9120 | #if ENABLE_FEATURE_SH_NOFORK |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9121 | /* Hmmm... shouldn't it happen somewhere in forkshell() instead? |
| 9122 | * 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] | 9123 | { |
| 9124 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 9125 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9126 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9127 | listsetvar(varlist.list, VEXPORT|VSTACK); |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9128 | /* run <applet>_main() */ |
| 9129 | exitstatus = run_nofork_applet(applet_no, argv); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9130 | break; |
| 9131 | } |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9132 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9133 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9134 | /* Fork off a child process if necessary. */ |
| 9135 | if (!(flags & EV_EXIT) || trap[0]) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9136 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9137 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9138 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
| 9139 | exitstatus = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9140 | INT_ON; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9141 | TRACE(("forked child exited with %d\n", exitstatus)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9142 | break; |
| 9143 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9144 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9145 | } |
| 9146 | listsetvar(varlist.list, VEXPORT|VSTACK); |
| 9147 | shellexec(argv, path, cmdentry.u.index); |
| 9148 | /* NOTREACHED */ |
| 9149 | |
| 9150 | case CMDBUILTIN: |
| 9151 | cmdenviron = varlist.list; |
| 9152 | if (cmdenviron) { |
| 9153 | struct strlist *list = cmdenviron; |
| 9154 | int i = VNOSET; |
| 9155 | if (spclbltin > 0 || argc == 0) { |
| 9156 | i = 0; |
| 9157 | if (cmd_is_exec && argc > 1) |
| 9158 | i = VEXPORT; |
| 9159 | } |
| 9160 | listsetvar(list, i); |
| 9161 | } |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9162 | /* Tight loop with builtins only: |
| 9163 | * "while kill -0 $child; do true; done" |
| 9164 | * will never exit even if $child died, unless we do this |
| 9165 | * to reap the zombie and make kill detect that it's gone: */ |
| 9166 | dowait(DOWAIT_NONBLOCK, NULL); |
| 9167 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9168 | if (evalbltin(cmdentry.u.cmd, argc, argv)) { |
| 9169 | int exit_status; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 9170 | int i = exception_type; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9171 | if (i == EXEXIT) |
| 9172 | goto raise; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9173 | exit_status = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9174 | if (i == EXINT) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 9175 | exit_status = 128 + SIGINT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9176 | if (i == EXSIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9177 | exit_status = 128 + pending_sig; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9178 | exitstatus = exit_status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9179 | if (i == EXINT || spclbltin > 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9180 | raise: |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9181 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9182 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9183 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9184 | } |
| 9185 | break; |
| 9186 | |
| 9187 | case CMDFUNCTION: |
| 9188 | listsetvar(varlist.list, 0); |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9189 | /* See above for the rationale */ |
| 9190 | dowait(DOWAIT_NONBLOCK, NULL); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9191 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 9192 | goto raise; |
| 9193 | break; |
| 9194 | } |
| 9195 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9196 | out: |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9197 | popredir(/*drop:*/ cmd_is_exec, /*restore:*/ cmd_is_exec); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9198 | if (lastarg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9199 | /* dsl: I think this is intended to be used to support |
| 9200 | * '_' in 'vi' command mode during line editing... |
| 9201 | * However I implemented that within libedit itself. |
| 9202 | */ |
| 9203 | setvar("_", lastarg, 0); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9204 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9205 | popstackmark(&smark); |
| 9206 | } |
| 9207 | |
| 9208 | static int |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9209 | evalbltin(const struct builtincmd *cmd, int argc, char **argv) |
| 9210 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9211 | char *volatile savecmdname; |
| 9212 | struct jmploc *volatile savehandler; |
| 9213 | struct jmploc jmploc; |
| 9214 | int i; |
| 9215 | |
| 9216 | savecmdname = commandname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9217 | i = setjmp(jmploc.loc); |
| 9218 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9219 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9220 | savehandler = exception_handler; |
| 9221 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9222 | commandname = argv[0]; |
| 9223 | argptr = argv + 1; |
| 9224 | optptr = NULL; /* initialize nextopt */ |
| 9225 | exitstatus = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9226 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9227 | cmddone: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9228 | exitstatus |= ferror(stdout); |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 9229 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9230 | commandname = savecmdname; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9231 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9232 | |
| 9233 | return i; |
| 9234 | } |
| 9235 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9236 | static int |
| 9237 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9238 | { |
| 9239 | return !*endofname(p); |
| 9240 | } |
| 9241 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9242 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9243 | /* |
| 9244 | * Search for a command. This is called before we fork so that the |
| 9245 | * 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] | 9246 | * the child. The check for "goodname" is an overly conservative |
| 9247 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9248 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9249 | static void |
| 9250 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9251 | { |
| 9252 | struct cmdentry entry; |
| 9253 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9254 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 9255 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9256 | } |
| 9257 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9258 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9259 | /* ============ Builtin commands |
| 9260 | * |
| 9261 | * Builtin commands whose functions are closely tied to evaluation |
| 9262 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9263 | */ |
| 9264 | |
| 9265 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9266 | * Handle break and continue commands. Break, continue, and return are |
| 9267 | * all handled by setting the evalskip flag. The evaluation routines |
| 9268 | * above all check this flag, and if it is set they start skipping |
| 9269 | * commands rather than executing them. The variable skipcount is |
| 9270 | * the number of loops to break/continue, or the number of function |
| 9271 | * levels to return. (The latter is always 1.) It should probably |
| 9272 | * be an error to break out of more loops than exist, but it isn't |
| 9273 | * in the standard shell so we don't make it one here. |
| 9274 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9275 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9276 | breakcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9277 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9278 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9279 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 9280 | if (n <= 0) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9281 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9282 | if (n > loopnest) |
| 9283 | n = loopnest; |
| 9284 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9285 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9286 | skipcount = n; |
| 9287 | } |
| 9288 | return 0; |
| 9289 | } |
| 9290 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9291 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9292 | /* ============ input.c |
| 9293 | * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 9294 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9295 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9296 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9297 | enum { |
| 9298 | INPUT_PUSH_FILE = 1, |
| 9299 | INPUT_NOFILE_OK = 2, |
| 9300 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9301 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9302 | static smallint checkkwd; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9303 | /* values of checkkwd variable */ |
| 9304 | #define CHKALIAS 0x1 |
| 9305 | #define CHKKWD 0x2 |
| 9306 | #define CHKNL 0x4 |
| 9307 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9308 | /* |
| 9309 | * Push a string back onto the input at this current parsefile level. |
| 9310 | * We handle aliases this way. |
| 9311 | */ |
| 9312 | #if !ENABLE_ASH_ALIAS |
| 9313 | #define pushstring(s, ap) pushstring(s) |
| 9314 | #endif |
| 9315 | static void |
| 9316 | pushstring(char *s, struct alias *ap) |
| 9317 | { |
| 9318 | struct strpush *sp; |
| 9319 | int len; |
| 9320 | |
| 9321 | len = strlen(s); |
| 9322 | INT_OFF; |
| 9323 | if (g_parsefile->strpush) { |
| 9324 | sp = ckzalloc(sizeof(*sp)); |
| 9325 | sp->prev = g_parsefile->strpush; |
| 9326 | } else { |
| 9327 | sp = &(g_parsefile->basestrpush); |
| 9328 | } |
| 9329 | g_parsefile->strpush = sp; |
| 9330 | sp->prev_string = g_parsefile->next_to_pgetc; |
| 9331 | sp->prev_left_in_line = g_parsefile->left_in_line; |
| 9332 | #if ENABLE_ASH_ALIAS |
| 9333 | sp->ap = ap; |
| 9334 | if (ap) { |
| 9335 | ap->flag |= ALIASINUSE; |
| 9336 | sp->string = s; |
| 9337 | } |
| 9338 | #endif |
| 9339 | g_parsefile->next_to_pgetc = s; |
| 9340 | g_parsefile->left_in_line = len; |
| 9341 | INT_ON; |
| 9342 | } |
| 9343 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9344 | static void |
| 9345 | popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9346 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9347 | struct strpush *sp = g_parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9348 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9349 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9350 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9351 | if (sp->ap) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9352 | if (g_parsefile->next_to_pgetc[-1] == ' ' |
| 9353 | || g_parsefile->next_to_pgetc[-1] == '\t' |
| 9354 | ) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9355 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9356 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9357 | if (sp->string != sp->ap->val) { |
| 9358 | free(sp->string); |
| 9359 | } |
| 9360 | sp->ap->flag &= ~ALIASINUSE; |
| 9361 | if (sp->ap->flag & ALIASDEAD) { |
| 9362 | unalias(sp->ap->name); |
| 9363 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9364 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9365 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9366 | g_parsefile->next_to_pgetc = sp->prev_string; |
| 9367 | g_parsefile->left_in_line = sp->prev_left_in_line; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9368 | g_parsefile->strpush = sp->prev; |
| 9369 | if (sp != &(g_parsefile->basestrpush)) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9370 | free(sp); |
| 9371 | INT_ON; |
| 9372 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9373 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9374 | //FIXME: BASH_COMPAT with "...&" does TWO pungetc(): |
| 9375 | //it peeks whether it is &>, and then pushes back both chars. |
| 9376 | //This function needs to save last *next_to_pgetc to buf[0] |
| 9377 | //to make two pungetc() reliable. Currently, |
| 9378 | // pgetc (out of buf: does preadfd), pgetc, pungetc, pungetc won't work... |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9379 | static int |
| 9380 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9381 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9382 | int nr; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 9383 | char *buf = g_parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9384 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9385 | g_parsefile->next_to_pgetc = buf; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9386 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9387 | retry: |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9388 | if (!iflag || g_parsefile->fd != STDIN_FILENO) |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9389 | nr = nonblock_safe_read(g_parsefile->fd, buf, BUFSIZ - 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9390 | else { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9391 | #if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9392 | line_input_state->path_lookup = pathval(); |
Eric Andersen | 4a79c0e | 2004-09-08 10:01:07 +0000 | [diff] [blame] | 9393 | #endif |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9394 | nr = read_line_input(cmdedit_prompt, buf, BUFSIZ, line_input_state); |
| 9395 | if (nr == 0) { |
| 9396 | /* Ctrl+C pressed */ |
| 9397 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9398 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9399 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9400 | raise(SIGINT); |
| 9401 | return 1; |
| 9402 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9403 | goto retry; |
| 9404 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9405 | if (nr < 0 && errno == 0) { |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9406 | /* Ctrl+D pressed */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9407 | nr = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9408 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9409 | } |
| 9410 | #else |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 9411 | nr = nonblock_safe_read(g_parsefile->fd, buf, BUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9412 | #endif |
| 9413 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9414 | #if 0 |
| 9415 | /* nonblock_safe_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9416 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9417 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 9418 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9419 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 9420 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9421 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 9422 | out2str("sh: turning off NDELAY mode\n"); |
| 9423 | goto retry; |
| 9424 | } |
| 9425 | } |
| 9426 | } |
| 9427 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9428 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9429 | return nr; |
| 9430 | } |
| 9431 | |
| 9432 | /* |
| 9433 | * Refill the input buffer and return the next input character: |
| 9434 | * |
| 9435 | * 1) If a string was pushed back on the input, pop it; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9436 | * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM) |
| 9437 | * or we are reading from a string so we can't refill the buffer, |
| 9438 | * return EOF. |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 9439 | * 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] | 9440 | * 4) Process input up to the next newline, deleting nul characters. |
| 9441 | */ |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9442 | //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__) |
| 9443 | #define pgetc_debug(...) ((void)0) |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 9444 | /* |
| 9445 | * NB: due to SIT(c) internals (syntax_index_table[] vector), |
| 9446 | * pgetc() and related functions must return chars SIGN-EXTENDED into ints, |
| 9447 | * not zero-extended. Seems fragile to me. Affects only !USE_SIT_FUNCTION case, |
| 9448 | * so we can fix it by ditching !USE_SIT_FUNCTION if Unicode requires that. |
| 9449 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9450 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9451 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9452 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9453 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9454 | int more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9455 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9456 | while (g_parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9457 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9458 | if (g_parsefile->left_in_line == -1 |
| 9459 | && g_parsefile->strpush->ap |
| 9460 | && g_parsefile->next_to_pgetc[-1] != ' ' |
| 9461 | && g_parsefile->next_to_pgetc[-1] != '\t' |
Denis Vlasenko | 1689840 | 2008-11-25 01:34:52 +0000 | [diff] [blame] | 9462 | ) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9463 | pgetc_debug("preadbuffer PEOA"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9464 | return PEOA; |
| 9465 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9466 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9467 | popstring(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9468 | /* try "pgetc" now: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9469 | pgetc_debug("preadbuffer internal pgetc at %d:%p'%s'", |
| 9470 | g_parsefile->left_in_line, |
| 9471 | g_parsefile->next_to_pgetc, |
| 9472 | g_parsefile->next_to_pgetc); |
| 9473 | if (--g_parsefile->left_in_line >= 0) |
| 9474 | return (unsigned char)(*g_parsefile->next_to_pgetc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9475 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9476 | /* on both branches above g_parsefile->left_in_line < 0. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9477 | * "pgetc" needs refilling. |
| 9478 | */ |
| 9479 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9480 | /* -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] | 9481 | * pungetc() may increment it a few times. |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9482 | * Assuming it won't increment it to less than -90. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9483 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9484 | if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9485 | pgetc_debug("preadbuffer PEOF1"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9486 | /* even in failure keep left_in_line and next_to_pgetc |
| 9487 | * in lock step, for correct multi-layer pungetc. |
| 9488 | * left_in_line was decremented before preadbuffer(), |
| 9489 | * must inc next_to_pgetc: */ |
| 9490 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9491 | return PEOF; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9492 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9493 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9494 | more = g_parsefile->left_in_buffer; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9495 | if (more <= 0) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9496 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9497 | again: |
| 9498 | more = preadfd(); |
| 9499 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9500 | /* don't try reading again */ |
| 9501 | g_parsefile->left_in_line = -99; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9502 | pgetc_debug("preadbuffer PEOF2"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9503 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9504 | return PEOF; |
| 9505 | } |
| 9506 | } |
| 9507 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9508 | /* Find out where's the end of line. |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9509 | * Set g_parsefile->left_in_line |
| 9510 | * and g_parsefile->left_in_buffer acordingly. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9511 | * NUL chars are deleted. |
| 9512 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9513 | q = g_parsefile->next_to_pgetc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9514 | for (;;) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9515 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9516 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9517 | more--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9518 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9519 | c = *q; |
| 9520 | if (c == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9521 | memmove(q, q + 1, more); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9522 | } else { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9523 | q++; |
| 9524 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9525 | 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] | 9526 | break; |
| 9527 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9528 | } |
| 9529 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9530 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9531 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
| 9532 | if (g_parsefile->left_in_line < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9533 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9534 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9535 | } |
| 9536 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9537 | g_parsefile->left_in_buffer = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9538 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9539 | if (vflag) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9540 | char save = *q; |
| 9541 | *q = '\0'; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9542 | out2str(g_parsefile->next_to_pgetc); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9543 | *q = save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9544 | } |
| 9545 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9546 | pgetc_debug("preadbuffer at %d:%p'%s'", |
| 9547 | g_parsefile->left_in_line, |
| 9548 | g_parsefile->next_to_pgetc, |
| 9549 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 9550 | return signed_char2int(*g_parsefile->next_to_pgetc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9551 | } |
| 9552 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9553 | #define pgetc_as_macro() \ |
| 9554 | (--g_parsefile->left_in_line >= 0 \ |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 9555 | ? signed_char2int(*g_parsefile->next_to_pgetc++) \ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9556 | : preadbuffer() \ |
| 9557 | ) |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9558 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9559 | static int |
| 9560 | pgetc(void) |
| 9561 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9562 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9563 | g_parsefile->left_in_line, |
| 9564 | g_parsefile->next_to_pgetc, |
| 9565 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9566 | return pgetc_as_macro(); |
| 9567 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9568 | |
| 9569 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9570 | #define pgetc_fast() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9571 | #else |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9572 | #define pgetc_fast() pgetc_as_macro() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9573 | #endif |
| 9574 | |
| 9575 | /* |
| 9576 | * Same as pgetc(), but ignores PEOA. |
| 9577 | */ |
| 9578 | #if ENABLE_ASH_ALIAS |
| 9579 | static int |
| 9580 | pgetc2(void) |
| 9581 | { |
| 9582 | int c; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9583 | do { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9584 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9585 | g_parsefile->left_in_line, |
| 9586 | g_parsefile->next_to_pgetc, |
| 9587 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9588 | c = pgetc_fast(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9589 | } while (c == PEOA); |
| 9590 | return c; |
| 9591 | } |
| 9592 | #else |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9593 | #define pgetc2() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9594 | #endif |
| 9595 | |
| 9596 | /* |
| 9597 | * Read a line from the script. |
| 9598 | */ |
| 9599 | static char * |
| 9600 | pfgets(char *line, int len) |
| 9601 | { |
| 9602 | char *p = line; |
| 9603 | int nleft = len; |
| 9604 | int c; |
| 9605 | |
| 9606 | while (--nleft > 0) { |
| 9607 | c = pgetc2(); |
| 9608 | if (c == PEOF) { |
| 9609 | if (p == line) |
| 9610 | return NULL; |
| 9611 | break; |
| 9612 | } |
| 9613 | *p++ = c; |
| 9614 | if (c == '\n') |
| 9615 | break; |
| 9616 | } |
| 9617 | *p = '\0'; |
| 9618 | return line; |
| 9619 | } |
| 9620 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9621 | /* |
| 9622 | * Undo the last call to pgetc. Only one character may be pushed back. |
| 9623 | * PEOF may be pushed back. |
| 9624 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9625 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9626 | pungetc(void) |
| 9627 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9628 | g_parsefile->left_in_line++; |
| 9629 | g_parsefile->next_to_pgetc--; |
| 9630 | pgetc_debug("pushed back to %d:%p'%s'", |
| 9631 | g_parsefile->left_in_line, |
| 9632 | g_parsefile->next_to_pgetc, |
| 9633 | g_parsefile->next_to_pgetc); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9634 | } |
| 9635 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9636 | /* |
| 9637 | * To handle the "." command, a stack of input files is used. Pushfile |
| 9638 | * adds a new entry to the stack and popfile restores the previous level. |
| 9639 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9640 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9641 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9642 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9643 | struct parsefile *pf; |
| 9644 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9645 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9646 | pf->prev = g_parsefile; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9647 | pf->fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9648 | /*pf->strpush = NULL; - ckzalloc did it */ |
| 9649 | /*pf->basestrpush.prev = NULL;*/ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9650 | g_parsefile = pf; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9651 | } |
| 9652 | |
| 9653 | static void |
| 9654 | popfile(void) |
| 9655 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9656 | struct parsefile *pf = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9657 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9658 | INT_OFF; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9659 | if (pf->fd >= 0) |
| 9660 | close(pf->fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 9661 | free(pf->buf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9662 | while (pf->strpush) |
| 9663 | popstring(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9664 | g_parsefile = pf->prev; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9665 | free(pf); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9666 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9667 | } |
| 9668 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9669 | /* |
| 9670 | * Return to top level. |
| 9671 | */ |
| 9672 | static void |
| 9673 | popallfiles(void) |
| 9674 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9675 | while (g_parsefile != &basepf) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9676 | popfile(); |
| 9677 | } |
| 9678 | |
| 9679 | /* |
| 9680 | * Close the file(s) that the shell is reading commands from. Called |
| 9681 | * after a fork is done. |
| 9682 | */ |
| 9683 | static void |
| 9684 | closescript(void) |
| 9685 | { |
| 9686 | popallfiles(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9687 | if (g_parsefile->fd > 0) { |
| 9688 | close(g_parsefile->fd); |
| 9689 | g_parsefile->fd = 0; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9690 | } |
| 9691 | } |
| 9692 | |
| 9693 | /* |
| 9694 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 9695 | * interrupts off. |
| 9696 | */ |
| 9697 | static void |
| 9698 | setinputfd(int fd, int push) |
| 9699 | { |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 9700 | close_on_exec_on(fd); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9701 | if (push) { |
| 9702 | pushfile(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9703 | g_parsefile->buf = NULL; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9704 | } |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9705 | g_parsefile->fd = fd; |
| 9706 | if (g_parsefile->buf == NULL) |
| 9707 | g_parsefile->buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9708 | g_parsefile->left_in_buffer = 0; |
| 9709 | g_parsefile->left_in_line = 0; |
| 9710 | g_parsefile->linno = 1; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9711 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9712 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9713 | /* |
| 9714 | * Set the input to take input from a file. If push is set, push the |
| 9715 | * old input onto the stack first. |
| 9716 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9717 | static int |
| 9718 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9719 | { |
| 9720 | int fd; |
| 9721 | int fd2; |
| 9722 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9723 | INT_OFF; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9724 | fd = open(fname, O_RDONLY); |
| 9725 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9726 | if (flags & INPUT_NOFILE_OK) |
| 9727 | goto out; |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 9728 | ash_msg_and_raise_error("can't open '%s'", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9729 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9730 | if (fd < 10) { |
| 9731 | fd2 = copyfd(fd, 10); |
| 9732 | close(fd); |
| 9733 | if (fd2 < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 9734 | ash_msg_and_raise_error("out of file descriptors"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9735 | fd = fd2; |
| 9736 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9737 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9738 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9739 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9740 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9741 | } |
| 9742 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9743 | /* |
| 9744 | * Like setinputfile, but takes input from a string. |
| 9745 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9746 | static void |
| 9747 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9748 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9749 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9750 | pushfile(); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9751 | g_parsefile->next_to_pgetc = string; |
| 9752 | g_parsefile->left_in_line = strlen(string); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9753 | g_parsefile->buf = NULL; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9754 | g_parsefile->linno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9755 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9756 | } |
| 9757 | |
| 9758 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9759 | /* ============ mail.c |
| 9760 | * |
| 9761 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9762 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9763 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9764 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9765 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9766 | #define MAXMBOXES 10 |
| 9767 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9768 | /* times of mailboxes */ |
| 9769 | static time_t mailtime[MAXMBOXES]; |
| 9770 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9771 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9772 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9773 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9774 | * Print appropriate message(s) if mail has arrived. |
| 9775 | * If mail_var_path_changed is set, |
| 9776 | * then the value of MAIL has mail_var_path_changed, |
| 9777 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9778 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9779 | static void |
| 9780 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9781 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9782 | const char *mpath; |
| 9783 | char *p; |
| 9784 | char *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9785 | time_t *mtp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9786 | struct stackmark smark; |
| 9787 | struct stat statb; |
| 9788 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9789 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9790 | mpath = mpathset() ? mpathval() : mailval(); |
| 9791 | for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 9792 | p = path_advance(&mpath, nullstr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9793 | if (p == NULL) |
| 9794 | break; |
| 9795 | if (*p == '\0') |
| 9796 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9797 | for (q = p; *q; q++) |
| 9798 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 9799 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9800 | if (q[-1] != '/') |
| 9801 | abort(); |
| 9802 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9803 | q[-1] = '\0'; /* delete trailing '/' */ |
| 9804 | if (stat(p, &statb) < 0) { |
| 9805 | *mtp = 0; |
| 9806 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9807 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9808 | if (!mail_var_path_changed && statb.st_mtime != *mtp) { |
| 9809 | fprintf( |
| 9810 | stderr, snlfmt, |
| 9811 | pathopt ? pathopt : "you have mail" |
| 9812 | ); |
| 9813 | } |
| 9814 | *mtp = statb.st_mtime; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9815 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9816 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9817 | popstackmark(&smark); |
| 9818 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9819 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9820 | static void FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9821 | changemail(const char *val UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9822 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9823 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9824 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9825 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9826 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9827 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9828 | |
| 9829 | /* ============ ??? */ |
| 9830 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9831 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9832 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9833 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9834 | static void |
| 9835 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9836 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9837 | char **newparam; |
| 9838 | char **ap; |
| 9839 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9840 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9841 | for (nparam = 0; argv[nparam]; nparam++) |
| 9842 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9843 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 9844 | while (*argv) { |
| 9845 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9846 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9847 | *ap = NULL; |
| 9848 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9849 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9850 | shellparam.nparam = nparam; |
| 9851 | shellparam.p = newparam; |
| 9852 | #if ENABLE_ASH_GETOPTS |
| 9853 | shellparam.optind = 1; |
| 9854 | shellparam.optoff = -1; |
| 9855 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9856 | } |
| 9857 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9858 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9859 | * Process shell options. The global variable argptr contains a pointer |
| 9860 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 9861 | * |
| 9862 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 9863 | * For a non-interactive shell, an error condition encountered |
| 9864 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 9865 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 9866 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 9867 | * ... |
| 9868 | * Utility syntax error (option or operand error) Shall exit |
| 9869 | * ... |
| 9870 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 9871 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 9872 | * and shell continues), and people rely on this behavior! |
| 9873 | * Testcase: |
| 9874 | * set -o barfoo 2>/dev/null |
| 9875 | * echo $? |
| 9876 | * |
| 9877 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9878 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9879 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9880 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9881 | { |
| 9882 | int i; |
| 9883 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9884 | if (name) { |
| 9885 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 9886 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9887 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9888 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9889 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9890 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9891 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9892 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9893 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9894 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9895 | if (val) { |
| 9896 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 9897 | } else { |
| 9898 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 9899 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9900 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9901 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9902 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9903 | static void |
| 9904 | setoption(int flag, int val) |
| 9905 | { |
| 9906 | int i; |
| 9907 | |
| 9908 | for (i = 0; i < NOPTS; i++) { |
| 9909 | if (optletters(i) == flag) { |
| 9910 | optlist[i] = val; |
| 9911 | return; |
| 9912 | } |
| 9913 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9914 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9915 | /* NOTREACHED */ |
| 9916 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9917 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9918 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9919 | { |
| 9920 | char *p; |
| 9921 | int val; |
| 9922 | int c; |
| 9923 | |
| 9924 | if (cmdline) |
| 9925 | minusc = NULL; |
| 9926 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9927 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9928 | if (c != '-' && c != '+') |
| 9929 | break; |
| 9930 | argptr++; |
| 9931 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9932 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9933 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 9934 | if (p[0] == '\0' || LONE_DASH(p)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9935 | if (!cmdline) { |
| 9936 | /* "-" means turn off -x and -v */ |
| 9937 | if (p[0] == '\0') |
| 9938 | xflag = vflag = 0; |
| 9939 | /* "--" means reset params */ |
| 9940 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9941 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9942 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9943 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9944 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9945 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9946 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9947 | while ((c = *p++) != '\0') { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9948 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9949 | if (c == 'c' && cmdline) { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9950 | minusc = p; /* command is after shell args */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9951 | } else if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9952 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9953 | /* it already printed err message */ |
| 9954 | return 1; /* error */ |
| 9955 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9956 | if (*argptr) |
| 9957 | argptr++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9958 | } else if (cmdline && (c == 'l')) { /* -l or +l == --login */ |
| 9959 | isloginsh = 1; |
| 9960 | /* bash does not accept +-login, we also won't */ |
| 9961 | } else if (cmdline && val && (c == '-')) { /* long options */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9962 | if (strcmp(p, "login") == 0) |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 9963 | isloginsh = 1; |
| 9964 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9965 | } else { |
| 9966 | setoption(c, val); |
| 9967 | } |
| 9968 | } |
| 9969 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9970 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9971 | } |
| 9972 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9973 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9974 | * The shift builtin command. |
| 9975 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9976 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9977 | shiftcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9978 | { |
| 9979 | int n; |
| 9980 | char **ap1, **ap2; |
| 9981 | |
| 9982 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9983 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9984 | n = number(argv[1]); |
| 9985 | if (n > shellparam.nparam) |
Denis Vlasenko | c90e1be | 2008-07-30 15:35:05 +0000 | [diff] [blame] | 9986 | n = 0; /* bash compat, was = shellparam.nparam; */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9987 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9988 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9989 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9990 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9991 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9992 | } |
| 9993 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9994 | while ((*ap2++ = *ap1++) != NULL) |
| 9995 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9996 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9997 | shellparam.optind = 1; |
| 9998 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9999 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10000 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10001 | return 0; |
| 10002 | } |
| 10003 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10004 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10005 | * POSIX requires that 'set' (but not export or readonly) output the |
| 10006 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 10007 | * Maybe we could keep them in an ordered balanced binary tree |
| 10008 | * instead of hashed lists. |
| 10009 | * For now just roll 'em through qsort for printing... |
| 10010 | */ |
| 10011 | static int |
| 10012 | showvars(const char *sep_prefix, int on, int off) |
| 10013 | { |
| 10014 | const char *sep; |
| 10015 | char **ep, **epend; |
| 10016 | |
| 10017 | ep = listvars(on, off, &epend); |
| 10018 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 10019 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10020 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10021 | |
| 10022 | for (; ep < epend; ep++) { |
| 10023 | const char *p; |
| 10024 | const char *q; |
| 10025 | |
| 10026 | p = strchrnul(*ep, '='); |
| 10027 | q = nullstr; |
| 10028 | if (*p) |
| 10029 | q = single_quote(++p); |
| 10030 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 10031 | } |
| 10032 | return 0; |
| 10033 | } |
| 10034 | |
| 10035 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10036 | * The set command builtin. |
| 10037 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10038 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10039 | setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10040 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10041 | int retval; |
| 10042 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10043 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10044 | return showvars(nullstr, 0, VUNSET); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10045 | INT_OFF; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10046 | retval = 1; |
| 10047 | if (!options(0)) { /* if no parse error... */ |
| 10048 | retval = 0; |
| 10049 | optschanged(); |
| 10050 | if (*argptr != NULL) { |
| 10051 | setparam(argptr); |
| 10052 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10053 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10054 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10055 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10056 | } |
| 10057 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10058 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10059 | static void FAST_FUNC |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 10060 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10061 | { |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 10062 | /* Galois LFSR parameter */ |
| 10063 | /* Taps at 32 31 29 1: */ |
| 10064 | enum { MASK = 0x8000000b }; |
| 10065 | /* Another example - taps at 32 31 30 10: */ |
| 10066 | /* MASK = 0x00400007 */ |
| 10067 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10068 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10069 | /* "get", generate */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 10070 | uint32_t t; |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10071 | |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 10072 | /* LCG has period of 2^32 and alternating lowest bit */ |
| 10073 | random_LCG = 1664525 * random_LCG + 1013904223; |
| 10074 | /* Galois LFSR has period of 2^32-1 = 3 * 5 * 17 * 257 * 65537 */ |
| 10075 | t = (random_galois_LFSR << 1); |
| 10076 | if (random_galois_LFSR < 0) /* if we just shifted 1 out of msb... */ |
| 10077 | t ^= MASK; |
| 10078 | random_galois_LFSR = t; |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 10079 | /* Both are weak, combining them gives better randomness |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 10080 | * and ~2^64 period. & 0x7fff is probably bash compat |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 10081 | * for $RANDOM range. Combining with subtraction is |
| 10082 | * just for fun. + and ^ would work equally well. */ |
| 10083 | t = (t - random_LCG) & 0x7fff; |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10084 | /* set without recursion */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 10085 | setvar(vrandom.text, utoa(t), VNOFUNC); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10086 | vrandom.flags &= ~VNOFUNC; |
| 10087 | } else { |
| 10088 | /* set/reset */ |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 10089 | random_galois_LFSR = random_LCG = strtoul(value, NULL, 10); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10090 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10091 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10092 | #endif |
| 10093 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10094 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10095 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10096 | getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10097 | { |
| 10098 | char *p, *q; |
| 10099 | char c = '?'; |
| 10100 | int done = 0; |
| 10101 | int err = 0; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10102 | char s[12]; |
| 10103 | char **optnext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10104 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10105 | if (*param_optind < 1) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10106 | return 1; |
| 10107 | optnext = optfirst + *param_optind - 1; |
| 10108 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10109 | if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10110 | p = NULL; |
| 10111 | else |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10112 | p = optnext[-1] + *optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10113 | if (p == NULL || *p == '\0') { |
| 10114 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10115 | p = *optnext; |
| 10116 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10117 | atend: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10118 | p = NULL; |
| 10119 | done = 1; |
| 10120 | goto out; |
| 10121 | } |
| 10122 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10123 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10124 | goto atend; |
| 10125 | } |
| 10126 | |
| 10127 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 10128 | for (q = optstr; *q != c;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10129 | if (*q == '\0') { |
| 10130 | if (optstr[0] == ':') { |
| 10131 | s[0] = c; |
| 10132 | s[1] = '\0'; |
| 10133 | err |= setvarsafe("OPTARG", s, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10134 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10135 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10136 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10137 | } |
| 10138 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10139 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10140 | } |
| 10141 | if (*++q == ':') |
| 10142 | q++; |
| 10143 | } |
| 10144 | |
| 10145 | if (*++q == ':') { |
| 10146 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 10147 | if (optstr[0] == ':') { |
| 10148 | s[0] = c; |
| 10149 | s[1] = '\0'; |
| 10150 | err |= setvarsafe("OPTARG", s, 0); |
| 10151 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10152 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10153 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10154 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10155 | c = '?'; |
| 10156 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10157 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10158 | } |
| 10159 | |
| 10160 | if (p == *optnext) |
| 10161 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10162 | err |= setvarsafe("OPTARG", p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10163 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10164 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10165 | err |= setvarsafe("OPTARG", nullstr, 0); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10166 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10167 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10168 | *param_optind = optnext - optfirst + 1; |
| 10169 | fmtstr(s, sizeof(s), "%d", *param_optind); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10170 | err |= setvarsafe("OPTIND", s, VNOFUNC); |
| 10171 | s[0] = c; |
| 10172 | s[1] = '\0'; |
| 10173 | err |= setvarsafe(optvar, s, 0); |
| 10174 | if (err) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10175 | *param_optind = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10176 | *optoff = -1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10177 | flush_stdout_stderr(); |
| 10178 | raise_exception(EXERROR); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10179 | } |
| 10180 | return done; |
| 10181 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10182 | |
| 10183 | /* |
| 10184 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 10185 | * to be processed. Shellparam.optptr points to the next character to |
| 10186 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 10187 | * then it's the first time getopts has been called. |
| 10188 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10189 | static int FAST_FUNC |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10190 | getoptscmd(int argc, char **argv) |
| 10191 | { |
| 10192 | char **optbase; |
| 10193 | |
| 10194 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 10195 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10196 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10197 | optbase = shellparam.p; |
| 10198 | if (shellparam.optind > shellparam.nparam + 1) { |
| 10199 | shellparam.optind = 1; |
| 10200 | shellparam.optoff = -1; |
| 10201 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10202 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10203 | optbase = &argv[3]; |
| 10204 | if (shellparam.optind > argc - 2) { |
| 10205 | shellparam.optind = 1; |
| 10206 | shellparam.optoff = -1; |
| 10207 | } |
| 10208 | } |
| 10209 | |
| 10210 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10211 | &shellparam.optoff); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10212 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10213 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10214 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10215 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10216 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10217 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10218 | struct heredoc { |
| 10219 | struct heredoc *next; /* next here document in list */ |
| 10220 | union node *here; /* redirection node */ |
| 10221 | char *eofmark; /* string indicating end of input */ |
| 10222 | smallint striptabs; /* if set, strip leading tabs */ |
| 10223 | }; |
| 10224 | |
| 10225 | static smallint tokpushback; /* last token pushed back */ |
| 10226 | static smallint parsebackquote; /* nonzero if we are inside backquotes */ |
| 10227 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
| 10228 | static token_id_t lasttoken; /* last token read (integer id Txxx) */ |
| 10229 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 10230 | static char *wordtext; /* text of last word returned by readtoken */ |
| 10231 | static struct nodelist *backquotelist; |
| 10232 | static union node *redirnode; |
| 10233 | static struct heredoc *heredoc; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10234 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10235 | /* |
| 10236 | * Called when an unexpected token is read during the parse. The argument |
| 10237 | * is the token that is expected, or -1 if more than one type of token can |
| 10238 | * occur at this point. |
| 10239 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10240 | static void raise_error_unexpected_syntax(int) NORETURN; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10241 | static void |
| 10242 | raise_error_unexpected_syntax(int token) |
| 10243 | { |
| 10244 | char msg[64]; |
| 10245 | int l; |
| 10246 | |
Denis Vlasenko | 7b2294e | 2008-11-28 03:50:46 +0000 | [diff] [blame] | 10247 | l = sprintf(msg, "unexpected %s", tokname(lasttoken)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10248 | if (token >= 0) |
| 10249 | sprintf(msg + l, " (expecting %s)", tokname(token)); |
| 10250 | raise_error_syntax(msg); |
| 10251 | /* NOTREACHED */ |
| 10252 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10253 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10254 | #define EOFMARKLEN 79 |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10255 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10256 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 10257 | static union node *andor(void); |
| 10258 | static union node *pipeline(void); |
| 10259 | static union node *parse_command(void); |
| 10260 | static void parseheredoc(void); |
| 10261 | static char peektoken(void); |
| 10262 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10263 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10264 | static union node * |
| 10265 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10266 | { |
| 10267 | union node *n1, *n2, *n3; |
| 10268 | int tok; |
| 10269 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10270 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10271 | if (nlflag == 2 && peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10272 | return NULL; |
| 10273 | n1 = NULL; |
| 10274 | for (;;) { |
| 10275 | n2 = andor(); |
| 10276 | tok = readtoken(); |
| 10277 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10278 | if (n2->type == NPIPE) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10279 | n2->npipe.pipe_backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10280 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10281 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10282 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10283 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10284 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10285 | n2 = n3; |
| 10286 | } |
| 10287 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10288 | } |
| 10289 | } |
| 10290 | if (n1 == NULL) { |
| 10291 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10292 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10293 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10294 | n3->type = NSEMI; |
| 10295 | n3->nbinary.ch1 = n1; |
| 10296 | n3->nbinary.ch2 = n2; |
| 10297 | n1 = n3; |
| 10298 | } |
| 10299 | switch (tok) { |
| 10300 | case TBACKGND: |
| 10301 | case TSEMI: |
| 10302 | tok = readtoken(); |
| 10303 | /* fall through */ |
| 10304 | case TNL: |
| 10305 | if (tok == TNL) { |
| 10306 | parseheredoc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10307 | if (nlflag == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10308 | return n1; |
| 10309 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10310 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10311 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10312 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10313 | if (peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10314 | return n1; |
| 10315 | break; |
| 10316 | case TEOF: |
| 10317 | if (heredoclist) |
| 10318 | parseheredoc(); |
| 10319 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10320 | pungetc(); /* push back EOF on input */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10321 | return n1; |
| 10322 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10323 | if (nlflag == 1) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10324 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10325 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10326 | return n1; |
| 10327 | } |
| 10328 | } |
| 10329 | } |
| 10330 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10331 | static union node * |
| 10332 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10333 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10334 | union node *n1, *n2, *n3; |
| 10335 | int t; |
| 10336 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10337 | n1 = pipeline(); |
| 10338 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10339 | t = readtoken(); |
| 10340 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10341 | t = NAND; |
| 10342 | } else if (t == TOR) { |
| 10343 | t = NOR; |
| 10344 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10345 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10346 | return n1; |
| 10347 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10348 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10349 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10350 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10351 | n3->type = t; |
| 10352 | n3->nbinary.ch1 = n1; |
| 10353 | n3->nbinary.ch2 = n2; |
| 10354 | n1 = n3; |
| 10355 | } |
| 10356 | } |
| 10357 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10358 | static union node * |
| 10359 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10360 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10361 | union node *n1, *n2, *pipenode; |
| 10362 | struct nodelist *lp, *prev; |
| 10363 | int negate; |
| 10364 | |
| 10365 | negate = 0; |
| 10366 | TRACE(("pipeline: entered\n")); |
| 10367 | if (readtoken() == TNOT) { |
| 10368 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10369 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10370 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10371 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10372 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10373 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10374 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10375 | pipenode->type = NPIPE; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10376 | /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10377 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10378 | pipenode->npipe.cmdlist = lp; |
| 10379 | lp->n = n1; |
| 10380 | do { |
| 10381 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10382 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10383 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10384 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10385 | prev->next = lp; |
| 10386 | } while (readtoken() == TPIPE); |
| 10387 | lp->next = NULL; |
| 10388 | n1 = pipenode; |
| 10389 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10390 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10391 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10392 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10393 | n2->type = NNOT; |
| 10394 | n2->nnot.com = n1; |
| 10395 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10396 | } |
| 10397 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10398 | } |
| 10399 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10400 | static union node * |
| 10401 | makename(void) |
| 10402 | { |
| 10403 | union node *n; |
| 10404 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10405 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10406 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10407 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10408 | n->narg.text = wordtext; |
| 10409 | n->narg.backquote = backquotelist; |
| 10410 | return n; |
| 10411 | } |
| 10412 | |
| 10413 | static void |
| 10414 | fixredir(union node *n, const char *text, int err) |
| 10415 | { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10416 | int fd; |
| 10417 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10418 | TRACE(("Fix redir %s %d\n", text, err)); |
| 10419 | if (!err) |
| 10420 | n->ndup.vname = NULL; |
| 10421 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10422 | fd = bb_strtou(text, NULL, 10); |
| 10423 | if (!errno && fd >= 0) |
| 10424 | n->ndup.dupfd = fd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10425 | else if (LONE_DASH(text)) |
| 10426 | n->ndup.dupfd = -1; |
| 10427 | else { |
| 10428 | if (err) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10429 | raise_error_syntax("bad fd number"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10430 | n->ndup.vname = makename(); |
| 10431 | } |
| 10432 | } |
| 10433 | |
| 10434 | /* |
| 10435 | * Returns true if the text contains nothing to expand (no dollar signs |
| 10436 | * or backquotes). |
| 10437 | */ |
| 10438 | static int |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10439 | noexpand(const char *text) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10440 | { |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10441 | const char *p; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10442 | char c; |
| 10443 | |
| 10444 | p = text; |
| 10445 | while ((c = *p++) != '\0') { |
| 10446 | if (c == CTLQUOTEMARK) |
| 10447 | continue; |
| 10448 | if (c == CTLESC) |
| 10449 | p++; |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10450 | else if (SIT((signed char)c, BASESYNTAX) == CCTL) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10451 | return 0; |
| 10452 | } |
| 10453 | return 1; |
| 10454 | } |
| 10455 | |
| 10456 | static void |
| 10457 | parsefname(void) |
| 10458 | { |
| 10459 | union node *n = redirnode; |
| 10460 | |
| 10461 | if (readtoken() != TWORD) |
| 10462 | raise_error_unexpected_syntax(-1); |
| 10463 | if (n->type == NHERE) { |
| 10464 | struct heredoc *here = heredoc; |
| 10465 | struct heredoc *p; |
| 10466 | int i; |
| 10467 | |
| 10468 | if (quoteflag == 0) |
| 10469 | n->type = NXHERE; |
| 10470 | TRACE(("Here document %d\n", n->type)); |
| 10471 | if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10472 | raise_error_syntax("illegal eof marker for << redirection"); |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 10473 | rmescapes(wordtext, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10474 | here->eofmark = wordtext; |
| 10475 | here->next = NULL; |
| 10476 | if (heredoclist == NULL) |
| 10477 | heredoclist = here; |
| 10478 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10479 | for (p = heredoclist; p->next; p = p->next) |
| 10480 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10481 | p->next = here; |
| 10482 | } |
| 10483 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 10484 | fixredir(n, wordtext, 0); |
| 10485 | } else { |
| 10486 | n->nfile.fname = makename(); |
| 10487 | } |
| 10488 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10489 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10490 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10491 | simplecmd(void) |
| 10492 | { |
| 10493 | union node *args, **app; |
| 10494 | union node *n = NULL; |
| 10495 | union node *vars, **vpp; |
| 10496 | union node **rpp, *redir; |
| 10497 | int savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10498 | #if ENABLE_ASH_BASH_COMPAT |
| 10499 | smallint double_brackets_flag = 0; |
| 10500 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10501 | |
| 10502 | args = NULL; |
| 10503 | app = &args; |
| 10504 | vars = NULL; |
| 10505 | vpp = &vars; |
| 10506 | redir = NULL; |
| 10507 | rpp = &redir; |
| 10508 | |
| 10509 | savecheckkwd = CHKALIAS; |
| 10510 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10511 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10512 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10513 | t = readtoken(); |
| 10514 | switch (t) { |
| 10515 | #if ENABLE_ASH_BASH_COMPAT |
| 10516 | case TAND: /* "&&" */ |
| 10517 | case TOR: /* "||" */ |
| 10518 | if (!double_brackets_flag) { |
| 10519 | tokpushback = 1; |
| 10520 | goto out; |
| 10521 | } |
| 10522 | wordtext = (char *) (t == TAND ? "-a" : "-o"); |
| 10523 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10524 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10525 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10526 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10527 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10528 | n->narg.text = wordtext; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10529 | #if ENABLE_ASH_BASH_COMPAT |
| 10530 | if (strcmp("[[", wordtext) == 0) |
| 10531 | double_brackets_flag = 1; |
| 10532 | else if (strcmp("]]", wordtext) == 0) |
| 10533 | double_brackets_flag = 0; |
| 10534 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10535 | n->narg.backquote = backquotelist; |
| 10536 | if (savecheckkwd && isassignment(wordtext)) { |
| 10537 | *vpp = n; |
| 10538 | vpp = &n->narg.next; |
| 10539 | } else { |
| 10540 | *app = n; |
| 10541 | app = &n->narg.next; |
| 10542 | savecheckkwd = 0; |
| 10543 | } |
| 10544 | break; |
| 10545 | case TREDIR: |
| 10546 | *rpp = n = redirnode; |
| 10547 | rpp = &n->nfile.next; |
| 10548 | parsefname(); /* read name of redirection file */ |
| 10549 | break; |
| 10550 | case TLP: |
| 10551 | if (args && app == &args->narg.next |
| 10552 | && !vars && !redir |
| 10553 | ) { |
| 10554 | struct builtincmd *bcmd; |
| 10555 | const char *name; |
| 10556 | |
| 10557 | /* We have a function */ |
| 10558 | if (readtoken() != TRP) |
| 10559 | raise_error_unexpected_syntax(TRP); |
| 10560 | name = n->narg.text; |
| 10561 | if (!goodname(name) |
| 10562 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 10563 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10564 | raise_error_syntax("bad function name"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10565 | } |
| 10566 | n->type = NDEFUN; |
| 10567 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10568 | n->narg.next = parse_command(); |
| 10569 | return n; |
| 10570 | } |
| 10571 | /* fall through */ |
| 10572 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10573 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10574 | goto out; |
| 10575 | } |
| 10576 | } |
| 10577 | out: |
| 10578 | *app = NULL; |
| 10579 | *vpp = NULL; |
| 10580 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10581 | n = stzalloc(sizeof(struct ncmd)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10582 | n->type = NCMD; |
| 10583 | n->ncmd.args = args; |
| 10584 | n->ncmd.assign = vars; |
| 10585 | n->ncmd.redirect = redir; |
| 10586 | return n; |
| 10587 | } |
| 10588 | |
| 10589 | static union node * |
| 10590 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10591 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10592 | union node *n1, *n2; |
| 10593 | union node *ap, **app; |
| 10594 | union node *cp, **cpp; |
| 10595 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10596 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10597 | int t; |
| 10598 | |
| 10599 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10600 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 10601 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10602 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10603 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10604 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10605 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10606 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10607 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10608 | n1->type = NIF; |
| 10609 | n1->nif.test = list(0); |
| 10610 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10611 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10612 | n1->nif.ifpart = list(0); |
| 10613 | n2 = n1; |
| 10614 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10615 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10616 | n2 = n2->nif.elsepart; |
| 10617 | n2->type = NIF; |
| 10618 | n2->nif.test = list(0); |
| 10619 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10620 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10621 | n2->nif.ifpart = list(0); |
| 10622 | } |
| 10623 | if (lasttoken == TELSE) |
| 10624 | n2->nif.elsepart = list(0); |
| 10625 | else { |
| 10626 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10627 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10628 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10629 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10630 | break; |
| 10631 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10632 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10633 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10634 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10635 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10636 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10637 | got = readtoken(); |
| 10638 | if (got != TDO) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10639 | TRACE(("expecting DO got %s %s\n", tokname(got), |
| 10640 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10641 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10642 | } |
| 10643 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10644 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10645 | break; |
| 10646 | } |
| 10647 | case TFOR: |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10648 | if (readtoken() != TWORD || quoteflag || !goodname(wordtext)) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10649 | raise_error_syntax("bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10650 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10651 | n1->type = NFOR; |
| 10652 | n1->nfor.var = wordtext; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10653 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10654 | if (readtoken() == TIN) { |
| 10655 | app = ≈ |
| 10656 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10657 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10658 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10659 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10660 | n2->narg.text = wordtext; |
| 10661 | n2->narg.backquote = backquotelist; |
| 10662 | *app = n2; |
| 10663 | app = &n2->narg.next; |
| 10664 | } |
| 10665 | *app = NULL; |
| 10666 | n1->nfor.args = ap; |
| 10667 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10668 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10669 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10670 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10671 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10672 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10673 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10674 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10675 | n1->nfor.args = n2; |
| 10676 | /* |
| 10677 | * Newline or semicolon here is optional (but note |
| 10678 | * that the original Bourne shell only allowed NL). |
| 10679 | */ |
| 10680 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10681 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10682 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10683 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10684 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10685 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10686 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10687 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10688 | break; |
| 10689 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10690 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10691 | n1->type = NCASE; |
| 10692 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10693 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10694 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10695 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10696 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10697 | n2->narg.text = wordtext; |
| 10698 | n2->narg.backquote = backquotelist; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10699 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10700 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10701 | } while (readtoken() == TNL); |
| 10702 | if (lasttoken != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10703 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10704 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10705 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10706 | checkkwd = CHKNL | CHKKWD; |
| 10707 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10708 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10709 | if (lasttoken == TLP) |
| 10710 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10711 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10712 | cp->type = NCLIST; |
| 10713 | app = &cp->nclist.pattern; |
| 10714 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10715 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10716 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10717 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10718 | ap->narg.text = wordtext; |
| 10719 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10720 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10721 | break; |
| 10722 | app = &ap->narg.next; |
| 10723 | readtoken(); |
| 10724 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10725 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10726 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10727 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10728 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10729 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10730 | cpp = &cp->nclist.next; |
| 10731 | |
| 10732 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10733 | t = readtoken(); |
| 10734 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10735 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10736 | raise_error_unexpected_syntax(TENDCASE); |
| 10737 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10738 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10739 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10740 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10741 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10742 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10743 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10744 | n1->type = NSUBSHELL; |
| 10745 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10746 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10747 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10748 | break; |
| 10749 | case TBEGIN: |
| 10750 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10751 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10752 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10753 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10754 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10755 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10756 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10757 | } |
| 10758 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10759 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10760 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10761 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10762 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10763 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10764 | checkkwd = CHKKWD | CHKALIAS; |
| 10765 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10766 | while (readtoken() == TREDIR) { |
| 10767 | *rpp = n2 = redirnode; |
| 10768 | rpp = &n2->nfile.next; |
| 10769 | parsefname(); |
| 10770 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10771 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10772 | *rpp = NULL; |
| 10773 | if (redir) { |
| 10774 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10775 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10776 | n2->type = NREDIR; |
| 10777 | n2->nredir.n = n1; |
| 10778 | n1 = n2; |
| 10779 | } |
| 10780 | n1->nredir.redirect = redir; |
| 10781 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10782 | return n1; |
| 10783 | } |
| 10784 | |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10785 | #if ENABLE_ASH_BASH_COMPAT |
| 10786 | static int decode_dollar_squote(void) |
| 10787 | { |
| 10788 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 10789 | int c, cnt; |
| 10790 | char *p; |
| 10791 | char buf[4]; |
| 10792 | |
| 10793 | c = pgetc(); |
| 10794 | p = strchr(C_escapes, c); |
| 10795 | if (p) { |
| 10796 | buf[0] = c; |
| 10797 | p = buf; |
| 10798 | cnt = 3; |
| 10799 | if ((unsigned char)(c - '0') <= 7) { /* \ooo */ |
| 10800 | do { |
| 10801 | c = pgetc(); |
| 10802 | *++p = c; |
| 10803 | } while ((unsigned char)(c - '0') <= 7 && --cnt); |
| 10804 | pungetc(); |
| 10805 | } else if (c == 'x') { /* \xHH */ |
| 10806 | do { |
| 10807 | c = pgetc(); |
| 10808 | *++p = c; |
| 10809 | } while (isxdigit(c) && --cnt); |
| 10810 | pungetc(); |
| 10811 | if (cnt == 3) { /* \x but next char is "bad" */ |
| 10812 | c = 'x'; |
| 10813 | goto unrecognized; |
| 10814 | } |
| 10815 | } else { /* simple seq like \\ or \t */ |
| 10816 | p++; |
| 10817 | } |
| 10818 | *p = '\0'; |
| 10819 | p = buf; |
| 10820 | c = bb_process_escape_sequence((void*)&p); |
| 10821 | } else { /* unrecognized "\z": print both chars unless ' or " */ |
| 10822 | if (c != '\'' && c != '"') { |
| 10823 | unrecognized: |
| 10824 | c |= 0x100; /* "please encode \, then me" */ |
| 10825 | } |
| 10826 | } |
| 10827 | return c; |
| 10828 | } |
| 10829 | #endif |
| 10830 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10831 | /* |
| 10832 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 10833 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 10834 | * word which marks the end of the document and striptabs is true if |
| 10835 | * leading tabs should be stripped from the document. The argument firstc |
| 10836 | * is the first character of the input token or document. |
| 10837 | * |
| 10838 | * Because C does not have internal subroutines, I have simulated them |
| 10839 | * using goto's to implement the subroutine linkage. The following macros |
| 10840 | * will run code that appears at the end of readtoken1. |
| 10841 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10842 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 10843 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 10844 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 10845 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 10846 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 10847 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10848 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10849 | readtoken1(int firstc, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10850 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10851 | /* NB: syntax parameter fits into smallint */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10852 | int c = firstc; |
| 10853 | char *out; |
| 10854 | int len; |
| 10855 | char line[EOFMARKLEN + 1]; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10856 | struct nodelist *bqlist; |
| 10857 | smallint quotef; |
| 10858 | smallint dblquote; |
| 10859 | smallint oldstyle; |
| 10860 | smallint prevsyntax; /* syntax before arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10861 | #if ENABLE_ASH_EXPAND_PRMT |
| 10862 | smallint pssyntax; /* we are expanding a prompt string */ |
| 10863 | #endif |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10864 | int varnest; /* levels of variables expansion */ |
| 10865 | int arinest; /* levels of arithmetic expansion */ |
| 10866 | int parenlevel; /* levels of parens in arithmetic */ |
| 10867 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 10868 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 10869 | IF_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10870 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10871 | #if __GNUC__ |
| 10872 | /* Avoid longjmp clobbering */ |
| 10873 | (void) &out; |
| 10874 | (void) "ef; |
| 10875 | (void) &dblquote; |
| 10876 | (void) &varnest; |
| 10877 | (void) &arinest; |
| 10878 | (void) &parenlevel; |
| 10879 | (void) &dqvarnest; |
| 10880 | (void) &oldstyle; |
| 10881 | (void) &prevsyntax; |
| 10882 | (void) &syntax; |
| 10883 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10884 | startlinno = g_parsefile->linno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10885 | bqlist = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10886 | quotef = 0; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10887 | oldstyle = 0; |
| 10888 | prevsyntax = 0; |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10889 | #if ENABLE_ASH_EXPAND_PRMT |
| 10890 | pssyntax = (syntax == PSSYNTAX); |
| 10891 | if (pssyntax) |
| 10892 | syntax = DQSYNTAX; |
| 10893 | #endif |
| 10894 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10895 | varnest = 0; |
| 10896 | arinest = 0; |
| 10897 | parenlevel = 0; |
| 10898 | dqvarnest = 0; |
| 10899 | |
| 10900 | STARTSTACKSTR(out); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 10901 | loop: |
| 10902 | /* For each line, until end of word */ |
| 10903 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10904 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 10905 | for (;;) { /* until end of line or end of word */ |
| 10906 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 10907 | switch (SIT(c, syntax)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10908 | case CNL: /* '\n' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10909 | if (syntax == BASESYNTAX) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10910 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10911 | USTPUTC(c, out); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10912 | g_parsefile->linno++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10913 | if (doprompt) |
| 10914 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10915 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10916 | goto loop; /* continue outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10917 | case CWORD: |
| 10918 | USTPUTC(c, out); |
| 10919 | break; |
| 10920 | case CCTL: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10921 | if (eofmark == NULL || dblquote) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10922 | USTPUTC(CTLESC, out); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10923 | #if ENABLE_ASH_BASH_COMPAT |
| 10924 | if (c == '\\' && bash_dollar_squote) { |
| 10925 | c = decode_dollar_squote(); |
| 10926 | if (c & 0x100) { |
| 10927 | USTPUTC('\\', out); |
| 10928 | c = (unsigned char)c; |
| 10929 | } |
| 10930 | } |
| 10931 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10932 | USTPUTC(c, out); |
| 10933 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10934 | case CBACK: /* backslash */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10935 | c = pgetc2(); |
| 10936 | if (c == PEOF) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10937 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10938 | USTPUTC('\\', out); |
| 10939 | pungetc(); |
| 10940 | } else if (c == '\n') { |
| 10941 | if (doprompt) |
| 10942 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10943 | } else { |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10944 | #if ENABLE_ASH_EXPAND_PRMT |
| 10945 | if (c == '$' && pssyntax) { |
| 10946 | USTPUTC(CTLESC, out); |
| 10947 | USTPUTC('\\', out); |
| 10948 | } |
| 10949 | #endif |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10950 | if (dblquote && c != '\\' |
| 10951 | && c != '`' && c != '$' |
| 10952 | && (c != '"' || eofmark != NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10953 | ) { |
| 10954 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10955 | USTPUTC('\\', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10956 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10957 | if (SIT(c, SQSYNTAX) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10958 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10959 | USTPUTC(c, out); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10960 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10961 | } |
| 10962 | break; |
| 10963 | case CSQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10964 | syntax = SQSYNTAX; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10965 | quotemark: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10966 | if (eofmark == NULL) { |
| 10967 | USTPUTC(CTLQUOTEMARK, out); |
| 10968 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10969 | break; |
| 10970 | case CDQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10971 | syntax = DQSYNTAX; |
| 10972 | dblquote = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10973 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10974 | case CENDQUOTE: |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 10975 | IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10976 | if (eofmark != NULL && arinest == 0 |
| 10977 | && varnest == 0 |
| 10978 | ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10979 | USTPUTC(c, out); |
| 10980 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10981 | if (dqvarnest == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10982 | syntax = BASESYNTAX; |
| 10983 | dblquote = 0; |
| 10984 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10985 | quotef = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10986 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10987 | } |
| 10988 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10989 | case CVAR: /* '$' */ |
| 10990 | PARSESUB(); /* parse substitution */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10991 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10992 | case CENDVAR: /* '}' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10993 | if (varnest > 0) { |
| 10994 | varnest--; |
| 10995 | if (dqvarnest > 0) { |
| 10996 | dqvarnest--; |
| 10997 | } |
| 10998 | USTPUTC(CTLENDVAR, out); |
| 10999 | } else { |
| 11000 | USTPUTC(c, out); |
| 11001 | } |
| 11002 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11003 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11004 | case CLP: /* '(' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11005 | parenlevel++; |
| 11006 | USTPUTC(c, out); |
| 11007 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11008 | case CRP: /* ')' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11009 | if (parenlevel > 0) { |
| 11010 | USTPUTC(c, out); |
| 11011 | --parenlevel; |
| 11012 | } else { |
| 11013 | if (pgetc() == ')') { |
| 11014 | if (--arinest == 0) { |
| 11015 | USTPUTC(CTLENDARI, out); |
| 11016 | syntax = prevsyntax; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11017 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11018 | } else |
| 11019 | USTPUTC(')', out); |
| 11020 | } else { |
| 11021 | /* |
| 11022 | * unbalanced parens |
| 11023 | * (don't 2nd guess - no error) |
| 11024 | */ |
| 11025 | pungetc(); |
| 11026 | USTPUTC(')', out); |
| 11027 | } |
| 11028 | } |
| 11029 | break; |
| 11030 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11031 | case CBQUOTE: /* '`' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11032 | PARSEBACKQOLD(); |
| 11033 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11034 | case CENDFILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11035 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11036 | case CIGN: |
| 11037 | break; |
| 11038 | default: |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11039 | if (varnest == 0) { |
| 11040 | #if ENABLE_ASH_BASH_COMPAT |
| 11041 | if (c == '&') { |
| 11042 | if (pgetc() == '>') |
| 11043 | c = 0x100 + '>'; /* flag &> */ |
| 11044 | pungetc(); |
| 11045 | } |
| 11046 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11047 | goto endword; /* exit outer loop */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11048 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11049 | #if ENABLE_ASH_ALIAS |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 11050 | if (c != PEOA) |
| 11051 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11052 | USTPUTC(c, out); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 11053 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11054 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11055 | c = pgetc_fast(); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11056 | } /* for (;;) */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11057 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11058 | endword: |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11059 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11060 | if (syntax == ARISYNTAX) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11061 | raise_error_syntax("missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11062 | #endif |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 11063 | if (syntax != BASESYNTAX && !parsebackquote && eofmark == NULL) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11064 | raise_error_syntax("unterminated quoted string"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11065 | if (varnest != 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11066 | startlinno = g_parsefile->linno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11067 | /* { */ |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11068 | raise_error_syntax("missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11069 | } |
| 11070 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11071 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11072 | out = stackblock(); |
| 11073 | if (eofmark == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11074 | if ((c == '>' || c == '<' IF_ASH_BASH_COMPAT( || c == 0x100 + '>')) |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11075 | && quotef == 0 |
| 11076 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11077 | if (isdigit_str9(out)) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11078 | PARSEREDIR(); /* passed as params: out, c */ |
| 11079 | lasttoken = TREDIR; |
| 11080 | return lasttoken; |
| 11081 | } |
| 11082 | /* else: non-number X seen, interpret it |
| 11083 | * as "NNNX>file" = "NNNX >file" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11084 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11085 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11086 | } |
| 11087 | quoteflag = quotef; |
| 11088 | backquotelist = bqlist; |
| 11089 | grabstackblock(len); |
| 11090 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11091 | lasttoken = TWORD; |
| 11092 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11093 | /* end of readtoken routine */ |
| 11094 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11095 | /* |
| 11096 | * Check to see whether we are at the end of the here document. When this |
| 11097 | * is called, c is set to the first character of the next input line. If |
| 11098 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 11099 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11100 | checkend: { |
| 11101 | if (eofmark) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11102 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11103 | if (c == PEOA) { |
| 11104 | c = pgetc2(); |
| 11105 | } |
| 11106 | #endif |
| 11107 | if (striptabs) { |
| 11108 | while (c == '\t') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11109 | c = pgetc2(); |
| 11110 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11111 | } |
| 11112 | if (c == *eofmark) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11113 | if (pfgets(line, sizeof(line)) != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11114 | char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11115 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11116 | p = line; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11117 | for (q = eofmark + 1; *q && *p == *q; p++, q++) |
| 11118 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11119 | if (*p == '\n' && *q == '\0') { |
| 11120 | c = PEOF; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11121 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11122 | needprompt = doprompt; |
| 11123 | } else { |
| 11124 | pushstring(line, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11125 | } |
| 11126 | } |
| 11127 | } |
| 11128 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11129 | goto checkend_return; |
| 11130 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11131 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11132 | /* |
| 11133 | * Parse a redirection operator. The variable "out" points to a string |
| 11134 | * specifying the fd to be redirected. The variable "c" contains the |
| 11135 | * first character of the redirection operator. |
| 11136 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11137 | parseredir: { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11138 | /* out is already checked to be a valid number or "" */ |
| 11139 | int fd = (*out == '\0' ? -1 : atoi(out)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11140 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11141 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11142 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11143 | if (c == '>') { |
| 11144 | np->nfile.fd = 1; |
| 11145 | c = pgetc(); |
| 11146 | if (c == '>') |
| 11147 | np->type = NAPPEND; |
| 11148 | else if (c == '|') |
| 11149 | np->type = NCLOBBER; |
| 11150 | else if (c == '&') |
| 11151 | np->type = NTOFD; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11152 | /* 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] | 11153 | else { |
| 11154 | np->type = NTO; |
| 11155 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11156 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11157 | } |
| 11158 | #if ENABLE_ASH_BASH_COMPAT |
| 11159 | else if (c == 0x100 + '>') { /* this flags &> redirection */ |
| 11160 | np->nfile.fd = 1; |
| 11161 | pgetc(); /* this is '>', no need to check */ |
| 11162 | np->type = NTO2; |
| 11163 | } |
| 11164 | #endif |
| 11165 | else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11166 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11167 | c = pgetc(); |
| 11168 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11169 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11170 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11171 | np = stzalloc(sizeof(struct nhere)); |
| 11172 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11173 | } |
| 11174 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11175 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11176 | heredoc->here = np; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11177 | c = pgetc(); |
| 11178 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11179 | heredoc->striptabs = 1; |
| 11180 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11181 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11182 | pungetc(); |
| 11183 | } |
| 11184 | break; |
| 11185 | |
| 11186 | case '&': |
| 11187 | np->type = NFROMFD; |
| 11188 | break; |
| 11189 | |
| 11190 | case '>': |
| 11191 | np->type = NFROMTO; |
| 11192 | break; |
| 11193 | |
| 11194 | default: |
| 11195 | np->type = NFROM; |
| 11196 | pungetc(); |
| 11197 | break; |
| 11198 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11199 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11200 | if (fd >= 0) |
| 11201 | np->nfile.fd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11202 | redirnode = np; |
| 11203 | goto parseredir_return; |
| 11204 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11205 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11206 | /* |
| 11207 | * Parse a substitution. At this point, we have read the dollar sign |
| 11208 | * and nothing else. |
| 11209 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11210 | |
| 11211 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 11212 | * (assuming ascii char codes, as the original implementation did) */ |
| 11213 | #define is_special(c) \ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11214 | (((unsigned)(c) - 33 < 32) \ |
| 11215 | && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11216 | parsesub: { |
| 11217 | int subtype; |
| 11218 | int typeloc; |
| 11219 | int flags; |
| 11220 | char *p; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11221 | static const char types[] ALIGN1 = "}-+?="; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11222 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11223 | c = pgetc(); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11224 | if (c <= PEOA_OR_PEOF |
| 11225 | || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11226 | ) { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11227 | #if ENABLE_ASH_BASH_COMPAT |
| 11228 | if (c == '\'') |
| 11229 | bash_dollar_squote = 1; |
| 11230 | else |
| 11231 | #endif |
| 11232 | USTPUTC('$', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11233 | pungetc(); |
| 11234 | } else if (c == '(') { /* $(command) or $((arith)) */ |
| 11235 | if (pgetc() == '(') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11236 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11237 | PARSEARITH(); |
| 11238 | #else |
Mike Frysinger | 98a6f56 | 2008-06-09 09:38:45 +0000 | [diff] [blame] | 11239 | raise_error_syntax("you disabled math support for $((arith)) syntax"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11240 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11241 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11242 | pungetc(); |
| 11243 | PARSEBACKQNEW(); |
| 11244 | } |
| 11245 | } else { |
| 11246 | USTPUTC(CTLVAR, out); |
| 11247 | typeloc = out - (char *)stackblock(); |
| 11248 | USTPUTC(VSNORMAL, out); |
| 11249 | subtype = VSNORMAL; |
| 11250 | if (c == '{') { |
| 11251 | c = pgetc(); |
| 11252 | if (c == '#') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11253 | c = pgetc(); |
| 11254 | if (c == '}') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11255 | c = '#'; |
| 11256 | else |
| 11257 | subtype = VSLENGTH; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11258 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11259 | subtype = 0; |
| 11260 | } |
| 11261 | if (c > PEOA_OR_PEOF && is_name(c)) { |
| 11262 | do { |
| 11263 | STPUTC(c, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11264 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11265 | } while (c > PEOA_OR_PEOF && is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11266 | } else if (isdigit(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11267 | do { |
| 11268 | STPUTC(c, out); |
| 11269 | c = pgetc(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11270 | } while (isdigit(c)); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11271 | } else if (is_special(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11272 | USTPUTC(c, out); |
| 11273 | c = pgetc(); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11274 | } else { |
| 11275 | badsub: |
| 11276 | raise_error_syntax("bad substitution"); |
| 11277 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11278 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11279 | STPUTC('=', out); |
| 11280 | flags = 0; |
| 11281 | if (subtype == 0) { |
| 11282 | switch (c) { |
| 11283 | case ':': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11284 | c = pgetc(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11285 | #if ENABLE_ASH_BASH_COMPAT |
| 11286 | if (c == ':' || c == '$' || isdigit(c)) { |
| 11287 | pungetc(); |
| 11288 | subtype = VSSUBSTR; |
| 11289 | break; |
| 11290 | } |
| 11291 | #endif |
| 11292 | flags = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11293 | /*FALLTHROUGH*/ |
| 11294 | default: |
| 11295 | p = strchr(types, c); |
| 11296 | if (p == NULL) |
| 11297 | goto badsub; |
| 11298 | subtype = p - types + VSNORMAL; |
| 11299 | break; |
| 11300 | case '%': |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11301 | case '#': { |
| 11302 | int cc = c; |
| 11303 | subtype = c == '#' ? VSTRIMLEFT : VSTRIMRIGHT; |
| 11304 | c = pgetc(); |
| 11305 | if (c == cc) |
| 11306 | subtype++; |
| 11307 | else |
| 11308 | pungetc(); |
| 11309 | break; |
| 11310 | } |
| 11311 | #if ENABLE_ASH_BASH_COMPAT |
| 11312 | case '/': |
| 11313 | subtype = VSREPLACE; |
| 11314 | c = pgetc(); |
| 11315 | if (c == '/') |
| 11316 | subtype++; /* VSREPLACEALL */ |
| 11317 | else |
| 11318 | pungetc(); |
| 11319 | break; |
| 11320 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11321 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11322 | } else { |
| 11323 | pungetc(); |
| 11324 | } |
| 11325 | if (dblquote || arinest) |
| 11326 | flags |= VSQUOTE; |
| 11327 | *((char *)stackblock() + typeloc) = subtype | flags; |
| 11328 | if (subtype != VSNORMAL) { |
| 11329 | varnest++; |
| 11330 | if (dblquote || arinest) { |
| 11331 | dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11332 | } |
| 11333 | } |
| 11334 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11335 | goto parsesub_return; |
| 11336 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11337 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11338 | /* |
| 11339 | * Called to parse command substitutions. Newstyle is set if the command |
| 11340 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 11341 | * list of commands (passed by reference), and savelen is the number of |
| 11342 | * characters on the top of the stack which must be preserved. |
| 11343 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11344 | parsebackq: { |
| 11345 | struct nodelist **nlpp; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11346 | smallint savepbq; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11347 | union node *n; |
| 11348 | char *volatile str; |
| 11349 | struct jmploc jmploc; |
| 11350 | struct jmploc *volatile savehandler; |
| 11351 | size_t savelen; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11352 | smallint saveprompt = 0; |
| 11353 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11354 | #ifdef __GNUC__ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11355 | (void) &saveprompt; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11356 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11357 | savepbq = parsebackquote; |
| 11358 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 11359 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11360 | parsebackquote = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11361 | exception_handler = savehandler; |
| 11362 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11363 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11364 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11365 | str = NULL; |
| 11366 | savelen = out - (char *)stackblock(); |
| 11367 | if (savelen > 0) { |
| 11368 | str = ckmalloc(savelen); |
| 11369 | memcpy(str, stackblock(), savelen); |
| 11370 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11371 | savehandler = exception_handler; |
| 11372 | exception_handler = &jmploc; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11373 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11374 | if (oldstyle) { |
| 11375 | /* We must read until the closing backquote, giving special |
| 11376 | treatment to some slashes, and then push the string and |
| 11377 | reread it as input, interpreting it normally. */ |
| 11378 | char *pout; |
| 11379 | int pc; |
| 11380 | size_t psavelen; |
| 11381 | char *pstr; |
| 11382 | |
| 11383 | |
| 11384 | STARTSTACKSTR(pout); |
| 11385 | for (;;) { |
| 11386 | if (needprompt) { |
| 11387 | setprompt(2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11388 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11389 | pc = pgetc(); |
| 11390 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11391 | case '`': |
| 11392 | goto done; |
| 11393 | |
| 11394 | case '\\': |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11395 | pc = pgetc(); |
| 11396 | if (pc == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11397 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11398 | if (doprompt) |
| 11399 | setprompt(2); |
| 11400 | /* |
| 11401 | * If eating a newline, avoid putting |
| 11402 | * the newline into the new character |
| 11403 | * stream (via the STPUTC after the |
| 11404 | * switch). |
| 11405 | */ |
| 11406 | continue; |
| 11407 | } |
| 11408 | if (pc != '\\' && pc != '`' && pc != '$' |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11409 | && (!dblquote || pc != '"')) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11410 | STPUTC('\\', pout); |
| 11411 | if (pc > PEOA_OR_PEOF) { |
| 11412 | break; |
| 11413 | } |
| 11414 | /* fall through */ |
| 11415 | |
| 11416 | case PEOF: |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11417 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11418 | case PEOA: |
| 11419 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11420 | startlinno = g_parsefile->linno; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11421 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11422 | |
| 11423 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11424 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11425 | needprompt = doprompt; |
| 11426 | break; |
| 11427 | |
| 11428 | default: |
| 11429 | break; |
| 11430 | } |
| 11431 | STPUTC(pc, pout); |
| 11432 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11433 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11434 | STPUTC('\0', pout); |
| 11435 | psavelen = pout - (char *)stackblock(); |
| 11436 | if (psavelen > 0) { |
| 11437 | pstr = grabstackstr(pout); |
| 11438 | setinputstring(pstr); |
| 11439 | } |
| 11440 | } |
| 11441 | nlpp = &bqlist; |
| 11442 | while (*nlpp) |
| 11443 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11444 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 11445 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11446 | parsebackquote = oldstyle; |
| 11447 | |
| 11448 | if (oldstyle) { |
| 11449 | saveprompt = doprompt; |
| 11450 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11451 | } |
| 11452 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11453 | n = list(2); |
| 11454 | |
| 11455 | if (oldstyle) |
| 11456 | doprompt = saveprompt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11457 | else if (readtoken() != TRP) |
| 11458 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11459 | |
| 11460 | (*nlpp)->n = n; |
| 11461 | if (oldstyle) { |
| 11462 | /* |
| 11463 | * Start reading from old file again, ignoring any pushed back |
| 11464 | * tokens left from the backquote parsing |
| 11465 | */ |
| 11466 | popfile(); |
| 11467 | tokpushback = 0; |
| 11468 | } |
| 11469 | while (stackblocksize() <= savelen) |
| 11470 | growstackblock(); |
| 11471 | STARTSTACKSTR(out); |
| 11472 | if (str) { |
| 11473 | memcpy(out, str, savelen); |
| 11474 | STADJUST(savelen, out); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11475 | INT_OFF; |
| 11476 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11477 | str = NULL; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11478 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11479 | } |
| 11480 | parsebackquote = savepbq; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11481 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11482 | if (arinest || dblquote) |
| 11483 | USTPUTC(CTLBACKQ | CTLQUOTE, out); |
| 11484 | else |
| 11485 | USTPUTC(CTLBACKQ, out); |
| 11486 | if (oldstyle) |
| 11487 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11488 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11489 | } |
| 11490 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11491 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11492 | /* |
| 11493 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 11494 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11495 | parsearith: { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11496 | if (++arinest == 1) { |
| 11497 | prevsyntax = syntax; |
| 11498 | syntax = ARISYNTAX; |
| 11499 | USTPUTC(CTLARI, out); |
| 11500 | if (dblquote) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11501 | USTPUTC('"', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11502 | else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11503 | USTPUTC(' ', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11504 | } else { |
| 11505 | /* |
| 11506 | * we collapse embedded arithmetic expansion to |
| 11507 | * parenthesis, which should be equivalent |
| 11508 | */ |
| 11509 | USTPUTC('(', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11510 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11511 | goto parsearith_return; |
| 11512 | } |
| 11513 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11514 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11515 | } /* end of readtoken */ |
| 11516 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11517 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11518 | * Read the next input token. |
| 11519 | * If the token is a word, we set backquotelist to the list of cmds in |
| 11520 | * backquotes. We set quoteflag to true if any part of the word was |
| 11521 | * quoted. |
| 11522 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 11523 | * the redirection. |
| 11524 | * In all cases, the variable startlinno is set to the number of the line |
| 11525 | * on which the token starts. |
| 11526 | * |
| 11527 | * [Change comment: here documents and internal procedures] |
| 11528 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 11529 | * word parsing code into a separate routine. In this case, readtoken |
| 11530 | * doesn't need to have any internal procedures, but parseword does. |
| 11531 | * We could also make parseoperator in essence the main routine, and |
| 11532 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11533 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11534 | #define NEW_xxreadtoken |
| 11535 | #ifdef NEW_xxreadtoken |
| 11536 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11537 | static const char xxreadtoken_chars[7] ALIGN1 = { |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11538 | '\n', '(', ')', /* singles */ |
| 11539 | '&', '|', ';', /* doubles */ |
| 11540 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11541 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11542 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11543 | #define xxreadtoken_singles 3 |
| 11544 | #define xxreadtoken_doubles 3 |
| 11545 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11546 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11547 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 11548 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 11549 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11550 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11551 | }; |
| 11552 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11553 | static int |
| 11554 | xxreadtoken(void) |
| 11555 | { |
| 11556 | int c; |
| 11557 | |
| 11558 | if (tokpushback) { |
| 11559 | tokpushback = 0; |
| 11560 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11561 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11562 | if (needprompt) { |
| 11563 | setprompt(2); |
| 11564 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11565 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11566 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11567 | c = pgetc_fast(); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11568 | if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA)) |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11569 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11570 | |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11571 | if (c == '#') { |
| 11572 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11573 | continue; |
| 11574 | pungetc(); |
| 11575 | } else if (c == '\\') { |
| 11576 | if (pgetc() != '\n') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11577 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11578 | break; /* return readtoken1(...) */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11579 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11580 | startlinno = ++g_parsefile->linno; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11581 | if (doprompt) |
| 11582 | setprompt(2); |
| 11583 | } else { |
| 11584 | const char *p; |
| 11585 | |
| 11586 | p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 11587 | if (c != PEOF) { |
| 11588 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11589 | g_parsefile->linno++; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11590 | needprompt = doprompt; |
| 11591 | } |
| 11592 | |
| 11593 | p = strchr(xxreadtoken_chars, c); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11594 | if (p == NULL) |
| 11595 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11596 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11597 | if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
| 11598 | int cc = pgetc(); |
| 11599 | if (cc == c) { /* double occurrence? */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11600 | p += xxreadtoken_doubles + 1; |
| 11601 | } else { |
| 11602 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11603 | #if ENABLE_ASH_BASH_COMPAT |
| 11604 | if (c == '&' && cc == '>') /* &> */ |
| 11605 | break; /* return readtoken1(...) */ |
| 11606 | #endif |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11607 | } |
| 11608 | } |
| 11609 | } |
| 11610 | lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 11611 | return lasttoken; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11612 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11613 | } /* for (;;) */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11614 | |
| 11615 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11616 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11617 | #else /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11618 | #define RETURN(token) return lasttoken = token |
| 11619 | static int |
| 11620 | xxreadtoken(void) |
| 11621 | { |
| 11622 | int c; |
| 11623 | |
| 11624 | if (tokpushback) { |
| 11625 | tokpushback = 0; |
| 11626 | return lasttoken; |
| 11627 | } |
| 11628 | if (needprompt) { |
| 11629 | setprompt(2); |
| 11630 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11631 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11632 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11633 | c = pgetc_fast(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11634 | switch (c) { |
| 11635 | case ' ': case '\t': |
| 11636 | #if ENABLE_ASH_ALIAS |
| 11637 | case PEOA: |
| 11638 | #endif |
| 11639 | continue; |
| 11640 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11641 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11642 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11643 | pungetc(); |
| 11644 | continue; |
| 11645 | case '\\': |
| 11646 | if (pgetc() == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11647 | startlinno = ++g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11648 | if (doprompt) |
| 11649 | setprompt(2); |
| 11650 | continue; |
| 11651 | } |
| 11652 | pungetc(); |
| 11653 | goto breakloop; |
| 11654 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11655 | g_parsefile->linno++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11656 | needprompt = doprompt; |
| 11657 | RETURN(TNL); |
| 11658 | case PEOF: |
| 11659 | RETURN(TEOF); |
| 11660 | case '&': |
| 11661 | if (pgetc() == '&') |
| 11662 | RETURN(TAND); |
| 11663 | pungetc(); |
| 11664 | RETURN(TBACKGND); |
| 11665 | case '|': |
| 11666 | if (pgetc() == '|') |
| 11667 | RETURN(TOR); |
| 11668 | pungetc(); |
| 11669 | RETURN(TPIPE); |
| 11670 | case ';': |
| 11671 | if (pgetc() == ';') |
| 11672 | RETURN(TENDCASE); |
| 11673 | pungetc(); |
| 11674 | RETURN(TSEMI); |
| 11675 | case '(': |
| 11676 | RETURN(TLP); |
| 11677 | case ')': |
| 11678 | RETURN(TRP); |
| 11679 | default: |
| 11680 | goto breakloop; |
| 11681 | } |
| 11682 | } |
| 11683 | breakloop: |
| 11684 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 11685 | #undef RETURN |
| 11686 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11687 | #endif /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11688 | |
| 11689 | static int |
| 11690 | readtoken(void) |
| 11691 | { |
| 11692 | int t; |
| 11693 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11694 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11695 | #endif |
| 11696 | |
| 11697 | #if ENABLE_ASH_ALIAS |
| 11698 | top: |
| 11699 | #endif |
| 11700 | |
| 11701 | t = xxreadtoken(); |
| 11702 | |
| 11703 | /* |
| 11704 | * eat newlines |
| 11705 | */ |
| 11706 | if (checkkwd & CHKNL) { |
| 11707 | while (t == TNL) { |
| 11708 | parseheredoc(); |
| 11709 | t = xxreadtoken(); |
| 11710 | } |
| 11711 | } |
| 11712 | |
| 11713 | if (t != TWORD || quoteflag) { |
| 11714 | goto out; |
| 11715 | } |
| 11716 | |
| 11717 | /* |
| 11718 | * check for keywords |
| 11719 | */ |
| 11720 | if (checkkwd & CHKKWD) { |
| 11721 | const char *const *pp; |
| 11722 | |
| 11723 | pp = findkwd(wordtext); |
| 11724 | if (pp) { |
| 11725 | lasttoken = t = pp - tokname_array; |
| 11726 | TRACE(("keyword %s recognized\n", tokname(t))); |
| 11727 | goto out; |
| 11728 | } |
| 11729 | } |
| 11730 | |
| 11731 | if (checkkwd & CHKALIAS) { |
| 11732 | #if ENABLE_ASH_ALIAS |
| 11733 | struct alias *ap; |
| 11734 | ap = lookupalias(wordtext, 1); |
| 11735 | if (ap != NULL) { |
| 11736 | if (*ap->val) { |
| 11737 | pushstring(ap->val, ap); |
| 11738 | } |
| 11739 | goto top; |
| 11740 | } |
| 11741 | #endif |
| 11742 | } |
| 11743 | out: |
| 11744 | checkkwd = 0; |
| 11745 | #if DEBUG |
| 11746 | if (!alreadyseen) |
| 11747 | TRACE(("token %s %s\n", tokname(t), t == TWORD ? wordtext : "")); |
| 11748 | else |
| 11749 | TRACE(("reread token %s %s\n", tokname(t), t == TWORD ? wordtext : "")); |
| 11750 | #endif |
| 11751 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11752 | } |
| 11753 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11754 | static char |
| 11755 | peektoken(void) |
| 11756 | { |
| 11757 | int t; |
| 11758 | |
| 11759 | t = readtoken(); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11760 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11761 | return tokname_array[t][0]; |
| 11762 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11763 | |
| 11764 | /* |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11765 | * Read and parse a command. Returns NODE_EOF on end of file. |
| 11766 | * (NULL is a valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11767 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11768 | static union node * |
| 11769 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11770 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11771 | int t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11772 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11773 | tokpushback = 0; |
| 11774 | doprompt = interact; |
| 11775 | if (doprompt) |
| 11776 | setprompt(doprompt); |
| 11777 | needprompt = 0; |
| 11778 | t = readtoken(); |
| 11779 | if (t == TEOF) |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11780 | return NODE_EOF; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11781 | if (t == TNL) |
| 11782 | return NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11783 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11784 | return list(1); |
| 11785 | } |
| 11786 | |
| 11787 | /* |
| 11788 | * Input any here documents. |
| 11789 | */ |
| 11790 | static void |
| 11791 | parseheredoc(void) |
| 11792 | { |
| 11793 | struct heredoc *here; |
| 11794 | union node *n; |
| 11795 | |
| 11796 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11797 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11798 | |
| 11799 | while (here) { |
| 11800 | if (needprompt) { |
| 11801 | setprompt(2); |
| 11802 | } |
| 11803 | readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, |
| 11804 | here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11805 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11806 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11807 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11808 | n->narg.text = wordtext; |
| 11809 | n->narg.backquote = backquotelist; |
| 11810 | here->here->nhere.doc = n; |
| 11811 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11812 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11813 | } |
| 11814 | |
| 11815 | |
| 11816 | /* |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 11817 | * called by editline -- any expansions to the prompt should be added here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11818 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11819 | #if ENABLE_ASH_EXPAND_PRMT |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11820 | static const char * |
| 11821 | expandstr(const char *ps) |
| 11822 | { |
| 11823 | union node n; |
| 11824 | |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 11825 | /* XXX Fix (char *) cast. It _is_ a bug. ps is variable's value, |
| 11826 | * and token processing _can_ alter it (delete NULs etc). */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11827 | setinputstring((char *)ps); |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11828 | readtoken1(pgetc(), PSSYNTAX, nullstr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11829 | popfile(); |
| 11830 | |
| 11831 | n.narg.type = NARG; |
| 11832 | n.narg.next = NULL; |
| 11833 | n.narg.text = wordtext; |
| 11834 | n.narg.backquote = backquotelist; |
| 11835 | |
| 11836 | expandarg(&n, NULL, 0); |
| 11837 | return stackblock(); |
| 11838 | } |
| 11839 | #endif |
| 11840 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11841 | /* |
| 11842 | * Execute a command or commands contained in a string. |
| 11843 | */ |
| 11844 | static int |
| 11845 | evalstring(char *s, int mask) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11846 | { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11847 | union node *n; |
| 11848 | struct stackmark smark; |
| 11849 | int skip; |
| 11850 | |
| 11851 | setinputstring(s); |
| 11852 | setstackmark(&smark); |
| 11853 | |
| 11854 | skip = 0; |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11855 | while ((n = parsecmd(0)) != NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11856 | evaltree(n, 0); |
| 11857 | popstackmark(&smark); |
| 11858 | skip = evalskip; |
| 11859 | if (skip) |
| 11860 | break; |
| 11861 | } |
| 11862 | popfile(); |
| 11863 | |
| 11864 | skip &= mask; |
| 11865 | evalskip = skip; |
| 11866 | return skip; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11867 | } |
| 11868 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11869 | /* |
| 11870 | * The eval command. |
| 11871 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11872 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11873 | evalcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11874 | { |
| 11875 | char *p; |
| 11876 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11877 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11878 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11879 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11880 | argv += 2; |
| 11881 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11882 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11883 | for (;;) { |
| 11884 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11885 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11886 | if (p == NULL) |
| 11887 | break; |
| 11888 | STPUTC(' ', concat); |
| 11889 | } |
| 11890 | STPUTC('\0', concat); |
| 11891 | p = grabstackstr(concat); |
| 11892 | } |
| 11893 | evalstring(p, ~SKIPEVAL); |
| 11894 | |
| 11895 | } |
| 11896 | return exitstatus; |
| 11897 | } |
| 11898 | |
| 11899 | /* |
| 11900 | * Read and execute commands. "Top" is nonzero for the top level command |
| 11901 | * loop; it turns on prompting if the shell is interactive. |
| 11902 | */ |
| 11903 | static int |
| 11904 | cmdloop(int top) |
| 11905 | { |
| 11906 | union node *n; |
| 11907 | struct stackmark smark; |
| 11908 | int inter; |
| 11909 | int numeof = 0; |
| 11910 | |
| 11911 | TRACE(("cmdloop(%d) called\n", top)); |
| 11912 | for (;;) { |
| 11913 | int skip; |
| 11914 | |
| 11915 | setstackmark(&smark); |
| 11916 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11917 | if (doing_jobctl) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11918 | showjobs(stderr, SHOW_CHANGED); |
| 11919 | #endif |
| 11920 | inter = 0; |
| 11921 | if (iflag && top) { |
| 11922 | inter++; |
| 11923 | #if ENABLE_ASH_MAIL |
| 11924 | chkmail(); |
| 11925 | #endif |
| 11926 | } |
| 11927 | n = parsecmd(inter); |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 11928 | #if DEBUG |
| 11929 | if (DEBUG > 2 && debug && (n != NODE_EOF)) |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 11930 | showtree(n); |
Denis Vlasenko | 135cecb | 2009-04-12 00:00:57 +0000 | [diff] [blame] | 11931 | #endif |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11932 | if (n == NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11933 | if (!top || numeof >= 50) |
| 11934 | break; |
| 11935 | if (!stoppedjobs()) { |
| 11936 | if (!Iflag) |
| 11937 | break; |
| 11938 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 11939 | } |
| 11940 | numeof++; |
| 11941 | } else if (nflag == 0) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 11942 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 11943 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11944 | numeof = 0; |
| 11945 | evaltree(n, 0); |
| 11946 | } |
| 11947 | popstackmark(&smark); |
| 11948 | skip = evalskip; |
| 11949 | |
| 11950 | if (skip) { |
| 11951 | evalskip = 0; |
| 11952 | return skip & SKIPEVAL; |
| 11953 | } |
| 11954 | } |
| 11955 | return 0; |
| 11956 | } |
| 11957 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11958 | /* |
| 11959 | * Take commands from a file. To be compatible we should do a path |
| 11960 | * search for the file, which is necessary to find sub-commands. |
| 11961 | */ |
| 11962 | static char * |
| 11963 | find_dot_file(char *name) |
| 11964 | { |
| 11965 | char *fullname; |
| 11966 | const char *path = pathval(); |
| 11967 | struct stat statb; |
| 11968 | |
| 11969 | /* don't try this for absolute or relative paths */ |
| 11970 | if (strchr(name, '/')) |
| 11971 | return name; |
| 11972 | |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 11973 | /* IIRC standards do not say whether . is to be searched. |
| 11974 | * And it is even smaller this way, making it unconditional for now: |
| 11975 | */ |
| 11976 | if (1) { /* ENABLE_ASH_BASH_COMPAT */ |
| 11977 | fullname = name; |
| 11978 | goto try_cur_dir; |
| 11979 | } |
| 11980 | |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 11981 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 11982 | try_cur_dir: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11983 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 11984 | /* |
| 11985 | * Don't bother freeing here, since it will |
| 11986 | * be freed by the caller. |
| 11987 | */ |
| 11988 | return fullname; |
| 11989 | } |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 11990 | if (fullname != name) |
| 11991 | stunalloc(fullname); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11992 | } |
| 11993 | |
| 11994 | /* not found in the PATH */ |
| 11995 | ash_msg_and_raise_error("%s: not found", name); |
| 11996 | /* NOTREACHED */ |
| 11997 | } |
| 11998 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11999 | static int FAST_FUNC |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12000 | dotcmd(int argc, char **argv) |
| 12001 | { |
| 12002 | struct strlist *sp; |
| 12003 | volatile struct shparam saveparam; |
| 12004 | int status = 0; |
| 12005 | |
| 12006 | for (sp = cmdenviron; sp; sp = sp->next) |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 12007 | setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12008 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12009 | if (argv[1]) { /* That's what SVR2 does */ |
| 12010 | char *fullname = find_dot_file(argv[1]); |
| 12011 | argv += 2; |
| 12012 | argc -= 2; |
| 12013 | if (argc) { /* argc > 0, argv[0] != NULL */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12014 | saveparam = shellparam; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12015 | shellparam.malloced = 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12016 | shellparam.nparam = argc; |
| 12017 | shellparam.p = argv; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12018 | }; |
| 12019 | |
| 12020 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 12021 | commandname = fullname; |
| 12022 | cmdloop(0); |
| 12023 | popfile(); |
| 12024 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12025 | if (argc) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12026 | freeparam(&shellparam); |
| 12027 | shellparam = saveparam; |
| 12028 | }; |
| 12029 | status = exitstatus; |
| 12030 | } |
| 12031 | return status; |
| 12032 | } |
| 12033 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12034 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12035 | exitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12036 | { |
| 12037 | if (stoppedjobs()) |
| 12038 | return 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12039 | if (argv[1]) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12040 | exitstatus = number(argv[1]); |
| 12041 | raise_exception(EXEXIT); |
| 12042 | /* NOTREACHED */ |
| 12043 | } |
| 12044 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12045 | /* |
| 12046 | * Read a file containing shell functions. |
| 12047 | */ |
| 12048 | static void |
| 12049 | readcmdfile(char *name) |
| 12050 | { |
| 12051 | setinputfile(name, INPUT_PUSH_FILE); |
| 12052 | cmdloop(0); |
| 12053 | popfile(); |
| 12054 | } |
| 12055 | |
| 12056 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12057 | /* ============ find_command inplementation */ |
| 12058 | |
| 12059 | /* |
| 12060 | * Resolve a command name. If you change this routine, you may have to |
| 12061 | * change the shellexec routine as well. |
| 12062 | */ |
| 12063 | static void |
| 12064 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 12065 | { |
| 12066 | struct tblentry *cmdp; |
| 12067 | int idx; |
| 12068 | int prev; |
| 12069 | char *fullname; |
| 12070 | struct stat statb; |
| 12071 | int e; |
| 12072 | int updatetbl; |
| 12073 | struct builtincmd *bcmd; |
| 12074 | |
| 12075 | /* If name contains a slash, don't use PATH or hash table */ |
| 12076 | if (strchr(name, '/') != NULL) { |
| 12077 | entry->u.index = -1; |
| 12078 | if (act & DO_ABS) { |
| 12079 | while (stat(name, &statb) < 0) { |
| 12080 | #ifdef SYSV |
| 12081 | if (errno == EINTR) |
| 12082 | continue; |
| 12083 | #endif |
| 12084 | entry->cmdtype = CMDUNKNOWN; |
| 12085 | return; |
| 12086 | } |
| 12087 | } |
| 12088 | entry->cmdtype = CMDNORMAL; |
| 12089 | return; |
| 12090 | } |
| 12091 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12092 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12093 | |
| 12094 | updatetbl = (path == pathval()); |
| 12095 | if (!updatetbl) { |
| 12096 | act |= DO_ALTPATH; |
| 12097 | if (strstr(path, "%builtin") != NULL) |
| 12098 | act |= DO_ALTBLTIN; |
| 12099 | } |
| 12100 | |
| 12101 | /* If name is in the table, check answer will be ok */ |
| 12102 | cmdp = cmdlookup(name, 0); |
| 12103 | if (cmdp != NULL) { |
| 12104 | int bit; |
| 12105 | |
| 12106 | switch (cmdp->cmdtype) { |
| 12107 | default: |
| 12108 | #if DEBUG |
| 12109 | abort(); |
| 12110 | #endif |
| 12111 | case CMDNORMAL: |
| 12112 | bit = DO_ALTPATH; |
| 12113 | break; |
| 12114 | case CMDFUNCTION: |
| 12115 | bit = DO_NOFUNC; |
| 12116 | break; |
| 12117 | case CMDBUILTIN: |
| 12118 | bit = DO_ALTBLTIN; |
| 12119 | break; |
| 12120 | } |
| 12121 | if (act & bit) { |
| 12122 | updatetbl = 0; |
| 12123 | cmdp = NULL; |
| 12124 | } else if (cmdp->rehash == 0) |
| 12125 | /* if not invalidated by cd, we're done */ |
| 12126 | goto success; |
| 12127 | } |
| 12128 | |
| 12129 | /* If %builtin not in path, check for builtin next */ |
| 12130 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12131 | if (bcmd) { |
| 12132 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 12133 | goto builtin_success; |
| 12134 | if (act & DO_ALTPATH) { |
| 12135 | if (!(act & DO_ALTBLTIN)) |
| 12136 | goto builtin_success; |
| 12137 | } else if (builtinloc <= 0) { |
| 12138 | goto builtin_success; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 12139 | } |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12140 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12141 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12142 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 12143 | { |
| 12144 | int applet_no = find_applet_by_name(name); |
| 12145 | if (applet_no >= 0) { |
| 12146 | entry->cmdtype = CMDNORMAL; |
| 12147 | entry->u.index = -2 - applet_no; |
| 12148 | return; |
| 12149 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12150 | } |
| 12151 | #endif |
| 12152 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12153 | /* We have to search path. */ |
| 12154 | prev = -1; /* where to start */ |
| 12155 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 12156 | if (cmdp->cmdtype == CMDBUILTIN) |
| 12157 | prev = builtinloc; |
| 12158 | else |
| 12159 | prev = cmdp->param.index; |
| 12160 | } |
| 12161 | |
| 12162 | e = ENOENT; |
| 12163 | idx = -1; |
| 12164 | loop: |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12165 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12166 | stunalloc(fullname); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12167 | /* NB: code below will still use fullname |
| 12168 | * despite it being "unallocated" */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12169 | idx++; |
| 12170 | if (pathopt) { |
| 12171 | if (prefix(pathopt, "builtin")) { |
| 12172 | if (bcmd) |
| 12173 | goto builtin_success; |
| 12174 | continue; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 12175 | } |
| 12176 | if ((act & DO_NOFUNC) |
| 12177 | || !prefix(pathopt, "func") |
| 12178 | ) { /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12179 | continue; |
| 12180 | } |
| 12181 | } |
| 12182 | /* if rehash, don't redo absolute path names */ |
| 12183 | if (fullname[0] == '/' && idx <= prev) { |
| 12184 | if (idx < prev) |
| 12185 | continue; |
| 12186 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 12187 | goto success; |
| 12188 | } |
| 12189 | while (stat(fullname, &statb) < 0) { |
| 12190 | #ifdef SYSV |
| 12191 | if (errno == EINTR) |
| 12192 | continue; |
| 12193 | #endif |
| 12194 | if (errno != ENOENT && errno != ENOTDIR) |
| 12195 | e = errno; |
| 12196 | goto loop; |
| 12197 | } |
| 12198 | e = EACCES; /* if we fail, this will be the error */ |
| 12199 | if (!S_ISREG(statb.st_mode)) |
| 12200 | continue; |
| 12201 | if (pathopt) { /* this is a %func directory */ |
| 12202 | stalloc(strlen(fullname) + 1); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12203 | /* NB: stalloc will return space pointed by fullname |
| 12204 | * (because we don't have any intervening allocations |
| 12205 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12206 | readcmdfile(fullname); |
| 12207 | cmdp = cmdlookup(name, 0); |
| 12208 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 12209 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 12210 | stunalloc(fullname); |
| 12211 | goto success; |
| 12212 | } |
| 12213 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 12214 | if (!updatetbl) { |
| 12215 | entry->cmdtype = CMDNORMAL; |
| 12216 | entry->u.index = idx; |
| 12217 | return; |
| 12218 | } |
| 12219 | INT_OFF; |
| 12220 | cmdp = cmdlookup(name, 1); |
| 12221 | cmdp->cmdtype = CMDNORMAL; |
| 12222 | cmdp->param.index = idx; |
| 12223 | INT_ON; |
| 12224 | goto success; |
| 12225 | } |
| 12226 | |
| 12227 | /* We failed. If there was an entry for this command, delete it */ |
| 12228 | if (cmdp && updatetbl) |
| 12229 | delete_cmd_entry(); |
| 12230 | if (act & DO_ERR) |
| 12231 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
| 12232 | entry->cmdtype = CMDUNKNOWN; |
| 12233 | return; |
| 12234 | |
| 12235 | builtin_success: |
| 12236 | if (!updatetbl) { |
| 12237 | entry->cmdtype = CMDBUILTIN; |
| 12238 | entry->u.cmd = bcmd; |
| 12239 | return; |
| 12240 | } |
| 12241 | INT_OFF; |
| 12242 | cmdp = cmdlookup(name, 1); |
| 12243 | cmdp->cmdtype = CMDBUILTIN; |
| 12244 | cmdp->param.cmd = bcmd; |
| 12245 | INT_ON; |
| 12246 | success: |
| 12247 | cmdp->rehash = 0; |
| 12248 | entry->cmdtype = cmdp->cmdtype; |
| 12249 | entry->u = cmdp->param; |
| 12250 | } |
| 12251 | |
| 12252 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12253 | /* ============ trap.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12254 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12255 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12256 | * The trap builtin. |
| 12257 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12258 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12259 | trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12260 | { |
| 12261 | char *action; |
| 12262 | char **ap; |
| 12263 | int signo; |
| 12264 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12265 | nextopt(nullstr); |
| 12266 | ap = argptr; |
| 12267 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12268 | for (signo = 0; signo < NSIG; signo++) { |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12269 | char *tr = trap_ptr[signo]; |
| 12270 | if (tr) { |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12271 | /* note: bash adds "SIG", but only if invoked |
| 12272 | * as "bash". If called as "sh", or if set -o posix, |
| 12273 | * then it prints short signal names. |
| 12274 | * We are printing short names: */ |
| 12275 | out1fmt("trap -- %s %s\n", |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12276 | single_quote(tr), |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12277 | get_signame(signo)); |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12278 | /* trap_ptr != trap only if we are in special-cased `trap` code. |
| 12279 | * In this case, we will exit very soon, no need to free(). */ |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12280 | /* if (trap_ptr != trap && tp[0]) */ |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12281 | /* free(tr); */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12282 | } |
| 12283 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12284 | /* |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12285 | if (trap_ptr != trap) { |
| 12286 | free(trap_ptr); |
| 12287 | trap_ptr = trap; |
| 12288 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12289 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12290 | return 0; |
| 12291 | } |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12292 | |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12293 | action = NULL; |
| 12294 | if (ap[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12295 | action = *ap++; |
| 12296 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12297 | signo = get_signum(*ap); |
| 12298 | if (signo < 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12299 | ash_msg_and_raise_error("%s: bad trap", *ap); |
| 12300 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12301 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 12302 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12303 | action = NULL; |
| 12304 | else |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12305 | action = ckstrdup(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12306 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 12307 | free(trap[signo]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12308 | trap[signo] = action; |
| 12309 | if (signo != 0) |
| 12310 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12311 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12312 | ap++; |
| 12313 | } |
| 12314 | return 0; |
| 12315 | } |
| 12316 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12317 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12318 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12319 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 12320 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12321 | /* |
| 12322 | * Lists available builtins |
| 12323 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12324 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12325 | helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12326 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 12327 | unsigned col; |
| 12328 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12329 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 12330 | out1fmt( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 12331 | "Built-in commands:\n" |
| 12332 | "------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 12333 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12334 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 12335 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12336 | if (col > 60) { |
| 12337 | out1fmt("\n"); |
| 12338 | col = 0; |
| 12339 | } |
| 12340 | } |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 12341 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 12342 | { |
| 12343 | const char *a = applet_names; |
| 12344 | while (*a) { |
| 12345 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 12346 | if (col > 60) { |
| 12347 | out1fmt("\n"); |
| 12348 | col = 0; |
| 12349 | } |
| 12350 | a += strlen(a) + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12351 | } |
| 12352 | } |
| 12353 | #endif |
| 12354 | out1fmt("\n\n"); |
| 12355 | return EXIT_SUCCESS; |
| 12356 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12357 | #endif /* FEATURE_SH_EXTRA_QUIET */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12358 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12359 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12360 | * The export and readonly commands. |
| 12361 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12362 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12363 | exportcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12364 | { |
| 12365 | struct var *vp; |
| 12366 | char *name; |
| 12367 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12368 | char **aptr; |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 12369 | int flag = argv[0][0] == 'r' ? VREADONLY : VEXPORT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12370 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12371 | if (nextopt("p") != 'p') { |
| 12372 | aptr = argptr; |
| 12373 | name = *aptr; |
| 12374 | if (name) { |
| 12375 | do { |
| 12376 | p = strchr(name, '='); |
| 12377 | if (p != NULL) { |
| 12378 | p++; |
| 12379 | } else { |
| 12380 | vp = *findvar(hashvar(name), name); |
| 12381 | if (vp) { |
| 12382 | vp->flags |= flag; |
| 12383 | continue; |
| 12384 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12385 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12386 | setvar(name, p, flag); |
| 12387 | } while ((name = *++aptr) != NULL); |
| 12388 | return 0; |
| 12389 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12390 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12391 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12392 | return 0; |
| 12393 | } |
| 12394 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12395 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12396 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12397 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12398 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12399 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12400 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12401 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12402 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12403 | cmdp = cmdlookup(name, 0); |
| 12404 | if (cmdp!= NULL && cmdp->cmdtype == CMDFUNCTION) |
| 12405 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12406 | } |
| 12407 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12408 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12409 | * The unset builtin command. We unset the function before we unset the |
| 12410 | * variable to allow a function to be unset when there is a readonly variable |
| 12411 | * with the same name. |
| 12412 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12413 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12414 | unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12415 | { |
| 12416 | char **ap; |
| 12417 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12418 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12419 | int ret = 0; |
| 12420 | |
| 12421 | while ((i = nextopt("vf")) != '\0') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12422 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12423 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12424 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12425 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12426 | if (flag != 'f') { |
| 12427 | i = unsetvar(*ap); |
| 12428 | ret |= i; |
| 12429 | if (!(i & 2)) |
| 12430 | continue; |
| 12431 | } |
| 12432 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12433 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12434 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12435 | return ret & 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12436 | } |
| 12437 | |
| 12438 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12439 | /* setmode.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12440 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12441 | #include <sys/times.h> |
| 12442 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12443 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12444 | ' ', offsetof(struct tms, tms_utime), |
| 12445 | '\n', offsetof(struct tms, tms_stime), |
| 12446 | ' ', offsetof(struct tms, tms_cutime), |
| 12447 | '\n', offsetof(struct tms, tms_cstime), |
| 12448 | 0 |
| 12449 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12450 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12451 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12452 | timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12453 | { |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12454 | long clk_tck, s, t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12455 | const unsigned char *p; |
| 12456 | struct tms buf; |
| 12457 | |
| 12458 | clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12459 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12460 | |
| 12461 | p = timescmd_str; |
| 12462 | do { |
| 12463 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 12464 | s = t / clk_tck; |
| 12465 | out1fmt("%ldm%ld.%.3lds%c", |
| 12466 | s/60, s%60, |
| 12467 | ((t - s * clk_tck) * 1000) / clk_tck, |
| 12468 | p[0]); |
| 12469 | } while (*(p += 2)); |
| 12470 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12471 | return 0; |
| 12472 | } |
| 12473 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12474 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12475 | /* |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12476 | * The let builtin. partial stolen from GNU Bash, the Bourne Again SHell. |
| 12477 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12478 | * |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12479 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12480 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12481 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12482 | letcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12483 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12484 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12485 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12486 | argv++; |
| 12487 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12488 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12489 | do { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12490 | i = ash_arith(*argv); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12491 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12492 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 12493 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12494 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12495 | #endif /* SH_MATH_SUPPORT */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12496 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12497 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12498 | /* ============ miscbltin.c |
| 12499 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12500 | * Miscellaneous builtins. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12501 | */ |
| 12502 | |
| 12503 | #undef rflag |
| 12504 | |
Denis Vlasenko | 83e5d6f | 2006-12-18 21:49:06 +0000 | [diff] [blame] | 12505 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 1 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12506 | typedef enum __rlimit_resource rlim_t; |
| 12507 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12508 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12509 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12510 | * The read builtin. Options: |
| 12511 | * -r Do not interpret '\' specially |
| 12512 | * -s Turn off echo (tty only) |
| 12513 | * -n NCHARS Read NCHARS max |
| 12514 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 12515 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 12516 | * -u FD Read from given FD instead of fd 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12517 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12518 | * TODO: bash also has: |
| 12519 | * -a ARRAY Read into array[0],[1],etc |
| 12520 | * -d DELIM End on DELIM char, not newline |
| 12521 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12522 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12523 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12524 | readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12525 | { |
Denis Vlasenko | 9cd4c76 | 2008-06-18 19:22:19 +0000 | [diff] [blame] | 12526 | static const char *const arg_REPLY[] = { "REPLY", NULL }; |
| 12527 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12528 | char **ap; |
| 12529 | int backslash; |
| 12530 | char c; |
| 12531 | int rflag; |
| 12532 | char *prompt; |
| 12533 | const char *ifs; |
| 12534 | char *p; |
| 12535 | int startword; |
| 12536 | int status; |
| 12537 | int i; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12538 | int fd = 0; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12539 | #if ENABLE_ASH_READ_NCHARS |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12540 | int nchars = 0; /* if != 0, -n is in effect */ |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12541 | int silent = 0; |
| 12542 | struct termios tty, old_tty; |
| 12543 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12544 | #if ENABLE_ASH_READ_TIMEOUT |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12545 | unsigned end_ms = 0; |
| 12546 | unsigned timeout = 0; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12547 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12548 | |
| 12549 | rflag = 0; |
| 12550 | prompt = NULL; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12551 | while ((i = nextopt("p:u:r" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 12552 | IF_ASH_READ_TIMEOUT("t:") |
| 12553 | IF_ASH_READ_NCHARS("n:s") |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12554 | )) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 12555 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12556 | case 'p': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12557 | prompt = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12558 | break; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12559 | #if ENABLE_ASH_READ_NCHARS |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12560 | case 'n': |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12561 | nchars = bb_strtou(optionarg, NULL, 10); |
| 12562 | if (nchars < 0 || errno) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12563 | ash_msg_and_raise_error("invalid count"); |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12564 | /* nchars == 0: off (bash 3.2 does this too) */ |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12565 | break; |
| 12566 | case 's': |
| 12567 | silent = 1; |
| 12568 | break; |
Ned Ludd | 2123b7c | 2005-02-09 21:07:23 +0000 | [diff] [blame] | 12569 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12570 | #if ENABLE_ASH_READ_TIMEOUT |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12571 | case 't': |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12572 | timeout = bb_strtou(optionarg, NULL, 10); |
| 12573 | if (errno || timeout > UINT_MAX / 2048) |
| 12574 | ash_msg_and_raise_error("invalid timeout"); |
| 12575 | timeout *= 1000; |
| 12576 | #if 0 /* even bash have no -t N.NNN support */ |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12577 | ts.tv_sec = bb_strtou(optionarg, &p, 10); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12578 | ts.tv_usec = 0; |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12579 | /* EINVAL means number is ok, but not terminated by NUL */ |
| 12580 | if (*p == '.' && errno == EINVAL) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12581 | char *p2; |
| 12582 | if (*++p) { |
| 12583 | int scale; |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12584 | ts.tv_usec = bb_strtou(p, &p2, 10); |
| 12585 | if (errno) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12586 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12587 | scale = p2 - p; |
| 12588 | /* normalize to usec */ |
| 12589 | if (scale > 6) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12590 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12591 | while (scale++ < 6) |
| 12592 | ts.tv_usec *= 10; |
| 12593 | } |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12594 | } else if (ts.tv_sec < 0 || errno) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12595 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12596 | } |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12597 | if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12598 | ash_msg_and_raise_error("invalid timeout"); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12599 | } |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12600 | #endif /* if 0 */ |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12601 | break; |
| 12602 | #endif |
| 12603 | case 'r': |
| 12604 | rflag = 1; |
| 12605 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12606 | case 'u': |
| 12607 | fd = bb_strtou(optionarg, NULL, 10); |
| 12608 | if (fd < 0 || errno) |
| 12609 | ash_msg_and_raise_error("invalid file descriptor"); |
| 12610 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12611 | default: |
| 12612 | break; |
| 12613 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12614 | } |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12615 | if (prompt && isatty(fd)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12616 | out2str(prompt); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12617 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12618 | ap = argptr; |
| 12619 | if (*ap == NULL) |
Denis Vlasenko | 9cd4c76 | 2008-06-18 19:22:19 +0000 | [diff] [blame] | 12620 | ap = (char**)arg_REPLY; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12621 | ifs = bltinlookup("IFS"); |
| 12622 | if (ifs == NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12623 | ifs = defifs; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12624 | #if ENABLE_ASH_READ_NCHARS |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12625 | tcgetattr(fd, &tty); |
| 12626 | old_tty = tty; |
| 12627 | if (nchars || silent) { |
| 12628 | if (nchars) { |
| 12629 | tty.c_lflag &= ~ICANON; |
| 12630 | tty.c_cc[VMIN] = nchars < 256 ? nchars : 255; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12631 | } |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12632 | if (silent) { |
| 12633 | tty.c_lflag &= ~(ECHO | ECHOK | ECHONL); |
| 12634 | } |
| 12635 | /* if tcgetattr failed, tcsetattr will fail too. |
| 12636 | * Ignoring, it's harmless. */ |
| 12637 | tcsetattr(fd, TCSANOW, &tty); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12638 | } |
| 12639 | #endif |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12640 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12641 | status = 0; |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12642 | startword = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12643 | backslash = 0; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12644 | #if ENABLE_ASH_READ_TIMEOUT |
| 12645 | if (timeout) /* NB: ensuring end_ms is nonzero */ |
| 12646 | end_ms = ((unsigned)(monotonic_us() / 1000) + timeout) | 1; |
| 12647 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12648 | STARTSTACKSTR(p); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12649 | do { |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12650 | const char *is_ifs; |
| 12651 | |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12652 | #if ENABLE_ASH_READ_TIMEOUT |
| 12653 | if (end_ms) { |
| 12654 | struct pollfd pfd[1]; |
| 12655 | pfd[0].fd = fd; |
| 12656 | pfd[0].events = POLLIN; |
| 12657 | timeout = end_ms - (unsigned)(monotonic_us() / 1000); |
| 12658 | if ((int)timeout <= 0 /* already late? */ |
| 12659 | || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */ |
| 12660 | ) { /* timed out! */ |
| 12661 | #if ENABLE_ASH_READ_NCHARS |
| 12662 | tcsetattr(fd, TCSANOW, &old_tty); |
| 12663 | #endif |
| 12664 | return 1; |
| 12665 | } |
| 12666 | } |
| 12667 | #endif |
| 12668 | if (nonblock_safe_read(fd, &c, 1) != 1) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12669 | status = 1; |
| 12670 | break; |
| 12671 | } |
| 12672 | if (c == '\0') |
| 12673 | continue; |
| 12674 | if (backslash) { |
| 12675 | backslash = 0; |
| 12676 | if (c != '\n') |
| 12677 | goto put; |
| 12678 | continue; |
| 12679 | } |
| 12680 | if (!rflag && c == '\\') { |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12681 | backslash = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12682 | continue; |
| 12683 | } |
| 12684 | if (c == '\n') |
| 12685 | break; |
Denis Vlasenko | f6fbd62 | 2009-03-31 19:36:58 +0000 | [diff] [blame] | 12686 | /* $IFS splitting */ |
Denis Vlasenko | 551ffdc | 2009-04-01 19:48:05 +0000 | [diff] [blame] | 12687 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */ |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12688 | is_ifs = strchr(ifs, c); |
| 12689 | if (startword && is_ifs) { |
| 12690 | if (isspace(c)) |
| 12691 | continue; |
Denis Vlasenko | f6fbd62 | 2009-03-31 19:36:58 +0000 | [diff] [blame] | 12692 | /* it is a non-space ifs char */ |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12693 | startword--; |
| 12694 | if (startword == 1) /* first one? */ |
Denis Vlasenko | f6fbd62 | 2009-03-31 19:36:58 +0000 | [diff] [blame] | 12695 | continue; /* yes, it is not next word yet */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12696 | } |
| 12697 | startword = 0; |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12698 | if (ap[1] != NULL && is_ifs) { |
| 12699 | const char *beg; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12700 | STACKSTRNUL(p); |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12701 | beg = stackblock(); |
| 12702 | setvar(*ap, beg, 0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12703 | ap++; |
Denis Vlasenko | f6fbd62 | 2009-03-31 19:36:58 +0000 | [diff] [blame] | 12704 | /* can we skip one non-space ifs char? (2: yes) */ |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12705 | startword = isspace(c) ? 2 : 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12706 | STARTSTACKSTR(p); |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12707 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12708 | } |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12709 | put: |
| 12710 | STPUTC(c, p); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12711 | } |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12712 | /* end of do {} while: */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12713 | #if ENABLE_ASH_READ_NCHARS |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12714 | while (--nchars); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12715 | #else |
| 12716 | while (1); |
| 12717 | #endif |
| 12718 | |
| 12719 | #if ENABLE_ASH_READ_NCHARS |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12720 | tcsetattr(fd, TCSANOW, &old_tty); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12721 | #endif |
| 12722 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12723 | STACKSTRNUL(p); |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12724 | /* Remove trailing space ifs chars */ |
| 12725 | while ((char *)stackblock() <= --p && isspace(*p) && strchr(ifs, *p) != NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12726 | *p = '\0'; |
| 12727 | setvar(*ap, stackblock(), 0); |
| 12728 | while (*++ap != NULL) |
| 12729 | setvar(*ap, nullstr, 0); |
| 12730 | return status; |
| 12731 | } |
| 12732 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12733 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12734 | umaskcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12735 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12736 | static const char permuser[3] ALIGN1 = "ugo"; |
| 12737 | static const char permmode[3] ALIGN1 = "rwx"; |
| 12738 | static const short permmask[] ALIGN2 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12739 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 12740 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 12741 | S_IROTH, S_IWOTH, S_IXOTH |
| 12742 | }; |
| 12743 | |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 12744 | /* TODO: use bb_parse_mode() instead */ |
| 12745 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12746 | char *ap; |
| 12747 | mode_t mask; |
| 12748 | int i; |
| 12749 | int symbolic_mode = 0; |
| 12750 | |
| 12751 | while (nextopt("S") != '\0') { |
| 12752 | symbolic_mode = 1; |
| 12753 | } |
| 12754 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12755 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12756 | mask = umask(0); |
| 12757 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12758 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12759 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12760 | ap = *argptr; |
| 12761 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12762 | if (symbolic_mode) { |
| 12763 | char buf[18]; |
| 12764 | char *p = buf; |
| 12765 | |
| 12766 | for (i = 0; i < 3; i++) { |
| 12767 | int j; |
| 12768 | |
| 12769 | *p++ = permuser[i]; |
| 12770 | *p++ = '='; |
| 12771 | for (j = 0; j < 3; j++) { |
| 12772 | if ((mask & permmask[3 * i + j]) == 0) { |
| 12773 | *p++ = permmode[j]; |
| 12774 | } |
| 12775 | } |
| 12776 | *p++ = ','; |
| 12777 | } |
| 12778 | *--p = 0; |
| 12779 | puts(buf); |
| 12780 | } else { |
| 12781 | out1fmt("%.4o\n", mask); |
| 12782 | } |
| 12783 | } else { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12784 | if (isdigit((unsigned char) *ap)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12785 | mask = 0; |
| 12786 | do { |
| 12787 | if (*ap >= '8' || *ap < '0') |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 12788 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12789 | mask = (mask << 3) + (*ap - '0'); |
| 12790 | } while (*++ap != '\0'); |
| 12791 | umask(mask); |
| 12792 | } else { |
| 12793 | mask = ~mask & 0777; |
| 12794 | if (!bb_parse_mode(ap, &mask)) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 12795 | ash_msg_and_raise_error("illegal mode: %s", ap); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12796 | } |
| 12797 | umask(~mask & 0777); |
| 12798 | } |
| 12799 | } |
| 12800 | return 0; |
| 12801 | } |
| 12802 | |
| 12803 | /* |
| 12804 | * ulimit builtin |
| 12805 | * |
| 12806 | * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and |
| 12807 | * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with |
| 12808 | * ash by J.T. Conklin. |
| 12809 | * |
| 12810 | * Public domain. |
| 12811 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12812 | struct limits { |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12813 | uint8_t cmd; /* RLIMIT_xxx fit into it */ |
| 12814 | uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12815 | char option; |
| 12816 | }; |
| 12817 | |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12818 | static const struct limits limits_tbl[] = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12819 | #ifdef RLIMIT_CPU |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12820 | { RLIMIT_CPU, 0, 't' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12821 | #endif |
| 12822 | #ifdef RLIMIT_FSIZE |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12823 | { RLIMIT_FSIZE, 9, 'f' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12824 | #endif |
| 12825 | #ifdef RLIMIT_DATA |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12826 | { RLIMIT_DATA, 10, 'd' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12827 | #endif |
| 12828 | #ifdef RLIMIT_STACK |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12829 | { RLIMIT_STACK, 10, 's' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12830 | #endif |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12831 | #ifdef RLIMIT_CORE |
| 12832 | { RLIMIT_CORE, 9, 'c' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12833 | #endif |
| 12834 | #ifdef RLIMIT_RSS |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12835 | { RLIMIT_RSS, 10, 'm' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12836 | #endif |
| 12837 | #ifdef RLIMIT_MEMLOCK |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12838 | { RLIMIT_MEMLOCK, 10, 'l' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12839 | #endif |
| 12840 | #ifdef RLIMIT_NPROC |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12841 | { RLIMIT_NPROC, 0, 'p' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12842 | #endif |
| 12843 | #ifdef RLIMIT_NOFILE |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12844 | { RLIMIT_NOFILE, 0, 'n' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12845 | #endif |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12846 | #ifdef RLIMIT_AS |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12847 | { RLIMIT_AS, 10, 'v' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12848 | #endif |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12849 | #ifdef RLIMIT_LOCKS |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12850 | { RLIMIT_LOCKS, 0, 'w' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12851 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12852 | }; |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12853 | static const char limits_name[] = |
| 12854 | #ifdef RLIMIT_CPU |
| 12855 | "time(seconds)" "\0" |
| 12856 | #endif |
| 12857 | #ifdef RLIMIT_FSIZE |
| 12858 | "file(blocks)" "\0" |
| 12859 | #endif |
| 12860 | #ifdef RLIMIT_DATA |
| 12861 | "data(kb)" "\0" |
| 12862 | #endif |
| 12863 | #ifdef RLIMIT_STACK |
| 12864 | "stack(kb)" "\0" |
| 12865 | #endif |
| 12866 | #ifdef RLIMIT_CORE |
| 12867 | "coredump(blocks)" "\0" |
| 12868 | #endif |
| 12869 | #ifdef RLIMIT_RSS |
| 12870 | "memory(kb)" "\0" |
| 12871 | #endif |
| 12872 | #ifdef RLIMIT_MEMLOCK |
| 12873 | "locked memory(kb)" "\0" |
| 12874 | #endif |
| 12875 | #ifdef RLIMIT_NPROC |
| 12876 | "process" "\0" |
| 12877 | #endif |
| 12878 | #ifdef RLIMIT_NOFILE |
| 12879 | "nofiles" "\0" |
| 12880 | #endif |
| 12881 | #ifdef RLIMIT_AS |
| 12882 | "vmemory(kb)" "\0" |
| 12883 | #endif |
| 12884 | #ifdef RLIMIT_LOCKS |
| 12885 | "locks" "\0" |
| 12886 | #endif |
| 12887 | ; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12888 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12889 | enum limtype { SOFT = 0x1, HARD = 0x2 }; |
| 12890 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12891 | static void |
| 12892 | printlim(enum limtype how, const struct rlimit *limit, |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12893 | const struct limits *l) |
| 12894 | { |
| 12895 | rlim_t val; |
| 12896 | |
| 12897 | val = limit->rlim_max; |
| 12898 | if (how & SOFT) |
| 12899 | val = limit->rlim_cur; |
| 12900 | |
| 12901 | if (val == RLIM_INFINITY) |
| 12902 | out1fmt("unlimited\n"); |
| 12903 | else { |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12904 | val >>= l->factor_shift; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12905 | out1fmt("%lld\n", (long long) val); |
| 12906 | } |
| 12907 | } |
| 12908 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12909 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12910 | ulimitcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12911 | { |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12912 | int c; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12913 | rlim_t val = 0; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12914 | enum limtype how = SOFT | HARD; |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12915 | const struct limits *l; |
| 12916 | int set, all = 0; |
| 12917 | int optc, what; |
| 12918 | struct rlimit limit; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12919 | |
| 12920 | what = 'f'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 12921 | while ((optc = nextopt("HSa" |
| 12922 | #ifdef RLIMIT_CPU |
| 12923 | "t" |
| 12924 | #endif |
| 12925 | #ifdef RLIMIT_FSIZE |
| 12926 | "f" |
| 12927 | #endif |
| 12928 | #ifdef RLIMIT_DATA |
| 12929 | "d" |
| 12930 | #endif |
| 12931 | #ifdef RLIMIT_STACK |
| 12932 | "s" |
| 12933 | #endif |
| 12934 | #ifdef RLIMIT_CORE |
| 12935 | "c" |
| 12936 | #endif |
| 12937 | #ifdef RLIMIT_RSS |
| 12938 | "m" |
| 12939 | #endif |
| 12940 | #ifdef RLIMIT_MEMLOCK |
| 12941 | "l" |
| 12942 | #endif |
| 12943 | #ifdef RLIMIT_NPROC |
| 12944 | "p" |
| 12945 | #endif |
| 12946 | #ifdef RLIMIT_NOFILE |
| 12947 | "n" |
| 12948 | #endif |
| 12949 | #ifdef RLIMIT_AS |
| 12950 | "v" |
| 12951 | #endif |
| 12952 | #ifdef RLIMIT_LOCKS |
| 12953 | "w" |
| 12954 | #endif |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12955 | )) != '\0') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12956 | switch (optc) { |
| 12957 | case 'H': |
| 12958 | how = HARD; |
| 12959 | break; |
| 12960 | case 'S': |
| 12961 | how = SOFT; |
| 12962 | break; |
| 12963 | case 'a': |
| 12964 | all = 1; |
| 12965 | break; |
| 12966 | default: |
| 12967 | what = optc; |
| 12968 | } |
| 12969 | |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12970 | for (l = limits_tbl; l->option != what; l++) |
| 12971 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12972 | |
| 12973 | set = *argptr ? 1 : 0; |
| 12974 | if (set) { |
| 12975 | char *p = *argptr; |
| 12976 | |
| 12977 | if (all || argptr[1]) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12978 | ash_msg_and_raise_error("too many arguments"); |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 12979 | if (strncmp(p, "unlimited\n", 9) == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12980 | val = RLIM_INFINITY; |
| 12981 | else { |
| 12982 | val = (rlim_t) 0; |
| 12983 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12984 | while ((c = *p++) >= '0' && c <= '9') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12985 | val = (val * 10) + (long)(c - '0'); |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 12986 | // val is actually 'unsigned long int' and can't get < 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12987 | if (val < (rlim_t) 0) |
| 12988 | break; |
| 12989 | } |
| 12990 | if (c) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12991 | ash_msg_and_raise_error("bad number"); |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12992 | val <<= l->factor_shift; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12993 | } |
| 12994 | } |
| 12995 | if (all) { |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12996 | const char *lname = limits_name; |
| 12997 | for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12998 | getrlimit(l->cmd, &limit); |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12999 | out1fmt("%-20s ", lname); |
| 13000 | lname += strlen(lname) + 1; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 13001 | printlim(how, &limit, l); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13002 | } |
| 13003 | return 0; |
| 13004 | } |
| 13005 | |
| 13006 | getrlimit(l->cmd, &limit); |
| 13007 | if (set) { |
| 13008 | if (how & HARD) |
| 13009 | limit.rlim_max = val; |
| 13010 | if (how & SOFT) |
| 13011 | limit.rlim_cur = val; |
| 13012 | if (setrlimit(l->cmd, &limit) < 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 13013 | ash_msg_and_raise_error("error setting limit (%m)"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13014 | } else { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 13015 | printlim(how, &limit, l); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13016 | } |
| 13017 | return 0; |
| 13018 | } |
| 13019 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13020 | /* ============ main() and helpers */ |
| 13021 | |
| 13022 | /* |
| 13023 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13024 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13025 | static void exitshell(void) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13026 | static void |
| 13027 | exitshell(void) |
| 13028 | { |
| 13029 | struct jmploc loc; |
| 13030 | char *p; |
| 13031 | int status; |
| 13032 | |
| 13033 | status = exitstatus; |
| 13034 | TRACE(("pid %d, exitshell(%d)\n", getpid(), status)); |
| 13035 | if (setjmp(loc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13036 | if (exception_type == EXEXIT) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13037 | /* dash bug: it just does _exit(exitstatus) here |
| 13038 | * but we have to do setjobctl(0) first! |
| 13039 | * (bug is still not fixed in dash-0.5.3 - if you run dash |
| 13040 | * under Midnight Commander, on exit from dash MC is backgrounded) */ |
| 13041 | status = exitstatus; |
| 13042 | goto out; |
| 13043 | } |
| 13044 | exception_handler = &loc; |
| 13045 | p = trap[0]; |
| 13046 | if (p) { |
| 13047 | trap[0] = NULL; |
| 13048 | evalstring(p, 0); |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 13049 | free(p); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13050 | } |
| 13051 | flush_stdout_stderr(); |
| 13052 | out: |
| 13053 | setjobctl(0); |
| 13054 | _exit(status); |
| 13055 | /* NOTREACHED */ |
| 13056 | } |
| 13057 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 13058 | static void |
| 13059 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13060 | { |
| 13061 | /* from input.c: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 13062 | basepf.next_to_pgetc = basepf.buf = basebuf; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13063 | |
| 13064 | /* from trap.c: */ |
| 13065 | signal(SIGCHLD, SIG_DFL); |
| 13066 | |
| 13067 | /* from var.c: */ |
| 13068 | { |
| 13069 | char **envp; |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 13070 | char ppid[sizeof(int)*3 + 2]; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13071 | const char *p; |
| 13072 | struct stat st1, st2; |
| 13073 | |
| 13074 | initvar(); |
| 13075 | for (envp = environ; envp && *envp; envp++) { |
| 13076 | if (strchr(*envp, '=')) { |
| 13077 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 13078 | } |
| 13079 | } |
| 13080 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 13081 | sprintf(ppid, "%u", (unsigned) getppid()); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13082 | setvar("PPID", ppid, 0); |
| 13083 | |
| 13084 | p = lookupvar("PWD"); |
| 13085 | if (p) |
| 13086 | if (*p != '/' || stat(p, &st1) || stat(".", &st2) |
| 13087 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) |
| 13088 | p = '\0'; |
| 13089 | setpwd(p, 0); |
| 13090 | } |
| 13091 | } |
| 13092 | |
| 13093 | /* |
| 13094 | * Process the shell command line arguments. |
| 13095 | */ |
| 13096 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13097 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13098 | { |
| 13099 | int i; |
| 13100 | const char *xminusc; |
| 13101 | char **xargv; |
| 13102 | |
| 13103 | xargv = argv; |
| 13104 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13105 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13106 | xargv++; |
| 13107 | for (i = 0; i < NOPTS; i++) |
| 13108 | optlist[i] = 2; |
| 13109 | argptr = xargv; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 13110 | if (options(1)) { |
| 13111 | /* it already printed err message */ |
| 13112 | raise_exception(EXERROR); |
| 13113 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13114 | xargv = argptr; |
| 13115 | xminusc = minusc; |
| 13116 | if (*xargv == NULL) { |
| 13117 | if (xminusc) |
| 13118 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 13119 | sflag = 1; |
| 13120 | } |
| 13121 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 13122 | iflag = 1; |
| 13123 | if (mflag == 2) |
| 13124 | mflag = iflag; |
| 13125 | for (i = 0; i < NOPTS; i++) |
| 13126 | if (optlist[i] == 2) |
| 13127 | optlist[i] = 0; |
| 13128 | #if DEBUG == 2 |
| 13129 | debug = 1; |
| 13130 | #endif |
| 13131 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 13132 | if (xminusc) { |
| 13133 | minusc = *xargv++; |
| 13134 | if (*xargv) |
| 13135 | goto setarg0; |
| 13136 | } else if (!sflag) { |
| 13137 | setinputfile(*xargv, 0); |
| 13138 | setarg0: |
| 13139 | arg0 = *xargv++; |
| 13140 | commandname = arg0; |
| 13141 | } |
| 13142 | |
| 13143 | shellparam.p = xargv; |
| 13144 | #if ENABLE_ASH_GETOPTS |
| 13145 | shellparam.optind = 1; |
| 13146 | shellparam.optoff = -1; |
| 13147 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13148 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13149 | while (*xargv) { |
| 13150 | shellparam.nparam++; |
| 13151 | xargv++; |
| 13152 | } |
| 13153 | optschanged(); |
| 13154 | } |
| 13155 | |
| 13156 | /* |
| 13157 | * Read /etc/profile or .profile. |
| 13158 | */ |
| 13159 | static void |
| 13160 | read_profile(const char *name) |
| 13161 | { |
| 13162 | int skip; |
| 13163 | |
| 13164 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 13165 | return; |
| 13166 | skip = cmdloop(0); |
| 13167 | popfile(); |
| 13168 | if (skip) |
| 13169 | exitshell(); |
| 13170 | } |
| 13171 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13172 | /* |
| 13173 | * This routine is called when an error or an interrupt occurs in an |
| 13174 | * interactive shell and control is returned to the main command loop. |
| 13175 | */ |
| 13176 | static void |
| 13177 | reset(void) |
| 13178 | { |
| 13179 | /* from eval.c: */ |
| 13180 | evalskip = 0; |
| 13181 | loopnest = 0; |
| 13182 | /* from input.c: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 13183 | g_parsefile->left_in_buffer = 0; |
| 13184 | g_parsefile->left_in_line = 0; /* clear input buffer */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13185 | popallfiles(); |
| 13186 | /* from parser.c: */ |
| 13187 | tokpushback = 0; |
| 13188 | checkkwd = 0; |
| 13189 | /* from redir.c: */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 13190 | clearredir(/*drop:*/ 0); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13191 | } |
| 13192 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13193 | #if PROFILE |
| 13194 | static short profile_buf[16384]; |
| 13195 | extern int etext(); |
| 13196 | #endif |
| 13197 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13198 | /* |
| 13199 | * Main routine. We initialize things, parse the arguments, execute |
| 13200 | * profiles if we're a login shell, and then call cmdloop to execute |
| 13201 | * commands. The setjmp call sets up the location to jump to when an |
| 13202 | * exception occurs. When an exception occurs the variable "state" |
| 13203 | * is used to figure out how far we had gotten. |
| 13204 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 13205 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13206 | int ash_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13207 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 13208 | const char *shinit; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13209 | volatile smallint state; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13210 | struct jmploc jmploc; |
| 13211 | struct stackmark smark; |
| 13212 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13213 | /* Initialize global data */ |
| 13214 | INIT_G_misc(); |
| 13215 | INIT_G_memstack(); |
| 13216 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 13217 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13218 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 13219 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13220 | INIT_G_cmdtable(); |
| 13221 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13222 | #if PROFILE |
| 13223 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 13224 | #endif |
| 13225 | |
| 13226 | #if ENABLE_FEATURE_EDITING |
| 13227 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
| 13228 | #endif |
| 13229 | state = 0; |
| 13230 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13231 | smallint e; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13232 | smallint s; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13233 | |
| 13234 | reset(); |
| 13235 | |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13236 | e = exception_type; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13237 | if (e == EXERROR) |
| 13238 | exitstatus = 2; |
| 13239 | s = state; |
| 13240 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) |
| 13241 | exitshell(); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13242 | if (e == EXINT) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13243 | outcslow('\n', stderr); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13244 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13245 | popstackmark(&smark); |
| 13246 | FORCE_INT_ON; /* enable interrupts */ |
| 13247 | if (s == 1) |
| 13248 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13249 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13250 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13251 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13252 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13253 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13254 | } |
| 13255 | exception_handler = &jmploc; |
| 13256 | #if DEBUG |
| 13257 | opentrace(); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 13258 | TRACE(("Shell args: ")); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13259 | trace_puts_args(argv); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13260 | #endif |
| 13261 | rootpid = getpid(); |
| 13262 | |
| 13263 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 13264 | /* Can use monotonic_ns() for better randomness but for now it is |
| 13265 | * not used anywhere else in busybox... so avoid bloat */ |
| 13266 | random_galois_LFSR = random_LCG = rootpid + monotonic_us(); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13267 | #endif |
| 13268 | init(); |
| 13269 | setstackmark(&smark); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13270 | procargs(argv); |
| 13271 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13272 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
| 13273 | if (iflag) { |
| 13274 | const char *hp = lookupvar("HISTFILE"); |
| 13275 | |
| 13276 | if (hp == NULL) { |
| 13277 | hp = lookupvar("HOME"); |
| 13278 | if (hp != NULL) { |
| 13279 | char *defhp = concat_path_file(hp, ".ash_history"); |
| 13280 | setvar("HISTFILE", defhp, 0); |
| 13281 | free(defhp); |
| 13282 | } |
| 13283 | } |
| 13284 | } |
| 13285 | #endif |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13286 | if (/* argv[0] && */ argv[0][0] == '-') |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13287 | isloginsh = 1; |
| 13288 | if (isloginsh) { |
| 13289 | state = 1; |
| 13290 | read_profile("/etc/profile"); |
| 13291 | state1: |
| 13292 | state = 2; |
| 13293 | read_profile(".profile"); |
| 13294 | } |
| 13295 | state2: |
| 13296 | state = 3; |
| 13297 | if ( |
| 13298 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13299 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13300 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13301 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13302 | ) { |
| 13303 | shinit = lookupvar("ENV"); |
| 13304 | if (shinit != NULL && *shinit != '\0') { |
| 13305 | read_profile(shinit); |
| 13306 | } |
| 13307 | } |
| 13308 | state3: |
| 13309 | state = 4; |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13310 | if (minusc) { |
| 13311 | /* evalstring pushes parsefile stack. |
| 13312 | * Ensure we don't falsely claim that 0 (stdin) |
Denis Vlasenko | 5368ad5 | 2009-03-20 10:20:08 +0000 | [diff] [blame] | 13313 | * is one of stacked source fds. |
| 13314 | * Testcase: ash -c 'exec 1>&0' must not complain. */ |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13315 | if (!sflag) |
| 13316 | g_parsefile->fd = -1; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13317 | evalstring(minusc, 0); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13318 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13319 | |
| 13320 | if (sflag || minusc == NULL) { |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 13321 | #if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 13322 | if (iflag) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13323 | const char *hp = lookupvar("HISTFILE"); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13324 | if (hp) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13325 | line_input_state->hist_file = hp; |
| 13326 | } |
| 13327 | #endif |
| 13328 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 13329 | cmdloop(1); |
| 13330 | } |
| 13331 | #if PROFILE |
| 13332 | monitor(0); |
| 13333 | #endif |
| 13334 | #ifdef GPROF |
| 13335 | { |
| 13336 | extern void _mcleanup(void); |
| 13337 | _mcleanup(); |
| 13338 | } |
| 13339 | #endif |
| 13340 | exitshell(); |
| 13341 | /* NOTREACHED */ |
| 13342 | } |
| 13343 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13344 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13345 | /*- |
| 13346 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 13347 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13348 | * |
| 13349 | * This code is derived from software contributed to Berkeley by |
| 13350 | * Kenneth Almquist. |
| 13351 | * |
| 13352 | * Redistribution and use in source and binary forms, with or without |
| 13353 | * modification, are permitted provided that the following conditions |
| 13354 | * are met: |
| 13355 | * 1. Redistributions of source code must retain the above copyright |
| 13356 | * notice, this list of conditions and the following disclaimer. |
| 13357 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13358 | * notice, this list of conditions and the following disclaimer in the |
| 13359 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13360 | * 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] | 13361 | * may be used to endorse or promote products derived from this software |
| 13362 | * without specific prior written permission. |
| 13363 | * |
| 13364 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 13365 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13366 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 13367 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13368 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13369 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 13370 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13371 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 13372 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 13373 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 13374 | * SUCH DAMAGE. |
| 13375 | */ |