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 | 5bb1677 | 2001-09-06 18:00:41 +0000 | [diff] [blame] | 45 | #define IFS_BROKEN |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 46 | #define PROFILE 0 |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 47 | #if ENABLE_ASH_JOB_CONTROL |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 48 | #define JOBS 1 |
| 49 | #else |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 50 | #define JOBS 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 53 | #if DEBUG |
| 54 | #define _GNU_SOURCE |
| 55 | #endif |
| 56 | #include "busybox.h" |
| 57 | #include <paths.h> |
| 58 | #include <setjmp.h> |
| 59 | #include <fnmatch.h> |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 60 | #if JOBS || ENABLE_ASH_READ_NCHARS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 61 | #include <termios.h> |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 64 | #if defined(__uClinux__) |
| 65 | #error "Do not even bother, ash will not run on uClinux" |
| 66 | #endif |
| 67 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 68 | #if DEBUG |
| 69 | #define TRACE(param) trace param |
| 70 | #define TRACEV(param) tracev param |
| 71 | #else |
| 72 | #define TRACE(param) |
| 73 | #define TRACEV(param) |
| 74 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 75 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 76 | #ifdef __GLIBC__ |
| 77 | /* glibc sucks */ |
| 78 | static int *dash_errno; |
| 79 | #undef errno |
| 80 | #define errno (*dash_errno) |
| 81 | #endif |
| 82 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 83 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 84 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 85 | #define ALIASINUSE 1 |
| 86 | #define ALIASDEAD 2 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 87 | struct alias { |
| 88 | struct alias *next; |
| 89 | char *name; |
| 90 | char *val; |
| 91 | int flag; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 92 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 93 | static int aliascmd(int, char **); |
| 94 | static int unaliascmd(int, char **); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 95 | static void printalias(const struct alias *); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 96 | #endif |
| 97 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 98 | |
| 99 | /* ============ Shell options */ |
| 100 | |
| 101 | static const char *const optletters_optnames[] = { |
| 102 | "e" "errexit", |
| 103 | "f" "noglob", |
| 104 | "I" "ignoreeof", |
| 105 | "i" "interactive", |
| 106 | "m" "monitor", |
| 107 | "n" "noexec", |
| 108 | "s" "stdin", |
| 109 | "x" "xtrace", |
| 110 | "v" "verbose", |
| 111 | "C" "noclobber", |
| 112 | "a" "allexport", |
| 113 | "b" "notify", |
| 114 | "u" "nounset", |
| 115 | "\0" "vi", |
| 116 | #if DEBUG |
| 117 | "\0" "nolog", |
| 118 | "\0" "debug", |
| 119 | #endif |
| 120 | }; |
| 121 | |
| 122 | #define optletters(n) optletters_optnames[(n)][0] |
| 123 | #define optnames(n) (&optletters_optnames[(n)][1]) |
| 124 | |
| 125 | #define NOPTS (sizeof(optletters_optnames)/sizeof(optletters_optnames[0])) |
| 126 | |
| 127 | static char optlist[NOPTS]; |
| 128 | |
| 129 | #define eflag optlist[0] |
| 130 | #define fflag optlist[1] |
| 131 | #define Iflag optlist[2] |
| 132 | #define iflag optlist[3] |
| 133 | #define mflag optlist[4] |
| 134 | #define nflag optlist[5] |
| 135 | #define sflag optlist[6] |
| 136 | #define xflag optlist[7] |
| 137 | #define vflag optlist[8] |
| 138 | #define Cflag optlist[9] |
| 139 | #define aflag optlist[10] |
| 140 | #define bflag optlist[11] |
| 141 | #define uflag optlist[12] |
| 142 | #define viflag optlist[13] |
| 143 | #if DEBUG |
| 144 | #define nolog optlist[14] |
| 145 | #define debug optlist[15] |
| 146 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 147 | |
| 148 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 149 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 150 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 151 | static char nullstr[1]; /* zero length string */ |
| 152 | static const char homestr[] = "HOME"; |
| 153 | static const char snlfmt[] = "%s\n"; |
| 154 | static const char illnum[] = "Illegal number: %s"; |
| 155 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 156 | static int isloginsh; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 157 | /* pid of main shell */ |
| 158 | static int rootpid; |
| 159 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 160 | static int shlvl; |
| 161 | #define rootshell (!shlvl) |
| 162 | /* trap handler commands */ |
| 163 | static char *trap[NSIG]; |
| 164 | /* current value of signal */ |
| 165 | static char sigmode[NSIG - 1]; |
| 166 | /* indicates specified signal received */ |
| 167 | static char gotsig[NSIG - 1]; |
| 168 | static char *arg0; /* value of $0 */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 169 | |
| 170 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 171 | /* ============ Interrupts / exceptions |
| 172 | * |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 173 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 174 | * jump locations. The global variable handler contains the location to |
| 175 | * jump to when an exception occurs, and the global variable exception |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 176 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 177 | * exception handlers, the user should save the value of handler on entry |
| 178 | * to an inner scope, set handler to point to a jmploc structure for the |
| 179 | * inner scope, and restore handler on exit from the scope. |
| 180 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 181 | struct jmploc { |
| 182 | jmp_buf loc; |
| 183 | }; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 184 | static struct jmploc *exception_handler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 185 | static int exception; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 186 | /* exceptions */ |
| 187 | #define EXINT 0 /* SIGINT received */ |
| 188 | #define EXERROR 1 /* a generic error */ |
| 189 | #define EXSHELLPROC 2 /* execute a shell procedure */ |
| 190 | #define EXEXEC 3 /* command execution failed */ |
| 191 | #define EXEXIT 4 /* exit the shell */ |
| 192 | #define EXSIG 5 /* trapped signal in wait(1) */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 193 | static volatile int suppressint; |
| 194 | static volatile sig_atomic_t intpending; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 195 | /* do we generate EXSIG events */ |
| 196 | static int exsig; |
| 197 | /* last pending signal */ |
| 198 | static volatile sig_atomic_t pendingsigs; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * These macros allow the user to suspend the handling of interrupt signals |
| 202 | * over a period of time. This is similar to SIGHOLD to or sigblock, but |
| 203 | * much more efficient and portable. (But hacking the kernel is so much |
| 204 | * more fun than worrying about efficiency and portability. :-)) |
| 205 | */ |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 206 | #define xbarrier() ({ __asm__ __volatile__ ("": : :"memory"); }) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 207 | #define INT_OFF \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 208 | ({ \ |
| 209 | suppressint++; \ |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 210 | xbarrier(); \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 211 | 0; \ |
| 212 | }) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 216 | * just do a longjmp to the exception handler. The type of exception is |
| 217 | * stored in the global variable "exception". |
| 218 | */ |
| 219 | static void raise_exception(int) ATTRIBUTE_NORETURN; |
| 220 | static void |
| 221 | raise_exception(int e) |
| 222 | { |
| 223 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 224 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 225 | abort(); |
| 226 | #endif |
| 227 | INT_OFF; |
| 228 | exception = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 229 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Called from trap.c when a SIGINT is received. (If the user specifies |
| 234 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 235 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 236 | * are held using the INT_OFF macro. (The test for iflag is just |
| 237 | * defensive programming.) |
| 238 | */ |
| 239 | static void raise_interrupt(void) ATTRIBUTE_NORETURN; |
| 240 | static void |
| 241 | raise_interrupt(void) |
| 242 | { |
| 243 | int i; |
| 244 | |
| 245 | intpending = 0; |
| 246 | i = EXSIG; |
| 247 | if (gotsig[SIGINT - 1] && !trap[SIGINT]) { |
| 248 | if (!(rootshell && iflag)) { |
| 249 | signal(SIGINT, SIG_DFL); |
| 250 | raise(SIGINT); |
| 251 | } |
| 252 | i = EXINT; |
| 253 | } |
| 254 | raise_exception(i); |
| 255 | /* NOTREACHED */ |
| 256 | } |
| 257 | |
| 258 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 259 | static void int_on(void) |
| 260 | { |
| 261 | if (--suppressint == 0 && intpending) { |
| 262 | raise_interrupt(); |
| 263 | } |
| 264 | } |
| 265 | #define INT_ON int_on() |
| 266 | static void force_int_on(void) |
| 267 | { |
| 268 | suppressint = 0; |
| 269 | if (intpending) |
| 270 | raise_interrupt(); |
| 271 | } |
| 272 | #define FORCE_INT_ON force_int_on() |
| 273 | #else |
| 274 | #define INT_ON \ |
| 275 | ({ \ |
| 276 | xbarrier(); \ |
| 277 | if (--suppressint == 0 && intpending) raise_interrupt(); \ |
| 278 | 0; \ |
| 279 | }) |
| 280 | #define FORCE_INT_ON \ |
| 281 | ({ \ |
| 282 | xbarrier(); \ |
| 283 | suppressint = 0; \ |
| 284 | if (intpending) raise_interrupt(); \ |
| 285 | 0; \ |
| 286 | }) |
| 287 | #endif /* ASH_OPTIMIZE_FOR_SIZE */ |
| 288 | |
| 289 | #define SAVE_INT(v) ((v) = suppressint) |
| 290 | |
| 291 | #define RESTORE_INT(v) \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 292 | ({ \ |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 293 | xbarrier(); \ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 294 | suppressint = (v); \ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 295 | if (suppressint == 0 && intpending) raise_interrupt(); \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 296 | 0; \ |
| 297 | }) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 298 | |
| 299 | #define EXSIGON \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 300 | ({ \ |
| 301 | exsig++; \ |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 302 | xbarrier(); \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 303 | if (pendingsigs) \ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 304 | raise_exception(EXSIG); \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 305 | 0; \ |
| 306 | }) |
| 307 | /* EXSIG is turned off by evalbltin(). */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 308 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 309 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 310 | /* ============ stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 311 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 312 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 313 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 314 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 315 | INT_OFF; |
| 316 | fputs(p, file); |
| 317 | INT_ON; |
| 318 | } |
| 319 | |
| 320 | static void |
| 321 | flush_stdout_stderr(void) |
| 322 | { |
| 323 | INT_OFF; |
| 324 | fflush(stdout); |
| 325 | fflush(stderr); |
| 326 | INT_ON; |
| 327 | } |
| 328 | |
| 329 | static void |
| 330 | flush_stderr(void) |
| 331 | { |
| 332 | INT_OFF; |
| 333 | fflush(stderr); |
| 334 | INT_ON; |
| 335 | } |
| 336 | |
| 337 | static void |
| 338 | outcslow(int c, FILE *dest) |
| 339 | { |
| 340 | INT_OFF; |
| 341 | putc(c, dest); |
| 342 | fflush(dest); |
| 343 | INT_ON; |
| 344 | } |
| 345 | |
| 346 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 347 | static int |
| 348 | out1fmt(const char *fmt, ...) |
| 349 | { |
| 350 | va_list ap; |
| 351 | int r; |
| 352 | |
| 353 | INT_OFF; |
| 354 | va_start(ap, fmt); |
| 355 | r = vprintf(fmt, ap); |
| 356 | va_end(ap); |
| 357 | INT_ON; |
| 358 | return r; |
| 359 | } |
| 360 | |
| 361 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 362 | static int |
| 363 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 364 | { |
| 365 | va_list ap; |
| 366 | int ret; |
| 367 | |
| 368 | va_start(ap, fmt); |
| 369 | INT_OFF; |
| 370 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 371 | va_end(ap); |
| 372 | INT_ON; |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | static void |
| 377 | out1str(const char *p) |
| 378 | { |
| 379 | outstr(p, stdout); |
| 380 | } |
| 381 | |
| 382 | static void |
| 383 | out2str(const char *p) |
| 384 | { |
| 385 | outstr(p, stderr); |
| 386 | flush_stderr(); |
| 387 | } |
| 388 | |
| 389 | |
| 390 | /* ============ Parser data |
| 391 | * |
| 392 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 393 | */ |
| 394 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 395 | struct strlist { |
| 396 | struct strlist *next; |
| 397 | char *text; |
| 398 | }; |
| 399 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 400 | struct strpush { |
| 401 | struct strpush *prev; /* preceding string on stack */ |
| 402 | char *prevstring; |
| 403 | int prevnleft; |
| 404 | #if ENABLE_ASH_ALIAS |
| 405 | struct alias *ap; /* if push was associated with an alias */ |
| 406 | #endif |
| 407 | char *string; /* remember the string since it may change */ |
| 408 | }; |
| 409 | |
| 410 | struct parsefile { |
| 411 | struct parsefile *prev; /* preceding file on stack */ |
| 412 | int linno; /* current line */ |
| 413 | int fd; /* file descriptor (or -1 if string) */ |
| 414 | int nleft; /* number of chars left in this line */ |
| 415 | int lleft; /* number of chars left in this buffer */ |
| 416 | char *nextc; /* next char in buffer */ |
| 417 | char *buf; /* input buffer */ |
| 418 | struct strpush *strpush; /* for pushing strings at this level */ |
| 419 | struct strpush basestrpush; /* so pushing one is fast */ |
| 420 | }; |
| 421 | |
| 422 | static struct parsefile basepf; /* top level input file */ |
| 423 | static struct parsefile *parsefile = &basepf; /* current input file */ |
| 424 | static int startlinno; /* line # where last token started */ |
| 425 | static char *commandname; /* currently executing command */ |
| 426 | static struct strlist *cmdenviron; /* environment for builtin command */ |
| 427 | static int exitstatus; /* exit status of last command */ |
| 428 | static int back_exitstatus; /* exit status of backquoted command */ |
| 429 | |
| 430 | |
| 431 | /* ============ Message printing */ |
| 432 | |
| 433 | static void |
| 434 | ash_vmsg(const char *msg, va_list ap) |
| 435 | { |
| 436 | fprintf(stderr, "%s: ", arg0); |
| 437 | if (commandname) { |
| 438 | const char *fmt = (!iflag || parsefile->fd) ? |
| 439 | "%s: %d: " : "%s: "; |
| 440 | fprintf(stderr, fmt, commandname, startlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 441 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 442 | vfprintf(stderr, msg, ap); |
| 443 | outcslow('\n', stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 444 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 445 | |
| 446 | /* |
| 447 | * Exverror is called to raise the error exception. If the second argument |
| 448 | * is not NULL then error prints an error message using printf style |
| 449 | * formatting. It then raises the error exception. |
| 450 | */ |
| 451 | static void ash_vmsg_and_raise(int, const char *, va_list) ATTRIBUTE_NORETURN; |
| 452 | static void |
| 453 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 454 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 455 | #if DEBUG |
| 456 | if (msg) { |
| 457 | TRACE(("ash_vmsg_and_raise(%d, \"", cond)); |
| 458 | TRACEV((msg, ap)); |
| 459 | TRACE(("\") pid=%d\n", getpid())); |
| 460 | } else |
| 461 | TRACE(("ash_vmsg_and_raise(%d, NULL) pid=%d\n", cond, getpid())); |
| 462 | if (msg) |
| 463 | #endif |
| 464 | ash_vmsg(msg, ap); |
| 465 | |
| 466 | flush_stdout_stderr(); |
| 467 | raise_exception(cond); |
| 468 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 469 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 470 | |
| 471 | static void ash_msg_and_raise_error(const char *, ...) ATTRIBUTE_NORETURN; |
| 472 | static void |
| 473 | ash_msg_and_raise_error(const char *msg, ...) |
| 474 | { |
| 475 | va_list ap; |
| 476 | |
| 477 | va_start(ap, msg); |
| 478 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 479 | /* NOTREACHED */ |
| 480 | va_end(ap); |
| 481 | } |
| 482 | |
| 483 | static void ash_msg_and_raise(int, const char *, ...) ATTRIBUTE_NORETURN; |
| 484 | static void |
| 485 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 486 | { |
| 487 | va_list ap; |
| 488 | |
| 489 | va_start(ap, msg); |
| 490 | ash_vmsg_and_raise(cond, msg, ap); |
| 491 | /* NOTREACHED */ |
| 492 | va_end(ap); |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * error/warning routines for external builtins |
| 497 | */ |
| 498 | static void |
| 499 | ash_msg(const char *fmt, ...) |
| 500 | { |
| 501 | va_list ap; |
| 502 | |
| 503 | va_start(ap, fmt); |
| 504 | ash_vmsg(fmt, ap); |
| 505 | va_end(ap); |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Return a string describing an error. The returned string may be a |
| 510 | * pointer to a static buffer that will be overwritten on the next call. |
| 511 | * Action describes the operation that got the error. |
| 512 | */ |
| 513 | static const char * |
| 514 | errmsg(int e, const char *em) |
| 515 | { |
| 516 | if (e == ENOENT || e == ENOTDIR) { |
| 517 | return em; |
| 518 | } |
| 519 | return strerror(e); |
| 520 | } |
| 521 | |
| 522 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 523 | /* ============ Memory allocation */ |
| 524 | |
| 525 | /* |
| 526 | * It appears that grabstackstr() will barf with such alignments |
| 527 | * because stalloc() will return a string allocated in a new stackblock. |
| 528 | */ |
| 529 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 530 | enum { |
| 531 | /* Most machines require the value returned from malloc to be aligned |
| 532 | * in some way. The following macro will get this right |
| 533 | * on many machines. */ |
| 534 | SHELL_SIZE = sizeof(union {int i; char *cp; double d; }) - 1, |
| 535 | /* Minimum size of a block */ |
| 536 | MINSIZE = SHELL_ALIGN(504), |
| 537 | }; |
| 538 | |
| 539 | struct stack_block { |
| 540 | struct stack_block *prev; |
| 541 | char space[MINSIZE]; |
| 542 | }; |
| 543 | |
| 544 | struct stackmark { |
| 545 | struct stack_block *stackp; |
| 546 | char *stacknxt; |
| 547 | size_t stacknleft; |
| 548 | struct stackmark *marknext; |
| 549 | }; |
| 550 | |
| 551 | static struct stack_block stackbase; |
| 552 | static struct stack_block *stackp = &stackbase; |
| 553 | static struct stackmark *markp; |
| 554 | static char *stacknxt = stackbase.space; |
| 555 | static size_t stacknleft = MINSIZE; |
| 556 | static char *sstrend = stackbase.space + MINSIZE; |
| 557 | static int herefd = -1; |
| 558 | |
| 559 | #define stackblock() ((void *)stacknxt) |
| 560 | #define stackblocksize() stacknleft |
| 561 | |
| 562 | static void * |
| 563 | ckrealloc(void * p, size_t nbytes) |
| 564 | { |
| 565 | p = realloc(p, nbytes); |
| 566 | if (!p) |
| 567 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 568 | return p; |
| 569 | } |
| 570 | |
| 571 | static void * |
| 572 | ckmalloc(size_t nbytes) |
| 573 | { |
| 574 | return ckrealloc(NULL, nbytes); |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * Make a copy of a string in safe storage. |
| 579 | */ |
| 580 | static char * |
| 581 | ckstrdup(const char *s) |
| 582 | { |
| 583 | char *p = strdup(s); |
| 584 | if (!p) |
| 585 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 586 | return p; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 591 | * to make this more efficient, and also to avoid all sorts of exception |
| 592 | * handling code to handle interrupts in the middle of a parse. |
| 593 | * |
| 594 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 595 | * well. |
| 596 | */ |
| 597 | static void * |
| 598 | stalloc(size_t nbytes) |
| 599 | { |
| 600 | char *p; |
| 601 | size_t aligned; |
| 602 | |
| 603 | aligned = SHELL_ALIGN(nbytes); |
| 604 | if (aligned > stacknleft) { |
| 605 | size_t len; |
| 606 | size_t blocksize; |
| 607 | struct stack_block *sp; |
| 608 | |
| 609 | blocksize = aligned; |
| 610 | if (blocksize < MINSIZE) |
| 611 | blocksize = MINSIZE; |
| 612 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 613 | if (len < blocksize) |
| 614 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 615 | INT_OFF; |
| 616 | sp = ckmalloc(len); |
| 617 | sp->prev = stackp; |
| 618 | stacknxt = sp->space; |
| 619 | stacknleft = blocksize; |
| 620 | sstrend = stacknxt + blocksize; |
| 621 | stackp = sp; |
| 622 | INT_ON; |
| 623 | } |
| 624 | p = stacknxt; |
| 625 | stacknxt += aligned; |
| 626 | stacknleft -= aligned; |
| 627 | return p; |
| 628 | } |
| 629 | |
| 630 | static void |
| 631 | stunalloc(void *p) |
| 632 | { |
| 633 | #if DEBUG |
| 634 | if (!p || (stacknxt < (char *)p) || ((char *)p < stackp->space)) { |
| 635 | write(2, "stunalloc\n", 10); |
| 636 | abort(); |
| 637 | } |
| 638 | #endif |
| 639 | stacknleft += stacknxt - (char *)p; |
| 640 | stacknxt = p; |
| 641 | } |
| 642 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 643 | /* |
| 644 | * Like strdup but works with the ash stack. |
| 645 | */ |
| 646 | static char * |
| 647 | ststrdup(const char *p) |
| 648 | { |
| 649 | size_t len = strlen(p) + 1; |
| 650 | return memcpy(stalloc(len), p, len); |
| 651 | } |
| 652 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 653 | static void |
| 654 | setstackmark(struct stackmark *mark) |
| 655 | { |
| 656 | mark->stackp = stackp; |
| 657 | mark->stacknxt = stacknxt; |
| 658 | mark->stacknleft = stacknleft; |
| 659 | mark->marknext = markp; |
| 660 | markp = mark; |
| 661 | } |
| 662 | |
| 663 | static void |
| 664 | popstackmark(struct stackmark *mark) |
| 665 | { |
| 666 | struct stack_block *sp; |
| 667 | |
| 668 | INT_OFF; |
| 669 | markp = mark->marknext; |
| 670 | while (stackp != mark->stackp) { |
| 671 | sp = stackp; |
| 672 | stackp = sp->prev; |
| 673 | free(sp); |
| 674 | } |
| 675 | stacknxt = mark->stacknxt; |
| 676 | stacknleft = mark->stacknleft; |
| 677 | sstrend = mark->stacknxt + mark->stacknleft; |
| 678 | INT_ON; |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * When the parser reads in a string, it wants to stick the string on the |
| 683 | * stack and only adjust the stack pointer when it knows how big the |
| 684 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 685 | * of space on top of the stack and stackblocklen returns the length of |
| 686 | * this block. Growstackblock will grow this space by at least one byte, |
| 687 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 688 | * part of the block that has been used. |
| 689 | */ |
| 690 | static void |
| 691 | growstackblock(void) |
| 692 | { |
| 693 | size_t newlen; |
| 694 | |
| 695 | newlen = stacknleft * 2; |
| 696 | if (newlen < stacknleft) |
| 697 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 698 | if (newlen < 128) |
| 699 | newlen += 128; |
| 700 | |
| 701 | if (stacknxt == stackp->space && stackp != &stackbase) { |
| 702 | struct stack_block *oldstackp; |
| 703 | struct stackmark *xmark; |
| 704 | struct stack_block *sp; |
| 705 | struct stack_block *prevstackp; |
| 706 | size_t grosslen; |
| 707 | |
| 708 | INT_OFF; |
| 709 | oldstackp = stackp; |
| 710 | sp = stackp; |
| 711 | prevstackp = sp->prev; |
| 712 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 713 | sp = ckrealloc(sp, grosslen); |
| 714 | sp->prev = prevstackp; |
| 715 | stackp = sp; |
| 716 | stacknxt = sp->space; |
| 717 | stacknleft = newlen; |
| 718 | sstrend = sp->space + newlen; |
| 719 | |
| 720 | /* |
| 721 | * Stack marks pointing to the start of the old block |
| 722 | * must be relocated to point to the new block |
| 723 | */ |
| 724 | xmark = markp; |
| 725 | while (xmark != NULL && xmark->stackp == oldstackp) { |
| 726 | xmark->stackp = stackp; |
| 727 | xmark->stacknxt = stacknxt; |
| 728 | xmark->stacknleft = stacknleft; |
| 729 | xmark = xmark->marknext; |
| 730 | } |
| 731 | INT_ON; |
| 732 | } else { |
| 733 | char *oldspace = stacknxt; |
| 734 | int oldlen = stacknleft; |
| 735 | char *p = stalloc(newlen); |
| 736 | |
| 737 | /* free the space we just allocated */ |
| 738 | stacknxt = memcpy(p, oldspace, oldlen); |
| 739 | stacknleft += newlen; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | static void |
| 744 | grabstackblock(size_t len) |
| 745 | { |
| 746 | len = SHELL_ALIGN(len); |
| 747 | stacknxt += len; |
| 748 | stacknleft -= len; |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * The following routines are somewhat easier to use than the above. |
| 753 | * The user declares a variable of type STACKSTR, which may be declared |
| 754 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 755 | * the user uses the macro STPUTC to add characters to the string. In |
| 756 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 757 | * grown as necessary. When the user is done, she can just leave the |
| 758 | * string there and refer to it using stackblock(). Or she can allocate |
| 759 | * the space for it using grabstackstr(). If it is necessary to allow |
| 760 | * someone else to use the stack temporarily and then continue to grow |
| 761 | * the string, the user should use grabstack to allocate the space, and |
| 762 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 763 | * |
| 764 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 765 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 766 | * is space for at least one character. |
| 767 | */ |
| 768 | static void * |
| 769 | growstackstr(void) |
| 770 | { |
| 771 | size_t len = stackblocksize(); |
| 772 | if (herefd >= 0 && len >= 1024) { |
| 773 | full_write(herefd, stackblock(), len); |
| 774 | return stackblock(); |
| 775 | } |
| 776 | growstackblock(); |
| 777 | return stackblock() + len; |
| 778 | } |
| 779 | |
| 780 | /* |
| 781 | * Called from CHECKSTRSPACE. |
| 782 | */ |
| 783 | static char * |
| 784 | makestrspace(size_t newlen, char *p) |
| 785 | { |
| 786 | size_t len = p - stacknxt; |
| 787 | size_t size = stackblocksize(); |
| 788 | |
| 789 | for (;;) { |
| 790 | size_t nleft; |
| 791 | |
| 792 | size = stackblocksize(); |
| 793 | nleft = size - len; |
| 794 | if (nleft >= newlen) |
| 795 | break; |
| 796 | growstackblock(); |
| 797 | } |
| 798 | return stackblock() + len; |
| 799 | } |
| 800 | |
| 801 | static char * |
| 802 | stack_nputstr(const char *s, size_t n, char *p) |
| 803 | { |
| 804 | p = makestrspace(n, p); |
| 805 | p = memcpy(p, s, n) + n; |
| 806 | return p; |
| 807 | } |
| 808 | |
| 809 | static char * |
| 810 | stack_putstr(const char *s, char *p) |
| 811 | { |
| 812 | return stack_nputstr(s, strlen(s), p); |
| 813 | } |
| 814 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 815 | static char * |
| 816 | _STPUTC(int c, char *p) |
| 817 | { |
| 818 | if (p == sstrend) |
| 819 | p = growstackstr(); |
| 820 | *p++ = c; |
| 821 | return p; |
| 822 | } |
| 823 | |
| 824 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 825 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
| 826 | #define CHECKSTRSPACE(n, p) \ |
| 827 | ({ \ |
| 828 | char *q = (p); \ |
| 829 | size_t l = (n); \ |
| 830 | size_t m = sstrend - q; \ |
| 831 | if (l > m) \ |
| 832 | (p) = makestrspace(l, q); \ |
| 833 | 0; \ |
| 834 | }) |
| 835 | #define USTPUTC(c, p) (*p++ = (c)) |
| 836 | #define STACKSTRNUL(p) ((p) == sstrend ? (p = growstackstr(), *p = '\0') : (*p = '\0')) |
| 837 | #define STUNPUTC(p) (--p) |
| 838 | #define STTOPC(p) p[-1] |
| 839 | #define STADJUST(amount, p) (p += (amount)) |
| 840 | |
| 841 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
| 842 | #define ungrabstackstr(s, p) stunalloc((s)) |
| 843 | #define stackstrend() ((void *)sstrend) |
| 844 | |
| 845 | |
| 846 | /* ============ String helpers */ |
| 847 | |
| 848 | /* |
| 849 | * prefix -- see if pfx is a prefix of string. |
| 850 | */ |
| 851 | static char * |
| 852 | prefix(const char *string, const char *pfx) |
| 853 | { |
| 854 | while (*pfx) { |
| 855 | if (*pfx++ != *string++) |
| 856 | return 0; |
| 857 | } |
| 858 | return (char *) string; |
| 859 | } |
| 860 | |
| 861 | /* |
| 862 | * Check for a valid number. This should be elsewhere. |
| 863 | */ |
| 864 | static int |
| 865 | is_number(const char *p) |
| 866 | { |
| 867 | do { |
| 868 | if (!isdigit(*p)) |
| 869 | return 0; |
| 870 | } while (*++p != '\0'); |
| 871 | return 1; |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * Convert a string of digits to an integer, printing an error message on |
| 876 | * failure. |
| 877 | */ |
| 878 | static int |
| 879 | number(const char *s) |
| 880 | { |
| 881 | if (!is_number(s)) |
| 882 | ash_msg_and_raise_error(illnum, s); |
| 883 | return atoi(s); |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * Produce a possibly single quoted string suitable as input to the shell. |
| 888 | * The return string is allocated on the stack. |
| 889 | */ |
| 890 | static char * |
| 891 | single_quote(const char *s) |
| 892 | { |
| 893 | char *p; |
| 894 | |
| 895 | STARTSTACKSTR(p); |
| 896 | |
| 897 | do { |
| 898 | char *q; |
| 899 | size_t len; |
| 900 | |
| 901 | len = strchrnul(s, '\'') - s; |
| 902 | |
| 903 | q = p = makestrspace(len + 3, p); |
| 904 | |
| 905 | *q++ = '\''; |
| 906 | q = memcpy(q, s, len) + len; |
| 907 | *q++ = '\''; |
| 908 | s += len; |
| 909 | |
| 910 | STADJUST(q - p, p); |
| 911 | |
| 912 | len = strspn(s, "'"); |
| 913 | if (!len) |
| 914 | break; |
| 915 | |
| 916 | q = p = makestrspace(len + 3, p); |
| 917 | |
| 918 | *q++ = '"'; |
| 919 | q = memcpy(q, s, len) + len; |
| 920 | *q++ = '"'; |
| 921 | s += len; |
| 922 | |
| 923 | STADJUST(q - p, p); |
| 924 | } while (*s); |
| 925 | |
| 926 | USTPUTC(0, p); |
| 927 | |
| 928 | return stackblock(); |
| 929 | } |
| 930 | |
| 931 | |
| 932 | /* ============ ... */ |
| 933 | |
| 934 | static char **argptr; /* argument list for builtin commands */ |
| 935 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 936 | static char *optptr; /* used by nextopt */ |
| 937 | |
| 938 | /* |
| 939 | * XXX - should get rid of. have all builtins use getopt(3). the |
| 940 | * library getopt must have the BSD extension static variable "optreset" |
| 941 | * otherwise it can't be used within the shell safely. |
| 942 | * |
| 943 | * Standard option processing (a la getopt) for builtin routines. The |
| 944 | * only argument that is passed to nextopt is the option string; the |
| 945 | * other arguments are unnecessary. It return the character, or '\0' on |
| 946 | * end of input. |
| 947 | */ |
| 948 | static int |
| 949 | nextopt(const char *optstring) |
| 950 | { |
| 951 | char *p; |
| 952 | const char *q; |
| 953 | char c; |
| 954 | |
| 955 | p = optptr; |
| 956 | if (p == NULL || *p == '\0') { |
| 957 | p = *argptr; |
| 958 | if (p == NULL || *p != '-' || *++p == '\0') |
| 959 | return '\0'; |
| 960 | argptr++; |
| 961 | if (LONE_DASH(p)) /* check for "--" */ |
| 962 | return '\0'; |
| 963 | } |
| 964 | c = *p++; |
| 965 | for (q = optstring; *q != c; ) { |
| 966 | if (*q == '\0') |
| 967 | ash_msg_and_raise_error("Illegal option -%c", c); |
| 968 | if (*++q == ':') |
| 969 | q++; |
| 970 | } |
| 971 | if (*++q == ':') { |
| 972 | if (*p == '\0' && (p = *argptr++) == NULL) |
| 973 | ash_msg_and_raise_error("No arg for -%c option", c); |
| 974 | optionarg = p; |
| 975 | p = NULL; |
| 976 | } |
| 977 | optptr = p; |
| 978 | return c; |
| 979 | } |
| 980 | |
| 981 | |
| 982 | /* ============ Variables */ |
| 983 | |
| 984 | /* flags */ |
| 985 | #define VEXPORT 0x01 /* variable is exported */ |
| 986 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 987 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 988 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 989 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 990 | #define VUNSET 0x20 /* the variable is not set */ |
| 991 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 992 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 993 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
| 994 | #ifdef DYNAMIC_VAR |
| 995 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 996 | # else |
| 997 | # define VDYNAMIC 0 |
| 998 | #endif |
| 999 | |
| 1000 | #if ENABLE_LOCALE_SUPPORT |
| 1001 | static void change_lc_all(const char *value); |
| 1002 | static void change_lc_ctype(const char *value); |
| 1003 | #endif |
| 1004 | |
| 1005 | static const char defpathvar[] = "PATH=/usr/local/bin:/usr/bin:/sbin:/bin"; |
| 1006 | #ifdef IFS_BROKEN |
| 1007 | static const char defifsvar[] = "IFS= \t\n"; |
| 1008 | #define defifs (defifsvar + 4) |
| 1009 | #else |
| 1010 | static const char defifs[] = " \t\n"; |
| 1011 | #endif |
| 1012 | |
| 1013 | struct var { |
| 1014 | struct var *next; /* next entry in hash list */ |
| 1015 | int flags; /* flags are defined above */ |
| 1016 | const char *text; /* name=value */ |
| 1017 | void (*func)(const char *); /* function to be called when */ |
| 1018 | /* the variable gets set/unset */ |
| 1019 | }; |
| 1020 | |
| 1021 | struct localvar { |
| 1022 | struct localvar *next; /* next local variable in list */ |
| 1023 | struct var *vp; /* the variable that was made local */ |
| 1024 | int flags; /* saved flags */ |
| 1025 | const char *text; /* saved text */ |
| 1026 | }; |
| 1027 | |
| 1028 | /* Forward decls for varinit[] */ |
| 1029 | #if ENABLE_ASH_MAIL |
| 1030 | static void chkmail(void); |
| 1031 | static void changemail(const char *); |
| 1032 | #endif |
| 1033 | static void changepath(const char *); |
| 1034 | #if ENABLE_ASH_GETOPTS |
| 1035 | static void getoptsreset(const char *); |
| 1036 | #endif |
| 1037 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 1038 | static void change_random(const char *); |
| 1039 | #endif |
| 1040 | |
| 1041 | static struct var varinit[] = { |
| 1042 | #ifdef IFS_BROKEN |
| 1043 | { 0, VSTRFIXED|VTEXTFIXED, defifsvar, 0 }, |
| 1044 | #else |
| 1045 | { 0, VSTRFIXED|VTEXTFIXED|VUNSET, "IFS\0", 0 }, |
| 1046 | #endif |
| 1047 | |
| 1048 | #if ENABLE_ASH_MAIL |
| 1049 | { 0, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL\0", changemail }, |
| 1050 | { 0, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH\0", changemail }, |
| 1051 | #endif |
| 1052 | |
| 1053 | { 0, VSTRFIXED|VTEXTFIXED, defpathvar, changepath }, |
| 1054 | { 0, VSTRFIXED|VTEXTFIXED, "PS1=$ ", 0 }, |
| 1055 | { 0, VSTRFIXED|VTEXTFIXED, "PS2=> ", 0 }, |
| 1056 | { 0, VSTRFIXED|VTEXTFIXED, "PS4=+ ", 0 }, |
| 1057 | #if ENABLE_ASH_GETOPTS |
| 1058 | { 0, VSTRFIXED|VTEXTFIXED, "OPTIND=1", getoptsreset }, |
| 1059 | #endif |
| 1060 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 1061 | {0, VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM\0", change_random }, |
| 1062 | #endif |
| 1063 | #if ENABLE_LOCALE_SUPPORT |
| 1064 | {0, VSTRFIXED | VTEXTFIXED | VUNSET, "LC_ALL\0", change_lc_all }, |
| 1065 | {0, VSTRFIXED | VTEXTFIXED | VUNSET, "LC_CTYPE\0", change_lc_ctype }, |
| 1066 | #endif |
| 1067 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
| 1068 | {0, VSTRFIXED | VTEXTFIXED | VUNSET, "HISTFILE\0", NULL }, |
| 1069 | #endif |
| 1070 | }; |
| 1071 | |
| 1072 | #define vifs varinit[0] |
| 1073 | #if ENABLE_ASH_MAIL |
| 1074 | #define vmail (&vifs)[1] |
| 1075 | #define vmpath (&vmail)[1] |
| 1076 | #else |
| 1077 | #define vmpath vifs |
| 1078 | #endif |
| 1079 | #define vpath (&vmpath)[1] |
| 1080 | #define vps1 (&vpath)[1] |
| 1081 | #define vps2 (&vps1)[1] |
| 1082 | #define vps4 (&vps2)[1] |
| 1083 | #define voptind (&vps4)[1] |
| 1084 | #if ENABLE_ASH_GETOPTS |
| 1085 | #define vrandom (&voptind)[1] |
| 1086 | #else |
| 1087 | #define vrandom (&vps4)[1] |
| 1088 | #endif |
| 1089 | #define defpath (defpathvar + 5) |
| 1090 | |
| 1091 | /* |
| 1092 | * The following macros access the values of the above variables. |
| 1093 | * They have to skip over the name. They return the null string |
| 1094 | * for unset variables. |
| 1095 | */ |
| 1096 | #define ifsval() (vifs.text + 4) |
| 1097 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
| 1098 | #define mailval() (vmail.text + 5) |
| 1099 | #define mpathval() (vmpath.text + 9) |
| 1100 | #define pathval() (vpath.text + 5) |
| 1101 | #define ps1val() (vps1.text + 4) |
| 1102 | #define ps2val() (vps2.text + 4) |
| 1103 | #define ps4val() (vps4.text + 4) |
| 1104 | #define optindval() (voptind.text + 7) |
| 1105 | |
| 1106 | #define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1107 | |
| 1108 | static struct var **hashvar(const char *); |
| 1109 | |
| 1110 | static int loopnest; /* current loop nesting level */ |
| 1111 | |
| 1112 | /* |
| 1113 | * The parsefile structure pointed to by the global variable parsefile |
| 1114 | * contains information about the current file being read. |
| 1115 | */ |
| 1116 | struct redirtab { |
| 1117 | struct redirtab *next; |
| 1118 | int renamed[10]; |
| 1119 | int nullredirs; |
| 1120 | }; |
| 1121 | |
| 1122 | static struct redirtab *redirlist; |
| 1123 | static int nullredirs; |
| 1124 | |
| 1125 | extern char **environ; |
| 1126 | |
| 1127 | static int preverrout_fd; /* save fd2 before print debug if xflag is set. */ |
| 1128 | |
| 1129 | struct shparam { |
| 1130 | int nparam; /* # of positional parameters (without $0) */ |
| 1131 | unsigned char malloc; /* if parameter list dynamically allocated */ |
| 1132 | char **p; /* parameter list */ |
| 1133 | #if ENABLE_ASH_GETOPTS |
| 1134 | int optind; /* next parameter to be processed by getopts */ |
| 1135 | int optoff; /* used by getopts */ |
| 1136 | #endif |
| 1137 | }; |
| 1138 | |
| 1139 | static struct shparam shellparam; /* $@ current positional parameters */ |
| 1140 | |
| 1141 | #define VTABSIZE 39 |
| 1142 | |
| 1143 | static struct var *vartab[VTABSIZE]; |
| 1144 | |
| 1145 | #if ENABLE_ASH_GETOPTS |
| 1146 | static void |
| 1147 | getoptsreset(const char *value) |
| 1148 | { |
| 1149 | shellparam.optind = number(value); |
| 1150 | shellparam.optoff = -1; |
| 1151 | } |
| 1152 | #endif |
| 1153 | |
| 1154 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1155 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1156 | |
| 1157 | /* |
| 1158 | * Return of a legal variable name (a letter or underscore followed by zero or |
| 1159 | * more letters, underscores, and digits). |
| 1160 | */ |
| 1161 | static char * |
| 1162 | endofname(const char *name) |
| 1163 | { |
| 1164 | char *p; |
| 1165 | |
| 1166 | p = (char *) name; |
| 1167 | if (!is_name(*p)) |
| 1168 | return p; |
| 1169 | while (*++p) { |
| 1170 | if (!is_in_name(*p)) |
| 1171 | break; |
| 1172 | } |
| 1173 | return p; |
| 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | * Compares two strings up to the first = or '\0'. The first |
| 1178 | * string must be terminated by '='; the second may be terminated by |
| 1179 | * either '=' or '\0'. |
| 1180 | */ |
| 1181 | static int |
| 1182 | varcmp(const char *p, const char *q) |
| 1183 | { |
| 1184 | int c, d; |
| 1185 | |
| 1186 | while ((c = *p) == (d = *q)) { |
| 1187 | if (!c || c == '=') |
| 1188 | goto out; |
| 1189 | p++; |
| 1190 | q++; |
| 1191 | } |
| 1192 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame^] | 1193 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1194 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame^] | 1195 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1196 | out: |
| 1197 | return c - d; |
| 1198 | } |
| 1199 | |
| 1200 | static int |
| 1201 | varequal(const char *a, const char *b) |
| 1202 | { |
| 1203 | return !varcmp(a, b); |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * Find the appropriate entry in the hash table from the name. |
| 1208 | */ |
| 1209 | static struct var ** |
| 1210 | hashvar(const char *p) |
| 1211 | { |
| 1212 | unsigned hashval; |
| 1213 | |
| 1214 | hashval = ((unsigned char) *p) << 4; |
| 1215 | while (*p && *p != '=') |
| 1216 | hashval += (unsigned char) *p++; |
| 1217 | return &vartab[hashval % VTABSIZE]; |
| 1218 | } |
| 1219 | |
| 1220 | static int |
| 1221 | vpcmp(const void *a, const void *b) |
| 1222 | { |
| 1223 | return varcmp(*(const char **)a, *(const char **)b); |
| 1224 | } |
| 1225 | |
| 1226 | /* |
| 1227 | * This routine initializes the builtin variables. |
| 1228 | */ |
| 1229 | static void |
| 1230 | initvar(void) |
| 1231 | { |
| 1232 | struct var *vp; |
| 1233 | struct var *end; |
| 1234 | struct var **vpp; |
| 1235 | |
| 1236 | /* |
| 1237 | * PS1 depends on uid |
| 1238 | */ |
| 1239 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 1240 | vps1.text = "PS1=\\w \\$ "; |
| 1241 | #else |
| 1242 | if (!geteuid()) |
| 1243 | vps1.text = "PS1=# "; |
| 1244 | #endif |
| 1245 | vp = varinit; |
| 1246 | end = vp + sizeof(varinit) / sizeof(varinit[0]); |
| 1247 | do { |
| 1248 | vpp = hashvar(vp->text); |
| 1249 | vp->next = *vpp; |
| 1250 | *vpp = vp; |
| 1251 | } while (++vp < end); |
| 1252 | } |
| 1253 | |
| 1254 | static struct var ** |
| 1255 | findvar(struct var **vpp, const char *name) |
| 1256 | { |
| 1257 | for (; *vpp; vpp = &(*vpp)->next) { |
| 1258 | if (varequal((*vpp)->text, name)) { |
| 1259 | break; |
| 1260 | } |
| 1261 | } |
| 1262 | return vpp; |
| 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * Find the value of a variable. Returns NULL if not set. |
| 1267 | */ |
| 1268 | static char * |
| 1269 | lookupvar(const char *name) |
| 1270 | { |
| 1271 | struct var *v; |
| 1272 | |
| 1273 | v = *findvar(hashvar(name), name); |
| 1274 | if (v) { |
| 1275 | #ifdef DYNAMIC_VAR |
| 1276 | /* |
| 1277 | * Dynamic variables are implemented roughly the same way they are |
| 1278 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 1279 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 1280 | * lookup will no longer happen at that point. -- PFM. |
| 1281 | */ |
| 1282 | if ((v->flags & VDYNAMIC)) |
| 1283 | (*v->func)(NULL); |
| 1284 | #endif |
| 1285 | if (!(v->flags & VUNSET)) |
| 1286 | return strchrnul(v->text, '=') + 1; |
| 1287 | } |
| 1288 | return NULL; |
| 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * Search the environment of a builtin command. |
| 1293 | */ |
| 1294 | static char * |
| 1295 | bltinlookup(const char *name) |
| 1296 | { |
| 1297 | struct strlist *sp; |
| 1298 | |
| 1299 | for (sp = cmdenviron; sp; sp = sp->next) { |
| 1300 | if (varequal(sp->text, name)) |
| 1301 | return strchrnul(sp->text, '=') + 1; |
| 1302 | } |
| 1303 | return lookupvar(name); |
| 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | * Same as setvar except that the variable and value are passed in |
| 1308 | * the first argument as name=value. Since the first argument will |
| 1309 | * be actually stored in the table, it should not be a string that |
| 1310 | * will go away. |
| 1311 | * Called with interrupts off. |
| 1312 | */ |
| 1313 | static void |
| 1314 | setvareq(char *s, int flags) |
| 1315 | { |
| 1316 | struct var *vp, **vpp; |
| 1317 | |
| 1318 | vpp = hashvar(s); |
| 1319 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
| 1320 | vp = *findvar(vpp, s); |
| 1321 | if (vp) { |
| 1322 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 1323 | const char *n; |
| 1324 | |
| 1325 | if (flags & VNOSAVE) |
| 1326 | free(s); |
| 1327 | n = vp->text; |
| 1328 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 1329 | } |
| 1330 | |
| 1331 | if (flags & VNOSET) |
| 1332 | return; |
| 1333 | |
| 1334 | if (vp->func && (flags & VNOFUNC) == 0) |
| 1335 | (*vp->func)(strchrnul(s, '=') + 1); |
| 1336 | |
| 1337 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 1338 | free((char*)vp->text); |
| 1339 | |
| 1340 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
| 1341 | } else { |
| 1342 | if (flags & VNOSET) |
| 1343 | return; |
| 1344 | /* not found */ |
| 1345 | vp = ckmalloc(sizeof(*vp)); |
| 1346 | vp->next = *vpp; |
| 1347 | vp->func = NULL; |
| 1348 | *vpp = vp; |
| 1349 | } |
| 1350 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 1351 | s = ckstrdup(s); |
| 1352 | vp->text = s; |
| 1353 | vp->flags = flags; |
| 1354 | } |
| 1355 | |
| 1356 | /* |
| 1357 | * Set the value of a variable. The flags argument is ored with the |
| 1358 | * flags of the variable. If val is NULL, the variable is unset. |
| 1359 | */ |
| 1360 | static void |
| 1361 | setvar(const char *name, const char *val, int flags) |
| 1362 | { |
| 1363 | char *p, *q; |
| 1364 | size_t namelen; |
| 1365 | char *nameeq; |
| 1366 | size_t vallen; |
| 1367 | |
| 1368 | q = endofname(name); |
| 1369 | p = strchrnul(q, '='); |
| 1370 | namelen = p - name; |
| 1371 | if (!namelen || p != q) |
| 1372 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 1373 | vallen = 0; |
| 1374 | if (val == NULL) { |
| 1375 | flags |= VUNSET; |
| 1376 | } else { |
| 1377 | vallen = strlen(val); |
| 1378 | } |
| 1379 | INT_OFF; |
| 1380 | nameeq = ckmalloc(namelen + vallen + 2); |
| 1381 | p = memcpy(nameeq, name, namelen) + namelen; |
| 1382 | if (val) { |
| 1383 | *p++ = '='; |
| 1384 | p = memcpy(p, val, vallen) + vallen; |
| 1385 | } |
| 1386 | *p = '\0'; |
| 1387 | setvareq(nameeq, flags | VNOSAVE); |
| 1388 | INT_ON; |
| 1389 | } |
| 1390 | |
| 1391 | #if ENABLE_ASH_GETOPTS |
| 1392 | /* |
| 1393 | * Safe version of setvar, returns 1 on success 0 on failure. |
| 1394 | */ |
| 1395 | static int |
| 1396 | setvarsafe(const char *name, const char *val, int flags) |
| 1397 | { |
| 1398 | int err; |
| 1399 | volatile int saveint; |
| 1400 | struct jmploc *volatile savehandler = exception_handler; |
| 1401 | struct jmploc jmploc; |
| 1402 | |
| 1403 | SAVE_INT(saveint); |
| 1404 | if (setjmp(jmploc.loc)) |
| 1405 | err = 1; |
| 1406 | else { |
| 1407 | exception_handler = &jmploc; |
| 1408 | setvar(name, val, flags); |
| 1409 | err = 0; |
| 1410 | } |
| 1411 | exception_handler = savehandler; |
| 1412 | RESTORE_INT(saveint); |
| 1413 | return err; |
| 1414 | } |
| 1415 | #endif |
| 1416 | |
| 1417 | /* |
| 1418 | * Unset the specified variable. |
| 1419 | */ |
| 1420 | static int |
| 1421 | unsetvar(const char *s) |
| 1422 | { |
| 1423 | struct var **vpp; |
| 1424 | struct var *vp; |
| 1425 | int retval; |
| 1426 | |
| 1427 | vpp = findvar(hashvar(s), s); |
| 1428 | vp = *vpp; |
| 1429 | retval = 2; |
| 1430 | if (vp) { |
| 1431 | int flags = vp->flags; |
| 1432 | |
| 1433 | retval = 1; |
| 1434 | if (flags & VREADONLY) |
| 1435 | goto out; |
| 1436 | #ifdef DYNAMIC_VAR |
| 1437 | vp->flags &= ~VDYNAMIC; |
| 1438 | #endif |
| 1439 | if (flags & VUNSET) |
| 1440 | goto ok; |
| 1441 | if ((flags & VSTRFIXED) == 0) { |
| 1442 | INT_OFF; |
| 1443 | if ((flags & (VTEXTFIXED|VSTACK)) == 0) |
| 1444 | free((char*)vp->text); |
| 1445 | *vpp = vp->next; |
| 1446 | free(vp); |
| 1447 | INT_ON; |
| 1448 | } else { |
| 1449 | setvar(s, 0, 0); |
| 1450 | vp->flags &= ~VEXPORT; |
| 1451 | } |
| 1452 | ok: |
| 1453 | retval = 0; |
| 1454 | } |
| 1455 | out: |
| 1456 | return retval; |
| 1457 | } |
| 1458 | |
| 1459 | /* |
| 1460 | * Process a linked list of variable assignments. |
| 1461 | */ |
| 1462 | static void |
| 1463 | listsetvar(struct strlist *list_set_var, int flags) |
| 1464 | { |
| 1465 | struct strlist *lp = list_set_var; |
| 1466 | |
| 1467 | if (!lp) |
| 1468 | return; |
| 1469 | INT_OFF; |
| 1470 | do { |
| 1471 | setvareq(lp->text, flags); |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame^] | 1472 | lp = lp->next; |
| 1473 | } while (lp); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1474 | INT_ON; |
| 1475 | } |
| 1476 | |
| 1477 | /* |
| 1478 | * Generate a list of variables satisfying the given conditions. |
| 1479 | */ |
| 1480 | static char ** |
| 1481 | listvars(int on, int off, char ***end) |
| 1482 | { |
| 1483 | struct var **vpp; |
| 1484 | struct var *vp; |
| 1485 | char **ep; |
| 1486 | int mask; |
| 1487 | |
| 1488 | STARTSTACKSTR(ep); |
| 1489 | vpp = vartab; |
| 1490 | mask = on | off; |
| 1491 | do { |
| 1492 | for (vp = *vpp; vp; vp = vp->next) { |
| 1493 | if ((vp->flags & mask) == on) { |
| 1494 | if (ep == stackstrend()) |
| 1495 | ep = growstackstr(); |
| 1496 | *ep++ = (char *) vp->text; |
| 1497 | } |
| 1498 | } |
| 1499 | } while (++vpp < vartab + VTABSIZE); |
| 1500 | if (ep == stackstrend()) |
| 1501 | ep = growstackstr(); |
| 1502 | if (end) |
| 1503 | *end = ep; |
| 1504 | *ep++ = NULL; |
| 1505 | return grabstackstr(ep); |
| 1506 | } |
| 1507 | |
| 1508 | |
| 1509 | /* ============ Path search helper |
| 1510 | * |
| 1511 | * The variable path (passed by reference) should be set to the start |
| 1512 | * of the path before the first call; padvance will update |
| 1513 | * this value as it proceeds. Successive calls to padvance will return |
| 1514 | * the possible path expansions in sequence. If an option (indicated by |
| 1515 | * a percent sign) appears in the path entry then the global variable |
| 1516 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 1517 | * NULL. |
| 1518 | */ |
| 1519 | static const char *pathopt; /* set by padvance */ |
| 1520 | |
| 1521 | static char * |
| 1522 | padvance(const char **path, const char *name) |
| 1523 | { |
| 1524 | const char *p; |
| 1525 | char *q; |
| 1526 | const char *start; |
| 1527 | size_t len; |
| 1528 | |
| 1529 | if (*path == NULL) |
| 1530 | return NULL; |
| 1531 | start = *path; |
| 1532 | for (p = start; *p && *p != ':' && *p != '%'; p++); |
| 1533 | len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ |
| 1534 | while (stackblocksize() < len) |
| 1535 | growstackblock(); |
| 1536 | q = stackblock(); |
| 1537 | if (p != start) { |
| 1538 | memcpy(q, start, p - start); |
| 1539 | q += p - start; |
| 1540 | *q++ = '/'; |
| 1541 | } |
| 1542 | strcpy(q, name); |
| 1543 | pathopt = NULL; |
| 1544 | if (*p == '%') { |
| 1545 | pathopt = ++p; |
| 1546 | while (*p && *p != ':') p++; |
| 1547 | } |
| 1548 | if (*p == ':') |
| 1549 | *path = p + 1; |
| 1550 | else |
| 1551 | *path = NULL; |
| 1552 | return stalloc(len); |
| 1553 | } |
| 1554 | |
| 1555 | |
| 1556 | /* ============ Prompt */ |
| 1557 | |
| 1558 | static int doprompt; /* if set, prompt the user */ |
| 1559 | static int needprompt; /* true if interactive and at start of line */ |
| 1560 | |
| 1561 | #if ENABLE_FEATURE_EDITING |
| 1562 | static line_input_t *line_input_state; |
| 1563 | static const char *cmdedit_prompt; |
| 1564 | static void |
| 1565 | putprompt(const char *s) |
| 1566 | { |
| 1567 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 1568 | free((char*)cmdedit_prompt); |
| 1569 | cmdedit_prompt = xstrdup(s); |
| 1570 | return; |
| 1571 | } |
| 1572 | cmdedit_prompt = s; |
| 1573 | } |
| 1574 | #else |
| 1575 | static void |
| 1576 | putprompt(const char *s) |
| 1577 | { |
| 1578 | out2str(s); |
| 1579 | } |
| 1580 | #endif |
| 1581 | |
| 1582 | #if ENABLE_ASH_EXPAND_PRMT |
| 1583 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
| 1584 | static const char *expandstr(const char *ps); |
| 1585 | #else |
| 1586 | #define expandstr(s) s |
| 1587 | #endif |
| 1588 | |
| 1589 | static void |
| 1590 | setprompt(int whichprompt) |
| 1591 | { |
| 1592 | const char *prompt; |
| 1593 | #if ENABLE_ASH_EXPAND_PRMT |
| 1594 | struct stackmark smark; |
| 1595 | #endif |
| 1596 | |
| 1597 | needprompt = 0; |
| 1598 | |
| 1599 | switch (whichprompt) { |
| 1600 | case 1: |
| 1601 | prompt = ps1val(); |
| 1602 | break; |
| 1603 | case 2: |
| 1604 | prompt = ps2val(); |
| 1605 | break; |
| 1606 | default: /* 0 */ |
| 1607 | prompt = nullstr; |
| 1608 | } |
| 1609 | #if ENABLE_ASH_EXPAND_PRMT |
| 1610 | setstackmark(&smark); |
| 1611 | stalloc(stackblocksize()); |
| 1612 | #endif |
| 1613 | putprompt(expandstr(prompt)); |
| 1614 | #if ENABLE_ASH_EXPAND_PRMT |
| 1615 | popstackmark(&smark); |
| 1616 | #endif |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | /* ============ The cd and pwd commands */ |
| 1621 | |
| 1622 | #define CD_PHYSICAL 1 |
| 1623 | #define CD_PRINT 2 |
| 1624 | |
| 1625 | static int docd(const char *, int); |
| 1626 | |
| 1627 | static char *curdir = nullstr; /* current working directory */ |
| 1628 | static char *physdir = nullstr; /* physical working directory */ |
| 1629 | |
| 1630 | static int |
| 1631 | cdopt(void) |
| 1632 | { |
| 1633 | int flags = 0; |
| 1634 | int i, j; |
| 1635 | |
| 1636 | j = 'L'; |
| 1637 | while ((i = nextopt("LP"))) { |
| 1638 | if (i != j) { |
| 1639 | flags ^= CD_PHYSICAL; |
| 1640 | j = i; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | return flags; |
| 1645 | } |
| 1646 | |
| 1647 | /* |
| 1648 | * Update curdir (the name of the current directory) in response to a |
| 1649 | * cd command. |
| 1650 | */ |
| 1651 | static const char * |
| 1652 | updatepwd(const char *dir) |
| 1653 | { |
| 1654 | char *new; |
| 1655 | char *p; |
| 1656 | char *cdcomppath; |
| 1657 | const char *lim; |
| 1658 | |
| 1659 | cdcomppath = ststrdup(dir); |
| 1660 | STARTSTACKSTR(new); |
| 1661 | if (*dir != '/') { |
| 1662 | if (curdir == nullstr) |
| 1663 | return 0; |
| 1664 | new = stack_putstr(curdir, new); |
| 1665 | } |
| 1666 | new = makestrspace(strlen(dir) + 2, new); |
| 1667 | lim = stackblock() + 1; |
| 1668 | if (*dir != '/') { |
| 1669 | if (new[-1] != '/') |
| 1670 | USTPUTC('/', new); |
| 1671 | if (new > lim && *lim == '/') |
| 1672 | lim++; |
| 1673 | } else { |
| 1674 | USTPUTC('/', new); |
| 1675 | cdcomppath++; |
| 1676 | if (dir[1] == '/' && dir[2] != '/') { |
| 1677 | USTPUTC('/', new); |
| 1678 | cdcomppath++; |
| 1679 | lim++; |
| 1680 | } |
| 1681 | } |
| 1682 | p = strtok(cdcomppath, "/"); |
| 1683 | while (p) { |
| 1684 | switch (*p) { |
| 1685 | case '.': |
| 1686 | if (p[1] == '.' && p[2] == '\0') { |
| 1687 | while (new > lim) { |
| 1688 | STUNPUTC(new); |
| 1689 | if (new[-1] == '/') |
| 1690 | break; |
| 1691 | } |
| 1692 | break; |
| 1693 | } else if (p[1] == '\0') |
| 1694 | break; |
| 1695 | /* fall through */ |
| 1696 | default: |
| 1697 | new = stack_putstr(p, new); |
| 1698 | USTPUTC('/', new); |
| 1699 | } |
| 1700 | p = strtok(0, "/"); |
| 1701 | } |
| 1702 | if (new > lim) |
| 1703 | STUNPUTC(new); |
| 1704 | *new = 0; |
| 1705 | return stackblock(); |
| 1706 | } |
| 1707 | |
| 1708 | /* |
| 1709 | * Find out what the current directory is. If we already know the current |
| 1710 | * directory, this routine returns immediately. |
| 1711 | */ |
| 1712 | static char * |
| 1713 | getpwd(void) |
| 1714 | { |
| 1715 | char *dir = getcwd(0, 0); |
| 1716 | return dir ? dir : nullstr; |
| 1717 | } |
| 1718 | |
| 1719 | static void |
| 1720 | setpwd(const char *val, int setold) |
| 1721 | { |
| 1722 | char *oldcur, *dir; |
| 1723 | |
| 1724 | oldcur = dir = curdir; |
| 1725 | |
| 1726 | if (setold) { |
| 1727 | setvar("OLDPWD", oldcur, VEXPORT); |
| 1728 | } |
| 1729 | INT_OFF; |
| 1730 | if (physdir != nullstr) { |
| 1731 | if (physdir != oldcur) |
| 1732 | free(physdir); |
| 1733 | physdir = nullstr; |
| 1734 | } |
| 1735 | if (oldcur == val || !val) { |
| 1736 | char *s = getpwd(); |
| 1737 | physdir = s; |
| 1738 | if (!val) |
| 1739 | dir = s; |
| 1740 | } else |
| 1741 | dir = ckstrdup(val); |
| 1742 | if (oldcur != dir && oldcur != nullstr) { |
| 1743 | free(oldcur); |
| 1744 | } |
| 1745 | curdir = dir; |
| 1746 | INT_ON; |
| 1747 | setvar("PWD", dir, VEXPORT); |
| 1748 | } |
| 1749 | |
| 1750 | static void hashcd(void); |
| 1751 | |
| 1752 | /* |
| 1753 | * Actually do the chdir. We also call hashcd to let the routines in exec.c |
| 1754 | * know that the current directory has changed. |
| 1755 | */ |
| 1756 | static int |
| 1757 | docd(const char *dest, int flags) |
| 1758 | { |
| 1759 | const char *dir = 0; |
| 1760 | int err; |
| 1761 | |
| 1762 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 1763 | |
| 1764 | INT_OFF; |
| 1765 | if (!(flags & CD_PHYSICAL)) { |
| 1766 | dir = updatepwd(dest); |
| 1767 | if (dir) |
| 1768 | dest = dir; |
| 1769 | } |
| 1770 | err = chdir(dest); |
| 1771 | if (err) |
| 1772 | goto out; |
| 1773 | setpwd(dir, 1); |
| 1774 | hashcd(); |
| 1775 | out: |
| 1776 | INT_ON; |
| 1777 | return err; |
| 1778 | } |
| 1779 | |
| 1780 | static int |
| 1781 | cdcmd(int argc, char **argv) |
| 1782 | { |
| 1783 | const char *dest; |
| 1784 | const char *path; |
| 1785 | const char *p; |
| 1786 | char c; |
| 1787 | struct stat statb; |
| 1788 | int flags; |
| 1789 | |
| 1790 | flags = cdopt(); |
| 1791 | dest = *argptr; |
| 1792 | if (!dest) |
| 1793 | dest = bltinlookup(homestr); |
| 1794 | else if (LONE_DASH(dest)) { |
| 1795 | dest = bltinlookup("OLDPWD"); |
| 1796 | flags |= CD_PRINT; |
| 1797 | } |
| 1798 | if (!dest) |
| 1799 | dest = nullstr; |
| 1800 | if (*dest == '/') |
| 1801 | goto step7; |
| 1802 | if (*dest == '.') { |
| 1803 | c = dest[1]; |
| 1804 | dotdot: |
| 1805 | switch (c) { |
| 1806 | case '\0': |
| 1807 | case '/': |
| 1808 | goto step6; |
| 1809 | case '.': |
| 1810 | c = dest[2]; |
| 1811 | if (c != '.') |
| 1812 | goto dotdot; |
| 1813 | } |
| 1814 | } |
| 1815 | if (!*dest) |
| 1816 | dest = "."; |
| 1817 | path = bltinlookup("CDPATH"); |
| 1818 | if (!path) { |
| 1819 | step6: |
| 1820 | step7: |
| 1821 | p = dest; |
| 1822 | goto docd; |
| 1823 | } |
| 1824 | do { |
| 1825 | c = *path; |
| 1826 | p = padvance(&path, dest); |
| 1827 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 1828 | if (c && c != ':') |
| 1829 | flags |= CD_PRINT; |
| 1830 | docd: |
| 1831 | if (!docd(p, flags)) |
| 1832 | goto out; |
| 1833 | break; |
| 1834 | } |
| 1835 | } while (path); |
| 1836 | ash_msg_and_raise_error("can't cd to %s", dest); |
| 1837 | /* NOTREACHED */ |
| 1838 | out: |
| 1839 | if (flags & CD_PRINT) |
| 1840 | out1fmt(snlfmt, curdir); |
| 1841 | return 0; |
| 1842 | } |
| 1843 | |
| 1844 | static int |
| 1845 | pwdcmd(int argc, char **argv) |
| 1846 | { |
| 1847 | int flags; |
| 1848 | const char *dir = curdir; |
| 1849 | |
| 1850 | flags = cdopt(); |
| 1851 | if (flags) { |
| 1852 | if (physdir == nullstr) |
| 1853 | setpwd(dir, 0); |
| 1854 | dir = physdir; |
| 1855 | } |
| 1856 | out1fmt(snlfmt, dir); |
| 1857 | return 0; |
| 1858 | } |
| 1859 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1860 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1861 | /* ============ Unsorted yet */ |
| 1862 | |
| 1863 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1864 | /* expand.h */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1865 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1866 | struct arglist { |
| 1867 | struct strlist *list; |
| 1868 | struct strlist **lastp; |
| 1869 | }; |
| 1870 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1871 | /* |
| 1872 | * expandarg() flags |
| 1873 | */ |
| 1874 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 1875 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 1876 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 1877 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 1878 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
| 1879 | #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */ |
| 1880 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ |
| 1881 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ |
| 1882 | #define EXP_QWORD 0x100 /* expand word in quoted parameter expansion */ |
| 1883 | |
| 1884 | |
| 1885 | union node; |
| 1886 | static void expandarg(union node *, struct arglist *, int); |
| 1887 | #define rmescapes(p) _rmescapes((p), 0) |
| 1888 | static char *_rmescapes(char *, int); |
| 1889 | static int casematch(union node *, char *); |
| 1890 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 1891 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1892 | static void expari(int); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1893 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1894 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1895 | /* eval.h */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1896 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1897 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1898 | |
| 1899 | struct backcmd { /* result of evalbackcmd */ |
| 1900 | int fd; /* file descriptor to read from */ |
| 1901 | char *buf; /* buffer */ |
| 1902 | int nleft; /* number of chars in buffer */ |
| 1903 | struct job *jp; /* job structure for command */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1904 | }; |
| 1905 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 1906 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1907 | * This file was generated by the mknodes program. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 1908 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1909 | |
| 1910 | #define NCMD 0 |
| 1911 | #define NPIPE 1 |
| 1912 | #define NREDIR 2 |
| 1913 | #define NBACKGND 3 |
| 1914 | #define NSUBSHELL 4 |
| 1915 | #define NAND 5 |
| 1916 | #define NOR 6 |
| 1917 | #define NSEMI 7 |
| 1918 | #define NIF 8 |
| 1919 | #define NWHILE 9 |
| 1920 | #define NUNTIL 10 |
| 1921 | #define NFOR 11 |
| 1922 | #define NCASE 12 |
| 1923 | #define NCLIST 13 |
| 1924 | #define NDEFUN 14 |
| 1925 | #define NARG 15 |
| 1926 | #define NTO 16 |
| 1927 | #define NCLOBBER 17 |
| 1928 | #define NFROM 18 |
| 1929 | #define NFROMTO 19 |
| 1930 | #define NAPPEND 20 |
| 1931 | #define NTOFD 21 |
| 1932 | #define NFROMFD 22 |
| 1933 | #define NHERE 23 |
| 1934 | #define NXHERE 24 |
| 1935 | #define NNOT 25 |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 1936 | |
| 1937 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1938 | struct ncmd { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 1939 | int type; |
| 1940 | union node *assign; |
| 1941 | union node *args; |
| 1942 | union node *redirect; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 1943 | }; |
| 1944 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1945 | struct npipe { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 1946 | int type; |
| 1947 | int backgnd; |
| 1948 | struct nodelist *cmdlist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1949 | }; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 1950 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1951 | struct nredir { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 1952 | int type; |
| 1953 | union node *n; |
| 1954 | union node *redirect; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1955 | }; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 1956 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1957 | struct nbinary { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 1958 | int type; |
| 1959 | union node *ch1; |
| 1960 | union node *ch2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1961 | }; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1962 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1963 | struct nif { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 1964 | int type; |
| 1965 | union node *test; |
| 1966 | union node *ifpart; |
| 1967 | union node *elsepart; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1968 | }; |
| 1969 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1970 | struct nfor { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 1971 | int type; |
| 1972 | union node *args; |
| 1973 | union node *body; |
| 1974 | char *var; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1975 | }; |
| 1976 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1977 | struct ncase { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 1978 | int type; |
| 1979 | union node *expr; |
| 1980 | union node *cases; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1981 | }; |
| 1982 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1983 | struct nclist { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 1984 | int type; |
| 1985 | union node *next; |
| 1986 | union node *pattern; |
| 1987 | union node *body; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1988 | }; |
| 1989 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1990 | struct narg { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 1991 | int type; |
| 1992 | union node *next; |
| 1993 | char *text; |
| 1994 | struct nodelist *backquote; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1995 | }; |
| 1996 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1997 | struct nfile { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 1998 | int type; |
| 1999 | union node *next; |
| 2000 | int fd; |
| 2001 | union node *fname; |
| 2002 | char *expfname; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2003 | }; |
| 2004 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2005 | struct ndup { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 2006 | int type; |
| 2007 | union node *next; |
| 2008 | int fd; |
| 2009 | int dupfd; |
| 2010 | union node *vname; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2011 | }; |
| 2012 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2013 | struct nhere { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 2014 | int type; |
| 2015 | union node *next; |
| 2016 | int fd; |
| 2017 | union node *doc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2018 | }; |
| 2019 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2020 | struct nnot { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 2021 | int type; |
| 2022 | union node *com; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2023 | }; |
| 2024 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2025 | union node { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 2026 | int type; |
| 2027 | struct ncmd ncmd; |
| 2028 | struct npipe npipe; |
| 2029 | struct nredir nredir; |
| 2030 | struct nbinary nbinary; |
| 2031 | struct nif nif; |
| 2032 | struct nfor nfor; |
| 2033 | struct ncase ncase; |
| 2034 | struct nclist nclist; |
| 2035 | struct narg narg; |
| 2036 | struct nfile nfile; |
| 2037 | struct ndup ndup; |
| 2038 | struct nhere nhere; |
| 2039 | struct nnot nnot; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2040 | }; |
| 2041 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2042 | struct nodelist { |
| 2043 | struct nodelist *next; |
| 2044 | union node *n; |
| 2045 | }; |
| 2046 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2047 | struct funcnode { |
| 2048 | int count; |
| 2049 | union node n; |
| 2050 | }; |
| 2051 | |
| 2052 | |
| 2053 | static void freefunc(struct funcnode *); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2054 | /* parser.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2055 | |
| 2056 | /* control characters in argument strings */ |
| 2057 | #define CTL_FIRST '\201' /* first 'special' character */ |
| 2058 | #define CTLESC '\201' /* escape next character */ |
| 2059 | #define CTLVAR '\202' /* variable defn */ |
| 2060 | #define CTLENDVAR '\203' |
| 2061 | #define CTLBACKQ '\204' |
| 2062 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ |
| 2063 | /* CTLBACKQ | CTLQUOTE == '\205' */ |
| 2064 | #define CTLARI '\206' /* arithmetic expression */ |
| 2065 | #define CTLENDARI '\207' |
| 2066 | #define CTLQUOTEMARK '\210' |
| 2067 | #define CTL_LAST '\210' /* last 'special' character */ |
| 2068 | |
| 2069 | /* variable substitution byte (follows CTLVAR) */ |
| 2070 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 2071 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
| 2072 | #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */ |
| 2073 | |
| 2074 | /* values of VSTYPE field */ |
| 2075 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 2076 | #define VSMINUS 0x2 /* ${var-text} */ |
| 2077 | #define VSPLUS 0x3 /* ${var+text} */ |
| 2078 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 2079 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 2080 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 2081 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 2082 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 2083 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 2084 | #define VSLENGTH 0xa /* ${#var} */ |
| 2085 | |
| 2086 | /* values of checkkwd variable */ |
| 2087 | #define CHKALIAS 0x1 |
| 2088 | #define CHKKWD 0x2 |
| 2089 | #define CHKNL 0x4 |
| 2090 | |
| 2091 | #define IBUFSIZ (BUFSIZ + 1) |
| 2092 | |
| 2093 | /* |
| 2094 | * NEOF is returned by parsecmd when it encounters an end of file. It |
| 2095 | * must be distinct from NULL, so we use the address of a variable that |
| 2096 | * happens to be handy. |
| 2097 | */ |
| 2098 | static int plinno = 1; /* input line number */ |
| 2099 | |
| 2100 | /* number of characters left in input buffer */ |
| 2101 | static int parsenleft; /* copy of parsefile->nleft */ |
| 2102 | static int parselleft; /* copy of parsefile->lleft */ |
| 2103 | |
| 2104 | /* next character in input buffer */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 2105 | static char *parsenextc; /* copy of parsefile->nextc */ |
Glenn L McGrath | d361217 | 2003-09-17 00:22:26 +0000 | [diff] [blame] | 2106 | |
"Vladimir N. Oleynik" | 6f347ef | 2005-10-15 10:23:55 +0000 | [diff] [blame] | 2107 | #define basebuf bb_common_bufsiz1 /* buffer for top level input file */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2108 | |
| 2109 | |
| 2110 | static int tokpushback; /* last token pushed back */ |
| 2111 | #define NEOF ((union node *)&tokpushback) |
| 2112 | static int parsebackquote; /* nonzero if we are inside backquotes */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2113 | static int lasttoken; /* last token read */ |
| 2114 | static char *wordtext; /* text of last word returned by readtoken */ |
| 2115 | static int checkkwd; |
| 2116 | static struct nodelist *backquotelist; |
| 2117 | static union node *redirnode; |
| 2118 | static struct heredoc *heredoc; |
| 2119 | static int quoteflag; /* set if (part of) last token was quoted */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2120 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2121 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2122 | static char *endofname(const char *); |
| 2123 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2124 | /* shell.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2125 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2126 | static const char spcstr[] = " "; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2127 | static const char dolatstr[] = { CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' }; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2128 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2129 | #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96) |
| 2130 | #define __builtin_expect(x, expected_value) (x) |
| 2131 | #endif |
| 2132 | |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 2133 | #define xlikely(x) __builtin_expect((x),1) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 2134 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2135 | |
| 2136 | #define TEOF 0 |
| 2137 | #define TNL 1 |
| 2138 | #define TREDIR 2 |
| 2139 | #define TWORD 3 |
| 2140 | #define TSEMI 4 |
| 2141 | #define TBACKGND 5 |
| 2142 | #define TAND 6 |
| 2143 | #define TOR 7 |
| 2144 | #define TPIPE 8 |
| 2145 | #define TLP 9 |
| 2146 | #define TRP 10 |
| 2147 | #define TENDCASE 11 |
| 2148 | #define TENDBQUOTE 12 |
| 2149 | #define TNOT 13 |
| 2150 | #define TCASE 14 |
| 2151 | #define TDO 15 |
| 2152 | #define TDONE 16 |
| 2153 | #define TELIF 17 |
| 2154 | #define TELSE 18 |
| 2155 | #define TESAC 19 |
| 2156 | #define TFI 20 |
| 2157 | #define TFOR 21 |
| 2158 | #define TIF 22 |
| 2159 | #define TIN 23 |
| 2160 | #define TTHEN 24 |
| 2161 | #define TUNTIL 25 |
| 2162 | #define TWHILE 26 |
| 2163 | #define TBEGIN 27 |
| 2164 | #define TEND 28 |
| 2165 | |
| 2166 | /* first char is indicating which tokens mark the end of a list */ |
| 2167 | static const char *const tokname_array[] = { |
| 2168 | "\1end of file", |
| 2169 | "\0newline", |
| 2170 | "\0redirection", |
| 2171 | "\0word", |
| 2172 | "\0;", |
| 2173 | "\0&", |
| 2174 | "\0&&", |
| 2175 | "\0||", |
| 2176 | "\0|", |
| 2177 | "\0(", |
| 2178 | "\1)", |
| 2179 | "\1;;", |
| 2180 | "\1`", |
| 2181 | #define KWDOFFSET 13 |
| 2182 | /* the following are keywords */ |
| 2183 | "\0!", |
| 2184 | "\0case", |
| 2185 | "\1do", |
| 2186 | "\1done", |
| 2187 | "\1elif", |
| 2188 | "\1else", |
| 2189 | "\1esac", |
| 2190 | "\1fi", |
| 2191 | "\0for", |
| 2192 | "\0if", |
| 2193 | "\0in", |
| 2194 | "\1then", |
| 2195 | "\0until", |
| 2196 | "\0while", |
| 2197 | "\0{", |
| 2198 | "\1}", |
| 2199 | }; |
| 2200 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2201 | static const char * |
| 2202 | tokname(int tok) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2203 | { |
| 2204 | static char buf[16]; |
| 2205 | |
| 2206 | if (tok >= TSEMI) |
| 2207 | buf[0] = '"'; |
| 2208 | sprintf(buf + (tok >= TSEMI), "%s%c", |
| 2209 | tokname_array[tok] + 1, (tok >= TSEMI ? '"' : 0)); |
| 2210 | return buf; |
| 2211 | } |
| 2212 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2213 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 2214 | static int |
| 2215 | pstrcmp(const void *a, const void *b) |
| 2216 | { |
| 2217 | return strcmp((const char *) a, (*(const char *const *) b) + 1); |
| 2218 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2219 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2220 | static const char *const * |
| 2221 | findkwd(const char *s) |
| 2222 | { |
| 2223 | return bsearch(s, tokname_array + KWDOFFSET, |
| 2224 | (sizeof(tokname_array) / sizeof(const char *)) - KWDOFFSET, |
| 2225 | sizeof(const char *), pstrcmp); |
| 2226 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2227 | |
| 2228 | /* Syntax classes */ |
| 2229 | #define CWORD 0 /* character is nothing special */ |
| 2230 | #define CNL 1 /* newline character */ |
| 2231 | #define CBACK 2 /* a backslash character */ |
| 2232 | #define CSQUOTE 3 /* single quote */ |
| 2233 | #define CDQUOTE 4 /* double quote */ |
| 2234 | #define CENDQUOTE 5 /* a terminating quote */ |
| 2235 | #define CBQUOTE 6 /* backwards single quote */ |
| 2236 | #define CVAR 7 /* a dollar sign */ |
| 2237 | #define CENDVAR 8 /* a '}' character */ |
| 2238 | #define CLP 9 /* a left paren in arithmetic */ |
| 2239 | #define CRP 10 /* a right paren in arithmetic */ |
| 2240 | #define CENDFILE 11 /* end of file */ |
| 2241 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 2242 | #define CSPCL 13 /* these terminate a word */ |
| 2243 | #define CIGN 14 /* character should be ignored */ |
| 2244 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2245 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2246 | #define SYNBASE 130 |
| 2247 | #define PEOF -130 |
| 2248 | #define PEOA -129 |
| 2249 | #define PEOA_OR_PEOF PEOA |
| 2250 | #else |
| 2251 | #define SYNBASE 129 |
| 2252 | #define PEOF -129 |
| 2253 | #define PEOA_OR_PEOF PEOF |
| 2254 | #endif |
| 2255 | |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 2256 | /* C99 say: "char" declaration may be signed or unsigned default */ |
| 2257 | #define SC2INT(chr2may_be_negative_int) (int)((signed char)chr2may_be_negative_int) |
| 2258 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2259 | /* |
| 2260 | * is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 2261 | * (assuming ascii char codes, as the original implementation did) |
| 2262 | */ |
| 2263 | #define is_special(c) \ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 2264 | ( (((unsigned int)c) - 33 < 32) \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2265 | && ((0xc1ff920dUL >> (((unsigned int)c) - 33)) & 1)) |
| 2266 | |
| 2267 | #define digit_val(c) ((c) - '0') |
| 2268 | |
| 2269 | /* |
| 2270 | * This file was generated by the mksyntax program. |
| 2271 | */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2272 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2273 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2274 | #define USE_SIT_FUNCTION |
| 2275 | #endif |
| 2276 | |
| 2277 | /* number syntax index */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2278 | #define BASESYNTAX 0 /* not in quotes */ |
| 2279 | #define DQSYNTAX 1 /* in double quotes */ |
| 2280 | #define SQSYNTAX 2 /* in single quotes */ |
| 2281 | #define ARISYNTAX 3 /* in arithmetic */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2282 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2283 | #if ENABLE_ASH_MATH_SUPPORT |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2284 | static const char S_I_T[][4] = { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2285 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2286 | {CSPCL, CIGN, CIGN, CIGN}, /* 0, PEOA */ |
| 2287 | #endif |
| 2288 | {CSPCL, CWORD, CWORD, CWORD}, /* 1, ' ' */ |
| 2289 | {CNL, CNL, CNL, CNL}, /* 2, \n */ |
| 2290 | {CWORD, CCTL, CCTL, CWORD}, /* 3, !*-/:=?[]~ */ |
| 2291 | {CDQUOTE, CENDQUOTE, CWORD, CWORD}, /* 4, '"' */ |
| 2292 | {CVAR, CVAR, CWORD, CVAR}, /* 5, $ */ |
| 2293 | {CSQUOTE, CWORD, CENDQUOTE, CWORD}, /* 6, "'" */ |
| 2294 | {CSPCL, CWORD, CWORD, CLP}, /* 7, ( */ |
| 2295 | {CSPCL, CWORD, CWORD, CRP}, /* 8, ) */ |
| 2296 | {CBACK, CBACK, CCTL, CBACK}, /* 9, \ */ |
| 2297 | {CBQUOTE, CBQUOTE, CWORD, CBQUOTE}, /* 10, ` */ |
| 2298 | {CENDVAR, CENDVAR, CWORD, CENDVAR}, /* 11, } */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2299 | #ifndef USE_SIT_FUNCTION |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2300 | {CENDFILE, CENDFILE, CENDFILE, CENDFILE}, /* 12, PEOF */ |
| 2301 | {CWORD, CWORD, CWORD, CWORD}, /* 13, 0-9A-Za-z */ |
| 2302 | {CCTL, CCTL, CCTL, CCTL} /* 14, CTLESC ... */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2303 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2304 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2305 | #else |
| 2306 | static const char S_I_T[][3] = { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2307 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2308 | {CSPCL, CIGN, CIGN}, /* 0, PEOA */ |
| 2309 | #endif |
| 2310 | {CSPCL, CWORD, CWORD}, /* 1, ' ' */ |
| 2311 | {CNL, CNL, CNL}, /* 2, \n */ |
| 2312 | {CWORD, CCTL, CCTL}, /* 3, !*-/:=?[]~ */ |
| 2313 | {CDQUOTE, CENDQUOTE, CWORD}, /* 4, '"' */ |
| 2314 | {CVAR, CVAR, CWORD}, /* 5, $ */ |
| 2315 | {CSQUOTE, CWORD, CENDQUOTE}, /* 6, "'" */ |
| 2316 | {CSPCL, CWORD, CWORD}, /* 7, ( */ |
| 2317 | {CSPCL, CWORD, CWORD}, /* 8, ) */ |
| 2318 | {CBACK, CBACK, CCTL}, /* 9, \ */ |
| 2319 | {CBQUOTE, CBQUOTE, CWORD}, /* 10, ` */ |
| 2320 | {CENDVAR, CENDVAR, CWORD}, /* 11, } */ |
| 2321 | #ifndef USE_SIT_FUNCTION |
| 2322 | {CENDFILE, CENDFILE, CENDFILE}, /* 12, PEOF */ |
| 2323 | {CWORD, CWORD, CWORD}, /* 13, 0-9A-Za-z */ |
| 2324 | {CCTL, CCTL, CCTL} /* 14, CTLESC ... */ |
| 2325 | #endif |
| 2326 | }; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2327 | #endif /* ASH_MATH_SUPPORT */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2328 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2329 | #ifdef USE_SIT_FUNCTION |
| 2330 | |
| 2331 | #define U_C(c) ((unsigned char)(c)) |
| 2332 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2333 | static int |
| 2334 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2335 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2336 | static const char spec_symbls[] = "\t\n !\"$&'()*-/:;<=>?[\\]`|}~"; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2337 | #if ENABLE_ASH_ALIAS |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2338 | static const char syntax_index_table[] = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2339 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
| 2340 | 7, 8, 3, 3, 3, 3, 1, 1, /* "()*-/:;<" */ |
| 2341 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 2342 | 11, 3 /* "}~" */ |
| 2343 | }; |
| 2344 | #else |
| 2345 | static const char syntax_index_table[] = { |
| 2346 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
| 2347 | 6, 7, 2, 2, 2, 2, 0, 0, /* "()*-/:;<" */ |
| 2348 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 2349 | 10, 2 /* "}~" */ |
| 2350 | }; |
| 2351 | #endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2352 | const char *s; |
| 2353 | int indx; |
| 2354 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2355 | if (c == PEOF) /* 2^8+2 */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2356 | return CENDFILE; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2357 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2358 | if (c == PEOA) /* 2^8+1 */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2359 | indx = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2360 | else |
| 2361 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 2362 | if (U_C(c) >= U_C(CTLESC) && U_C(c) <= U_C(CTLQUOTEMARK)) |
| 2363 | return CCTL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2364 | else { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2365 | s = strchr(spec_symbls, c); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 2366 | if (s == NULL || *s == '\0') |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2367 | return CWORD; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2368 | indx = syntax_index_table[(s - spec_symbls)]; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2369 | } |
| 2370 | return S_I_T[indx][syntax]; |
| 2371 | } |
| 2372 | |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 2373 | #else /* USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2374 | |
| 2375 | #define SIT(c, syntax) S_I_T[(int)syntax_index_table[((int)c)+SYNBASE]][syntax] |
| 2376 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2377 | #if ENABLE_ASH_ALIAS |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2378 | #define CSPCL_CIGN_CIGN_CIGN 0 |
| 2379 | #define CSPCL_CWORD_CWORD_CWORD 1 |
| 2380 | #define CNL_CNL_CNL_CNL 2 |
| 2381 | #define CWORD_CCTL_CCTL_CWORD 3 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2382 | #define CDQUOTE_CENDQUOTE_CWORD_CWORD 4 |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2383 | #define CVAR_CVAR_CWORD_CVAR 5 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2384 | #define CSQUOTE_CWORD_CENDQUOTE_CWORD 6 |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2385 | #define CSPCL_CWORD_CWORD_CLP 7 |
| 2386 | #define CSPCL_CWORD_CWORD_CRP 8 |
| 2387 | #define CBACK_CBACK_CCTL_CBACK 9 |
| 2388 | #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE 10 |
| 2389 | #define CENDVAR_CENDVAR_CWORD_CENDVAR 11 |
| 2390 | #define CENDFILE_CENDFILE_CENDFILE_CENDFILE 12 |
| 2391 | #define CWORD_CWORD_CWORD_CWORD 13 |
| 2392 | #define CCTL_CCTL_CCTL_CCTL 14 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2393 | #else |
| 2394 | #define CSPCL_CWORD_CWORD_CWORD 0 |
| 2395 | #define CNL_CNL_CNL_CNL 1 |
| 2396 | #define CWORD_CCTL_CCTL_CWORD 2 |
| 2397 | #define CDQUOTE_CENDQUOTE_CWORD_CWORD 3 |
| 2398 | #define CVAR_CVAR_CWORD_CVAR 4 |
| 2399 | #define CSQUOTE_CWORD_CENDQUOTE_CWORD 5 |
| 2400 | #define CSPCL_CWORD_CWORD_CLP 6 |
| 2401 | #define CSPCL_CWORD_CWORD_CRP 7 |
| 2402 | #define CBACK_CBACK_CCTL_CBACK 8 |
| 2403 | #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE 9 |
| 2404 | #define CENDVAR_CENDVAR_CWORD_CENDVAR 10 |
| 2405 | #define CENDFILE_CENDFILE_CENDFILE_CENDFILE 11 |
| 2406 | #define CWORD_CWORD_CWORD_CWORD 12 |
| 2407 | #define CCTL_CCTL_CCTL_CCTL 13 |
| 2408 | #endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2409 | |
| 2410 | static const char syntax_index_table[258] = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2411 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2412 | /* 0 PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2413 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2414 | /* 1 PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 2415 | #endif |
| 2416 | /* 2 -128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 2417 | /* 3 -127 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 2418 | /* 4 -126 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2419 | /* 5 -125 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2420 | /* 6 -124 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 2421 | /* 7 -123 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 2422 | /* 8 -122 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 2423 | /* 9 -121 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 2424 | /* 10 -120 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2425 | /* 11 -119 */ CWORD_CWORD_CWORD_CWORD, |
| 2426 | /* 12 -118 */ CWORD_CWORD_CWORD_CWORD, |
| 2427 | /* 13 -117 */ CWORD_CWORD_CWORD_CWORD, |
| 2428 | /* 14 -116 */ CWORD_CWORD_CWORD_CWORD, |
| 2429 | /* 15 -115 */ CWORD_CWORD_CWORD_CWORD, |
| 2430 | /* 16 -114 */ CWORD_CWORD_CWORD_CWORD, |
| 2431 | /* 17 -113 */ CWORD_CWORD_CWORD_CWORD, |
| 2432 | /* 18 -112 */ CWORD_CWORD_CWORD_CWORD, |
| 2433 | /* 19 -111 */ CWORD_CWORD_CWORD_CWORD, |
| 2434 | /* 20 -110 */ CWORD_CWORD_CWORD_CWORD, |
| 2435 | /* 21 -109 */ CWORD_CWORD_CWORD_CWORD, |
| 2436 | /* 22 -108 */ CWORD_CWORD_CWORD_CWORD, |
| 2437 | /* 23 -107 */ CWORD_CWORD_CWORD_CWORD, |
| 2438 | /* 24 -106 */ CWORD_CWORD_CWORD_CWORD, |
| 2439 | /* 25 -105 */ CWORD_CWORD_CWORD_CWORD, |
| 2440 | /* 26 -104 */ CWORD_CWORD_CWORD_CWORD, |
| 2441 | /* 27 -103 */ CWORD_CWORD_CWORD_CWORD, |
| 2442 | /* 28 -102 */ CWORD_CWORD_CWORD_CWORD, |
| 2443 | /* 29 -101 */ CWORD_CWORD_CWORD_CWORD, |
| 2444 | /* 30 -100 */ CWORD_CWORD_CWORD_CWORD, |
| 2445 | /* 31 -99 */ CWORD_CWORD_CWORD_CWORD, |
| 2446 | /* 32 -98 */ CWORD_CWORD_CWORD_CWORD, |
| 2447 | /* 33 -97 */ CWORD_CWORD_CWORD_CWORD, |
| 2448 | /* 34 -96 */ CWORD_CWORD_CWORD_CWORD, |
| 2449 | /* 35 -95 */ CWORD_CWORD_CWORD_CWORD, |
| 2450 | /* 36 -94 */ CWORD_CWORD_CWORD_CWORD, |
| 2451 | /* 37 -93 */ CWORD_CWORD_CWORD_CWORD, |
| 2452 | /* 38 -92 */ CWORD_CWORD_CWORD_CWORD, |
| 2453 | /* 39 -91 */ CWORD_CWORD_CWORD_CWORD, |
| 2454 | /* 40 -90 */ CWORD_CWORD_CWORD_CWORD, |
| 2455 | /* 41 -89 */ CWORD_CWORD_CWORD_CWORD, |
| 2456 | /* 42 -88 */ CWORD_CWORD_CWORD_CWORD, |
| 2457 | /* 43 -87 */ CWORD_CWORD_CWORD_CWORD, |
| 2458 | /* 44 -86 */ CWORD_CWORD_CWORD_CWORD, |
| 2459 | /* 45 -85 */ CWORD_CWORD_CWORD_CWORD, |
| 2460 | /* 46 -84 */ CWORD_CWORD_CWORD_CWORD, |
| 2461 | /* 47 -83 */ CWORD_CWORD_CWORD_CWORD, |
| 2462 | /* 48 -82 */ CWORD_CWORD_CWORD_CWORD, |
| 2463 | /* 49 -81 */ CWORD_CWORD_CWORD_CWORD, |
| 2464 | /* 50 -80 */ CWORD_CWORD_CWORD_CWORD, |
| 2465 | /* 51 -79 */ CWORD_CWORD_CWORD_CWORD, |
| 2466 | /* 52 -78 */ CWORD_CWORD_CWORD_CWORD, |
| 2467 | /* 53 -77 */ CWORD_CWORD_CWORD_CWORD, |
| 2468 | /* 54 -76 */ CWORD_CWORD_CWORD_CWORD, |
| 2469 | /* 55 -75 */ CWORD_CWORD_CWORD_CWORD, |
| 2470 | /* 56 -74 */ CWORD_CWORD_CWORD_CWORD, |
| 2471 | /* 57 -73 */ CWORD_CWORD_CWORD_CWORD, |
| 2472 | /* 58 -72 */ CWORD_CWORD_CWORD_CWORD, |
| 2473 | /* 59 -71 */ CWORD_CWORD_CWORD_CWORD, |
| 2474 | /* 60 -70 */ CWORD_CWORD_CWORD_CWORD, |
| 2475 | /* 61 -69 */ CWORD_CWORD_CWORD_CWORD, |
| 2476 | /* 62 -68 */ CWORD_CWORD_CWORD_CWORD, |
| 2477 | /* 63 -67 */ CWORD_CWORD_CWORD_CWORD, |
| 2478 | /* 64 -66 */ CWORD_CWORD_CWORD_CWORD, |
| 2479 | /* 65 -65 */ CWORD_CWORD_CWORD_CWORD, |
| 2480 | /* 66 -64 */ CWORD_CWORD_CWORD_CWORD, |
| 2481 | /* 67 -63 */ CWORD_CWORD_CWORD_CWORD, |
| 2482 | /* 68 -62 */ CWORD_CWORD_CWORD_CWORD, |
| 2483 | /* 69 -61 */ CWORD_CWORD_CWORD_CWORD, |
| 2484 | /* 70 -60 */ CWORD_CWORD_CWORD_CWORD, |
| 2485 | /* 71 -59 */ CWORD_CWORD_CWORD_CWORD, |
| 2486 | /* 72 -58 */ CWORD_CWORD_CWORD_CWORD, |
| 2487 | /* 73 -57 */ CWORD_CWORD_CWORD_CWORD, |
| 2488 | /* 74 -56 */ CWORD_CWORD_CWORD_CWORD, |
| 2489 | /* 75 -55 */ CWORD_CWORD_CWORD_CWORD, |
| 2490 | /* 76 -54 */ CWORD_CWORD_CWORD_CWORD, |
| 2491 | /* 77 -53 */ CWORD_CWORD_CWORD_CWORD, |
| 2492 | /* 78 -52 */ CWORD_CWORD_CWORD_CWORD, |
| 2493 | /* 79 -51 */ CWORD_CWORD_CWORD_CWORD, |
| 2494 | /* 80 -50 */ CWORD_CWORD_CWORD_CWORD, |
| 2495 | /* 81 -49 */ CWORD_CWORD_CWORD_CWORD, |
| 2496 | /* 82 -48 */ CWORD_CWORD_CWORD_CWORD, |
| 2497 | /* 83 -47 */ CWORD_CWORD_CWORD_CWORD, |
| 2498 | /* 84 -46 */ CWORD_CWORD_CWORD_CWORD, |
| 2499 | /* 85 -45 */ CWORD_CWORD_CWORD_CWORD, |
| 2500 | /* 86 -44 */ CWORD_CWORD_CWORD_CWORD, |
| 2501 | /* 87 -43 */ CWORD_CWORD_CWORD_CWORD, |
| 2502 | /* 88 -42 */ CWORD_CWORD_CWORD_CWORD, |
| 2503 | /* 89 -41 */ CWORD_CWORD_CWORD_CWORD, |
| 2504 | /* 90 -40 */ CWORD_CWORD_CWORD_CWORD, |
| 2505 | /* 91 -39 */ CWORD_CWORD_CWORD_CWORD, |
| 2506 | /* 92 -38 */ CWORD_CWORD_CWORD_CWORD, |
| 2507 | /* 93 -37 */ CWORD_CWORD_CWORD_CWORD, |
| 2508 | /* 94 -36 */ CWORD_CWORD_CWORD_CWORD, |
| 2509 | /* 95 -35 */ CWORD_CWORD_CWORD_CWORD, |
| 2510 | /* 96 -34 */ CWORD_CWORD_CWORD_CWORD, |
| 2511 | /* 97 -33 */ CWORD_CWORD_CWORD_CWORD, |
| 2512 | /* 98 -32 */ CWORD_CWORD_CWORD_CWORD, |
| 2513 | /* 99 -31 */ CWORD_CWORD_CWORD_CWORD, |
| 2514 | /* 100 -30 */ CWORD_CWORD_CWORD_CWORD, |
| 2515 | /* 101 -29 */ CWORD_CWORD_CWORD_CWORD, |
| 2516 | /* 102 -28 */ CWORD_CWORD_CWORD_CWORD, |
| 2517 | /* 103 -27 */ CWORD_CWORD_CWORD_CWORD, |
| 2518 | /* 104 -26 */ CWORD_CWORD_CWORD_CWORD, |
| 2519 | /* 105 -25 */ CWORD_CWORD_CWORD_CWORD, |
| 2520 | /* 106 -24 */ CWORD_CWORD_CWORD_CWORD, |
| 2521 | /* 107 -23 */ CWORD_CWORD_CWORD_CWORD, |
| 2522 | /* 108 -22 */ CWORD_CWORD_CWORD_CWORD, |
| 2523 | /* 109 -21 */ CWORD_CWORD_CWORD_CWORD, |
| 2524 | /* 110 -20 */ CWORD_CWORD_CWORD_CWORD, |
| 2525 | /* 111 -19 */ CWORD_CWORD_CWORD_CWORD, |
| 2526 | /* 112 -18 */ CWORD_CWORD_CWORD_CWORD, |
| 2527 | /* 113 -17 */ CWORD_CWORD_CWORD_CWORD, |
| 2528 | /* 114 -16 */ CWORD_CWORD_CWORD_CWORD, |
| 2529 | /* 115 -15 */ CWORD_CWORD_CWORD_CWORD, |
| 2530 | /* 116 -14 */ CWORD_CWORD_CWORD_CWORD, |
| 2531 | /* 117 -13 */ CWORD_CWORD_CWORD_CWORD, |
| 2532 | /* 118 -12 */ CWORD_CWORD_CWORD_CWORD, |
| 2533 | /* 119 -11 */ CWORD_CWORD_CWORD_CWORD, |
| 2534 | /* 120 -10 */ CWORD_CWORD_CWORD_CWORD, |
| 2535 | /* 121 -9 */ CWORD_CWORD_CWORD_CWORD, |
| 2536 | /* 122 -8 */ CWORD_CWORD_CWORD_CWORD, |
| 2537 | /* 123 -7 */ CWORD_CWORD_CWORD_CWORD, |
| 2538 | /* 124 -6 */ CWORD_CWORD_CWORD_CWORD, |
| 2539 | /* 125 -5 */ CWORD_CWORD_CWORD_CWORD, |
| 2540 | /* 126 -4 */ CWORD_CWORD_CWORD_CWORD, |
| 2541 | /* 127 -3 */ CWORD_CWORD_CWORD_CWORD, |
| 2542 | /* 128 -2 */ CWORD_CWORD_CWORD_CWORD, |
| 2543 | /* 129 -1 */ CWORD_CWORD_CWORD_CWORD, |
| 2544 | /* 130 0 */ CWORD_CWORD_CWORD_CWORD, |
| 2545 | /* 131 1 */ CWORD_CWORD_CWORD_CWORD, |
| 2546 | /* 132 2 */ CWORD_CWORD_CWORD_CWORD, |
| 2547 | /* 133 3 */ CWORD_CWORD_CWORD_CWORD, |
| 2548 | /* 134 4 */ CWORD_CWORD_CWORD_CWORD, |
| 2549 | /* 135 5 */ CWORD_CWORD_CWORD_CWORD, |
| 2550 | /* 136 6 */ CWORD_CWORD_CWORD_CWORD, |
| 2551 | /* 137 7 */ CWORD_CWORD_CWORD_CWORD, |
| 2552 | /* 138 8 */ CWORD_CWORD_CWORD_CWORD, |
| 2553 | /* 139 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 2554 | /* 140 10 "\n" */ CNL_CNL_CNL_CNL, |
| 2555 | /* 141 11 */ CWORD_CWORD_CWORD_CWORD, |
| 2556 | /* 142 12 */ CWORD_CWORD_CWORD_CWORD, |
| 2557 | /* 143 13 */ CWORD_CWORD_CWORD_CWORD, |
| 2558 | /* 144 14 */ CWORD_CWORD_CWORD_CWORD, |
| 2559 | /* 145 15 */ CWORD_CWORD_CWORD_CWORD, |
| 2560 | /* 146 16 */ CWORD_CWORD_CWORD_CWORD, |
| 2561 | /* 147 17 */ CWORD_CWORD_CWORD_CWORD, |
| 2562 | /* 148 18 */ CWORD_CWORD_CWORD_CWORD, |
| 2563 | /* 149 19 */ CWORD_CWORD_CWORD_CWORD, |
| 2564 | /* 150 20 */ CWORD_CWORD_CWORD_CWORD, |
| 2565 | /* 151 21 */ CWORD_CWORD_CWORD_CWORD, |
| 2566 | /* 152 22 */ CWORD_CWORD_CWORD_CWORD, |
| 2567 | /* 153 23 */ CWORD_CWORD_CWORD_CWORD, |
| 2568 | /* 154 24 */ CWORD_CWORD_CWORD_CWORD, |
| 2569 | /* 155 25 */ CWORD_CWORD_CWORD_CWORD, |
| 2570 | /* 156 26 */ CWORD_CWORD_CWORD_CWORD, |
| 2571 | /* 157 27 */ CWORD_CWORD_CWORD_CWORD, |
| 2572 | /* 158 28 */ CWORD_CWORD_CWORD_CWORD, |
| 2573 | /* 159 29 */ CWORD_CWORD_CWORD_CWORD, |
| 2574 | /* 160 30 */ CWORD_CWORD_CWORD_CWORD, |
| 2575 | /* 161 31 */ CWORD_CWORD_CWORD_CWORD, |
| 2576 | /* 162 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 2577 | /* 163 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2578 | /* 164 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2579 | /* 165 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 2580 | /* 166 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 2581 | /* 167 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 2582 | /* 168 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2583 | /* 169 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2584 | /* 170 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 2585 | /* 171 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 2586 | /* 172 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 2587 | /* 173 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 2588 | /* 174 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 2589 | /* 175 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 2590 | /* 176 46 "." */ CWORD_CWORD_CWORD_CWORD, |
| 2591 | /* 177 47 "/" */ CWORD_CCTL_CCTL_CWORD, |
| 2592 | /* 178 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 2593 | /* 179 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 2594 | /* 180 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 2595 | /* 181 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 2596 | /* 182 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 2597 | /* 183 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 2598 | /* 184 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 2599 | /* 185 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 2600 | /* 186 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 2601 | /* 187 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 2602 | /* 188 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 2603 | /* 189 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 2604 | /* 190 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 2605 | /* 191 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 2606 | /* 192 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 2607 | /* 193 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 2608 | /* 194 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 2609 | /* 195 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 2610 | /* 196 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 2611 | /* 197 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 2612 | /* 198 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 2613 | /* 199 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 2614 | /* 200 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 2615 | /* 201 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 2616 | /* 202 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 2617 | /* 203 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 2618 | /* 204 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 2619 | /* 205 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 2620 | /* 206 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 2621 | /* 207 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 2622 | /* 208 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 2623 | /* 209 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 2624 | /* 210 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 2625 | /* 211 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 2626 | /* 212 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 2627 | /* 213 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 2628 | /* 214 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 2629 | /* 215 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 2630 | /* 216 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 2631 | /* 217 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 2632 | /* 218 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 2633 | /* 219 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 2634 | /* 220 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 2635 | /* 221 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 2636 | /* 222 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 2637 | /* 223 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 2638 | /* 224 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 2639 | /* 225 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 2640 | /* 226 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 2641 | /* 227 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 2642 | /* 228 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 2643 | /* 229 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 2644 | /* 230 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 2645 | /* 231 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 2646 | /* 232 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 2647 | /* 233 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 2648 | /* 234 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 2649 | /* 235 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 2650 | /* 236 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 2651 | /* 237 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 2652 | /* 238 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 2653 | /* 239 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 2654 | /* 240 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 2655 | /* 241 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 2656 | /* 242 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 2657 | /* 243 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 2658 | /* 244 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 2659 | /* 245 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 2660 | /* 246 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 2661 | /* 247 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 2662 | /* 248 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 2663 | /* 249 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 2664 | /* 250 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 2665 | /* 251 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 2666 | /* 252 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 2667 | /* 253 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 2668 | /* 254 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 2669 | /* 255 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 2670 | /* 256 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 2671 | /* 257 127 */ CWORD_CWORD_CWORD_CWORD, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2672 | }; |
| 2673 | |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 2674 | #endif /* USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2675 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2676 | /* alias.c */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2677 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2678 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2679 | #define ATABSIZE 39 |
| 2680 | |
| 2681 | static int funcblocksize; /* size of structures in function */ |
| 2682 | static int funcstringsize; /* size of strings in node */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 2683 | static void *funcblock; /* block to allocate function from */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2684 | static char *funcstring; /* block to allocate strings from */ |
| 2685 | |
| 2686 | static const short nodesize[26] = { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 2687 | SHELL_ALIGN(sizeof(struct ncmd)), |
| 2688 | SHELL_ALIGN(sizeof(struct npipe)), |
| 2689 | SHELL_ALIGN(sizeof(struct nredir)), |
| 2690 | SHELL_ALIGN(sizeof(struct nredir)), |
| 2691 | SHELL_ALIGN(sizeof(struct nredir)), |
| 2692 | SHELL_ALIGN(sizeof(struct nbinary)), |
| 2693 | SHELL_ALIGN(sizeof(struct nbinary)), |
| 2694 | SHELL_ALIGN(sizeof(struct nbinary)), |
| 2695 | SHELL_ALIGN(sizeof(struct nif)), |
| 2696 | SHELL_ALIGN(sizeof(struct nbinary)), |
| 2697 | SHELL_ALIGN(sizeof(struct nbinary)), |
| 2698 | SHELL_ALIGN(sizeof(struct nfor)), |
| 2699 | SHELL_ALIGN(sizeof(struct ncase)), |
| 2700 | SHELL_ALIGN(sizeof(struct nclist)), |
| 2701 | SHELL_ALIGN(sizeof(struct narg)), |
| 2702 | SHELL_ALIGN(sizeof(struct narg)), |
| 2703 | SHELL_ALIGN(sizeof(struct nfile)), |
| 2704 | SHELL_ALIGN(sizeof(struct nfile)), |
| 2705 | SHELL_ALIGN(sizeof(struct nfile)), |
| 2706 | SHELL_ALIGN(sizeof(struct nfile)), |
| 2707 | SHELL_ALIGN(sizeof(struct nfile)), |
| 2708 | SHELL_ALIGN(sizeof(struct ndup)), |
| 2709 | SHELL_ALIGN(sizeof(struct ndup)), |
| 2710 | SHELL_ALIGN(sizeof(struct nhere)), |
| 2711 | SHELL_ALIGN(sizeof(struct nhere)), |
| 2712 | SHELL_ALIGN(sizeof(struct nnot)), |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2713 | }; |
| 2714 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2715 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2716 | static void calcsize(union node *); |
| 2717 | static void sizenodelist(struct nodelist *); |
| 2718 | static union node *copynode(union node *); |
| 2719 | static struct nodelist *copynodelist(struct nodelist *); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2720 | static char *nodeckstrdup(char *); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2721 | |
| 2722 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2723 | static int evalskip; /* set if we are skipping commands */ |
| 2724 | static int skipcount; /* number of levels to skip */ |
| 2725 | static int funcnest; /* depth of function calls */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2726 | |
| 2727 | /* reasons for skipping commands (see comment on breakcmd routine) */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2728 | #define SKIPBREAK (1 << 0) |
| 2729 | #define SKIPCONT (1 << 1) |
| 2730 | #define SKIPFUNC (1 << 2) |
| 2731 | #define SKIPFILE (1 << 3) |
| 2732 | #define SKIPEVAL (1 << 4) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2733 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2734 | /* |
| 2735 | * This file was generated by the mkbuiltins program. |
| 2736 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2737 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2738 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2739 | static int bgcmd(int, char **); |
| 2740 | #endif |
| 2741 | static int breakcmd(int, char **); |
| 2742 | static int cdcmd(int, char **); |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2743 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2744 | static int commandcmd(int, char **); |
| 2745 | #endif |
| 2746 | static int dotcmd(int, char **); |
| 2747 | static int evalcmd(int, char **); |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2748 | #if ENABLE_ASH_BUILTIN_ECHO |
Paul Fox | 0b62158 | 2005-08-09 19:38:05 +0000 | [diff] [blame] | 2749 | static int echocmd(int, char **); |
| 2750 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2751 | #if ENABLE_ASH_BUILTIN_TEST |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 2752 | static int testcmd(int, char **); |
| 2753 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2754 | static int execcmd(int, char **); |
| 2755 | static int exitcmd(int, char **); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2756 | static int exportcmd(int, char **); |
| 2757 | static int falsecmd(int, char **); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2758 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2759 | static int fgcmd(int, char **); |
| 2760 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2761 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2762 | static int getoptscmd(int, char **); |
| 2763 | #endif |
| 2764 | static int hashcmd(int, char **); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2765 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2766 | static int helpcmd(int argc, char **argv); |
| 2767 | #endif |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2768 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2769 | static int jobscmd(int, char **); |
| 2770 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2771 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 2772 | static int letcmd(int, char **); |
| 2773 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2774 | static int localcmd(int, char **); |
| 2775 | static int pwdcmd(int, char **); |
| 2776 | static int readcmd(int, char **); |
| 2777 | static int returncmd(int, char **); |
| 2778 | static int setcmd(int, char **); |
| 2779 | static int shiftcmd(int, char **); |
| 2780 | static int timescmd(int, char **); |
| 2781 | static int trapcmd(int, char **); |
| 2782 | static int truecmd(int, char **); |
| 2783 | static int typecmd(int, char **); |
| 2784 | static int umaskcmd(int, char **); |
| 2785 | static int unsetcmd(int, char **); |
| 2786 | static int waitcmd(int, char **); |
| 2787 | static int ulimitcmd(int, char **); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2788 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2789 | static int killcmd(int, char **); |
| 2790 | #endif |
| 2791 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2792 | /* exec.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2793 | |
| 2794 | /* values of cmdtype */ |
| 2795 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 2796 | #define CMDNORMAL 0 /* command is an executable program */ |
| 2797 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 2798 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 2799 | |
| 2800 | struct builtincmd { |
| 2801 | const char *name; |
| 2802 | int (*builtin)(int, char **); |
| 2803 | /* unsigned flags; */ |
| 2804 | }; |
| 2805 | |
Paul Fox | 0b62158 | 2005-08-09 19:38:05 +0000 | [diff] [blame] | 2806 | |
| 2807 | #define COMMANDCMD (builtincmd + 5 + \ |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 2808 | 2 * ENABLE_ASH_BUILTIN_TEST + \ |
| 2809 | ENABLE_ASH_ALIAS + \ |
| 2810 | ENABLE_ASH_JOB_CONTROL) |
Paul Fox | 0b62158 | 2005-08-09 19:38:05 +0000 | [diff] [blame] | 2811 | #define EXECCMD (builtincmd + 7 + \ |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 2812 | 2 * ENABLE_ASH_BUILTIN_TEST + \ |
| 2813 | ENABLE_ASH_ALIAS + \ |
| 2814 | ENABLE_ASH_JOB_CONTROL + \ |
| 2815 | ENABLE_ASH_CMDCMD + \ |
| 2816 | ENABLE_ASH_BUILTIN_ECHO) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2817 | |
| 2818 | #define BUILTIN_NOSPEC "0" |
| 2819 | #define BUILTIN_SPECIAL "1" |
| 2820 | #define BUILTIN_REGULAR "2" |
| 2821 | #define BUILTIN_SPEC_REG "3" |
| 2822 | #define BUILTIN_ASSIGN "4" |
| 2823 | #define BUILTIN_SPEC_ASSG "5" |
| 2824 | #define BUILTIN_REG_ASSG "6" |
| 2825 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 2826 | |
| 2827 | #define IS_BUILTIN_SPECIAL(builtincmd) ((builtincmd)->name[0] & 1) |
| 2828 | #define IS_BUILTIN_REGULAR(builtincmd) ((builtincmd)->name[0] & 2) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 2829 | #define IS_BUILTIN_ASSIGN(builtincmd) ((builtincmd)->name[0] & 4) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2830 | |
Bernhard Reutner-Fischer | 3549213 | 2006-06-21 18:19:53 +0000 | [diff] [blame] | 2831 | /* make sure to keep these in proper order since it is searched via bsearch() */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2832 | static const struct builtincmd builtincmd[] = { |
| 2833 | { BUILTIN_SPEC_REG ".", dotcmd }, |
| 2834 | { BUILTIN_SPEC_REG ":", truecmd }, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2835 | #if ENABLE_ASH_BUILTIN_TEST |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 2836 | { BUILTIN_REGULAR "[", testcmd }, |
| 2837 | { BUILTIN_REGULAR "[[", testcmd }, |
| 2838 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2839 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2840 | { BUILTIN_REG_ASSG "alias", aliascmd }, |
| 2841 | #endif |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2842 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2843 | { BUILTIN_REGULAR "bg", bgcmd }, |
| 2844 | #endif |
| 2845 | { BUILTIN_SPEC_REG "break", breakcmd }, |
| 2846 | { BUILTIN_REGULAR "cd", cdcmd }, |
| 2847 | { BUILTIN_NOSPEC "chdir", cdcmd }, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2848 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2849 | { BUILTIN_REGULAR "command", commandcmd }, |
| 2850 | #endif |
| 2851 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2852 | #if ENABLE_ASH_BUILTIN_ECHO |
Paul Fox | 0b62158 | 2005-08-09 19:38:05 +0000 | [diff] [blame] | 2853 | { BUILTIN_REGULAR "echo", echocmd }, |
| 2854 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2855 | { BUILTIN_SPEC_REG "eval", evalcmd }, |
| 2856 | { BUILTIN_SPEC_REG "exec", execcmd }, |
| 2857 | { BUILTIN_SPEC_REG "exit", exitcmd }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2858 | { BUILTIN_SPEC_REG_ASSG "export", exportcmd }, |
| 2859 | { BUILTIN_REGULAR "false", falsecmd }, |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2860 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2861 | { BUILTIN_REGULAR "fg", fgcmd }, |
| 2862 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2863 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2864 | { BUILTIN_REGULAR "getopts", getoptscmd }, |
| 2865 | #endif |
| 2866 | { BUILTIN_NOSPEC "hash", hashcmd }, |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2867 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2868 | { BUILTIN_NOSPEC "help", helpcmd }, |
| 2869 | #endif |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2870 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2871 | { BUILTIN_REGULAR "jobs", jobscmd }, |
| 2872 | { BUILTIN_REGULAR "kill", killcmd }, |
| 2873 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2874 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 2875 | { BUILTIN_NOSPEC "let", letcmd }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2876 | #endif |
| 2877 | { BUILTIN_ASSIGN "local", localcmd }, |
| 2878 | { BUILTIN_NOSPEC "pwd", pwdcmd }, |
| 2879 | { BUILTIN_REGULAR "read", readcmd }, |
| 2880 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 2881 | { BUILTIN_SPEC_REG "return", returncmd }, |
| 2882 | { BUILTIN_SPEC_REG "set", setcmd }, |
| 2883 | { BUILTIN_SPEC_REG "shift", shiftcmd }, |
Mike Frysinger | c2ad4f5 | 2006-06-21 18:04:49 +0000 | [diff] [blame] | 2884 | { BUILTIN_SPEC_REG "source", dotcmd }, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2885 | #if ENABLE_ASH_BUILTIN_TEST |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 2886 | { BUILTIN_REGULAR "test", testcmd }, |
| 2887 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2888 | { BUILTIN_SPEC_REG "times", timescmd }, |
| 2889 | { BUILTIN_SPEC_REG "trap", trapcmd }, |
| 2890 | { BUILTIN_REGULAR "true", truecmd }, |
| 2891 | { BUILTIN_NOSPEC "type", typecmd }, |
| 2892 | { BUILTIN_NOSPEC "ulimit", ulimitcmd }, |
| 2893 | { BUILTIN_REGULAR "umask", umaskcmd }, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2894 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2895 | { BUILTIN_REGULAR "unalias", unaliascmd }, |
| 2896 | #endif |
| 2897 | { BUILTIN_SPEC_REG "unset", unsetcmd }, |
| 2898 | { BUILTIN_REGULAR "wait", waitcmd }, |
| 2899 | }; |
| 2900 | |
Denis Vlasenko | 8f27c34 | 2006-12-26 21:31:11 +0000 | [diff] [blame] | 2901 | #define NUMBUILTINS (sizeof(builtincmd) / sizeof(builtincmd[0])) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2902 | |
| 2903 | |
| 2904 | struct cmdentry { |
| 2905 | int cmdtype; |
| 2906 | union param { |
| 2907 | int index; |
| 2908 | const struct builtincmd *cmd; |
| 2909 | struct funcnode *func; |
| 2910 | } u; |
| 2911 | }; |
| 2912 | |
| 2913 | |
| 2914 | /* action to find_command() */ |
| 2915 | #define DO_ERR 0x01 /* prints errors */ |
| 2916 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 2917 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 2918 | #define DO_ALTPATH 0x08 /* using alternate path */ |
| 2919 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ |
| 2920 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 2921 | static void shellexec(char **, const char *, int) ATTRIBUTE_NORETURN; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2922 | static char *padvance(const char **, const char *); |
| 2923 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 2924 | static struct builtincmd *find_builtin(const char *); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2925 | static void defun(char *, union node *); |
| 2926 | static void unsetfunc(const char *); |
| 2927 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2928 | #if ENABLE_ASH_MATH_SUPPORT_64 |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 2929 | typedef int64_t arith_t; |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 2930 | #define arith_t_type (long long) |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 2931 | #else |
| 2932 | typedef long arith_t; |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 2933 | #define arith_t_type (long) |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 2934 | #endif |
Glenn L McGrath | 5f2a23c | 2004-06-25 07:05:13 +0000 | [diff] [blame] | 2935 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2936 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 2937 | static arith_t dash_arith(const char *); |
| 2938 | static arith_t arith(const char *expr, int *perrcode); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2939 | #endif |
| 2940 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2941 | #if ENABLE_ASH_RANDOM_SUPPORT |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 2942 | static unsigned long rseed; |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 2943 | # ifndef DYNAMIC_VAR |
| 2944 | # define DYNAMIC_VAR |
| 2945 | # endif |
| 2946 | #endif |
| 2947 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2948 | /* PEOF (the end of file marker) */ |
| 2949 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2950 | enum { |
| 2951 | INPUT_PUSH_FILE = 1, |
| 2952 | INPUT_NOFILE_OK = 2, |
| 2953 | }; |
| 2954 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2955 | /* |
| 2956 | * The input line number. Input.c just defines this variable, and saves |
| 2957 | * and restores it when files are pushed and popped. The user of this |
| 2958 | * package must set its value. |
| 2959 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2960 | static void pungetc(void); |
| 2961 | static void pushstring(char *, void *); |
| 2962 | static void popstring(void); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2963 | static void setinputfd(int, int); |
| 2964 | static void setinputstring(char *); |
| 2965 | static void popfile(void); |
| 2966 | static void popallfiles(void); |
| 2967 | static void closescript(void); |
| 2968 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2969 | /* jobs.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2970 | |
| 2971 | |
| 2972 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
| 2973 | #define FORK_FG 0 |
| 2974 | #define FORK_BG 1 |
| 2975 | #define FORK_NOJOB 2 |
| 2976 | |
| 2977 | /* mode flags for showjob(s) */ |
| 2978 | #define SHOW_PGID 0x01 /* only show pgid - for jobs -p */ |
| 2979 | #define SHOW_PID 0x04 /* include process pid */ |
| 2980 | #define SHOW_CHANGED 0x08 /* only jobs whose state has changed */ |
| 2981 | |
| 2982 | |
| 2983 | /* |
| 2984 | * A job structure contains information about a job. A job is either a |
| 2985 | * single process or a set of processes contained in a pipeline. In the |
| 2986 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 2987 | * array of pids. |
| 2988 | */ |
| 2989 | |
| 2990 | struct procstat { |
| 2991 | pid_t pid; /* process id */ |
| 2992 | int status; /* last process status from wait() */ |
| 2993 | char *cmd; /* text of command being run */ |
| 2994 | }; |
| 2995 | |
| 2996 | struct job { |
| 2997 | struct procstat ps0; /* status of process */ |
| 2998 | struct procstat *ps; /* status or processes when more than one */ |
| 2999 | #if JOBS |
| 3000 | int stopstatus; /* status of a stopped job */ |
| 3001 | #endif |
| 3002 | uint32_t |
| 3003 | nprocs: 16, /* number of processes */ |
| 3004 | state: 8, |
| 3005 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3006 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3007 | #define JOBDONE 2 /* all procs are completed */ |
| 3008 | #if JOBS |
| 3009 | sigint: 1, /* job was killed by SIGINT */ |
| 3010 | jobctl: 1, /* job running under job control */ |
| 3011 | #endif |
| 3012 | waited: 1, /* true if this entry has been waited for */ |
| 3013 | used: 1, /* true if this entry is in used */ |
| 3014 | changed: 1; /* true if status has changed */ |
| 3015 | struct job *prev_job; /* previous job */ |
| 3016 | }; |
| 3017 | |
| 3018 | static pid_t backgndpid; /* pid of last background process */ |
| 3019 | static int job_warning; /* user was warned about stopped jobs */ |
| 3020 | #if JOBS |
| 3021 | static int jobctl; /* true if doing job control */ |
| 3022 | #endif |
| 3023 | |
| 3024 | static struct job *makejob(union node *, int); |
| 3025 | static int forkshell(struct job *, union node *, int); |
| 3026 | static int waitforjob(struct job *); |
| 3027 | static int stoppedjobs(void); |
| 3028 | |
| 3029 | #if ! JOBS |
| 3030 | #define setjobctl(on) /* do nothing */ |
| 3031 | #else |
| 3032 | static void setjobctl(int); |
| 3033 | static void showjobs(FILE *, int); |
| 3034 | #endif |
| 3035 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3036 | /* main.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3037 | |
| 3038 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3039 | static void readcmdfile(char *); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 3040 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3041 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3042 | /* mystring.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3043 | |
| 3044 | |
| 3045 | #define DOLATSTRLEN 4 |
| 3046 | |
| 3047 | static char *prefix(const char *, const char *); |
| 3048 | static int number(const char *); |
| 3049 | static int is_number(const char *); |
| 3050 | static char *single_quote(const char *); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3051 | |
| 3052 | #define equal(s1, s2) (strcmp(s1, s2) == 0) |
| 3053 | #define scopy(s1, s2) ((void)strcpy(s2, s1)) |
| 3054 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3055 | /* options.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3056 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3057 | static char *minusc; /* argument to -c option */ |
| 3058 | |
| 3059 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3060 | static void optschanged(void); |
| 3061 | static void setparam(char **); |
| 3062 | static void freeparam(volatile struct shparam *); |
| 3063 | static int shiftcmd(int, char **); |
| 3064 | static int setcmd(int, char **); |
| 3065 | static int nextopt(const char *); |
| 3066 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3067 | /* redir.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3068 | |
| 3069 | /* flags passed to redirect */ |
| 3070 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3071 | #define REDIR_SAVEFD2 03 /* set preverrout */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3072 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3073 | static void redirect(union node *, int); |
| 3074 | static void popredir(int); |
| 3075 | static void clearredir(int); |
| 3076 | static int copyfd(int, int); |
| 3077 | static int redirectsafe(union node *, int); |
| 3078 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3079 | /* show.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3080 | |
| 3081 | |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 3082 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3083 | static void showtree(union node *); |
| 3084 | static void trace(const char *, ...); |
| 3085 | static void tracev(const char *, va_list); |
| 3086 | static void trargs(char **); |
| 3087 | static void trputc(int); |
| 3088 | static void trputs(const char *); |
| 3089 | static void opentrace(void); |
| 3090 | #endif |
| 3091 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3092 | /* trap.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3093 | |
| 3094 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3095 | static void clear_traps(void); |
| 3096 | static void setsignal(int); |
| 3097 | static void ignoresig(int); |
| 3098 | static void onsig(int); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3099 | static int dotrap(void); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3100 | static void setinteractive(int); |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 3101 | static void exitshell(void) ATTRIBUTE_NORETURN; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3102 | |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 3103 | |
| 3104 | static int is_safe_applet(char *name) |
| 3105 | { |
Denis Vlasenko | 8f27c34 | 2006-12-26 21:31:11 +0000 | [diff] [blame] | 3106 | /* It isn't a bug to have non-existent applet here... */ |
| 3107 | /* ...just a waste of space... */ |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 3108 | static const char safe_applets[][8] = { |
Denis Vlasenko | 8f27c34 | 2006-12-26 21:31:11 +0000 | [diff] [blame] | 3109 | "[" |
| 3110 | USE_AWK (, "awk" ) |
| 3111 | USE_CAT (, "cat" ) |
| 3112 | USE_CHMOD (, "chmod" ) |
| 3113 | USE_CHOWN (, "chown" ) |
| 3114 | USE_CP (, "cp" ) |
| 3115 | USE_CUT (, "cut" ) |
| 3116 | USE_DD (, "dd" ) |
| 3117 | USE_ECHO (, "echo" ) |
| 3118 | USE_FIND (, "find" ) |
| 3119 | USE_HEXDUMP(, "hexdump") |
| 3120 | USE_LN (, "ln" ) |
| 3121 | USE_LS (, "ls" ) |
| 3122 | USE_MKDIR (, "mkdir" ) |
| 3123 | USE_RM (, "rm" ) |
| 3124 | USE_SORT (, "sort" ) |
| 3125 | USE_TEST (, "test" ) |
| 3126 | USE_TOUCH (, "touch" ) |
| 3127 | USE_XARGS (, "xargs" ) |
| 3128 | }; |
| 3129 | int n = sizeof(safe_applets) / sizeof(safe_applets[0]); |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 3130 | int i; |
| 3131 | for (i = 0; i < n; i++) |
| 3132 | if (strcmp(safe_applets[i], name) == 0) |
| 3133 | return 1; |
| 3134 | |
| 3135 | return 0; |
| 3136 | } |
| 3137 | |
| 3138 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3139 | #if ENABLE_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3140 | static struct alias *atab[ATABSIZE]; |
| 3141 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3142 | static void setalias(const char *, const char *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3143 | static struct alias *freealias(struct alias *); |
| 3144 | static struct alias **__lookupalias(const char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3145 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3146 | static void |
| 3147 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3148 | { |
| 3149 | struct alias *ap, **app; |
| 3150 | |
| 3151 | app = __lookupalias(name); |
| 3152 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3153 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3154 | if (ap) { |
| 3155 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3156 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3157 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3158 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3159 | ap->flag &= ~ALIASDEAD; |
| 3160 | } else { |
| 3161 | /* not found */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 3162 | ap = ckmalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3163 | ap->name = ckstrdup(name); |
| 3164 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3165 | ap->flag = 0; |
| 3166 | ap->next = 0; |
| 3167 | *app = ap; |
| 3168 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3169 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3172 | static int |
| 3173 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3174 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3175 | struct alias **app; |
| 3176 | |
| 3177 | app = __lookupalias(name); |
| 3178 | |
| 3179 | if (*app) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3180 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3181 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3182 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3183 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3186 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3189 | static void |
| 3190 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3191 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3192 | struct alias *ap, **app; |
| 3193 | int i; |
| 3194 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3195 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3196 | for (i = 0; i < ATABSIZE; i++) { |
| 3197 | app = &atab[i]; |
| 3198 | for (ap = *app; ap; ap = *app) { |
| 3199 | *app = freealias(*app); |
| 3200 | if (ap == *app) { |
| 3201 | app = &ap->next; |
| 3202 | } |
| 3203 | } |
| 3204 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3205 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3208 | static struct alias * |
| 3209 | lookupalias(const char *name, int check) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3210 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3211 | struct alias *ap = *__lookupalias(name); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3212 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3213 | if (check && ap && (ap->flag & ALIASINUSE)) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3214 | return NULL; |
| 3215 | return ap; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3218 | /* |
| 3219 | * TODO - sort output |
| 3220 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3221 | static int |
| 3222 | aliascmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3223 | { |
| 3224 | char *n, *v; |
| 3225 | int ret = 0; |
| 3226 | struct alias *ap; |
| 3227 | |
| 3228 | if (argc == 1) { |
| 3229 | int i; |
| 3230 | |
| 3231 | for (i = 0; i < ATABSIZE; i++) |
| 3232 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3233 | printalias(ap); |
| 3234 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3235 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3236 | } |
| 3237 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3238 | v = strchr(n+1, '='); |
| 3239 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3240 | ap = *__lookupalias(n); |
| 3241 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3242 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3243 | ret = 1; |
| 3244 | } else |
| 3245 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3246 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3247 | *v++ = '\0'; |
| 3248 | setalias(n, v); |
| 3249 | } |
| 3250 | } |
| 3251 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3252 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3255 | static int |
| 3256 | unaliascmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3257 | { |
| 3258 | int i; |
| 3259 | |
| 3260 | while ((i = nextopt("a")) != '\0') { |
| 3261 | if (i == 'a') { |
| 3262 | rmaliases(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3263 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3264 | } |
| 3265 | } |
| 3266 | for (i = 0; *argptr; argptr++) { |
| 3267 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3268 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3269 | i = 1; |
| 3270 | } |
| 3271 | } |
| 3272 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3273 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3276 | static struct alias * |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 3277 | freealias(struct alias *ap) |
| 3278 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3279 | struct alias *next; |
| 3280 | |
| 3281 | if (ap->flag & ALIASINUSE) { |
| 3282 | ap->flag |= ALIASDEAD; |
| 3283 | return ap; |
| 3284 | } |
| 3285 | |
| 3286 | next = ap->next; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3287 | free(ap->name); |
| 3288 | free(ap->val); |
| 3289 | free(ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3290 | return next; |
| 3291 | } |
| 3292 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3293 | static void |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 3294 | printalias(const struct alias *ap) |
| 3295 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3296 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3297 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3298 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3299 | static struct alias ** |
| 3300 | __lookupalias(const char *name) { |
| 3301 | unsigned int hashval; |
| 3302 | struct alias **app; |
| 3303 | const char *p; |
| 3304 | unsigned int ch; |
| 3305 | |
| 3306 | p = name; |
| 3307 | |
| 3308 | ch = (unsigned char)*p; |
| 3309 | hashval = ch << 4; |
| 3310 | while (ch) { |
| 3311 | hashval += ch; |
| 3312 | ch = (unsigned char)*++p; |
| 3313 | } |
| 3314 | app = &atab[hashval % ATABSIZE]; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3315 | |
| 3316 | for (; *app; app = &(*app)->next) { |
| 3317 | if (equal(name, (*app)->name)) { |
| 3318 | break; |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | return app; |
| 3323 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3324 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3325 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3326 | /* eval.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3327 | |
| 3328 | /* |
| 3329 | * Evaluate a command. |
| 3330 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3331 | |
| 3332 | /* flags in argument to evaltree */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3333 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 3334 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
| 3335 | #define EV_BACKCMD 04 /* command executing within back quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3336 | |
| 3337 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3338 | static void evalloop(union node *, int); |
| 3339 | static void evalfor(union node *, int); |
| 3340 | static void evalcase(union node *, int); |
| 3341 | static void evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3342 | static void expredir(union node *); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3343 | static void evalpipe(union node *, int); |
| 3344 | static void evalcommand(union node *, int); |
| 3345 | static int evalbltin(const struct builtincmd *, int, char **); |
| 3346 | static int evalfun(struct funcnode *, int, char **, int); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3347 | static void prehash(union node *); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3348 | static int bltincmd(int, char **); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3349 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3350 | |
| 3351 | static const struct builtincmd bltin = { |
| 3352 | "\0\0", bltincmd |
| 3353 | }; |
| 3354 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3355 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3356 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3357 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3358 | * Evaluate a parse tree. The value is left in the global variable |
| 3359 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3360 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3361 | static void |
| 3362 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3363 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3364 | int checkexit = 0; |
| 3365 | void (*evalfn)(union node *, int); |
| 3366 | unsigned isor; |
| 3367 | int status; |
| 3368 | if (n == NULL) { |
| 3369 | TRACE(("evaltree(NULL) called\n")); |
| 3370 | goto out; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3371 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3372 | TRACE(("pid %d, evaltree(%p: %d, %d) called\n", |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 3373 | getpid(), n, n->type, flags)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3374 | switch (n->type) { |
| 3375 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 3376 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3377 | out1fmt("Node type = %d\n", n->type); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3378 | fflush(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3379 | break; |
| 3380 | #endif |
| 3381 | case NNOT: |
| 3382 | evaltree(n->nnot.com, EV_TESTED); |
| 3383 | status = !exitstatus; |
| 3384 | goto setstatus; |
| 3385 | case NREDIR: |
| 3386 | expredir(n->nredir.redirect); |
| 3387 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 3388 | if (!status) { |
| 3389 | evaltree(n->nredir.n, flags & EV_TESTED); |
| 3390 | status = exitstatus; |
| 3391 | } |
| 3392 | popredir(0); |
| 3393 | goto setstatus; |
| 3394 | case NCMD: |
| 3395 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3396 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3397 | if (eflag && !(flags & EV_TESTED)) |
| 3398 | checkexit = ~0; |
| 3399 | goto calleval; |
| 3400 | case NFOR: |
| 3401 | evalfn = evalfor; |
| 3402 | goto calleval; |
| 3403 | case NWHILE: |
| 3404 | case NUNTIL: |
| 3405 | evalfn = evalloop; |
| 3406 | goto calleval; |
| 3407 | case NSUBSHELL: |
| 3408 | case NBACKGND: |
| 3409 | evalfn = evalsubshell; |
| 3410 | goto calleval; |
| 3411 | case NPIPE: |
| 3412 | evalfn = evalpipe; |
| 3413 | goto checkexit; |
| 3414 | case NCASE: |
| 3415 | evalfn = evalcase; |
| 3416 | goto calleval; |
| 3417 | case NAND: |
| 3418 | case NOR: |
| 3419 | case NSEMI: |
| 3420 | #if NAND + 1 != NOR |
| 3421 | #error NAND + 1 != NOR |
| 3422 | #endif |
| 3423 | #if NOR + 1 != NSEMI |
| 3424 | #error NOR + 1 != NSEMI |
| 3425 | #endif |
| 3426 | isor = n->type - NAND; |
| 3427 | evaltree( |
| 3428 | n->nbinary.ch1, |
| 3429 | (flags | ((isor >> 1) - 1)) & EV_TESTED |
| 3430 | ); |
| 3431 | if (!exitstatus == isor) |
| 3432 | break; |
| 3433 | if (!evalskip) { |
| 3434 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3435 | evaln: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3436 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3437 | calleval: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3438 | evalfn(n, flags); |
| 3439 | break; |
| 3440 | } |
| 3441 | break; |
| 3442 | case NIF: |
| 3443 | evaltree(n->nif.test, EV_TESTED); |
| 3444 | if (evalskip) |
| 3445 | break; |
| 3446 | if (exitstatus == 0) { |
| 3447 | n = n->nif.ifpart; |
| 3448 | goto evaln; |
| 3449 | } else if (n->nif.elsepart) { |
| 3450 | n = n->nif.elsepart; |
| 3451 | goto evaln; |
| 3452 | } |
| 3453 | goto success; |
| 3454 | case NDEFUN: |
| 3455 | defun(n->narg.text, n->narg.next); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3456 | success: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3457 | status = 0; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3458 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3459 | exitstatus = status; |
| 3460 | break; |
| 3461 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3462 | out: |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3463 | if ((checkexit & exitstatus)) |
| 3464 | evalskip |= SKIPEVAL; |
| 3465 | else if (pendingsigs && dotrap()) |
| 3466 | goto exexit; |
| 3467 | |
| 3468 | if (flags & EV_EXIT) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3469 | exexit: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3470 | raise_exception(EXEXIT); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3471 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3474 | |
| 3475 | #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3) |
| 3476 | static |
| 3477 | #endif |
| 3478 | void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__)); |
| 3479 | |
| 3480 | |
| 3481 | static void |
| 3482 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3483 | { |
| 3484 | int status; |
| 3485 | |
| 3486 | loopnest++; |
| 3487 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3488 | flags &= EV_TESTED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3489 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3490 | int i; |
| 3491 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3492 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 3493 | if (evalskip) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3494 | skipping: |
| 3495 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3496 | evalskip = 0; |
| 3497 | continue; |
| 3498 | } |
| 3499 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 3500 | evalskip = 0; |
| 3501 | break; |
| 3502 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3503 | i = exitstatus; |
| 3504 | if (n->type != NWHILE) |
| 3505 | i = !i; |
| 3506 | if (i != 0) |
| 3507 | break; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3508 | evaltree(n->nbinary.ch2, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3509 | status = exitstatus; |
| 3510 | if (evalskip) |
| 3511 | goto skipping; |
| 3512 | } |
| 3513 | loopnest--; |
| 3514 | exitstatus = status; |
| 3515 | } |
| 3516 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3517 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3518 | static void |
| 3519 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3520 | { |
| 3521 | struct arglist arglist; |
| 3522 | union node *argp; |
| 3523 | struct strlist *sp; |
| 3524 | struct stackmark smark; |
| 3525 | |
| 3526 | setstackmark(&smark); |
| 3527 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 3528 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3529 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3530 | /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3531 | if (evalskip) |
| 3532 | goto out; |
| 3533 | } |
| 3534 | *arglist.lastp = NULL; |
| 3535 | |
| 3536 | exitstatus = 0; |
| 3537 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3538 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 3539 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3540 | setvar(n->nfor.var, sp->text, 0); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3541 | evaltree(n->nfor.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3542 | if (evalskip) { |
| 3543 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 3544 | evalskip = 0; |
| 3545 | continue; |
| 3546 | } |
| 3547 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 3548 | evalskip = 0; |
| 3549 | break; |
| 3550 | } |
| 3551 | } |
| 3552 | loopnest--; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3553 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3554 | popstackmark(&smark); |
| 3555 | } |
| 3556 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3557 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3558 | static void |
| 3559 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3560 | { |
| 3561 | union node *cp; |
| 3562 | union node *patp; |
| 3563 | struct arglist arglist; |
| 3564 | struct stackmark smark; |
| 3565 | |
| 3566 | setstackmark(&smark); |
| 3567 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3568 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3569 | exitstatus = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 3570 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 3571 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3572 | if (casematch(patp, arglist.list->text)) { |
| 3573 | if (evalskip == 0) { |
| 3574 | evaltree(cp->nclist.body, flags); |
| 3575 | } |
| 3576 | goto out; |
| 3577 | } |
| 3578 | } |
| 3579 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3580 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3581 | popstackmark(&smark); |
| 3582 | } |
| 3583 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3584 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3585 | /* |
| 3586 | * Kick off a subshell to evaluate a tree. |
| 3587 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3588 | static void |
| 3589 | evalsubshell(union node *n, int flags) |
| 3590 | { |
| 3591 | struct job *jp; |
| 3592 | int backgnd = (n->type == NBACKGND); |
| 3593 | int status; |
| 3594 | |
| 3595 | expredir(n->nredir.redirect); |
| 3596 | if (!backgnd && flags & EV_EXIT && !trap[0]) |
| 3597 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3598 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3599 | jp = makejob(n, 1); |
| 3600 | if (forkshell(jp, n, backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3601 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3602 | flags |= EV_EXIT; |
| 3603 | if (backgnd) |
| 3604 | flags &=~ EV_TESTED; |
| 3605 | nofork: |
| 3606 | redirect(n->nredir.redirect, 0); |
| 3607 | evaltreenr(n->nredir.n, flags); |
| 3608 | /* never returns */ |
| 3609 | } |
| 3610 | status = 0; |
| 3611 | if (! backgnd) |
| 3612 | status = waitforjob(jp); |
| 3613 | exitstatus = status; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3614 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
| 3617 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3618 | /* |
| 3619 | * Compute the names of the files in a redirection list. |
| 3620 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3621 | static void |
| 3622 | expredir(union node *n) |
| 3623 | { |
| 3624 | union node *redir; |
| 3625 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 3626 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3627 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 3628 | |
| 3629 | memset(&fn, 0, sizeof(fn)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3630 | fn.lastp = &fn.list; |
| 3631 | switch (redir->type) { |
| 3632 | case NFROMTO: |
| 3633 | case NFROM: |
| 3634 | case NTO: |
| 3635 | case NCLOBBER: |
| 3636 | case NAPPEND: |
| 3637 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
| 3638 | redir->nfile.expfname = fn.list->text; |
| 3639 | break; |
| 3640 | case NFROMFD: |
| 3641 | case NTOFD: |
| 3642 | if (redir->ndup.vname) { |
| 3643 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 3644 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3645 | ash_msg_and_raise_error("redir error"); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 3646 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3647 | } |
| 3648 | break; |
| 3649 | } |
| 3650 | } |
| 3651 | } |
| 3652 | |
| 3653 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3654 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3655 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 3656 | * of the process creating the pipeline. (This differs from some versions |
| 3657 | * of the shell, which make the last process in a pipeline the parent |
| 3658 | * of all the rest.) |
| 3659 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3660 | static void |
| 3661 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3662 | { |
| 3663 | struct job *jp; |
| 3664 | struct nodelist *lp; |
| 3665 | int pipelen; |
| 3666 | int prevfd; |
| 3667 | int pip[2]; |
| 3668 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3669 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3670 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 3671 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3672 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3673 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3674 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3675 | jp = makejob(n, pipelen); |
| 3676 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 3677 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3678 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3679 | pip[1] = -1; |
| 3680 | if (lp->next) { |
| 3681 | if (pipe(pip) < 0) { |
| 3682 | close(prevfd); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3683 | ash_msg_and_raise_error("Pipe call failed"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3684 | } |
| 3685 | } |
| 3686 | if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3687 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3688 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3689 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3690 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3691 | if (prevfd > 0) { |
| 3692 | dup2(prevfd, 0); |
| 3693 | close(prevfd); |
| 3694 | } |
| 3695 | if (pip[1] > 1) { |
| 3696 | dup2(pip[1], 1); |
| 3697 | close(pip[1]); |
| 3698 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3699 | evaltreenr(lp->n, flags); |
| 3700 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3701 | } |
| 3702 | if (prevfd >= 0) |
| 3703 | close(prevfd); |
| 3704 | prevfd = pip[0]; |
| 3705 | close(pip[1]); |
| 3706 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3707 | if (n->npipe.backgnd == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3708 | exitstatus = waitforjob(jp); |
| 3709 | TRACE(("evalpipe: job done exit status %d\n", exitstatus)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3710 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3711 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3714 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3715 | /* |
| 3716 | * Execute a command inside back quotes. If it's a builtin command, we |
| 3717 | * want to save its output in a block obtained from malloc. Otherwise |
| 3718 | * we fork off a subprocess and get the output of the command via a pipe. |
| 3719 | * Should be called with interrupts off. |
| 3720 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3721 | static void |
| 3722 | evalbackcmd(union node *n, struct backcmd *result) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3723 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3724 | int saveherefd; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3725 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3726 | result->fd = -1; |
| 3727 | result->buf = NULL; |
| 3728 | result->nleft = 0; |
| 3729 | result->jp = NULL; |
| 3730 | if (n == NULL) { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3731 | goto out; |
| 3732 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3733 | |
| 3734 | saveherefd = herefd; |
| 3735 | herefd = -1; |
| 3736 | |
| 3737 | { |
| 3738 | int pip[2]; |
| 3739 | struct job *jp; |
| 3740 | |
| 3741 | if (pipe(pip) < 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3742 | ash_msg_and_raise_error("Pipe call failed"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3743 | jp = makejob(n, 1); |
| 3744 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3745 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3746 | close(pip[0]); |
| 3747 | if (pip[1] != 1) { |
| 3748 | close(1); |
| 3749 | copyfd(pip[1], 1); |
| 3750 | close(pip[1]); |
| 3751 | } |
| 3752 | eflag = 0; |
| 3753 | evaltreenr(n, EV_EXIT); |
| 3754 | /* NOTREACHED */ |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3755 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3756 | close(pip[1]); |
| 3757 | result->fd = pip[0]; |
| 3758 | result->jp = jp; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3759 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3760 | herefd = saveherefd; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3761 | out: |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3762 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3763 | result->fd, result->buf, result->nleft, result->jp)); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3766 | #if ENABLE_ASH_CMDCMD |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 3767 | static char ** |
| 3768 | parse_command_args(char **argv, const char **path) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3769 | { |
| 3770 | char *cp, c; |
| 3771 | |
| 3772 | for (;;) { |
| 3773 | cp = *++argv; |
| 3774 | if (!cp) |
| 3775 | return 0; |
| 3776 | if (*cp++ != '-') |
| 3777 | break; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3778 | c = *cp++; |
| 3779 | if (!c) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3780 | break; |
| 3781 | if (c == '-' && !*cp) { |
| 3782 | argv++; |
| 3783 | break; |
| 3784 | } |
| 3785 | do { |
| 3786 | switch (c) { |
| 3787 | case 'p': |
| 3788 | *path = defpath; |
| 3789 | break; |
| 3790 | default: |
| 3791 | /* run 'typecmd' for other options */ |
| 3792 | return 0; |
| 3793 | } |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame^] | 3794 | c = *cp++; |
| 3795 | } while (c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3796 | } |
| 3797 | return argv; |
| 3798 | } |
| 3799 | #endif |
| 3800 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 3801 | static int isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 3802 | { |
| 3803 | const char *q = endofname(p); |
| 3804 | if (p == q) |
| 3805 | return 0; |
| 3806 | return *q == '='; |
| 3807 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3808 | |
| 3809 | /* |
| 3810 | * Execute a simple command. |
| 3811 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3812 | static void |
| 3813 | evalcommand(union node *cmd, int flags) |
| 3814 | { |
| 3815 | struct stackmark smark; |
| 3816 | union node *argp; |
| 3817 | struct arglist arglist; |
| 3818 | struct arglist varlist; |
| 3819 | char **argv; |
| 3820 | int argc; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3821 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3822 | struct cmdentry cmdentry; |
| 3823 | struct job *jp; |
| 3824 | char *lastarg; |
| 3825 | const char *path; |
| 3826 | int spclbltin; |
| 3827 | int cmd_is_exec; |
| 3828 | int status; |
| 3829 | char **nargv; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 3830 | struct builtincmd *bcmd; |
| 3831 | int pseudovarflag = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3832 | |
| 3833 | /* First expand the arguments. */ |
| 3834 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 3835 | setstackmark(&smark); |
| 3836 | back_exitstatus = 0; |
| 3837 | |
| 3838 | cmdentry.cmdtype = CMDBUILTIN; |
| 3839 | cmdentry.u.cmd = &bltin; |
| 3840 | varlist.lastp = &varlist.list; |
| 3841 | *varlist.lastp = NULL; |
| 3842 | arglist.lastp = &arglist.list; |
| 3843 | *arglist.lastp = NULL; |
| 3844 | |
| 3845 | argc = 0; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3846 | if (cmd->ncmd.args) { |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 3847 | bcmd = find_builtin(cmd->ncmd.args->narg.text); |
| 3848 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
| 3849 | } |
| 3850 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3851 | for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) { |
| 3852 | struct strlist **spp; |
| 3853 | |
| 3854 | spp = arglist.lastp; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 3855 | if (pseudovarflag && isassignment(argp->narg.text)) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 3856 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 3857 | else |
| 3858 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 3859 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3860 | for (sp = *spp; sp; sp = sp->next) |
| 3861 | argc++; |
| 3862 | } |
| 3863 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 3864 | argv = nargv = stalloc(sizeof(char *) * (argc + 1)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 3865 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3866 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 3867 | *nargv++ = sp->text; |
| 3868 | } |
| 3869 | *nargv = NULL; |
| 3870 | |
| 3871 | lastarg = NULL; |
| 3872 | if (iflag && funcnest == 0 && argc > 0) |
| 3873 | lastarg = nargv[-1]; |
| 3874 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3875 | preverrout_fd = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3876 | expredir(cmd->ncmd.redirect); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3877 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH|REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3878 | |
| 3879 | path = vpath.text; |
| 3880 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 3881 | struct strlist **spp; |
| 3882 | char *p; |
| 3883 | |
| 3884 | spp = varlist.lastp; |
| 3885 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 3886 | |
| 3887 | /* |
| 3888 | * Modify the command lookup path, if a PATH= assignment |
| 3889 | * is present |
| 3890 | */ |
| 3891 | p = (*spp)->text; |
| 3892 | if (varequal(p, path)) |
| 3893 | path = p; |
| 3894 | } |
| 3895 | |
| 3896 | /* Print the command if xflag is set. */ |
| 3897 | if (xflag) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3898 | int n; |
| 3899 | const char *p = " %s"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3900 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3901 | p++; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 3902 | dprintf(preverrout_fd, p, expandstr(ps4val())); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3903 | |
| 3904 | sp = varlist.list; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 3905 | for (n = 0; n < 2; n++) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3906 | while (sp) { |
| 3907 | dprintf(preverrout_fd, p, sp->text); |
| 3908 | sp = sp->next; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 3909 | if (*p == '%') { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3910 | p--; |
| 3911 | } |
| 3912 | } |
| 3913 | sp = arglist.list; |
| 3914 | } |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 3915 | full_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | cmd_is_exec = 0; |
| 3919 | spclbltin = -1; |
| 3920 | |
| 3921 | /* Now locate the command. */ |
| 3922 | if (argc) { |
| 3923 | const char *oldpath; |
| 3924 | int cmd_flag = DO_ERR; |
| 3925 | |
| 3926 | path += 5; |
| 3927 | oldpath = path; |
| 3928 | for (;;) { |
| 3929 | find_command(argv[0], &cmdentry, cmd_flag, path); |
| 3930 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
| 3931 | status = 127; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3932 | flush_stderr(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3933 | goto bail; |
| 3934 | } |
| 3935 | |
| 3936 | /* implement bltin and command here */ |
| 3937 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 3938 | break; |
| 3939 | if (spclbltin < 0) |
| 3940 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
| 3941 | if (cmdentry.u.cmd == EXECCMD) |
| 3942 | cmd_is_exec++; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3943 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3944 | if (cmdentry.u.cmd == COMMANDCMD) { |
| 3945 | |
| 3946 | path = oldpath; |
| 3947 | nargv = parse_command_args(argv, &path); |
| 3948 | if (!nargv) |
| 3949 | break; |
| 3950 | argc -= nargv - argv; |
| 3951 | argv = nargv; |
| 3952 | cmd_flag |= DO_NOFUNC; |
| 3953 | } else |
| 3954 | #endif |
| 3955 | break; |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | if (status) { |
| 3960 | /* We have a redirection error. */ |
| 3961 | if (spclbltin > 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3962 | raise_exception(EXERROR); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3963 | bail: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3964 | exitstatus = status; |
| 3965 | goto out; |
| 3966 | } |
| 3967 | |
| 3968 | /* Execute the command. */ |
| 3969 | switch (cmdentry.cmdtype) { |
| 3970 | default: |
| 3971 | /* Fork off a child process if necessary. */ |
| 3972 | if (!(flags & EV_EXIT) || trap[0]) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3973 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3974 | jp = makejob(cmd, 1); |
| 3975 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
| 3976 | exitstatus = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3977 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3978 | break; |
| 3979 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3980 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3981 | } |
| 3982 | listsetvar(varlist.list, VEXPORT|VSTACK); |
| 3983 | shellexec(argv, path, cmdentry.u.index); |
| 3984 | /* NOTREACHED */ |
| 3985 | |
| 3986 | case CMDBUILTIN: |
| 3987 | cmdenviron = varlist.list; |
| 3988 | if (cmdenviron) { |
| 3989 | struct strlist *list = cmdenviron; |
| 3990 | int i = VNOSET; |
| 3991 | if (spclbltin > 0 || argc == 0) { |
| 3992 | i = 0; |
| 3993 | if (cmd_is_exec && argc > 1) |
| 3994 | i = VEXPORT; |
| 3995 | } |
| 3996 | listsetvar(list, i); |
| 3997 | } |
| 3998 | if (evalbltin(cmdentry.u.cmd, argc, argv)) { |
| 3999 | int exit_status; |
| 4000 | int i, j; |
| 4001 | |
| 4002 | i = exception; |
| 4003 | if (i == EXEXIT) |
| 4004 | goto raise; |
| 4005 | |
| 4006 | exit_status = 2; |
| 4007 | j = 0; |
| 4008 | if (i == EXINT) |
| 4009 | j = SIGINT; |
| 4010 | if (i == EXSIG) |
| 4011 | j = pendingsigs; |
| 4012 | if (j) |
| 4013 | exit_status = j + 128; |
| 4014 | exitstatus = exit_status; |
| 4015 | |
| 4016 | if (i == EXINT || spclbltin > 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4017 | raise: |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 4018 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4019 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4020 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4021 | } |
| 4022 | break; |
| 4023 | |
| 4024 | case CMDFUNCTION: |
| 4025 | listsetvar(varlist.list, 0); |
| 4026 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 4027 | goto raise; |
| 4028 | break; |
| 4029 | } |
| 4030 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4031 | out: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4032 | popredir(cmd_is_exec); |
| 4033 | if (lastarg) |
| 4034 | /* dsl: I think this is intended to be used to support |
| 4035 | * '_' in 'vi' command mode during line editing... |
| 4036 | * However I implemented that within libedit itself. |
| 4037 | */ |
| 4038 | setvar("_", lastarg, 0); |
| 4039 | popstackmark(&smark); |
| 4040 | } |
| 4041 | |
| 4042 | static int |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4043 | evalbltin(const struct builtincmd *cmd, int argc, char **argv) |
| 4044 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4045 | char *volatile savecmdname; |
| 4046 | struct jmploc *volatile savehandler; |
| 4047 | struct jmploc jmploc; |
| 4048 | int i; |
| 4049 | |
| 4050 | savecmdname = commandname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4051 | i = setjmp(jmploc.loc); |
| 4052 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4053 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 4054 | savehandler = exception_handler; |
| 4055 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4056 | commandname = argv[0]; |
| 4057 | argptr = argv + 1; |
| 4058 | optptr = NULL; /* initialize nextopt */ |
| 4059 | exitstatus = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4060 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4061 | cmddone: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 4062 | exitstatus |= ferror(stdout); |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 4063 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4064 | commandname = savecmdname; |
| 4065 | exsig = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 4066 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4067 | |
| 4068 | return i; |
| 4069 | } |
| 4070 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 4071 | static struct localvar *localvars; |
| 4072 | |
| 4073 | /* |
| 4074 | * Called after a function returns. |
| 4075 | * Interrupts must be off. |
| 4076 | */ |
| 4077 | static void |
| 4078 | poplocalvars(void) |
| 4079 | { |
| 4080 | struct localvar *lvp; |
| 4081 | struct var *vp; |
| 4082 | |
| 4083 | while ((lvp = localvars) != NULL) { |
| 4084 | localvars = lvp->next; |
| 4085 | vp = lvp->vp; |
| 4086 | TRACE(("poplocalvar %s", vp ? vp->text : "-")); |
| 4087 | if (vp == NULL) { /* $- saved */ |
| 4088 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 4089 | free((char*)lvp->text); |
| 4090 | optschanged(); |
| 4091 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
| 4092 | unsetvar(vp->text); |
| 4093 | } else { |
| 4094 | if (vp->func) |
| 4095 | (*vp->func)(strchrnul(lvp->text, '=') + 1); |
| 4096 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 4097 | free((char*)vp->text); |
| 4098 | vp->flags = lvp->flags; |
| 4099 | vp->text = lvp->text; |
| 4100 | } |
| 4101 | free(lvp); |
| 4102 | } |
| 4103 | } |
| 4104 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4105 | static int |
| 4106 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 4107 | { |
| 4108 | volatile struct shparam saveparam; |
| 4109 | struct localvar *volatile savelocalvars; |
| 4110 | struct jmploc *volatile savehandler; |
| 4111 | struct jmploc jmploc; |
| 4112 | int e; |
| 4113 | |
| 4114 | saveparam = shellparam; |
| 4115 | savelocalvars = localvars; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4116 | e = setjmp(jmploc.loc); |
| 4117 | if (e) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4118 | goto funcdone; |
| 4119 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4120 | INT_OFF; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 4121 | savehandler = exception_handler; |
| 4122 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4123 | localvars = NULL; |
| 4124 | shellparam.malloc = 0; |
| 4125 | func->count++; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4126 | funcnest++; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4127 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4128 | shellparam.nparam = argc - 1; |
| 4129 | shellparam.p = argv + 1; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 4130 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4131 | shellparam.optind = 1; |
| 4132 | shellparam.optoff = -1; |
| 4133 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4134 | evaltree(&func->n, flags & EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4135 | funcdone: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4136 | INT_OFF; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4137 | funcnest--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4138 | freefunc(func); |
| 4139 | poplocalvars(); |
| 4140 | localvars = savelocalvars; |
| 4141 | freeparam(&shellparam); |
| 4142 | shellparam = saveparam; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 4143 | exception_handler = savehandler; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4144 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4145 | evalskip &= ~SKIPFUNC; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4146 | return e; |
| 4147 | } |
| 4148 | |
| 4149 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 4150 | static int |
| 4151 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 4152 | { |
| 4153 | return !*endofname(p); |
| 4154 | } |
| 4155 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4156 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 4157 | /* |
| 4158 | * Search for a command. This is called before we fork so that the |
| 4159 | * 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] | 4160 | * the child. The check for "goodname" is an overly conservative |
| 4161 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 4162 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4163 | static void |
| 4164 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 4165 | { |
| 4166 | struct cmdentry entry; |
| 4167 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 4168 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 4169 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4172 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4173 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4174 | /* |
| 4175 | * Builtin commands. Builtin commands whose functions are closely |
| 4176 | * tied to evaluation are implemented here. |
| 4177 | */ |
| 4178 | |
| 4179 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4180 | * No command given. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4181 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4182 | static int |
| 4183 | bltincmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4184 | { |
| 4185 | /* |
| 4186 | * Preserve exitstatus of a previous possible redirection |
| 4187 | * as POSIX mandates |
| 4188 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4189 | return back_exitstatus; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | |
| 4193 | /* |
| 4194 | * Handle break and continue commands. Break, continue, and return are |
| 4195 | * all handled by setting the evalskip flag. The evaluation routines |
| 4196 | * above all check this flag, and if it is set they start skipping |
| 4197 | * commands rather than executing them. The variable skipcount is |
| 4198 | * the number of loops to break/continue, or the number of function |
| 4199 | * levels to return. (The latter is always 1.) It should probably |
| 4200 | * be an error to break out of more loops than exist, but it isn't |
| 4201 | * in the standard shell so we don't make it one here. |
| 4202 | */ |
| 4203 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4204 | static int |
| 4205 | breakcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4206 | { |
| 4207 | int n = argc > 1 ? number(argv[1]) : 1; |
| 4208 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 4209 | if (n <= 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4210 | ash_msg_and_raise_error(illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4211 | if (n > loopnest) |
| 4212 | n = loopnest; |
| 4213 | if (n > 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4214 | evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4215 | skipcount = n; |
| 4216 | } |
| 4217 | return 0; |
| 4218 | } |
| 4219 | |
| 4220 | |
| 4221 | /* |
| 4222 | * The return command. |
| 4223 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4224 | static int |
| 4225 | returncmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4226 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4227 | /* |
| 4228 | * If called outside a function, do what ksh does; |
| 4229 | * skip the rest of the file. |
| 4230 | */ |
| 4231 | evalskip = funcnest ? SKIPFUNC : SKIPFILE; |
| 4232 | return argv[1] ? number(argv[1]) : exitstatus; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4236 | static int |
| 4237 | falsecmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4238 | { |
| 4239 | return 1; |
| 4240 | } |
| 4241 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4242 | |
| 4243 | static int |
| 4244 | truecmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4245 | { |
| 4246 | return 0; |
| 4247 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4248 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4249 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4250 | static int |
| 4251 | execcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4252 | { |
| 4253 | if (argc > 1) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4254 | iflag = 0; /* exit on error */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4255 | mflag = 0; |
| 4256 | optschanged(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4257 | shellexec(argv + 1, pathval(), 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4258 | } |
| 4259 | return 0; |
| 4260 | } |
| 4261 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4262 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4263 | /* exec.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4264 | |
| 4265 | /* |
| 4266 | * When commands are first encountered, they are entered in a hash table. |
| 4267 | * This ensures that a full path search will not have to be done for them |
| 4268 | * on each invocation. |
| 4269 | * |
| 4270 | * We should investigate converting to a linear search, even though that |
| 4271 | * would make the command name "hash" a misnomer. |
| 4272 | */ |
| 4273 | |
| 4274 | #define CMDTABLESIZE 31 /* should be prime */ |
| 4275 | #define ARB 1 /* actual size determined at run time */ |
| 4276 | |
| 4277 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4278 | struct tblentry { |
| 4279 | struct tblentry *next; /* next entry in hash chain */ |
| 4280 | union param param; /* definition of builtin function */ |
| 4281 | short cmdtype; /* index identifying command */ |
| 4282 | char rehash; /* if set, cd done since entry created */ |
| 4283 | char cmdname[ARB]; /* name of command */ |
| 4284 | }; |
| 4285 | |
| 4286 | |
| 4287 | static struct tblentry *cmdtable[CMDTABLESIZE]; |
| 4288 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 4289 | |
| 4290 | |
| 4291 | static void tryexec(char *, char **, char **); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4292 | static void clearcmdentry(int); |
| 4293 | static struct tblentry *cmdlookup(const char *, int); |
| 4294 | static void delete_cmd_entry(void); |
| 4295 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4296 | |
| 4297 | /* |
| 4298 | * Exec a program. Never returns. If you change this routine, you may |
| 4299 | * have to change the find_command routine as well. |
| 4300 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 4301 | #define environment() listvars(VEXPORT, VUNSET, 0) |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 4302 | static void shellexec(char **, const char *, int) ATTRIBUTE_NORETURN; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4303 | static void |
| 4304 | shellexec(char **argv, const char *path, int idx) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4305 | { |
| 4306 | char *cmdname; |
| 4307 | int e; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4308 | char **envp; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4309 | int exerrno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4310 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4311 | clearredir(1); |
| 4312 | envp = environment(); |
Denis Vlasenko | 62a6983 | 2007-01-10 00:24:50 +0000 | [diff] [blame] | 4313 | if (strchr(argv[0], '/') || is_safe_applet(argv[0]) |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 4314 | #if ENABLE_FEATURE_SH_STANDALONE_SHELL |
Denis Vlasenko | 8f27c34 | 2006-12-26 21:31:11 +0000 | [diff] [blame] | 4315 | || find_applet_by_name(argv[0]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4316 | #endif |
Denis Vlasenko | 8f27c34 | 2006-12-26 21:31:11 +0000 | [diff] [blame] | 4317 | ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4318 | tryexec(argv[0], argv, envp); |
| 4319 | e = errno; |
| 4320 | } else { |
| 4321 | e = ENOENT; |
| 4322 | while ((cmdname = padvance(&path, argv[0])) != NULL) { |
| 4323 | if (--idx < 0 && pathopt == NULL) { |
| 4324 | tryexec(cmdname, argv, envp); |
| 4325 | if (errno != ENOENT && errno != ENOTDIR) |
| 4326 | e = errno; |
| 4327 | } |
| 4328 | stunalloc(cmdname); |
| 4329 | } |
| 4330 | } |
| 4331 | |
| 4332 | /* Map to POSIX errors */ |
| 4333 | switch (e) { |
| 4334 | case EACCES: |
| 4335 | exerrno = 126; |
| 4336 | break; |
| 4337 | case ENOENT: |
| 4338 | exerrno = 127; |
| 4339 | break; |
| 4340 | default: |
| 4341 | exerrno = 2; |
| 4342 | break; |
| 4343 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4344 | exitstatus = exerrno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4345 | TRACE(("shellexec failed for %s, errno %d, suppressint %d\n", |
| 4346 | argv[0], e, suppressint )); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4347 | ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found")); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4348 | /* NOTREACHED */ |
| 4349 | } |
| 4350 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4351 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4352 | static void |
| 4353 | tryexec(char *cmd, char **argv, char **envp) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4354 | { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 4355 | int repeated = 0; |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 4356 | struct BB_applet *a; |
| 4357 | int argc = 0; |
| 4358 | char **c; |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 4359 | |
Denis Vlasenko | 8f27c34 | 2006-12-26 21:31:11 +0000 | [diff] [blame] | 4360 | if (strchr(cmd, '/') == NULL |
| 4361 | && (a = find_applet_by_name(cmd)) != NULL |
| 4362 | && is_safe_applet(cmd) |
| 4363 | ) { |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 4364 | c = argv; |
| 4365 | while (*c != NULL) { |
| 4366 | c++; argc++; |
| 4367 | } |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 4368 | applet_name = cmd; |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 4369 | exit(a->main(argc, argv)); |
| 4370 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 4371 | #if ENABLE_FEATURE_SH_STANDALONE_SHELL |
Denis Vlasenko | 8f27c34 | 2006-12-26 21:31:11 +0000 | [diff] [blame] | 4372 | if (find_applet_by_name(cmd) != NULL) { |
Rob Landley | 0fcd943 | 2005-05-07 08:27:34 +0000 | [diff] [blame] | 4373 | /* re-exec ourselves with the new arguments */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 4374 | execve(CONFIG_BUSYBOX_EXEC_PATH, argv, envp); |
Rob Landley | 0fcd943 | 2005-05-07 08:27:34 +0000 | [diff] [blame] | 4375 | /* If they called chroot or otherwise made the binary no longer |
| 4376 | * executable, fall through */ |
Glenn L McGrath | c00c56e | 2002-12-23 10:23:10 +0000 | [diff] [blame] | 4377 | } |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 4378 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4379 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4380 | repeat: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4381 | #ifdef SYSV |
| 4382 | do { |
| 4383 | execve(cmd, argv, envp); |
| 4384 | } while (errno == EINTR); |
| 4385 | #else |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4386 | execve(cmd, argv, envp); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4387 | #endif |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 4388 | if (repeated++) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4389 | free(argv); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 4390 | } else if (errno == ENOEXEC) { |
| 4391 | char **ap; |
| 4392 | char **new; |
| 4393 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4394 | for (ap = argv; *ap; ap++) |
| 4395 | ; |
| 4396 | ap = new = ckmalloc((ap - argv + 2) * sizeof(char *)); |
Glenn L McGrath | 47e5ca1 | 2003-09-15 12:00:19 +0000 | [diff] [blame] | 4397 | ap[1] = cmd; |
| 4398 | *ap = cmd = (char *)DEFAULT_SHELL; |
| 4399 | ap += 2; |
| 4400 | argv++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4401 | while ((*ap++ = *argv++)) |
| 4402 | ; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 4403 | argv = new; |
| 4404 | goto repeat; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4405 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4406 | } |
| 4407 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4408 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4409 | /*** Command hashing code ***/ |
| 4410 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 4411 | static void |
| 4412 | printentry(struct tblentry *cmdp) |
| 4413 | { |
| 4414 | int idx; |
| 4415 | const char *path; |
| 4416 | char *name; |
| 4417 | |
| 4418 | idx = cmdp->param.index; |
| 4419 | path = pathval(); |
| 4420 | do { |
| 4421 | name = padvance(&path, cmdp->cmdname); |
| 4422 | stunalloc(name); |
| 4423 | } while (--idx >= 0); |
| 4424 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 4425 | } |
| 4426 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4427 | |
| 4428 | static int |
| 4429 | hashcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4430 | { |
| 4431 | struct tblentry **pp; |
| 4432 | struct tblentry *cmdp; |
| 4433 | int c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4434 | struct cmdentry entry; |
| 4435 | char *name; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 4436 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4437 | while ((c = nextopt("r")) != '\0') { |
| 4438 | clearcmdentry(0); |
| 4439 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4440 | } |
| 4441 | if (*argptr == NULL) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 4442 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 4443 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4444 | if (cmdp->cmdtype == CMDNORMAL) |
| 4445 | printentry(cmdp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4446 | } |
| 4447 | } |
| 4448 | return 0; |
| 4449 | } |
| 4450 | c = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4451 | while ((name = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4452 | cmdp = cmdlookup(name, 0); |
| 4453 | if (cmdp != NULL |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4454 | && (cmdp->cmdtype == CMDNORMAL |
| 4455 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4456 | delete_cmd_entry(); |
| 4457 | find_command(name, &entry, DO_ERR, pathval()); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 4458 | if (entry.cmdtype == CMDUNKNOWN) |
| 4459 | c = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4460 | argptr++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4461 | } |
| 4462 | return c; |
| 4463 | } |
| 4464 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4465 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4466 | /* |
| 4467 | * Resolve a command name. If you change this routine, you may have to |
| 4468 | * change the shellexec routine as well. |
| 4469 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4470 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4471 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4472 | { |
| 4473 | struct tblentry *cmdp; |
| 4474 | int idx; |
| 4475 | int prev; |
| 4476 | char *fullname; |
| 4477 | struct stat statb; |
| 4478 | int e; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4479 | int updatetbl; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4480 | struct builtincmd *bcmd; |
| 4481 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4482 | /* If name contains a slash, don't use PATH or hash table */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4483 | if (strchr(name, '/') != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4484 | entry->u.index = -1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4485 | if (act & DO_ABS) { |
| 4486 | while (stat(name, &statb) < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4487 | #ifdef SYSV |
| 4488 | if (errno == EINTR) |
| 4489 | continue; |
| 4490 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4491 | entry->cmdtype = CMDUNKNOWN; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4492 | return; |
| 4493 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4494 | } |
| 4495 | entry->cmdtype = CMDNORMAL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4496 | return; |
| 4497 | } |
| 4498 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 4499 | #if ENABLE_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | bf8bf10 | 2002-09-17 08:41:08 +0000 | [diff] [blame] | 4500 | if (find_applet_by_name(name)) { |
| 4501 | entry->cmdtype = CMDNORMAL; |
| 4502 | entry->u.index = -1; |
| 4503 | return; |
| 4504 | } |
| 4505 | #endif |
| 4506 | |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 4507 | if (is_safe_applet(name)) { |
| 4508 | entry->cmdtype = CMDNORMAL; |
| 4509 | entry->u.index = -1; |
| 4510 | return; |
| 4511 | } |
| 4512 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4513 | updatetbl = (path == pathval()); |
| 4514 | if (!updatetbl) { |
| 4515 | act |= DO_ALTPATH; |
| 4516 | if (strstr(path, "%builtin") != NULL) |
| 4517 | act |= DO_ALTBLTIN; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4520 | /* If name is in the table, check answer will be ok */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4521 | cmdp = cmdlookup(name, 0); |
| 4522 | if (cmdp != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4523 | int bit; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4524 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4525 | switch (cmdp->cmdtype) { |
| 4526 | default: |
| 4527 | #if DEBUG |
| 4528 | abort(); |
| 4529 | #endif |
| 4530 | case CMDNORMAL: |
| 4531 | bit = DO_ALTPATH; |
| 4532 | break; |
| 4533 | case CMDFUNCTION: |
| 4534 | bit = DO_NOFUNC; |
| 4535 | break; |
| 4536 | case CMDBUILTIN: |
| 4537 | bit = DO_ALTBLTIN; |
| 4538 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4539 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4540 | if (act & bit) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4541 | updatetbl = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4542 | cmdp = NULL; |
| 4543 | } else if (cmdp->rehash == 0) |
| 4544 | /* if not invalidated by cd, we're done */ |
| 4545 | goto success; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | /* If %builtin not in path, check for builtin next */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4549 | bcmd = find_builtin(name); |
| 4550 | if (bcmd && (IS_BUILTIN_REGULAR(bcmd) || ( |
| 4551 | act & DO_ALTPATH ? !(act & DO_ALTBLTIN) : builtinloc <= 0 |
| 4552 | ))) |
| 4553 | goto builtin_success; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4554 | |
| 4555 | /* We have to search path. */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4556 | prev = -1; /* where to start */ |
| 4557 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4558 | if (cmdp->cmdtype == CMDBUILTIN) |
| 4559 | prev = builtinloc; |
| 4560 | else |
| 4561 | prev = cmdp->param.index; |
| 4562 | } |
| 4563 | |
| 4564 | e = ENOENT; |
| 4565 | idx = -1; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4566 | loop: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4567 | while ((fullname = padvance(&path, name)) != NULL) { |
| 4568 | stunalloc(fullname); |
| 4569 | idx++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4570 | if (pathopt) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4571 | if (prefix(pathopt, "builtin")) { |
| 4572 | if (bcmd) |
| 4573 | goto builtin_success; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4574 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4575 | } else if (!(act & DO_NOFUNC) && |
| 4576 | prefix(pathopt, "func")) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4577 | /* handled below */ |
| 4578 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4579 | /* ignore unimplemented options */ |
| 4580 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4581 | } |
| 4582 | } |
| 4583 | /* if rehash, don't redo absolute path names */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4584 | if (fullname[0] == '/' && idx <= prev) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4585 | if (idx < prev) |
| 4586 | continue; |
| 4587 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 4588 | goto success; |
| 4589 | } |
| 4590 | while (stat(fullname, &statb) < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4591 | #ifdef SYSV |
| 4592 | if (errno == EINTR) |
| 4593 | continue; |
| 4594 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4595 | if (errno != ENOENT && errno != ENOTDIR) |
| 4596 | e = errno; |
| 4597 | goto loop; |
| 4598 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4599 | e = EACCES; /* if we fail, this will be the error */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4600 | if (!S_ISREG(statb.st_mode)) |
| 4601 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4602 | if (pathopt) { /* this is a %func directory */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4603 | stalloc(strlen(fullname) + 1); |
| 4604 | readcmdfile(fullname); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4605 | cmdp = cmdlookup(name, 0); |
| 4606 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4607 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4608 | stunalloc(fullname); |
| 4609 | goto success; |
| 4610 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4611 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4612 | if (!updatetbl) { |
| 4613 | entry->cmdtype = CMDNORMAL; |
| 4614 | entry->u.index = idx; |
| 4615 | return; |
| 4616 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4617 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4618 | cmdp = cmdlookup(name, 1); |
| 4619 | cmdp->cmdtype = CMDNORMAL; |
| 4620 | cmdp->param.index = idx; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4621 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4622 | goto success; |
| 4623 | } |
| 4624 | |
| 4625 | /* We failed. If there was an entry for this command, delete it */ |
| 4626 | if (cmdp && updatetbl) |
| 4627 | delete_cmd_entry(); |
| 4628 | if (act & DO_ERR) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4629 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4630 | entry->cmdtype = CMDUNKNOWN; |
| 4631 | return; |
| 4632 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4633 | builtin_success: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4634 | if (!updatetbl) { |
| 4635 | entry->cmdtype = CMDBUILTIN; |
| 4636 | entry->u.cmd = bcmd; |
| 4637 | return; |
| 4638 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4639 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4640 | cmdp = cmdlookup(name, 1); |
| 4641 | cmdp->cmdtype = CMDBUILTIN; |
| 4642 | cmdp->param.cmd = bcmd; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4643 | INT_ON; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4644 | success: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4645 | cmdp->rehash = 0; |
| 4646 | entry->cmdtype = cmdp->cmdtype; |
| 4647 | entry->u = cmdp->param; |
| 4648 | } |
| 4649 | |
| 4650 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4651 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4652 | * Search the table of builtin commands. |
| 4653 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4654 | static struct builtincmd * |
| 4655 | find_builtin(const char *name) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4656 | { |
| 4657 | struct builtincmd *bp; |
| 4658 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4659 | bp = bsearch( |
| 4660 | name, builtincmd, NUMBUILTINS, sizeof(struct builtincmd), |
| 4661 | pstrcmp |
| 4662 | ); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4663 | return bp; |
| 4664 | } |
| 4665 | |
| 4666 | |
| 4667 | /* |
| 4668 | * Called when a cd is done. Marks all commands so the next time they |
| 4669 | * are executed they will be rehashed. |
| 4670 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4671 | static void |
| 4672 | hashcd(void) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4673 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4674 | struct tblentry **pp; |
| 4675 | struct tblentry *cmdp; |
| 4676 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 4677 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 4678 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4679 | if (cmdp->cmdtype == CMDNORMAL || ( |
| 4680 | cmdp->cmdtype == CMDBUILTIN && |
| 4681 | !(IS_BUILTIN_REGULAR(cmdp->param.cmd)) && |
| 4682 | builtinloc > 0 |
| 4683 | )) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4684 | cmdp->rehash = 1; |
| 4685 | } |
| 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4690 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4691 | * Fix command hash table when PATH changed. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4692 | * Called before PATH is changed. The argument is the new value of PATH; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4693 | * pathval() still returns the old value at this point. |
| 4694 | * Called with interrupts off. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4695 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4696 | static void |
| 4697 | changepath(const char *newval) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4698 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4699 | const char *old, *new; |
| 4700 | int idx; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4701 | int firstchange; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4702 | int idx_bltin; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4703 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4704 | old = pathval(); |
| 4705 | new = newval; |
| 4706 | firstchange = 9999; /* assume no change */ |
| 4707 | idx = 0; |
| 4708 | idx_bltin = -1; |
| 4709 | for (;;) { |
| 4710 | if (*old != *new) { |
| 4711 | firstchange = idx; |
| 4712 | if ((*old == '\0' && *new == ':') |
| 4713 | || (*old == ':' && *new == '\0')) |
| 4714 | firstchange++; |
| 4715 | old = new; /* ignore subsequent differences */ |
| 4716 | } |
| 4717 | if (*new == '\0') |
| 4718 | break; |
| 4719 | if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin")) |
| 4720 | idx_bltin = idx; |
| 4721 | if (*new == ':') { |
| 4722 | idx++; |
| 4723 | } |
| 4724 | new++, old++; |
| 4725 | } |
| 4726 | if (builtinloc < 0 && idx_bltin >= 0) |
| 4727 | builtinloc = idx_bltin; /* zap builtins */ |
| 4728 | if (builtinloc >= 0 && idx_bltin < 0) |
| 4729 | firstchange = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4730 | clearcmdentry(firstchange); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4731 | builtinloc = idx_bltin; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | |
| 4735 | /* |
| 4736 | * Clear out command entries. The argument specifies the first entry in |
| 4737 | * PATH which has changed. |
| 4738 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4739 | static void |
| 4740 | clearcmdentry(int firstchange) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4741 | { |
| 4742 | struct tblentry **tblp; |
| 4743 | struct tblentry **pp; |
| 4744 | struct tblentry *cmdp; |
| 4745 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4746 | INT_OFF; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 4747 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4748 | pp = tblp; |
| 4749 | while ((cmdp = *pp) != NULL) { |
| 4750 | if ((cmdp->cmdtype == CMDNORMAL && |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4751 | cmdp->param.index >= firstchange) |
| 4752 | || (cmdp->cmdtype == CMDBUILTIN && |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 4753 | builtinloc >= firstchange) |
| 4754 | ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4755 | *pp = cmdp->next; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4756 | free(cmdp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4757 | } else { |
| 4758 | pp = &cmdp->next; |
| 4759 | } |
| 4760 | } |
| 4761 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4762 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
| 4765 | |
| 4766 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4767 | * Locate a command in the command hash table. If "add" is nonzero, |
| 4768 | * add the command to the table if it is not already present. The |
| 4769 | * variable "lastcmdentry" is set to point to the address of the link |
| 4770 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 4771 | * entry. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4772 | * |
| 4773 | * Interrupts must be off if called with add != 0. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4774 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4775 | static struct tblentry **lastcmdentry; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4776 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4777 | static struct tblentry * |
| 4778 | cmdlookup(const char *name, int add) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4779 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4780 | unsigned int hashval; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4781 | const char *p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4782 | struct tblentry *cmdp; |
| 4783 | struct tblentry **pp; |
| 4784 | |
| 4785 | p = name; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4786 | hashval = (unsigned char)*p << 4; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4787 | while (*p) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4788 | hashval += (unsigned char)*p++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4789 | hashval &= 0x7FFF; |
| 4790 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 4791 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4792 | if (equal(cmdp->cmdname, name)) |
| 4793 | break; |
| 4794 | pp = &cmdp->next; |
| 4795 | } |
| 4796 | if (add && cmdp == NULL) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 4797 | cmdp = *pp = ckmalloc(sizeof(struct tblentry) - ARB |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4798 | + strlen(name) + 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4799 | cmdp->next = NULL; |
| 4800 | cmdp->cmdtype = CMDUNKNOWN; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4801 | strcpy(cmdp->cmdname, name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4802 | } |
| 4803 | lastcmdentry = pp; |
| 4804 | return cmdp; |
| 4805 | } |
| 4806 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4807 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4808 | /* |
| 4809 | * Delete the command entry returned on the last lookup. |
| 4810 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4811 | static void |
| 4812 | delete_cmd_entry(void) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4813 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4814 | struct tblentry *cmdp; |
| 4815 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4816 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4817 | cmdp = *lastcmdentry; |
| 4818 | *lastcmdentry = cmdp->next; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4819 | if (cmdp->cmdtype == CMDFUNCTION) |
| 4820 | freefunc(cmdp->param.func); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4821 | free(cmdp); |
| 4822 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4823 | } |
| 4824 | |
| 4825 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4826 | /* |
| 4827 | * Add a new command entry, replacing any existing command entry for |
| 4828 | * the same name - except special builtins. |
| 4829 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 4830 | static void addcmdentry(char *name, struct cmdentry *entry) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4831 | { |
| 4832 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4833 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4834 | cmdp = cmdlookup(name, 1); |
| 4835 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 4836 | freefunc(cmdp->param.func); |
| 4837 | } |
| 4838 | cmdp->cmdtype = entry->cmdtype; |
| 4839 | cmdp->param = entry->u; |
| 4840 | cmdp->rehash = 0; |
| 4841 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4842 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4843 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4844 | /* |
| 4845 | * Make a copy of a parse tree. |
| 4846 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 4847 | static struct funcnode * copyfunc(union node *n) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4848 | { |
| 4849 | struct funcnode *f; |
| 4850 | size_t blocksize; |
| 4851 | |
| 4852 | funcblocksize = offsetof(struct funcnode, n); |
| 4853 | funcstringsize = 0; |
| 4854 | calcsize(n); |
| 4855 | blocksize = funcblocksize; |
| 4856 | f = ckmalloc(blocksize + funcstringsize); |
| 4857 | funcblock = (char *) f + offsetof(struct funcnode, n); |
| 4858 | funcstring = (char *) f + blocksize; |
| 4859 | copynode(n); |
| 4860 | f->count = 0; |
| 4861 | return f; |
| 4862 | } |
| 4863 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4864 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4865 | /* |
| 4866 | * Define a shell function. |
| 4867 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4868 | static void |
| 4869 | defun(char *name, union node *func) |
| 4870 | { |
| 4871 | struct cmdentry entry; |
| 4872 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4873 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4874 | entry.cmdtype = CMDFUNCTION; |
| 4875 | entry.u.func = copyfunc(func); |
| 4876 | addcmdentry(name, &entry); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 4877 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4878 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4879 | |
| 4880 | |
| 4881 | /* |
| 4882 | * Delete a function if it exists. |
| 4883 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4884 | static void |
| 4885 | unsetfunc(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4886 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4887 | struct tblentry *cmdp; |
| 4888 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 4889 | cmdp = cmdlookup(name, 0); |
| 4890 | if (cmdp!= NULL && cmdp->cmdtype == CMDFUNCTION) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4891 | delete_cmd_entry(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4894 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4895 | * Locate and print what a word is... |
| 4896 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 4897 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4898 | static int |
| 4899 | describe_command(char *command, int describe_command_verbose) |
| 4900 | #else |
| 4901 | #define describe_command_verbose 1 |
| 4902 | static int |
| 4903 | describe_command(char *command) |
| 4904 | #endif |
| 4905 | { |
| 4906 | struct cmdentry entry; |
| 4907 | struct tblentry *cmdp; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 4908 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4909 | const struct alias *ap; |
| 4910 | #endif |
| 4911 | const char *path = pathval(); |
| 4912 | |
| 4913 | if (describe_command_verbose) { |
| 4914 | out1str(command); |
| 4915 | } |
| 4916 | |
| 4917 | /* First look at the keywords */ |
| 4918 | if (findkwd(command)) { |
| 4919 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 4920 | goto out; |
| 4921 | } |
| 4922 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 4923 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4924 | /* Then look at the aliases */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4925 | ap = lookupalias(command, 0); |
| 4926 | if (ap != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4927 | if (describe_command_verbose) { |
| 4928 | out1fmt(" is an alias for %s", ap->val); |
| 4929 | } else { |
| 4930 | out1str("alias "); |
| 4931 | printalias(ap); |
| 4932 | return 0; |
| 4933 | } |
| 4934 | goto out; |
| 4935 | } |
| 4936 | #endif |
| 4937 | /* Then check if it is a tracked alias */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4938 | cmdp = cmdlookup(command, 0); |
| 4939 | if (cmdp != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4940 | entry.cmdtype = cmdp->cmdtype; |
| 4941 | entry.u = cmdp->param; |
| 4942 | } else { |
| 4943 | /* Finally use brute force */ |
| 4944 | find_command(command, &entry, DO_ABS, path); |
| 4945 | } |
| 4946 | |
| 4947 | switch (entry.cmdtype) { |
| 4948 | case CMDNORMAL: { |
| 4949 | int j = entry.u.index; |
| 4950 | char *p; |
| 4951 | if (j == -1) { |
| 4952 | p = command; |
| 4953 | } else { |
| 4954 | do { |
| 4955 | p = padvance(&path, command); |
| 4956 | stunalloc(p); |
| 4957 | } while (--j >= 0); |
| 4958 | } |
| 4959 | if (describe_command_verbose) { |
| 4960 | out1fmt(" is%s %s", |
| 4961 | (cmdp ? " a tracked alias for" : nullstr), p |
| 4962 | ); |
| 4963 | } else { |
| 4964 | out1str(p); |
| 4965 | } |
| 4966 | break; |
| 4967 | } |
| 4968 | |
| 4969 | case CMDFUNCTION: |
| 4970 | if (describe_command_verbose) { |
| 4971 | out1str(" is a shell function"); |
| 4972 | } else { |
| 4973 | out1str(command); |
| 4974 | } |
| 4975 | break; |
| 4976 | |
| 4977 | case CMDBUILTIN: |
| 4978 | if (describe_command_verbose) { |
| 4979 | out1fmt(" is a %sshell builtin", |
| 4980 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 4981 | "special " : nullstr |
| 4982 | ); |
| 4983 | } else { |
| 4984 | out1str(command); |
| 4985 | } |
| 4986 | break; |
| 4987 | |
| 4988 | default: |
| 4989 | if (describe_command_verbose) { |
| 4990 | out1str(": not found\n"); |
| 4991 | } |
| 4992 | return 127; |
| 4993 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 4994 | out: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 4995 | outstr("\n", stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4996 | return 0; |
| 4997 | } |
| 4998 | |
| 4999 | static int |
| 5000 | typecmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5001 | { |
| 5002 | int i; |
| 5003 | int err = 0; |
| 5004 | |
| 5005 | for (i = 1; i < argc; i++) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 5006 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5007 | err |= describe_command(argv[i], 1); |
| 5008 | #else |
| 5009 | err |= describe_command(argv[i]); |
| 5010 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5011 | } |
| 5012 | return err; |
| 5013 | } |
| 5014 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 5015 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5016 | static int |
| 5017 | commandcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5018 | { |
| 5019 | int c; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 5020 | enum { |
| 5021 | VERIFY_BRIEF = 1, |
| 5022 | VERIFY_VERBOSE = 2, |
| 5023 | } verify = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5024 | |
| 5025 | while ((c = nextopt("pvV")) != '\0') |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 5026 | if (c == 'V') |
| 5027 | verify |= VERIFY_VERBOSE; |
| 5028 | else if (c == 'v') |
| 5029 | verify |= VERIFY_BRIEF; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 5030 | #if DEBUG |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 5031 | else if (c != 'p') |
| 5032 | abort(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5033 | #endif |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 5034 | if (verify) |
| 5035 | return describe_command(*argptr, verify - VERIFY_BRIEF); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5036 | |
| 5037 | return 0; |
| 5038 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5039 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5040 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 5041 | /* expand.c */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 5042 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5043 | /* |
| 5044 | * Routines to expand arguments to commands. We have to deal with |
| 5045 | * backquotes, shell variables, and file metacharacters. |
| 5046 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5047 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5048 | /* |
| 5049 | * _rmescape() flags |
| 5050 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5051 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 5052 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
| 5053 | #define RMESCAPE_QUOTED 0x4 /* Remove CTLESC unless in quotes */ |
| 5054 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 5055 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5056 | |
| 5057 | /* |
| 5058 | * Structure specifying which parts of the string should be searched |
| 5059 | * for IFS characters. |
| 5060 | */ |
| 5061 | |
| 5062 | struct ifsregion { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5063 | struct ifsregion *next; /* next region in list */ |
| 5064 | int begoff; /* offset of start of region */ |
| 5065 | int endoff; /* offset of end of region */ |
| 5066 | int nulonly; /* search for nul bytes only */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5067 | }; |
| 5068 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5069 | /* output of current string */ |
| 5070 | static char *expdest; |
| 5071 | /* list of back quote expressions */ |
| 5072 | static struct nodelist *argbackq; |
| 5073 | /* first struct in list of ifs regions */ |
| 5074 | static struct ifsregion ifsfirst; |
| 5075 | /* last struct in list */ |
| 5076 | static struct ifsregion *ifslastp; |
| 5077 | /* holds expanded arg list */ |
| 5078 | static struct arglist exparg; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5079 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 5080 | static void argstr(char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5081 | static char *exptilde(char *, char *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 5082 | static void expbackq(union node *, int, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5083 | static const char *subevalvar(char *, char *, int, int, int, int, int); |
| 5084 | static char *evalvar(char *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 5085 | static void strtodest(const char *, int, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5086 | static void memtodest(const char *p, size_t len, int syntax, int quotes); |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5087 | static ssize_t varvalue(char *, int, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 5088 | static void recordregion(int, int, int); |
| 5089 | static void removerecordregions(int); |
| 5090 | static void ifsbreakup(char *, struct arglist *); |
| 5091 | static void ifsfree(void); |
| 5092 | static void expandmeta(struct strlist *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5093 | static int patmatch(char *, const char *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 5094 | |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 5095 | static int cvtnum(arith_t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5096 | static size_t esclen(const char *, const char *); |
| 5097 | static char *scanleft(char *, char *, char *, char *, int, int); |
| 5098 | static char *scanright(char *, char *, char *, char *, int, int); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 5099 | static void varunset(const char *, const char *, const char *, int) ATTRIBUTE_NORETURN; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 5100 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5101 | |
| 5102 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 5103 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5104 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5105 | * |
| 5106 | * Returns an stalloced string. |
| 5107 | */ |
| 5108 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5109 | static char * preglob(const char *pattern, int quoted, int flag) |
| 5110 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5111 | flag |= RMESCAPE_GLOB; |
| 5112 | if (quoted) { |
| 5113 | flag |= RMESCAPE_QUOTED; |
| 5114 | } |
| 5115 | return _rmescapes((char *)pattern, flag); |
| 5116 | } |
| 5117 | |
| 5118 | |
| 5119 | static size_t |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 5120 | esclen(const char *start, const char *p) |
| 5121 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5122 | size_t esc = 0; |
| 5123 | |
| 5124 | while (p > start && *--p == CTLESC) { |
| 5125 | esc++; |
| 5126 | } |
| 5127 | return esc; |
| 5128 | } |
| 5129 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5130 | |
| 5131 | /* |
| 5132 | * Expand shell variables and backquotes inside a here document. |
| 5133 | */ |
| 5134 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 5135 | static void expandhere(union node *arg, int fd) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5136 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5137 | herefd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5138 | expandarg(arg, (struct arglist *)NULL, 0); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 5139 | full_write(fd, stackblock(), expdest - (char *)stackblock()); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | |
| 5143 | /* |
| 5144 | * Perform variable substitution and command substitution on an argument, |
| 5145 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 5146 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 5147 | * here document expansion. |
| 5148 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5149 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5150 | expandarg(union node *arg, struct arglist *arglist, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5151 | { |
| 5152 | struct strlist *sp; |
| 5153 | char *p; |
| 5154 | |
| 5155 | argbackq = arg->narg.backquote; |
| 5156 | STARTSTACKSTR(expdest); |
| 5157 | ifsfirst.next = NULL; |
| 5158 | ifslastp = NULL; |
| 5159 | argstr(arg->narg.text, flag); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 5160 | p = _STPUTC('\0', expdest); |
| 5161 | expdest = p - 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5162 | if (arglist == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5163 | return; /* here document expanded */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5164 | } |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 5165 | p = grabstackstr(p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5166 | exparg.lastp = &exparg.list; |
| 5167 | /* |
| 5168 | * TODO - EXP_REDIR |
| 5169 | */ |
| 5170 | if (flag & EXP_FULL) { |
| 5171 | ifsbreakup(p, &exparg); |
| 5172 | *exparg.lastp = NULL; |
| 5173 | exparg.lastp = &exparg.list; |
| 5174 | expandmeta(exparg.list, flag); |
| 5175 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5176 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5177 | rmescapes(p); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 5178 | sp = stalloc(sizeof(*sp)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5179 | sp->text = p; |
| 5180 | *exparg.lastp = sp; |
| 5181 | exparg.lastp = &sp->next; |
| 5182 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5183 | if (ifsfirst.next) |
| 5184 | ifsfree(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5185 | *exparg.lastp = NULL; |
| 5186 | if (exparg.list) { |
| 5187 | *arglist->lastp = exparg.list; |
| 5188 | arglist->lastp = exparg.lastp; |
| 5189 | } |
| 5190 | } |
| 5191 | |
| 5192 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5193 | /* |
| 5194 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 5195 | * characters to allow for further processing. Otherwise treat |
| 5196 | * $@ like $* since no splitting will be performed. |
| 5197 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5198 | static void |
| 5199 | argstr(char *p, int flag) |
| 5200 | { |
| 5201 | static const char spclchars[] = { |
| 5202 | '=', |
| 5203 | ':', |
| 5204 | CTLQUOTEMARK, |
| 5205 | CTLENDVAR, |
| 5206 | CTLESC, |
| 5207 | CTLVAR, |
| 5208 | CTLBACKQ, |
| 5209 | CTLBACKQ | CTLQUOTE, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 5210 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5211 | CTLENDARI, |
| 5212 | #endif |
| 5213 | 0 |
| 5214 | }; |
| 5215 | const char *reject = spclchars; |
| 5216 | int c; |
| 5217 | int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */ |
| 5218 | int breakall = flag & EXP_WORD; |
| 5219 | int inquotes; |
| 5220 | size_t length; |
| 5221 | int startloc; |
| 5222 | |
| 5223 | if (!(flag & EXP_VARTILDE)) { |
| 5224 | reject += 2; |
| 5225 | } else if (flag & EXP_VARTILDE2) { |
| 5226 | reject++; |
| 5227 | } |
| 5228 | inquotes = 0; |
| 5229 | length = 0; |
| 5230 | if (flag & EXP_TILDE) { |
| 5231 | char *q; |
| 5232 | |
| 5233 | flag &= ~EXP_TILDE; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5234 | tilde: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5235 | q = p; |
| 5236 | if (*q == CTLESC && (flag & EXP_QWORD)) |
| 5237 | q++; |
| 5238 | if (*q == '~') |
| 5239 | p = exptilde(p, q, flag); |
| 5240 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5241 | start: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5242 | startloc = expdest - (char *)stackblock(); |
| 5243 | for (;;) { |
| 5244 | length += strcspn(p + length, reject); |
| 5245 | c = p[length]; |
| 5246 | if (c && (!(c & 0x80) |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 5247 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5248 | || c == CTLENDARI |
| 5249 | #endif |
| 5250 | )) { |
| 5251 | /* c == '=' || c == ':' || c == CTLENDARI */ |
| 5252 | length++; |
| 5253 | } |
| 5254 | if (length > 0) { |
| 5255 | int newloc; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 5256 | expdest = stack_nputstr(p, length, expdest); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5257 | newloc = expdest - (char *)stackblock(); |
| 5258 | if (breakall && !inquotes && newloc > startloc) { |
| 5259 | recordregion(startloc, newloc, 0); |
| 5260 | } |
| 5261 | startloc = newloc; |
| 5262 | } |
| 5263 | p += length + 1; |
| 5264 | length = 0; |
| 5265 | |
| 5266 | switch (c) { |
| 5267 | case '\0': |
| 5268 | goto breakloop; |
| 5269 | case '=': |
| 5270 | if (flag & EXP_VARTILDE2) { |
| 5271 | p--; |
| 5272 | continue; |
| 5273 | } |
| 5274 | flag |= EXP_VARTILDE2; |
| 5275 | reject++; |
| 5276 | /* fall through */ |
| 5277 | case ':': |
| 5278 | /* |
| 5279 | * sort of a hack - expand tildes in variable |
| 5280 | * assignments (after the first '=' and after ':'s). |
| 5281 | */ |
| 5282 | if (*--p == '~') { |
| 5283 | goto tilde; |
| 5284 | } |
| 5285 | continue; |
| 5286 | } |
| 5287 | |
| 5288 | switch (c) { |
| 5289 | case CTLENDVAR: /* ??? */ |
| 5290 | goto breakloop; |
| 5291 | case CTLQUOTEMARK: |
| 5292 | /* "$@" syntax adherence hack */ |
| 5293 | if ( |
| 5294 | !inquotes && |
| 5295 | !memcmp(p, dolatstr, DOLATSTRLEN) && |
| 5296 | (p[4] == CTLQUOTEMARK || ( |
| 5297 | p[4] == CTLENDVAR && |
| 5298 | p[5] == CTLQUOTEMARK |
| 5299 | )) |
| 5300 | ) { |
| 5301 | p = evalvar(p + 1, flag) + 1; |
| 5302 | goto start; |
| 5303 | } |
| 5304 | inquotes = !inquotes; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5305 | addquote: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5306 | if (quotes) { |
| 5307 | p--; |
| 5308 | length++; |
| 5309 | startloc++; |
| 5310 | } |
| 5311 | break; |
| 5312 | case CTLESC: |
| 5313 | startloc++; |
| 5314 | length++; |
| 5315 | goto addquote; |
| 5316 | case CTLVAR: |
| 5317 | p = evalvar(p, flag); |
| 5318 | goto start; |
| 5319 | case CTLBACKQ: |
| 5320 | c = 0; |
| 5321 | case CTLBACKQ|CTLQUOTE: |
| 5322 | expbackq(argbackq->n, c, quotes); |
| 5323 | argbackq = argbackq->next; |
| 5324 | goto start; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 5325 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5326 | case CTLENDARI: |
| 5327 | p--; |
| 5328 | expari(quotes); |
| 5329 | goto start; |
| 5330 | #endif |
| 5331 | } |
| 5332 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5333 | breakloop: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5334 | ; |
| 5335 | } |
| 5336 | |
| 5337 | static char * |
| 5338 | exptilde(char *startp, char *p, int flag) |
| 5339 | { |
| 5340 | char c; |
| 5341 | char *name; |
| 5342 | struct passwd *pw; |
| 5343 | const char *home; |
| 5344 | int quotes = flag & (EXP_FULL | EXP_CASE); |
| 5345 | int startloc; |
| 5346 | |
| 5347 | name = p + 1; |
| 5348 | |
| 5349 | while ((c = *++p) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 5350 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5351 | case CTLESC: |
Denis Vlasenko | 097c324 | 2006-11-27 16:59:15 +0000 | [diff] [blame] | 5352 | return startp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5353 | case CTLQUOTEMARK: |
Denis Vlasenko | 097c324 | 2006-11-27 16:59:15 +0000 | [diff] [blame] | 5354 | return startp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5355 | case ':': |
| 5356 | if (flag & EXP_VARTILDE) |
| 5357 | goto done; |
| 5358 | break; |
| 5359 | case '/': |
| 5360 | case CTLENDVAR: |
| 5361 | goto done; |
| 5362 | } |
| 5363 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5364 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5365 | *p = '\0'; |
| 5366 | if (*name == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 5367 | home = lookupvar(homestr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5368 | } else { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5369 | pw = getpwnam(name); |
| 5370 | if (pw == NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5371 | goto lose; |
| 5372 | home = pw->pw_dir; |
| 5373 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 5374 | if (!home || !*home) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5375 | goto lose; |
| 5376 | *p = c; |
| 5377 | startloc = expdest - (char *)stackblock(); |
| 5378 | strtodest(home, SQSYNTAX, quotes); |
| 5379 | recordregion(startloc, expdest - (char *)stackblock(), 0); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 5380 | return p; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5381 | lose: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5382 | *p = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 5383 | return startp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5384 | } |
| 5385 | |
| 5386 | |
| 5387 | static void |
| 5388 | removerecordregions(int endoff) |
| 5389 | { |
| 5390 | if (ifslastp == NULL) |
| 5391 | return; |
| 5392 | |
| 5393 | if (ifsfirst.endoff > endoff) { |
| 5394 | while (ifsfirst.next != NULL) { |
| 5395 | struct ifsregion *ifsp; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 5396 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5397 | ifsp = ifsfirst.next->next; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 5398 | free(ifsfirst.next); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5399 | ifsfirst.next = ifsp; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 5400 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5401 | } |
| 5402 | if (ifsfirst.begoff > endoff) |
| 5403 | ifslastp = NULL; |
| 5404 | else { |
| 5405 | ifslastp = &ifsfirst; |
| 5406 | ifsfirst.endoff = endoff; |
| 5407 | } |
| 5408 | return; |
| 5409 | } |
| 5410 | |
| 5411 | ifslastp = &ifsfirst; |
| 5412 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
| 5413 | ifslastp=ifslastp->next; |
| 5414 | while (ifslastp->next != NULL) { |
| 5415 | struct ifsregion *ifsp; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 5416 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5417 | ifsp = ifslastp->next->next; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 5418 | free(ifslastp->next); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5419 | ifslastp->next = ifsp; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 5420 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5421 | } |
| 5422 | if (ifslastp->endoff > endoff) |
| 5423 | ifslastp->endoff = endoff; |
| 5424 | } |
| 5425 | |
| 5426 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 5427 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5428 | /* |
| 5429 | * Expand arithmetic expression. Backup to start of expression, |
| 5430 | * evaluate, place result in (backed up) result, adjust string position. |
| 5431 | */ |
| 5432 | void |
| 5433 | expari(int quotes) |
| 5434 | { |
| 5435 | char *p, *start; |
| 5436 | int begoff; |
| 5437 | int flag; |
| 5438 | int len; |
| 5439 | |
| 5440 | /* ifsfree(); */ |
| 5441 | |
| 5442 | /* |
| 5443 | * This routine is slightly over-complicated for |
| 5444 | * efficiency. Next we scan backwards looking for the |
| 5445 | * start of arithmetic. |
| 5446 | */ |
| 5447 | start = stackblock(); |
| 5448 | p = expdest - 1; |
| 5449 | *p = '\0'; |
| 5450 | p--; |
| 5451 | do { |
| 5452 | int esc; |
| 5453 | |
| 5454 | while (*p != CTLARI) { |
| 5455 | p--; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 5456 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5457 | if (p < start) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 5458 | ash_msg_and_raise_error("missing CTLARI (shouldn't happen)"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5459 | } |
| 5460 | #endif |
| 5461 | } |
| 5462 | |
| 5463 | esc = esclen(start, p); |
| 5464 | if (!(esc % 2)) { |
| 5465 | break; |
| 5466 | } |
| 5467 | |
| 5468 | p -= esc + 1; |
| 5469 | } while (1); |
| 5470 | |
| 5471 | begoff = p - start; |
| 5472 | |
| 5473 | removerecordregions(begoff); |
| 5474 | |
| 5475 | flag = p[1]; |
| 5476 | |
| 5477 | expdest = p; |
| 5478 | |
| 5479 | if (quotes) |
| 5480 | rmescapes(p + 2); |
| 5481 | |
| 5482 | len = cvtnum(dash_arith(p + 2)); |
| 5483 | |
| 5484 | if (flag != '"') |
| 5485 | recordregion(begoff, begoff + len, 0); |
| 5486 | } |
| 5487 | #endif |
| 5488 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5489 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5490 | /* |
| 5491 | * Expand stuff in backwards quotes. |
| 5492 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5493 | static void |
| 5494 | expbackq(union node *cmd, int quoted, int quotes) |
| 5495 | { |
| 5496 | struct backcmd in; |
| 5497 | int i; |
| 5498 | char buf[128]; |
| 5499 | char *p; |
| 5500 | char *dest; |
| 5501 | int startloc; |
| 5502 | int syntax = quoted? DQSYNTAX : BASESYNTAX; |
| 5503 | struct stackmark smark; |
| 5504 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 5505 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5506 | setstackmark(&smark); |
| 5507 | dest = expdest; |
| 5508 | startloc = dest - (char *)stackblock(); |
| 5509 | grabstackstr(dest); |
| 5510 | evalbackcmd(cmd, (struct backcmd *) &in); |
| 5511 | popstackmark(&smark); |
| 5512 | |
| 5513 | p = in.buf; |
| 5514 | i = in.nleft; |
| 5515 | if (i == 0) |
| 5516 | goto read; |
| 5517 | for (;;) { |
| 5518 | memtodest(p, i, syntax, quotes); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5519 | read: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5520 | if (in.fd < 0) |
| 5521 | break; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 5522 | i = safe_read(in.fd, buf, sizeof(buf)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5523 | TRACE(("expbackq: read returns %d\n", i)); |
| 5524 | if (i <= 0) |
| 5525 | break; |
| 5526 | p = buf; |
| 5527 | } |
| 5528 | |
| 5529 | if (in.buf) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 5530 | free(in.buf); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5531 | if (in.fd >= 0) { |
| 5532 | close(in.fd); |
| 5533 | back_exitstatus = waitforjob(in.jp); |
| 5534 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 5535 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5536 | |
| 5537 | /* Eat all trailing newlines */ |
| 5538 | dest = expdest; |
| 5539 | for (; dest > (char *)stackblock() && dest[-1] == '\n';) |
| 5540 | STUNPUTC(dest); |
| 5541 | expdest = dest; |
| 5542 | |
| 5543 | if (quoted == 0) |
| 5544 | recordregion(startloc, dest - (char *)stackblock(), 0); |
| 5545 | TRACE(("evalbackq: size=%d: \"%.*s\"\n", |
| 5546 | (dest - (char *)stackblock()) - startloc, |
| 5547 | (dest - (char *)stackblock()) - startloc, |
| 5548 | stackblock() + startloc)); |
| 5549 | } |
| 5550 | |
| 5551 | |
| 5552 | static char * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5553 | scanleft(char *startp, char *rmesc, char *rmescend, char *str, int quotes, |
| 5554 | int zero) |
| 5555 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5556 | char *loc; |
| 5557 | char *loc2; |
| 5558 | char c; |
| 5559 | |
| 5560 | loc = startp; |
| 5561 | loc2 = rmesc; |
| 5562 | do { |
| 5563 | int match; |
| 5564 | const char *s = loc2; |
| 5565 | c = *loc2; |
| 5566 | if (zero) { |
| 5567 | *loc2 = '\0'; |
| 5568 | s = rmesc; |
| 5569 | } |
| 5570 | match = pmatch(str, s); |
| 5571 | *loc2 = c; |
| 5572 | if (match) |
| 5573 | return loc; |
| 5574 | if (quotes && *loc == CTLESC) |
| 5575 | loc++; |
| 5576 | loc++; |
| 5577 | loc2++; |
| 5578 | } while (c); |
| 5579 | return 0; |
| 5580 | } |
| 5581 | |
| 5582 | |
| 5583 | static char * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5584 | scanright(char *startp, char *rmesc, char *rmescend, char *str, int quotes, |
| 5585 | int zero) |
| 5586 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5587 | int esc = 0; |
| 5588 | char *loc; |
| 5589 | char *loc2; |
| 5590 | |
| 5591 | for (loc = str - 1, loc2 = rmescend; loc >= startp; loc2--) { |
| 5592 | int match; |
| 5593 | char c = *loc2; |
| 5594 | const char *s = loc2; |
| 5595 | if (zero) { |
| 5596 | *loc2 = '\0'; |
| 5597 | s = rmesc; |
| 5598 | } |
| 5599 | match = pmatch(str, s); |
| 5600 | *loc2 = c; |
| 5601 | if (match) |
| 5602 | return loc; |
| 5603 | loc--; |
| 5604 | if (quotes) { |
| 5605 | if (--esc < 0) { |
| 5606 | esc = esclen(startp, loc); |
| 5607 | } |
| 5608 | if (esc % 2) { |
| 5609 | esc--; |
| 5610 | loc--; |
| 5611 | } |
| 5612 | } |
| 5613 | } |
| 5614 | return 0; |
| 5615 | } |
| 5616 | |
| 5617 | static const char * |
| 5618 | subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varflags, int quotes) |
| 5619 | { |
| 5620 | char *startp; |
| 5621 | char *loc; |
| 5622 | int saveherefd = herefd; |
| 5623 | struct nodelist *saveargbackq = argbackq; |
| 5624 | int amount; |
| 5625 | char *rmesc, *rmescend; |
| 5626 | int zero; |
| 5627 | char *(*scan)(char *, char *, char *, char *, int , int); |
| 5628 | |
| 5629 | herefd = -1; |
| 5630 | argstr(p, subtype != VSASSIGN && subtype != VSQUESTION ? EXP_CASE : 0); |
| 5631 | STPUTC('\0', expdest); |
| 5632 | herefd = saveherefd; |
| 5633 | argbackq = saveargbackq; |
| 5634 | startp = stackblock() + startloc; |
| 5635 | |
| 5636 | switch (subtype) { |
| 5637 | case VSASSIGN: |
| 5638 | setvar(str, startp, 0); |
| 5639 | amount = startp - expdest; |
| 5640 | STADJUST(amount, expdest); |
| 5641 | return startp; |
| 5642 | |
| 5643 | case VSQUESTION: |
| 5644 | varunset(p, str, startp, varflags); |
| 5645 | /* NOTREACHED */ |
| 5646 | } |
| 5647 | |
| 5648 | subtype -= VSTRIMRIGHT; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 5649 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5650 | if (subtype < 0 || subtype > 3) |
| 5651 | abort(); |
| 5652 | #endif |
| 5653 | |
| 5654 | rmesc = startp; |
| 5655 | rmescend = stackblock() + strloc; |
| 5656 | if (quotes) { |
| 5657 | rmesc = _rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); |
| 5658 | if (rmesc != startp) { |
| 5659 | rmescend = expdest; |
| 5660 | startp = stackblock() + startloc; |
| 5661 | } |
| 5662 | } |
| 5663 | rmescend--; |
| 5664 | str = stackblock() + strloc; |
| 5665 | preglob(str, varflags & VSQUOTE, 0); |
| 5666 | |
| 5667 | /* zero = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX */ |
| 5668 | zero = subtype >> 1; |
| 5669 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 5670 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 5671 | |
| 5672 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 5673 | if (loc) { |
| 5674 | if (zero) { |
| 5675 | memmove(startp, loc, str - loc); |
| 5676 | loc = startp + (str - loc) - 1; |
| 5677 | } |
| 5678 | *loc = '\0'; |
| 5679 | amount = loc - expdest; |
| 5680 | STADJUST(amount, expdest); |
| 5681 | } |
| 5682 | return loc; |
| 5683 | } |
| 5684 | |
| 5685 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5686 | /* |
| 5687 | * Expand a variable, and return a pointer to the next character in the |
| 5688 | * input string. |
| 5689 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5690 | static char * |
| 5691 | evalvar(char *p, int flag) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5692 | { |
| 5693 | int subtype; |
| 5694 | int varflags; |
| 5695 | char *var; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5696 | int patloc; |
| 5697 | int c; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5698 | int startloc; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5699 | ssize_t varlen; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5700 | int easy; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5701 | int quotes; |
| 5702 | int quoted; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5703 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5704 | quotes = flag & (EXP_FULL | EXP_CASE); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5705 | varflags = *p++; |
| 5706 | subtype = varflags & VSTYPE; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5707 | quoted = varflags & VSQUOTE; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5708 | var = p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5709 | easy = (!quoted || (*var == '@' && shellparam.nparam)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5710 | startloc = expdest - (char *)stackblock(); |
| 5711 | p = strchr(p, '=') + 1; |
| 5712 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5713 | again: |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5714 | varlen = varvalue(var, varflags, flag); |
| 5715 | if (varflags & VSNUL) |
| 5716 | varlen--; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5717 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5718 | if (subtype == VSPLUS) { |
| 5719 | varlen = -1 - varlen; |
| 5720 | goto vsplus; |
| 5721 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5722 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5723 | if (subtype == VSMINUS) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5724 | vsplus: |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5725 | if (varlen < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5726 | argstr( |
| 5727 | p, flag | EXP_TILDE | |
| 5728 | (quoted ? EXP_QWORD : EXP_WORD) |
| 5729 | ); |
| 5730 | goto end; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5731 | } |
| 5732 | if (easy) |
| 5733 | goto record; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5734 | goto end; |
| 5735 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5736 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5737 | if (subtype == VSASSIGN || subtype == VSQUESTION) { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5738 | if (varlen < 0) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 5739 | if (subevalvar(p, var, 0, subtype, startloc, varflags, 0)) { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5740 | varflags &= ~VSNUL; |
| 5741 | /* |
| 5742 | * Remove any recorded regions beyond |
| 5743 | * start of variable |
| 5744 | */ |
| 5745 | removerecordregions(startloc); |
| 5746 | goto again; |
| 5747 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5748 | goto end; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5749 | } |
| 5750 | if (easy) |
| 5751 | goto record; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5752 | goto end; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5753 | } |
| 5754 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5755 | if (varlen < 0 && uflag) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5756 | varunset(p, var, 0, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 5757 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5758 | if (subtype == VSLENGTH) { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5759 | cvtnum(varlen > 0 ? varlen : 0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5760 | goto record; |
| 5761 | } |
| 5762 | |
| 5763 | if (subtype == VSNORMAL) { |
| 5764 | if (!easy) |
| 5765 | goto end; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5766 | record: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5767 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
| 5768 | goto end; |
| 5769 | } |
| 5770 | |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 5771 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5772 | switch (subtype) { |
| 5773 | case VSTRIMLEFT: |
| 5774 | case VSTRIMLEFTMAX: |
| 5775 | case VSTRIMRIGHT: |
| 5776 | case VSTRIMRIGHTMAX: |
| 5777 | break; |
| 5778 | default: |
| 5779 | abort(); |
| 5780 | } |
| 5781 | #endif |
| 5782 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5783 | if (varlen >= 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5784 | /* |
| 5785 | * Terminate the string and start recording the pattern |
| 5786 | * right after it |
| 5787 | */ |
| 5788 | STPUTC('\0', expdest); |
| 5789 | patloc = expdest - (char *)stackblock(); |
| 5790 | if (subevalvar(p, NULL, patloc, subtype, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 5791 | startloc, varflags, quotes) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5792 | int amount = expdest - ( |
| 5793 | (char *)stackblock() + patloc - 1 |
| 5794 | ); |
| 5795 | STADJUST(-amount, expdest); |
| 5796 | } |
| 5797 | /* Remove any recorded regions beyond start of variable */ |
| 5798 | removerecordregions(startloc); |
| 5799 | goto record; |
| 5800 | } |
| 5801 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5802 | end: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5803 | if (subtype != VSNORMAL) { /* skip to end of alternative */ |
| 5804 | int nesting = 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5805 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5806 | c = *p++; |
| 5807 | if (c == CTLESC) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5808 | p++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5809 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5810 | if (varlen >= 0) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5811 | argbackq = argbackq->next; |
| 5812 | } else if (c == CTLVAR) { |
| 5813 | if ((*p++ & VSTYPE) != VSNORMAL) |
| 5814 | nesting++; |
| 5815 | } else if (c == CTLENDVAR) { |
| 5816 | if (--nesting == 0) |
| 5817 | break; |
| 5818 | } |
| 5819 | } |
| 5820 | } |
| 5821 | return p; |
| 5822 | } |
| 5823 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5824 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5825 | /* |
| 5826 | * Put a string on the stack. |
| 5827 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5828 | static void |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 5829 | memtodest(const char *p, size_t len, int syntax, int quotes) |
| 5830 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5831 | char *q = expdest; |
| 5832 | |
| 5833 | q = makestrspace(len * 2, q); |
| 5834 | |
| 5835 | while (len--) { |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 5836 | int c = SC2INT(*p++); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5837 | if (!c) |
| 5838 | continue; |
| 5839 | if (quotes && (SIT(c, syntax) == CCTL || SIT(c, syntax) == CBACK)) |
| 5840 | USTPUTC(CTLESC, q); |
| 5841 | USTPUTC(c, q); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5842 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5843 | |
| 5844 | expdest = q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5845 | } |
| 5846 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5847 | |
| 5848 | static void |
| 5849 | strtodest(const char *p, int syntax, int quotes) |
| 5850 | { |
| 5851 | memtodest(p, strlen(p), syntax, quotes); |
| 5852 | } |
| 5853 | |
| 5854 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5855 | /* |
| 5856 | * Add the value of a specialized variable to the stack string. |
| 5857 | */ |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5858 | static ssize_t |
| 5859 | varvalue(char *name, int varflags, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5860 | { |
| 5861 | int num; |
| 5862 | char *p; |
| 5863 | int i; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5864 | int sep = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5865 | int sepq = 0; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5866 | ssize_t len = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5867 | char **ap; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 5868 | int syntax; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5869 | int quoted = varflags & VSQUOTE; |
| 5870 | int subtype = varflags & VSTYPE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5871 | int quotes = flags & (EXP_FULL | EXP_CASE); |
| 5872 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5873 | if (quoted && (flags & EXP_FULL)) |
| 5874 | sep = 1 << CHAR_BIT; |
| 5875 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5876 | syntax = quoted ? DQSYNTAX : BASESYNTAX; |
| 5877 | switch (*name) { |
| 5878 | case '$': |
| 5879 | num = rootpid; |
| 5880 | goto numvar; |
| 5881 | case '?': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5882 | num = exitstatus; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5883 | goto numvar; |
| 5884 | case '#': |
| 5885 | num = shellparam.nparam; |
| 5886 | goto numvar; |
| 5887 | case '!': |
| 5888 | num = backgndpid; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5889 | if (num == 0) |
| 5890 | return -1; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5891 | numvar: |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5892 | len = cvtnum(num); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5893 | break; |
| 5894 | case '-': |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5895 | p = makestrspace(NOPTS, expdest); |
| 5896 | for (i = NOPTS - 1; i >= 0; i--) { |
| 5897 | if (optlist[i]) { |
| 5898 | USTPUTC(optletters(i), p); |
| 5899 | len++; |
| 5900 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5901 | } |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5902 | expdest = p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5903 | break; |
| 5904 | case '@': |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5905 | if (sep) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5906 | goto param; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5907 | /* fall through */ |
| 5908 | case '*': |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 5909 | sep = ifsset() ? SC2INT(ifsval()[0]) : ' '; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5910 | if (quotes && (SIT(sep, syntax) == CCTL || SIT(sep, syntax) == CBACK)) |
| 5911 | sepq = 1; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5912 | param: |
| 5913 | ap = shellparam.p; |
| 5914 | if (!ap) |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5915 | return -1; |
| 5916 | while ((p = *ap++)) { |
| 5917 | size_t partlen; |
| 5918 | |
| 5919 | partlen = strlen(p); |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5920 | len += partlen; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 5921 | |
| 5922 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 5923 | memtodest(p, partlen, syntax, quotes); |
| 5924 | |
| 5925 | if (*ap && sep) { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5926 | char *q; |
| 5927 | |
| 5928 | len++; |
| 5929 | if (subtype == VSPLUS || subtype == VSLENGTH) { |
| 5930 | continue; |
| 5931 | } |
| 5932 | q = expdest; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5933 | if (sepq) |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5934 | STPUTC(CTLESC, q); |
| 5935 | STPUTC(sep, q); |
| 5936 | expdest = q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5937 | } |
| 5938 | } |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5939 | return len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5940 | case '0': |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5941 | case '1': |
| 5942 | case '2': |
| 5943 | case '3': |
| 5944 | case '4': |
| 5945 | case '5': |
| 5946 | case '6': |
| 5947 | case '7': |
| 5948 | case '8': |
| 5949 | case '9': |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5950 | num = atoi(name); |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5951 | if (num < 0 || num > shellparam.nparam) |
| 5952 | return -1; |
| 5953 | p = num ? shellparam.p[num - 1] : arg0; |
| 5954 | goto value; |
| 5955 | default: |
| 5956 | p = lookupvar(name); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 5957 | value: |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5958 | if (!p) |
| 5959 | return -1; |
| 5960 | |
| 5961 | len = strlen(p); |
| 5962 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 5963 | memtodest(p, len, syntax, quotes); |
| 5964 | return len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5965 | } |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5966 | |
| 5967 | if (subtype == VSPLUS || subtype == VSLENGTH) |
| 5968 | STADJUST(-len, expdest); |
| 5969 | return len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5970 | } |
| 5971 | |
| 5972 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5973 | /* |
| 5974 | * Record the fact that we have to scan this region of the |
| 5975 | * string for IFS characters. |
| 5976 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5977 | static void |
| 5978 | recordregion(int start, int end, int nulonly) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5979 | { |
| 5980 | struct ifsregion *ifsp; |
| 5981 | |
| 5982 | if (ifslastp == NULL) { |
| 5983 | ifsp = &ifsfirst; |
| 5984 | } else { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 5985 | INT_OFF; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 5986 | ifsp = ckmalloc(sizeof(*ifsp)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5987 | ifsp->next = NULL; |
| 5988 | ifslastp->next = ifsp; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 5989 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5990 | } |
| 5991 | ifslastp = ifsp; |
| 5992 | ifslastp->begoff = start; |
| 5993 | ifslastp->endoff = end; |
| 5994 | ifslastp->nulonly = nulonly; |
| 5995 | } |
| 5996 | |
| 5997 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5998 | /* |
| 5999 | * Break the argument string into pieces based upon IFS and add the |
| 6000 | * strings to the argument list. The regions of the string to be |
| 6001 | * searched for IFS characters have been stored by recordregion. |
| 6002 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6003 | static void |
| 6004 | ifsbreakup(char *string, struct arglist *arglist) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6005 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6006 | struct ifsregion *ifsp; |
| 6007 | struct strlist *sp; |
| 6008 | char *start; |
| 6009 | char *p; |
| 6010 | char *q; |
| 6011 | const char *ifs, *realifs; |
| 6012 | int ifsspc; |
| 6013 | int nulonly; |
| 6014 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6015 | start = string; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6016 | if (ifslastp != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6017 | ifsspc = 0; |
| 6018 | nulonly = 0; |
| 6019 | realifs = ifsset() ? ifsval() : defifs; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6020 | ifsp = &ifsfirst; |
| 6021 | do { |
| 6022 | p = string + ifsp->begoff; |
| 6023 | nulonly = ifsp->nulonly; |
| 6024 | ifs = nulonly ? nullstr : realifs; |
| 6025 | ifsspc = 0; |
| 6026 | while (p < string + ifsp->endoff) { |
| 6027 | q = p; |
| 6028 | if (*p == CTLESC) |
| 6029 | p++; |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame^] | 6030 | if (!strchr(ifs, *p)) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6031 | p++; |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame^] | 6032 | continue; |
| 6033 | } |
| 6034 | if (!nulonly) |
| 6035 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6036 | /* Ignore IFS whitespace at start */ |
| 6037 | if (q == start && ifsspc) { |
| 6038 | p++; |
| 6039 | start = p; |
| 6040 | continue; |
| 6041 | } |
| 6042 | *q = '\0'; |
| 6043 | sp = stalloc(sizeof(*sp)); |
| 6044 | sp->text = start; |
| 6045 | *arglist->lastp = sp; |
| 6046 | arglist->lastp = &sp->next; |
| 6047 | p++; |
| 6048 | if (!nulonly) { |
| 6049 | for (;;) { |
| 6050 | if (p >= string + ifsp->endoff) { |
| 6051 | break; |
| 6052 | } |
| 6053 | q = p; |
| 6054 | if (*p == CTLESC) |
| 6055 | p++; |
| 6056 | if (strchr(ifs, *p) == NULL ) { |
| 6057 | p = q; |
| 6058 | break; |
| 6059 | } else if (strchr(defifs, *p) == NULL) { |
| 6060 | if (ifsspc) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6061 | p++; |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame^] | 6062 | ifsspc = 0; |
| 6063 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6064 | p = q; |
| 6065 | break; |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame^] | 6066 | } |
| 6067 | } else |
| 6068 | p++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6069 | } |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame^] | 6070 | } |
| 6071 | start = p; |
| 6072 | } /* while */ |
| 6073 | ifsp = ifsp->next; |
| 6074 | } while (ifsp != NULL); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6075 | if (nulonly) |
| 6076 | goto add; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6077 | } |
| 6078 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6079 | if (!*start) |
| 6080 | return; |
| 6081 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6082 | add: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 6083 | sp = stalloc(sizeof(*sp)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6084 | sp->text = start; |
| 6085 | *arglist->lastp = sp; |
| 6086 | arglist->lastp = &sp->next; |
| 6087 | } |
| 6088 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6089 | static void |
| 6090 | ifsfree(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6091 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6092 | struct ifsregion *p; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 6093 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6094 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6095 | p = ifsfirst.next; |
| 6096 | do { |
| 6097 | struct ifsregion *ifsp; |
| 6098 | ifsp = p->next; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6099 | free(p); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6100 | p = ifsp; |
| 6101 | } while (p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6102 | ifslastp = NULL; |
| 6103 | ifsfirst.next = NULL; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6104 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6105 | } |
| 6106 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6107 | static void expmeta(char *, char *); |
| 6108 | static struct strlist *expsort(struct strlist *); |
| 6109 | static struct strlist *msort(struct strlist *, int); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6110 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6111 | static char *expdir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6112 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6113 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6114 | static void |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6115 | expandmeta(struct strlist *str, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6116 | { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6117 | static const char metachars[] = { |
| 6118 | '*', '?', '[', 0 |
| 6119 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6120 | /* TODO - EXP_REDIR */ |
| 6121 | |
| 6122 | while (str) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6123 | struct strlist **savelastp; |
| 6124 | struct strlist *sp; |
| 6125 | char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6126 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6127 | if (fflag) |
| 6128 | goto nometa; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6129 | if (!strpbrk(str->text, metachars)) |
| 6130 | goto nometa; |
| 6131 | savelastp = exparg.lastp; |
| 6132 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6133 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6134 | p = preglob(str->text, 0, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6135 | { |
| 6136 | int i = strlen(str->text); |
| 6137 | expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ |
| 6138 | } |
| 6139 | |
| 6140 | expmeta(expdir, p); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6141 | free(expdir); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6142 | if (p != str->text) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6143 | free(p); |
| 6144 | INT_ON; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6145 | if (exparg.lastp == savelastp) { |
| 6146 | /* |
| 6147 | * no matches |
| 6148 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6149 | nometa: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6150 | *exparg.lastp = str; |
| 6151 | rmescapes(str->text); |
| 6152 | exparg.lastp = &str->next; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6153 | } else { |
| 6154 | *exparg.lastp = NULL; |
| 6155 | *savelastp = sp = expsort(*savelastp); |
| 6156 | while (sp->next != NULL) |
| 6157 | sp = sp->next; |
| 6158 | exparg.lastp = &sp->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6159 | } |
| 6160 | str = str->next; |
| 6161 | } |
| 6162 | } |
| 6163 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6164 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6165 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6166 | * Add a file name to the list. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6167 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6168 | static void |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6169 | addfname(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6170 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6171 | struct strlist *sp; |
| 6172 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 6173 | sp = stalloc(sizeof(*sp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 6174 | sp->text = ststrdup(name); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6175 | *exparg.lastp = sp; |
| 6176 | exparg.lastp = &sp->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6177 | } |
| 6178 | |
| 6179 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6180 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6181 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 6182 | */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6183 | static void |
| 6184 | expmeta(char *enddir, char *name) |
| 6185 | { |
| 6186 | char *p; |
| 6187 | const char *cp; |
| 6188 | char *start; |
| 6189 | char *endname; |
| 6190 | int metaflag; |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 6191 | struct stat statb; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6192 | DIR *dirp; |
| 6193 | struct dirent *dp; |
| 6194 | int atend; |
| 6195 | int matchdot; |
| 6196 | |
| 6197 | metaflag = 0; |
| 6198 | start = name; |
| 6199 | for (p = name; *p; p++) { |
| 6200 | if (*p == '*' || *p == '?') |
| 6201 | metaflag = 1; |
| 6202 | else if (*p == '[') { |
| 6203 | char *q = p + 1; |
| 6204 | if (*q == '!') |
| 6205 | q++; |
| 6206 | for (;;) { |
| 6207 | if (*q == '\\') |
| 6208 | q++; |
| 6209 | if (*q == '/' || *q == '\0') |
| 6210 | break; |
| 6211 | if (*++q == ']') { |
| 6212 | metaflag = 1; |
| 6213 | break; |
| 6214 | } |
| 6215 | } |
| 6216 | } else if (*p == '\\') |
| 6217 | p++; |
| 6218 | else if (*p == '/') { |
| 6219 | if (metaflag) |
| 6220 | goto out; |
| 6221 | start = p + 1; |
| 6222 | } |
| 6223 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6224 | out: |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6225 | if (metaflag == 0) { /* we've reached the end of the file name */ |
| 6226 | if (enddir != expdir) |
| 6227 | metaflag++; |
| 6228 | p = name; |
| 6229 | do { |
| 6230 | if (*p == '\\') |
| 6231 | p++; |
| 6232 | *enddir++ = *p; |
| 6233 | } while (*p++); |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 6234 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6235 | addfname(expdir); |
| 6236 | return; |
| 6237 | } |
| 6238 | endname = p; |
| 6239 | if (name < start) { |
| 6240 | p = name; |
| 6241 | do { |
| 6242 | if (*p == '\\') |
| 6243 | p++; |
| 6244 | *enddir++ = *p++; |
| 6245 | } while (p < start); |
| 6246 | } |
| 6247 | if (enddir == expdir) { |
| 6248 | cp = "."; |
| 6249 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 6250 | cp = "/"; |
| 6251 | } else { |
| 6252 | cp = expdir; |
| 6253 | enddir[-1] = '\0'; |
| 6254 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6255 | dirp = opendir(cp); |
| 6256 | if (dirp == NULL) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6257 | return; |
| 6258 | if (enddir != expdir) |
| 6259 | enddir[-1] = '/'; |
| 6260 | if (*endname == 0) { |
| 6261 | atend = 1; |
| 6262 | } else { |
| 6263 | atend = 0; |
| 6264 | *endname++ = '\0'; |
| 6265 | } |
| 6266 | matchdot = 0; |
| 6267 | p = start; |
| 6268 | if (*p == '\\') |
| 6269 | p++; |
| 6270 | if (*p == '.') |
| 6271 | matchdot++; |
| 6272 | while (! intpending && (dp = readdir(dirp)) != NULL) { |
| 6273 | if (dp->d_name[0] == '.' && ! matchdot) |
| 6274 | continue; |
| 6275 | if (pmatch(start, dp->d_name)) { |
| 6276 | if (atend) { |
| 6277 | scopy(dp->d_name, enddir); |
| 6278 | addfname(expdir); |
| 6279 | } else { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 6280 | for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6281 | continue; |
| 6282 | p[-1] = '/'; |
| 6283 | expmeta(p, endname); |
| 6284 | } |
| 6285 | } |
| 6286 | } |
| 6287 | closedir(dirp); |
| 6288 | if (! atend) |
| 6289 | endname[-1] = '/'; |
| 6290 | } |
| 6291 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6292 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6293 | /* |
| 6294 | * Sort the results of file name expansion. It calculates the number of |
| 6295 | * strings to sort and then calls msort (short for merge sort) to do the |
| 6296 | * work. |
| 6297 | */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6298 | static struct strlist * |
| 6299 | expsort(struct strlist *str) |
| 6300 | { |
| 6301 | int len; |
| 6302 | struct strlist *sp; |
| 6303 | |
| 6304 | len = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 6305 | for (sp = str; sp; sp = sp->next) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6306 | len++; |
| 6307 | return msort(str, len); |
| 6308 | } |
| 6309 | |
| 6310 | |
| 6311 | static struct strlist * |
| 6312 | msort(struct strlist *list, int len) |
| 6313 | { |
| 6314 | struct strlist *p, *q = NULL; |
| 6315 | struct strlist **lpp; |
| 6316 | int half; |
| 6317 | int n; |
| 6318 | |
| 6319 | if (len <= 1) |
| 6320 | return list; |
| 6321 | half = len >> 1; |
| 6322 | p = list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 6323 | for (n = half; --n >= 0; ) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6324 | q = p; |
| 6325 | p = p->next; |
| 6326 | } |
| 6327 | q->next = NULL; /* terminate first half of list */ |
| 6328 | q = msort(list, half); /* sort first half of list */ |
| 6329 | p = msort(p, len - half); /* sort second half */ |
| 6330 | lpp = &list; |
| 6331 | for (;;) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 6332 | #if ENABLE_LOCALE_SUPPORT |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6333 | if (strcoll(p->text, q->text) < 0) |
| 6334 | #else |
| 6335 | if (strcmp(p->text, q->text) < 0) |
| 6336 | #endif |
| 6337 | { |
| 6338 | *lpp = p; |
| 6339 | lpp = &p->next; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6340 | p = *lpp; |
| 6341 | if (p == NULL) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6342 | *lpp = q; |
| 6343 | break; |
| 6344 | } |
| 6345 | } else { |
| 6346 | *lpp = q; |
| 6347 | lpp = &q->next; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6348 | q = *lpp; |
| 6349 | if (q == NULL) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6350 | *lpp = p; |
| 6351 | break; |
| 6352 | } |
| 6353 | } |
| 6354 | } |
| 6355 | return list; |
| 6356 | } |
| 6357 | |
| 6358 | |
| 6359 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6360 | * Returns true if the pattern matches the string. |
| 6361 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 6362 | static int patmatch(char *pattern, const char *string) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6363 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6364 | return pmatch(preglob(pattern, 0, 0), string); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6365 | } |
| 6366 | |
| 6367 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6368 | /* |
| 6369 | * Remove any CTLESC characters from a string. |
| 6370 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6371 | static char * |
| 6372 | _rmescapes(char *str, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6373 | { |
| 6374 | char *p, *q, *r; |
| 6375 | static const char qchars[] = { CTLESC, CTLQUOTEMARK, 0 }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6376 | unsigned inquotes; |
| 6377 | int notescaped; |
| 6378 | int globbing; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6379 | |
| 6380 | p = strpbrk(str, qchars); |
| 6381 | if (!p) { |
| 6382 | return str; |
| 6383 | } |
| 6384 | q = p; |
| 6385 | r = str; |
| 6386 | if (flag & RMESCAPE_ALLOC) { |
| 6387 | size_t len = p - str; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6388 | size_t fulllen = len + strlen(p) + 1; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 6389 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6390 | if (flag & RMESCAPE_GROW) { |
| 6391 | r = makestrspace(fulllen, expdest); |
| 6392 | } else if (flag & RMESCAPE_HEAP) { |
| 6393 | r = ckmalloc(fulllen); |
| 6394 | } else { |
| 6395 | r = stalloc(fulllen); |
| 6396 | } |
| 6397 | q = r; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6398 | if (len > 0) { |
Denis Vlasenko | 7cfecc4 | 2006-12-18 22:32:45 +0000 | [diff] [blame] | 6399 | q = memcpy(q, str, len) + len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6400 | } |
| 6401 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6402 | inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED; |
| 6403 | globbing = flag & RMESCAPE_GLOB; |
| 6404 | notescaped = globbing; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6405 | while (*p) { |
| 6406 | if (*p == CTLQUOTEMARK) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6407 | inquotes = ~inquotes; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6408 | p++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6409 | notescaped = globbing; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6410 | continue; |
| 6411 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6412 | if (*p == '\\') { |
| 6413 | /* naked back slash */ |
| 6414 | notescaped = 0; |
| 6415 | goto copy; |
| 6416 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6417 | if (*p == CTLESC) { |
| 6418 | p++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6419 | if (notescaped && inquotes && *p != '/') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6420 | *q++ = '\\'; |
| 6421 | } |
| 6422 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6423 | notescaped = globbing; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6424 | copy: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6425 | *q++ = *p++; |
| 6426 | } |
| 6427 | *q = '\0'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6428 | if (flag & RMESCAPE_GROW) { |
| 6429 | expdest = r; |
| 6430 | STADJUST(q - r + 1, expdest); |
| 6431 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6432 | return r; |
| 6433 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6434 | |
| 6435 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6436 | /* |
| 6437 | * See if a pattern matches in a case statement. |
| 6438 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6439 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6440 | casematch(union node *pattern, char *val) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6441 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6442 | struct stackmark smark; |
| 6443 | int result; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6444 | |
| 6445 | setstackmark(&smark); |
| 6446 | argbackq = pattern->narg.backquote; |
| 6447 | STARTSTACKSTR(expdest); |
| 6448 | ifslastp = NULL; |
| 6449 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6450 | STACKSTRNUL(expdest); |
| 6451 | result = patmatch(stackblock(), val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6452 | popstackmark(&smark); |
| 6453 | return result; |
| 6454 | } |
| 6455 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6456 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6457 | /* |
| 6458 | * Our own itoa(). |
| 6459 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6460 | static int |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 6461 | cvtnum(arith_t num) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6462 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6463 | int len; |
| 6464 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6465 | expdest = makestrspace(32, expdest); |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 6466 | #if ENABLE_ASH_MATH_SUPPORT_64 |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 6467 | len = fmtstr(expdest, 32, "%lld", (long long) num); |
| 6468 | #else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6469 | len = fmtstr(expdest, 32, "%ld", num); |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 6470 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6471 | STADJUST(len, expdest); |
| 6472 | return len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6473 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 6474 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 6475 | static void varunset(const char *, const char *, const char *, int) ATTRIBUTE_NORETURN; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6476 | static void |
| 6477 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6478 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6479 | const char *msg; |
| 6480 | const char *tail; |
| 6481 | |
| 6482 | tail = nullstr; |
| 6483 | msg = "parameter not set"; |
| 6484 | if (umsg) { |
| 6485 | if (*end == CTLENDVAR) { |
| 6486 | if (varflags & VSNUL) |
| 6487 | tail = " or null"; |
| 6488 | } else |
| 6489 | msg = umsg; |
| 6490 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6491 | ash_msg_and_raise_error("%.*s: %s%s", end - var - 1, var, msg, tail); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6492 | } |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6493 | |
| 6494 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6495 | /* input.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6496 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6497 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6498 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6499 | */ |
| 6500 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6501 | #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6502 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6503 | static void pushfile(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6504 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6505 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6506 | * Read a character from the script, returning PEOF on end of file. |
| 6507 | * Nul characters in the input are silently discarded. |
| 6508 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 6509 | static int preadbuffer(void); |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 6510 | #define pgetc_as_macro() (--parsenleft >= 0? SC2INT(*parsenextc++) : preadbuffer()) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6511 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 6512 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6513 | #define pgetc_macro() pgetc() |
| 6514 | static int |
| 6515 | pgetc(void) |
| 6516 | { |
| 6517 | return pgetc_as_macro(); |
| 6518 | } |
| 6519 | #else |
| 6520 | #define pgetc_macro() pgetc_as_macro() |
| 6521 | static int |
| 6522 | pgetc(void) |
| 6523 | { |
| 6524 | return pgetc_macro(); |
| 6525 | } |
| 6526 | #endif |
| 6527 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6528 | /* |
| 6529 | * Same as pgetc(), but ignores PEOA. |
| 6530 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 6531 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 6532 | static int |
| 6533 | pgetc2(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6534 | { |
| 6535 | int c; |
| 6536 | |
| 6537 | do { |
| 6538 | c = pgetc_macro(); |
| 6539 | } while (c == PEOA); |
| 6540 | return c; |
| 6541 | } |
| 6542 | #else |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 6543 | static int |
| 6544 | pgetc2(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6545 | { |
| 6546 | return pgetc_macro(); |
| 6547 | } |
| 6548 | #endif |
| 6549 | |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 6550 | /* |
| 6551 | * Read a line from the script. |
| 6552 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 6553 | static char * |
| 6554 | pfgets(char *line, int len) |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 6555 | { |
| 6556 | char *p = line; |
| 6557 | int nleft = len; |
| 6558 | int c; |
| 6559 | |
| 6560 | while (--nleft > 0) { |
| 6561 | c = pgetc2(); |
| 6562 | if (c == PEOF) { |
| 6563 | if (p == line) |
| 6564 | return NULL; |
| 6565 | break; |
| 6566 | } |
| 6567 | *p++ = c; |
| 6568 | if (c == '\n') |
| 6569 | break; |
| 6570 | } |
| 6571 | *p = '\0'; |
| 6572 | return line; |
| 6573 | } |
| 6574 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6575 | #if ENABLE_FEATURE_EDITING_VI |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 6576 | #define setvimode(on) do { \ |
| 6577 | if (on) line_input_state->flags |= VI_MODE; \ |
| 6578 | else line_input_state->flags &= ~VI_MODE; \ |
| 6579 | } while (0) |
| 6580 | #else |
| 6581 | #define setvimode(on) viflag = 0 /* forcibly keep the option off */ |
| 6582 | #endif |
| 6583 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 6584 | static int |
| 6585 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6586 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 6587 | int nr; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6588 | char *buf = parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 6589 | parsenextc = buf; |
| 6590 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6591 | retry: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6592 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6593 | if (!iflag || parsefile->fd) |
| 6594 | nr = safe_read(parsefile->fd, buf, BUFSIZ - 1); |
| 6595 | else { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6596 | #if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 6597 | line_input_state->path_lookup = pathval(); |
Eric Andersen | 4a79c0e | 2004-09-08 10:01:07 +0000 | [diff] [blame] | 6598 | #endif |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 6599 | nr = read_line_input(cmdedit_prompt, buf, BUFSIZ, line_input_state); |
| 6600 | if (nr == 0) { |
| 6601 | /* Ctrl+C pressed */ |
| 6602 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 6603 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 6604 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 6605 | raise(SIGINT); |
| 6606 | return 1; |
| 6607 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6608 | goto retry; |
| 6609 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 6610 | if (nr < 0 && errno == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6611 | /* Ctrl+D presend */ |
| 6612 | nr = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 6613 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6614 | } |
| 6615 | #else |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 6616 | nr = safe_read(parsefile->fd, buf, BUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6617 | #endif |
| 6618 | |
| 6619 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6620 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
| 6621 | int flags = fcntl(0, F_GETFL, 0); |
| 6622 | if (flags >= 0 && flags & O_NONBLOCK) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6623 | flags &=~ O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6624 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 6625 | out2str("sh: turning off NDELAY mode\n"); |
| 6626 | goto retry; |
| 6627 | } |
| 6628 | } |
| 6629 | } |
| 6630 | } |
| 6631 | return nr; |
| 6632 | } |
| 6633 | |
| 6634 | /* |
| 6635 | * Refill the input buffer and return the next input character: |
| 6636 | * |
| 6637 | * 1) If a string was pushed back on the input, pop it; |
| 6638 | * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading |
| 6639 | * from a string so we can't refill the buffer, return EOF. |
| 6640 | * 3) If the is more stuff in this buffer, use it else call read to fill it. |
| 6641 | * 4) Process input up to the next newline, deleting nul characters. |
| 6642 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6643 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6644 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6645 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6646 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6647 | int more; |
| 6648 | char savec; |
| 6649 | |
| 6650 | while (parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 6651 | #if ENABLE_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6652 | if (parsenleft == -1 && parsefile->strpush->ap && |
| 6653 | parsenextc[-1] != ' ' && parsenextc[-1] != '\t') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6654 | return PEOA; |
| 6655 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6656 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6657 | popstring(); |
| 6658 | if (--parsenleft >= 0) |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 6659 | return SC2INT(*parsenextc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6660 | } |
| 6661 | if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) |
| 6662 | return PEOF; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6663 | flush_stdout_stderr(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6664 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6665 | more = parselleft; |
| 6666 | if (more <= 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6667 | again: |
| 6668 | more = preadfd(); |
| 6669 | if (more <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6670 | parselleft = parsenleft = EOF_NLEFT; |
| 6671 | return PEOF; |
| 6672 | } |
| 6673 | } |
| 6674 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6675 | q = parsenextc; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6676 | |
| 6677 | /* delete nul characters */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6678 | for (;;) { |
| 6679 | int c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6680 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6681 | more--; |
| 6682 | c = *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6683 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6684 | if (!c) |
| 6685 | memmove(q, q + 1, more); |
| 6686 | else { |
| 6687 | q++; |
| 6688 | if (c == '\n') { |
| 6689 | parsenleft = q - parsenextc - 1; |
| 6690 | break; |
| 6691 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6692 | } |
| 6693 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6694 | if (more <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6695 | parsenleft = q - parsenextc - 1; |
| 6696 | if (parsenleft < 0) |
| 6697 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6698 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6699 | } |
| 6700 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6701 | parselleft = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6702 | |
| 6703 | savec = *q; |
| 6704 | *q = '\0'; |
| 6705 | |
| 6706 | if (vflag) { |
| 6707 | out2str(parsenextc); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6708 | } |
| 6709 | |
| 6710 | *q = savec; |
| 6711 | |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 6712 | return SC2INT(*parsenextc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6713 | } |
| 6714 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6715 | /* |
| 6716 | * Undo the last call to pgetc. Only one character may be pushed back. |
| 6717 | * PEOF may be pushed back. |
| 6718 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6719 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6720 | pungetc(void) |
| 6721 | { |
| 6722 | parsenleft++; |
| 6723 | parsenextc--; |
| 6724 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6725 | |
| 6726 | /* |
| 6727 | * Push a string back onto the input at this current parsefile level. |
| 6728 | * We handle aliases this way. |
| 6729 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6730 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6731 | pushstring(char *s, void *ap) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6732 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6733 | struct strpush *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6734 | size_t len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6735 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6736 | len = strlen(s); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6737 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6738 | /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/ |
| 6739 | if (parsefile->strpush) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 6740 | sp = ckmalloc(sizeof(struct strpush)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6741 | sp->prev = parsefile->strpush; |
| 6742 | parsefile->strpush = sp; |
| 6743 | } else |
| 6744 | sp = parsefile->strpush = &(parsefile->basestrpush); |
| 6745 | sp->prevstring = parsenextc; |
| 6746 | sp->prevnleft = parsenleft; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 6747 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6748 | sp->ap = (struct alias *)ap; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6749 | if (ap) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6750 | ((struct alias *)ap)->flag |= ALIASINUSE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6751 | sp->string = s; |
| 6752 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6753 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6754 | parsenextc = s; |
| 6755 | parsenleft = len; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6756 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6757 | } |
| 6758 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6759 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6760 | popstring(void) |
| 6761 | { |
| 6762 | struct strpush *sp = parsefile->strpush; |
| 6763 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6764 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 6765 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6766 | if (sp->ap) { |
| 6767 | if (parsenextc[-1] == ' ' || parsenextc[-1] == '\t') { |
| 6768 | checkkwd |= CHKALIAS; |
| 6769 | } |
| 6770 | if (sp->string != sp->ap->val) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6771 | free(sp->string); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6772 | } |
| 6773 | sp->ap->flag &= ~ALIASINUSE; |
| 6774 | if (sp->ap->flag & ALIASDEAD) { |
| 6775 | unalias(sp->ap->name); |
| 6776 | } |
| 6777 | } |
| 6778 | #endif |
| 6779 | parsenextc = sp->prevstring; |
| 6780 | parsenleft = sp->prevnleft; |
| 6781 | /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/ |
| 6782 | parsefile->strpush = sp->prev; |
| 6783 | if (sp != &(parsefile->basestrpush)) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6784 | free(sp); |
| 6785 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6786 | } |
| 6787 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6788 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6789 | /* |
| 6790 | * Set the input to take input from a file. If push is set, push the |
| 6791 | * old input onto the stack first. |
| 6792 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6793 | static int |
| 6794 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6795 | { |
| 6796 | int fd; |
| 6797 | int fd2; |
| 6798 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6799 | INT_OFF; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6800 | fd = open(fname, O_RDONLY); |
| 6801 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6802 | if (flags & INPUT_NOFILE_OK) |
| 6803 | goto out; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6804 | ash_msg_and_raise_error("Can't open %s", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6805 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6806 | if (fd < 10) { |
| 6807 | fd2 = copyfd(fd, 10); |
| 6808 | close(fd); |
| 6809 | if (fd2 < 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6810 | ash_msg_and_raise_error("Out of file descriptors"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6811 | fd = fd2; |
| 6812 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6813 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 6814 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6815 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6816 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6817 | } |
| 6818 | |
| 6819 | |
| 6820 | /* |
| 6821 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 6822 | * interrupts off. |
| 6823 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6824 | static void |
| 6825 | setinputfd(int fd, int push) |
| 6826 | { |
| 6827 | (void) fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 6828 | if (push) { |
| 6829 | pushfile(); |
| 6830 | parsefile->buf = 0; |
| 6831 | } |
| 6832 | parsefile->fd = fd; |
| 6833 | if (parsefile->buf == NULL) |
| 6834 | parsefile->buf = ckmalloc(IBUFSIZ); |
| 6835 | parselleft = parsenleft = 0; |
| 6836 | plinno = 1; |
| 6837 | } |
| 6838 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6839 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6840 | /* |
| 6841 | * Like setinputfile, but takes input from a string. |
| 6842 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6843 | static void |
| 6844 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6845 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6846 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6847 | pushfile(); |
| 6848 | parsenextc = string; |
| 6849 | parsenleft = strlen(string); |
| 6850 | parsefile->buf = NULL; |
| 6851 | plinno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6852 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6853 | } |
| 6854 | |
| 6855 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6856 | /* |
| 6857 | * To handle the "." command, a stack of input files is used. Pushfile |
| 6858 | * adds a new entry to the stack and popfile restores the previous level. |
| 6859 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6860 | static void |
| 6861 | pushfile(void) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6862 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6863 | struct parsefile *pf; |
| 6864 | |
| 6865 | parsefile->nleft = parsenleft; |
| 6866 | parsefile->lleft = parselleft; |
| 6867 | parsefile->nextc = parsenextc; |
| 6868 | parsefile->linno = plinno; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 6869 | pf = ckmalloc(sizeof(*pf)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6870 | pf->prev = parsefile; |
| 6871 | pf->fd = -1; |
| 6872 | pf->strpush = NULL; |
| 6873 | pf->basestrpush.prev = NULL; |
| 6874 | parsefile = pf; |
| 6875 | } |
| 6876 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6877 | |
| 6878 | static void |
| 6879 | popfile(void) |
| 6880 | { |
| 6881 | struct parsefile *pf = parsefile; |
| 6882 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6883 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6884 | if (pf->fd >= 0) |
| 6885 | close(pf->fd); |
| 6886 | if (pf->buf) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6887 | free(pf->buf); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6888 | while (pf->strpush) |
| 6889 | popstring(); |
| 6890 | parsefile = pf->prev; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6891 | free(pf); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6892 | parsenleft = parsefile->nleft; |
| 6893 | parselleft = parsefile->lleft; |
| 6894 | parsenextc = parsefile->nextc; |
| 6895 | plinno = parsefile->linno; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 6896 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6897 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6898 | |
| 6899 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6900 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6901 | * Return to top level. |
| 6902 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6903 | static void |
| 6904 | popallfiles(void) |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 6905 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6906 | while (parsefile != &basepf) |
| 6907 | popfile(); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6908 | } |
| 6909 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6910 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6911 | /* |
| 6912 | * Close the file(s) that the shell is reading commands from. Called |
| 6913 | * after a fork is done. |
| 6914 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6915 | static void |
| 6916 | closescript(void) |
| 6917 | { |
| 6918 | popallfiles(); |
| 6919 | if (parsefile->fd > 0) { |
| 6920 | close(parsefile->fd); |
| 6921 | parsefile->fd = 0; |
| 6922 | } |
| 6923 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6924 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6925 | /* jobs.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6926 | |
| 6927 | /* mode flags for set_curjob */ |
| 6928 | #define CUR_DELETE 2 |
| 6929 | #define CUR_RUNNING 1 |
| 6930 | #define CUR_STOPPED 0 |
| 6931 | |
| 6932 | /* mode flags for dowait */ |
| 6933 | #define DOWAIT_NORMAL 0 |
| 6934 | #define DOWAIT_BLOCK 1 |
| 6935 | |
| 6936 | /* array of jobs */ |
| 6937 | static struct job *jobtab; |
| 6938 | /* size of array */ |
| 6939 | static unsigned njobs; |
| 6940 | #if JOBS |
| 6941 | /* pgrp of shell on invocation */ |
| 6942 | static int initialpgrp; |
| 6943 | static int ttyfd = -1; |
| 6944 | #endif |
| 6945 | /* current job */ |
| 6946 | static struct job *curjob; |
| 6947 | /* number of presumed living untracked jobs */ |
| 6948 | static int jobless; |
| 6949 | |
| 6950 | static void set_curjob(struct job *, unsigned); |
| 6951 | #if JOBS |
| 6952 | static int restartjob(struct job *, int); |
| 6953 | static void xtcsetpgrp(int, pid_t); |
| 6954 | static char *commandtext(union node *); |
| 6955 | static void cmdlist(union node *, int); |
| 6956 | static void cmdtxt(union node *); |
| 6957 | static void cmdputs(const char *); |
| 6958 | static void showpipe(struct job *, FILE *); |
| 6959 | #endif |
| 6960 | static int sprint_status(char *, int, int); |
| 6961 | static void freejob(struct job *); |
| 6962 | static struct job *getjob(const char *, int); |
| 6963 | static struct job *growjobtab(void); |
| 6964 | static void forkchild(struct job *, union node *, int); |
| 6965 | static void forkparent(struct job *, union node *, int, pid_t); |
| 6966 | static int dowait(int, struct job *); |
| 6967 | static int getstatus(struct job *); |
| 6968 | |
| 6969 | static void |
| 6970 | set_curjob(struct job *jp, unsigned mode) |
| 6971 | { |
| 6972 | struct job *jp1; |
| 6973 | struct job **jpp, **curp; |
| 6974 | |
| 6975 | /* first remove from list */ |
| 6976 | jpp = curp = &curjob; |
| 6977 | do { |
| 6978 | jp1 = *jpp; |
| 6979 | if (jp1 == jp) |
| 6980 | break; |
| 6981 | jpp = &jp1->prev_job; |
| 6982 | } while (1); |
| 6983 | *jpp = jp1->prev_job; |
| 6984 | |
| 6985 | /* Then re-insert in correct position */ |
| 6986 | jpp = curp; |
| 6987 | switch (mode) { |
| 6988 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 6989 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6990 | abort(); |
| 6991 | #endif |
| 6992 | case CUR_DELETE: |
| 6993 | /* job being deleted */ |
| 6994 | break; |
| 6995 | case CUR_RUNNING: |
| 6996 | /* newly created job or backgrounded job, |
| 6997 | put after all stopped jobs. */ |
| 6998 | do { |
| 6999 | jp1 = *jpp; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7000 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7001 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 7002 | #endif |
| 7003 | break; |
| 7004 | jpp = &jp1->prev_job; |
| 7005 | } while (1); |
| 7006 | /* FALLTHROUGH */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7007 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7008 | case CUR_STOPPED: |
| 7009 | #endif |
| 7010 | /* newly stopped job - becomes curjob */ |
| 7011 | jp->prev_job = *jpp; |
| 7012 | *jpp = jp; |
| 7013 | break; |
| 7014 | } |
| 7015 | } |
| 7016 | |
| 7017 | #if JOBS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7018 | /* |
| 7019 | * Turn job control on and off. |
| 7020 | * |
| 7021 | * Note: This code assumes that the third arg to ioctl is a character |
| 7022 | * pointer, which is true on Berkeley systems but not System V. Since |
| 7023 | * System V doesn't have job control yet, this isn't a problem now. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7024 | * |
| 7025 | * Called with interrupts off. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7026 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7027 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7028 | setjobctl(int on) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7029 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7030 | int fd; |
| 7031 | int pgrp; |
| 7032 | |
| 7033 | if (on == jobctl || rootshell == 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7034 | return; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7035 | if (on) { |
| 7036 | int ofd; |
| 7037 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 7038 | if (fd < 0) { |
Denis Vlasenko | 2f0c0d0 | 2007-01-21 00:41:04 +0000 | [diff] [blame] | 7039 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 7040 | * That sometimes helps to acquire controlling tty. |
| 7041 | * Obviously, a workaround for bugs when someone |
| 7042 | * failed to provide a controlling tty to bash! :) */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7043 | fd += 3; |
| 7044 | while (!isatty(fd) && --fd >= 0) |
| 7045 | ; |
| 7046 | } |
| 7047 | fd = fcntl(fd, F_DUPFD, 10); |
| 7048 | close(ofd); |
| 7049 | if (fd < 0) |
| 7050 | goto out; |
| 7051 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 7052 | do { /* while we are in the background */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7053 | pgrp = tcgetpgrp(fd); |
| 7054 | if (pgrp < 0) { |
| 7055 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7056 | ash_msg("can't access tty; job control turned off"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7057 | mflag = on = 0; |
| 7058 | goto close; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7059 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7060 | if (pgrp == getpgrp()) |
Glenn L McGrath | 7040ecc | 2003-01-06 16:27:07 +0000 | [diff] [blame] | 7061 | break; |
| 7062 | killpg(0, SIGTTIN); |
| 7063 | } while (1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7064 | initialpgrp = pgrp; |
| 7065 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7066 | setsignal(SIGTSTP); |
| 7067 | setsignal(SIGTTOU); |
| 7068 | setsignal(SIGTTIN); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7069 | pgrp = rootpid; |
| 7070 | setpgid(0, pgrp); |
| 7071 | xtcsetpgrp(fd, pgrp); |
| 7072 | } else { |
| 7073 | /* turning job control off */ |
| 7074 | fd = ttyfd; |
| 7075 | pgrp = initialpgrp; |
| 7076 | xtcsetpgrp(fd, pgrp); |
| 7077 | setpgid(0, pgrp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7078 | setsignal(SIGTSTP); |
| 7079 | setsignal(SIGTTOU); |
| 7080 | setsignal(SIGTTIN); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7081 | close: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7082 | close(fd); |
| 7083 | fd = -1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7084 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7085 | ttyfd = fd; |
| 7086 | jobctl = on; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7087 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7088 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7089 | static int |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 7090 | killcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7091 | { |
| 7092 | int signo = -1; |
| 7093 | int list = 0; |
| 7094 | int i; |
| 7095 | pid_t pid; |
| 7096 | struct job *jp; |
| 7097 | |
| 7098 | if (argc <= 1) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7099 | usage: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7100 | ash_msg_and_raise_error( |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7101 | "Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or\n" |
| 7102 | "kill -l [exitstatus]" |
| 7103 | ); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7104 | } |
| 7105 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7106 | if (**++argv == '-') { |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 7107 | signo = get_signum(*argv + 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7108 | if (signo < 0) { |
| 7109 | int c; |
| 7110 | |
| 7111 | while ((c = nextopt("ls:")) != '\0') |
| 7112 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7113 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 7114 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7115 | abort(); |
| 7116 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7117 | case 'l': |
| 7118 | list = 1; |
| 7119 | break; |
| 7120 | case 's': |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 7121 | signo = get_signum(optionarg); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7122 | if (signo < 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7123 | ash_msg_and_raise_error( |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7124 | "invalid signal number or name: %s", |
| 7125 | optionarg |
| 7126 | ); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7127 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7128 | break; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 7129 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7130 | argv = argptr; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7131 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7132 | argv++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7133 | } |
| 7134 | |
| 7135 | if (!list && signo < 0) |
| 7136 | signo = SIGTERM; |
| 7137 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7138 | if ((signo < 0 || !*argv) ^ list) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7139 | goto usage; |
| 7140 | } |
| 7141 | |
| 7142 | if (list) { |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 7143 | const char *name; |
| 7144 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7145 | if (!*argv) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7146 | for (i = 1; i < NSIG; i++) { |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 7147 | name = get_signame(i); |
| 7148 | if (isdigit(*name)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7149 | out1fmt(snlfmt, name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7150 | } |
| 7151 | return 0; |
| 7152 | } |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 7153 | name = get_signame(signo); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 7154 | if (!isdigit(*name)) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7155 | ash_msg_and_raise_error("invalid signal number or exit status: %s", *argptr); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 7156 | out1fmt(snlfmt, name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7157 | return 0; |
| 7158 | } |
| 7159 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7160 | i = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7161 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7162 | if (**argv == '%') { |
| 7163 | jp = getjob(*argv, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7164 | pid = -jp->ps[0].pid; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 7165 | } else { |
| 7166 | pid = **argv == '-' ? |
| 7167 | -number(*argv + 1) : number(*argv); |
| 7168 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7169 | if (kill(pid, signo) != 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7170 | ash_msg("(%d) - %m", pid); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7171 | i = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7172 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7173 | } while (*++argv); |
| 7174 | |
| 7175 | return i; |
| 7176 | } |
| 7177 | #endif /* JOBS */ |
| 7178 | |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 7179 | #if JOBS || DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7180 | static int |
| 7181 | jobno(const struct job *jp) |
| 7182 | { |
| 7183 | return jp - jobtab + 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7184 | } |
| 7185 | #endif |
| 7186 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7187 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7188 | static int |
| 7189 | fgcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7190 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7191 | struct job *jp; |
| 7192 | FILE *out; |
| 7193 | int mode; |
| 7194 | int retval; |
| 7195 | |
| 7196 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 7197 | nextopt(nullstr); |
| 7198 | argv = argptr; |
| 7199 | out = stdout; |
| 7200 | do { |
| 7201 | jp = getjob(*argv, 1); |
| 7202 | if (mode == FORK_BG) { |
| 7203 | set_curjob(jp, CUR_RUNNING); |
| 7204 | fprintf(out, "[%d] ", jobno(jp)); |
| 7205 | } |
| 7206 | outstr(jp->ps->cmd, out); |
| 7207 | showpipe(jp, out); |
| 7208 | retval = restartjob(jp, mode); |
| 7209 | } while (*argv && *++argv); |
| 7210 | return retval; |
| 7211 | } |
| 7212 | |
| 7213 | static int bgcmd(int, char **) __attribute__((__alias__("fgcmd"))); |
| 7214 | |
| 7215 | |
| 7216 | static int |
| 7217 | restartjob(struct job *jp, int mode) |
| 7218 | { |
| 7219 | struct procstat *ps; |
| 7220 | int i; |
| 7221 | int status; |
| 7222 | pid_t pgid; |
| 7223 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7224 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7225 | if (jp->state == JOBDONE) |
| 7226 | goto out; |
| 7227 | jp->state = JOBRUNNING; |
| 7228 | pgid = jp->ps->pid; |
| 7229 | if (mode == FORK_FG) |
| 7230 | xtcsetpgrp(ttyfd, pgid); |
| 7231 | killpg(pgid, SIGCONT); |
| 7232 | ps = jp->ps; |
| 7233 | i = jp->nprocs; |
| 7234 | do { |
| 7235 | if (WIFSTOPPED(ps->status)) { |
| 7236 | ps->status = -1; |
| 7237 | } |
| 7238 | } while (ps++, --i); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7239 | out: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7240 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7241 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7242 | return status; |
| 7243 | } |
| 7244 | #endif |
| 7245 | |
| 7246 | static int |
| 7247 | sprint_status(char *s, int status, int sigonly) |
| 7248 | { |
| 7249 | int col; |
| 7250 | int st; |
| 7251 | |
| 7252 | col = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7253 | if (!WIFEXITED(status)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7254 | #if JOBS |
Glenn L McGrath | a3822de | 2003-09-15 14:42:39 +0000 | [diff] [blame] | 7255 | if (WIFSTOPPED(status)) |
| 7256 | st = WSTOPSIG(status); |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 7257 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7258 | #endif |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 7259 | st = WTERMSIG(status); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7260 | if (sigonly) { |
Glenn L McGrath | 4ddddd1 | 2003-11-25 20:45:38 +0000 | [diff] [blame] | 7261 | if (st == SIGINT || st == SIGPIPE) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 7262 | goto out; |
| 7263 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7264 | if (WIFSTOPPED(status)) |
| 7265 | goto out; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 7266 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7267 | } |
| 7268 | st &= 0x7f; |
Glenn L McGrath | 5c2c8ec | 2003-11-14 21:01:26 +0000 | [diff] [blame] | 7269 | col = fmtstr(s, 32, strsignal(st)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7270 | if (WCOREDUMP(status)) { |
| 7271 | col += fmtstr(s + col, 16, " (core dumped)"); |
| 7272 | } |
| 7273 | } else if (!sigonly) { |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 7274 | st = WEXITSTATUS(status); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7275 | if (st) |
| 7276 | col = fmtstr(s, 16, "Done(%d)", st); |
| 7277 | else |
| 7278 | col = fmtstr(s, 16, "Done"); |
| 7279 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7280 | out: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7281 | return col; |
| 7282 | } |
| 7283 | |
| 7284 | #if JOBS |
| 7285 | static void |
| 7286 | showjob(FILE *out, struct job *jp, int mode) |
| 7287 | { |
| 7288 | struct procstat *ps; |
| 7289 | struct procstat *psend; |
| 7290 | int col; |
| 7291 | int indent; |
| 7292 | char s[80]; |
| 7293 | |
| 7294 | ps = jp->ps; |
| 7295 | |
| 7296 | if (mode & SHOW_PGID) { |
| 7297 | /* just output process (group) id of pipeline */ |
| 7298 | fprintf(out, "%d\n", ps->pid); |
| 7299 | return; |
| 7300 | } |
| 7301 | |
| 7302 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
| 7303 | indent = col; |
| 7304 | |
| 7305 | if (jp == curjob) |
| 7306 | s[col - 2] = '+'; |
| 7307 | else if (curjob && jp == curjob->prev_job) |
| 7308 | s[col - 2] = '-'; |
| 7309 | |
| 7310 | if (mode & SHOW_PID) |
| 7311 | col += fmtstr(s + col, 16, "%d ", ps->pid); |
| 7312 | |
| 7313 | psend = ps + jp->nprocs; |
| 7314 | |
| 7315 | if (jp->state == JOBRUNNING) { |
| 7316 | scopy("Running", s + col); |
| 7317 | col += strlen("Running"); |
| 7318 | } else { |
| 7319 | int status = psend[-1].status; |
| 7320 | #if JOBS |
| 7321 | if (jp->state == JOBSTOPPED) |
| 7322 | status = jp->stopstatus; |
| 7323 | #endif |
| 7324 | col += sprint_status(s + col, status, 0); |
| 7325 | } |
| 7326 | |
| 7327 | goto start; |
| 7328 | |
| 7329 | do { |
| 7330 | /* for each process */ |
| 7331 | col = fmtstr(s, 48, " |\n%*c%d ", indent, ' ', ps->pid) - 3; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7332 | start: |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 7333 | fprintf(out, "%s%*c%s", |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7334 | s, 33 - col >= 0 ? 33 - col : 0, ' ', ps->cmd |
| 7335 | ); |
| 7336 | if (!(mode & SHOW_PID)) { |
| 7337 | showpipe(jp, out); |
| 7338 | break; |
| 7339 | } |
| 7340 | if (++ps == psend) { |
| 7341 | outcslow('\n', out); |
| 7342 | break; |
| 7343 | } |
| 7344 | } while (1); |
| 7345 | |
| 7346 | jp->changed = 0; |
| 7347 | |
| 7348 | if (jp->state == JOBDONE) { |
| 7349 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 7350 | freejob(jp); |
| 7351 | } |
| 7352 | } |
| 7353 | |
| 7354 | |
| 7355 | static int |
| 7356 | jobscmd(int argc, char **argv) |
| 7357 | { |
| 7358 | int mode, m; |
| 7359 | FILE *out; |
| 7360 | |
| 7361 | mode = 0; |
| 7362 | while ((m = nextopt("lp"))) |
| 7363 | if (m == 'l') |
| 7364 | mode = SHOW_PID; |
| 7365 | else |
| 7366 | mode = SHOW_PGID; |
| 7367 | |
| 7368 | out = stdout; |
| 7369 | argv = argptr; |
| 7370 | if (*argv) |
| 7371 | do |
| 7372 | showjob(out, getjob(*argv,0), mode); |
| 7373 | while (*++argv); |
| 7374 | else |
| 7375 | showjobs(out, mode); |
| 7376 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7377 | return 0; |
| 7378 | } |
| 7379 | |
| 7380 | |
| 7381 | /* |
| 7382 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 7383 | * statuses have changed since the last call to showjobs. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7384 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7385 | static void |
| 7386 | showjobs(FILE *out, int mode) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7387 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7388 | struct job *jp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7389 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7390 | TRACE(("showjobs(%x) called\n", mode)); |
| 7391 | |
| 7392 | /* If not even one one job changed, there is nothing to do */ |
| 7393 | while (dowait(DOWAIT_NORMAL, NULL) > 0) |
| 7394 | continue; |
| 7395 | |
| 7396 | for (jp = curjob; jp; jp = jp->prev_job) { |
| 7397 | if (!(mode & SHOW_CHANGED) || jp->changed) |
| 7398 | showjob(out, jp, mode); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7399 | } |
| 7400 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7401 | #endif /* JOBS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7402 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7403 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7404 | /* |
| 7405 | * Mark a job structure as unused. |
| 7406 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7407 | static void |
| 7408 | freejob(struct job *jp) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7409 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7410 | struct procstat *ps; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7411 | int i; |
| 7412 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7413 | INT_OFF; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 7414 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7415 | if (ps->cmd != nullstr) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7416 | free(ps->cmd); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7417 | } |
| 7418 | if (jp->ps != &jp->ps0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7419 | free(jp->ps); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7420 | jp->used = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7421 | set_curjob(jp, CUR_DELETE); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7422 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7423 | } |
| 7424 | |
| 7425 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7426 | static int |
| 7427 | waitcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7428 | { |
| 7429 | struct job *job; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7430 | int retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7431 | struct job *jp; |
| 7432 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7433 | EXSIGON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7434 | |
| 7435 | nextopt(nullstr); |
| 7436 | retval = 0; |
| 7437 | |
| 7438 | argv = argptr; |
| 7439 | if (!*argv) { |
| 7440 | /* wait for all jobs */ |
| 7441 | for (;;) { |
| 7442 | jp = curjob; |
| 7443 | while (1) { |
| 7444 | if (!jp) { |
| 7445 | /* no running procs */ |
| 7446 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7447 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7448 | if (jp->state == JOBRUNNING) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7449 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7450 | jp->waited = 1; |
| 7451 | jp = jp->prev_job; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7452 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7453 | dowait(DOWAIT_BLOCK, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7454 | } |
| 7455 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7456 | |
| 7457 | retval = 127; |
| 7458 | do { |
| 7459 | if (**argv != '%') { |
| 7460 | pid_t pid = number(*argv); |
| 7461 | job = curjob; |
| 7462 | goto start; |
| 7463 | do { |
| 7464 | if (job->ps[job->nprocs - 1].pid == pid) |
| 7465 | break; |
| 7466 | job = job->prev_job; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7467 | start: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7468 | if (!job) |
| 7469 | goto repeat; |
| 7470 | } while (1); |
| 7471 | } else |
| 7472 | job = getjob(*argv, 0); |
| 7473 | /* loop until process terminated or stopped */ |
| 7474 | while (job->state == JOBRUNNING) |
| 7475 | dowait(DOWAIT_BLOCK, 0); |
| 7476 | job->waited = 1; |
| 7477 | retval = getstatus(job); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7478 | repeat: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7479 | ; |
| 7480 | } while (*++argv); |
| 7481 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7482 | out: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7483 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7484 | } |
| 7485 | |
| 7486 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7487 | /* |
| 7488 | * Convert a job name to a job structure. |
| 7489 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7490 | static struct job * |
| 7491 | getjob(const char *name, int getctl) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7492 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7493 | struct job *jp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7494 | struct job *found; |
| 7495 | const char *err_msg = "No such job: %s"; |
| 7496 | unsigned num; |
| 7497 | int c; |
| 7498 | const char *p; |
| 7499 | char *(*match)(const char *, const char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7500 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7501 | jp = curjob; |
| 7502 | p = name; |
| 7503 | if (!p) |
| 7504 | goto currentjob; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 7505 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7506 | if (*p != '%') |
| 7507 | goto err; |
| 7508 | |
| 7509 | c = *++p; |
| 7510 | if (!c) |
| 7511 | goto currentjob; |
| 7512 | |
| 7513 | if (!p[1]) { |
| 7514 | if (c == '+' || c == '%') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7515 | currentjob: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7516 | err_msg = "No current job"; |
| 7517 | goto check; |
| 7518 | } else if (c == '-') { |
| 7519 | if (jp) |
| 7520 | jp = jp->prev_job; |
| 7521 | err_msg = "No previous job"; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7522 | check: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7523 | if (!jp) |
| 7524 | goto err; |
| 7525 | goto gotit; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7526 | } |
| 7527 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7528 | |
| 7529 | if (is_number(p)) { |
| 7530 | num = atoi(p); |
| 7531 | if (num < njobs) { |
| 7532 | jp = jobtab + num - 1; |
| 7533 | if (jp->used) |
| 7534 | goto gotit; |
| 7535 | goto err; |
| 7536 | } |
| 7537 | } |
| 7538 | |
| 7539 | match = prefix; |
| 7540 | if (*p == '?') { |
| 7541 | match = strstr; |
| 7542 | p++; |
| 7543 | } |
| 7544 | |
| 7545 | found = 0; |
| 7546 | while (1) { |
| 7547 | if (!jp) |
| 7548 | goto err; |
| 7549 | if (match(jp->ps[0].cmd, p)) { |
| 7550 | if (found) |
| 7551 | goto err; |
| 7552 | found = jp; |
| 7553 | err_msg = "%s: ambiguous"; |
| 7554 | } |
| 7555 | jp = jp->prev_job; |
| 7556 | } |
| 7557 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7558 | gotit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7559 | #if JOBS |
| 7560 | err_msg = "job %s not created under job control"; |
| 7561 | if (getctl && jp->jobctl == 0) |
| 7562 | goto err; |
| 7563 | #endif |
| 7564 | return jp; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7565 | err: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7566 | ash_msg_and_raise_error(err_msg, name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7567 | } |
| 7568 | |
| 7569 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7570 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7571 | * Return a new job structure. |
| 7572 | * Called with interrupts off. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7573 | */ |
| 7574 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7575 | static struct job * |
| 7576 | makejob(union node *node, int nprocs) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7577 | { |
| 7578 | int i; |
| 7579 | struct job *jp; |
| 7580 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 7581 | for (i = njobs, jp = jobtab; ; jp++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7582 | if (--i < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7583 | jp = growjobtab(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7584 | break; |
| 7585 | } |
| 7586 | if (jp->used == 0) |
| 7587 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7588 | if (jp->state != JOBDONE || !jp->waited) |
| 7589 | continue; |
| 7590 | #if JOBS |
| 7591 | if (jobctl) |
| 7592 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7593 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7594 | freejob(jp); |
| 7595 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7596 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7597 | memset(jp, 0, sizeof(*jp)); |
| 7598 | #if JOBS |
| 7599 | if (jobctl) |
| 7600 | jp->jobctl = 1; |
| 7601 | #endif |
| 7602 | jp->prev_job = curjob; |
| 7603 | curjob = jp; |
| 7604 | jp->used = 1; |
| 7605 | jp->ps = &jp->ps0; |
| 7606 | if (nprocs > 1) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 7607 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7608 | } |
| 7609 | TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 7610 | jobno(jp))); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7611 | return jp; |
| 7612 | } |
| 7613 | |
| 7614 | static struct job * |
| 7615 | growjobtab(void) |
| 7616 | { |
| 7617 | size_t len; |
| 7618 | ptrdiff_t offset; |
| 7619 | struct job *jp, *jq; |
| 7620 | |
| 7621 | len = njobs * sizeof(*jp); |
| 7622 | jq = jobtab; |
| 7623 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 7624 | |
| 7625 | offset = (char *)jp - (char *)jq; |
| 7626 | if (offset) { |
| 7627 | /* Relocate pointers */ |
| 7628 | size_t l = len; |
| 7629 | |
| 7630 | jq = (struct job *)((char *)jq + l); |
| 7631 | while (l) { |
| 7632 | l -= sizeof(*jp); |
| 7633 | jq--; |
| 7634 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 7635 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 7636 | if (xlikely(joff(jp)->ps == &jq->ps0)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7637 | jmove(joff(jp)->ps); |
| 7638 | if (joff(jp)->prev_job) |
| 7639 | jmove(joff(jp)->prev_job); |
| 7640 | } |
| 7641 | if (curjob) |
| 7642 | jmove(curjob); |
| 7643 | #undef joff |
| 7644 | #undef jmove |
| 7645 | } |
| 7646 | |
| 7647 | njobs += 4; |
| 7648 | jobtab = jp; |
| 7649 | jp = (struct job *)((char *)jp + len); |
| 7650 | jq = jp + 3; |
| 7651 | do { |
| 7652 | jq->used = 0; |
| 7653 | } while (--jq >= jp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7654 | return jp; |
| 7655 | } |
| 7656 | |
| 7657 | |
| 7658 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7659 | * Fork off a subshell. If we are doing job control, give the subshell its |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7660 | * own process group. Jp is a job structure that the job is to be added to. |
| 7661 | * N is the command that will be evaluated by the child. Both jp and n may |
| 7662 | * be NULL. The mode parameter can be one of the following: |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7663 | * FORK_FG - Fork off a foreground process. |
| 7664 | * FORK_BG - Fork off a background process. |
| 7665 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 7666 | * process group even if job control is on. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7667 | * |
| 7668 | * When job control is turned off, background processes have their standard |
| 7669 | * input redirected to /dev/null (except for the second and later processes |
| 7670 | * in a pipeline). |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7671 | * |
| 7672 | * Called with interrupts off. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7673 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 7674 | static void forkchild(struct job *jp, union node *n, int mode) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7675 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7676 | int oldlvl; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 7677 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7678 | TRACE(("Child shell %d\n", getpid())); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7679 | oldlvl = shlvl; |
| 7680 | shlvl++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7681 | |
| 7682 | closescript(); |
| 7683 | clear_traps(); |
| 7684 | #if JOBS |
| 7685 | /* do job control only in root shell */ |
| 7686 | jobctl = 0; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7687 | if (mode != FORK_NOJOB && jp->jobctl && !oldlvl) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7688 | pid_t pgrp; |
| 7689 | |
| 7690 | if (jp->nprocs == 0) |
| 7691 | pgrp = getpid(); |
| 7692 | else |
| 7693 | pgrp = jp->ps[0].pid; |
| 7694 | /* This can fail because we are doing it in the parent also */ |
| 7695 | (void)setpgid(0, pgrp); |
| 7696 | if (mode == FORK_FG) |
| 7697 | xtcsetpgrp(ttyfd, pgrp); |
| 7698 | setsignal(SIGTSTP); |
| 7699 | setsignal(SIGTTOU); |
| 7700 | } else |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7701 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7702 | if (mode == FORK_BG) { |
| 7703 | ignoresig(SIGINT); |
| 7704 | ignoresig(SIGQUIT); |
| 7705 | if (jp->nprocs == 0) { |
| 7706 | close(0); |
Bernhard Reutner-Fischer | 0a8812b | 2006-05-19 13:12:21 +0000 | [diff] [blame] | 7707 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7708 | ash_msg_and_raise_error("Can't open %s", bb_dev_null); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7709 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7710 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7711 | if (!oldlvl && iflag) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7712 | setsignal(SIGINT); |
| 7713 | setsignal(SIGQUIT); |
| 7714 | setsignal(SIGTERM); |
| 7715 | } |
| 7716 | for (jp = curjob; jp; jp = jp->prev_job) |
| 7717 | freejob(jp); |
| 7718 | jobless = 0; |
| 7719 | } |
| 7720 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 7721 | static void forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7722 | { |
| 7723 | TRACE(("In parent shell: child = %d\n", pid)); |
| 7724 | if (!jp) { |
| 7725 | while (jobless && dowait(DOWAIT_NORMAL, 0) > 0); |
| 7726 | jobless++; |
| 7727 | return; |
| 7728 | } |
| 7729 | #if JOBS |
| 7730 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 7731 | int pgrp; |
| 7732 | |
| 7733 | if (jp->nprocs == 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7734 | pgrp = pid; |
| 7735 | else |
| 7736 | pgrp = jp->ps[0].pid; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7737 | /* This can fail because we are doing it in the child also */ |
| 7738 | (void)setpgid(pid, pgrp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7739 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7740 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7741 | if (mode == FORK_BG) { |
| 7742 | backgndpid = pid; /* set $! */ |
| 7743 | set_curjob(jp, CUR_RUNNING); |
| 7744 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7745 | if (jp) { |
| 7746 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
| 7747 | ps->pid = pid; |
| 7748 | ps->status = -1; |
| 7749 | ps->cmd = nullstr; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7750 | #if JOBS |
| 7751 | if (jobctl && n) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7752 | ps->cmd = commandtext(n); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7753 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7754 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7755 | } |
| 7756 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7757 | static int |
| 7758 | forkshell(struct job *jp, union node *n, int mode) |
| 7759 | { |
| 7760 | int pid; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7761 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7762 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 7763 | pid = fork(); |
| 7764 | if (pid < 0) { |
| 7765 | TRACE(("Fork failed, errno=%d", errno)); |
| 7766 | if (jp) |
| 7767 | freejob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7768 | ash_msg_and_raise_error("Cannot fork"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7769 | } |
| 7770 | if (pid == 0) |
| 7771 | forkchild(jp, n, mode); |
| 7772 | else |
| 7773 | forkparent(jp, n, mode, pid); |
| 7774 | return pid; |
| 7775 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7776 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7777 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7778 | /* |
| 7779 | * Wait for job to finish. |
| 7780 | * |
| 7781 | * Under job control we have the problem that while a child process is |
| 7782 | * running interrupts generated by the user are sent to the child but not |
| 7783 | * to the shell. This means that an infinite loop started by an inter- |
| 7784 | * active user may be hard to kill. With job control turned off, an |
| 7785 | * interactive user may place an interactive program inside a loop. If |
| 7786 | * the interactive program catches interrupts, the user doesn't want |
| 7787 | * these interrupts to also abort the loop. The approach we take here |
| 7788 | * is to have the shell ignore interrupt signals while waiting for a |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 7789 | * foreground process to terminate, and then send itself an interrupt |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7790 | * signal if the child process was terminated by an interrupt signal. |
| 7791 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 7792 | * exit on interrupt; unless these processes terminate themselves by |
| 7793 | * sending a signal to themselves (instead of calling exit) they will |
| 7794 | * confuse this approach. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7795 | * |
| 7796 | * Called with interrupts off. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7797 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7798 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7799 | waitforjob(struct job *jp) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7800 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7801 | int st; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7802 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7803 | TRACE(("waitforjob(%%%d) called\n", jobno(jp))); |
| 7804 | while (jp->state == JOBRUNNING) { |
| 7805 | dowait(DOWAIT_BLOCK, jp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7806 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7807 | st = getstatus(jp); |
| 7808 | #if JOBS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7809 | if (jp->jobctl) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7810 | xtcsetpgrp(ttyfd, rootpid); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7811 | /* |
| 7812 | * This is truly gross. |
| 7813 | * If we're doing job control, then we did a TIOCSPGRP which |
| 7814 | * caused us (the shell) to no longer be in the controlling |
| 7815 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 7816 | * intuit from the subprocess exit status whether a SIGINT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7817 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7818 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7819 | if (jp->sigint) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7820 | raise(SIGINT); |
| 7821 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7822 | if (jp->state == JOBDONE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7823 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7824 | freejob(jp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7825 | return st; |
| 7826 | } |
| 7827 | |
| 7828 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7829 | /* |
| 7830 | * Do a wait system call. If job control is compiled in, we accept |
| 7831 | * stopped processes. If block is zero, we return a value of zero |
| 7832 | * rather than blocking. |
| 7833 | * |
| 7834 | * System V doesn't have a non-blocking wait system call. It does |
| 7835 | * have a SIGCLD signal that is sent to a process when one of it's |
| 7836 | * children dies. The obvious way to use SIGCLD would be to install |
| 7837 | * a handler for SIGCLD which simply bumped a counter when a SIGCLD |
| 7838 | * was received, and have waitproc bump another counter when it got |
| 7839 | * the status of a process. Waitproc would then know that a wait |
| 7840 | * system call would not block if the two counters were different. |
| 7841 | * This approach doesn't work because if a process has children that |
| 7842 | * have not been waited for, System V will send it a SIGCLD when it |
| 7843 | * installs a signal handler for SIGCLD. What this means is that when |
| 7844 | * a child exits, the shell will be sent SIGCLD signals continuously |
| 7845 | * until is runs out of stack space, unless it does a wait call before |
| 7846 | * restoring the signal handler. The code below takes advantage of |
| 7847 | * this (mis)feature by installing a signal handler for SIGCLD and |
| 7848 | * then checking to see whether it was called. If there are any |
| 7849 | * children to be waited for, it will be. |
| 7850 | * |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7851 | * If neither SYSV nor BSD is defined, we don't implement nonblocking |
| 7852 | * waits at all. In this case, the user will not be informed when |
| 7853 | * a background process until the next time she runs a real program |
| 7854 | * (as opposed to running a builtin command or just typing return), |
| 7855 | * and the jobs command may give out of date information. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7856 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 7857 | static int waitproc(int block, int *status) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7858 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7859 | int flags = 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7860 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7861 | #if JOBS |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7862 | if (jobctl) |
| 7863 | flags |= WUNTRACED; |
| 7864 | #endif |
| 7865 | if (block == 0) |
| 7866 | flags |= WNOHANG; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7867 | return wait3(status, flags, (struct rusage *)NULL); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7868 | } |
| 7869 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7870 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7871 | /* |
| 7872 | * Wait for a process to terminate. |
| 7873 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7874 | static int |
| 7875 | dowait(int block, struct job *job) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7876 | { |
| 7877 | int pid; |
| 7878 | int status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7879 | struct job *jp; |
| 7880 | struct job *thisjob; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7881 | int state; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7882 | |
| 7883 | TRACE(("dowait(%d) called\n", block)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7884 | pid = waitproc(block, &status); |
| 7885 | TRACE(("wait returns pid %d, status=%d\n", pid, status)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7886 | if (pid <= 0) |
| 7887 | return pid; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7888 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7889 | thisjob = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7890 | for (jp = curjob; jp; jp = jp->prev_job) { |
| 7891 | struct procstat *sp; |
| 7892 | struct procstat *spend; |
| 7893 | if (jp->state == JOBDONE) |
| 7894 | continue; |
| 7895 | state = JOBDONE; |
| 7896 | spend = jp->ps + jp->nprocs; |
| 7897 | sp = jp->ps; |
| 7898 | do { |
| 7899 | if (sp->pid == pid) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 7900 | TRACE(("Job %d: changing status of proc %d " |
| 7901 | "from 0x%x to 0x%x\n", |
| 7902 | jobno(jp), pid, sp->status, status)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7903 | sp->status = status; |
| 7904 | thisjob = jp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7905 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7906 | if (sp->status == -1) |
| 7907 | state = JOBRUNNING; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7908 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7909 | if (state == JOBRUNNING) |
| 7910 | continue; |
| 7911 | if (WIFSTOPPED(sp->status)) { |
| 7912 | jp->stopstatus = sp->status; |
| 7913 | state = JOBSTOPPED; |
| 7914 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7915 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7916 | } while (++sp < spend); |
| 7917 | if (thisjob) |
| 7918 | goto gotjob; |
| 7919 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7920 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7921 | if (!WIFSTOPPED(status)) |
| 7922 | #endif |
| 7923 | |
| 7924 | jobless--; |
| 7925 | goto out; |
| 7926 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7927 | gotjob: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7928 | if (state != JOBRUNNING) { |
| 7929 | thisjob->changed = 1; |
| 7930 | |
| 7931 | if (thisjob->state != state) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 7932 | TRACE(("Job %d: changing state from %d to %d\n", |
| 7933 | jobno(thisjob), thisjob->state, state)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7934 | thisjob->state = state; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7935 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7936 | if (state == JOBSTOPPED) { |
| 7937 | set_curjob(thisjob, CUR_STOPPED); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7938 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7939 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7940 | } |
| 7941 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7942 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 7943 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 7944 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7945 | |
| 7946 | if (thisjob && thisjob == job) { |
| 7947 | char s[48 + 1]; |
| 7948 | int len; |
| 7949 | |
| 7950 | len = sprint_status(s, status, 1); |
| 7951 | if (len) { |
| 7952 | s[len] = '\n'; |
| 7953 | s[len + 1] = 0; |
| 7954 | out2str(s); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7955 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7956 | } |
| 7957 | return pid; |
| 7958 | } |
| 7959 | |
| 7960 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7961 | /* |
| 7962 | * return 1 if there are stopped jobs, otherwise 0 |
| 7963 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7964 | int |
| 7965 | stoppedjobs(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7966 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7967 | struct job *jp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7968 | int retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7969 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7970 | retval = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7971 | if (job_warning) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7972 | goto out; |
| 7973 | jp = curjob; |
| 7974 | if (jp && jp->state == JOBSTOPPED) { |
| 7975 | out2str("You have stopped jobs.\n"); |
| 7976 | job_warning = 2; |
| 7977 | retval++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7978 | } |
| 7979 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7980 | out: |
| 7981 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7982 | } |
| 7983 | |
| 7984 | /* |
| 7985 | * Return a string identifying a command (to be printed by the |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7986 | * jobs command). |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7987 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7988 | #if JOBS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7989 | static char *cmdnextc; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 7990 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7991 | static char * |
| 7992 | commandtext(union node *n) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7993 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7994 | char *name; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7995 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7996 | STARTSTACKSTR(cmdnextc); |
| 7997 | cmdtxt(n); |
| 7998 | name = stackblock(); |
| 7999 | TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n", |
| 8000 | name, cmdnextc, cmdnextc)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 8001 | return ckstrdup(name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8002 | } |
| 8003 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8004 | static void |
| 8005 | cmdtxt(union node *n) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8006 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8007 | union node *np; |
| 8008 | struct nodelist *lp; |
| 8009 | const char *p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8010 | char s[2]; |
| 8011 | |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 8012 | if (!n) |
| 8013 | return; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8014 | switch (n->type) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8015 | default: |
| 8016 | #if DEBUG |
| 8017 | abort(); |
| 8018 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8019 | case NPIPE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8020 | lp = n->npipe.cmdlist; |
| 8021 | for (;;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8022 | cmdtxt(lp->n); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8023 | lp = lp->next; |
| 8024 | if (!lp) |
| 8025 | break; |
| 8026 | cmdputs(" | "); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8027 | } |
| 8028 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8029 | case NSEMI: |
| 8030 | p = "; "; |
| 8031 | goto binop; |
| 8032 | case NAND: |
| 8033 | p = " && "; |
| 8034 | goto binop; |
| 8035 | case NOR: |
| 8036 | p = " || "; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8037 | binop: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8038 | cmdtxt(n->nbinary.ch1); |
| 8039 | cmdputs(p); |
| 8040 | n = n->nbinary.ch2; |
| 8041 | goto donode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8042 | case NREDIR: |
| 8043 | case NBACKGND: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8044 | n = n->nredir.n; |
| 8045 | goto donode; |
| 8046 | case NNOT: |
| 8047 | cmdputs("!"); |
| 8048 | n = n->nnot.com; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8049 | donode: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8050 | cmdtxt(n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8051 | break; |
| 8052 | case NIF: |
| 8053 | cmdputs("if "); |
| 8054 | cmdtxt(n->nif.test); |
| 8055 | cmdputs("; then "); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8056 | n = n->nif.ifpart; |
| 8057 | if (n->nif.elsepart) { |
| 8058 | cmdtxt(n); |
| 8059 | cmdputs("; else "); |
| 8060 | n = n->nif.elsepart; |
| 8061 | } |
| 8062 | p = "; fi"; |
| 8063 | goto dotail; |
| 8064 | case NSUBSHELL: |
| 8065 | cmdputs("("); |
| 8066 | n = n->nredir.n; |
| 8067 | p = ")"; |
| 8068 | goto dotail; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8069 | case NWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8070 | p = "while "; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8071 | goto until; |
| 8072 | case NUNTIL: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8073 | p = "until "; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8074 | until: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8075 | cmdputs(p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8076 | cmdtxt(n->nbinary.ch1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8077 | n = n->nbinary.ch2; |
| 8078 | p = "; done"; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8079 | dodo: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8080 | cmdputs("; do "); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8081 | dotail: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8082 | cmdtxt(n); |
| 8083 | goto dotail2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8084 | case NFOR: |
| 8085 | cmdputs("for "); |
| 8086 | cmdputs(n->nfor.var); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8087 | cmdputs(" in "); |
| 8088 | cmdlist(n->nfor.args, 1); |
| 8089 | n = n->nfor.body; |
| 8090 | p = "; done"; |
| 8091 | goto dodo; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8092 | case NDEFUN: |
| 8093 | cmdputs(n->narg.text); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8094 | p = "() { ... }"; |
| 8095 | goto dotail2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8096 | case NCMD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8097 | cmdlist(n->ncmd.args, 1); |
| 8098 | cmdlist(n->ncmd.redirect, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8099 | break; |
| 8100 | case NARG: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8101 | p = n->narg.text; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8102 | dotail2: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8103 | cmdputs(p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8104 | break; |
| 8105 | case NHERE: |
| 8106 | case NXHERE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8107 | p = "<<..."; |
| 8108 | goto dotail2; |
| 8109 | case NCASE: |
| 8110 | cmdputs("case "); |
| 8111 | cmdputs(n->ncase.expr->narg.text); |
| 8112 | cmdputs(" in "); |
| 8113 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 8114 | cmdtxt(np->nclist.pattern); |
| 8115 | cmdputs(") "); |
| 8116 | cmdtxt(np->nclist.body); |
| 8117 | cmdputs(";; "); |
| 8118 | } |
| 8119 | p = "esac"; |
| 8120 | goto dotail2; |
| 8121 | case NTO: |
| 8122 | p = ">"; |
| 8123 | goto redir; |
| 8124 | case NCLOBBER: |
| 8125 | p = ">|"; |
| 8126 | goto redir; |
| 8127 | case NAPPEND: |
| 8128 | p = ">>"; |
| 8129 | goto redir; |
| 8130 | case NTOFD: |
| 8131 | p = ">&"; |
| 8132 | goto redir; |
| 8133 | case NFROM: |
| 8134 | p = "<"; |
| 8135 | goto redir; |
| 8136 | case NFROMFD: |
| 8137 | p = "<&"; |
| 8138 | goto redir; |
| 8139 | case NFROMTO: |
| 8140 | p = "<>"; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8141 | redir: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8142 | s[0] = n->nfile.fd + '0'; |
| 8143 | s[1] = '\0'; |
| 8144 | cmdputs(s); |
| 8145 | cmdputs(p); |
| 8146 | if (n->type == NTOFD || n->type == NFROMFD) { |
| 8147 | s[0] = n->ndup.dupfd + '0'; |
| 8148 | p = s; |
| 8149 | goto dotail2; |
| 8150 | } else { |
| 8151 | n = n->nfile.fname; |
| 8152 | goto donode; |
| 8153 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8154 | } |
| 8155 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8156 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8157 | static void |
| 8158 | cmdlist(union node *np, int sep) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8159 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8160 | for (; np; np = np->narg.next) { |
| 8161 | if (!sep) |
| 8162 | cmdputs(spcstr); |
| 8163 | cmdtxt(np); |
| 8164 | if (sep && np->narg.next) |
| 8165 | cmdputs(spcstr); |
| 8166 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8167 | } |
| 8168 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8169 | static void |
| 8170 | cmdputs(const char *s) |
| 8171 | { |
| 8172 | const char *p, *str; |
| 8173 | char c, cc[2] = " "; |
| 8174 | char *nextc; |
| 8175 | int subtype = 0; |
| 8176 | int quoted = 0; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 8177 | static const char vstype[VSTYPE + 1][4] = { |
| 8178 | "", "}", "-", "+", "?", "=", |
| 8179 | "%", "%%", "#", "##" |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8180 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8181 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 8182 | p = s; |
| 8183 | while ((c = *p++) != 0) { |
| 8184 | str = 0; |
| 8185 | switch (c) { |
| 8186 | case CTLESC: |
| 8187 | c = *p++; |
| 8188 | break; |
| 8189 | case CTLVAR: |
| 8190 | subtype = *p++; |
| 8191 | if ((subtype & VSTYPE) == VSLENGTH) |
| 8192 | str = "${#"; |
| 8193 | else |
| 8194 | str = "${"; |
| 8195 | if (!(subtype & VSQUOTE) != !(quoted & 1)) { |
| 8196 | quoted ^= 1; |
| 8197 | c = '"'; |
| 8198 | } else |
| 8199 | goto dostr; |
| 8200 | break; |
| 8201 | case CTLENDVAR: |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 8202 | str = "\"}" + !(quoted & 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8203 | quoted >>= 1; |
| 8204 | subtype = 0; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 8205 | goto dostr; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8206 | case CTLBACKQ: |
| 8207 | str = "$(...)"; |
| 8208 | goto dostr; |
| 8209 | case CTLBACKQ+CTLQUOTE: |
| 8210 | str = "\"$(...)\""; |
| 8211 | goto dostr; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8212 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8213 | case CTLARI: |
| 8214 | str = "$(("; |
| 8215 | goto dostr; |
| 8216 | case CTLENDARI: |
| 8217 | str = "))"; |
| 8218 | goto dostr; |
| 8219 | #endif |
| 8220 | case CTLQUOTEMARK: |
| 8221 | quoted ^= 1; |
| 8222 | c = '"'; |
| 8223 | break; |
| 8224 | case '=': |
| 8225 | if (subtype == 0) |
| 8226 | break; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 8227 | if ((subtype & VSTYPE) != VSNORMAL) |
| 8228 | quoted <<= 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8229 | str = vstype[subtype & VSTYPE]; |
| 8230 | if (subtype & VSNUL) |
| 8231 | c = ':'; |
| 8232 | else |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 8233 | goto checkstr; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8234 | break; |
| 8235 | case '\'': |
| 8236 | case '\\': |
| 8237 | case '"': |
| 8238 | case '$': |
| 8239 | /* These can only happen inside quotes */ |
| 8240 | cc[0] = c; |
| 8241 | str = cc; |
| 8242 | c = '\\'; |
| 8243 | break; |
| 8244 | default: |
| 8245 | break; |
| 8246 | } |
| 8247 | USTPUTC(c, nextc); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8248 | checkstr: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8249 | if (!str) |
| 8250 | continue; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8251 | dostr: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8252 | while ((c = *str++)) { |
| 8253 | USTPUTC(c, nextc); |
| 8254 | } |
| 8255 | } |
| 8256 | if (quoted & 1) { |
| 8257 | USTPUTC('"', nextc); |
| 8258 | } |
| 8259 | *nextc = 0; |
| 8260 | cmdnextc = nextc; |
| 8261 | } |
| 8262 | |
| 8263 | |
| 8264 | static void |
| 8265 | showpipe(struct job *jp, FILE *out) |
| 8266 | { |
| 8267 | struct procstat *sp; |
| 8268 | struct procstat *spend; |
| 8269 | |
| 8270 | spend = jp->ps + jp->nprocs; |
| 8271 | for (sp = jp->ps + 1; sp < spend; sp++) |
| 8272 | fprintf(out, " | %s", sp->cmd); |
| 8273 | outcslow('\n', out); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8274 | flush_stdout_stderr(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8275 | } |
| 8276 | |
| 8277 | static void |
| 8278 | xtcsetpgrp(int fd, pid_t pgrp) |
| 8279 | { |
| 8280 | if (tcsetpgrp(fd, pgrp)) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8281 | ash_msg_and_raise_error("Cannot set tty process group (%m)"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8282 | } |
| 8283 | #endif /* JOBS */ |
| 8284 | |
| 8285 | static int |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 8286 | getstatus(struct job *job) |
| 8287 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8288 | int status; |
| 8289 | int retval; |
| 8290 | |
| 8291 | status = job->ps[job->nprocs - 1].status; |
| 8292 | retval = WEXITSTATUS(status); |
| 8293 | if (!WIFEXITED(status)) { |
| 8294 | #if JOBS |
| 8295 | retval = WSTOPSIG(status); |
| 8296 | if (!WIFSTOPPED(status)) |
| 8297 | #endif |
| 8298 | { |
| 8299 | /* XXX: limits number of signals */ |
| 8300 | retval = WTERMSIG(status); |
| 8301 | #if JOBS |
| 8302 | if (retval == SIGINT) |
| 8303 | job->sigint = 1; |
| 8304 | #endif |
| 8305 | } |
| 8306 | retval += 128; |
| 8307 | } |
| 8308 | TRACE(("getstatus: job %d, nproc %d, status %x, retval %x\n", |
| 8309 | jobno(job), job->nprocs, status, retval)); |
| 8310 | return retval; |
| 8311 | } |
| 8312 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8313 | #if ENABLE_ASH_MAIL |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8314 | /* mail.c */ |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 8315 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8316 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8317 | * Routines to check for mail. (Perhaps make part of main.c?) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8318 | */ |
| 8319 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8320 | #define MAXMBOXES 10 |
| 8321 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8322 | /* times of mailboxes */ |
| 8323 | static time_t mailtime[MAXMBOXES]; |
| 8324 | /* Set if MAIL or MAILPATH is changed. */ |
| 8325 | static int mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8326 | |
| 8327 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8328 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8329 | * Print appropriate message(s) if mail has arrived. |
| 8330 | * If mail_var_path_changed is set, |
| 8331 | * then the value of MAIL has mail_var_path_changed, |
| 8332 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8333 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8334 | static void |
| 8335 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8336 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8337 | const char *mpath; |
| 8338 | char *p; |
| 8339 | char *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8340 | time_t *mtp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8341 | struct stackmark smark; |
| 8342 | struct stat statb; |
| 8343 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8344 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8345 | mpath = mpathset() ? mpathval() : mailval(); |
| 8346 | for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8347 | p = padvance(&mpath, nullstr); |
| 8348 | if (p == NULL) |
| 8349 | break; |
| 8350 | if (*p == '\0') |
| 8351 | continue; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8352 | for (q = p; *q; q++); |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 8353 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8354 | if (q[-1] != '/') |
| 8355 | abort(); |
| 8356 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8357 | q[-1] = '\0'; /* delete trailing '/' */ |
| 8358 | if (stat(p, &statb) < 0) { |
| 8359 | *mtp = 0; |
| 8360 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8361 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8362 | if (!mail_var_path_changed && statb.st_mtime != *mtp) { |
| 8363 | fprintf( |
| 8364 | stderr, snlfmt, |
| 8365 | pathopt ? pathopt : "you have mail" |
| 8366 | ); |
| 8367 | } |
| 8368 | *mtp = statb.st_mtime; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8369 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8370 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8371 | popstackmark(&smark); |
| 8372 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8373 | |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 8374 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8375 | static void |
| 8376 | changemail(const char *val) |
| 8377 | { |
| 8378 | mail_var_path_changed++; |
| 8379 | } |
| 8380 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8381 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8382 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8383 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8384 | * Take commands from a file. To be compatible we should do a path |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8385 | * search for the file, which is necessary to find sub-commands. |
| 8386 | */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 8387 | static char * |
| 8388 | find_dot_file(char *name) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8389 | { |
| 8390 | char *fullname; |
| 8391 | const char *path = pathval(); |
| 8392 | struct stat statb; |
| 8393 | |
| 8394 | /* don't try this for absolute or relative paths */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8395 | if (strchr(name, '/')) |
| 8396 | return name; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8397 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8398 | while ((fullname = padvance(&path, name)) != NULL) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8399 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 8400 | /* |
| 8401 | * Don't bother freeing here, since it will |
| 8402 | * be freed by the caller. |
| 8403 | */ |
| 8404 | return fullname; |
| 8405 | } |
| 8406 | stunalloc(fullname); |
| 8407 | } |
| 8408 | |
| 8409 | /* not found in the PATH */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8410 | ash_msg_and_raise_error("%s: not found", name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8411 | /* NOTREACHED */ |
| 8412 | } |
| 8413 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8414 | static void |
| 8415 | calcsize(union node *n) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8416 | { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8417 | if (n == NULL) |
| 8418 | return; |
| 8419 | funcblocksize += nodesize[n->type]; |
| 8420 | switch (n->type) { |
| 8421 | case NCMD: |
| 8422 | calcsize(n->ncmd.redirect); |
| 8423 | calcsize(n->ncmd.args); |
| 8424 | calcsize(n->ncmd.assign); |
| 8425 | break; |
| 8426 | case NPIPE: |
| 8427 | sizenodelist(n->npipe.cmdlist); |
| 8428 | break; |
| 8429 | case NREDIR: |
| 8430 | case NBACKGND: |
| 8431 | case NSUBSHELL: |
| 8432 | calcsize(n->nredir.redirect); |
| 8433 | calcsize(n->nredir.n); |
| 8434 | break; |
| 8435 | case NAND: |
| 8436 | case NOR: |
| 8437 | case NSEMI: |
| 8438 | case NWHILE: |
| 8439 | case NUNTIL: |
| 8440 | calcsize(n->nbinary.ch2); |
| 8441 | calcsize(n->nbinary.ch1); |
| 8442 | break; |
| 8443 | case NIF: |
| 8444 | calcsize(n->nif.elsepart); |
| 8445 | calcsize(n->nif.ifpart); |
| 8446 | calcsize(n->nif.test); |
| 8447 | break; |
| 8448 | case NFOR: |
| 8449 | funcstringsize += strlen(n->nfor.var) + 1; |
| 8450 | calcsize(n->nfor.body); |
| 8451 | calcsize(n->nfor.args); |
| 8452 | break; |
| 8453 | case NCASE: |
| 8454 | calcsize(n->ncase.cases); |
| 8455 | calcsize(n->ncase.expr); |
| 8456 | break; |
| 8457 | case NCLIST: |
| 8458 | calcsize(n->nclist.body); |
| 8459 | calcsize(n->nclist.pattern); |
| 8460 | calcsize(n->nclist.next); |
| 8461 | break; |
| 8462 | case NDEFUN: |
| 8463 | case NARG: |
| 8464 | sizenodelist(n->narg.backquote); |
| 8465 | funcstringsize += strlen(n->narg.text) + 1; |
| 8466 | calcsize(n->narg.next); |
| 8467 | break; |
| 8468 | case NTO: |
| 8469 | case NCLOBBER: |
| 8470 | case NFROM: |
| 8471 | case NFROMTO: |
| 8472 | case NAPPEND: |
| 8473 | calcsize(n->nfile.fname); |
| 8474 | calcsize(n->nfile.next); |
| 8475 | break; |
| 8476 | case NTOFD: |
| 8477 | case NFROMFD: |
| 8478 | calcsize(n->ndup.vname); |
| 8479 | calcsize(n->ndup.next); |
| 8480 | break; |
| 8481 | case NHERE: |
| 8482 | case NXHERE: |
| 8483 | calcsize(n->nhere.doc); |
| 8484 | calcsize(n->nhere.next); |
| 8485 | break; |
| 8486 | case NNOT: |
| 8487 | calcsize(n->nnot.com); |
| 8488 | break; |
| 8489 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8490 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8491 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8492 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8493 | static void |
| 8494 | sizenodelist(struct nodelist *lp) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8495 | { |
| 8496 | while (lp) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8497 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8498 | calcsize(lp->n); |
| 8499 | lp = lp->next; |
| 8500 | } |
| 8501 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8502 | |
| 8503 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8504 | static union node * |
| 8505 | copynode(union node *n) |
| 8506 | { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8507 | union node *new; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8508 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8509 | if (n == NULL) |
| 8510 | return NULL; |
| 8511 | new = funcblock; |
| 8512 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 8513 | |
| 8514 | switch (n->type) { |
| 8515 | case NCMD: |
| 8516 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 8517 | new->ncmd.args = copynode(n->ncmd.args); |
| 8518 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 8519 | break; |
| 8520 | case NPIPE: |
| 8521 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
| 8522 | new->npipe.backgnd = n->npipe.backgnd; |
| 8523 | break; |
| 8524 | case NREDIR: |
| 8525 | case NBACKGND: |
| 8526 | case NSUBSHELL: |
| 8527 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 8528 | new->nredir.n = copynode(n->nredir.n); |
| 8529 | break; |
| 8530 | case NAND: |
| 8531 | case NOR: |
| 8532 | case NSEMI: |
| 8533 | case NWHILE: |
| 8534 | case NUNTIL: |
| 8535 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 8536 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 8537 | break; |
| 8538 | case NIF: |
| 8539 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 8540 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 8541 | new->nif.test = copynode(n->nif.test); |
| 8542 | break; |
| 8543 | case NFOR: |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 8544 | new->nfor.var = nodeckstrdup(n->nfor.var); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8545 | new->nfor.body = copynode(n->nfor.body); |
| 8546 | new->nfor.args = copynode(n->nfor.args); |
| 8547 | break; |
| 8548 | case NCASE: |
| 8549 | new->ncase.cases = copynode(n->ncase.cases); |
| 8550 | new->ncase.expr = copynode(n->ncase.expr); |
| 8551 | break; |
| 8552 | case NCLIST: |
| 8553 | new->nclist.body = copynode(n->nclist.body); |
| 8554 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 8555 | new->nclist.next = copynode(n->nclist.next); |
| 8556 | break; |
| 8557 | case NDEFUN: |
| 8558 | case NARG: |
| 8559 | new->narg.backquote = copynodelist(n->narg.backquote); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 8560 | new->narg.text = nodeckstrdup(n->narg.text); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8561 | new->narg.next = copynode(n->narg.next); |
| 8562 | break; |
| 8563 | case NTO: |
| 8564 | case NCLOBBER: |
| 8565 | case NFROM: |
| 8566 | case NFROMTO: |
| 8567 | case NAPPEND: |
| 8568 | new->nfile.fname = copynode(n->nfile.fname); |
| 8569 | new->nfile.fd = n->nfile.fd; |
| 8570 | new->nfile.next = copynode(n->nfile.next); |
| 8571 | break; |
| 8572 | case NTOFD: |
| 8573 | case NFROMFD: |
| 8574 | new->ndup.vname = copynode(n->ndup.vname); |
| 8575 | new->ndup.dupfd = n->ndup.dupfd; |
| 8576 | new->ndup.fd = n->ndup.fd; |
| 8577 | new->ndup.next = copynode(n->ndup.next); |
| 8578 | break; |
| 8579 | case NHERE: |
| 8580 | case NXHERE: |
| 8581 | new->nhere.doc = copynode(n->nhere.doc); |
| 8582 | new->nhere.fd = n->nhere.fd; |
| 8583 | new->nhere.next = copynode(n->nhere.next); |
| 8584 | break; |
| 8585 | case NNOT: |
| 8586 | new->nnot.com = copynode(n->nnot.com); |
| 8587 | break; |
| 8588 | }; |
| 8589 | new->type = n->type; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8590 | return new; |
| 8591 | } |
| 8592 | |
| 8593 | |
| 8594 | static struct nodelist * |
| 8595 | copynodelist(struct nodelist *lp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8596 | { |
| 8597 | struct nodelist *start; |
| 8598 | struct nodelist **lpp; |
| 8599 | |
| 8600 | lpp = &start; |
| 8601 | while (lp) { |
| 8602 | *lpp = funcblock; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8603 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8604 | (*lpp)->n = copynode(lp->n); |
| 8605 | lp = lp->next; |
| 8606 | lpp = &(*lpp)->next; |
| 8607 | } |
| 8608 | *lpp = NULL; |
| 8609 | return start; |
| 8610 | } |
| 8611 | |
| 8612 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8613 | static char * |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 8614 | nodeckstrdup(char *s) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8615 | { |
Denis Vlasenko | 7cfecc4 | 2006-12-18 22:32:45 +0000 | [diff] [blame] | 8616 | char *rtn = funcstring; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8617 | |
Denis Vlasenko | 7cfecc4 | 2006-12-18 22:32:45 +0000 | [diff] [blame] | 8618 | strcpy(funcstring, s); |
| 8619 | funcstring += strlen(s) + 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8620 | return rtn; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8621 | } |
| 8622 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8623 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8624 | /* |
| 8625 | * Free a parse tree. |
| 8626 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8627 | static void |
| 8628 | freefunc(struct funcnode *f) |
| 8629 | { |
| 8630 | if (f && --f->count < 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8631 | free(f); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8632 | } |
| 8633 | |
| 8634 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8635 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8636 | optschanged(void) |
| 8637 | { |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 8638 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8639 | opentrace(); |
| 8640 | #endif |
| 8641 | setinteractive(iflag); |
| 8642 | setjobctl(mflag); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 8643 | setvimode(viflag); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8644 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8645 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8646 | static void |
| 8647 | minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8648 | { |
| 8649 | int i; |
| 8650 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 8651 | if (name) { |
| 8652 | for (i = 0; i < NOPTS; i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8653 | if (equal(name, optnames(i))) { |
| 8654 | optlist[i] = val; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8655 | return; |
| 8656 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 8657 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8658 | ash_msg_and_raise_error("Illegal option -o %s", name); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8659 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 8660 | out1str("Current option settings\n"); |
| 8661 | for (i = 0; i < NOPTS; i++) |
| 8662 | out1fmt("%-16s%s\n", optnames(i), |
| 8663 | optlist[i] ? "on" : "off"); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8664 | } |
| 8665 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8666 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8667 | static void |
| 8668 | setoption(int flag, int val) |
| 8669 | { |
| 8670 | int i; |
| 8671 | |
| 8672 | for (i = 0; i < NOPTS; i++) { |
| 8673 | if (optletters(i) == flag) { |
| 8674 | optlist[i] = val; |
| 8675 | return; |
| 8676 | } |
| 8677 | } |
| 8678 | ash_msg_and_raise_error("Illegal option -%c", flag); |
| 8679 | /* NOTREACHED */ |
| 8680 | } |
| 8681 | |
| 8682 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8683 | /* |
| 8684 | * Process shell options. The global variable argptr contains a pointer |
| 8685 | * to the argument list; we advance it past the options. |
| 8686 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8687 | static void |
| 8688 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8689 | { |
| 8690 | char *p; |
| 8691 | int val; |
| 8692 | int c; |
| 8693 | |
| 8694 | if (cmdline) |
| 8695 | minusc = NULL; |
| 8696 | while ((p = *argptr) != NULL) { |
| 8697 | argptr++; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8698 | c = *p++; |
| 8699 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8700 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 8701 | if (p[0] == '\0' || LONE_DASH(p)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8702 | if (!cmdline) { |
| 8703 | /* "-" means turn off -x and -v */ |
| 8704 | if (p[0] == '\0') |
| 8705 | xflag = vflag = 0; |
| 8706 | /* "--" means reset params */ |
| 8707 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8708 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8709 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8710 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8711 | } |
| 8712 | } else if (c == '+') { |
| 8713 | val = 0; |
| 8714 | } else { |
| 8715 | argptr--; |
| 8716 | break; |
| 8717 | } |
| 8718 | while ((c = *p++) != '\0') { |
| 8719 | if (c == 'c' && cmdline) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8720 | minusc = p; /* command is after shell args*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8721 | } else if (c == 'o') { |
| 8722 | minus_o(*argptr, val); |
| 8723 | if (*argptr) |
| 8724 | argptr++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8725 | } else if (cmdline && (c == '-')) { // long options |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8726 | if (strcmp(p, "login") == 0) |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 8727 | isloginsh = 1; |
| 8728 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8729 | } else { |
| 8730 | setoption(c, val); |
| 8731 | } |
| 8732 | } |
| 8733 | } |
| 8734 | } |
| 8735 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8736 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8737 | /* |
| 8738 | * Set the shell parameters. |
| 8739 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8740 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8741 | setparam(char **argv) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8742 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8743 | char **newparam; |
| 8744 | char **ap; |
| 8745 | int nparam; |
| 8746 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8747 | for (nparam = 0; argv[nparam]; nparam++); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8748 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8749 | while (*argv) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 8750 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8751 | } |
| 8752 | *ap = NULL; |
| 8753 | freeparam(&shellparam); |
| 8754 | shellparam.malloc = 1; |
| 8755 | shellparam.nparam = nparam; |
| 8756 | shellparam.p = newparam; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8757 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8758 | shellparam.optind = 1; |
| 8759 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8760 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8761 | } |
| 8762 | |
| 8763 | |
| 8764 | /* |
| 8765 | * Free the list of positional parameters. |
| 8766 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8767 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8768 | freeparam(volatile struct shparam *param) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8769 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8770 | char **ap; |
| 8771 | |
| 8772 | if (param->malloc) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8773 | for (ap = param->p; *ap; ap++) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8774 | free(*ap); |
| 8775 | free(param->p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8776 | } |
| 8777 | } |
| 8778 | |
| 8779 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8780 | /* |
| 8781 | * The shift builtin command. |
| 8782 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8783 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8784 | shiftcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8785 | { |
| 8786 | int n; |
| 8787 | char **ap1, **ap2; |
| 8788 | |
| 8789 | n = 1; |
| 8790 | if (argc > 1) |
| 8791 | n = number(argv[1]); |
| 8792 | if (n > shellparam.nparam) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8793 | ash_msg_and_raise_error("can't shift that many"); |
| 8794 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8795 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8796 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8797 | if (shellparam.malloc) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8798 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8799 | } |
| 8800 | ap2 = shellparam.p; |
| 8801 | while ((*ap2++ = *ap1++) != NULL); |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8802 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8803 | shellparam.optind = 1; |
| 8804 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8805 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8806 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8807 | return 0; |
| 8808 | } |
| 8809 | |
| 8810 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8811 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8812 | * POSIX requires that 'set' (but not export or readonly) output the |
| 8813 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 8814 | * Maybe we could keep them in an ordered balanced binary tree |
| 8815 | * instead of hashed lists. |
| 8816 | * For now just roll 'em through qsort for printing... |
| 8817 | */ |
| 8818 | static int |
| 8819 | showvars(const char *sep_prefix, int on, int off) |
| 8820 | { |
| 8821 | const char *sep; |
| 8822 | char **ep, **epend; |
| 8823 | |
| 8824 | ep = listvars(on, off, &epend); |
| 8825 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 8826 | |
| 8827 | sep = *sep_prefix ? spcstr : sep_prefix; |
| 8828 | |
| 8829 | for (; ep < epend; ep++) { |
| 8830 | const char *p; |
| 8831 | const char *q; |
| 8832 | |
| 8833 | p = strchrnul(*ep, '='); |
| 8834 | q = nullstr; |
| 8835 | if (*p) |
| 8836 | q = single_quote(++p); |
| 8837 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 8838 | } |
| 8839 | return 0; |
| 8840 | } |
| 8841 | |
| 8842 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8843 | * The set command builtin. |
| 8844 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8845 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8846 | setcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8847 | { |
| 8848 | if (argc == 1) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8849 | return showvars(nullstr, 0, VUNSET); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8850 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8851 | options(0); |
| 8852 | optschanged(); |
| 8853 | if (*argptr != NULL) { |
| 8854 | setparam(argptr); |
| 8855 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8856 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8857 | return 0; |
| 8858 | } |
| 8859 | |
| 8860 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8861 | #if ENABLE_LOCALE_SUPPORT |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8862 | static void change_lc_all(const char *value) |
| 8863 | { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 8864 | if (value && *value != '\0') |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8865 | setlocale(LC_ALL, value); |
| 8866 | } |
| 8867 | |
| 8868 | static void change_lc_ctype(const char *value) |
| 8869 | { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 8870 | if (value && *value != '\0') |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8871 | setlocale(LC_CTYPE, value); |
| 8872 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8873 | #endif |
| 8874 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8875 | #if ENABLE_ASH_RANDOM_SUPPORT |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 8876 | /* Roughly copied from bash.. */ |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 8877 | static void change_random(const char *value) |
| 8878 | { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8879 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 8880 | /* "get", generate */ |
| 8881 | char buf[16]; |
| 8882 | |
| 8883 | rseed = rseed * 1103515245 + 12345; |
| 8884 | sprintf(buf, "%d", (unsigned int)((rseed & 32767))); |
| 8885 | /* set without recursion */ |
| 8886 | setvar(vrandom.text, buf, VNOFUNC); |
| 8887 | vrandom.flags &= ~VNOFUNC; |
| 8888 | } else { |
| 8889 | /* set/reset */ |
| 8890 | rseed = strtoul(value, (char **)NULL, 10); |
| 8891 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 8892 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 8893 | #endif |
| 8894 | |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 8895 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8896 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8897 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8898 | getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8899 | { |
| 8900 | char *p, *q; |
| 8901 | char c = '?'; |
| 8902 | int done = 0; |
| 8903 | int err = 0; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 8904 | char s[12]; |
| 8905 | char **optnext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8906 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8907 | if (*param_optind < 1) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 8908 | return 1; |
| 8909 | optnext = optfirst + *param_optind - 1; |
| 8910 | |
| 8911 | if (*param_optind <= 1 || *optoff < 0 || strlen(optnext[-1]) < *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8912 | p = NULL; |
| 8913 | else |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 8914 | p = optnext[-1] + *optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8915 | if (p == NULL || *p == '\0') { |
| 8916 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8917 | p = *optnext; |
| 8918 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8919 | atend: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8920 | p = NULL; |
| 8921 | done = 1; |
| 8922 | goto out; |
| 8923 | } |
| 8924 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 8925 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8926 | goto atend; |
| 8927 | } |
| 8928 | |
| 8929 | c = *p++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8930 | for (q = optstr; *q != c; ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8931 | if (*q == '\0') { |
| 8932 | if (optstr[0] == ':') { |
| 8933 | s[0] = c; |
| 8934 | s[1] = '\0'; |
| 8935 | err |= setvarsafe("OPTARG", s, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8936 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8937 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8938 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8939 | } |
| 8940 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8941 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8942 | } |
| 8943 | if (*++q == ':') |
| 8944 | q++; |
| 8945 | } |
| 8946 | |
| 8947 | if (*++q == ':') { |
| 8948 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 8949 | if (optstr[0] == ':') { |
| 8950 | s[0] = c; |
| 8951 | s[1] = '\0'; |
| 8952 | err |= setvarsafe("OPTARG", s, 0); |
| 8953 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8954 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8955 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8956 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8957 | c = '?'; |
| 8958 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8959 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8960 | } |
| 8961 | |
| 8962 | if (p == *optnext) |
| 8963 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8964 | err |= setvarsafe("OPTARG", p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8965 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8966 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8967 | err |= setvarsafe("OPTARG", nullstr, 0); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8968 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8969 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8970 | *param_optind = optnext - optfirst + 1; |
| 8971 | fmtstr(s, sizeof(s), "%d", *param_optind); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8972 | err |= setvarsafe("OPTIND", s, VNOFUNC); |
| 8973 | s[0] = c; |
| 8974 | s[1] = '\0'; |
| 8975 | err |= setvarsafe(optvar, s, 0); |
| 8976 | if (err) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8977 | *param_optind = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8978 | *optoff = -1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8979 | flush_stdout_stderr(); |
| 8980 | raise_exception(EXERROR); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8981 | } |
| 8982 | return done; |
| 8983 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8984 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8985 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8986 | /* |
| 8987 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 8988 | * to be processed. Shellparam.optptr points to the next character to |
| 8989 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 8990 | * then it's the first time getopts has been called. |
| 8991 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8992 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8993 | getoptscmd(int argc, char **argv) |
| 8994 | { |
| 8995 | char **optbase; |
| 8996 | |
| 8997 | if (argc < 3) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8998 | ash_msg_and_raise_error("Usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8999 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9000 | optbase = shellparam.p; |
| 9001 | if (shellparam.optind > shellparam.nparam + 1) { |
| 9002 | shellparam.optind = 1; |
| 9003 | shellparam.optoff = -1; |
| 9004 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9005 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9006 | optbase = &argv[3]; |
| 9007 | if (shellparam.optind > argc - 2) { |
| 9008 | shellparam.optind = 1; |
| 9009 | shellparam.optoff = -1; |
| 9010 | } |
| 9011 | } |
| 9012 | |
| 9013 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9014 | &shellparam.optoff); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9015 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9016 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9017 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9018 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9019 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9020 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9021 | static void raise_error_syntax(const char *) ATTRIBUTE_NORETURN; |
| 9022 | static void |
| 9023 | raise_error_syntax(const char *msg) |
| 9024 | { |
| 9025 | ash_msg_and_raise_error("Syntax error: %s", msg); |
| 9026 | /* NOTREACHED */ |
| 9027 | } |
| 9028 | |
| 9029 | /* |
| 9030 | * Called when an unexpected token is read during the parse. The argument |
| 9031 | * is the token that is expected, or -1 if more than one type of token can |
| 9032 | * occur at this point. |
| 9033 | */ |
| 9034 | static void raise_error_unexpected_syntax(int) ATTRIBUTE_NORETURN; |
| 9035 | static void |
| 9036 | raise_error_unexpected_syntax(int token) |
| 9037 | { |
| 9038 | char msg[64]; |
| 9039 | int l; |
| 9040 | |
| 9041 | l = sprintf(msg, "%s unexpected", tokname(lasttoken)); |
| 9042 | if (token >= 0) |
| 9043 | sprintf(msg + l, " (expecting %s)", tokname(token)); |
| 9044 | raise_error_syntax(msg); |
| 9045 | /* NOTREACHED */ |
| 9046 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9047 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9048 | #define EOFMARKLEN 79 |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9049 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9050 | struct heredoc { |
| 9051 | struct heredoc *next; /* next here document in list */ |
| 9052 | union node *here; /* redirection node */ |
| 9053 | char *eofmark; /* string indicating end of input */ |
| 9054 | int striptabs; /* if set, strip leading tabs */ |
| 9055 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9056 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9057 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 9058 | |
| 9059 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 9060 | static union node *andor(void); |
| 9061 | static union node *pipeline(void); |
| 9062 | static union node *parse_command(void); |
| 9063 | static void parseheredoc(void); |
| 9064 | static char peektoken(void); |
| 9065 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9066 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9067 | static union node * |
| 9068 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9069 | { |
| 9070 | union node *n1, *n2, *n3; |
| 9071 | int tok; |
| 9072 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9073 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 9074 | if (nlflag == 2 && peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9075 | return NULL; |
| 9076 | n1 = NULL; |
| 9077 | for (;;) { |
| 9078 | n2 = andor(); |
| 9079 | tok = readtoken(); |
| 9080 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9081 | if (n2->type == NPIPE) { |
| 9082 | n2->npipe.backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9083 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9084 | if (n2->type != NREDIR) { |
| 9085 | n3 = stalloc(sizeof(struct nredir)); |
| 9086 | n3->nredir.n = n2; |
| 9087 | n3->nredir.redirect = NULL; |
| 9088 | n2 = n3; |
| 9089 | } |
| 9090 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9091 | } |
| 9092 | } |
| 9093 | if (n1 == NULL) { |
| 9094 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9095 | } else { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9096 | n3 = stalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9097 | n3->type = NSEMI; |
| 9098 | n3->nbinary.ch1 = n1; |
| 9099 | n3->nbinary.ch2 = n2; |
| 9100 | n1 = n3; |
| 9101 | } |
| 9102 | switch (tok) { |
| 9103 | case TBACKGND: |
| 9104 | case TSEMI: |
| 9105 | tok = readtoken(); |
| 9106 | /* fall through */ |
| 9107 | case TNL: |
| 9108 | if (tok == TNL) { |
| 9109 | parseheredoc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9110 | if (nlflag == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9111 | return n1; |
| 9112 | } else { |
| 9113 | tokpushback++; |
| 9114 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9115 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 9116 | if (peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9117 | return n1; |
| 9118 | break; |
| 9119 | case TEOF: |
| 9120 | if (heredoclist) |
| 9121 | parseheredoc(); |
| 9122 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9123 | pungetc(); /* push back EOF on input */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9124 | return n1; |
| 9125 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9126 | if (nlflag == 1) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9127 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9128 | tokpushback++; |
| 9129 | return n1; |
| 9130 | } |
| 9131 | } |
| 9132 | } |
| 9133 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9134 | static union node * |
| 9135 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9136 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9137 | union node *n1, *n2, *n3; |
| 9138 | int t; |
| 9139 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9140 | n1 = pipeline(); |
| 9141 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9142 | t = readtoken(); |
| 9143 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9144 | t = NAND; |
| 9145 | } else if (t == TOR) { |
| 9146 | t = NOR; |
| 9147 | } else { |
| 9148 | tokpushback++; |
| 9149 | return n1; |
| 9150 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9151 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9152 | n2 = pipeline(); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9153 | n3 = stalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9154 | n3->type = t; |
| 9155 | n3->nbinary.ch1 = n1; |
| 9156 | n3->nbinary.ch2 = n2; |
| 9157 | n1 = n3; |
| 9158 | } |
| 9159 | } |
| 9160 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9161 | static union node * |
| 9162 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9163 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9164 | union node *n1, *n2, *pipenode; |
| 9165 | struct nodelist *lp, *prev; |
| 9166 | int negate; |
| 9167 | |
| 9168 | negate = 0; |
| 9169 | TRACE(("pipeline: entered\n")); |
| 9170 | if (readtoken() == TNOT) { |
| 9171 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9172 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9173 | } else |
| 9174 | tokpushback++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9175 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9176 | if (readtoken() == TPIPE) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9177 | pipenode = stalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9178 | pipenode->type = NPIPE; |
| 9179 | pipenode->npipe.backgnd = 0; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9180 | lp = stalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9181 | pipenode->npipe.cmdlist = lp; |
| 9182 | lp->n = n1; |
| 9183 | do { |
| 9184 | prev = lp; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9185 | lp = stalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9186 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9187 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9188 | prev->next = lp; |
| 9189 | } while (readtoken() == TPIPE); |
| 9190 | lp->next = NULL; |
| 9191 | n1 = pipenode; |
| 9192 | } |
| 9193 | tokpushback++; |
| 9194 | if (negate) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9195 | n2 = stalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9196 | n2->type = NNOT; |
| 9197 | n2->nnot.com = n1; |
| 9198 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9199 | } |
| 9200 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9201 | } |
| 9202 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9203 | static union node * |
| 9204 | makename(void) |
| 9205 | { |
| 9206 | union node *n; |
| 9207 | |
| 9208 | n = stalloc(sizeof(struct narg)); |
| 9209 | n->type = NARG; |
| 9210 | n->narg.next = NULL; |
| 9211 | n->narg.text = wordtext; |
| 9212 | n->narg.backquote = backquotelist; |
| 9213 | return n; |
| 9214 | } |
| 9215 | |
| 9216 | static void |
| 9217 | fixredir(union node *n, const char *text, int err) |
| 9218 | { |
| 9219 | TRACE(("Fix redir %s %d\n", text, err)); |
| 9220 | if (!err) |
| 9221 | n->ndup.vname = NULL; |
| 9222 | |
| 9223 | if (isdigit(text[0]) && text[1] == '\0') |
| 9224 | n->ndup.dupfd = digit_val(text[0]); |
| 9225 | else if (LONE_DASH(text)) |
| 9226 | n->ndup.dupfd = -1; |
| 9227 | else { |
| 9228 | if (err) |
| 9229 | raise_error_syntax("Bad fd number"); |
| 9230 | n->ndup.vname = makename(); |
| 9231 | } |
| 9232 | } |
| 9233 | |
| 9234 | /* |
| 9235 | * Returns true if the text contains nothing to expand (no dollar signs |
| 9236 | * or backquotes). |
| 9237 | */ |
| 9238 | static int |
| 9239 | noexpand(char *text) |
| 9240 | { |
| 9241 | char *p; |
| 9242 | char c; |
| 9243 | |
| 9244 | p = text; |
| 9245 | while ((c = *p++) != '\0') { |
| 9246 | if (c == CTLQUOTEMARK) |
| 9247 | continue; |
| 9248 | if (c == CTLESC) |
| 9249 | p++; |
| 9250 | else if (SIT(c, BASESYNTAX) == CCTL) |
| 9251 | return 0; |
| 9252 | } |
| 9253 | return 1; |
| 9254 | } |
| 9255 | |
| 9256 | static void |
| 9257 | parsefname(void) |
| 9258 | { |
| 9259 | union node *n = redirnode; |
| 9260 | |
| 9261 | if (readtoken() != TWORD) |
| 9262 | raise_error_unexpected_syntax(-1); |
| 9263 | if (n->type == NHERE) { |
| 9264 | struct heredoc *here = heredoc; |
| 9265 | struct heredoc *p; |
| 9266 | int i; |
| 9267 | |
| 9268 | if (quoteflag == 0) |
| 9269 | n->type = NXHERE; |
| 9270 | TRACE(("Here document %d\n", n->type)); |
| 9271 | if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
| 9272 | raise_error_syntax("Illegal eof marker for << redirection"); |
| 9273 | rmescapes(wordtext); |
| 9274 | here->eofmark = wordtext; |
| 9275 | here->next = NULL; |
| 9276 | if (heredoclist == NULL) |
| 9277 | heredoclist = here; |
| 9278 | else { |
| 9279 | for (p = heredoclist; p->next; p = p->next); |
| 9280 | p->next = here; |
| 9281 | } |
| 9282 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 9283 | fixredir(n, wordtext, 0); |
| 9284 | } else { |
| 9285 | n->nfile.fname = makename(); |
| 9286 | } |
| 9287 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9288 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9289 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9290 | simplecmd(void) |
| 9291 | { |
| 9292 | union node *args, **app; |
| 9293 | union node *n = NULL; |
| 9294 | union node *vars, **vpp; |
| 9295 | union node **rpp, *redir; |
| 9296 | int savecheckkwd; |
| 9297 | |
| 9298 | args = NULL; |
| 9299 | app = &args; |
| 9300 | vars = NULL; |
| 9301 | vpp = &vars; |
| 9302 | redir = NULL; |
| 9303 | rpp = &redir; |
| 9304 | |
| 9305 | savecheckkwd = CHKALIAS; |
| 9306 | for (;;) { |
| 9307 | checkkwd = savecheckkwd; |
| 9308 | switch (readtoken()) { |
| 9309 | case TWORD: |
| 9310 | n = stalloc(sizeof(struct narg)); |
| 9311 | n->type = NARG; |
| 9312 | n->narg.text = wordtext; |
| 9313 | n->narg.backquote = backquotelist; |
| 9314 | if (savecheckkwd && isassignment(wordtext)) { |
| 9315 | *vpp = n; |
| 9316 | vpp = &n->narg.next; |
| 9317 | } else { |
| 9318 | *app = n; |
| 9319 | app = &n->narg.next; |
| 9320 | savecheckkwd = 0; |
| 9321 | } |
| 9322 | break; |
| 9323 | case TREDIR: |
| 9324 | *rpp = n = redirnode; |
| 9325 | rpp = &n->nfile.next; |
| 9326 | parsefname(); /* read name of redirection file */ |
| 9327 | break; |
| 9328 | case TLP: |
| 9329 | if (args && app == &args->narg.next |
| 9330 | && !vars && !redir |
| 9331 | ) { |
| 9332 | struct builtincmd *bcmd; |
| 9333 | const char *name; |
| 9334 | |
| 9335 | /* We have a function */ |
| 9336 | if (readtoken() != TRP) |
| 9337 | raise_error_unexpected_syntax(TRP); |
| 9338 | name = n->narg.text; |
| 9339 | if (!goodname(name) |
| 9340 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 9341 | ) { |
| 9342 | raise_error_syntax("Bad function name"); |
| 9343 | } |
| 9344 | n->type = NDEFUN; |
| 9345 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 9346 | n->narg.next = parse_command(); |
| 9347 | return n; |
| 9348 | } |
| 9349 | /* fall through */ |
| 9350 | default: |
| 9351 | tokpushback++; |
| 9352 | goto out; |
| 9353 | } |
| 9354 | } |
| 9355 | out: |
| 9356 | *app = NULL; |
| 9357 | *vpp = NULL; |
| 9358 | *rpp = NULL; |
| 9359 | n = stalloc(sizeof(struct ncmd)); |
| 9360 | n->type = NCMD; |
| 9361 | n->ncmd.args = args; |
| 9362 | n->ncmd.assign = vars; |
| 9363 | n->ncmd.redirect = redir; |
| 9364 | return n; |
| 9365 | } |
| 9366 | |
| 9367 | static union node * |
| 9368 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9369 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9370 | union node *n1, *n2; |
| 9371 | union node *ap, **app; |
| 9372 | union node *cp, **cpp; |
| 9373 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9374 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9375 | int t; |
| 9376 | |
| 9377 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9378 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 9379 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9380 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9381 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9382 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9383 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9384 | case TIF: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9385 | n1 = stalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9386 | n1->type = NIF; |
| 9387 | n1->nif.test = list(0); |
| 9388 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9389 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9390 | n1->nif.ifpart = list(0); |
| 9391 | n2 = n1; |
| 9392 | while (readtoken() == TELIF) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9393 | n2->nif.elsepart = stalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9394 | n2 = n2->nif.elsepart; |
| 9395 | n2->type = NIF; |
| 9396 | n2->nif.test = list(0); |
| 9397 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9398 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9399 | n2->nif.ifpart = list(0); |
| 9400 | } |
| 9401 | if (lasttoken == TELSE) |
| 9402 | n2->nif.elsepart = list(0); |
| 9403 | else { |
| 9404 | n2->nif.elsepart = NULL; |
| 9405 | tokpushback++; |
| 9406 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9407 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9408 | break; |
| 9409 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9410 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9411 | int got; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9412 | n1 = stalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9413 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9414 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9415 | got = readtoken(); |
| 9416 | if (got != TDO) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9417 | TRACE(("expecting DO got %s %s\n", tokname(got), |
| 9418 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9419 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9420 | } |
| 9421 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9422 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9423 | break; |
| 9424 | } |
| 9425 | case TFOR: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9426 | if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9427 | raise_error_syntax("Bad for loop variable"); |
| 9428 | n1 = stalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9429 | n1->type = NFOR; |
| 9430 | n1->nfor.var = wordtext; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9431 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9432 | if (readtoken() == TIN) { |
| 9433 | app = ≈ |
| 9434 | while (readtoken() == TWORD) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9435 | n2 = stalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9436 | n2->type = NARG; |
| 9437 | n2->narg.text = wordtext; |
| 9438 | n2->narg.backquote = backquotelist; |
| 9439 | *app = n2; |
| 9440 | app = &n2->narg.next; |
| 9441 | } |
| 9442 | *app = NULL; |
| 9443 | n1->nfor.args = ap; |
| 9444 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9445 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9446 | } else { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9447 | n2 = stalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9448 | n2->type = NARG; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9449 | n2->narg.text = (char *)dolatstr; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9450 | n2->narg.backquote = NULL; |
| 9451 | n2->narg.next = NULL; |
| 9452 | n1->nfor.args = n2; |
| 9453 | /* |
| 9454 | * Newline or semicolon here is optional (but note |
| 9455 | * that the original Bourne shell only allowed NL). |
| 9456 | */ |
| 9457 | if (lasttoken != TNL && lasttoken != TSEMI) |
| 9458 | tokpushback++; |
| 9459 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9460 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9461 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9462 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9463 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9464 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9465 | break; |
| 9466 | case TCASE: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9467 | n1 = stalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9468 | n1->type = NCASE; |
| 9469 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9470 | raise_error_unexpected_syntax(TWORD); |
| 9471 | n1->ncase.expr = n2 = stalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9472 | n2->type = NARG; |
| 9473 | n2->narg.text = wordtext; |
| 9474 | n2->narg.backquote = backquotelist; |
| 9475 | n2->narg.next = NULL; |
| 9476 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9477 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9478 | } while (readtoken() == TNL); |
| 9479 | if (lasttoken != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9480 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9481 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9482 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9483 | checkkwd = CHKNL | CHKKWD; |
| 9484 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9485 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9486 | if (lasttoken == TLP) |
| 9487 | readtoken(); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9488 | *cpp = cp = stalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9489 | cp->type = NCLIST; |
| 9490 | app = &cp->nclist.pattern; |
| 9491 | for (;;) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9492 | *app = ap = stalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9493 | ap->type = NARG; |
| 9494 | ap->narg.text = wordtext; |
| 9495 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9496 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9497 | break; |
| 9498 | app = &ap->narg.next; |
| 9499 | readtoken(); |
| 9500 | } |
| 9501 | ap->narg.next = NULL; |
| 9502 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9503 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9504 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9505 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9506 | cpp = &cp->nclist.next; |
| 9507 | |
| 9508 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9509 | t = readtoken(); |
| 9510 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9511 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9512 | raise_error_unexpected_syntax(TENDCASE); |
| 9513 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9514 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9515 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9516 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9517 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9518 | case TLP: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9519 | n1 = stalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9520 | n1->type = NSUBSHELL; |
| 9521 | n1->nredir.n = list(0); |
| 9522 | n1->nredir.redirect = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9523 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9524 | break; |
| 9525 | case TBEGIN: |
| 9526 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9527 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9528 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9529 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9530 | case TREDIR: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9531 | tokpushback++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9532 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9533 | } |
| 9534 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9535 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9536 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9537 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9538 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9539 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9540 | checkkwd = CHKKWD | CHKALIAS; |
| 9541 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9542 | while (readtoken() == TREDIR) { |
| 9543 | *rpp = n2 = redirnode; |
| 9544 | rpp = &n2->nfile.next; |
| 9545 | parsefname(); |
| 9546 | } |
| 9547 | tokpushback++; |
| 9548 | *rpp = NULL; |
| 9549 | if (redir) { |
| 9550 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9551 | n2 = stalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9552 | n2->type = NREDIR; |
| 9553 | n2->nredir.n = n1; |
| 9554 | n1 = n2; |
| 9555 | } |
| 9556 | n1->nredir.redirect = redir; |
| 9557 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9558 | return n1; |
| 9559 | } |
| 9560 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9561 | /* |
| 9562 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 9563 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 9564 | * word which marks the end of the document and striptabs is true if |
| 9565 | * leading tabs should be stripped from the document. The argument firstc |
| 9566 | * is the first character of the input token or document. |
| 9567 | * |
| 9568 | * Because C does not have internal subroutines, I have simulated them |
| 9569 | * using goto's to implement the subroutine linkage. The following macros |
| 9570 | * will run code that appears at the end of readtoken1. |
| 9571 | */ |
| 9572 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9573 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 9574 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 9575 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 9576 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 9577 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 9578 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9579 | |
| 9580 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9581 | readtoken1(int firstc, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 9582 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9583 | int c = firstc; |
| 9584 | char *out; |
| 9585 | int len; |
| 9586 | char line[EOFMARKLEN + 1]; |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 9587 | struct nodelist *bqlist = 0; |
| 9588 | int quotef = 0; |
| 9589 | int dblquote = 0; |
| 9590 | int varnest = 0; /* levels of variables expansion */ |
| 9591 | int arinest = 0; /* levels of arithmetic expansion */ |
| 9592 | int parenlevel = 0; /* levels of parens in arithmetic */ |
| 9593 | int dqvarnest = 0; /* levels of variables expansion within double quotes */ |
| 9594 | int oldstyle = 0; |
| 9595 | int prevsyntax = 0; /* syntax before arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9596 | #if __GNUC__ |
| 9597 | /* Avoid longjmp clobbering */ |
| 9598 | (void) &out; |
| 9599 | (void) "ef; |
| 9600 | (void) &dblquote; |
| 9601 | (void) &varnest; |
| 9602 | (void) &arinest; |
| 9603 | (void) &parenlevel; |
| 9604 | (void) &dqvarnest; |
| 9605 | (void) &oldstyle; |
| 9606 | (void) &prevsyntax; |
| 9607 | (void) &syntax; |
| 9608 | #endif |
| 9609 | |
| 9610 | startlinno = plinno; |
| 9611 | dblquote = 0; |
| 9612 | if (syntax == DQSYNTAX) |
| 9613 | dblquote = 1; |
| 9614 | quotef = 0; |
| 9615 | bqlist = NULL; |
| 9616 | varnest = 0; |
| 9617 | arinest = 0; |
| 9618 | parenlevel = 0; |
| 9619 | dqvarnest = 0; |
| 9620 | |
| 9621 | STARTSTACKSTR(out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9622 | loop: { /* for each line, until end of word */ |
| 9623 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 9624 | for (;;) { /* until end of line or end of word */ |
| 9625 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 9626 | switch (SIT(c, syntax)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9627 | case CNL: /* '\n' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9628 | if (syntax == BASESYNTAX) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9629 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9630 | USTPUTC(c, out); |
| 9631 | plinno++; |
| 9632 | if (doprompt) |
| 9633 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9634 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9635 | goto loop; /* continue outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9636 | case CWORD: |
| 9637 | USTPUTC(c, out); |
| 9638 | break; |
| 9639 | case CCTL: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9640 | if (eofmark == NULL || dblquote) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9641 | USTPUTC(CTLESC, out); |
| 9642 | USTPUTC(c, out); |
| 9643 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9644 | case CBACK: /* backslash */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9645 | c = pgetc2(); |
| 9646 | if (c == PEOF) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9647 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9648 | USTPUTC('\\', out); |
| 9649 | pungetc(); |
| 9650 | } else if (c == '\n') { |
| 9651 | if (doprompt) |
| 9652 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9653 | } else { |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 9654 | if (dblquote && |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9655 | c != '\\' && c != '`' && |
| 9656 | c != '$' && ( |
| 9657 | c != '"' || |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 9658 | eofmark != NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9659 | ) { |
| 9660 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9661 | USTPUTC('\\', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9662 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9663 | if (SIT(c, SQSYNTAX) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9664 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9665 | USTPUTC(c, out); |
| 9666 | quotef++; |
| 9667 | } |
| 9668 | break; |
| 9669 | case CSQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9670 | syntax = SQSYNTAX; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9671 | quotemark: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9672 | if (eofmark == NULL) { |
| 9673 | USTPUTC(CTLQUOTEMARK, out); |
| 9674 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9675 | break; |
| 9676 | case CDQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9677 | syntax = DQSYNTAX; |
| 9678 | dblquote = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9679 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9680 | case CENDQUOTE: |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9681 | if (eofmark != NULL && arinest == 0 |
| 9682 | && varnest == 0 |
| 9683 | ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9684 | USTPUTC(c, out); |
| 9685 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9686 | if (dqvarnest == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9687 | syntax = BASESYNTAX; |
| 9688 | dblquote = 0; |
| 9689 | } |
| 9690 | quotef++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9691 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9692 | } |
| 9693 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9694 | case CVAR: /* '$' */ |
| 9695 | PARSESUB(); /* parse substitution */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9696 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9697 | case CENDVAR: /* '}' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9698 | if (varnest > 0) { |
| 9699 | varnest--; |
| 9700 | if (dqvarnest > 0) { |
| 9701 | dqvarnest--; |
| 9702 | } |
| 9703 | USTPUTC(CTLENDVAR, out); |
| 9704 | } else { |
| 9705 | USTPUTC(c, out); |
| 9706 | } |
| 9707 | break; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9708 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9709 | case CLP: /* '(' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9710 | parenlevel++; |
| 9711 | USTPUTC(c, out); |
| 9712 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9713 | case CRP: /* ')' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9714 | if (parenlevel > 0) { |
| 9715 | USTPUTC(c, out); |
| 9716 | --parenlevel; |
| 9717 | } else { |
| 9718 | if (pgetc() == ')') { |
| 9719 | if (--arinest == 0) { |
| 9720 | USTPUTC(CTLENDARI, out); |
| 9721 | syntax = prevsyntax; |
| 9722 | if (syntax == DQSYNTAX) |
| 9723 | dblquote = 1; |
| 9724 | else |
| 9725 | dblquote = 0; |
| 9726 | } else |
| 9727 | USTPUTC(')', out); |
| 9728 | } else { |
| 9729 | /* |
| 9730 | * unbalanced parens |
| 9731 | * (don't 2nd guess - no error) |
| 9732 | */ |
| 9733 | pungetc(); |
| 9734 | USTPUTC(')', out); |
| 9735 | } |
| 9736 | } |
| 9737 | break; |
| 9738 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9739 | case CBQUOTE: /* '`' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9740 | PARSEBACKQOLD(); |
| 9741 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9742 | case CENDFILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9743 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9744 | case CIGN: |
| 9745 | break; |
| 9746 | default: |
| 9747 | if (varnest == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9748 | goto endword; /* exit outer loop */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9749 | #if ENABLE_ASH_ALIAS |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 9750 | if (c != PEOA) |
| 9751 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9752 | USTPUTC(c, out); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 9753 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9754 | } |
| 9755 | c = pgetc_macro(); |
| 9756 | } |
| 9757 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9758 | endword: |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9759 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9760 | if (syntax == ARISYNTAX) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9761 | raise_error_syntax("Missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9762 | #endif |
| 9763 | if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9764 | raise_error_syntax("Unterminated quoted string"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9765 | if (varnest != 0) { |
| 9766 | startlinno = plinno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9767 | /* { */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9768 | raise_error_syntax("Missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9769 | } |
| 9770 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9771 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9772 | out = stackblock(); |
| 9773 | if (eofmark == NULL) { |
| 9774 | if ((c == '>' || c == '<') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9775 | && quotef == 0 |
| 9776 | && len <= 2 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9777 | && (*out == '\0' || isdigit(*out))) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9778 | PARSEREDIR(); |
| 9779 | return lasttoken = TREDIR; |
| 9780 | } else { |
| 9781 | pungetc(); |
| 9782 | } |
| 9783 | } |
| 9784 | quoteflag = quotef; |
| 9785 | backquotelist = bqlist; |
| 9786 | grabstackblock(len); |
| 9787 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9788 | lasttoken = TWORD; |
| 9789 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9790 | /* end of readtoken routine */ |
| 9791 | |
| 9792 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9793 | /* |
| 9794 | * Check to see whether we are at the end of the here document. When this |
| 9795 | * is called, c is set to the first character of the next input line. If |
| 9796 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 9797 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9798 | checkend: { |
| 9799 | if (eofmark) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9800 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9801 | if (c == PEOA) { |
| 9802 | c = pgetc2(); |
| 9803 | } |
| 9804 | #endif |
| 9805 | if (striptabs) { |
| 9806 | while (c == '\t') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9807 | c = pgetc2(); |
| 9808 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9809 | } |
| 9810 | if (c == *eofmark) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9811 | if (pfgets(line, sizeof(line)) != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9812 | char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9813 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9814 | p = line; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9815 | for (q = eofmark + 1; *q && *p == *q; p++, q++); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9816 | if (*p == '\n' && *q == '\0') { |
| 9817 | c = PEOF; |
| 9818 | plinno++; |
| 9819 | needprompt = doprompt; |
| 9820 | } else { |
| 9821 | pushstring(line, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9822 | } |
| 9823 | } |
| 9824 | } |
| 9825 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9826 | goto checkend_return; |
| 9827 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9828 | |
| 9829 | |
| 9830 | /* |
| 9831 | * Parse a redirection operator. The variable "out" points to a string |
| 9832 | * specifying the fd to be redirected. The variable "c" contains the |
| 9833 | * first character of the redirection operator. |
| 9834 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9835 | parseredir: { |
| 9836 | char fd = *out; |
| 9837 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9838 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9839 | np = stalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9840 | if (c == '>') { |
| 9841 | np->nfile.fd = 1; |
| 9842 | c = pgetc(); |
| 9843 | if (c == '>') |
| 9844 | np->type = NAPPEND; |
| 9845 | else if (c == '|') |
| 9846 | np->type = NCLOBBER; |
| 9847 | else if (c == '&') |
| 9848 | np->type = NTOFD; |
| 9849 | else { |
| 9850 | np->type = NTO; |
| 9851 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9852 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9853 | } else { /* c == '<' */ |
| 9854 | np->nfile.fd = 0; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9855 | c = pgetc(); |
| 9856 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9857 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9858 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9859 | np = stalloc(sizeof(struct nhere)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9860 | np->nfile.fd = 0; |
| 9861 | } |
| 9862 | np->type = NHERE; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9863 | heredoc = stalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9864 | heredoc->here = np; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9865 | c = pgetc(); |
| 9866 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9867 | heredoc->striptabs = 1; |
| 9868 | } else { |
| 9869 | heredoc->striptabs = 0; |
| 9870 | pungetc(); |
| 9871 | } |
| 9872 | break; |
| 9873 | |
| 9874 | case '&': |
| 9875 | np->type = NFROMFD; |
| 9876 | break; |
| 9877 | |
| 9878 | case '>': |
| 9879 | np->type = NFROMTO; |
| 9880 | break; |
| 9881 | |
| 9882 | default: |
| 9883 | np->type = NFROM; |
| 9884 | pungetc(); |
| 9885 | break; |
| 9886 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9887 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9888 | if (fd != '\0') |
| 9889 | np->nfile.fd = digit_val(fd); |
| 9890 | redirnode = np; |
| 9891 | goto parseredir_return; |
| 9892 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9893 | |
| 9894 | |
| 9895 | /* |
| 9896 | * Parse a substitution. At this point, we have read the dollar sign |
| 9897 | * and nothing else. |
| 9898 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9899 | parsesub: { |
| 9900 | int subtype; |
| 9901 | int typeloc; |
| 9902 | int flags; |
| 9903 | char *p; |
| 9904 | static const char types[] = "}-+?="; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9905 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9906 | c = pgetc(); |
| 9907 | if ( |
| 9908 | c <= PEOA_OR_PEOF || |
| 9909 | (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
| 9910 | ) { |
| 9911 | USTPUTC('$', out); |
| 9912 | pungetc(); |
| 9913 | } else if (c == '(') { /* $(command) or $((arith)) */ |
| 9914 | if (pgetc() == '(') { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9915 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9916 | PARSEARITH(); |
| 9917 | #else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9918 | raise_error_syntax("We unsupport $((arith))"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9919 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9920 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9921 | pungetc(); |
| 9922 | PARSEBACKQNEW(); |
| 9923 | } |
| 9924 | } else { |
| 9925 | USTPUTC(CTLVAR, out); |
| 9926 | typeloc = out - (char *)stackblock(); |
| 9927 | USTPUTC(VSNORMAL, out); |
| 9928 | subtype = VSNORMAL; |
| 9929 | if (c == '{') { |
| 9930 | c = pgetc(); |
| 9931 | if (c == '#') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9932 | c = pgetc(); |
| 9933 | if (c == '}') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9934 | c = '#'; |
| 9935 | else |
| 9936 | subtype = VSLENGTH; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9937 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9938 | subtype = 0; |
| 9939 | } |
| 9940 | if (c > PEOA_OR_PEOF && is_name(c)) { |
| 9941 | do { |
| 9942 | STPUTC(c, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9943 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9944 | } while (c > PEOA_OR_PEOF && is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9945 | } else if (isdigit(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9946 | do { |
| 9947 | STPUTC(c, out); |
| 9948 | c = pgetc(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9949 | } while (isdigit(c)); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9950 | } else if (is_special(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9951 | USTPUTC(c, out); |
| 9952 | c = pgetc(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9953 | } else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9954 | badsub: raise_error_syntax("Bad substitution"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9955 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9956 | STPUTC('=', out); |
| 9957 | flags = 0; |
| 9958 | if (subtype == 0) { |
| 9959 | switch (c) { |
| 9960 | case ':': |
| 9961 | flags = VSNUL; |
| 9962 | c = pgetc(); |
| 9963 | /*FALLTHROUGH*/ |
| 9964 | default: |
| 9965 | p = strchr(types, c); |
| 9966 | if (p == NULL) |
| 9967 | goto badsub; |
| 9968 | subtype = p - types + VSNORMAL; |
| 9969 | break; |
| 9970 | case '%': |
| 9971 | case '#': |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9972 | { |
| 9973 | int cc = c; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9974 | subtype = c == '#' ? VSTRIMLEFT : |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9975 | VSTRIMRIGHT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9976 | c = pgetc(); |
| 9977 | if (c == cc) |
| 9978 | subtype++; |
| 9979 | else |
| 9980 | pungetc(); |
| 9981 | break; |
| 9982 | } |
| 9983 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9984 | } else { |
| 9985 | pungetc(); |
| 9986 | } |
| 9987 | if (dblquote || arinest) |
| 9988 | flags |= VSQUOTE; |
| 9989 | *((char *)stackblock() + typeloc) = subtype | flags; |
| 9990 | if (subtype != VSNORMAL) { |
| 9991 | varnest++; |
| 9992 | if (dblquote || arinest) { |
| 9993 | dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9994 | } |
| 9995 | } |
| 9996 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9997 | goto parsesub_return; |
| 9998 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9999 | |
| 10000 | |
| 10001 | /* |
| 10002 | * Called to parse command substitutions. Newstyle is set if the command |
| 10003 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 10004 | * list of commands (passed by reference), and savelen is the number of |
| 10005 | * characters on the top of the stack which must be preserved. |
| 10006 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10007 | parsebackq: { |
| 10008 | struct nodelist **nlpp; |
| 10009 | int savepbq; |
| 10010 | union node *n; |
| 10011 | char *volatile str; |
| 10012 | struct jmploc jmploc; |
| 10013 | struct jmploc *volatile savehandler; |
| 10014 | size_t savelen; |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 10015 | int saveprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10016 | #ifdef __GNUC__ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10017 | (void) &saveprompt; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10018 | #endif |
| 10019 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10020 | savepbq = parsebackquote; |
| 10021 | if (setjmp(jmploc.loc)) { |
| 10022 | if (str) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10023 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10024 | parsebackquote = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10025 | exception_handler = savehandler; |
| 10026 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10027 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10028 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10029 | str = NULL; |
| 10030 | savelen = out - (char *)stackblock(); |
| 10031 | if (savelen > 0) { |
| 10032 | str = ckmalloc(savelen); |
| 10033 | memcpy(str, stackblock(), savelen); |
| 10034 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10035 | savehandler = exception_handler; |
| 10036 | exception_handler = &jmploc; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10037 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10038 | if (oldstyle) { |
| 10039 | /* We must read until the closing backquote, giving special |
| 10040 | treatment to some slashes, and then push the string and |
| 10041 | reread it as input, interpreting it normally. */ |
| 10042 | char *pout; |
| 10043 | int pc; |
| 10044 | size_t psavelen; |
| 10045 | char *pstr; |
| 10046 | |
| 10047 | |
| 10048 | STARTSTACKSTR(pout); |
| 10049 | for (;;) { |
| 10050 | if (needprompt) { |
| 10051 | setprompt(2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10052 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10053 | pc = pgetc(); |
| 10054 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10055 | case '`': |
| 10056 | goto done; |
| 10057 | |
| 10058 | case '\\': |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10059 | pc = pgetc(); |
| 10060 | if (pc == '\n') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10061 | plinno++; |
| 10062 | if (doprompt) |
| 10063 | setprompt(2); |
| 10064 | /* |
| 10065 | * If eating a newline, avoid putting |
| 10066 | * the newline into the new character |
| 10067 | * stream (via the STPUTC after the |
| 10068 | * switch). |
| 10069 | */ |
| 10070 | continue; |
| 10071 | } |
| 10072 | if (pc != '\\' && pc != '`' && pc != '$' |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10073 | && (!dblquote || pc != '"')) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10074 | STPUTC('\\', pout); |
| 10075 | if (pc > PEOA_OR_PEOF) { |
| 10076 | break; |
| 10077 | } |
| 10078 | /* fall through */ |
| 10079 | |
| 10080 | case PEOF: |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10081 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10082 | case PEOA: |
| 10083 | #endif |
| 10084 | startlinno = plinno; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10085 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10086 | |
| 10087 | case '\n': |
| 10088 | plinno++; |
| 10089 | needprompt = doprompt; |
| 10090 | break; |
| 10091 | |
| 10092 | default: |
| 10093 | break; |
| 10094 | } |
| 10095 | STPUTC(pc, pout); |
| 10096 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10097 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10098 | STPUTC('\0', pout); |
| 10099 | psavelen = pout - (char *)stackblock(); |
| 10100 | if (psavelen > 0) { |
| 10101 | pstr = grabstackstr(pout); |
| 10102 | setinputstring(pstr); |
| 10103 | } |
| 10104 | } |
| 10105 | nlpp = &bqlist; |
| 10106 | while (*nlpp) |
| 10107 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10108 | *nlpp = stalloc(sizeof(**nlpp)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10109 | (*nlpp)->next = NULL; |
| 10110 | parsebackquote = oldstyle; |
| 10111 | |
| 10112 | if (oldstyle) { |
| 10113 | saveprompt = doprompt; |
| 10114 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10115 | } |
| 10116 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10117 | n = list(2); |
| 10118 | |
| 10119 | if (oldstyle) |
| 10120 | doprompt = saveprompt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10121 | else if (readtoken() != TRP) |
| 10122 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10123 | |
| 10124 | (*nlpp)->n = n; |
| 10125 | if (oldstyle) { |
| 10126 | /* |
| 10127 | * Start reading from old file again, ignoring any pushed back |
| 10128 | * tokens left from the backquote parsing |
| 10129 | */ |
| 10130 | popfile(); |
| 10131 | tokpushback = 0; |
| 10132 | } |
| 10133 | while (stackblocksize() <= savelen) |
| 10134 | growstackblock(); |
| 10135 | STARTSTACKSTR(out); |
| 10136 | if (str) { |
| 10137 | memcpy(out, str, savelen); |
| 10138 | STADJUST(savelen, out); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10139 | INT_OFF; |
| 10140 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10141 | str = NULL; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10142 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10143 | } |
| 10144 | parsebackquote = savepbq; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10145 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10146 | if (arinest || dblquote) |
| 10147 | USTPUTC(CTLBACKQ | CTLQUOTE, out); |
| 10148 | else |
| 10149 | USTPUTC(CTLBACKQ, out); |
| 10150 | if (oldstyle) |
| 10151 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10152 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10153 | } |
| 10154 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10155 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10156 | /* |
| 10157 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 10158 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10159 | parsearith: { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10160 | if (++arinest == 1) { |
| 10161 | prevsyntax = syntax; |
| 10162 | syntax = ARISYNTAX; |
| 10163 | USTPUTC(CTLARI, out); |
| 10164 | if (dblquote) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10165 | USTPUTC('"', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10166 | else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10167 | USTPUTC(' ', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10168 | } else { |
| 10169 | /* |
| 10170 | * we collapse embedded arithmetic expansion to |
| 10171 | * parenthesis, which should be equivalent |
| 10172 | */ |
| 10173 | USTPUTC('(', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10174 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10175 | goto parsearith_return; |
| 10176 | } |
| 10177 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10178 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10179 | } /* end of readtoken */ |
| 10180 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10181 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10182 | * Read the next input token. |
| 10183 | * If the token is a word, we set backquotelist to the list of cmds in |
| 10184 | * backquotes. We set quoteflag to true if any part of the word was |
| 10185 | * quoted. |
| 10186 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 10187 | * the redirection. |
| 10188 | * In all cases, the variable startlinno is set to the number of the line |
| 10189 | * on which the token starts. |
| 10190 | * |
| 10191 | * [Change comment: here documents and internal procedures] |
| 10192 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 10193 | * word parsing code into a separate routine. In this case, readtoken |
| 10194 | * doesn't need to have any internal procedures, but parseword does. |
| 10195 | * We could also make parseoperator in essence the main routine, and |
| 10196 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10197 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10198 | #define NEW_xxreadtoken |
| 10199 | #ifdef NEW_xxreadtoken |
| 10200 | /* singles must be first! */ |
| 10201 | static const char xxreadtoken_chars[7] = { '\n', '(', ')', '&', '|', ';', 0 }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10202 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10203 | static const char xxreadtoken_tokens[] = { |
| 10204 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 10205 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 10206 | TEOF, /* corresponds to trailing nul */ |
| 10207 | TAND, TOR, TENDCASE, /* if double occurrence */ |
| 10208 | }; |
| 10209 | |
| 10210 | #define xxreadtoken_doubles \ |
| 10211 | (sizeof(xxreadtoken_tokens) - sizeof(xxreadtoken_chars)) |
| 10212 | #define xxreadtoken_singles \ |
| 10213 | (sizeof(xxreadtoken_chars) - xxreadtoken_doubles - 1) |
| 10214 | |
| 10215 | static int |
| 10216 | xxreadtoken(void) |
| 10217 | { |
| 10218 | int c; |
| 10219 | |
| 10220 | if (tokpushback) { |
| 10221 | tokpushback = 0; |
| 10222 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10223 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10224 | if (needprompt) { |
| 10225 | setprompt(2); |
| 10226 | } |
| 10227 | startlinno = plinno; |
| 10228 | for (;;) { /* until token or start of word found */ |
| 10229 | c = pgetc_macro(); |
| 10230 | |
| 10231 | if ((c != ' ') && (c != '\t') |
| 10232 | #if ENABLE_ASH_ALIAS |
| 10233 | && (c != PEOA) |
| 10234 | #endif |
| 10235 | ) { |
| 10236 | if (c == '#') { |
| 10237 | while ((c = pgetc()) != '\n' && c != PEOF); |
| 10238 | pungetc(); |
| 10239 | } else if (c == '\\') { |
| 10240 | if (pgetc() != '\n') { |
| 10241 | pungetc(); |
| 10242 | goto READTOKEN1; |
| 10243 | } |
| 10244 | startlinno = ++plinno; |
| 10245 | if (doprompt) |
| 10246 | setprompt(2); |
| 10247 | } else { |
| 10248 | const char *p |
| 10249 | = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 10250 | |
| 10251 | if (c != PEOF) { |
| 10252 | if (c == '\n') { |
| 10253 | plinno++; |
| 10254 | needprompt = doprompt; |
| 10255 | } |
| 10256 | |
| 10257 | p = strchr(xxreadtoken_chars, c); |
| 10258 | if (p == NULL) { |
| 10259 | READTOKEN1: |
| 10260 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
| 10261 | } |
| 10262 | |
| 10263 | if (p - xxreadtoken_chars >= xxreadtoken_singles) { |
| 10264 | if (pgetc() == *p) { /* double occurrence? */ |
| 10265 | p += xxreadtoken_doubles + 1; |
| 10266 | } else { |
| 10267 | pungetc(); |
| 10268 | } |
| 10269 | } |
| 10270 | } |
| 10271 | return lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 10272 | } |
| 10273 | } |
| 10274 | } /* for */ |
| 10275 | } |
| 10276 | #else |
| 10277 | #define RETURN(token) return lasttoken = token |
| 10278 | static int |
| 10279 | xxreadtoken(void) |
| 10280 | { |
| 10281 | int c; |
| 10282 | |
| 10283 | if (tokpushback) { |
| 10284 | tokpushback = 0; |
| 10285 | return lasttoken; |
| 10286 | } |
| 10287 | if (needprompt) { |
| 10288 | setprompt(2); |
| 10289 | } |
| 10290 | startlinno = plinno; |
| 10291 | for (;;) { /* until token or start of word found */ |
| 10292 | c = pgetc_macro(); |
| 10293 | switch (c) { |
| 10294 | case ' ': case '\t': |
| 10295 | #if ENABLE_ASH_ALIAS |
| 10296 | case PEOA: |
| 10297 | #endif |
| 10298 | continue; |
| 10299 | case '#': |
| 10300 | while ((c = pgetc()) != '\n' && c != PEOF); |
| 10301 | pungetc(); |
| 10302 | continue; |
| 10303 | case '\\': |
| 10304 | if (pgetc() == '\n') { |
| 10305 | startlinno = ++plinno; |
| 10306 | if (doprompt) |
| 10307 | setprompt(2); |
| 10308 | continue; |
| 10309 | } |
| 10310 | pungetc(); |
| 10311 | goto breakloop; |
| 10312 | case '\n': |
| 10313 | plinno++; |
| 10314 | needprompt = doprompt; |
| 10315 | RETURN(TNL); |
| 10316 | case PEOF: |
| 10317 | RETURN(TEOF); |
| 10318 | case '&': |
| 10319 | if (pgetc() == '&') |
| 10320 | RETURN(TAND); |
| 10321 | pungetc(); |
| 10322 | RETURN(TBACKGND); |
| 10323 | case '|': |
| 10324 | if (pgetc() == '|') |
| 10325 | RETURN(TOR); |
| 10326 | pungetc(); |
| 10327 | RETURN(TPIPE); |
| 10328 | case ';': |
| 10329 | if (pgetc() == ';') |
| 10330 | RETURN(TENDCASE); |
| 10331 | pungetc(); |
| 10332 | RETURN(TSEMI); |
| 10333 | case '(': |
| 10334 | RETURN(TLP); |
| 10335 | case ')': |
| 10336 | RETURN(TRP); |
| 10337 | default: |
| 10338 | goto breakloop; |
| 10339 | } |
| 10340 | } |
| 10341 | breakloop: |
| 10342 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 10343 | #undef RETURN |
| 10344 | } |
| 10345 | #endif /* NEW_xxreadtoken */ |
| 10346 | |
| 10347 | static int |
| 10348 | readtoken(void) |
| 10349 | { |
| 10350 | int t; |
| 10351 | #if DEBUG |
| 10352 | int alreadyseen = tokpushback; |
| 10353 | #endif |
| 10354 | |
| 10355 | #if ENABLE_ASH_ALIAS |
| 10356 | top: |
| 10357 | #endif |
| 10358 | |
| 10359 | t = xxreadtoken(); |
| 10360 | |
| 10361 | /* |
| 10362 | * eat newlines |
| 10363 | */ |
| 10364 | if (checkkwd & CHKNL) { |
| 10365 | while (t == TNL) { |
| 10366 | parseheredoc(); |
| 10367 | t = xxreadtoken(); |
| 10368 | } |
| 10369 | } |
| 10370 | |
| 10371 | if (t != TWORD || quoteflag) { |
| 10372 | goto out; |
| 10373 | } |
| 10374 | |
| 10375 | /* |
| 10376 | * check for keywords |
| 10377 | */ |
| 10378 | if (checkkwd & CHKKWD) { |
| 10379 | const char *const *pp; |
| 10380 | |
| 10381 | pp = findkwd(wordtext); |
| 10382 | if (pp) { |
| 10383 | lasttoken = t = pp - tokname_array; |
| 10384 | TRACE(("keyword %s recognized\n", tokname(t))); |
| 10385 | goto out; |
| 10386 | } |
| 10387 | } |
| 10388 | |
| 10389 | if (checkkwd & CHKALIAS) { |
| 10390 | #if ENABLE_ASH_ALIAS |
| 10391 | struct alias *ap; |
| 10392 | ap = lookupalias(wordtext, 1); |
| 10393 | if (ap != NULL) { |
| 10394 | if (*ap->val) { |
| 10395 | pushstring(ap->val, ap); |
| 10396 | } |
| 10397 | goto top; |
| 10398 | } |
| 10399 | #endif |
| 10400 | } |
| 10401 | out: |
| 10402 | checkkwd = 0; |
| 10403 | #if DEBUG |
| 10404 | if (!alreadyseen) |
| 10405 | TRACE(("token %s %s\n", tokname(t), t == TWORD ? wordtext : "")); |
| 10406 | else |
| 10407 | TRACE(("reread token %s %s\n", tokname(t), t == TWORD ? wordtext : "")); |
| 10408 | #endif |
| 10409 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10410 | } |
| 10411 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10412 | static char |
| 10413 | peektoken(void) |
| 10414 | { |
| 10415 | int t; |
| 10416 | |
| 10417 | t = readtoken(); |
| 10418 | tokpushback++; |
| 10419 | return tokname_array[t][0]; |
| 10420 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10421 | |
| 10422 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10423 | * Read and parse a command. Returns NEOF on end of file. (NULL is a |
| 10424 | * valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10425 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10426 | static union node * |
| 10427 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 10428 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10429 | int t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10430 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10431 | tokpushback = 0; |
| 10432 | doprompt = interact; |
| 10433 | if (doprompt) |
| 10434 | setprompt(doprompt); |
| 10435 | needprompt = 0; |
| 10436 | t = readtoken(); |
| 10437 | if (t == TEOF) |
| 10438 | return NEOF; |
| 10439 | if (t == TNL) |
| 10440 | return NULL; |
| 10441 | tokpushback++; |
| 10442 | return list(1); |
| 10443 | } |
| 10444 | |
| 10445 | /* |
| 10446 | * Input any here documents. |
| 10447 | */ |
| 10448 | static void |
| 10449 | parseheredoc(void) |
| 10450 | { |
| 10451 | struct heredoc *here; |
| 10452 | union node *n; |
| 10453 | |
| 10454 | here = heredoclist; |
| 10455 | heredoclist = 0; |
| 10456 | |
| 10457 | while (here) { |
| 10458 | if (needprompt) { |
| 10459 | setprompt(2); |
| 10460 | } |
| 10461 | readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, |
| 10462 | here->eofmark, here->striptabs); |
| 10463 | n = stalloc(sizeof(struct narg)); |
| 10464 | n->narg.type = NARG; |
| 10465 | n->narg.next = NULL; |
| 10466 | n->narg.text = wordtext; |
| 10467 | n->narg.backquote = backquotelist; |
| 10468 | here->here->nhere.doc = n; |
| 10469 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10470 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10471 | } |
| 10472 | |
| 10473 | |
| 10474 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10475 | * called by editline -- any expansions to the prompt |
| 10476 | * should be added here. |
| 10477 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10478 | #if ENABLE_ASH_EXPAND_PRMT |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 10479 | static const char * |
| 10480 | expandstr(const char *ps) |
| 10481 | { |
| 10482 | union node n; |
| 10483 | |
| 10484 | /* XXX Fix (char *) cast. */ |
| 10485 | setinputstring((char *)ps); |
| 10486 | readtoken1(pgetc(), DQSYNTAX, nullstr, 0); |
| 10487 | popfile(); |
| 10488 | |
| 10489 | n.narg.type = NARG; |
| 10490 | n.narg.next = NULL; |
| 10491 | n.narg.text = wordtext; |
| 10492 | n.narg.backquote = backquotelist; |
| 10493 | |
| 10494 | expandarg(&n, NULL, 0); |
| 10495 | return stackblock(); |
| 10496 | } |
| 10497 | #endif |
| 10498 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10499 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 10500 | /* |
| 10501 | * Execute a command or commands contained in a string. |
| 10502 | */ |
| 10503 | static int |
| 10504 | evalstring(char *s, int mask) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10505 | { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 10506 | union node *n; |
| 10507 | struct stackmark smark; |
| 10508 | int skip; |
| 10509 | |
| 10510 | setinputstring(s); |
| 10511 | setstackmark(&smark); |
| 10512 | |
| 10513 | skip = 0; |
| 10514 | while ((n = parsecmd(0)) != NEOF) { |
| 10515 | evaltree(n, 0); |
| 10516 | popstackmark(&smark); |
| 10517 | skip = evalskip; |
| 10518 | if (skip) |
| 10519 | break; |
| 10520 | } |
| 10521 | popfile(); |
| 10522 | |
| 10523 | skip &= mask; |
| 10524 | evalskip = skip; |
| 10525 | return skip; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10526 | } |
| 10527 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 10528 | /* |
| 10529 | * The eval command. |
| 10530 | */ |
| 10531 | static int |
| 10532 | evalcmd(int argc, char **argv) |
| 10533 | { |
| 10534 | char *p; |
| 10535 | char *concat; |
| 10536 | char **ap; |
| 10537 | |
| 10538 | if (argc > 1) { |
| 10539 | p = argv[1]; |
| 10540 | if (argc > 2) { |
| 10541 | STARTSTACKSTR(concat); |
| 10542 | ap = argv + 2; |
| 10543 | for (;;) { |
| 10544 | concat = stack_putstr(p, concat); |
| 10545 | p = *ap++; |
| 10546 | if (p == NULL) |
| 10547 | break; |
| 10548 | STPUTC(' ', concat); |
| 10549 | } |
| 10550 | STPUTC('\0', concat); |
| 10551 | p = grabstackstr(concat); |
| 10552 | } |
| 10553 | evalstring(p, ~SKIPEVAL); |
| 10554 | |
| 10555 | } |
| 10556 | return exitstatus; |
| 10557 | } |
| 10558 | |
| 10559 | /* |
| 10560 | * Read and execute commands. "Top" is nonzero for the top level command |
| 10561 | * loop; it turns on prompting if the shell is interactive. |
| 10562 | */ |
| 10563 | static int |
| 10564 | cmdloop(int top) |
| 10565 | { |
| 10566 | union node *n; |
| 10567 | struct stackmark smark; |
| 10568 | int inter; |
| 10569 | int numeof = 0; |
| 10570 | |
| 10571 | TRACE(("cmdloop(%d) called\n", top)); |
| 10572 | for (;;) { |
| 10573 | int skip; |
| 10574 | |
| 10575 | setstackmark(&smark); |
| 10576 | #if JOBS |
| 10577 | if (jobctl) |
| 10578 | showjobs(stderr, SHOW_CHANGED); |
| 10579 | #endif |
| 10580 | inter = 0; |
| 10581 | if (iflag && top) { |
| 10582 | inter++; |
| 10583 | #if ENABLE_ASH_MAIL |
| 10584 | chkmail(); |
| 10585 | #endif |
| 10586 | } |
| 10587 | n = parsecmd(inter); |
| 10588 | /* showtree(n); DEBUG */ |
| 10589 | if (n == NEOF) { |
| 10590 | if (!top || numeof >= 50) |
| 10591 | break; |
| 10592 | if (!stoppedjobs()) { |
| 10593 | if (!Iflag) |
| 10594 | break; |
| 10595 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 10596 | } |
| 10597 | numeof++; |
| 10598 | } else if (nflag == 0) { |
| 10599 | job_warning = (job_warning == 2) ? 1 : 0; |
| 10600 | numeof = 0; |
| 10601 | evaltree(n, 0); |
| 10602 | } |
| 10603 | popstackmark(&smark); |
| 10604 | skip = evalskip; |
| 10605 | |
| 10606 | if (skip) { |
| 10607 | evalskip = 0; |
| 10608 | return skip & SKIPEVAL; |
| 10609 | } |
| 10610 | } |
| 10611 | return 0; |
| 10612 | } |
| 10613 | |
| 10614 | static int |
| 10615 | dotcmd(int argc, char **argv) |
| 10616 | { |
| 10617 | struct strlist *sp; |
| 10618 | volatile struct shparam saveparam; |
| 10619 | int status = 0; |
| 10620 | |
| 10621 | for (sp = cmdenviron; sp; sp = sp->next) |
| 10622 | setvareq(xstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
| 10623 | |
| 10624 | if (argc >= 2) { /* That's what SVR2 does */ |
| 10625 | char *fullname; |
| 10626 | |
| 10627 | fullname = find_dot_file(argv[1]); |
| 10628 | |
| 10629 | if (argc > 2) { |
| 10630 | saveparam = shellparam; |
| 10631 | shellparam.malloc = 0; |
| 10632 | shellparam.nparam = argc - 2; |
| 10633 | shellparam.p = argv + 2; |
| 10634 | }; |
| 10635 | |
| 10636 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 10637 | commandname = fullname; |
| 10638 | cmdloop(0); |
| 10639 | popfile(); |
| 10640 | |
| 10641 | if (argc > 2) { |
| 10642 | freeparam(&shellparam); |
| 10643 | shellparam = saveparam; |
| 10644 | }; |
| 10645 | status = exitstatus; |
| 10646 | } |
| 10647 | return status; |
| 10648 | } |
| 10649 | |
| 10650 | static int |
| 10651 | exitcmd(int argc, char **argv) |
| 10652 | { |
| 10653 | if (stoppedjobs()) |
| 10654 | return 0; |
| 10655 | if (argc > 1) |
| 10656 | exitstatus = number(argv[1]); |
| 10657 | raise_exception(EXEXIT); |
| 10658 | /* NOTREACHED */ |
| 10659 | } |
| 10660 | |
| 10661 | #if ENABLE_ASH_BUILTIN_ECHO |
| 10662 | static int |
| 10663 | echocmd(int argc, char **argv) |
| 10664 | { |
| 10665 | return bb_echo(argv); |
| 10666 | } |
| 10667 | #endif |
| 10668 | |
| 10669 | #if ENABLE_ASH_BUILTIN_TEST |
| 10670 | static int |
| 10671 | testcmd(int argc, char **argv) |
| 10672 | { |
| 10673 | return bb_test(argc, argv); |
| 10674 | } |
| 10675 | #endif |
| 10676 | |
| 10677 | /* |
| 10678 | * Read a file containing shell functions. |
| 10679 | */ |
| 10680 | static void |
| 10681 | readcmdfile(char *name) |
| 10682 | { |
| 10683 | setinputfile(name, INPUT_PUSH_FILE); |
| 10684 | cmdloop(0); |
| 10685 | popfile(); |
| 10686 | } |
| 10687 | |
| 10688 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10689 | /* redir.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10690 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10691 | /* |
| 10692 | * Code for dealing with input/output redirection. |
| 10693 | */ |
| 10694 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10695 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10696 | #ifndef PIPE_BUF |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10697 | # define PIPESIZE 4096 /* amount of buffering in a pipe */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10698 | #else |
| 10699 | # define PIPESIZE PIPE_BUF |
| 10700 | #endif |
| 10701 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10702 | /* |
| 10703 | * Open a file in noclobber mode. |
| 10704 | * The code was copied from bash. |
| 10705 | */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 10706 | static int |
| 10707 | noclobberopen(const char *fname) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10708 | { |
| 10709 | int r, fd; |
| 10710 | struct stat finfo, finfo2; |
| 10711 | |
| 10712 | /* |
| 10713 | * If the file exists and is a regular file, return an error |
| 10714 | * immediately. |
| 10715 | */ |
| 10716 | r = stat(fname, &finfo); |
| 10717 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 10718 | errno = EEXIST; |
| 10719 | return -1; |
| 10720 | } |
| 10721 | |
| 10722 | /* |
| 10723 | * If the file was not present (r != 0), make sure we open it |
| 10724 | * exclusively so that if it is created before we open it, our open |
| 10725 | * will fail. Make sure that we do not truncate an existing file. |
| 10726 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 10727 | * file was not a regular file, we leave O_EXCL off. |
| 10728 | */ |
| 10729 | if (r != 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10730 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 10731 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10732 | |
| 10733 | /* If the open failed, return the file descriptor right away. */ |
| 10734 | if (fd < 0) |
| 10735 | return fd; |
| 10736 | |
| 10737 | /* |
| 10738 | * OK, the open succeeded, but the file may have been changed from a |
| 10739 | * non-regular file to a regular file between the stat and the open. |
| 10740 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 10741 | * did not exist and is symlinked to an existing file between the stat |
| 10742 | * and open. |
| 10743 | */ |
| 10744 | |
| 10745 | /* |
| 10746 | * If we can open it and fstat the file descriptor, and neither check |
| 10747 | * revealed that it was a regular file, and the file has not been |
| 10748 | * replaced, return the file descriptor. |
| 10749 | */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10750 | if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode) |
| 10751 | && finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10752 | return fd; |
| 10753 | |
| 10754 | /* The file has been replaced. badness. */ |
| 10755 | close(fd); |
| 10756 | errno = EEXIST; |
| 10757 | return -1; |
| 10758 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10759 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10760 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10761 | /* |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10762 | * Handle here documents. Normally we fork off a process to write the |
| 10763 | * data to a pipe. If the document is short, we can stuff the data in |
| 10764 | * the pipe without forking. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10765 | */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 10766 | static int |
| 10767 | openhere(union node *redir) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10768 | { |
| 10769 | int pip[2]; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10770 | size_t len = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10771 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10772 | if (pipe(pip) < 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10773 | ash_msg_and_raise_error("Pipe call failed"); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10774 | if (redir->type == NHERE) { |
| 10775 | len = strlen(redir->nhere.doc->narg.text); |
| 10776 | if (len <= PIPESIZE) { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 10777 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10778 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10779 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10780 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10781 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10782 | close(pip[0]); |
| 10783 | signal(SIGINT, SIG_IGN); |
| 10784 | signal(SIGQUIT, SIG_IGN); |
| 10785 | signal(SIGHUP, SIG_IGN); |
| 10786 | #ifdef SIGTSTP |
| 10787 | signal(SIGTSTP, SIG_IGN); |
| 10788 | #endif |
| 10789 | signal(SIGPIPE, SIG_DFL); |
| 10790 | if (redir->type == NHERE) |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 10791 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10792 | else |
| 10793 | expandhere(redir->nhere.doc, pip[1]); |
| 10794 | _exit(0); |
| 10795 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10796 | out: |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10797 | close(pip[1]); |
| 10798 | return pip[0]; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10799 | } |
| 10800 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10801 | static int |
| 10802 | openredirect(union node *redir) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10803 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10804 | char *fname; |
| 10805 | int f; |
| 10806 | |
| 10807 | switch (redir->nfile.type) { |
| 10808 | case NFROM: |
| 10809 | fname = redir->nfile.expfname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10810 | f = open(fname, O_RDONLY); |
| 10811 | if (f < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10812 | goto eopen; |
| 10813 | break; |
| 10814 | case NFROMTO: |
| 10815 | fname = redir->nfile.expfname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10816 | f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666); |
| 10817 | if (f < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10818 | goto ecreate; |
| 10819 | break; |
| 10820 | case NTO: |
| 10821 | /* Take care of noclobber mode. */ |
| 10822 | if (Cflag) { |
| 10823 | fname = redir->nfile.expfname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10824 | f = noclobberopen(fname); |
| 10825 | if (f < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10826 | goto ecreate; |
| 10827 | break; |
| 10828 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10829 | /* FALLTHROUGH */ |
| 10830 | case NCLOBBER: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10831 | fname = redir->nfile.expfname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10832 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 10833 | if (f < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10834 | goto ecreate; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10835 | break; |
| 10836 | case NAPPEND: |
| 10837 | fname = redir->nfile.expfname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10838 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 10839 | if (f < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10840 | goto ecreate; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10841 | break; |
| 10842 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 10843 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10844 | abort(); |
| 10845 | #endif |
| 10846 | /* Fall through to eliminate warning. */ |
| 10847 | case NTOFD: |
| 10848 | case NFROMFD: |
| 10849 | f = -1; |
| 10850 | break; |
| 10851 | case NHERE: |
| 10852 | case NXHERE: |
| 10853 | f = openhere(redir); |
| 10854 | break; |
| 10855 | } |
| 10856 | |
| 10857 | return f; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10858 | ecreate: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10859 | ash_msg_and_raise_error("cannot create %s: %s", fname, errmsg(errno, "Directory nonexistent")); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10860 | eopen: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10861 | ash_msg_and_raise_error("cannot open %s: %s", fname, errmsg(errno, "No such file")); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10862 | } |
| 10863 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 10864 | static void |
| 10865 | dupredirect(union node *redir, int f) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10866 | { |
| 10867 | int fd = redir->nfile.fd; |
| 10868 | |
| 10869 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
| 10870 | if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10871 | copyfd(redir->ndup.dupfd, fd); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10872 | } |
| 10873 | return; |
| 10874 | } |
| 10875 | |
| 10876 | if (f != fd) { |
| 10877 | copyfd(f, fd); |
| 10878 | close(f); |
| 10879 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10880 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10881 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10882 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10883 | /* |
| 10884 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 10885 | * old file descriptors are stashed away so that the redirection can be |
| 10886 | * undone by calling popredir. If the REDIR_BACKQ flag is set, then the |
| 10887 | * standard output, and the standard error if it becomes a duplicate of |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10888 | * stdout, is saved in memory. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10889 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10890 | static void |
| 10891 | redirect(union node *redir, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10892 | { |
| 10893 | union node *n; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10894 | struct redirtab *sv; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10895 | int i; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10896 | int fd; |
| 10897 | int newfd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10898 | int *p; |
| 10899 | nullredirs++; |
| 10900 | if (!redir) { |
| 10901 | return; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10902 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10903 | sv = NULL; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10904 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10905 | if (flags & REDIR_PUSH) { |
| 10906 | struct redirtab *q; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10907 | q = ckmalloc(sizeof(struct redirtab)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10908 | q->next = redirlist; |
| 10909 | redirlist = q; |
| 10910 | q->nullredirs = nullredirs - 1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10911 | for (i = 0; i < 10; i++) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10912 | q->renamed[i] = EMPTY; |
| 10913 | nullredirs = 0; |
| 10914 | sv = q; |
| 10915 | } |
| 10916 | n = redir; |
| 10917 | do { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10918 | fd = n->nfile.fd; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10919 | if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) |
| 10920 | && n->ndup.dupfd == fd) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10921 | continue; /* redirect from/to same file descriptor */ |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10922 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10923 | newfd = openredirect(n); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10924 | if (fd == newfd) |
| 10925 | continue; |
| 10926 | if (sv && *(p = &sv->renamed[fd]) == EMPTY) { |
| 10927 | i = fcntl(fd, F_DUPFD, 10); |
| 10928 | |
| 10929 | if (i == -1) { |
| 10930 | i = errno; |
| 10931 | if (i != EBADF) { |
| 10932 | close(newfd); |
| 10933 | errno = i; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10934 | ash_msg_and_raise_error("%d: %m", fd); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10935 | /* NOTREACHED */ |
| 10936 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10937 | } else { |
| 10938 | *p = i; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10939 | close(fd); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10940 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10941 | } else { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10942 | close(fd); |
| 10943 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10944 | dupredirect(n, newfd); |
| 10945 | } while ((n = n->nfile.next)); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10946 | INT_ON; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 10947 | if (flags & REDIR_SAVEFD2 && sv && sv->renamed[2] >= 0) |
| 10948 | preverrout_fd = sv->renamed[2]; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10949 | } |
| 10950 | |
| 10951 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10952 | /* |
| 10953 | * Undo the effects of the last redirection. |
| 10954 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10955 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10956 | popredir(int drop) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10957 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10958 | struct redirtab *rp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10959 | int i; |
| 10960 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10961 | if (--nullredirs >= 0) |
| 10962 | return; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10963 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10964 | rp = redirlist; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10965 | for (i = 0; i < 10; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10966 | if (rp->renamed[i] != EMPTY) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10967 | if (!drop) { |
| 10968 | close(i); |
| 10969 | copyfd(rp->renamed[i], i); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10970 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10971 | close(rp->renamed[i]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10972 | } |
| 10973 | } |
| 10974 | redirlist = rp->next; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10975 | nullredirs = rp->nullredirs; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10976 | free(rp); |
| 10977 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10978 | } |
| 10979 | |
| 10980 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10981 | * Undo all redirections. Called on error or interrupt. |
| 10982 | */ |
| 10983 | |
| 10984 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10985 | * Discard all saved file descriptors. |
| 10986 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10987 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10988 | clearredir(int drop) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10989 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10990 | for (;;) { |
| 10991 | nullredirs = 0; |
| 10992 | if (!redirlist) |
| 10993 | break; |
| 10994 | popredir(drop); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10995 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10996 | } |
| 10997 | |
| 10998 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10999 | /* |
| 11000 | * Copy a file descriptor to be >= to. Returns -1 |
| 11001 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 11002 | * file descriptors left. |
| 11003 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11004 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11005 | copyfd(int from, int to) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11006 | { |
| 11007 | int newfd; |
| 11008 | |
| 11009 | newfd = fcntl(from, F_DUPFD, to); |
| 11010 | if (newfd < 0) { |
| 11011 | if (errno == EMFILE) |
| 11012 | return EMPTY; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11013 | ash_msg_and_raise_error("%d: %m", from); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11014 | } |
| 11015 | return newfd; |
| 11016 | } |
| 11017 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11018 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11019 | redirectsafe(union node *redir, int flags) |
| 11020 | { |
| 11021 | int err; |
| 11022 | volatile int saveint; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11023 | struct jmploc *volatile savehandler = exception_handler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11024 | struct jmploc jmploc; |
| 11025 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11026 | SAVE_INT(saveint); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11027 | err = setjmp(jmploc.loc) * 2; |
| 11028 | if (!err) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11029 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11030 | redirect(redir, flags); |
| 11031 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11032 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11033 | if (err && exception != EXERROR) |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11034 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11035 | RESTORE_INT(saveint); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11036 | return err; |
| 11037 | } |
| 11038 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11039 | /* show.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11040 | |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 11041 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11042 | static void shtree(union node *, int, char *, FILE*); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11043 | static void shcmd(union node *, FILE *); |
| 11044 | static void sharg(union node *, FILE *); |
| 11045 | static void indent(int, char *, FILE *); |
| 11046 | static void trstring(char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11047 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11048 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11049 | showtree(union node *n) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11050 | { |
| 11051 | trputs("showtree called\n"); |
| 11052 | shtree(n, 1, NULL, stdout); |
| 11053 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11054 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11055 | static void |
| 11056 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11057 | { |
| 11058 | struct nodelist *lp; |
| 11059 | const char *s; |
| 11060 | |
| 11061 | if (n == NULL) |
| 11062 | return; |
| 11063 | |
| 11064 | indent(ind, pfx, fp); |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 11065 | switch (n->type) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11066 | case NSEMI: |
| 11067 | s = "; "; |
| 11068 | goto binop; |
| 11069 | case NAND: |
| 11070 | s = " && "; |
| 11071 | goto binop; |
| 11072 | case NOR: |
| 11073 | s = " || "; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11074 | binop: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11075 | shtree(n->nbinary.ch1, ind, NULL, fp); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11076 | /* if (ind < 0) */ |
| 11077 | fputs(s, fp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11078 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 11079 | break; |
| 11080 | case NCMD: |
| 11081 | shcmd(n, fp); |
| 11082 | if (ind >= 0) |
| 11083 | putc('\n', fp); |
| 11084 | break; |
| 11085 | case NPIPE: |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11086 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11087 | shcmd(lp->n, fp); |
| 11088 | if (lp->next) |
| 11089 | fputs(" | ", fp); |
| 11090 | } |
| 11091 | if (n->npipe.backgnd) |
| 11092 | fputs(" &", fp); |
| 11093 | if (ind >= 0) |
| 11094 | putc('\n', fp); |
| 11095 | break; |
| 11096 | default: |
| 11097 | fprintf(fp, "<node type %d>", n->type); |
| 11098 | if (ind >= 0) |
| 11099 | putc('\n', fp); |
| 11100 | break; |
| 11101 | } |
| 11102 | } |
| 11103 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11104 | static void |
| 11105 | shcmd(union node *cmd, FILE *fp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11106 | { |
| 11107 | union node *np; |
| 11108 | int first; |
| 11109 | const char *s; |
| 11110 | int dftfd; |
| 11111 | |
| 11112 | first = 1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11113 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11114 | if (! first) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11115 | putchar(' '); |
| 11116 | sharg(np, fp); |
| 11117 | first = 0; |
| 11118 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11119 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11120 | if (! first) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11121 | putchar(' '); |
| 11122 | switch (np->nfile.type) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11123 | case NTO: s = ">"; dftfd = 1; break; |
| 11124 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 11125 | case NAPPEND: s = ">>"; dftfd = 1; break; |
| 11126 | case NTOFD: s = ">&"; dftfd = 1; break; |
| 11127 | case NFROM: s = "<"; dftfd = 0; break; |
| 11128 | case NFROMFD: s = "<&"; dftfd = 0; break; |
| 11129 | case NFROMTO: s = "<>"; dftfd = 0; break; |
| 11130 | default: s = "*error*"; dftfd = 0; break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11131 | } |
| 11132 | if (np->nfile.fd != dftfd) |
| 11133 | fprintf(fp, "%d", np->nfile.fd); |
| 11134 | fputs(s, fp); |
| 11135 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 11136 | fprintf(fp, "%d", np->ndup.dupfd); |
| 11137 | } else { |
| 11138 | sharg(np->nfile.fname, fp); |
| 11139 | } |
| 11140 | first = 0; |
| 11141 | } |
| 11142 | } |
| 11143 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11144 | static void |
| 11145 | sharg(union node *arg, FILE *fp) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11146 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11147 | char *p; |
| 11148 | struct nodelist *bqlist; |
| 11149 | int subtype; |
| 11150 | |
| 11151 | if (arg->type != NARG) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11152 | out1fmt("<node type %d>\n", arg->type); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11153 | abort(); |
| 11154 | } |
| 11155 | bqlist = arg->narg.backquote; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11156 | for (p = arg->narg.text; *p; p++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11157 | switch (*p) { |
| 11158 | case CTLESC: |
| 11159 | putc(*++p, fp); |
| 11160 | break; |
| 11161 | case CTLVAR: |
| 11162 | putc('$', fp); |
| 11163 | putc('{', fp); |
| 11164 | subtype = *++p; |
| 11165 | if (subtype == VSLENGTH) |
| 11166 | putc('#', fp); |
| 11167 | |
| 11168 | while (*p != '=') |
| 11169 | putc(*p++, fp); |
| 11170 | |
| 11171 | if (subtype & VSNUL) |
| 11172 | putc(':', fp); |
| 11173 | |
| 11174 | switch (subtype & VSTYPE) { |
| 11175 | case VSNORMAL: |
| 11176 | putc('}', fp); |
| 11177 | break; |
| 11178 | case VSMINUS: |
| 11179 | putc('-', fp); |
| 11180 | break; |
| 11181 | case VSPLUS: |
| 11182 | putc('+', fp); |
| 11183 | break; |
| 11184 | case VSQUESTION: |
| 11185 | putc('?', fp); |
| 11186 | break; |
| 11187 | case VSASSIGN: |
| 11188 | putc('=', fp); |
| 11189 | break; |
| 11190 | case VSTRIMLEFT: |
| 11191 | putc('#', fp); |
| 11192 | break; |
| 11193 | case VSTRIMLEFTMAX: |
| 11194 | putc('#', fp); |
| 11195 | putc('#', fp); |
| 11196 | break; |
| 11197 | case VSTRIMRIGHT: |
| 11198 | putc('%', fp); |
| 11199 | break; |
| 11200 | case VSTRIMRIGHTMAX: |
| 11201 | putc('%', fp); |
| 11202 | putc('%', fp); |
| 11203 | break; |
| 11204 | case VSLENGTH: |
| 11205 | break; |
| 11206 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11207 | out1fmt("<subtype %d>", subtype); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11208 | } |
| 11209 | break; |
| 11210 | case CTLENDVAR: |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11211 | putc('}', fp); |
| 11212 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11213 | case CTLBACKQ: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11214 | case CTLBACKQ|CTLQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11215 | putc('$', fp); |
| 11216 | putc('(', fp); |
| 11217 | shtree(bqlist->n, -1, NULL, fp); |
| 11218 | putc(')', fp); |
| 11219 | break; |
| 11220 | default: |
| 11221 | putc(*p, fp); |
| 11222 | break; |
| 11223 | } |
| 11224 | } |
| 11225 | } |
| 11226 | |
| 11227 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11228 | static void |
| 11229 | indent(int amount, char *pfx, FILE *fp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11230 | { |
| 11231 | int i; |
| 11232 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11233 | for (i = 0; i < amount; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11234 | if (pfx && i == amount - 1) |
| 11235 | fputs(pfx, fp); |
| 11236 | putc('\t', fp); |
| 11237 | } |
| 11238 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11239 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11240 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11241 | /* |
| 11242 | * Debugging stuff. |
| 11243 | */ |
| 11244 | |
| 11245 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11246 | static FILE *tracefile; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11247 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11248 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11249 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11250 | trputc(int c) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11251 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11252 | if (debug != 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11253 | return; |
| 11254 | putc(c, tracefile); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11255 | } |
| 11256 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11257 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11258 | trace(const char *fmt, ...) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11259 | { |
| 11260 | va_list va; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11261 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11262 | if (debug != 1) |
| 11263 | return; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11264 | va_start(va, fmt); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11265 | (void) vfprintf(tracefile, fmt, va); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11266 | va_end(va); |
| 11267 | } |
| 11268 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11269 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11270 | tracev(const char *fmt, va_list va) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11271 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11272 | if (debug != 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11273 | return; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11274 | (void) vfprintf(tracefile, fmt, va); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11275 | } |
| 11276 | |
| 11277 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11278 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11279 | trputs(const char *s) |
| 11280 | { |
| 11281 | if (debug != 1) |
| 11282 | return; |
| 11283 | fputs(s, tracefile); |
| 11284 | } |
| 11285 | |
| 11286 | |
| 11287 | static void |
| 11288 | trstring(char *s) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11289 | { |
| 11290 | char *p; |
| 11291 | char c; |
| 11292 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11293 | if (debug != 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11294 | return; |
| 11295 | putc('"', tracefile); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11296 | for (p = s; *p; p++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11297 | switch (*p) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11298 | case '\n': c = 'n'; goto backslash; |
| 11299 | case '\t': c = 't'; goto backslash; |
| 11300 | case '\r': c = 'r'; goto backslash; |
| 11301 | case '"': c = '"'; goto backslash; |
| 11302 | case '\\': c = '\\'; goto backslash; |
| 11303 | case CTLESC: c = 'e'; goto backslash; |
| 11304 | case CTLVAR: c = 'v'; goto backslash; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11305 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11306 | case CTLBACKQ: c = 'q'; goto backslash; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11307 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash; |
| 11308 | backslash: |
| 11309 | putc('\\', tracefile); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11310 | putc(c, tracefile); |
| 11311 | break; |
| 11312 | default: |
| 11313 | if (*p >= ' ' && *p <= '~') |
| 11314 | putc(*p, tracefile); |
| 11315 | else { |
| 11316 | putc('\\', tracefile); |
| 11317 | putc(*p >> 6 & 03, tracefile); |
| 11318 | putc(*p >> 3 & 07, tracefile); |
| 11319 | putc(*p & 07, tracefile); |
| 11320 | } |
| 11321 | break; |
| 11322 | } |
| 11323 | } |
| 11324 | putc('"', tracefile); |
| 11325 | } |
| 11326 | |
| 11327 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11328 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11329 | trargs(char **ap) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11330 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11331 | if (debug != 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11332 | return; |
| 11333 | while (*ap) { |
| 11334 | trstring(*ap++); |
| 11335 | if (*ap) |
| 11336 | putc(' ', tracefile); |
| 11337 | else |
| 11338 | putc('\n', tracefile); |
| 11339 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11340 | } |
| 11341 | |
| 11342 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11343 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11344 | opentrace(void) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11345 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11346 | char s[100]; |
| 11347 | #ifdef O_APPEND |
| 11348 | int flags; |
| 11349 | #endif |
| 11350 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11351 | if (debug != 1) { |
| 11352 | if (tracefile) |
| 11353 | fflush(tracefile); |
| 11354 | /* leave open because libedit might be using it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11355 | return; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11356 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11357 | scopy("./trace", s); |
| 11358 | if (tracefile) { |
| 11359 | if (!freopen(s, "a", tracefile)) { |
| 11360 | fprintf(stderr, "Can't re-open %s\n", s); |
| 11361 | debug = 0; |
| 11362 | return; |
| 11363 | } |
| 11364 | } else { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11365 | tracefile = fopen(s, "a"); |
| 11366 | if (tracefile == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11367 | fprintf(stderr, "Can't open %s\n", s); |
| 11368 | debug = 0; |
| 11369 | return; |
| 11370 | } |
| 11371 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11372 | #ifdef O_APPEND |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11373 | flags = fcntl(fileno(tracefile), F_GETFL, 0); |
| 11374 | if (flags >= 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11375 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 11376 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11377 | setlinebuf(tracefile); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11378 | fputs("\nTracing started.\n", tracefile); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11379 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11380 | #endif /* DEBUG */ |
| 11381 | |
| 11382 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11383 | /* trap.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11384 | |
| 11385 | /* |
| 11386 | * Sigmode records the current value of the signal handlers for the various |
| 11387 | * modes. A value of zero means that the current handler is not known. |
| 11388 | * S_HARD_IGN indicates that the signal was ignored on entry to the shell, |
| 11389 | */ |
| 11390 | |
| 11391 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 11392 | #define S_CATCH 2 /* signal is caught */ |
| 11393 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
| 11394 | #define S_HARD_IGN 4 /* signal is ignored permenantly */ |
| 11395 | #define S_RESET 5 /* temporary - to reset a hard ignored sig */ |
| 11396 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11397 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11398 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11399 | * The trap builtin. |
| 11400 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11401 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11402 | trapcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11403 | { |
| 11404 | char *action; |
| 11405 | char **ap; |
| 11406 | int signo; |
| 11407 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11408 | nextopt(nullstr); |
| 11409 | ap = argptr; |
| 11410 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11411 | for (signo = 0; signo < NSIG; signo++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11412 | if (trap[signo] != NULL) { |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11413 | const char *sn; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11414 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 11415 | sn = get_signame(signo); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11416 | out1fmt("trap -- %s %s\n", |
| 11417 | single_quote(trap[signo]), sn); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11418 | } |
| 11419 | } |
| 11420 | return 0; |
| 11421 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11422 | if (!ap[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11423 | action = NULL; |
| 11424 | else |
| 11425 | action = *ap++; |
| 11426 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11427 | signo = get_signum(*ap); |
| 11428 | if (signo < 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11429 | ash_msg_and_raise_error("%s: bad trap", *ap); |
| 11430 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11431 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 11432 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11433 | action = NULL; |
| 11434 | else |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11435 | action = ckstrdup(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11436 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11437 | if (trap[signo]) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11438 | free(trap[signo]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11439 | trap[signo] = action; |
| 11440 | if (signo != 0) |
| 11441 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11442 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11443 | ap++; |
| 11444 | } |
| 11445 | return 0; |
| 11446 | } |
| 11447 | |
| 11448 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11449 | /* |
| 11450 | * Clear traps on a fork. |
| 11451 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11452 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11453 | clear_traps(void) |
| 11454 | { |
| 11455 | char **tp; |
| 11456 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11457 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11458 | if (*tp && **tp) { /* trap not NULL or SIG_IGN */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11459 | INT_OFF; |
| 11460 | free(*tp); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11461 | *tp = NULL; |
| 11462 | if (tp != &trap[0]) |
| 11463 | setsignal(tp - trap); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11464 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11465 | } |
| 11466 | } |
| 11467 | } |
| 11468 | |
| 11469 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11470 | /* |
| 11471 | * Set the signal handler for the specified signal. The routine figures |
| 11472 | * out what it should be set to. |
| 11473 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11474 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11475 | setsignal(int signo) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11476 | { |
| 11477 | int action; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11478 | char *t, tsig; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11479 | struct sigaction act; |
| 11480 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11481 | t = trap[signo]; |
| 11482 | if (t == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11483 | action = S_DFL; |
| 11484 | else if (*t != '\0') |
| 11485 | action = S_CATCH; |
| 11486 | else |
| 11487 | action = S_IGN; |
| 11488 | if (rootshell && action == S_DFL) { |
| 11489 | switch (signo) { |
| 11490 | case SIGINT: |
| 11491 | if (iflag || minusc || sflag == 0) |
| 11492 | action = S_CATCH; |
| 11493 | break; |
| 11494 | case SIGQUIT: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 11495 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11496 | if (debug) |
| 11497 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11498 | #endif |
| 11499 | /* FALLTHROUGH */ |
| 11500 | case SIGTERM: |
| 11501 | if (iflag) |
| 11502 | action = S_IGN; |
| 11503 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11504 | #if JOBS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11505 | case SIGTSTP: |
| 11506 | case SIGTTOU: |
| 11507 | if (mflag) |
| 11508 | action = S_IGN; |
| 11509 | break; |
| 11510 | #endif |
| 11511 | } |
| 11512 | } |
| 11513 | |
| 11514 | t = &sigmode[signo - 1]; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11515 | tsig = *t; |
| 11516 | if (tsig == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11517 | /* |
| 11518 | * current setting unknown |
| 11519 | */ |
| 11520 | if (sigaction(signo, 0, &act) == -1) { |
| 11521 | /* |
| 11522 | * Pretend it worked; maybe we should give a warning |
| 11523 | * here, but other shells don't. We don't alter |
| 11524 | * sigmode, so that we retry every time. |
| 11525 | */ |
| 11526 | return; |
| 11527 | } |
| 11528 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11529 | if (mflag |
| 11530 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11531 | ) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11532 | tsig = S_IGN; /* don't hard ignore these */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11533 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11534 | tsig = S_HARD_IGN; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11535 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11536 | tsig = S_RESET; /* force to be set */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11537 | } |
| 11538 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11539 | if (tsig == S_HARD_IGN || tsig == action) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11540 | return; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11541 | switch (action) { |
| 11542 | case S_CATCH: |
| 11543 | act.sa_handler = onsig; |
| 11544 | break; |
| 11545 | case S_IGN: |
| 11546 | act.sa_handler = SIG_IGN; |
| 11547 | break; |
| 11548 | default: |
| 11549 | act.sa_handler = SIG_DFL; |
| 11550 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11551 | *t = action; |
| 11552 | act.sa_flags = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11553 | sigfillset(&act.sa_mask); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11554 | sigaction(signo, &act, 0); |
| 11555 | } |
| 11556 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11557 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11558 | /* |
| 11559 | * Ignore a signal. |
| 11560 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11561 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11562 | ignoresig(int signo) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11563 | { |
| 11564 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 11565 | signal(signo, SIG_IGN); |
| 11566 | } |
| 11567 | sigmode[signo - 1] = S_HARD_IGN; |
| 11568 | } |
| 11569 | |
| 11570 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11571 | /* |
| 11572 | * Signal handler. |
| 11573 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11574 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11575 | onsig(int signo) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11576 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11577 | gotsig[signo - 1] = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11578 | pendingsigs = signo; |
| 11579 | |
| 11580 | if (exsig || (signo == SIGINT && !trap[SIGINT])) { |
| 11581 | if (!suppressint) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11582 | raise_interrupt(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11583 | intpending = 1; |
| 11584 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11585 | } |
| 11586 | |
| 11587 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11588 | /* |
| 11589 | * Called to execute a trap. Perhaps we should avoid entering new trap |
| 11590 | * handlers while we are executing a trap handler. |
| 11591 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11592 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11593 | dotrap(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11594 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11595 | char *p; |
| 11596 | char *q; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11597 | int i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11598 | int savestatus; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11599 | int skip = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11600 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11601 | savestatus = exitstatus; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11602 | pendingsigs = 0; |
| 11603 | xbarrier(); |
| 11604 | |
| 11605 | for (i = 0, q = gotsig; i < NSIG - 1; i++, q++) { |
| 11606 | if (!*q) |
| 11607 | continue; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11608 | *q = '\0'; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11609 | |
| 11610 | p = trap[i + 1]; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11611 | if (!p) |
| 11612 | continue; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11613 | skip = evalstring(p, SKIPEVAL); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11614 | exitstatus = savestatus; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11615 | if (skip) |
| 11616 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11617 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11618 | |
| 11619 | return skip; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11620 | } |
| 11621 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11622 | /* |
| 11623 | * Controls whether the shell is interactive or not. |
| 11624 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11625 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11626 | setinteractive(int on) |
| 11627 | { |
| 11628 | static int is_interactive; |
| 11629 | |
| 11630 | if (++on == is_interactive) |
| 11631 | return; |
| 11632 | is_interactive = on; |
| 11633 | setsignal(SIGINT); |
| 11634 | setsignal(SIGQUIT); |
| 11635 | setsignal(SIGTERM); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 11636 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11637 | if (is_interactive > 1) { |
| 11638 | /* Looks like they want an interactive shell */ |
| 11639 | static int do_banner; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11640 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11641 | if (!do_banner) { |
| 11642 | out1fmt( |
| 11643 | "\n\n" |
| 11644 | "%s Built-in shell (ash)\n" |
| 11645 | "Enter 'help' for a list of built-in commands." |
| 11646 | "\n\n", |
| 11647 | BB_BANNER); |
| 11648 | do_banner++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11649 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11650 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11651 | #endif |
| 11652 | } |
| 11653 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 11654 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11655 | /*** List the available builtins ***/ |
| 11656 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11657 | static int |
| 11658 | helpcmd(int argc, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11659 | { |
| 11660 | int col, i; |
| 11661 | |
| 11662 | out1fmt("\nBuilt-in commands:\n-------------------\n"); |
| 11663 | for (col = 0, i = 0; i < NUMBUILTINS; i++) { |
| 11664 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
| 11665 | builtincmd[i].name + 1); |
| 11666 | if (col > 60) { |
| 11667 | out1fmt("\n"); |
| 11668 | col = 0; |
| 11669 | } |
| 11670 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 11671 | #if ENABLE_FEATURE_SH_STANDALONE_SHELL |
Denis Vlasenko | 0ee3999 | 2006-12-24 15:23:28 +0000 | [diff] [blame] | 11672 | for (i = 0; i < NUM_APPLETS; i++) { |
| 11673 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), applets[i].name); |
| 11674 | if (col > 60) { |
| 11675 | out1fmt("\n"); |
| 11676 | col = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11677 | } |
| 11678 | } |
| 11679 | #endif |
| 11680 | out1fmt("\n\n"); |
| 11681 | return EXIT_SUCCESS; |
| 11682 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11683 | #endif /* FEATURE_SH_EXTRA_QUIET */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11684 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11685 | /* |
| 11686 | * Called to exit the shell. |
| 11687 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11688 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11689 | exitshell(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11690 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11691 | struct jmploc loc; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11692 | char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11693 | int status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11694 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11695 | status = exitstatus; |
| 11696 | TRACE(("pid %d, exitshell(%d)\n", getpid(), status)); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11697 | if (setjmp(loc.loc)) { |
| 11698 | if (exception == EXEXIT) |
Denis Vlasenko | 7f0d7ae | 2007-01-18 01:12:57 +0000 | [diff] [blame] | 11699 | /* dash bug: it just does _exit(exitstatus) here |
| 11700 | * but we have to do setjobctl(0) first! |
| 11701 | * (bug is still not fixed in dash-0.5.3 - if you run dash |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 11702 | * under Midnight Commander, on exit from dash MC is backgrounded) */ |
Denis Vlasenko | 7f0d7ae | 2007-01-18 01:12:57 +0000 | [diff] [blame] | 11703 | status = exitstatus; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11704 | goto out; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11705 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11706 | exception_handler = &loc; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11707 | p = trap[0]; |
| 11708 | if (p) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11709 | trap[0] = NULL; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11710 | evalstring(p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11711 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11712 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11713 | out: |
Denis Vlasenko | 7f0d7ae | 2007-01-18 01:12:57 +0000 | [diff] [blame] | 11714 | setjobctl(0); |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 11715 | _exit(status); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11716 | /* NOTREACHED */ |
| 11717 | } |
| 11718 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11719 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11720 | /* |
| 11721 | * The export and readonly commands. |
| 11722 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11723 | static int |
| 11724 | exportcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11725 | { |
| 11726 | struct var *vp; |
| 11727 | char *name; |
| 11728 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11729 | char **aptr; |
| 11730 | int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11731 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11732 | if (nextopt("p") != 'p') { |
| 11733 | aptr = argptr; |
| 11734 | name = *aptr; |
| 11735 | if (name) { |
| 11736 | do { |
| 11737 | p = strchr(name, '='); |
| 11738 | if (p != NULL) { |
| 11739 | p++; |
| 11740 | } else { |
| 11741 | vp = *findvar(hashvar(name), name); |
| 11742 | if (vp) { |
| 11743 | vp->flags |= flag; |
| 11744 | continue; |
| 11745 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11746 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11747 | setvar(name, p, flag); |
| 11748 | } while ((name = *++aptr) != NULL); |
| 11749 | return 0; |
| 11750 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11751 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11752 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11753 | return 0; |
| 11754 | } |
| 11755 | |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11756 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11757 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11758 | * Make a variable a local variable. When a variable is made local, it's |
| 11759 | * value and flags are saved in a localvar structure. The saved values |
| 11760 | * will be restored when the shell function returns. We handle the name |
| 11761 | * "-" as a special case. |
| 11762 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 11763 | static void mklocal(char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11764 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11765 | struct localvar *lvp; |
| 11766 | struct var **vpp; |
| 11767 | struct var *vp; |
| 11768 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11769 | INT_OFF; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11770 | lvp = ckmalloc(sizeof(struct localvar)); |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 11771 | if (LONE_DASH(name)) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11772 | char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11773 | p = ckmalloc(sizeof(optlist)); |
| 11774 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11775 | vp = NULL; |
| 11776 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11777 | char *eq; |
| 11778 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11779 | vpp = hashvar(name); |
| 11780 | vp = *findvar(vpp, name); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11781 | eq = strchr(name, '='); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11782 | if (vp == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11783 | if (eq) |
| 11784 | setvareq(name, VSTRFIXED); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11785 | else |
| 11786 | setvar(name, NULL, VSTRFIXED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11787 | vp = *vpp; /* the new variable */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11788 | lvp->flags = VUNSET; |
| 11789 | } else { |
| 11790 | lvp->text = vp->text; |
| 11791 | lvp->flags = vp->flags; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11792 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 11793 | if (eq) |
| 11794 | setvareq(name, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11795 | } |
| 11796 | } |
| 11797 | lvp->vp = vp; |
| 11798 | lvp->next = localvars; |
| 11799 | localvars = lvp; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11800 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11801 | } |
| 11802 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11803 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11804 | /* |
| 11805 | * The "local" command. |
| 11806 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11807 | static int |
| 11808 | localcmd(int argc, char **argv) |
| 11809 | { |
| 11810 | char *name; |
| 11811 | |
| 11812 | argv = argptr; |
| 11813 | while ((name = *argv++) != NULL) { |
| 11814 | mklocal(name); |
| 11815 | } |
| 11816 | return 0; |
| 11817 | } |
| 11818 | |
| 11819 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11820 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11821 | * The unset builtin command. We unset the function before we unset the |
| 11822 | * variable to allow a function to be unset when there is a readonly variable |
| 11823 | * with the same name. |
| 11824 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11825 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11826 | unsetcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11827 | { |
| 11828 | char **ap; |
| 11829 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11830 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11831 | int ret = 0; |
| 11832 | |
| 11833 | while ((i = nextopt("vf")) != '\0') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11834 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11835 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11836 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11837 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11838 | if (flag != 'f') { |
| 11839 | i = unsetvar(*ap); |
| 11840 | ret |= i; |
| 11841 | if (!(i & 2)) |
| 11842 | continue; |
| 11843 | } |
| 11844 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11845 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11846 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11847 | return ret & 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11848 | } |
| 11849 | |
| 11850 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11851 | /* setmode.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11852 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11853 | #include <sys/times.h> |
| 11854 | |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 11855 | static const unsigned char timescmd_str[] = { |
| 11856 | ' ', offsetof(struct tms, tms_utime), |
| 11857 | '\n', offsetof(struct tms, tms_stime), |
| 11858 | ' ', offsetof(struct tms, tms_cutime), |
| 11859 | '\n', offsetof(struct tms, tms_cstime), |
| 11860 | 0 |
| 11861 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11862 | |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 11863 | static int timescmd(int ac, char **av) |
| 11864 | { |
| 11865 | long int clk_tck, s, t; |
| 11866 | const unsigned char *p; |
| 11867 | struct tms buf; |
| 11868 | |
| 11869 | clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11870 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 11871 | |
| 11872 | p = timescmd_str; |
| 11873 | do { |
| 11874 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 11875 | s = t / clk_tck; |
| 11876 | out1fmt("%ldm%ld.%.3lds%c", |
| 11877 | s/60, s%60, |
| 11878 | ((t - s * clk_tck) * 1000) / clk_tck, |
| 11879 | p[0]); |
| 11880 | } while (*(p += 2)); |
| 11881 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11882 | return 0; |
| 11883 | } |
| 11884 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11885 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 11886 | static arith_t |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11887 | dash_arith(const char *s) |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 11888 | { |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 11889 | arith_t result; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11890 | int errcode = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11891 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11892 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11893 | result = arith(s, &errcode); |
| 11894 | if (errcode < 0) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11895 | if (errcode == -3) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11896 | ash_msg_and_raise_error("exponent less than 0"); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11897 | else if (errcode == -2) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11898 | ash_msg_and_raise_error("divide by zero"); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11899 | else if (errcode == -5) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11900 | ash_msg_and_raise_error("expression recursion loop detected"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11901 | else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11902 | raise_error_syntax(s); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11903 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11904 | INT_ON; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11905 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 11906 | return result; |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 11907 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11908 | |
| 11909 | |
| 11910 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11911 | * The let builtin. partial stolen from GNU Bash, the Bourne Again SHell. |
| 11912 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
| 11913 | * |
| 11914 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11915 | */ |
| 11916 | static int |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11917 | letcmd(int argc, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11918 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11919 | char **ap; |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 11920 | arith_t i = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11921 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11922 | ap = argv + 1; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11923 | if (!*ap) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11924 | ash_msg_and_raise_error("expression expected"); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11925 | for (ap = argv + 1; *ap; ap++) { |
| 11926 | i = dash_arith(*ap); |
| 11927 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11928 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 11929 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11930 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11931 | #endif /* ASH_MATH_SUPPORT */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11932 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11933 | /* miscbltin.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11934 | |
| 11935 | /* |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 11936 | * Miscellaneous builtins. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11937 | */ |
| 11938 | |
| 11939 | #undef rflag |
| 11940 | |
Denis Vlasenko | 83e5d6f | 2006-12-18 21:49:06 +0000 | [diff] [blame] | 11941 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 1 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11942 | typedef enum __rlimit_resource rlim_t; |
| 11943 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 11944 | |
| 11945 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11946 | /* |
| 11947 | * The read builtin. The -e option causes backslashes to escape the |
| 11948 | * following character. |
| 11949 | * |
| 11950 | * This uses unbuffered input, which may be avoidable in some cases. |
| 11951 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11952 | static int |
| 11953 | readcmd(int argc, char **argv) |
| 11954 | { |
| 11955 | char **ap; |
| 11956 | int backslash; |
| 11957 | char c; |
| 11958 | int rflag; |
| 11959 | char *prompt; |
| 11960 | const char *ifs; |
| 11961 | char *p; |
| 11962 | int startword; |
| 11963 | int status; |
| 11964 | int i; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11965 | #if ENABLE_ASH_READ_NCHARS |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 11966 | int nch_flag = 0; |
| 11967 | int nchars = 0; |
| 11968 | int silent = 0; |
| 11969 | struct termios tty, old_tty; |
| 11970 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11971 | #if ENABLE_ASH_READ_TIMEOUT |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 11972 | fd_set set; |
| 11973 | struct timeval ts; |
| 11974 | |
| 11975 | ts.tv_sec = ts.tv_usec = 0; |
| 11976 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11977 | |
| 11978 | rflag = 0; |
| 11979 | prompt = NULL; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11980 | #if ENABLE_ASH_READ_NCHARS && ENABLE_ASH_READ_TIMEOUT |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 11981 | while ((i = nextopt("p:rt:n:s")) != '\0') |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11982 | #elif ENABLE_ASH_READ_NCHARS |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 11983 | while ((i = nextopt("p:rn:s")) != '\0') |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11984 | #elif ENABLE_ASH_READ_TIMEOUT |
Ned Ludd | 2123b7c | 2005-02-09 21:07:23 +0000 | [diff] [blame] | 11985 | while ((i = nextopt("p:rt:")) != '\0') |
| 11986 | #else |
| 11987 | while ((i = nextopt("p:r")) != '\0') |
| 11988 | #endif |
| 11989 | { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 11990 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 11991 | case 'p': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11992 | prompt = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 11993 | break; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11994 | #if ENABLE_ASH_READ_NCHARS |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 11995 | case 'n': |
| 11996 | nchars = strtol(optionarg, &p, 10); |
| 11997 | if (*p) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11998 | ash_msg_and_raise_error("invalid count"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 11999 | nch_flag = (nchars > 0); |
| 12000 | break; |
| 12001 | case 's': |
| 12002 | silent = 1; |
| 12003 | break; |
Ned Ludd | 2123b7c | 2005-02-09 21:07:23 +0000 | [diff] [blame] | 12004 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12005 | #if ENABLE_ASH_READ_TIMEOUT |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12006 | case 't': |
"Vladimir N. Oleynik" | 11d7c52 | 2005-09-26 13:24:45 +0000 | [diff] [blame] | 12007 | ts.tv_sec = strtol(optionarg, &p, 10); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12008 | ts.tv_usec = 0; |
| 12009 | if (*p == '.') { |
| 12010 | char *p2; |
| 12011 | if (*++p) { |
| 12012 | int scale; |
"Vladimir N. Oleynik" | 11d7c52 | 2005-09-26 13:24:45 +0000 | [diff] [blame] | 12013 | ts.tv_usec = strtol(p, &p2, 10); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12014 | if (*p2) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12015 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12016 | scale = p2 - p; |
| 12017 | /* normalize to usec */ |
| 12018 | if (scale > 6) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12019 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12020 | while (scale++ < 6) |
| 12021 | ts.tv_usec *= 10; |
| 12022 | } |
| 12023 | } else if (*p) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12024 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12025 | } |
| 12026 | if ( ! ts.tv_sec && ! ts.tv_usec) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12027 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12028 | break; |
| 12029 | #endif |
| 12030 | case 'r': |
| 12031 | rflag = 1; |
| 12032 | break; |
| 12033 | default: |
| 12034 | break; |
| 12035 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12036 | } |
| 12037 | if (prompt && isatty(0)) { |
| 12038 | out2str(prompt); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12039 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12040 | ap = argptr; |
| 12041 | if (*ap == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12042 | ash_msg_and_raise_error("arg count"); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12043 | ifs = bltinlookup("IFS"); |
| 12044 | if (ifs == NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12045 | ifs = defifs; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12046 | #if ENABLE_ASH_READ_NCHARS |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12047 | if (nch_flag || silent) { |
| 12048 | tcgetattr(0, &tty); |
| 12049 | old_tty = tty; |
| 12050 | if (nch_flag) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12051 | tty.c_lflag &= ~ICANON; |
| 12052 | tty.c_cc[VMIN] = nchars; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12053 | } |
| 12054 | if (silent) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12055 | tty.c_lflag &= ~(ECHO|ECHOK|ECHONL); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12056 | |
| 12057 | } |
| 12058 | tcsetattr(0, TCSANOW, &tty); |
| 12059 | } |
| 12060 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12061 | #if ENABLE_ASH_READ_TIMEOUT |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12062 | if (ts.tv_sec || ts.tv_usec) { |
| 12063 | FD_ZERO (&set); |
| 12064 | FD_SET (0, &set); |
| 12065 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 12066 | i = select(FD_SETSIZE, &set, NULL, NULL, &ts); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12067 | if (!i) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12068 | #if ENABLE_ASH_READ_NCHARS |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12069 | if (nch_flag) |
| 12070 | tcsetattr(0, TCSANOW, &old_tty); |
| 12071 | #endif |
| 12072 | return 1; |
| 12073 | } |
| 12074 | } |
Ned Ludd | 2123b7c | 2005-02-09 21:07:23 +0000 | [diff] [blame] | 12075 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12076 | status = 0; |
| 12077 | startword = 1; |
| 12078 | backslash = 0; |
| 12079 | STARTSTACKSTR(p); |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12080 | #if ENABLE_ASH_READ_NCHARS |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12081 | while (!nch_flag || nchars--) |
Ned Ludd | 2123b7c | 2005-02-09 21:07:23 +0000 | [diff] [blame] | 12082 | #else |
| 12083 | for (;;) |
| 12084 | #endif |
| 12085 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12086 | if (read(0, &c, 1) != 1) { |
| 12087 | status = 1; |
| 12088 | break; |
| 12089 | } |
| 12090 | if (c == '\0') |
| 12091 | continue; |
| 12092 | if (backslash) { |
| 12093 | backslash = 0; |
| 12094 | if (c != '\n') |
| 12095 | goto put; |
| 12096 | continue; |
| 12097 | } |
| 12098 | if (!rflag && c == '\\') { |
| 12099 | backslash++; |
| 12100 | continue; |
| 12101 | } |
| 12102 | if (c == '\n') |
| 12103 | break; |
| 12104 | if (startword && *ifs == ' ' && strchr(ifs, c)) { |
| 12105 | continue; |
| 12106 | } |
| 12107 | startword = 0; |
| 12108 | if (ap[1] != NULL && strchr(ifs, c) != NULL) { |
| 12109 | STACKSTRNUL(p); |
| 12110 | setvar(*ap, stackblock(), 0); |
| 12111 | ap++; |
| 12112 | startword = 1; |
| 12113 | STARTSTACKSTR(p); |
| 12114 | } else { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12115 | put: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12116 | STPUTC(c, p); |
| 12117 | } |
| 12118 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12119 | #if ENABLE_ASH_READ_NCHARS |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12120 | if (nch_flag || silent) |
| 12121 | tcsetattr(0, TCSANOW, &old_tty); |
| 12122 | #endif |
| 12123 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12124 | STACKSTRNUL(p); |
| 12125 | /* Remove trailing blanks */ |
| 12126 | while ((char *)stackblock() <= --p && strchr(ifs, *p) != NULL) |
| 12127 | *p = '\0'; |
| 12128 | setvar(*ap, stackblock(), 0); |
| 12129 | while (*++ap != NULL) |
| 12130 | setvar(*ap, nullstr, 0); |
| 12131 | return status; |
| 12132 | } |
| 12133 | |
| 12134 | |
| 12135 | static int umaskcmd(int argc, char **argv) |
| 12136 | { |
| 12137 | static const char permuser[3] = "ugo"; |
| 12138 | static const char permmode[3] = "rwx"; |
| 12139 | static const short int permmask[] = { |
| 12140 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 12141 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 12142 | S_IROTH, S_IWOTH, S_IXOTH |
| 12143 | }; |
| 12144 | |
| 12145 | char *ap; |
| 12146 | mode_t mask; |
| 12147 | int i; |
| 12148 | int symbolic_mode = 0; |
| 12149 | |
| 12150 | while (nextopt("S") != '\0') { |
| 12151 | symbolic_mode = 1; |
| 12152 | } |
| 12153 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12154 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12155 | mask = umask(0); |
| 12156 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12157 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12158 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12159 | ap = *argptr; |
| 12160 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12161 | if (symbolic_mode) { |
| 12162 | char buf[18]; |
| 12163 | char *p = buf; |
| 12164 | |
| 12165 | for (i = 0; i < 3; i++) { |
| 12166 | int j; |
| 12167 | |
| 12168 | *p++ = permuser[i]; |
| 12169 | *p++ = '='; |
| 12170 | for (j = 0; j < 3; j++) { |
| 12171 | if ((mask & permmask[3 * i + j]) == 0) { |
| 12172 | *p++ = permmode[j]; |
| 12173 | } |
| 12174 | } |
| 12175 | *p++ = ','; |
| 12176 | } |
| 12177 | *--p = 0; |
| 12178 | puts(buf); |
| 12179 | } else { |
| 12180 | out1fmt("%.4o\n", mask); |
| 12181 | } |
| 12182 | } else { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12183 | if (isdigit((unsigned char) *ap)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12184 | mask = 0; |
| 12185 | do { |
| 12186 | if (*ap >= '8' || *ap < '0') |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12187 | ash_msg_and_raise_error(illnum, argv[1]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12188 | mask = (mask << 3) + (*ap - '0'); |
| 12189 | } while (*++ap != '\0'); |
| 12190 | umask(mask); |
| 12191 | } else { |
| 12192 | mask = ~mask & 0777; |
| 12193 | if (!bb_parse_mode(ap, &mask)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12194 | ash_msg_and_raise_error("Illegal mode: %s", ap); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12195 | } |
| 12196 | umask(~mask & 0777); |
| 12197 | } |
| 12198 | } |
| 12199 | return 0; |
| 12200 | } |
| 12201 | |
| 12202 | /* |
| 12203 | * ulimit builtin |
| 12204 | * |
| 12205 | * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and |
| 12206 | * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with |
| 12207 | * ash by J.T. Conklin. |
| 12208 | * |
| 12209 | * Public domain. |
| 12210 | */ |
| 12211 | |
| 12212 | struct limits { |
| 12213 | const char *name; |
| 12214 | int cmd; |
| 12215 | int factor; /* multiply by to get rlim_{cur,max} values */ |
| 12216 | char option; |
| 12217 | }; |
| 12218 | |
| 12219 | static const struct limits limits[] = { |
| 12220 | #ifdef RLIMIT_CPU |
| 12221 | { "time(seconds)", RLIMIT_CPU, 1, 't' }, |
| 12222 | #endif |
| 12223 | #ifdef RLIMIT_FSIZE |
| 12224 | { "file(blocks)", RLIMIT_FSIZE, 512, 'f' }, |
| 12225 | #endif |
| 12226 | #ifdef RLIMIT_DATA |
| 12227 | { "data(kbytes)", RLIMIT_DATA, 1024, 'd' }, |
| 12228 | #endif |
| 12229 | #ifdef RLIMIT_STACK |
| 12230 | { "stack(kbytes)", RLIMIT_STACK, 1024, 's' }, |
| 12231 | #endif |
| 12232 | #ifdef RLIMIT_CORE |
| 12233 | { "coredump(blocks)", RLIMIT_CORE, 512, 'c' }, |
| 12234 | #endif |
| 12235 | #ifdef RLIMIT_RSS |
| 12236 | { "memory(kbytes)", RLIMIT_RSS, 1024, 'm' }, |
| 12237 | #endif |
| 12238 | #ifdef RLIMIT_MEMLOCK |
| 12239 | { "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024, 'l' }, |
| 12240 | #endif |
| 12241 | #ifdef RLIMIT_NPROC |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12242 | { "process", RLIMIT_NPROC, 1, 'p' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12243 | #endif |
| 12244 | #ifdef RLIMIT_NOFILE |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12245 | { "nofiles", RLIMIT_NOFILE, 1, 'n' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12246 | #endif |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12247 | #ifdef RLIMIT_AS |
| 12248 | { "vmemory(kbytes)", RLIMIT_AS, 1024, 'v' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12249 | #endif |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12250 | #ifdef RLIMIT_LOCKS |
| 12251 | { "locks", RLIMIT_LOCKS, 1, 'w' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12252 | #endif |
| 12253 | { (char *) 0, 0, 0, '\0' } |
| 12254 | }; |
| 12255 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12256 | enum limtype { SOFT = 0x1, HARD = 0x2 }; |
| 12257 | |
| 12258 | static void printlim(enum limtype how, const struct rlimit *limit, |
| 12259 | const struct limits *l) |
| 12260 | { |
| 12261 | rlim_t val; |
| 12262 | |
| 12263 | val = limit->rlim_max; |
| 12264 | if (how & SOFT) |
| 12265 | val = limit->rlim_cur; |
| 12266 | |
| 12267 | if (val == RLIM_INFINITY) |
| 12268 | out1fmt("unlimited\n"); |
| 12269 | else { |
| 12270 | val /= l->factor; |
| 12271 | out1fmt("%lld\n", (long long) val); |
| 12272 | } |
| 12273 | } |
| 12274 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12275 | int |
| 12276 | ulimitcmd(int argc, char **argv) |
| 12277 | { |
| 12278 | int c; |
| 12279 | rlim_t val = 0; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12280 | enum limtype how = SOFT | HARD; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12281 | const struct limits *l; |
| 12282 | int set, all = 0; |
| 12283 | int optc, what; |
| 12284 | struct rlimit limit; |
| 12285 | |
| 12286 | what = 'f'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 12287 | while ((optc = nextopt("HSa" |
| 12288 | #ifdef RLIMIT_CPU |
| 12289 | "t" |
| 12290 | #endif |
| 12291 | #ifdef RLIMIT_FSIZE |
| 12292 | "f" |
| 12293 | #endif |
| 12294 | #ifdef RLIMIT_DATA |
| 12295 | "d" |
| 12296 | #endif |
| 12297 | #ifdef RLIMIT_STACK |
| 12298 | "s" |
| 12299 | #endif |
| 12300 | #ifdef RLIMIT_CORE |
| 12301 | "c" |
| 12302 | #endif |
| 12303 | #ifdef RLIMIT_RSS |
| 12304 | "m" |
| 12305 | #endif |
| 12306 | #ifdef RLIMIT_MEMLOCK |
| 12307 | "l" |
| 12308 | #endif |
| 12309 | #ifdef RLIMIT_NPROC |
| 12310 | "p" |
| 12311 | #endif |
| 12312 | #ifdef RLIMIT_NOFILE |
| 12313 | "n" |
| 12314 | #endif |
| 12315 | #ifdef RLIMIT_AS |
| 12316 | "v" |
| 12317 | #endif |
| 12318 | #ifdef RLIMIT_LOCKS |
| 12319 | "w" |
| 12320 | #endif |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12321 | )) != '\0') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12322 | switch (optc) { |
| 12323 | case 'H': |
| 12324 | how = HARD; |
| 12325 | break; |
| 12326 | case 'S': |
| 12327 | how = SOFT; |
| 12328 | break; |
| 12329 | case 'a': |
| 12330 | all = 1; |
| 12331 | break; |
| 12332 | default: |
| 12333 | what = optc; |
| 12334 | } |
| 12335 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12336 | for (l = limits; l->option != what; l++) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12337 | ; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12338 | |
| 12339 | set = *argptr ? 1 : 0; |
| 12340 | if (set) { |
| 12341 | char *p = *argptr; |
| 12342 | |
| 12343 | if (all || argptr[1]) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12344 | ash_msg_and_raise_error("too many arguments"); |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 12345 | if (strncmp(p, "unlimited\n", 9) == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12346 | val = RLIM_INFINITY; |
| 12347 | else { |
| 12348 | val = (rlim_t) 0; |
| 12349 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12350 | while ((c = *p++) >= '0' && c <= '9') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12351 | val = (val * 10) + (long)(c - '0'); |
| 12352 | if (val < (rlim_t) 0) |
| 12353 | break; |
| 12354 | } |
| 12355 | if (c) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12356 | ash_msg_and_raise_error("bad number"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12357 | val *= l->factor; |
| 12358 | } |
| 12359 | } |
| 12360 | if (all) { |
| 12361 | for (l = limits; l->name; l++) { |
| 12362 | getrlimit(l->cmd, &limit); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12363 | out1fmt("%-20s ", l->name); |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12364 | printlim(how, &limit, l); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12365 | } |
| 12366 | return 0; |
| 12367 | } |
| 12368 | |
| 12369 | getrlimit(l->cmd, &limit); |
| 12370 | if (set) { |
| 12371 | if (how & HARD) |
| 12372 | limit.rlim_max = val; |
| 12373 | if (how & SOFT) |
| 12374 | limit.rlim_cur = val; |
| 12375 | if (setrlimit(l->cmd, &limit) < 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12376 | ash_msg_and_raise_error("error setting limit (%m)"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12377 | } else { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12378 | printlim(how, &limit, l); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12379 | } |
| 12380 | return 0; |
| 12381 | } |
| 12382 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12383 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12384 | #if ENABLE_ASH_MATH_SUPPORT |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12385 | |
| 12386 | /* Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com> |
| 12387 | |
| 12388 | Permission is hereby granted, free of charge, to any person obtaining |
| 12389 | a copy of this software and associated documentation files (the |
| 12390 | "Software"), to deal in the Software without restriction, including |
| 12391 | without limitation the rights to use, copy, modify, merge, publish, |
| 12392 | distribute, sublicense, and/or sell copies of the Software, and to |
| 12393 | permit persons to whom the Software is furnished to do so, subject to |
| 12394 | the following conditions: |
| 12395 | |
| 12396 | The above copyright notice and this permission notice shall be |
| 12397 | included in all copies or substantial portions of the Software. |
| 12398 | |
| 12399 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 12400 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 12401 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 12402 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 12403 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 12404 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 12405 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 12406 | */ |
| 12407 | |
| 12408 | /* This is my infix parser/evaluator. It is optimized for size, intended |
| 12409 | * 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] | 12410 | * than a comparable parser written in yacc. The supported operators are |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12411 | * listed in #defines below. Parens, order of operations, and error handling |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12412 | * are supported. This code is thread safe. The exact expression format should |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12413 | * be that which POSIX specifies for shells. */ |
| 12414 | |
| 12415 | /* The code uses a simple two-stack algorithm. See |
| 12416 | * http://www.onthenet.com.au/~grahamis/int2008/week02/lect02.html |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12417 | * for a detailed explanation of the infix-to-postfix algorithm on which |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12418 | * this is based (this code differs in that it applies operators immediately |
| 12419 | * to the stack instead of adding them to a queue to end up with an |
| 12420 | * expression). */ |
| 12421 | |
| 12422 | /* To use the routine, call it with an expression string and error return |
| 12423 | * pointer */ |
| 12424 | |
| 12425 | /* |
| 12426 | * Aug 24, 2001 Manuel Novoa III |
| 12427 | * |
| 12428 | * Reduced the generated code size by about 30% (i386) and fixed several bugs. |
| 12429 | * |
| 12430 | * 1) In arith_apply(): |
| 12431 | * a) Cached values of *numptr and &(numptr[-1]). |
| 12432 | * b) Removed redundant test for zero denominator. |
| 12433 | * |
| 12434 | * 2) In arith(): |
| 12435 | * a) Eliminated redundant code for processing operator tokens by moving |
| 12436 | * to a table-based implementation. Also folded handling of parens |
| 12437 | * into the table. |
| 12438 | * b) Combined all 3 loops which called arith_apply to reduce generated |
| 12439 | * code size at the cost of speed. |
| 12440 | * |
| 12441 | * 3) The following expressions were treated as valid by the original code: |
| 12442 | * 1() , 0! , 1 ( *3 ) . |
| 12443 | * These bugs have been fixed by internally enclosing the expression in |
| 12444 | * parens and then checking that all binary ops and right parens are |
| 12445 | * preceded by a valid expression (NUM_TOKEN). |
| 12446 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12447 | * 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] | 12448 | * ctype's isspace() if it is used by another busybox applet or if additional |
| 12449 | * whitespace chars should be considered. Look below the "#include"s for a |
| 12450 | * precompiler test. |
| 12451 | */ |
| 12452 | |
| 12453 | /* |
| 12454 | * Aug 26, 2001 Manuel Novoa III |
| 12455 | * |
| 12456 | * Return 0 for null expressions. Pointed out by Vladimir Oleynik. |
| 12457 | * |
| 12458 | * Merge in Aaron's comments previously posted to the busybox list, |
| 12459 | * modified slightly to take account of my changes to the code. |
| 12460 | * |
| 12461 | */ |
| 12462 | |
| 12463 | /* |
| 12464 | * (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
| 12465 | * |
| 12466 | * - allow access to variable, |
| 12467 | * used recursive find value indirection (c=2*2; a="c"; $((a+=2)) produce 6) |
| 12468 | * - realize assign syntax (VAR=expr, +=, *= etc) |
| 12469 | * - realize exponentiation (** operator) |
| 12470 | * - realize comma separated - expr, expr |
| 12471 | * - realise ++expr --expr expr++ expr-- |
| 12472 | * - realise expr ? expr : expr (but, second expr calculate always) |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12473 | * - allow hexadecimal and octal numbers |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12474 | * - was restored loses XOR operator |
| 12475 | * - remove one goto label, added three ;-) |
| 12476 | * - protect $((num num)) as true zero expr (Manuel`s error) |
| 12477 | * - always use special isspace(), see comment from bash ;-) |
| 12478 | */ |
| 12479 | |
| 12480 | |
| 12481 | #define arith_isspace(arithval) \ |
| 12482 | (arithval == ' ' || arithval == '\n' || arithval == '\t') |
| 12483 | |
| 12484 | |
| 12485 | typedef unsigned char operator; |
| 12486 | |
| 12487 | /* 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] | 12488 | * 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] | 12489 | * precedence. The ID portion is so that multiple operators can have the |
| 12490 | * same precedence, ensuring that the leftmost one is evaluated first. |
| 12491 | * Consider * and /. */ |
| 12492 | |
| 12493 | #define tok_decl(prec,id) (((id)<<5)|(prec)) |
| 12494 | #define PREC(op) ((op) & 0x1F) |
| 12495 | |
| 12496 | #define TOK_LPAREN tok_decl(0,0) |
| 12497 | |
| 12498 | #define TOK_COMMA tok_decl(1,0) |
| 12499 | |
| 12500 | #define TOK_ASSIGN tok_decl(2,0) |
| 12501 | #define TOK_AND_ASSIGN tok_decl(2,1) |
| 12502 | #define TOK_OR_ASSIGN tok_decl(2,2) |
| 12503 | #define TOK_XOR_ASSIGN tok_decl(2,3) |
| 12504 | #define TOK_PLUS_ASSIGN tok_decl(2,4) |
| 12505 | #define TOK_MINUS_ASSIGN tok_decl(2,5) |
| 12506 | #define TOK_LSHIFT_ASSIGN tok_decl(2,6) |
| 12507 | #define TOK_RSHIFT_ASSIGN tok_decl(2,7) |
| 12508 | |
| 12509 | #define TOK_MUL_ASSIGN tok_decl(3,0) |
| 12510 | #define TOK_DIV_ASSIGN tok_decl(3,1) |
| 12511 | #define TOK_REM_ASSIGN tok_decl(3,2) |
| 12512 | |
| 12513 | /* 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] | 12514 | #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] | 12515 | |
| 12516 | /* conditional is right associativity too */ |
| 12517 | #define TOK_CONDITIONAL tok_decl(4,0) |
| 12518 | #define TOK_CONDITIONAL_SEP tok_decl(4,1) |
| 12519 | |
| 12520 | #define TOK_OR tok_decl(5,0) |
| 12521 | |
| 12522 | #define TOK_AND tok_decl(6,0) |
| 12523 | |
| 12524 | #define TOK_BOR tok_decl(7,0) |
| 12525 | |
| 12526 | #define TOK_BXOR tok_decl(8,0) |
| 12527 | |
| 12528 | #define TOK_BAND tok_decl(9,0) |
| 12529 | |
| 12530 | #define TOK_EQ tok_decl(10,0) |
| 12531 | #define TOK_NE tok_decl(10,1) |
| 12532 | |
| 12533 | #define TOK_LT tok_decl(11,0) |
| 12534 | #define TOK_GT tok_decl(11,1) |
| 12535 | #define TOK_GE tok_decl(11,2) |
| 12536 | #define TOK_LE tok_decl(11,3) |
| 12537 | |
| 12538 | #define TOK_LSHIFT tok_decl(12,0) |
| 12539 | #define TOK_RSHIFT tok_decl(12,1) |
| 12540 | |
| 12541 | #define TOK_ADD tok_decl(13,0) |
| 12542 | #define TOK_SUB tok_decl(13,1) |
| 12543 | |
| 12544 | #define TOK_MUL tok_decl(14,0) |
| 12545 | #define TOK_DIV tok_decl(14,1) |
| 12546 | #define TOK_REM tok_decl(14,2) |
| 12547 | |
| 12548 | /* exponent is right associativity */ |
| 12549 | #define TOK_EXPONENT tok_decl(15,1) |
| 12550 | |
| 12551 | /* For now unary operators. */ |
| 12552 | #define UNARYPREC 16 |
| 12553 | #define TOK_BNOT tok_decl(UNARYPREC,0) |
| 12554 | #define TOK_NOT tok_decl(UNARYPREC,1) |
| 12555 | |
| 12556 | #define TOK_UMINUS tok_decl(UNARYPREC+1,0) |
| 12557 | #define TOK_UPLUS tok_decl(UNARYPREC+1,1) |
| 12558 | |
| 12559 | #define PREC_PRE (UNARYPREC+2) |
| 12560 | |
| 12561 | #define TOK_PRE_INC tok_decl(PREC_PRE, 0) |
| 12562 | #define TOK_PRE_DEC tok_decl(PREC_PRE, 1) |
| 12563 | |
| 12564 | #define PREC_POST (UNARYPREC+3) |
| 12565 | |
| 12566 | #define TOK_POST_INC tok_decl(PREC_POST, 0) |
| 12567 | #define TOK_POST_DEC tok_decl(PREC_POST, 1) |
| 12568 | |
| 12569 | #define SPEC_PREC (UNARYPREC+4) |
| 12570 | |
| 12571 | #define TOK_NUM tok_decl(SPEC_PREC, 0) |
| 12572 | #define TOK_RPAREN tok_decl(SPEC_PREC, 1) |
| 12573 | |
| 12574 | #define NUMPTR (*numstackptr) |
| 12575 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 12576 | static int tok_have_assign(operator op) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12577 | { |
| 12578 | operator prec = PREC(op); |
| 12579 | |
| 12580 | convert_prec_is_assing(prec); |
| 12581 | return (prec == PREC(TOK_ASSIGN) || |
| 12582 | prec == PREC_PRE || prec == PREC_POST); |
| 12583 | } |
| 12584 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 12585 | static int is_right_associativity(operator prec) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12586 | { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12587 | return (prec == PREC(TOK_ASSIGN) || prec == PREC(TOK_EXPONENT) |
| 12588 | || prec == PREC(TOK_CONDITIONAL)); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12589 | } |
| 12590 | |
| 12591 | |
| 12592 | typedef struct ARITCH_VAR_NUM { |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 12593 | arith_t val; |
| 12594 | arith_t contidional_second_val; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12595 | char contidional_second_val_initialized; |
| 12596 | char *var; /* if NULL then is regular number, |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12597 | else is variable name */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12598 | } v_n_t; |
| 12599 | |
| 12600 | |
| 12601 | typedef struct CHK_VAR_RECURSIVE_LOOPED { |
| 12602 | const char *var; |
| 12603 | struct CHK_VAR_RECURSIVE_LOOPED *next; |
| 12604 | } chk_var_recursive_looped_t; |
| 12605 | |
| 12606 | static chk_var_recursive_looped_t *prev_chk_var_recursive; |
| 12607 | |
| 12608 | |
| 12609 | static int arith_lookup_val(v_n_t *t) |
| 12610 | { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12611 | if (t->var) { |
| 12612 | const char * p = lookupvar(t->var); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12613 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12614 | if (p) { |
| 12615 | int errcode; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12616 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12617 | /* recursive try as expression */ |
| 12618 | chk_var_recursive_looped_t *cur; |
| 12619 | chk_var_recursive_looped_t cur_save; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12620 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12621 | for (cur = prev_chk_var_recursive; cur; cur = cur->next) { |
| 12622 | if (strcmp(cur->var, t->var) == 0) { |
| 12623 | /* expression recursion loop detected */ |
| 12624 | return -5; |
| 12625 | } |
| 12626 | } |
| 12627 | /* save current lookuped var name */ |
| 12628 | cur = prev_chk_var_recursive; |
| 12629 | cur_save.var = t->var; |
| 12630 | cur_save.next = cur; |
| 12631 | prev_chk_var_recursive = &cur_save; |
| 12632 | |
| 12633 | t->val = arith (p, &errcode); |
| 12634 | /* restore previous ptr after recursiving */ |
| 12635 | prev_chk_var_recursive = cur; |
| 12636 | return errcode; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12637 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12638 | /* allow undefined var as 0 */ |
| 12639 | t->val = 0; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12640 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12641 | return 0; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12642 | } |
| 12643 | |
| 12644 | /* "applying" a token means performing it on the top elements on the integer |
| 12645 | * stack. For a unary operator it will only change the top element, but a |
| 12646 | * binary operator will pop two arguments and push a result */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 12647 | static int arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12648 | { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12649 | v_n_t *numptr_m1; |
Eric Andersen | fac312d | 2004-06-22 20:09:40 +0000 | [diff] [blame] | 12650 | arith_t numptr_val, rez; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12651 | int ret_arith_lookup_val; |
| 12652 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12653 | /* There is no operator that can work without arguments */ |
| 12654 | if (NUMPTR == numstack) goto err; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12655 | numptr_m1 = NUMPTR - 1; |
| 12656 | |
| 12657 | /* check operand is var with noninteger value */ |
| 12658 | ret_arith_lookup_val = arith_lookup_val(numptr_m1); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12659 | if (ret_arith_lookup_val) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12660 | return ret_arith_lookup_val; |
| 12661 | |
| 12662 | rez = numptr_m1->val; |
| 12663 | if (op == TOK_UMINUS) |
| 12664 | rez *= -1; |
| 12665 | else if (op == TOK_NOT) |
| 12666 | rez = !rez; |
| 12667 | else if (op == TOK_BNOT) |
| 12668 | rez = ~rez; |
| 12669 | else if (op == TOK_POST_INC || op == TOK_PRE_INC) |
| 12670 | rez++; |
| 12671 | else if (op == TOK_POST_DEC || op == TOK_PRE_DEC) |
| 12672 | rez--; |
| 12673 | else if (op != TOK_UPLUS) { |
| 12674 | /* Binary operators */ |
| 12675 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12676 | /* check and binary operators need two arguments */ |
| 12677 | if (numptr_m1 == numstack) goto err; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12678 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12679 | /* ... and they pop one */ |
| 12680 | --NUMPTR; |
| 12681 | numptr_val = rez; |
| 12682 | if (op == TOK_CONDITIONAL) { |
| 12683 | if (! numptr_m1->contidional_second_val_initialized) { |
| 12684 | /* protect $((expr1 ? expr2)) without ": expr" */ |
| 12685 | goto err; |
| 12686 | } |
| 12687 | rez = numptr_m1->contidional_second_val; |
| 12688 | } else if (numptr_m1->contidional_second_val_initialized) { |
| 12689 | /* protect $((expr1 : expr2)) without "expr ? " */ |
| 12690 | goto err; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12691 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12692 | numptr_m1 = NUMPTR - 1; |
| 12693 | if (op != TOK_ASSIGN) { |
| 12694 | /* check operand is var with noninteger value for not '=' */ |
| 12695 | ret_arith_lookup_val = arith_lookup_val(numptr_m1); |
| 12696 | if (ret_arith_lookup_val) |
| 12697 | return ret_arith_lookup_val; |
| 12698 | } |
| 12699 | if (op == TOK_CONDITIONAL) { |
| 12700 | numptr_m1->contidional_second_val = rez; |
| 12701 | } |
| 12702 | rez = numptr_m1->val; |
| 12703 | if (op == TOK_BOR || op == TOK_OR_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12704 | rez |= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12705 | else if (op == TOK_OR) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12706 | rez = numptr_val || rez; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12707 | else if (op == TOK_BAND || op == TOK_AND_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12708 | rez &= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12709 | else if (op == TOK_BXOR || op == TOK_XOR_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12710 | rez ^= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12711 | else if (op == TOK_AND) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12712 | rez = rez && numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12713 | else if (op == TOK_EQ) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12714 | rez = (rez == numptr_val); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12715 | else if (op == TOK_NE) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12716 | rez = (rez != numptr_val); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12717 | else if (op == TOK_GE) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12718 | rez = (rez >= numptr_val); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12719 | else if (op == TOK_RSHIFT || op == TOK_RSHIFT_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12720 | rez >>= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12721 | else if (op == TOK_LSHIFT || op == TOK_LSHIFT_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12722 | rez <<= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12723 | else if (op == TOK_GT) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12724 | rez = (rez > numptr_val); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12725 | else if (op == TOK_LT) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12726 | rez = (rez < numptr_val); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12727 | else if (op == TOK_LE) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12728 | rez = (rez <= numptr_val); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12729 | else if (op == TOK_MUL || op == TOK_MUL_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12730 | rez *= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12731 | else if (op == TOK_ADD || op == TOK_PLUS_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12732 | rez += numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12733 | else if (op == TOK_SUB || op == TOK_MINUS_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12734 | rez -= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12735 | else if (op == TOK_ASSIGN || op == TOK_COMMA) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12736 | rez = numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12737 | else if (op == TOK_CONDITIONAL_SEP) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12738 | if (numptr_m1 == numstack) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12739 | /* protect $((expr : expr)) without "expr ? " */ |
| 12740 | goto err; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12741 | } |
| 12742 | numptr_m1->contidional_second_val_initialized = op; |
| 12743 | numptr_m1->contidional_second_val = numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12744 | } |
| 12745 | else if (op == TOK_CONDITIONAL) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12746 | rez = rez ? |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12747 | numptr_val : numptr_m1->contidional_second_val; |
| 12748 | } |
| 12749 | else if (op == TOK_EXPONENT) { |
| 12750 | if (numptr_val < 0) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12751 | return -3; /* exponent less than 0 */ |
| 12752 | else { |
Eric Andersen | ad63cb2 | 2004-10-08 09:43:34 +0000 | [diff] [blame] | 12753 | arith_t c = 1; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12754 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12755 | if (numptr_val) |
| 12756 | while (numptr_val--) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12757 | c *= rez; |
| 12758 | rez = c; |
| 12759 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12760 | } |
| 12761 | else if (numptr_val==0) /* zero divisor check */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12762 | return -2; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12763 | else if (op == TOK_DIV || op == TOK_DIV_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12764 | rez /= numptr_val; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12765 | else if (op == TOK_REM || op == TOK_REM_ASSIGN) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12766 | rez %= numptr_val; |
| 12767 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12768 | if (tok_have_assign(op)) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12769 | char buf[32]; |
| 12770 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12771 | if (numptr_m1->var == NULL) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12772 | /* Hmm, 1=2 ? */ |
| 12773 | goto err; |
| 12774 | } |
| 12775 | /* save to shell variable */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12776 | #if ENABLE_ASH_MATH_SUPPORT_64 |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 12777 | snprintf(buf, sizeof(buf), "%lld", arith_t_type rez); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 12778 | #else |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 12779 | snprintf(buf, sizeof(buf), "%ld", arith_t_type rez); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 12780 | #endif |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12781 | setvar(numptr_m1->var, buf, 0); |
| 12782 | /* after saving, make previous value for v++ or v-- */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12783 | if (op == TOK_POST_INC) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12784 | rez--; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12785 | else if (op == TOK_POST_DEC) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12786 | rez++; |
| 12787 | } |
| 12788 | numptr_m1->val = rez; |
| 12789 | /* protect geting var value, is number now */ |
| 12790 | numptr_m1->var = NULL; |
| 12791 | return 0; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 12792 | err: |
| 12793 | return -1; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12794 | } |
| 12795 | |
| 12796 | /* longest must first */ |
| 12797 | static const char op_tokens[] = { |
| 12798 | '<','<','=',0, TOK_LSHIFT_ASSIGN, |
| 12799 | '>','>','=',0, TOK_RSHIFT_ASSIGN, |
| 12800 | '<','<', 0, TOK_LSHIFT, |
| 12801 | '>','>', 0, TOK_RSHIFT, |
| 12802 | '|','|', 0, TOK_OR, |
| 12803 | '&','&', 0, TOK_AND, |
| 12804 | '!','=', 0, TOK_NE, |
| 12805 | '<','=', 0, TOK_LE, |
| 12806 | '>','=', 0, TOK_GE, |
| 12807 | '=','=', 0, TOK_EQ, |
| 12808 | '|','=', 0, TOK_OR_ASSIGN, |
| 12809 | '&','=', 0, TOK_AND_ASSIGN, |
| 12810 | '*','=', 0, TOK_MUL_ASSIGN, |
| 12811 | '/','=', 0, TOK_DIV_ASSIGN, |
| 12812 | '%','=', 0, TOK_REM_ASSIGN, |
| 12813 | '+','=', 0, TOK_PLUS_ASSIGN, |
| 12814 | '-','=', 0, TOK_MINUS_ASSIGN, |
| 12815 | '-','-', 0, TOK_POST_DEC, |
| 12816 | '^','=', 0, TOK_XOR_ASSIGN, |
| 12817 | '+','+', 0, TOK_POST_INC, |
| 12818 | '*','*', 0, TOK_EXPONENT, |
| 12819 | '!', 0, TOK_NOT, |
| 12820 | '<', 0, TOK_LT, |
| 12821 | '>', 0, TOK_GT, |
| 12822 | '=', 0, TOK_ASSIGN, |
| 12823 | '|', 0, TOK_BOR, |
| 12824 | '&', 0, TOK_BAND, |
| 12825 | '*', 0, TOK_MUL, |
| 12826 | '/', 0, TOK_DIV, |
| 12827 | '%', 0, TOK_REM, |
| 12828 | '+', 0, TOK_ADD, |
| 12829 | '-', 0, TOK_SUB, |
| 12830 | '^', 0, TOK_BXOR, |
| 12831 | /* uniq */ |
| 12832 | '~', 0, TOK_BNOT, |
| 12833 | ',', 0, TOK_COMMA, |
| 12834 | '?', 0, TOK_CONDITIONAL, |
| 12835 | ':', 0, TOK_CONDITIONAL_SEP, |
| 12836 | ')', 0, TOK_RPAREN, |
| 12837 | '(', 0, TOK_LPAREN, |
| 12838 | 0 |
| 12839 | }; |
| 12840 | /* ptr to ")" */ |
| 12841 | #define endexpression &op_tokens[sizeof(op_tokens)-7] |
| 12842 | |
| 12843 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 12844 | static arith_t arith(const char *expr, int *perrcode) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12845 | { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12846 | char arithval; /* Current character under analysis */ |
| 12847 | operator lasttok, op; |
| 12848 | operator prec; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12849 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12850 | const char *p = endexpression; |
| 12851 | int errcode; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12852 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12853 | size_t datasizes = strlen(expr) + 2; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12854 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12855 | /* Stack of integers */ |
| 12856 | /* The proof that there can be no more than strlen(startbuf)/2+1 integers |
| 12857 | * in any given correct or incorrect expression is left as an exercise to |
| 12858 | * the reader. */ |
| 12859 | v_n_t *numstack = alloca(((datasizes)/2)*sizeof(v_n_t)), |
| 12860 | *numstackptr = numstack; |
| 12861 | /* Stack of operator tokens */ |
| 12862 | operator *stack = alloca((datasizes) * sizeof(operator)), |
| 12863 | *stackptr = stack; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12864 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12865 | *stackptr++ = lasttok = TOK_LPAREN; /* start off with a left paren */ |
| 12866 | *perrcode = errcode = 0; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12867 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12868 | while (1) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12869 | arithval = *expr; |
| 12870 | if (arithval == 0) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12871 | if (p == endexpression) { |
| 12872 | /* Null expression. */ |
| 12873 | return 0; |
| 12874 | } |
| 12875 | |
| 12876 | /* This is only reached after all tokens have been extracted from the |
| 12877 | * input stream. If there are still tokens on the operator stack, they |
| 12878 | * are to be applied in order. At the end, there should be a final |
| 12879 | * result on the integer stack */ |
| 12880 | |
| 12881 | if (expr != endexpression + 1) { |
| 12882 | /* If we haven't done so already, */ |
| 12883 | /* append a closing right paren */ |
| 12884 | expr = endexpression; |
| 12885 | /* and let the loop process it. */ |
| 12886 | continue; |
| 12887 | } |
| 12888 | /* At this point, we're done with the expression. */ |
| 12889 | if (numstackptr != numstack+1) { |
| 12890 | /* ... but if there isn't, it's bad */ |
| 12891 | err: |
| 12892 | return (*perrcode = -1); |
| 12893 | } |
| 12894 | if (numstack->var) { |
| 12895 | /* expression is $((var)) only, lookup now */ |
| 12896 | errcode = arith_lookup_val(numstack); |
| 12897 | } |
| 12898 | ret: |
| 12899 | *perrcode = errcode; |
| 12900 | return numstack->val; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12901 | } |
| 12902 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12903 | /* Continue processing the expression. */ |
| 12904 | if (arith_isspace(arithval)) { |
| 12905 | /* Skip whitespace */ |
| 12906 | goto prologue; |
| 12907 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12908 | p = endofname(expr); |
| 12909 | if (p != expr) { |
Eric Andersen | ad63cb2 | 2004-10-08 09:43:34 +0000 | [diff] [blame] | 12910 | size_t var_name_size = (p-expr) + 1; /* trailing zero */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12911 | |
| 12912 | numstackptr->var = alloca(var_name_size); |
| 12913 | safe_strncpy(numstackptr->var, expr, var_name_size); |
| 12914 | expr = p; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12915 | num: |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12916 | numstackptr->contidional_second_val_initialized = 0; |
| 12917 | numstackptr++; |
| 12918 | lasttok = TOK_NUM; |
| 12919 | continue; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12920 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12921 | if (isdigit(arithval)) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12922 | numstackptr->var = NULL; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12923 | #if ENABLE_ASH_MATH_SUPPORT_64 |
Eric Andersen | ad63cb2 | 2004-10-08 09:43:34 +0000 | [diff] [blame] | 12924 | numstackptr->val = strtoll(expr, (char **) &expr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 12925 | #else |
| 12926 | numstackptr->val = strtol(expr, (char **) &expr, 0); |
| 12927 | #endif |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12928 | goto num; |
| 12929 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12930 | for (p = op_tokens; ; p++) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12931 | const char *o; |
| 12932 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12933 | if (*p == 0) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12934 | /* strange operator not found */ |
| 12935 | goto err; |
| 12936 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12937 | for (o = expr; *p && *o == *p; p++) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12938 | o++; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12939 | if (! *p) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12940 | /* found */ |
| 12941 | expr = o - 1; |
| 12942 | break; |
| 12943 | } |
| 12944 | /* skip tail uncompared token */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12945 | while (*p) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12946 | p++; |
| 12947 | /* skip zero delim */ |
| 12948 | p++; |
| 12949 | } |
| 12950 | op = p[1]; |
| 12951 | |
| 12952 | /* post grammar: a++ reduce to num */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12953 | if (lasttok == TOK_POST_INC || lasttok == TOK_POST_DEC) |
| 12954 | lasttok = TOK_NUM; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12955 | |
| 12956 | /* Plus and minus are binary (not unary) _only_ if the last |
| 12957 | * token was as number, or a right paren (which pretends to be |
| 12958 | * a number, since it evaluates to one). Think about it. |
| 12959 | * It makes sense. */ |
| 12960 | if (lasttok != TOK_NUM) { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 12961 | switch (op) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12962 | case TOK_ADD: |
| 12963 | op = TOK_UPLUS; |
| 12964 | break; |
| 12965 | case TOK_SUB: |
| 12966 | op = TOK_UMINUS; |
| 12967 | break; |
| 12968 | case TOK_POST_INC: |
| 12969 | op = TOK_PRE_INC; |
| 12970 | break; |
| 12971 | case TOK_POST_DEC: |
| 12972 | op = TOK_PRE_DEC; |
| 12973 | break; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12974 | } |
| 12975 | } |
| 12976 | /* We don't want a unary operator to cause recursive descent on the |
| 12977 | * stack, because there can be many in a row and it could cause an |
| 12978 | * operator to be evaluated before its argument is pushed onto the |
| 12979 | * integer stack. */ |
| 12980 | /* But for binary operators, "apply" everything on the operator |
| 12981 | * stack until we find an operator with a lesser priority than the |
| 12982 | * one we have just extracted. */ |
| 12983 | /* Left paren is given the lowest priority so it will never be |
| 12984 | * "applied" in this way. |
| 12985 | * if associativity is right and priority eq, applied also skip |
| 12986 | */ |
| 12987 | prec = PREC(op); |
| 12988 | if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) { |
| 12989 | /* not left paren or unary */ |
| 12990 | if (lasttok != TOK_NUM) { |
| 12991 | /* binary op must be preceded by a num */ |
| 12992 | goto err; |
| 12993 | } |
| 12994 | while (stackptr != stack) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12995 | if (op == TOK_RPAREN) { |
| 12996 | /* The algorithm employed here is simple: while we don't |
| 12997 | * hit an open paren nor the bottom of the stack, pop |
| 12998 | * tokens and apply them */ |
| 12999 | if (stackptr[-1] == TOK_LPAREN) { |
| 13000 | --stackptr; |
| 13001 | /* Any operator directly after a */ |
| 13002 | lasttok = TOK_NUM; |
| 13003 | /* close paren should consider itself binary */ |
| 13004 | goto prologue; |
| 13005 | } |
| 13006 | } else { |
| 13007 | operator prev_prec = PREC(stackptr[-1]); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13008 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13009 | convert_prec_is_assing(prec); |
| 13010 | convert_prec_is_assing(prev_prec); |
| 13011 | if (prev_prec < prec) |
| 13012 | break; |
| 13013 | /* check right assoc */ |
| 13014 | if (prev_prec == prec && is_right_associativity(prec)) |
| 13015 | break; |
| 13016 | } |
| 13017 | errcode = arith_apply(*--stackptr, numstack, &numstackptr); |
| 13018 | if (errcode) goto ret; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13019 | } |
| 13020 | if (op == TOK_RPAREN) { |
| 13021 | goto err; |
| 13022 | } |
| 13023 | } |
| 13024 | |
| 13025 | /* Push this operator to the stack and remember it. */ |
| 13026 | *stackptr++ = lasttok = op; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13027 | prologue: |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13028 | ++expr; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 13029 | } /* while */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13030 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 13031 | #endif /* ASH_MATH_SUPPORT */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13032 | |
| 13033 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13034 | /* ============ main() and helpers |
| 13035 | * |
| 13036 | * Main routine. We initialize things, parse the arguments, execute |
| 13037 | * profiles if we're a login shell, and then call cmdloop to execute |
| 13038 | * commands. The setjmp call sets up the location to jump to when an |
| 13039 | * exception occurs. When an exception occurs the variable "state" |
| 13040 | * is used to figure out how far we had gotten. |
| 13041 | */ |
| 13042 | |
| 13043 | static void init(void) |
| 13044 | { |
| 13045 | /* from input.c: */ |
| 13046 | basepf.nextc = basepf.buf = basebuf; |
| 13047 | |
| 13048 | /* from trap.c: */ |
| 13049 | signal(SIGCHLD, SIG_DFL); |
| 13050 | |
| 13051 | /* from var.c: */ |
| 13052 | { |
| 13053 | char **envp; |
| 13054 | char ppid[32]; |
| 13055 | const char *p; |
| 13056 | struct stat st1, st2; |
| 13057 | |
| 13058 | initvar(); |
| 13059 | for (envp = environ; envp && *envp; envp++) { |
| 13060 | if (strchr(*envp, '=')) { |
| 13061 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 13062 | } |
| 13063 | } |
| 13064 | |
| 13065 | snprintf(ppid, sizeof(ppid), "%d", (int) getppid()); |
| 13066 | setvar("PPID", ppid, 0); |
| 13067 | |
| 13068 | p = lookupvar("PWD"); |
| 13069 | if (p) |
| 13070 | if (*p != '/' || stat(p, &st1) || stat(".", &st2) |
| 13071 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) |
| 13072 | p = '\0'; |
| 13073 | setpwd(p, 0); |
| 13074 | } |
| 13075 | } |
| 13076 | |
| 13077 | /* |
| 13078 | * Process the shell command line arguments. |
| 13079 | */ |
| 13080 | static void |
| 13081 | procargs(int argc, char **argv) |
| 13082 | { |
| 13083 | int i; |
| 13084 | const char *xminusc; |
| 13085 | char **xargv; |
| 13086 | |
| 13087 | xargv = argv; |
| 13088 | arg0 = xargv[0]; |
| 13089 | if (argc > 0) |
| 13090 | xargv++; |
| 13091 | for (i = 0; i < NOPTS; i++) |
| 13092 | optlist[i] = 2; |
| 13093 | argptr = xargv; |
| 13094 | options(1); |
| 13095 | xargv = argptr; |
| 13096 | xminusc = minusc; |
| 13097 | if (*xargv == NULL) { |
| 13098 | if (xminusc) |
| 13099 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 13100 | sflag = 1; |
| 13101 | } |
| 13102 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 13103 | iflag = 1; |
| 13104 | if (mflag == 2) |
| 13105 | mflag = iflag; |
| 13106 | for (i = 0; i < NOPTS; i++) |
| 13107 | if (optlist[i] == 2) |
| 13108 | optlist[i] = 0; |
| 13109 | #if DEBUG == 2 |
| 13110 | debug = 1; |
| 13111 | #endif |
| 13112 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 13113 | if (xminusc) { |
| 13114 | minusc = *xargv++; |
| 13115 | if (*xargv) |
| 13116 | goto setarg0; |
| 13117 | } else if (!sflag) { |
| 13118 | setinputfile(*xargv, 0); |
| 13119 | setarg0: |
| 13120 | arg0 = *xargv++; |
| 13121 | commandname = arg0; |
| 13122 | } |
| 13123 | |
| 13124 | shellparam.p = xargv; |
| 13125 | #if ENABLE_ASH_GETOPTS |
| 13126 | shellparam.optind = 1; |
| 13127 | shellparam.optoff = -1; |
| 13128 | #endif |
| 13129 | /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ |
| 13130 | while (*xargv) { |
| 13131 | shellparam.nparam++; |
| 13132 | xargv++; |
| 13133 | } |
| 13134 | optschanged(); |
| 13135 | } |
| 13136 | |
| 13137 | /* |
| 13138 | * Read /etc/profile or .profile. |
| 13139 | */ |
| 13140 | static void |
| 13141 | read_profile(const char *name) |
| 13142 | { |
| 13143 | int skip; |
| 13144 | |
| 13145 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 13146 | return; |
| 13147 | skip = cmdloop(0); |
| 13148 | popfile(); |
| 13149 | if (skip) |
| 13150 | exitshell(); |
| 13151 | } |
| 13152 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13153 | /* |
| 13154 | * This routine is called when an error or an interrupt occurs in an |
| 13155 | * interactive shell and control is returned to the main command loop. |
| 13156 | */ |
| 13157 | static void |
| 13158 | reset(void) |
| 13159 | { |
| 13160 | /* from eval.c: */ |
| 13161 | evalskip = 0; |
| 13162 | loopnest = 0; |
| 13163 | /* from input.c: */ |
| 13164 | parselleft = parsenleft = 0; /* clear input buffer */ |
| 13165 | popallfiles(); |
| 13166 | /* from parser.c: */ |
| 13167 | tokpushback = 0; |
| 13168 | checkkwd = 0; |
| 13169 | /* from redir.c: */ |
| 13170 | clearredir(0); |
| 13171 | } |
| 13172 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13173 | #if PROFILE |
| 13174 | static short profile_buf[16384]; |
| 13175 | extern int etext(); |
| 13176 | #endif |
| 13177 | |
| 13178 | int ash_main(int argc, char **argv); |
| 13179 | int ash_main(int argc, char **argv) |
| 13180 | { |
| 13181 | char *shinit; |
| 13182 | volatile int state; |
| 13183 | struct jmploc jmploc; |
| 13184 | struct stackmark smark; |
| 13185 | |
| 13186 | #ifdef __GLIBC__ |
| 13187 | dash_errno = __errno_location(); |
| 13188 | #endif |
| 13189 | |
| 13190 | #if PROFILE |
| 13191 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 13192 | #endif |
| 13193 | |
| 13194 | #if ENABLE_FEATURE_EDITING |
| 13195 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
| 13196 | #endif |
| 13197 | state = 0; |
| 13198 | if (setjmp(jmploc.loc)) { |
| 13199 | int e; |
| 13200 | int s; |
| 13201 | |
| 13202 | reset(); |
| 13203 | |
| 13204 | e = exception; |
| 13205 | if (e == EXERROR) |
| 13206 | exitstatus = 2; |
| 13207 | s = state; |
| 13208 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) |
| 13209 | exitshell(); |
| 13210 | |
| 13211 | if (e == EXINT) { |
| 13212 | outcslow('\n', stderr); |
| 13213 | } |
| 13214 | popstackmark(&smark); |
| 13215 | FORCE_INT_ON; /* enable interrupts */ |
| 13216 | if (s == 1) |
| 13217 | goto state1; |
| 13218 | else if (s == 2) |
| 13219 | goto state2; |
| 13220 | else if (s == 3) |
| 13221 | goto state3; |
| 13222 | else |
| 13223 | goto state4; |
| 13224 | } |
| 13225 | exception_handler = &jmploc; |
| 13226 | #if DEBUG |
| 13227 | opentrace(); |
| 13228 | trputs("Shell args: "); |
| 13229 | trargs(argv); |
| 13230 | #endif |
| 13231 | rootpid = getpid(); |
| 13232 | |
| 13233 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 13234 | rseed = rootpid + time(NULL); |
| 13235 | #endif |
| 13236 | init(); |
| 13237 | setstackmark(&smark); |
| 13238 | procargs(argc, argv); |
| 13239 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
| 13240 | if (iflag) { |
| 13241 | const char *hp = lookupvar("HISTFILE"); |
| 13242 | |
| 13243 | if (hp == NULL) { |
| 13244 | hp = lookupvar("HOME"); |
| 13245 | if (hp != NULL) { |
| 13246 | char *defhp = concat_path_file(hp, ".ash_history"); |
| 13247 | setvar("HISTFILE", defhp, 0); |
| 13248 | free(defhp); |
| 13249 | } |
| 13250 | } |
| 13251 | } |
| 13252 | #endif |
| 13253 | if (argv[0] && argv[0][0] == '-') |
| 13254 | isloginsh = 1; |
| 13255 | if (isloginsh) { |
| 13256 | state = 1; |
| 13257 | read_profile("/etc/profile"); |
| 13258 | state1: |
| 13259 | state = 2; |
| 13260 | read_profile(".profile"); |
| 13261 | } |
| 13262 | state2: |
| 13263 | state = 3; |
| 13264 | if ( |
| 13265 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13266 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13267 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13268 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13269 | ) { |
| 13270 | shinit = lookupvar("ENV"); |
| 13271 | if (shinit != NULL && *shinit != '\0') { |
| 13272 | read_profile(shinit); |
| 13273 | } |
| 13274 | } |
| 13275 | state3: |
| 13276 | state = 4; |
| 13277 | if (minusc) |
| 13278 | evalstring(minusc, 0); |
| 13279 | |
| 13280 | if (sflag || minusc == NULL) { |
| 13281 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
| 13282 | if ( iflag ) { |
| 13283 | const char *hp = lookupvar("HISTFILE"); |
| 13284 | |
| 13285 | if (hp != NULL) |
| 13286 | line_input_state->hist_file = hp; |
| 13287 | } |
| 13288 | #endif |
| 13289 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 13290 | cmdloop(1); |
| 13291 | } |
| 13292 | #if PROFILE |
| 13293 | monitor(0); |
| 13294 | #endif |
| 13295 | #ifdef GPROF |
| 13296 | { |
| 13297 | extern void _mcleanup(void); |
| 13298 | _mcleanup(); |
| 13299 | } |
| 13300 | #endif |
| 13301 | exitshell(); |
| 13302 | /* NOTREACHED */ |
| 13303 | } |
| 13304 | |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 13305 | #if DEBUG |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 13306 | const char *applet_name = "debug stuff usage"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13307 | int main(int argc, char **argv) |
| 13308 | { |
| 13309 | return ash_main(argc, argv); |
| 13310 | } |
| 13311 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 13312 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13313 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13314 | /*- |
| 13315 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 13316 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13317 | * |
| 13318 | * This code is derived from software contributed to Berkeley by |
| 13319 | * Kenneth Almquist. |
| 13320 | * |
| 13321 | * Redistribution and use in source and binary forms, with or without |
| 13322 | * modification, are permitted provided that the following conditions |
| 13323 | * are met: |
| 13324 | * 1. Redistributions of source code must retain the above copyright |
| 13325 | * notice, this list of conditions and the following disclaimer. |
| 13326 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13327 | * notice, this list of conditions and the following disclaimer in the |
| 13328 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13329 | * 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] | 13330 | * may be used to endorse or promote products derived from this software |
| 13331 | * without specific prior written permission. |
| 13332 | * |
| 13333 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 13334 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13335 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 13336 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13337 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13338 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 13339 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13340 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 13341 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 13342 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 13343 | * SUCH DAMAGE. |
| 13344 | */ |