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 | * |
| 11 | * |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12 | * This code is derived from software contributed to Berkeley by |
| 13 | * Kenneth Almquist. |
| 14 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 15 | * 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] | 16 | * |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 17 | * Original BSD copyright notice is retained at the end of this file. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 20 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 21 | * rewrite arith.y to micro stack based cryptic algorithm by |
| 22 | * Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com> |
| 23 | * |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 24 | * Modified by Paul Mundt <lethal@linux-sh.org> (c) 2004 to support |
| 25 | * dynamic variables. |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 26 | * |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 27 | * Modified by Vladimir Oleynik <dzo@simtreas.ru> (c) 2001-2005 to be |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 28 | * used in busybox and size optimizations, |
| 29 | * rewrote arith (see notes to this), added locale support, |
| 30 | * rewrote dynamic variables. |
| 31 | * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 32 | */ |
| 33 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 34 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 35 | * The follow should be set to reflect the type of system you have: |
| 36 | * JOBS -> 1 if you have Berkeley job control, 0 otherwise. |
| 37 | * define SYSV if you are running under System V. |
| 38 | * define DEBUG=1 to compile in debugging ('set -o debug' to turn on) |
| 39 | * define DEBUG=2 to compile in and turn on debugging. |
| 40 | * |
| 41 | * When debugging is on, debugging info will be written to ./trace and |
| 42 | * a quit signal will generate a core dump. |
| 43 | */ |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 44 | #define DEBUG 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 45 | #define PROFILE 0 |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 46 | |
| 47 | #define IFS_BROKEN |
| 48 | |
| 49 | #define JOBS ENABLE_ASH_JOB_CONTROL |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 51 | #if DEBUG |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 52 | #ifndef _GNU_SOURCE |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 53 | #define _GNU_SOURCE |
| 54 | #endif |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 55 | #endif |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 57 | #include "busybox.h" /* for applet_names */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 58 | #include <paths.h> |
| 59 | #include <setjmp.h> |
| 60 | #include <fnmatch.h> |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 61 | #if JOBS || ENABLE_ASH_READ_NCHARS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 62 | #include <termios.h> |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 65 | #if defined(__uClinux__) |
| 66 | #error "Do not even bother, ash will not run on uClinux" |
| 67 | #endif |
| 68 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 70 | /* ============ Hash table sizes. Configurable. */ |
| 71 | |
| 72 | #define VTABSIZE 39 |
| 73 | #define ATABSIZE 39 |
| 74 | #define CMDTABLESIZE 31 /* should be prime */ |
| 75 | |
| 76 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 77 | /* ============ Misc helpers */ |
| 78 | |
| 79 | #define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0) |
| 80 | |
| 81 | /* C99 say: "char" declaration may be signed or unsigned default */ |
| 82 | #define signed_char2int(sc) ((int)((signed char)sc)) |
| 83 | |
| 84 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 85 | /* ============ Shell options */ |
| 86 | |
| 87 | static const char *const optletters_optnames[] = { |
| 88 | "e" "errexit", |
| 89 | "f" "noglob", |
| 90 | "I" "ignoreeof", |
| 91 | "i" "interactive", |
| 92 | "m" "monitor", |
| 93 | "n" "noexec", |
| 94 | "s" "stdin", |
| 95 | "x" "xtrace", |
| 96 | "v" "verbose", |
| 97 | "C" "noclobber", |
| 98 | "a" "allexport", |
| 99 | "b" "notify", |
| 100 | "u" "nounset", |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 101 | "\0" "vi" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 102 | #if DEBUG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 103 | ,"\0" "nolog" |
| 104 | ,"\0" "debug" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 105 | #endif |
| 106 | }; |
| 107 | |
| 108 | #define optletters(n) optletters_optnames[(n)][0] |
| 109 | #define optnames(n) (&optletters_optnames[(n)][1]) |
| 110 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 111 | enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 113 | static char optlist[NOPTS] ALIGN1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 114 | |
| 115 | #define eflag optlist[0] |
| 116 | #define fflag optlist[1] |
| 117 | #define Iflag optlist[2] |
| 118 | #define iflag optlist[3] |
| 119 | #define mflag optlist[4] |
| 120 | #define nflag optlist[5] |
| 121 | #define sflag optlist[6] |
| 122 | #define xflag optlist[7] |
| 123 | #define vflag optlist[8] |
| 124 | #define Cflag optlist[9] |
| 125 | #define aflag optlist[10] |
| 126 | #define bflag optlist[11] |
| 127 | #define uflag optlist[12] |
| 128 | #define viflag optlist[13] |
| 129 | #if DEBUG |
| 130 | #define nolog optlist[14] |
| 131 | #define debug optlist[15] |
| 132 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 133 | |
| 134 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 135 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 137 | static const char homestr[] ALIGN1 = "HOME"; |
| 138 | static const char snlfmt[] ALIGN1 = "%s\n"; |
| 139 | static const char illnum[] ALIGN1 = "Illegal number: %s"; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 141 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 142 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 143 | * jump locations. The global variable handler contains the location to |
| 144 | * jump to when an exception occurs, and the global variable exception |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 145 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 146 | * exception handlers, the user should save the value of handler on entry |
| 147 | * to an inner scope, set handler to point to a jmploc structure for the |
| 148 | * inner scope, and restore handler on exit from the scope. |
| 149 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 150 | struct jmploc { |
| 151 | jmp_buf loc; |
| 152 | }; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 153 | |
| 154 | struct globals_misc { |
| 155 | /* pid of main shell */ |
| 156 | int rootpid; |
| 157 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 158 | int shlvl; |
| 159 | #define rootshell (!shlvl) |
| 160 | char *minusc; /* argument to -c option */ |
| 161 | |
| 162 | char *curdir; // = nullstr; /* current working directory */ |
| 163 | char *physdir; // = nullstr; /* physical working directory */ |
| 164 | |
| 165 | char *arg0; /* value of $0 */ |
| 166 | |
| 167 | struct jmploc *exception_handler; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 168 | |
| 169 | // disabled by vda: cannot understand how it was supposed to work - |
| 170 | // cannot fix bugs. That's why you have to explain your non-trivial designs! |
| 171 | // /* do we generate EXSIG events */ |
| 172 | // int exsig; /* counter */ |
| 173 | volatile int suppressint; /* counter */ |
| 174 | volatile /*sig_atomic_t*/ smallint intpending; /* 1 = got SIGINT */ |
| 175 | /* last pending signal */ |
| 176 | volatile /*sig_atomic_t*/ smallint pendingsig; |
| 177 | smallint exception; /* kind of exception (0..5) */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 178 | /* exceptions */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 179 | #define EXINT 0 /* SIGINT received */ |
| 180 | #define EXERROR 1 /* a generic error */ |
| 181 | #define EXSHELLPROC 2 /* execute a shell procedure */ |
| 182 | #define EXEXEC 3 /* command execution failed */ |
| 183 | #define EXEXIT 4 /* exit the shell */ |
| 184 | #define EXSIG 5 /* trapped signal in wait(1) */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 185 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 186 | /* trap handler commands */ |
| 187 | char *trap[NSIG]; |
| 188 | smallint isloginsh; |
| 189 | char nullstr[1]; /* zero length string */ |
| 190 | /* |
| 191 | * Sigmode records the current value of the signal handlers for the various |
| 192 | * modes. A value of zero means that the current handler is not known. |
| 193 | * S_HARD_IGN indicates that the signal was ignored on entry to the shell, |
| 194 | */ |
| 195 | char sigmode[NSIG - 1]; |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 196 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 197 | #define S_CATCH 2 /* signal is caught */ |
| 198 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
| 199 | #define S_HARD_IGN 4 /* signal is ignored permenantly */ |
| 200 | #define S_RESET 5 /* temporary - to reset a hard ignored sig */ |
| 201 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 202 | /* indicates specified signal received */ |
| 203 | char gotsig[NSIG - 1]; |
| 204 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 205 | extern struct globals_misc *const ash_ptr_to_globals_misc; |
| 206 | #define G_misc (*ash_ptr_to_globals_misc) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 207 | #define rootpid (G_misc.rootpid ) |
| 208 | #define shlvl (G_misc.shlvl ) |
| 209 | #define minusc (G_misc.minusc ) |
| 210 | #define curdir (G_misc.curdir ) |
| 211 | #define physdir (G_misc.physdir ) |
| 212 | #define arg0 (G_misc.arg0 ) |
| 213 | #define exception_handler (G_misc.exception_handler) |
| 214 | #define exception (G_misc.exception ) |
| 215 | #define suppressint (G_misc.suppressint ) |
| 216 | #define intpending (G_misc.intpending ) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 217 | //#define exsig (G_misc.exsig ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 218 | #define pendingsig (G_misc.pendingsig ) |
| 219 | #define trap (G_misc.trap ) |
| 220 | #define isloginsh (G_misc.isloginsh) |
| 221 | #define nullstr (G_misc.nullstr ) |
| 222 | #define sigmode (G_misc.sigmode ) |
| 223 | #define gotsig (G_misc.gotsig ) |
| 224 | #define INIT_G_misc() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 225 | (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ |
| 226 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 227 | curdir = nullstr; \ |
| 228 | physdir = nullstr; \ |
| 229 | } while (0) |
| 230 | |
| 231 | |
| 232 | /* ============ Interrupts / exceptions */ |
| 233 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 234 | /* |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 235 | * These macros allow the user to suspend the handling of interrupt signals |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 236 | * 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] | 237 | * much more efficient and portable. (But hacking the kernel is so much |
| 238 | * more fun than worrying about efficiency and portability. :-)) |
| 239 | */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 240 | #define INT_OFF \ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 241 | do { \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 242 | suppressint++; \ |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 243 | xbarrier(); \ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 244 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 245 | |
| 246 | /* |
| 247 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 248 | * just do a longjmp to the exception handler. The type of exception is |
| 249 | * stored in the global variable "exception". |
| 250 | */ |
| 251 | static void raise_exception(int) ATTRIBUTE_NORETURN; |
| 252 | static void |
| 253 | raise_exception(int e) |
| 254 | { |
| 255 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 256 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 257 | abort(); |
| 258 | #endif |
| 259 | INT_OFF; |
| 260 | exception = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 261 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Called from trap.c when a SIGINT is received. (If the user specifies |
| 266 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 267 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 268 | * are held using the INT_OFF macro. (The test for iflag is just |
| 269 | * defensive programming.) |
| 270 | */ |
| 271 | static void raise_interrupt(void) ATTRIBUTE_NORETURN; |
| 272 | static void |
| 273 | raise_interrupt(void) |
| 274 | { |
| 275 | int i; |
| 276 | |
| 277 | intpending = 0; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 278 | /* Signal is not automatically unmasked after it is raised, |
| 279 | * do it ourself - unmask all signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 280 | sigprocmask_allsigs(SIG_UNBLOCK); |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 281 | /* pendingsig = 0; - now done in onsig() */ |
| 282 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 283 | i = EXSIG; |
| 284 | if (gotsig[SIGINT - 1] && !trap[SIGINT]) { |
| 285 | if (!(rootshell && iflag)) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 286 | /* Kill ourself with SIGINT */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 287 | signal(SIGINT, SIG_DFL); |
| 288 | raise(SIGINT); |
| 289 | } |
| 290 | i = EXINT; |
| 291 | } |
| 292 | raise_exception(i); |
| 293 | /* NOTREACHED */ |
| 294 | } |
| 295 | |
| 296 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 297 | static void |
| 298 | int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 299 | { |
| 300 | if (--suppressint == 0 && intpending) { |
| 301 | raise_interrupt(); |
| 302 | } |
| 303 | } |
| 304 | #define INT_ON int_on() |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 305 | static void |
| 306 | force_int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 307 | { |
| 308 | suppressint = 0; |
| 309 | if (intpending) |
| 310 | raise_interrupt(); |
| 311 | } |
| 312 | #define FORCE_INT_ON force_int_on() |
| 313 | #else |
| 314 | #define INT_ON \ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 315 | do { \ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 316 | xbarrier(); \ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 317 | if (--suppressint == 0 && intpending) \ |
| 318 | raise_interrupt(); \ |
| 319 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 320 | #define FORCE_INT_ON \ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 321 | do { \ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 322 | xbarrier(); \ |
| 323 | suppressint = 0; \ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 324 | if (intpending) \ |
| 325 | raise_interrupt(); \ |
| 326 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 327 | #endif /* ASH_OPTIMIZE_FOR_SIZE */ |
| 328 | |
| 329 | #define SAVE_INT(v) ((v) = suppressint) |
| 330 | |
| 331 | #define RESTORE_INT(v) \ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 332 | do { \ |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 333 | xbarrier(); \ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 334 | suppressint = (v); \ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 335 | if (suppressint == 0 && intpending) \ |
| 336 | raise_interrupt(); \ |
| 337 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 338 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 339 | /* |
| 340 | * Ignore a signal. Only one usage site - in forkchild() |
| 341 | */ |
| 342 | static void |
| 343 | ignoresig(int signo) |
| 344 | { |
| 345 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 346 | signal(signo, SIG_IGN); |
| 347 | } |
| 348 | sigmode[signo - 1] = S_HARD_IGN; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Signal handler. Only one usage site - in setsignal() |
| 353 | */ |
| 354 | static void |
| 355 | onsig(int signo) |
| 356 | { |
| 357 | gotsig[signo - 1] = 1; |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 358 | pendingsig = signo; |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 359 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 360 | if ( /* exsig || */ (signo == SIGINT && !trap[SIGINT])) { |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 361 | if (!suppressint) { |
| 362 | pendingsig = 0; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 363 | raise_interrupt(); /* does not return */ |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 364 | } |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 365 | intpending = 1; |
| 366 | } |
| 367 | } |
| 368 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 369 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 370 | /* ============ Stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 371 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 372 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 373 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 374 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 375 | INT_OFF; |
| 376 | fputs(p, file); |
| 377 | INT_ON; |
| 378 | } |
| 379 | |
| 380 | static void |
| 381 | flush_stdout_stderr(void) |
| 382 | { |
| 383 | INT_OFF; |
| 384 | fflush(stdout); |
| 385 | fflush(stderr); |
| 386 | INT_ON; |
| 387 | } |
| 388 | |
| 389 | static void |
| 390 | flush_stderr(void) |
| 391 | { |
| 392 | INT_OFF; |
| 393 | fflush(stderr); |
| 394 | INT_ON; |
| 395 | } |
| 396 | |
| 397 | static void |
| 398 | outcslow(int c, FILE *dest) |
| 399 | { |
| 400 | INT_OFF; |
| 401 | putc(c, dest); |
| 402 | fflush(dest); |
| 403 | INT_ON; |
| 404 | } |
| 405 | |
| 406 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 407 | static int |
| 408 | out1fmt(const char *fmt, ...) |
| 409 | { |
| 410 | va_list ap; |
| 411 | int r; |
| 412 | |
| 413 | INT_OFF; |
| 414 | va_start(ap, fmt); |
| 415 | r = vprintf(fmt, ap); |
| 416 | va_end(ap); |
| 417 | INT_ON; |
| 418 | return r; |
| 419 | } |
| 420 | |
| 421 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 422 | static int |
| 423 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 424 | { |
| 425 | va_list ap; |
| 426 | int ret; |
| 427 | |
| 428 | va_start(ap, fmt); |
| 429 | INT_OFF; |
| 430 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 431 | va_end(ap); |
| 432 | INT_ON; |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | static void |
| 437 | out1str(const char *p) |
| 438 | { |
| 439 | outstr(p, stdout); |
| 440 | } |
| 441 | |
| 442 | static void |
| 443 | out2str(const char *p) |
| 444 | { |
| 445 | outstr(p, stderr); |
| 446 | flush_stderr(); |
| 447 | } |
| 448 | |
| 449 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 450 | /* ============ Parser structures */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 451 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 452 | /* control characters in argument strings */ |
| 453 | #define CTLESC '\201' /* escape next character */ |
| 454 | #define CTLVAR '\202' /* variable defn */ |
| 455 | #define CTLENDVAR '\203' |
| 456 | #define CTLBACKQ '\204' |
| 457 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ |
| 458 | /* CTLBACKQ | CTLQUOTE == '\205' */ |
| 459 | #define CTLARI '\206' /* arithmetic expression */ |
| 460 | #define CTLENDARI '\207' |
| 461 | #define CTLQUOTEMARK '\210' |
| 462 | |
| 463 | /* variable substitution byte (follows CTLVAR) */ |
| 464 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 465 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
| 466 | #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */ |
| 467 | |
| 468 | /* values of VSTYPE field */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 469 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 470 | #define VSMINUS 0x2 /* ${var-text} */ |
| 471 | #define VSPLUS 0x3 /* ${var+text} */ |
| 472 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 473 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 474 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 475 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 476 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 477 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 478 | #define VSLENGTH 0xa /* ${#var} */ |
| 479 | #if ENABLE_ASH_BASH_COMPAT |
| 480 | #define VSSUBSTR 0xc /* ${var:position:length} */ |
| 481 | #define VSREPLACE 0xd /* ${var/pattern/replacement} */ |
| 482 | #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */ |
| 483 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 484 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 485 | static const char dolatstr[] ALIGN1 = { |
| 486 | CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' |
| 487 | }; |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 488 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 489 | #define NCMD 0 |
| 490 | #define NPIPE 1 |
| 491 | #define NREDIR 2 |
| 492 | #define NBACKGND 3 |
| 493 | #define NSUBSHELL 4 |
| 494 | #define NAND 5 |
| 495 | #define NOR 6 |
| 496 | #define NSEMI 7 |
| 497 | #define NIF 8 |
| 498 | #define NWHILE 9 |
| 499 | #define NUNTIL 10 |
| 500 | #define NFOR 11 |
| 501 | #define NCASE 12 |
| 502 | #define NCLIST 13 |
| 503 | #define NDEFUN 14 |
| 504 | #define NARG 15 |
| 505 | #define NTO 16 |
| 506 | #define NCLOBBER 17 |
| 507 | #define NFROM 18 |
| 508 | #define NFROMTO 19 |
| 509 | #define NAPPEND 20 |
| 510 | #define NTOFD 21 |
| 511 | #define NFROMFD 22 |
| 512 | #define NHERE 23 |
| 513 | #define NXHERE 24 |
| 514 | #define NNOT 25 |
| 515 | |
| 516 | union node; |
| 517 | |
| 518 | struct ncmd { |
| 519 | int type; |
| 520 | union node *assign; |
| 521 | union node *args; |
| 522 | union node *redirect; |
| 523 | }; |
| 524 | |
| 525 | struct npipe { |
| 526 | int type; |
| 527 | int backgnd; |
| 528 | struct nodelist *cmdlist; |
| 529 | }; |
| 530 | |
| 531 | struct nredir { |
| 532 | int type; |
| 533 | union node *n; |
| 534 | union node *redirect; |
| 535 | }; |
| 536 | |
| 537 | struct nbinary { |
| 538 | int type; |
| 539 | union node *ch1; |
| 540 | union node *ch2; |
| 541 | }; |
| 542 | |
| 543 | struct nif { |
| 544 | int type; |
| 545 | union node *test; |
| 546 | union node *ifpart; |
| 547 | union node *elsepart; |
| 548 | }; |
| 549 | |
| 550 | struct nfor { |
| 551 | int type; |
| 552 | union node *args; |
| 553 | union node *body; |
| 554 | char *var; |
| 555 | }; |
| 556 | |
| 557 | struct ncase { |
| 558 | int type; |
| 559 | union node *expr; |
| 560 | union node *cases; |
| 561 | }; |
| 562 | |
| 563 | struct nclist { |
| 564 | int type; |
| 565 | union node *next; |
| 566 | union node *pattern; |
| 567 | union node *body; |
| 568 | }; |
| 569 | |
| 570 | struct narg { |
| 571 | int type; |
| 572 | union node *next; |
| 573 | char *text; |
| 574 | struct nodelist *backquote; |
| 575 | }; |
| 576 | |
| 577 | struct nfile { |
| 578 | int type; |
| 579 | union node *next; |
| 580 | int fd; |
| 581 | union node *fname; |
| 582 | char *expfname; |
| 583 | }; |
| 584 | |
| 585 | struct ndup { |
| 586 | int type; |
| 587 | union node *next; |
| 588 | int fd; |
| 589 | int dupfd; |
| 590 | union node *vname; |
| 591 | }; |
| 592 | |
| 593 | struct nhere { |
| 594 | int type; |
| 595 | union node *next; |
| 596 | int fd; |
| 597 | union node *doc; |
| 598 | }; |
| 599 | |
| 600 | struct nnot { |
| 601 | int type; |
| 602 | union node *com; |
| 603 | }; |
| 604 | |
| 605 | union node { |
| 606 | int type; |
| 607 | struct ncmd ncmd; |
| 608 | struct npipe npipe; |
| 609 | struct nredir nredir; |
| 610 | struct nbinary nbinary; |
| 611 | struct nif nif; |
| 612 | struct nfor nfor; |
| 613 | struct ncase ncase; |
| 614 | struct nclist nclist; |
| 615 | struct narg narg; |
| 616 | struct nfile nfile; |
| 617 | struct ndup ndup; |
| 618 | struct nhere nhere; |
| 619 | struct nnot nnot; |
| 620 | }; |
| 621 | |
| 622 | struct nodelist { |
| 623 | struct nodelist *next; |
| 624 | union node *n; |
| 625 | }; |
| 626 | |
| 627 | struct funcnode { |
| 628 | int count; |
| 629 | union node n; |
| 630 | }; |
| 631 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 632 | /* |
| 633 | * Free a parse tree. |
| 634 | */ |
| 635 | static void |
| 636 | freefunc(struct funcnode *f) |
| 637 | { |
| 638 | if (f && --f->count < 0) |
| 639 | free(f); |
| 640 | } |
| 641 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 642 | |
| 643 | /* ============ Debugging output */ |
| 644 | |
| 645 | #if DEBUG |
| 646 | |
| 647 | static FILE *tracefile; |
| 648 | |
| 649 | static void |
| 650 | trace_printf(const char *fmt, ...) |
| 651 | { |
| 652 | va_list va; |
| 653 | |
| 654 | if (debug != 1) |
| 655 | return; |
| 656 | va_start(va, fmt); |
| 657 | vfprintf(tracefile, fmt, va); |
| 658 | va_end(va); |
| 659 | } |
| 660 | |
| 661 | static void |
| 662 | trace_vprintf(const char *fmt, va_list va) |
| 663 | { |
| 664 | if (debug != 1) |
| 665 | return; |
| 666 | vfprintf(tracefile, fmt, va); |
| 667 | } |
| 668 | |
| 669 | static void |
| 670 | trace_puts(const char *s) |
| 671 | { |
| 672 | if (debug != 1) |
| 673 | return; |
| 674 | fputs(s, tracefile); |
| 675 | } |
| 676 | |
| 677 | static void |
| 678 | trace_puts_quoted(char *s) |
| 679 | { |
| 680 | char *p; |
| 681 | char c; |
| 682 | |
| 683 | if (debug != 1) |
| 684 | return; |
| 685 | putc('"', tracefile); |
| 686 | for (p = s; *p; p++) { |
| 687 | switch (*p) { |
| 688 | case '\n': c = 'n'; goto backslash; |
| 689 | case '\t': c = 't'; goto backslash; |
| 690 | case '\r': c = 'r'; goto backslash; |
| 691 | case '"': c = '"'; goto backslash; |
| 692 | case '\\': c = '\\'; goto backslash; |
| 693 | case CTLESC: c = 'e'; goto backslash; |
| 694 | case CTLVAR: c = 'v'; goto backslash; |
| 695 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash; |
| 696 | case CTLBACKQ: c = 'q'; goto backslash; |
| 697 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash; |
| 698 | backslash: |
| 699 | putc('\\', tracefile); |
| 700 | putc(c, tracefile); |
| 701 | break; |
| 702 | default: |
| 703 | if (*p >= ' ' && *p <= '~') |
| 704 | putc(*p, tracefile); |
| 705 | else { |
| 706 | putc('\\', tracefile); |
| 707 | putc(*p >> 6 & 03, tracefile); |
| 708 | putc(*p >> 3 & 07, tracefile); |
| 709 | putc(*p & 07, tracefile); |
| 710 | } |
| 711 | break; |
| 712 | } |
| 713 | } |
| 714 | putc('"', tracefile); |
| 715 | } |
| 716 | |
| 717 | static void |
| 718 | trace_puts_args(char **ap) |
| 719 | { |
| 720 | if (debug != 1) |
| 721 | return; |
| 722 | if (!*ap) |
| 723 | return; |
| 724 | while (1) { |
| 725 | trace_puts_quoted(*ap); |
| 726 | if (!*++ap) { |
| 727 | putc('\n', tracefile); |
| 728 | break; |
| 729 | } |
| 730 | putc(' ', tracefile); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | static void |
| 735 | opentrace(void) |
| 736 | { |
| 737 | char s[100]; |
| 738 | #ifdef O_APPEND |
| 739 | int flags; |
| 740 | #endif |
| 741 | |
| 742 | if (debug != 1) { |
| 743 | if (tracefile) |
| 744 | fflush(tracefile); |
| 745 | /* leave open because libedit might be using it */ |
| 746 | return; |
| 747 | } |
| 748 | strcpy(s, "./trace"); |
| 749 | if (tracefile) { |
| 750 | if (!freopen(s, "a", tracefile)) { |
| 751 | fprintf(stderr, "Can't re-open %s\n", s); |
| 752 | debug = 0; |
| 753 | return; |
| 754 | } |
| 755 | } else { |
| 756 | tracefile = fopen(s, "a"); |
| 757 | if (tracefile == NULL) { |
| 758 | fprintf(stderr, "Can't open %s\n", s); |
| 759 | debug = 0; |
| 760 | return; |
| 761 | } |
| 762 | } |
| 763 | #ifdef O_APPEND |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 764 | flags = fcntl(fileno(tracefile), F_GETFL); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 765 | if (flags >= 0) |
| 766 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 767 | #endif |
| 768 | setlinebuf(tracefile); |
| 769 | fputs("\nTracing started.\n", tracefile); |
| 770 | } |
| 771 | |
| 772 | static void |
| 773 | indent(int amount, char *pfx, FILE *fp) |
| 774 | { |
| 775 | int i; |
| 776 | |
| 777 | for (i = 0; i < amount; i++) { |
| 778 | if (pfx && i == amount - 1) |
| 779 | fputs(pfx, fp); |
| 780 | putc('\t', fp); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | /* little circular references here... */ |
| 785 | static void shtree(union node *n, int ind, char *pfx, FILE *fp); |
| 786 | |
| 787 | static void |
| 788 | sharg(union node *arg, FILE *fp) |
| 789 | { |
| 790 | char *p; |
| 791 | struct nodelist *bqlist; |
| 792 | int subtype; |
| 793 | |
| 794 | if (arg->type != NARG) { |
| 795 | out1fmt("<node type %d>\n", arg->type); |
| 796 | abort(); |
| 797 | } |
| 798 | bqlist = arg->narg.backquote; |
| 799 | for (p = arg->narg.text; *p; p++) { |
| 800 | switch (*p) { |
| 801 | case CTLESC: |
| 802 | putc(*++p, fp); |
| 803 | break; |
| 804 | case CTLVAR: |
| 805 | putc('$', fp); |
| 806 | putc('{', fp); |
| 807 | subtype = *++p; |
| 808 | if (subtype == VSLENGTH) |
| 809 | putc('#', fp); |
| 810 | |
| 811 | while (*p != '=') |
| 812 | putc(*p++, fp); |
| 813 | |
| 814 | if (subtype & VSNUL) |
| 815 | putc(':', fp); |
| 816 | |
| 817 | switch (subtype & VSTYPE) { |
| 818 | case VSNORMAL: |
| 819 | putc('}', fp); |
| 820 | break; |
| 821 | case VSMINUS: |
| 822 | putc('-', fp); |
| 823 | break; |
| 824 | case VSPLUS: |
| 825 | putc('+', fp); |
| 826 | break; |
| 827 | case VSQUESTION: |
| 828 | putc('?', fp); |
| 829 | break; |
| 830 | case VSASSIGN: |
| 831 | putc('=', fp); |
| 832 | break; |
| 833 | case VSTRIMLEFT: |
| 834 | putc('#', fp); |
| 835 | break; |
| 836 | case VSTRIMLEFTMAX: |
| 837 | putc('#', fp); |
| 838 | putc('#', fp); |
| 839 | break; |
| 840 | case VSTRIMRIGHT: |
| 841 | putc('%', fp); |
| 842 | break; |
| 843 | case VSTRIMRIGHTMAX: |
| 844 | putc('%', fp); |
| 845 | putc('%', fp); |
| 846 | break; |
| 847 | case VSLENGTH: |
| 848 | break; |
| 849 | default: |
| 850 | out1fmt("<subtype %d>", subtype); |
| 851 | } |
| 852 | break; |
| 853 | case CTLENDVAR: |
| 854 | putc('}', fp); |
| 855 | break; |
| 856 | case CTLBACKQ: |
| 857 | case CTLBACKQ|CTLQUOTE: |
| 858 | putc('$', fp); |
| 859 | putc('(', fp); |
| 860 | shtree(bqlist->n, -1, NULL, fp); |
| 861 | putc(')', fp); |
| 862 | break; |
| 863 | default: |
| 864 | putc(*p, fp); |
| 865 | break; |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | static void |
| 871 | shcmd(union node *cmd, FILE *fp) |
| 872 | { |
| 873 | union node *np; |
| 874 | int first; |
| 875 | const char *s; |
| 876 | int dftfd; |
| 877 | |
| 878 | first = 1; |
| 879 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 880 | if (!first) |
| 881 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 882 | sharg(np, fp); |
| 883 | first = 0; |
| 884 | } |
| 885 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 886 | if (!first) |
| 887 | putc(' ', fp); |
| 888 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 889 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 890 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 891 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 892 | case NAPPEND: s = ">>"; dftfd = 1; break; |
| 893 | case NTOFD: s = ">&"; dftfd = 1; break; |
| 894 | case NFROM: s = "<"; break; |
| 895 | case NFROMFD: s = "<&"; break; |
| 896 | case NFROMTO: s = "<>"; break; |
| 897 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 898 | } |
| 899 | if (np->nfile.fd != dftfd) |
| 900 | fprintf(fp, "%d", np->nfile.fd); |
| 901 | fputs(s, fp); |
| 902 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 903 | fprintf(fp, "%d", np->ndup.dupfd); |
| 904 | } else { |
| 905 | sharg(np->nfile.fname, fp); |
| 906 | } |
| 907 | first = 0; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | static void |
| 912 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 913 | { |
| 914 | struct nodelist *lp; |
| 915 | const char *s; |
| 916 | |
| 917 | if (n == NULL) |
| 918 | return; |
| 919 | |
| 920 | indent(ind, pfx, fp); |
| 921 | switch (n->type) { |
| 922 | case NSEMI: |
| 923 | s = "; "; |
| 924 | goto binop; |
| 925 | case NAND: |
| 926 | s = " && "; |
| 927 | goto binop; |
| 928 | case NOR: |
| 929 | s = " || "; |
| 930 | binop: |
| 931 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 932 | /* if (ind < 0) */ |
| 933 | fputs(s, fp); |
| 934 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 935 | break; |
| 936 | case NCMD: |
| 937 | shcmd(n, fp); |
| 938 | if (ind >= 0) |
| 939 | putc('\n', fp); |
| 940 | break; |
| 941 | case NPIPE: |
| 942 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
| 943 | shcmd(lp->n, fp); |
| 944 | if (lp->next) |
| 945 | fputs(" | ", fp); |
| 946 | } |
| 947 | if (n->npipe.backgnd) |
| 948 | fputs(" &", fp); |
| 949 | if (ind >= 0) |
| 950 | putc('\n', fp); |
| 951 | break; |
| 952 | default: |
| 953 | fprintf(fp, "<node type %d>", n->type); |
| 954 | if (ind >= 0) |
| 955 | putc('\n', fp); |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | static void |
| 961 | showtree(union node *n) |
| 962 | { |
| 963 | trace_puts("showtree called\n"); |
| 964 | shtree(n, 1, NULL, stdout); |
| 965 | } |
| 966 | |
| 967 | #define TRACE(param) trace_printf param |
| 968 | #define TRACEV(param) trace_vprintf param |
| 969 | |
| 970 | #else |
| 971 | |
| 972 | #define TRACE(param) |
| 973 | #define TRACEV(param) |
| 974 | |
| 975 | #endif /* DEBUG */ |
| 976 | |
| 977 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 978 | /* ============ Parser data */ |
| 979 | |
| 980 | /* |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 981 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 982 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 983 | struct strlist { |
| 984 | struct strlist *next; |
| 985 | char *text; |
| 986 | }; |
| 987 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 988 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 989 | struct alias; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 990 | #endif |
| 991 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 992 | struct strpush { |
| 993 | struct strpush *prev; /* preceding string on stack */ |
| 994 | char *prevstring; |
| 995 | int prevnleft; |
| 996 | #if ENABLE_ASH_ALIAS |
| 997 | struct alias *ap; /* if push was associated with an alias */ |
| 998 | #endif |
| 999 | char *string; /* remember the string since it may change */ |
| 1000 | }; |
| 1001 | |
| 1002 | struct parsefile { |
| 1003 | struct parsefile *prev; /* preceding file on stack */ |
| 1004 | int linno; /* current line */ |
| 1005 | int fd; /* file descriptor (or -1 if string) */ |
| 1006 | int nleft; /* number of chars left in this line */ |
| 1007 | int lleft; /* number of chars left in this buffer */ |
| 1008 | char *nextc; /* next char in buffer */ |
| 1009 | char *buf; /* input buffer */ |
| 1010 | struct strpush *strpush; /* for pushing strings at this level */ |
| 1011 | struct strpush basestrpush; /* so pushing one is fast */ |
| 1012 | }; |
| 1013 | |
| 1014 | static struct parsefile basepf; /* top level input file */ |
| 1015 | static struct parsefile *parsefile = &basepf; /* current input file */ |
| 1016 | static int startlinno; /* line # where last token started */ |
| 1017 | static char *commandname; /* currently executing command */ |
| 1018 | static struct strlist *cmdenviron; /* environment for builtin command */ |
| 1019 | static int exitstatus; /* exit status of last command */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1020 | |
| 1021 | |
| 1022 | /* ============ Message printing */ |
| 1023 | |
| 1024 | static void |
| 1025 | ash_vmsg(const char *msg, va_list ap) |
| 1026 | { |
| 1027 | fprintf(stderr, "%s: ", arg0); |
| 1028 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1029 | if (strcmp(arg0, commandname)) |
| 1030 | fprintf(stderr, "%s: ", commandname); |
| 1031 | if (!iflag || parsefile->fd) |
| 1032 | fprintf(stderr, "line %d: ", startlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1033 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1034 | vfprintf(stderr, msg, ap); |
| 1035 | outcslow('\n', stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1036 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1037 | |
| 1038 | /* |
| 1039 | * Exverror is called to raise the error exception. If the second argument |
| 1040 | * is not NULL then error prints an error message using printf style |
| 1041 | * formatting. It then raises the error exception. |
| 1042 | */ |
| 1043 | static void ash_vmsg_and_raise(int, const char *, va_list) ATTRIBUTE_NORETURN; |
| 1044 | static void |
| 1045 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1046 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1047 | #if DEBUG |
| 1048 | if (msg) { |
| 1049 | TRACE(("ash_vmsg_and_raise(%d, \"", cond)); |
| 1050 | TRACEV((msg, ap)); |
| 1051 | TRACE(("\") pid=%d\n", getpid())); |
| 1052 | } else |
| 1053 | TRACE(("ash_vmsg_and_raise(%d, NULL) pid=%d\n", cond, getpid())); |
| 1054 | if (msg) |
| 1055 | #endif |
| 1056 | ash_vmsg(msg, ap); |
| 1057 | |
| 1058 | flush_stdout_stderr(); |
| 1059 | raise_exception(cond); |
| 1060 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1061 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1062 | |
| 1063 | static void ash_msg_and_raise_error(const char *, ...) ATTRIBUTE_NORETURN; |
| 1064 | static void |
| 1065 | ash_msg_and_raise_error(const char *msg, ...) |
| 1066 | { |
| 1067 | va_list ap; |
| 1068 | |
| 1069 | va_start(ap, msg); |
| 1070 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1071 | /* NOTREACHED */ |
| 1072 | va_end(ap); |
| 1073 | } |
| 1074 | |
| 1075 | static void ash_msg_and_raise(int, const char *, ...) ATTRIBUTE_NORETURN; |
| 1076 | static void |
| 1077 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1078 | { |
| 1079 | va_list ap; |
| 1080 | |
| 1081 | va_start(ap, msg); |
| 1082 | ash_vmsg_and_raise(cond, msg, ap); |
| 1083 | /* NOTREACHED */ |
| 1084 | va_end(ap); |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * error/warning routines for external builtins |
| 1089 | */ |
| 1090 | static void |
| 1091 | ash_msg(const char *fmt, ...) |
| 1092 | { |
| 1093 | va_list ap; |
| 1094 | |
| 1095 | va_start(ap, fmt); |
| 1096 | ash_vmsg(fmt, ap); |
| 1097 | va_end(ap); |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * Return a string describing an error. The returned string may be a |
| 1102 | * pointer to a static buffer that will be overwritten on the next call. |
| 1103 | * Action describes the operation that got the error. |
| 1104 | */ |
| 1105 | static const char * |
| 1106 | errmsg(int e, const char *em) |
| 1107 | { |
| 1108 | if (e == ENOENT || e == ENOTDIR) { |
| 1109 | return em; |
| 1110 | } |
| 1111 | return strerror(e); |
| 1112 | } |
| 1113 | |
| 1114 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1115 | /* ============ Memory allocation */ |
| 1116 | |
| 1117 | /* |
| 1118 | * It appears that grabstackstr() will barf with such alignments |
| 1119 | * because stalloc() will return a string allocated in a new stackblock. |
| 1120 | */ |
| 1121 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1122 | enum { |
| 1123 | /* Most machines require the value returned from malloc to be aligned |
| 1124 | * in some way. The following macro will get this right |
| 1125 | * on many machines. */ |
| 1126 | SHELL_SIZE = sizeof(union {int i; char *cp; double d; }) - 1, |
| 1127 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1128 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1129 | }; |
| 1130 | |
| 1131 | struct stack_block { |
| 1132 | struct stack_block *prev; |
| 1133 | char space[MINSIZE]; |
| 1134 | }; |
| 1135 | |
| 1136 | struct stackmark { |
| 1137 | struct stack_block *stackp; |
| 1138 | char *stacknxt; |
| 1139 | size_t stacknleft; |
| 1140 | struct stackmark *marknext; |
| 1141 | }; |
| 1142 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1143 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1144 | struct globals_memstack { |
| 1145 | struct stack_block *g_stackp; // = &stackbase; |
| 1146 | struct stackmark *markp; |
| 1147 | char *g_stacknxt; // = stackbase.space; |
| 1148 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1149 | size_t g_stacknleft; // = MINSIZE; |
| 1150 | int herefd; // = -1; |
| 1151 | struct stack_block stackbase; |
| 1152 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1153 | extern struct globals_memstack *const ash_ptr_to_globals_memstack; |
| 1154 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1155 | #define g_stackp (G_memstack.g_stackp ) |
| 1156 | #define markp (G_memstack.markp ) |
| 1157 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1158 | #define sstrend (G_memstack.sstrend ) |
| 1159 | #define g_stacknleft (G_memstack.g_stacknleft) |
| 1160 | #define herefd (G_memstack.herefd ) |
| 1161 | #define stackbase (G_memstack.stackbase ) |
| 1162 | #define INIT_G_memstack() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1163 | (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \ |
| 1164 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1165 | g_stackp = &stackbase; \ |
| 1166 | g_stacknxt = stackbase.space; \ |
| 1167 | g_stacknleft = MINSIZE; \ |
| 1168 | sstrend = stackbase.space + MINSIZE; \ |
| 1169 | herefd = -1; \ |
| 1170 | } while (0) |
| 1171 | |
| 1172 | #define stackblock() ((void *)g_stacknxt) |
| 1173 | #define stackblocksize() g_stacknleft |
| 1174 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1175 | |
| 1176 | static void * |
| 1177 | ckrealloc(void * p, size_t nbytes) |
| 1178 | { |
| 1179 | p = realloc(p, nbytes); |
| 1180 | if (!p) |
| 1181 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1182 | return p; |
| 1183 | } |
| 1184 | |
| 1185 | static void * |
| 1186 | ckmalloc(size_t nbytes) |
| 1187 | { |
| 1188 | return ckrealloc(NULL, nbytes); |
| 1189 | } |
| 1190 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1191 | static void * |
| 1192 | ckzalloc(size_t nbytes) |
| 1193 | { |
| 1194 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1195 | } |
| 1196 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1197 | /* |
| 1198 | * Make a copy of a string in safe storage. |
| 1199 | */ |
| 1200 | static char * |
| 1201 | ckstrdup(const char *s) |
| 1202 | { |
| 1203 | char *p = strdup(s); |
| 1204 | if (!p) |
| 1205 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1206 | return p; |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1211 | * to make this more efficient, and also to avoid all sorts of exception |
| 1212 | * handling code to handle interrupts in the middle of a parse. |
| 1213 | * |
| 1214 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1215 | * well. |
| 1216 | */ |
| 1217 | static void * |
| 1218 | stalloc(size_t nbytes) |
| 1219 | { |
| 1220 | char *p; |
| 1221 | size_t aligned; |
| 1222 | |
| 1223 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1224 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1225 | size_t len; |
| 1226 | size_t blocksize; |
| 1227 | struct stack_block *sp; |
| 1228 | |
| 1229 | blocksize = aligned; |
| 1230 | if (blocksize < MINSIZE) |
| 1231 | blocksize = MINSIZE; |
| 1232 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1233 | if (len < blocksize) |
| 1234 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1235 | INT_OFF; |
| 1236 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1237 | sp->prev = g_stackp; |
| 1238 | g_stacknxt = sp->space; |
| 1239 | g_stacknleft = blocksize; |
| 1240 | sstrend = g_stacknxt + blocksize; |
| 1241 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1242 | INT_ON; |
| 1243 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1244 | p = g_stacknxt; |
| 1245 | g_stacknxt += aligned; |
| 1246 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1247 | return p; |
| 1248 | } |
| 1249 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1250 | static void * |
| 1251 | stzalloc(size_t nbytes) |
| 1252 | { |
| 1253 | return memset(stalloc(nbytes), 0, nbytes); |
| 1254 | } |
| 1255 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1256 | static void |
| 1257 | stunalloc(void *p) |
| 1258 | { |
| 1259 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1260 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1261 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1262 | abort(); |
| 1263 | } |
| 1264 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1265 | g_stacknleft += g_stacknxt - (char *)p; |
| 1266 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1269 | /* |
| 1270 | * Like strdup but works with the ash stack. |
| 1271 | */ |
| 1272 | static char * |
| 1273 | ststrdup(const char *p) |
| 1274 | { |
| 1275 | size_t len = strlen(p) + 1; |
| 1276 | return memcpy(stalloc(len), p, len); |
| 1277 | } |
| 1278 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1279 | static void |
| 1280 | setstackmark(struct stackmark *mark) |
| 1281 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1282 | mark->stackp = g_stackp; |
| 1283 | mark->stacknxt = g_stacknxt; |
| 1284 | mark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1285 | mark->marknext = markp; |
| 1286 | markp = mark; |
| 1287 | } |
| 1288 | |
| 1289 | static void |
| 1290 | popstackmark(struct stackmark *mark) |
| 1291 | { |
| 1292 | struct stack_block *sp; |
| 1293 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1294 | if (!mark->stackp) |
| 1295 | return; |
| 1296 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1297 | INT_OFF; |
| 1298 | markp = mark->marknext; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1299 | while (g_stackp != mark->stackp) { |
| 1300 | sp = g_stackp; |
| 1301 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1302 | free(sp); |
| 1303 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1304 | g_stacknxt = mark->stacknxt; |
| 1305 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1306 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1307 | INT_ON; |
| 1308 | } |
| 1309 | |
| 1310 | /* |
| 1311 | * When the parser reads in a string, it wants to stick the string on the |
| 1312 | * stack and only adjust the stack pointer when it knows how big the |
| 1313 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1314 | * of space on top of the stack and stackblocklen returns the length of |
| 1315 | * this block. Growstackblock will grow this space by at least one byte, |
| 1316 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1317 | * part of the block that has been used. |
| 1318 | */ |
| 1319 | static void |
| 1320 | growstackblock(void) |
| 1321 | { |
| 1322 | size_t newlen; |
| 1323 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1324 | newlen = g_stacknleft * 2; |
| 1325 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1326 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1327 | if (newlen < 128) |
| 1328 | newlen += 128; |
| 1329 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1330 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1331 | struct stack_block *oldstackp; |
| 1332 | struct stackmark *xmark; |
| 1333 | struct stack_block *sp; |
| 1334 | struct stack_block *prevstackp; |
| 1335 | size_t grosslen; |
| 1336 | |
| 1337 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1338 | oldstackp = g_stackp; |
| 1339 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1340 | prevstackp = sp->prev; |
| 1341 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1342 | sp = ckrealloc(sp, grosslen); |
| 1343 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1344 | g_stackp = sp; |
| 1345 | g_stacknxt = sp->space; |
| 1346 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1347 | sstrend = sp->space + newlen; |
| 1348 | |
| 1349 | /* |
| 1350 | * Stack marks pointing to the start of the old block |
| 1351 | * must be relocated to point to the new block |
| 1352 | */ |
| 1353 | xmark = markp; |
| 1354 | while (xmark != NULL && xmark->stackp == oldstackp) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1355 | xmark->stackp = g_stackp; |
| 1356 | xmark->stacknxt = g_stacknxt; |
| 1357 | xmark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1358 | xmark = xmark->marknext; |
| 1359 | } |
| 1360 | INT_ON; |
| 1361 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1362 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1363 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1364 | char *p = stalloc(newlen); |
| 1365 | |
| 1366 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1367 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1368 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | static void |
| 1373 | grabstackblock(size_t len) |
| 1374 | { |
| 1375 | len = SHELL_ALIGN(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1376 | g_stacknxt += len; |
| 1377 | g_stacknleft -= len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | /* |
| 1381 | * The following routines are somewhat easier to use than the above. |
| 1382 | * The user declares a variable of type STACKSTR, which may be declared |
| 1383 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1384 | * the user uses the macro STPUTC to add characters to the string. In |
| 1385 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1386 | * grown as necessary. When the user is done, she can just leave the |
| 1387 | * string there and refer to it using stackblock(). Or she can allocate |
| 1388 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1389 | * someone else to use the stack temporarily and then continue to grow |
| 1390 | * the string, the user should use grabstack to allocate the space, and |
| 1391 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1392 | * |
| 1393 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1394 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1395 | * is space for at least one character. |
| 1396 | */ |
| 1397 | static void * |
| 1398 | growstackstr(void) |
| 1399 | { |
| 1400 | size_t len = stackblocksize(); |
| 1401 | if (herefd >= 0 && len >= 1024) { |
| 1402 | full_write(herefd, stackblock(), len); |
| 1403 | return stackblock(); |
| 1404 | } |
| 1405 | growstackblock(); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1406 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | /* |
| 1410 | * Called from CHECKSTRSPACE. |
| 1411 | */ |
| 1412 | static char * |
| 1413 | makestrspace(size_t newlen, char *p) |
| 1414 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1415 | size_t len = p - g_stacknxt; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1416 | size_t size = stackblocksize(); |
| 1417 | |
| 1418 | for (;;) { |
| 1419 | size_t nleft; |
| 1420 | |
| 1421 | size = stackblocksize(); |
| 1422 | nleft = size - len; |
| 1423 | if (nleft >= newlen) |
| 1424 | break; |
| 1425 | growstackblock(); |
| 1426 | } |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1427 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | static char * |
| 1431 | stack_nputstr(const char *s, size_t n, char *p) |
| 1432 | { |
| 1433 | p = makestrspace(n, p); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1434 | p = (char *)memcpy(p, s, n) + n; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1435 | return p; |
| 1436 | } |
| 1437 | |
| 1438 | static char * |
| 1439 | stack_putstr(const char *s, char *p) |
| 1440 | { |
| 1441 | return stack_nputstr(s, strlen(s), p); |
| 1442 | } |
| 1443 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1444 | static char * |
| 1445 | _STPUTC(int c, char *p) |
| 1446 | { |
| 1447 | if (p == sstrend) |
| 1448 | p = growstackstr(); |
| 1449 | *p++ = c; |
| 1450 | return p; |
| 1451 | } |
| 1452 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1453 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1454 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1455 | #define CHECKSTRSPACE(n, p) \ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1456 | do { \ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1457 | char *q = (p); \ |
| 1458 | size_t l = (n); \ |
| 1459 | size_t m = sstrend - q; \ |
| 1460 | if (l > m) \ |
| 1461 | (p) = makestrspace(l, q); \ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1462 | } while (0) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1463 | #define USTPUTC(c, p) (*p++ = (c)) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1464 | #define STACKSTRNUL(p) \ |
| 1465 | do { \ |
| 1466 | if ((p) == sstrend) \ |
| 1467 | p = growstackstr(); \ |
| 1468 | *p = '\0'; \ |
| 1469 | } while (0) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1470 | #define STUNPUTC(p) (--p) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1471 | #define STTOPC(p) (p[-1]) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1472 | #define STADJUST(amount, p) (p += (amount)) |
| 1473 | |
| 1474 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
| 1475 | #define ungrabstackstr(s, p) stunalloc((s)) |
| 1476 | #define stackstrend() ((void *)sstrend) |
| 1477 | |
| 1478 | |
| 1479 | /* ============ String helpers */ |
| 1480 | |
| 1481 | /* |
| 1482 | * prefix -- see if pfx is a prefix of string. |
| 1483 | */ |
| 1484 | static char * |
| 1485 | prefix(const char *string, const char *pfx) |
| 1486 | { |
| 1487 | while (*pfx) { |
| 1488 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1489 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1490 | } |
| 1491 | return (char *) string; |
| 1492 | } |
| 1493 | |
| 1494 | /* |
| 1495 | * Check for a valid number. This should be elsewhere. |
| 1496 | */ |
| 1497 | static int |
| 1498 | is_number(const char *p) |
| 1499 | { |
| 1500 | do { |
| 1501 | if (!isdigit(*p)) |
| 1502 | return 0; |
| 1503 | } while (*++p != '\0'); |
| 1504 | return 1; |
| 1505 | } |
| 1506 | |
| 1507 | /* |
| 1508 | * Convert a string of digits to an integer, printing an error message on |
| 1509 | * failure. |
| 1510 | */ |
| 1511 | static int |
| 1512 | number(const char *s) |
| 1513 | { |
| 1514 | if (!is_number(s)) |
| 1515 | ash_msg_and_raise_error(illnum, s); |
| 1516 | return atoi(s); |
| 1517 | } |
| 1518 | |
| 1519 | /* |
| 1520 | * Produce a possibly single quoted string suitable as input to the shell. |
| 1521 | * The return string is allocated on the stack. |
| 1522 | */ |
| 1523 | static char * |
| 1524 | single_quote(const char *s) |
| 1525 | { |
| 1526 | char *p; |
| 1527 | |
| 1528 | STARTSTACKSTR(p); |
| 1529 | |
| 1530 | do { |
| 1531 | char *q; |
| 1532 | size_t len; |
| 1533 | |
| 1534 | len = strchrnul(s, '\'') - s; |
| 1535 | |
| 1536 | q = p = makestrspace(len + 3, p); |
| 1537 | |
| 1538 | *q++ = '\''; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1539 | q = (char *)memcpy(q, s, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1540 | *q++ = '\''; |
| 1541 | s += len; |
| 1542 | |
| 1543 | STADJUST(q - p, p); |
| 1544 | |
| 1545 | len = strspn(s, "'"); |
| 1546 | if (!len) |
| 1547 | break; |
| 1548 | |
| 1549 | q = p = makestrspace(len + 3, p); |
| 1550 | |
| 1551 | *q++ = '"'; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1552 | q = (char *)memcpy(q, s, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1553 | *q++ = '"'; |
| 1554 | s += len; |
| 1555 | |
| 1556 | STADJUST(q - p, p); |
| 1557 | } while (*s); |
| 1558 | |
| 1559 | USTPUTC(0, p); |
| 1560 | |
| 1561 | return stackblock(); |
| 1562 | } |
| 1563 | |
| 1564 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1565 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1566 | |
| 1567 | static char **argptr; /* argument list for builtin commands */ |
| 1568 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1569 | static char *optptr; /* used by nextopt */ |
| 1570 | |
| 1571 | /* |
| 1572 | * XXX - should get rid of. have all builtins use getopt(3). the |
| 1573 | * library getopt must have the BSD extension static variable "optreset" |
| 1574 | * otherwise it can't be used within the shell safely. |
| 1575 | * |
| 1576 | * Standard option processing (a la getopt) for builtin routines. The |
| 1577 | * only argument that is passed to nextopt is the option string; the |
| 1578 | * other arguments are unnecessary. It return the character, or '\0' on |
| 1579 | * end of input. |
| 1580 | */ |
| 1581 | static int |
| 1582 | nextopt(const char *optstring) |
| 1583 | { |
| 1584 | char *p; |
| 1585 | const char *q; |
| 1586 | char c; |
| 1587 | |
| 1588 | p = optptr; |
| 1589 | if (p == NULL || *p == '\0') { |
| 1590 | p = *argptr; |
| 1591 | if (p == NULL || *p != '-' || *++p == '\0') |
| 1592 | return '\0'; |
| 1593 | argptr++; |
| 1594 | if (LONE_DASH(p)) /* check for "--" */ |
| 1595 | return '\0'; |
| 1596 | } |
| 1597 | c = *p++; |
| 1598 | for (q = optstring; *q != c; ) { |
| 1599 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1600 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1601 | if (*++q == ':') |
| 1602 | q++; |
| 1603 | } |
| 1604 | if (*++q == ':') { |
| 1605 | if (*p == '\0' && (p = *argptr++) == NULL) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1606 | ash_msg_and_raise_error("no arg for -%c option", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1607 | optionarg = p; |
| 1608 | p = NULL; |
| 1609 | } |
| 1610 | optptr = p; |
| 1611 | return c; |
| 1612 | } |
| 1613 | |
| 1614 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1615 | /* ============ Math support definitions */ |
| 1616 | |
| 1617 | #if ENABLE_ASH_MATH_SUPPORT_64 |
| 1618 | typedef int64_t arith_t; |
| 1619 | #define arith_t_type long long |
| 1620 | #else |
| 1621 | typedef long arith_t; |
| 1622 | #define arith_t_type long |
| 1623 | #endif |
| 1624 | |
| 1625 | #if ENABLE_ASH_MATH_SUPPORT |
| 1626 | static arith_t dash_arith(const char *); |
| 1627 | static arith_t arith(const char *expr, int *perrcode); |
| 1628 | #endif |
| 1629 | |
| 1630 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 1631 | static unsigned long rseed; |
| 1632 | #ifndef DYNAMIC_VAR |
| 1633 | #define DYNAMIC_VAR |
| 1634 | #endif |
| 1635 | #endif |
| 1636 | |
| 1637 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1638 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1639 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1640 | /* |
| 1641 | * The parsefile structure pointed to by the global variable parsefile |
| 1642 | * contains information about the current file being read. |
| 1643 | */ |
| 1644 | struct redirtab { |
| 1645 | struct redirtab *next; |
| 1646 | int renamed[10]; |
| 1647 | int nullredirs; |
| 1648 | }; |
| 1649 | |
| 1650 | struct shparam { |
| 1651 | int nparam; /* # of positional parameters (without $0) */ |
| 1652 | #if ENABLE_ASH_GETOPTS |
| 1653 | int optind; /* next parameter to be processed by getopts */ |
| 1654 | int optoff; /* used by getopts */ |
| 1655 | #endif |
| 1656 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 1657 | char **p; /* parameter list */ |
| 1658 | }; |
| 1659 | |
| 1660 | /* |
| 1661 | * Free the list of positional parameters. |
| 1662 | */ |
| 1663 | static void |
| 1664 | freeparam(volatile struct shparam *param) |
| 1665 | { |
| 1666 | char **ap; |
| 1667 | |
| 1668 | if (param->malloced) { |
| 1669 | for (ap = param->p; *ap; ap++) |
| 1670 | free(*ap); |
| 1671 | free(param->p); |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | #if ENABLE_ASH_GETOPTS |
| 1676 | static void getoptsreset(const char *value); |
| 1677 | #endif |
| 1678 | |
| 1679 | struct var { |
| 1680 | struct var *next; /* next entry in hash list */ |
| 1681 | int flags; /* flags are defined above */ |
| 1682 | const char *text; /* name=value */ |
| 1683 | void (*func)(const char *); /* function to be called when */ |
| 1684 | /* the variable gets set/unset */ |
| 1685 | }; |
| 1686 | |
| 1687 | struct localvar { |
| 1688 | struct localvar *next; /* next local variable in list */ |
| 1689 | struct var *vp; /* the variable that was made local */ |
| 1690 | int flags; /* saved flags */ |
| 1691 | const char *text; /* saved text */ |
| 1692 | }; |
| 1693 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1694 | /* flags */ |
| 1695 | #define VEXPORT 0x01 /* variable is exported */ |
| 1696 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 1697 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 1698 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 1699 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 1700 | #define VUNSET 0x20 /* the variable is not set */ |
| 1701 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 1702 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 1703 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
| 1704 | #ifdef DYNAMIC_VAR |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1705 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 1706 | #else |
| 1707 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1708 | #endif |
| 1709 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1710 | #ifdef IFS_BROKEN |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1711 | static const char defifsvar[] ALIGN1 = "IFS= \t\n"; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1712 | #define defifs (defifsvar + 4) |
| 1713 | #else |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1714 | static const char defifs[] ALIGN1 = " \t\n"; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1715 | #endif |
| 1716 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1717 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1718 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1719 | #if ENABLE_LOCALE_SUPPORT |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1720 | static void |
| 1721 | change_lc_all(const char *value) |
| 1722 | { |
| 1723 | if (value && *value != '\0') |
| 1724 | setlocale(LC_ALL, value); |
| 1725 | } |
| 1726 | static void |
| 1727 | change_lc_ctype(const char *value) |
| 1728 | { |
| 1729 | if (value && *value != '\0') |
| 1730 | setlocale(LC_CTYPE, value); |
| 1731 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1732 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1733 | #if ENABLE_ASH_MAIL |
| 1734 | static void chkmail(void); |
| 1735 | static void changemail(const char *); |
| 1736 | #endif |
| 1737 | static void changepath(const char *); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1738 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 1739 | static void change_random(const char *); |
| 1740 | #endif |
| 1741 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1742 | static const struct { |
| 1743 | int flags; |
| 1744 | const char *text; |
| 1745 | void (*func)(const char *); |
| 1746 | } varinit_data[] = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1747 | #ifdef IFS_BROKEN |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1748 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1749 | #else |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1750 | { VSTRFIXED|VTEXTFIXED|VUNSET, "IFS\0" , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1751 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1752 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1753 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL\0" , changemail }, |
| 1754 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH\0", changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1755 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1756 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 1757 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 1758 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 1759 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1760 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1761 | { VSTRFIXED|VTEXTFIXED , "OPTIND=1" , getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1762 | #endif |
| 1763 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1764 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM\0", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1765 | #endif |
| 1766 | #if ENABLE_LOCALE_SUPPORT |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1767 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL\0" , change_lc_all }, |
| 1768 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE\0", change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1769 | #endif |
| 1770 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1771 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE\0", NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1772 | #endif |
| 1773 | }; |
| 1774 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1775 | |
| 1776 | struct globals_var { |
| 1777 | struct shparam shellparam; /* $@ current positional parameters */ |
| 1778 | struct redirtab *redirlist; |
| 1779 | int g_nullredirs; |
| 1780 | int preverrout_fd; /* save fd2 before print debug if xflag is set. */ |
| 1781 | struct var *vartab[VTABSIZE]; |
| 1782 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
| 1783 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1784 | extern struct globals_var *const ash_ptr_to_globals_var; |
| 1785 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1786 | #define shellparam (G_var.shellparam ) |
| 1787 | #define redirlist (G_var.redirlist ) |
| 1788 | #define g_nullredirs (G_var.g_nullredirs ) |
| 1789 | #define preverrout_fd (G_var.preverrout_fd) |
| 1790 | #define vartab (G_var.vartab ) |
| 1791 | #define varinit (G_var.varinit ) |
| 1792 | #define INIT_G_var() do { \ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1793 | unsigned i; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1794 | (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \ |
| 1795 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1796 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
| 1797 | varinit[i].flags = varinit_data[i].flags; \ |
| 1798 | varinit[i].text = varinit_data[i].text; \ |
| 1799 | varinit[i].func = varinit_data[i].func; \ |
| 1800 | } \ |
| 1801 | } while (0) |
| 1802 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1803 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1804 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1805 | # define vmail (&vifs)[1] |
| 1806 | # define vmpath (&vmail)[1] |
| 1807 | # define vpath (&vmpath)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1808 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1809 | # define vpath (&vifs)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1810 | #endif |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1811 | #define vps1 (&vpath)[1] |
| 1812 | #define vps2 (&vps1)[1] |
| 1813 | #define vps4 (&vps2)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1814 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1815 | # define voptind (&vps4)[1] |
| 1816 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1817 | # define vrandom (&voptind)[1] |
| 1818 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1819 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1820 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1821 | # define vrandom (&vps4)[1] |
| 1822 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1823 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1824 | |
| 1825 | /* |
| 1826 | * The following macros access the values of the above variables. |
| 1827 | * They have to skip over the name. They return the null string |
| 1828 | * for unset variables. |
| 1829 | */ |
| 1830 | #define ifsval() (vifs.text + 4) |
| 1831 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1832 | #if ENABLE_ASH_MAIL |
| 1833 | # define mailval() (vmail.text + 5) |
| 1834 | # define mpathval() (vmpath.text + 9) |
| 1835 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1836 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1837 | #define pathval() (vpath.text + 5) |
| 1838 | #define ps1val() (vps1.text + 4) |
| 1839 | #define ps2val() (vps2.text + 4) |
| 1840 | #define ps4val() (vps4.text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1841 | #if ENABLE_ASH_GETOPTS |
| 1842 | # define optindval() (voptind.text + 7) |
| 1843 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1844 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1845 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1846 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1847 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1848 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1849 | #if ENABLE_ASH_GETOPTS |
| 1850 | static void |
| 1851 | getoptsreset(const char *value) |
| 1852 | { |
| 1853 | shellparam.optind = number(value); |
| 1854 | shellparam.optoff = -1; |
| 1855 | } |
| 1856 | #endif |
| 1857 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1858 | /* |
| 1859 | * Return of a legal variable name (a letter or underscore followed by zero or |
| 1860 | * more letters, underscores, and digits). |
| 1861 | */ |
| 1862 | static char * |
| 1863 | endofname(const char *name) |
| 1864 | { |
| 1865 | char *p; |
| 1866 | |
| 1867 | p = (char *) name; |
| 1868 | if (!is_name(*p)) |
| 1869 | return p; |
| 1870 | while (*++p) { |
| 1871 | if (!is_in_name(*p)) |
| 1872 | break; |
| 1873 | } |
| 1874 | return p; |
| 1875 | } |
| 1876 | |
| 1877 | /* |
| 1878 | * Compares two strings up to the first = or '\0'. The first |
| 1879 | * string must be terminated by '='; the second may be terminated by |
| 1880 | * either '=' or '\0'. |
| 1881 | */ |
| 1882 | static int |
| 1883 | varcmp(const char *p, const char *q) |
| 1884 | { |
| 1885 | int c, d; |
| 1886 | |
| 1887 | while ((c = *p) == (d = *q)) { |
| 1888 | if (!c || c == '=') |
| 1889 | goto out; |
| 1890 | p++; |
| 1891 | q++; |
| 1892 | } |
| 1893 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 1894 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1895 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 1896 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1897 | out: |
| 1898 | return c - d; |
| 1899 | } |
| 1900 | |
| 1901 | static int |
| 1902 | varequal(const char *a, const char *b) |
| 1903 | { |
| 1904 | return !varcmp(a, b); |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Find the appropriate entry in the hash table from the name. |
| 1909 | */ |
| 1910 | static struct var ** |
| 1911 | hashvar(const char *p) |
| 1912 | { |
| 1913 | unsigned hashval; |
| 1914 | |
| 1915 | hashval = ((unsigned char) *p) << 4; |
| 1916 | while (*p && *p != '=') |
| 1917 | hashval += (unsigned char) *p++; |
| 1918 | return &vartab[hashval % VTABSIZE]; |
| 1919 | } |
| 1920 | |
| 1921 | static int |
| 1922 | vpcmp(const void *a, const void *b) |
| 1923 | { |
| 1924 | return varcmp(*(const char **)a, *(const char **)b); |
| 1925 | } |
| 1926 | |
| 1927 | /* |
| 1928 | * This routine initializes the builtin variables. |
| 1929 | */ |
| 1930 | static void |
| 1931 | initvar(void) |
| 1932 | { |
| 1933 | struct var *vp; |
| 1934 | struct var *end; |
| 1935 | struct var **vpp; |
| 1936 | |
| 1937 | /* |
| 1938 | * PS1 depends on uid |
| 1939 | */ |
| 1940 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 1941 | vps1.text = "PS1=\\w \\$ "; |
| 1942 | #else |
| 1943 | if (!geteuid()) |
| 1944 | vps1.text = "PS1=# "; |
| 1945 | #endif |
| 1946 | vp = varinit; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 1947 | end = vp + ARRAY_SIZE(varinit); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1948 | do { |
| 1949 | vpp = hashvar(vp->text); |
| 1950 | vp->next = *vpp; |
| 1951 | *vpp = vp; |
| 1952 | } while (++vp < end); |
| 1953 | } |
| 1954 | |
| 1955 | static struct var ** |
| 1956 | findvar(struct var **vpp, const char *name) |
| 1957 | { |
| 1958 | for (; *vpp; vpp = &(*vpp)->next) { |
| 1959 | if (varequal((*vpp)->text, name)) { |
| 1960 | break; |
| 1961 | } |
| 1962 | } |
| 1963 | return vpp; |
| 1964 | } |
| 1965 | |
| 1966 | /* |
| 1967 | * Find the value of a variable. Returns NULL if not set. |
| 1968 | */ |
| 1969 | static char * |
| 1970 | lookupvar(const char *name) |
| 1971 | { |
| 1972 | struct var *v; |
| 1973 | |
| 1974 | v = *findvar(hashvar(name), name); |
| 1975 | if (v) { |
| 1976 | #ifdef DYNAMIC_VAR |
| 1977 | /* |
| 1978 | * Dynamic variables are implemented roughly the same way they are |
| 1979 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 1980 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 1981 | * lookup will no longer happen at that point. -- PFM. |
| 1982 | */ |
| 1983 | if ((v->flags & VDYNAMIC)) |
| 1984 | (*v->func)(NULL); |
| 1985 | #endif |
| 1986 | if (!(v->flags & VUNSET)) |
| 1987 | return strchrnul(v->text, '=') + 1; |
| 1988 | } |
| 1989 | return NULL; |
| 1990 | } |
| 1991 | |
| 1992 | /* |
| 1993 | * Search the environment of a builtin command. |
| 1994 | */ |
| 1995 | static char * |
| 1996 | bltinlookup(const char *name) |
| 1997 | { |
| 1998 | struct strlist *sp; |
| 1999 | |
| 2000 | for (sp = cmdenviron; sp; sp = sp->next) { |
| 2001 | if (varequal(sp->text, name)) |
| 2002 | return strchrnul(sp->text, '=') + 1; |
| 2003 | } |
| 2004 | return lookupvar(name); |
| 2005 | } |
| 2006 | |
| 2007 | /* |
| 2008 | * Same as setvar except that the variable and value are passed in |
| 2009 | * the first argument as name=value. Since the first argument will |
| 2010 | * be actually stored in the table, it should not be a string that |
| 2011 | * will go away. |
| 2012 | * Called with interrupts off. |
| 2013 | */ |
| 2014 | static void |
| 2015 | setvareq(char *s, int flags) |
| 2016 | { |
| 2017 | struct var *vp, **vpp; |
| 2018 | |
| 2019 | vpp = hashvar(s); |
| 2020 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
| 2021 | vp = *findvar(vpp, s); |
| 2022 | if (vp) { |
| 2023 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 2024 | const char *n; |
| 2025 | |
| 2026 | if (flags & VNOSAVE) |
| 2027 | free(s); |
| 2028 | n = vp->text; |
| 2029 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2030 | } |
| 2031 | |
| 2032 | if (flags & VNOSET) |
| 2033 | return; |
| 2034 | |
| 2035 | if (vp->func && (flags & VNOFUNC) == 0) |
| 2036 | (*vp->func)(strchrnul(s, '=') + 1); |
| 2037 | |
| 2038 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 2039 | free((char*)vp->text); |
| 2040 | |
| 2041 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
| 2042 | } else { |
| 2043 | if (flags & VNOSET) |
| 2044 | return; |
| 2045 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2046 | vp = ckzalloc(sizeof(*vp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2047 | vp->next = *vpp; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2048 | /*vp->func = NULL; - ckzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2049 | *vpp = vp; |
| 2050 | } |
| 2051 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 2052 | s = ckstrdup(s); |
| 2053 | vp->text = s; |
| 2054 | vp->flags = flags; |
| 2055 | } |
| 2056 | |
| 2057 | /* |
| 2058 | * Set the value of a variable. The flags argument is ored with the |
| 2059 | * flags of the variable. If val is NULL, the variable is unset. |
| 2060 | */ |
| 2061 | static void |
| 2062 | setvar(const char *name, const char *val, int flags) |
| 2063 | { |
| 2064 | char *p, *q; |
| 2065 | size_t namelen; |
| 2066 | char *nameeq; |
| 2067 | size_t vallen; |
| 2068 | |
| 2069 | q = endofname(name); |
| 2070 | p = strchrnul(q, '='); |
| 2071 | namelen = p - name; |
| 2072 | if (!namelen || p != q) |
| 2073 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 2074 | vallen = 0; |
| 2075 | if (val == NULL) { |
| 2076 | flags |= VUNSET; |
| 2077 | } else { |
| 2078 | vallen = strlen(val); |
| 2079 | } |
| 2080 | INT_OFF; |
| 2081 | nameeq = ckmalloc(namelen + vallen + 2); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2082 | p = (char *)memcpy(nameeq, name, namelen) + namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2083 | if (val) { |
| 2084 | *p++ = '='; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2085 | p = (char *)memcpy(p, val, vallen) + vallen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2086 | } |
| 2087 | *p = '\0'; |
| 2088 | setvareq(nameeq, flags | VNOSAVE); |
| 2089 | INT_ON; |
| 2090 | } |
| 2091 | |
| 2092 | #if ENABLE_ASH_GETOPTS |
| 2093 | /* |
| 2094 | * Safe version of setvar, returns 1 on success 0 on failure. |
| 2095 | */ |
| 2096 | static int |
| 2097 | setvarsafe(const char *name, const char *val, int flags) |
| 2098 | { |
| 2099 | int err; |
| 2100 | volatile int saveint; |
| 2101 | struct jmploc *volatile savehandler = exception_handler; |
| 2102 | struct jmploc jmploc; |
| 2103 | |
| 2104 | SAVE_INT(saveint); |
| 2105 | if (setjmp(jmploc.loc)) |
| 2106 | err = 1; |
| 2107 | else { |
| 2108 | exception_handler = &jmploc; |
| 2109 | setvar(name, val, flags); |
| 2110 | err = 0; |
| 2111 | } |
| 2112 | exception_handler = savehandler; |
| 2113 | RESTORE_INT(saveint); |
| 2114 | return err; |
| 2115 | } |
| 2116 | #endif |
| 2117 | |
| 2118 | /* |
| 2119 | * Unset the specified variable. |
| 2120 | */ |
| 2121 | static int |
| 2122 | unsetvar(const char *s) |
| 2123 | { |
| 2124 | struct var **vpp; |
| 2125 | struct var *vp; |
| 2126 | int retval; |
| 2127 | |
| 2128 | vpp = findvar(hashvar(s), s); |
| 2129 | vp = *vpp; |
| 2130 | retval = 2; |
| 2131 | if (vp) { |
| 2132 | int flags = vp->flags; |
| 2133 | |
| 2134 | retval = 1; |
| 2135 | if (flags & VREADONLY) |
| 2136 | goto out; |
| 2137 | #ifdef DYNAMIC_VAR |
| 2138 | vp->flags &= ~VDYNAMIC; |
| 2139 | #endif |
| 2140 | if (flags & VUNSET) |
| 2141 | goto ok; |
| 2142 | if ((flags & VSTRFIXED) == 0) { |
| 2143 | INT_OFF; |
| 2144 | if ((flags & (VTEXTFIXED|VSTACK)) == 0) |
| 2145 | free((char*)vp->text); |
| 2146 | *vpp = vp->next; |
| 2147 | free(vp); |
| 2148 | INT_ON; |
| 2149 | } else { |
| 2150 | setvar(s, 0, 0); |
| 2151 | vp->flags &= ~VEXPORT; |
| 2152 | } |
| 2153 | ok: |
| 2154 | retval = 0; |
| 2155 | } |
| 2156 | out: |
| 2157 | return retval; |
| 2158 | } |
| 2159 | |
| 2160 | /* |
| 2161 | * Process a linked list of variable assignments. |
| 2162 | */ |
| 2163 | static void |
| 2164 | listsetvar(struct strlist *list_set_var, int flags) |
| 2165 | { |
| 2166 | struct strlist *lp = list_set_var; |
| 2167 | |
| 2168 | if (!lp) |
| 2169 | return; |
| 2170 | INT_OFF; |
| 2171 | do { |
| 2172 | setvareq(lp->text, flags); |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2173 | lp = lp->next; |
| 2174 | } while (lp); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2175 | INT_ON; |
| 2176 | } |
| 2177 | |
| 2178 | /* |
| 2179 | * Generate a list of variables satisfying the given conditions. |
| 2180 | */ |
| 2181 | static char ** |
| 2182 | listvars(int on, int off, char ***end) |
| 2183 | { |
| 2184 | struct var **vpp; |
| 2185 | struct var *vp; |
| 2186 | char **ep; |
| 2187 | int mask; |
| 2188 | |
| 2189 | STARTSTACKSTR(ep); |
| 2190 | vpp = vartab; |
| 2191 | mask = on | off; |
| 2192 | do { |
| 2193 | for (vp = *vpp; vp; vp = vp->next) { |
| 2194 | if ((vp->flags & mask) == on) { |
| 2195 | if (ep == stackstrend()) |
| 2196 | ep = growstackstr(); |
| 2197 | *ep++ = (char *) vp->text; |
| 2198 | } |
| 2199 | } |
| 2200 | } while (++vpp < vartab + VTABSIZE); |
| 2201 | if (ep == stackstrend()) |
| 2202 | ep = growstackstr(); |
| 2203 | if (end) |
| 2204 | *end = ep; |
| 2205 | *ep++ = NULL; |
| 2206 | return grabstackstr(ep); |
| 2207 | } |
| 2208 | |
| 2209 | |
| 2210 | /* ============ Path search helper |
| 2211 | * |
| 2212 | * The variable path (passed by reference) should be set to the start |
| 2213 | * of the path before the first call; padvance will update |
| 2214 | * this value as it proceeds. Successive calls to padvance will return |
| 2215 | * the possible path expansions in sequence. If an option (indicated by |
| 2216 | * a percent sign) appears in the path entry then the global variable |
| 2217 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 2218 | * NULL. |
| 2219 | */ |
| 2220 | static const char *pathopt; /* set by padvance */ |
| 2221 | |
| 2222 | static char * |
| 2223 | padvance(const char **path, const char *name) |
| 2224 | { |
| 2225 | const char *p; |
| 2226 | char *q; |
| 2227 | const char *start; |
| 2228 | size_t len; |
| 2229 | |
| 2230 | if (*path == NULL) |
| 2231 | return NULL; |
| 2232 | start = *path; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2233 | for (p = start; *p && *p != ':' && *p != '%'; p++) |
| 2234 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2235 | len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ |
| 2236 | while (stackblocksize() < len) |
| 2237 | growstackblock(); |
| 2238 | q = stackblock(); |
| 2239 | if (p != start) { |
| 2240 | memcpy(q, start, p - start); |
| 2241 | q += p - start; |
| 2242 | *q++ = '/'; |
| 2243 | } |
| 2244 | strcpy(q, name); |
| 2245 | pathopt = NULL; |
| 2246 | if (*p == '%') { |
| 2247 | pathopt = ++p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2248 | while (*p && *p != ':') |
| 2249 | p++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2250 | } |
| 2251 | if (*p == ':') |
| 2252 | *path = p + 1; |
| 2253 | else |
| 2254 | *path = NULL; |
| 2255 | return stalloc(len); |
| 2256 | } |
| 2257 | |
| 2258 | |
| 2259 | /* ============ Prompt */ |
| 2260 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2261 | static smallint doprompt; /* if set, prompt the user */ |
| 2262 | static smallint needprompt; /* true if interactive and at start of line */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2263 | |
| 2264 | #if ENABLE_FEATURE_EDITING |
| 2265 | static line_input_t *line_input_state; |
| 2266 | static const char *cmdedit_prompt; |
| 2267 | static void |
| 2268 | putprompt(const char *s) |
| 2269 | { |
| 2270 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 2271 | free((char*)cmdedit_prompt); |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 2272 | cmdedit_prompt = ckstrdup(s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2273 | return; |
| 2274 | } |
| 2275 | cmdedit_prompt = s; |
| 2276 | } |
| 2277 | #else |
| 2278 | static void |
| 2279 | putprompt(const char *s) |
| 2280 | { |
| 2281 | out2str(s); |
| 2282 | } |
| 2283 | #endif |
| 2284 | |
| 2285 | #if ENABLE_ASH_EXPAND_PRMT |
| 2286 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
| 2287 | static const char *expandstr(const char *ps); |
| 2288 | #else |
| 2289 | #define expandstr(s) s |
| 2290 | #endif |
| 2291 | |
| 2292 | static void |
| 2293 | setprompt(int whichprompt) |
| 2294 | { |
| 2295 | const char *prompt; |
| 2296 | #if ENABLE_ASH_EXPAND_PRMT |
| 2297 | struct stackmark smark; |
| 2298 | #endif |
| 2299 | |
| 2300 | needprompt = 0; |
| 2301 | |
| 2302 | switch (whichprompt) { |
| 2303 | case 1: |
| 2304 | prompt = ps1val(); |
| 2305 | break; |
| 2306 | case 2: |
| 2307 | prompt = ps2val(); |
| 2308 | break; |
| 2309 | default: /* 0 */ |
| 2310 | prompt = nullstr; |
| 2311 | } |
| 2312 | #if ENABLE_ASH_EXPAND_PRMT |
| 2313 | setstackmark(&smark); |
| 2314 | stalloc(stackblocksize()); |
| 2315 | #endif |
| 2316 | putprompt(expandstr(prompt)); |
| 2317 | #if ENABLE_ASH_EXPAND_PRMT |
| 2318 | popstackmark(&smark); |
| 2319 | #endif |
| 2320 | } |
| 2321 | |
| 2322 | |
| 2323 | /* ============ The cd and pwd commands */ |
| 2324 | |
| 2325 | #define CD_PHYSICAL 1 |
| 2326 | #define CD_PRINT 2 |
| 2327 | |
| 2328 | static int docd(const char *, int); |
| 2329 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2330 | static int |
| 2331 | cdopt(void) |
| 2332 | { |
| 2333 | int flags = 0; |
| 2334 | int i, j; |
| 2335 | |
| 2336 | j = 'L'; |
| 2337 | while ((i = nextopt("LP"))) { |
| 2338 | if (i != j) { |
| 2339 | flags ^= CD_PHYSICAL; |
| 2340 | j = i; |
| 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | return flags; |
| 2345 | } |
| 2346 | |
| 2347 | /* |
| 2348 | * Update curdir (the name of the current directory) in response to a |
| 2349 | * cd command. |
| 2350 | */ |
| 2351 | static const char * |
| 2352 | updatepwd(const char *dir) |
| 2353 | { |
| 2354 | char *new; |
| 2355 | char *p; |
| 2356 | char *cdcomppath; |
| 2357 | const char *lim; |
| 2358 | |
| 2359 | cdcomppath = ststrdup(dir); |
| 2360 | STARTSTACKSTR(new); |
| 2361 | if (*dir != '/') { |
| 2362 | if (curdir == nullstr) |
| 2363 | return 0; |
| 2364 | new = stack_putstr(curdir, new); |
| 2365 | } |
| 2366 | new = makestrspace(strlen(dir) + 2, new); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2367 | lim = (char *)stackblock() + 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2368 | if (*dir != '/') { |
| 2369 | if (new[-1] != '/') |
| 2370 | USTPUTC('/', new); |
| 2371 | if (new > lim && *lim == '/') |
| 2372 | lim++; |
| 2373 | } else { |
| 2374 | USTPUTC('/', new); |
| 2375 | cdcomppath++; |
| 2376 | if (dir[1] == '/' && dir[2] != '/') { |
| 2377 | USTPUTC('/', new); |
| 2378 | cdcomppath++; |
| 2379 | lim++; |
| 2380 | } |
| 2381 | } |
| 2382 | p = strtok(cdcomppath, "/"); |
| 2383 | while (p) { |
| 2384 | switch (*p) { |
| 2385 | case '.': |
| 2386 | if (p[1] == '.' && p[2] == '\0') { |
| 2387 | while (new > lim) { |
| 2388 | STUNPUTC(new); |
| 2389 | if (new[-1] == '/') |
| 2390 | break; |
| 2391 | } |
| 2392 | break; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 2393 | } |
| 2394 | if (p[1] == '\0') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2395 | break; |
| 2396 | /* fall through */ |
| 2397 | default: |
| 2398 | new = stack_putstr(p, new); |
| 2399 | USTPUTC('/', new); |
| 2400 | } |
| 2401 | p = strtok(0, "/"); |
| 2402 | } |
| 2403 | if (new > lim) |
| 2404 | STUNPUTC(new); |
| 2405 | *new = 0; |
| 2406 | return stackblock(); |
| 2407 | } |
| 2408 | |
| 2409 | /* |
| 2410 | * Find out what the current directory is. If we already know the current |
| 2411 | * directory, this routine returns immediately. |
| 2412 | */ |
| 2413 | static char * |
| 2414 | getpwd(void) |
| 2415 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2416 | char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2417 | return dir ? dir : nullstr; |
| 2418 | } |
| 2419 | |
| 2420 | static void |
| 2421 | setpwd(const char *val, int setold) |
| 2422 | { |
| 2423 | char *oldcur, *dir; |
| 2424 | |
| 2425 | oldcur = dir = curdir; |
| 2426 | |
| 2427 | if (setold) { |
| 2428 | setvar("OLDPWD", oldcur, VEXPORT); |
| 2429 | } |
| 2430 | INT_OFF; |
| 2431 | if (physdir != nullstr) { |
| 2432 | if (physdir != oldcur) |
| 2433 | free(physdir); |
| 2434 | physdir = nullstr; |
| 2435 | } |
| 2436 | if (oldcur == val || !val) { |
| 2437 | char *s = getpwd(); |
| 2438 | physdir = s; |
| 2439 | if (!val) |
| 2440 | dir = s; |
| 2441 | } else |
| 2442 | dir = ckstrdup(val); |
| 2443 | if (oldcur != dir && oldcur != nullstr) { |
| 2444 | free(oldcur); |
| 2445 | } |
| 2446 | curdir = dir; |
| 2447 | INT_ON; |
| 2448 | setvar("PWD", dir, VEXPORT); |
| 2449 | } |
| 2450 | |
| 2451 | static void hashcd(void); |
| 2452 | |
| 2453 | /* |
| 2454 | * Actually do the chdir. We also call hashcd to let the routines in exec.c |
| 2455 | * know that the current directory has changed. |
| 2456 | */ |
| 2457 | static int |
| 2458 | docd(const char *dest, int flags) |
| 2459 | { |
| 2460 | const char *dir = 0; |
| 2461 | int err; |
| 2462 | |
| 2463 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 2464 | |
| 2465 | INT_OFF; |
| 2466 | if (!(flags & CD_PHYSICAL)) { |
| 2467 | dir = updatepwd(dest); |
| 2468 | if (dir) |
| 2469 | dest = dir; |
| 2470 | } |
| 2471 | err = chdir(dest); |
| 2472 | if (err) |
| 2473 | goto out; |
| 2474 | setpwd(dir, 1); |
| 2475 | hashcd(); |
| 2476 | out: |
| 2477 | INT_ON; |
| 2478 | return err; |
| 2479 | } |
| 2480 | |
| 2481 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 2482 | cdcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2483 | { |
| 2484 | const char *dest; |
| 2485 | const char *path; |
| 2486 | const char *p; |
| 2487 | char c; |
| 2488 | struct stat statb; |
| 2489 | int flags; |
| 2490 | |
| 2491 | flags = cdopt(); |
| 2492 | dest = *argptr; |
| 2493 | if (!dest) |
| 2494 | dest = bltinlookup(homestr); |
| 2495 | else if (LONE_DASH(dest)) { |
| 2496 | dest = bltinlookup("OLDPWD"); |
| 2497 | flags |= CD_PRINT; |
| 2498 | } |
| 2499 | if (!dest) |
| 2500 | dest = nullstr; |
| 2501 | if (*dest == '/') |
| 2502 | goto step7; |
| 2503 | if (*dest == '.') { |
| 2504 | c = dest[1]; |
| 2505 | dotdot: |
| 2506 | switch (c) { |
| 2507 | case '\0': |
| 2508 | case '/': |
| 2509 | goto step6; |
| 2510 | case '.': |
| 2511 | c = dest[2]; |
| 2512 | if (c != '.') |
| 2513 | goto dotdot; |
| 2514 | } |
| 2515 | } |
| 2516 | if (!*dest) |
| 2517 | dest = "."; |
| 2518 | path = bltinlookup("CDPATH"); |
| 2519 | if (!path) { |
| 2520 | step6: |
| 2521 | step7: |
| 2522 | p = dest; |
| 2523 | goto docd; |
| 2524 | } |
| 2525 | do { |
| 2526 | c = *path; |
| 2527 | p = padvance(&path, dest); |
| 2528 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 2529 | if (c && c != ':') |
| 2530 | flags |= CD_PRINT; |
| 2531 | docd: |
| 2532 | if (!docd(p, flags)) |
| 2533 | goto out; |
| 2534 | break; |
| 2535 | } |
| 2536 | } while (path); |
| 2537 | ash_msg_and_raise_error("can't cd to %s", dest); |
| 2538 | /* NOTREACHED */ |
| 2539 | out: |
| 2540 | if (flags & CD_PRINT) |
| 2541 | out1fmt(snlfmt, curdir); |
| 2542 | return 0; |
| 2543 | } |
| 2544 | |
| 2545 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 2546 | pwdcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2547 | { |
| 2548 | int flags; |
| 2549 | const char *dir = curdir; |
| 2550 | |
| 2551 | flags = cdopt(); |
| 2552 | if (flags) { |
| 2553 | if (physdir == nullstr) |
| 2554 | setpwd(dir, 0); |
| 2555 | dir = physdir; |
| 2556 | } |
| 2557 | out1fmt(snlfmt, dir); |
| 2558 | return 0; |
| 2559 | } |
| 2560 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2561 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 2562 | /* ============ ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2563 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2564 | #define IBUFSIZ COMMON_BUFSIZE |
"Vladimir N. Oleynik" | 6f347ef | 2005-10-15 10:23:55 +0000 | [diff] [blame] | 2565 | #define basebuf bb_common_bufsiz1 /* buffer for top level input file */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2566 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2567 | /* Syntax classes */ |
| 2568 | #define CWORD 0 /* character is nothing special */ |
| 2569 | #define CNL 1 /* newline character */ |
| 2570 | #define CBACK 2 /* a backslash character */ |
| 2571 | #define CSQUOTE 3 /* single quote */ |
| 2572 | #define CDQUOTE 4 /* double quote */ |
| 2573 | #define CENDQUOTE 5 /* a terminating quote */ |
| 2574 | #define CBQUOTE 6 /* backwards single quote */ |
| 2575 | #define CVAR 7 /* a dollar sign */ |
| 2576 | #define CENDVAR 8 /* a '}' character */ |
| 2577 | #define CLP 9 /* a left paren in arithmetic */ |
| 2578 | #define CRP 10 /* a right paren in arithmetic */ |
| 2579 | #define CENDFILE 11 /* end of file */ |
| 2580 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 2581 | #define CSPCL 13 /* these terminate a word */ |
| 2582 | #define CIGN 14 /* character should be ignored */ |
| 2583 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2584 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2585 | #define SYNBASE 130 |
| 2586 | #define PEOF -130 |
| 2587 | #define PEOA -129 |
| 2588 | #define PEOA_OR_PEOF PEOA |
| 2589 | #else |
| 2590 | #define SYNBASE 129 |
| 2591 | #define PEOF -129 |
| 2592 | #define PEOA_OR_PEOF PEOF |
| 2593 | #endif |
| 2594 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2595 | /* number syntax index */ |
| 2596 | #define BASESYNTAX 0 /* not in quotes */ |
| 2597 | #define DQSYNTAX 1 /* in double quotes */ |
| 2598 | #define SQSYNTAX 2 /* in single quotes */ |
| 2599 | #define ARISYNTAX 3 /* in arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 2600 | #define PSSYNTAX 4 /* prompt */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2601 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2602 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2603 | #define USE_SIT_FUNCTION |
| 2604 | #endif |
| 2605 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2606 | #if ENABLE_ASH_MATH_SUPPORT |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2607 | static const char S_I_T[][4] = { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2608 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2609 | { CSPCL, CIGN, CIGN, CIGN }, /* 0, PEOA */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2610 | #endif |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2611 | { CSPCL, CWORD, CWORD, CWORD }, /* 1, ' ' */ |
| 2612 | { CNL, CNL, CNL, CNL }, /* 2, \n */ |
| 2613 | { CWORD, CCTL, CCTL, CWORD }, /* 3, !*-/:=?[]~ */ |
| 2614 | { CDQUOTE, CENDQUOTE, CWORD, CWORD }, /* 4, '"' */ |
| 2615 | { CVAR, CVAR, CWORD, CVAR }, /* 5, $ */ |
| 2616 | { CSQUOTE, CWORD, CENDQUOTE, CWORD }, /* 6, "'" */ |
| 2617 | { CSPCL, CWORD, CWORD, CLP }, /* 7, ( */ |
| 2618 | { CSPCL, CWORD, CWORD, CRP }, /* 8, ) */ |
| 2619 | { CBACK, CBACK, CCTL, CBACK }, /* 9, \ */ |
| 2620 | { CBQUOTE, CBQUOTE, CWORD, CBQUOTE }, /* 10, ` */ |
| 2621 | { CENDVAR, CENDVAR, CWORD, CENDVAR }, /* 11, } */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2622 | #ifndef USE_SIT_FUNCTION |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2623 | { CENDFILE, CENDFILE, CENDFILE, CENDFILE }, /* 12, PEOF */ |
| 2624 | { CWORD, CWORD, CWORD, CWORD }, /* 13, 0-9A-Za-z */ |
| 2625 | { CCTL, CCTL, CCTL, CCTL } /* 14, CTLESC ... */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2626 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2627 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2628 | #else |
| 2629 | static const char S_I_T[][3] = { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2630 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2631 | { CSPCL, CIGN, CIGN }, /* 0, PEOA */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2632 | #endif |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2633 | { CSPCL, CWORD, CWORD }, /* 1, ' ' */ |
| 2634 | { CNL, CNL, CNL }, /* 2, \n */ |
| 2635 | { CWORD, CCTL, CCTL }, /* 3, !*-/:=?[]~ */ |
| 2636 | { CDQUOTE, CENDQUOTE, CWORD }, /* 4, '"' */ |
| 2637 | { CVAR, CVAR, CWORD }, /* 5, $ */ |
| 2638 | { CSQUOTE, CWORD, CENDQUOTE }, /* 6, "'" */ |
| 2639 | { CSPCL, CWORD, CWORD }, /* 7, ( */ |
| 2640 | { CSPCL, CWORD, CWORD }, /* 8, ) */ |
| 2641 | { CBACK, CBACK, CCTL }, /* 9, \ */ |
| 2642 | { CBQUOTE, CBQUOTE, CWORD }, /* 10, ` */ |
| 2643 | { CENDVAR, CENDVAR, CWORD }, /* 11, } */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2644 | #ifndef USE_SIT_FUNCTION |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2645 | { CENDFILE, CENDFILE, CENDFILE }, /* 12, PEOF */ |
| 2646 | { CWORD, CWORD, CWORD }, /* 13, 0-9A-Za-z */ |
| 2647 | { CCTL, CCTL, CCTL } /* 14, CTLESC ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2648 | #endif |
| 2649 | }; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2650 | #endif /* ASH_MATH_SUPPORT */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2651 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2652 | #ifdef USE_SIT_FUNCTION |
| 2653 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2654 | static int |
| 2655 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2656 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2657 | static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-/:;<=>?[\\]`|}~"; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2658 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2659 | static const char syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2660 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
| 2661 | 7, 8, 3, 3, 3, 3, 1, 1, /* "()*-/:;<" */ |
| 2662 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 2663 | 11, 3 /* "}~" */ |
| 2664 | }; |
| 2665 | #else |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2666 | static const char syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2667 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
| 2668 | 6, 7, 2, 2, 2, 2, 0, 0, /* "()*-/:;<" */ |
| 2669 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 2670 | 10, 2 /* "}~" */ |
| 2671 | }; |
| 2672 | #endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2673 | const char *s; |
| 2674 | int indx; |
| 2675 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2676 | if (c == PEOF) /* 2^8+2 */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2677 | return CENDFILE; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2678 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2679 | if (c == PEOA) /* 2^8+1 */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2680 | indx = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2681 | else |
| 2682 | #endif |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2683 | #define U_C(c) ((unsigned char)(c)) |
| 2684 | |
| 2685 | if ((unsigned char)c >= (unsigned char)(CTLESC) |
| 2686 | && (unsigned char)c <= (unsigned char)(CTLQUOTEMARK) |
| 2687 | ) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 2688 | return CCTL; |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2689 | } else { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2690 | s = strchr(spec_symbls, c); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 2691 | if (s == NULL || *s == '\0') |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2692 | return CWORD; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2693 | indx = syntax_index_table[(s - spec_symbls)]; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2694 | } |
| 2695 | return S_I_T[indx][syntax]; |
| 2696 | } |
| 2697 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2698 | #else /* !USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2699 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2700 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2701 | #define CSPCL_CIGN_CIGN_CIGN 0 |
| 2702 | #define CSPCL_CWORD_CWORD_CWORD 1 |
| 2703 | #define CNL_CNL_CNL_CNL 2 |
| 2704 | #define CWORD_CCTL_CCTL_CWORD 3 |
| 2705 | #define CDQUOTE_CENDQUOTE_CWORD_CWORD 4 |
| 2706 | #define CVAR_CVAR_CWORD_CVAR 5 |
| 2707 | #define CSQUOTE_CWORD_CENDQUOTE_CWORD 6 |
| 2708 | #define CSPCL_CWORD_CWORD_CLP 7 |
| 2709 | #define CSPCL_CWORD_CWORD_CRP 8 |
| 2710 | #define CBACK_CBACK_CCTL_CBACK 9 |
| 2711 | #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE 10 |
| 2712 | #define CENDVAR_CENDVAR_CWORD_CENDVAR 11 |
| 2713 | #define CENDFILE_CENDFILE_CENDFILE_CENDFILE 12 |
| 2714 | #define CWORD_CWORD_CWORD_CWORD 13 |
| 2715 | #define CCTL_CCTL_CCTL_CCTL 14 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2716 | #else |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2717 | #define CSPCL_CWORD_CWORD_CWORD 0 |
| 2718 | #define CNL_CNL_CNL_CNL 1 |
| 2719 | #define CWORD_CCTL_CCTL_CWORD 2 |
| 2720 | #define CDQUOTE_CENDQUOTE_CWORD_CWORD 3 |
| 2721 | #define CVAR_CVAR_CWORD_CVAR 4 |
| 2722 | #define CSQUOTE_CWORD_CENDQUOTE_CWORD 5 |
| 2723 | #define CSPCL_CWORD_CWORD_CLP 6 |
| 2724 | #define CSPCL_CWORD_CWORD_CRP 7 |
| 2725 | #define CBACK_CBACK_CCTL_CBACK 8 |
| 2726 | #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE 9 |
| 2727 | #define CENDVAR_CENDVAR_CWORD_CENDVAR 10 |
| 2728 | #define CENDFILE_CENDFILE_CENDFILE_CENDFILE 11 |
| 2729 | #define CWORD_CWORD_CWORD_CWORD 12 |
| 2730 | #define CCTL_CCTL_CCTL_CCTL 13 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2731 | #endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2732 | |
| 2733 | static const char syntax_index_table[258] = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2734 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2735 | /* 0 PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2736 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2737 | /* 1 PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 2738 | #endif |
| 2739 | /* 2 -128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 2740 | /* 3 -127 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 2741 | /* 4 -126 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2742 | /* 5 -125 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2743 | /* 6 -124 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 2744 | /* 7 -123 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 2745 | /* 8 -122 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 2746 | /* 9 -121 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 2747 | /* 10 -120 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2748 | /* 11 -119 */ CWORD_CWORD_CWORD_CWORD, |
| 2749 | /* 12 -118 */ CWORD_CWORD_CWORD_CWORD, |
| 2750 | /* 13 -117 */ CWORD_CWORD_CWORD_CWORD, |
| 2751 | /* 14 -116 */ CWORD_CWORD_CWORD_CWORD, |
| 2752 | /* 15 -115 */ CWORD_CWORD_CWORD_CWORD, |
| 2753 | /* 16 -114 */ CWORD_CWORD_CWORD_CWORD, |
| 2754 | /* 17 -113 */ CWORD_CWORD_CWORD_CWORD, |
| 2755 | /* 18 -112 */ CWORD_CWORD_CWORD_CWORD, |
| 2756 | /* 19 -111 */ CWORD_CWORD_CWORD_CWORD, |
| 2757 | /* 20 -110 */ CWORD_CWORD_CWORD_CWORD, |
| 2758 | /* 21 -109 */ CWORD_CWORD_CWORD_CWORD, |
| 2759 | /* 22 -108 */ CWORD_CWORD_CWORD_CWORD, |
| 2760 | /* 23 -107 */ CWORD_CWORD_CWORD_CWORD, |
| 2761 | /* 24 -106 */ CWORD_CWORD_CWORD_CWORD, |
| 2762 | /* 25 -105 */ CWORD_CWORD_CWORD_CWORD, |
| 2763 | /* 26 -104 */ CWORD_CWORD_CWORD_CWORD, |
| 2764 | /* 27 -103 */ CWORD_CWORD_CWORD_CWORD, |
| 2765 | /* 28 -102 */ CWORD_CWORD_CWORD_CWORD, |
| 2766 | /* 29 -101 */ CWORD_CWORD_CWORD_CWORD, |
| 2767 | /* 30 -100 */ CWORD_CWORD_CWORD_CWORD, |
| 2768 | /* 31 -99 */ CWORD_CWORD_CWORD_CWORD, |
| 2769 | /* 32 -98 */ CWORD_CWORD_CWORD_CWORD, |
| 2770 | /* 33 -97 */ CWORD_CWORD_CWORD_CWORD, |
| 2771 | /* 34 -96 */ CWORD_CWORD_CWORD_CWORD, |
| 2772 | /* 35 -95 */ CWORD_CWORD_CWORD_CWORD, |
| 2773 | /* 36 -94 */ CWORD_CWORD_CWORD_CWORD, |
| 2774 | /* 37 -93 */ CWORD_CWORD_CWORD_CWORD, |
| 2775 | /* 38 -92 */ CWORD_CWORD_CWORD_CWORD, |
| 2776 | /* 39 -91 */ CWORD_CWORD_CWORD_CWORD, |
| 2777 | /* 40 -90 */ CWORD_CWORD_CWORD_CWORD, |
| 2778 | /* 41 -89 */ CWORD_CWORD_CWORD_CWORD, |
| 2779 | /* 42 -88 */ CWORD_CWORD_CWORD_CWORD, |
| 2780 | /* 43 -87 */ CWORD_CWORD_CWORD_CWORD, |
| 2781 | /* 44 -86 */ CWORD_CWORD_CWORD_CWORD, |
| 2782 | /* 45 -85 */ CWORD_CWORD_CWORD_CWORD, |
| 2783 | /* 46 -84 */ CWORD_CWORD_CWORD_CWORD, |
| 2784 | /* 47 -83 */ CWORD_CWORD_CWORD_CWORD, |
| 2785 | /* 48 -82 */ CWORD_CWORD_CWORD_CWORD, |
| 2786 | /* 49 -81 */ CWORD_CWORD_CWORD_CWORD, |
| 2787 | /* 50 -80 */ CWORD_CWORD_CWORD_CWORD, |
| 2788 | /* 51 -79 */ CWORD_CWORD_CWORD_CWORD, |
| 2789 | /* 52 -78 */ CWORD_CWORD_CWORD_CWORD, |
| 2790 | /* 53 -77 */ CWORD_CWORD_CWORD_CWORD, |
| 2791 | /* 54 -76 */ CWORD_CWORD_CWORD_CWORD, |
| 2792 | /* 55 -75 */ CWORD_CWORD_CWORD_CWORD, |
| 2793 | /* 56 -74 */ CWORD_CWORD_CWORD_CWORD, |
| 2794 | /* 57 -73 */ CWORD_CWORD_CWORD_CWORD, |
| 2795 | /* 58 -72 */ CWORD_CWORD_CWORD_CWORD, |
| 2796 | /* 59 -71 */ CWORD_CWORD_CWORD_CWORD, |
| 2797 | /* 60 -70 */ CWORD_CWORD_CWORD_CWORD, |
| 2798 | /* 61 -69 */ CWORD_CWORD_CWORD_CWORD, |
| 2799 | /* 62 -68 */ CWORD_CWORD_CWORD_CWORD, |
| 2800 | /* 63 -67 */ CWORD_CWORD_CWORD_CWORD, |
| 2801 | /* 64 -66 */ CWORD_CWORD_CWORD_CWORD, |
| 2802 | /* 65 -65 */ CWORD_CWORD_CWORD_CWORD, |
| 2803 | /* 66 -64 */ CWORD_CWORD_CWORD_CWORD, |
| 2804 | /* 67 -63 */ CWORD_CWORD_CWORD_CWORD, |
| 2805 | /* 68 -62 */ CWORD_CWORD_CWORD_CWORD, |
| 2806 | /* 69 -61 */ CWORD_CWORD_CWORD_CWORD, |
| 2807 | /* 70 -60 */ CWORD_CWORD_CWORD_CWORD, |
| 2808 | /* 71 -59 */ CWORD_CWORD_CWORD_CWORD, |
| 2809 | /* 72 -58 */ CWORD_CWORD_CWORD_CWORD, |
| 2810 | /* 73 -57 */ CWORD_CWORD_CWORD_CWORD, |
| 2811 | /* 74 -56 */ CWORD_CWORD_CWORD_CWORD, |
| 2812 | /* 75 -55 */ CWORD_CWORD_CWORD_CWORD, |
| 2813 | /* 76 -54 */ CWORD_CWORD_CWORD_CWORD, |
| 2814 | /* 77 -53 */ CWORD_CWORD_CWORD_CWORD, |
| 2815 | /* 78 -52 */ CWORD_CWORD_CWORD_CWORD, |
| 2816 | /* 79 -51 */ CWORD_CWORD_CWORD_CWORD, |
| 2817 | /* 80 -50 */ CWORD_CWORD_CWORD_CWORD, |
| 2818 | /* 81 -49 */ CWORD_CWORD_CWORD_CWORD, |
| 2819 | /* 82 -48 */ CWORD_CWORD_CWORD_CWORD, |
| 2820 | /* 83 -47 */ CWORD_CWORD_CWORD_CWORD, |
| 2821 | /* 84 -46 */ CWORD_CWORD_CWORD_CWORD, |
| 2822 | /* 85 -45 */ CWORD_CWORD_CWORD_CWORD, |
| 2823 | /* 86 -44 */ CWORD_CWORD_CWORD_CWORD, |
| 2824 | /* 87 -43 */ CWORD_CWORD_CWORD_CWORD, |
| 2825 | /* 88 -42 */ CWORD_CWORD_CWORD_CWORD, |
| 2826 | /* 89 -41 */ CWORD_CWORD_CWORD_CWORD, |
| 2827 | /* 90 -40 */ CWORD_CWORD_CWORD_CWORD, |
| 2828 | /* 91 -39 */ CWORD_CWORD_CWORD_CWORD, |
| 2829 | /* 92 -38 */ CWORD_CWORD_CWORD_CWORD, |
| 2830 | /* 93 -37 */ CWORD_CWORD_CWORD_CWORD, |
| 2831 | /* 94 -36 */ CWORD_CWORD_CWORD_CWORD, |
| 2832 | /* 95 -35 */ CWORD_CWORD_CWORD_CWORD, |
| 2833 | /* 96 -34 */ CWORD_CWORD_CWORD_CWORD, |
| 2834 | /* 97 -33 */ CWORD_CWORD_CWORD_CWORD, |
| 2835 | /* 98 -32 */ CWORD_CWORD_CWORD_CWORD, |
| 2836 | /* 99 -31 */ CWORD_CWORD_CWORD_CWORD, |
| 2837 | /* 100 -30 */ CWORD_CWORD_CWORD_CWORD, |
| 2838 | /* 101 -29 */ CWORD_CWORD_CWORD_CWORD, |
| 2839 | /* 102 -28 */ CWORD_CWORD_CWORD_CWORD, |
| 2840 | /* 103 -27 */ CWORD_CWORD_CWORD_CWORD, |
| 2841 | /* 104 -26 */ CWORD_CWORD_CWORD_CWORD, |
| 2842 | /* 105 -25 */ CWORD_CWORD_CWORD_CWORD, |
| 2843 | /* 106 -24 */ CWORD_CWORD_CWORD_CWORD, |
| 2844 | /* 107 -23 */ CWORD_CWORD_CWORD_CWORD, |
| 2845 | /* 108 -22 */ CWORD_CWORD_CWORD_CWORD, |
| 2846 | /* 109 -21 */ CWORD_CWORD_CWORD_CWORD, |
| 2847 | /* 110 -20 */ CWORD_CWORD_CWORD_CWORD, |
| 2848 | /* 111 -19 */ CWORD_CWORD_CWORD_CWORD, |
| 2849 | /* 112 -18 */ CWORD_CWORD_CWORD_CWORD, |
| 2850 | /* 113 -17 */ CWORD_CWORD_CWORD_CWORD, |
| 2851 | /* 114 -16 */ CWORD_CWORD_CWORD_CWORD, |
| 2852 | /* 115 -15 */ CWORD_CWORD_CWORD_CWORD, |
| 2853 | /* 116 -14 */ CWORD_CWORD_CWORD_CWORD, |
| 2854 | /* 117 -13 */ CWORD_CWORD_CWORD_CWORD, |
| 2855 | /* 118 -12 */ CWORD_CWORD_CWORD_CWORD, |
| 2856 | /* 119 -11 */ CWORD_CWORD_CWORD_CWORD, |
| 2857 | /* 120 -10 */ CWORD_CWORD_CWORD_CWORD, |
| 2858 | /* 121 -9 */ CWORD_CWORD_CWORD_CWORD, |
| 2859 | /* 122 -8 */ CWORD_CWORD_CWORD_CWORD, |
| 2860 | /* 123 -7 */ CWORD_CWORD_CWORD_CWORD, |
| 2861 | /* 124 -6 */ CWORD_CWORD_CWORD_CWORD, |
| 2862 | /* 125 -5 */ CWORD_CWORD_CWORD_CWORD, |
| 2863 | /* 126 -4 */ CWORD_CWORD_CWORD_CWORD, |
| 2864 | /* 127 -3 */ CWORD_CWORD_CWORD_CWORD, |
| 2865 | /* 128 -2 */ CWORD_CWORD_CWORD_CWORD, |
| 2866 | /* 129 -1 */ CWORD_CWORD_CWORD_CWORD, |
| 2867 | /* 130 0 */ CWORD_CWORD_CWORD_CWORD, |
| 2868 | /* 131 1 */ CWORD_CWORD_CWORD_CWORD, |
| 2869 | /* 132 2 */ CWORD_CWORD_CWORD_CWORD, |
| 2870 | /* 133 3 */ CWORD_CWORD_CWORD_CWORD, |
| 2871 | /* 134 4 */ CWORD_CWORD_CWORD_CWORD, |
| 2872 | /* 135 5 */ CWORD_CWORD_CWORD_CWORD, |
| 2873 | /* 136 6 */ CWORD_CWORD_CWORD_CWORD, |
| 2874 | /* 137 7 */ CWORD_CWORD_CWORD_CWORD, |
| 2875 | /* 138 8 */ CWORD_CWORD_CWORD_CWORD, |
| 2876 | /* 139 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 2877 | /* 140 10 "\n" */ CNL_CNL_CNL_CNL, |
| 2878 | /* 141 11 */ CWORD_CWORD_CWORD_CWORD, |
| 2879 | /* 142 12 */ CWORD_CWORD_CWORD_CWORD, |
| 2880 | /* 143 13 */ CWORD_CWORD_CWORD_CWORD, |
| 2881 | /* 144 14 */ CWORD_CWORD_CWORD_CWORD, |
| 2882 | /* 145 15 */ CWORD_CWORD_CWORD_CWORD, |
| 2883 | /* 146 16 */ CWORD_CWORD_CWORD_CWORD, |
| 2884 | /* 147 17 */ CWORD_CWORD_CWORD_CWORD, |
| 2885 | /* 148 18 */ CWORD_CWORD_CWORD_CWORD, |
| 2886 | /* 149 19 */ CWORD_CWORD_CWORD_CWORD, |
| 2887 | /* 150 20 */ CWORD_CWORD_CWORD_CWORD, |
| 2888 | /* 151 21 */ CWORD_CWORD_CWORD_CWORD, |
| 2889 | /* 152 22 */ CWORD_CWORD_CWORD_CWORD, |
| 2890 | /* 153 23 */ CWORD_CWORD_CWORD_CWORD, |
| 2891 | /* 154 24 */ CWORD_CWORD_CWORD_CWORD, |
| 2892 | /* 155 25 */ CWORD_CWORD_CWORD_CWORD, |
| 2893 | /* 156 26 */ CWORD_CWORD_CWORD_CWORD, |
| 2894 | /* 157 27 */ CWORD_CWORD_CWORD_CWORD, |
| 2895 | /* 158 28 */ CWORD_CWORD_CWORD_CWORD, |
| 2896 | /* 159 29 */ CWORD_CWORD_CWORD_CWORD, |
| 2897 | /* 160 30 */ CWORD_CWORD_CWORD_CWORD, |
| 2898 | /* 161 31 */ CWORD_CWORD_CWORD_CWORD, |
| 2899 | /* 162 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 2900 | /* 163 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2901 | /* 164 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2902 | /* 165 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 2903 | /* 166 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 2904 | /* 167 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 2905 | /* 168 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2906 | /* 169 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2907 | /* 170 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 2908 | /* 171 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 2909 | /* 172 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 2910 | /* 173 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 2911 | /* 174 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 2912 | /* 175 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 2913 | /* 176 46 "." */ CWORD_CWORD_CWORD_CWORD, |
| 2914 | /* 177 47 "/" */ CWORD_CCTL_CCTL_CWORD, |
| 2915 | /* 178 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 2916 | /* 179 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 2917 | /* 180 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 2918 | /* 181 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 2919 | /* 182 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 2920 | /* 183 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 2921 | /* 184 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 2922 | /* 185 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 2923 | /* 186 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 2924 | /* 187 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 2925 | /* 188 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 2926 | /* 189 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 2927 | /* 190 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 2928 | /* 191 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 2929 | /* 192 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 2930 | /* 193 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 2931 | /* 194 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 2932 | /* 195 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 2933 | /* 196 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 2934 | /* 197 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 2935 | /* 198 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 2936 | /* 199 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 2937 | /* 200 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 2938 | /* 201 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 2939 | /* 202 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 2940 | /* 203 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 2941 | /* 204 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 2942 | /* 205 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 2943 | /* 206 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 2944 | /* 207 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 2945 | /* 208 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 2946 | /* 209 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 2947 | /* 210 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 2948 | /* 211 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 2949 | /* 212 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 2950 | /* 213 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 2951 | /* 214 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 2952 | /* 215 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 2953 | /* 216 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 2954 | /* 217 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 2955 | /* 218 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 2956 | /* 219 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 2957 | /* 220 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 2958 | /* 221 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 2959 | /* 222 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 2960 | /* 223 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 2961 | /* 224 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 2962 | /* 225 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 2963 | /* 226 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 2964 | /* 227 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 2965 | /* 228 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 2966 | /* 229 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 2967 | /* 230 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 2968 | /* 231 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 2969 | /* 232 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 2970 | /* 233 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 2971 | /* 234 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 2972 | /* 235 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 2973 | /* 236 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 2974 | /* 237 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 2975 | /* 238 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 2976 | /* 239 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 2977 | /* 240 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 2978 | /* 241 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 2979 | /* 242 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 2980 | /* 243 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 2981 | /* 244 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 2982 | /* 245 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 2983 | /* 246 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 2984 | /* 247 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 2985 | /* 248 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 2986 | /* 249 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 2987 | /* 250 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 2988 | /* 251 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 2989 | /* 252 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 2990 | /* 253 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 2991 | /* 254 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 2992 | /* 255 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 2993 | /* 256 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 2994 | /* 257 127 */ CWORD_CWORD_CWORD_CWORD, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2995 | }; |
| 2996 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2997 | #define SIT(c, syntax) (S_I_T[(int)syntax_index_table[((int)c)+SYNBASE]][syntax]) |
| 2998 | |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 2999 | #endif /* USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3000 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3001 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3002 | /* ============ Alias handling */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3003 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3004 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3005 | |
| 3006 | #define ALIASINUSE 1 |
| 3007 | #define ALIASDEAD 2 |
| 3008 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3009 | struct alias { |
| 3010 | struct alias *next; |
| 3011 | char *name; |
| 3012 | char *val; |
| 3013 | int flag; |
| 3014 | }; |
| 3015 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 3016 | |
| 3017 | static struct alias **atab; // [ATABSIZE]; |
| 3018 | #define INIT_G_alias() do { \ |
| 3019 | atab = xzalloc(ATABSIZE * sizeof(atab[0])); \ |
| 3020 | } while (0) |
| 3021 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3022 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3023 | static struct alias ** |
| 3024 | __lookupalias(const char *name) { |
| 3025 | unsigned int hashval; |
| 3026 | struct alias **app; |
| 3027 | const char *p; |
| 3028 | unsigned int ch; |
| 3029 | |
| 3030 | p = name; |
| 3031 | |
| 3032 | ch = (unsigned char)*p; |
| 3033 | hashval = ch << 4; |
| 3034 | while (ch) { |
| 3035 | hashval += ch; |
| 3036 | ch = (unsigned char)*++p; |
| 3037 | } |
| 3038 | app = &atab[hashval % ATABSIZE]; |
| 3039 | |
| 3040 | for (; *app; app = &(*app)->next) { |
| 3041 | if (strcmp(name, (*app)->name) == 0) { |
| 3042 | break; |
| 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | return app; |
| 3047 | } |
| 3048 | |
| 3049 | static struct alias * |
| 3050 | lookupalias(const char *name, int check) |
| 3051 | { |
| 3052 | struct alias *ap = *__lookupalias(name); |
| 3053 | |
| 3054 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 3055 | return NULL; |
| 3056 | return ap; |
| 3057 | } |
| 3058 | |
| 3059 | static struct alias * |
| 3060 | freealias(struct alias *ap) |
| 3061 | { |
| 3062 | struct alias *next; |
| 3063 | |
| 3064 | if (ap->flag & ALIASINUSE) { |
| 3065 | ap->flag |= ALIASDEAD; |
| 3066 | return ap; |
| 3067 | } |
| 3068 | |
| 3069 | next = ap->next; |
| 3070 | free(ap->name); |
| 3071 | free(ap->val); |
| 3072 | free(ap); |
| 3073 | return next; |
| 3074 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3075 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3076 | static void |
| 3077 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3078 | { |
| 3079 | struct alias *ap, **app; |
| 3080 | |
| 3081 | app = __lookupalias(name); |
| 3082 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3083 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3084 | if (ap) { |
| 3085 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3086 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3087 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3088 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3089 | ap->flag &= ~ALIASDEAD; |
| 3090 | } else { |
| 3091 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3092 | ap = ckzalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3093 | ap->name = ckstrdup(name); |
| 3094 | ap->val = ckstrdup(val); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3095 | /*ap->flag = 0; - ckzalloc did it */ |
| 3096 | /*ap->next = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3097 | *app = ap; |
| 3098 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3099 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3102 | static int |
| 3103 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3104 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3105 | struct alias **app; |
| 3106 | |
| 3107 | app = __lookupalias(name); |
| 3108 | |
| 3109 | if (*app) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3110 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3111 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3112 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3113 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3116 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3119 | static void |
| 3120 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3121 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3122 | struct alias *ap, **app; |
| 3123 | int i; |
| 3124 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3125 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3126 | for (i = 0; i < ATABSIZE; i++) { |
| 3127 | app = &atab[i]; |
| 3128 | for (ap = *app; ap; ap = *app) { |
| 3129 | *app = freealias(*app); |
| 3130 | if (ap == *app) { |
| 3131 | app = &ap->next; |
| 3132 | } |
| 3133 | } |
| 3134 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3135 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3138 | static void |
| 3139 | printalias(const struct alias *ap) |
| 3140 | { |
| 3141 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3142 | } |
| 3143 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3144 | /* |
| 3145 | * TODO - sort output |
| 3146 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3147 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3148 | aliascmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3149 | { |
| 3150 | char *n, *v; |
| 3151 | int ret = 0; |
| 3152 | struct alias *ap; |
| 3153 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3154 | if (!argv[1]) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3155 | int i; |
| 3156 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3157 | for (i = 0; i < ATABSIZE; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3158 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3159 | printalias(ap); |
| 3160 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3161 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3162 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3163 | } |
| 3164 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3165 | v = strchr(n+1, '='); |
| 3166 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3167 | ap = *__lookupalias(n); |
| 3168 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3169 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3170 | ret = 1; |
| 3171 | } else |
| 3172 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3173 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3174 | *v++ = '\0'; |
| 3175 | setalias(n, v); |
| 3176 | } |
| 3177 | } |
| 3178 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3179 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3182 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3183 | unaliascmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3184 | { |
| 3185 | int i; |
| 3186 | |
| 3187 | while ((i = nextopt("a")) != '\0') { |
| 3188 | if (i == 'a') { |
| 3189 | rmaliases(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3190 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3191 | } |
| 3192 | } |
| 3193 | for (i = 0; *argptr; argptr++) { |
| 3194 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3195 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3196 | i = 1; |
| 3197 | } |
| 3198 | } |
| 3199 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3200 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3201 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3202 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3203 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3204 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3205 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3206 | /* ============ jobs.c */ |
| 3207 | |
| 3208 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
| 3209 | #define FORK_FG 0 |
| 3210 | #define FORK_BG 1 |
| 3211 | #define FORK_NOJOB 2 |
| 3212 | |
| 3213 | /* mode flags for showjob(s) */ |
| 3214 | #define SHOW_PGID 0x01 /* only show pgid - for jobs -p */ |
| 3215 | #define SHOW_PID 0x04 /* include process pid */ |
| 3216 | #define SHOW_CHANGED 0x08 /* only jobs whose state has changed */ |
| 3217 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3218 | /* |
| 3219 | * A job structure contains information about a job. A job is either a |
| 3220 | * single process or a set of processes contained in a pipeline. In the |
| 3221 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 3222 | * array of pids. |
| 3223 | */ |
| 3224 | |
| 3225 | struct procstat { |
| 3226 | pid_t pid; /* process id */ |
| 3227 | int status; /* last process status from wait() */ |
| 3228 | char *cmd; /* text of command being run */ |
| 3229 | }; |
| 3230 | |
| 3231 | struct job { |
| 3232 | struct procstat ps0; /* status of process */ |
| 3233 | struct procstat *ps; /* status or processes when more than one */ |
| 3234 | #if JOBS |
| 3235 | int stopstatus; /* status of a stopped job */ |
| 3236 | #endif |
| 3237 | uint32_t |
| 3238 | nprocs: 16, /* number of processes */ |
| 3239 | state: 8, |
| 3240 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3241 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3242 | #define JOBDONE 2 /* all procs are completed */ |
| 3243 | #if JOBS |
| 3244 | sigint: 1, /* job was killed by SIGINT */ |
| 3245 | jobctl: 1, /* job running under job control */ |
| 3246 | #endif |
| 3247 | waited: 1, /* true if this entry has been waited for */ |
| 3248 | used: 1, /* true if this entry is in used */ |
| 3249 | changed: 1; /* true if status has changed */ |
| 3250 | struct job *prev_job; /* previous job */ |
| 3251 | }; |
| 3252 | |
| 3253 | static pid_t backgndpid; /* pid of last background process */ |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3254 | static smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3255 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3256 | static struct job *makejob(/*union node *,*/ int); |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3257 | #if !JOBS |
| 3258 | #define forkshell(job, node, mode) forkshell(job, mode) |
| 3259 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3260 | static int forkshell(struct job *, union node *, int); |
| 3261 | static int waitforjob(struct job *); |
| 3262 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3263 | #if !JOBS |
| 3264 | enum { jobctl = 0 }; |
| 3265 | #define setjobctl(on) do {} while (0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3266 | #else |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3267 | static smallint jobctl; /* true if doing job control */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3268 | static void setjobctl(int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3269 | #endif |
| 3270 | |
| 3271 | /* |
| 3272 | * Set the signal handler for the specified signal. The routine figures |
| 3273 | * out what it should be set to. |
| 3274 | */ |
| 3275 | static void |
| 3276 | setsignal(int signo) |
| 3277 | { |
| 3278 | int action; |
| 3279 | char *t, tsig; |
| 3280 | struct sigaction act; |
| 3281 | |
| 3282 | t = trap[signo]; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3283 | action = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3284 | if (t == NULL) |
| 3285 | action = S_DFL; |
| 3286 | else if (*t != '\0') |
| 3287 | action = S_CATCH; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3288 | if (rootshell && action == S_DFL) { |
| 3289 | switch (signo) { |
| 3290 | case SIGINT: |
| 3291 | if (iflag || minusc || sflag == 0) |
| 3292 | action = S_CATCH; |
| 3293 | break; |
| 3294 | case SIGQUIT: |
| 3295 | #if DEBUG |
| 3296 | if (debug) |
| 3297 | break; |
| 3298 | #endif |
| 3299 | /* FALLTHROUGH */ |
| 3300 | case SIGTERM: |
| 3301 | if (iflag) |
| 3302 | action = S_IGN; |
| 3303 | break; |
| 3304 | #if JOBS |
| 3305 | case SIGTSTP: |
| 3306 | case SIGTTOU: |
| 3307 | if (mflag) |
| 3308 | action = S_IGN; |
| 3309 | break; |
| 3310 | #endif |
| 3311 | } |
| 3312 | } |
| 3313 | |
| 3314 | t = &sigmode[signo - 1]; |
| 3315 | tsig = *t; |
| 3316 | if (tsig == 0) { |
| 3317 | /* |
| 3318 | * current setting unknown |
| 3319 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3320 | if (sigaction(signo, NULL, &act) == -1) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3321 | /* |
| 3322 | * Pretend it worked; maybe we should give a warning |
| 3323 | * here, but other shells don't. We don't alter |
| 3324 | * sigmode, so that we retry every time. |
| 3325 | */ |
| 3326 | return; |
| 3327 | } |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3328 | tsig = S_RESET; /* force to be set */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3329 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3330 | tsig = S_HARD_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3331 | if (mflag |
| 3332 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
| 3333 | ) { |
| 3334 | tsig = S_IGN; /* don't hard ignore these */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3335 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3336 | } |
| 3337 | } |
| 3338 | if (tsig == S_HARD_IGN || tsig == action) |
| 3339 | return; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3340 | act.sa_handler = SIG_DFL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3341 | switch (action) { |
| 3342 | case S_CATCH: |
| 3343 | act.sa_handler = onsig; |
| 3344 | break; |
| 3345 | case S_IGN: |
| 3346 | act.sa_handler = SIG_IGN; |
| 3347 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3348 | } |
| 3349 | *t = action; |
| 3350 | act.sa_flags = 0; |
| 3351 | sigfillset(&act.sa_mask); |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3352 | sigaction_set(signo, &act); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | /* mode flags for set_curjob */ |
| 3356 | #define CUR_DELETE 2 |
| 3357 | #define CUR_RUNNING 1 |
| 3358 | #define CUR_STOPPED 0 |
| 3359 | |
| 3360 | /* mode flags for dowait */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3361 | #define DOWAIT_NONBLOCK WNOHANG |
| 3362 | #define DOWAIT_BLOCK 0 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3363 | |
| 3364 | #if JOBS |
| 3365 | /* pgrp of shell on invocation */ |
| 3366 | static int initialpgrp; |
| 3367 | static int ttyfd = -1; |
| 3368 | #endif |
| 3369 | /* array of jobs */ |
| 3370 | static struct job *jobtab; |
| 3371 | /* size of array */ |
| 3372 | static unsigned njobs; |
| 3373 | /* current job */ |
| 3374 | static struct job *curjob; |
| 3375 | /* number of presumed living untracked jobs */ |
| 3376 | static int jobless; |
| 3377 | |
| 3378 | static void |
| 3379 | set_curjob(struct job *jp, unsigned mode) |
| 3380 | { |
| 3381 | struct job *jp1; |
| 3382 | struct job **jpp, **curp; |
| 3383 | |
| 3384 | /* first remove from list */ |
| 3385 | jpp = curp = &curjob; |
| 3386 | do { |
| 3387 | jp1 = *jpp; |
| 3388 | if (jp1 == jp) |
| 3389 | break; |
| 3390 | jpp = &jp1->prev_job; |
| 3391 | } while (1); |
| 3392 | *jpp = jp1->prev_job; |
| 3393 | |
| 3394 | /* Then re-insert in correct position */ |
| 3395 | jpp = curp; |
| 3396 | switch (mode) { |
| 3397 | default: |
| 3398 | #if DEBUG |
| 3399 | abort(); |
| 3400 | #endif |
| 3401 | case CUR_DELETE: |
| 3402 | /* job being deleted */ |
| 3403 | break; |
| 3404 | case CUR_RUNNING: |
| 3405 | /* newly created job or backgrounded job, |
| 3406 | put after all stopped jobs. */ |
| 3407 | do { |
| 3408 | jp1 = *jpp; |
| 3409 | #if JOBS |
| 3410 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3411 | #endif |
| 3412 | break; |
| 3413 | jpp = &jp1->prev_job; |
| 3414 | } while (1); |
| 3415 | /* FALLTHROUGH */ |
| 3416 | #if JOBS |
| 3417 | case CUR_STOPPED: |
| 3418 | #endif |
| 3419 | /* newly stopped job - becomes curjob */ |
| 3420 | jp->prev_job = *jpp; |
| 3421 | *jpp = jp; |
| 3422 | break; |
| 3423 | } |
| 3424 | } |
| 3425 | |
| 3426 | #if JOBS || DEBUG |
| 3427 | static int |
| 3428 | jobno(const struct job *jp) |
| 3429 | { |
| 3430 | return jp - jobtab + 1; |
| 3431 | } |
| 3432 | #endif |
| 3433 | |
| 3434 | /* |
| 3435 | * Convert a job name to a job structure. |
| 3436 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3437 | #if !JOBS |
| 3438 | #define getjob(name, getctl) getjob(name) |
| 3439 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3440 | static struct job * |
| 3441 | getjob(const char *name, int getctl) |
| 3442 | { |
| 3443 | struct job *jp; |
| 3444 | struct job *found; |
| 3445 | const char *err_msg = "No such job: %s"; |
| 3446 | unsigned num; |
| 3447 | int c; |
| 3448 | const char *p; |
| 3449 | char *(*match)(const char *, const char *); |
| 3450 | |
| 3451 | jp = curjob; |
| 3452 | p = name; |
| 3453 | if (!p) |
| 3454 | goto currentjob; |
| 3455 | |
| 3456 | if (*p != '%') |
| 3457 | goto err; |
| 3458 | |
| 3459 | c = *++p; |
| 3460 | if (!c) |
| 3461 | goto currentjob; |
| 3462 | |
| 3463 | if (!p[1]) { |
| 3464 | if (c == '+' || c == '%') { |
| 3465 | currentjob: |
| 3466 | err_msg = "No current job"; |
| 3467 | goto check; |
| 3468 | } |
| 3469 | if (c == '-') { |
| 3470 | if (jp) |
| 3471 | jp = jp->prev_job; |
| 3472 | err_msg = "No previous job"; |
| 3473 | check: |
| 3474 | if (!jp) |
| 3475 | goto err; |
| 3476 | goto gotit; |
| 3477 | } |
| 3478 | } |
| 3479 | |
| 3480 | if (is_number(p)) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 3481 | // TODO: number() instead? It does error checking... |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3482 | num = atoi(p); |
| 3483 | if (num < njobs) { |
| 3484 | jp = jobtab + num - 1; |
| 3485 | if (jp->used) |
| 3486 | goto gotit; |
| 3487 | goto err; |
| 3488 | } |
| 3489 | } |
| 3490 | |
| 3491 | match = prefix; |
| 3492 | if (*p == '?') { |
| 3493 | match = strstr; |
| 3494 | p++; |
| 3495 | } |
| 3496 | |
| 3497 | found = 0; |
| 3498 | while (1) { |
| 3499 | if (!jp) |
| 3500 | goto err; |
| 3501 | if (match(jp->ps[0].cmd, p)) { |
| 3502 | if (found) |
| 3503 | goto err; |
| 3504 | found = jp; |
| 3505 | err_msg = "%s: ambiguous"; |
| 3506 | } |
| 3507 | jp = jp->prev_job; |
| 3508 | } |
| 3509 | |
| 3510 | gotit: |
| 3511 | #if JOBS |
| 3512 | err_msg = "job %s not created under job control"; |
| 3513 | if (getctl && jp->jobctl == 0) |
| 3514 | goto err; |
| 3515 | #endif |
| 3516 | return jp; |
| 3517 | err: |
| 3518 | ash_msg_and_raise_error(err_msg, name); |
| 3519 | } |
| 3520 | |
| 3521 | /* |
| 3522 | * Mark a job structure as unused. |
| 3523 | */ |
| 3524 | static void |
| 3525 | freejob(struct job *jp) |
| 3526 | { |
| 3527 | struct procstat *ps; |
| 3528 | int i; |
| 3529 | |
| 3530 | INT_OFF; |
| 3531 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
| 3532 | if (ps->cmd != nullstr) |
| 3533 | free(ps->cmd); |
| 3534 | } |
| 3535 | if (jp->ps != &jp->ps0) |
| 3536 | free(jp->ps); |
| 3537 | jp->used = 0; |
| 3538 | set_curjob(jp, CUR_DELETE); |
| 3539 | INT_ON; |
| 3540 | } |
| 3541 | |
| 3542 | #if JOBS |
| 3543 | static void |
| 3544 | xtcsetpgrp(int fd, pid_t pgrp) |
| 3545 | { |
| 3546 | if (tcsetpgrp(fd, pgrp)) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 3547 | ash_msg_and_raise_error("cannot set tty process group (%m)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
| 3550 | /* |
| 3551 | * Turn job control on and off. |
| 3552 | * |
| 3553 | * Note: This code assumes that the third arg to ioctl is a character |
| 3554 | * pointer, which is true on Berkeley systems but not System V. Since |
| 3555 | * System V doesn't have job control yet, this isn't a problem now. |
| 3556 | * |
| 3557 | * Called with interrupts off. |
| 3558 | */ |
| 3559 | static void |
| 3560 | setjobctl(int on) |
| 3561 | { |
| 3562 | int fd; |
| 3563 | int pgrp; |
| 3564 | |
| 3565 | if (on == jobctl || rootshell == 0) |
| 3566 | return; |
| 3567 | if (on) { |
| 3568 | int ofd; |
| 3569 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 3570 | if (fd < 0) { |
| 3571 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 3572 | * That sometimes helps to acquire controlling tty. |
| 3573 | * Obviously, a workaround for bugs when someone |
| 3574 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3575 | fd = 2; |
| 3576 | while (!isatty(fd)) |
| 3577 | if (--fd < 0) |
| 3578 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3579 | } |
| 3580 | fd = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3581 | if (ofd >= 0) |
| 3582 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3583 | if (fd < 0) |
| 3584 | goto out; |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3585 | /* fd is a tty at this point */ |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 3586 | close_on_exec_on(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3587 | do { /* while we are in the background */ |
| 3588 | pgrp = tcgetpgrp(fd); |
| 3589 | if (pgrp < 0) { |
| 3590 | out: |
| 3591 | ash_msg("can't access tty; job control turned off"); |
| 3592 | mflag = on = 0; |
| 3593 | goto close; |
| 3594 | } |
| 3595 | if (pgrp == getpgrp()) |
| 3596 | break; |
| 3597 | killpg(0, SIGTTIN); |
| 3598 | } while (1); |
| 3599 | initialpgrp = pgrp; |
| 3600 | |
| 3601 | setsignal(SIGTSTP); |
| 3602 | setsignal(SIGTTOU); |
| 3603 | setsignal(SIGTTIN); |
| 3604 | pgrp = rootpid; |
| 3605 | setpgid(0, pgrp); |
| 3606 | xtcsetpgrp(fd, pgrp); |
| 3607 | } else { |
| 3608 | /* turning job control off */ |
| 3609 | fd = ttyfd; |
| 3610 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3611 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3612 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3613 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3614 | setpgid(0, pgrp); |
| 3615 | setsignal(SIGTSTP); |
| 3616 | setsignal(SIGTTOU); |
| 3617 | setsignal(SIGTTIN); |
| 3618 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3619 | if (fd >= 0) |
| 3620 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3621 | fd = -1; |
| 3622 | } |
| 3623 | ttyfd = fd; |
| 3624 | jobctl = on; |
| 3625 | } |
| 3626 | |
| 3627 | static int |
| 3628 | killcmd(int argc, char **argv) |
| 3629 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3630 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3631 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3632 | do { |
| 3633 | if (argv[i][0] == '%') { |
| 3634 | struct job *jp = getjob(argv[i], 0); |
| 3635 | unsigned pid = jp->ps[0].pid; |
| 3636 | /* Enough space for ' -NNN<nul>' */ |
| 3637 | argv[i] = alloca(sizeof(int)*3 + 3); |
| 3638 | /* kill_main has matching code to expect |
| 3639 | * leading space. Needed to not confuse |
| 3640 | * negative pids with "kill -SIGNAL_NO" syntax */ |
| 3641 | sprintf(argv[i], " -%u", pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3642 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3643 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3644 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3645 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
| 3648 | static void |
| 3649 | showpipe(struct job *jp, FILE *out) |
| 3650 | { |
| 3651 | struct procstat *sp; |
| 3652 | struct procstat *spend; |
| 3653 | |
| 3654 | spend = jp->ps + jp->nprocs; |
| 3655 | for (sp = jp->ps + 1; sp < spend; sp++) |
| 3656 | fprintf(out, " | %s", sp->cmd); |
| 3657 | outcslow('\n', out); |
| 3658 | flush_stdout_stderr(); |
| 3659 | } |
| 3660 | |
| 3661 | |
| 3662 | static int |
| 3663 | restartjob(struct job *jp, int mode) |
| 3664 | { |
| 3665 | struct procstat *ps; |
| 3666 | int i; |
| 3667 | int status; |
| 3668 | pid_t pgid; |
| 3669 | |
| 3670 | INT_OFF; |
| 3671 | if (jp->state == JOBDONE) |
| 3672 | goto out; |
| 3673 | jp->state = JOBRUNNING; |
| 3674 | pgid = jp->ps->pid; |
| 3675 | if (mode == FORK_FG) |
| 3676 | xtcsetpgrp(ttyfd, pgid); |
| 3677 | killpg(pgid, SIGCONT); |
| 3678 | ps = jp->ps; |
| 3679 | i = jp->nprocs; |
| 3680 | do { |
| 3681 | if (WIFSTOPPED(ps->status)) { |
| 3682 | ps->status = -1; |
| 3683 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3684 | ps++; |
| 3685 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3686 | out: |
| 3687 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 3688 | INT_ON; |
| 3689 | return status; |
| 3690 | } |
| 3691 | |
| 3692 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3693 | fg_bgcmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3694 | { |
| 3695 | struct job *jp; |
| 3696 | FILE *out; |
| 3697 | int mode; |
| 3698 | int retval; |
| 3699 | |
| 3700 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 3701 | nextopt(nullstr); |
| 3702 | argv = argptr; |
| 3703 | out = stdout; |
| 3704 | do { |
| 3705 | jp = getjob(*argv, 1); |
| 3706 | if (mode == FORK_BG) { |
| 3707 | set_curjob(jp, CUR_RUNNING); |
| 3708 | fprintf(out, "[%d] ", jobno(jp)); |
| 3709 | } |
| 3710 | outstr(jp->ps->cmd, out); |
| 3711 | showpipe(jp, out); |
| 3712 | retval = restartjob(jp, mode); |
| 3713 | } while (*argv && *++argv); |
| 3714 | return retval; |
| 3715 | } |
| 3716 | #endif |
| 3717 | |
| 3718 | static int |
| 3719 | sprint_status(char *s, int status, int sigonly) |
| 3720 | { |
| 3721 | int col; |
| 3722 | int st; |
| 3723 | |
| 3724 | col = 0; |
| 3725 | if (!WIFEXITED(status)) { |
| 3726 | #if JOBS |
| 3727 | if (WIFSTOPPED(status)) |
| 3728 | st = WSTOPSIG(status); |
| 3729 | else |
| 3730 | #endif |
| 3731 | st = WTERMSIG(status); |
| 3732 | if (sigonly) { |
| 3733 | if (st == SIGINT || st == SIGPIPE) |
| 3734 | goto out; |
| 3735 | #if JOBS |
| 3736 | if (WIFSTOPPED(status)) |
| 3737 | goto out; |
| 3738 | #endif |
| 3739 | } |
| 3740 | st &= 0x7f; |
| 3741 | col = fmtstr(s, 32, strsignal(st)); |
| 3742 | if (WCOREDUMP(status)) { |
| 3743 | col += fmtstr(s + col, 16, " (core dumped)"); |
| 3744 | } |
| 3745 | } else if (!sigonly) { |
| 3746 | st = WEXITSTATUS(status); |
| 3747 | if (st) |
| 3748 | col = fmtstr(s, 16, "Done(%d)", st); |
| 3749 | else |
| 3750 | col = fmtstr(s, 16, "Done"); |
| 3751 | } |
| 3752 | out: |
| 3753 | return col; |
| 3754 | } |
| 3755 | |
| 3756 | /* |
| 3757 | * Do a wait system call. If job control is compiled in, we accept |
| 3758 | * stopped processes. If block is zero, we return a value of zero |
| 3759 | * rather than blocking. |
| 3760 | * |
| 3761 | * System V doesn't have a non-blocking wait system call. It does |
| 3762 | * have a SIGCLD signal that is sent to a process when one of it's |
| 3763 | * children dies. The obvious way to use SIGCLD would be to install |
| 3764 | * a handler for SIGCLD which simply bumped a counter when a SIGCLD |
| 3765 | * was received, and have waitproc bump another counter when it got |
| 3766 | * the status of a process. Waitproc would then know that a wait |
| 3767 | * system call would not block if the two counters were different. |
| 3768 | * This approach doesn't work because if a process has children that |
| 3769 | * have not been waited for, System V will send it a SIGCLD when it |
| 3770 | * installs a signal handler for SIGCLD. What this means is that when |
| 3771 | * a child exits, the shell will be sent SIGCLD signals continuously |
| 3772 | * until is runs out of stack space, unless it does a wait call before |
| 3773 | * restoring the signal handler. The code below takes advantage of |
| 3774 | * this (mis)feature by installing a signal handler for SIGCLD and |
| 3775 | * then checking to see whether it was called. If there are any |
| 3776 | * children to be waited for, it will be. |
| 3777 | * |
| 3778 | * If neither SYSV nor BSD is defined, we don't implement nonblocking |
| 3779 | * waits at all. In this case, the user will not be informed when |
| 3780 | * a background process until the next time she runs a real program |
| 3781 | * (as opposed to running a builtin command or just typing return), |
| 3782 | * and the jobs command may give out of date information. |
| 3783 | */ |
| 3784 | static int |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3785 | waitproc(int wait_flags, int *status) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3786 | { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3787 | #if JOBS |
| 3788 | if (jobctl) |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3789 | wait_flags |= WUNTRACED; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3790 | #endif |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3791 | /* NB: _not_ safe_waitpid, we need to detect EINTR */ |
| 3792 | return waitpid(-1, status, wait_flags); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | /* |
| 3796 | * Wait for a process to terminate. |
| 3797 | */ |
| 3798 | static int |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3799 | dowait(int wait_flags, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3800 | { |
| 3801 | int pid; |
| 3802 | int status; |
| 3803 | struct job *jp; |
| 3804 | struct job *thisjob; |
| 3805 | int state; |
| 3806 | |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3807 | TRACE(("dowait(%d) called\n", wait_flags)); |
| 3808 | pid = waitproc(wait_flags, &status); |
| 3809 | TRACE(("wait returns pid=%d, status=%d\n", pid, status)); |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3810 | if (pid <= 0) { |
| 3811 | /* If we were doing blocking wait and (probably) got EINTR, |
| 3812 | * check for pending sigs received while waiting. |
| 3813 | * (NB: can be moved into callers if needed) */ |
| 3814 | if (wait_flags == DOWAIT_BLOCK && pendingsig) |
| 3815 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3816 | return pid; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3817 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3818 | INT_OFF; |
| 3819 | thisjob = NULL; |
| 3820 | for (jp = curjob; jp; jp = jp->prev_job) { |
| 3821 | struct procstat *sp; |
| 3822 | struct procstat *spend; |
| 3823 | if (jp->state == JOBDONE) |
| 3824 | continue; |
| 3825 | state = JOBDONE; |
| 3826 | spend = jp->ps + jp->nprocs; |
| 3827 | sp = jp->ps; |
| 3828 | do { |
| 3829 | if (sp->pid == pid) { |
| 3830 | TRACE(("Job %d: changing status of proc %d " |
| 3831 | "from 0x%x to 0x%x\n", |
| 3832 | jobno(jp), pid, sp->status, status)); |
| 3833 | sp->status = status; |
| 3834 | thisjob = jp; |
| 3835 | } |
| 3836 | if (sp->status == -1) |
| 3837 | state = JOBRUNNING; |
| 3838 | #if JOBS |
| 3839 | if (state == JOBRUNNING) |
| 3840 | continue; |
| 3841 | if (WIFSTOPPED(sp->status)) { |
| 3842 | jp->stopstatus = sp->status; |
| 3843 | state = JOBSTOPPED; |
| 3844 | } |
| 3845 | #endif |
| 3846 | } while (++sp < spend); |
| 3847 | if (thisjob) |
| 3848 | goto gotjob; |
| 3849 | } |
| 3850 | #if JOBS |
| 3851 | if (!WIFSTOPPED(status)) |
| 3852 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3853 | jobless--; |
| 3854 | goto out; |
| 3855 | |
| 3856 | gotjob: |
| 3857 | if (state != JOBRUNNING) { |
| 3858 | thisjob->changed = 1; |
| 3859 | |
| 3860 | if (thisjob->state != state) { |
| 3861 | TRACE(("Job %d: changing state from %d to %d\n", |
| 3862 | jobno(thisjob), thisjob->state, state)); |
| 3863 | thisjob->state = state; |
| 3864 | #if JOBS |
| 3865 | if (state == JOBSTOPPED) { |
| 3866 | set_curjob(thisjob, CUR_STOPPED); |
| 3867 | } |
| 3868 | #endif |
| 3869 | } |
| 3870 | } |
| 3871 | |
| 3872 | out: |
| 3873 | INT_ON; |
| 3874 | |
| 3875 | if (thisjob && thisjob == job) { |
| 3876 | char s[48 + 1]; |
| 3877 | int len; |
| 3878 | |
| 3879 | len = sprint_status(s, status, 1); |
| 3880 | if (len) { |
| 3881 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3882 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3883 | out2str(s); |
| 3884 | } |
| 3885 | } |
| 3886 | return pid; |
| 3887 | } |
| 3888 | |
| 3889 | #if JOBS |
| 3890 | static void |
| 3891 | showjob(FILE *out, struct job *jp, int mode) |
| 3892 | { |
| 3893 | struct procstat *ps; |
| 3894 | struct procstat *psend; |
| 3895 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3896 | int indent_col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3897 | char s[80]; |
| 3898 | |
| 3899 | ps = jp->ps; |
| 3900 | |
| 3901 | if (mode & SHOW_PGID) { |
| 3902 | /* just output process (group) id of pipeline */ |
| 3903 | fprintf(out, "%d\n", ps->pid); |
| 3904 | return; |
| 3905 | } |
| 3906 | |
| 3907 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3908 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3909 | |
| 3910 | if (jp == curjob) |
| 3911 | s[col - 2] = '+'; |
| 3912 | else if (curjob && jp == curjob->prev_job) |
| 3913 | s[col - 2] = '-'; |
| 3914 | |
| 3915 | if (mode & SHOW_PID) |
| 3916 | col += fmtstr(s + col, 16, "%d ", ps->pid); |
| 3917 | |
| 3918 | psend = ps + jp->nprocs; |
| 3919 | |
| 3920 | if (jp->state == JOBRUNNING) { |
| 3921 | strcpy(s + col, "Running"); |
| 3922 | col += sizeof("Running") - 1; |
| 3923 | } else { |
| 3924 | int status = psend[-1].status; |
| 3925 | if (jp->state == JOBSTOPPED) |
| 3926 | status = jp->stopstatus; |
| 3927 | col += sprint_status(s + col, status, 0); |
| 3928 | } |
| 3929 | |
| 3930 | goto start; |
| 3931 | |
| 3932 | do { |
| 3933 | /* for each process */ |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3934 | col = fmtstr(s, 48, " |\n%*c%d ", indent_col, ' ', ps->pid) - 3; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3935 | start: |
| 3936 | fprintf(out, "%s%*c%s", |
| 3937 | s, 33 - col >= 0 ? 33 - col : 0, ' ', ps->cmd |
| 3938 | ); |
| 3939 | if (!(mode & SHOW_PID)) { |
| 3940 | showpipe(jp, out); |
| 3941 | break; |
| 3942 | } |
| 3943 | if (++ps == psend) { |
| 3944 | outcslow('\n', out); |
| 3945 | break; |
| 3946 | } |
| 3947 | } while (1); |
| 3948 | |
| 3949 | jp->changed = 0; |
| 3950 | |
| 3951 | if (jp->state == JOBDONE) { |
| 3952 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 3953 | freejob(jp); |
| 3954 | } |
| 3955 | } |
| 3956 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3957 | /* |
| 3958 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 3959 | * statuses have changed since the last call to showjobs. |
| 3960 | */ |
| 3961 | static void |
| 3962 | showjobs(FILE *out, int mode) |
| 3963 | { |
| 3964 | struct job *jp; |
| 3965 | |
| 3966 | TRACE(("showjobs(%x) called\n", mode)); |
| 3967 | |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3968 | /* If not even one job changed, there is nothing to do */ |
| 3969 | while (dowait(DOWAIT_NONBLOCK, NULL) > 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3970 | continue; |
| 3971 | |
| 3972 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3973 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3974 | showjob(out, jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3975 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3976 | } |
| 3977 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3978 | |
| 3979 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3980 | jobscmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3981 | { |
| 3982 | int mode, m; |
| 3983 | |
| 3984 | mode = 0; |
| 3985 | while ((m = nextopt("lp"))) { |
| 3986 | if (m == 'l') |
| 3987 | mode = SHOW_PID; |
| 3988 | else |
| 3989 | mode = SHOW_PGID; |
| 3990 | } |
| 3991 | |
| 3992 | argv = argptr; |
| 3993 | if (*argv) { |
| 3994 | do |
| 3995 | showjob(stdout, getjob(*argv,0), mode); |
| 3996 | while (*++argv); |
| 3997 | } else |
| 3998 | showjobs(stdout, mode); |
| 3999 | |
| 4000 | return 0; |
| 4001 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4002 | #endif /* JOBS */ |
| 4003 | |
| 4004 | static int |
| 4005 | getstatus(struct job *job) |
| 4006 | { |
| 4007 | int status; |
| 4008 | int retval; |
| 4009 | |
| 4010 | status = job->ps[job->nprocs - 1].status; |
| 4011 | retval = WEXITSTATUS(status); |
| 4012 | if (!WIFEXITED(status)) { |
| 4013 | #if JOBS |
| 4014 | retval = WSTOPSIG(status); |
| 4015 | if (!WIFSTOPPED(status)) |
| 4016 | #endif |
| 4017 | { |
| 4018 | /* XXX: limits number of signals */ |
| 4019 | retval = WTERMSIG(status); |
| 4020 | #if JOBS |
| 4021 | if (retval == SIGINT) |
| 4022 | job->sigint = 1; |
| 4023 | #endif |
| 4024 | } |
| 4025 | retval += 128; |
| 4026 | } |
| 4027 | TRACE(("getstatus: job %d, nproc %d, status %x, retval %x\n", |
| 4028 | jobno(job), job->nprocs, status, retval)); |
| 4029 | return retval; |
| 4030 | } |
| 4031 | |
| 4032 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4033 | waitcmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4034 | { |
| 4035 | struct job *job; |
| 4036 | int retval; |
| 4037 | struct job *jp; |
| 4038 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4039 | // exsig++; |
| 4040 | // xbarrier(); |
| 4041 | if (pendingsig) |
| 4042 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4043 | |
| 4044 | nextopt(nullstr); |
| 4045 | retval = 0; |
| 4046 | |
| 4047 | argv = argptr; |
| 4048 | if (!*argv) { |
| 4049 | /* wait for all jobs */ |
| 4050 | for (;;) { |
| 4051 | jp = curjob; |
| 4052 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4053 | if (!jp) /* no running procs */ |
| 4054 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4055 | if (jp->state == JOBRUNNING) |
| 4056 | break; |
| 4057 | jp->waited = 1; |
| 4058 | jp = jp->prev_job; |
| 4059 | } |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4060 | dowait(DOWAIT_BLOCK, NULL); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4061 | } |
| 4062 | } |
| 4063 | |
| 4064 | retval = 127; |
| 4065 | do { |
| 4066 | if (**argv != '%') { |
| 4067 | pid_t pid = number(*argv); |
| 4068 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4069 | while (1) { |
| 4070 | if (!job) |
| 4071 | goto repeat; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4072 | if (job->ps[job->nprocs - 1].pid == pid) |
| 4073 | break; |
| 4074 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4075 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4076 | } else |
| 4077 | job = getjob(*argv, 0); |
| 4078 | /* loop until process terminated or stopped */ |
| 4079 | while (job->state == JOBRUNNING) |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4080 | dowait(DOWAIT_BLOCK, NULL); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4081 | job->waited = 1; |
| 4082 | retval = getstatus(job); |
| 4083 | repeat: |
| 4084 | ; |
| 4085 | } while (*++argv); |
| 4086 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4087 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4088 | return retval; |
| 4089 | } |
| 4090 | |
| 4091 | static struct job * |
| 4092 | growjobtab(void) |
| 4093 | { |
| 4094 | size_t len; |
| 4095 | ptrdiff_t offset; |
| 4096 | struct job *jp, *jq; |
| 4097 | |
| 4098 | len = njobs * sizeof(*jp); |
| 4099 | jq = jobtab; |
| 4100 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4101 | |
| 4102 | offset = (char *)jp - (char *)jq; |
| 4103 | if (offset) { |
| 4104 | /* Relocate pointers */ |
| 4105 | size_t l = len; |
| 4106 | |
| 4107 | jq = (struct job *)((char *)jq + l); |
| 4108 | while (l) { |
| 4109 | l -= sizeof(*jp); |
| 4110 | jq--; |
| 4111 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4112 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4113 | if (joff(jp)->ps == &jq->ps0) |
| 4114 | jmove(joff(jp)->ps); |
| 4115 | if (joff(jp)->prev_job) |
| 4116 | jmove(joff(jp)->prev_job); |
| 4117 | } |
| 4118 | if (curjob) |
| 4119 | jmove(curjob); |
| 4120 | #undef joff |
| 4121 | #undef jmove |
| 4122 | } |
| 4123 | |
| 4124 | njobs += 4; |
| 4125 | jobtab = jp; |
| 4126 | jp = (struct job *)((char *)jp + len); |
| 4127 | jq = jp + 3; |
| 4128 | do { |
| 4129 | jq->used = 0; |
| 4130 | } while (--jq >= jp); |
| 4131 | return jp; |
| 4132 | } |
| 4133 | |
| 4134 | /* |
| 4135 | * Return a new job structure. |
| 4136 | * Called with interrupts off. |
| 4137 | */ |
| 4138 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4139 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4140 | { |
| 4141 | int i; |
| 4142 | struct job *jp; |
| 4143 | |
| 4144 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4145 | if (--i < 0) { |
| 4146 | jp = growjobtab(); |
| 4147 | break; |
| 4148 | } |
| 4149 | if (jp->used == 0) |
| 4150 | break; |
| 4151 | if (jp->state != JOBDONE || !jp->waited) |
| 4152 | continue; |
| 4153 | #if JOBS |
| 4154 | if (jobctl) |
| 4155 | continue; |
| 4156 | #endif |
| 4157 | freejob(jp); |
| 4158 | break; |
| 4159 | } |
| 4160 | memset(jp, 0, sizeof(*jp)); |
| 4161 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4162 | /* jp->jobctl is a bitfield. |
| 4163 | * "jp->jobctl |= jobctl" likely to give awful code */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4164 | if (jobctl) |
| 4165 | jp->jobctl = 1; |
| 4166 | #endif |
| 4167 | jp->prev_job = curjob; |
| 4168 | curjob = jp; |
| 4169 | jp->used = 1; |
| 4170 | jp->ps = &jp->ps0; |
| 4171 | if (nprocs > 1) { |
| 4172 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4173 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4174 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4175 | jobno(jp))); |
| 4176 | return jp; |
| 4177 | } |
| 4178 | |
| 4179 | #if JOBS |
| 4180 | /* |
| 4181 | * Return a string identifying a command (to be printed by the |
| 4182 | * jobs command). |
| 4183 | */ |
| 4184 | static char *cmdnextc; |
| 4185 | |
| 4186 | static void |
| 4187 | cmdputs(const char *s) |
| 4188 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4189 | static const char vstype[VSTYPE + 1][3] = { |
| 4190 | "", "}", "-", "+", "?", "=", |
| 4191 | "%", "%%", "#", "##" |
| 4192 | USE_ASH_BASH_COMPAT(, ":", "/", "//") |
| 4193 | }; |
| 4194 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4195 | const char *p, *str; |
| 4196 | char c, cc[2] = " "; |
| 4197 | char *nextc; |
| 4198 | int subtype = 0; |
| 4199 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4200 | |
| 4201 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4202 | p = s; |
| 4203 | while ((c = *p++) != 0) { |
| 4204 | str = 0; |
| 4205 | switch (c) { |
| 4206 | case CTLESC: |
| 4207 | c = *p++; |
| 4208 | break; |
| 4209 | case CTLVAR: |
| 4210 | subtype = *p++; |
| 4211 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4212 | str = "${#"; |
| 4213 | else |
| 4214 | str = "${"; |
| 4215 | if (!(subtype & VSQUOTE) == !(quoted & 1)) |
| 4216 | goto dostr; |
| 4217 | quoted ^= 1; |
| 4218 | c = '"'; |
| 4219 | break; |
| 4220 | case CTLENDVAR: |
| 4221 | str = "\"}" + !(quoted & 1); |
| 4222 | quoted >>= 1; |
| 4223 | subtype = 0; |
| 4224 | goto dostr; |
| 4225 | case CTLBACKQ: |
| 4226 | str = "$(...)"; |
| 4227 | goto dostr; |
| 4228 | case CTLBACKQ+CTLQUOTE: |
| 4229 | str = "\"$(...)\""; |
| 4230 | goto dostr; |
| 4231 | #if ENABLE_ASH_MATH_SUPPORT |
| 4232 | case CTLARI: |
| 4233 | str = "$(("; |
| 4234 | goto dostr; |
| 4235 | case CTLENDARI: |
| 4236 | str = "))"; |
| 4237 | goto dostr; |
| 4238 | #endif |
| 4239 | case CTLQUOTEMARK: |
| 4240 | quoted ^= 1; |
| 4241 | c = '"'; |
| 4242 | break; |
| 4243 | case '=': |
| 4244 | if (subtype == 0) |
| 4245 | break; |
| 4246 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4247 | quoted <<= 1; |
| 4248 | str = vstype[subtype & VSTYPE]; |
| 4249 | if (subtype & VSNUL) |
| 4250 | c = ':'; |
| 4251 | else |
| 4252 | goto checkstr; |
| 4253 | break; |
| 4254 | case '\'': |
| 4255 | case '\\': |
| 4256 | case '"': |
| 4257 | case '$': |
| 4258 | /* These can only happen inside quotes */ |
| 4259 | cc[0] = c; |
| 4260 | str = cc; |
| 4261 | c = '\\'; |
| 4262 | break; |
| 4263 | default: |
| 4264 | break; |
| 4265 | } |
| 4266 | USTPUTC(c, nextc); |
| 4267 | checkstr: |
| 4268 | if (!str) |
| 4269 | continue; |
| 4270 | dostr: |
| 4271 | while ((c = *str++)) { |
| 4272 | USTPUTC(c, nextc); |
| 4273 | } |
| 4274 | } |
| 4275 | if (quoted & 1) { |
| 4276 | USTPUTC('"', nextc); |
| 4277 | } |
| 4278 | *nextc = 0; |
| 4279 | cmdnextc = nextc; |
| 4280 | } |
| 4281 | |
| 4282 | /* cmdtxt() and cmdlist() call each other */ |
| 4283 | static void cmdtxt(union node *n); |
| 4284 | |
| 4285 | static void |
| 4286 | cmdlist(union node *np, int sep) |
| 4287 | { |
| 4288 | for (; np; np = np->narg.next) { |
| 4289 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4290 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4291 | cmdtxt(np); |
| 4292 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4293 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4294 | } |
| 4295 | } |
| 4296 | |
| 4297 | static void |
| 4298 | cmdtxt(union node *n) |
| 4299 | { |
| 4300 | union node *np; |
| 4301 | struct nodelist *lp; |
| 4302 | const char *p; |
| 4303 | char s[2]; |
| 4304 | |
| 4305 | if (!n) |
| 4306 | return; |
| 4307 | switch (n->type) { |
| 4308 | default: |
| 4309 | #if DEBUG |
| 4310 | abort(); |
| 4311 | #endif |
| 4312 | case NPIPE: |
| 4313 | lp = n->npipe.cmdlist; |
| 4314 | for (;;) { |
| 4315 | cmdtxt(lp->n); |
| 4316 | lp = lp->next; |
| 4317 | if (!lp) |
| 4318 | break; |
| 4319 | cmdputs(" | "); |
| 4320 | } |
| 4321 | break; |
| 4322 | case NSEMI: |
| 4323 | p = "; "; |
| 4324 | goto binop; |
| 4325 | case NAND: |
| 4326 | p = " && "; |
| 4327 | goto binop; |
| 4328 | case NOR: |
| 4329 | p = " || "; |
| 4330 | binop: |
| 4331 | cmdtxt(n->nbinary.ch1); |
| 4332 | cmdputs(p); |
| 4333 | n = n->nbinary.ch2; |
| 4334 | goto donode; |
| 4335 | case NREDIR: |
| 4336 | case NBACKGND: |
| 4337 | n = n->nredir.n; |
| 4338 | goto donode; |
| 4339 | case NNOT: |
| 4340 | cmdputs("!"); |
| 4341 | n = n->nnot.com; |
| 4342 | donode: |
| 4343 | cmdtxt(n); |
| 4344 | break; |
| 4345 | case NIF: |
| 4346 | cmdputs("if "); |
| 4347 | cmdtxt(n->nif.test); |
| 4348 | cmdputs("; then "); |
| 4349 | n = n->nif.ifpart; |
| 4350 | if (n->nif.elsepart) { |
| 4351 | cmdtxt(n); |
| 4352 | cmdputs("; else "); |
| 4353 | n = n->nif.elsepart; |
| 4354 | } |
| 4355 | p = "; fi"; |
| 4356 | goto dotail; |
| 4357 | case NSUBSHELL: |
| 4358 | cmdputs("("); |
| 4359 | n = n->nredir.n; |
| 4360 | p = ")"; |
| 4361 | goto dotail; |
| 4362 | case NWHILE: |
| 4363 | p = "while "; |
| 4364 | goto until; |
| 4365 | case NUNTIL: |
| 4366 | p = "until "; |
| 4367 | until: |
| 4368 | cmdputs(p); |
| 4369 | cmdtxt(n->nbinary.ch1); |
| 4370 | n = n->nbinary.ch2; |
| 4371 | p = "; done"; |
| 4372 | dodo: |
| 4373 | cmdputs("; do "); |
| 4374 | dotail: |
| 4375 | cmdtxt(n); |
| 4376 | goto dotail2; |
| 4377 | case NFOR: |
| 4378 | cmdputs("for "); |
| 4379 | cmdputs(n->nfor.var); |
| 4380 | cmdputs(" in "); |
| 4381 | cmdlist(n->nfor.args, 1); |
| 4382 | n = n->nfor.body; |
| 4383 | p = "; done"; |
| 4384 | goto dodo; |
| 4385 | case NDEFUN: |
| 4386 | cmdputs(n->narg.text); |
| 4387 | p = "() { ... }"; |
| 4388 | goto dotail2; |
| 4389 | case NCMD: |
| 4390 | cmdlist(n->ncmd.args, 1); |
| 4391 | cmdlist(n->ncmd.redirect, 0); |
| 4392 | break; |
| 4393 | case NARG: |
| 4394 | p = n->narg.text; |
| 4395 | dotail2: |
| 4396 | cmdputs(p); |
| 4397 | break; |
| 4398 | case NHERE: |
| 4399 | case NXHERE: |
| 4400 | p = "<<..."; |
| 4401 | goto dotail2; |
| 4402 | case NCASE: |
| 4403 | cmdputs("case "); |
| 4404 | cmdputs(n->ncase.expr->narg.text); |
| 4405 | cmdputs(" in "); |
| 4406 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 4407 | cmdtxt(np->nclist.pattern); |
| 4408 | cmdputs(") "); |
| 4409 | cmdtxt(np->nclist.body); |
| 4410 | cmdputs(";; "); |
| 4411 | } |
| 4412 | p = "esac"; |
| 4413 | goto dotail2; |
| 4414 | case NTO: |
| 4415 | p = ">"; |
| 4416 | goto redir; |
| 4417 | case NCLOBBER: |
| 4418 | p = ">|"; |
| 4419 | goto redir; |
| 4420 | case NAPPEND: |
| 4421 | p = ">>"; |
| 4422 | goto redir; |
| 4423 | case NTOFD: |
| 4424 | p = ">&"; |
| 4425 | goto redir; |
| 4426 | case NFROM: |
| 4427 | p = "<"; |
| 4428 | goto redir; |
| 4429 | case NFROMFD: |
| 4430 | p = "<&"; |
| 4431 | goto redir; |
| 4432 | case NFROMTO: |
| 4433 | p = "<>"; |
| 4434 | redir: |
| 4435 | s[0] = n->nfile.fd + '0'; |
| 4436 | s[1] = '\0'; |
| 4437 | cmdputs(s); |
| 4438 | cmdputs(p); |
| 4439 | if (n->type == NTOFD || n->type == NFROMFD) { |
| 4440 | s[0] = n->ndup.dupfd + '0'; |
| 4441 | p = s; |
| 4442 | goto dotail2; |
| 4443 | } |
| 4444 | n = n->nfile.fname; |
| 4445 | goto donode; |
| 4446 | } |
| 4447 | } |
| 4448 | |
| 4449 | static char * |
| 4450 | commandtext(union node *n) |
| 4451 | { |
| 4452 | char *name; |
| 4453 | |
| 4454 | STARTSTACKSTR(cmdnextc); |
| 4455 | cmdtxt(n); |
| 4456 | name = stackblock(); |
| 4457 | TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n", |
| 4458 | name, cmdnextc, cmdnextc)); |
| 4459 | return ckstrdup(name); |
| 4460 | } |
| 4461 | #endif /* JOBS */ |
| 4462 | |
| 4463 | /* |
| 4464 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 4465 | * own process group. Jp is a job structure that the job is to be added to. |
| 4466 | * N is the command that will be evaluated by the child. Both jp and n may |
| 4467 | * be NULL. The mode parameter can be one of the following: |
| 4468 | * FORK_FG - Fork off a foreground process. |
| 4469 | * FORK_BG - Fork off a background process. |
| 4470 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 4471 | * process group even if job control is on. |
| 4472 | * |
| 4473 | * When job control is turned off, background processes have their standard |
| 4474 | * input redirected to /dev/null (except for the second and later processes |
| 4475 | * in a pipeline). |
| 4476 | * |
| 4477 | * Called with interrupts off. |
| 4478 | */ |
| 4479 | /* |
| 4480 | * Clear traps on a fork. |
| 4481 | */ |
| 4482 | static void |
| 4483 | clear_traps(void) |
| 4484 | { |
| 4485 | char **tp; |
| 4486 | |
| 4487 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4488 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4489 | INT_OFF; |
| 4490 | free(*tp); |
| 4491 | *tp = NULL; |
| 4492 | if (tp != &trap[0]) |
| 4493 | setsignal(tp - trap); |
| 4494 | INT_ON; |
| 4495 | } |
| 4496 | } |
| 4497 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4498 | |
| 4499 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4500 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 4501 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4502 | /* Called after fork(), in child */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4503 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4504 | forkchild(struct job *jp, /*union node *n,*/ int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4505 | { |
| 4506 | int oldlvl; |
| 4507 | |
| 4508 | TRACE(("Child shell %d\n", getpid())); |
| 4509 | oldlvl = shlvl; |
| 4510 | shlvl++; |
| 4511 | |
| 4512 | closescript(); |
| 4513 | clear_traps(); |
| 4514 | #if JOBS |
| 4515 | /* do job control only in root shell */ |
| 4516 | jobctl = 0; |
| 4517 | if (mode != FORK_NOJOB && jp->jobctl && !oldlvl) { |
| 4518 | pid_t pgrp; |
| 4519 | |
| 4520 | if (jp->nprocs == 0) |
| 4521 | pgrp = getpid(); |
| 4522 | else |
| 4523 | pgrp = jp->ps[0].pid; |
| 4524 | /* This can fail because we are doing it in the parent also */ |
| 4525 | (void)setpgid(0, pgrp); |
| 4526 | if (mode == FORK_FG) |
| 4527 | xtcsetpgrp(ttyfd, pgrp); |
| 4528 | setsignal(SIGTSTP); |
| 4529 | setsignal(SIGTTOU); |
| 4530 | } else |
| 4531 | #endif |
| 4532 | if (mode == FORK_BG) { |
| 4533 | ignoresig(SIGINT); |
| 4534 | ignoresig(SIGQUIT); |
| 4535 | if (jp->nprocs == 0) { |
| 4536 | close(0); |
| 4537 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 4538 | ash_msg_and_raise_error("can't open %s", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4539 | } |
| 4540 | } |
| 4541 | if (!oldlvl && iflag) { |
| 4542 | setsignal(SIGINT); |
| 4543 | setsignal(SIGQUIT); |
| 4544 | setsignal(SIGTERM); |
| 4545 | } |
| 4546 | for (jp = curjob; jp; jp = jp->prev_job) |
| 4547 | freejob(jp); |
| 4548 | jobless = 0; |
| 4549 | } |
| 4550 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4551 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 4552 | #if !JOBS |
| 4553 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 4554 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4555 | static void |
| 4556 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 4557 | { |
| 4558 | TRACE(("In parent shell: child = %d\n", pid)); |
| 4559 | if (!jp) { |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4560 | while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0) |
| 4561 | continue; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4562 | jobless++; |
| 4563 | return; |
| 4564 | } |
| 4565 | #if JOBS |
| 4566 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 4567 | int pgrp; |
| 4568 | |
| 4569 | if (jp->nprocs == 0) |
| 4570 | pgrp = pid; |
| 4571 | else |
| 4572 | pgrp = jp->ps[0].pid; |
| 4573 | /* This can fail because we are doing it in the child also */ |
| 4574 | setpgid(pid, pgrp); |
| 4575 | } |
| 4576 | #endif |
| 4577 | if (mode == FORK_BG) { |
| 4578 | backgndpid = pid; /* set $! */ |
| 4579 | set_curjob(jp, CUR_RUNNING); |
| 4580 | } |
| 4581 | if (jp) { |
| 4582 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
| 4583 | ps->pid = pid; |
| 4584 | ps->status = -1; |
| 4585 | ps->cmd = nullstr; |
| 4586 | #if JOBS |
| 4587 | if (jobctl && n) |
| 4588 | ps->cmd = commandtext(n); |
| 4589 | #endif |
| 4590 | } |
| 4591 | } |
| 4592 | |
| 4593 | static int |
| 4594 | forkshell(struct job *jp, union node *n, int mode) |
| 4595 | { |
| 4596 | int pid; |
| 4597 | |
| 4598 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 4599 | pid = fork(); |
| 4600 | if (pid < 0) { |
| 4601 | TRACE(("Fork failed, errno=%d", errno)); |
| 4602 | if (jp) |
| 4603 | freejob(jp); |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 4604 | ash_msg_and_raise_error("cannot fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4605 | } |
| 4606 | if (pid == 0) |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4607 | forkchild(jp, /*n,*/ mode); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4608 | else |
| 4609 | forkparent(jp, n, mode, pid); |
| 4610 | return pid; |
| 4611 | } |
| 4612 | |
| 4613 | /* |
| 4614 | * Wait for job to finish. |
| 4615 | * |
| 4616 | * Under job control we have the problem that while a child process is |
| 4617 | * running interrupts generated by the user are sent to the child but not |
| 4618 | * to the shell. This means that an infinite loop started by an inter- |
| 4619 | * active user may be hard to kill. With job control turned off, an |
| 4620 | * interactive user may place an interactive program inside a loop. If |
| 4621 | * the interactive program catches interrupts, the user doesn't want |
| 4622 | * these interrupts to also abort the loop. The approach we take here |
| 4623 | * is to have the shell ignore interrupt signals while waiting for a |
| 4624 | * foreground process to terminate, and then send itself an interrupt |
| 4625 | * signal if the child process was terminated by an interrupt signal. |
| 4626 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 4627 | * exit on interrupt; unless these processes terminate themselves by |
| 4628 | * sending a signal to themselves (instead of calling exit) they will |
| 4629 | * confuse this approach. |
| 4630 | * |
| 4631 | * Called with interrupts off. |
| 4632 | */ |
| 4633 | static int |
| 4634 | waitforjob(struct job *jp) |
| 4635 | { |
| 4636 | int st; |
| 4637 | |
| 4638 | TRACE(("waitforjob(%%%d) called\n", jobno(jp))); |
| 4639 | while (jp->state == JOBRUNNING) { |
| 4640 | dowait(DOWAIT_BLOCK, jp); |
| 4641 | } |
| 4642 | st = getstatus(jp); |
| 4643 | #if JOBS |
| 4644 | if (jp->jobctl) { |
| 4645 | xtcsetpgrp(ttyfd, rootpid); |
| 4646 | /* |
| 4647 | * This is truly gross. |
| 4648 | * If we're doing job control, then we did a TIOCSPGRP which |
| 4649 | * caused us (the shell) to no longer be in the controlling |
| 4650 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 4651 | * intuit from the subprocess exit status whether a SIGINT |
| 4652 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 4653 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4654 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 4655 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4656 | } |
| 4657 | if (jp->state == JOBDONE) |
| 4658 | #endif |
| 4659 | freejob(jp); |
| 4660 | return st; |
| 4661 | } |
| 4662 | |
| 4663 | /* |
| 4664 | * return 1 if there are stopped jobs, otherwise 0 |
| 4665 | */ |
| 4666 | static int |
| 4667 | stoppedjobs(void) |
| 4668 | { |
| 4669 | struct job *jp; |
| 4670 | int retval; |
| 4671 | |
| 4672 | retval = 0; |
| 4673 | if (job_warning) |
| 4674 | goto out; |
| 4675 | jp = curjob; |
| 4676 | if (jp && jp->state == JOBSTOPPED) { |
| 4677 | out2str("You have stopped jobs.\n"); |
| 4678 | job_warning = 2; |
| 4679 | retval++; |
| 4680 | } |
| 4681 | out: |
| 4682 | return retval; |
| 4683 | } |
| 4684 | |
| 4685 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4686 | /* ============ redir.c |
| 4687 | * |
| 4688 | * Code for dealing with input/output redirection. |
| 4689 | */ |
| 4690 | |
| 4691 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4692 | #define CLOSED -3 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4693 | #ifndef PIPE_BUF |
| 4694 | # define PIPESIZE 4096 /* amount of buffering in a pipe */ |
| 4695 | #else |
| 4696 | # define PIPESIZE PIPE_BUF |
| 4697 | #endif |
| 4698 | |
| 4699 | /* |
| 4700 | * Open a file in noclobber mode. |
| 4701 | * The code was copied from bash. |
| 4702 | */ |
| 4703 | static int |
| 4704 | noclobberopen(const char *fname) |
| 4705 | { |
| 4706 | int r, fd; |
| 4707 | struct stat finfo, finfo2; |
| 4708 | |
| 4709 | /* |
| 4710 | * If the file exists and is a regular file, return an error |
| 4711 | * immediately. |
| 4712 | */ |
| 4713 | r = stat(fname, &finfo); |
| 4714 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 4715 | errno = EEXIST; |
| 4716 | return -1; |
| 4717 | } |
| 4718 | |
| 4719 | /* |
| 4720 | * If the file was not present (r != 0), make sure we open it |
| 4721 | * exclusively so that if it is created before we open it, our open |
| 4722 | * will fail. Make sure that we do not truncate an existing file. |
| 4723 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 4724 | * file was not a regular file, we leave O_EXCL off. |
| 4725 | */ |
| 4726 | if (r != 0) |
| 4727 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 4728 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
| 4729 | |
| 4730 | /* If the open failed, return the file descriptor right away. */ |
| 4731 | if (fd < 0) |
| 4732 | return fd; |
| 4733 | |
| 4734 | /* |
| 4735 | * OK, the open succeeded, but the file may have been changed from a |
| 4736 | * non-regular file to a regular file between the stat and the open. |
| 4737 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 4738 | * did not exist and is symlinked to an existing file between the stat |
| 4739 | * and open. |
| 4740 | */ |
| 4741 | |
| 4742 | /* |
| 4743 | * If we can open it and fstat the file descriptor, and neither check |
| 4744 | * revealed that it was a regular file, and the file has not been |
| 4745 | * replaced, return the file descriptor. |
| 4746 | */ |
| 4747 | if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode) |
| 4748 | && finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino) |
| 4749 | return fd; |
| 4750 | |
| 4751 | /* The file has been replaced. badness. */ |
| 4752 | close(fd); |
| 4753 | errno = EEXIST; |
| 4754 | return -1; |
| 4755 | } |
| 4756 | |
| 4757 | /* |
| 4758 | * Handle here documents. Normally we fork off a process to write the |
| 4759 | * data to a pipe. If the document is short, we can stuff the data in |
| 4760 | * the pipe without forking. |
| 4761 | */ |
| 4762 | /* openhere needs this forward reference */ |
| 4763 | static void expandhere(union node *arg, int fd); |
| 4764 | static int |
| 4765 | openhere(union node *redir) |
| 4766 | { |
| 4767 | int pip[2]; |
| 4768 | size_t len = 0; |
| 4769 | |
| 4770 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 4771 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4772 | if (redir->type == NHERE) { |
| 4773 | len = strlen(redir->nhere.doc->narg.text); |
| 4774 | if (len <= PIPESIZE) { |
| 4775 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
| 4776 | goto out; |
| 4777 | } |
| 4778 | } |
| 4779 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
| 4780 | close(pip[0]); |
| 4781 | signal(SIGINT, SIG_IGN); |
| 4782 | signal(SIGQUIT, SIG_IGN); |
| 4783 | signal(SIGHUP, SIG_IGN); |
| 4784 | #ifdef SIGTSTP |
| 4785 | signal(SIGTSTP, SIG_IGN); |
| 4786 | #endif |
| 4787 | signal(SIGPIPE, SIG_DFL); |
| 4788 | if (redir->type == NHERE) |
| 4789 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
| 4790 | else |
| 4791 | expandhere(redir->nhere.doc, pip[1]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 4792 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4793 | } |
| 4794 | out: |
| 4795 | close(pip[1]); |
| 4796 | return pip[0]; |
| 4797 | } |
| 4798 | |
| 4799 | static int |
| 4800 | openredirect(union node *redir) |
| 4801 | { |
| 4802 | char *fname; |
| 4803 | int f; |
| 4804 | |
| 4805 | switch (redir->nfile.type) { |
| 4806 | case NFROM: |
| 4807 | fname = redir->nfile.expfname; |
| 4808 | f = open(fname, O_RDONLY); |
| 4809 | if (f < 0) |
| 4810 | goto eopen; |
| 4811 | break; |
| 4812 | case NFROMTO: |
| 4813 | fname = redir->nfile.expfname; |
| 4814 | f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666); |
| 4815 | if (f < 0) |
| 4816 | goto ecreate; |
| 4817 | break; |
| 4818 | case NTO: |
| 4819 | /* Take care of noclobber mode. */ |
| 4820 | if (Cflag) { |
| 4821 | fname = redir->nfile.expfname; |
| 4822 | f = noclobberopen(fname); |
| 4823 | if (f < 0) |
| 4824 | goto ecreate; |
| 4825 | break; |
| 4826 | } |
| 4827 | /* FALLTHROUGH */ |
| 4828 | case NCLOBBER: |
| 4829 | fname = redir->nfile.expfname; |
| 4830 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 4831 | if (f < 0) |
| 4832 | goto ecreate; |
| 4833 | break; |
| 4834 | case NAPPEND: |
| 4835 | fname = redir->nfile.expfname; |
| 4836 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 4837 | if (f < 0) |
| 4838 | goto ecreate; |
| 4839 | break; |
| 4840 | default: |
| 4841 | #if DEBUG |
| 4842 | abort(); |
| 4843 | #endif |
| 4844 | /* Fall through to eliminate warning. */ |
| 4845 | case NTOFD: |
| 4846 | case NFROMFD: |
| 4847 | f = -1; |
| 4848 | break; |
| 4849 | case NHERE: |
| 4850 | case NXHERE: |
| 4851 | f = openhere(redir); |
| 4852 | break; |
| 4853 | } |
| 4854 | |
| 4855 | return f; |
| 4856 | ecreate: |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 4857 | ash_msg_and_raise_error("cannot create %s: %s", fname, errmsg(errno, "nonexistent directory")); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4858 | eopen: |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 4859 | ash_msg_and_raise_error("cannot open %s: %s", fname, errmsg(errno, "no such file")); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | /* |
| 4863 | * Copy a file descriptor to be >= to. Returns -1 |
| 4864 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 4865 | * file descriptors left. |
| 4866 | */ |
| 4867 | static int |
| 4868 | copyfd(int from, int to) |
| 4869 | { |
| 4870 | int newfd; |
| 4871 | |
| 4872 | newfd = fcntl(from, F_DUPFD, to); |
| 4873 | if (newfd < 0) { |
| 4874 | if (errno == EMFILE) |
| 4875 | return EMPTY; |
| 4876 | ash_msg_and_raise_error("%d: %m", from); |
| 4877 | } |
| 4878 | return newfd; |
| 4879 | } |
| 4880 | |
| 4881 | static void |
| 4882 | dupredirect(union node *redir, int f) |
| 4883 | { |
| 4884 | int fd = redir->nfile.fd; |
| 4885 | |
| 4886 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
| 4887 | if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ |
| 4888 | copyfd(redir->ndup.dupfd, fd); |
| 4889 | } |
| 4890 | return; |
| 4891 | } |
| 4892 | |
| 4893 | if (f != fd) { |
| 4894 | copyfd(f, fd); |
| 4895 | close(f); |
| 4896 | } |
| 4897 | } |
| 4898 | |
| 4899 | /* |
| 4900 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 4901 | * old file descriptors are stashed away so that the redirection can be |
| 4902 | * undone by calling popredir. If the REDIR_BACKQ flag is set, then the |
| 4903 | * standard output, and the standard error if it becomes a duplicate of |
| 4904 | * stdout, is saved in memory. |
| 4905 | */ |
| 4906 | /* flags passed to redirect */ |
| 4907 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
| 4908 | #define REDIR_SAVEFD2 03 /* set preverrout */ |
| 4909 | static void |
| 4910 | redirect(union node *redir, int flags) |
| 4911 | { |
| 4912 | union node *n; |
| 4913 | struct redirtab *sv; |
| 4914 | int i; |
| 4915 | int fd; |
| 4916 | int newfd; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4917 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 4918 | g_nullredirs++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4919 | if (!redir) { |
| 4920 | return; |
| 4921 | } |
| 4922 | sv = NULL; |
| 4923 | INT_OFF; |
| 4924 | if (flags & REDIR_PUSH) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4925 | sv = ckmalloc(sizeof(*sv)); |
| 4926 | sv->next = redirlist; |
| 4927 | redirlist = sv; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 4928 | sv->nullredirs = g_nullredirs - 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4929 | for (i = 0; i < 10; i++) |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4930 | sv->renamed[i] = EMPTY; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 4931 | g_nullredirs = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4932 | } |
| 4933 | n = redir; |
| 4934 | do { |
| 4935 | fd = n->nfile.fd; |
| 4936 | if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) |
| 4937 | && n->ndup.dupfd == fd) |
| 4938 | continue; /* redirect from/to same file descriptor */ |
| 4939 | |
| 4940 | newfd = openredirect(n); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4941 | if (fd == newfd) { |
| 4942 | /* Descriptor wasn't open before redirect. |
| 4943 | * Mark it for close in the future */ |
| 4944 | if (sv && sv->renamed[fd] == EMPTY) |
| 4945 | sv->renamed[fd] = CLOSED; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4946 | continue; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4947 | } |
| 4948 | if (sv && sv->renamed[fd] == EMPTY) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4949 | i = fcntl(fd, F_DUPFD, 10); |
| 4950 | |
| 4951 | if (i == -1) { |
| 4952 | i = errno; |
| 4953 | if (i != EBADF) { |
| 4954 | close(newfd); |
| 4955 | errno = i; |
| 4956 | ash_msg_and_raise_error("%d: %m", fd); |
| 4957 | /* NOTREACHED */ |
| 4958 | } |
| 4959 | } else { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4960 | sv->renamed[fd] = i; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4961 | close(fd); |
| 4962 | } |
| 4963 | } else { |
| 4964 | close(fd); |
| 4965 | } |
| 4966 | dupredirect(n, newfd); |
| 4967 | } while ((n = n->nfile.next)); |
| 4968 | INT_ON; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4969 | if ((flags & REDIR_SAVEFD2) && sv && sv->renamed[2] >= 0) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4970 | preverrout_fd = sv->renamed[2]; |
| 4971 | } |
| 4972 | |
| 4973 | /* |
| 4974 | * Undo the effects of the last redirection. |
| 4975 | */ |
| 4976 | static void |
| 4977 | popredir(int drop) |
| 4978 | { |
| 4979 | struct redirtab *rp; |
| 4980 | int i; |
| 4981 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 4982 | if (--g_nullredirs >= 0) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4983 | return; |
| 4984 | INT_OFF; |
| 4985 | rp = redirlist; |
| 4986 | for (i = 0; i < 10; i++) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4987 | if (rp->renamed[i] == CLOSED) { |
| 4988 | if (!drop) |
| 4989 | close(i); |
| 4990 | continue; |
| 4991 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4992 | if (rp->renamed[i] != EMPTY) { |
| 4993 | if (!drop) { |
| 4994 | close(i); |
| 4995 | copyfd(rp->renamed[i], i); |
| 4996 | } |
| 4997 | close(rp->renamed[i]); |
| 4998 | } |
| 4999 | } |
| 5000 | redirlist = rp->next; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5001 | g_nullredirs = rp->nullredirs; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5002 | free(rp); |
| 5003 | INT_ON; |
| 5004 | } |
| 5005 | |
| 5006 | /* |
| 5007 | * Undo all redirections. Called on error or interrupt. |
| 5008 | */ |
| 5009 | |
| 5010 | /* |
| 5011 | * Discard all saved file descriptors. |
| 5012 | */ |
| 5013 | static void |
| 5014 | clearredir(int drop) |
| 5015 | { |
| 5016 | for (;;) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5017 | g_nullredirs = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5018 | if (!redirlist) |
| 5019 | break; |
| 5020 | popredir(drop); |
| 5021 | } |
| 5022 | } |
| 5023 | |
| 5024 | static int |
| 5025 | redirectsafe(union node *redir, int flags) |
| 5026 | { |
| 5027 | int err; |
| 5028 | volatile int saveint; |
| 5029 | struct jmploc *volatile savehandler = exception_handler; |
| 5030 | struct jmploc jmploc; |
| 5031 | |
| 5032 | SAVE_INT(saveint); |
| 5033 | err = setjmp(jmploc.loc) * 2; |
| 5034 | if (!err) { |
| 5035 | exception_handler = &jmploc; |
| 5036 | redirect(redir, flags); |
| 5037 | } |
| 5038 | exception_handler = savehandler; |
| 5039 | if (err && exception != EXERROR) |
| 5040 | longjmp(exception_handler->loc, 1); |
| 5041 | RESTORE_INT(saveint); |
| 5042 | return err; |
| 5043 | } |
| 5044 | |
| 5045 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5046 | /* ============ Routines to expand arguments to commands |
| 5047 | * |
| 5048 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 5049 | */ |
| 5050 | |
| 5051 | /* |
| 5052 | * expandarg flags |
| 5053 | */ |
| 5054 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 5055 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 5056 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 5057 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 5058 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
| 5059 | #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */ |
| 5060 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ |
| 5061 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ |
| 5062 | #define EXP_QWORD 0x100 /* expand word in quoted parameter expansion */ |
| 5063 | /* |
| 5064 | * _rmescape() flags |
| 5065 | */ |
| 5066 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 5067 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
| 5068 | #define RMESCAPE_QUOTED 0x4 /* Remove CTLESC unless in quotes */ |
| 5069 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 5070 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
| 5071 | |
| 5072 | /* |
| 5073 | * Structure specifying which parts of the string should be searched |
| 5074 | * for IFS characters. |
| 5075 | */ |
| 5076 | struct ifsregion { |
| 5077 | struct ifsregion *next; /* next region in list */ |
| 5078 | int begoff; /* offset of start of region */ |
| 5079 | int endoff; /* offset of end of region */ |
| 5080 | int nulonly; /* search for nul bytes only */ |
| 5081 | }; |
| 5082 | |
| 5083 | struct arglist { |
| 5084 | struct strlist *list; |
| 5085 | struct strlist **lastp; |
| 5086 | }; |
| 5087 | |
| 5088 | /* output of current string */ |
| 5089 | static char *expdest; |
| 5090 | /* list of back quote expressions */ |
| 5091 | static struct nodelist *argbackq; |
| 5092 | /* first struct in list of ifs regions */ |
| 5093 | static struct ifsregion ifsfirst; |
| 5094 | /* last struct in list */ |
| 5095 | static struct ifsregion *ifslastp; |
| 5096 | /* holds expanded arg list */ |
| 5097 | static struct arglist exparg; |
| 5098 | |
| 5099 | /* |
| 5100 | * Our own itoa(). |
| 5101 | */ |
| 5102 | static int |
| 5103 | cvtnum(arith_t num) |
| 5104 | { |
| 5105 | int len; |
| 5106 | |
| 5107 | expdest = makestrspace(32, expdest); |
| 5108 | #if ENABLE_ASH_MATH_SUPPORT_64 |
| 5109 | len = fmtstr(expdest, 32, "%lld", (long long) num); |
| 5110 | #else |
| 5111 | len = fmtstr(expdest, 32, "%ld", num); |
| 5112 | #endif |
| 5113 | STADJUST(len, expdest); |
| 5114 | return len; |
| 5115 | } |
| 5116 | |
| 5117 | static size_t |
| 5118 | esclen(const char *start, const char *p) |
| 5119 | { |
| 5120 | size_t esc = 0; |
| 5121 | |
| 5122 | while (p > start && *--p == CTLESC) { |
| 5123 | esc++; |
| 5124 | } |
| 5125 | return esc; |
| 5126 | } |
| 5127 | |
| 5128 | /* |
| 5129 | * Remove any CTLESC characters from a string. |
| 5130 | */ |
| 5131 | static char * |
| 5132 | _rmescapes(char *str, int flag) |
| 5133 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5134 | static const char qchars[] ALIGN1 = { CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 5135 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5136 | char *p, *q, *r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5137 | unsigned inquotes; |
| 5138 | int notescaped; |
| 5139 | int globbing; |
| 5140 | |
| 5141 | p = strpbrk(str, qchars); |
| 5142 | if (!p) { |
| 5143 | return str; |
| 5144 | } |
| 5145 | q = p; |
| 5146 | r = str; |
| 5147 | if (flag & RMESCAPE_ALLOC) { |
| 5148 | size_t len = p - str; |
| 5149 | size_t fulllen = len + strlen(p) + 1; |
| 5150 | |
| 5151 | if (flag & RMESCAPE_GROW) { |
| 5152 | r = makestrspace(fulllen, expdest); |
| 5153 | } else if (flag & RMESCAPE_HEAP) { |
| 5154 | r = ckmalloc(fulllen); |
| 5155 | } else { |
| 5156 | r = stalloc(fulllen); |
| 5157 | } |
| 5158 | q = r; |
| 5159 | if (len > 0) { |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5160 | q = (char *)memcpy(q, str, len) + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5161 | } |
| 5162 | } |
| 5163 | inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED; |
| 5164 | globbing = flag & RMESCAPE_GLOB; |
| 5165 | notescaped = globbing; |
| 5166 | while (*p) { |
| 5167 | if (*p == CTLQUOTEMARK) { |
| 5168 | inquotes = ~inquotes; |
| 5169 | p++; |
| 5170 | notescaped = globbing; |
| 5171 | continue; |
| 5172 | } |
| 5173 | if (*p == '\\') { |
| 5174 | /* naked back slash */ |
| 5175 | notescaped = 0; |
| 5176 | goto copy; |
| 5177 | } |
| 5178 | if (*p == CTLESC) { |
| 5179 | p++; |
| 5180 | if (notescaped && inquotes && *p != '/') { |
| 5181 | *q++ = '\\'; |
| 5182 | } |
| 5183 | } |
| 5184 | notescaped = globbing; |
| 5185 | copy: |
| 5186 | *q++ = *p++; |
| 5187 | } |
| 5188 | *q = '\0'; |
| 5189 | if (flag & RMESCAPE_GROW) { |
| 5190 | expdest = r; |
| 5191 | STADJUST(q - r + 1, expdest); |
| 5192 | } |
| 5193 | return r; |
| 5194 | } |
| 5195 | #define rmescapes(p) _rmescapes((p), 0) |
| 5196 | |
| 5197 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 5198 | |
| 5199 | /* |
| 5200 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 5201 | * |
| 5202 | * Returns an stalloced string. |
| 5203 | */ |
| 5204 | static char * |
| 5205 | preglob(const char *pattern, int quoted, int flag) |
| 5206 | { |
| 5207 | flag |= RMESCAPE_GLOB; |
| 5208 | if (quoted) { |
| 5209 | flag |= RMESCAPE_QUOTED; |
| 5210 | } |
| 5211 | return _rmescapes((char *)pattern, flag); |
| 5212 | } |
| 5213 | |
| 5214 | /* |
| 5215 | * Put a string on the stack. |
| 5216 | */ |
| 5217 | static void |
| 5218 | memtodest(const char *p, size_t len, int syntax, int quotes) |
| 5219 | { |
| 5220 | char *q = expdest; |
| 5221 | |
| 5222 | q = makestrspace(len * 2, q); |
| 5223 | |
| 5224 | while (len--) { |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 5225 | int c = signed_char2int(*p++); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5226 | if (!c) |
| 5227 | continue; |
| 5228 | if (quotes && (SIT(c, syntax) == CCTL || SIT(c, syntax) == CBACK)) |
| 5229 | USTPUTC(CTLESC, q); |
| 5230 | USTPUTC(c, q); |
| 5231 | } |
| 5232 | |
| 5233 | expdest = q; |
| 5234 | } |
| 5235 | |
| 5236 | static void |
| 5237 | strtodest(const char *p, int syntax, int quotes) |
| 5238 | { |
| 5239 | memtodest(p, strlen(p), syntax, quotes); |
| 5240 | } |
| 5241 | |
| 5242 | /* |
| 5243 | * Record the fact that we have to scan this region of the |
| 5244 | * string for IFS characters. |
| 5245 | */ |
| 5246 | static void |
| 5247 | recordregion(int start, int end, int nulonly) |
| 5248 | { |
| 5249 | struct ifsregion *ifsp; |
| 5250 | |
| 5251 | if (ifslastp == NULL) { |
| 5252 | ifsp = &ifsfirst; |
| 5253 | } else { |
| 5254 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 5255 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 5256 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5257 | ifslastp->next = ifsp; |
| 5258 | INT_ON; |
| 5259 | } |
| 5260 | ifslastp = ifsp; |
| 5261 | ifslastp->begoff = start; |
| 5262 | ifslastp->endoff = end; |
| 5263 | ifslastp->nulonly = nulonly; |
| 5264 | } |
| 5265 | |
| 5266 | static void |
| 5267 | removerecordregions(int endoff) |
| 5268 | { |
| 5269 | if (ifslastp == NULL) |
| 5270 | return; |
| 5271 | |
| 5272 | if (ifsfirst.endoff > endoff) { |
| 5273 | while (ifsfirst.next != NULL) { |
| 5274 | struct ifsregion *ifsp; |
| 5275 | INT_OFF; |
| 5276 | ifsp = ifsfirst.next->next; |
| 5277 | free(ifsfirst.next); |
| 5278 | ifsfirst.next = ifsp; |
| 5279 | INT_ON; |
| 5280 | } |
| 5281 | if (ifsfirst.begoff > endoff) |
| 5282 | ifslastp = NULL; |
| 5283 | else { |
| 5284 | ifslastp = &ifsfirst; |
| 5285 | ifsfirst.endoff = endoff; |
| 5286 | } |
| 5287 | return; |
| 5288 | } |
| 5289 | |
| 5290 | ifslastp = &ifsfirst; |
| 5291 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
| 5292 | ifslastp=ifslastp->next; |
| 5293 | while (ifslastp->next != NULL) { |
| 5294 | struct ifsregion *ifsp; |
| 5295 | INT_OFF; |
| 5296 | ifsp = ifslastp->next->next; |
| 5297 | free(ifslastp->next); |
| 5298 | ifslastp->next = ifsp; |
| 5299 | INT_ON; |
| 5300 | } |
| 5301 | if (ifslastp->endoff > endoff) |
| 5302 | ifslastp->endoff = endoff; |
| 5303 | } |
| 5304 | |
| 5305 | static char * |
| 5306 | exptilde(char *startp, char *p, int flag) |
| 5307 | { |
| 5308 | char c; |
| 5309 | char *name; |
| 5310 | struct passwd *pw; |
| 5311 | const char *home; |
| 5312 | int quotes = flag & (EXP_FULL | EXP_CASE); |
| 5313 | int startloc; |
| 5314 | |
| 5315 | name = p + 1; |
| 5316 | |
| 5317 | while ((c = *++p) != '\0') { |
| 5318 | switch (c) { |
| 5319 | case CTLESC: |
| 5320 | return startp; |
| 5321 | case CTLQUOTEMARK: |
| 5322 | return startp; |
| 5323 | case ':': |
| 5324 | if (flag & EXP_VARTILDE) |
| 5325 | goto done; |
| 5326 | break; |
| 5327 | case '/': |
| 5328 | case CTLENDVAR: |
| 5329 | goto done; |
| 5330 | } |
| 5331 | } |
| 5332 | done: |
| 5333 | *p = '\0'; |
| 5334 | if (*name == '\0') { |
| 5335 | home = lookupvar(homestr); |
| 5336 | } else { |
| 5337 | pw = getpwnam(name); |
| 5338 | if (pw == NULL) |
| 5339 | goto lose; |
| 5340 | home = pw->pw_dir; |
| 5341 | } |
| 5342 | if (!home || !*home) |
| 5343 | goto lose; |
| 5344 | *p = c; |
| 5345 | startloc = expdest - (char *)stackblock(); |
| 5346 | strtodest(home, SQSYNTAX, quotes); |
| 5347 | recordregion(startloc, expdest - (char *)stackblock(), 0); |
| 5348 | return p; |
| 5349 | lose: |
| 5350 | *p = c; |
| 5351 | return startp; |
| 5352 | } |
| 5353 | |
| 5354 | /* |
| 5355 | * Execute a command inside back quotes. If it's a builtin command, we |
| 5356 | * want to save its output in a block obtained from malloc. Otherwise |
| 5357 | * we fork off a subprocess and get the output of the command via a pipe. |
| 5358 | * Should be called with interrupts off. |
| 5359 | */ |
| 5360 | struct backcmd { /* result of evalbackcmd */ |
| 5361 | int fd; /* file descriptor to read from */ |
| 5362 | char *buf; /* buffer */ |
| 5363 | int nleft; /* number of chars in buffer */ |
| 5364 | struct job *jp; /* job structure for command */ |
| 5365 | }; |
| 5366 | |
| 5367 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
| 5368 | static int back_exitstatus; /* exit status of backquoted command */ |
| 5369 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 5370 | static void evaltree(union node *, int); |
| 5371 | |
| 5372 | static void |
| 5373 | evalbackcmd(union node *n, struct backcmd *result) |
| 5374 | { |
| 5375 | int saveherefd; |
| 5376 | |
| 5377 | result->fd = -1; |
| 5378 | result->buf = NULL; |
| 5379 | result->nleft = 0; |
| 5380 | result->jp = NULL; |
| 5381 | if (n == NULL) { |
| 5382 | goto out; |
| 5383 | } |
| 5384 | |
| 5385 | saveherefd = herefd; |
| 5386 | herefd = -1; |
| 5387 | |
| 5388 | { |
| 5389 | int pip[2]; |
| 5390 | struct job *jp; |
| 5391 | |
| 5392 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5393 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 5394 | jp = makejob(/*n,*/ 1); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5395 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
| 5396 | FORCE_INT_ON; |
| 5397 | close(pip[0]); |
| 5398 | if (pip[1] != 1) { |
| 5399 | close(1); |
| 5400 | copyfd(pip[1], 1); |
| 5401 | close(pip[1]); |
| 5402 | } |
| 5403 | eflag = 0; |
| 5404 | evaltree(n, EV_EXIT); /* actually evaltreenr... */ |
| 5405 | /* NOTREACHED */ |
| 5406 | } |
| 5407 | close(pip[1]); |
| 5408 | result->fd = pip[0]; |
| 5409 | result->jp = jp; |
| 5410 | } |
| 5411 | herefd = saveherefd; |
| 5412 | out: |
| 5413 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 5414 | result->fd, result->buf, result->nleft, result->jp)); |
| 5415 | } |
| 5416 | |
| 5417 | /* |
| 5418 | * Expand stuff in backwards quotes. |
| 5419 | */ |
| 5420 | static void |
| 5421 | expbackq(union node *cmd, int quoted, int quotes) |
| 5422 | { |
| 5423 | struct backcmd in; |
| 5424 | int i; |
| 5425 | char buf[128]; |
| 5426 | char *p; |
| 5427 | char *dest; |
| 5428 | int startloc; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5429 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5430 | struct stackmark smark; |
| 5431 | |
| 5432 | INT_OFF; |
| 5433 | setstackmark(&smark); |
| 5434 | dest = expdest; |
| 5435 | startloc = dest - (char *)stackblock(); |
| 5436 | grabstackstr(dest); |
| 5437 | evalbackcmd(cmd, &in); |
| 5438 | popstackmark(&smark); |
| 5439 | |
| 5440 | p = in.buf; |
| 5441 | i = in.nleft; |
| 5442 | if (i == 0) |
| 5443 | goto read; |
| 5444 | for (;;) { |
| 5445 | memtodest(p, i, syntax, quotes); |
| 5446 | read: |
| 5447 | if (in.fd < 0) |
| 5448 | break; |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 5449 | i = nonblock_safe_read(in.fd, buf, sizeof(buf)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5450 | TRACE(("expbackq: read returns %d\n", i)); |
| 5451 | if (i <= 0) |
| 5452 | break; |
| 5453 | p = buf; |
| 5454 | } |
| 5455 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 5456 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5457 | if (in.fd >= 0) { |
| 5458 | close(in.fd); |
| 5459 | back_exitstatus = waitforjob(in.jp); |
| 5460 | } |
| 5461 | INT_ON; |
| 5462 | |
| 5463 | /* Eat all trailing newlines */ |
| 5464 | dest = expdest; |
| 5465 | for (; dest > (char *)stackblock() && dest[-1] == '\n';) |
| 5466 | STUNPUTC(dest); |
| 5467 | expdest = dest; |
| 5468 | |
| 5469 | if (quoted == 0) |
| 5470 | recordregion(startloc, dest - (char *)stackblock(), 0); |
| 5471 | TRACE(("evalbackq: size=%d: \"%.*s\"\n", |
| 5472 | (dest - (char *)stackblock()) - startloc, |
| 5473 | (dest - (char *)stackblock()) - startloc, |
| 5474 | stackblock() + startloc)); |
| 5475 | } |
| 5476 | |
| 5477 | #if ENABLE_ASH_MATH_SUPPORT |
| 5478 | /* |
| 5479 | * Expand arithmetic expression. Backup to start of expression, |
| 5480 | * evaluate, place result in (backed up) result, adjust string position. |
| 5481 | */ |
| 5482 | static void |
| 5483 | expari(int quotes) |
| 5484 | { |
| 5485 | char *p, *start; |
| 5486 | int begoff; |
| 5487 | int flag; |
| 5488 | int len; |
| 5489 | |
| 5490 | /* ifsfree(); */ |
| 5491 | |
| 5492 | /* |
| 5493 | * This routine is slightly over-complicated for |
| 5494 | * efficiency. Next we scan backwards looking for the |
| 5495 | * start of arithmetic. |
| 5496 | */ |
| 5497 | start = stackblock(); |
| 5498 | p = expdest - 1; |
| 5499 | *p = '\0'; |
| 5500 | p--; |
| 5501 | do { |
| 5502 | int esc; |
| 5503 | |
| 5504 | while (*p != CTLARI) { |
| 5505 | p--; |
| 5506 | #if DEBUG |
| 5507 | if (p < start) { |
| 5508 | ash_msg_and_raise_error("missing CTLARI (shouldn't happen)"); |
| 5509 | } |
| 5510 | #endif |
| 5511 | } |
| 5512 | |
| 5513 | esc = esclen(start, p); |
| 5514 | if (!(esc % 2)) { |
| 5515 | break; |
| 5516 | } |
| 5517 | |
| 5518 | p -= esc + 1; |
| 5519 | } while (1); |
| 5520 | |
| 5521 | begoff = p - start; |
| 5522 | |
| 5523 | removerecordregions(begoff); |
| 5524 | |
| 5525 | flag = p[1]; |
| 5526 | |
| 5527 | expdest = p; |
| 5528 | |
| 5529 | if (quotes) |
| 5530 | rmescapes(p + 2); |
| 5531 | |
| 5532 | len = cvtnum(dash_arith(p + 2)); |
| 5533 | |
| 5534 | if (flag != '"') |
| 5535 | recordregion(begoff, begoff + len, 0); |
| 5536 | } |
| 5537 | #endif |
| 5538 | |
| 5539 | /* argstr needs it */ |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5540 | static char *evalvar(char *p, int flag, struct strlist *var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5541 | |
| 5542 | /* |
| 5543 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 5544 | * characters to allow for further processing. Otherwise treat |
| 5545 | * $@ like $* since no splitting will be performed. |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5546 | * |
| 5547 | * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence |
| 5548 | * over shell varables. Needed for "A=a B=$A; echo $B" case - we use it |
| 5549 | * for correct expansion of "B=$A" word. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5550 | */ |
| 5551 | static void |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5552 | argstr(char *p, int flag, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5553 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5554 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5555 | '=', |
| 5556 | ':', |
| 5557 | CTLQUOTEMARK, |
| 5558 | CTLENDVAR, |
| 5559 | CTLESC, |
| 5560 | CTLVAR, |
| 5561 | CTLBACKQ, |
| 5562 | CTLBACKQ | CTLQUOTE, |
| 5563 | #if ENABLE_ASH_MATH_SUPPORT |
| 5564 | CTLENDARI, |
| 5565 | #endif |
| 5566 | 0 |
| 5567 | }; |
| 5568 | const char *reject = spclchars; |
| 5569 | int c; |
| 5570 | int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */ |
| 5571 | int breakall = flag & EXP_WORD; |
| 5572 | int inquotes; |
| 5573 | size_t length; |
| 5574 | int startloc; |
| 5575 | |
| 5576 | if (!(flag & EXP_VARTILDE)) { |
| 5577 | reject += 2; |
| 5578 | } else if (flag & EXP_VARTILDE2) { |
| 5579 | reject++; |
| 5580 | } |
| 5581 | inquotes = 0; |
| 5582 | length = 0; |
| 5583 | if (flag & EXP_TILDE) { |
| 5584 | char *q; |
| 5585 | |
| 5586 | flag &= ~EXP_TILDE; |
| 5587 | tilde: |
| 5588 | q = p; |
| 5589 | if (*q == CTLESC && (flag & EXP_QWORD)) |
| 5590 | q++; |
| 5591 | if (*q == '~') |
| 5592 | p = exptilde(p, q, flag); |
| 5593 | } |
| 5594 | start: |
| 5595 | startloc = expdest - (char *)stackblock(); |
| 5596 | for (;;) { |
| 5597 | length += strcspn(p + length, reject); |
| 5598 | c = p[length]; |
| 5599 | if (c && (!(c & 0x80) |
| 5600 | #if ENABLE_ASH_MATH_SUPPORT |
| 5601 | || c == CTLENDARI |
| 5602 | #endif |
| 5603 | )) { |
| 5604 | /* c == '=' || c == ':' || c == CTLENDARI */ |
| 5605 | length++; |
| 5606 | } |
| 5607 | if (length > 0) { |
| 5608 | int newloc; |
| 5609 | expdest = stack_nputstr(p, length, expdest); |
| 5610 | newloc = expdest - (char *)stackblock(); |
| 5611 | if (breakall && !inquotes && newloc > startloc) { |
| 5612 | recordregion(startloc, newloc, 0); |
| 5613 | } |
| 5614 | startloc = newloc; |
| 5615 | } |
| 5616 | p += length + 1; |
| 5617 | length = 0; |
| 5618 | |
| 5619 | switch (c) { |
| 5620 | case '\0': |
| 5621 | goto breakloop; |
| 5622 | case '=': |
| 5623 | if (flag & EXP_VARTILDE2) { |
| 5624 | p--; |
| 5625 | continue; |
| 5626 | } |
| 5627 | flag |= EXP_VARTILDE2; |
| 5628 | reject++; |
| 5629 | /* fall through */ |
| 5630 | case ':': |
| 5631 | /* |
| 5632 | * sort of a hack - expand tildes in variable |
| 5633 | * assignments (after the first '=' and after ':'s). |
| 5634 | */ |
| 5635 | if (*--p == '~') { |
| 5636 | goto tilde; |
| 5637 | } |
| 5638 | continue; |
| 5639 | } |
| 5640 | |
| 5641 | switch (c) { |
| 5642 | case CTLENDVAR: /* ??? */ |
| 5643 | goto breakloop; |
| 5644 | case CTLQUOTEMARK: |
| 5645 | /* "$@" syntax adherence hack */ |
| 5646 | if ( |
| 5647 | !inquotes && |
| 5648 | !memcmp(p, dolatstr, 4) && |
| 5649 | (p[4] == CTLQUOTEMARK || ( |
| 5650 | p[4] == CTLENDVAR && |
| 5651 | p[5] == CTLQUOTEMARK |
| 5652 | )) |
| 5653 | ) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5654 | p = evalvar(p + 1, flag, /* var_str_list: */ NULL) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5655 | goto start; |
| 5656 | } |
| 5657 | inquotes = !inquotes; |
| 5658 | addquote: |
| 5659 | if (quotes) { |
| 5660 | p--; |
| 5661 | length++; |
| 5662 | startloc++; |
| 5663 | } |
| 5664 | break; |
| 5665 | case CTLESC: |
| 5666 | startloc++; |
| 5667 | length++; |
| 5668 | goto addquote; |
| 5669 | case CTLVAR: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5670 | p = evalvar(p, flag, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5671 | goto start; |
| 5672 | case CTLBACKQ: |
| 5673 | c = 0; |
| 5674 | case CTLBACKQ|CTLQUOTE: |
| 5675 | expbackq(argbackq->n, c, quotes); |
| 5676 | argbackq = argbackq->next; |
| 5677 | goto start; |
| 5678 | #if ENABLE_ASH_MATH_SUPPORT |
| 5679 | case CTLENDARI: |
| 5680 | p--; |
| 5681 | expari(quotes); |
| 5682 | goto start; |
| 5683 | #endif |
| 5684 | } |
| 5685 | } |
| 5686 | breakloop: |
| 5687 | ; |
| 5688 | } |
| 5689 | |
| 5690 | static char * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 5691 | scanleft(char *startp, char *rmesc, char *rmescend ATTRIBUTE_UNUSED, char *str, int quotes, |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5692 | int zero) |
| 5693 | { |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5694 | // This commented out code was added by James Simmons <jsimmons@infradead.org> |
| 5695 | // as part of a larger change when he added support for ${var/a/b}. |
| 5696 | // However, it broke # and % operators: |
| 5697 | // |
| 5698 | //var=ababcdcd |
| 5699 | // ok bad |
| 5700 | //echo ${var#ab} abcdcd abcdcd |
| 5701 | //echo ${var##ab} abcdcd abcdcd |
| 5702 | //echo ${var#a*b} abcdcd ababcdcd (!) |
| 5703 | //echo ${var##a*b} cdcd cdcd |
| 5704 | //echo ${var#?} babcdcd ababcdcd (!) |
| 5705 | //echo ${var##?} babcdcd babcdcd |
| 5706 | //echo ${var#*} ababcdcd babcdcd (!) |
| 5707 | //echo ${var##*} |
| 5708 | //echo ${var%cd} ababcd ababcd |
| 5709 | //echo ${var%%cd} ababcd abab (!) |
| 5710 | //echo ${var%c*d} ababcd ababcd |
| 5711 | //echo ${var%%c*d} abab ababcdcd (!) |
| 5712 | //echo ${var%?} ababcdc ababcdc |
| 5713 | //echo ${var%%?} ababcdc ababcdcd (!) |
| 5714 | //echo ${var%*} ababcdcd ababcdcd |
| 5715 | //echo ${var%%*} |
| 5716 | // |
| 5717 | // Commenting it back out helped. Remove it completely if it really |
| 5718 | // is not needed. |
| 5719 | |
| 5720 | char *loc, *loc2; //, *full; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5721 | char c; |
| 5722 | |
| 5723 | loc = startp; |
| 5724 | loc2 = rmesc; |
| 5725 | do { |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5726 | int match; // = strlen(str); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5727 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 5728 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5729 | c = *loc2; |
| 5730 | if (zero) { |
| 5731 | *loc2 = '\0'; |
| 5732 | s = rmesc; |
| 5733 | } |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5734 | match = pmatch(str, s); // this line was deleted |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 5735 | |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5736 | // // chop off end if its '*' |
| 5737 | // full = strrchr(str, '*'); |
| 5738 | // if (full && full != str) |
| 5739 | // match--; |
| 5740 | // |
| 5741 | // // If str starts with '*' replace with s. |
| 5742 | // if ((*str == '*') && strlen(s) >= match) { |
| 5743 | // full = xstrdup(s); |
| 5744 | // strncpy(full+strlen(s)-match+1, str+1, match-1); |
| 5745 | // } else |
| 5746 | // full = xstrndup(str, match); |
| 5747 | // match = strncmp(s, full, strlen(full)); |
| 5748 | // free(full); |
| 5749 | // |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5750 | *loc2 = c; |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5751 | if (match) // if (!match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5752 | return loc; |
| 5753 | if (quotes && *loc == CTLESC) |
| 5754 | loc++; |
| 5755 | loc++; |
| 5756 | loc2++; |
| 5757 | } while (c); |
| 5758 | return 0; |
| 5759 | } |
| 5760 | |
| 5761 | static char * |
| 5762 | scanright(char *startp, char *rmesc, char *rmescend, char *str, int quotes, |
| 5763 | int zero) |
| 5764 | { |
| 5765 | int esc = 0; |
| 5766 | char *loc; |
| 5767 | char *loc2; |
| 5768 | |
| 5769 | for (loc = str - 1, loc2 = rmescend; loc >= startp; loc2--) { |
| 5770 | int match; |
| 5771 | char c = *loc2; |
| 5772 | const char *s = loc2; |
| 5773 | if (zero) { |
| 5774 | *loc2 = '\0'; |
| 5775 | s = rmesc; |
| 5776 | } |
| 5777 | match = pmatch(str, s); |
| 5778 | *loc2 = c; |
| 5779 | if (match) |
| 5780 | return loc; |
| 5781 | loc--; |
| 5782 | if (quotes) { |
| 5783 | if (--esc < 0) { |
| 5784 | esc = esclen(startp, loc); |
| 5785 | } |
| 5786 | if (esc % 2) { |
| 5787 | esc--; |
| 5788 | loc--; |
| 5789 | } |
| 5790 | } |
| 5791 | } |
| 5792 | return 0; |
| 5793 | } |
| 5794 | |
| 5795 | static void varunset(const char *, const char *, const char *, int) ATTRIBUTE_NORETURN; |
| 5796 | static void |
| 5797 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 5798 | { |
| 5799 | const char *msg; |
| 5800 | const char *tail; |
| 5801 | |
| 5802 | tail = nullstr; |
| 5803 | msg = "parameter not set"; |
| 5804 | if (umsg) { |
| 5805 | if (*end == CTLENDVAR) { |
| 5806 | if (varflags & VSNUL) |
| 5807 | tail = " or null"; |
| 5808 | } else |
| 5809 | msg = umsg; |
| 5810 | } |
| 5811 | ash_msg_and_raise_error("%.*s: %s%s", end - var - 1, var, msg, tail); |
| 5812 | } |
| 5813 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 5814 | #if ENABLE_ASH_BASH_COMPAT |
| 5815 | static char * |
| 5816 | parse_sub_pattern(char *arg, int inquotes) |
| 5817 | { |
| 5818 | char *idx, *repl = NULL; |
| 5819 | unsigned char c; |
| 5820 | |
| 5821 | for (idx = arg; *arg; arg++) { |
| 5822 | if (*arg == '/') { |
| 5823 | /* Only the first '/' seen is our seperator */ |
| 5824 | if (!repl) { |
| 5825 | *idx++ = '\0'; |
| 5826 | repl = idx; |
| 5827 | } else |
| 5828 | *idx++ = *arg; |
| 5829 | } else if (*arg != '\\') { |
| 5830 | *idx++ = *arg; |
| 5831 | } else { |
| 5832 | if (inquotes) |
| 5833 | arg++; |
| 5834 | else { |
| 5835 | if (*(arg + 1) != '\\') |
| 5836 | goto single_backslash; |
| 5837 | arg += 2; |
| 5838 | } |
| 5839 | |
| 5840 | switch (*arg) { |
| 5841 | case 'n': c = '\n'; break; |
| 5842 | case 'r': c = '\r'; break; |
| 5843 | case 't': c = '\t'; break; |
| 5844 | case 'v': c = '\v'; break; |
| 5845 | case 'f': c = '\f'; break; |
| 5846 | case 'b': c = '\b'; break; |
| 5847 | case 'a': c = '\a'; break; |
| 5848 | case '\\': |
| 5849 | if (*(arg + 1) != '\\' && !inquotes) |
| 5850 | goto single_backslash; |
| 5851 | arg++; |
| 5852 | /* FALLTHROUGH */ |
| 5853 | case '\0': |
| 5854 | /* Trailing backslash, just stuff one in the buffer |
| 5855 | * and backup arg so the loop will exit. |
| 5856 | */ |
| 5857 | c = '\\'; |
| 5858 | if (!*arg) |
| 5859 | arg--; |
| 5860 | break; |
| 5861 | default: |
| 5862 | c = *arg; |
| 5863 | if (isdigit(c)) { |
| 5864 | /* It's an octal number, parse it. */ |
| 5865 | int i; |
| 5866 | c = 0; |
| 5867 | |
| 5868 | for (i = 0; *arg && i < 3; arg++, i++) { |
| 5869 | if (*arg >= '8' || *arg < '0') |
| 5870 | ash_msg_and_raise_error("Invalid octal char in pattern"); |
| 5871 | // TODO: number() instead? It does error checking... |
| 5872 | c = (c << 3) + atoi(arg); |
| 5873 | } |
| 5874 | /* back off one (so outer loop can do it) */ |
| 5875 | arg--; |
| 5876 | } |
| 5877 | } |
| 5878 | *idx++ = c; |
| 5879 | } |
| 5880 | } |
| 5881 | *idx = *arg; |
| 5882 | |
| 5883 | return repl; |
| 5884 | |
| 5885 | single_backslash: |
| 5886 | ash_msg_and_raise_error("single backslash unexpected"); |
| 5887 | /* NOTREACHED */ |
| 5888 | } |
| 5889 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 5890 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5891 | static const char * |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5892 | subevalvar(char *p, char *str, int strloc, int subtype, |
| 5893 | int startloc, int varflags, int quotes, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5894 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 5895 | struct nodelist *saveargbackq = argbackq; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5896 | char *startp; |
| 5897 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5898 | char *rmesc, *rmescend; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 5899 | USE_ASH_BASH_COMPAT(char *repl = NULL;) |
| 5900 | USE_ASH_BASH_COMPAT(char null = '\0';) |
| 5901 | USE_ASH_BASH_COMPAT(int pos, len, orig_len;) |
| 5902 | int saveherefd = herefd; |
| 5903 | int amount, workloc, resetloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5904 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 5905 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5906 | |
| 5907 | herefd = -1; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5908 | argstr(p, (subtype != VSASSIGN && subtype != VSQUESTION) ? EXP_CASE : 0, |
| 5909 | var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5910 | STPUTC('\0', expdest); |
| 5911 | herefd = saveherefd; |
| 5912 | argbackq = saveargbackq; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5913 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5914 | |
| 5915 | switch (subtype) { |
| 5916 | case VSASSIGN: |
| 5917 | setvar(str, startp, 0); |
| 5918 | amount = startp - expdest; |
| 5919 | STADJUST(amount, expdest); |
| 5920 | return startp; |
| 5921 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 5922 | #if ENABLE_ASH_BASH_COMPAT |
| 5923 | case VSSUBSTR: |
| 5924 | loc = str = stackblock() + strloc; |
| 5925 | // TODO: number() instead? It does error checking... |
| 5926 | pos = atoi(loc); |
| 5927 | len = str - startp - 1; |
| 5928 | |
| 5929 | /* *loc != '\0', guaranteed by parser */ |
| 5930 | if (quotes) { |
| 5931 | char *ptr; |
| 5932 | |
| 5933 | /* We must adjust the length by the number of escapes we find. */ |
| 5934 | for (ptr = startp; ptr < (str - 1); ptr++) { |
| 5935 | if(*ptr == CTLESC) { |
| 5936 | len--; |
| 5937 | ptr++; |
| 5938 | } |
| 5939 | } |
| 5940 | } |
| 5941 | orig_len = len; |
| 5942 | |
| 5943 | if (*loc++ == ':') { |
| 5944 | // TODO: number() instead? It does error checking... |
| 5945 | len = atoi(loc); |
| 5946 | } else { |
| 5947 | len = orig_len; |
| 5948 | while (*loc && *loc != ':') |
| 5949 | loc++; |
| 5950 | if (*loc++ == ':') |
| 5951 | // TODO: number() instead? It does error checking... |
| 5952 | len = atoi(loc); |
| 5953 | } |
| 5954 | if (pos >= orig_len) { |
| 5955 | pos = 0; |
| 5956 | len = 0; |
| 5957 | } |
| 5958 | if (len > (orig_len - pos)) |
| 5959 | len = orig_len - pos; |
| 5960 | |
| 5961 | for (str = startp; pos; str++, pos--) { |
| 5962 | if (quotes && *str == CTLESC) |
| 5963 | str++; |
| 5964 | } |
| 5965 | for (loc = startp; len; len--) { |
| 5966 | if (quotes && *str == CTLESC) |
| 5967 | *loc++ = *str++; |
| 5968 | *loc++ = *str++; |
| 5969 | } |
| 5970 | *loc = '\0'; |
| 5971 | amount = loc - expdest; |
| 5972 | STADJUST(amount, expdest); |
| 5973 | return loc; |
| 5974 | #endif |
| 5975 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5976 | case VSQUESTION: |
| 5977 | varunset(p, str, startp, varflags); |
| 5978 | /* NOTREACHED */ |
| 5979 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 5980 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5981 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 5982 | /* We'll comeback here if we grow the stack while handling |
| 5983 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 5984 | * stack will need rebasing, and we'll need to remove our work |
| 5985 | * areas each time |
| 5986 | */ |
| 5987 | USE_ASH_BASH_COMPAT(restart:) |
| 5988 | |
| 5989 | amount = expdest - ((char *)stackblock() + resetloc); |
| 5990 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5991 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5992 | |
| 5993 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5994 | rmescend = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5995 | if (quotes) { |
| 5996 | rmesc = _rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); |
| 5997 | if (rmesc != startp) { |
| 5998 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5999 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6000 | } |
| 6001 | } |
| 6002 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6003 | str = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6004 | preglob(str, varflags & VSQUOTE, 0); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6005 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6006 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6007 | #if ENABLE_ASH_BASH_COMPAT |
| 6008 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
| 6009 | char *idx, *end, *restart_detect; |
| 6010 | |
| 6011 | if(!repl) { |
| 6012 | repl = parse_sub_pattern(str, varflags & VSQUOTE); |
| 6013 | if (!repl) |
| 6014 | repl = &null; |
| 6015 | } |
| 6016 | |
| 6017 | /* If there's no pattern to match, return the expansion unmolested */ |
| 6018 | if (*str == '\0') |
| 6019 | return 0; |
| 6020 | |
| 6021 | len = 0; |
| 6022 | idx = startp; |
| 6023 | end = str - 1; |
| 6024 | while (idx < end) { |
| 6025 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); |
| 6026 | if (!loc) { |
| 6027 | /* No match, advance */ |
| 6028 | restart_detect = stackblock(); |
| 6029 | STPUTC(*idx, expdest); |
| 6030 | if (quotes && *idx == CTLESC) { |
| 6031 | idx++; |
| 6032 | len++; |
| 6033 | STPUTC(*idx, expdest); |
| 6034 | } |
| 6035 | if (stackblock() != restart_detect) |
| 6036 | goto restart; |
| 6037 | idx++; |
| 6038 | len++; |
| 6039 | rmesc++; |
| 6040 | continue; |
| 6041 | } |
| 6042 | |
| 6043 | if (subtype == VSREPLACEALL) { |
| 6044 | while (idx < loc) { |
| 6045 | if (quotes && *idx == CTLESC) |
| 6046 | idx++; |
| 6047 | idx++; |
| 6048 | rmesc++; |
| 6049 | } |
| 6050 | } else |
| 6051 | idx = loc; |
| 6052 | |
| 6053 | for (loc = repl; *loc; loc++) { |
| 6054 | restart_detect = stackblock(); |
| 6055 | STPUTC(*loc, expdest); |
| 6056 | if (stackblock() != restart_detect) |
| 6057 | goto restart; |
| 6058 | len++; |
| 6059 | } |
| 6060 | |
| 6061 | if (subtype == VSREPLACE) { |
| 6062 | while (*idx) { |
| 6063 | restart_detect = stackblock(); |
| 6064 | STPUTC(*idx, expdest); |
| 6065 | if (stackblock() != restart_detect) |
| 6066 | goto restart; |
| 6067 | len++; |
| 6068 | idx++; |
| 6069 | } |
| 6070 | break; |
| 6071 | } |
| 6072 | } |
| 6073 | |
| 6074 | /* We've put the replaced text into a buffer at workloc, now |
| 6075 | * move it to the right place and adjust the stack. |
| 6076 | */ |
| 6077 | startp = stackblock() + startloc; |
| 6078 | STPUTC('\0', expdest); |
| 6079 | memmove(startp, stackblock() + workloc, len); |
| 6080 | startp[len++] = '\0'; |
| 6081 | amount = expdest - ((char *)stackblock() + startloc + len - 1); |
| 6082 | STADJUST(-amount, expdest); |
| 6083 | return startp; |
| 6084 | } |
| 6085 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6086 | |
| 6087 | subtype -= VSTRIMRIGHT; |
| 6088 | #if DEBUG |
| 6089 | if (subtype < 0 || subtype > 7) |
| 6090 | abort(); |
| 6091 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6092 | /* zero = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX */ |
| 6093 | zero = subtype >> 1; |
| 6094 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 6095 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 6096 | |
| 6097 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 6098 | if (loc) { |
| 6099 | if (zero) { |
| 6100 | memmove(startp, loc, str - loc); |
| 6101 | loc = startp + (str - loc) - 1; |
| 6102 | } |
| 6103 | *loc = '\0'; |
| 6104 | amount = loc - expdest; |
| 6105 | STADJUST(amount, expdest); |
| 6106 | } |
| 6107 | return loc; |
| 6108 | } |
| 6109 | |
| 6110 | /* |
| 6111 | * Add the value of a specialized variable to the stack string. |
| 6112 | */ |
| 6113 | static ssize_t |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6114 | varvalue(char *name, int varflags, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6115 | { |
| 6116 | int num; |
| 6117 | char *p; |
| 6118 | int i; |
| 6119 | int sep = 0; |
| 6120 | int sepq = 0; |
| 6121 | ssize_t len = 0; |
| 6122 | char **ap; |
| 6123 | int syntax; |
| 6124 | int quoted = varflags & VSQUOTE; |
| 6125 | int subtype = varflags & VSTYPE; |
| 6126 | int quotes = flags & (EXP_FULL | EXP_CASE); |
| 6127 | |
| 6128 | if (quoted && (flags & EXP_FULL)) |
| 6129 | sep = 1 << CHAR_BIT; |
| 6130 | |
| 6131 | syntax = quoted ? DQSYNTAX : BASESYNTAX; |
| 6132 | switch (*name) { |
| 6133 | case '$': |
| 6134 | num = rootpid; |
| 6135 | goto numvar; |
| 6136 | case '?': |
| 6137 | num = exitstatus; |
| 6138 | goto numvar; |
| 6139 | case '#': |
| 6140 | num = shellparam.nparam; |
| 6141 | goto numvar; |
| 6142 | case '!': |
| 6143 | num = backgndpid; |
| 6144 | if (num == 0) |
| 6145 | return -1; |
| 6146 | numvar: |
| 6147 | len = cvtnum(num); |
| 6148 | break; |
| 6149 | case '-': |
| 6150 | p = makestrspace(NOPTS, expdest); |
| 6151 | for (i = NOPTS - 1; i >= 0; i--) { |
| 6152 | if (optlist[i]) { |
| 6153 | USTPUTC(optletters(i), p); |
| 6154 | len++; |
| 6155 | } |
| 6156 | } |
| 6157 | expdest = p; |
| 6158 | break; |
| 6159 | case '@': |
| 6160 | if (sep) |
| 6161 | goto param; |
| 6162 | /* fall through */ |
| 6163 | case '*': |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 6164 | sep = ifsset() ? signed_char2int(ifsval()[0]) : ' '; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6165 | if (quotes && (SIT(sep, syntax) == CCTL || SIT(sep, syntax) == CBACK)) |
| 6166 | sepq = 1; |
| 6167 | param: |
| 6168 | ap = shellparam.p; |
| 6169 | if (!ap) |
| 6170 | return -1; |
| 6171 | while ((p = *ap++)) { |
| 6172 | size_t partlen; |
| 6173 | |
| 6174 | partlen = strlen(p); |
| 6175 | len += partlen; |
| 6176 | |
| 6177 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6178 | memtodest(p, partlen, syntax, quotes); |
| 6179 | |
| 6180 | if (*ap && sep) { |
| 6181 | char *q; |
| 6182 | |
| 6183 | len++; |
| 6184 | if (subtype == VSPLUS || subtype == VSLENGTH) { |
| 6185 | continue; |
| 6186 | } |
| 6187 | q = expdest; |
| 6188 | if (sepq) |
| 6189 | STPUTC(CTLESC, q); |
| 6190 | STPUTC(sep, q); |
| 6191 | expdest = q; |
| 6192 | } |
| 6193 | } |
| 6194 | return len; |
| 6195 | case '0': |
| 6196 | case '1': |
| 6197 | case '2': |
| 6198 | case '3': |
| 6199 | case '4': |
| 6200 | case '5': |
| 6201 | case '6': |
| 6202 | case '7': |
| 6203 | case '8': |
| 6204 | case '9': |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6205 | // TODO: number() instead? It does error checking... |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6206 | num = atoi(name); |
| 6207 | if (num < 0 || num > shellparam.nparam) |
| 6208 | return -1; |
| 6209 | p = num ? shellparam.p[num - 1] : arg0; |
| 6210 | goto value; |
| 6211 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6212 | /* NB: name has form "VAR=..." */ |
| 6213 | |
| 6214 | /* "A=a B=$A" case: var_str_list is a list of "A=a" strings |
| 6215 | * which should be considered before we check variables. */ |
| 6216 | if (var_str_list) { |
| 6217 | unsigned name_len = (strchrnul(name, '=') - name) + 1; |
| 6218 | p = NULL; |
| 6219 | do { |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 6220 | char *str, *eq; |
| 6221 | str = var_str_list->text; |
| 6222 | eq = strchr(str, '='); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6223 | if (!eq) /* stop at first non-assignment */ |
| 6224 | break; |
| 6225 | eq++; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 6226 | if (name_len == (unsigned)(eq - str) |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6227 | && strncmp(str, name, name_len) == 0) { |
| 6228 | p = eq; |
| 6229 | /* goto value; - WRONG! */ |
| 6230 | /* think "A=1 A=2 B=$A" */ |
| 6231 | } |
| 6232 | var_str_list = var_str_list->next; |
| 6233 | } while (var_str_list); |
| 6234 | if (p) |
| 6235 | goto value; |
| 6236 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6237 | p = lookupvar(name); |
| 6238 | value: |
| 6239 | if (!p) |
| 6240 | return -1; |
| 6241 | |
| 6242 | len = strlen(p); |
| 6243 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6244 | memtodest(p, len, syntax, quotes); |
| 6245 | return len; |
| 6246 | } |
| 6247 | |
| 6248 | if (subtype == VSPLUS || subtype == VSLENGTH) |
| 6249 | STADJUST(-len, expdest); |
| 6250 | return len; |
| 6251 | } |
| 6252 | |
| 6253 | /* |
| 6254 | * Expand a variable, and return a pointer to the next character in the |
| 6255 | * input string. |
| 6256 | */ |
| 6257 | static char * |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6258 | evalvar(char *p, int flag, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6259 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6260 | char varflags; |
| 6261 | char subtype; |
| 6262 | char quoted; |
| 6263 | char easy; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6264 | char *var; |
| 6265 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6266 | int startloc; |
| 6267 | ssize_t varlen; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6268 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6269 | varflags = *p++; |
| 6270 | subtype = varflags & VSTYPE; |
| 6271 | quoted = varflags & VSQUOTE; |
| 6272 | var = p; |
| 6273 | easy = (!quoted || (*var == '@' && shellparam.nparam)); |
| 6274 | startloc = expdest - (char *)stackblock(); |
| 6275 | p = strchr(p, '=') + 1; |
| 6276 | |
| 6277 | again: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6278 | varlen = varvalue(var, varflags, flag, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6279 | if (varflags & VSNUL) |
| 6280 | varlen--; |
| 6281 | |
| 6282 | if (subtype == VSPLUS) { |
| 6283 | varlen = -1 - varlen; |
| 6284 | goto vsplus; |
| 6285 | } |
| 6286 | |
| 6287 | if (subtype == VSMINUS) { |
| 6288 | vsplus: |
| 6289 | if (varlen < 0) { |
| 6290 | argstr( |
| 6291 | p, flag | EXP_TILDE | |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6292 | (quoted ? EXP_QWORD : EXP_WORD), |
| 6293 | var_str_list |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6294 | ); |
| 6295 | goto end; |
| 6296 | } |
| 6297 | if (easy) |
| 6298 | goto record; |
| 6299 | goto end; |
| 6300 | } |
| 6301 | |
| 6302 | if (subtype == VSASSIGN || subtype == VSQUESTION) { |
| 6303 | if (varlen < 0) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6304 | if (subevalvar(p, var, /* strloc: */ 0, |
| 6305 | subtype, startloc, varflags, |
| 6306 | /* quotes: */ 0, |
| 6307 | var_str_list) |
| 6308 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6309 | varflags &= ~VSNUL; |
| 6310 | /* |
| 6311 | * Remove any recorded regions beyond |
| 6312 | * start of variable |
| 6313 | */ |
| 6314 | removerecordregions(startloc); |
| 6315 | goto again; |
| 6316 | } |
| 6317 | goto end; |
| 6318 | } |
| 6319 | if (easy) |
| 6320 | goto record; |
| 6321 | goto end; |
| 6322 | } |
| 6323 | |
| 6324 | if (varlen < 0 && uflag) |
| 6325 | varunset(p, var, 0, 0); |
| 6326 | |
| 6327 | if (subtype == VSLENGTH) { |
| 6328 | cvtnum(varlen > 0 ? varlen : 0); |
| 6329 | goto record; |
| 6330 | } |
| 6331 | |
| 6332 | if (subtype == VSNORMAL) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6333 | if (easy) |
| 6334 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6335 | goto end; |
| 6336 | } |
| 6337 | |
| 6338 | #if DEBUG |
| 6339 | switch (subtype) { |
| 6340 | case VSTRIMLEFT: |
| 6341 | case VSTRIMLEFTMAX: |
| 6342 | case VSTRIMRIGHT: |
| 6343 | case VSTRIMRIGHTMAX: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6344 | #if ENABLE_ASH_BASH_COMPAT |
| 6345 | case VSSUBSTR: |
| 6346 | case VSREPLACE: |
| 6347 | case VSREPLACEALL: |
| 6348 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6349 | break; |
| 6350 | default: |
| 6351 | abort(); |
| 6352 | } |
| 6353 | #endif |
| 6354 | |
| 6355 | if (varlen >= 0) { |
| 6356 | /* |
| 6357 | * Terminate the string and start recording the pattern |
| 6358 | * right after it |
| 6359 | */ |
| 6360 | STPUTC('\0', expdest); |
| 6361 | patloc = expdest - (char *)stackblock(); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6362 | if (0 == subevalvar(p, /* str: */ NULL, patloc, subtype, |
| 6363 | startloc, varflags, |
| 6364 | /* quotes: */ flag & (EXP_FULL | EXP_CASE), |
| 6365 | var_str_list) |
| 6366 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6367 | int amount = expdest - ( |
| 6368 | (char *)stackblock() + patloc - 1 |
| 6369 | ); |
| 6370 | STADJUST(-amount, expdest); |
| 6371 | } |
| 6372 | /* Remove any recorded regions beyond start of variable */ |
| 6373 | removerecordregions(startloc); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6374 | record: |
| 6375 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6376 | } |
| 6377 | |
| 6378 | end: |
| 6379 | if (subtype != VSNORMAL) { /* skip to end of alternative */ |
| 6380 | int nesting = 1; |
| 6381 | for (;;) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6382 | char c = *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6383 | if (c == CTLESC) |
| 6384 | p++; |
| 6385 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { |
| 6386 | if (varlen >= 0) |
| 6387 | argbackq = argbackq->next; |
| 6388 | } else if (c == CTLVAR) { |
| 6389 | if ((*p++ & VSTYPE) != VSNORMAL) |
| 6390 | nesting++; |
| 6391 | } else if (c == CTLENDVAR) { |
| 6392 | if (--nesting == 0) |
| 6393 | break; |
| 6394 | } |
| 6395 | } |
| 6396 | } |
| 6397 | return p; |
| 6398 | } |
| 6399 | |
| 6400 | /* |
| 6401 | * Break the argument string into pieces based upon IFS and add the |
| 6402 | * strings to the argument list. The regions of the string to be |
| 6403 | * searched for IFS characters have been stored by recordregion. |
| 6404 | */ |
| 6405 | static void |
| 6406 | ifsbreakup(char *string, struct arglist *arglist) |
| 6407 | { |
| 6408 | struct ifsregion *ifsp; |
| 6409 | struct strlist *sp; |
| 6410 | char *start; |
| 6411 | char *p; |
| 6412 | char *q; |
| 6413 | const char *ifs, *realifs; |
| 6414 | int ifsspc; |
| 6415 | int nulonly; |
| 6416 | |
| 6417 | start = string; |
| 6418 | if (ifslastp != NULL) { |
| 6419 | ifsspc = 0; |
| 6420 | nulonly = 0; |
| 6421 | realifs = ifsset() ? ifsval() : defifs; |
| 6422 | ifsp = &ifsfirst; |
| 6423 | do { |
| 6424 | p = string + ifsp->begoff; |
| 6425 | nulonly = ifsp->nulonly; |
| 6426 | ifs = nulonly ? nullstr : realifs; |
| 6427 | ifsspc = 0; |
| 6428 | while (p < string + ifsp->endoff) { |
| 6429 | q = p; |
| 6430 | if (*p == CTLESC) |
| 6431 | p++; |
| 6432 | if (!strchr(ifs, *p)) { |
| 6433 | p++; |
| 6434 | continue; |
| 6435 | } |
| 6436 | if (!nulonly) |
| 6437 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6438 | /* Ignore IFS whitespace at start */ |
| 6439 | if (q == start && ifsspc) { |
| 6440 | p++; |
| 6441 | start = p; |
| 6442 | continue; |
| 6443 | } |
| 6444 | *q = '\0'; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6445 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6446 | sp->text = start; |
| 6447 | *arglist->lastp = sp; |
| 6448 | arglist->lastp = &sp->next; |
| 6449 | p++; |
| 6450 | if (!nulonly) { |
| 6451 | for (;;) { |
| 6452 | if (p >= string + ifsp->endoff) { |
| 6453 | break; |
| 6454 | } |
| 6455 | q = p; |
| 6456 | if (*p == CTLESC) |
| 6457 | p++; |
| 6458 | if (strchr(ifs, *p) == NULL ) { |
| 6459 | p = q; |
| 6460 | break; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6461 | } |
| 6462 | if (strchr(defifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6463 | if (ifsspc) { |
| 6464 | p++; |
| 6465 | ifsspc = 0; |
| 6466 | } else { |
| 6467 | p = q; |
| 6468 | break; |
| 6469 | } |
| 6470 | } else |
| 6471 | p++; |
| 6472 | } |
| 6473 | } |
| 6474 | start = p; |
| 6475 | } /* while */ |
| 6476 | ifsp = ifsp->next; |
| 6477 | } while (ifsp != NULL); |
| 6478 | if (nulonly) |
| 6479 | goto add; |
| 6480 | } |
| 6481 | |
| 6482 | if (!*start) |
| 6483 | return; |
| 6484 | |
| 6485 | add: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6486 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6487 | sp->text = start; |
| 6488 | *arglist->lastp = sp; |
| 6489 | arglist->lastp = &sp->next; |
| 6490 | } |
| 6491 | |
| 6492 | static void |
| 6493 | ifsfree(void) |
| 6494 | { |
| 6495 | struct ifsregion *p; |
| 6496 | |
| 6497 | INT_OFF; |
| 6498 | p = ifsfirst.next; |
| 6499 | do { |
| 6500 | struct ifsregion *ifsp; |
| 6501 | ifsp = p->next; |
| 6502 | free(p); |
| 6503 | p = ifsp; |
| 6504 | } while (p); |
| 6505 | ifslastp = NULL; |
| 6506 | ifsfirst.next = NULL; |
| 6507 | INT_ON; |
| 6508 | } |
| 6509 | |
| 6510 | /* |
| 6511 | * Add a file name to the list. |
| 6512 | */ |
| 6513 | static void |
| 6514 | addfname(const char *name) |
| 6515 | { |
| 6516 | struct strlist *sp; |
| 6517 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6518 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6519 | sp->text = ststrdup(name); |
| 6520 | *exparg.lastp = sp; |
| 6521 | exparg.lastp = &sp->next; |
| 6522 | } |
| 6523 | |
| 6524 | static char *expdir; |
| 6525 | |
| 6526 | /* |
| 6527 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 6528 | */ |
| 6529 | static void |
| 6530 | expmeta(char *enddir, char *name) |
| 6531 | { |
| 6532 | char *p; |
| 6533 | const char *cp; |
| 6534 | char *start; |
| 6535 | char *endname; |
| 6536 | int metaflag; |
| 6537 | struct stat statb; |
| 6538 | DIR *dirp; |
| 6539 | struct dirent *dp; |
| 6540 | int atend; |
| 6541 | int matchdot; |
| 6542 | |
| 6543 | metaflag = 0; |
| 6544 | start = name; |
| 6545 | for (p = name; *p; p++) { |
| 6546 | if (*p == '*' || *p == '?') |
| 6547 | metaflag = 1; |
| 6548 | else if (*p == '[') { |
| 6549 | char *q = p + 1; |
| 6550 | if (*q == '!') |
| 6551 | q++; |
| 6552 | for (;;) { |
| 6553 | if (*q == '\\') |
| 6554 | q++; |
| 6555 | if (*q == '/' || *q == '\0') |
| 6556 | break; |
| 6557 | if (*++q == ']') { |
| 6558 | metaflag = 1; |
| 6559 | break; |
| 6560 | } |
| 6561 | } |
| 6562 | } else if (*p == '\\') |
| 6563 | p++; |
| 6564 | else if (*p == '/') { |
| 6565 | if (metaflag) |
| 6566 | goto out; |
| 6567 | start = p + 1; |
| 6568 | } |
| 6569 | } |
| 6570 | out: |
| 6571 | if (metaflag == 0) { /* we've reached the end of the file name */ |
| 6572 | if (enddir != expdir) |
| 6573 | metaflag++; |
| 6574 | p = name; |
| 6575 | do { |
| 6576 | if (*p == '\\') |
| 6577 | p++; |
| 6578 | *enddir++ = *p; |
| 6579 | } while (*p++); |
| 6580 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
| 6581 | addfname(expdir); |
| 6582 | return; |
| 6583 | } |
| 6584 | endname = p; |
| 6585 | if (name < start) { |
| 6586 | p = name; |
| 6587 | do { |
| 6588 | if (*p == '\\') |
| 6589 | p++; |
| 6590 | *enddir++ = *p++; |
| 6591 | } while (p < start); |
| 6592 | } |
| 6593 | if (enddir == expdir) { |
| 6594 | cp = "."; |
| 6595 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 6596 | cp = "/"; |
| 6597 | } else { |
| 6598 | cp = expdir; |
| 6599 | enddir[-1] = '\0'; |
| 6600 | } |
| 6601 | dirp = opendir(cp); |
| 6602 | if (dirp == NULL) |
| 6603 | return; |
| 6604 | if (enddir != expdir) |
| 6605 | enddir[-1] = '/'; |
| 6606 | if (*endname == 0) { |
| 6607 | atend = 1; |
| 6608 | } else { |
| 6609 | atend = 0; |
| 6610 | *endname++ = '\0'; |
| 6611 | } |
| 6612 | matchdot = 0; |
| 6613 | p = start; |
| 6614 | if (*p == '\\') |
| 6615 | p++; |
| 6616 | if (*p == '.') |
| 6617 | matchdot++; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 6618 | while (!intpending && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6619 | if (dp->d_name[0] == '.' && ! matchdot) |
| 6620 | continue; |
| 6621 | if (pmatch(start, dp->d_name)) { |
| 6622 | if (atend) { |
| 6623 | strcpy(enddir, dp->d_name); |
| 6624 | addfname(expdir); |
| 6625 | } else { |
| 6626 | for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';) |
| 6627 | continue; |
| 6628 | p[-1] = '/'; |
| 6629 | expmeta(p, endname); |
| 6630 | } |
| 6631 | } |
| 6632 | } |
| 6633 | closedir(dirp); |
| 6634 | if (! atend) |
| 6635 | endname[-1] = '/'; |
| 6636 | } |
| 6637 | |
| 6638 | static struct strlist * |
| 6639 | msort(struct strlist *list, int len) |
| 6640 | { |
| 6641 | struct strlist *p, *q = NULL; |
| 6642 | struct strlist **lpp; |
| 6643 | int half; |
| 6644 | int n; |
| 6645 | |
| 6646 | if (len <= 1) |
| 6647 | return list; |
| 6648 | half = len >> 1; |
| 6649 | p = list; |
| 6650 | for (n = half; --n >= 0; ) { |
| 6651 | q = p; |
| 6652 | p = p->next; |
| 6653 | } |
| 6654 | q->next = NULL; /* terminate first half of list */ |
| 6655 | q = msort(list, half); /* sort first half of list */ |
| 6656 | p = msort(p, len - half); /* sort second half */ |
| 6657 | lpp = &list; |
| 6658 | for (;;) { |
| 6659 | #if ENABLE_LOCALE_SUPPORT |
| 6660 | if (strcoll(p->text, q->text) < 0) |
| 6661 | #else |
| 6662 | if (strcmp(p->text, q->text) < 0) |
| 6663 | #endif |
| 6664 | { |
| 6665 | *lpp = p; |
| 6666 | lpp = &p->next; |
| 6667 | p = *lpp; |
| 6668 | if (p == NULL) { |
| 6669 | *lpp = q; |
| 6670 | break; |
| 6671 | } |
| 6672 | } else { |
| 6673 | *lpp = q; |
| 6674 | lpp = &q->next; |
| 6675 | q = *lpp; |
| 6676 | if (q == NULL) { |
| 6677 | *lpp = p; |
| 6678 | break; |
| 6679 | } |
| 6680 | } |
| 6681 | } |
| 6682 | return list; |
| 6683 | } |
| 6684 | |
| 6685 | /* |
| 6686 | * Sort the results of file name expansion. It calculates the number of |
| 6687 | * strings to sort and then calls msort (short for merge sort) to do the |
| 6688 | * work. |
| 6689 | */ |
| 6690 | static struct strlist * |
| 6691 | expsort(struct strlist *str) |
| 6692 | { |
| 6693 | int len; |
| 6694 | struct strlist *sp; |
| 6695 | |
| 6696 | len = 0; |
| 6697 | for (sp = str; sp; sp = sp->next) |
| 6698 | len++; |
| 6699 | return msort(str, len); |
| 6700 | } |
| 6701 | |
| 6702 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 6703 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6704 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 6705 | static const char metachars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6706 | '*', '?', '[', 0 |
| 6707 | }; |
| 6708 | /* TODO - EXP_REDIR */ |
| 6709 | |
| 6710 | while (str) { |
| 6711 | struct strlist **savelastp; |
| 6712 | struct strlist *sp; |
| 6713 | char *p; |
| 6714 | |
| 6715 | if (fflag) |
| 6716 | goto nometa; |
| 6717 | if (!strpbrk(str->text, metachars)) |
| 6718 | goto nometa; |
| 6719 | savelastp = exparg.lastp; |
| 6720 | |
| 6721 | INT_OFF; |
| 6722 | p = preglob(str->text, 0, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
| 6723 | { |
| 6724 | int i = strlen(str->text); |
| 6725 | expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ |
| 6726 | } |
| 6727 | |
| 6728 | expmeta(expdir, p); |
| 6729 | free(expdir); |
| 6730 | if (p != str->text) |
| 6731 | free(p); |
| 6732 | INT_ON; |
| 6733 | if (exparg.lastp == savelastp) { |
| 6734 | /* |
| 6735 | * no matches |
| 6736 | */ |
| 6737 | nometa: |
| 6738 | *exparg.lastp = str; |
| 6739 | rmescapes(str->text); |
| 6740 | exparg.lastp = &str->next; |
| 6741 | } else { |
| 6742 | *exparg.lastp = NULL; |
| 6743 | *savelastp = sp = expsort(*savelastp); |
| 6744 | while (sp->next != NULL) |
| 6745 | sp = sp->next; |
| 6746 | exparg.lastp = &sp->next; |
| 6747 | } |
| 6748 | str = str->next; |
| 6749 | } |
| 6750 | } |
| 6751 | |
| 6752 | /* |
| 6753 | * Perform variable substitution and command substitution on an argument, |
| 6754 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 6755 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 6756 | * here document expansion. |
| 6757 | */ |
| 6758 | static void |
| 6759 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 6760 | { |
| 6761 | struct strlist *sp; |
| 6762 | char *p; |
| 6763 | |
| 6764 | argbackq = arg->narg.backquote; |
| 6765 | STARTSTACKSTR(expdest); |
| 6766 | ifsfirst.next = NULL; |
| 6767 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6768 | argstr(arg->narg.text, flag, |
| 6769 | /* var_str_list: */ arglist ? arglist->list : NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6770 | p = _STPUTC('\0', expdest); |
| 6771 | expdest = p - 1; |
| 6772 | if (arglist == NULL) { |
| 6773 | return; /* here document expanded */ |
| 6774 | } |
| 6775 | p = grabstackstr(p); |
| 6776 | exparg.lastp = &exparg.list; |
| 6777 | /* |
| 6778 | * TODO - EXP_REDIR |
| 6779 | */ |
| 6780 | if (flag & EXP_FULL) { |
| 6781 | ifsbreakup(p, &exparg); |
| 6782 | *exparg.lastp = NULL; |
| 6783 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 6784 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6785 | } else { |
| 6786 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ |
| 6787 | rmescapes(p); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6788 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6789 | sp->text = p; |
| 6790 | *exparg.lastp = sp; |
| 6791 | exparg.lastp = &sp->next; |
| 6792 | } |
| 6793 | if (ifsfirst.next) |
| 6794 | ifsfree(); |
| 6795 | *exparg.lastp = NULL; |
| 6796 | if (exparg.list) { |
| 6797 | *arglist->lastp = exparg.list; |
| 6798 | arglist->lastp = exparg.lastp; |
| 6799 | } |
| 6800 | } |
| 6801 | |
| 6802 | /* |
| 6803 | * Expand shell variables and backquotes inside a here document. |
| 6804 | */ |
| 6805 | static void |
| 6806 | expandhere(union node *arg, int fd) |
| 6807 | { |
| 6808 | herefd = fd; |
| 6809 | expandarg(arg, (struct arglist *)NULL, 0); |
| 6810 | full_write(fd, stackblock(), expdest - (char *)stackblock()); |
| 6811 | } |
| 6812 | |
| 6813 | /* |
| 6814 | * Returns true if the pattern matches the string. |
| 6815 | */ |
| 6816 | static int |
| 6817 | patmatch(char *pattern, const char *string) |
| 6818 | { |
| 6819 | return pmatch(preglob(pattern, 0, 0), string); |
| 6820 | } |
| 6821 | |
| 6822 | /* |
| 6823 | * See if a pattern matches in a case statement. |
| 6824 | */ |
| 6825 | static int |
| 6826 | casematch(union node *pattern, char *val) |
| 6827 | { |
| 6828 | struct stackmark smark; |
| 6829 | int result; |
| 6830 | |
| 6831 | setstackmark(&smark); |
| 6832 | argbackq = pattern->narg.backquote; |
| 6833 | STARTSTACKSTR(expdest); |
| 6834 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6835 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE, |
| 6836 | /* var_str_list: */ NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6837 | STACKSTRNUL(expdest); |
| 6838 | result = patmatch(stackblock(), val); |
| 6839 | popstackmark(&smark); |
| 6840 | return result; |
| 6841 | } |
| 6842 | |
| 6843 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6844 | /* ============ find_command */ |
| 6845 | |
| 6846 | struct builtincmd { |
| 6847 | const char *name; |
| 6848 | int (*builtin)(int, char **); |
| 6849 | /* unsigned flags; */ |
| 6850 | }; |
| 6851 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 6852 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 6853 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6854 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 6855 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6856 | |
| 6857 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 6858 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6859 | union param { |
| 6860 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 6861 | /* index >= 0 for commands without path (slashes) */ |
| 6862 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 6863 | /* index == -1 for commands with slashes */ |
| 6864 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6865 | const struct builtincmd *cmd; |
| 6866 | struct funcnode *func; |
| 6867 | } u; |
| 6868 | }; |
| 6869 | /* values of cmdtype */ |
| 6870 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 6871 | #define CMDNORMAL 0 /* command is an executable program */ |
| 6872 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 6873 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 6874 | |
| 6875 | /* action to find_command() */ |
| 6876 | #define DO_ERR 0x01 /* prints errors */ |
| 6877 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 6878 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 6879 | #define DO_ALTPATH 0x08 /* using alternate path */ |
| 6880 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ |
| 6881 | |
| 6882 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 6883 | |
| 6884 | |
| 6885 | /* ============ Hashing commands */ |
| 6886 | |
| 6887 | /* |
| 6888 | * When commands are first encountered, they are entered in a hash table. |
| 6889 | * This ensures that a full path search will not have to be done for them |
| 6890 | * on each invocation. |
| 6891 | * |
| 6892 | * We should investigate converting to a linear search, even though that |
| 6893 | * would make the command name "hash" a misnomer. |
| 6894 | */ |
| 6895 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6896 | #define ARB 1 /* actual size determined at run time */ |
| 6897 | |
| 6898 | struct tblentry { |
| 6899 | struct tblentry *next; /* next entry in hash chain */ |
| 6900 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 6901 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6902 | char rehash; /* if set, cd done since entry created */ |
| 6903 | char cmdname[ARB]; /* name of command */ |
| 6904 | }; |
| 6905 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 6906 | static struct tblentry **cmdtable; |
| 6907 | #define INIT_G_cmdtable() do { \ |
| 6908 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 6909 | } while (0) |
| 6910 | |
| 6911 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 6912 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6913 | |
| 6914 | static void |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 6915 | tryexec(USE_FEATURE_SH_STANDALONE(int applet_no,) char *cmd, char **argv, char **envp) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6916 | { |
| 6917 | int repeated = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6918 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 6919 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 6920 | if (applet_no >= 0) { |
| 6921 | if (APPLET_IS_NOEXEC(applet_no)) |
| 6922 | run_applet_no_and_exit(applet_no, argv); |
| 6923 | /* re-exec ourselves with the new arguments */ |
| 6924 | execve(bb_busybox_exec_path, argv, envp); |
| 6925 | /* If they called chroot or otherwise made the binary no longer |
| 6926 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6927 | } |
| 6928 | #endif |
| 6929 | |
| 6930 | repeat: |
| 6931 | #ifdef SYSV |
| 6932 | do { |
| 6933 | execve(cmd, argv, envp); |
| 6934 | } while (errno == EINTR); |
| 6935 | #else |
| 6936 | execve(cmd, argv, envp); |
| 6937 | #endif |
| 6938 | if (repeated++) { |
| 6939 | free(argv); |
| 6940 | } else if (errno == ENOEXEC) { |
| 6941 | char **ap; |
| 6942 | char **new; |
| 6943 | |
| 6944 | for (ap = argv; *ap; ap++) |
| 6945 | ; |
| 6946 | ap = new = ckmalloc((ap - argv + 2) * sizeof(char *)); |
| 6947 | ap[1] = cmd; |
Denis Vlasenko | c44ab01 | 2007-04-09 03:11:58 +0000 | [diff] [blame] | 6948 | ap[0] = cmd = (char *)DEFAULT_SHELL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6949 | ap += 2; |
| 6950 | argv++; |
| 6951 | while ((*ap++ = *argv++)) |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6952 | continue; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6953 | argv = new; |
| 6954 | goto repeat; |
| 6955 | } |
| 6956 | } |
| 6957 | |
| 6958 | /* |
| 6959 | * Exec a program. Never returns. If you change this routine, you may |
| 6960 | * have to change the find_command routine as well. |
| 6961 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6962 | static void shellexec(char **, const char *, int) ATTRIBUTE_NORETURN; |
| 6963 | static void |
| 6964 | shellexec(char **argv, const char *path, int idx) |
| 6965 | { |
| 6966 | char *cmdname; |
| 6967 | int e; |
| 6968 | char **envp; |
| 6969 | int exerrno; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 6970 | #if ENABLE_FEATURE_SH_STANDALONE |
| 6971 | int applet_no = -1; |
| 6972 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6973 | |
| 6974 | clearredir(1); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 6975 | envp = listvars(VEXPORT, VUNSET, 0); |
| 6976 | if (strchr(argv[0], '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 6977 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 6978 | || (applet_no = find_applet_by_name(argv[0])) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6979 | #endif |
| 6980 | ) { |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 6981 | tryexec(USE_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6982 | e = errno; |
| 6983 | } else { |
| 6984 | e = ENOENT; |
| 6985 | while ((cmdname = padvance(&path, argv[0])) != NULL) { |
| 6986 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 6987 | tryexec(USE_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 6988 | if (errno != ENOENT && errno != ENOTDIR) |
| 6989 | e = errno; |
| 6990 | } |
| 6991 | stunalloc(cmdname); |
| 6992 | } |
| 6993 | } |
| 6994 | |
| 6995 | /* Map to POSIX errors */ |
| 6996 | switch (e) { |
| 6997 | case EACCES: |
| 6998 | exerrno = 126; |
| 6999 | break; |
| 7000 | case ENOENT: |
| 7001 | exerrno = 127; |
| 7002 | break; |
| 7003 | default: |
| 7004 | exerrno = 2; |
| 7005 | break; |
| 7006 | } |
| 7007 | exitstatus = exerrno; |
| 7008 | TRACE(("shellexec failed for %s, errno %d, suppressint %d\n", |
| 7009 | argv[0], e, suppressint )); |
| 7010 | ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found")); |
| 7011 | /* NOTREACHED */ |
| 7012 | } |
| 7013 | |
| 7014 | static void |
| 7015 | printentry(struct tblentry *cmdp) |
| 7016 | { |
| 7017 | int idx; |
| 7018 | const char *path; |
| 7019 | char *name; |
| 7020 | |
| 7021 | idx = cmdp->param.index; |
| 7022 | path = pathval(); |
| 7023 | do { |
| 7024 | name = padvance(&path, cmdp->cmdname); |
| 7025 | stunalloc(name); |
| 7026 | } while (--idx >= 0); |
| 7027 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 7028 | } |
| 7029 | |
| 7030 | /* |
| 7031 | * Clear out command entries. The argument specifies the first entry in |
| 7032 | * PATH which has changed. |
| 7033 | */ |
| 7034 | static void |
| 7035 | clearcmdentry(int firstchange) |
| 7036 | { |
| 7037 | struct tblentry **tblp; |
| 7038 | struct tblentry **pp; |
| 7039 | struct tblentry *cmdp; |
| 7040 | |
| 7041 | INT_OFF; |
| 7042 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 7043 | pp = tblp; |
| 7044 | while ((cmdp = *pp) != NULL) { |
| 7045 | if ((cmdp->cmdtype == CMDNORMAL && |
| 7046 | cmdp->param.index >= firstchange) |
| 7047 | || (cmdp->cmdtype == CMDBUILTIN && |
| 7048 | builtinloc >= firstchange) |
| 7049 | ) { |
| 7050 | *pp = cmdp->next; |
| 7051 | free(cmdp); |
| 7052 | } else { |
| 7053 | pp = &cmdp->next; |
| 7054 | } |
| 7055 | } |
| 7056 | } |
| 7057 | INT_ON; |
| 7058 | } |
| 7059 | |
| 7060 | /* |
| 7061 | * Locate a command in the command hash table. If "add" is nonzero, |
| 7062 | * add the command to the table if it is not already present. The |
| 7063 | * variable "lastcmdentry" is set to point to the address of the link |
| 7064 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 7065 | * entry. |
| 7066 | * |
| 7067 | * Interrupts must be off if called with add != 0. |
| 7068 | */ |
| 7069 | static struct tblentry **lastcmdentry; |
| 7070 | |
| 7071 | static struct tblentry * |
| 7072 | cmdlookup(const char *name, int add) |
| 7073 | { |
| 7074 | unsigned int hashval; |
| 7075 | const char *p; |
| 7076 | struct tblentry *cmdp; |
| 7077 | struct tblentry **pp; |
| 7078 | |
| 7079 | p = name; |
| 7080 | hashval = (unsigned char)*p << 4; |
| 7081 | while (*p) |
| 7082 | hashval += (unsigned char)*p++; |
| 7083 | hashval &= 0x7FFF; |
| 7084 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 7085 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7086 | if (strcmp(cmdp->cmdname, name) == 0) |
| 7087 | break; |
| 7088 | pp = &cmdp->next; |
| 7089 | } |
| 7090 | if (add && cmdp == NULL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7091 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) - ARB |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7092 | + strlen(name) + 1); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7093 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7094 | cmdp->cmdtype = CMDUNKNOWN; |
| 7095 | strcpy(cmdp->cmdname, name); |
| 7096 | } |
| 7097 | lastcmdentry = pp; |
| 7098 | return cmdp; |
| 7099 | } |
| 7100 | |
| 7101 | /* |
| 7102 | * Delete the command entry returned on the last lookup. |
| 7103 | */ |
| 7104 | static void |
| 7105 | delete_cmd_entry(void) |
| 7106 | { |
| 7107 | struct tblentry *cmdp; |
| 7108 | |
| 7109 | INT_OFF; |
| 7110 | cmdp = *lastcmdentry; |
| 7111 | *lastcmdentry = cmdp->next; |
| 7112 | if (cmdp->cmdtype == CMDFUNCTION) |
| 7113 | freefunc(cmdp->param.func); |
| 7114 | free(cmdp); |
| 7115 | INT_ON; |
| 7116 | } |
| 7117 | |
| 7118 | /* |
| 7119 | * Add a new command entry, replacing any existing command entry for |
| 7120 | * the same name - except special builtins. |
| 7121 | */ |
| 7122 | static void |
| 7123 | addcmdentry(char *name, struct cmdentry *entry) |
| 7124 | { |
| 7125 | struct tblentry *cmdp; |
| 7126 | |
| 7127 | cmdp = cmdlookup(name, 1); |
| 7128 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 7129 | freefunc(cmdp->param.func); |
| 7130 | } |
| 7131 | cmdp->cmdtype = entry->cmdtype; |
| 7132 | cmdp->param = entry->u; |
| 7133 | cmdp->rehash = 0; |
| 7134 | } |
| 7135 | |
| 7136 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7137 | hashcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7138 | { |
| 7139 | struct tblentry **pp; |
| 7140 | struct tblentry *cmdp; |
| 7141 | int c; |
| 7142 | struct cmdentry entry; |
| 7143 | char *name; |
| 7144 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7145 | if (nextopt("r") != '\0') { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7146 | clearcmdentry(0); |
| 7147 | return 0; |
| 7148 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7149 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7150 | if (*argptr == NULL) { |
| 7151 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7152 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7153 | if (cmdp->cmdtype == CMDNORMAL) |
| 7154 | printentry(cmdp); |
| 7155 | } |
| 7156 | } |
| 7157 | return 0; |
| 7158 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7159 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7160 | c = 0; |
| 7161 | while ((name = *argptr) != NULL) { |
| 7162 | cmdp = cmdlookup(name, 0); |
| 7163 | if (cmdp != NULL |
| 7164 | && (cmdp->cmdtype == CMDNORMAL |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7165 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)) |
| 7166 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7167 | delete_cmd_entry(); |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7168 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7169 | find_command(name, &entry, DO_ERR, pathval()); |
| 7170 | if (entry.cmdtype == CMDUNKNOWN) |
| 7171 | c = 1; |
| 7172 | argptr++; |
| 7173 | } |
| 7174 | return c; |
| 7175 | } |
| 7176 | |
| 7177 | /* |
| 7178 | * Called when a cd is done. Marks all commands so the next time they |
| 7179 | * are executed they will be rehashed. |
| 7180 | */ |
| 7181 | static void |
| 7182 | hashcd(void) |
| 7183 | { |
| 7184 | struct tblentry **pp; |
| 7185 | struct tblentry *cmdp; |
| 7186 | |
| 7187 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7188 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7189 | if (cmdp->cmdtype == CMDNORMAL |
| 7190 | || (cmdp->cmdtype == CMDBUILTIN |
| 7191 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 7192 | && builtinloc > 0) |
| 7193 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7194 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7195 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7196 | } |
| 7197 | } |
| 7198 | } |
| 7199 | |
| 7200 | /* |
| 7201 | * Fix command hash table when PATH changed. |
| 7202 | * Called before PATH is changed. The argument is the new value of PATH; |
| 7203 | * pathval() still returns the old value at this point. |
| 7204 | * Called with interrupts off. |
| 7205 | */ |
| 7206 | static void |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7207 | changepath(const char *new) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7208 | { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7209 | const char *old; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7210 | int firstchange; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7211 | int idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7212 | int idx_bltin; |
| 7213 | |
| 7214 | old = pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7215 | firstchange = 9999; /* assume no change */ |
| 7216 | idx = 0; |
| 7217 | idx_bltin = -1; |
| 7218 | for (;;) { |
| 7219 | if (*old != *new) { |
| 7220 | firstchange = idx; |
| 7221 | if ((*old == '\0' && *new == ':') |
| 7222 | || (*old == ':' && *new == '\0')) |
| 7223 | firstchange++; |
| 7224 | old = new; /* ignore subsequent differences */ |
| 7225 | } |
| 7226 | if (*new == '\0') |
| 7227 | break; |
| 7228 | if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin")) |
| 7229 | idx_bltin = idx; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7230 | if (*new == ':') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7231 | idx++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7232 | new++, old++; |
| 7233 | } |
| 7234 | if (builtinloc < 0 && idx_bltin >= 0) |
| 7235 | builtinloc = idx_bltin; /* zap builtins */ |
| 7236 | if (builtinloc >= 0 && idx_bltin < 0) |
| 7237 | firstchange = 0; |
| 7238 | clearcmdentry(firstchange); |
| 7239 | builtinloc = idx_bltin; |
| 7240 | } |
| 7241 | |
| 7242 | #define TEOF 0 |
| 7243 | #define TNL 1 |
| 7244 | #define TREDIR 2 |
| 7245 | #define TWORD 3 |
| 7246 | #define TSEMI 4 |
| 7247 | #define TBACKGND 5 |
| 7248 | #define TAND 6 |
| 7249 | #define TOR 7 |
| 7250 | #define TPIPE 8 |
| 7251 | #define TLP 9 |
| 7252 | #define TRP 10 |
| 7253 | #define TENDCASE 11 |
| 7254 | #define TENDBQUOTE 12 |
| 7255 | #define TNOT 13 |
| 7256 | #define TCASE 14 |
| 7257 | #define TDO 15 |
| 7258 | #define TDONE 16 |
| 7259 | #define TELIF 17 |
| 7260 | #define TELSE 18 |
| 7261 | #define TESAC 19 |
| 7262 | #define TFI 20 |
| 7263 | #define TFOR 21 |
| 7264 | #define TIF 22 |
| 7265 | #define TIN 23 |
| 7266 | #define TTHEN 24 |
| 7267 | #define TUNTIL 25 |
| 7268 | #define TWHILE 26 |
| 7269 | #define TBEGIN 27 |
| 7270 | #define TEND 28 |
| 7271 | |
| 7272 | /* first char is indicating which tokens mark the end of a list */ |
| 7273 | static const char *const tokname_array[] = { |
| 7274 | "\1end of file", |
| 7275 | "\0newline", |
| 7276 | "\0redirection", |
| 7277 | "\0word", |
| 7278 | "\0;", |
| 7279 | "\0&", |
| 7280 | "\0&&", |
| 7281 | "\0||", |
| 7282 | "\0|", |
| 7283 | "\0(", |
| 7284 | "\1)", |
| 7285 | "\1;;", |
| 7286 | "\1`", |
| 7287 | #define KWDOFFSET 13 |
| 7288 | /* the following are keywords */ |
| 7289 | "\0!", |
| 7290 | "\0case", |
| 7291 | "\1do", |
| 7292 | "\1done", |
| 7293 | "\1elif", |
| 7294 | "\1else", |
| 7295 | "\1esac", |
| 7296 | "\1fi", |
| 7297 | "\0for", |
| 7298 | "\0if", |
| 7299 | "\0in", |
| 7300 | "\1then", |
| 7301 | "\0until", |
| 7302 | "\0while", |
| 7303 | "\0{", |
| 7304 | "\1}", |
| 7305 | }; |
| 7306 | |
| 7307 | static const char * |
| 7308 | tokname(int tok) |
| 7309 | { |
| 7310 | static char buf[16]; |
| 7311 | |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7312 | //try this: |
| 7313 | //if (tok < TSEMI) return tokname_array[tok] + 1; |
| 7314 | //sprintf(buf, "\"%s\"", tokname_array[tok] + 1); |
| 7315 | //return buf; |
| 7316 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7317 | if (tok >= TSEMI) |
| 7318 | buf[0] = '"'; |
| 7319 | sprintf(buf + (tok >= TSEMI), "%s%c", |
| 7320 | tokname_array[tok] + 1, (tok >= TSEMI ? '"' : 0)); |
| 7321 | return buf; |
| 7322 | } |
| 7323 | |
| 7324 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 7325 | static int |
| 7326 | pstrcmp(const void *a, const void *b) |
| 7327 | { |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7328 | return strcmp((char*) a, (*(char**) b) + 1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7329 | } |
| 7330 | |
| 7331 | static const char *const * |
| 7332 | findkwd(const char *s) |
| 7333 | { |
| 7334 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 7335 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 7336 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7337 | } |
| 7338 | |
| 7339 | /* |
| 7340 | * Locate and print what a word is... |
| 7341 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7342 | static int |
| 7343 | describe_command(char *command, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7344 | { |
| 7345 | struct cmdentry entry; |
| 7346 | struct tblentry *cmdp; |
| 7347 | #if ENABLE_ASH_ALIAS |
| 7348 | const struct alias *ap; |
| 7349 | #endif |
| 7350 | const char *path = pathval(); |
| 7351 | |
| 7352 | if (describe_command_verbose) { |
| 7353 | out1str(command); |
| 7354 | } |
| 7355 | |
| 7356 | /* First look at the keywords */ |
| 7357 | if (findkwd(command)) { |
| 7358 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 7359 | goto out; |
| 7360 | } |
| 7361 | |
| 7362 | #if ENABLE_ASH_ALIAS |
| 7363 | /* Then look at the aliases */ |
| 7364 | ap = lookupalias(command, 0); |
| 7365 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7366 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7367 | out1str("alias "); |
| 7368 | printalias(ap); |
| 7369 | return 0; |
| 7370 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7371 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7372 | goto out; |
| 7373 | } |
| 7374 | #endif |
| 7375 | /* Then check if it is a tracked alias */ |
| 7376 | cmdp = cmdlookup(command, 0); |
| 7377 | if (cmdp != NULL) { |
| 7378 | entry.cmdtype = cmdp->cmdtype; |
| 7379 | entry.u = cmdp->param; |
| 7380 | } else { |
| 7381 | /* Finally use brute force */ |
| 7382 | find_command(command, &entry, DO_ABS, path); |
| 7383 | } |
| 7384 | |
| 7385 | switch (entry.cmdtype) { |
| 7386 | case CMDNORMAL: { |
| 7387 | int j = entry.u.index; |
| 7388 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7389 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7390 | p = command; |
| 7391 | } else { |
| 7392 | do { |
| 7393 | p = padvance(&path, command); |
| 7394 | stunalloc(p); |
| 7395 | } while (--j >= 0); |
| 7396 | } |
| 7397 | if (describe_command_verbose) { |
| 7398 | out1fmt(" is%s %s", |
| 7399 | (cmdp ? " a tracked alias for" : nullstr), p |
| 7400 | ); |
| 7401 | } else { |
| 7402 | out1str(p); |
| 7403 | } |
| 7404 | break; |
| 7405 | } |
| 7406 | |
| 7407 | case CMDFUNCTION: |
| 7408 | if (describe_command_verbose) { |
| 7409 | out1str(" is a shell function"); |
| 7410 | } else { |
| 7411 | out1str(command); |
| 7412 | } |
| 7413 | break; |
| 7414 | |
| 7415 | case CMDBUILTIN: |
| 7416 | if (describe_command_verbose) { |
| 7417 | out1fmt(" is a %sshell builtin", |
| 7418 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 7419 | "special " : nullstr |
| 7420 | ); |
| 7421 | } else { |
| 7422 | out1str(command); |
| 7423 | } |
| 7424 | break; |
| 7425 | |
| 7426 | default: |
| 7427 | if (describe_command_verbose) { |
| 7428 | out1str(": not found\n"); |
| 7429 | } |
| 7430 | return 127; |
| 7431 | } |
| 7432 | out: |
| 7433 | outstr("\n", stdout); |
| 7434 | return 0; |
| 7435 | } |
| 7436 | |
| 7437 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7438 | typecmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7439 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7440 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7441 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7442 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7443 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7444 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 7445 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7446 | i++; |
| 7447 | verbose = 0; |
| 7448 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7449 | while (argv[i]) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7450 | err |= describe_command(argv[i++], verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7451 | } |
| 7452 | return err; |
| 7453 | } |
| 7454 | |
| 7455 | #if ENABLE_ASH_CMDCMD |
| 7456 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7457 | commandcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7458 | { |
| 7459 | int c; |
| 7460 | enum { |
| 7461 | VERIFY_BRIEF = 1, |
| 7462 | VERIFY_VERBOSE = 2, |
| 7463 | } verify = 0; |
| 7464 | |
| 7465 | while ((c = nextopt("pvV")) != '\0') |
| 7466 | if (c == 'V') |
| 7467 | verify |= VERIFY_VERBOSE; |
| 7468 | else if (c == 'v') |
| 7469 | verify |= VERIFY_BRIEF; |
| 7470 | #if DEBUG |
| 7471 | else if (c != 'p') |
| 7472 | abort(); |
| 7473 | #endif |
| 7474 | if (verify) |
| 7475 | return describe_command(*argptr, verify - VERIFY_BRIEF); |
| 7476 | |
| 7477 | return 0; |
| 7478 | } |
| 7479 | #endif |
| 7480 | |
| 7481 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7482 | /* ============ eval.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7483 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7484 | static int funcblocksize; /* size of structures in function */ |
| 7485 | static int funcstringsize; /* size of strings in node */ |
| 7486 | static void *funcblock; /* block to allocate function from */ |
| 7487 | static char *funcstring; /* block to allocate strings from */ |
| 7488 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7489 | /* flags in argument to evaltree */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7490 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 7491 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
| 7492 | #define EV_BACKCMD 04 /* command executing within back quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7493 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7494 | static const short nodesize[26] = { |
| 7495 | SHELL_ALIGN(sizeof(struct ncmd)), |
| 7496 | SHELL_ALIGN(sizeof(struct npipe)), |
| 7497 | SHELL_ALIGN(sizeof(struct nredir)), |
| 7498 | SHELL_ALIGN(sizeof(struct nredir)), |
| 7499 | SHELL_ALIGN(sizeof(struct nredir)), |
| 7500 | SHELL_ALIGN(sizeof(struct nbinary)), |
| 7501 | SHELL_ALIGN(sizeof(struct nbinary)), |
| 7502 | SHELL_ALIGN(sizeof(struct nbinary)), |
| 7503 | SHELL_ALIGN(sizeof(struct nif)), |
| 7504 | SHELL_ALIGN(sizeof(struct nbinary)), |
| 7505 | SHELL_ALIGN(sizeof(struct nbinary)), |
| 7506 | SHELL_ALIGN(sizeof(struct nfor)), |
| 7507 | SHELL_ALIGN(sizeof(struct ncase)), |
| 7508 | SHELL_ALIGN(sizeof(struct nclist)), |
| 7509 | SHELL_ALIGN(sizeof(struct narg)), |
| 7510 | SHELL_ALIGN(sizeof(struct narg)), |
| 7511 | SHELL_ALIGN(sizeof(struct nfile)), |
| 7512 | SHELL_ALIGN(sizeof(struct nfile)), |
| 7513 | SHELL_ALIGN(sizeof(struct nfile)), |
| 7514 | SHELL_ALIGN(sizeof(struct nfile)), |
| 7515 | SHELL_ALIGN(sizeof(struct nfile)), |
| 7516 | SHELL_ALIGN(sizeof(struct ndup)), |
| 7517 | SHELL_ALIGN(sizeof(struct ndup)), |
| 7518 | SHELL_ALIGN(sizeof(struct nhere)), |
| 7519 | SHELL_ALIGN(sizeof(struct nhere)), |
| 7520 | SHELL_ALIGN(sizeof(struct nnot)), |
| 7521 | }; |
| 7522 | |
| 7523 | static void calcsize(union node *n); |
| 7524 | |
| 7525 | static void |
| 7526 | sizenodelist(struct nodelist *lp) |
| 7527 | { |
| 7528 | while (lp) { |
| 7529 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
| 7530 | calcsize(lp->n); |
| 7531 | lp = lp->next; |
| 7532 | } |
| 7533 | } |
| 7534 | |
| 7535 | static void |
| 7536 | calcsize(union node *n) |
| 7537 | { |
| 7538 | if (n == NULL) |
| 7539 | return; |
| 7540 | funcblocksize += nodesize[n->type]; |
| 7541 | switch (n->type) { |
| 7542 | case NCMD: |
| 7543 | calcsize(n->ncmd.redirect); |
| 7544 | calcsize(n->ncmd.args); |
| 7545 | calcsize(n->ncmd.assign); |
| 7546 | break; |
| 7547 | case NPIPE: |
| 7548 | sizenodelist(n->npipe.cmdlist); |
| 7549 | break; |
| 7550 | case NREDIR: |
| 7551 | case NBACKGND: |
| 7552 | case NSUBSHELL: |
| 7553 | calcsize(n->nredir.redirect); |
| 7554 | calcsize(n->nredir.n); |
| 7555 | break; |
| 7556 | case NAND: |
| 7557 | case NOR: |
| 7558 | case NSEMI: |
| 7559 | case NWHILE: |
| 7560 | case NUNTIL: |
| 7561 | calcsize(n->nbinary.ch2); |
| 7562 | calcsize(n->nbinary.ch1); |
| 7563 | break; |
| 7564 | case NIF: |
| 7565 | calcsize(n->nif.elsepart); |
| 7566 | calcsize(n->nif.ifpart); |
| 7567 | calcsize(n->nif.test); |
| 7568 | break; |
| 7569 | case NFOR: |
| 7570 | funcstringsize += strlen(n->nfor.var) + 1; |
| 7571 | calcsize(n->nfor.body); |
| 7572 | calcsize(n->nfor.args); |
| 7573 | break; |
| 7574 | case NCASE: |
| 7575 | calcsize(n->ncase.cases); |
| 7576 | calcsize(n->ncase.expr); |
| 7577 | break; |
| 7578 | case NCLIST: |
| 7579 | calcsize(n->nclist.body); |
| 7580 | calcsize(n->nclist.pattern); |
| 7581 | calcsize(n->nclist.next); |
| 7582 | break; |
| 7583 | case NDEFUN: |
| 7584 | case NARG: |
| 7585 | sizenodelist(n->narg.backquote); |
| 7586 | funcstringsize += strlen(n->narg.text) + 1; |
| 7587 | calcsize(n->narg.next); |
| 7588 | break; |
| 7589 | case NTO: |
| 7590 | case NCLOBBER: |
| 7591 | case NFROM: |
| 7592 | case NFROMTO: |
| 7593 | case NAPPEND: |
| 7594 | calcsize(n->nfile.fname); |
| 7595 | calcsize(n->nfile.next); |
| 7596 | break; |
| 7597 | case NTOFD: |
| 7598 | case NFROMFD: |
| 7599 | calcsize(n->ndup.vname); |
| 7600 | calcsize(n->ndup.next); |
| 7601 | break; |
| 7602 | case NHERE: |
| 7603 | case NXHERE: |
| 7604 | calcsize(n->nhere.doc); |
| 7605 | calcsize(n->nhere.next); |
| 7606 | break; |
| 7607 | case NNOT: |
| 7608 | calcsize(n->nnot.com); |
| 7609 | break; |
| 7610 | }; |
| 7611 | } |
| 7612 | |
| 7613 | static char * |
| 7614 | nodeckstrdup(char *s) |
| 7615 | { |
| 7616 | char *rtn = funcstring; |
| 7617 | |
| 7618 | strcpy(funcstring, s); |
| 7619 | funcstring += strlen(s) + 1; |
| 7620 | return rtn; |
| 7621 | } |
| 7622 | |
| 7623 | static union node *copynode(union node *); |
| 7624 | |
| 7625 | static struct nodelist * |
| 7626 | copynodelist(struct nodelist *lp) |
| 7627 | { |
| 7628 | struct nodelist *start; |
| 7629 | struct nodelist **lpp; |
| 7630 | |
| 7631 | lpp = &start; |
| 7632 | while (lp) { |
| 7633 | *lpp = funcblock; |
| 7634 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 7635 | (*lpp)->n = copynode(lp->n); |
| 7636 | lp = lp->next; |
| 7637 | lpp = &(*lpp)->next; |
| 7638 | } |
| 7639 | *lpp = NULL; |
| 7640 | return start; |
| 7641 | } |
| 7642 | |
| 7643 | static union node * |
| 7644 | copynode(union node *n) |
| 7645 | { |
| 7646 | union node *new; |
| 7647 | |
| 7648 | if (n == NULL) |
| 7649 | return NULL; |
| 7650 | new = funcblock; |
| 7651 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 7652 | |
| 7653 | switch (n->type) { |
| 7654 | case NCMD: |
| 7655 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 7656 | new->ncmd.args = copynode(n->ncmd.args); |
| 7657 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 7658 | break; |
| 7659 | case NPIPE: |
| 7660 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
| 7661 | new->npipe.backgnd = n->npipe.backgnd; |
| 7662 | break; |
| 7663 | case NREDIR: |
| 7664 | case NBACKGND: |
| 7665 | case NSUBSHELL: |
| 7666 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 7667 | new->nredir.n = copynode(n->nredir.n); |
| 7668 | break; |
| 7669 | case NAND: |
| 7670 | case NOR: |
| 7671 | case NSEMI: |
| 7672 | case NWHILE: |
| 7673 | case NUNTIL: |
| 7674 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 7675 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 7676 | break; |
| 7677 | case NIF: |
| 7678 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 7679 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 7680 | new->nif.test = copynode(n->nif.test); |
| 7681 | break; |
| 7682 | case NFOR: |
| 7683 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 7684 | new->nfor.body = copynode(n->nfor.body); |
| 7685 | new->nfor.args = copynode(n->nfor.args); |
| 7686 | break; |
| 7687 | case NCASE: |
| 7688 | new->ncase.cases = copynode(n->ncase.cases); |
| 7689 | new->ncase.expr = copynode(n->ncase.expr); |
| 7690 | break; |
| 7691 | case NCLIST: |
| 7692 | new->nclist.body = copynode(n->nclist.body); |
| 7693 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 7694 | new->nclist.next = copynode(n->nclist.next); |
| 7695 | break; |
| 7696 | case NDEFUN: |
| 7697 | case NARG: |
| 7698 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 7699 | new->narg.text = nodeckstrdup(n->narg.text); |
| 7700 | new->narg.next = copynode(n->narg.next); |
| 7701 | break; |
| 7702 | case NTO: |
| 7703 | case NCLOBBER: |
| 7704 | case NFROM: |
| 7705 | case NFROMTO: |
| 7706 | case NAPPEND: |
| 7707 | new->nfile.fname = copynode(n->nfile.fname); |
| 7708 | new->nfile.fd = n->nfile.fd; |
| 7709 | new->nfile.next = copynode(n->nfile.next); |
| 7710 | break; |
| 7711 | case NTOFD: |
| 7712 | case NFROMFD: |
| 7713 | new->ndup.vname = copynode(n->ndup.vname); |
| 7714 | new->ndup.dupfd = n->ndup.dupfd; |
| 7715 | new->ndup.fd = n->ndup.fd; |
| 7716 | new->ndup.next = copynode(n->ndup.next); |
| 7717 | break; |
| 7718 | case NHERE: |
| 7719 | case NXHERE: |
| 7720 | new->nhere.doc = copynode(n->nhere.doc); |
| 7721 | new->nhere.fd = n->nhere.fd; |
| 7722 | new->nhere.next = copynode(n->nhere.next); |
| 7723 | break; |
| 7724 | case NNOT: |
| 7725 | new->nnot.com = copynode(n->nnot.com); |
| 7726 | break; |
| 7727 | }; |
| 7728 | new->type = n->type; |
| 7729 | return new; |
| 7730 | } |
| 7731 | |
| 7732 | /* |
| 7733 | * Make a copy of a parse tree. |
| 7734 | */ |
| 7735 | static struct funcnode * |
| 7736 | copyfunc(union node *n) |
| 7737 | { |
| 7738 | struct funcnode *f; |
| 7739 | size_t blocksize; |
| 7740 | |
| 7741 | funcblocksize = offsetof(struct funcnode, n); |
| 7742 | funcstringsize = 0; |
| 7743 | calcsize(n); |
| 7744 | blocksize = funcblocksize; |
| 7745 | f = ckmalloc(blocksize + funcstringsize); |
| 7746 | funcblock = (char *) f + offsetof(struct funcnode, n); |
| 7747 | funcstring = (char *) f + blocksize; |
| 7748 | copynode(n); |
| 7749 | f->count = 0; |
| 7750 | return f; |
| 7751 | } |
| 7752 | |
| 7753 | /* |
| 7754 | * Define a shell function. |
| 7755 | */ |
| 7756 | static void |
| 7757 | defun(char *name, union node *func) |
| 7758 | { |
| 7759 | struct cmdentry entry; |
| 7760 | |
| 7761 | INT_OFF; |
| 7762 | entry.cmdtype = CMDFUNCTION; |
| 7763 | entry.u.func = copyfunc(func); |
| 7764 | addcmdentry(name, &entry); |
| 7765 | INT_ON; |
| 7766 | } |
| 7767 | |
| 7768 | static int evalskip; /* set if we are skipping commands */ |
| 7769 | /* reasons for skipping commands (see comment on breakcmd routine) */ |
| 7770 | #define SKIPBREAK (1 << 0) |
| 7771 | #define SKIPCONT (1 << 1) |
| 7772 | #define SKIPFUNC (1 << 2) |
| 7773 | #define SKIPFILE (1 << 3) |
| 7774 | #define SKIPEVAL (1 << 4) |
| 7775 | static int skipcount; /* number of levels to skip */ |
| 7776 | static int funcnest; /* depth of function calls */ |
| 7777 | |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 7778 | /* forward decl way out to parsing code - dotrap needs it */ |
| 7779 | static int evalstring(char *s, int mask); |
| 7780 | |
| 7781 | /* |
| 7782 | * Called to execute a trap. Perhaps we should avoid entering new trap |
| 7783 | * handlers while we are executing a trap handler. |
| 7784 | */ |
| 7785 | static int |
| 7786 | dotrap(void) |
| 7787 | { |
| 7788 | char *p; |
| 7789 | char *q; |
| 7790 | int i; |
| 7791 | int savestatus; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 7792 | int skip; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 7793 | |
| 7794 | savestatus = exitstatus; |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 7795 | pendingsig = 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 7796 | xbarrier(); |
| 7797 | |
| 7798 | for (i = 0, q = gotsig; i < NSIG - 1; i++, q++) { |
| 7799 | if (!*q) |
| 7800 | continue; |
| 7801 | *q = '\0'; |
| 7802 | |
| 7803 | p = trap[i + 1]; |
| 7804 | if (!p) |
| 7805 | continue; |
| 7806 | skip = evalstring(p, SKIPEVAL); |
| 7807 | exitstatus = savestatus; |
| 7808 | if (skip) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 7809 | return skip; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 7810 | } |
| 7811 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 7812 | return 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 7813 | } |
| 7814 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7815 | /* forward declarations - evaluation is fairly recursive business... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7816 | static void evalloop(union node *, int); |
| 7817 | static void evalfor(union node *, int); |
| 7818 | static void evalcase(union node *, int); |
| 7819 | static void evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 7820 | static void expredir(union node *); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7821 | static void evalpipe(union node *, int); |
| 7822 | static void evalcommand(union node *, int); |
| 7823 | static int evalbltin(const struct builtincmd *, int, char **); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 7824 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 7825 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7826 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7827 | * Evaluate a parse tree. The value is left in the global variable |
| 7828 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7829 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7830 | static void |
| 7831 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7832 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7833 | int checkexit = 0; |
| 7834 | void (*evalfn)(union node *, int); |
| 7835 | unsigned isor; |
| 7836 | int status; |
| 7837 | if (n == NULL) { |
| 7838 | TRACE(("evaltree(NULL) called\n")); |
| 7839 | goto out; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7840 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7841 | TRACE(("pid %d, evaltree(%p: %d, %d) called\n", |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 7842 | getpid(), n, n->type, flags)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7843 | switch (n->type) { |
| 7844 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 7845 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7846 | out1fmt("Node type = %d\n", n->type); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 7847 | fflush(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7848 | break; |
| 7849 | #endif |
| 7850 | case NNOT: |
| 7851 | evaltree(n->nnot.com, EV_TESTED); |
| 7852 | status = !exitstatus; |
| 7853 | goto setstatus; |
| 7854 | case NREDIR: |
| 7855 | expredir(n->nredir.redirect); |
| 7856 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 7857 | if (!status) { |
| 7858 | evaltree(n->nredir.n, flags & EV_TESTED); |
| 7859 | status = exitstatus; |
| 7860 | } |
| 7861 | popredir(0); |
| 7862 | goto setstatus; |
| 7863 | case NCMD: |
| 7864 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7865 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7866 | if (eflag && !(flags & EV_TESTED)) |
| 7867 | checkexit = ~0; |
| 7868 | goto calleval; |
| 7869 | case NFOR: |
| 7870 | evalfn = evalfor; |
| 7871 | goto calleval; |
| 7872 | case NWHILE: |
| 7873 | case NUNTIL: |
| 7874 | evalfn = evalloop; |
| 7875 | goto calleval; |
| 7876 | case NSUBSHELL: |
| 7877 | case NBACKGND: |
| 7878 | evalfn = evalsubshell; |
| 7879 | goto calleval; |
| 7880 | case NPIPE: |
| 7881 | evalfn = evalpipe; |
| 7882 | goto checkexit; |
| 7883 | case NCASE: |
| 7884 | evalfn = evalcase; |
| 7885 | goto calleval; |
| 7886 | case NAND: |
| 7887 | case NOR: |
| 7888 | case NSEMI: |
| 7889 | #if NAND + 1 != NOR |
| 7890 | #error NAND + 1 != NOR |
| 7891 | #endif |
| 7892 | #if NOR + 1 != NSEMI |
| 7893 | #error NOR + 1 != NSEMI |
| 7894 | #endif |
| 7895 | isor = n->type - NAND; |
| 7896 | evaltree( |
| 7897 | n->nbinary.ch1, |
| 7898 | (flags | ((isor >> 1) - 1)) & EV_TESTED |
| 7899 | ); |
| 7900 | if (!exitstatus == isor) |
| 7901 | break; |
| 7902 | if (!evalskip) { |
| 7903 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7904 | evaln: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7905 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7906 | calleval: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7907 | evalfn(n, flags); |
| 7908 | break; |
| 7909 | } |
| 7910 | break; |
| 7911 | case NIF: |
| 7912 | evaltree(n->nif.test, EV_TESTED); |
| 7913 | if (evalskip) |
| 7914 | break; |
| 7915 | if (exitstatus == 0) { |
| 7916 | n = n->nif.ifpart; |
| 7917 | goto evaln; |
| 7918 | } else if (n->nif.elsepart) { |
| 7919 | n = n->nif.elsepart; |
| 7920 | goto evaln; |
| 7921 | } |
| 7922 | goto success; |
| 7923 | case NDEFUN: |
| 7924 | defun(n->narg.text, n->narg.next); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7925 | success: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7926 | status = 0; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7927 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7928 | exitstatus = status; |
| 7929 | break; |
| 7930 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7931 | out: |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7932 | if ((checkexit & exitstatus)) |
| 7933 | evalskip |= SKIPEVAL; |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 7934 | else if (pendingsig && dotrap()) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7935 | goto exexit; |
| 7936 | |
| 7937 | if (flags & EV_EXIT) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7938 | exexit: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7939 | raise_exception(EXEXIT); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7940 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7941 | } |
| 7942 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7943 | #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3) |
| 7944 | static |
| 7945 | #endif |
| 7946 | void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__)); |
| 7947 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 7948 | static int loopnest; /* current loop nesting level */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7949 | |
| 7950 | static void |
| 7951 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7952 | { |
| 7953 | int status; |
| 7954 | |
| 7955 | loopnest++; |
| 7956 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 7957 | flags &= EV_TESTED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7958 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7959 | int i; |
| 7960 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7961 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 7962 | if (evalskip) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7963 | skipping: |
| 7964 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7965 | evalskip = 0; |
| 7966 | continue; |
| 7967 | } |
| 7968 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 7969 | evalskip = 0; |
| 7970 | break; |
| 7971 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7972 | i = exitstatus; |
| 7973 | if (n->type != NWHILE) |
| 7974 | i = !i; |
| 7975 | if (i != 0) |
| 7976 | break; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 7977 | evaltree(n->nbinary.ch2, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7978 | status = exitstatus; |
| 7979 | if (evalskip) |
| 7980 | goto skipping; |
| 7981 | } |
| 7982 | loopnest--; |
| 7983 | exitstatus = status; |
| 7984 | } |
| 7985 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7986 | static void |
| 7987 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7988 | { |
| 7989 | struct arglist arglist; |
| 7990 | union node *argp; |
| 7991 | struct strlist *sp; |
| 7992 | struct stackmark smark; |
| 7993 | |
| 7994 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 7995 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7996 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 7997 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7998 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7999 | /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8000 | if (evalskip) |
| 8001 | goto out; |
| 8002 | } |
| 8003 | *arglist.lastp = NULL; |
| 8004 | |
| 8005 | exitstatus = 0; |
| 8006 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8007 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8008 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8009 | setvar(n->nfor.var, sp->text, 0); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8010 | evaltree(n->nfor.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8011 | if (evalskip) { |
| 8012 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 8013 | evalskip = 0; |
| 8014 | continue; |
| 8015 | } |
| 8016 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8017 | evalskip = 0; |
| 8018 | break; |
| 8019 | } |
| 8020 | } |
| 8021 | loopnest--; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8022 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8023 | popstackmark(&smark); |
| 8024 | } |
| 8025 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8026 | static void |
| 8027 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8028 | { |
| 8029 | union node *cp; |
| 8030 | union node *patp; |
| 8031 | struct arglist arglist; |
| 8032 | struct stackmark smark; |
| 8033 | |
| 8034 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8035 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8036 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8037 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8038 | exitstatus = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8039 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 8040 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8041 | if (casematch(patp, arglist.list->text)) { |
| 8042 | if (evalskip == 0) { |
| 8043 | evaltree(cp->nclist.body, flags); |
| 8044 | } |
| 8045 | goto out; |
| 8046 | } |
| 8047 | } |
| 8048 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8049 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8050 | popstackmark(&smark); |
| 8051 | } |
| 8052 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8053 | /* |
| 8054 | * Kick off a subshell to evaluate a tree. |
| 8055 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8056 | static void |
| 8057 | evalsubshell(union node *n, int flags) |
| 8058 | { |
| 8059 | struct job *jp; |
| 8060 | int backgnd = (n->type == NBACKGND); |
| 8061 | int status; |
| 8062 | |
| 8063 | expredir(n->nredir.redirect); |
| 8064 | if (!backgnd && flags & EV_EXIT && !trap[0]) |
| 8065 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8066 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8067 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8068 | if (forkshell(jp, n, backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8069 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8070 | flags |= EV_EXIT; |
| 8071 | if (backgnd) |
| 8072 | flags &=~ EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 8073 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8074 | redirect(n->nredir.redirect, 0); |
| 8075 | evaltreenr(n->nredir.n, flags); |
| 8076 | /* never returns */ |
| 8077 | } |
| 8078 | status = 0; |
| 8079 | if (! backgnd) |
| 8080 | status = waitforjob(jp); |
| 8081 | exitstatus = status; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8082 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8083 | } |
| 8084 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8085 | /* |
| 8086 | * Compute the names of the files in a redirection list. |
| 8087 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 8088 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8089 | static void |
| 8090 | expredir(union node *n) |
| 8091 | { |
| 8092 | union node *redir; |
| 8093 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8094 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8095 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8096 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8097 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8098 | fn.lastp = &fn.list; |
| 8099 | switch (redir->type) { |
| 8100 | case NFROMTO: |
| 8101 | case NFROM: |
| 8102 | case NTO: |
| 8103 | case NCLOBBER: |
| 8104 | case NAPPEND: |
| 8105 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
| 8106 | redir->nfile.expfname = fn.list->text; |
| 8107 | break; |
| 8108 | case NFROMFD: |
| 8109 | case NTOFD: |
| 8110 | if (redir->ndup.vname) { |
| 8111 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8112 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8113 | ash_msg_and_raise_error("redir error"); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8114 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8115 | } |
| 8116 | break; |
| 8117 | } |
| 8118 | } |
| 8119 | } |
| 8120 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8121 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8122 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 8123 | * of the process creating the pipeline. (This differs from some versions |
| 8124 | * of the shell, which make the last process in a pipeline the parent |
| 8125 | * of all the rest.) |
| 8126 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8127 | static void |
| 8128 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8129 | { |
| 8130 | struct job *jp; |
| 8131 | struct nodelist *lp; |
| 8132 | int pipelen; |
| 8133 | int prevfd; |
| 8134 | int pip[2]; |
| 8135 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8136 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8137 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8138 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8139 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8140 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8141 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8142 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8143 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8144 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8145 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8146 | pip[1] = -1; |
| 8147 | if (lp->next) { |
| 8148 | if (pipe(pip) < 0) { |
| 8149 | close(prevfd); |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 8150 | ash_msg_and_raise_error("pipe call failed"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8151 | } |
| 8152 | } |
| 8153 | if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8154 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8155 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8156 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8157 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8158 | if (prevfd > 0) { |
| 8159 | dup2(prevfd, 0); |
| 8160 | close(prevfd); |
| 8161 | } |
| 8162 | if (pip[1] > 1) { |
| 8163 | dup2(pip[1], 1); |
| 8164 | close(pip[1]); |
| 8165 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8166 | evaltreenr(lp->n, flags); |
| 8167 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8168 | } |
| 8169 | if (prevfd >= 0) |
| 8170 | close(prevfd); |
| 8171 | prevfd = pip[0]; |
| 8172 | close(pip[1]); |
| 8173 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8174 | if (n->npipe.backgnd == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8175 | exitstatus = waitforjob(jp); |
| 8176 | TRACE(("evalpipe: job done exit status %d\n", exitstatus)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8177 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8178 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8179 | } |
| 8180 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8181 | /* |
| 8182 | * Controls whether the shell is interactive or not. |
| 8183 | */ |
| 8184 | static void |
| 8185 | setinteractive(int on) |
| 8186 | { |
| 8187 | static int is_interactive; |
| 8188 | |
| 8189 | if (++on == is_interactive) |
| 8190 | return; |
| 8191 | is_interactive = on; |
| 8192 | setsignal(SIGINT); |
| 8193 | setsignal(SIGQUIT); |
| 8194 | setsignal(SIGTERM); |
| 8195 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8196 | if (is_interactive > 1) { |
| 8197 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8198 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8199 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8200 | if (!did_banner) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8201 | out1fmt( |
| 8202 | "\n\n" |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8203 | "%s built-in shell (ash)\n" |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8204 | "Enter 'help' for a list of built-in commands." |
| 8205 | "\n\n", |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8206 | bb_banner); |
| 8207 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8208 | } |
| 8209 | } |
| 8210 | #endif |
| 8211 | } |
| 8212 | |
| 8213 | #if ENABLE_FEATURE_EDITING_VI |
| 8214 | #define setvimode(on) do { \ |
| 8215 | if (on) line_input_state->flags |= VI_MODE; \ |
| 8216 | else line_input_state->flags &= ~VI_MODE; \ |
| 8217 | } while (0) |
| 8218 | #else |
| 8219 | #define setvimode(on) viflag = 0 /* forcibly keep the option off */ |
| 8220 | #endif |
| 8221 | |
| 8222 | static void |
| 8223 | optschanged(void) |
| 8224 | { |
| 8225 | #if DEBUG |
| 8226 | opentrace(); |
| 8227 | #endif |
| 8228 | setinteractive(iflag); |
| 8229 | setjobctl(mflag); |
| 8230 | setvimode(viflag); |
| 8231 | } |
| 8232 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8233 | static struct localvar *localvars; |
| 8234 | |
| 8235 | /* |
| 8236 | * Called after a function returns. |
| 8237 | * Interrupts must be off. |
| 8238 | */ |
| 8239 | static void |
| 8240 | poplocalvars(void) |
| 8241 | { |
| 8242 | struct localvar *lvp; |
| 8243 | struct var *vp; |
| 8244 | |
| 8245 | while ((lvp = localvars) != NULL) { |
| 8246 | localvars = lvp->next; |
| 8247 | vp = lvp->vp; |
| 8248 | TRACE(("poplocalvar %s", vp ? vp->text : "-")); |
| 8249 | if (vp == NULL) { /* $- saved */ |
| 8250 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 8251 | free((char*)lvp->text); |
| 8252 | optschanged(); |
| 8253 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
| 8254 | unsetvar(vp->text); |
| 8255 | } else { |
| 8256 | if (vp->func) |
| 8257 | (*vp->func)(strchrnul(lvp->text, '=') + 1); |
| 8258 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 8259 | free((char*)vp->text); |
| 8260 | vp->flags = lvp->flags; |
| 8261 | vp->text = lvp->text; |
| 8262 | } |
| 8263 | free(lvp); |
| 8264 | } |
| 8265 | } |
| 8266 | |
| 8267 | static int |
| 8268 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 8269 | { |
| 8270 | volatile struct shparam saveparam; |
| 8271 | struct localvar *volatile savelocalvars; |
| 8272 | struct jmploc *volatile savehandler; |
| 8273 | struct jmploc jmploc; |
| 8274 | int e; |
| 8275 | |
| 8276 | saveparam = shellparam; |
| 8277 | savelocalvars = localvars; |
| 8278 | e = setjmp(jmploc.loc); |
| 8279 | if (e) { |
| 8280 | goto funcdone; |
| 8281 | } |
| 8282 | INT_OFF; |
| 8283 | savehandler = exception_handler; |
| 8284 | exception_handler = &jmploc; |
| 8285 | localvars = NULL; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8286 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8287 | func->count++; |
| 8288 | funcnest++; |
| 8289 | INT_ON; |
| 8290 | shellparam.nparam = argc - 1; |
| 8291 | shellparam.p = argv + 1; |
| 8292 | #if ENABLE_ASH_GETOPTS |
| 8293 | shellparam.optind = 1; |
| 8294 | shellparam.optoff = -1; |
| 8295 | #endif |
| 8296 | evaltree(&func->n, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8297 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8298 | INT_OFF; |
| 8299 | funcnest--; |
| 8300 | freefunc(func); |
| 8301 | poplocalvars(); |
| 8302 | localvars = savelocalvars; |
| 8303 | freeparam(&shellparam); |
| 8304 | shellparam = saveparam; |
| 8305 | exception_handler = savehandler; |
| 8306 | INT_ON; |
| 8307 | evalskip &= ~SKIPFUNC; |
| 8308 | return e; |
| 8309 | } |
| 8310 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8311 | #if ENABLE_ASH_CMDCMD |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8312 | static char ** |
| 8313 | parse_command_args(char **argv, const char **path) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8314 | { |
| 8315 | char *cp, c; |
| 8316 | |
| 8317 | for (;;) { |
| 8318 | cp = *++argv; |
| 8319 | if (!cp) |
| 8320 | return 0; |
| 8321 | if (*cp++ != '-') |
| 8322 | break; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8323 | c = *cp++; |
| 8324 | if (!c) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8325 | break; |
| 8326 | if (c == '-' && !*cp) { |
| 8327 | argv++; |
| 8328 | break; |
| 8329 | } |
| 8330 | do { |
| 8331 | switch (c) { |
| 8332 | case 'p': |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 8333 | *path = bb_default_path; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8334 | break; |
| 8335 | default: |
| 8336 | /* run 'typecmd' for other options */ |
| 8337 | return 0; |
| 8338 | } |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 8339 | c = *cp++; |
| 8340 | } while (c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8341 | } |
| 8342 | return argv; |
| 8343 | } |
| 8344 | #endif |
| 8345 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8346 | /* |
| 8347 | * Make a variable a local variable. When a variable is made local, it's |
| 8348 | * value and flags are saved in a localvar structure. The saved values |
| 8349 | * will be restored when the shell function returns. We handle the name |
| 8350 | * "-" as a special case. |
| 8351 | */ |
| 8352 | static void |
| 8353 | mklocal(char *name) |
| 8354 | { |
| 8355 | struct localvar *lvp; |
| 8356 | struct var **vpp; |
| 8357 | struct var *vp; |
| 8358 | |
| 8359 | INT_OFF; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 8360 | lvp = ckzalloc(sizeof(struct localvar)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8361 | if (LONE_DASH(name)) { |
| 8362 | char *p; |
| 8363 | p = ckmalloc(sizeof(optlist)); |
| 8364 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 8365 | vp = NULL; |
| 8366 | } else { |
| 8367 | char *eq; |
| 8368 | |
| 8369 | vpp = hashvar(name); |
| 8370 | vp = *findvar(vpp, name); |
| 8371 | eq = strchr(name, '='); |
| 8372 | if (vp == NULL) { |
| 8373 | if (eq) |
| 8374 | setvareq(name, VSTRFIXED); |
| 8375 | else |
| 8376 | setvar(name, NULL, VSTRFIXED); |
| 8377 | vp = *vpp; /* the new variable */ |
| 8378 | lvp->flags = VUNSET; |
| 8379 | } else { |
| 8380 | lvp->text = vp->text; |
| 8381 | lvp->flags = vp->flags; |
| 8382 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 8383 | if (eq) |
| 8384 | setvareq(name, 0); |
| 8385 | } |
| 8386 | } |
| 8387 | lvp->vp = vp; |
| 8388 | lvp->next = localvars; |
| 8389 | localvars = lvp; |
| 8390 | INT_ON; |
| 8391 | } |
| 8392 | |
| 8393 | /* |
| 8394 | * The "local" command. |
| 8395 | */ |
| 8396 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8397 | localcmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8398 | { |
| 8399 | char *name; |
| 8400 | |
| 8401 | argv = argptr; |
| 8402 | while ((name = *argv++) != NULL) { |
| 8403 | mklocal(name); |
| 8404 | } |
| 8405 | return 0; |
| 8406 | } |
| 8407 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8408 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8409 | falsecmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8410 | { |
| 8411 | return 1; |
| 8412 | } |
| 8413 | |
| 8414 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8415 | truecmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8416 | { |
| 8417 | return 0; |
| 8418 | } |
| 8419 | |
| 8420 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8421 | execcmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8422 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8423 | if (argv[1]) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8424 | iflag = 0; /* exit on error */ |
| 8425 | mflag = 0; |
| 8426 | optschanged(); |
| 8427 | shellexec(argv + 1, pathval(), 0); |
| 8428 | } |
| 8429 | return 0; |
| 8430 | } |
| 8431 | |
| 8432 | /* |
| 8433 | * The return command. |
| 8434 | */ |
| 8435 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8436 | returncmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8437 | { |
| 8438 | /* |
| 8439 | * If called outside a function, do what ksh does; |
| 8440 | * skip the rest of the file. |
| 8441 | */ |
| 8442 | evalskip = funcnest ? SKIPFUNC : SKIPFILE; |
| 8443 | return argv[1] ? number(argv[1]) : exitstatus; |
| 8444 | } |
| 8445 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8446 | /* Forward declarations for builtintab[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8447 | static int breakcmd(int, char **); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8448 | static int dotcmd(int, char **); |
| 8449 | static int evalcmd(int, char **); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8450 | static int exitcmd(int, char **); |
| 8451 | static int exportcmd(int, char **); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8452 | #if ENABLE_ASH_GETOPTS |
| 8453 | static int getoptscmd(int, char **); |
| 8454 | #endif |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8455 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 8456 | static int helpcmd(int, char **); |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8457 | #endif |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8458 | #if ENABLE_ASH_MATH_SUPPORT |
| 8459 | static int letcmd(int, char **); |
| 8460 | #endif |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8461 | static int readcmd(int, char **); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8462 | static int setcmd(int, char **); |
| 8463 | static int shiftcmd(int, char **); |
| 8464 | static int timescmd(int, char **); |
| 8465 | static int trapcmd(int, char **); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8466 | static int umaskcmd(int, char **); |
| 8467 | static int unsetcmd(int, char **); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8468 | static int ulimitcmd(int, char **); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8469 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8470 | #define BUILTIN_NOSPEC "0" |
| 8471 | #define BUILTIN_SPECIAL "1" |
| 8472 | #define BUILTIN_REGULAR "2" |
| 8473 | #define BUILTIN_SPEC_REG "3" |
| 8474 | #define BUILTIN_ASSIGN "4" |
| 8475 | #define BUILTIN_SPEC_ASSG "5" |
| 8476 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8477 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 8478 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 8479 | /* We do not handle [[ expr ]] bashism bash-compatibly, |
| 8480 | * we make it a synonym of [ expr ]. |
| 8481 | * Basically, word splitting and pathname expansion should NOT be performed |
| 8482 | * Examples: |
| 8483 | * no word splitting: a="a b"; [[ $a = "a b" ]]; echo $? should print "0" |
| 8484 | * no pathname expansion: [[ /bin/m* = "/bin/m*" ]]; echo $? should print "0" |
| 8485 | * Additional operators: |
| 8486 | * || and && should work as -o and -a |
| 8487 | * =~ regexp match |
| 8488 | * Apart from the above, [[ expr ]] should work as [ expr ] |
| 8489 | */ |
| 8490 | |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8491 | #define echocmd echo_main |
| 8492 | #define printfcmd printf_main |
| 8493 | #define testcmd test_main |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 8494 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 8495 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8496 | static const struct builtincmd builtintab[] = { |
| 8497 | { BUILTIN_SPEC_REG ".", dotcmd }, |
| 8498 | { BUILTIN_SPEC_REG ":", truecmd }, |
| 8499 | #if ENABLE_ASH_BUILTIN_TEST |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8500 | { BUILTIN_REGULAR "[", testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8501 | #if ENABLE_ASH_BASH_COMPAT |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8502 | { BUILTIN_REGULAR "[[", testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8503 | #endif |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8504 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8505 | #if ENABLE_ASH_ALIAS |
| 8506 | { BUILTIN_REG_ASSG "alias", aliascmd }, |
| 8507 | #endif |
| 8508 | #if JOBS |
| 8509 | { BUILTIN_REGULAR "bg", fg_bgcmd }, |
| 8510 | #endif |
| 8511 | { BUILTIN_SPEC_REG "break", breakcmd }, |
| 8512 | { BUILTIN_REGULAR "cd", cdcmd }, |
| 8513 | { BUILTIN_NOSPEC "chdir", cdcmd }, |
| 8514 | #if ENABLE_ASH_CMDCMD |
| 8515 | { BUILTIN_REGULAR "command", commandcmd }, |
| 8516 | #endif |
| 8517 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
| 8518 | #if ENABLE_ASH_BUILTIN_ECHO |
| 8519 | { BUILTIN_REGULAR "echo", echocmd }, |
| 8520 | #endif |
| 8521 | { BUILTIN_SPEC_REG "eval", evalcmd }, |
| 8522 | { BUILTIN_SPEC_REG "exec", execcmd }, |
| 8523 | { BUILTIN_SPEC_REG "exit", exitcmd }, |
| 8524 | { BUILTIN_SPEC_REG_ASSG "export", exportcmd }, |
| 8525 | { BUILTIN_REGULAR "false", falsecmd }, |
| 8526 | #if JOBS |
| 8527 | { BUILTIN_REGULAR "fg", fg_bgcmd }, |
| 8528 | #endif |
| 8529 | #if ENABLE_ASH_GETOPTS |
| 8530 | { BUILTIN_REGULAR "getopts", getoptscmd }, |
| 8531 | #endif |
| 8532 | { BUILTIN_NOSPEC "hash", hashcmd }, |
| 8533 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8534 | { BUILTIN_NOSPEC "help", helpcmd }, |
| 8535 | #endif |
| 8536 | #if JOBS |
| 8537 | { BUILTIN_REGULAR "jobs", jobscmd }, |
| 8538 | { BUILTIN_REGULAR "kill", killcmd }, |
| 8539 | #endif |
| 8540 | #if ENABLE_ASH_MATH_SUPPORT |
| 8541 | { BUILTIN_NOSPEC "let", letcmd }, |
| 8542 | #endif |
| 8543 | { BUILTIN_ASSIGN "local", localcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8544 | #if ENABLE_ASH_BUILTIN_PRINTF |
| 8545 | { BUILTIN_REGULAR "printf", printfcmd }, |
| 8546 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8547 | { BUILTIN_NOSPEC "pwd", pwdcmd }, |
| 8548 | { BUILTIN_REGULAR "read", readcmd }, |
| 8549 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 8550 | { BUILTIN_SPEC_REG "return", returncmd }, |
| 8551 | { BUILTIN_SPEC_REG "set", setcmd }, |
| 8552 | { BUILTIN_SPEC_REG "shift", shiftcmd }, |
| 8553 | { BUILTIN_SPEC_REG "source", dotcmd }, |
| 8554 | #if ENABLE_ASH_BUILTIN_TEST |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8555 | { BUILTIN_REGULAR "test", testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8556 | #endif |
| 8557 | { BUILTIN_SPEC_REG "times", timescmd }, |
| 8558 | { BUILTIN_SPEC_REG "trap", trapcmd }, |
| 8559 | { BUILTIN_REGULAR "true", truecmd }, |
| 8560 | { BUILTIN_NOSPEC "type", typecmd }, |
| 8561 | { BUILTIN_NOSPEC "ulimit", ulimitcmd }, |
| 8562 | { BUILTIN_REGULAR "umask", umaskcmd }, |
| 8563 | #if ENABLE_ASH_ALIAS |
| 8564 | { BUILTIN_REGULAR "unalias", unaliascmd }, |
| 8565 | #endif |
| 8566 | { BUILTIN_SPEC_REG "unset", unsetcmd }, |
| 8567 | { BUILTIN_REGULAR "wait", waitcmd }, |
| 8568 | }; |
| 8569 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8570 | /* Should match the above table! */ |
| 8571 | #define COMMANDCMD (builtintab + \ |
| 8572 | 2 + \ |
| 8573 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 8574 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 8575 | 1 * ENABLE_ASH_ALIAS + \ |
| 8576 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 8577 | 3) |
| 8578 | #define EXECCMD (builtintab + \ |
| 8579 | 2 + \ |
| 8580 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 8581 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 8582 | 1 * ENABLE_ASH_ALIAS + \ |
| 8583 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 8584 | 3 + \ |
| 8585 | 1 * ENABLE_ASH_CMDCMD + \ |
| 8586 | 1 + \ |
| 8587 | ENABLE_ASH_BUILTIN_ECHO + \ |
| 8588 | 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8589 | |
| 8590 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8591 | * Search the table of builtin commands. |
| 8592 | */ |
| 8593 | static struct builtincmd * |
| 8594 | find_builtin(const char *name) |
| 8595 | { |
| 8596 | struct builtincmd *bp; |
| 8597 | |
| 8598 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 8599 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8600 | pstrcmp |
| 8601 | ); |
| 8602 | return bp; |
| 8603 | } |
| 8604 | |
| 8605 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8606 | * Execute a simple command. |
| 8607 | */ |
| 8608 | static int back_exitstatus; /* exit status of backquoted command */ |
| 8609 | static int |
| 8610 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8611 | { |
| 8612 | const char *q = endofname(p); |
| 8613 | if (p == q) |
| 8614 | return 0; |
| 8615 | return *q == '='; |
| 8616 | } |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8617 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8618 | bltincmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8619 | { |
| 8620 | /* Preserve exitstatus of a previous possible redirection |
| 8621 | * as POSIX mandates */ |
| 8622 | return back_exitstatus; |
| 8623 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8624 | static void |
| 8625 | evalcommand(union node *cmd, int flags) |
| 8626 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 8627 | static const struct builtincmd null_bltin = { |
| 8628 | "\0\0", bltincmd /* why three NULs? */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8629 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8630 | struct stackmark smark; |
| 8631 | union node *argp; |
| 8632 | struct arglist arglist; |
| 8633 | struct arglist varlist; |
| 8634 | char **argv; |
| 8635 | int argc; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8636 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8637 | struct cmdentry cmdentry; |
| 8638 | struct job *jp; |
| 8639 | char *lastarg; |
| 8640 | const char *path; |
| 8641 | int spclbltin; |
| 8642 | int cmd_is_exec; |
| 8643 | int status; |
| 8644 | char **nargv; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8645 | struct builtincmd *bcmd; |
| 8646 | int pseudovarflag = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8647 | |
| 8648 | /* First expand the arguments. */ |
| 8649 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 8650 | setstackmark(&smark); |
| 8651 | back_exitstatus = 0; |
| 8652 | |
| 8653 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 8654 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8655 | varlist.lastp = &varlist.list; |
| 8656 | *varlist.lastp = NULL; |
| 8657 | arglist.lastp = &arglist.list; |
| 8658 | *arglist.lastp = NULL; |
| 8659 | |
| 8660 | argc = 0; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8661 | if (cmd->ncmd.args) { |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8662 | bcmd = find_builtin(cmd->ncmd.args->narg.text); |
| 8663 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
| 8664 | } |
| 8665 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8666 | for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) { |
| 8667 | struct strlist **spp; |
| 8668 | |
| 8669 | spp = arglist.lastp; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 8670 | if (pseudovarflag && isassignment(argp->narg.text)) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8671 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 8672 | else |
| 8673 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 8674 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8675 | for (sp = *spp; sp; sp = sp->next) |
| 8676 | argc++; |
| 8677 | } |
| 8678 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8679 | argv = nargv = stalloc(sizeof(char *) * (argc + 1)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8680 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8681 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 8682 | *nargv++ = sp->text; |
| 8683 | } |
| 8684 | *nargv = NULL; |
| 8685 | |
| 8686 | lastarg = NULL; |
| 8687 | if (iflag && funcnest == 0 && argc > 0) |
| 8688 | lastarg = nargv[-1]; |
| 8689 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8690 | preverrout_fd = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8691 | expredir(cmd->ncmd.redirect); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8692 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8693 | |
| 8694 | path = vpath.text; |
| 8695 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 8696 | struct strlist **spp; |
| 8697 | char *p; |
| 8698 | |
| 8699 | spp = varlist.lastp; |
| 8700 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 8701 | |
| 8702 | /* |
| 8703 | * Modify the command lookup path, if a PATH= assignment |
| 8704 | * is present |
| 8705 | */ |
| 8706 | p = (*spp)->text; |
| 8707 | if (varequal(p, path)) |
| 8708 | path = p; |
| 8709 | } |
| 8710 | |
| 8711 | /* Print the command if xflag is set. */ |
| 8712 | if (xflag) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8713 | int n; |
| 8714 | const char *p = " %s"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8715 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8716 | p++; |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 8717 | fdprintf(preverrout_fd, p, expandstr(ps4val())); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8718 | |
| 8719 | sp = varlist.list; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8720 | for (n = 0; n < 2; n++) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8721 | while (sp) { |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 8722 | fdprintf(preverrout_fd, p, sp->text); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8723 | sp = sp->next; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8724 | if (*p == '%') { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8725 | p--; |
| 8726 | } |
| 8727 | } |
| 8728 | sp = arglist.list; |
| 8729 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 8730 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8731 | } |
| 8732 | |
| 8733 | cmd_is_exec = 0; |
| 8734 | spclbltin = -1; |
| 8735 | |
| 8736 | /* Now locate the command. */ |
| 8737 | if (argc) { |
| 8738 | const char *oldpath; |
| 8739 | int cmd_flag = DO_ERR; |
| 8740 | |
| 8741 | path += 5; |
| 8742 | oldpath = path; |
| 8743 | for (;;) { |
| 8744 | find_command(argv[0], &cmdentry, cmd_flag, path); |
| 8745 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
| 8746 | status = 127; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8747 | flush_stderr(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8748 | goto bail; |
| 8749 | } |
| 8750 | |
| 8751 | /* implement bltin and command here */ |
| 8752 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 8753 | break; |
| 8754 | if (spclbltin < 0) |
| 8755 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
| 8756 | if (cmdentry.u.cmd == EXECCMD) |
| 8757 | cmd_is_exec++; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8758 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8759 | if (cmdentry.u.cmd == COMMANDCMD) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8760 | path = oldpath; |
| 8761 | nargv = parse_command_args(argv, &path); |
| 8762 | if (!nargv) |
| 8763 | break; |
| 8764 | argc -= nargv - argv; |
| 8765 | argv = nargv; |
| 8766 | cmd_flag |= DO_NOFUNC; |
| 8767 | } else |
| 8768 | #endif |
| 8769 | break; |
| 8770 | } |
| 8771 | } |
| 8772 | |
| 8773 | if (status) { |
| 8774 | /* We have a redirection error. */ |
| 8775 | if (spclbltin > 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8776 | raise_exception(EXERROR); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8777 | bail: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8778 | exitstatus = status; |
| 8779 | goto out; |
| 8780 | } |
| 8781 | |
| 8782 | /* Execute the command. */ |
| 8783 | switch (cmdentry.cmdtype) { |
| 8784 | default: |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 8785 | #if ENABLE_FEATURE_SH_NOFORK |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8786 | { |
| 8787 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 8788 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 8789 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 8790 | listsetvar(varlist.list, VEXPORT|VSTACK); |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8791 | /* run <applet>_main() */ |
| 8792 | exitstatus = run_nofork_applet(applet_no, argv); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 8793 | break; |
| 8794 | } |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8795 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 8796 | #endif |
| 8797 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8798 | /* Fork off a child process if necessary. */ |
| 8799 | if (!(flags & EV_EXIT) || trap[0]) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8800 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8801 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8802 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
| 8803 | exitstatus = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8804 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8805 | break; |
| 8806 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8807 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8808 | } |
| 8809 | listsetvar(varlist.list, VEXPORT|VSTACK); |
| 8810 | shellexec(argv, path, cmdentry.u.index); |
| 8811 | /* NOTREACHED */ |
| 8812 | |
| 8813 | case CMDBUILTIN: |
| 8814 | cmdenviron = varlist.list; |
| 8815 | if (cmdenviron) { |
| 8816 | struct strlist *list = cmdenviron; |
| 8817 | int i = VNOSET; |
| 8818 | if (spclbltin > 0 || argc == 0) { |
| 8819 | i = 0; |
| 8820 | if (cmd_is_exec && argc > 1) |
| 8821 | i = VEXPORT; |
| 8822 | } |
| 8823 | listsetvar(list, i); |
| 8824 | } |
| 8825 | if (evalbltin(cmdentry.u.cmd, argc, argv)) { |
| 8826 | int exit_status; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8827 | int i = exception; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8828 | if (i == EXEXIT) |
| 8829 | goto raise; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8830 | exit_status = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8831 | if (i == EXINT) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8832 | exit_status = 128 + SIGINT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8833 | if (i == EXSIG) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8834 | exit_status = 128 + pendingsig; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8835 | exitstatus = exit_status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8836 | if (i == EXINT || spclbltin > 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8837 | raise: |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8838 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8839 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8840 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8841 | } |
| 8842 | break; |
| 8843 | |
| 8844 | case CMDFUNCTION: |
| 8845 | listsetvar(varlist.list, 0); |
| 8846 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 8847 | goto raise; |
| 8848 | break; |
| 8849 | } |
| 8850 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8851 | out: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8852 | popredir(cmd_is_exec); |
| 8853 | if (lastarg) |
| 8854 | /* dsl: I think this is intended to be used to support |
| 8855 | * '_' in 'vi' command mode during line editing... |
| 8856 | * However I implemented that within libedit itself. |
| 8857 | */ |
| 8858 | setvar("_", lastarg, 0); |
| 8859 | popstackmark(&smark); |
| 8860 | } |
| 8861 | |
| 8862 | static int |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8863 | evalbltin(const struct builtincmd *cmd, int argc, char **argv) |
| 8864 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8865 | char *volatile savecmdname; |
| 8866 | struct jmploc *volatile savehandler; |
| 8867 | struct jmploc jmploc; |
| 8868 | int i; |
| 8869 | |
| 8870 | savecmdname = commandname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8871 | i = setjmp(jmploc.loc); |
| 8872 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8873 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8874 | savehandler = exception_handler; |
| 8875 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8876 | commandname = argv[0]; |
| 8877 | argptr = argv + 1; |
| 8878 | optptr = NULL; /* initialize nextopt */ |
| 8879 | exitstatus = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8880 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8881 | cmddone: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8882 | exitstatus |= ferror(stdout); |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 8883 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8884 | commandname = savecmdname; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8885 | // exsig = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8886 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8887 | |
| 8888 | return i; |
| 8889 | } |
| 8890 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8891 | static int |
| 8892 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 8893 | { |
| 8894 | return !*endofname(p); |
| 8895 | } |
| 8896 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8897 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8898 | /* |
| 8899 | * Search for a command. This is called before we fork so that the |
| 8900 | * 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] | 8901 | * the child. The check for "goodname" is an overly conservative |
| 8902 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8903 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8904 | static void |
| 8905 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8906 | { |
| 8907 | struct cmdentry entry; |
| 8908 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8909 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 8910 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8911 | } |
| 8912 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8913 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8914 | /* ============ Builtin commands |
| 8915 | * |
| 8916 | * Builtin commands whose functions are closely tied to evaluation |
| 8917 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8918 | */ |
| 8919 | |
| 8920 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8921 | * Handle break and continue commands. Break, continue, and return are |
| 8922 | * all handled by setting the evalskip flag. The evaluation routines |
| 8923 | * above all check this flag, and if it is set they start skipping |
| 8924 | * commands rather than executing them. The variable skipcount is |
| 8925 | * the number of loops to break/continue, or the number of function |
| 8926 | * levels to return. (The latter is always 1.) It should probably |
| 8927 | * be an error to break out of more loops than exist, but it isn't |
| 8928 | * in the standard shell so we don't make it one here. |
| 8929 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8930 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8931 | breakcmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8932 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8933 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8934 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 8935 | if (n <= 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8936 | ash_msg_and_raise_error(illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8937 | if (n > loopnest) |
| 8938 | n = loopnest; |
| 8939 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8940 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8941 | skipcount = n; |
| 8942 | } |
| 8943 | return 0; |
| 8944 | } |
| 8945 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8946 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8947 | /* ============ input.c |
| 8948 | * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 8949 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8950 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 8951 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8952 | #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8953 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8954 | enum { |
| 8955 | INPUT_PUSH_FILE = 1, |
| 8956 | INPUT_NOFILE_OK = 2, |
| 8957 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8958 | |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 8959 | static int plinno = 1; /* input line number */ |
| 8960 | /* number of characters left in input buffer */ |
| 8961 | static int parsenleft; /* copy of parsefile->nleft */ |
| 8962 | static int parselleft; /* copy of parsefile->lleft */ |
| 8963 | /* next character in input buffer */ |
| 8964 | static char *parsenextc; /* copy of parsefile->nextc */ |
| 8965 | |
| 8966 | static int checkkwd; |
| 8967 | /* values of checkkwd variable */ |
| 8968 | #define CHKALIAS 0x1 |
| 8969 | #define CHKKWD 0x2 |
| 8970 | #define CHKNL 0x4 |
| 8971 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8972 | static void |
| 8973 | popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8974 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8975 | struct strpush *sp = parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8976 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8977 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8978 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8979 | if (sp->ap) { |
| 8980 | if (parsenextc[-1] == ' ' || parsenextc[-1] == '\t') { |
| 8981 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 8982 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8983 | if (sp->string != sp->ap->val) { |
| 8984 | free(sp->string); |
| 8985 | } |
| 8986 | sp->ap->flag &= ~ALIASINUSE; |
| 8987 | if (sp->ap->flag & ALIASDEAD) { |
| 8988 | unalias(sp->ap->name); |
| 8989 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 8990 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 8991 | #endif |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8992 | parsenextc = sp->prevstring; |
| 8993 | parsenleft = sp->prevnleft; |
| 8994 | /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/ |
| 8995 | parsefile->strpush = sp->prev; |
| 8996 | if (sp != &(parsefile->basestrpush)) |
| 8997 | free(sp); |
| 8998 | INT_ON; |
| 8999 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9000 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9001 | static int |
| 9002 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9003 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9004 | int nr; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9005 | char *buf = parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9006 | parsenextc = buf; |
| 9007 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9008 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9009 | retry: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9010 | if (!iflag || parsefile->fd) |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9011 | nr = nonblock_safe_read(parsefile->fd, buf, BUFSIZ - 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9012 | else { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9013 | #if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9014 | line_input_state->path_lookup = pathval(); |
Eric Andersen | 4a79c0e | 2004-09-08 10:01:07 +0000 | [diff] [blame] | 9015 | #endif |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9016 | nr = read_line_input(cmdedit_prompt, buf, BUFSIZ, line_input_state); |
| 9017 | if (nr == 0) { |
| 9018 | /* Ctrl+C pressed */ |
| 9019 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9020 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9021 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9022 | raise(SIGINT); |
| 9023 | return 1; |
| 9024 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9025 | goto retry; |
| 9026 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9027 | if (nr < 0 && errno == 0) { |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9028 | /* Ctrl+D pressed */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9029 | nr = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9030 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9031 | } |
| 9032 | #else |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9033 | nr = nonblock_safe_read(parsefile->fd, buf, BUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9034 | #endif |
| 9035 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9036 | #if 0 |
| 9037 | /* nonblock_safe_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9038 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9039 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 9040 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9041 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 9042 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9043 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 9044 | out2str("sh: turning off NDELAY mode\n"); |
| 9045 | goto retry; |
| 9046 | } |
| 9047 | } |
| 9048 | } |
| 9049 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9050 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9051 | return nr; |
| 9052 | } |
| 9053 | |
| 9054 | /* |
| 9055 | * Refill the input buffer and return the next input character: |
| 9056 | * |
| 9057 | * 1) If a string was pushed back on the input, pop it; |
| 9058 | * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading |
| 9059 | * from a string so we can't refill the buffer, return EOF. |
| 9060 | * 3) If the is more stuff in this buffer, use it else call read to fill it. |
| 9061 | * 4) Process input up to the next newline, deleting nul characters. |
| 9062 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9063 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9064 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9065 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9066 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9067 | int more; |
| 9068 | char savec; |
| 9069 | |
| 9070 | while (parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9071 | #if ENABLE_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9072 | if (parsenleft == -1 && parsefile->strpush->ap && |
| 9073 | parsenextc[-1] != ' ' && parsenextc[-1] != '\t') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9074 | return PEOA; |
| 9075 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9076 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9077 | popstring(); |
| 9078 | if (--parsenleft >= 0) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9079 | return signed_char2int(*parsenextc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9080 | } |
| 9081 | if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) |
| 9082 | return PEOF; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9083 | flush_stdout_stderr(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9084 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9085 | more = parselleft; |
| 9086 | if (more <= 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9087 | again: |
| 9088 | more = preadfd(); |
| 9089 | if (more <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9090 | parselleft = parsenleft = EOF_NLEFT; |
| 9091 | return PEOF; |
| 9092 | } |
| 9093 | } |
| 9094 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9095 | q = parsenextc; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9096 | |
| 9097 | /* delete nul characters */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9098 | for (;;) { |
| 9099 | int c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9100 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9101 | more--; |
| 9102 | c = *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9103 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9104 | if (!c) |
| 9105 | memmove(q, q + 1, more); |
| 9106 | else { |
| 9107 | q++; |
| 9108 | if (c == '\n') { |
| 9109 | parsenleft = q - parsenextc - 1; |
| 9110 | break; |
| 9111 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9112 | } |
| 9113 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9114 | if (more <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9115 | parsenleft = q - parsenextc - 1; |
| 9116 | if (parsenleft < 0) |
| 9117 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9118 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9119 | } |
| 9120 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9121 | parselleft = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9122 | |
| 9123 | savec = *q; |
| 9124 | *q = '\0'; |
| 9125 | |
| 9126 | if (vflag) { |
| 9127 | out2str(parsenextc); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9128 | } |
| 9129 | |
| 9130 | *q = savec; |
| 9131 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9132 | return signed_char2int(*parsenextc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9133 | } |
| 9134 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9135 | #define pgetc_as_macro() (--parsenleft >= 0? signed_char2int(*parsenextc++) : preadbuffer()) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9136 | static int |
| 9137 | pgetc(void) |
| 9138 | { |
| 9139 | return pgetc_as_macro(); |
| 9140 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9141 | |
| 9142 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 9143 | #define pgetc_macro() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9144 | #else |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9145 | #define pgetc_macro() pgetc_as_macro() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9146 | #endif |
| 9147 | |
| 9148 | /* |
| 9149 | * Same as pgetc(), but ignores PEOA. |
| 9150 | */ |
| 9151 | #if ENABLE_ASH_ALIAS |
| 9152 | static int |
| 9153 | pgetc2(void) |
| 9154 | { |
| 9155 | int c; |
| 9156 | |
| 9157 | do { |
| 9158 | c = pgetc_macro(); |
| 9159 | } while (c == PEOA); |
| 9160 | return c; |
| 9161 | } |
| 9162 | #else |
| 9163 | static int |
| 9164 | pgetc2(void) |
| 9165 | { |
| 9166 | return pgetc_macro(); |
| 9167 | } |
| 9168 | #endif |
| 9169 | |
| 9170 | /* |
| 9171 | * Read a line from the script. |
| 9172 | */ |
| 9173 | static char * |
| 9174 | pfgets(char *line, int len) |
| 9175 | { |
| 9176 | char *p = line; |
| 9177 | int nleft = len; |
| 9178 | int c; |
| 9179 | |
| 9180 | while (--nleft > 0) { |
| 9181 | c = pgetc2(); |
| 9182 | if (c == PEOF) { |
| 9183 | if (p == line) |
| 9184 | return NULL; |
| 9185 | break; |
| 9186 | } |
| 9187 | *p++ = c; |
| 9188 | if (c == '\n') |
| 9189 | break; |
| 9190 | } |
| 9191 | *p = '\0'; |
| 9192 | return line; |
| 9193 | } |
| 9194 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9195 | /* |
| 9196 | * Undo the last call to pgetc. Only one character may be pushed back. |
| 9197 | * PEOF may be pushed back. |
| 9198 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9199 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9200 | pungetc(void) |
| 9201 | { |
| 9202 | parsenleft++; |
| 9203 | parsenextc--; |
| 9204 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9205 | |
| 9206 | /* |
| 9207 | * Push a string back onto the input at this current parsefile level. |
| 9208 | * We handle aliases this way. |
| 9209 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9210 | #if !ENABLE_ASH_ALIAS |
| 9211 | #define pushstring(s, ap) pushstring(s) |
| 9212 | #endif |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9213 | static void |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9214 | pushstring(char *s, struct alias *ap) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9215 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9216 | struct strpush *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9217 | size_t len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9218 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9219 | len = strlen(s); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9220 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9221 | /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/ |
| 9222 | if (parsefile->strpush) { |
Denis Vlasenko | 6aa74fc | 2008-02-21 04:35:14 +0000 | [diff] [blame] | 9223 | sp = ckzalloc(sizeof(struct strpush)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9224 | sp->prev = parsefile->strpush; |
| 9225 | parsefile->strpush = sp; |
| 9226 | } else |
| 9227 | sp = parsefile->strpush = &(parsefile->basestrpush); |
| 9228 | sp->prevstring = parsenextc; |
| 9229 | sp->prevnleft = parsenleft; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9230 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9231 | sp->ap = ap; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9232 | if (ap) { |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9233 | ap->flag |= ALIASINUSE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9234 | sp->string = s; |
| 9235 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9236 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9237 | parsenextc = s; |
| 9238 | parsenleft = len; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9239 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9240 | } |
| 9241 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9242 | /* |
| 9243 | * To handle the "." command, a stack of input files is used. Pushfile |
| 9244 | * adds a new entry to the stack and popfile restores the previous level. |
| 9245 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9246 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9247 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9248 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9249 | struct parsefile *pf; |
| 9250 | |
| 9251 | parsefile->nleft = parsenleft; |
| 9252 | parsefile->lleft = parselleft; |
| 9253 | parsefile->nextc = parsenextc; |
| 9254 | parsefile->linno = plinno; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9255 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9256 | pf->prev = parsefile; |
| 9257 | pf->fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9258 | /*pf->strpush = NULL; - ckzalloc did it */ |
| 9259 | /*pf->basestrpush.prev = NULL;*/ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9260 | parsefile = pf; |
| 9261 | } |
| 9262 | |
| 9263 | static void |
| 9264 | popfile(void) |
| 9265 | { |
| 9266 | struct parsefile *pf = parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9267 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9268 | INT_OFF; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9269 | if (pf->fd >= 0) |
| 9270 | close(pf->fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 9271 | free(pf->buf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9272 | while (pf->strpush) |
| 9273 | popstring(); |
| 9274 | parsefile = pf->prev; |
| 9275 | free(pf); |
| 9276 | parsenleft = parsefile->nleft; |
| 9277 | parselleft = parsefile->lleft; |
| 9278 | parsenextc = parsefile->nextc; |
| 9279 | plinno = parsefile->linno; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9280 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9281 | } |
| 9282 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9283 | /* |
| 9284 | * Return to top level. |
| 9285 | */ |
| 9286 | static void |
| 9287 | popallfiles(void) |
| 9288 | { |
| 9289 | while (parsefile != &basepf) |
| 9290 | popfile(); |
| 9291 | } |
| 9292 | |
| 9293 | /* |
| 9294 | * Close the file(s) that the shell is reading commands from. Called |
| 9295 | * after a fork is done. |
| 9296 | */ |
| 9297 | static void |
| 9298 | closescript(void) |
| 9299 | { |
| 9300 | popallfiles(); |
| 9301 | if (parsefile->fd > 0) { |
| 9302 | close(parsefile->fd); |
| 9303 | parsefile->fd = 0; |
| 9304 | } |
| 9305 | } |
| 9306 | |
| 9307 | /* |
| 9308 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 9309 | * interrupts off. |
| 9310 | */ |
| 9311 | static void |
| 9312 | setinputfd(int fd, int push) |
| 9313 | { |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 9314 | close_on_exec_on(fd); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9315 | if (push) { |
| 9316 | pushfile(); |
| 9317 | parsefile->buf = 0; |
| 9318 | } |
| 9319 | parsefile->fd = fd; |
| 9320 | if (parsefile->buf == NULL) |
| 9321 | parsefile->buf = ckmalloc(IBUFSIZ); |
| 9322 | parselleft = parsenleft = 0; |
| 9323 | plinno = 1; |
| 9324 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9325 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9326 | /* |
| 9327 | * Set the input to take input from a file. If push is set, push the |
| 9328 | * old input onto the stack first. |
| 9329 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9330 | static int |
| 9331 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9332 | { |
| 9333 | int fd; |
| 9334 | int fd2; |
| 9335 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9336 | INT_OFF; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9337 | fd = open(fname, O_RDONLY); |
| 9338 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9339 | if (flags & INPUT_NOFILE_OK) |
| 9340 | goto out; |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 9341 | ash_msg_and_raise_error("can't open %s", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9342 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9343 | if (fd < 10) { |
| 9344 | fd2 = copyfd(fd, 10); |
| 9345 | close(fd); |
| 9346 | if (fd2 < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 9347 | ash_msg_and_raise_error("out of file descriptors"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9348 | fd = fd2; |
| 9349 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9350 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9351 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9352 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9353 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9354 | } |
| 9355 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9356 | /* |
| 9357 | * Like setinputfile, but takes input from a string. |
| 9358 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9359 | static void |
| 9360 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9361 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9362 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9363 | pushfile(); |
| 9364 | parsenextc = string; |
| 9365 | parsenleft = strlen(string); |
| 9366 | parsefile->buf = NULL; |
| 9367 | plinno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9368 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9369 | } |
| 9370 | |
| 9371 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9372 | /* ============ mail.c |
| 9373 | * |
| 9374 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9375 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9376 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9377 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9378 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9379 | #define MAXMBOXES 10 |
| 9380 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9381 | /* times of mailboxes */ |
| 9382 | static time_t mailtime[MAXMBOXES]; |
| 9383 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9384 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9385 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9386 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9387 | * Print appropriate message(s) if mail has arrived. |
| 9388 | * If mail_var_path_changed is set, |
| 9389 | * then the value of MAIL has mail_var_path_changed, |
| 9390 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9391 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9392 | static void |
| 9393 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9394 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9395 | const char *mpath; |
| 9396 | char *p; |
| 9397 | char *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9398 | time_t *mtp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9399 | struct stackmark smark; |
| 9400 | struct stat statb; |
| 9401 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9402 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9403 | mpath = mpathset() ? mpathval() : mailval(); |
| 9404 | for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9405 | p = padvance(&mpath, nullstr); |
| 9406 | if (p == NULL) |
| 9407 | break; |
| 9408 | if (*p == '\0') |
| 9409 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9410 | for (q = p; *q; q++) |
| 9411 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 9412 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9413 | if (q[-1] != '/') |
| 9414 | abort(); |
| 9415 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9416 | q[-1] = '\0'; /* delete trailing '/' */ |
| 9417 | if (stat(p, &statb) < 0) { |
| 9418 | *mtp = 0; |
| 9419 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9420 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9421 | if (!mail_var_path_changed && statb.st_mtime != *mtp) { |
| 9422 | fprintf( |
| 9423 | stderr, snlfmt, |
| 9424 | pathopt ? pathopt : "you have mail" |
| 9425 | ); |
| 9426 | } |
| 9427 | *mtp = statb.st_mtime; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9428 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9429 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9430 | popstackmark(&smark); |
| 9431 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9432 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9433 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9434 | changemail(const char *val ATTRIBUTE_UNUSED) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9435 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9436 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9437 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9438 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9439 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9440 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9441 | |
| 9442 | /* ============ ??? */ |
| 9443 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9444 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9445 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9446 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9447 | static void |
| 9448 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9449 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9450 | char **newparam; |
| 9451 | char **ap; |
| 9452 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9453 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9454 | for (nparam = 0; argv[nparam]; nparam++) |
| 9455 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9456 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 9457 | while (*argv) { |
| 9458 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9459 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9460 | *ap = NULL; |
| 9461 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9462 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9463 | shellparam.nparam = nparam; |
| 9464 | shellparam.p = newparam; |
| 9465 | #if ENABLE_ASH_GETOPTS |
| 9466 | shellparam.optind = 1; |
| 9467 | shellparam.optoff = -1; |
| 9468 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9469 | } |
| 9470 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9471 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9472 | * Process shell options. The global variable argptr contains a pointer |
| 9473 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 9474 | * |
| 9475 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 9476 | * For a non-interactive shell, an error condition encountered |
| 9477 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 9478 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 9479 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 9480 | * ... |
| 9481 | * Utility syntax error (option or operand error) Shall exit |
| 9482 | * ... |
| 9483 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 9484 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 9485 | * and shell continues), and people rely on this behavior! |
| 9486 | * Testcase: |
| 9487 | * set -o barfoo 2>/dev/null |
| 9488 | * echo $? |
| 9489 | * |
| 9490 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9491 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9492 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9493 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9494 | { |
| 9495 | int i; |
| 9496 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9497 | if (name) { |
| 9498 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 9499 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9500 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9501 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9502 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9503 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9504 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9505 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9506 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9507 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9508 | if (val) { |
| 9509 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 9510 | } else { |
| 9511 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 9512 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9513 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9514 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9515 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9516 | static void |
| 9517 | setoption(int flag, int val) |
| 9518 | { |
| 9519 | int i; |
| 9520 | |
| 9521 | for (i = 0; i < NOPTS; i++) { |
| 9522 | if (optletters(i) == flag) { |
| 9523 | optlist[i] = val; |
| 9524 | return; |
| 9525 | } |
| 9526 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9527 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9528 | /* NOTREACHED */ |
| 9529 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9530 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9531 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9532 | { |
| 9533 | char *p; |
| 9534 | int val; |
| 9535 | int c; |
| 9536 | |
| 9537 | if (cmdline) |
| 9538 | minusc = NULL; |
| 9539 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9540 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9541 | if (c != '-' && c != '+') |
| 9542 | break; |
| 9543 | argptr++; |
| 9544 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9545 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9546 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 9547 | if (p[0] == '\0' || LONE_DASH(p)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9548 | if (!cmdline) { |
| 9549 | /* "-" means turn off -x and -v */ |
| 9550 | if (p[0] == '\0') |
| 9551 | xflag = vflag = 0; |
| 9552 | /* "--" means reset params */ |
| 9553 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9554 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9555 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9556 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9557 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9558 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9559 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9560 | while ((c = *p++) != '\0') { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9561 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9562 | if (c == 'c' && cmdline) { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9563 | minusc = p; /* command is after shell args */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9564 | } else if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9565 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9566 | /* it already printed err message */ |
| 9567 | return 1; /* error */ |
| 9568 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9569 | if (*argptr) |
| 9570 | argptr++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9571 | } else if (cmdline && (c == 'l')) { /* -l or +l == --login */ |
| 9572 | isloginsh = 1; |
| 9573 | /* bash does not accept +-login, we also won't */ |
| 9574 | } else if (cmdline && val && (c == '-')) { /* long options */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9575 | if (strcmp(p, "login") == 0) |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 9576 | isloginsh = 1; |
| 9577 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9578 | } else { |
| 9579 | setoption(c, val); |
| 9580 | } |
| 9581 | } |
| 9582 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9583 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9584 | } |
| 9585 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9586 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9587 | * The shift builtin command. |
| 9588 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9589 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9590 | shiftcmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9591 | { |
| 9592 | int n; |
| 9593 | char **ap1, **ap2; |
| 9594 | |
| 9595 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9596 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9597 | n = number(argv[1]); |
| 9598 | if (n > shellparam.nparam) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9599 | ash_msg_and_raise_error("can't shift that many"); |
| 9600 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9601 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9602 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9603 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9604 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9605 | } |
| 9606 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9607 | while ((*ap2++ = *ap1++) != NULL) |
| 9608 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9609 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9610 | shellparam.optind = 1; |
| 9611 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9612 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9613 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9614 | return 0; |
| 9615 | } |
| 9616 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9617 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9618 | * POSIX requires that 'set' (but not export or readonly) output the |
| 9619 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 9620 | * Maybe we could keep them in an ordered balanced binary tree |
| 9621 | * instead of hashed lists. |
| 9622 | * For now just roll 'em through qsort for printing... |
| 9623 | */ |
| 9624 | static int |
| 9625 | showvars(const char *sep_prefix, int on, int off) |
| 9626 | { |
| 9627 | const char *sep; |
| 9628 | char **ep, **epend; |
| 9629 | |
| 9630 | ep = listvars(on, off, &epend); |
| 9631 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 9632 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9633 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9634 | |
| 9635 | for (; ep < epend; ep++) { |
| 9636 | const char *p; |
| 9637 | const char *q; |
| 9638 | |
| 9639 | p = strchrnul(*ep, '='); |
| 9640 | q = nullstr; |
| 9641 | if (*p) |
| 9642 | q = single_quote(++p); |
| 9643 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 9644 | } |
| 9645 | return 0; |
| 9646 | } |
| 9647 | |
| 9648 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9649 | * The set command builtin. |
| 9650 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9651 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9652 | setcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9653 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9654 | int retval; |
| 9655 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9656 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9657 | return showvars(nullstr, 0, VUNSET); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9658 | INT_OFF; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9659 | retval = 1; |
| 9660 | if (!options(0)) { /* if no parse error... */ |
| 9661 | retval = 0; |
| 9662 | optschanged(); |
| 9663 | if (*argptr != NULL) { |
| 9664 | setparam(argptr); |
| 9665 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9666 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9667 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9668 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9669 | } |
| 9670 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9671 | #if ENABLE_ASH_RANDOM_SUPPORT |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 9672 | /* Roughly copied from bash.. */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 9673 | static void |
| 9674 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 9675 | { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9676 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 9677 | /* "get", generate */ |
| 9678 | char buf[16]; |
| 9679 | |
| 9680 | rseed = rseed * 1103515245 + 12345; |
| 9681 | sprintf(buf, "%d", (unsigned int)((rseed & 32767))); |
| 9682 | /* set without recursion */ |
| 9683 | setvar(vrandom.text, buf, VNOFUNC); |
| 9684 | vrandom.flags &= ~VNOFUNC; |
| 9685 | } else { |
| 9686 | /* set/reset */ |
| 9687 | rseed = strtoul(value, (char **)NULL, 10); |
| 9688 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 9689 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 9690 | #endif |
| 9691 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9692 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9693 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9694 | getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9695 | { |
| 9696 | char *p, *q; |
| 9697 | char c = '?'; |
| 9698 | int done = 0; |
| 9699 | int err = 0; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 9700 | char s[12]; |
| 9701 | char **optnext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9702 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9703 | if (*param_optind < 1) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 9704 | return 1; |
| 9705 | optnext = optfirst + *param_optind - 1; |
| 9706 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9707 | if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9708 | p = NULL; |
| 9709 | else |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 9710 | p = optnext[-1] + *optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9711 | if (p == NULL || *p == '\0') { |
| 9712 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9713 | p = *optnext; |
| 9714 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9715 | atend: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9716 | p = NULL; |
| 9717 | done = 1; |
| 9718 | goto out; |
| 9719 | } |
| 9720 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 9721 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9722 | goto atend; |
| 9723 | } |
| 9724 | |
| 9725 | c = *p++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9726 | for (q = optstr; *q != c; ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9727 | if (*q == '\0') { |
| 9728 | if (optstr[0] == ':') { |
| 9729 | s[0] = c; |
| 9730 | s[1] = '\0'; |
| 9731 | err |= setvarsafe("OPTARG", s, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9732 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9733 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9734 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9735 | } |
| 9736 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9737 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9738 | } |
| 9739 | if (*++q == ':') |
| 9740 | q++; |
| 9741 | } |
| 9742 | |
| 9743 | if (*++q == ':') { |
| 9744 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 9745 | if (optstr[0] == ':') { |
| 9746 | s[0] = c; |
| 9747 | s[1] = '\0'; |
| 9748 | err |= setvarsafe("OPTARG", s, 0); |
| 9749 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9750 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9751 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9752 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9753 | c = '?'; |
| 9754 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9755 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9756 | } |
| 9757 | |
| 9758 | if (p == *optnext) |
| 9759 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9760 | err |= setvarsafe("OPTARG", p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9761 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9762 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9763 | err |= setvarsafe("OPTARG", nullstr, 0); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9764 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9765 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9766 | *param_optind = optnext - optfirst + 1; |
| 9767 | fmtstr(s, sizeof(s), "%d", *param_optind); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9768 | err |= setvarsafe("OPTIND", s, VNOFUNC); |
| 9769 | s[0] = c; |
| 9770 | s[1] = '\0'; |
| 9771 | err |= setvarsafe(optvar, s, 0); |
| 9772 | if (err) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9773 | *param_optind = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9774 | *optoff = -1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9775 | flush_stdout_stderr(); |
| 9776 | raise_exception(EXERROR); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9777 | } |
| 9778 | return done; |
| 9779 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9780 | |
| 9781 | /* |
| 9782 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 9783 | * to be processed. Shellparam.optptr points to the next character to |
| 9784 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 9785 | * then it's the first time getopts has been called. |
| 9786 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9787 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9788 | getoptscmd(int argc, char **argv) |
| 9789 | { |
| 9790 | char **optbase; |
| 9791 | |
| 9792 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 9793 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9794 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9795 | optbase = shellparam.p; |
| 9796 | if (shellparam.optind > shellparam.nparam + 1) { |
| 9797 | shellparam.optind = 1; |
| 9798 | shellparam.optoff = -1; |
| 9799 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9800 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9801 | optbase = &argv[3]; |
| 9802 | if (shellparam.optind > argc - 2) { |
| 9803 | shellparam.optind = 1; |
| 9804 | shellparam.optoff = -1; |
| 9805 | } |
| 9806 | } |
| 9807 | |
| 9808 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9809 | &shellparam.optoff); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9810 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9811 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9812 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9813 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9814 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9815 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9816 | /* |
| 9817 | * NEOF is returned by parsecmd when it encounters an end of file. It |
| 9818 | * must be distinct from NULL, so we use the address of a variable that |
| 9819 | * happens to be handy. |
| 9820 | */ |
| 9821 | static smallint tokpushback; /* last token pushed back */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9822 | #define NEOF ((union node *)&tokpushback) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9823 | static smallint parsebackquote; /* nonzero if we are inside backquotes */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9824 | static int lasttoken; /* last token read */ |
| 9825 | static char *wordtext; /* text of last word returned by readtoken */ |
| 9826 | static struct nodelist *backquotelist; |
| 9827 | static union node *redirnode; |
| 9828 | static struct heredoc *heredoc; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9829 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9830 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9831 | static void raise_error_syntax(const char *) ATTRIBUTE_NORETURN; |
| 9832 | static void |
| 9833 | raise_error_syntax(const char *msg) |
| 9834 | { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 9835 | ash_msg_and_raise_error("syntax error: %s", msg); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9836 | /* NOTREACHED */ |
| 9837 | } |
| 9838 | |
| 9839 | /* |
| 9840 | * Called when an unexpected token is read during the parse. The argument |
| 9841 | * is the token that is expected, or -1 if more than one type of token can |
| 9842 | * occur at this point. |
| 9843 | */ |
| 9844 | static void raise_error_unexpected_syntax(int) ATTRIBUTE_NORETURN; |
| 9845 | static void |
| 9846 | raise_error_unexpected_syntax(int token) |
| 9847 | { |
| 9848 | char msg[64]; |
| 9849 | int l; |
| 9850 | |
| 9851 | l = sprintf(msg, "%s unexpected", tokname(lasttoken)); |
| 9852 | if (token >= 0) |
| 9853 | sprintf(msg + l, " (expecting %s)", tokname(token)); |
| 9854 | raise_error_syntax(msg); |
| 9855 | /* NOTREACHED */ |
| 9856 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9857 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9858 | #define EOFMARKLEN 79 |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9859 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9860 | struct heredoc { |
| 9861 | struct heredoc *next; /* next here document in list */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 9862 | union node *here; /* redirection node */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9863 | char *eofmark; /* string indicating end of input */ |
| 9864 | int striptabs; /* if set, strip leading tabs */ |
| 9865 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9866 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9867 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 9868 | |
| 9869 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 9870 | static union node *andor(void); |
| 9871 | static union node *pipeline(void); |
| 9872 | static union node *parse_command(void); |
| 9873 | static void parseheredoc(void); |
| 9874 | static char peektoken(void); |
| 9875 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9876 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9877 | static union node * |
| 9878 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9879 | { |
| 9880 | union node *n1, *n2, *n3; |
| 9881 | int tok; |
| 9882 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9883 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 9884 | if (nlflag == 2 && peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9885 | return NULL; |
| 9886 | n1 = NULL; |
| 9887 | for (;;) { |
| 9888 | n2 = andor(); |
| 9889 | tok = readtoken(); |
| 9890 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9891 | if (n2->type == NPIPE) { |
| 9892 | n2->npipe.backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9893 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9894 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9895 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9896 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9897 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9898 | n2 = n3; |
| 9899 | } |
| 9900 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9901 | } |
| 9902 | } |
| 9903 | if (n1 == NULL) { |
| 9904 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9905 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 9906 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9907 | n3->type = NSEMI; |
| 9908 | n3->nbinary.ch1 = n1; |
| 9909 | n3->nbinary.ch2 = n2; |
| 9910 | n1 = n3; |
| 9911 | } |
| 9912 | switch (tok) { |
| 9913 | case TBACKGND: |
| 9914 | case TSEMI: |
| 9915 | tok = readtoken(); |
| 9916 | /* fall through */ |
| 9917 | case TNL: |
| 9918 | if (tok == TNL) { |
| 9919 | parseheredoc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9920 | if (nlflag == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9921 | return n1; |
| 9922 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9923 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9924 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9925 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 9926 | if (peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9927 | return n1; |
| 9928 | break; |
| 9929 | case TEOF: |
| 9930 | if (heredoclist) |
| 9931 | parseheredoc(); |
| 9932 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9933 | pungetc(); /* push back EOF on input */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9934 | return n1; |
| 9935 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9936 | if (nlflag == 1) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9937 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9938 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9939 | return n1; |
| 9940 | } |
| 9941 | } |
| 9942 | } |
| 9943 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9944 | static union node * |
| 9945 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9946 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9947 | union node *n1, *n2, *n3; |
| 9948 | int t; |
| 9949 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9950 | n1 = pipeline(); |
| 9951 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9952 | t = readtoken(); |
| 9953 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9954 | t = NAND; |
| 9955 | } else if (t == TOR) { |
| 9956 | t = NOR; |
| 9957 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9958 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9959 | return n1; |
| 9960 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9961 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9962 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 9963 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9964 | n3->type = t; |
| 9965 | n3->nbinary.ch1 = n1; |
| 9966 | n3->nbinary.ch2 = n2; |
| 9967 | n1 = n3; |
| 9968 | } |
| 9969 | } |
| 9970 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9971 | static union node * |
| 9972 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9973 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9974 | union node *n1, *n2, *pipenode; |
| 9975 | struct nodelist *lp, *prev; |
| 9976 | int negate; |
| 9977 | |
| 9978 | negate = 0; |
| 9979 | TRACE(("pipeline: entered\n")); |
| 9980 | if (readtoken() == TNOT) { |
| 9981 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9982 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9983 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9984 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9985 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9986 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9987 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9988 | pipenode->type = NPIPE; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9989 | /*pipenode->npipe.backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 9990 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9991 | pipenode->npipe.cmdlist = lp; |
| 9992 | lp->n = n1; |
| 9993 | do { |
| 9994 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 9995 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9996 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9997 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9998 | prev->next = lp; |
| 9999 | } while (readtoken() == TPIPE); |
| 10000 | lp->next = NULL; |
| 10001 | n1 = pipenode; |
| 10002 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10003 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10004 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10005 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10006 | n2->type = NNOT; |
| 10007 | n2->nnot.com = n1; |
| 10008 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10009 | } |
| 10010 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10011 | } |
| 10012 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10013 | static union node * |
| 10014 | makename(void) |
| 10015 | { |
| 10016 | union node *n; |
| 10017 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10018 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10019 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10020 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10021 | n->narg.text = wordtext; |
| 10022 | n->narg.backquote = backquotelist; |
| 10023 | return n; |
| 10024 | } |
| 10025 | |
| 10026 | static void |
| 10027 | fixredir(union node *n, const char *text, int err) |
| 10028 | { |
| 10029 | TRACE(("Fix redir %s %d\n", text, err)); |
| 10030 | if (!err) |
| 10031 | n->ndup.vname = NULL; |
| 10032 | |
| 10033 | if (isdigit(text[0]) && text[1] == '\0') |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10034 | n->ndup.dupfd = text[0] - '0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10035 | else if (LONE_DASH(text)) |
| 10036 | n->ndup.dupfd = -1; |
| 10037 | else { |
| 10038 | if (err) |
| 10039 | raise_error_syntax("Bad fd number"); |
| 10040 | n->ndup.vname = makename(); |
| 10041 | } |
| 10042 | } |
| 10043 | |
| 10044 | /* |
| 10045 | * Returns true if the text contains nothing to expand (no dollar signs |
| 10046 | * or backquotes). |
| 10047 | */ |
| 10048 | static int |
| 10049 | noexpand(char *text) |
| 10050 | { |
| 10051 | char *p; |
| 10052 | char c; |
| 10053 | |
| 10054 | p = text; |
| 10055 | while ((c = *p++) != '\0') { |
| 10056 | if (c == CTLQUOTEMARK) |
| 10057 | continue; |
| 10058 | if (c == CTLESC) |
| 10059 | p++; |
| 10060 | else if (SIT(c, BASESYNTAX) == CCTL) |
| 10061 | return 0; |
| 10062 | } |
| 10063 | return 1; |
| 10064 | } |
| 10065 | |
| 10066 | static void |
| 10067 | parsefname(void) |
| 10068 | { |
| 10069 | union node *n = redirnode; |
| 10070 | |
| 10071 | if (readtoken() != TWORD) |
| 10072 | raise_error_unexpected_syntax(-1); |
| 10073 | if (n->type == NHERE) { |
| 10074 | struct heredoc *here = heredoc; |
| 10075 | struct heredoc *p; |
| 10076 | int i; |
| 10077 | |
| 10078 | if (quoteflag == 0) |
| 10079 | n->type = NXHERE; |
| 10080 | TRACE(("Here document %d\n", n->type)); |
| 10081 | if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
| 10082 | raise_error_syntax("Illegal eof marker for << redirection"); |
| 10083 | rmescapes(wordtext); |
| 10084 | here->eofmark = wordtext; |
| 10085 | here->next = NULL; |
| 10086 | if (heredoclist == NULL) |
| 10087 | heredoclist = here; |
| 10088 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10089 | for (p = heredoclist; p->next; p = p->next) |
| 10090 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10091 | p->next = here; |
| 10092 | } |
| 10093 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 10094 | fixredir(n, wordtext, 0); |
| 10095 | } else { |
| 10096 | n->nfile.fname = makename(); |
| 10097 | } |
| 10098 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10099 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10100 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10101 | simplecmd(void) |
| 10102 | { |
| 10103 | union node *args, **app; |
| 10104 | union node *n = NULL; |
| 10105 | union node *vars, **vpp; |
| 10106 | union node **rpp, *redir; |
| 10107 | int savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10108 | #if ENABLE_ASH_BASH_COMPAT |
| 10109 | smallint double_brackets_flag = 0; |
| 10110 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10111 | |
| 10112 | args = NULL; |
| 10113 | app = &args; |
| 10114 | vars = NULL; |
| 10115 | vpp = &vars; |
| 10116 | redir = NULL; |
| 10117 | rpp = &redir; |
| 10118 | |
| 10119 | savecheckkwd = CHKALIAS; |
| 10120 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10121 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10122 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10123 | t = readtoken(); |
| 10124 | switch (t) { |
| 10125 | #if ENABLE_ASH_BASH_COMPAT |
| 10126 | case TAND: /* "&&" */ |
| 10127 | case TOR: /* "||" */ |
| 10128 | if (!double_brackets_flag) { |
| 10129 | tokpushback = 1; |
| 10130 | goto out; |
| 10131 | } |
| 10132 | wordtext = (char *) (t == TAND ? "-a" : "-o"); |
| 10133 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10134 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10135 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10136 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10137 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10138 | n->narg.text = wordtext; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10139 | #if ENABLE_ASH_BASH_COMPAT |
| 10140 | if (strcmp("[[", wordtext) == 0) |
| 10141 | double_brackets_flag = 1; |
| 10142 | else if (strcmp("]]", wordtext) == 0) |
| 10143 | double_brackets_flag = 0; |
| 10144 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10145 | n->narg.backquote = backquotelist; |
| 10146 | if (savecheckkwd && isassignment(wordtext)) { |
| 10147 | *vpp = n; |
| 10148 | vpp = &n->narg.next; |
| 10149 | } else { |
| 10150 | *app = n; |
| 10151 | app = &n->narg.next; |
| 10152 | savecheckkwd = 0; |
| 10153 | } |
| 10154 | break; |
| 10155 | case TREDIR: |
| 10156 | *rpp = n = redirnode; |
| 10157 | rpp = &n->nfile.next; |
| 10158 | parsefname(); /* read name of redirection file */ |
| 10159 | break; |
| 10160 | case TLP: |
| 10161 | if (args && app == &args->narg.next |
| 10162 | && !vars && !redir |
| 10163 | ) { |
| 10164 | struct builtincmd *bcmd; |
| 10165 | const char *name; |
| 10166 | |
| 10167 | /* We have a function */ |
| 10168 | if (readtoken() != TRP) |
| 10169 | raise_error_unexpected_syntax(TRP); |
| 10170 | name = n->narg.text; |
| 10171 | if (!goodname(name) |
| 10172 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 10173 | ) { |
| 10174 | raise_error_syntax("Bad function name"); |
| 10175 | } |
| 10176 | n->type = NDEFUN; |
| 10177 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10178 | n->narg.next = parse_command(); |
| 10179 | return n; |
| 10180 | } |
| 10181 | /* fall through */ |
| 10182 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10183 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10184 | goto out; |
| 10185 | } |
| 10186 | } |
| 10187 | out: |
| 10188 | *app = NULL; |
| 10189 | *vpp = NULL; |
| 10190 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10191 | n = stzalloc(sizeof(struct ncmd)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10192 | n->type = NCMD; |
| 10193 | n->ncmd.args = args; |
| 10194 | n->ncmd.assign = vars; |
| 10195 | n->ncmd.redirect = redir; |
| 10196 | return n; |
| 10197 | } |
| 10198 | |
| 10199 | static union node * |
| 10200 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10201 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10202 | union node *n1, *n2; |
| 10203 | union node *ap, **app; |
| 10204 | union node *cp, **cpp; |
| 10205 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10206 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10207 | int t; |
| 10208 | |
| 10209 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10210 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 10211 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10212 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10213 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10214 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10215 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10216 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10217 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10218 | n1->type = NIF; |
| 10219 | n1->nif.test = list(0); |
| 10220 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10221 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10222 | n1->nif.ifpart = list(0); |
| 10223 | n2 = n1; |
| 10224 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10225 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10226 | n2 = n2->nif.elsepart; |
| 10227 | n2->type = NIF; |
| 10228 | n2->nif.test = list(0); |
| 10229 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10230 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10231 | n2->nif.ifpart = list(0); |
| 10232 | } |
| 10233 | if (lasttoken == TELSE) |
| 10234 | n2->nif.elsepart = list(0); |
| 10235 | else { |
| 10236 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10237 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10238 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10239 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10240 | break; |
| 10241 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10242 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10243 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10244 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10245 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10246 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10247 | got = readtoken(); |
| 10248 | if (got != TDO) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10249 | TRACE(("expecting DO got %s %s\n", tokname(got), |
| 10250 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10251 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10252 | } |
| 10253 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10254 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10255 | break; |
| 10256 | } |
| 10257 | case TFOR: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10258 | if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10259 | raise_error_syntax("Bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10260 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10261 | n1->type = NFOR; |
| 10262 | n1->nfor.var = wordtext; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10263 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10264 | if (readtoken() == TIN) { |
| 10265 | app = ≈ |
| 10266 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10267 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10268 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10269 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10270 | n2->narg.text = wordtext; |
| 10271 | n2->narg.backquote = backquotelist; |
| 10272 | *app = n2; |
| 10273 | app = &n2->narg.next; |
| 10274 | } |
| 10275 | *app = NULL; |
| 10276 | n1->nfor.args = ap; |
| 10277 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10278 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10279 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10280 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10281 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10282 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10283 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10284 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10285 | n1->nfor.args = n2; |
| 10286 | /* |
| 10287 | * Newline or semicolon here is optional (but note |
| 10288 | * that the original Bourne shell only allowed NL). |
| 10289 | */ |
| 10290 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10291 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10292 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10293 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10294 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10295 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10296 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10297 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10298 | break; |
| 10299 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10300 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10301 | n1->type = NCASE; |
| 10302 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10303 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10304 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10305 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10306 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10307 | n2->narg.text = wordtext; |
| 10308 | n2->narg.backquote = backquotelist; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10309 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10310 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10311 | } while (readtoken() == TNL); |
| 10312 | if (lasttoken != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10313 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10314 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10315 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10316 | checkkwd = CHKNL | CHKKWD; |
| 10317 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10318 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10319 | if (lasttoken == TLP) |
| 10320 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10321 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10322 | cp->type = NCLIST; |
| 10323 | app = &cp->nclist.pattern; |
| 10324 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10325 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10326 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10327 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10328 | ap->narg.text = wordtext; |
| 10329 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10330 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10331 | break; |
| 10332 | app = &ap->narg.next; |
| 10333 | readtoken(); |
| 10334 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10335 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10336 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10337 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10338 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10339 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10340 | cpp = &cp->nclist.next; |
| 10341 | |
| 10342 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10343 | t = readtoken(); |
| 10344 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10345 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10346 | raise_error_unexpected_syntax(TENDCASE); |
| 10347 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10348 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10349 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10350 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10351 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10352 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10353 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10354 | n1->type = NSUBSHELL; |
| 10355 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10356 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10357 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10358 | break; |
| 10359 | case TBEGIN: |
| 10360 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10361 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10362 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10363 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10364 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10365 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10366 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10367 | } |
| 10368 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10369 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10370 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10371 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10372 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10373 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10374 | checkkwd = CHKKWD | CHKALIAS; |
| 10375 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10376 | while (readtoken() == TREDIR) { |
| 10377 | *rpp = n2 = redirnode; |
| 10378 | rpp = &n2->nfile.next; |
| 10379 | parsefname(); |
| 10380 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10381 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10382 | *rpp = NULL; |
| 10383 | if (redir) { |
| 10384 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10385 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10386 | n2->type = NREDIR; |
| 10387 | n2->nredir.n = n1; |
| 10388 | n1 = n2; |
| 10389 | } |
| 10390 | n1->nredir.redirect = redir; |
| 10391 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10392 | return n1; |
| 10393 | } |
| 10394 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10395 | /* |
| 10396 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 10397 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 10398 | * word which marks the end of the document and striptabs is true if |
| 10399 | * leading tabs should be stripped from the document. The argument firstc |
| 10400 | * is the first character of the input token or document. |
| 10401 | * |
| 10402 | * Because C does not have internal subroutines, I have simulated them |
| 10403 | * using goto's to implement the subroutine linkage. The following macros |
| 10404 | * will run code that appears at the end of readtoken1. |
| 10405 | */ |
| 10406 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10407 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 10408 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 10409 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 10410 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 10411 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 10412 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10413 | |
| 10414 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10415 | readtoken1(int firstc, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10416 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10417 | /* NB: syntax parameter fits into smallint */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10418 | int c = firstc; |
| 10419 | char *out; |
| 10420 | int len; |
| 10421 | char line[EOFMARKLEN + 1]; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10422 | struct nodelist *bqlist; |
| 10423 | smallint quotef; |
| 10424 | smallint dblquote; |
| 10425 | smallint oldstyle; |
| 10426 | smallint prevsyntax; /* syntax before arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10427 | #if ENABLE_ASH_EXPAND_PRMT |
| 10428 | smallint pssyntax; /* we are expanding a prompt string */ |
| 10429 | #endif |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10430 | int varnest; /* levels of variables expansion */ |
| 10431 | int arinest; /* levels of arithmetic expansion */ |
| 10432 | int parenlevel; /* levels of parens in arithmetic */ |
| 10433 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 10434 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10435 | #if __GNUC__ |
| 10436 | /* Avoid longjmp clobbering */ |
| 10437 | (void) &out; |
| 10438 | (void) "ef; |
| 10439 | (void) &dblquote; |
| 10440 | (void) &varnest; |
| 10441 | (void) &arinest; |
| 10442 | (void) &parenlevel; |
| 10443 | (void) &dqvarnest; |
| 10444 | (void) &oldstyle; |
| 10445 | (void) &prevsyntax; |
| 10446 | (void) &syntax; |
| 10447 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10448 | startlinno = plinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10449 | bqlist = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10450 | quotef = 0; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10451 | oldstyle = 0; |
| 10452 | prevsyntax = 0; |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10453 | #if ENABLE_ASH_EXPAND_PRMT |
| 10454 | pssyntax = (syntax == PSSYNTAX); |
| 10455 | if (pssyntax) |
| 10456 | syntax = DQSYNTAX; |
| 10457 | #endif |
| 10458 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10459 | varnest = 0; |
| 10460 | arinest = 0; |
| 10461 | parenlevel = 0; |
| 10462 | dqvarnest = 0; |
| 10463 | |
| 10464 | STARTSTACKSTR(out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10465 | loop: { /* for each line, until end of word */ |
| 10466 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 10467 | for (;;) { /* until end of line or end of word */ |
| 10468 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 10469 | switch (SIT(c, syntax)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10470 | case CNL: /* '\n' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10471 | if (syntax == BASESYNTAX) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10472 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10473 | USTPUTC(c, out); |
| 10474 | plinno++; |
| 10475 | if (doprompt) |
| 10476 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10477 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10478 | goto loop; /* continue outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10479 | case CWORD: |
| 10480 | USTPUTC(c, out); |
| 10481 | break; |
| 10482 | case CCTL: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10483 | if (eofmark == NULL || dblquote) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10484 | USTPUTC(CTLESC, out); |
| 10485 | USTPUTC(c, out); |
| 10486 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10487 | case CBACK: /* backslash */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10488 | c = pgetc2(); |
| 10489 | if (c == PEOF) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10490 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10491 | USTPUTC('\\', out); |
| 10492 | pungetc(); |
| 10493 | } else if (c == '\n') { |
| 10494 | if (doprompt) |
| 10495 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10496 | } else { |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10497 | #if ENABLE_ASH_EXPAND_PRMT |
| 10498 | if (c == '$' && pssyntax) { |
| 10499 | USTPUTC(CTLESC, out); |
| 10500 | USTPUTC('\\', out); |
| 10501 | } |
| 10502 | #endif |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 10503 | if (dblquote && |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10504 | c != '\\' && c != '`' && |
| 10505 | c != '$' && ( |
| 10506 | c != '"' || |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 10507 | eofmark != NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10508 | ) { |
| 10509 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10510 | USTPUTC('\\', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10511 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10512 | if (SIT(c, SQSYNTAX) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10513 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10514 | USTPUTC(c, out); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10515 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10516 | } |
| 10517 | break; |
| 10518 | case CSQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10519 | syntax = SQSYNTAX; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10520 | quotemark: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10521 | if (eofmark == NULL) { |
| 10522 | USTPUTC(CTLQUOTEMARK, out); |
| 10523 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10524 | break; |
| 10525 | case CDQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10526 | syntax = DQSYNTAX; |
| 10527 | dblquote = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10528 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10529 | case CENDQUOTE: |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10530 | if (eofmark != NULL && arinest == 0 |
| 10531 | && varnest == 0 |
| 10532 | ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10533 | USTPUTC(c, out); |
| 10534 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10535 | if (dqvarnest == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10536 | syntax = BASESYNTAX; |
| 10537 | dblquote = 0; |
| 10538 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10539 | quotef = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10540 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10541 | } |
| 10542 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10543 | case CVAR: /* '$' */ |
| 10544 | PARSESUB(); /* parse substitution */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10545 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10546 | case CENDVAR: /* '}' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10547 | if (varnest > 0) { |
| 10548 | varnest--; |
| 10549 | if (dqvarnest > 0) { |
| 10550 | dqvarnest--; |
| 10551 | } |
| 10552 | USTPUTC(CTLENDVAR, out); |
| 10553 | } else { |
| 10554 | USTPUTC(c, out); |
| 10555 | } |
| 10556 | break; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10557 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10558 | case CLP: /* '(' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10559 | parenlevel++; |
| 10560 | USTPUTC(c, out); |
| 10561 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10562 | case CRP: /* ')' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10563 | if (parenlevel > 0) { |
| 10564 | USTPUTC(c, out); |
| 10565 | --parenlevel; |
| 10566 | } else { |
| 10567 | if (pgetc() == ')') { |
| 10568 | if (--arinest == 0) { |
| 10569 | USTPUTC(CTLENDARI, out); |
| 10570 | syntax = prevsyntax; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10571 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10572 | } else |
| 10573 | USTPUTC(')', out); |
| 10574 | } else { |
| 10575 | /* |
| 10576 | * unbalanced parens |
| 10577 | * (don't 2nd guess - no error) |
| 10578 | */ |
| 10579 | pungetc(); |
| 10580 | USTPUTC(')', out); |
| 10581 | } |
| 10582 | } |
| 10583 | break; |
| 10584 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10585 | case CBQUOTE: /* '`' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10586 | PARSEBACKQOLD(); |
| 10587 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10588 | case CENDFILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10589 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10590 | case CIGN: |
| 10591 | break; |
| 10592 | default: |
| 10593 | if (varnest == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10594 | goto endword; /* exit outer loop */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10595 | #if ENABLE_ASH_ALIAS |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 10596 | if (c != PEOA) |
| 10597 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10598 | USTPUTC(c, out); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 10599 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10600 | } |
| 10601 | c = pgetc_macro(); |
| 10602 | } |
| 10603 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10604 | endword: |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10605 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10606 | if (syntax == ARISYNTAX) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10607 | raise_error_syntax("Missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10608 | #endif |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10609 | if (syntax != BASESYNTAX && !parsebackquote && eofmark == NULL) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10610 | raise_error_syntax("Unterminated quoted string"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10611 | if (varnest != 0) { |
| 10612 | startlinno = plinno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10613 | /* { */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10614 | raise_error_syntax("Missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10615 | } |
| 10616 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10617 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10618 | out = stackblock(); |
| 10619 | if (eofmark == NULL) { |
| 10620 | if ((c == '>' || c == '<') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10621 | && quotef == 0 |
| 10622 | && len <= 2 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10623 | && (*out == '\0' || isdigit(*out))) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10624 | PARSEREDIR(); |
| 10625 | return lasttoken = TREDIR; |
| 10626 | } else { |
| 10627 | pungetc(); |
| 10628 | } |
| 10629 | } |
| 10630 | quoteflag = quotef; |
| 10631 | backquotelist = bqlist; |
| 10632 | grabstackblock(len); |
| 10633 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10634 | lasttoken = TWORD; |
| 10635 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10636 | /* end of readtoken routine */ |
| 10637 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10638 | /* |
| 10639 | * Check to see whether we are at the end of the here document. When this |
| 10640 | * is called, c is set to the first character of the next input line. If |
| 10641 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 10642 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10643 | checkend: { |
| 10644 | if (eofmark) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10645 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10646 | if (c == PEOA) { |
| 10647 | c = pgetc2(); |
| 10648 | } |
| 10649 | #endif |
| 10650 | if (striptabs) { |
| 10651 | while (c == '\t') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10652 | c = pgetc2(); |
| 10653 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10654 | } |
| 10655 | if (c == *eofmark) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10656 | if (pfgets(line, sizeof(line)) != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10657 | char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10658 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10659 | p = line; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10660 | for (q = eofmark + 1; *q && *p == *q; p++, q++) |
| 10661 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10662 | if (*p == '\n' && *q == '\0') { |
| 10663 | c = PEOF; |
| 10664 | plinno++; |
| 10665 | needprompt = doprompt; |
| 10666 | } else { |
| 10667 | pushstring(line, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10668 | } |
| 10669 | } |
| 10670 | } |
| 10671 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10672 | goto checkend_return; |
| 10673 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10674 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10675 | /* |
| 10676 | * Parse a redirection operator. The variable "out" points to a string |
| 10677 | * specifying the fd to be redirected. The variable "c" contains the |
| 10678 | * first character of the redirection operator. |
| 10679 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10680 | parseredir: { |
| 10681 | char fd = *out; |
| 10682 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10683 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10684 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10685 | if (c == '>') { |
| 10686 | np->nfile.fd = 1; |
| 10687 | c = pgetc(); |
| 10688 | if (c == '>') |
| 10689 | np->type = NAPPEND; |
| 10690 | else if (c == '|') |
| 10691 | np->type = NCLOBBER; |
| 10692 | else if (c == '&') |
| 10693 | np->type = NTOFD; |
| 10694 | else { |
| 10695 | np->type = NTO; |
| 10696 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10697 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10698 | } else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10699 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10700 | c = pgetc(); |
| 10701 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10702 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10703 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10704 | np = stzalloc(sizeof(struct nhere)); |
| 10705 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10706 | } |
| 10707 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10708 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10709 | heredoc->here = np; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10710 | c = pgetc(); |
| 10711 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10712 | heredoc->striptabs = 1; |
| 10713 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10714 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10715 | pungetc(); |
| 10716 | } |
| 10717 | break; |
| 10718 | |
| 10719 | case '&': |
| 10720 | np->type = NFROMFD; |
| 10721 | break; |
| 10722 | |
| 10723 | case '>': |
| 10724 | np->type = NFROMTO; |
| 10725 | break; |
| 10726 | |
| 10727 | default: |
| 10728 | np->type = NFROM; |
| 10729 | pungetc(); |
| 10730 | break; |
| 10731 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10732 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10733 | if (fd != '\0') |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10734 | np->nfile.fd = fd - '0'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10735 | redirnode = np; |
| 10736 | goto parseredir_return; |
| 10737 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10738 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10739 | /* |
| 10740 | * Parse a substitution. At this point, we have read the dollar sign |
| 10741 | * and nothing else. |
| 10742 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 10743 | |
| 10744 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 10745 | * (assuming ascii char codes, as the original implementation did) */ |
| 10746 | #define is_special(c) \ |
| 10747 | ((((unsigned int)c) - 33 < 32) \ |
| 10748 | && ((0xc1ff920dUL >> (((unsigned int)c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10749 | parsesub: { |
| 10750 | int subtype; |
| 10751 | int typeloc; |
| 10752 | int flags; |
| 10753 | char *p; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 10754 | static const char types[] ALIGN1 = "}-+?="; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10755 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10756 | c = pgetc(); |
| 10757 | if ( |
| 10758 | c <= PEOA_OR_PEOF || |
| 10759 | (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
| 10760 | ) { |
| 10761 | USTPUTC('$', out); |
| 10762 | pungetc(); |
| 10763 | } else if (c == '(') { /* $(command) or $((arith)) */ |
| 10764 | if (pgetc() == '(') { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10765 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10766 | PARSEARITH(); |
| 10767 | #else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10768 | raise_error_syntax("We unsupport $((arith))"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10769 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10770 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10771 | pungetc(); |
| 10772 | PARSEBACKQNEW(); |
| 10773 | } |
| 10774 | } else { |
| 10775 | USTPUTC(CTLVAR, out); |
| 10776 | typeloc = out - (char *)stackblock(); |
| 10777 | USTPUTC(VSNORMAL, out); |
| 10778 | subtype = VSNORMAL; |
| 10779 | if (c == '{') { |
| 10780 | c = pgetc(); |
| 10781 | if (c == '#') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10782 | c = pgetc(); |
| 10783 | if (c == '}') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10784 | c = '#'; |
| 10785 | else |
| 10786 | subtype = VSLENGTH; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10787 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10788 | subtype = 0; |
| 10789 | } |
| 10790 | if (c > PEOA_OR_PEOF && is_name(c)) { |
| 10791 | do { |
| 10792 | STPUTC(c, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10793 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10794 | } while (c > PEOA_OR_PEOF && is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10795 | } else if (isdigit(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10796 | do { |
| 10797 | STPUTC(c, out); |
| 10798 | c = pgetc(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10799 | } while (isdigit(c)); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10800 | } else if (is_special(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10801 | USTPUTC(c, out); |
| 10802 | c = pgetc(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10803 | } else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10804 | badsub: raise_error_syntax("Bad substitution"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10805 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10806 | STPUTC('=', out); |
| 10807 | flags = 0; |
| 10808 | if (subtype == 0) { |
| 10809 | switch (c) { |
| 10810 | case ':': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10811 | c = pgetc(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 10812 | #if ENABLE_ASH_BASH_COMPAT |
| 10813 | if (c == ':' || c == '$' || isdigit(c)) { |
| 10814 | pungetc(); |
| 10815 | subtype = VSSUBSTR; |
| 10816 | break; |
| 10817 | } |
| 10818 | #endif |
| 10819 | flags = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10820 | /*FALLTHROUGH*/ |
| 10821 | default: |
| 10822 | p = strchr(types, c); |
| 10823 | if (p == NULL) |
| 10824 | goto badsub; |
| 10825 | subtype = p - types + VSNORMAL; |
| 10826 | break; |
| 10827 | case '%': |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 10828 | case '#': { |
| 10829 | int cc = c; |
| 10830 | subtype = c == '#' ? VSTRIMLEFT : VSTRIMRIGHT; |
| 10831 | c = pgetc(); |
| 10832 | if (c == cc) |
| 10833 | subtype++; |
| 10834 | else |
| 10835 | pungetc(); |
| 10836 | break; |
| 10837 | } |
| 10838 | #if ENABLE_ASH_BASH_COMPAT |
| 10839 | case '/': |
| 10840 | subtype = VSREPLACE; |
| 10841 | c = pgetc(); |
| 10842 | if (c == '/') |
| 10843 | subtype++; /* VSREPLACEALL */ |
| 10844 | else |
| 10845 | pungetc(); |
| 10846 | break; |
| 10847 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10848 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10849 | } else { |
| 10850 | pungetc(); |
| 10851 | } |
| 10852 | if (dblquote || arinest) |
| 10853 | flags |= VSQUOTE; |
| 10854 | *((char *)stackblock() + typeloc) = subtype | flags; |
| 10855 | if (subtype != VSNORMAL) { |
| 10856 | varnest++; |
| 10857 | if (dblquote || arinest) { |
| 10858 | dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10859 | } |
| 10860 | } |
| 10861 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10862 | goto parsesub_return; |
| 10863 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10864 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10865 | /* |
| 10866 | * Called to parse command substitutions. Newstyle is set if the command |
| 10867 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 10868 | * list of commands (passed by reference), and savelen is the number of |
| 10869 | * characters on the top of the stack which must be preserved. |
| 10870 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10871 | parsebackq: { |
| 10872 | struct nodelist **nlpp; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10873 | smallint savepbq; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10874 | union node *n; |
| 10875 | char *volatile str; |
| 10876 | struct jmploc jmploc; |
| 10877 | struct jmploc *volatile savehandler; |
| 10878 | size_t savelen; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10879 | smallint saveprompt = 0; |
| 10880 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10881 | #ifdef __GNUC__ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10882 | (void) &saveprompt; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10883 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10884 | savepbq = parsebackquote; |
| 10885 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 10886 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10887 | parsebackquote = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10888 | exception_handler = savehandler; |
| 10889 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10890 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10891 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10892 | str = NULL; |
| 10893 | savelen = out - (char *)stackblock(); |
| 10894 | if (savelen > 0) { |
| 10895 | str = ckmalloc(savelen); |
| 10896 | memcpy(str, stackblock(), savelen); |
| 10897 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10898 | savehandler = exception_handler; |
| 10899 | exception_handler = &jmploc; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10900 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10901 | if (oldstyle) { |
| 10902 | /* We must read until the closing backquote, giving special |
| 10903 | treatment to some slashes, and then push the string and |
| 10904 | reread it as input, interpreting it normally. */ |
| 10905 | char *pout; |
| 10906 | int pc; |
| 10907 | size_t psavelen; |
| 10908 | char *pstr; |
| 10909 | |
| 10910 | |
| 10911 | STARTSTACKSTR(pout); |
| 10912 | for (;;) { |
| 10913 | if (needprompt) { |
| 10914 | setprompt(2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10915 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10916 | pc = pgetc(); |
| 10917 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10918 | case '`': |
| 10919 | goto done; |
| 10920 | |
| 10921 | case '\\': |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10922 | pc = pgetc(); |
| 10923 | if (pc == '\n') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10924 | plinno++; |
| 10925 | if (doprompt) |
| 10926 | setprompt(2); |
| 10927 | /* |
| 10928 | * If eating a newline, avoid putting |
| 10929 | * the newline into the new character |
| 10930 | * stream (via the STPUTC after the |
| 10931 | * switch). |
| 10932 | */ |
| 10933 | continue; |
| 10934 | } |
| 10935 | if (pc != '\\' && pc != '`' && pc != '$' |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10936 | && (!dblquote || pc != '"')) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10937 | STPUTC('\\', pout); |
| 10938 | if (pc > PEOA_OR_PEOF) { |
| 10939 | break; |
| 10940 | } |
| 10941 | /* fall through */ |
| 10942 | |
| 10943 | case PEOF: |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10944 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10945 | case PEOA: |
| 10946 | #endif |
| 10947 | startlinno = plinno; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10948 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10949 | |
| 10950 | case '\n': |
| 10951 | plinno++; |
| 10952 | needprompt = doprompt; |
| 10953 | break; |
| 10954 | |
| 10955 | default: |
| 10956 | break; |
| 10957 | } |
| 10958 | STPUTC(pc, pout); |
| 10959 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10960 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10961 | STPUTC('\0', pout); |
| 10962 | psavelen = pout - (char *)stackblock(); |
| 10963 | if (psavelen > 0) { |
| 10964 | pstr = grabstackstr(pout); |
| 10965 | setinputstring(pstr); |
| 10966 | } |
| 10967 | } |
| 10968 | nlpp = &bqlist; |
| 10969 | while (*nlpp) |
| 10970 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10971 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 10972 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10973 | parsebackquote = oldstyle; |
| 10974 | |
| 10975 | if (oldstyle) { |
| 10976 | saveprompt = doprompt; |
| 10977 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10978 | } |
| 10979 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10980 | n = list(2); |
| 10981 | |
| 10982 | if (oldstyle) |
| 10983 | doprompt = saveprompt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10984 | else if (readtoken() != TRP) |
| 10985 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10986 | |
| 10987 | (*nlpp)->n = n; |
| 10988 | if (oldstyle) { |
| 10989 | /* |
| 10990 | * Start reading from old file again, ignoring any pushed back |
| 10991 | * tokens left from the backquote parsing |
| 10992 | */ |
| 10993 | popfile(); |
| 10994 | tokpushback = 0; |
| 10995 | } |
| 10996 | while (stackblocksize() <= savelen) |
| 10997 | growstackblock(); |
| 10998 | STARTSTACKSTR(out); |
| 10999 | if (str) { |
| 11000 | memcpy(out, str, savelen); |
| 11001 | STADJUST(savelen, out); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11002 | INT_OFF; |
| 11003 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11004 | str = NULL; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11005 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11006 | } |
| 11007 | parsebackquote = savepbq; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11008 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11009 | if (arinest || dblquote) |
| 11010 | USTPUTC(CTLBACKQ | CTLQUOTE, out); |
| 11011 | else |
| 11012 | USTPUTC(CTLBACKQ, out); |
| 11013 | if (oldstyle) |
| 11014 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11015 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11016 | } |
| 11017 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11018 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11019 | /* |
| 11020 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 11021 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11022 | parsearith: { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11023 | if (++arinest == 1) { |
| 11024 | prevsyntax = syntax; |
| 11025 | syntax = ARISYNTAX; |
| 11026 | USTPUTC(CTLARI, out); |
| 11027 | if (dblquote) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11028 | USTPUTC('"', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11029 | else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11030 | USTPUTC(' ', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11031 | } else { |
| 11032 | /* |
| 11033 | * we collapse embedded arithmetic expansion to |
| 11034 | * parenthesis, which should be equivalent |
| 11035 | */ |
| 11036 | USTPUTC('(', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11037 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11038 | goto parsearith_return; |
| 11039 | } |
| 11040 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11041 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11042 | } /* end of readtoken */ |
| 11043 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11044 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11045 | * Read the next input token. |
| 11046 | * If the token is a word, we set backquotelist to the list of cmds in |
| 11047 | * backquotes. We set quoteflag to true if any part of the word was |
| 11048 | * quoted. |
| 11049 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 11050 | * the redirection. |
| 11051 | * In all cases, the variable startlinno is set to the number of the line |
| 11052 | * on which the token starts. |
| 11053 | * |
| 11054 | * [Change comment: here documents and internal procedures] |
| 11055 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 11056 | * word parsing code into a separate routine. In this case, readtoken |
| 11057 | * doesn't need to have any internal procedures, but parseword does. |
| 11058 | * We could also make parseoperator in essence the main routine, and |
| 11059 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11060 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11061 | #define NEW_xxreadtoken |
| 11062 | #ifdef NEW_xxreadtoken |
| 11063 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11064 | static const char xxreadtoken_chars[7] ALIGN1 = { |
| 11065 | '\n', '(', ')', '&', '|', ';', 0 |
| 11066 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11067 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11068 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11069 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 11070 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 11071 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11072 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11073 | }; |
| 11074 | |
| 11075 | #define xxreadtoken_doubles \ |
| 11076 | (sizeof(xxreadtoken_tokens) - sizeof(xxreadtoken_chars)) |
| 11077 | #define xxreadtoken_singles \ |
| 11078 | (sizeof(xxreadtoken_chars) - xxreadtoken_doubles - 1) |
| 11079 | |
| 11080 | static int |
| 11081 | xxreadtoken(void) |
| 11082 | { |
| 11083 | int c; |
| 11084 | |
| 11085 | if (tokpushback) { |
| 11086 | tokpushback = 0; |
| 11087 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11088 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11089 | if (needprompt) { |
| 11090 | setprompt(2); |
| 11091 | } |
| 11092 | startlinno = plinno; |
| 11093 | for (;;) { /* until token or start of word found */ |
| 11094 | c = pgetc_macro(); |
| 11095 | |
| 11096 | if ((c != ' ') && (c != '\t') |
| 11097 | #if ENABLE_ASH_ALIAS |
| 11098 | && (c != PEOA) |
| 11099 | #endif |
| 11100 | ) { |
| 11101 | if (c == '#') { |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11102 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11103 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11104 | pungetc(); |
| 11105 | } else if (c == '\\') { |
| 11106 | if (pgetc() != '\n') { |
| 11107 | pungetc(); |
| 11108 | goto READTOKEN1; |
| 11109 | } |
| 11110 | startlinno = ++plinno; |
| 11111 | if (doprompt) |
| 11112 | setprompt(2); |
| 11113 | } else { |
| 11114 | const char *p |
| 11115 | = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 11116 | |
| 11117 | if (c != PEOF) { |
| 11118 | if (c == '\n') { |
| 11119 | plinno++; |
| 11120 | needprompt = doprompt; |
| 11121 | } |
| 11122 | |
| 11123 | p = strchr(xxreadtoken_chars, c); |
| 11124 | if (p == NULL) { |
| 11125 | READTOKEN1: |
| 11126 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
| 11127 | } |
| 11128 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 11129 | if ((size_t)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11130 | if (pgetc() == *p) { /* double occurrence? */ |
| 11131 | p += xxreadtoken_doubles + 1; |
| 11132 | } else { |
| 11133 | pungetc(); |
| 11134 | } |
| 11135 | } |
| 11136 | } |
| 11137 | return lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 11138 | } |
| 11139 | } |
| 11140 | } /* for */ |
| 11141 | } |
| 11142 | #else |
| 11143 | #define RETURN(token) return lasttoken = token |
| 11144 | static int |
| 11145 | xxreadtoken(void) |
| 11146 | { |
| 11147 | int c; |
| 11148 | |
| 11149 | if (tokpushback) { |
| 11150 | tokpushback = 0; |
| 11151 | return lasttoken; |
| 11152 | } |
| 11153 | if (needprompt) { |
| 11154 | setprompt(2); |
| 11155 | } |
| 11156 | startlinno = plinno; |
| 11157 | for (;;) { /* until token or start of word found */ |
| 11158 | c = pgetc_macro(); |
| 11159 | switch (c) { |
| 11160 | case ' ': case '\t': |
| 11161 | #if ENABLE_ASH_ALIAS |
| 11162 | case PEOA: |
| 11163 | #endif |
| 11164 | continue; |
| 11165 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11166 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11167 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11168 | pungetc(); |
| 11169 | continue; |
| 11170 | case '\\': |
| 11171 | if (pgetc() == '\n') { |
| 11172 | startlinno = ++plinno; |
| 11173 | if (doprompt) |
| 11174 | setprompt(2); |
| 11175 | continue; |
| 11176 | } |
| 11177 | pungetc(); |
| 11178 | goto breakloop; |
| 11179 | case '\n': |
| 11180 | plinno++; |
| 11181 | needprompt = doprompt; |
| 11182 | RETURN(TNL); |
| 11183 | case PEOF: |
| 11184 | RETURN(TEOF); |
| 11185 | case '&': |
| 11186 | if (pgetc() == '&') |
| 11187 | RETURN(TAND); |
| 11188 | pungetc(); |
| 11189 | RETURN(TBACKGND); |
| 11190 | case '|': |
| 11191 | if (pgetc() == '|') |
| 11192 | RETURN(TOR); |
| 11193 | pungetc(); |
| 11194 | RETURN(TPIPE); |
| 11195 | case ';': |
| 11196 | if (pgetc() == ';') |
| 11197 | RETURN(TENDCASE); |
| 11198 | pungetc(); |
| 11199 | RETURN(TSEMI); |
| 11200 | case '(': |
| 11201 | RETURN(TLP); |
| 11202 | case ')': |
| 11203 | RETURN(TRP); |
| 11204 | default: |
| 11205 | goto breakloop; |
| 11206 | } |
| 11207 | } |
| 11208 | breakloop: |
| 11209 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 11210 | #undef RETURN |
| 11211 | } |
| 11212 | #endif /* NEW_xxreadtoken */ |
| 11213 | |
| 11214 | static int |
| 11215 | readtoken(void) |
| 11216 | { |
| 11217 | int t; |
| 11218 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11219 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11220 | #endif |
| 11221 | |
| 11222 | #if ENABLE_ASH_ALIAS |
| 11223 | top: |
| 11224 | #endif |
| 11225 | |
| 11226 | t = xxreadtoken(); |
| 11227 | |
| 11228 | /* |
| 11229 | * eat newlines |
| 11230 | */ |
| 11231 | if (checkkwd & CHKNL) { |
| 11232 | while (t == TNL) { |
| 11233 | parseheredoc(); |
| 11234 | t = xxreadtoken(); |
| 11235 | } |
| 11236 | } |
| 11237 | |
| 11238 | if (t != TWORD || quoteflag) { |
| 11239 | goto out; |
| 11240 | } |
| 11241 | |
| 11242 | /* |
| 11243 | * check for keywords |
| 11244 | */ |
| 11245 | if (checkkwd & CHKKWD) { |
| 11246 | const char *const *pp; |
| 11247 | |
| 11248 | pp = findkwd(wordtext); |
| 11249 | if (pp) { |
| 11250 | lasttoken = t = pp - tokname_array; |
| 11251 | TRACE(("keyword %s recognized\n", tokname(t))); |
| 11252 | goto out; |
| 11253 | } |
| 11254 | } |
| 11255 | |
| 11256 | if (checkkwd & CHKALIAS) { |
| 11257 | #if ENABLE_ASH_ALIAS |
| 11258 | struct alias *ap; |
| 11259 | ap = lookupalias(wordtext, 1); |
| 11260 | if (ap != NULL) { |
| 11261 | if (*ap->val) { |
| 11262 | pushstring(ap->val, ap); |
| 11263 | } |
| 11264 | goto top; |
| 11265 | } |
| 11266 | #endif |
| 11267 | } |
| 11268 | out: |
| 11269 | checkkwd = 0; |
| 11270 | #if DEBUG |
| 11271 | if (!alreadyseen) |
| 11272 | TRACE(("token %s %s\n", tokname(t), t == TWORD ? wordtext : "")); |
| 11273 | else |
| 11274 | TRACE(("reread token %s %s\n", tokname(t), t == TWORD ? wordtext : "")); |
| 11275 | #endif |
| 11276 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11277 | } |
| 11278 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11279 | static char |
| 11280 | peektoken(void) |
| 11281 | { |
| 11282 | int t; |
| 11283 | |
| 11284 | t = readtoken(); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11285 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11286 | return tokname_array[t][0]; |
| 11287 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11288 | |
| 11289 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11290 | * Read and parse a command. Returns NEOF on end of file. (NULL is a |
| 11291 | * valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11292 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11293 | static union node * |
| 11294 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11295 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11296 | int t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11297 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11298 | tokpushback = 0; |
| 11299 | doprompt = interact; |
| 11300 | if (doprompt) |
| 11301 | setprompt(doprompt); |
| 11302 | needprompt = 0; |
| 11303 | t = readtoken(); |
| 11304 | if (t == TEOF) |
| 11305 | return NEOF; |
| 11306 | if (t == TNL) |
| 11307 | return NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11308 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11309 | return list(1); |
| 11310 | } |
| 11311 | |
| 11312 | /* |
| 11313 | * Input any here documents. |
| 11314 | */ |
| 11315 | static void |
| 11316 | parseheredoc(void) |
| 11317 | { |
| 11318 | struct heredoc *here; |
| 11319 | union node *n; |
| 11320 | |
| 11321 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11322 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11323 | |
| 11324 | while (here) { |
| 11325 | if (needprompt) { |
| 11326 | setprompt(2); |
| 11327 | } |
| 11328 | readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, |
| 11329 | here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11330 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11331 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11332 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11333 | n->narg.text = wordtext; |
| 11334 | n->narg.backquote = backquotelist; |
| 11335 | here->here->nhere.doc = n; |
| 11336 | here = here->next; |
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 | |
| 11340 | |
| 11341 | /* |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 11342 | * called by editline -- any expansions to the prompt should be added here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11343 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11344 | #if ENABLE_ASH_EXPAND_PRMT |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11345 | static const char * |
| 11346 | expandstr(const char *ps) |
| 11347 | { |
| 11348 | union node n; |
| 11349 | |
| 11350 | /* XXX Fix (char *) cast. */ |
| 11351 | setinputstring((char *)ps); |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11352 | readtoken1(pgetc(), PSSYNTAX, nullstr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11353 | popfile(); |
| 11354 | |
| 11355 | n.narg.type = NARG; |
| 11356 | n.narg.next = NULL; |
| 11357 | n.narg.text = wordtext; |
| 11358 | n.narg.backquote = backquotelist; |
| 11359 | |
| 11360 | expandarg(&n, NULL, 0); |
| 11361 | return stackblock(); |
| 11362 | } |
| 11363 | #endif |
| 11364 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11365 | /* |
| 11366 | * Execute a command or commands contained in a string. |
| 11367 | */ |
| 11368 | static int |
| 11369 | evalstring(char *s, int mask) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11370 | { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11371 | union node *n; |
| 11372 | struct stackmark smark; |
| 11373 | int skip; |
| 11374 | |
| 11375 | setinputstring(s); |
| 11376 | setstackmark(&smark); |
| 11377 | |
| 11378 | skip = 0; |
| 11379 | while ((n = parsecmd(0)) != NEOF) { |
| 11380 | evaltree(n, 0); |
| 11381 | popstackmark(&smark); |
| 11382 | skip = evalskip; |
| 11383 | if (skip) |
| 11384 | break; |
| 11385 | } |
| 11386 | popfile(); |
| 11387 | |
| 11388 | skip &= mask; |
| 11389 | evalskip = skip; |
| 11390 | return skip; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11391 | } |
| 11392 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11393 | /* |
| 11394 | * The eval command. |
| 11395 | */ |
| 11396 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11397 | evalcmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11398 | { |
| 11399 | char *p; |
| 11400 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11401 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11402 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11403 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11404 | argv += 2; |
| 11405 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11406 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11407 | for (;;) { |
| 11408 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11409 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11410 | if (p == NULL) |
| 11411 | break; |
| 11412 | STPUTC(' ', concat); |
| 11413 | } |
| 11414 | STPUTC('\0', concat); |
| 11415 | p = grabstackstr(concat); |
| 11416 | } |
| 11417 | evalstring(p, ~SKIPEVAL); |
| 11418 | |
| 11419 | } |
| 11420 | return exitstatus; |
| 11421 | } |
| 11422 | |
| 11423 | /* |
| 11424 | * Read and execute commands. "Top" is nonzero for the top level command |
| 11425 | * loop; it turns on prompting if the shell is interactive. |
| 11426 | */ |
| 11427 | static int |
| 11428 | cmdloop(int top) |
| 11429 | { |
| 11430 | union node *n; |
| 11431 | struct stackmark smark; |
| 11432 | int inter; |
| 11433 | int numeof = 0; |
| 11434 | |
| 11435 | TRACE(("cmdloop(%d) called\n", top)); |
| 11436 | for (;;) { |
| 11437 | int skip; |
| 11438 | |
| 11439 | setstackmark(&smark); |
| 11440 | #if JOBS |
| 11441 | if (jobctl) |
| 11442 | showjobs(stderr, SHOW_CHANGED); |
| 11443 | #endif |
| 11444 | inter = 0; |
| 11445 | if (iflag && top) { |
| 11446 | inter++; |
| 11447 | #if ENABLE_ASH_MAIL |
| 11448 | chkmail(); |
| 11449 | #endif |
| 11450 | } |
| 11451 | n = parsecmd(inter); |
| 11452 | /* showtree(n); DEBUG */ |
| 11453 | if (n == NEOF) { |
| 11454 | if (!top || numeof >= 50) |
| 11455 | break; |
| 11456 | if (!stoppedjobs()) { |
| 11457 | if (!Iflag) |
| 11458 | break; |
| 11459 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 11460 | } |
| 11461 | numeof++; |
| 11462 | } else if (nflag == 0) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 11463 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 11464 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11465 | numeof = 0; |
| 11466 | evaltree(n, 0); |
| 11467 | } |
| 11468 | popstackmark(&smark); |
| 11469 | skip = evalskip; |
| 11470 | |
| 11471 | if (skip) { |
| 11472 | evalskip = 0; |
| 11473 | return skip & SKIPEVAL; |
| 11474 | } |
| 11475 | } |
| 11476 | return 0; |
| 11477 | } |
| 11478 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11479 | /* |
| 11480 | * Take commands from a file. To be compatible we should do a path |
| 11481 | * search for the file, which is necessary to find sub-commands. |
| 11482 | */ |
| 11483 | static char * |
| 11484 | find_dot_file(char *name) |
| 11485 | { |
| 11486 | char *fullname; |
| 11487 | const char *path = pathval(); |
| 11488 | struct stat statb; |
| 11489 | |
| 11490 | /* don't try this for absolute or relative paths */ |
| 11491 | if (strchr(name, '/')) |
| 11492 | return name; |
| 11493 | |
| 11494 | while ((fullname = padvance(&path, name)) != NULL) { |
| 11495 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 11496 | /* |
| 11497 | * Don't bother freeing here, since it will |
| 11498 | * be freed by the caller. |
| 11499 | */ |
| 11500 | return fullname; |
| 11501 | } |
| 11502 | stunalloc(fullname); |
| 11503 | } |
| 11504 | |
| 11505 | /* not found in the PATH */ |
| 11506 | ash_msg_and_raise_error("%s: not found", name); |
| 11507 | /* NOTREACHED */ |
| 11508 | } |
| 11509 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11510 | static int |
| 11511 | dotcmd(int argc, char **argv) |
| 11512 | { |
| 11513 | struct strlist *sp; |
| 11514 | volatile struct shparam saveparam; |
| 11515 | int status = 0; |
| 11516 | |
| 11517 | for (sp = cmdenviron; sp; sp = sp->next) |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 11518 | setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11519 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11520 | if (argv[1]) { /* That's what SVR2 does */ |
| 11521 | char *fullname = find_dot_file(argv[1]); |
| 11522 | argv += 2; |
| 11523 | argc -= 2; |
| 11524 | if (argc) { /* argc > 0, argv[0] != NULL */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11525 | saveparam = shellparam; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 11526 | shellparam.malloced = 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11527 | shellparam.nparam = argc; |
| 11528 | shellparam.p = argv; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11529 | }; |
| 11530 | |
| 11531 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 11532 | commandname = fullname; |
| 11533 | cmdloop(0); |
| 11534 | popfile(); |
| 11535 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11536 | if (argc) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11537 | freeparam(&shellparam); |
| 11538 | shellparam = saveparam; |
| 11539 | }; |
| 11540 | status = exitstatus; |
| 11541 | } |
| 11542 | return status; |
| 11543 | } |
| 11544 | |
| 11545 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11546 | exitcmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11547 | { |
| 11548 | if (stoppedjobs()) |
| 11549 | return 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11550 | if (argv[1]) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11551 | exitstatus = number(argv[1]); |
| 11552 | raise_exception(EXEXIT); |
| 11553 | /* NOTREACHED */ |
| 11554 | } |
| 11555 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11556 | /* |
| 11557 | * Read a file containing shell functions. |
| 11558 | */ |
| 11559 | static void |
| 11560 | readcmdfile(char *name) |
| 11561 | { |
| 11562 | setinputfile(name, INPUT_PUSH_FILE); |
| 11563 | cmdloop(0); |
| 11564 | popfile(); |
| 11565 | } |
| 11566 | |
| 11567 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11568 | /* ============ find_command inplementation */ |
| 11569 | |
| 11570 | /* |
| 11571 | * Resolve a command name. If you change this routine, you may have to |
| 11572 | * change the shellexec routine as well. |
| 11573 | */ |
| 11574 | static void |
| 11575 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 11576 | { |
| 11577 | struct tblentry *cmdp; |
| 11578 | int idx; |
| 11579 | int prev; |
| 11580 | char *fullname; |
| 11581 | struct stat statb; |
| 11582 | int e; |
| 11583 | int updatetbl; |
| 11584 | struct builtincmd *bcmd; |
| 11585 | |
| 11586 | /* If name contains a slash, don't use PATH or hash table */ |
| 11587 | if (strchr(name, '/') != NULL) { |
| 11588 | entry->u.index = -1; |
| 11589 | if (act & DO_ABS) { |
| 11590 | while (stat(name, &statb) < 0) { |
| 11591 | #ifdef SYSV |
| 11592 | if (errno == EINTR) |
| 11593 | continue; |
| 11594 | #endif |
| 11595 | entry->cmdtype = CMDUNKNOWN; |
| 11596 | return; |
| 11597 | } |
| 11598 | } |
| 11599 | entry->cmdtype = CMDNORMAL; |
| 11600 | return; |
| 11601 | } |
| 11602 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 11603 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11604 | |
| 11605 | updatetbl = (path == pathval()); |
| 11606 | if (!updatetbl) { |
| 11607 | act |= DO_ALTPATH; |
| 11608 | if (strstr(path, "%builtin") != NULL) |
| 11609 | act |= DO_ALTBLTIN; |
| 11610 | } |
| 11611 | |
| 11612 | /* If name is in the table, check answer will be ok */ |
| 11613 | cmdp = cmdlookup(name, 0); |
| 11614 | if (cmdp != NULL) { |
| 11615 | int bit; |
| 11616 | |
| 11617 | switch (cmdp->cmdtype) { |
| 11618 | default: |
| 11619 | #if DEBUG |
| 11620 | abort(); |
| 11621 | #endif |
| 11622 | case CMDNORMAL: |
| 11623 | bit = DO_ALTPATH; |
| 11624 | break; |
| 11625 | case CMDFUNCTION: |
| 11626 | bit = DO_NOFUNC; |
| 11627 | break; |
| 11628 | case CMDBUILTIN: |
| 11629 | bit = DO_ALTBLTIN; |
| 11630 | break; |
| 11631 | } |
| 11632 | if (act & bit) { |
| 11633 | updatetbl = 0; |
| 11634 | cmdp = NULL; |
| 11635 | } else if (cmdp->rehash == 0) |
| 11636 | /* if not invalidated by cd, we're done */ |
| 11637 | goto success; |
| 11638 | } |
| 11639 | |
| 11640 | /* If %builtin not in path, check for builtin next */ |
| 11641 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 11642 | if (bcmd) { |
| 11643 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 11644 | goto builtin_success; |
| 11645 | if (act & DO_ALTPATH) { |
| 11646 | if (!(act & DO_ALTBLTIN)) |
| 11647 | goto builtin_success; |
| 11648 | } else if (builtinloc <= 0) { |
| 11649 | goto builtin_success; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 11650 | } |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 11651 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11652 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 11653 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 11654 | { |
| 11655 | int applet_no = find_applet_by_name(name); |
| 11656 | if (applet_no >= 0) { |
| 11657 | entry->cmdtype = CMDNORMAL; |
| 11658 | entry->u.index = -2 - applet_no; |
| 11659 | return; |
| 11660 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 11661 | } |
| 11662 | #endif |
| 11663 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11664 | /* We have to search path. */ |
| 11665 | prev = -1; /* where to start */ |
| 11666 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 11667 | if (cmdp->cmdtype == CMDBUILTIN) |
| 11668 | prev = builtinloc; |
| 11669 | else |
| 11670 | prev = cmdp->param.index; |
| 11671 | } |
| 11672 | |
| 11673 | e = ENOENT; |
| 11674 | idx = -1; |
| 11675 | loop: |
| 11676 | while ((fullname = padvance(&path, name)) != NULL) { |
| 11677 | stunalloc(fullname); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 11678 | /* NB: code below will still use fullname |
| 11679 | * despite it being "unallocated" */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11680 | idx++; |
| 11681 | if (pathopt) { |
| 11682 | if (prefix(pathopt, "builtin")) { |
| 11683 | if (bcmd) |
| 11684 | goto builtin_success; |
| 11685 | continue; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 11686 | } |
| 11687 | if ((act & DO_NOFUNC) |
| 11688 | || !prefix(pathopt, "func") |
| 11689 | ) { /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11690 | continue; |
| 11691 | } |
| 11692 | } |
| 11693 | /* if rehash, don't redo absolute path names */ |
| 11694 | if (fullname[0] == '/' && idx <= prev) { |
| 11695 | if (idx < prev) |
| 11696 | continue; |
| 11697 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 11698 | goto success; |
| 11699 | } |
| 11700 | while (stat(fullname, &statb) < 0) { |
| 11701 | #ifdef SYSV |
| 11702 | if (errno == EINTR) |
| 11703 | continue; |
| 11704 | #endif |
| 11705 | if (errno != ENOENT && errno != ENOTDIR) |
| 11706 | e = errno; |
| 11707 | goto loop; |
| 11708 | } |
| 11709 | e = EACCES; /* if we fail, this will be the error */ |
| 11710 | if (!S_ISREG(statb.st_mode)) |
| 11711 | continue; |
| 11712 | if (pathopt) { /* this is a %func directory */ |
| 11713 | stalloc(strlen(fullname) + 1); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 11714 | /* NB: stalloc will return space pointed by fullname |
| 11715 | * (because we don't have any intervening allocations |
| 11716 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11717 | readcmdfile(fullname); |
| 11718 | cmdp = cmdlookup(name, 0); |
| 11719 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 11720 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 11721 | stunalloc(fullname); |
| 11722 | goto success; |
| 11723 | } |
| 11724 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 11725 | if (!updatetbl) { |
| 11726 | entry->cmdtype = CMDNORMAL; |
| 11727 | entry->u.index = idx; |
| 11728 | return; |
| 11729 | } |
| 11730 | INT_OFF; |
| 11731 | cmdp = cmdlookup(name, 1); |
| 11732 | cmdp->cmdtype = CMDNORMAL; |
| 11733 | cmdp->param.index = idx; |
| 11734 | INT_ON; |
| 11735 | goto success; |
| 11736 | } |
| 11737 | |
| 11738 | /* We failed. If there was an entry for this command, delete it */ |
| 11739 | if (cmdp && updatetbl) |
| 11740 | delete_cmd_entry(); |
| 11741 | if (act & DO_ERR) |
| 11742 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
| 11743 | entry->cmdtype = CMDUNKNOWN; |
| 11744 | return; |
| 11745 | |
| 11746 | builtin_success: |
| 11747 | if (!updatetbl) { |
| 11748 | entry->cmdtype = CMDBUILTIN; |
| 11749 | entry->u.cmd = bcmd; |
| 11750 | return; |
| 11751 | } |
| 11752 | INT_OFF; |
| 11753 | cmdp = cmdlookup(name, 1); |
| 11754 | cmdp->cmdtype = CMDBUILTIN; |
| 11755 | cmdp->param.cmd = bcmd; |
| 11756 | INT_ON; |
| 11757 | success: |
| 11758 | cmdp->rehash = 0; |
| 11759 | entry->cmdtype = cmdp->cmdtype; |
| 11760 | entry->u = cmdp->param; |
| 11761 | } |
| 11762 | |
| 11763 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11764 | /* ============ trap.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11765 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11766 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11767 | * The trap builtin. |
| 11768 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11769 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11770 | trapcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11771 | { |
| 11772 | char *action; |
| 11773 | char **ap; |
| 11774 | int signo; |
| 11775 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11776 | nextopt(nullstr); |
| 11777 | ap = argptr; |
| 11778 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11779 | for (signo = 0; signo < NSIG; signo++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11780 | if (trap[signo] != NULL) { |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11781 | const char *sn; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11782 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 11783 | sn = get_signame(signo); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11784 | out1fmt("trap -- %s %s\n", |
| 11785 | single_quote(trap[signo]), sn); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11786 | } |
| 11787 | } |
| 11788 | return 0; |
| 11789 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11790 | if (!ap[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11791 | action = NULL; |
| 11792 | else |
| 11793 | action = *ap++; |
| 11794 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11795 | signo = get_signum(*ap); |
| 11796 | if (signo < 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11797 | ash_msg_and_raise_error("%s: bad trap", *ap); |
| 11798 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11799 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 11800 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11801 | action = NULL; |
| 11802 | else |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11803 | action = ckstrdup(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11804 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 11805 | free(trap[signo]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11806 | trap[signo] = action; |
| 11807 | if (signo != 0) |
| 11808 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11809 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11810 | ap++; |
| 11811 | } |
| 11812 | return 0; |
| 11813 | } |
| 11814 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11815 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11816 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11817 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 11818 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 11819 | /* |
| 11820 | * Lists available builtins |
| 11821 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11822 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11823 | helpcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11824 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 11825 | unsigned col; |
| 11826 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11827 | |
| 11828 | out1fmt("\nBuilt-in commands:\n-------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 11829 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11830 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 11831 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11832 | if (col > 60) { |
| 11833 | out1fmt("\n"); |
| 11834 | col = 0; |
| 11835 | } |
| 11836 | } |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 11837 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 11838 | { |
| 11839 | const char *a = applet_names; |
| 11840 | while (*a) { |
| 11841 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 11842 | if (col > 60) { |
| 11843 | out1fmt("\n"); |
| 11844 | col = 0; |
| 11845 | } |
| 11846 | a += strlen(a) + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11847 | } |
| 11848 | } |
| 11849 | #endif |
| 11850 | out1fmt("\n\n"); |
| 11851 | return EXIT_SUCCESS; |
| 11852 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11853 | #endif /* FEATURE_SH_EXTRA_QUIET */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11854 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11855 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11856 | * The export and readonly commands. |
| 11857 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11858 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11859 | exportcmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11860 | { |
| 11861 | struct var *vp; |
| 11862 | char *name; |
| 11863 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11864 | char **aptr; |
| 11865 | int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11866 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11867 | if (nextopt("p") != 'p') { |
| 11868 | aptr = argptr; |
| 11869 | name = *aptr; |
| 11870 | if (name) { |
| 11871 | do { |
| 11872 | p = strchr(name, '='); |
| 11873 | if (p != NULL) { |
| 11874 | p++; |
| 11875 | } else { |
| 11876 | vp = *findvar(hashvar(name), name); |
| 11877 | if (vp) { |
| 11878 | vp->flags |= flag; |
| 11879 | continue; |
| 11880 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11881 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11882 | setvar(name, p, flag); |
| 11883 | } while ((name = *++aptr) != NULL); |
| 11884 | return 0; |
| 11885 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11886 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11887 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11888 | return 0; |
| 11889 | } |
| 11890 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11891 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 11892 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11893 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 11894 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 11895 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11896 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 11897 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11898 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 11899 | cmdp = cmdlookup(name, 0); |
| 11900 | if (cmdp!= NULL && cmdp->cmdtype == CMDFUNCTION) |
| 11901 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11902 | } |
| 11903 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11904 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11905 | * The unset builtin command. We unset the function before we unset the |
| 11906 | * variable to allow a function to be unset when there is a readonly variable |
| 11907 | * with the same name. |
| 11908 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11909 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11910 | unsetcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11911 | { |
| 11912 | char **ap; |
| 11913 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11914 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11915 | int ret = 0; |
| 11916 | |
| 11917 | while ((i = nextopt("vf")) != '\0') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11918 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11919 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11920 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11921 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11922 | if (flag != 'f') { |
| 11923 | i = unsetvar(*ap); |
| 11924 | ret |= i; |
| 11925 | if (!(i & 2)) |
| 11926 | continue; |
| 11927 | } |
| 11928 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11929 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11930 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11931 | return ret & 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11932 | } |
| 11933 | |
| 11934 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11935 | /* setmode.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11936 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11937 | #include <sys/times.h> |
| 11938 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11939 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 11940 | ' ', offsetof(struct tms, tms_utime), |
| 11941 | '\n', offsetof(struct tms, tms_stime), |
| 11942 | ' ', offsetof(struct tms, tms_cutime), |
| 11943 | '\n', offsetof(struct tms, tms_cstime), |
| 11944 | 0 |
| 11945 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11946 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 11947 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11948 | timescmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 11949 | { |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 11950 | long clk_tck, s, t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 11951 | const unsigned char *p; |
| 11952 | struct tms buf; |
| 11953 | |
| 11954 | clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11955 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 11956 | |
| 11957 | p = timescmd_str; |
| 11958 | do { |
| 11959 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 11960 | s = t / clk_tck; |
| 11961 | out1fmt("%ldm%ld.%.3lds%c", |
| 11962 | s/60, s%60, |
| 11963 | ((t - s * clk_tck) * 1000) / clk_tck, |
| 11964 | p[0]); |
| 11965 | } while (*(p += 2)); |
| 11966 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11967 | return 0; |
| 11968 | } |
| 11969 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11970 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 11971 | static arith_t |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11972 | dash_arith(const char *s) |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 11973 | { |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 11974 | arith_t result; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11975 | int errcode = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11976 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11977 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11978 | result = arith(s, &errcode); |
| 11979 | if (errcode < 0) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11980 | if (errcode == -3) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11981 | ash_msg_and_raise_error("exponent less than 0"); |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 11982 | if (errcode == -2) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11983 | ash_msg_and_raise_error("divide by zero"); |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 11984 | if (errcode == -5) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11985 | ash_msg_and_raise_error("expression recursion loop detected"); |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 11986 | raise_error_syntax(s); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11987 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11988 | INT_ON; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11989 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 11990 | return result; |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 11991 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11992 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11993 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11994 | * The let builtin. partial stolen from GNU Bash, the Bourne Again SHell. |
| 11995 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
| 11996 | * |
| 11997 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11998 | */ |
| 11999 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12000 | letcmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12001 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12002 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12003 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12004 | argv++; |
| 12005 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12006 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12007 | do { |
| 12008 | i = dash_arith(*argv); |
| 12009 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12010 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 12011 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12012 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12013 | #endif /* ASH_MATH_SUPPORT */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12014 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12015 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12016 | /* ============ miscbltin.c |
| 12017 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12018 | * Miscellaneous builtins. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12019 | */ |
| 12020 | |
| 12021 | #undef rflag |
| 12022 | |
Denis Vlasenko | 83e5d6f | 2006-12-18 21:49:06 +0000 | [diff] [blame] | 12023 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 1 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12024 | typedef enum __rlimit_resource rlim_t; |
| 12025 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12026 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12027 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12028 | * The read builtin. Options: |
| 12029 | * -r Do not interpret '\' specially |
| 12030 | * -s Turn off echo (tty only) |
| 12031 | * -n NCHARS Read NCHARS max |
| 12032 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 12033 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 12034 | * -u FD Read from given FD instead of fd 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12035 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12036 | * TODO: bash also has: |
| 12037 | * -a ARRAY Read into array[0],[1],etc |
| 12038 | * -d DELIM End on DELIM char, not newline |
| 12039 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12040 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12041 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12042 | readcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12043 | { |
| 12044 | char **ap; |
| 12045 | int backslash; |
| 12046 | char c; |
| 12047 | int rflag; |
| 12048 | char *prompt; |
| 12049 | const char *ifs; |
| 12050 | char *p; |
| 12051 | int startword; |
| 12052 | int status; |
| 12053 | int i; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12054 | int fd = 0; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12055 | #if ENABLE_ASH_READ_NCHARS |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12056 | int nchars = 0; /* if != 0, -n is in effect */ |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12057 | int silent = 0; |
| 12058 | struct termios tty, old_tty; |
| 12059 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12060 | #if ENABLE_ASH_READ_TIMEOUT |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12061 | unsigned end_ms = 0; |
| 12062 | unsigned timeout = 0; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12063 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12064 | |
| 12065 | rflag = 0; |
| 12066 | prompt = NULL; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12067 | while ((i = nextopt("p:u:r" |
| 12068 | USE_ASH_READ_TIMEOUT("t:") |
| 12069 | USE_ASH_READ_NCHARS("n:s") |
| 12070 | )) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 12071 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12072 | case 'p': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12073 | prompt = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12074 | break; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12075 | #if ENABLE_ASH_READ_NCHARS |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12076 | case 'n': |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12077 | nchars = bb_strtou(optionarg, NULL, 10); |
| 12078 | if (nchars < 0 || errno) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12079 | ash_msg_and_raise_error("invalid count"); |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12080 | /* nchars == 0: off (bash 3.2 does this too) */ |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12081 | break; |
| 12082 | case 's': |
| 12083 | silent = 1; |
| 12084 | break; |
Ned Ludd | 2123b7c | 2005-02-09 21:07:23 +0000 | [diff] [blame] | 12085 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12086 | #if ENABLE_ASH_READ_TIMEOUT |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12087 | case 't': |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12088 | timeout = bb_strtou(optionarg, NULL, 10); |
| 12089 | if (errno || timeout > UINT_MAX / 2048) |
| 12090 | ash_msg_and_raise_error("invalid timeout"); |
| 12091 | timeout *= 1000; |
| 12092 | #if 0 /* even bash have no -t N.NNN support */ |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12093 | ts.tv_sec = bb_strtou(optionarg, &p, 10); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12094 | ts.tv_usec = 0; |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12095 | /* EINVAL means number is ok, but not terminated by NUL */ |
| 12096 | if (*p == '.' && errno == EINVAL) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12097 | char *p2; |
| 12098 | if (*++p) { |
| 12099 | int scale; |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12100 | ts.tv_usec = bb_strtou(p, &p2, 10); |
| 12101 | if (errno) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12102 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12103 | scale = p2 - p; |
| 12104 | /* normalize to usec */ |
| 12105 | if (scale > 6) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12106 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12107 | while (scale++ < 6) |
| 12108 | ts.tv_usec *= 10; |
| 12109 | } |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12110 | } else if (ts.tv_sec < 0 || errno) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12111 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12112 | } |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12113 | if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12114 | ash_msg_and_raise_error("invalid timeout"); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12115 | } |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12116 | #endif /* if 0 */ |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12117 | break; |
| 12118 | #endif |
| 12119 | case 'r': |
| 12120 | rflag = 1; |
| 12121 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12122 | case 'u': |
| 12123 | fd = bb_strtou(optionarg, NULL, 10); |
| 12124 | if (fd < 0 || errno) |
| 12125 | ash_msg_and_raise_error("invalid file descriptor"); |
| 12126 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12127 | default: |
| 12128 | break; |
| 12129 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12130 | } |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12131 | if (prompt && isatty(fd)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12132 | out2str(prompt); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12133 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12134 | ap = argptr; |
| 12135 | if (*ap == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12136 | ash_msg_and_raise_error("arg count"); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12137 | ifs = bltinlookup("IFS"); |
| 12138 | if (ifs == NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12139 | ifs = defifs; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12140 | #if ENABLE_ASH_READ_NCHARS |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12141 | tcgetattr(fd, &tty); |
| 12142 | old_tty = tty; |
| 12143 | if (nchars || silent) { |
| 12144 | if (nchars) { |
| 12145 | tty.c_lflag &= ~ICANON; |
| 12146 | tty.c_cc[VMIN] = nchars < 256 ? nchars : 255; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12147 | } |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12148 | if (silent) { |
| 12149 | tty.c_lflag &= ~(ECHO | ECHOK | ECHONL); |
| 12150 | } |
| 12151 | /* if tcgetattr failed, tcsetattr will fail too. |
| 12152 | * Ignoring, it's harmless. */ |
| 12153 | tcsetattr(fd, TCSANOW, &tty); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12154 | } |
| 12155 | #endif |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12156 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12157 | status = 0; |
| 12158 | startword = 1; |
| 12159 | backslash = 0; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12160 | #if ENABLE_ASH_READ_TIMEOUT |
| 12161 | if (timeout) /* NB: ensuring end_ms is nonzero */ |
| 12162 | end_ms = ((unsigned)(monotonic_us() / 1000) + timeout) | 1; |
| 12163 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12164 | STARTSTACKSTR(p); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12165 | do { |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12166 | #if ENABLE_ASH_READ_TIMEOUT |
| 12167 | if (end_ms) { |
| 12168 | struct pollfd pfd[1]; |
| 12169 | pfd[0].fd = fd; |
| 12170 | pfd[0].events = POLLIN; |
| 12171 | timeout = end_ms - (unsigned)(monotonic_us() / 1000); |
| 12172 | if ((int)timeout <= 0 /* already late? */ |
| 12173 | || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */ |
| 12174 | ) { /* timed out! */ |
| 12175 | #if ENABLE_ASH_READ_NCHARS |
| 12176 | tcsetattr(fd, TCSANOW, &old_tty); |
| 12177 | #endif |
| 12178 | return 1; |
| 12179 | } |
| 12180 | } |
| 12181 | #endif |
| 12182 | if (nonblock_safe_read(fd, &c, 1) != 1) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12183 | status = 1; |
| 12184 | break; |
| 12185 | } |
| 12186 | if (c == '\0') |
| 12187 | continue; |
| 12188 | if (backslash) { |
| 12189 | backslash = 0; |
| 12190 | if (c != '\n') |
| 12191 | goto put; |
| 12192 | continue; |
| 12193 | } |
| 12194 | if (!rflag && c == '\\') { |
| 12195 | backslash++; |
| 12196 | continue; |
| 12197 | } |
| 12198 | if (c == '\n') |
| 12199 | break; |
| 12200 | if (startword && *ifs == ' ' && strchr(ifs, c)) { |
| 12201 | continue; |
| 12202 | } |
| 12203 | startword = 0; |
| 12204 | if (ap[1] != NULL && strchr(ifs, c) != NULL) { |
| 12205 | STACKSTRNUL(p); |
| 12206 | setvar(*ap, stackblock(), 0); |
| 12207 | ap++; |
| 12208 | startword = 1; |
| 12209 | STARTSTACKSTR(p); |
| 12210 | } else { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12211 | put: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12212 | STPUTC(c, p); |
| 12213 | } |
| 12214 | } |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12215 | /* end of do {} while: */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12216 | #if ENABLE_ASH_READ_NCHARS |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12217 | while (--nchars); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12218 | #else |
| 12219 | while (1); |
| 12220 | #endif |
| 12221 | |
| 12222 | #if ENABLE_ASH_READ_NCHARS |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12223 | tcsetattr(fd, TCSANOW, &old_tty); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12224 | #endif |
| 12225 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12226 | STACKSTRNUL(p); |
| 12227 | /* Remove trailing blanks */ |
| 12228 | while ((char *)stackblock() <= --p && strchr(ifs, *p) != NULL) |
| 12229 | *p = '\0'; |
| 12230 | setvar(*ap, stackblock(), 0); |
| 12231 | while (*++ap != NULL) |
| 12232 | setvar(*ap, nullstr, 0); |
| 12233 | return status; |
| 12234 | } |
| 12235 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12236 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12237 | umaskcmd(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12238 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12239 | static const char permuser[3] ALIGN1 = "ugo"; |
| 12240 | static const char permmode[3] ALIGN1 = "rwx"; |
| 12241 | static const short permmask[] ALIGN2 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12242 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 12243 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 12244 | S_IROTH, S_IWOTH, S_IXOTH |
| 12245 | }; |
| 12246 | |
| 12247 | char *ap; |
| 12248 | mode_t mask; |
| 12249 | int i; |
| 12250 | int symbolic_mode = 0; |
| 12251 | |
| 12252 | while (nextopt("S") != '\0') { |
| 12253 | symbolic_mode = 1; |
| 12254 | } |
| 12255 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12256 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12257 | mask = umask(0); |
| 12258 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12259 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12260 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12261 | ap = *argptr; |
| 12262 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12263 | if (symbolic_mode) { |
| 12264 | char buf[18]; |
| 12265 | char *p = buf; |
| 12266 | |
| 12267 | for (i = 0; i < 3; i++) { |
| 12268 | int j; |
| 12269 | |
| 12270 | *p++ = permuser[i]; |
| 12271 | *p++ = '='; |
| 12272 | for (j = 0; j < 3; j++) { |
| 12273 | if ((mask & permmask[3 * i + j]) == 0) { |
| 12274 | *p++ = permmode[j]; |
| 12275 | } |
| 12276 | } |
| 12277 | *p++ = ','; |
| 12278 | } |
| 12279 | *--p = 0; |
| 12280 | puts(buf); |
| 12281 | } else { |
| 12282 | out1fmt("%.4o\n", mask); |
| 12283 | } |
| 12284 | } else { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12285 | if (isdigit((unsigned char) *ap)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12286 | mask = 0; |
| 12287 | do { |
| 12288 | if (*ap >= '8' || *ap < '0') |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12289 | ash_msg_and_raise_error(illnum, argv[1]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12290 | mask = (mask << 3) + (*ap - '0'); |
| 12291 | } while (*++ap != '\0'); |
| 12292 | umask(mask); |
| 12293 | } else { |
| 12294 | mask = ~mask & 0777; |
| 12295 | if (!bb_parse_mode(ap, &mask)) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 12296 | ash_msg_and_raise_error("illegal mode: %s", ap); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12297 | } |
| 12298 | umask(~mask & 0777); |
| 12299 | } |
| 12300 | } |
| 12301 | return 0; |
| 12302 | } |
| 12303 | |
| 12304 | /* |
| 12305 | * ulimit builtin |
| 12306 | * |
| 12307 | * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and |
| 12308 | * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with |
| 12309 | * ash by J.T. Conklin. |
| 12310 | * |
| 12311 | * Public domain. |
| 12312 | */ |
| 12313 | |
| 12314 | struct limits { |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12315 | uint8_t cmd; /* RLIMIT_xxx fit into it */ |
| 12316 | uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12317 | char option; |
| 12318 | }; |
| 12319 | |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12320 | static const struct limits limits_tbl[] = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12321 | #ifdef RLIMIT_CPU |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12322 | { RLIMIT_CPU, 0, 't' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12323 | #endif |
| 12324 | #ifdef RLIMIT_FSIZE |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12325 | { RLIMIT_FSIZE, 9, 'f' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12326 | #endif |
| 12327 | #ifdef RLIMIT_DATA |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12328 | { RLIMIT_DATA, 10, 'd' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12329 | #endif |
| 12330 | #ifdef RLIMIT_STACK |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12331 | { RLIMIT_STACK, 10, 's' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12332 | #endif |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12333 | #ifdef RLIMIT_CORE |
| 12334 | { RLIMIT_CORE, 9, 'c' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12335 | #endif |
| 12336 | #ifdef RLIMIT_RSS |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12337 | { RLIMIT_RSS, 10, 'm' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12338 | #endif |
| 12339 | #ifdef RLIMIT_MEMLOCK |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12340 | { RLIMIT_MEMLOCK, 10, 'l' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12341 | #endif |
| 12342 | #ifdef RLIMIT_NPROC |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12343 | { RLIMIT_NPROC, 0, 'p' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12344 | #endif |
| 12345 | #ifdef RLIMIT_NOFILE |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12346 | { RLIMIT_NOFILE, 0, 'n' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12347 | #endif |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12348 | #ifdef RLIMIT_AS |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12349 | { RLIMIT_AS, 10, 'v' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12350 | #endif |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12351 | #ifdef RLIMIT_LOCKS |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12352 | { RLIMIT_LOCKS, 0, 'w' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12353 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12354 | }; |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12355 | static const char limits_name[] = |
| 12356 | #ifdef RLIMIT_CPU |
| 12357 | "time(seconds)" "\0" |
| 12358 | #endif |
| 12359 | #ifdef RLIMIT_FSIZE |
| 12360 | "file(blocks)" "\0" |
| 12361 | #endif |
| 12362 | #ifdef RLIMIT_DATA |
| 12363 | "data(kb)" "\0" |
| 12364 | #endif |
| 12365 | #ifdef RLIMIT_STACK |
| 12366 | "stack(kb)" "\0" |
| 12367 | #endif |
| 12368 | #ifdef RLIMIT_CORE |
| 12369 | "coredump(blocks)" "\0" |
| 12370 | #endif |
| 12371 | #ifdef RLIMIT_RSS |
| 12372 | "memory(kb)" "\0" |
| 12373 | #endif |
| 12374 | #ifdef RLIMIT_MEMLOCK |
| 12375 | "locked memory(kb)" "\0" |
| 12376 | #endif |
| 12377 | #ifdef RLIMIT_NPROC |
| 12378 | "process" "\0" |
| 12379 | #endif |
| 12380 | #ifdef RLIMIT_NOFILE |
| 12381 | "nofiles" "\0" |
| 12382 | #endif |
| 12383 | #ifdef RLIMIT_AS |
| 12384 | "vmemory(kb)" "\0" |
| 12385 | #endif |
| 12386 | #ifdef RLIMIT_LOCKS |
| 12387 | "locks" "\0" |
| 12388 | #endif |
| 12389 | ; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12390 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12391 | enum limtype { SOFT = 0x1, HARD = 0x2 }; |
| 12392 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12393 | static void |
| 12394 | printlim(enum limtype how, const struct rlimit *limit, |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12395 | const struct limits *l) |
| 12396 | { |
| 12397 | rlim_t val; |
| 12398 | |
| 12399 | val = limit->rlim_max; |
| 12400 | if (how & SOFT) |
| 12401 | val = limit->rlim_cur; |
| 12402 | |
| 12403 | if (val == RLIM_INFINITY) |
| 12404 | out1fmt("unlimited\n"); |
| 12405 | else { |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12406 | val >>= l->factor_shift; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12407 | out1fmt("%lld\n", (long long) val); |
| 12408 | } |
| 12409 | } |
| 12410 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12411 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12412 | ulimitcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12413 | { |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12414 | int c; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12415 | rlim_t val = 0; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12416 | enum limtype how = SOFT | HARD; |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12417 | const struct limits *l; |
| 12418 | int set, all = 0; |
| 12419 | int optc, what; |
| 12420 | struct rlimit limit; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12421 | |
| 12422 | what = 'f'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 12423 | while ((optc = nextopt("HSa" |
| 12424 | #ifdef RLIMIT_CPU |
| 12425 | "t" |
| 12426 | #endif |
| 12427 | #ifdef RLIMIT_FSIZE |
| 12428 | "f" |
| 12429 | #endif |
| 12430 | #ifdef RLIMIT_DATA |
| 12431 | "d" |
| 12432 | #endif |
| 12433 | #ifdef RLIMIT_STACK |
| 12434 | "s" |
| 12435 | #endif |
| 12436 | #ifdef RLIMIT_CORE |
| 12437 | "c" |
| 12438 | #endif |
| 12439 | #ifdef RLIMIT_RSS |
| 12440 | "m" |
| 12441 | #endif |
| 12442 | #ifdef RLIMIT_MEMLOCK |
| 12443 | "l" |
| 12444 | #endif |
| 12445 | #ifdef RLIMIT_NPROC |
| 12446 | "p" |
| 12447 | #endif |
| 12448 | #ifdef RLIMIT_NOFILE |
| 12449 | "n" |
| 12450 | #endif |
| 12451 | #ifdef RLIMIT_AS |
| 12452 | "v" |
| 12453 | #endif |
| 12454 | #ifdef RLIMIT_LOCKS |
| 12455 | "w" |
| 12456 | #endif |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12457 | )) != '\0') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12458 | switch (optc) { |
| 12459 | case 'H': |
| 12460 | how = HARD; |
| 12461 | break; |
| 12462 | case 'S': |
| 12463 | how = SOFT; |
| 12464 | break; |
| 12465 | case 'a': |
| 12466 | all = 1; |
| 12467 | break; |
| 12468 | default: |
| 12469 | what = optc; |
| 12470 | } |
| 12471 | |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12472 | for (l = limits_tbl; l->option != what; l++) |
| 12473 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12474 | |
| 12475 | set = *argptr ? 1 : 0; |
| 12476 | if (set) { |
| 12477 | char *p = *argptr; |
| 12478 | |
| 12479 | if (all || argptr[1]) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12480 | ash_msg_and_raise_error("too many arguments"); |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 12481 | if (strncmp(p, "unlimited\n", 9) == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12482 | val = RLIM_INFINITY; |
| 12483 | else { |
| 12484 | val = (rlim_t) 0; |
| 12485 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12486 | while ((c = *p++) >= '0' && c <= '9') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12487 | val = (val * 10) + (long)(c - '0'); |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 12488 | // val is actually 'unsigned long int' and can't get < 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12489 | if (val < (rlim_t) 0) |
| 12490 | break; |
| 12491 | } |
| 12492 | if (c) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12493 | ash_msg_and_raise_error("bad number"); |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12494 | val <<= l->factor_shift; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12495 | } |
| 12496 | } |
| 12497 | if (all) { |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12498 | const char *lname = limits_name; |
| 12499 | for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12500 | getrlimit(l->cmd, &limit); |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12501 | out1fmt("%-20s ", lname); |
| 12502 | lname += strlen(lname) + 1; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12503 | printlim(how, &limit, l); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12504 | } |
| 12505 | return 0; |
| 12506 | } |
| 12507 | |
| 12508 | getrlimit(l->cmd, &limit); |
| 12509 | if (set) { |
| 12510 | if (how & HARD) |
| 12511 | limit.rlim_max = val; |
| 12512 | if (how & SOFT) |
| 12513 | limit.rlim_cur = val; |
| 12514 | if (setrlimit(l->cmd, &limit) < 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12515 | ash_msg_and_raise_error("error setting limit (%m)"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12516 | } else { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12517 | printlim(how, &limit, l); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12518 | } |
| 12519 | return 0; |
| 12520 | } |
| 12521 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12522 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12523 | /* ============ Math support */ |
| 12524 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12525 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12526 | |
| 12527 | /* Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com> |
| 12528 | |
| 12529 | Permission is hereby granted, free of charge, to any person obtaining |
| 12530 | a copy of this software and associated documentation files (the |
| 12531 | "Software"), to deal in the Software without restriction, including |
| 12532 | without limitation the rights to use, copy, modify, merge, publish, |
| 12533 | distribute, sublicense, and/or sell copies of the Software, and to |
| 12534 | permit persons to whom the Software is furnished to do so, subject to |
| 12535 | the following conditions: |
| 12536 | |
| 12537 | The above copyright notice and this permission notice shall be |
| 12538 | included in all copies or substantial portions of the Software. |
| 12539 | |
| 12540 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 12541 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 12542 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 12543 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 12544 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 12545 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 12546 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 12547 | */ |
| 12548 | |
| 12549 | /* This is my infix parser/evaluator. It is optimized for size, intended |
| 12550 | * as a replacement for yacc-based parsers. However, it may well be faster |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12551 | * than a comparable parser written in yacc. The supported operators are |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12552 | * listed in #defines below. Parens, order of operations, and error handling |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12553 | * are supported. This code is thread safe. The exact expression format should |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12554 | * be that which POSIX specifies for shells. */ |
| 12555 | |
| 12556 | /* The code uses a simple two-stack algorithm. See |
| 12557 | * http://www.onthenet.com.au/~grahamis/int2008/week02/lect02.html |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12558 | * for a detailed explanation of the infix-to-postfix algorithm on which |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12559 | * this is based (this code differs in that it applies operators immediately |
| 12560 | * to the stack instead of adding them to a queue to end up with an |
| 12561 | * expression). */ |
| 12562 | |
| 12563 | /* To use the routine, call it with an expression string and error return |
| 12564 | * pointer */ |
| 12565 | |
| 12566 | /* |
| 12567 | * Aug 24, 2001 Manuel Novoa III |
| 12568 | * |
| 12569 | * Reduced the generated code size by about 30% (i386) and fixed several bugs. |
| 12570 | * |
| 12571 | * 1) In arith_apply(): |
| 12572 | * a) Cached values of *numptr and &(numptr[-1]). |
| 12573 | * b) Removed redundant test for zero denominator. |
| 12574 | * |
| 12575 | * 2) In arith(): |
| 12576 | * a) Eliminated redundant code for processing operator tokens by moving |
| 12577 | * to a table-based implementation. Also folded handling of parens |
| 12578 | * into the table. |
| 12579 | * b) Combined all 3 loops which called arith_apply to reduce generated |
| 12580 | * code size at the cost of speed. |
| 12581 | * |
| 12582 | * 3) The following expressions were treated as valid by the original code: |
| 12583 | * 1() , 0! , 1 ( *3 ) . |
| 12584 | * These bugs have been fixed by internally enclosing the expression in |
| 12585 | * parens and then checking that all binary ops and right parens are |
| 12586 | * preceded by a valid expression (NUM_TOKEN). |
| 12587 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12588 | * Note: It may be desirable to replace Aaron's test for whitespace with |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12589 | * ctype's isspace() if it is used by another busybox applet or if additional |
| 12590 | * whitespace chars should be considered. Look below the "#include"s for a |
| 12591 | * precompiler test. |
| 12592 | */ |
| 12593 | |
| 12594 | /* |
| 12595 | * Aug 26, 2001 Manuel Novoa III |
| 12596 | * |
| 12597 | * Return 0 for null expressions. Pointed out by Vladimir Oleynik. |
| 12598 | * |
| 12599 | * Merge in Aaron's comments previously posted to the busybox list, |
| 12600 | * modified slightly to take account of my changes to the code. |
| 12601 | * |
| 12602 | */ |
| 12603 | |
| 12604 | /* |
| 12605 | * (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
| 12606 | * |
| 12607 | * - allow access to variable, |
| 12608 | * used recursive find value indirection (c=2*2; a="c"; $((a+=2)) produce 6) |
| 12609 | * - realize assign syntax (VAR=expr, +=, *= etc) |
| 12610 | * - realize exponentiation (** operator) |
| 12611 | * - realize comma separated - expr, expr |
| 12612 | * - realise ++expr --expr expr++ expr-- |
| 12613 | * - realise expr ? expr : expr (but, second expr calculate always) |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12614 | * - allow hexadecimal and octal numbers |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12615 | * - was restored loses XOR operator |
| 12616 | * - remove one goto label, added three ;-) |
| 12617 | * - protect $((num num)) as true zero expr (Manuel`s error) |
| 12618 | * - always use special isspace(), see comment from bash ;-) |
| 12619 | */ |
| 12620 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12621 | #define arith_isspace(arithval) \ |
| 12622 | (arithval == ' ' || arithval == '\n' || arithval == '\t') |
| 12623 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12624 | typedef unsigned char operator; |
| 12625 | |
| 12626 | /* An operator's token id is a bit of a bitfield. The lower 5 bits are the |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12627 | * precedence, and 3 high bits are an ID unique across operators of that |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12628 | * precedence. The ID portion is so that multiple operators can have the |
| 12629 | * same precedence, ensuring that the leftmost one is evaluated first. |
| 12630 | * Consider * and /. */ |
| 12631 | |
| 12632 | #define tok_decl(prec,id) (((id)<<5)|(prec)) |
| 12633 | #define PREC(op) ((op) & 0x1F) |
| 12634 | |
| 12635 | #define TOK_LPAREN tok_decl(0,0) |
| 12636 | |
| 12637 | #define TOK_COMMA tok_decl(1,0) |
| 12638 | |
| 12639 | #define TOK_ASSIGN tok_decl(2,0) |
| 12640 | #define TOK_AND_ASSIGN tok_decl(2,1) |
| 12641 | #define TOK_OR_ASSIGN tok_decl(2,2) |
| 12642 | #define TOK_XOR_ASSIGN tok_decl(2,3) |
| 12643 | #define TOK_PLUS_ASSIGN tok_decl(2,4) |
| 12644 | #define TOK_MINUS_ASSIGN tok_decl(2,5) |
| 12645 | #define TOK_LSHIFT_ASSIGN tok_decl(2,6) |
| 12646 | #define TOK_RSHIFT_ASSIGN tok_decl(2,7) |
| 12647 | |
| 12648 | #define TOK_MUL_ASSIGN tok_decl(3,0) |
| 12649 | #define TOK_DIV_ASSIGN tok_decl(3,1) |
| 12650 | #define TOK_REM_ASSIGN tok_decl(3,2) |
| 12651 | |
| 12652 | /* all assign is right associativity and precedence eq, but (7+3)<<5 > 256 */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12653 | #define convert_prec_is_assing(prec) do { if (prec == 3) prec = 2; } while (0) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12654 | |
| 12655 | /* conditional is right associativity too */ |
| 12656 | #define TOK_CONDITIONAL tok_decl(4,0) |
| 12657 | #define TOK_CONDITIONAL_SEP tok_decl(4,1) |
| 12658 | |
| 12659 | #define TOK_OR tok_decl(5,0) |
| 12660 | |
| 12661 | #define TOK_AND tok_decl(6,0) |
| 12662 | |
| 12663 | #define TOK_BOR tok_decl(7,0) |
| 12664 | |
| 12665 | #define TOK_BXOR tok_decl(8,0) |
| 12666 | |
| 12667 | #define TOK_BAND tok_decl(9,0) |
| 12668 | |
| 12669 | #define TOK_EQ tok_decl(10,0) |
| 12670 | #define TOK_NE tok_decl(10,1) |
| 12671 | |
| 12672 | #define TOK_LT tok_decl(11,0) |
| 12673 | #define TOK_GT tok_decl(11,1) |
| 12674 | #define TOK_GE tok_decl(11,2) |
| 12675 | #define TOK_LE tok_decl(11,3) |
| 12676 | |
| 12677 | #define TOK_LSHIFT tok_decl(12,0) |
| 12678 | #define TOK_RSHIFT tok_decl(12,1) |
| 12679 | |
| 12680 | #define TOK_ADD tok_decl(13,0) |
| 12681 | #define TOK_SUB tok_decl(13,1) |
| 12682 | |
| 12683 | #define TOK_MUL tok_decl(14,0) |
| 12684 | #define TOK_DIV tok_decl(14,1) |
| 12685 | #define TOK_REM tok_decl(14,2) |
| 12686 | |
| 12687 | /* exponent is right associativity */ |
| 12688 | #define TOK_EXPONENT tok_decl(15,1) |
| 12689 | |
| 12690 | /* For now unary operators. */ |
| 12691 | #define UNARYPREC 16 |
| 12692 | #define TOK_BNOT tok_decl(UNARYPREC,0) |
| 12693 | #define TOK_NOT tok_decl(UNARYPREC,1) |
| 12694 | |
| 12695 | #define TOK_UMINUS tok_decl(UNARYPREC+1,0) |
| 12696 | #define TOK_UPLUS tok_decl(UNARYPREC+1,1) |
| 12697 | |
| 12698 | #define PREC_PRE (UNARYPREC+2) |
| 12699 | |
| 12700 | #define TOK_PRE_INC tok_decl(PREC_PRE, 0) |
| 12701 | #define TOK_PRE_DEC tok_decl(PREC_PRE, 1) |
| 12702 | |
| 12703 | #define PREC_POST (UNARYPREC+3) |
| 12704 | |
| 12705 | #define TOK_POST_INC tok_decl(PREC_POST, 0) |
| 12706 | #define TOK_POST_DEC tok_decl(PREC_POST, 1) |
| 12707 | |
| 12708 | #define SPEC_PREC (UNARYPREC+4) |
| 12709 | |
| 12710 | #define TOK_NUM tok_decl(SPEC_PREC, 0) |
| 12711 | #define TOK_RPAREN tok_decl(SPEC_PREC, 1) |
| 12712 | |
| 12713 | #define NUMPTR (*numstackptr) |
| 12714 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12715 | static int |
| 12716 | tok_have_assign(operator op) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12717 | { |
| 12718 | operator prec = PREC(op); |
| 12719 | |
| 12720 | convert_prec_is_assing(prec); |
| 12721 | return (prec == PREC(TOK_ASSIGN) || |
| 12722 | prec == PREC_PRE || prec == PREC_POST); |
| 12723 | } |
| 12724 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12725 | static int |
| 12726 | is_right_associativity(operator prec) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12727 | { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12728 | return (prec == PREC(TOK_ASSIGN) || prec == PREC(TOK_EXPONENT) |
| 12729 | || prec == PREC(TOK_CONDITIONAL)); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12730 | } |
| 12731 | |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 12732 | typedef struct { |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 12733 | arith_t val; |
| 12734 | arith_t contidional_second_val; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12735 | char contidional_second_val_initialized; |
| 12736 | char *var; /* if NULL then is regular number, |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12737 | else is variable name */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12738 | } v_n_t; |
| 12739 | |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 12740 | typedef struct chk_var_recursive_looped_t { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12741 | const char *var; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 12742 | struct chk_var_recursive_looped_t *next; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12743 | } chk_var_recursive_looped_t; |
| 12744 | |
| 12745 | static chk_var_recursive_looped_t *prev_chk_var_recursive; |
| 12746 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12747 | static int |
| 12748 | arith_lookup_val(v_n_t *t) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12749 | { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12750 | if (t->var) { |
| 12751 | const char * p = lookupvar(t->var); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12752 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12753 | if (p) { |
| 12754 | int errcode; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12755 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12756 | /* recursive try as expression */ |
| 12757 | chk_var_recursive_looped_t *cur; |
| 12758 | chk_var_recursive_looped_t cur_save; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12759 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12760 | for (cur = prev_chk_var_recursive; cur; cur = cur->next) { |
| 12761 | if (strcmp(cur->var, t->var) == 0) { |
| 12762 | /* expression recursion loop detected */ |
| 12763 | return -5; |
| 12764 | } |
| 12765 | } |
| 12766 | /* save current lookuped var name */ |
| 12767 | cur = prev_chk_var_recursive; |
| 12768 | cur_save.var = t->var; |
| 12769 | cur_save.next = cur; |
| 12770 | prev_chk_var_recursive = &cur_save; |
| 12771 | |
| 12772 | t->val = arith (p, &errcode); |
| 12773 | /* restore previous ptr after recursiving */ |
| 12774 | prev_chk_var_recursive = cur; |
| 12775 | return errcode; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12776 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12777 | /* allow undefined var as 0 */ |
| 12778 | t->val = 0; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12779 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12780 | return 0; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12781 | } |
| 12782 | |
| 12783 | /* "applying" a token means performing it on the top elements on the integer |
| 12784 | * stack. For a unary operator it will only change the top element, but a |
| 12785 | * binary operator will pop two arguments and push a result */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12786 | static int |
| 12787 | arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12788 | { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12789 | v_n_t *numptr_m1; |
Eric Andersen | fac312d | 2004-06-22 20:09:40 +0000 | [diff] [blame] | 12790 | arith_t numptr_val, rez; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12791 | int ret_arith_lookup_val; |
| 12792 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12793 | /* There is no operator that can work without arguments */ |
| 12794 | if (NUMPTR == numstack) goto err; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12795 | numptr_m1 = NUMPTR - 1; |
| 12796 | |
| 12797 | /* check operand is var with noninteger value */ |
| 12798 | ret_arith_lookup_val = arith_lookup_val(numptr_m1); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12799 | if (ret_arith_lookup_val) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12800 | return ret_arith_lookup_val; |
| 12801 | |
| 12802 | rez = numptr_m1->val; |
| 12803 | if (op == TOK_UMINUS) |
| 12804 | rez *= -1; |
| 12805 | else if (op == TOK_NOT) |
| 12806 | rez = !rez; |
| 12807 | else if (op == TOK_BNOT) |
| 12808 | rez = ~rez; |
| 12809 | else if (op == TOK_POST_INC || op == TOK_PRE_INC) |
| 12810 | rez++; |
| 12811 | else if (op == TOK_POST_DEC || op == TOK_PRE_DEC) |
| 12812 | rez--; |
| 12813 | else if (op != TOK_UPLUS) { |
| 12814 | /* Binary operators */ |
| 12815 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12816 | /* check and binary operators need two arguments */ |
| 12817 | if (numptr_m1 == numstack) goto err; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12818 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12819 | /* ... and they pop one */ |
| 12820 | --NUMPTR; |
| 12821 | numptr_val = rez; |
| 12822 | if (op == TOK_CONDITIONAL) { |
| 12823 | if (! numptr_m1->contidional_second_val_initialized) { |
| 12824 | /* protect $((expr1 ? expr2)) without ": expr" */ |
| 12825 | goto err; |
| 12826 | } |
| 12827 | rez = numptr_m1->contidional_second_val; |
| 12828 | } else if (numptr_m1->contidional_second_val_initialized) { |
| 12829 | /* protect $((expr1 : expr2)) without "expr ? " */ |
| 12830 | goto err; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12831 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12832 | numptr_m1 = NUMPTR - 1; |
| 12833 | if (op != TOK_ASSIGN) { |
| 12834 | /* check operand is var with noninteger value for not '=' */ |
| 12835 | ret_arith_lookup_val = arith_lookup_val(numptr_m1); |
| 12836 | if (ret_arith_lookup_val) |
| 12837 | return ret_arith_lookup_val; |
| 12838 | } |
| 12839 | if (op == TOK_CONDITIONAL) { |
| 12840 | numptr_m1->contidional_second_val = rez; |
| 12841 | } |
| 12842 | rez = numptr_m1->val; |
| 12843 | if (op == TOK_BOR || op == TOK_OR_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12844 | rez |= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12845 | else if (op == TOK_OR) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12846 | rez = numptr_val || rez; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12847 | else if (op == TOK_BAND || op == TOK_AND_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12848 | rez &= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12849 | else if (op == TOK_BXOR || op == TOK_XOR_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12850 | rez ^= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12851 | else if (op == TOK_AND) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12852 | rez = rez && numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12853 | else if (op == TOK_EQ) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12854 | rez = (rez == numptr_val); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12855 | else if (op == TOK_NE) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12856 | rez = (rez != numptr_val); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12857 | else if (op == TOK_GE) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12858 | rez = (rez >= numptr_val); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12859 | else if (op == TOK_RSHIFT || op == TOK_RSHIFT_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12860 | rez >>= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12861 | else if (op == TOK_LSHIFT || op == TOK_LSHIFT_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12862 | rez <<= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12863 | else if (op == TOK_GT) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12864 | rez = (rez > numptr_val); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12865 | else if (op == TOK_LT) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12866 | rez = (rez < numptr_val); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12867 | else if (op == TOK_LE) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12868 | rez = (rez <= numptr_val); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12869 | else if (op == TOK_MUL || op == TOK_MUL_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12870 | rez *= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12871 | else if (op == TOK_ADD || op == TOK_PLUS_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12872 | rez += numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12873 | else if (op == TOK_SUB || op == TOK_MINUS_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12874 | rez -= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12875 | else if (op == TOK_ASSIGN || op == TOK_COMMA) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12876 | rez = numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12877 | else if (op == TOK_CONDITIONAL_SEP) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12878 | if (numptr_m1 == numstack) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12879 | /* protect $((expr : expr)) without "expr ? " */ |
| 12880 | goto err; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12881 | } |
| 12882 | numptr_m1->contidional_second_val_initialized = op; |
| 12883 | numptr_m1->contidional_second_val = numptr_val; |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12884 | } else if (op == TOK_CONDITIONAL) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12885 | rez = rez ? |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12886 | numptr_val : numptr_m1->contidional_second_val; |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12887 | } else if (op == TOK_EXPONENT) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12888 | if (numptr_val < 0) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12889 | return -3; /* exponent less than 0 */ |
| 12890 | else { |
Eric Andersen | ad63cb2 | 2004-10-08 09:43:34 +0000 | [diff] [blame] | 12891 | arith_t c = 1; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12892 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12893 | if (numptr_val) |
| 12894 | while (numptr_val--) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12895 | c *= rez; |
| 12896 | rez = c; |
| 12897 | } |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12898 | } else if (numptr_val==0) /* zero divisor check */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12899 | return -2; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12900 | else if (op == TOK_DIV || op == TOK_DIV_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12901 | rez /= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12902 | else if (op == TOK_REM || op == TOK_REM_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12903 | rez %= numptr_val; |
| 12904 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12905 | if (tok_have_assign(op)) { |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12906 | char buf[sizeof(arith_t_type)*3 + 2]; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12907 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12908 | if (numptr_m1->var == NULL) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12909 | /* Hmm, 1=2 ? */ |
| 12910 | goto err; |
| 12911 | } |
| 12912 | /* save to shell variable */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12913 | #if ENABLE_ASH_MATH_SUPPORT_64 |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12914 | snprintf(buf, sizeof(buf), "%lld", (arith_t_type) rez); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 12915 | #else |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12916 | snprintf(buf, sizeof(buf), "%ld", (arith_t_type) rez); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 12917 | #endif |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12918 | setvar(numptr_m1->var, buf, 0); |
| 12919 | /* after saving, make previous value for v++ or v-- */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12920 | if (op == TOK_POST_INC) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12921 | rez--; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12922 | else if (op == TOK_POST_DEC) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12923 | rez++; |
| 12924 | } |
| 12925 | numptr_m1->val = rez; |
| 12926 | /* protect geting var value, is number now */ |
| 12927 | numptr_m1->var = NULL; |
| 12928 | return 0; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 12929 | err: |
| 12930 | return -1; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12931 | } |
| 12932 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12933 | /* longest must be first */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12934 | static const char op_tokens[] ALIGN1 = { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12935 | '<','<','=',0, TOK_LSHIFT_ASSIGN, |
| 12936 | '>','>','=',0, TOK_RSHIFT_ASSIGN, |
| 12937 | '<','<', 0, TOK_LSHIFT, |
| 12938 | '>','>', 0, TOK_RSHIFT, |
| 12939 | '|','|', 0, TOK_OR, |
| 12940 | '&','&', 0, TOK_AND, |
| 12941 | '!','=', 0, TOK_NE, |
| 12942 | '<','=', 0, TOK_LE, |
| 12943 | '>','=', 0, TOK_GE, |
| 12944 | '=','=', 0, TOK_EQ, |
| 12945 | '|','=', 0, TOK_OR_ASSIGN, |
| 12946 | '&','=', 0, TOK_AND_ASSIGN, |
| 12947 | '*','=', 0, TOK_MUL_ASSIGN, |
| 12948 | '/','=', 0, TOK_DIV_ASSIGN, |
| 12949 | '%','=', 0, TOK_REM_ASSIGN, |
| 12950 | '+','=', 0, TOK_PLUS_ASSIGN, |
| 12951 | '-','=', 0, TOK_MINUS_ASSIGN, |
| 12952 | '-','-', 0, TOK_POST_DEC, |
| 12953 | '^','=', 0, TOK_XOR_ASSIGN, |
| 12954 | '+','+', 0, TOK_POST_INC, |
| 12955 | '*','*', 0, TOK_EXPONENT, |
| 12956 | '!', 0, TOK_NOT, |
| 12957 | '<', 0, TOK_LT, |
| 12958 | '>', 0, TOK_GT, |
| 12959 | '=', 0, TOK_ASSIGN, |
| 12960 | '|', 0, TOK_BOR, |
| 12961 | '&', 0, TOK_BAND, |
| 12962 | '*', 0, TOK_MUL, |
| 12963 | '/', 0, TOK_DIV, |
| 12964 | '%', 0, TOK_REM, |
| 12965 | '+', 0, TOK_ADD, |
| 12966 | '-', 0, TOK_SUB, |
| 12967 | '^', 0, TOK_BXOR, |
| 12968 | /* uniq */ |
| 12969 | '~', 0, TOK_BNOT, |
| 12970 | ',', 0, TOK_COMMA, |
| 12971 | '?', 0, TOK_CONDITIONAL, |
| 12972 | ':', 0, TOK_CONDITIONAL_SEP, |
| 12973 | ')', 0, TOK_RPAREN, |
| 12974 | '(', 0, TOK_LPAREN, |
| 12975 | 0 |
| 12976 | }; |
| 12977 | /* ptr to ")" */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12978 | #define endexpression (&op_tokens[sizeof(op_tokens)-7]) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12979 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12980 | static arith_t |
| 12981 | arith(const char *expr, int *perrcode) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12982 | { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12983 | char arithval; /* Current character under analysis */ |
| 12984 | operator lasttok, op; |
| 12985 | operator prec; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12986 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12987 | const char *p = endexpression; |
| 12988 | int errcode; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12989 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12990 | size_t datasizes = strlen(expr) + 2; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12991 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12992 | /* Stack of integers */ |
| 12993 | /* The proof that there can be no more than strlen(startbuf)/2+1 integers |
| 12994 | * in any given correct or incorrect expression is left as an exercise to |
| 12995 | * the reader. */ |
| 12996 | v_n_t *numstack = alloca(((datasizes)/2)*sizeof(v_n_t)), |
| 12997 | *numstackptr = numstack; |
| 12998 | /* Stack of operator tokens */ |
| 12999 | operator *stack = alloca((datasizes) * sizeof(operator)), |
| 13000 | *stackptr = stack; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13001 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13002 | *stackptr++ = lasttok = TOK_LPAREN; /* start off with a left paren */ |
| 13003 | *perrcode = errcode = 0; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13004 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13005 | while (1) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 13006 | arithval = *expr; |
| 13007 | if (arithval == 0) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13008 | if (p == endexpression) { |
| 13009 | /* Null expression. */ |
| 13010 | return 0; |
| 13011 | } |
| 13012 | |
| 13013 | /* This is only reached after all tokens have been extracted from the |
| 13014 | * input stream. If there are still tokens on the operator stack, they |
| 13015 | * are to be applied in order. At the end, there should be a final |
| 13016 | * result on the integer stack */ |
| 13017 | |
| 13018 | if (expr != endexpression + 1) { |
| 13019 | /* If we haven't done so already, */ |
| 13020 | /* append a closing right paren */ |
| 13021 | expr = endexpression; |
| 13022 | /* and let the loop process it. */ |
| 13023 | continue; |
| 13024 | } |
| 13025 | /* At this point, we're done with the expression. */ |
| 13026 | if (numstackptr != numstack+1) { |
| 13027 | /* ... but if there isn't, it's bad */ |
| 13028 | err: |
| 13029 | return (*perrcode = -1); |
| 13030 | } |
| 13031 | if (numstack->var) { |
| 13032 | /* expression is $((var)) only, lookup now */ |
| 13033 | errcode = arith_lookup_val(numstack); |
| 13034 | } |
| 13035 | ret: |
| 13036 | *perrcode = errcode; |
| 13037 | return numstack->val; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13038 | } |
| 13039 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13040 | /* Continue processing the expression. */ |
| 13041 | if (arith_isspace(arithval)) { |
| 13042 | /* Skip whitespace */ |
| 13043 | goto prologue; |
| 13044 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 13045 | p = endofname(expr); |
| 13046 | if (p != expr) { |
Eric Andersen | ad63cb2 | 2004-10-08 09:43:34 +0000 | [diff] [blame] | 13047 | size_t var_name_size = (p-expr) + 1; /* trailing zero */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13048 | |
| 13049 | numstackptr->var = alloca(var_name_size); |
| 13050 | safe_strncpy(numstackptr->var, expr, var_name_size); |
| 13051 | expr = p; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13052 | num: |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13053 | numstackptr->contidional_second_val_initialized = 0; |
| 13054 | numstackptr++; |
| 13055 | lasttok = TOK_NUM; |
| 13056 | continue; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13057 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13058 | if (isdigit(arithval)) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13059 | numstackptr->var = NULL; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 13060 | #if ENABLE_ASH_MATH_SUPPORT_64 |
Eric Andersen | ad63cb2 | 2004-10-08 09:43:34 +0000 | [diff] [blame] | 13061 | numstackptr->val = strtoll(expr, (char **) &expr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13062 | #else |
| 13063 | numstackptr->val = strtol(expr, (char **) &expr, 0); |
| 13064 | #endif |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13065 | goto num; |
| 13066 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13067 | for (p = op_tokens; ; p++) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13068 | const char *o; |
| 13069 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13070 | if (*p == 0) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13071 | /* strange operator not found */ |
| 13072 | goto err; |
| 13073 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13074 | for (o = expr; *p && *o == *p; p++) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13075 | o++; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13076 | if (! *p) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13077 | /* found */ |
| 13078 | expr = o - 1; |
| 13079 | break; |
| 13080 | } |
| 13081 | /* skip tail uncompared token */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13082 | while (*p) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13083 | p++; |
| 13084 | /* skip zero delim */ |
| 13085 | p++; |
| 13086 | } |
| 13087 | op = p[1]; |
| 13088 | |
| 13089 | /* post grammar: a++ reduce to num */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13090 | if (lasttok == TOK_POST_INC || lasttok == TOK_POST_DEC) |
| 13091 | lasttok = TOK_NUM; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13092 | |
| 13093 | /* Plus and minus are binary (not unary) _only_ if the last |
| 13094 | * token was as number, or a right paren (which pretends to be |
| 13095 | * a number, since it evaluates to one). Think about it. |
| 13096 | * It makes sense. */ |
| 13097 | if (lasttok != TOK_NUM) { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 13098 | switch (op) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13099 | case TOK_ADD: |
| 13100 | op = TOK_UPLUS; |
| 13101 | break; |
| 13102 | case TOK_SUB: |
| 13103 | op = TOK_UMINUS; |
| 13104 | break; |
| 13105 | case TOK_POST_INC: |
| 13106 | op = TOK_PRE_INC; |
| 13107 | break; |
| 13108 | case TOK_POST_DEC: |
| 13109 | op = TOK_PRE_DEC; |
| 13110 | break; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13111 | } |
| 13112 | } |
| 13113 | /* We don't want a unary operator to cause recursive descent on the |
| 13114 | * stack, because there can be many in a row and it could cause an |
| 13115 | * operator to be evaluated before its argument is pushed onto the |
| 13116 | * integer stack. */ |
| 13117 | /* But for binary operators, "apply" everything on the operator |
| 13118 | * stack until we find an operator with a lesser priority than the |
| 13119 | * one we have just extracted. */ |
| 13120 | /* Left paren is given the lowest priority so it will never be |
| 13121 | * "applied" in this way. |
| 13122 | * if associativity is right and priority eq, applied also skip |
| 13123 | */ |
| 13124 | prec = PREC(op); |
| 13125 | if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) { |
| 13126 | /* not left paren or unary */ |
| 13127 | if (lasttok != TOK_NUM) { |
| 13128 | /* binary op must be preceded by a num */ |
| 13129 | goto err; |
| 13130 | } |
| 13131 | while (stackptr != stack) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13132 | if (op == TOK_RPAREN) { |
| 13133 | /* The algorithm employed here is simple: while we don't |
| 13134 | * hit an open paren nor the bottom of the stack, pop |
| 13135 | * tokens and apply them */ |
| 13136 | if (stackptr[-1] == TOK_LPAREN) { |
| 13137 | --stackptr; |
| 13138 | /* Any operator directly after a */ |
| 13139 | lasttok = TOK_NUM; |
| 13140 | /* close paren should consider itself binary */ |
| 13141 | goto prologue; |
| 13142 | } |
| 13143 | } else { |
| 13144 | operator prev_prec = PREC(stackptr[-1]); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13145 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13146 | convert_prec_is_assing(prec); |
| 13147 | convert_prec_is_assing(prev_prec); |
| 13148 | if (prev_prec < prec) |
| 13149 | break; |
| 13150 | /* check right assoc */ |
| 13151 | if (prev_prec == prec && is_right_associativity(prec)) |
| 13152 | break; |
| 13153 | } |
| 13154 | errcode = arith_apply(*--stackptr, numstack, &numstackptr); |
| 13155 | if (errcode) goto ret; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13156 | } |
| 13157 | if (op == TOK_RPAREN) { |
| 13158 | goto err; |
| 13159 | } |
| 13160 | } |
| 13161 | |
| 13162 | /* Push this operator to the stack and remember it. */ |
| 13163 | *stackptr++ = lasttok = op; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13164 | prologue: |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13165 | ++expr; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13166 | } /* while */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13167 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 13168 | #endif /* ASH_MATH_SUPPORT */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13169 | |
| 13170 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13171 | /* ============ main() and helpers */ |
| 13172 | |
| 13173 | /* |
| 13174 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13175 | */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13176 | static void exitshell(void) ATTRIBUTE_NORETURN; |
| 13177 | static void |
| 13178 | exitshell(void) |
| 13179 | { |
| 13180 | struct jmploc loc; |
| 13181 | char *p; |
| 13182 | int status; |
| 13183 | |
| 13184 | status = exitstatus; |
| 13185 | TRACE(("pid %d, exitshell(%d)\n", getpid(), status)); |
| 13186 | if (setjmp(loc.loc)) { |
| 13187 | if (exception == EXEXIT) |
| 13188 | /* dash bug: it just does _exit(exitstatus) here |
| 13189 | * but we have to do setjobctl(0) first! |
| 13190 | * (bug is still not fixed in dash-0.5.3 - if you run dash |
| 13191 | * under Midnight Commander, on exit from dash MC is backgrounded) */ |
| 13192 | status = exitstatus; |
| 13193 | goto out; |
| 13194 | } |
| 13195 | exception_handler = &loc; |
| 13196 | p = trap[0]; |
| 13197 | if (p) { |
| 13198 | trap[0] = NULL; |
| 13199 | evalstring(p, 0); |
| 13200 | } |
| 13201 | flush_stdout_stderr(); |
| 13202 | out: |
| 13203 | setjobctl(0); |
| 13204 | _exit(status); |
| 13205 | /* NOTREACHED */ |
| 13206 | } |
| 13207 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 13208 | static void |
| 13209 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13210 | { |
| 13211 | /* from input.c: */ |
| 13212 | basepf.nextc = basepf.buf = basebuf; |
| 13213 | |
| 13214 | /* from trap.c: */ |
| 13215 | signal(SIGCHLD, SIG_DFL); |
| 13216 | |
| 13217 | /* from var.c: */ |
| 13218 | { |
| 13219 | char **envp; |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 13220 | char ppid[sizeof(int)*3 + 1]; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13221 | const char *p; |
| 13222 | struct stat st1, st2; |
| 13223 | |
| 13224 | initvar(); |
| 13225 | for (envp = environ; envp && *envp; envp++) { |
| 13226 | if (strchr(*envp, '=')) { |
| 13227 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 13228 | } |
| 13229 | } |
| 13230 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 13231 | snprintf(ppid, sizeof(ppid), "%u", (unsigned) getppid()); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13232 | setvar("PPID", ppid, 0); |
| 13233 | |
| 13234 | p = lookupvar("PWD"); |
| 13235 | if (p) |
| 13236 | if (*p != '/' || stat(p, &st1) || stat(".", &st2) |
| 13237 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) |
| 13238 | p = '\0'; |
| 13239 | setpwd(p, 0); |
| 13240 | } |
| 13241 | } |
| 13242 | |
| 13243 | /* |
| 13244 | * Process the shell command line arguments. |
| 13245 | */ |
| 13246 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13247 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13248 | { |
| 13249 | int i; |
| 13250 | const char *xminusc; |
| 13251 | char **xargv; |
| 13252 | |
| 13253 | xargv = argv; |
| 13254 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13255 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13256 | xargv++; |
| 13257 | for (i = 0; i < NOPTS; i++) |
| 13258 | optlist[i] = 2; |
| 13259 | argptr = xargv; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 13260 | if (options(1)) { |
| 13261 | /* it already printed err message */ |
| 13262 | raise_exception(EXERROR); |
| 13263 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13264 | xargv = argptr; |
| 13265 | xminusc = minusc; |
| 13266 | if (*xargv == NULL) { |
| 13267 | if (xminusc) |
| 13268 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 13269 | sflag = 1; |
| 13270 | } |
| 13271 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 13272 | iflag = 1; |
| 13273 | if (mflag == 2) |
| 13274 | mflag = iflag; |
| 13275 | for (i = 0; i < NOPTS; i++) |
| 13276 | if (optlist[i] == 2) |
| 13277 | optlist[i] = 0; |
| 13278 | #if DEBUG == 2 |
| 13279 | debug = 1; |
| 13280 | #endif |
| 13281 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 13282 | if (xminusc) { |
| 13283 | minusc = *xargv++; |
| 13284 | if (*xargv) |
| 13285 | goto setarg0; |
| 13286 | } else if (!sflag) { |
| 13287 | setinputfile(*xargv, 0); |
| 13288 | setarg0: |
| 13289 | arg0 = *xargv++; |
| 13290 | commandname = arg0; |
| 13291 | } |
| 13292 | |
| 13293 | shellparam.p = xargv; |
| 13294 | #if ENABLE_ASH_GETOPTS |
| 13295 | shellparam.optind = 1; |
| 13296 | shellparam.optoff = -1; |
| 13297 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13298 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13299 | while (*xargv) { |
| 13300 | shellparam.nparam++; |
| 13301 | xargv++; |
| 13302 | } |
| 13303 | optschanged(); |
| 13304 | } |
| 13305 | |
| 13306 | /* |
| 13307 | * Read /etc/profile or .profile. |
| 13308 | */ |
| 13309 | static void |
| 13310 | read_profile(const char *name) |
| 13311 | { |
| 13312 | int skip; |
| 13313 | |
| 13314 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 13315 | return; |
| 13316 | skip = cmdloop(0); |
| 13317 | popfile(); |
| 13318 | if (skip) |
| 13319 | exitshell(); |
| 13320 | } |
| 13321 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13322 | /* |
| 13323 | * This routine is called when an error or an interrupt occurs in an |
| 13324 | * interactive shell and control is returned to the main command loop. |
| 13325 | */ |
| 13326 | static void |
| 13327 | reset(void) |
| 13328 | { |
| 13329 | /* from eval.c: */ |
| 13330 | evalskip = 0; |
| 13331 | loopnest = 0; |
| 13332 | /* from input.c: */ |
| 13333 | parselleft = parsenleft = 0; /* clear input buffer */ |
| 13334 | popallfiles(); |
| 13335 | /* from parser.c: */ |
| 13336 | tokpushback = 0; |
| 13337 | checkkwd = 0; |
| 13338 | /* from redir.c: */ |
| 13339 | clearredir(0); |
| 13340 | } |
| 13341 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13342 | #if PROFILE |
| 13343 | static short profile_buf[16384]; |
| 13344 | extern int etext(); |
| 13345 | #endif |
| 13346 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13347 | /* |
| 13348 | * Main routine. We initialize things, parse the arguments, execute |
| 13349 | * profiles if we're a login shell, and then call cmdloop to execute |
| 13350 | * commands. The setjmp call sets up the location to jump to when an |
| 13351 | * exception occurs. When an exception occurs the variable "state" |
| 13352 | * is used to figure out how far we had gotten. |
| 13353 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 13354 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13355 | int ash_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13356 | { |
| 13357 | char *shinit; |
| 13358 | volatile int state; |
| 13359 | struct jmploc jmploc; |
| 13360 | struct stackmark smark; |
| 13361 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13362 | /* Initialize global data */ |
| 13363 | INIT_G_misc(); |
| 13364 | INIT_G_memstack(); |
| 13365 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 13366 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13367 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 13368 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13369 | INIT_G_cmdtable(); |
| 13370 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13371 | #if PROFILE |
| 13372 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 13373 | #endif |
| 13374 | |
| 13375 | #if ENABLE_FEATURE_EDITING |
| 13376 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
| 13377 | #endif |
| 13378 | state = 0; |
| 13379 | if (setjmp(jmploc.loc)) { |
| 13380 | int e; |
| 13381 | int s; |
| 13382 | |
| 13383 | reset(); |
| 13384 | |
| 13385 | e = exception; |
| 13386 | if (e == EXERROR) |
| 13387 | exitstatus = 2; |
| 13388 | s = state; |
| 13389 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) |
| 13390 | exitshell(); |
| 13391 | |
| 13392 | if (e == EXINT) { |
| 13393 | outcslow('\n', stderr); |
| 13394 | } |
| 13395 | popstackmark(&smark); |
| 13396 | FORCE_INT_ON; /* enable interrupts */ |
| 13397 | if (s == 1) |
| 13398 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13399 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13400 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13401 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13402 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13403 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13404 | } |
| 13405 | exception_handler = &jmploc; |
| 13406 | #if DEBUG |
| 13407 | opentrace(); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13408 | trace_puts("Shell args: "); |
| 13409 | trace_puts_args(argv); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13410 | #endif |
| 13411 | rootpid = getpid(); |
| 13412 | |
| 13413 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 13414 | rseed = rootpid + time(NULL); |
| 13415 | #endif |
| 13416 | init(); |
| 13417 | setstackmark(&smark); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13418 | procargs(argv); |
| 13419 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13420 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
| 13421 | if (iflag) { |
| 13422 | const char *hp = lookupvar("HISTFILE"); |
| 13423 | |
| 13424 | if (hp == NULL) { |
| 13425 | hp = lookupvar("HOME"); |
| 13426 | if (hp != NULL) { |
| 13427 | char *defhp = concat_path_file(hp, ".ash_history"); |
| 13428 | setvar("HISTFILE", defhp, 0); |
| 13429 | free(defhp); |
| 13430 | } |
| 13431 | } |
| 13432 | } |
| 13433 | #endif |
| 13434 | if (argv[0] && argv[0][0] == '-') |
| 13435 | isloginsh = 1; |
| 13436 | if (isloginsh) { |
| 13437 | state = 1; |
| 13438 | read_profile("/etc/profile"); |
| 13439 | state1: |
| 13440 | state = 2; |
| 13441 | read_profile(".profile"); |
| 13442 | } |
| 13443 | state2: |
| 13444 | state = 3; |
| 13445 | if ( |
| 13446 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13447 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13448 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13449 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13450 | ) { |
| 13451 | shinit = lookupvar("ENV"); |
| 13452 | if (shinit != NULL && *shinit != '\0') { |
| 13453 | read_profile(shinit); |
| 13454 | } |
| 13455 | } |
| 13456 | state3: |
| 13457 | state = 4; |
| 13458 | if (minusc) |
| 13459 | evalstring(minusc, 0); |
| 13460 | |
| 13461 | if (sflag || minusc == NULL) { |
| 13462 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
| 13463 | if ( iflag ) { |
| 13464 | const char *hp = lookupvar("HISTFILE"); |
| 13465 | |
| 13466 | if (hp != NULL) |
| 13467 | line_input_state->hist_file = hp; |
| 13468 | } |
| 13469 | #endif |
| 13470 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 13471 | cmdloop(1); |
| 13472 | } |
| 13473 | #if PROFILE |
| 13474 | monitor(0); |
| 13475 | #endif |
| 13476 | #ifdef GPROF |
| 13477 | { |
| 13478 | extern void _mcleanup(void); |
| 13479 | _mcleanup(); |
| 13480 | } |
| 13481 | #endif |
| 13482 | exitshell(); |
| 13483 | /* NOTREACHED */ |
| 13484 | } |
| 13485 | |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 13486 | #if DEBUG |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 13487 | const char *applet_name = "debug stuff usage"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13488 | int main(int argc, char **argv) |
| 13489 | { |
| 13490 | return ash_main(argc, argv); |
| 13491 | } |
| 13492 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 13493 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13494 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13495 | /*- |
| 13496 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 13497 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13498 | * |
| 13499 | * This code is derived from software contributed to Berkeley by |
| 13500 | * Kenneth Almquist. |
| 13501 | * |
| 13502 | * Redistribution and use in source and binary forms, with or without |
| 13503 | * modification, are permitted provided that the following conditions |
| 13504 | * are met: |
| 13505 | * 1. Redistributions of source code must retain the above copyright |
| 13506 | * notice, this list of conditions and the following disclaimer. |
| 13507 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13508 | * notice, this list of conditions and the following disclaimer in the |
| 13509 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13510 | * 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] | 13511 | * may be used to endorse or promote products derived from this software |
| 13512 | * without specific prior written permission. |
| 13513 | * |
| 13514 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 13515 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13516 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 13517 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13518 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13519 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 13520 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13521 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 13522 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 13523 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 13524 | * SUCH DAMAGE. |
| 13525 | */ |