Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * ash shell port for busybox |
| 4 | * |
| 5 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7 | * |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 8 | * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au> |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 9 | * was re-ported from NetBSD and debianized. |
| 10 | * |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11 | * This code is derived from software contributed to Berkeley by |
| 12 | * Kenneth Almquist. |
| 13 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 14 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 15 | * |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 16 | * Original BSD copyright notice is retained at the end of this file. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 19 | /* |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 20 | * The following should be set to reflect the type of system you have: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 21 | * JOBS -> 1 if you have Berkeley job control, 0 otherwise. |
| 22 | * define SYSV if you are running under System V. |
| 23 | * define DEBUG=1 to compile in debugging ('set -o debug' to turn on) |
| 24 | * define DEBUG=2 to compile in and turn on debugging. |
| 25 | * |
| 26 | * When debugging is on, debugging info will be written to ./trace and |
| 27 | * a quit signal will generate a core dump. |
| 28 | */ |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 29 | #define DEBUG 0 |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 30 | /* Tweak debug output verbosity here */ |
| 31 | #define DEBUG_TIME 0 |
| 32 | #define DEBUG_PID 1 |
| 33 | #define DEBUG_SIG 1 |
| 34 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 35 | #define PROFILE 0 |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 36 | |
| 37 | #define IFS_BROKEN |
| 38 | |
| 39 | #define JOBS ENABLE_ASH_JOB_CONTROL |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 41 | #if DEBUG |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 42 | # ifndef _GNU_SOURCE |
| 43 | # define _GNU_SOURCE |
| 44 | # endif |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 45 | #endif |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 47 | #include "busybox.h" /* for applet_names */ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 48 | //TODO: pull in some .h and find out do we have SINGLE_APPLET_MAIN? |
| 49 | //#include "applet_tables.h" doesn't work |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 50 | #include <paths.h> |
| 51 | #include <setjmp.h> |
| 52 | #include <fnmatch.h> |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 53 | #include "math.h" |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 54 | |
| 55 | #if defined SINGLE_APPLET_MAIN |
| 56 | /* STANDALONE does not make sense, and won't compile */ |
| 57 | #undef CONFIG_FEATURE_SH_STANDALONE |
| 58 | #undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 59 | #undef IF_FEATURE_SH_STANDALONE |
| 60 | #undef IF_NOT_FEATURE_SH_STANDALONE(...) |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 61 | #define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 62 | #define IF_FEATURE_SH_STANDALONE(...) |
| 63 | #define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 66 | #ifndef PIPE_BUF |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 67 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 70 | #if defined(__uClinux__) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 71 | # error "Do not even bother, ash will not run on NOMMU machine" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 75 | /* ============ Hash table sizes. Configurable. */ |
| 76 | |
| 77 | #define VTABSIZE 39 |
| 78 | #define ATABSIZE 39 |
| 79 | #define CMDTABLESIZE 31 /* should be prime */ |
| 80 | |
| 81 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 82 | /* ============ Shell options */ |
| 83 | |
| 84 | static const char *const optletters_optnames[] = { |
| 85 | "e" "errexit", |
| 86 | "f" "noglob", |
| 87 | "I" "ignoreeof", |
| 88 | "i" "interactive", |
| 89 | "m" "monitor", |
| 90 | "n" "noexec", |
| 91 | "s" "stdin", |
| 92 | "x" "xtrace", |
| 93 | "v" "verbose", |
| 94 | "C" "noclobber", |
| 95 | "a" "allexport", |
| 96 | "b" "notify", |
| 97 | "u" "nounset", |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 98 | "\0" "vi" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 99 | #if DEBUG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 100 | ,"\0" "nolog" |
| 101 | ,"\0" "debug" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 102 | #endif |
| 103 | }; |
| 104 | |
| 105 | #define optletters(n) optletters_optnames[(n)][0] |
| 106 | #define optnames(n) (&optletters_optnames[(n)][1]) |
| 107 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 108 | enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 109 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 110 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 111 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 113 | static const char homestr[] ALIGN1 = "HOME"; |
| 114 | static const char snlfmt[] ALIGN1 = "%s\n"; |
| 115 | static const char illnum[] ALIGN1 = "Illegal number: %s"; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 116 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 117 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 118 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 119 | * jump locations. The global variable handler contains the location to |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 120 | * jump to when an exception occurs, and the global variable exception_type |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 121 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 122 | * exception handlers, the user should save the value of handler on entry |
| 123 | * to an inner scope, set handler to point to a jmploc structure for the |
| 124 | * inner scope, and restore handler on exit from the scope. |
| 125 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 126 | struct jmploc { |
| 127 | jmp_buf loc; |
| 128 | }; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 129 | |
| 130 | struct globals_misc { |
| 131 | /* pid of main shell */ |
| 132 | int rootpid; |
| 133 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 134 | int shlvl; |
| 135 | #define rootshell (!shlvl) |
| 136 | char *minusc; /* argument to -c option */ |
| 137 | |
| 138 | char *curdir; // = nullstr; /* current working directory */ |
| 139 | char *physdir; // = nullstr; /* physical working directory */ |
| 140 | |
| 141 | char *arg0; /* value of $0 */ |
| 142 | |
| 143 | struct jmploc *exception_handler; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 144 | |
| 145 | // disabled by vda: cannot understand how it was supposed to work - |
| 146 | // cannot fix bugs. That's why you have to explain your non-trivial designs! |
| 147 | // /* do we generate EXSIG events */ |
| 148 | // int exsig; /* counter */ |
| 149 | volatile int suppressint; /* counter */ |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 150 | // TODO: rename |
| 151 | // pendingsig -> pending_sig |
| 152 | // intpending -> pending_int |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 153 | volatile /*sig_atomic_t*/ smallint intpending; /* 1 = got SIGINT */ |
| 154 | /* last pending signal */ |
| 155 | volatile /*sig_atomic_t*/ smallint pendingsig; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 156 | smallint exception_type; /* kind of exception (0..5) */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 157 | /* exceptions */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 158 | #define EXINT 0 /* SIGINT received */ |
| 159 | #define EXERROR 1 /* a generic error */ |
| 160 | #define EXSHELLPROC 2 /* execute a shell procedure */ |
| 161 | #define EXEXEC 3 /* command execution failed */ |
| 162 | #define EXEXIT 4 /* exit the shell */ |
| 163 | #define EXSIG 5 /* trapped signal in wait(1) */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 164 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 165 | smallint isloginsh; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 166 | char nullstr[1]; /* zero length string */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 167 | |
| 168 | char optlist[NOPTS]; |
| 169 | #define eflag optlist[0] |
| 170 | #define fflag optlist[1] |
| 171 | #define Iflag optlist[2] |
| 172 | #define iflag optlist[3] |
| 173 | #define mflag optlist[4] |
| 174 | #define nflag optlist[5] |
| 175 | #define sflag optlist[6] |
| 176 | #define xflag optlist[7] |
| 177 | #define vflag optlist[8] |
| 178 | #define Cflag optlist[9] |
| 179 | #define aflag optlist[10] |
| 180 | #define bflag optlist[11] |
| 181 | #define uflag optlist[12] |
| 182 | #define viflag optlist[13] |
| 183 | #if DEBUG |
| 184 | #define nolog optlist[14] |
| 185 | #define debug optlist[15] |
| 186 | #endif |
| 187 | |
| 188 | /* trap handler commands */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 189 | /* |
| 190 | * Sigmode records the current value of the signal handlers for the various |
| 191 | * modes. A value of zero means that the current handler is not known. |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 192 | * S_HARD_IGN indicates that the signal was ignored on entry to the shell. |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 193 | */ |
| 194 | char sigmode[NSIG - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 195 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 196 | #define S_CATCH 2 /* signal is caught */ |
| 197 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 198 | #define S_HARD_IGN 4 /* signal is ignored permenantly */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 199 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 200 | /* indicates specified signal received */ |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 201 | uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 202 | char *trap[NSIG]; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 203 | |
| 204 | /* Rarely referenced stuff */ |
| 205 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 206 | /* Random number generators */ |
| 207 | int32_t random_galois_LFSR; /* Galois LFSR (fast but weak). signed! */ |
| 208 | uint32_t random_LCG; /* LCG (fast but weak) */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 209 | #endif |
| 210 | pid_t backgndpid; /* pid of last background process */ |
| 211 | smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 212 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 213 | extern struct globals_misc *const ash_ptr_to_globals_misc; |
| 214 | #define G_misc (*ash_ptr_to_globals_misc) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 215 | #define rootpid (G_misc.rootpid ) |
| 216 | #define shlvl (G_misc.shlvl ) |
| 217 | #define minusc (G_misc.minusc ) |
| 218 | #define curdir (G_misc.curdir ) |
| 219 | #define physdir (G_misc.physdir ) |
| 220 | #define arg0 (G_misc.arg0 ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 221 | #define exception_handler (G_misc.exception_handler) |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 222 | #define exception_type (G_misc.exception_type ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 223 | #define suppressint (G_misc.suppressint ) |
| 224 | #define intpending (G_misc.intpending ) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 225 | //#define exsig (G_misc.exsig ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 226 | #define pendingsig (G_misc.pendingsig ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 227 | #define isloginsh (G_misc.isloginsh ) |
| 228 | #define nullstr (G_misc.nullstr ) |
| 229 | #define optlist (G_misc.optlist ) |
| 230 | #define sigmode (G_misc.sigmode ) |
| 231 | #define gotsig (G_misc.gotsig ) |
| 232 | #define trap (G_misc.trap ) |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 233 | #define random_galois_LFSR (G_misc.random_galois_LFSR) |
| 234 | #define random_LCG (G_misc.random_LCG ) |
| 235 | #define backgndpid (G_misc.backgndpid ) |
| 236 | #define job_warning (G_misc.job_warning) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 237 | #define INIT_G_misc() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 238 | (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ |
| 239 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 240 | curdir = nullstr; \ |
| 241 | physdir = nullstr; \ |
| 242 | } while (0) |
| 243 | |
| 244 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 245 | /* ============ DEBUG */ |
| 246 | #if DEBUG |
| 247 | static void trace_printf(const char *fmt, ...); |
| 248 | static void trace_vprintf(const char *fmt, va_list va); |
| 249 | # define TRACE(param) trace_printf param |
| 250 | # define TRACEV(param) trace_vprintf param |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 251 | # define close(fd) do { \ |
| 252 | int dfd = (fd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 253 | if (close(dfd) < 0) \ |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame^] | 254 | bb_error_msg("bug on %d: closing %d(0x%x)", \ |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 255 | __LINE__, dfd, dfd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 256 | } while (0) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 257 | #else |
| 258 | # define TRACE(param) |
| 259 | # define TRACEV(param) |
| 260 | #endif |
| 261 | |
| 262 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 263 | /* ============ Utility functions */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 264 | #define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0) |
| 265 | |
| 266 | /* C99 say: "char" declaration may be signed or unsigned by default */ |
| 267 | #define signed_char2int(sc) ((int)(signed char)(sc)) |
| 268 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 269 | static int isdigit_str9(const char *str) |
| 270 | { |
| 271 | int maxlen = 9 + 1; /* max 9 digits: 999999999 */ |
| 272 | while (--maxlen && isdigit(*str)) |
| 273 | str++; |
| 274 | return (*str == '\0'); |
| 275 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 276 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 277 | |
| 278 | /* ============ Interrupts / exceptions */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 279 | /* |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 280 | * These macros allow the user to suspend the handling of interrupt signals |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 281 | * over a period of time. This is similar to SIGHOLD or to sigblock, but |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 282 | * much more efficient and portable. (But hacking the kernel is so much |
| 283 | * more fun than worrying about efficiency and portability. :-)) |
| 284 | */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 285 | #define INT_OFF do { \ |
| 286 | suppressint++; \ |
| 287 | xbarrier(); \ |
| 288 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 292 | * just do a longjmp to the exception handler. The type of exception is |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 293 | * stored in the global variable "exception_type". |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 294 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 295 | static void raise_exception(int) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 296 | static void |
| 297 | raise_exception(int e) |
| 298 | { |
| 299 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 300 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 301 | abort(); |
| 302 | #endif |
| 303 | INT_OFF; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 304 | exception_type = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 305 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 306 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 307 | #if DEBUG |
| 308 | #define raise_exception(e) do { \ |
| 309 | TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \ |
| 310 | raise_exception(e); \ |
| 311 | } while (0) |
| 312 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 313 | |
| 314 | /* |
| 315 | * Called from trap.c when a SIGINT is received. (If the user specifies |
| 316 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 317 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 318 | * are held using the INT_OFF macro. (The test for iflag is just |
| 319 | * defensive programming.) |
| 320 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 321 | static void raise_interrupt(void) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 322 | static void |
| 323 | raise_interrupt(void) |
| 324 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 325 | int ex_type; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 326 | |
| 327 | intpending = 0; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 328 | /* Signal is not automatically unmasked after it is raised, |
| 329 | * do it ourself - unmask all signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 330 | sigprocmask_allsigs(SIG_UNBLOCK); |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 331 | /* pendingsig = 0; - now done in onsig() */ |
| 332 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 333 | ex_type = EXSIG; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 334 | if (gotsig[SIGINT - 1] && !trap[SIGINT]) { |
| 335 | if (!(rootshell && iflag)) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 336 | /* Kill ourself with SIGINT */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 337 | signal(SIGINT, SIG_DFL); |
| 338 | raise(SIGINT); |
| 339 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 340 | ex_type = EXINT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 341 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 342 | raise_exception(ex_type); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 343 | /* NOTREACHED */ |
| 344 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 345 | #if DEBUG |
| 346 | #define raise_interrupt() do { \ |
| 347 | TRACE(("raising interrupt on line %d\n", __LINE__)); \ |
| 348 | raise_interrupt(); \ |
| 349 | } while (0) |
| 350 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 351 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 352 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 353 | int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 354 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 355 | xbarrier(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 356 | if (--suppressint == 0 && intpending) { |
| 357 | raise_interrupt(); |
| 358 | } |
| 359 | } |
| 360 | #define INT_ON int_on() |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 361 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 362 | force_int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 363 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 364 | xbarrier(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 365 | suppressint = 0; |
| 366 | if (intpending) |
| 367 | raise_interrupt(); |
| 368 | } |
| 369 | #define FORCE_INT_ON force_int_on() |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 370 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 371 | #define SAVE_INT(v) ((v) = suppressint) |
| 372 | |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 373 | #define RESTORE_INT(v) do { \ |
| 374 | xbarrier(); \ |
| 375 | suppressint = (v); \ |
| 376 | if (suppressint == 0 && intpending) \ |
| 377 | raise_interrupt(); \ |
| 378 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 379 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 380 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 381 | /* ============ Stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 382 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 383 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 384 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 385 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 386 | INT_OFF; |
| 387 | fputs(p, file); |
| 388 | INT_ON; |
| 389 | } |
| 390 | |
| 391 | static void |
| 392 | flush_stdout_stderr(void) |
| 393 | { |
| 394 | INT_OFF; |
| 395 | fflush(stdout); |
| 396 | fflush(stderr); |
| 397 | INT_ON; |
| 398 | } |
| 399 | |
| 400 | static void |
| 401 | flush_stderr(void) |
| 402 | { |
| 403 | INT_OFF; |
| 404 | fflush(stderr); |
| 405 | INT_ON; |
| 406 | } |
| 407 | |
| 408 | static void |
| 409 | outcslow(int c, FILE *dest) |
| 410 | { |
| 411 | INT_OFF; |
| 412 | putc(c, dest); |
| 413 | fflush(dest); |
| 414 | INT_ON; |
| 415 | } |
| 416 | |
| 417 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 418 | static int |
| 419 | out1fmt(const char *fmt, ...) |
| 420 | { |
| 421 | va_list ap; |
| 422 | int r; |
| 423 | |
| 424 | INT_OFF; |
| 425 | va_start(ap, fmt); |
| 426 | r = vprintf(fmt, ap); |
| 427 | va_end(ap); |
| 428 | INT_ON; |
| 429 | return r; |
| 430 | } |
| 431 | |
| 432 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 433 | static int |
| 434 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 435 | { |
| 436 | va_list ap; |
| 437 | int ret; |
| 438 | |
| 439 | va_start(ap, fmt); |
| 440 | INT_OFF; |
| 441 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 442 | va_end(ap); |
| 443 | INT_ON; |
| 444 | return ret; |
| 445 | } |
| 446 | |
| 447 | static void |
| 448 | out1str(const char *p) |
| 449 | { |
| 450 | outstr(p, stdout); |
| 451 | } |
| 452 | |
| 453 | static void |
| 454 | out2str(const char *p) |
| 455 | { |
| 456 | outstr(p, stderr); |
| 457 | flush_stderr(); |
| 458 | } |
| 459 | |
| 460 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 461 | /* ============ Parser structures */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 462 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 463 | /* control characters in argument strings */ |
| 464 | #define CTLESC '\201' /* escape next character */ |
| 465 | #define CTLVAR '\202' /* variable defn */ |
| 466 | #define CTLENDVAR '\203' |
| 467 | #define CTLBACKQ '\204' |
| 468 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ |
| 469 | /* CTLBACKQ | CTLQUOTE == '\205' */ |
| 470 | #define CTLARI '\206' /* arithmetic expression */ |
| 471 | #define CTLENDARI '\207' |
| 472 | #define CTLQUOTEMARK '\210' |
| 473 | |
| 474 | /* variable substitution byte (follows CTLVAR) */ |
| 475 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 476 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
| 477 | #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */ |
| 478 | |
| 479 | /* values of VSTYPE field */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 480 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 481 | #define VSMINUS 0x2 /* ${var-text} */ |
| 482 | #define VSPLUS 0x3 /* ${var+text} */ |
| 483 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 484 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 485 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 486 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 487 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 488 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 489 | #define VSLENGTH 0xa /* ${#var} */ |
| 490 | #if ENABLE_ASH_BASH_COMPAT |
| 491 | #define VSSUBSTR 0xc /* ${var:position:length} */ |
| 492 | #define VSREPLACE 0xd /* ${var/pattern/replacement} */ |
| 493 | #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */ |
| 494 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 495 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 496 | static const char dolatstr[] ALIGN1 = { |
| 497 | CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' |
| 498 | }; |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 499 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 500 | #define NCMD 0 |
| 501 | #define NPIPE 1 |
| 502 | #define NREDIR 2 |
| 503 | #define NBACKGND 3 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 504 | #define NSUBSHELL 4 |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 505 | #define NAND 5 |
| 506 | #define NOR 6 |
| 507 | #define NSEMI 7 |
| 508 | #define NIF 8 |
| 509 | #define NWHILE 9 |
| 510 | #define NUNTIL 10 |
| 511 | #define NFOR 11 |
| 512 | #define NCASE 12 |
| 513 | #define NCLIST 13 |
| 514 | #define NDEFUN 14 |
| 515 | #define NARG 15 |
| 516 | #define NTO 16 |
| 517 | #if ENABLE_ASH_BASH_COMPAT |
| 518 | #define NTO2 17 |
| 519 | #endif |
| 520 | #define NCLOBBER 18 |
| 521 | #define NFROM 19 |
| 522 | #define NFROMTO 20 |
| 523 | #define NAPPEND 21 |
| 524 | #define NTOFD 22 |
| 525 | #define NFROMFD 23 |
| 526 | #define NHERE 24 |
| 527 | #define NXHERE 25 |
| 528 | #define NNOT 26 |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 529 | #define N_NUMBER 27 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 530 | |
| 531 | union node; |
| 532 | |
| 533 | struct ncmd { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 534 | smallint type; /* Nxxxx */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 535 | union node *assign; |
| 536 | union node *args; |
| 537 | union node *redirect; |
| 538 | }; |
| 539 | |
| 540 | struct npipe { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 541 | smallint type; |
| 542 | smallint pipe_backgnd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 543 | struct nodelist *cmdlist; |
| 544 | }; |
| 545 | |
| 546 | struct nredir { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 547 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 548 | union node *n; |
| 549 | union node *redirect; |
| 550 | }; |
| 551 | |
| 552 | struct nbinary { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 553 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 554 | union node *ch1; |
| 555 | union node *ch2; |
| 556 | }; |
| 557 | |
| 558 | struct nif { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 559 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 560 | union node *test; |
| 561 | union node *ifpart; |
| 562 | union node *elsepart; |
| 563 | }; |
| 564 | |
| 565 | struct nfor { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 566 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 567 | union node *args; |
| 568 | union node *body; |
| 569 | char *var; |
| 570 | }; |
| 571 | |
| 572 | struct ncase { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 573 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 574 | union node *expr; |
| 575 | union node *cases; |
| 576 | }; |
| 577 | |
| 578 | struct nclist { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 579 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 580 | union node *next; |
| 581 | union node *pattern; |
| 582 | union node *body; |
| 583 | }; |
| 584 | |
| 585 | struct narg { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 586 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 587 | union node *next; |
| 588 | char *text; |
| 589 | struct nodelist *backquote; |
| 590 | }; |
| 591 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 592 | /* nfile and ndup layout must match! |
| 593 | * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight |
| 594 | * that it is actually NTO2 (>&file), and change its type. |
| 595 | */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 596 | struct nfile { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 597 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 598 | union node *next; |
| 599 | int fd; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 600 | int _unused_dupfd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 601 | union node *fname; |
| 602 | char *expfname; |
| 603 | }; |
| 604 | |
| 605 | struct ndup { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 606 | smallint type; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 607 | union node *next; |
| 608 | int fd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 609 | int dupfd; |
| 610 | union node *vname; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 611 | char *_unused_expfname; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 612 | }; |
| 613 | |
| 614 | struct nhere { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 615 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 616 | union node *next; |
| 617 | int fd; |
| 618 | union node *doc; |
| 619 | }; |
| 620 | |
| 621 | struct nnot { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 622 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 623 | union node *com; |
| 624 | }; |
| 625 | |
| 626 | union node { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 627 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 628 | struct ncmd ncmd; |
| 629 | struct npipe npipe; |
| 630 | struct nredir nredir; |
| 631 | struct nbinary nbinary; |
| 632 | struct nif nif; |
| 633 | struct nfor nfor; |
| 634 | struct ncase ncase; |
| 635 | struct nclist nclist; |
| 636 | struct narg narg; |
| 637 | struct nfile nfile; |
| 638 | struct ndup ndup; |
| 639 | struct nhere nhere; |
| 640 | struct nnot nnot; |
| 641 | }; |
| 642 | |
| 643 | struct nodelist { |
| 644 | struct nodelist *next; |
| 645 | union node *n; |
| 646 | }; |
| 647 | |
| 648 | struct funcnode { |
| 649 | int count; |
| 650 | union node n; |
| 651 | }; |
| 652 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 653 | /* |
| 654 | * Free a parse tree. |
| 655 | */ |
| 656 | static void |
| 657 | freefunc(struct funcnode *f) |
| 658 | { |
| 659 | if (f && --f->count < 0) |
| 660 | free(f); |
| 661 | } |
| 662 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 663 | |
| 664 | /* ============ Debugging output */ |
| 665 | |
| 666 | #if DEBUG |
| 667 | |
| 668 | static FILE *tracefile; |
| 669 | |
| 670 | static void |
| 671 | trace_printf(const char *fmt, ...) |
| 672 | { |
| 673 | va_list va; |
| 674 | |
| 675 | if (debug != 1) |
| 676 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 677 | if (DEBUG_TIME) |
| 678 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 679 | if (DEBUG_PID) |
| 680 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 681 | if (DEBUG_SIG) |
| 682 | fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pendingsig, intpending, suppressint); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 683 | va_start(va, fmt); |
| 684 | vfprintf(tracefile, fmt, va); |
| 685 | va_end(va); |
| 686 | } |
| 687 | |
| 688 | static void |
| 689 | trace_vprintf(const char *fmt, va_list va) |
| 690 | { |
| 691 | if (debug != 1) |
| 692 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 693 | if (DEBUG_TIME) |
| 694 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 695 | if (DEBUG_PID) |
| 696 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 697 | if (DEBUG_SIG) |
| 698 | fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pendingsig, intpending, suppressint); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 699 | vfprintf(tracefile, fmt, va); |
| 700 | } |
| 701 | |
| 702 | static void |
| 703 | trace_puts(const char *s) |
| 704 | { |
| 705 | if (debug != 1) |
| 706 | return; |
| 707 | fputs(s, tracefile); |
| 708 | } |
| 709 | |
| 710 | static void |
| 711 | trace_puts_quoted(char *s) |
| 712 | { |
| 713 | char *p; |
| 714 | char c; |
| 715 | |
| 716 | if (debug != 1) |
| 717 | return; |
| 718 | putc('"', tracefile); |
| 719 | for (p = s; *p; p++) { |
| 720 | switch (*p) { |
| 721 | case '\n': c = 'n'; goto backslash; |
| 722 | case '\t': c = 't'; goto backslash; |
| 723 | case '\r': c = 'r'; goto backslash; |
| 724 | case '"': c = '"'; goto backslash; |
| 725 | case '\\': c = '\\'; goto backslash; |
| 726 | case CTLESC: c = 'e'; goto backslash; |
| 727 | case CTLVAR: c = 'v'; goto backslash; |
| 728 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash; |
| 729 | case CTLBACKQ: c = 'q'; goto backslash; |
| 730 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash; |
| 731 | backslash: |
| 732 | putc('\\', tracefile); |
| 733 | putc(c, tracefile); |
| 734 | break; |
| 735 | default: |
| 736 | if (*p >= ' ' && *p <= '~') |
| 737 | putc(*p, tracefile); |
| 738 | else { |
| 739 | putc('\\', tracefile); |
| 740 | putc(*p >> 6 & 03, tracefile); |
| 741 | putc(*p >> 3 & 07, tracefile); |
| 742 | putc(*p & 07, tracefile); |
| 743 | } |
| 744 | break; |
| 745 | } |
| 746 | } |
| 747 | putc('"', tracefile); |
| 748 | } |
| 749 | |
| 750 | static void |
| 751 | trace_puts_args(char **ap) |
| 752 | { |
| 753 | if (debug != 1) |
| 754 | return; |
| 755 | if (!*ap) |
| 756 | return; |
| 757 | while (1) { |
| 758 | trace_puts_quoted(*ap); |
| 759 | if (!*++ap) { |
| 760 | putc('\n', tracefile); |
| 761 | break; |
| 762 | } |
| 763 | putc(' ', tracefile); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | static void |
| 768 | opentrace(void) |
| 769 | { |
| 770 | char s[100]; |
| 771 | #ifdef O_APPEND |
| 772 | int flags; |
| 773 | #endif |
| 774 | |
| 775 | if (debug != 1) { |
| 776 | if (tracefile) |
| 777 | fflush(tracefile); |
| 778 | /* leave open because libedit might be using it */ |
| 779 | return; |
| 780 | } |
| 781 | strcpy(s, "./trace"); |
| 782 | if (tracefile) { |
| 783 | if (!freopen(s, "a", tracefile)) { |
| 784 | fprintf(stderr, "Can't re-open %s\n", s); |
| 785 | debug = 0; |
| 786 | return; |
| 787 | } |
| 788 | } else { |
| 789 | tracefile = fopen(s, "a"); |
| 790 | if (tracefile == NULL) { |
| 791 | fprintf(stderr, "Can't open %s\n", s); |
| 792 | debug = 0; |
| 793 | return; |
| 794 | } |
| 795 | } |
| 796 | #ifdef O_APPEND |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 797 | flags = fcntl(fileno(tracefile), F_GETFL); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 798 | if (flags >= 0) |
| 799 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 800 | #endif |
| 801 | setlinebuf(tracefile); |
| 802 | fputs("\nTracing started.\n", tracefile); |
| 803 | } |
| 804 | |
| 805 | static void |
| 806 | indent(int amount, char *pfx, FILE *fp) |
| 807 | { |
| 808 | int i; |
| 809 | |
| 810 | for (i = 0; i < amount; i++) { |
| 811 | if (pfx && i == amount - 1) |
| 812 | fputs(pfx, fp); |
| 813 | putc('\t', fp); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /* little circular references here... */ |
| 818 | static void shtree(union node *n, int ind, char *pfx, FILE *fp); |
| 819 | |
| 820 | static void |
| 821 | sharg(union node *arg, FILE *fp) |
| 822 | { |
| 823 | char *p; |
| 824 | struct nodelist *bqlist; |
| 825 | int subtype; |
| 826 | |
| 827 | if (arg->type != NARG) { |
| 828 | out1fmt("<node type %d>\n", arg->type); |
| 829 | abort(); |
| 830 | } |
| 831 | bqlist = arg->narg.backquote; |
| 832 | for (p = arg->narg.text; *p; p++) { |
| 833 | switch (*p) { |
| 834 | case CTLESC: |
| 835 | putc(*++p, fp); |
| 836 | break; |
| 837 | case CTLVAR: |
| 838 | putc('$', fp); |
| 839 | putc('{', fp); |
| 840 | subtype = *++p; |
| 841 | if (subtype == VSLENGTH) |
| 842 | putc('#', fp); |
| 843 | |
| 844 | while (*p != '=') |
| 845 | putc(*p++, fp); |
| 846 | |
| 847 | if (subtype & VSNUL) |
| 848 | putc(':', fp); |
| 849 | |
| 850 | switch (subtype & VSTYPE) { |
| 851 | case VSNORMAL: |
| 852 | putc('}', fp); |
| 853 | break; |
| 854 | case VSMINUS: |
| 855 | putc('-', fp); |
| 856 | break; |
| 857 | case VSPLUS: |
| 858 | putc('+', fp); |
| 859 | break; |
| 860 | case VSQUESTION: |
| 861 | putc('?', fp); |
| 862 | break; |
| 863 | case VSASSIGN: |
| 864 | putc('=', fp); |
| 865 | break; |
| 866 | case VSTRIMLEFT: |
| 867 | putc('#', fp); |
| 868 | break; |
| 869 | case VSTRIMLEFTMAX: |
| 870 | putc('#', fp); |
| 871 | putc('#', fp); |
| 872 | break; |
| 873 | case VSTRIMRIGHT: |
| 874 | putc('%', fp); |
| 875 | break; |
| 876 | case VSTRIMRIGHTMAX: |
| 877 | putc('%', fp); |
| 878 | putc('%', fp); |
| 879 | break; |
| 880 | case VSLENGTH: |
| 881 | break; |
| 882 | default: |
| 883 | out1fmt("<subtype %d>", subtype); |
| 884 | } |
| 885 | break; |
| 886 | case CTLENDVAR: |
| 887 | putc('}', fp); |
| 888 | break; |
| 889 | case CTLBACKQ: |
| 890 | case CTLBACKQ|CTLQUOTE: |
| 891 | putc('$', fp); |
| 892 | putc('(', fp); |
| 893 | shtree(bqlist->n, -1, NULL, fp); |
| 894 | putc(')', fp); |
| 895 | break; |
| 896 | default: |
| 897 | putc(*p, fp); |
| 898 | break; |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 903 | static void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 904 | shcmd(union node *cmd, FILE *fp) |
| 905 | { |
| 906 | union node *np; |
| 907 | int first; |
| 908 | const char *s; |
| 909 | int dftfd; |
| 910 | |
| 911 | first = 1; |
| 912 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 913 | if (!first) |
| 914 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 915 | sharg(np, fp); |
| 916 | first = 0; |
| 917 | } |
| 918 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 919 | if (!first) |
| 920 | putc(' ', fp); |
| 921 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 922 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 923 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 924 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 925 | case NAPPEND: s = ">>"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 926 | #if ENABLE_ASH_BASH_COMPAT |
| 927 | case NTO2: |
| 928 | #endif |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 929 | case NTOFD: s = ">&"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 930 | case NFROM: s = "<"; break; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 931 | case NFROMFD: s = "<&"; break; |
| 932 | case NFROMTO: s = "<>"; break; |
| 933 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 934 | } |
| 935 | if (np->nfile.fd != dftfd) |
| 936 | fprintf(fp, "%d", np->nfile.fd); |
| 937 | fputs(s, fp); |
| 938 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 939 | fprintf(fp, "%d", np->ndup.dupfd); |
| 940 | } else { |
| 941 | sharg(np->nfile.fname, fp); |
| 942 | } |
| 943 | first = 0; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | static void |
| 948 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 949 | { |
| 950 | struct nodelist *lp; |
| 951 | const char *s; |
| 952 | |
| 953 | if (n == NULL) |
| 954 | return; |
| 955 | |
| 956 | indent(ind, pfx, fp); |
| 957 | switch (n->type) { |
| 958 | case NSEMI: |
| 959 | s = "; "; |
| 960 | goto binop; |
| 961 | case NAND: |
| 962 | s = " && "; |
| 963 | goto binop; |
| 964 | case NOR: |
| 965 | s = " || "; |
| 966 | binop: |
| 967 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 968 | /* if (ind < 0) */ |
| 969 | fputs(s, fp); |
| 970 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 971 | break; |
| 972 | case NCMD: |
| 973 | shcmd(n, fp); |
| 974 | if (ind >= 0) |
| 975 | putc('\n', fp); |
| 976 | break; |
| 977 | case NPIPE: |
| 978 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
| 979 | shcmd(lp->n, fp); |
| 980 | if (lp->next) |
| 981 | fputs(" | ", fp); |
| 982 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 983 | if (n->npipe.pipe_backgnd) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 984 | fputs(" &", fp); |
| 985 | if (ind >= 0) |
| 986 | putc('\n', fp); |
| 987 | break; |
| 988 | default: |
| 989 | fprintf(fp, "<node type %d>", n->type); |
| 990 | if (ind >= 0) |
| 991 | putc('\n', fp); |
| 992 | break; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | static void |
| 997 | showtree(union node *n) |
| 998 | { |
| 999 | trace_puts("showtree called\n"); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame^] | 1000 | shtree(n, 1, NULL, stderr); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1003 | #endif /* DEBUG */ |
| 1004 | |
| 1005 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1006 | /* ============ Parser data */ |
| 1007 | |
| 1008 | /* |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1009 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 1010 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1011 | struct strlist { |
| 1012 | struct strlist *next; |
| 1013 | char *text; |
| 1014 | }; |
| 1015 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1016 | struct alias; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1017 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1018 | struct strpush { |
| 1019 | struct strpush *prev; /* preceding string on stack */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1020 | char *prev_string; |
| 1021 | int prev_left_in_line; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1022 | #if ENABLE_ASH_ALIAS |
| 1023 | struct alias *ap; /* if push was associated with an alias */ |
| 1024 | #endif |
| 1025 | char *string; /* remember the string since it may change */ |
| 1026 | }; |
| 1027 | |
| 1028 | struct parsefile { |
| 1029 | struct parsefile *prev; /* preceding file on stack */ |
| 1030 | int linno; /* current line */ |
| 1031 | int fd; /* file descriptor (or -1 if string) */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1032 | int left_in_line; /* number of chars left in this line */ |
| 1033 | int left_in_buffer; /* number of chars left in this buffer past the line */ |
| 1034 | char *next_to_pgetc; /* next char in buffer */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1035 | char *buf; /* input buffer */ |
| 1036 | struct strpush *strpush; /* for pushing strings at this level */ |
| 1037 | struct strpush basestrpush; /* so pushing one is fast */ |
| 1038 | }; |
| 1039 | |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1040 | static struct parsefile basepf; /* top level input file */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1041 | static struct parsefile *g_parsefile = &basepf; /* current input file */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1042 | static int startlinno; /* line # where last token started */ |
| 1043 | static char *commandname; /* currently executing command */ |
| 1044 | static struct strlist *cmdenviron; /* environment for builtin command */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1045 | static uint8_t exitstatus; /* exit status of last command */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1046 | |
| 1047 | |
| 1048 | /* ============ Message printing */ |
| 1049 | |
| 1050 | static void |
| 1051 | ash_vmsg(const char *msg, va_list ap) |
| 1052 | { |
| 1053 | fprintf(stderr, "%s: ", arg0); |
| 1054 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1055 | if (strcmp(arg0, commandname)) |
| 1056 | fprintf(stderr, "%s: ", commandname); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1057 | if (!iflag || g_parsefile->fd) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1058 | fprintf(stderr, "line %d: ", startlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1059 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1060 | vfprintf(stderr, msg, ap); |
| 1061 | outcslow('\n', stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1062 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1063 | |
| 1064 | /* |
| 1065 | * Exverror is called to raise the error exception. If the second argument |
| 1066 | * is not NULL then error prints an error message using printf style |
| 1067 | * formatting. It then raises the error exception. |
| 1068 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1069 | static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1070 | static void |
| 1071 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1072 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1073 | #if DEBUG |
| 1074 | if (msg) { |
| 1075 | TRACE(("ash_vmsg_and_raise(%d, \"", cond)); |
| 1076 | TRACEV((msg, ap)); |
| 1077 | TRACE(("\") pid=%d\n", getpid())); |
| 1078 | } else |
| 1079 | TRACE(("ash_vmsg_and_raise(%d, NULL) pid=%d\n", cond, getpid())); |
| 1080 | if (msg) |
| 1081 | #endif |
| 1082 | ash_vmsg(msg, ap); |
| 1083 | |
| 1084 | flush_stdout_stderr(); |
| 1085 | raise_exception(cond); |
| 1086 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1087 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1088 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1089 | static void ash_msg_and_raise_error(const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1090 | static void |
| 1091 | ash_msg_and_raise_error(const char *msg, ...) |
| 1092 | { |
| 1093 | va_list ap; |
| 1094 | |
| 1095 | va_start(ap, msg); |
| 1096 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1097 | /* NOTREACHED */ |
| 1098 | va_end(ap); |
| 1099 | } |
| 1100 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1101 | static void raise_error_syntax(const char *) NORETURN; |
| 1102 | static void |
| 1103 | raise_error_syntax(const char *msg) |
| 1104 | { |
| 1105 | ash_msg_and_raise_error("syntax error: %s", msg); |
| 1106 | /* NOTREACHED */ |
| 1107 | } |
| 1108 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1109 | static void ash_msg_and_raise(int, const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1110 | static void |
| 1111 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1112 | { |
| 1113 | va_list ap; |
| 1114 | |
| 1115 | va_start(ap, msg); |
| 1116 | ash_vmsg_and_raise(cond, msg, ap); |
| 1117 | /* NOTREACHED */ |
| 1118 | va_end(ap); |
| 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * error/warning routines for external builtins |
| 1123 | */ |
| 1124 | static void |
| 1125 | ash_msg(const char *fmt, ...) |
| 1126 | { |
| 1127 | va_list ap; |
| 1128 | |
| 1129 | va_start(ap, fmt); |
| 1130 | ash_vmsg(fmt, ap); |
| 1131 | va_end(ap); |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * Return a string describing an error. The returned string may be a |
| 1136 | * pointer to a static buffer that will be overwritten on the next call. |
| 1137 | * Action describes the operation that got the error. |
| 1138 | */ |
| 1139 | static const char * |
| 1140 | errmsg(int e, const char *em) |
| 1141 | { |
| 1142 | if (e == ENOENT || e == ENOTDIR) { |
| 1143 | return em; |
| 1144 | } |
| 1145 | return strerror(e); |
| 1146 | } |
| 1147 | |
| 1148 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1149 | /* ============ Memory allocation */ |
| 1150 | |
| 1151 | /* |
| 1152 | * It appears that grabstackstr() will barf with such alignments |
| 1153 | * because stalloc() will return a string allocated in a new stackblock. |
| 1154 | */ |
| 1155 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1156 | enum { |
| 1157 | /* Most machines require the value returned from malloc to be aligned |
| 1158 | * in some way. The following macro will get this right |
| 1159 | * on many machines. */ |
| 1160 | SHELL_SIZE = sizeof(union {int i; char *cp; double d; }) - 1, |
| 1161 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1162 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1163 | }; |
| 1164 | |
| 1165 | struct stack_block { |
| 1166 | struct stack_block *prev; |
| 1167 | char space[MINSIZE]; |
| 1168 | }; |
| 1169 | |
| 1170 | struct stackmark { |
| 1171 | struct stack_block *stackp; |
| 1172 | char *stacknxt; |
| 1173 | size_t stacknleft; |
| 1174 | struct stackmark *marknext; |
| 1175 | }; |
| 1176 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1177 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1178 | struct globals_memstack { |
| 1179 | struct stack_block *g_stackp; // = &stackbase; |
| 1180 | struct stackmark *markp; |
| 1181 | char *g_stacknxt; // = stackbase.space; |
| 1182 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1183 | size_t g_stacknleft; // = MINSIZE; |
| 1184 | int herefd; // = -1; |
| 1185 | struct stack_block stackbase; |
| 1186 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1187 | extern struct globals_memstack *const ash_ptr_to_globals_memstack; |
| 1188 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1189 | #define g_stackp (G_memstack.g_stackp ) |
| 1190 | #define markp (G_memstack.markp ) |
| 1191 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1192 | #define sstrend (G_memstack.sstrend ) |
| 1193 | #define g_stacknleft (G_memstack.g_stacknleft) |
| 1194 | #define herefd (G_memstack.herefd ) |
| 1195 | #define stackbase (G_memstack.stackbase ) |
| 1196 | #define INIT_G_memstack() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1197 | (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \ |
| 1198 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1199 | g_stackp = &stackbase; \ |
| 1200 | g_stacknxt = stackbase.space; \ |
| 1201 | g_stacknleft = MINSIZE; \ |
| 1202 | sstrend = stackbase.space + MINSIZE; \ |
| 1203 | herefd = -1; \ |
| 1204 | } while (0) |
| 1205 | |
| 1206 | #define stackblock() ((void *)g_stacknxt) |
| 1207 | #define stackblocksize() g_stacknleft |
| 1208 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1209 | |
| 1210 | static void * |
| 1211 | ckrealloc(void * p, size_t nbytes) |
| 1212 | { |
| 1213 | p = realloc(p, nbytes); |
| 1214 | if (!p) |
| 1215 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1216 | return p; |
| 1217 | } |
| 1218 | |
| 1219 | static void * |
| 1220 | ckmalloc(size_t nbytes) |
| 1221 | { |
| 1222 | return ckrealloc(NULL, nbytes); |
| 1223 | } |
| 1224 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1225 | static void * |
| 1226 | ckzalloc(size_t nbytes) |
| 1227 | { |
| 1228 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1229 | } |
| 1230 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1231 | /* |
| 1232 | * Make a copy of a string in safe storage. |
| 1233 | */ |
| 1234 | static char * |
| 1235 | ckstrdup(const char *s) |
| 1236 | { |
| 1237 | char *p = strdup(s); |
| 1238 | if (!p) |
| 1239 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1240 | return p; |
| 1241 | } |
| 1242 | |
| 1243 | /* |
| 1244 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1245 | * to make this more efficient, and also to avoid all sorts of exception |
| 1246 | * handling code to handle interrupts in the middle of a parse. |
| 1247 | * |
| 1248 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1249 | * well. |
| 1250 | */ |
| 1251 | static void * |
| 1252 | stalloc(size_t nbytes) |
| 1253 | { |
| 1254 | char *p; |
| 1255 | size_t aligned; |
| 1256 | |
| 1257 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1258 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1259 | size_t len; |
| 1260 | size_t blocksize; |
| 1261 | struct stack_block *sp; |
| 1262 | |
| 1263 | blocksize = aligned; |
| 1264 | if (blocksize < MINSIZE) |
| 1265 | blocksize = MINSIZE; |
| 1266 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1267 | if (len < blocksize) |
| 1268 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1269 | INT_OFF; |
| 1270 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1271 | sp->prev = g_stackp; |
| 1272 | g_stacknxt = sp->space; |
| 1273 | g_stacknleft = blocksize; |
| 1274 | sstrend = g_stacknxt + blocksize; |
| 1275 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1276 | INT_ON; |
| 1277 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1278 | p = g_stacknxt; |
| 1279 | g_stacknxt += aligned; |
| 1280 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1281 | return p; |
| 1282 | } |
| 1283 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1284 | static void * |
| 1285 | stzalloc(size_t nbytes) |
| 1286 | { |
| 1287 | return memset(stalloc(nbytes), 0, nbytes); |
| 1288 | } |
| 1289 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1290 | static void |
| 1291 | stunalloc(void *p) |
| 1292 | { |
| 1293 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1294 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1295 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1296 | abort(); |
| 1297 | } |
| 1298 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1299 | g_stacknleft += g_stacknxt - (char *)p; |
| 1300 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1303 | /* |
| 1304 | * Like strdup but works with the ash stack. |
| 1305 | */ |
| 1306 | static char * |
| 1307 | ststrdup(const char *p) |
| 1308 | { |
| 1309 | size_t len = strlen(p) + 1; |
| 1310 | return memcpy(stalloc(len), p, len); |
| 1311 | } |
| 1312 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1313 | static void |
| 1314 | setstackmark(struct stackmark *mark) |
| 1315 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1316 | mark->stackp = g_stackp; |
| 1317 | mark->stacknxt = g_stacknxt; |
| 1318 | mark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1319 | mark->marknext = markp; |
| 1320 | markp = mark; |
| 1321 | } |
| 1322 | |
| 1323 | static void |
| 1324 | popstackmark(struct stackmark *mark) |
| 1325 | { |
| 1326 | struct stack_block *sp; |
| 1327 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1328 | if (!mark->stackp) |
| 1329 | return; |
| 1330 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1331 | INT_OFF; |
| 1332 | markp = mark->marknext; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1333 | while (g_stackp != mark->stackp) { |
| 1334 | sp = g_stackp; |
| 1335 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1336 | free(sp); |
| 1337 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1338 | g_stacknxt = mark->stacknxt; |
| 1339 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1340 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1341 | INT_ON; |
| 1342 | } |
| 1343 | |
| 1344 | /* |
| 1345 | * When the parser reads in a string, it wants to stick the string on the |
| 1346 | * stack and only adjust the stack pointer when it knows how big the |
| 1347 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1348 | * of space on top of the stack and stackblocklen returns the length of |
| 1349 | * this block. Growstackblock will grow this space by at least one byte, |
| 1350 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1351 | * part of the block that has been used. |
| 1352 | */ |
| 1353 | static void |
| 1354 | growstackblock(void) |
| 1355 | { |
| 1356 | size_t newlen; |
| 1357 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1358 | newlen = g_stacknleft * 2; |
| 1359 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1360 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1361 | if (newlen < 128) |
| 1362 | newlen += 128; |
| 1363 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1364 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1365 | struct stack_block *oldstackp; |
| 1366 | struct stackmark *xmark; |
| 1367 | struct stack_block *sp; |
| 1368 | struct stack_block *prevstackp; |
| 1369 | size_t grosslen; |
| 1370 | |
| 1371 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1372 | oldstackp = g_stackp; |
| 1373 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1374 | prevstackp = sp->prev; |
| 1375 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1376 | sp = ckrealloc(sp, grosslen); |
| 1377 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1378 | g_stackp = sp; |
| 1379 | g_stacknxt = sp->space; |
| 1380 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1381 | sstrend = sp->space + newlen; |
| 1382 | |
| 1383 | /* |
| 1384 | * Stack marks pointing to the start of the old block |
| 1385 | * must be relocated to point to the new block |
| 1386 | */ |
| 1387 | xmark = markp; |
| 1388 | while (xmark != NULL && xmark->stackp == oldstackp) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1389 | xmark->stackp = g_stackp; |
| 1390 | xmark->stacknxt = g_stacknxt; |
| 1391 | xmark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1392 | xmark = xmark->marknext; |
| 1393 | } |
| 1394 | INT_ON; |
| 1395 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1396 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1397 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1398 | char *p = stalloc(newlen); |
| 1399 | |
| 1400 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1401 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1402 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | static void |
| 1407 | grabstackblock(size_t len) |
| 1408 | { |
| 1409 | len = SHELL_ALIGN(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1410 | g_stacknxt += len; |
| 1411 | g_stacknleft -= len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | /* |
| 1415 | * The following routines are somewhat easier to use than the above. |
| 1416 | * The user declares a variable of type STACKSTR, which may be declared |
| 1417 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1418 | * the user uses the macro STPUTC to add characters to the string. In |
| 1419 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1420 | * grown as necessary. When the user is done, she can just leave the |
| 1421 | * string there and refer to it using stackblock(). Or she can allocate |
| 1422 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1423 | * someone else to use the stack temporarily and then continue to grow |
| 1424 | * the string, the user should use grabstack to allocate the space, and |
| 1425 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1426 | * |
| 1427 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1428 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1429 | * is space for at least one character. |
| 1430 | */ |
| 1431 | static void * |
| 1432 | growstackstr(void) |
| 1433 | { |
| 1434 | size_t len = stackblocksize(); |
| 1435 | if (herefd >= 0 && len >= 1024) { |
| 1436 | full_write(herefd, stackblock(), len); |
| 1437 | return stackblock(); |
| 1438 | } |
| 1439 | growstackblock(); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1440 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | /* |
| 1444 | * Called from CHECKSTRSPACE. |
| 1445 | */ |
| 1446 | static char * |
| 1447 | makestrspace(size_t newlen, char *p) |
| 1448 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1449 | size_t len = p - g_stacknxt; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1450 | size_t size = stackblocksize(); |
| 1451 | |
| 1452 | for (;;) { |
| 1453 | size_t nleft; |
| 1454 | |
| 1455 | size = stackblocksize(); |
| 1456 | nleft = size - len; |
| 1457 | if (nleft >= newlen) |
| 1458 | break; |
| 1459 | growstackblock(); |
| 1460 | } |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1461 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | static char * |
| 1465 | stack_nputstr(const char *s, size_t n, char *p) |
| 1466 | { |
| 1467 | p = makestrspace(n, p); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1468 | p = (char *)memcpy(p, s, n) + n; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1469 | return p; |
| 1470 | } |
| 1471 | |
| 1472 | static char * |
| 1473 | stack_putstr(const char *s, char *p) |
| 1474 | { |
| 1475 | return stack_nputstr(s, strlen(s), p); |
| 1476 | } |
| 1477 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1478 | static char * |
| 1479 | _STPUTC(int c, char *p) |
| 1480 | { |
| 1481 | if (p == sstrend) |
| 1482 | p = growstackstr(); |
| 1483 | *p++ = c; |
| 1484 | return p; |
| 1485 | } |
| 1486 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1487 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1488 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1489 | #define CHECKSTRSPACE(n, p) do { \ |
| 1490 | char *q = (p); \ |
| 1491 | size_t l = (n); \ |
| 1492 | size_t m = sstrend - q; \ |
| 1493 | if (l > m) \ |
| 1494 | (p) = makestrspace(l, q); \ |
| 1495 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1496 | #define USTPUTC(c, p) (*(p)++ = (c)) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1497 | #define STACKSTRNUL(p) do { \ |
| 1498 | if ((p) == sstrend) \ |
| 1499 | (p) = growstackstr(); \ |
| 1500 | *(p) = '\0'; \ |
| 1501 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1502 | #define STUNPUTC(p) (--(p)) |
| 1503 | #define STTOPC(p) ((p)[-1]) |
| 1504 | #define STADJUST(amount, p) ((p) += (amount)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1505 | |
| 1506 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1507 | #define ungrabstackstr(s, p) stunalloc(s) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1508 | #define stackstrend() ((void *)sstrend) |
| 1509 | |
| 1510 | |
| 1511 | /* ============ String helpers */ |
| 1512 | |
| 1513 | /* |
| 1514 | * prefix -- see if pfx is a prefix of string. |
| 1515 | */ |
| 1516 | static char * |
| 1517 | prefix(const char *string, const char *pfx) |
| 1518 | { |
| 1519 | while (*pfx) { |
| 1520 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1521 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1522 | } |
| 1523 | return (char *) string; |
| 1524 | } |
| 1525 | |
| 1526 | /* |
| 1527 | * Check for a valid number. This should be elsewhere. |
| 1528 | */ |
| 1529 | static int |
| 1530 | is_number(const char *p) |
| 1531 | { |
| 1532 | do { |
| 1533 | if (!isdigit(*p)) |
| 1534 | return 0; |
| 1535 | } while (*++p != '\0'); |
| 1536 | return 1; |
| 1537 | } |
| 1538 | |
| 1539 | /* |
| 1540 | * Convert a string of digits to an integer, printing an error message on |
| 1541 | * failure. |
| 1542 | */ |
| 1543 | static int |
| 1544 | number(const char *s) |
| 1545 | { |
| 1546 | if (!is_number(s)) |
| 1547 | ash_msg_and_raise_error(illnum, s); |
| 1548 | return atoi(s); |
| 1549 | } |
| 1550 | |
| 1551 | /* |
| 1552 | * Produce a possibly single quoted string suitable as input to the shell. |
| 1553 | * The return string is allocated on the stack. |
| 1554 | */ |
| 1555 | static char * |
| 1556 | single_quote(const char *s) |
| 1557 | { |
| 1558 | char *p; |
| 1559 | |
| 1560 | STARTSTACKSTR(p); |
| 1561 | |
| 1562 | do { |
| 1563 | char *q; |
| 1564 | size_t len; |
| 1565 | |
| 1566 | len = strchrnul(s, '\'') - s; |
| 1567 | |
| 1568 | q = p = makestrspace(len + 3, p); |
| 1569 | |
| 1570 | *q++ = '\''; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1571 | q = (char *)memcpy(q, s, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1572 | *q++ = '\''; |
| 1573 | s += len; |
| 1574 | |
| 1575 | STADJUST(q - p, p); |
| 1576 | |
| 1577 | len = strspn(s, "'"); |
| 1578 | if (!len) |
| 1579 | break; |
| 1580 | |
| 1581 | q = p = makestrspace(len + 3, p); |
| 1582 | |
| 1583 | *q++ = '"'; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1584 | q = (char *)memcpy(q, s, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1585 | *q++ = '"'; |
| 1586 | s += len; |
| 1587 | |
| 1588 | STADJUST(q - p, p); |
| 1589 | } while (*s); |
| 1590 | |
| 1591 | USTPUTC(0, p); |
| 1592 | |
| 1593 | return stackblock(); |
| 1594 | } |
| 1595 | |
| 1596 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1597 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1598 | |
| 1599 | static char **argptr; /* argument list for builtin commands */ |
| 1600 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1601 | static char *optptr; /* used by nextopt */ |
| 1602 | |
| 1603 | /* |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1604 | * XXX - should get rid of. Have all builtins use getopt(3). |
| 1605 | * The library getopt must have the BSD extension static variable |
| 1606 | * "optreset", otherwise it can't be used within the shell safely. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1607 | * |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1608 | * Standard option processing (a la getopt) for builtin routines. |
| 1609 | * The only argument that is passed to nextopt is the option string; |
| 1610 | * the other arguments are unnecessary. It returns the character, |
| 1611 | * or '\0' on end of input. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1612 | */ |
| 1613 | static int |
| 1614 | nextopt(const char *optstring) |
| 1615 | { |
| 1616 | char *p; |
| 1617 | const char *q; |
| 1618 | char c; |
| 1619 | |
| 1620 | p = optptr; |
| 1621 | if (p == NULL || *p == '\0') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1622 | /* We ate entire "-param", take next one */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1623 | p = *argptr; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1624 | if (p == NULL) |
| 1625 | return '\0'; |
| 1626 | if (*p != '-') |
| 1627 | return '\0'; |
| 1628 | if (*++p == '\0') /* just "-" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1629 | return '\0'; |
| 1630 | argptr++; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1631 | if (LONE_DASH(p)) /* "--" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1632 | return '\0'; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1633 | /* p => next "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1634 | } |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1635 | /* p => some option char in the middle of a "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1636 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 1637 | for (q = optstring; *q != c;) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1638 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1639 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1640 | if (*++q == ':') |
| 1641 | q++; |
| 1642 | } |
| 1643 | if (*++q == ':') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1644 | if (*p == '\0') { |
| 1645 | p = *argptr++; |
| 1646 | if (p == NULL) |
| 1647 | ash_msg_and_raise_error("no arg for -%c option", c); |
| 1648 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1649 | optionarg = p; |
| 1650 | p = NULL; |
| 1651 | } |
| 1652 | optptr = p; |
| 1653 | return c; |
| 1654 | } |
| 1655 | |
| 1656 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1657 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1658 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1659 | /* |
| 1660 | * The parsefile structure pointed to by the global variable parsefile |
| 1661 | * contains information about the current file being read. |
| 1662 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1663 | struct shparam { |
| 1664 | int nparam; /* # of positional parameters (without $0) */ |
| 1665 | #if ENABLE_ASH_GETOPTS |
| 1666 | int optind; /* next parameter to be processed by getopts */ |
| 1667 | int optoff; /* used by getopts */ |
| 1668 | #endif |
| 1669 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 1670 | char **p; /* parameter list */ |
| 1671 | }; |
| 1672 | |
| 1673 | /* |
| 1674 | * Free the list of positional parameters. |
| 1675 | */ |
| 1676 | static void |
| 1677 | freeparam(volatile struct shparam *param) |
| 1678 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1679 | if (param->malloced) { |
Denis Vlasenko | 3177ba0 | 2008-07-13 20:39:23 +0000 | [diff] [blame] | 1680 | char **ap, **ap1; |
| 1681 | ap = ap1 = param->p; |
| 1682 | while (*ap) |
| 1683 | free(*ap++); |
| 1684 | free(ap1); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1689 | static void FAST_FUNC getoptsreset(const char *value); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1690 | #endif |
| 1691 | |
| 1692 | struct var { |
| 1693 | struct var *next; /* next entry in hash list */ |
| 1694 | int flags; /* flags are defined above */ |
| 1695 | const char *text; /* name=value */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1696 | void (*func)(const char *) FAST_FUNC; /* function to be called when */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1697 | /* the variable gets set/unset */ |
| 1698 | }; |
| 1699 | |
| 1700 | struct localvar { |
| 1701 | struct localvar *next; /* next local variable in list */ |
| 1702 | struct var *vp; /* the variable that was made local */ |
| 1703 | int flags; /* saved flags */ |
| 1704 | const char *text; /* saved text */ |
| 1705 | }; |
| 1706 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1707 | /* flags */ |
| 1708 | #define VEXPORT 0x01 /* variable is exported */ |
| 1709 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 1710 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 1711 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 1712 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 1713 | #define VUNSET 0x20 /* the variable is not set */ |
| 1714 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 1715 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 1716 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1717 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1718 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 1719 | #else |
| 1720 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1721 | #endif |
| 1722 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1723 | #ifdef IFS_BROKEN |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1724 | static const char defifsvar[] ALIGN1 = "IFS= \t\n"; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1725 | #define defifs (defifsvar + 4) |
| 1726 | #else |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1727 | static const char defifs[] ALIGN1 = " \t\n"; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1728 | #endif |
| 1729 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1730 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1731 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1732 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1733 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1734 | change_lc_all(const char *value) |
| 1735 | { |
| 1736 | if (value && *value != '\0') |
| 1737 | setlocale(LC_ALL, value); |
| 1738 | } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1739 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1740 | change_lc_ctype(const char *value) |
| 1741 | { |
| 1742 | if (value && *value != '\0') |
| 1743 | setlocale(LC_CTYPE, value); |
| 1744 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1745 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1746 | #if ENABLE_ASH_MAIL |
| 1747 | static void chkmail(void); |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1748 | static void changemail(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1749 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1750 | static void changepath(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1751 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1752 | static void change_random(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1753 | #endif |
| 1754 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1755 | static const struct { |
| 1756 | int flags; |
| 1757 | const char *text; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1758 | void (*func)(const char *) FAST_FUNC; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1759 | } varinit_data[] = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1760 | #ifdef IFS_BROKEN |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1761 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1762 | #else |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1763 | { VSTRFIXED|VTEXTFIXED|VUNSET, "IFS\0" , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1764 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1765 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1766 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL\0" , changemail }, |
| 1767 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH\0", changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1768 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1769 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 1770 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 1771 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 1772 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1773 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1774 | { VSTRFIXED|VTEXTFIXED , "OPTIND=1" , getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1775 | #endif |
| 1776 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1777 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM\0", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1778 | #endif |
| 1779 | #if ENABLE_LOCALE_SUPPORT |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1780 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL\0" , change_lc_all }, |
| 1781 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE\0", change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1782 | #endif |
| 1783 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1784 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE\0", NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1785 | #endif |
| 1786 | }; |
| 1787 | |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1788 | struct redirtab; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1789 | |
| 1790 | struct globals_var { |
| 1791 | struct shparam shellparam; /* $@ current positional parameters */ |
| 1792 | struct redirtab *redirlist; |
| 1793 | int g_nullredirs; |
| 1794 | int preverrout_fd; /* save fd2 before print debug if xflag is set. */ |
| 1795 | struct var *vartab[VTABSIZE]; |
| 1796 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
| 1797 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1798 | extern struct globals_var *const ash_ptr_to_globals_var; |
| 1799 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1800 | #define shellparam (G_var.shellparam ) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1801 | //#define redirlist (G_var.redirlist ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1802 | #define g_nullredirs (G_var.g_nullredirs ) |
| 1803 | #define preverrout_fd (G_var.preverrout_fd) |
| 1804 | #define vartab (G_var.vartab ) |
| 1805 | #define varinit (G_var.varinit ) |
| 1806 | #define INIT_G_var() do { \ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1807 | unsigned i; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1808 | (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \ |
| 1809 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1810 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
| 1811 | varinit[i].flags = varinit_data[i].flags; \ |
| 1812 | varinit[i].text = varinit_data[i].text; \ |
| 1813 | varinit[i].func = varinit_data[i].func; \ |
| 1814 | } \ |
| 1815 | } while (0) |
| 1816 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1817 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1818 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1819 | # define vmail (&vifs)[1] |
| 1820 | # define vmpath (&vmail)[1] |
| 1821 | # define vpath (&vmpath)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1822 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1823 | # define vpath (&vifs)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1824 | #endif |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1825 | #define vps1 (&vpath)[1] |
| 1826 | #define vps2 (&vps1)[1] |
| 1827 | #define vps4 (&vps2)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1828 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1829 | # define voptind (&vps4)[1] |
| 1830 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1831 | # define vrandom (&voptind)[1] |
| 1832 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1833 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1834 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1835 | # define vrandom (&vps4)[1] |
| 1836 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1837 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1838 | |
| 1839 | /* |
| 1840 | * The following macros access the values of the above variables. |
| 1841 | * They have to skip over the name. They return the null string |
| 1842 | * for unset variables. |
| 1843 | */ |
| 1844 | #define ifsval() (vifs.text + 4) |
| 1845 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1846 | #if ENABLE_ASH_MAIL |
| 1847 | # define mailval() (vmail.text + 5) |
| 1848 | # define mpathval() (vmpath.text + 9) |
| 1849 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1850 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1851 | #define pathval() (vpath.text + 5) |
| 1852 | #define ps1val() (vps1.text + 4) |
| 1853 | #define ps2val() (vps2.text + 4) |
| 1854 | #define ps4val() (vps4.text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1855 | #if ENABLE_ASH_GETOPTS |
| 1856 | # define optindval() (voptind.text + 7) |
| 1857 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1858 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1859 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1860 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1861 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1862 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1863 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1864 | static void FAST_FUNC |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1865 | getoptsreset(const char *value) |
| 1866 | { |
| 1867 | shellparam.optind = number(value); |
| 1868 | shellparam.optoff = -1; |
| 1869 | } |
| 1870 | #endif |
| 1871 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1872 | /* |
| 1873 | * Return of a legal variable name (a letter or underscore followed by zero or |
| 1874 | * more letters, underscores, and digits). |
| 1875 | */ |
| 1876 | static char * |
| 1877 | endofname(const char *name) |
| 1878 | { |
| 1879 | char *p; |
| 1880 | |
| 1881 | p = (char *) name; |
| 1882 | if (!is_name(*p)) |
| 1883 | return p; |
| 1884 | while (*++p) { |
| 1885 | if (!is_in_name(*p)) |
| 1886 | break; |
| 1887 | } |
| 1888 | return p; |
| 1889 | } |
| 1890 | |
| 1891 | /* |
| 1892 | * Compares two strings up to the first = or '\0'. The first |
| 1893 | * string must be terminated by '='; the second may be terminated by |
| 1894 | * either '=' or '\0'. |
| 1895 | */ |
| 1896 | static int |
| 1897 | varcmp(const char *p, const char *q) |
| 1898 | { |
| 1899 | int c, d; |
| 1900 | |
| 1901 | while ((c = *p) == (d = *q)) { |
| 1902 | if (!c || c == '=') |
| 1903 | goto out; |
| 1904 | p++; |
| 1905 | q++; |
| 1906 | } |
| 1907 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 1908 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1909 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 1910 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1911 | out: |
| 1912 | return c - d; |
| 1913 | } |
| 1914 | |
| 1915 | static int |
| 1916 | varequal(const char *a, const char *b) |
| 1917 | { |
| 1918 | return !varcmp(a, b); |
| 1919 | } |
| 1920 | |
| 1921 | /* |
| 1922 | * Find the appropriate entry in the hash table from the name. |
| 1923 | */ |
| 1924 | static struct var ** |
| 1925 | hashvar(const char *p) |
| 1926 | { |
| 1927 | unsigned hashval; |
| 1928 | |
| 1929 | hashval = ((unsigned char) *p) << 4; |
| 1930 | while (*p && *p != '=') |
| 1931 | hashval += (unsigned char) *p++; |
| 1932 | return &vartab[hashval % VTABSIZE]; |
| 1933 | } |
| 1934 | |
| 1935 | static int |
| 1936 | vpcmp(const void *a, const void *b) |
| 1937 | { |
| 1938 | return varcmp(*(const char **)a, *(const char **)b); |
| 1939 | } |
| 1940 | |
| 1941 | /* |
| 1942 | * This routine initializes the builtin variables. |
| 1943 | */ |
| 1944 | static void |
| 1945 | initvar(void) |
| 1946 | { |
| 1947 | struct var *vp; |
| 1948 | struct var *end; |
| 1949 | struct var **vpp; |
| 1950 | |
| 1951 | /* |
| 1952 | * PS1 depends on uid |
| 1953 | */ |
| 1954 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 1955 | vps1.text = "PS1=\\w \\$ "; |
| 1956 | #else |
| 1957 | if (!geteuid()) |
| 1958 | vps1.text = "PS1=# "; |
| 1959 | #endif |
| 1960 | vp = varinit; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 1961 | end = vp + ARRAY_SIZE(varinit); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1962 | do { |
| 1963 | vpp = hashvar(vp->text); |
| 1964 | vp->next = *vpp; |
| 1965 | *vpp = vp; |
| 1966 | } while (++vp < end); |
| 1967 | } |
| 1968 | |
| 1969 | static struct var ** |
| 1970 | findvar(struct var **vpp, const char *name) |
| 1971 | { |
| 1972 | for (; *vpp; vpp = &(*vpp)->next) { |
| 1973 | if (varequal((*vpp)->text, name)) { |
| 1974 | break; |
| 1975 | } |
| 1976 | } |
| 1977 | return vpp; |
| 1978 | } |
| 1979 | |
| 1980 | /* |
| 1981 | * Find the value of a variable. Returns NULL if not set. |
| 1982 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1983 | static const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1984 | lookupvar(const char *name) |
| 1985 | { |
| 1986 | struct var *v; |
| 1987 | |
| 1988 | v = *findvar(hashvar(name), name); |
| 1989 | if (v) { |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1990 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1991 | /* |
| 1992 | * Dynamic variables are implemented roughly the same way they are |
| 1993 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 1994 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 1995 | * lookup will no longer happen at that point. -- PFM. |
| 1996 | */ |
| 1997 | if ((v->flags & VDYNAMIC)) |
| 1998 | (*v->func)(NULL); |
| 1999 | #endif |
| 2000 | if (!(v->flags & VUNSET)) |
| 2001 | return strchrnul(v->text, '=') + 1; |
| 2002 | } |
| 2003 | return NULL; |
| 2004 | } |
| 2005 | |
| 2006 | /* |
| 2007 | * Search the environment of a builtin command. |
| 2008 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2009 | static const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2010 | bltinlookup(const char *name) |
| 2011 | { |
| 2012 | struct strlist *sp; |
| 2013 | |
| 2014 | for (sp = cmdenviron; sp; sp = sp->next) { |
| 2015 | if (varequal(sp->text, name)) |
| 2016 | return strchrnul(sp->text, '=') + 1; |
| 2017 | } |
| 2018 | return lookupvar(name); |
| 2019 | } |
| 2020 | |
| 2021 | /* |
| 2022 | * Same as setvar except that the variable and value are passed in |
| 2023 | * the first argument as name=value. Since the first argument will |
| 2024 | * be actually stored in the table, it should not be a string that |
| 2025 | * will go away. |
| 2026 | * Called with interrupts off. |
| 2027 | */ |
| 2028 | static void |
| 2029 | setvareq(char *s, int flags) |
| 2030 | { |
| 2031 | struct var *vp, **vpp; |
| 2032 | |
| 2033 | vpp = hashvar(s); |
| 2034 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
| 2035 | vp = *findvar(vpp, s); |
| 2036 | if (vp) { |
| 2037 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 2038 | const char *n; |
| 2039 | |
| 2040 | if (flags & VNOSAVE) |
| 2041 | free(s); |
| 2042 | n = vp->text; |
| 2043 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2044 | } |
| 2045 | |
| 2046 | if (flags & VNOSET) |
| 2047 | return; |
| 2048 | |
| 2049 | if (vp->func && (flags & VNOFUNC) == 0) |
| 2050 | (*vp->func)(strchrnul(s, '=') + 1); |
| 2051 | |
| 2052 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 2053 | free((char*)vp->text); |
| 2054 | |
| 2055 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
| 2056 | } else { |
| 2057 | if (flags & VNOSET) |
| 2058 | return; |
| 2059 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2060 | vp = ckzalloc(sizeof(*vp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2061 | vp->next = *vpp; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2062 | /*vp->func = NULL; - ckzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2063 | *vpp = vp; |
| 2064 | } |
| 2065 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 2066 | s = ckstrdup(s); |
| 2067 | vp->text = s; |
| 2068 | vp->flags = flags; |
| 2069 | } |
| 2070 | |
| 2071 | /* |
| 2072 | * Set the value of a variable. The flags argument is ored with the |
| 2073 | * flags of the variable. If val is NULL, the variable is unset. |
| 2074 | */ |
| 2075 | static void |
| 2076 | setvar(const char *name, const char *val, int flags) |
| 2077 | { |
| 2078 | char *p, *q; |
| 2079 | size_t namelen; |
| 2080 | char *nameeq; |
| 2081 | size_t vallen; |
| 2082 | |
| 2083 | q = endofname(name); |
| 2084 | p = strchrnul(q, '='); |
| 2085 | namelen = p - name; |
| 2086 | if (!namelen || p != q) |
| 2087 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 2088 | vallen = 0; |
| 2089 | if (val == NULL) { |
| 2090 | flags |= VUNSET; |
| 2091 | } else { |
| 2092 | vallen = strlen(val); |
| 2093 | } |
| 2094 | INT_OFF; |
| 2095 | nameeq = ckmalloc(namelen + vallen + 2); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2096 | p = (char *)memcpy(nameeq, name, namelen) + namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2097 | if (val) { |
| 2098 | *p++ = '='; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2099 | p = (char *)memcpy(p, val, vallen) + vallen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2100 | } |
| 2101 | *p = '\0'; |
| 2102 | setvareq(nameeq, flags | VNOSAVE); |
| 2103 | INT_ON; |
| 2104 | } |
| 2105 | |
| 2106 | #if ENABLE_ASH_GETOPTS |
| 2107 | /* |
| 2108 | * Safe version of setvar, returns 1 on success 0 on failure. |
| 2109 | */ |
| 2110 | static int |
| 2111 | setvarsafe(const char *name, const char *val, int flags) |
| 2112 | { |
| 2113 | int err; |
| 2114 | volatile int saveint; |
| 2115 | struct jmploc *volatile savehandler = exception_handler; |
| 2116 | struct jmploc jmploc; |
| 2117 | |
| 2118 | SAVE_INT(saveint); |
| 2119 | if (setjmp(jmploc.loc)) |
| 2120 | err = 1; |
| 2121 | else { |
| 2122 | exception_handler = &jmploc; |
| 2123 | setvar(name, val, flags); |
| 2124 | err = 0; |
| 2125 | } |
| 2126 | exception_handler = savehandler; |
| 2127 | RESTORE_INT(saveint); |
| 2128 | return err; |
| 2129 | } |
| 2130 | #endif |
| 2131 | |
| 2132 | /* |
| 2133 | * Unset the specified variable. |
| 2134 | */ |
| 2135 | static int |
| 2136 | unsetvar(const char *s) |
| 2137 | { |
| 2138 | struct var **vpp; |
| 2139 | struct var *vp; |
| 2140 | int retval; |
| 2141 | |
| 2142 | vpp = findvar(hashvar(s), s); |
| 2143 | vp = *vpp; |
| 2144 | retval = 2; |
| 2145 | if (vp) { |
| 2146 | int flags = vp->flags; |
| 2147 | |
| 2148 | retval = 1; |
| 2149 | if (flags & VREADONLY) |
| 2150 | goto out; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2151 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2152 | vp->flags &= ~VDYNAMIC; |
| 2153 | #endif |
| 2154 | if (flags & VUNSET) |
| 2155 | goto ok; |
| 2156 | if ((flags & VSTRFIXED) == 0) { |
| 2157 | INT_OFF; |
| 2158 | if ((flags & (VTEXTFIXED|VSTACK)) == 0) |
| 2159 | free((char*)vp->text); |
| 2160 | *vpp = vp->next; |
| 2161 | free(vp); |
| 2162 | INT_ON; |
| 2163 | } else { |
| 2164 | setvar(s, 0, 0); |
| 2165 | vp->flags &= ~VEXPORT; |
| 2166 | } |
| 2167 | ok: |
| 2168 | retval = 0; |
| 2169 | } |
| 2170 | out: |
| 2171 | return retval; |
| 2172 | } |
| 2173 | |
| 2174 | /* |
| 2175 | * Process a linked list of variable assignments. |
| 2176 | */ |
| 2177 | static void |
| 2178 | listsetvar(struct strlist *list_set_var, int flags) |
| 2179 | { |
| 2180 | struct strlist *lp = list_set_var; |
| 2181 | |
| 2182 | if (!lp) |
| 2183 | return; |
| 2184 | INT_OFF; |
| 2185 | do { |
| 2186 | setvareq(lp->text, flags); |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2187 | lp = lp->next; |
| 2188 | } while (lp); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2189 | INT_ON; |
| 2190 | } |
| 2191 | |
| 2192 | /* |
| 2193 | * Generate a list of variables satisfying the given conditions. |
| 2194 | */ |
| 2195 | static char ** |
| 2196 | listvars(int on, int off, char ***end) |
| 2197 | { |
| 2198 | struct var **vpp; |
| 2199 | struct var *vp; |
| 2200 | char **ep; |
| 2201 | int mask; |
| 2202 | |
| 2203 | STARTSTACKSTR(ep); |
| 2204 | vpp = vartab; |
| 2205 | mask = on | off; |
| 2206 | do { |
| 2207 | for (vp = *vpp; vp; vp = vp->next) { |
| 2208 | if ((vp->flags & mask) == on) { |
| 2209 | if (ep == stackstrend()) |
| 2210 | ep = growstackstr(); |
| 2211 | *ep++ = (char *) vp->text; |
| 2212 | } |
| 2213 | } |
| 2214 | } while (++vpp < vartab + VTABSIZE); |
| 2215 | if (ep == stackstrend()) |
| 2216 | ep = growstackstr(); |
| 2217 | if (end) |
| 2218 | *end = ep; |
| 2219 | *ep++ = NULL; |
| 2220 | return grabstackstr(ep); |
| 2221 | } |
| 2222 | |
| 2223 | |
| 2224 | /* ============ Path search helper |
| 2225 | * |
| 2226 | * The variable path (passed by reference) should be set to the start |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2227 | * of the path before the first call; path_advance will update |
| 2228 | * this value as it proceeds. Successive calls to path_advance will return |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2229 | * the possible path expansions in sequence. If an option (indicated by |
| 2230 | * a percent sign) appears in the path entry then the global variable |
| 2231 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 2232 | * NULL. |
| 2233 | */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2234 | static const char *pathopt; /* set by path_advance */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2235 | |
| 2236 | static char * |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2237 | path_advance(const char **path, const char *name) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2238 | { |
| 2239 | const char *p; |
| 2240 | char *q; |
| 2241 | const char *start; |
| 2242 | size_t len; |
| 2243 | |
| 2244 | if (*path == NULL) |
| 2245 | return NULL; |
| 2246 | start = *path; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2247 | for (p = start; *p && *p != ':' && *p != '%'; p++) |
| 2248 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2249 | len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ |
| 2250 | while (stackblocksize() < len) |
| 2251 | growstackblock(); |
| 2252 | q = stackblock(); |
| 2253 | if (p != start) { |
| 2254 | memcpy(q, start, p - start); |
| 2255 | q += p - start; |
| 2256 | *q++ = '/'; |
| 2257 | } |
| 2258 | strcpy(q, name); |
| 2259 | pathopt = NULL; |
| 2260 | if (*p == '%') { |
| 2261 | pathopt = ++p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2262 | while (*p && *p != ':') |
| 2263 | p++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2264 | } |
| 2265 | if (*p == ':') |
| 2266 | *path = p + 1; |
| 2267 | else |
| 2268 | *path = NULL; |
| 2269 | return stalloc(len); |
| 2270 | } |
| 2271 | |
| 2272 | |
| 2273 | /* ============ Prompt */ |
| 2274 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2275 | static smallint doprompt; /* if set, prompt the user */ |
| 2276 | static smallint needprompt; /* true if interactive and at start of line */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2277 | |
| 2278 | #if ENABLE_FEATURE_EDITING |
| 2279 | static line_input_t *line_input_state; |
| 2280 | static const char *cmdedit_prompt; |
| 2281 | static void |
| 2282 | putprompt(const char *s) |
| 2283 | { |
| 2284 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 2285 | free((char*)cmdedit_prompt); |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 2286 | cmdedit_prompt = ckstrdup(s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2287 | return; |
| 2288 | } |
| 2289 | cmdedit_prompt = s; |
| 2290 | } |
| 2291 | #else |
| 2292 | static void |
| 2293 | putprompt(const char *s) |
| 2294 | { |
| 2295 | out2str(s); |
| 2296 | } |
| 2297 | #endif |
| 2298 | |
| 2299 | #if ENABLE_ASH_EXPAND_PRMT |
| 2300 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
| 2301 | static const char *expandstr(const char *ps); |
| 2302 | #else |
| 2303 | #define expandstr(s) s |
| 2304 | #endif |
| 2305 | |
| 2306 | static void |
| 2307 | setprompt(int whichprompt) |
| 2308 | { |
| 2309 | const char *prompt; |
| 2310 | #if ENABLE_ASH_EXPAND_PRMT |
| 2311 | struct stackmark smark; |
| 2312 | #endif |
| 2313 | |
| 2314 | needprompt = 0; |
| 2315 | |
| 2316 | switch (whichprompt) { |
| 2317 | case 1: |
| 2318 | prompt = ps1val(); |
| 2319 | break; |
| 2320 | case 2: |
| 2321 | prompt = ps2val(); |
| 2322 | break; |
| 2323 | default: /* 0 */ |
| 2324 | prompt = nullstr; |
| 2325 | } |
| 2326 | #if ENABLE_ASH_EXPAND_PRMT |
| 2327 | setstackmark(&smark); |
| 2328 | stalloc(stackblocksize()); |
| 2329 | #endif |
| 2330 | putprompt(expandstr(prompt)); |
| 2331 | #if ENABLE_ASH_EXPAND_PRMT |
| 2332 | popstackmark(&smark); |
| 2333 | #endif |
| 2334 | } |
| 2335 | |
| 2336 | |
| 2337 | /* ============ The cd and pwd commands */ |
| 2338 | |
| 2339 | #define CD_PHYSICAL 1 |
| 2340 | #define CD_PRINT 2 |
| 2341 | |
| 2342 | static int docd(const char *, int); |
| 2343 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2344 | static int |
| 2345 | cdopt(void) |
| 2346 | { |
| 2347 | int flags = 0; |
| 2348 | int i, j; |
| 2349 | |
| 2350 | j = 'L'; |
| 2351 | while ((i = nextopt("LP"))) { |
| 2352 | if (i != j) { |
| 2353 | flags ^= CD_PHYSICAL; |
| 2354 | j = i; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | return flags; |
| 2359 | } |
| 2360 | |
| 2361 | /* |
| 2362 | * Update curdir (the name of the current directory) in response to a |
| 2363 | * cd command. |
| 2364 | */ |
| 2365 | static const char * |
| 2366 | updatepwd(const char *dir) |
| 2367 | { |
| 2368 | char *new; |
| 2369 | char *p; |
| 2370 | char *cdcomppath; |
| 2371 | const char *lim; |
| 2372 | |
| 2373 | cdcomppath = ststrdup(dir); |
| 2374 | STARTSTACKSTR(new); |
| 2375 | if (*dir != '/') { |
| 2376 | if (curdir == nullstr) |
| 2377 | return 0; |
| 2378 | new = stack_putstr(curdir, new); |
| 2379 | } |
| 2380 | new = makestrspace(strlen(dir) + 2, new); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2381 | lim = (char *)stackblock() + 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2382 | if (*dir != '/') { |
| 2383 | if (new[-1] != '/') |
| 2384 | USTPUTC('/', new); |
| 2385 | if (new > lim && *lim == '/') |
| 2386 | lim++; |
| 2387 | } else { |
| 2388 | USTPUTC('/', new); |
| 2389 | cdcomppath++; |
| 2390 | if (dir[1] == '/' && dir[2] != '/') { |
| 2391 | USTPUTC('/', new); |
| 2392 | cdcomppath++; |
| 2393 | lim++; |
| 2394 | } |
| 2395 | } |
| 2396 | p = strtok(cdcomppath, "/"); |
| 2397 | while (p) { |
| 2398 | switch (*p) { |
| 2399 | case '.': |
| 2400 | if (p[1] == '.' && p[2] == '\0') { |
| 2401 | while (new > lim) { |
| 2402 | STUNPUTC(new); |
| 2403 | if (new[-1] == '/') |
| 2404 | break; |
| 2405 | } |
| 2406 | break; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 2407 | } |
| 2408 | if (p[1] == '\0') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2409 | break; |
| 2410 | /* fall through */ |
| 2411 | default: |
| 2412 | new = stack_putstr(p, new); |
| 2413 | USTPUTC('/', new); |
| 2414 | } |
| 2415 | p = strtok(0, "/"); |
| 2416 | } |
| 2417 | if (new > lim) |
| 2418 | STUNPUTC(new); |
| 2419 | *new = 0; |
| 2420 | return stackblock(); |
| 2421 | } |
| 2422 | |
| 2423 | /* |
| 2424 | * Find out what the current directory is. If we already know the current |
| 2425 | * directory, this routine returns immediately. |
| 2426 | */ |
| 2427 | static char * |
| 2428 | getpwd(void) |
| 2429 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2430 | char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2431 | return dir ? dir : nullstr; |
| 2432 | } |
| 2433 | |
| 2434 | static void |
| 2435 | setpwd(const char *val, int setold) |
| 2436 | { |
| 2437 | char *oldcur, *dir; |
| 2438 | |
| 2439 | oldcur = dir = curdir; |
| 2440 | |
| 2441 | if (setold) { |
| 2442 | setvar("OLDPWD", oldcur, VEXPORT); |
| 2443 | } |
| 2444 | INT_OFF; |
| 2445 | if (physdir != nullstr) { |
| 2446 | if (physdir != oldcur) |
| 2447 | free(physdir); |
| 2448 | physdir = nullstr; |
| 2449 | } |
| 2450 | if (oldcur == val || !val) { |
| 2451 | char *s = getpwd(); |
| 2452 | physdir = s; |
| 2453 | if (!val) |
| 2454 | dir = s; |
| 2455 | } else |
| 2456 | dir = ckstrdup(val); |
| 2457 | if (oldcur != dir && oldcur != nullstr) { |
| 2458 | free(oldcur); |
| 2459 | } |
| 2460 | curdir = dir; |
| 2461 | INT_ON; |
| 2462 | setvar("PWD", dir, VEXPORT); |
| 2463 | } |
| 2464 | |
| 2465 | static void hashcd(void); |
| 2466 | |
| 2467 | /* |
| 2468 | * Actually do the chdir. We also call hashcd to let the routines in exec.c |
| 2469 | * know that the current directory has changed. |
| 2470 | */ |
| 2471 | static int |
| 2472 | docd(const char *dest, int flags) |
| 2473 | { |
| 2474 | const char *dir = 0; |
| 2475 | int err; |
| 2476 | |
| 2477 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 2478 | |
| 2479 | INT_OFF; |
| 2480 | if (!(flags & CD_PHYSICAL)) { |
| 2481 | dir = updatepwd(dest); |
| 2482 | if (dir) |
| 2483 | dest = dir; |
| 2484 | } |
| 2485 | err = chdir(dest); |
| 2486 | if (err) |
| 2487 | goto out; |
| 2488 | setpwd(dir, 1); |
| 2489 | hashcd(); |
| 2490 | out: |
| 2491 | INT_ON; |
| 2492 | return err; |
| 2493 | } |
| 2494 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2495 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2496 | cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2497 | { |
| 2498 | const char *dest; |
| 2499 | const char *path; |
| 2500 | const char *p; |
| 2501 | char c; |
| 2502 | struct stat statb; |
| 2503 | int flags; |
| 2504 | |
| 2505 | flags = cdopt(); |
| 2506 | dest = *argptr; |
| 2507 | if (!dest) |
| 2508 | dest = bltinlookup(homestr); |
| 2509 | else if (LONE_DASH(dest)) { |
| 2510 | dest = bltinlookup("OLDPWD"); |
| 2511 | flags |= CD_PRINT; |
| 2512 | } |
| 2513 | if (!dest) |
| 2514 | dest = nullstr; |
| 2515 | if (*dest == '/') |
| 2516 | goto step7; |
| 2517 | if (*dest == '.') { |
| 2518 | c = dest[1]; |
| 2519 | dotdot: |
| 2520 | switch (c) { |
| 2521 | case '\0': |
| 2522 | case '/': |
| 2523 | goto step6; |
| 2524 | case '.': |
| 2525 | c = dest[2]; |
| 2526 | if (c != '.') |
| 2527 | goto dotdot; |
| 2528 | } |
| 2529 | } |
| 2530 | if (!*dest) |
| 2531 | dest = "."; |
| 2532 | path = bltinlookup("CDPATH"); |
| 2533 | if (!path) { |
| 2534 | step6: |
| 2535 | step7: |
| 2536 | p = dest; |
| 2537 | goto docd; |
| 2538 | } |
| 2539 | do { |
| 2540 | c = *path; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2541 | p = path_advance(&path, dest); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2542 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 2543 | if (c && c != ':') |
| 2544 | flags |= CD_PRINT; |
| 2545 | docd: |
| 2546 | if (!docd(p, flags)) |
| 2547 | goto out; |
| 2548 | break; |
| 2549 | } |
| 2550 | } while (path); |
| 2551 | ash_msg_and_raise_error("can't cd to %s", dest); |
| 2552 | /* NOTREACHED */ |
| 2553 | out: |
| 2554 | if (flags & CD_PRINT) |
| 2555 | out1fmt(snlfmt, curdir); |
| 2556 | return 0; |
| 2557 | } |
| 2558 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2559 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2560 | pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2561 | { |
| 2562 | int flags; |
| 2563 | const char *dir = curdir; |
| 2564 | |
| 2565 | flags = cdopt(); |
| 2566 | if (flags) { |
| 2567 | if (physdir == nullstr) |
| 2568 | setpwd(dir, 0); |
| 2569 | dir = physdir; |
| 2570 | } |
| 2571 | out1fmt(snlfmt, dir); |
| 2572 | return 0; |
| 2573 | } |
| 2574 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2575 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 2576 | /* ============ ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2577 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2578 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2579 | #define IBUFSIZ COMMON_BUFSIZE |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2580 | /* buffer for top level input file */ |
| 2581 | #define basebuf bb_common_bufsiz1 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2582 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2583 | /* Syntax classes */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2584 | #define CWORD 0 /* character is nothing special */ |
| 2585 | #define CNL 1 /* newline character */ |
| 2586 | #define CBACK 2 /* a backslash character */ |
| 2587 | #define CSQUOTE 3 /* single quote */ |
| 2588 | #define CDQUOTE 4 /* double quote */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2589 | #define CENDQUOTE 5 /* a terminating quote */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2590 | #define CBQUOTE 6 /* backwards single quote */ |
| 2591 | #define CVAR 7 /* a dollar sign */ |
| 2592 | #define CENDVAR 8 /* a '}' character */ |
| 2593 | #define CLP 9 /* a left paren in arithmetic */ |
| 2594 | #define CRP 10 /* a right paren in arithmetic */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2595 | #define CENDFILE 11 /* end of file */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2596 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 2597 | #define CSPCL 13 /* these terminate a word */ |
| 2598 | #define CIGN 14 /* character should be ignored */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2599 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2600 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2601 | #define SYNBASE 130 |
| 2602 | #define PEOF -130 |
| 2603 | #define PEOA -129 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2604 | #define PEOA_OR_PEOF PEOA |
| 2605 | #else |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2606 | #define SYNBASE 129 |
| 2607 | #define PEOF -129 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2608 | #define PEOA_OR_PEOF PEOF |
| 2609 | #endif |
| 2610 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2611 | /* number syntax index */ |
| 2612 | #define BASESYNTAX 0 /* not in quotes */ |
| 2613 | #define DQSYNTAX 1 /* in double quotes */ |
| 2614 | #define SQSYNTAX 2 /* in single quotes */ |
| 2615 | #define ARISYNTAX 3 /* in arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 2616 | #define PSSYNTAX 4 /* prompt */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2617 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2618 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2619 | #define USE_SIT_FUNCTION |
| 2620 | #endif |
| 2621 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2622 | #if ENABLE_SH_MATH_SUPPORT |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2623 | static const char S_I_T[][4] = { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2624 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2625 | { CSPCL, CIGN, CIGN, CIGN }, /* 0, PEOA */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2626 | #endif |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2627 | { CSPCL, CWORD, CWORD, CWORD }, /* 1, ' ' */ |
| 2628 | { CNL, CNL, CNL, CNL }, /* 2, \n */ |
| 2629 | { CWORD, CCTL, CCTL, CWORD }, /* 3, !*-/:=?[]~ */ |
| 2630 | { CDQUOTE, CENDQUOTE, CWORD, CWORD }, /* 4, '"' */ |
| 2631 | { CVAR, CVAR, CWORD, CVAR }, /* 5, $ */ |
| 2632 | { CSQUOTE, CWORD, CENDQUOTE, CWORD }, /* 6, "'" */ |
| 2633 | { CSPCL, CWORD, CWORD, CLP }, /* 7, ( */ |
| 2634 | { CSPCL, CWORD, CWORD, CRP }, /* 8, ) */ |
| 2635 | { CBACK, CBACK, CCTL, CBACK }, /* 9, \ */ |
| 2636 | { CBQUOTE, CBQUOTE, CWORD, CBQUOTE }, /* 10, ` */ |
| 2637 | { CENDVAR, CENDVAR, CWORD, CENDVAR }, /* 11, } */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2638 | #ifndef USE_SIT_FUNCTION |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2639 | { CENDFILE, CENDFILE, CENDFILE, CENDFILE }, /* 12, PEOF */ |
| 2640 | { CWORD, CWORD, CWORD, CWORD }, /* 13, 0-9A-Za-z */ |
| 2641 | { CCTL, CCTL, CCTL, CCTL } /* 14, CTLESC ... */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2642 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2643 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2644 | #else |
| 2645 | static const char S_I_T[][3] = { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2646 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2647 | { CSPCL, CIGN, CIGN }, /* 0, PEOA */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2648 | #endif |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2649 | { CSPCL, CWORD, CWORD }, /* 1, ' ' */ |
| 2650 | { CNL, CNL, CNL }, /* 2, \n */ |
| 2651 | { CWORD, CCTL, CCTL }, /* 3, !*-/:=?[]~ */ |
| 2652 | { CDQUOTE, CENDQUOTE, CWORD }, /* 4, '"' */ |
| 2653 | { CVAR, CVAR, CWORD }, /* 5, $ */ |
| 2654 | { CSQUOTE, CWORD, CENDQUOTE }, /* 6, "'" */ |
| 2655 | { CSPCL, CWORD, CWORD }, /* 7, ( */ |
| 2656 | { CSPCL, CWORD, CWORD }, /* 8, ) */ |
| 2657 | { CBACK, CBACK, CCTL }, /* 9, \ */ |
| 2658 | { CBQUOTE, CBQUOTE, CWORD }, /* 10, ` */ |
| 2659 | { CENDVAR, CENDVAR, CWORD }, /* 11, } */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2660 | #ifndef USE_SIT_FUNCTION |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2661 | { CENDFILE, CENDFILE, CENDFILE }, /* 12, PEOF */ |
| 2662 | { CWORD, CWORD, CWORD }, /* 13, 0-9A-Za-z */ |
| 2663 | { CCTL, CCTL, CCTL } /* 14, CTLESC ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2664 | #endif |
| 2665 | }; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2666 | #endif /* SH_MATH_SUPPORT */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2667 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2668 | #ifdef USE_SIT_FUNCTION |
| 2669 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2670 | static int |
| 2671 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2672 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2673 | static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-/:;<=>?[\\]`|}~"; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2674 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2675 | static const char syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2676 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
| 2677 | 7, 8, 3, 3, 3, 3, 1, 1, /* "()*-/:;<" */ |
| 2678 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 2679 | 11, 3 /* "}~" */ |
| 2680 | }; |
| 2681 | #else |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2682 | static const char syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2683 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
| 2684 | 6, 7, 2, 2, 2, 2, 0, 0, /* "()*-/:;<" */ |
| 2685 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 2686 | 10, 2 /* "}~" */ |
| 2687 | }; |
| 2688 | #endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2689 | const char *s; |
| 2690 | int indx; |
| 2691 | |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2692 | if (c == PEOF) { /* 2^8+2 */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2693 | return CENDFILE; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2694 | } |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2695 | #if ENABLE_ASH_ALIAS |
| 2696 | if (c == PEOA) { /* 2^8+1 */ |
| 2697 | indx = 0; |
| 2698 | } else |
| 2699 | #endif |
| 2700 | { |
| 2701 | if ((unsigned char)c >= (unsigned char)(CTLESC) |
| 2702 | && (unsigned char)c <= (unsigned char)(CTLQUOTEMARK) |
| 2703 | ) { |
| 2704 | return CCTL; |
| 2705 | } |
| 2706 | s = strchrnul(spec_symbls, c); |
| 2707 | if (*s == '\0') { |
| 2708 | return CWORD; |
| 2709 | } |
| 2710 | indx = syntax_index_table[s - spec_symbls]; |
| 2711 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2712 | return S_I_T[indx][syntax]; |
| 2713 | } |
| 2714 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2715 | #else /* !USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2716 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2717 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2718 | #define CSPCL_CIGN_CIGN_CIGN 0 |
| 2719 | #define CSPCL_CWORD_CWORD_CWORD 1 |
| 2720 | #define CNL_CNL_CNL_CNL 2 |
| 2721 | #define CWORD_CCTL_CCTL_CWORD 3 |
| 2722 | #define CDQUOTE_CENDQUOTE_CWORD_CWORD 4 |
| 2723 | #define CVAR_CVAR_CWORD_CVAR 5 |
| 2724 | #define CSQUOTE_CWORD_CENDQUOTE_CWORD 6 |
| 2725 | #define CSPCL_CWORD_CWORD_CLP 7 |
| 2726 | #define CSPCL_CWORD_CWORD_CRP 8 |
| 2727 | #define CBACK_CBACK_CCTL_CBACK 9 |
| 2728 | #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE 10 |
| 2729 | #define CENDVAR_CENDVAR_CWORD_CENDVAR 11 |
| 2730 | #define CENDFILE_CENDFILE_CENDFILE_CENDFILE 12 |
| 2731 | #define CWORD_CWORD_CWORD_CWORD 13 |
| 2732 | #define CCTL_CCTL_CCTL_CCTL 14 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2733 | #else |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2734 | #define CSPCL_CWORD_CWORD_CWORD 0 |
| 2735 | #define CNL_CNL_CNL_CNL 1 |
| 2736 | #define CWORD_CCTL_CCTL_CWORD 2 |
| 2737 | #define CDQUOTE_CENDQUOTE_CWORD_CWORD 3 |
| 2738 | #define CVAR_CVAR_CWORD_CVAR 4 |
| 2739 | #define CSQUOTE_CWORD_CENDQUOTE_CWORD 5 |
| 2740 | #define CSPCL_CWORD_CWORD_CLP 6 |
| 2741 | #define CSPCL_CWORD_CWORD_CRP 7 |
| 2742 | #define CBACK_CBACK_CCTL_CBACK 8 |
| 2743 | #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE 9 |
| 2744 | #define CENDVAR_CENDVAR_CWORD_CENDVAR 10 |
| 2745 | #define CENDFILE_CENDFILE_CENDFILE_CENDFILE 11 |
| 2746 | #define CWORD_CWORD_CWORD_CWORD 12 |
| 2747 | #define CCTL_CCTL_CCTL_CCTL 13 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2748 | #endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2749 | |
| 2750 | static const char syntax_index_table[258] = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2751 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2752 | /* 0 PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2753 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2754 | /* 1 PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 2755 | #endif |
| 2756 | /* 2 -128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 2757 | /* 3 -127 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 2758 | /* 4 -126 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2759 | /* 5 -125 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2760 | /* 6 -124 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 2761 | /* 7 -123 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 2762 | /* 8 -122 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 2763 | /* 9 -121 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 2764 | /* 10 -120 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2765 | /* 11 -119 */ CWORD_CWORD_CWORD_CWORD, |
| 2766 | /* 12 -118 */ CWORD_CWORD_CWORD_CWORD, |
| 2767 | /* 13 -117 */ CWORD_CWORD_CWORD_CWORD, |
| 2768 | /* 14 -116 */ CWORD_CWORD_CWORD_CWORD, |
| 2769 | /* 15 -115 */ CWORD_CWORD_CWORD_CWORD, |
| 2770 | /* 16 -114 */ CWORD_CWORD_CWORD_CWORD, |
| 2771 | /* 17 -113 */ CWORD_CWORD_CWORD_CWORD, |
| 2772 | /* 18 -112 */ CWORD_CWORD_CWORD_CWORD, |
| 2773 | /* 19 -111 */ CWORD_CWORD_CWORD_CWORD, |
| 2774 | /* 20 -110 */ CWORD_CWORD_CWORD_CWORD, |
| 2775 | /* 21 -109 */ CWORD_CWORD_CWORD_CWORD, |
| 2776 | /* 22 -108 */ CWORD_CWORD_CWORD_CWORD, |
| 2777 | /* 23 -107 */ CWORD_CWORD_CWORD_CWORD, |
| 2778 | /* 24 -106 */ CWORD_CWORD_CWORD_CWORD, |
| 2779 | /* 25 -105 */ CWORD_CWORD_CWORD_CWORD, |
| 2780 | /* 26 -104 */ CWORD_CWORD_CWORD_CWORD, |
| 2781 | /* 27 -103 */ CWORD_CWORD_CWORD_CWORD, |
| 2782 | /* 28 -102 */ CWORD_CWORD_CWORD_CWORD, |
| 2783 | /* 29 -101 */ CWORD_CWORD_CWORD_CWORD, |
| 2784 | /* 30 -100 */ CWORD_CWORD_CWORD_CWORD, |
| 2785 | /* 31 -99 */ CWORD_CWORD_CWORD_CWORD, |
| 2786 | /* 32 -98 */ CWORD_CWORD_CWORD_CWORD, |
| 2787 | /* 33 -97 */ CWORD_CWORD_CWORD_CWORD, |
| 2788 | /* 34 -96 */ CWORD_CWORD_CWORD_CWORD, |
| 2789 | /* 35 -95 */ CWORD_CWORD_CWORD_CWORD, |
| 2790 | /* 36 -94 */ CWORD_CWORD_CWORD_CWORD, |
| 2791 | /* 37 -93 */ CWORD_CWORD_CWORD_CWORD, |
| 2792 | /* 38 -92 */ CWORD_CWORD_CWORD_CWORD, |
| 2793 | /* 39 -91 */ CWORD_CWORD_CWORD_CWORD, |
| 2794 | /* 40 -90 */ CWORD_CWORD_CWORD_CWORD, |
| 2795 | /* 41 -89 */ CWORD_CWORD_CWORD_CWORD, |
| 2796 | /* 42 -88 */ CWORD_CWORD_CWORD_CWORD, |
| 2797 | /* 43 -87 */ CWORD_CWORD_CWORD_CWORD, |
| 2798 | /* 44 -86 */ CWORD_CWORD_CWORD_CWORD, |
| 2799 | /* 45 -85 */ CWORD_CWORD_CWORD_CWORD, |
| 2800 | /* 46 -84 */ CWORD_CWORD_CWORD_CWORD, |
| 2801 | /* 47 -83 */ CWORD_CWORD_CWORD_CWORD, |
| 2802 | /* 48 -82 */ CWORD_CWORD_CWORD_CWORD, |
| 2803 | /* 49 -81 */ CWORD_CWORD_CWORD_CWORD, |
| 2804 | /* 50 -80 */ CWORD_CWORD_CWORD_CWORD, |
| 2805 | /* 51 -79 */ CWORD_CWORD_CWORD_CWORD, |
| 2806 | /* 52 -78 */ CWORD_CWORD_CWORD_CWORD, |
| 2807 | /* 53 -77 */ CWORD_CWORD_CWORD_CWORD, |
| 2808 | /* 54 -76 */ CWORD_CWORD_CWORD_CWORD, |
| 2809 | /* 55 -75 */ CWORD_CWORD_CWORD_CWORD, |
| 2810 | /* 56 -74 */ CWORD_CWORD_CWORD_CWORD, |
| 2811 | /* 57 -73 */ CWORD_CWORD_CWORD_CWORD, |
| 2812 | /* 58 -72 */ CWORD_CWORD_CWORD_CWORD, |
| 2813 | /* 59 -71 */ CWORD_CWORD_CWORD_CWORD, |
| 2814 | /* 60 -70 */ CWORD_CWORD_CWORD_CWORD, |
| 2815 | /* 61 -69 */ CWORD_CWORD_CWORD_CWORD, |
| 2816 | /* 62 -68 */ CWORD_CWORD_CWORD_CWORD, |
| 2817 | /* 63 -67 */ CWORD_CWORD_CWORD_CWORD, |
| 2818 | /* 64 -66 */ CWORD_CWORD_CWORD_CWORD, |
| 2819 | /* 65 -65 */ CWORD_CWORD_CWORD_CWORD, |
| 2820 | /* 66 -64 */ CWORD_CWORD_CWORD_CWORD, |
| 2821 | /* 67 -63 */ CWORD_CWORD_CWORD_CWORD, |
| 2822 | /* 68 -62 */ CWORD_CWORD_CWORD_CWORD, |
| 2823 | /* 69 -61 */ CWORD_CWORD_CWORD_CWORD, |
| 2824 | /* 70 -60 */ CWORD_CWORD_CWORD_CWORD, |
| 2825 | /* 71 -59 */ CWORD_CWORD_CWORD_CWORD, |
| 2826 | /* 72 -58 */ CWORD_CWORD_CWORD_CWORD, |
| 2827 | /* 73 -57 */ CWORD_CWORD_CWORD_CWORD, |
| 2828 | /* 74 -56 */ CWORD_CWORD_CWORD_CWORD, |
| 2829 | /* 75 -55 */ CWORD_CWORD_CWORD_CWORD, |
| 2830 | /* 76 -54 */ CWORD_CWORD_CWORD_CWORD, |
| 2831 | /* 77 -53 */ CWORD_CWORD_CWORD_CWORD, |
| 2832 | /* 78 -52 */ CWORD_CWORD_CWORD_CWORD, |
| 2833 | /* 79 -51 */ CWORD_CWORD_CWORD_CWORD, |
| 2834 | /* 80 -50 */ CWORD_CWORD_CWORD_CWORD, |
| 2835 | /* 81 -49 */ CWORD_CWORD_CWORD_CWORD, |
| 2836 | /* 82 -48 */ CWORD_CWORD_CWORD_CWORD, |
| 2837 | /* 83 -47 */ CWORD_CWORD_CWORD_CWORD, |
| 2838 | /* 84 -46 */ CWORD_CWORD_CWORD_CWORD, |
| 2839 | /* 85 -45 */ CWORD_CWORD_CWORD_CWORD, |
| 2840 | /* 86 -44 */ CWORD_CWORD_CWORD_CWORD, |
| 2841 | /* 87 -43 */ CWORD_CWORD_CWORD_CWORD, |
| 2842 | /* 88 -42 */ CWORD_CWORD_CWORD_CWORD, |
| 2843 | /* 89 -41 */ CWORD_CWORD_CWORD_CWORD, |
| 2844 | /* 90 -40 */ CWORD_CWORD_CWORD_CWORD, |
| 2845 | /* 91 -39 */ CWORD_CWORD_CWORD_CWORD, |
| 2846 | /* 92 -38 */ CWORD_CWORD_CWORD_CWORD, |
| 2847 | /* 93 -37 */ CWORD_CWORD_CWORD_CWORD, |
| 2848 | /* 94 -36 */ CWORD_CWORD_CWORD_CWORD, |
| 2849 | /* 95 -35 */ CWORD_CWORD_CWORD_CWORD, |
| 2850 | /* 96 -34 */ CWORD_CWORD_CWORD_CWORD, |
| 2851 | /* 97 -33 */ CWORD_CWORD_CWORD_CWORD, |
| 2852 | /* 98 -32 */ CWORD_CWORD_CWORD_CWORD, |
| 2853 | /* 99 -31 */ CWORD_CWORD_CWORD_CWORD, |
| 2854 | /* 100 -30 */ CWORD_CWORD_CWORD_CWORD, |
| 2855 | /* 101 -29 */ CWORD_CWORD_CWORD_CWORD, |
| 2856 | /* 102 -28 */ CWORD_CWORD_CWORD_CWORD, |
| 2857 | /* 103 -27 */ CWORD_CWORD_CWORD_CWORD, |
| 2858 | /* 104 -26 */ CWORD_CWORD_CWORD_CWORD, |
| 2859 | /* 105 -25 */ CWORD_CWORD_CWORD_CWORD, |
| 2860 | /* 106 -24 */ CWORD_CWORD_CWORD_CWORD, |
| 2861 | /* 107 -23 */ CWORD_CWORD_CWORD_CWORD, |
| 2862 | /* 108 -22 */ CWORD_CWORD_CWORD_CWORD, |
| 2863 | /* 109 -21 */ CWORD_CWORD_CWORD_CWORD, |
| 2864 | /* 110 -20 */ CWORD_CWORD_CWORD_CWORD, |
| 2865 | /* 111 -19 */ CWORD_CWORD_CWORD_CWORD, |
| 2866 | /* 112 -18 */ CWORD_CWORD_CWORD_CWORD, |
| 2867 | /* 113 -17 */ CWORD_CWORD_CWORD_CWORD, |
| 2868 | /* 114 -16 */ CWORD_CWORD_CWORD_CWORD, |
| 2869 | /* 115 -15 */ CWORD_CWORD_CWORD_CWORD, |
| 2870 | /* 116 -14 */ CWORD_CWORD_CWORD_CWORD, |
| 2871 | /* 117 -13 */ CWORD_CWORD_CWORD_CWORD, |
| 2872 | /* 118 -12 */ CWORD_CWORD_CWORD_CWORD, |
| 2873 | /* 119 -11 */ CWORD_CWORD_CWORD_CWORD, |
| 2874 | /* 120 -10 */ CWORD_CWORD_CWORD_CWORD, |
| 2875 | /* 121 -9 */ CWORD_CWORD_CWORD_CWORD, |
| 2876 | /* 122 -8 */ CWORD_CWORD_CWORD_CWORD, |
| 2877 | /* 123 -7 */ CWORD_CWORD_CWORD_CWORD, |
| 2878 | /* 124 -6 */ CWORD_CWORD_CWORD_CWORD, |
| 2879 | /* 125 -5 */ CWORD_CWORD_CWORD_CWORD, |
| 2880 | /* 126 -4 */ CWORD_CWORD_CWORD_CWORD, |
| 2881 | /* 127 -3 */ CWORD_CWORD_CWORD_CWORD, |
| 2882 | /* 128 -2 */ CWORD_CWORD_CWORD_CWORD, |
| 2883 | /* 129 -1 */ CWORD_CWORD_CWORD_CWORD, |
| 2884 | /* 130 0 */ CWORD_CWORD_CWORD_CWORD, |
| 2885 | /* 131 1 */ CWORD_CWORD_CWORD_CWORD, |
| 2886 | /* 132 2 */ CWORD_CWORD_CWORD_CWORD, |
| 2887 | /* 133 3 */ CWORD_CWORD_CWORD_CWORD, |
| 2888 | /* 134 4 */ CWORD_CWORD_CWORD_CWORD, |
| 2889 | /* 135 5 */ CWORD_CWORD_CWORD_CWORD, |
| 2890 | /* 136 6 */ CWORD_CWORD_CWORD_CWORD, |
| 2891 | /* 137 7 */ CWORD_CWORD_CWORD_CWORD, |
| 2892 | /* 138 8 */ CWORD_CWORD_CWORD_CWORD, |
| 2893 | /* 139 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 2894 | /* 140 10 "\n" */ CNL_CNL_CNL_CNL, |
| 2895 | /* 141 11 */ CWORD_CWORD_CWORD_CWORD, |
| 2896 | /* 142 12 */ CWORD_CWORD_CWORD_CWORD, |
| 2897 | /* 143 13 */ CWORD_CWORD_CWORD_CWORD, |
| 2898 | /* 144 14 */ CWORD_CWORD_CWORD_CWORD, |
| 2899 | /* 145 15 */ CWORD_CWORD_CWORD_CWORD, |
| 2900 | /* 146 16 */ CWORD_CWORD_CWORD_CWORD, |
| 2901 | /* 147 17 */ CWORD_CWORD_CWORD_CWORD, |
| 2902 | /* 148 18 */ CWORD_CWORD_CWORD_CWORD, |
| 2903 | /* 149 19 */ CWORD_CWORD_CWORD_CWORD, |
| 2904 | /* 150 20 */ CWORD_CWORD_CWORD_CWORD, |
| 2905 | /* 151 21 */ CWORD_CWORD_CWORD_CWORD, |
| 2906 | /* 152 22 */ CWORD_CWORD_CWORD_CWORD, |
| 2907 | /* 153 23 */ CWORD_CWORD_CWORD_CWORD, |
| 2908 | /* 154 24 */ CWORD_CWORD_CWORD_CWORD, |
| 2909 | /* 155 25 */ CWORD_CWORD_CWORD_CWORD, |
| 2910 | /* 156 26 */ CWORD_CWORD_CWORD_CWORD, |
| 2911 | /* 157 27 */ CWORD_CWORD_CWORD_CWORD, |
| 2912 | /* 158 28 */ CWORD_CWORD_CWORD_CWORD, |
| 2913 | /* 159 29 */ CWORD_CWORD_CWORD_CWORD, |
| 2914 | /* 160 30 */ CWORD_CWORD_CWORD_CWORD, |
| 2915 | /* 161 31 */ CWORD_CWORD_CWORD_CWORD, |
| 2916 | /* 162 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 2917 | /* 163 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2918 | /* 164 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2919 | /* 165 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 2920 | /* 166 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 2921 | /* 167 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 2922 | /* 168 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2923 | /* 169 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2924 | /* 170 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 2925 | /* 171 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 2926 | /* 172 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 2927 | /* 173 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 2928 | /* 174 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 2929 | /* 175 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 2930 | /* 176 46 "." */ CWORD_CWORD_CWORD_CWORD, |
| 2931 | /* 177 47 "/" */ CWORD_CCTL_CCTL_CWORD, |
| 2932 | /* 178 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 2933 | /* 179 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 2934 | /* 180 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 2935 | /* 181 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 2936 | /* 182 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 2937 | /* 183 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 2938 | /* 184 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 2939 | /* 185 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 2940 | /* 186 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 2941 | /* 187 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 2942 | /* 188 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 2943 | /* 189 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 2944 | /* 190 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 2945 | /* 191 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 2946 | /* 192 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 2947 | /* 193 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 2948 | /* 194 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 2949 | /* 195 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 2950 | /* 196 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 2951 | /* 197 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 2952 | /* 198 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 2953 | /* 199 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 2954 | /* 200 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 2955 | /* 201 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 2956 | /* 202 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 2957 | /* 203 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 2958 | /* 204 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 2959 | /* 205 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 2960 | /* 206 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 2961 | /* 207 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 2962 | /* 208 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 2963 | /* 209 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 2964 | /* 210 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 2965 | /* 211 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 2966 | /* 212 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 2967 | /* 213 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 2968 | /* 214 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 2969 | /* 215 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 2970 | /* 216 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 2971 | /* 217 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 2972 | /* 218 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 2973 | /* 219 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 2974 | /* 220 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 2975 | /* 221 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 2976 | /* 222 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 2977 | /* 223 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 2978 | /* 224 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 2979 | /* 225 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 2980 | /* 226 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 2981 | /* 227 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 2982 | /* 228 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 2983 | /* 229 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 2984 | /* 230 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 2985 | /* 231 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 2986 | /* 232 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 2987 | /* 233 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 2988 | /* 234 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 2989 | /* 235 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 2990 | /* 236 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 2991 | /* 237 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 2992 | /* 238 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 2993 | /* 239 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 2994 | /* 240 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 2995 | /* 241 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 2996 | /* 242 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 2997 | /* 243 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 2998 | /* 244 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 2999 | /* 245 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 3000 | /* 246 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 3001 | /* 247 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 3002 | /* 248 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 3003 | /* 249 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 3004 | /* 250 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 3005 | /* 251 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 3006 | /* 252 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 3007 | /* 253 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 3008 | /* 254 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 3009 | /* 255 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 3010 | /* 256 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 3011 | /* 257 127 */ CWORD_CWORD_CWORD_CWORD, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3012 | }; |
| 3013 | |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3014 | #define SIT(c, syntax) (S_I_T[(int)syntax_index_table[(int)(c) + SYNBASE]][syntax]) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 3015 | |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 3016 | #endif /* USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3017 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3018 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3019 | /* ============ Alias handling */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3020 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3021 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3022 | |
| 3023 | #define ALIASINUSE 1 |
| 3024 | #define ALIASDEAD 2 |
| 3025 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3026 | struct alias { |
| 3027 | struct alias *next; |
| 3028 | char *name; |
| 3029 | char *val; |
| 3030 | int flag; |
| 3031 | }; |
| 3032 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 3033 | |
| 3034 | static struct alias **atab; // [ATABSIZE]; |
| 3035 | #define INIT_G_alias() do { \ |
| 3036 | atab = xzalloc(ATABSIZE * sizeof(atab[0])); \ |
| 3037 | } while (0) |
| 3038 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3039 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3040 | static struct alias ** |
| 3041 | __lookupalias(const char *name) { |
| 3042 | unsigned int hashval; |
| 3043 | struct alias **app; |
| 3044 | const char *p; |
| 3045 | unsigned int ch; |
| 3046 | |
| 3047 | p = name; |
| 3048 | |
| 3049 | ch = (unsigned char)*p; |
| 3050 | hashval = ch << 4; |
| 3051 | while (ch) { |
| 3052 | hashval += ch; |
| 3053 | ch = (unsigned char)*++p; |
| 3054 | } |
| 3055 | app = &atab[hashval % ATABSIZE]; |
| 3056 | |
| 3057 | for (; *app; app = &(*app)->next) { |
| 3058 | if (strcmp(name, (*app)->name) == 0) { |
| 3059 | break; |
| 3060 | } |
| 3061 | } |
| 3062 | |
| 3063 | return app; |
| 3064 | } |
| 3065 | |
| 3066 | static struct alias * |
| 3067 | lookupalias(const char *name, int check) |
| 3068 | { |
| 3069 | struct alias *ap = *__lookupalias(name); |
| 3070 | |
| 3071 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 3072 | return NULL; |
| 3073 | return ap; |
| 3074 | } |
| 3075 | |
| 3076 | static struct alias * |
| 3077 | freealias(struct alias *ap) |
| 3078 | { |
| 3079 | struct alias *next; |
| 3080 | |
| 3081 | if (ap->flag & ALIASINUSE) { |
| 3082 | ap->flag |= ALIASDEAD; |
| 3083 | return ap; |
| 3084 | } |
| 3085 | |
| 3086 | next = ap->next; |
| 3087 | free(ap->name); |
| 3088 | free(ap->val); |
| 3089 | free(ap); |
| 3090 | return next; |
| 3091 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3092 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3093 | static void |
| 3094 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3095 | { |
| 3096 | struct alias *ap, **app; |
| 3097 | |
| 3098 | app = __lookupalias(name); |
| 3099 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3100 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3101 | if (ap) { |
| 3102 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3103 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3104 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3105 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3106 | ap->flag &= ~ALIASDEAD; |
| 3107 | } else { |
| 3108 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3109 | ap = ckzalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3110 | ap->name = ckstrdup(name); |
| 3111 | ap->val = ckstrdup(val); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3112 | /*ap->flag = 0; - ckzalloc did it */ |
| 3113 | /*ap->next = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3114 | *app = ap; |
| 3115 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3116 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3119 | static int |
| 3120 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3121 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3122 | struct alias **app; |
| 3123 | |
| 3124 | app = __lookupalias(name); |
| 3125 | |
| 3126 | if (*app) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3127 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3128 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3129 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3130 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3133 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3136 | static void |
| 3137 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3138 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3139 | struct alias *ap, **app; |
| 3140 | int i; |
| 3141 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3142 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3143 | for (i = 0; i < ATABSIZE; i++) { |
| 3144 | app = &atab[i]; |
| 3145 | for (ap = *app; ap; ap = *app) { |
| 3146 | *app = freealias(*app); |
| 3147 | if (ap == *app) { |
| 3148 | app = &ap->next; |
| 3149 | } |
| 3150 | } |
| 3151 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3152 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3155 | static void |
| 3156 | printalias(const struct alias *ap) |
| 3157 | { |
| 3158 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3159 | } |
| 3160 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3161 | /* |
| 3162 | * TODO - sort output |
| 3163 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3164 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3165 | aliascmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3166 | { |
| 3167 | char *n, *v; |
| 3168 | int ret = 0; |
| 3169 | struct alias *ap; |
| 3170 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3171 | if (!argv[1]) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3172 | int i; |
| 3173 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3174 | for (i = 0; i < ATABSIZE; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3175 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3176 | printalias(ap); |
| 3177 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3178 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3179 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3180 | } |
| 3181 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3182 | v = strchr(n+1, '='); |
| 3183 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3184 | ap = *__lookupalias(n); |
| 3185 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3186 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3187 | ret = 1; |
| 3188 | } else |
| 3189 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3190 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3191 | *v++ = '\0'; |
| 3192 | setalias(n, v); |
| 3193 | } |
| 3194 | } |
| 3195 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3196 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3199 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3200 | unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3201 | { |
| 3202 | int i; |
| 3203 | |
| 3204 | while ((i = nextopt("a")) != '\0') { |
| 3205 | if (i == 'a') { |
| 3206 | rmaliases(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3207 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3208 | } |
| 3209 | } |
| 3210 | for (i = 0; *argptr; argptr++) { |
| 3211 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3212 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3213 | i = 1; |
| 3214 | } |
| 3215 | } |
| 3216 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3217 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3218 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3219 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3220 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3221 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3222 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3223 | /* ============ jobs.c */ |
| 3224 | |
| 3225 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
| 3226 | #define FORK_FG 0 |
| 3227 | #define FORK_BG 1 |
| 3228 | #define FORK_NOJOB 2 |
| 3229 | |
| 3230 | /* mode flags for showjob(s) */ |
| 3231 | #define SHOW_PGID 0x01 /* only show pgid - for jobs -p */ |
| 3232 | #define SHOW_PID 0x04 /* include process pid */ |
| 3233 | #define SHOW_CHANGED 0x08 /* only jobs whose state has changed */ |
| 3234 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3235 | /* |
| 3236 | * A job structure contains information about a job. A job is either a |
| 3237 | * single process or a set of processes contained in a pipeline. In the |
| 3238 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 3239 | * array of pids. |
| 3240 | */ |
| 3241 | |
| 3242 | struct procstat { |
| 3243 | pid_t pid; /* process id */ |
| 3244 | int status; /* last process status from wait() */ |
| 3245 | char *cmd; /* text of command being run */ |
| 3246 | }; |
| 3247 | |
| 3248 | struct job { |
| 3249 | struct procstat ps0; /* status of process */ |
| 3250 | struct procstat *ps; /* status or processes when more than one */ |
| 3251 | #if JOBS |
| 3252 | int stopstatus; /* status of a stopped job */ |
| 3253 | #endif |
| 3254 | uint32_t |
| 3255 | nprocs: 16, /* number of processes */ |
| 3256 | state: 8, |
| 3257 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3258 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3259 | #define JOBDONE 2 /* all procs are completed */ |
| 3260 | #if JOBS |
| 3261 | sigint: 1, /* job was killed by SIGINT */ |
| 3262 | jobctl: 1, /* job running under job control */ |
| 3263 | #endif |
| 3264 | waited: 1, /* true if this entry has been waited for */ |
| 3265 | used: 1, /* true if this entry is in used */ |
| 3266 | changed: 1; /* true if status has changed */ |
| 3267 | struct job *prev_job; /* previous job */ |
| 3268 | }; |
| 3269 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3270 | static struct job *makejob(/*union node *,*/ int); |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3271 | #if !JOBS |
| 3272 | #define forkshell(job, node, mode) forkshell(job, mode) |
| 3273 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3274 | static int forkshell(struct job *, union node *, int); |
| 3275 | static int waitforjob(struct job *); |
| 3276 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3277 | #if !JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3278 | enum { doing_jobctl = 0 }; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3279 | #define setjobctl(on) do {} while (0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3280 | #else |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3281 | static smallint doing_jobctl; //references:8 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3282 | static void setjobctl(int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3283 | #endif |
| 3284 | |
| 3285 | /* |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3286 | * Ignore a signal. |
| 3287 | */ |
| 3288 | static void |
| 3289 | ignoresig(int signo) |
| 3290 | { |
| 3291 | /* Avoid unnecessary system calls. Is it already SIG_IGNed? */ |
| 3292 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 3293 | /* No, need to do it */ |
| 3294 | signal(signo, SIG_IGN); |
| 3295 | } |
| 3296 | sigmode[signo - 1] = S_HARD_IGN; |
| 3297 | } |
| 3298 | |
| 3299 | /* |
| 3300 | * Signal handler. Only one usage site - in setsignal() |
| 3301 | */ |
| 3302 | static void |
| 3303 | onsig(int signo) |
| 3304 | { |
| 3305 | gotsig[signo - 1] = 1; |
| 3306 | |
| 3307 | if (/* exsig || */ (signo == SIGINT && !trap[SIGINT])) { |
| 3308 | if (!suppressint) { |
| 3309 | pendingsig = 0; |
| 3310 | raise_interrupt(); /* does not return */ |
| 3311 | } |
| 3312 | intpending = 1; |
| 3313 | } else { |
| 3314 | pendingsig = signo; |
| 3315 | } |
| 3316 | } |
| 3317 | |
| 3318 | /* |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3319 | * Set the signal handler for the specified signal. The routine figures |
| 3320 | * out what it should be set to. |
| 3321 | */ |
| 3322 | static void |
| 3323 | setsignal(int signo) |
| 3324 | { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3325 | char *t; |
| 3326 | char cur_act, new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3327 | struct sigaction act; |
| 3328 | |
| 3329 | t = trap[signo]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3330 | new_act = S_DFL; |
| 3331 | if (t != NULL) { /* trap for this sig is set */ |
| 3332 | new_act = S_CATCH; |
| 3333 | if (t[0] == '\0') /* trap is "": ignore this sig */ |
| 3334 | new_act = S_IGN; |
| 3335 | } |
| 3336 | |
| 3337 | if (rootshell && new_act == S_DFL) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3338 | switch (signo) { |
| 3339 | case SIGINT: |
| 3340 | if (iflag || minusc || sflag == 0) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3341 | new_act = S_CATCH; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3342 | break; |
| 3343 | case SIGQUIT: |
| 3344 | #if DEBUG |
| 3345 | if (debug) |
| 3346 | break; |
| 3347 | #endif |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3348 | /* man bash: |
| 3349 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 3350 | * commands run by bash have signal handlers |
| 3351 | * set to the values inherited by the shell |
| 3352 | * from its parent". */ |
| 3353 | new_act = S_IGN; |
| 3354 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3355 | case SIGTERM: |
| 3356 | if (iflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3357 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3358 | break; |
| 3359 | #if JOBS |
| 3360 | case SIGTSTP: |
| 3361 | case SIGTTOU: |
| 3362 | if (mflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3363 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3364 | break; |
| 3365 | #endif |
| 3366 | } |
| 3367 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3368 | //TODO: if !rootshell, we reset SIGQUIT to DFL, |
| 3369 | //whereas we have to restore it to what shell got on entry |
| 3370 | //from the parent. See comment above |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3371 | |
| 3372 | t = &sigmode[signo - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3373 | cur_act = *t; |
| 3374 | if (cur_act == 0) { |
| 3375 | /* current setting is not yet known */ |
| 3376 | if (sigaction(signo, NULL, &act)) { |
| 3377 | /* pretend it worked; maybe we should give a warning, |
| 3378 | * but other shells don't. We don't alter sigmode, |
| 3379 | * so we retry every time. |
| 3380 | * btw, in Linux it never fails. --vda */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3381 | return; |
| 3382 | } |
| 3383 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3384 | cur_act = S_HARD_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3385 | if (mflag |
| 3386 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
| 3387 | ) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3388 | cur_act = S_IGN; /* don't hard ignore these */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3389 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3390 | } |
| 3391 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3392 | if (cur_act == S_HARD_IGN || cur_act == new_act) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3393 | return; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3394 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3395 | act.sa_handler = SIG_DFL; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3396 | switch (new_act) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3397 | case S_CATCH: |
| 3398 | act.sa_handler = onsig; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3399 | act.sa_flags = 0; /* matters only if !DFL and !IGN */ |
| 3400 | sigfillset(&act.sa_mask); /* ditto */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3401 | break; |
| 3402 | case S_IGN: |
| 3403 | act.sa_handler = SIG_IGN; |
| 3404 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3405 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3406 | sigaction_set(signo, &act); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3407 | |
| 3408 | *t = new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | /* mode flags for set_curjob */ |
| 3412 | #define CUR_DELETE 2 |
| 3413 | #define CUR_RUNNING 1 |
| 3414 | #define CUR_STOPPED 0 |
| 3415 | |
| 3416 | /* mode flags for dowait */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3417 | #define DOWAIT_NONBLOCK WNOHANG |
| 3418 | #define DOWAIT_BLOCK 0 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3419 | |
| 3420 | #if JOBS |
| 3421 | /* pgrp of shell on invocation */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3422 | static int initialpgrp; //references:2 |
| 3423 | static int ttyfd = -1; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3424 | #endif |
| 3425 | /* array of jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3426 | static struct job *jobtab; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3427 | /* size of array */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3428 | static unsigned njobs; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3429 | /* current job */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3430 | static struct job *curjob; //lots |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3431 | /* number of presumed living untracked jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3432 | static int jobless; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3433 | |
| 3434 | static void |
| 3435 | set_curjob(struct job *jp, unsigned mode) |
| 3436 | { |
| 3437 | struct job *jp1; |
| 3438 | struct job **jpp, **curp; |
| 3439 | |
| 3440 | /* first remove from list */ |
| 3441 | jpp = curp = &curjob; |
| 3442 | do { |
| 3443 | jp1 = *jpp; |
| 3444 | if (jp1 == jp) |
| 3445 | break; |
| 3446 | jpp = &jp1->prev_job; |
| 3447 | } while (1); |
| 3448 | *jpp = jp1->prev_job; |
| 3449 | |
| 3450 | /* Then re-insert in correct position */ |
| 3451 | jpp = curp; |
| 3452 | switch (mode) { |
| 3453 | default: |
| 3454 | #if DEBUG |
| 3455 | abort(); |
| 3456 | #endif |
| 3457 | case CUR_DELETE: |
| 3458 | /* job being deleted */ |
| 3459 | break; |
| 3460 | case CUR_RUNNING: |
| 3461 | /* newly created job or backgrounded job, |
| 3462 | put after all stopped jobs. */ |
| 3463 | do { |
| 3464 | jp1 = *jpp; |
| 3465 | #if JOBS |
| 3466 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3467 | #endif |
| 3468 | break; |
| 3469 | jpp = &jp1->prev_job; |
| 3470 | } while (1); |
| 3471 | /* FALLTHROUGH */ |
| 3472 | #if JOBS |
| 3473 | case CUR_STOPPED: |
| 3474 | #endif |
| 3475 | /* newly stopped job - becomes curjob */ |
| 3476 | jp->prev_job = *jpp; |
| 3477 | *jpp = jp; |
| 3478 | break; |
| 3479 | } |
| 3480 | } |
| 3481 | |
| 3482 | #if JOBS || DEBUG |
| 3483 | static int |
| 3484 | jobno(const struct job *jp) |
| 3485 | { |
| 3486 | return jp - jobtab + 1; |
| 3487 | } |
| 3488 | #endif |
| 3489 | |
| 3490 | /* |
| 3491 | * Convert a job name to a job structure. |
| 3492 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3493 | #if !JOBS |
| 3494 | #define getjob(name, getctl) getjob(name) |
| 3495 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3496 | static struct job * |
| 3497 | getjob(const char *name, int getctl) |
| 3498 | { |
| 3499 | struct job *jp; |
| 3500 | struct job *found; |
| 3501 | const char *err_msg = "No such job: %s"; |
| 3502 | unsigned num; |
| 3503 | int c; |
| 3504 | const char *p; |
| 3505 | char *(*match)(const char *, const char *); |
| 3506 | |
| 3507 | jp = curjob; |
| 3508 | p = name; |
| 3509 | if (!p) |
| 3510 | goto currentjob; |
| 3511 | |
| 3512 | if (*p != '%') |
| 3513 | goto err; |
| 3514 | |
| 3515 | c = *++p; |
| 3516 | if (!c) |
| 3517 | goto currentjob; |
| 3518 | |
| 3519 | if (!p[1]) { |
| 3520 | if (c == '+' || c == '%') { |
| 3521 | currentjob: |
| 3522 | err_msg = "No current job"; |
| 3523 | goto check; |
| 3524 | } |
| 3525 | if (c == '-') { |
| 3526 | if (jp) |
| 3527 | jp = jp->prev_job; |
| 3528 | err_msg = "No previous job"; |
| 3529 | check: |
| 3530 | if (!jp) |
| 3531 | goto err; |
| 3532 | goto gotit; |
| 3533 | } |
| 3534 | } |
| 3535 | |
| 3536 | if (is_number(p)) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 3537 | // TODO: number() instead? It does error checking... |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3538 | num = atoi(p); |
| 3539 | if (num < njobs) { |
| 3540 | jp = jobtab + num - 1; |
| 3541 | if (jp->used) |
| 3542 | goto gotit; |
| 3543 | goto err; |
| 3544 | } |
| 3545 | } |
| 3546 | |
| 3547 | match = prefix; |
| 3548 | if (*p == '?') { |
| 3549 | match = strstr; |
| 3550 | p++; |
| 3551 | } |
| 3552 | |
| 3553 | found = 0; |
| 3554 | while (1) { |
| 3555 | if (!jp) |
| 3556 | goto err; |
| 3557 | if (match(jp->ps[0].cmd, p)) { |
| 3558 | if (found) |
| 3559 | goto err; |
| 3560 | found = jp; |
| 3561 | err_msg = "%s: ambiguous"; |
| 3562 | } |
| 3563 | jp = jp->prev_job; |
| 3564 | } |
| 3565 | |
| 3566 | gotit: |
| 3567 | #if JOBS |
| 3568 | err_msg = "job %s not created under job control"; |
| 3569 | if (getctl && jp->jobctl == 0) |
| 3570 | goto err; |
| 3571 | #endif |
| 3572 | return jp; |
| 3573 | err: |
| 3574 | ash_msg_and_raise_error(err_msg, name); |
| 3575 | } |
| 3576 | |
| 3577 | /* |
| 3578 | * Mark a job structure as unused. |
| 3579 | */ |
| 3580 | static void |
| 3581 | freejob(struct job *jp) |
| 3582 | { |
| 3583 | struct procstat *ps; |
| 3584 | int i; |
| 3585 | |
| 3586 | INT_OFF; |
| 3587 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
| 3588 | if (ps->cmd != nullstr) |
| 3589 | free(ps->cmd); |
| 3590 | } |
| 3591 | if (jp->ps != &jp->ps0) |
| 3592 | free(jp->ps); |
| 3593 | jp->used = 0; |
| 3594 | set_curjob(jp, CUR_DELETE); |
| 3595 | INT_ON; |
| 3596 | } |
| 3597 | |
| 3598 | #if JOBS |
| 3599 | static void |
| 3600 | xtcsetpgrp(int fd, pid_t pgrp) |
| 3601 | { |
| 3602 | if (tcsetpgrp(fd, pgrp)) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 3603 | ash_msg_and_raise_error("can't set tty process group (%m)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
| 3606 | /* |
| 3607 | * Turn job control on and off. |
| 3608 | * |
| 3609 | * Note: This code assumes that the third arg to ioctl is a character |
| 3610 | * pointer, which is true on Berkeley systems but not System V. Since |
| 3611 | * System V doesn't have job control yet, this isn't a problem now. |
| 3612 | * |
| 3613 | * Called with interrupts off. |
| 3614 | */ |
| 3615 | static void |
| 3616 | setjobctl(int on) |
| 3617 | { |
| 3618 | int fd; |
| 3619 | int pgrp; |
| 3620 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3621 | if (on == doing_jobctl || rootshell == 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3622 | return; |
| 3623 | if (on) { |
| 3624 | int ofd; |
| 3625 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 3626 | if (fd < 0) { |
| 3627 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 3628 | * That sometimes helps to acquire controlling tty. |
| 3629 | * Obviously, a workaround for bugs when someone |
| 3630 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3631 | fd = 2; |
| 3632 | while (!isatty(fd)) |
| 3633 | if (--fd < 0) |
| 3634 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3635 | } |
| 3636 | fd = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3637 | if (ofd >= 0) |
| 3638 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3639 | if (fd < 0) |
| 3640 | goto out; |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3641 | /* fd is a tty at this point */ |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 3642 | close_on_exec_on(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3643 | do { /* while we are in the background */ |
| 3644 | pgrp = tcgetpgrp(fd); |
| 3645 | if (pgrp < 0) { |
| 3646 | out: |
| 3647 | ash_msg("can't access tty; job control turned off"); |
| 3648 | mflag = on = 0; |
| 3649 | goto close; |
| 3650 | } |
| 3651 | if (pgrp == getpgrp()) |
| 3652 | break; |
| 3653 | killpg(0, SIGTTIN); |
| 3654 | } while (1); |
| 3655 | initialpgrp = pgrp; |
| 3656 | |
| 3657 | setsignal(SIGTSTP); |
| 3658 | setsignal(SIGTTOU); |
| 3659 | setsignal(SIGTTIN); |
| 3660 | pgrp = rootpid; |
| 3661 | setpgid(0, pgrp); |
| 3662 | xtcsetpgrp(fd, pgrp); |
| 3663 | } else { |
| 3664 | /* turning job control off */ |
| 3665 | fd = ttyfd; |
| 3666 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3667 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3668 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3669 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3670 | setpgid(0, pgrp); |
| 3671 | setsignal(SIGTSTP); |
| 3672 | setsignal(SIGTTOU); |
| 3673 | setsignal(SIGTTIN); |
| 3674 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3675 | if (fd >= 0) |
| 3676 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3677 | fd = -1; |
| 3678 | } |
| 3679 | ttyfd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3680 | doing_jobctl = on; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3683 | static int FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3684 | killcmd(int argc, char **argv) |
| 3685 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3686 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3687 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3688 | do { |
| 3689 | if (argv[i][0] == '%') { |
| 3690 | struct job *jp = getjob(argv[i], 0); |
| 3691 | unsigned pid = jp->ps[0].pid; |
| 3692 | /* Enough space for ' -NNN<nul>' */ |
| 3693 | argv[i] = alloca(sizeof(int)*3 + 3); |
| 3694 | /* kill_main has matching code to expect |
| 3695 | * leading space. Needed to not confuse |
| 3696 | * negative pids with "kill -SIGNAL_NO" syntax */ |
| 3697 | sprintf(argv[i], " -%u", pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3698 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3699 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3700 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3701 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | static void |
| 3705 | showpipe(struct job *jp, FILE *out) |
| 3706 | { |
| 3707 | struct procstat *sp; |
| 3708 | struct procstat *spend; |
| 3709 | |
| 3710 | spend = jp->ps + jp->nprocs; |
| 3711 | for (sp = jp->ps + 1; sp < spend; sp++) |
| 3712 | fprintf(out, " | %s", sp->cmd); |
| 3713 | outcslow('\n', out); |
| 3714 | flush_stdout_stderr(); |
| 3715 | } |
| 3716 | |
| 3717 | |
| 3718 | static int |
| 3719 | restartjob(struct job *jp, int mode) |
| 3720 | { |
| 3721 | struct procstat *ps; |
| 3722 | int i; |
| 3723 | int status; |
| 3724 | pid_t pgid; |
| 3725 | |
| 3726 | INT_OFF; |
| 3727 | if (jp->state == JOBDONE) |
| 3728 | goto out; |
| 3729 | jp->state = JOBRUNNING; |
| 3730 | pgid = jp->ps->pid; |
| 3731 | if (mode == FORK_FG) |
| 3732 | xtcsetpgrp(ttyfd, pgid); |
| 3733 | killpg(pgid, SIGCONT); |
| 3734 | ps = jp->ps; |
| 3735 | i = jp->nprocs; |
| 3736 | do { |
| 3737 | if (WIFSTOPPED(ps->status)) { |
| 3738 | ps->status = -1; |
| 3739 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3740 | ps++; |
| 3741 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3742 | out: |
| 3743 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 3744 | INT_ON; |
| 3745 | return status; |
| 3746 | } |
| 3747 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3748 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3749 | fg_bgcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3750 | { |
| 3751 | struct job *jp; |
| 3752 | FILE *out; |
| 3753 | int mode; |
| 3754 | int retval; |
| 3755 | |
| 3756 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 3757 | nextopt(nullstr); |
| 3758 | argv = argptr; |
| 3759 | out = stdout; |
| 3760 | do { |
| 3761 | jp = getjob(*argv, 1); |
| 3762 | if (mode == FORK_BG) { |
| 3763 | set_curjob(jp, CUR_RUNNING); |
| 3764 | fprintf(out, "[%d] ", jobno(jp)); |
| 3765 | } |
| 3766 | outstr(jp->ps->cmd, out); |
| 3767 | showpipe(jp, out); |
| 3768 | retval = restartjob(jp, mode); |
| 3769 | } while (*argv && *++argv); |
| 3770 | return retval; |
| 3771 | } |
| 3772 | #endif |
| 3773 | |
| 3774 | static int |
| 3775 | sprint_status(char *s, int status, int sigonly) |
| 3776 | { |
| 3777 | int col; |
| 3778 | int st; |
| 3779 | |
| 3780 | col = 0; |
| 3781 | if (!WIFEXITED(status)) { |
| 3782 | #if JOBS |
| 3783 | if (WIFSTOPPED(status)) |
| 3784 | st = WSTOPSIG(status); |
| 3785 | else |
| 3786 | #endif |
| 3787 | st = WTERMSIG(status); |
| 3788 | if (sigonly) { |
| 3789 | if (st == SIGINT || st == SIGPIPE) |
| 3790 | goto out; |
| 3791 | #if JOBS |
| 3792 | if (WIFSTOPPED(status)) |
| 3793 | goto out; |
| 3794 | #endif |
| 3795 | } |
| 3796 | st &= 0x7f; |
| 3797 | col = fmtstr(s, 32, strsignal(st)); |
| 3798 | if (WCOREDUMP(status)) { |
| 3799 | col += fmtstr(s + col, 16, " (core dumped)"); |
| 3800 | } |
| 3801 | } else if (!sigonly) { |
| 3802 | st = WEXITSTATUS(status); |
| 3803 | if (st) |
| 3804 | col = fmtstr(s, 16, "Done(%d)", st); |
| 3805 | else |
| 3806 | col = fmtstr(s, 16, "Done"); |
| 3807 | } |
| 3808 | out: |
| 3809 | return col; |
| 3810 | } |
| 3811 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3812 | static int |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3813 | dowait(int wait_flags, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3814 | { |
| 3815 | int pid; |
| 3816 | int status; |
| 3817 | struct job *jp; |
| 3818 | struct job *thisjob; |
| 3819 | int state; |
| 3820 | |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 3821 | TRACE(("dowait(0x%x) called\n", wait_flags)); |
| 3822 | |
| 3823 | /* Do a wait system call. If job control is compiled in, we accept |
| 3824 | * stopped processes. wait_flags may have WNOHANG, preventing blocking. |
| 3825 | * NB: _not_ safe_waitpid, we need to detect EINTR */ |
| 3826 | pid = waitpid(-1, &status, |
| 3827 | (doing_jobctl ? (wait_flags | WUNTRACED) : wait_flags)); |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 3828 | TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n", |
| 3829 | pid, status, errno, strerror(errno))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3830 | if (pid <= 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3831 | return pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3832 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3833 | INT_OFF; |
| 3834 | thisjob = NULL; |
| 3835 | for (jp = curjob; jp; jp = jp->prev_job) { |
| 3836 | struct procstat *sp; |
| 3837 | struct procstat *spend; |
| 3838 | if (jp->state == JOBDONE) |
| 3839 | continue; |
| 3840 | state = JOBDONE; |
| 3841 | spend = jp->ps + jp->nprocs; |
| 3842 | sp = jp->ps; |
| 3843 | do { |
| 3844 | if (sp->pid == pid) { |
| 3845 | TRACE(("Job %d: changing status of proc %d " |
| 3846 | "from 0x%x to 0x%x\n", |
| 3847 | jobno(jp), pid, sp->status, status)); |
| 3848 | sp->status = status; |
| 3849 | thisjob = jp; |
| 3850 | } |
| 3851 | if (sp->status == -1) |
| 3852 | state = JOBRUNNING; |
| 3853 | #if JOBS |
| 3854 | if (state == JOBRUNNING) |
| 3855 | continue; |
| 3856 | if (WIFSTOPPED(sp->status)) { |
| 3857 | jp->stopstatus = sp->status; |
| 3858 | state = JOBSTOPPED; |
| 3859 | } |
| 3860 | #endif |
| 3861 | } while (++sp < spend); |
| 3862 | if (thisjob) |
| 3863 | goto gotjob; |
| 3864 | } |
| 3865 | #if JOBS |
| 3866 | if (!WIFSTOPPED(status)) |
| 3867 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3868 | jobless--; |
| 3869 | goto out; |
| 3870 | |
| 3871 | gotjob: |
| 3872 | if (state != JOBRUNNING) { |
| 3873 | thisjob->changed = 1; |
| 3874 | |
| 3875 | if (thisjob->state != state) { |
| 3876 | TRACE(("Job %d: changing state from %d to %d\n", |
| 3877 | jobno(thisjob), thisjob->state, state)); |
| 3878 | thisjob->state = state; |
| 3879 | #if JOBS |
| 3880 | if (state == JOBSTOPPED) { |
| 3881 | set_curjob(thisjob, CUR_STOPPED); |
| 3882 | } |
| 3883 | #endif |
| 3884 | } |
| 3885 | } |
| 3886 | |
| 3887 | out: |
| 3888 | INT_ON; |
| 3889 | |
| 3890 | if (thisjob && thisjob == job) { |
| 3891 | char s[48 + 1]; |
| 3892 | int len; |
| 3893 | |
| 3894 | len = sprint_status(s, status, 1); |
| 3895 | if (len) { |
| 3896 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3897 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3898 | out2str(s); |
| 3899 | } |
| 3900 | } |
| 3901 | return pid; |
| 3902 | } |
| 3903 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3904 | static int |
| 3905 | blocking_wait_with_raise_on_sig(struct job *job) |
| 3906 | { |
| 3907 | pid_t pid = dowait(DOWAIT_BLOCK, job); |
| 3908 | if (pid <= 0 && pendingsig) |
| 3909 | raise_exception(EXSIG); |
| 3910 | return pid; |
| 3911 | } |
| 3912 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3913 | #if JOBS |
| 3914 | static void |
| 3915 | showjob(FILE *out, struct job *jp, int mode) |
| 3916 | { |
| 3917 | struct procstat *ps; |
| 3918 | struct procstat *psend; |
| 3919 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3920 | int indent_col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3921 | char s[80]; |
| 3922 | |
| 3923 | ps = jp->ps; |
| 3924 | |
| 3925 | if (mode & SHOW_PGID) { |
| 3926 | /* just output process (group) id of pipeline */ |
| 3927 | fprintf(out, "%d\n", ps->pid); |
| 3928 | return; |
| 3929 | } |
| 3930 | |
| 3931 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3932 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3933 | |
| 3934 | if (jp == curjob) |
| 3935 | s[col - 2] = '+'; |
| 3936 | else if (curjob && jp == curjob->prev_job) |
| 3937 | s[col - 2] = '-'; |
| 3938 | |
| 3939 | if (mode & SHOW_PID) |
| 3940 | col += fmtstr(s + col, 16, "%d ", ps->pid); |
| 3941 | |
| 3942 | psend = ps + jp->nprocs; |
| 3943 | |
| 3944 | if (jp->state == JOBRUNNING) { |
| 3945 | strcpy(s + col, "Running"); |
| 3946 | col += sizeof("Running") - 1; |
| 3947 | } else { |
| 3948 | int status = psend[-1].status; |
| 3949 | if (jp->state == JOBSTOPPED) |
| 3950 | status = jp->stopstatus; |
| 3951 | col += sprint_status(s + col, status, 0); |
| 3952 | } |
| 3953 | |
| 3954 | goto start; |
| 3955 | |
| 3956 | do { |
| 3957 | /* for each process */ |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3958 | col = fmtstr(s, 48, " |\n%*c%d ", indent_col, ' ', ps->pid) - 3; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3959 | start: |
| 3960 | fprintf(out, "%s%*c%s", |
| 3961 | s, 33 - col >= 0 ? 33 - col : 0, ' ', ps->cmd |
| 3962 | ); |
| 3963 | if (!(mode & SHOW_PID)) { |
| 3964 | showpipe(jp, out); |
| 3965 | break; |
| 3966 | } |
| 3967 | if (++ps == psend) { |
| 3968 | outcslow('\n', out); |
| 3969 | break; |
| 3970 | } |
| 3971 | } while (1); |
| 3972 | |
| 3973 | jp->changed = 0; |
| 3974 | |
| 3975 | if (jp->state == JOBDONE) { |
| 3976 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 3977 | freejob(jp); |
| 3978 | } |
| 3979 | } |
| 3980 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3981 | /* |
| 3982 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 3983 | * statuses have changed since the last call to showjobs. |
| 3984 | */ |
| 3985 | static void |
| 3986 | showjobs(FILE *out, int mode) |
| 3987 | { |
| 3988 | struct job *jp; |
| 3989 | |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame^] | 3990 | TRACE(("showjobs(0x%x) called\n", mode)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3991 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3992 | /* Handle all finished jobs */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3993 | while (dowait(DOWAIT_NONBLOCK, NULL) > 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3994 | continue; |
| 3995 | |
| 3996 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3997 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3998 | showjob(out, jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3999 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4000 | } |
| 4001 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4002 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4003 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4004 | jobscmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4005 | { |
| 4006 | int mode, m; |
| 4007 | |
| 4008 | mode = 0; |
| 4009 | while ((m = nextopt("lp"))) { |
| 4010 | if (m == 'l') |
| 4011 | mode = SHOW_PID; |
| 4012 | else |
| 4013 | mode = SHOW_PGID; |
| 4014 | } |
| 4015 | |
| 4016 | argv = argptr; |
| 4017 | if (*argv) { |
| 4018 | do |
| 4019 | showjob(stdout, getjob(*argv,0), mode); |
| 4020 | while (*++argv); |
| 4021 | } else |
| 4022 | showjobs(stdout, mode); |
| 4023 | |
| 4024 | return 0; |
| 4025 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4026 | #endif /* JOBS */ |
| 4027 | |
| 4028 | static int |
| 4029 | getstatus(struct job *job) |
| 4030 | { |
| 4031 | int status; |
| 4032 | int retval; |
| 4033 | |
| 4034 | status = job->ps[job->nprocs - 1].status; |
| 4035 | retval = WEXITSTATUS(status); |
| 4036 | if (!WIFEXITED(status)) { |
| 4037 | #if JOBS |
| 4038 | retval = WSTOPSIG(status); |
| 4039 | if (!WIFSTOPPED(status)) |
| 4040 | #endif |
| 4041 | { |
| 4042 | /* XXX: limits number of signals */ |
| 4043 | retval = WTERMSIG(status); |
| 4044 | #if JOBS |
| 4045 | if (retval == SIGINT) |
| 4046 | job->sigint = 1; |
| 4047 | #endif |
| 4048 | } |
| 4049 | retval += 128; |
| 4050 | } |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame^] | 4051 | TRACE(("getstatus: job %d, nproc %d, status 0x%x, retval 0x%x\n", |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4052 | jobno(job), job->nprocs, status, retval)); |
| 4053 | return retval; |
| 4054 | } |
| 4055 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4056 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4057 | waitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4058 | { |
| 4059 | struct job *job; |
| 4060 | int retval; |
| 4061 | struct job *jp; |
| 4062 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4063 | // exsig++; |
| 4064 | // xbarrier(); |
| 4065 | if (pendingsig) |
| 4066 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4067 | |
| 4068 | nextopt(nullstr); |
| 4069 | retval = 0; |
| 4070 | |
| 4071 | argv = argptr; |
| 4072 | if (!*argv) { |
| 4073 | /* wait for all jobs */ |
| 4074 | for (;;) { |
| 4075 | jp = curjob; |
| 4076 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4077 | if (!jp) /* no running procs */ |
| 4078 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4079 | if (jp->state == JOBRUNNING) |
| 4080 | break; |
| 4081 | jp->waited = 1; |
| 4082 | jp = jp->prev_job; |
| 4083 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4084 | /* man bash: |
| 4085 | * "When bash is waiting for an asynchronous command via |
| 4086 | * the wait builtin, the reception of a signal for which a trap |
| 4087 | * has been set will cause the wait builtin to return immediately |
| 4088 | * with an exit status greater than 128, immediately after which |
| 4089 | * the trap is executed." |
| 4090 | * Do we do it that way? */ |
| 4091 | blocking_wait_with_raise_on_sig(NULL); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4092 | } |
| 4093 | } |
| 4094 | |
| 4095 | retval = 127; |
| 4096 | do { |
| 4097 | if (**argv != '%') { |
| 4098 | pid_t pid = number(*argv); |
| 4099 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4100 | while (1) { |
| 4101 | if (!job) |
| 4102 | goto repeat; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4103 | if (job->ps[job->nprocs - 1].pid == pid) |
| 4104 | break; |
| 4105 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4106 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4107 | } else |
| 4108 | job = getjob(*argv, 0); |
| 4109 | /* loop until process terminated or stopped */ |
| 4110 | while (job->state == JOBRUNNING) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4111 | blocking_wait_with_raise_on_sig(NULL); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4112 | job->waited = 1; |
| 4113 | retval = getstatus(job); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4114 | repeat: ; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4115 | } while (*++argv); |
| 4116 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4117 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4118 | return retval; |
| 4119 | } |
| 4120 | |
| 4121 | static struct job * |
| 4122 | growjobtab(void) |
| 4123 | { |
| 4124 | size_t len; |
| 4125 | ptrdiff_t offset; |
| 4126 | struct job *jp, *jq; |
| 4127 | |
| 4128 | len = njobs * sizeof(*jp); |
| 4129 | jq = jobtab; |
| 4130 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4131 | |
| 4132 | offset = (char *)jp - (char *)jq; |
| 4133 | if (offset) { |
| 4134 | /* Relocate pointers */ |
| 4135 | size_t l = len; |
| 4136 | |
| 4137 | jq = (struct job *)((char *)jq + l); |
| 4138 | while (l) { |
| 4139 | l -= sizeof(*jp); |
| 4140 | jq--; |
| 4141 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4142 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4143 | if (joff(jp)->ps == &jq->ps0) |
| 4144 | jmove(joff(jp)->ps); |
| 4145 | if (joff(jp)->prev_job) |
| 4146 | jmove(joff(jp)->prev_job); |
| 4147 | } |
| 4148 | if (curjob) |
| 4149 | jmove(curjob); |
| 4150 | #undef joff |
| 4151 | #undef jmove |
| 4152 | } |
| 4153 | |
| 4154 | njobs += 4; |
| 4155 | jobtab = jp; |
| 4156 | jp = (struct job *)((char *)jp + len); |
| 4157 | jq = jp + 3; |
| 4158 | do { |
| 4159 | jq->used = 0; |
| 4160 | } while (--jq >= jp); |
| 4161 | return jp; |
| 4162 | } |
| 4163 | |
| 4164 | /* |
| 4165 | * Return a new job structure. |
| 4166 | * Called with interrupts off. |
| 4167 | */ |
| 4168 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4169 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4170 | { |
| 4171 | int i; |
| 4172 | struct job *jp; |
| 4173 | |
| 4174 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4175 | if (--i < 0) { |
| 4176 | jp = growjobtab(); |
| 4177 | break; |
| 4178 | } |
| 4179 | if (jp->used == 0) |
| 4180 | break; |
| 4181 | if (jp->state != JOBDONE || !jp->waited) |
| 4182 | continue; |
| 4183 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4184 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4185 | continue; |
| 4186 | #endif |
| 4187 | freejob(jp); |
| 4188 | break; |
| 4189 | } |
| 4190 | memset(jp, 0, sizeof(*jp)); |
| 4191 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4192 | /* jp->jobctl is a bitfield. |
| 4193 | * "jp->jobctl |= jobctl" likely to give awful code */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4194 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4195 | jp->jobctl = 1; |
| 4196 | #endif |
| 4197 | jp->prev_job = curjob; |
| 4198 | curjob = jp; |
| 4199 | jp->used = 1; |
| 4200 | jp->ps = &jp->ps0; |
| 4201 | if (nprocs > 1) { |
| 4202 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4203 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4204 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4205 | jobno(jp))); |
| 4206 | return jp; |
| 4207 | } |
| 4208 | |
| 4209 | #if JOBS |
| 4210 | /* |
| 4211 | * Return a string identifying a command (to be printed by the |
| 4212 | * jobs command). |
| 4213 | */ |
| 4214 | static char *cmdnextc; |
| 4215 | |
| 4216 | static void |
| 4217 | cmdputs(const char *s) |
| 4218 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4219 | static const char vstype[VSTYPE + 1][3] = { |
| 4220 | "", "}", "-", "+", "?", "=", |
| 4221 | "%", "%%", "#", "##" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4222 | IF_ASH_BASH_COMPAT(, ":", "/", "//") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4223 | }; |
| 4224 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4225 | const char *p, *str; |
| 4226 | char c, cc[2] = " "; |
| 4227 | char *nextc; |
| 4228 | int subtype = 0; |
| 4229 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4230 | |
| 4231 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4232 | p = s; |
| 4233 | while ((c = *p++) != 0) { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 4234 | str = NULL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4235 | switch (c) { |
| 4236 | case CTLESC: |
| 4237 | c = *p++; |
| 4238 | break; |
| 4239 | case CTLVAR: |
| 4240 | subtype = *p++; |
| 4241 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4242 | str = "${#"; |
| 4243 | else |
| 4244 | str = "${"; |
| 4245 | if (!(subtype & VSQUOTE) == !(quoted & 1)) |
| 4246 | goto dostr; |
| 4247 | quoted ^= 1; |
| 4248 | c = '"'; |
| 4249 | break; |
| 4250 | case CTLENDVAR: |
| 4251 | str = "\"}" + !(quoted & 1); |
| 4252 | quoted >>= 1; |
| 4253 | subtype = 0; |
| 4254 | goto dostr; |
| 4255 | case CTLBACKQ: |
| 4256 | str = "$(...)"; |
| 4257 | goto dostr; |
| 4258 | case CTLBACKQ+CTLQUOTE: |
| 4259 | str = "\"$(...)\""; |
| 4260 | goto dostr; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4261 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4262 | case CTLARI: |
| 4263 | str = "$(("; |
| 4264 | goto dostr; |
| 4265 | case CTLENDARI: |
| 4266 | str = "))"; |
| 4267 | goto dostr; |
| 4268 | #endif |
| 4269 | case CTLQUOTEMARK: |
| 4270 | quoted ^= 1; |
| 4271 | c = '"'; |
| 4272 | break; |
| 4273 | case '=': |
| 4274 | if (subtype == 0) |
| 4275 | break; |
| 4276 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4277 | quoted <<= 1; |
| 4278 | str = vstype[subtype & VSTYPE]; |
| 4279 | if (subtype & VSNUL) |
| 4280 | c = ':'; |
| 4281 | else |
| 4282 | goto checkstr; |
| 4283 | break; |
| 4284 | case '\'': |
| 4285 | case '\\': |
| 4286 | case '"': |
| 4287 | case '$': |
| 4288 | /* These can only happen inside quotes */ |
| 4289 | cc[0] = c; |
| 4290 | str = cc; |
| 4291 | c = '\\'; |
| 4292 | break; |
| 4293 | default: |
| 4294 | break; |
| 4295 | } |
| 4296 | USTPUTC(c, nextc); |
| 4297 | checkstr: |
| 4298 | if (!str) |
| 4299 | continue; |
| 4300 | dostr: |
| 4301 | while ((c = *str++)) { |
| 4302 | USTPUTC(c, nextc); |
| 4303 | } |
| 4304 | } |
| 4305 | if (quoted & 1) { |
| 4306 | USTPUTC('"', nextc); |
| 4307 | } |
| 4308 | *nextc = 0; |
| 4309 | cmdnextc = nextc; |
| 4310 | } |
| 4311 | |
| 4312 | /* cmdtxt() and cmdlist() call each other */ |
| 4313 | static void cmdtxt(union node *n); |
| 4314 | |
| 4315 | static void |
| 4316 | cmdlist(union node *np, int sep) |
| 4317 | { |
| 4318 | for (; np; np = np->narg.next) { |
| 4319 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4320 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4321 | cmdtxt(np); |
| 4322 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4323 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4324 | } |
| 4325 | } |
| 4326 | |
| 4327 | static void |
| 4328 | cmdtxt(union node *n) |
| 4329 | { |
| 4330 | union node *np; |
| 4331 | struct nodelist *lp; |
| 4332 | const char *p; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4333 | |
| 4334 | if (!n) |
| 4335 | return; |
| 4336 | switch (n->type) { |
| 4337 | default: |
| 4338 | #if DEBUG |
| 4339 | abort(); |
| 4340 | #endif |
| 4341 | case NPIPE: |
| 4342 | lp = n->npipe.cmdlist; |
| 4343 | for (;;) { |
| 4344 | cmdtxt(lp->n); |
| 4345 | lp = lp->next; |
| 4346 | if (!lp) |
| 4347 | break; |
| 4348 | cmdputs(" | "); |
| 4349 | } |
| 4350 | break; |
| 4351 | case NSEMI: |
| 4352 | p = "; "; |
| 4353 | goto binop; |
| 4354 | case NAND: |
| 4355 | p = " && "; |
| 4356 | goto binop; |
| 4357 | case NOR: |
| 4358 | p = " || "; |
| 4359 | binop: |
| 4360 | cmdtxt(n->nbinary.ch1); |
| 4361 | cmdputs(p); |
| 4362 | n = n->nbinary.ch2; |
| 4363 | goto donode; |
| 4364 | case NREDIR: |
| 4365 | case NBACKGND: |
| 4366 | n = n->nredir.n; |
| 4367 | goto donode; |
| 4368 | case NNOT: |
| 4369 | cmdputs("!"); |
| 4370 | n = n->nnot.com; |
| 4371 | donode: |
| 4372 | cmdtxt(n); |
| 4373 | break; |
| 4374 | case NIF: |
| 4375 | cmdputs("if "); |
| 4376 | cmdtxt(n->nif.test); |
| 4377 | cmdputs("; then "); |
| 4378 | n = n->nif.ifpart; |
| 4379 | if (n->nif.elsepart) { |
| 4380 | cmdtxt(n); |
| 4381 | cmdputs("; else "); |
| 4382 | n = n->nif.elsepart; |
| 4383 | } |
| 4384 | p = "; fi"; |
| 4385 | goto dotail; |
| 4386 | case NSUBSHELL: |
| 4387 | cmdputs("("); |
| 4388 | n = n->nredir.n; |
| 4389 | p = ")"; |
| 4390 | goto dotail; |
| 4391 | case NWHILE: |
| 4392 | p = "while "; |
| 4393 | goto until; |
| 4394 | case NUNTIL: |
| 4395 | p = "until "; |
| 4396 | until: |
| 4397 | cmdputs(p); |
| 4398 | cmdtxt(n->nbinary.ch1); |
| 4399 | n = n->nbinary.ch2; |
| 4400 | p = "; done"; |
| 4401 | dodo: |
| 4402 | cmdputs("; do "); |
| 4403 | dotail: |
| 4404 | cmdtxt(n); |
| 4405 | goto dotail2; |
| 4406 | case NFOR: |
| 4407 | cmdputs("for "); |
| 4408 | cmdputs(n->nfor.var); |
| 4409 | cmdputs(" in "); |
| 4410 | cmdlist(n->nfor.args, 1); |
| 4411 | n = n->nfor.body; |
| 4412 | p = "; done"; |
| 4413 | goto dodo; |
| 4414 | case NDEFUN: |
| 4415 | cmdputs(n->narg.text); |
| 4416 | p = "() { ... }"; |
| 4417 | goto dotail2; |
| 4418 | case NCMD: |
| 4419 | cmdlist(n->ncmd.args, 1); |
| 4420 | cmdlist(n->ncmd.redirect, 0); |
| 4421 | break; |
| 4422 | case NARG: |
| 4423 | p = n->narg.text; |
| 4424 | dotail2: |
| 4425 | cmdputs(p); |
| 4426 | break; |
| 4427 | case NHERE: |
| 4428 | case NXHERE: |
| 4429 | p = "<<..."; |
| 4430 | goto dotail2; |
| 4431 | case NCASE: |
| 4432 | cmdputs("case "); |
| 4433 | cmdputs(n->ncase.expr->narg.text); |
| 4434 | cmdputs(" in "); |
| 4435 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 4436 | cmdtxt(np->nclist.pattern); |
| 4437 | cmdputs(") "); |
| 4438 | cmdtxt(np->nclist.body); |
| 4439 | cmdputs(";; "); |
| 4440 | } |
| 4441 | p = "esac"; |
| 4442 | goto dotail2; |
| 4443 | case NTO: |
| 4444 | p = ">"; |
| 4445 | goto redir; |
| 4446 | case NCLOBBER: |
| 4447 | p = ">|"; |
| 4448 | goto redir; |
| 4449 | case NAPPEND: |
| 4450 | p = ">>"; |
| 4451 | goto redir; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4452 | #if ENABLE_ASH_BASH_COMPAT |
| 4453 | case NTO2: |
| 4454 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4455 | case NTOFD: |
| 4456 | p = ">&"; |
| 4457 | goto redir; |
| 4458 | case NFROM: |
| 4459 | p = "<"; |
| 4460 | goto redir; |
| 4461 | case NFROMFD: |
| 4462 | p = "<&"; |
| 4463 | goto redir; |
| 4464 | case NFROMTO: |
| 4465 | p = "<>"; |
| 4466 | redir: |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4467 | cmdputs(utoa(n->nfile.fd)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4468 | cmdputs(p); |
| 4469 | if (n->type == NTOFD || n->type == NFROMFD) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4470 | cmdputs(utoa(n->ndup.dupfd)); |
| 4471 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4472 | } |
| 4473 | n = n->nfile.fname; |
| 4474 | goto donode; |
| 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | static char * |
| 4479 | commandtext(union node *n) |
| 4480 | { |
| 4481 | char *name; |
| 4482 | |
| 4483 | STARTSTACKSTR(cmdnextc); |
| 4484 | cmdtxt(n); |
| 4485 | name = stackblock(); |
| 4486 | TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n", |
| 4487 | name, cmdnextc, cmdnextc)); |
| 4488 | return ckstrdup(name); |
| 4489 | } |
| 4490 | #endif /* JOBS */ |
| 4491 | |
| 4492 | /* |
| 4493 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 4494 | * own process group. Jp is a job structure that the job is to be added to. |
| 4495 | * N is the command that will be evaluated by the child. Both jp and n may |
| 4496 | * be NULL. The mode parameter can be one of the following: |
| 4497 | * FORK_FG - Fork off a foreground process. |
| 4498 | * FORK_BG - Fork off a background process. |
| 4499 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 4500 | * process group even if job control is on. |
| 4501 | * |
| 4502 | * When job control is turned off, background processes have their standard |
| 4503 | * input redirected to /dev/null (except for the second and later processes |
| 4504 | * in a pipeline). |
| 4505 | * |
| 4506 | * Called with interrupts off. |
| 4507 | */ |
| 4508 | /* |
| 4509 | * Clear traps on a fork. |
| 4510 | */ |
| 4511 | static void |
| 4512 | clear_traps(void) |
| 4513 | { |
| 4514 | char **tp; |
| 4515 | |
| 4516 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4517 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4518 | INT_OFF; |
| 4519 | free(*tp); |
| 4520 | *tp = NULL; |
| 4521 | if (tp != &trap[0]) |
| 4522 | setsignal(tp - trap); |
| 4523 | INT_ON; |
| 4524 | } |
| 4525 | } |
| 4526 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4527 | |
| 4528 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4529 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 4530 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4531 | /* Called after fork(), in child */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4532 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4533 | forkchild(struct job *jp, /*union node *n,*/ int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4534 | { |
| 4535 | int oldlvl; |
| 4536 | |
| 4537 | TRACE(("Child shell %d\n", getpid())); |
| 4538 | oldlvl = shlvl; |
| 4539 | shlvl++; |
| 4540 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4541 | /* man bash: "Non-builtin commands run by bash have signal handlers |
| 4542 | * set to the values inherited by the shell from its parent". |
| 4543 | * Do we do it correctly? */ |
| 4544 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4545 | closescript(); |
| 4546 | clear_traps(); |
| 4547 | #if JOBS |
| 4548 | /* do job control only in root shell */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4549 | doing_jobctl = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4550 | if (mode != FORK_NOJOB && jp->jobctl && !oldlvl) { |
| 4551 | pid_t pgrp; |
| 4552 | |
| 4553 | if (jp->nprocs == 0) |
| 4554 | pgrp = getpid(); |
| 4555 | else |
| 4556 | pgrp = jp->ps[0].pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4557 | /* this can fail because we are doing it in the parent also */ |
| 4558 | setpgid(0, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4559 | if (mode == FORK_FG) |
| 4560 | xtcsetpgrp(ttyfd, pgrp); |
| 4561 | setsignal(SIGTSTP); |
| 4562 | setsignal(SIGTTOU); |
| 4563 | } else |
| 4564 | #endif |
| 4565 | if (mode == FORK_BG) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4566 | /* man bash: "When job control is not in effect, |
| 4567 | * asynchronous commands ignore SIGINT and SIGQUIT" */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4568 | ignoresig(SIGINT); |
| 4569 | ignoresig(SIGQUIT); |
| 4570 | if (jp->nprocs == 0) { |
| 4571 | close(0); |
| 4572 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 4573 | ash_msg_and_raise_error("can't open '%s'", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4574 | } |
| 4575 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4576 | if (!oldlvl) { |
| 4577 | if (iflag) { /* why if iflag only? */ |
| 4578 | setsignal(SIGINT); |
| 4579 | setsignal(SIGTERM); |
| 4580 | } |
| 4581 | /* man bash: |
| 4582 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 4583 | * commands run by bash have signal handlers |
| 4584 | * set to the values inherited by the shell |
| 4585 | * from its parent". |
| 4586 | * Take care of the second rule: */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4587 | setsignal(SIGQUIT); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4588 | } |
| 4589 | for (jp = curjob; jp; jp = jp->prev_job) |
| 4590 | freejob(jp); |
| 4591 | jobless = 0; |
| 4592 | } |
| 4593 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4594 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 4595 | #if !JOBS |
| 4596 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 4597 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4598 | static void |
| 4599 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 4600 | { |
| 4601 | TRACE(("In parent shell: child = %d\n", pid)); |
| 4602 | if (!jp) { |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4603 | while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0) |
| 4604 | continue; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4605 | jobless++; |
| 4606 | return; |
| 4607 | } |
| 4608 | #if JOBS |
| 4609 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 4610 | int pgrp; |
| 4611 | |
| 4612 | if (jp->nprocs == 0) |
| 4613 | pgrp = pid; |
| 4614 | else |
| 4615 | pgrp = jp->ps[0].pid; |
| 4616 | /* This can fail because we are doing it in the child also */ |
| 4617 | setpgid(pid, pgrp); |
| 4618 | } |
| 4619 | #endif |
| 4620 | if (mode == FORK_BG) { |
| 4621 | backgndpid = pid; /* set $! */ |
| 4622 | set_curjob(jp, CUR_RUNNING); |
| 4623 | } |
| 4624 | if (jp) { |
| 4625 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
| 4626 | ps->pid = pid; |
| 4627 | ps->status = -1; |
| 4628 | ps->cmd = nullstr; |
| 4629 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4630 | if (doing_jobctl && n) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4631 | ps->cmd = commandtext(n); |
| 4632 | #endif |
| 4633 | } |
| 4634 | } |
| 4635 | |
| 4636 | static int |
| 4637 | forkshell(struct job *jp, union node *n, int mode) |
| 4638 | { |
| 4639 | int pid; |
| 4640 | |
| 4641 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 4642 | pid = fork(); |
| 4643 | if (pid < 0) { |
| 4644 | TRACE(("Fork failed, errno=%d", errno)); |
| 4645 | if (jp) |
| 4646 | freejob(jp); |
Denis Vlasenko | fa0b56d | 2008-07-01 16:09:07 +0000 | [diff] [blame] | 4647 | ash_msg_and_raise_error("can't fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4648 | } |
| 4649 | if (pid == 0) |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4650 | forkchild(jp, /*n,*/ mode); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4651 | else |
| 4652 | forkparent(jp, n, mode, pid); |
| 4653 | return pid; |
| 4654 | } |
| 4655 | |
| 4656 | /* |
| 4657 | * Wait for job to finish. |
| 4658 | * |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4659 | * Under job control we have the problem that while a child process |
| 4660 | * is running interrupts generated by the user are sent to the child |
| 4661 | * but not to the shell. This means that an infinite loop started by |
| 4662 | * an interactive user may be hard to kill. With job control turned off, |
| 4663 | * an interactive user may place an interactive program inside a loop. |
| 4664 | * If the interactive program catches interrupts, the user doesn't want |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4665 | * these interrupts to also abort the loop. The approach we take here |
| 4666 | * is to have the shell ignore interrupt signals while waiting for a |
| 4667 | * foreground process to terminate, and then send itself an interrupt |
| 4668 | * signal if the child process was terminated by an interrupt signal. |
| 4669 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 4670 | * exit on interrupt; unless these processes terminate themselves by |
| 4671 | * sending a signal to themselves (instead of calling exit) they will |
| 4672 | * confuse this approach. |
| 4673 | * |
| 4674 | * Called with interrupts off. |
| 4675 | */ |
| 4676 | static int |
| 4677 | waitforjob(struct job *jp) |
| 4678 | { |
| 4679 | int st; |
| 4680 | |
| 4681 | TRACE(("waitforjob(%%%d) called\n", jobno(jp))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4682 | |
| 4683 | INT_OFF; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4684 | while (jp->state == JOBRUNNING) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4685 | /* In non-interactive shells, we _can_ get |
| 4686 | * a keyboard signal here and be EINTRed, |
| 4687 | * but we just loop back, waiting for command to complete. |
| 4688 | * |
| 4689 | * man bash: |
| 4690 | * "If bash is waiting for a command to complete and receives |
| 4691 | * a signal for which a trap has been set, the trap |
| 4692 | * will not be executed until the command completes." |
| 4693 | * |
| 4694 | * Reality is that even if trap is not set, bash |
| 4695 | * will not act on the signal until command completes. |
| 4696 | * Try this. sleep5intoff.c: |
| 4697 | * #include <signal.h> |
| 4698 | * #include <unistd.h> |
| 4699 | * int main() { |
| 4700 | * sigset_t set; |
| 4701 | * sigemptyset(&set); |
| 4702 | * sigaddset(&set, SIGINT); |
| 4703 | * sigaddset(&set, SIGQUIT); |
| 4704 | * sigprocmask(SIG_BLOCK, &set, NULL); |
| 4705 | * sleep(5); |
| 4706 | * return 0; |
| 4707 | * } |
| 4708 | * $ bash -c './sleep5intoff; echo hi' |
| 4709 | * ^C^C^C^C <--- pressing ^C once a second |
| 4710 | * $ _ |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4711 | * $ bash -c './sleep5intoff; echo hi' |
| 4712 | * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT) |
| 4713 | * $ _ |
| 4714 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4715 | dowait(DOWAIT_BLOCK, jp); |
| 4716 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4717 | INT_ON; |
| 4718 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4719 | st = getstatus(jp); |
| 4720 | #if JOBS |
| 4721 | if (jp->jobctl) { |
| 4722 | xtcsetpgrp(ttyfd, rootpid); |
| 4723 | /* |
| 4724 | * This is truly gross. |
| 4725 | * If we're doing job control, then we did a TIOCSPGRP which |
| 4726 | * caused us (the shell) to no longer be in the controlling |
| 4727 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 4728 | * intuit from the subprocess exit status whether a SIGINT |
| 4729 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 4730 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4731 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 4732 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4733 | } |
| 4734 | if (jp->state == JOBDONE) |
| 4735 | #endif |
| 4736 | freejob(jp); |
| 4737 | return st; |
| 4738 | } |
| 4739 | |
| 4740 | /* |
| 4741 | * return 1 if there are stopped jobs, otherwise 0 |
| 4742 | */ |
| 4743 | static int |
| 4744 | stoppedjobs(void) |
| 4745 | { |
| 4746 | struct job *jp; |
| 4747 | int retval; |
| 4748 | |
| 4749 | retval = 0; |
| 4750 | if (job_warning) |
| 4751 | goto out; |
| 4752 | jp = curjob; |
| 4753 | if (jp && jp->state == JOBSTOPPED) { |
| 4754 | out2str("You have stopped jobs.\n"); |
| 4755 | job_warning = 2; |
| 4756 | retval++; |
| 4757 | } |
| 4758 | out: |
| 4759 | return retval; |
| 4760 | } |
| 4761 | |
| 4762 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4763 | /* ============ redir.c |
| 4764 | * |
| 4765 | * Code for dealing with input/output redirection. |
| 4766 | */ |
| 4767 | |
| 4768 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4769 | #define CLOSED -3 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4770 | |
| 4771 | /* |
| 4772 | * Open a file in noclobber mode. |
| 4773 | * The code was copied from bash. |
| 4774 | */ |
| 4775 | static int |
| 4776 | noclobberopen(const char *fname) |
| 4777 | { |
| 4778 | int r, fd; |
| 4779 | struct stat finfo, finfo2; |
| 4780 | |
| 4781 | /* |
| 4782 | * If the file exists and is a regular file, return an error |
| 4783 | * immediately. |
| 4784 | */ |
| 4785 | r = stat(fname, &finfo); |
| 4786 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 4787 | errno = EEXIST; |
| 4788 | return -1; |
| 4789 | } |
| 4790 | |
| 4791 | /* |
| 4792 | * If the file was not present (r != 0), make sure we open it |
| 4793 | * exclusively so that if it is created before we open it, our open |
| 4794 | * will fail. Make sure that we do not truncate an existing file. |
| 4795 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 4796 | * file was not a regular file, we leave O_EXCL off. |
| 4797 | */ |
| 4798 | if (r != 0) |
| 4799 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 4800 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
| 4801 | |
| 4802 | /* If the open failed, return the file descriptor right away. */ |
| 4803 | if (fd < 0) |
| 4804 | return fd; |
| 4805 | |
| 4806 | /* |
| 4807 | * OK, the open succeeded, but the file may have been changed from a |
| 4808 | * non-regular file to a regular file between the stat and the open. |
| 4809 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 4810 | * did not exist and is symlinked to an existing file between the stat |
| 4811 | * and open. |
| 4812 | */ |
| 4813 | |
| 4814 | /* |
| 4815 | * If we can open it and fstat the file descriptor, and neither check |
| 4816 | * revealed that it was a regular file, and the file has not been |
| 4817 | * replaced, return the file descriptor. |
| 4818 | */ |
| 4819 | if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode) |
| 4820 | && finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino) |
| 4821 | return fd; |
| 4822 | |
| 4823 | /* The file has been replaced. badness. */ |
| 4824 | close(fd); |
| 4825 | errno = EEXIST; |
| 4826 | return -1; |
| 4827 | } |
| 4828 | |
| 4829 | /* |
| 4830 | * Handle here documents. Normally we fork off a process to write the |
| 4831 | * data to a pipe. If the document is short, we can stuff the data in |
| 4832 | * the pipe without forking. |
| 4833 | */ |
| 4834 | /* openhere needs this forward reference */ |
| 4835 | static void expandhere(union node *arg, int fd); |
| 4836 | static int |
| 4837 | openhere(union node *redir) |
| 4838 | { |
| 4839 | int pip[2]; |
| 4840 | size_t len = 0; |
| 4841 | |
| 4842 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 4843 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4844 | if (redir->type == NHERE) { |
| 4845 | len = strlen(redir->nhere.doc->narg.text); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4846 | if (len <= PIPE_BUF) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4847 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
| 4848 | goto out; |
| 4849 | } |
| 4850 | } |
| 4851 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4852 | /* child */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4853 | close(pip[0]); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4854 | ignoresig(SIGINT); //signal(SIGINT, SIG_IGN); |
| 4855 | ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN); |
| 4856 | ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN); |
| 4857 | ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4858 | signal(SIGPIPE, SIG_DFL); |
| 4859 | if (redir->type == NHERE) |
| 4860 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4861 | else /* NXHERE */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4862 | expandhere(redir->nhere.doc, pip[1]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 4863 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4864 | } |
| 4865 | out: |
| 4866 | close(pip[1]); |
| 4867 | return pip[0]; |
| 4868 | } |
| 4869 | |
| 4870 | static int |
| 4871 | openredirect(union node *redir) |
| 4872 | { |
| 4873 | char *fname; |
| 4874 | int f; |
| 4875 | |
| 4876 | switch (redir->nfile.type) { |
| 4877 | case NFROM: |
| 4878 | fname = redir->nfile.expfname; |
| 4879 | f = open(fname, O_RDONLY); |
| 4880 | if (f < 0) |
| 4881 | goto eopen; |
| 4882 | break; |
| 4883 | case NFROMTO: |
| 4884 | fname = redir->nfile.expfname; |
| 4885 | f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666); |
| 4886 | if (f < 0) |
| 4887 | goto ecreate; |
| 4888 | break; |
| 4889 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4890 | #if ENABLE_ASH_BASH_COMPAT |
| 4891 | case NTO2: |
| 4892 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4893 | /* Take care of noclobber mode. */ |
| 4894 | if (Cflag) { |
| 4895 | fname = redir->nfile.expfname; |
| 4896 | f = noclobberopen(fname); |
| 4897 | if (f < 0) |
| 4898 | goto ecreate; |
| 4899 | break; |
| 4900 | } |
| 4901 | /* FALLTHROUGH */ |
| 4902 | case NCLOBBER: |
| 4903 | fname = redir->nfile.expfname; |
| 4904 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 4905 | if (f < 0) |
| 4906 | goto ecreate; |
| 4907 | break; |
| 4908 | case NAPPEND: |
| 4909 | fname = redir->nfile.expfname; |
| 4910 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 4911 | if (f < 0) |
| 4912 | goto ecreate; |
| 4913 | break; |
| 4914 | default: |
| 4915 | #if DEBUG |
| 4916 | abort(); |
| 4917 | #endif |
| 4918 | /* Fall through to eliminate warning. */ |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 4919 | /* Our single caller does this itself */ |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4920 | // case NTOFD: |
| 4921 | // case NFROMFD: |
| 4922 | // f = -1; |
| 4923 | // break; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4924 | case NHERE: |
| 4925 | case NXHERE: |
| 4926 | f = openhere(redir); |
| 4927 | break; |
| 4928 | } |
| 4929 | |
| 4930 | return f; |
| 4931 | ecreate: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 4932 | ash_msg_and_raise_error("can't create %s: %s", fname, errmsg(errno, "nonexistent directory")); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4933 | eopen: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 4934 | ash_msg_and_raise_error("can't open %s: %s", fname, errmsg(errno, "no such file")); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | /* |
| 4938 | * Copy a file descriptor to be >= to. Returns -1 |
| 4939 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 4940 | * file descriptors left. |
| 4941 | */ |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 4942 | /* 0x800..00: bit to set in "to" to request dup2 instead of fcntl(F_DUPFD). |
| 4943 | * old code was doing close(to) prior to copyfd() to achieve the same */ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 4944 | enum { |
| 4945 | COPYFD_EXACT = (int)~(INT_MAX), |
| 4946 | COPYFD_RESTORE = (int)((unsigned)COPYFD_EXACT >> 1), |
| 4947 | }; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4948 | static int |
| 4949 | copyfd(int from, int to) |
| 4950 | { |
| 4951 | int newfd; |
| 4952 | |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 4953 | if (to & COPYFD_EXACT) { |
| 4954 | to &= ~COPYFD_EXACT; |
| 4955 | /*if (from != to)*/ |
| 4956 | newfd = dup2(from, to); |
| 4957 | } else { |
| 4958 | newfd = fcntl(from, F_DUPFD, to); |
| 4959 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4960 | if (newfd < 0) { |
| 4961 | if (errno == EMFILE) |
| 4962 | return EMPTY; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4963 | /* Happens when source fd is not open: try "echo >&99" */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4964 | ash_msg_and_raise_error("%d: %m", from); |
| 4965 | } |
| 4966 | return newfd; |
| 4967 | } |
| 4968 | |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 4969 | /* Struct def and variable are moved down to the first usage site */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4970 | struct two_fd_t { |
| 4971 | int orig, copy; |
| 4972 | }; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4973 | struct redirtab { |
| 4974 | struct redirtab *next; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4975 | int nullredirs; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4976 | int pair_count; |
| 4977 | struct two_fd_t two_fd[0]; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4978 | }; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 4979 | #define redirlist (G_var.redirlist) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4980 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4981 | static int need_to_remember(struct redirtab *rp, int fd) |
| 4982 | { |
| 4983 | int i; |
| 4984 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 4985 | if (!rp) /* remembering was not requested */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4986 | return 0; |
| 4987 | |
| 4988 | for (i = 0; i < rp->pair_count; i++) { |
| 4989 | if (rp->two_fd[i].orig == fd) { |
| 4990 | /* already remembered */ |
| 4991 | return 0; |
| 4992 | } |
| 4993 | } |
| 4994 | return 1; |
| 4995 | } |
| 4996 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 4997 | /* "hidden" fd is a fd used to read scripts, or a copy of such */ |
| 4998 | static int is_hidden_fd(struct redirtab *rp, int fd) |
| 4999 | { |
| 5000 | int i; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5001 | struct parsefile *pf; |
| 5002 | |
| 5003 | if (fd == -1) |
| 5004 | return 0; |
| 5005 | pf = g_parsefile; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5006 | while (pf) { |
| 5007 | if (fd == pf->fd) { |
| 5008 | return 1; |
| 5009 | } |
| 5010 | pf = pf->prev; |
| 5011 | } |
| 5012 | if (!rp) |
| 5013 | return 0; |
| 5014 | fd |= COPYFD_RESTORE; |
| 5015 | for (i = 0; i < rp->pair_count; i++) { |
| 5016 | if (rp->two_fd[i].copy == fd) { |
| 5017 | return 1; |
| 5018 | } |
| 5019 | } |
| 5020 | return 0; |
| 5021 | } |
| 5022 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5023 | /* |
| 5024 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 5025 | * old file descriptors are stashed away so that the redirection can be |
| 5026 | * undone by calling popredir. If the REDIR_BACKQ flag is set, then the |
| 5027 | * standard output, and the standard error if it becomes a duplicate of |
| 5028 | * stdout, is saved in memory. |
| 5029 | */ |
| 5030 | /* flags passed to redirect */ |
| 5031 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
| 5032 | #define REDIR_SAVEFD2 03 /* set preverrout */ |
| 5033 | static void |
| 5034 | redirect(union node *redir, int flags) |
| 5035 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5036 | struct redirtab *sv; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5037 | int sv_pos; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5038 | int i; |
| 5039 | int fd; |
| 5040 | int newfd; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5041 | int copied_fd2 = -1; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5042 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5043 | g_nullredirs++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5044 | if (!redir) { |
| 5045 | return; |
| 5046 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5047 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5048 | sv = NULL; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5049 | sv_pos = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5050 | INT_OFF; |
| 5051 | if (flags & REDIR_PUSH) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5052 | union node *tmp = redir; |
| 5053 | do { |
| 5054 | sv_pos++; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5055 | #if ENABLE_ASH_BASH_COMPAT |
| 5056 | if (redir->nfile.type == NTO2) |
| 5057 | sv_pos++; |
| 5058 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5059 | tmp = tmp->nfile.next; |
| 5060 | } while (tmp); |
| 5061 | sv = ckmalloc(sizeof(*sv) + sv_pos * sizeof(sv->two_fd[0])); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5062 | sv->next = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5063 | sv->pair_count = sv_pos; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5064 | redirlist = sv; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5065 | sv->nullredirs = g_nullredirs - 1; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5066 | g_nullredirs = 0; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5067 | while (sv_pos > 0) { |
| 5068 | sv_pos--; |
| 5069 | sv->two_fd[sv_pos].orig = sv->two_fd[sv_pos].copy = EMPTY; |
| 5070 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5071 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5072 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5073 | do { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5074 | fd = redir->nfile.fd; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5075 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5076 | int right_fd = redir->ndup.dupfd; |
| 5077 | /* redirect from/to same file descriptor? */ |
| 5078 | if (right_fd == fd) |
| 5079 | continue; |
| 5080 | /* echo >&10 and 10 is a fd opened to the sh script? */ |
| 5081 | if (is_hidden_fd(sv, right_fd)) { |
| 5082 | errno = EBADF; /* as if it is closed */ |
| 5083 | ash_msg_and_raise_error("%d: %m", right_fd); |
| 5084 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5085 | newfd = -1; |
| 5086 | } else { |
| 5087 | newfd = openredirect(redir); /* always >= 0 */ |
| 5088 | if (fd == newfd) { |
| 5089 | /* Descriptor wasn't open before redirect. |
| 5090 | * Mark it for close in the future */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5091 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5092 | goto remember_to_close; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5093 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5094 | continue; |
| 5095 | } |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5096 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5097 | #if ENABLE_ASH_BASH_COMPAT |
| 5098 | redirect_more: |
| 5099 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5100 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5101 | /* Copy old descriptor */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5102 | i = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5103 | /* You'd expect copy to be CLOEXECed. Currently these extra "saved" fds |
| 5104 | * are closed in popredir() in the child, preventing them from leaking |
| 5105 | * into child. (popredir() also cleans up the mess in case of failures) |
| 5106 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5107 | if (i == -1) { |
| 5108 | i = errno; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5109 | if (i != EBADF) { |
| 5110 | /* Strange error (e.g. "too many files" EMFILE?) */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5111 | if (newfd >= 0) |
| 5112 | close(newfd); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5113 | errno = i; |
| 5114 | ash_msg_and_raise_error("%d: %m", fd); |
| 5115 | /* NOTREACHED */ |
| 5116 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5117 | /* EBADF: it is not open - good, remember to close it */ |
| 5118 | remember_to_close: |
| 5119 | i = CLOSED; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5120 | } else { /* fd is open, save its copy */ |
| 5121 | /* "exec fd>&-" should not close fds |
| 5122 | * which point to script file(s). |
| 5123 | * Force them to be restored afterwards */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5124 | if (is_hidden_fd(sv, fd)) |
| 5125 | i |= COPYFD_RESTORE; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5126 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5127 | if (fd == 2) |
| 5128 | copied_fd2 = i; |
| 5129 | sv->two_fd[sv_pos].orig = fd; |
| 5130 | sv->two_fd[sv_pos].copy = i; |
| 5131 | sv_pos++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5132 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5133 | if (newfd < 0) { |
| 5134 | /* NTOFD/NFROMFD: copy redir->ndup.dupfd to fd */ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5135 | if (redir->ndup.dupfd < 0) { /* "fd>&-" */ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5136 | /* Don't want to trigger debugging */ |
| 5137 | if (fd != -1) |
| 5138 | close(fd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5139 | } else { |
| 5140 | copyfd(redir->ndup.dupfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5141 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5142 | } else if (fd != newfd) { /* move newfd to fd */ |
| 5143 | copyfd(newfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5144 | #if ENABLE_ASH_BASH_COMPAT |
| 5145 | if (!(redir->nfile.type == NTO2 && fd == 2)) |
| 5146 | #endif |
| 5147 | close(newfd); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5148 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5149 | #if ENABLE_ASH_BASH_COMPAT |
| 5150 | if (redir->nfile.type == NTO2 && fd == 1) { |
| 5151 | /* We already redirected it to fd 1, now copy it to 2 */ |
| 5152 | newfd = 1; |
| 5153 | fd = 2; |
| 5154 | goto redirect_more; |
| 5155 | } |
| 5156 | #endif |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5157 | } while ((redir = redir->nfile.next) != NULL); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5158 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5159 | INT_ON; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5160 | if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0) |
| 5161 | preverrout_fd = copied_fd2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5162 | } |
| 5163 | |
| 5164 | /* |
| 5165 | * Undo the effects of the last redirection. |
| 5166 | */ |
| 5167 | static void |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5168 | popredir(int drop, int restore) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5169 | { |
| 5170 | struct redirtab *rp; |
| 5171 | int i; |
| 5172 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5173 | if (--g_nullredirs >= 0) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5174 | return; |
| 5175 | INT_OFF; |
| 5176 | rp = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5177 | for (i = 0; i < rp->pair_count; i++) { |
| 5178 | int fd = rp->two_fd[i].orig; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5179 | int copy = rp->two_fd[i].copy; |
| 5180 | if (copy == CLOSED) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5181 | if (!drop) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5182 | close(fd); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5183 | continue; |
| 5184 | } |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5185 | if (copy != EMPTY) { |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5186 | if (!drop || (restore && (copy & COPYFD_RESTORE))) { |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5187 | copy &= ~COPYFD_RESTORE; |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5188 | /*close(fd);*/ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5189 | copyfd(copy, fd | COPYFD_EXACT); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5190 | } |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5191 | close(copy & ~COPYFD_RESTORE); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5192 | } |
| 5193 | } |
| 5194 | redirlist = rp->next; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5195 | g_nullredirs = rp->nullredirs; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5196 | free(rp); |
| 5197 | INT_ON; |
| 5198 | } |
| 5199 | |
| 5200 | /* |
| 5201 | * Undo all redirections. Called on error or interrupt. |
| 5202 | */ |
| 5203 | |
| 5204 | /* |
| 5205 | * Discard all saved file descriptors. |
| 5206 | */ |
| 5207 | static void |
| 5208 | clearredir(int drop) |
| 5209 | { |
| 5210 | for (;;) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5211 | g_nullredirs = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5212 | if (!redirlist) |
| 5213 | break; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5214 | popredir(drop, /*restore:*/ 0); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5215 | } |
| 5216 | } |
| 5217 | |
| 5218 | static int |
| 5219 | redirectsafe(union node *redir, int flags) |
| 5220 | { |
| 5221 | int err; |
| 5222 | volatile int saveint; |
| 5223 | struct jmploc *volatile savehandler = exception_handler; |
| 5224 | struct jmploc jmploc; |
| 5225 | |
| 5226 | SAVE_INT(saveint); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5227 | /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */ |
| 5228 | err = setjmp(jmploc.loc); // huh?? was = setjmp(jmploc.loc) * 2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5229 | if (!err) { |
| 5230 | exception_handler = &jmploc; |
| 5231 | redirect(redir, flags); |
| 5232 | } |
| 5233 | exception_handler = savehandler; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 5234 | if (err && exception_type != EXERROR) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5235 | longjmp(exception_handler->loc, 1); |
| 5236 | RESTORE_INT(saveint); |
| 5237 | return err; |
| 5238 | } |
| 5239 | |
| 5240 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5241 | /* ============ Routines to expand arguments to commands |
| 5242 | * |
| 5243 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 5244 | */ |
| 5245 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5246 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5247 | static arith_t |
| 5248 | ash_arith(const char *s) |
| 5249 | { |
| 5250 | arith_eval_hooks_t math_hooks; |
| 5251 | arith_t result; |
| 5252 | int errcode = 0; |
| 5253 | |
| 5254 | math_hooks.lookupvar = lookupvar; |
| 5255 | math_hooks.setvar = setvar; |
| 5256 | math_hooks.endofname = endofname; |
| 5257 | |
| 5258 | INT_OFF; |
| 5259 | result = arith(s, &errcode, &math_hooks); |
| 5260 | if (errcode < 0) { |
| 5261 | if (errcode == -3) |
| 5262 | ash_msg_and_raise_error("exponent less than 0"); |
| 5263 | if (errcode == -2) |
| 5264 | ash_msg_and_raise_error("divide by zero"); |
| 5265 | if (errcode == -5) |
| 5266 | ash_msg_and_raise_error("expression recursion loop detected"); |
| 5267 | raise_error_syntax(s); |
| 5268 | } |
| 5269 | INT_ON; |
| 5270 | |
| 5271 | return result; |
| 5272 | } |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5273 | #endif |
| 5274 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5275 | /* |
| 5276 | * expandarg flags |
| 5277 | */ |
| 5278 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 5279 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 5280 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 5281 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 5282 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
| 5283 | #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */ |
| 5284 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ |
| 5285 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ |
| 5286 | #define EXP_QWORD 0x100 /* expand word in quoted parameter expansion */ |
| 5287 | /* |
| 5288 | * _rmescape() flags |
| 5289 | */ |
| 5290 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 5291 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
| 5292 | #define RMESCAPE_QUOTED 0x4 /* Remove CTLESC unless in quotes */ |
| 5293 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 5294 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
| 5295 | |
| 5296 | /* |
| 5297 | * Structure specifying which parts of the string should be searched |
| 5298 | * for IFS characters. |
| 5299 | */ |
| 5300 | struct ifsregion { |
| 5301 | struct ifsregion *next; /* next region in list */ |
| 5302 | int begoff; /* offset of start of region */ |
| 5303 | int endoff; /* offset of end of region */ |
| 5304 | int nulonly; /* search for nul bytes only */ |
| 5305 | }; |
| 5306 | |
| 5307 | struct arglist { |
| 5308 | struct strlist *list; |
| 5309 | struct strlist **lastp; |
| 5310 | }; |
| 5311 | |
| 5312 | /* output of current string */ |
| 5313 | static char *expdest; |
| 5314 | /* list of back quote expressions */ |
| 5315 | static struct nodelist *argbackq; |
| 5316 | /* first struct in list of ifs regions */ |
| 5317 | static struct ifsregion ifsfirst; |
| 5318 | /* last struct in list */ |
| 5319 | static struct ifsregion *ifslastp; |
| 5320 | /* holds expanded arg list */ |
| 5321 | static struct arglist exparg; |
| 5322 | |
| 5323 | /* |
| 5324 | * Our own itoa(). |
| 5325 | */ |
| 5326 | static int |
| 5327 | cvtnum(arith_t num) |
| 5328 | { |
| 5329 | int len; |
| 5330 | |
| 5331 | expdest = makestrspace(32, expdest); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5332 | len = fmtstr(expdest, 32, arith_t_fmt, num); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5333 | STADJUST(len, expdest); |
| 5334 | return len; |
| 5335 | } |
| 5336 | |
| 5337 | static size_t |
| 5338 | esclen(const char *start, const char *p) |
| 5339 | { |
| 5340 | size_t esc = 0; |
| 5341 | |
| 5342 | while (p > start && *--p == CTLESC) { |
| 5343 | esc++; |
| 5344 | } |
| 5345 | return esc; |
| 5346 | } |
| 5347 | |
| 5348 | /* |
| 5349 | * Remove any CTLESC characters from a string. |
| 5350 | */ |
| 5351 | static char * |
| 5352 | _rmescapes(char *str, int flag) |
| 5353 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5354 | static const char qchars[] ALIGN1 = { CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 5355 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5356 | char *p, *q, *r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5357 | unsigned inquotes; |
| 5358 | int notescaped; |
| 5359 | int globbing; |
| 5360 | |
| 5361 | p = strpbrk(str, qchars); |
| 5362 | if (!p) { |
| 5363 | return str; |
| 5364 | } |
| 5365 | q = p; |
| 5366 | r = str; |
| 5367 | if (flag & RMESCAPE_ALLOC) { |
| 5368 | size_t len = p - str; |
| 5369 | size_t fulllen = len + strlen(p) + 1; |
| 5370 | |
| 5371 | if (flag & RMESCAPE_GROW) { |
| 5372 | r = makestrspace(fulllen, expdest); |
| 5373 | } else if (flag & RMESCAPE_HEAP) { |
| 5374 | r = ckmalloc(fulllen); |
| 5375 | } else { |
| 5376 | r = stalloc(fulllen); |
| 5377 | } |
| 5378 | q = r; |
| 5379 | if (len > 0) { |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5380 | q = (char *)memcpy(q, str, len) + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5381 | } |
| 5382 | } |
| 5383 | inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED; |
| 5384 | globbing = flag & RMESCAPE_GLOB; |
| 5385 | notescaped = globbing; |
| 5386 | while (*p) { |
| 5387 | if (*p == CTLQUOTEMARK) { |
| 5388 | inquotes = ~inquotes; |
| 5389 | p++; |
| 5390 | notescaped = globbing; |
| 5391 | continue; |
| 5392 | } |
| 5393 | if (*p == '\\') { |
| 5394 | /* naked back slash */ |
| 5395 | notescaped = 0; |
| 5396 | goto copy; |
| 5397 | } |
| 5398 | if (*p == CTLESC) { |
| 5399 | p++; |
| 5400 | if (notescaped && inquotes && *p != '/') { |
| 5401 | *q++ = '\\'; |
| 5402 | } |
| 5403 | } |
| 5404 | notescaped = globbing; |
| 5405 | copy: |
| 5406 | *q++ = *p++; |
| 5407 | } |
| 5408 | *q = '\0'; |
| 5409 | if (flag & RMESCAPE_GROW) { |
| 5410 | expdest = r; |
| 5411 | STADJUST(q - r + 1, expdest); |
| 5412 | } |
| 5413 | return r; |
| 5414 | } |
| 5415 | #define rmescapes(p) _rmescapes((p), 0) |
| 5416 | |
| 5417 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 5418 | |
| 5419 | /* |
| 5420 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 5421 | * |
| 5422 | * Returns an stalloced string. |
| 5423 | */ |
| 5424 | static char * |
| 5425 | preglob(const char *pattern, int quoted, int flag) |
| 5426 | { |
| 5427 | flag |= RMESCAPE_GLOB; |
| 5428 | if (quoted) { |
| 5429 | flag |= RMESCAPE_QUOTED; |
| 5430 | } |
| 5431 | return _rmescapes((char *)pattern, flag); |
| 5432 | } |
| 5433 | |
| 5434 | /* |
| 5435 | * Put a string on the stack. |
| 5436 | */ |
| 5437 | static void |
| 5438 | memtodest(const char *p, size_t len, int syntax, int quotes) |
| 5439 | { |
| 5440 | char *q = expdest; |
| 5441 | |
| 5442 | q = makestrspace(len * 2, q); |
| 5443 | |
| 5444 | while (len--) { |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 5445 | int c = signed_char2int(*p++); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5446 | if (!c) |
| 5447 | continue; |
| 5448 | if (quotes && (SIT(c, syntax) == CCTL || SIT(c, syntax) == CBACK)) |
| 5449 | USTPUTC(CTLESC, q); |
| 5450 | USTPUTC(c, q); |
| 5451 | } |
| 5452 | |
| 5453 | expdest = q; |
| 5454 | } |
| 5455 | |
| 5456 | static void |
| 5457 | strtodest(const char *p, int syntax, int quotes) |
| 5458 | { |
| 5459 | memtodest(p, strlen(p), syntax, quotes); |
| 5460 | } |
| 5461 | |
| 5462 | /* |
| 5463 | * Record the fact that we have to scan this region of the |
| 5464 | * string for IFS characters. |
| 5465 | */ |
| 5466 | static void |
| 5467 | recordregion(int start, int end, int nulonly) |
| 5468 | { |
| 5469 | struct ifsregion *ifsp; |
| 5470 | |
| 5471 | if (ifslastp == NULL) { |
| 5472 | ifsp = &ifsfirst; |
| 5473 | } else { |
| 5474 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 5475 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 5476 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5477 | ifslastp->next = ifsp; |
| 5478 | INT_ON; |
| 5479 | } |
| 5480 | ifslastp = ifsp; |
| 5481 | ifslastp->begoff = start; |
| 5482 | ifslastp->endoff = end; |
| 5483 | ifslastp->nulonly = nulonly; |
| 5484 | } |
| 5485 | |
| 5486 | static void |
| 5487 | removerecordregions(int endoff) |
| 5488 | { |
| 5489 | if (ifslastp == NULL) |
| 5490 | return; |
| 5491 | |
| 5492 | if (ifsfirst.endoff > endoff) { |
| 5493 | while (ifsfirst.next != NULL) { |
| 5494 | struct ifsregion *ifsp; |
| 5495 | INT_OFF; |
| 5496 | ifsp = ifsfirst.next->next; |
| 5497 | free(ifsfirst.next); |
| 5498 | ifsfirst.next = ifsp; |
| 5499 | INT_ON; |
| 5500 | } |
| 5501 | if (ifsfirst.begoff > endoff) |
| 5502 | ifslastp = NULL; |
| 5503 | else { |
| 5504 | ifslastp = &ifsfirst; |
| 5505 | ifsfirst.endoff = endoff; |
| 5506 | } |
| 5507 | return; |
| 5508 | } |
| 5509 | |
| 5510 | ifslastp = &ifsfirst; |
| 5511 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
| 5512 | ifslastp=ifslastp->next; |
| 5513 | while (ifslastp->next != NULL) { |
| 5514 | struct ifsregion *ifsp; |
| 5515 | INT_OFF; |
| 5516 | ifsp = ifslastp->next->next; |
| 5517 | free(ifslastp->next); |
| 5518 | ifslastp->next = ifsp; |
| 5519 | INT_ON; |
| 5520 | } |
| 5521 | if (ifslastp->endoff > endoff) |
| 5522 | ifslastp->endoff = endoff; |
| 5523 | } |
| 5524 | |
| 5525 | static char * |
| 5526 | exptilde(char *startp, char *p, int flag) |
| 5527 | { |
| 5528 | char c; |
| 5529 | char *name; |
| 5530 | struct passwd *pw; |
| 5531 | const char *home; |
| 5532 | int quotes = flag & (EXP_FULL | EXP_CASE); |
| 5533 | int startloc; |
| 5534 | |
| 5535 | name = p + 1; |
| 5536 | |
| 5537 | while ((c = *++p) != '\0') { |
| 5538 | switch (c) { |
| 5539 | case CTLESC: |
| 5540 | return startp; |
| 5541 | case CTLQUOTEMARK: |
| 5542 | return startp; |
| 5543 | case ':': |
| 5544 | if (flag & EXP_VARTILDE) |
| 5545 | goto done; |
| 5546 | break; |
| 5547 | case '/': |
| 5548 | case CTLENDVAR: |
| 5549 | goto done; |
| 5550 | } |
| 5551 | } |
| 5552 | done: |
| 5553 | *p = '\0'; |
| 5554 | if (*name == '\0') { |
| 5555 | home = lookupvar(homestr); |
| 5556 | } else { |
| 5557 | pw = getpwnam(name); |
| 5558 | if (pw == NULL) |
| 5559 | goto lose; |
| 5560 | home = pw->pw_dir; |
| 5561 | } |
| 5562 | if (!home || !*home) |
| 5563 | goto lose; |
| 5564 | *p = c; |
| 5565 | startloc = expdest - (char *)stackblock(); |
| 5566 | strtodest(home, SQSYNTAX, quotes); |
| 5567 | recordregion(startloc, expdest - (char *)stackblock(), 0); |
| 5568 | return p; |
| 5569 | lose: |
| 5570 | *p = c; |
| 5571 | return startp; |
| 5572 | } |
| 5573 | |
| 5574 | /* |
| 5575 | * Execute a command inside back quotes. If it's a builtin command, we |
| 5576 | * want to save its output in a block obtained from malloc. Otherwise |
| 5577 | * we fork off a subprocess and get the output of the command via a pipe. |
| 5578 | * Should be called with interrupts off. |
| 5579 | */ |
| 5580 | struct backcmd { /* result of evalbackcmd */ |
| 5581 | int fd; /* file descriptor to read from */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5582 | int nleft; /* number of chars in buffer */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5583 | char *buf; /* buffer */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5584 | struct job *jp; /* job structure for command */ |
| 5585 | }; |
| 5586 | |
| 5587 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5588 | static uint8_t back_exitstatus; /* exit status of backquoted command */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5589 | #define EV_EXIT 01 /* exit after evaluating tree */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 5590 | static void evaltree(union node *, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5591 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 5592 | static void FAST_FUNC |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5593 | evalbackcmd(union node *n, struct backcmd *result) |
| 5594 | { |
| 5595 | int saveherefd; |
| 5596 | |
| 5597 | result->fd = -1; |
| 5598 | result->buf = NULL; |
| 5599 | result->nleft = 0; |
| 5600 | result->jp = NULL; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5601 | if (n == NULL) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5602 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5603 | |
| 5604 | saveherefd = herefd; |
| 5605 | herefd = -1; |
| 5606 | |
| 5607 | { |
| 5608 | int pip[2]; |
| 5609 | struct job *jp; |
| 5610 | |
| 5611 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5612 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 5613 | jp = makejob(/*n,*/ 1); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5614 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
| 5615 | FORCE_INT_ON; |
| 5616 | close(pip[0]); |
| 5617 | if (pip[1] != 1) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5618 | /*close(1);*/ |
| 5619 | copyfd(pip[1], 1 | COPYFD_EXACT); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5620 | close(pip[1]); |
| 5621 | } |
| 5622 | eflag = 0; |
| 5623 | evaltree(n, EV_EXIT); /* actually evaltreenr... */ |
| 5624 | /* NOTREACHED */ |
| 5625 | } |
| 5626 | close(pip[1]); |
| 5627 | result->fd = pip[0]; |
| 5628 | result->jp = jp; |
| 5629 | } |
| 5630 | herefd = saveherefd; |
| 5631 | out: |
| 5632 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 5633 | result->fd, result->buf, result->nleft, result->jp)); |
| 5634 | } |
| 5635 | |
| 5636 | /* |
| 5637 | * Expand stuff in backwards quotes. |
| 5638 | */ |
| 5639 | static void |
| 5640 | expbackq(union node *cmd, int quoted, int quotes) |
| 5641 | { |
| 5642 | struct backcmd in; |
| 5643 | int i; |
| 5644 | char buf[128]; |
| 5645 | char *p; |
| 5646 | char *dest; |
| 5647 | int startloc; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5648 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5649 | struct stackmark smark; |
| 5650 | |
| 5651 | INT_OFF; |
| 5652 | setstackmark(&smark); |
| 5653 | dest = expdest; |
| 5654 | startloc = dest - (char *)stackblock(); |
| 5655 | grabstackstr(dest); |
| 5656 | evalbackcmd(cmd, &in); |
| 5657 | popstackmark(&smark); |
| 5658 | |
| 5659 | p = in.buf; |
| 5660 | i = in.nleft; |
| 5661 | if (i == 0) |
| 5662 | goto read; |
| 5663 | for (;;) { |
| 5664 | memtodest(p, i, syntax, quotes); |
| 5665 | read: |
| 5666 | if (in.fd < 0) |
| 5667 | break; |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 5668 | i = nonblock_safe_read(in.fd, buf, sizeof(buf)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5669 | TRACE(("expbackq: read returns %d\n", i)); |
| 5670 | if (i <= 0) |
| 5671 | break; |
| 5672 | p = buf; |
| 5673 | } |
| 5674 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 5675 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5676 | if (in.fd >= 0) { |
| 5677 | close(in.fd); |
| 5678 | back_exitstatus = waitforjob(in.jp); |
| 5679 | } |
| 5680 | INT_ON; |
| 5681 | |
| 5682 | /* Eat all trailing newlines */ |
| 5683 | dest = expdest; |
| 5684 | for (; dest > (char *)stackblock() && dest[-1] == '\n';) |
| 5685 | STUNPUTC(dest); |
| 5686 | expdest = dest; |
| 5687 | |
| 5688 | if (quoted == 0) |
| 5689 | recordregion(startloc, dest - (char *)stackblock(), 0); |
| 5690 | TRACE(("evalbackq: size=%d: \"%.*s\"\n", |
| 5691 | (dest - (char *)stackblock()) - startloc, |
| 5692 | (dest - (char *)stackblock()) - startloc, |
| 5693 | stackblock() + startloc)); |
| 5694 | } |
| 5695 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5696 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5697 | /* |
| 5698 | * Expand arithmetic expression. Backup to start of expression, |
| 5699 | * evaluate, place result in (backed up) result, adjust string position. |
| 5700 | */ |
| 5701 | static void |
| 5702 | expari(int quotes) |
| 5703 | { |
| 5704 | char *p, *start; |
| 5705 | int begoff; |
| 5706 | int flag; |
| 5707 | int len; |
| 5708 | |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5709 | /* ifsfree(); */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5710 | |
| 5711 | /* |
| 5712 | * This routine is slightly over-complicated for |
| 5713 | * efficiency. Next we scan backwards looking for the |
| 5714 | * start of arithmetic. |
| 5715 | */ |
| 5716 | start = stackblock(); |
| 5717 | p = expdest - 1; |
| 5718 | *p = '\0'; |
| 5719 | p--; |
| 5720 | do { |
| 5721 | int esc; |
| 5722 | |
| 5723 | while (*p != CTLARI) { |
| 5724 | p--; |
| 5725 | #if DEBUG |
| 5726 | if (p < start) { |
| 5727 | ash_msg_and_raise_error("missing CTLARI (shouldn't happen)"); |
| 5728 | } |
| 5729 | #endif |
| 5730 | } |
| 5731 | |
| 5732 | esc = esclen(start, p); |
| 5733 | if (!(esc % 2)) { |
| 5734 | break; |
| 5735 | } |
| 5736 | |
| 5737 | p -= esc + 1; |
| 5738 | } while (1); |
| 5739 | |
| 5740 | begoff = p - start; |
| 5741 | |
| 5742 | removerecordregions(begoff); |
| 5743 | |
| 5744 | flag = p[1]; |
| 5745 | |
| 5746 | expdest = p; |
| 5747 | |
| 5748 | if (quotes) |
| 5749 | rmescapes(p + 2); |
| 5750 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5751 | len = cvtnum(ash_arith(p + 2)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5752 | |
| 5753 | if (flag != '"') |
| 5754 | recordregion(begoff, begoff + len, 0); |
| 5755 | } |
| 5756 | #endif |
| 5757 | |
| 5758 | /* argstr needs it */ |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5759 | static char *evalvar(char *p, int flag, struct strlist *var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5760 | |
| 5761 | /* |
| 5762 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 5763 | * characters to allow for further processing. Otherwise treat |
| 5764 | * $@ like $* since no splitting will be performed. |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5765 | * |
| 5766 | * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence |
| 5767 | * over shell varables. Needed for "A=a B=$A; echo $B" case - we use it |
| 5768 | * for correct expansion of "B=$A" word. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5769 | */ |
| 5770 | static void |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5771 | argstr(char *p, int flag, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5772 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5773 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5774 | '=', |
| 5775 | ':', |
| 5776 | CTLQUOTEMARK, |
| 5777 | CTLENDVAR, |
| 5778 | CTLESC, |
| 5779 | CTLVAR, |
| 5780 | CTLBACKQ, |
| 5781 | CTLBACKQ | CTLQUOTE, |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5782 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5783 | CTLENDARI, |
| 5784 | #endif |
| 5785 | 0 |
| 5786 | }; |
| 5787 | const char *reject = spclchars; |
| 5788 | int c; |
| 5789 | int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */ |
| 5790 | int breakall = flag & EXP_WORD; |
| 5791 | int inquotes; |
| 5792 | size_t length; |
| 5793 | int startloc; |
| 5794 | |
| 5795 | if (!(flag & EXP_VARTILDE)) { |
| 5796 | reject += 2; |
| 5797 | } else if (flag & EXP_VARTILDE2) { |
| 5798 | reject++; |
| 5799 | } |
| 5800 | inquotes = 0; |
| 5801 | length = 0; |
| 5802 | if (flag & EXP_TILDE) { |
| 5803 | char *q; |
| 5804 | |
| 5805 | flag &= ~EXP_TILDE; |
| 5806 | tilde: |
| 5807 | q = p; |
| 5808 | if (*q == CTLESC && (flag & EXP_QWORD)) |
| 5809 | q++; |
| 5810 | if (*q == '~') |
| 5811 | p = exptilde(p, q, flag); |
| 5812 | } |
| 5813 | start: |
| 5814 | startloc = expdest - (char *)stackblock(); |
| 5815 | for (;;) { |
| 5816 | length += strcspn(p + length, reject); |
| 5817 | c = p[length]; |
| 5818 | if (c && (!(c & 0x80) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5819 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5820 | || c == CTLENDARI |
| 5821 | #endif |
| 5822 | )) { |
| 5823 | /* c == '=' || c == ':' || c == CTLENDARI */ |
| 5824 | length++; |
| 5825 | } |
| 5826 | if (length > 0) { |
| 5827 | int newloc; |
| 5828 | expdest = stack_nputstr(p, length, expdest); |
| 5829 | newloc = expdest - (char *)stackblock(); |
| 5830 | if (breakall && !inquotes && newloc > startloc) { |
| 5831 | recordregion(startloc, newloc, 0); |
| 5832 | } |
| 5833 | startloc = newloc; |
| 5834 | } |
| 5835 | p += length + 1; |
| 5836 | length = 0; |
| 5837 | |
| 5838 | switch (c) { |
| 5839 | case '\0': |
| 5840 | goto breakloop; |
| 5841 | case '=': |
| 5842 | if (flag & EXP_VARTILDE2) { |
| 5843 | p--; |
| 5844 | continue; |
| 5845 | } |
| 5846 | flag |= EXP_VARTILDE2; |
| 5847 | reject++; |
| 5848 | /* fall through */ |
| 5849 | case ':': |
| 5850 | /* |
| 5851 | * sort of a hack - expand tildes in variable |
| 5852 | * assignments (after the first '=' and after ':'s). |
| 5853 | */ |
| 5854 | if (*--p == '~') { |
| 5855 | goto tilde; |
| 5856 | } |
| 5857 | continue; |
| 5858 | } |
| 5859 | |
| 5860 | switch (c) { |
| 5861 | case CTLENDVAR: /* ??? */ |
| 5862 | goto breakloop; |
| 5863 | case CTLQUOTEMARK: |
| 5864 | /* "$@" syntax adherence hack */ |
| 5865 | if ( |
| 5866 | !inquotes && |
| 5867 | !memcmp(p, dolatstr, 4) && |
| 5868 | (p[4] == CTLQUOTEMARK || ( |
| 5869 | p[4] == CTLENDVAR && |
| 5870 | p[5] == CTLQUOTEMARK |
| 5871 | )) |
| 5872 | ) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5873 | p = evalvar(p + 1, flag, /* var_str_list: */ NULL) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5874 | goto start; |
| 5875 | } |
| 5876 | inquotes = !inquotes; |
| 5877 | addquote: |
| 5878 | if (quotes) { |
| 5879 | p--; |
| 5880 | length++; |
| 5881 | startloc++; |
| 5882 | } |
| 5883 | break; |
| 5884 | case CTLESC: |
| 5885 | startloc++; |
| 5886 | length++; |
| 5887 | goto addquote; |
| 5888 | case CTLVAR: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5889 | p = evalvar(p, flag, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5890 | goto start; |
| 5891 | case CTLBACKQ: |
| 5892 | c = 0; |
| 5893 | case CTLBACKQ|CTLQUOTE: |
| 5894 | expbackq(argbackq->n, c, quotes); |
| 5895 | argbackq = argbackq->next; |
| 5896 | goto start; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5897 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5898 | case CTLENDARI: |
| 5899 | p--; |
| 5900 | expari(quotes); |
| 5901 | goto start; |
| 5902 | #endif |
| 5903 | } |
| 5904 | } |
| 5905 | breakloop: |
| 5906 | ; |
| 5907 | } |
| 5908 | |
| 5909 | static char * |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 5910 | scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM, char *str, int quotes, |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5911 | int zero) |
| 5912 | { |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5913 | // This commented out code was added by James Simmons <jsimmons@infradead.org> |
| 5914 | // as part of a larger change when he added support for ${var/a/b}. |
| 5915 | // However, it broke # and % operators: |
| 5916 | // |
| 5917 | //var=ababcdcd |
| 5918 | // ok bad |
| 5919 | //echo ${var#ab} abcdcd abcdcd |
| 5920 | //echo ${var##ab} abcdcd abcdcd |
| 5921 | //echo ${var#a*b} abcdcd ababcdcd (!) |
| 5922 | //echo ${var##a*b} cdcd cdcd |
| 5923 | //echo ${var#?} babcdcd ababcdcd (!) |
| 5924 | //echo ${var##?} babcdcd babcdcd |
| 5925 | //echo ${var#*} ababcdcd babcdcd (!) |
| 5926 | //echo ${var##*} |
| 5927 | //echo ${var%cd} ababcd ababcd |
| 5928 | //echo ${var%%cd} ababcd abab (!) |
| 5929 | //echo ${var%c*d} ababcd ababcd |
| 5930 | //echo ${var%%c*d} abab ababcdcd (!) |
| 5931 | //echo ${var%?} ababcdc ababcdc |
| 5932 | //echo ${var%%?} ababcdc ababcdcd (!) |
| 5933 | //echo ${var%*} ababcdcd ababcdcd |
| 5934 | //echo ${var%%*} |
| 5935 | // |
| 5936 | // Commenting it back out helped. Remove it completely if it really |
| 5937 | // is not needed. |
| 5938 | |
| 5939 | char *loc, *loc2; //, *full; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5940 | char c; |
| 5941 | |
| 5942 | loc = startp; |
| 5943 | loc2 = rmesc; |
| 5944 | do { |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5945 | int match; // = strlen(str); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5946 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 5947 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5948 | c = *loc2; |
| 5949 | if (zero) { |
| 5950 | *loc2 = '\0'; |
| 5951 | s = rmesc; |
| 5952 | } |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5953 | match = pmatch(str, s); // this line was deleted |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 5954 | |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5955 | // // chop off end if its '*' |
| 5956 | // full = strrchr(str, '*'); |
| 5957 | // if (full && full != str) |
| 5958 | // match--; |
| 5959 | // |
| 5960 | // // If str starts with '*' replace with s. |
| 5961 | // if ((*str == '*') && strlen(s) >= match) { |
| 5962 | // full = xstrdup(s); |
| 5963 | // strncpy(full+strlen(s)-match+1, str+1, match-1); |
| 5964 | // } else |
| 5965 | // full = xstrndup(str, match); |
| 5966 | // match = strncmp(s, full, strlen(full)); |
| 5967 | // free(full); |
| 5968 | // |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5969 | *loc2 = c; |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5970 | if (match) // if (!match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5971 | return loc; |
| 5972 | if (quotes && *loc == CTLESC) |
| 5973 | loc++; |
| 5974 | loc++; |
| 5975 | loc2++; |
| 5976 | } while (c); |
| 5977 | return 0; |
| 5978 | } |
| 5979 | |
| 5980 | static char * |
| 5981 | scanright(char *startp, char *rmesc, char *rmescend, char *str, int quotes, |
| 5982 | int zero) |
| 5983 | { |
| 5984 | int esc = 0; |
| 5985 | char *loc; |
| 5986 | char *loc2; |
| 5987 | |
| 5988 | for (loc = str - 1, loc2 = rmescend; loc >= startp; loc2--) { |
| 5989 | int match; |
| 5990 | char c = *loc2; |
| 5991 | const char *s = loc2; |
| 5992 | if (zero) { |
| 5993 | *loc2 = '\0'; |
| 5994 | s = rmesc; |
| 5995 | } |
| 5996 | match = pmatch(str, s); |
| 5997 | *loc2 = c; |
| 5998 | if (match) |
| 5999 | return loc; |
| 6000 | loc--; |
| 6001 | if (quotes) { |
| 6002 | if (--esc < 0) { |
| 6003 | esc = esclen(startp, loc); |
| 6004 | } |
| 6005 | if (esc % 2) { |
| 6006 | esc--; |
| 6007 | loc--; |
| 6008 | } |
| 6009 | } |
| 6010 | } |
| 6011 | return 0; |
| 6012 | } |
| 6013 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6014 | static void varunset(const char *, const char *, const char *, int) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6015 | static void |
| 6016 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 6017 | { |
| 6018 | const char *msg; |
| 6019 | const char *tail; |
| 6020 | |
| 6021 | tail = nullstr; |
| 6022 | msg = "parameter not set"; |
| 6023 | if (umsg) { |
| 6024 | if (*end == CTLENDVAR) { |
| 6025 | if (varflags & VSNUL) |
| 6026 | tail = " or null"; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6027 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6028 | msg = umsg; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6029 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6030 | } |
| 6031 | ash_msg_and_raise_error("%.*s: %s%s", end - var - 1, var, msg, tail); |
| 6032 | } |
| 6033 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6034 | #if ENABLE_ASH_BASH_COMPAT |
| 6035 | static char * |
| 6036 | parse_sub_pattern(char *arg, int inquotes) |
| 6037 | { |
| 6038 | char *idx, *repl = NULL; |
| 6039 | unsigned char c; |
| 6040 | |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6041 | idx = arg; |
| 6042 | while (1) { |
| 6043 | c = *arg; |
| 6044 | if (!c) |
| 6045 | break; |
| 6046 | if (c == '/') { |
| 6047 | /* Only the first '/' seen is our separator */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6048 | if (!repl) { |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6049 | repl = idx + 1; |
| 6050 | c = '\0'; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6051 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6052 | } |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6053 | *idx++ = c; |
| 6054 | if (!inquotes && c == '\\' && arg[1] == '\\') |
| 6055 | arg++; /* skip both \\, not just first one */ |
| 6056 | arg++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6057 | } |
Denis Vlasenko | 29038c0 | 2008-06-14 06:14:02 +0000 | [diff] [blame] | 6058 | *idx = c; /* NUL */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6059 | |
| 6060 | return repl; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6061 | } |
| 6062 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6063 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6064 | static const char * |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6065 | subevalvar(char *p, char *str, int strloc, int subtype, |
| 6066 | int startloc, int varflags, int quotes, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6067 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6068 | struct nodelist *saveargbackq = argbackq; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6069 | char *startp; |
| 6070 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6071 | char *rmesc, *rmescend; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6072 | IF_ASH_BASH_COMPAT(char *repl = NULL;) |
| 6073 | IF_ASH_BASH_COMPAT(char null = '\0';) |
| 6074 | IF_ASH_BASH_COMPAT(int pos, len, orig_len;) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6075 | int saveherefd = herefd; |
| 6076 | int amount, workloc, resetloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6077 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6078 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6079 | |
| 6080 | herefd = -1; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6081 | argstr(p, (subtype != VSASSIGN && subtype != VSQUESTION) ? EXP_CASE : 0, |
| 6082 | var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6083 | STPUTC('\0', expdest); |
| 6084 | herefd = saveherefd; |
| 6085 | argbackq = saveargbackq; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6086 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6087 | |
| 6088 | switch (subtype) { |
| 6089 | case VSASSIGN: |
| 6090 | setvar(str, startp, 0); |
| 6091 | amount = startp - expdest; |
| 6092 | STADJUST(amount, expdest); |
| 6093 | return startp; |
| 6094 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6095 | #if ENABLE_ASH_BASH_COMPAT |
| 6096 | case VSSUBSTR: |
| 6097 | loc = str = stackblock() + strloc; |
| 6098 | // TODO: number() instead? It does error checking... |
| 6099 | pos = atoi(loc); |
| 6100 | len = str - startp - 1; |
| 6101 | |
| 6102 | /* *loc != '\0', guaranteed by parser */ |
| 6103 | if (quotes) { |
| 6104 | char *ptr; |
| 6105 | |
| 6106 | /* We must adjust the length by the number of escapes we find. */ |
| 6107 | for (ptr = startp; ptr < (str - 1); ptr++) { |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 6108 | if (*ptr == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6109 | len--; |
| 6110 | ptr++; |
| 6111 | } |
| 6112 | } |
| 6113 | } |
| 6114 | orig_len = len; |
| 6115 | |
| 6116 | if (*loc++ == ':') { |
| 6117 | // TODO: number() instead? It does error checking... |
| 6118 | len = atoi(loc); |
| 6119 | } else { |
| 6120 | len = orig_len; |
| 6121 | while (*loc && *loc != ':') |
| 6122 | loc++; |
| 6123 | if (*loc++ == ':') |
| 6124 | // TODO: number() instead? It does error checking... |
| 6125 | len = atoi(loc); |
| 6126 | } |
| 6127 | if (pos >= orig_len) { |
| 6128 | pos = 0; |
| 6129 | len = 0; |
| 6130 | } |
| 6131 | if (len > (orig_len - pos)) |
| 6132 | len = orig_len - pos; |
| 6133 | |
| 6134 | for (str = startp; pos; str++, pos--) { |
| 6135 | if (quotes && *str == CTLESC) |
| 6136 | str++; |
| 6137 | } |
| 6138 | for (loc = startp; len; len--) { |
| 6139 | if (quotes && *str == CTLESC) |
| 6140 | *loc++ = *str++; |
| 6141 | *loc++ = *str++; |
| 6142 | } |
| 6143 | *loc = '\0'; |
| 6144 | amount = loc - expdest; |
| 6145 | STADJUST(amount, expdest); |
| 6146 | return loc; |
| 6147 | #endif |
| 6148 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6149 | case VSQUESTION: |
| 6150 | varunset(p, str, startp, varflags); |
| 6151 | /* NOTREACHED */ |
| 6152 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6153 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6154 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6155 | /* We'll comeback here if we grow the stack while handling |
| 6156 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 6157 | * stack will need rebasing, and we'll need to remove our work |
| 6158 | * areas each time |
| 6159 | */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6160 | IF_ASH_BASH_COMPAT(restart:) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6161 | |
| 6162 | amount = expdest - ((char *)stackblock() + resetloc); |
| 6163 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6164 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6165 | |
| 6166 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6167 | rmescend = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6168 | if (quotes) { |
| 6169 | rmesc = _rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); |
| 6170 | if (rmesc != startp) { |
| 6171 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6172 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6173 | } |
| 6174 | } |
| 6175 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6176 | str = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6177 | preglob(str, varflags & VSQUOTE, 0); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6178 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6179 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6180 | #if ENABLE_ASH_BASH_COMPAT |
| 6181 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
| 6182 | char *idx, *end, *restart_detect; |
| 6183 | |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 6184 | if (!repl) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6185 | repl = parse_sub_pattern(str, varflags & VSQUOTE); |
| 6186 | if (!repl) |
| 6187 | repl = &null; |
| 6188 | } |
| 6189 | |
| 6190 | /* If there's no pattern to match, return the expansion unmolested */ |
| 6191 | if (*str == '\0') |
| 6192 | return 0; |
| 6193 | |
| 6194 | len = 0; |
| 6195 | idx = startp; |
| 6196 | end = str - 1; |
| 6197 | while (idx < end) { |
| 6198 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); |
| 6199 | if (!loc) { |
| 6200 | /* No match, advance */ |
| 6201 | restart_detect = stackblock(); |
| 6202 | STPUTC(*idx, expdest); |
| 6203 | if (quotes && *idx == CTLESC) { |
| 6204 | idx++; |
| 6205 | len++; |
| 6206 | STPUTC(*idx, expdest); |
| 6207 | } |
| 6208 | if (stackblock() != restart_detect) |
| 6209 | goto restart; |
| 6210 | idx++; |
| 6211 | len++; |
| 6212 | rmesc++; |
| 6213 | continue; |
| 6214 | } |
| 6215 | |
| 6216 | if (subtype == VSREPLACEALL) { |
| 6217 | while (idx < loc) { |
| 6218 | if (quotes && *idx == CTLESC) |
| 6219 | idx++; |
| 6220 | idx++; |
| 6221 | rmesc++; |
| 6222 | } |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6223 | } else { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6224 | idx = loc; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6225 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6226 | |
| 6227 | for (loc = repl; *loc; loc++) { |
| 6228 | restart_detect = stackblock(); |
| 6229 | STPUTC(*loc, expdest); |
| 6230 | if (stackblock() != restart_detect) |
| 6231 | goto restart; |
| 6232 | len++; |
| 6233 | } |
| 6234 | |
| 6235 | if (subtype == VSREPLACE) { |
| 6236 | while (*idx) { |
| 6237 | restart_detect = stackblock(); |
| 6238 | STPUTC(*idx, expdest); |
| 6239 | if (stackblock() != restart_detect) |
| 6240 | goto restart; |
| 6241 | len++; |
| 6242 | idx++; |
| 6243 | } |
| 6244 | break; |
| 6245 | } |
| 6246 | } |
| 6247 | |
| 6248 | /* We've put the replaced text into a buffer at workloc, now |
| 6249 | * move it to the right place and adjust the stack. |
| 6250 | */ |
| 6251 | startp = stackblock() + startloc; |
| 6252 | STPUTC('\0', expdest); |
| 6253 | memmove(startp, stackblock() + workloc, len); |
| 6254 | startp[len++] = '\0'; |
| 6255 | amount = expdest - ((char *)stackblock() + startloc + len - 1); |
| 6256 | STADJUST(-amount, expdest); |
| 6257 | return startp; |
| 6258 | } |
| 6259 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6260 | |
| 6261 | subtype -= VSTRIMRIGHT; |
| 6262 | #if DEBUG |
| 6263 | if (subtype < 0 || subtype > 7) |
| 6264 | abort(); |
| 6265 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6266 | /* zero = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX */ |
| 6267 | zero = subtype >> 1; |
| 6268 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 6269 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 6270 | |
| 6271 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 6272 | if (loc) { |
| 6273 | if (zero) { |
| 6274 | memmove(startp, loc, str - loc); |
| 6275 | loc = startp + (str - loc) - 1; |
| 6276 | } |
| 6277 | *loc = '\0'; |
| 6278 | amount = loc - expdest; |
| 6279 | STADJUST(amount, expdest); |
| 6280 | } |
| 6281 | return loc; |
| 6282 | } |
| 6283 | |
| 6284 | /* |
| 6285 | * Add the value of a specialized variable to the stack string. |
| 6286 | */ |
| 6287 | static ssize_t |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6288 | varvalue(char *name, int varflags, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6289 | { |
| 6290 | int num; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6291 | const char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6292 | int i; |
| 6293 | int sep = 0; |
| 6294 | int sepq = 0; |
| 6295 | ssize_t len = 0; |
| 6296 | char **ap; |
| 6297 | int syntax; |
| 6298 | int quoted = varflags & VSQUOTE; |
| 6299 | int subtype = varflags & VSTYPE; |
| 6300 | int quotes = flags & (EXP_FULL | EXP_CASE); |
| 6301 | |
| 6302 | if (quoted && (flags & EXP_FULL)) |
| 6303 | sep = 1 << CHAR_BIT; |
| 6304 | |
| 6305 | syntax = quoted ? DQSYNTAX : BASESYNTAX; |
| 6306 | switch (*name) { |
| 6307 | case '$': |
| 6308 | num = rootpid; |
| 6309 | goto numvar; |
| 6310 | case '?': |
| 6311 | num = exitstatus; |
| 6312 | goto numvar; |
| 6313 | case '#': |
| 6314 | num = shellparam.nparam; |
| 6315 | goto numvar; |
| 6316 | case '!': |
| 6317 | num = backgndpid; |
| 6318 | if (num == 0) |
| 6319 | return -1; |
| 6320 | numvar: |
| 6321 | len = cvtnum(num); |
| 6322 | break; |
| 6323 | case '-': |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6324 | expdest = makestrspace(NOPTS, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6325 | for (i = NOPTS - 1; i >= 0; i--) { |
| 6326 | if (optlist[i]) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6327 | USTPUTC(optletters(i), expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6328 | len++; |
| 6329 | } |
| 6330 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6331 | break; |
| 6332 | case '@': |
| 6333 | if (sep) |
| 6334 | goto param; |
| 6335 | /* fall through */ |
| 6336 | case '*': |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 6337 | sep = ifsset() ? signed_char2int(ifsval()[0]) : ' '; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6338 | if (quotes && (SIT(sep, syntax) == CCTL || SIT(sep, syntax) == CBACK)) |
| 6339 | sepq = 1; |
| 6340 | param: |
| 6341 | ap = shellparam.p; |
| 6342 | if (!ap) |
| 6343 | return -1; |
| 6344 | while ((p = *ap++)) { |
| 6345 | size_t partlen; |
| 6346 | |
| 6347 | partlen = strlen(p); |
| 6348 | len += partlen; |
| 6349 | |
| 6350 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6351 | memtodest(p, partlen, syntax, quotes); |
| 6352 | |
| 6353 | if (*ap && sep) { |
| 6354 | char *q; |
| 6355 | |
| 6356 | len++; |
| 6357 | if (subtype == VSPLUS || subtype == VSLENGTH) { |
| 6358 | continue; |
| 6359 | } |
| 6360 | q = expdest; |
| 6361 | if (sepq) |
| 6362 | STPUTC(CTLESC, q); |
| 6363 | STPUTC(sep, q); |
| 6364 | expdest = q; |
| 6365 | } |
| 6366 | } |
| 6367 | return len; |
| 6368 | case '0': |
| 6369 | case '1': |
| 6370 | case '2': |
| 6371 | case '3': |
| 6372 | case '4': |
| 6373 | case '5': |
| 6374 | case '6': |
| 6375 | case '7': |
| 6376 | case '8': |
| 6377 | case '9': |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6378 | // TODO: number() instead? It does error checking... |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6379 | num = atoi(name); |
| 6380 | if (num < 0 || num > shellparam.nparam) |
| 6381 | return -1; |
| 6382 | p = num ? shellparam.p[num - 1] : arg0; |
| 6383 | goto value; |
| 6384 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6385 | /* NB: name has form "VAR=..." */ |
| 6386 | |
| 6387 | /* "A=a B=$A" case: var_str_list is a list of "A=a" strings |
| 6388 | * which should be considered before we check variables. */ |
| 6389 | if (var_str_list) { |
| 6390 | unsigned name_len = (strchrnul(name, '=') - name) + 1; |
| 6391 | p = NULL; |
| 6392 | do { |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 6393 | char *str, *eq; |
| 6394 | str = var_str_list->text; |
| 6395 | eq = strchr(str, '='); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6396 | if (!eq) /* stop at first non-assignment */ |
| 6397 | break; |
| 6398 | eq++; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 6399 | if (name_len == (unsigned)(eq - str) |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6400 | && strncmp(str, name, name_len) == 0) { |
| 6401 | p = eq; |
| 6402 | /* goto value; - WRONG! */ |
| 6403 | /* think "A=1 A=2 B=$A" */ |
| 6404 | } |
| 6405 | var_str_list = var_str_list->next; |
| 6406 | } while (var_str_list); |
| 6407 | if (p) |
| 6408 | goto value; |
| 6409 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6410 | p = lookupvar(name); |
| 6411 | value: |
| 6412 | if (!p) |
| 6413 | return -1; |
| 6414 | |
| 6415 | len = strlen(p); |
| 6416 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6417 | memtodest(p, len, syntax, quotes); |
| 6418 | return len; |
| 6419 | } |
| 6420 | |
| 6421 | if (subtype == VSPLUS || subtype == VSLENGTH) |
| 6422 | STADJUST(-len, expdest); |
| 6423 | return len; |
| 6424 | } |
| 6425 | |
| 6426 | /* |
| 6427 | * Expand a variable, and return a pointer to the next character in the |
| 6428 | * input string. |
| 6429 | */ |
| 6430 | static char * |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6431 | evalvar(char *p, int flag, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6432 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6433 | char varflags; |
| 6434 | char subtype; |
| 6435 | char quoted; |
| 6436 | char easy; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6437 | char *var; |
| 6438 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6439 | int startloc; |
| 6440 | ssize_t varlen; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6441 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6442 | varflags = *p++; |
| 6443 | subtype = varflags & VSTYPE; |
| 6444 | quoted = varflags & VSQUOTE; |
| 6445 | var = p; |
| 6446 | easy = (!quoted || (*var == '@' && shellparam.nparam)); |
| 6447 | startloc = expdest - (char *)stackblock(); |
| 6448 | p = strchr(p, '=') + 1; |
| 6449 | |
| 6450 | again: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6451 | varlen = varvalue(var, varflags, flag, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6452 | if (varflags & VSNUL) |
| 6453 | varlen--; |
| 6454 | |
| 6455 | if (subtype == VSPLUS) { |
| 6456 | varlen = -1 - varlen; |
| 6457 | goto vsplus; |
| 6458 | } |
| 6459 | |
| 6460 | if (subtype == VSMINUS) { |
| 6461 | vsplus: |
| 6462 | if (varlen < 0) { |
| 6463 | argstr( |
| 6464 | p, flag | EXP_TILDE | |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6465 | (quoted ? EXP_QWORD : EXP_WORD), |
| 6466 | var_str_list |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6467 | ); |
| 6468 | goto end; |
| 6469 | } |
| 6470 | if (easy) |
| 6471 | goto record; |
| 6472 | goto end; |
| 6473 | } |
| 6474 | |
| 6475 | if (subtype == VSASSIGN || subtype == VSQUESTION) { |
| 6476 | if (varlen < 0) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6477 | if (subevalvar(p, var, /* strloc: */ 0, |
| 6478 | subtype, startloc, varflags, |
| 6479 | /* quotes: */ 0, |
| 6480 | var_str_list) |
| 6481 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6482 | varflags &= ~VSNUL; |
| 6483 | /* |
| 6484 | * Remove any recorded regions beyond |
| 6485 | * start of variable |
| 6486 | */ |
| 6487 | removerecordregions(startloc); |
| 6488 | goto again; |
| 6489 | } |
| 6490 | goto end; |
| 6491 | } |
| 6492 | if (easy) |
| 6493 | goto record; |
| 6494 | goto end; |
| 6495 | } |
| 6496 | |
| 6497 | if (varlen < 0 && uflag) |
| 6498 | varunset(p, var, 0, 0); |
| 6499 | |
| 6500 | if (subtype == VSLENGTH) { |
| 6501 | cvtnum(varlen > 0 ? varlen : 0); |
| 6502 | goto record; |
| 6503 | } |
| 6504 | |
| 6505 | if (subtype == VSNORMAL) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6506 | if (easy) |
| 6507 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6508 | goto end; |
| 6509 | } |
| 6510 | |
| 6511 | #if DEBUG |
| 6512 | switch (subtype) { |
| 6513 | case VSTRIMLEFT: |
| 6514 | case VSTRIMLEFTMAX: |
| 6515 | case VSTRIMRIGHT: |
| 6516 | case VSTRIMRIGHTMAX: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6517 | #if ENABLE_ASH_BASH_COMPAT |
| 6518 | case VSSUBSTR: |
| 6519 | case VSREPLACE: |
| 6520 | case VSREPLACEALL: |
| 6521 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6522 | break; |
| 6523 | default: |
| 6524 | abort(); |
| 6525 | } |
| 6526 | #endif |
| 6527 | |
| 6528 | if (varlen >= 0) { |
| 6529 | /* |
| 6530 | * Terminate the string and start recording the pattern |
| 6531 | * right after it |
| 6532 | */ |
| 6533 | STPUTC('\0', expdest); |
| 6534 | patloc = expdest - (char *)stackblock(); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6535 | if (0 == subevalvar(p, /* str: */ NULL, patloc, subtype, |
| 6536 | startloc, varflags, |
| 6537 | /* quotes: */ flag & (EXP_FULL | EXP_CASE), |
| 6538 | var_str_list) |
| 6539 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6540 | int amount = expdest - ( |
| 6541 | (char *)stackblock() + patloc - 1 |
| 6542 | ); |
| 6543 | STADJUST(-amount, expdest); |
| 6544 | } |
| 6545 | /* Remove any recorded regions beyond start of variable */ |
| 6546 | removerecordregions(startloc); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6547 | record: |
| 6548 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6549 | } |
| 6550 | |
| 6551 | end: |
| 6552 | if (subtype != VSNORMAL) { /* skip to end of alternative */ |
| 6553 | int nesting = 1; |
| 6554 | for (;;) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6555 | char c = *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6556 | if (c == CTLESC) |
| 6557 | p++; |
| 6558 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { |
| 6559 | if (varlen >= 0) |
| 6560 | argbackq = argbackq->next; |
| 6561 | } else if (c == CTLVAR) { |
| 6562 | if ((*p++ & VSTYPE) != VSNORMAL) |
| 6563 | nesting++; |
| 6564 | } else if (c == CTLENDVAR) { |
| 6565 | if (--nesting == 0) |
| 6566 | break; |
| 6567 | } |
| 6568 | } |
| 6569 | } |
| 6570 | return p; |
| 6571 | } |
| 6572 | |
| 6573 | /* |
| 6574 | * Break the argument string into pieces based upon IFS and add the |
| 6575 | * strings to the argument list. The regions of the string to be |
| 6576 | * searched for IFS characters have been stored by recordregion. |
| 6577 | */ |
| 6578 | static void |
| 6579 | ifsbreakup(char *string, struct arglist *arglist) |
| 6580 | { |
| 6581 | struct ifsregion *ifsp; |
| 6582 | struct strlist *sp; |
| 6583 | char *start; |
| 6584 | char *p; |
| 6585 | char *q; |
| 6586 | const char *ifs, *realifs; |
| 6587 | int ifsspc; |
| 6588 | int nulonly; |
| 6589 | |
| 6590 | start = string; |
| 6591 | if (ifslastp != NULL) { |
| 6592 | ifsspc = 0; |
| 6593 | nulonly = 0; |
| 6594 | realifs = ifsset() ? ifsval() : defifs; |
| 6595 | ifsp = &ifsfirst; |
| 6596 | do { |
| 6597 | p = string + ifsp->begoff; |
| 6598 | nulonly = ifsp->nulonly; |
| 6599 | ifs = nulonly ? nullstr : realifs; |
| 6600 | ifsspc = 0; |
| 6601 | while (p < string + ifsp->endoff) { |
| 6602 | q = p; |
| 6603 | if (*p == CTLESC) |
| 6604 | p++; |
| 6605 | if (!strchr(ifs, *p)) { |
| 6606 | p++; |
| 6607 | continue; |
| 6608 | } |
| 6609 | if (!nulonly) |
| 6610 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6611 | /* Ignore IFS whitespace at start */ |
| 6612 | if (q == start && ifsspc) { |
| 6613 | p++; |
| 6614 | start = p; |
| 6615 | continue; |
| 6616 | } |
| 6617 | *q = '\0'; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6618 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6619 | sp->text = start; |
| 6620 | *arglist->lastp = sp; |
| 6621 | arglist->lastp = &sp->next; |
| 6622 | p++; |
| 6623 | if (!nulonly) { |
| 6624 | for (;;) { |
| 6625 | if (p >= string + ifsp->endoff) { |
| 6626 | break; |
| 6627 | } |
| 6628 | q = p; |
| 6629 | if (*p == CTLESC) |
| 6630 | p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6631 | if (strchr(ifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6632 | p = q; |
| 6633 | break; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6634 | } |
| 6635 | if (strchr(defifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6636 | if (ifsspc) { |
| 6637 | p++; |
| 6638 | ifsspc = 0; |
| 6639 | } else { |
| 6640 | p = q; |
| 6641 | break; |
| 6642 | } |
| 6643 | } else |
| 6644 | p++; |
| 6645 | } |
| 6646 | } |
| 6647 | start = p; |
| 6648 | } /* while */ |
| 6649 | ifsp = ifsp->next; |
| 6650 | } while (ifsp != NULL); |
| 6651 | if (nulonly) |
| 6652 | goto add; |
| 6653 | } |
| 6654 | |
| 6655 | if (!*start) |
| 6656 | return; |
| 6657 | |
| 6658 | add: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6659 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6660 | sp->text = start; |
| 6661 | *arglist->lastp = sp; |
| 6662 | arglist->lastp = &sp->next; |
| 6663 | } |
| 6664 | |
| 6665 | static void |
| 6666 | ifsfree(void) |
| 6667 | { |
| 6668 | struct ifsregion *p; |
| 6669 | |
| 6670 | INT_OFF; |
| 6671 | p = ifsfirst.next; |
| 6672 | do { |
| 6673 | struct ifsregion *ifsp; |
| 6674 | ifsp = p->next; |
| 6675 | free(p); |
| 6676 | p = ifsp; |
| 6677 | } while (p); |
| 6678 | ifslastp = NULL; |
| 6679 | ifsfirst.next = NULL; |
| 6680 | INT_ON; |
| 6681 | } |
| 6682 | |
| 6683 | /* |
| 6684 | * Add a file name to the list. |
| 6685 | */ |
| 6686 | static void |
| 6687 | addfname(const char *name) |
| 6688 | { |
| 6689 | struct strlist *sp; |
| 6690 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6691 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6692 | sp->text = ststrdup(name); |
| 6693 | *exparg.lastp = sp; |
| 6694 | exparg.lastp = &sp->next; |
| 6695 | } |
| 6696 | |
| 6697 | static char *expdir; |
| 6698 | |
| 6699 | /* |
| 6700 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 6701 | */ |
| 6702 | static void |
| 6703 | expmeta(char *enddir, char *name) |
| 6704 | { |
| 6705 | char *p; |
| 6706 | const char *cp; |
| 6707 | char *start; |
| 6708 | char *endname; |
| 6709 | int metaflag; |
| 6710 | struct stat statb; |
| 6711 | DIR *dirp; |
| 6712 | struct dirent *dp; |
| 6713 | int atend; |
| 6714 | int matchdot; |
| 6715 | |
| 6716 | metaflag = 0; |
| 6717 | start = name; |
| 6718 | for (p = name; *p; p++) { |
| 6719 | if (*p == '*' || *p == '?') |
| 6720 | metaflag = 1; |
| 6721 | else if (*p == '[') { |
| 6722 | char *q = p + 1; |
| 6723 | if (*q == '!') |
| 6724 | q++; |
| 6725 | for (;;) { |
| 6726 | if (*q == '\\') |
| 6727 | q++; |
| 6728 | if (*q == '/' || *q == '\0') |
| 6729 | break; |
| 6730 | if (*++q == ']') { |
| 6731 | metaflag = 1; |
| 6732 | break; |
| 6733 | } |
| 6734 | } |
| 6735 | } else if (*p == '\\') |
| 6736 | p++; |
| 6737 | else if (*p == '/') { |
| 6738 | if (metaflag) |
| 6739 | goto out; |
| 6740 | start = p + 1; |
| 6741 | } |
| 6742 | } |
| 6743 | out: |
| 6744 | if (metaflag == 0) { /* we've reached the end of the file name */ |
| 6745 | if (enddir != expdir) |
| 6746 | metaflag++; |
| 6747 | p = name; |
| 6748 | do { |
| 6749 | if (*p == '\\') |
| 6750 | p++; |
| 6751 | *enddir++ = *p; |
| 6752 | } while (*p++); |
| 6753 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
| 6754 | addfname(expdir); |
| 6755 | return; |
| 6756 | } |
| 6757 | endname = p; |
| 6758 | if (name < start) { |
| 6759 | p = name; |
| 6760 | do { |
| 6761 | if (*p == '\\') |
| 6762 | p++; |
| 6763 | *enddir++ = *p++; |
| 6764 | } while (p < start); |
| 6765 | } |
| 6766 | if (enddir == expdir) { |
| 6767 | cp = "."; |
| 6768 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 6769 | cp = "/"; |
| 6770 | } else { |
| 6771 | cp = expdir; |
| 6772 | enddir[-1] = '\0'; |
| 6773 | } |
| 6774 | dirp = opendir(cp); |
| 6775 | if (dirp == NULL) |
| 6776 | return; |
| 6777 | if (enddir != expdir) |
| 6778 | enddir[-1] = '/'; |
| 6779 | if (*endname == 0) { |
| 6780 | atend = 1; |
| 6781 | } else { |
| 6782 | atend = 0; |
| 6783 | *endname++ = '\0'; |
| 6784 | } |
| 6785 | matchdot = 0; |
| 6786 | p = start; |
| 6787 | if (*p == '\\') |
| 6788 | p++; |
| 6789 | if (*p == '.') |
| 6790 | matchdot++; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 6791 | while (!intpending && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 6792 | if (dp->d_name[0] == '.' && !matchdot) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6793 | continue; |
| 6794 | if (pmatch(start, dp->d_name)) { |
| 6795 | if (atend) { |
| 6796 | strcpy(enddir, dp->d_name); |
| 6797 | addfname(expdir); |
| 6798 | } else { |
| 6799 | for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';) |
| 6800 | continue; |
| 6801 | p[-1] = '/'; |
| 6802 | expmeta(p, endname); |
| 6803 | } |
| 6804 | } |
| 6805 | } |
| 6806 | closedir(dirp); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 6807 | if (!atend) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6808 | endname[-1] = '/'; |
| 6809 | } |
| 6810 | |
| 6811 | static struct strlist * |
| 6812 | msort(struct strlist *list, int len) |
| 6813 | { |
| 6814 | struct strlist *p, *q = NULL; |
| 6815 | struct strlist **lpp; |
| 6816 | int half; |
| 6817 | int n; |
| 6818 | |
| 6819 | if (len <= 1) |
| 6820 | return list; |
| 6821 | half = len >> 1; |
| 6822 | p = list; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6823 | for (n = half; --n >= 0;) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6824 | q = p; |
| 6825 | p = p->next; |
| 6826 | } |
| 6827 | q->next = NULL; /* terminate first half of list */ |
| 6828 | q = msort(list, half); /* sort first half of list */ |
| 6829 | p = msort(p, len - half); /* sort second half */ |
| 6830 | lpp = &list; |
| 6831 | for (;;) { |
| 6832 | #if ENABLE_LOCALE_SUPPORT |
| 6833 | if (strcoll(p->text, q->text) < 0) |
| 6834 | #else |
| 6835 | if (strcmp(p->text, q->text) < 0) |
| 6836 | #endif |
| 6837 | { |
| 6838 | *lpp = p; |
| 6839 | lpp = &p->next; |
| 6840 | p = *lpp; |
| 6841 | if (p == NULL) { |
| 6842 | *lpp = q; |
| 6843 | break; |
| 6844 | } |
| 6845 | } else { |
| 6846 | *lpp = q; |
| 6847 | lpp = &q->next; |
| 6848 | q = *lpp; |
| 6849 | if (q == NULL) { |
| 6850 | *lpp = p; |
| 6851 | break; |
| 6852 | } |
| 6853 | } |
| 6854 | } |
| 6855 | return list; |
| 6856 | } |
| 6857 | |
| 6858 | /* |
| 6859 | * Sort the results of file name expansion. It calculates the number of |
| 6860 | * strings to sort and then calls msort (short for merge sort) to do the |
| 6861 | * work. |
| 6862 | */ |
| 6863 | static struct strlist * |
| 6864 | expsort(struct strlist *str) |
| 6865 | { |
| 6866 | int len; |
| 6867 | struct strlist *sp; |
| 6868 | |
| 6869 | len = 0; |
| 6870 | for (sp = str; sp; sp = sp->next) |
| 6871 | len++; |
| 6872 | return msort(str, len); |
| 6873 | } |
| 6874 | |
| 6875 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 6876 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6877 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 6878 | static const char metachars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6879 | '*', '?', '[', 0 |
| 6880 | }; |
| 6881 | /* TODO - EXP_REDIR */ |
| 6882 | |
| 6883 | while (str) { |
| 6884 | struct strlist **savelastp; |
| 6885 | struct strlist *sp; |
| 6886 | char *p; |
| 6887 | |
| 6888 | if (fflag) |
| 6889 | goto nometa; |
| 6890 | if (!strpbrk(str->text, metachars)) |
| 6891 | goto nometa; |
| 6892 | savelastp = exparg.lastp; |
| 6893 | |
| 6894 | INT_OFF; |
| 6895 | p = preglob(str->text, 0, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
| 6896 | { |
| 6897 | int i = strlen(str->text); |
| 6898 | expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ |
| 6899 | } |
| 6900 | |
| 6901 | expmeta(expdir, p); |
| 6902 | free(expdir); |
| 6903 | if (p != str->text) |
| 6904 | free(p); |
| 6905 | INT_ON; |
| 6906 | if (exparg.lastp == savelastp) { |
| 6907 | /* |
| 6908 | * no matches |
| 6909 | */ |
| 6910 | nometa: |
| 6911 | *exparg.lastp = str; |
| 6912 | rmescapes(str->text); |
| 6913 | exparg.lastp = &str->next; |
| 6914 | } else { |
| 6915 | *exparg.lastp = NULL; |
| 6916 | *savelastp = sp = expsort(*savelastp); |
| 6917 | while (sp->next != NULL) |
| 6918 | sp = sp->next; |
| 6919 | exparg.lastp = &sp->next; |
| 6920 | } |
| 6921 | str = str->next; |
| 6922 | } |
| 6923 | } |
| 6924 | |
| 6925 | /* |
| 6926 | * Perform variable substitution and command substitution on an argument, |
| 6927 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 6928 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 6929 | * here document expansion. |
| 6930 | */ |
| 6931 | static void |
| 6932 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 6933 | { |
| 6934 | struct strlist *sp; |
| 6935 | char *p; |
| 6936 | |
| 6937 | argbackq = arg->narg.backquote; |
| 6938 | STARTSTACKSTR(expdest); |
| 6939 | ifsfirst.next = NULL; |
| 6940 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6941 | argstr(arg->narg.text, flag, |
| 6942 | /* var_str_list: */ arglist ? arglist->list : NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6943 | p = _STPUTC('\0', expdest); |
| 6944 | expdest = p - 1; |
| 6945 | if (arglist == NULL) { |
| 6946 | return; /* here document expanded */ |
| 6947 | } |
| 6948 | p = grabstackstr(p); |
| 6949 | exparg.lastp = &exparg.list; |
| 6950 | /* |
| 6951 | * TODO - EXP_REDIR |
| 6952 | */ |
| 6953 | if (flag & EXP_FULL) { |
| 6954 | ifsbreakup(p, &exparg); |
| 6955 | *exparg.lastp = NULL; |
| 6956 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 6957 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6958 | } else { |
| 6959 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ |
| 6960 | rmescapes(p); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6961 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6962 | sp->text = p; |
| 6963 | *exparg.lastp = sp; |
| 6964 | exparg.lastp = &sp->next; |
| 6965 | } |
| 6966 | if (ifsfirst.next) |
| 6967 | ifsfree(); |
| 6968 | *exparg.lastp = NULL; |
| 6969 | if (exparg.list) { |
| 6970 | *arglist->lastp = exparg.list; |
| 6971 | arglist->lastp = exparg.lastp; |
| 6972 | } |
| 6973 | } |
| 6974 | |
| 6975 | /* |
| 6976 | * Expand shell variables and backquotes inside a here document. |
| 6977 | */ |
| 6978 | static void |
| 6979 | expandhere(union node *arg, int fd) |
| 6980 | { |
| 6981 | herefd = fd; |
| 6982 | expandarg(arg, (struct arglist *)NULL, 0); |
| 6983 | full_write(fd, stackblock(), expdest - (char *)stackblock()); |
| 6984 | } |
| 6985 | |
| 6986 | /* |
| 6987 | * Returns true if the pattern matches the string. |
| 6988 | */ |
| 6989 | static int |
| 6990 | patmatch(char *pattern, const char *string) |
| 6991 | { |
| 6992 | return pmatch(preglob(pattern, 0, 0), string); |
| 6993 | } |
| 6994 | |
| 6995 | /* |
| 6996 | * See if a pattern matches in a case statement. |
| 6997 | */ |
| 6998 | static int |
| 6999 | casematch(union node *pattern, char *val) |
| 7000 | { |
| 7001 | struct stackmark smark; |
| 7002 | int result; |
| 7003 | |
| 7004 | setstackmark(&smark); |
| 7005 | argbackq = pattern->narg.backquote; |
| 7006 | STARTSTACKSTR(expdest); |
| 7007 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7008 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE, |
| 7009 | /* var_str_list: */ NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7010 | STACKSTRNUL(expdest); |
| 7011 | result = patmatch(stackblock(), val); |
| 7012 | popstackmark(&smark); |
| 7013 | return result; |
| 7014 | } |
| 7015 | |
| 7016 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7017 | /* ============ find_command */ |
| 7018 | |
| 7019 | struct builtincmd { |
| 7020 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7021 | int (*builtin)(int, char **) FAST_FUNC; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7022 | /* unsigned flags; */ |
| 7023 | }; |
| 7024 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 7025 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7026 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7027 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7028 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7029 | |
| 7030 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7031 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7032 | union param { |
| 7033 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7034 | /* index >= 0 for commands without path (slashes) */ |
| 7035 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 7036 | /* index == -1 for commands with slashes */ |
| 7037 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7038 | const struct builtincmd *cmd; |
| 7039 | struct funcnode *func; |
| 7040 | } u; |
| 7041 | }; |
| 7042 | /* values of cmdtype */ |
| 7043 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 7044 | #define CMDNORMAL 0 /* command is an executable program */ |
| 7045 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 7046 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 7047 | |
| 7048 | /* action to find_command() */ |
| 7049 | #define DO_ERR 0x01 /* prints errors */ |
| 7050 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 7051 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 7052 | #define DO_ALTPATH 0x08 /* using alternate path */ |
| 7053 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ |
| 7054 | |
| 7055 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 7056 | |
| 7057 | |
| 7058 | /* ============ Hashing commands */ |
| 7059 | |
| 7060 | /* |
| 7061 | * When commands are first encountered, they are entered in a hash table. |
| 7062 | * This ensures that a full path search will not have to be done for them |
| 7063 | * on each invocation. |
| 7064 | * |
| 7065 | * We should investigate converting to a linear search, even though that |
| 7066 | * would make the command name "hash" a misnomer. |
| 7067 | */ |
| 7068 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7069 | struct tblentry { |
| 7070 | struct tblentry *next; /* next entry in hash chain */ |
| 7071 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7072 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7073 | char rehash; /* if set, cd done since entry created */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7074 | char cmdname[1]; /* name of command */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7075 | }; |
| 7076 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 7077 | static struct tblentry **cmdtable; |
| 7078 | #define INIT_G_cmdtable() do { \ |
| 7079 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 7080 | } while (0) |
| 7081 | |
| 7082 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 7083 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7084 | |
| 7085 | static void |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7086 | tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) char *cmd, char **argv, char **envp) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7087 | { |
| 7088 | int repeated = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7089 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7090 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7091 | if (applet_no >= 0) { |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7092 | if (APPLET_IS_NOEXEC(applet_no)) { |
| 7093 | while (*envp) |
| 7094 | putenv(*envp++); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7095 | run_applet_no_and_exit(applet_no, argv); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7096 | } |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7097 | /* re-exec ourselves with the new arguments */ |
| 7098 | execve(bb_busybox_exec_path, argv, envp); |
| 7099 | /* If they called chroot or otherwise made the binary no longer |
| 7100 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7101 | } |
| 7102 | #endif |
| 7103 | |
| 7104 | repeat: |
| 7105 | #ifdef SYSV |
| 7106 | do { |
| 7107 | execve(cmd, argv, envp); |
| 7108 | } while (errno == EINTR); |
| 7109 | #else |
| 7110 | execve(cmd, argv, envp); |
| 7111 | #endif |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7112 | if (repeated) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7113 | free(argv); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7114 | return; |
| 7115 | } |
| 7116 | if (errno == ENOEXEC) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7117 | char **ap; |
| 7118 | char **new; |
| 7119 | |
| 7120 | for (ap = argv; *ap; ap++) |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7121 | continue; |
| 7122 | ap = new = ckmalloc((ap - argv + 2) * sizeof(ap[0])); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7123 | ap[1] = cmd; |
Denis Vlasenko | c44ab01 | 2007-04-09 03:11:58 +0000 | [diff] [blame] | 7124 | ap[0] = cmd = (char *)DEFAULT_SHELL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7125 | ap += 2; |
| 7126 | argv++; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7127 | while ((*ap++ = *argv++) != NULL) |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7128 | continue; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7129 | argv = new; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7130 | repeated++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7131 | goto repeat; |
| 7132 | } |
| 7133 | } |
| 7134 | |
| 7135 | /* |
| 7136 | * Exec a program. Never returns. If you change this routine, you may |
| 7137 | * have to change the find_command routine as well. |
| 7138 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7139 | static void shellexec(char **, const char *, int) NORETURN; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7140 | static void |
| 7141 | shellexec(char **argv, const char *path, int idx) |
| 7142 | { |
| 7143 | char *cmdname; |
| 7144 | int e; |
| 7145 | char **envp; |
| 7146 | int exerrno; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7147 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7148 | int applet_no = -1; |
| 7149 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7150 | |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 7151 | clearredir(/*drop:*/ 1); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7152 | envp = listvars(VEXPORT, VUNSET, 0); |
| 7153 | if (strchr(argv[0], '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7154 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7155 | || (applet_no = find_applet_by_name(argv[0])) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7156 | #endif |
| 7157 | ) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7158 | tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7159 | e = errno; |
| 7160 | } else { |
| 7161 | e = ENOENT; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7162 | while ((cmdname = path_advance(&path, argv[0])) != NULL) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7163 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7164 | tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7165 | if (errno != ENOENT && errno != ENOTDIR) |
| 7166 | e = errno; |
| 7167 | } |
| 7168 | stunalloc(cmdname); |
| 7169 | } |
| 7170 | } |
| 7171 | |
| 7172 | /* Map to POSIX errors */ |
| 7173 | switch (e) { |
| 7174 | case EACCES: |
| 7175 | exerrno = 126; |
| 7176 | break; |
| 7177 | case ENOENT: |
| 7178 | exerrno = 127; |
| 7179 | break; |
| 7180 | default: |
| 7181 | exerrno = 2; |
| 7182 | break; |
| 7183 | } |
| 7184 | exitstatus = exerrno; |
| 7185 | TRACE(("shellexec failed for %s, errno %d, suppressint %d\n", |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 7186 | argv[0], e, suppressint)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7187 | ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found")); |
| 7188 | /* NOTREACHED */ |
| 7189 | } |
| 7190 | |
| 7191 | static void |
| 7192 | printentry(struct tblentry *cmdp) |
| 7193 | { |
| 7194 | int idx; |
| 7195 | const char *path; |
| 7196 | char *name; |
| 7197 | |
| 7198 | idx = cmdp->param.index; |
| 7199 | path = pathval(); |
| 7200 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7201 | name = path_advance(&path, cmdp->cmdname); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7202 | stunalloc(name); |
| 7203 | } while (--idx >= 0); |
| 7204 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 7205 | } |
| 7206 | |
| 7207 | /* |
| 7208 | * Clear out command entries. The argument specifies the first entry in |
| 7209 | * PATH which has changed. |
| 7210 | */ |
| 7211 | static void |
| 7212 | clearcmdentry(int firstchange) |
| 7213 | { |
| 7214 | struct tblentry **tblp; |
| 7215 | struct tblentry **pp; |
| 7216 | struct tblentry *cmdp; |
| 7217 | |
| 7218 | INT_OFF; |
| 7219 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 7220 | pp = tblp; |
| 7221 | while ((cmdp = *pp) != NULL) { |
| 7222 | if ((cmdp->cmdtype == CMDNORMAL && |
| 7223 | cmdp->param.index >= firstchange) |
| 7224 | || (cmdp->cmdtype == CMDBUILTIN && |
| 7225 | builtinloc >= firstchange) |
| 7226 | ) { |
| 7227 | *pp = cmdp->next; |
| 7228 | free(cmdp); |
| 7229 | } else { |
| 7230 | pp = &cmdp->next; |
| 7231 | } |
| 7232 | } |
| 7233 | } |
| 7234 | INT_ON; |
| 7235 | } |
| 7236 | |
| 7237 | /* |
| 7238 | * Locate a command in the command hash table. If "add" is nonzero, |
| 7239 | * add the command to the table if it is not already present. The |
| 7240 | * variable "lastcmdentry" is set to point to the address of the link |
| 7241 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 7242 | * entry. |
| 7243 | * |
| 7244 | * Interrupts must be off if called with add != 0. |
| 7245 | */ |
| 7246 | static struct tblentry **lastcmdentry; |
| 7247 | |
| 7248 | static struct tblentry * |
| 7249 | cmdlookup(const char *name, int add) |
| 7250 | { |
| 7251 | unsigned int hashval; |
| 7252 | const char *p; |
| 7253 | struct tblentry *cmdp; |
| 7254 | struct tblentry **pp; |
| 7255 | |
| 7256 | p = name; |
| 7257 | hashval = (unsigned char)*p << 4; |
| 7258 | while (*p) |
| 7259 | hashval += (unsigned char)*p++; |
| 7260 | hashval &= 0x7FFF; |
| 7261 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 7262 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7263 | if (strcmp(cmdp->cmdname, name) == 0) |
| 7264 | break; |
| 7265 | pp = &cmdp->next; |
| 7266 | } |
| 7267 | if (add && cmdp == NULL) { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7268 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) |
| 7269 | + strlen(name) |
| 7270 | /* + 1 - already done because |
| 7271 | * tblentry::cmdname is char[1] */); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7272 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7273 | cmdp->cmdtype = CMDUNKNOWN; |
| 7274 | strcpy(cmdp->cmdname, name); |
| 7275 | } |
| 7276 | lastcmdentry = pp; |
| 7277 | return cmdp; |
| 7278 | } |
| 7279 | |
| 7280 | /* |
| 7281 | * Delete the command entry returned on the last lookup. |
| 7282 | */ |
| 7283 | static void |
| 7284 | delete_cmd_entry(void) |
| 7285 | { |
| 7286 | struct tblentry *cmdp; |
| 7287 | |
| 7288 | INT_OFF; |
| 7289 | cmdp = *lastcmdentry; |
| 7290 | *lastcmdentry = cmdp->next; |
| 7291 | if (cmdp->cmdtype == CMDFUNCTION) |
| 7292 | freefunc(cmdp->param.func); |
| 7293 | free(cmdp); |
| 7294 | INT_ON; |
| 7295 | } |
| 7296 | |
| 7297 | /* |
| 7298 | * Add a new command entry, replacing any existing command entry for |
| 7299 | * the same name - except special builtins. |
| 7300 | */ |
| 7301 | static void |
| 7302 | addcmdentry(char *name, struct cmdentry *entry) |
| 7303 | { |
| 7304 | struct tblentry *cmdp; |
| 7305 | |
| 7306 | cmdp = cmdlookup(name, 1); |
| 7307 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 7308 | freefunc(cmdp->param.func); |
| 7309 | } |
| 7310 | cmdp->cmdtype = entry->cmdtype; |
| 7311 | cmdp->param = entry->u; |
| 7312 | cmdp->rehash = 0; |
| 7313 | } |
| 7314 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7315 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7316 | hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7317 | { |
| 7318 | struct tblentry **pp; |
| 7319 | struct tblentry *cmdp; |
| 7320 | int c; |
| 7321 | struct cmdentry entry; |
| 7322 | char *name; |
| 7323 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7324 | if (nextopt("r") != '\0') { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7325 | clearcmdentry(0); |
| 7326 | return 0; |
| 7327 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7328 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7329 | if (*argptr == NULL) { |
| 7330 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7331 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7332 | if (cmdp->cmdtype == CMDNORMAL) |
| 7333 | printentry(cmdp); |
| 7334 | } |
| 7335 | } |
| 7336 | return 0; |
| 7337 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7338 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7339 | c = 0; |
| 7340 | while ((name = *argptr) != NULL) { |
| 7341 | cmdp = cmdlookup(name, 0); |
| 7342 | if (cmdp != NULL |
| 7343 | && (cmdp->cmdtype == CMDNORMAL |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7344 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)) |
| 7345 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7346 | delete_cmd_entry(); |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7347 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7348 | find_command(name, &entry, DO_ERR, pathval()); |
| 7349 | if (entry.cmdtype == CMDUNKNOWN) |
| 7350 | c = 1; |
| 7351 | argptr++; |
| 7352 | } |
| 7353 | return c; |
| 7354 | } |
| 7355 | |
| 7356 | /* |
| 7357 | * Called when a cd is done. Marks all commands so the next time they |
| 7358 | * are executed they will be rehashed. |
| 7359 | */ |
| 7360 | static void |
| 7361 | hashcd(void) |
| 7362 | { |
| 7363 | struct tblentry **pp; |
| 7364 | struct tblentry *cmdp; |
| 7365 | |
| 7366 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7367 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7368 | if (cmdp->cmdtype == CMDNORMAL |
| 7369 | || (cmdp->cmdtype == CMDBUILTIN |
| 7370 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 7371 | && builtinloc > 0) |
| 7372 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7373 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7374 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7375 | } |
| 7376 | } |
| 7377 | } |
| 7378 | |
| 7379 | /* |
| 7380 | * Fix command hash table when PATH changed. |
| 7381 | * Called before PATH is changed. The argument is the new value of PATH; |
| 7382 | * pathval() still returns the old value at this point. |
| 7383 | * Called with interrupts off. |
| 7384 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7385 | static void FAST_FUNC |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7386 | changepath(const char *new) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7387 | { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7388 | const char *old; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7389 | int firstchange; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7390 | int idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7391 | int idx_bltin; |
| 7392 | |
| 7393 | old = pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7394 | firstchange = 9999; /* assume no change */ |
| 7395 | idx = 0; |
| 7396 | idx_bltin = -1; |
| 7397 | for (;;) { |
| 7398 | if (*old != *new) { |
| 7399 | firstchange = idx; |
| 7400 | if ((*old == '\0' && *new == ':') |
| 7401 | || (*old == ':' && *new == '\0')) |
| 7402 | firstchange++; |
| 7403 | old = new; /* ignore subsequent differences */ |
| 7404 | } |
| 7405 | if (*new == '\0') |
| 7406 | break; |
| 7407 | if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin")) |
| 7408 | idx_bltin = idx; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7409 | if (*new == ':') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7410 | idx++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7411 | new++, old++; |
| 7412 | } |
| 7413 | if (builtinloc < 0 && idx_bltin >= 0) |
| 7414 | builtinloc = idx_bltin; /* zap builtins */ |
| 7415 | if (builtinloc >= 0 && idx_bltin < 0) |
| 7416 | firstchange = 0; |
| 7417 | clearcmdentry(firstchange); |
| 7418 | builtinloc = idx_bltin; |
| 7419 | } |
| 7420 | |
| 7421 | #define TEOF 0 |
| 7422 | #define TNL 1 |
| 7423 | #define TREDIR 2 |
| 7424 | #define TWORD 3 |
| 7425 | #define TSEMI 4 |
| 7426 | #define TBACKGND 5 |
| 7427 | #define TAND 6 |
| 7428 | #define TOR 7 |
| 7429 | #define TPIPE 8 |
| 7430 | #define TLP 9 |
| 7431 | #define TRP 10 |
| 7432 | #define TENDCASE 11 |
| 7433 | #define TENDBQUOTE 12 |
| 7434 | #define TNOT 13 |
| 7435 | #define TCASE 14 |
| 7436 | #define TDO 15 |
| 7437 | #define TDONE 16 |
| 7438 | #define TELIF 17 |
| 7439 | #define TELSE 18 |
| 7440 | #define TESAC 19 |
| 7441 | #define TFI 20 |
| 7442 | #define TFOR 21 |
| 7443 | #define TIF 22 |
| 7444 | #define TIN 23 |
| 7445 | #define TTHEN 24 |
| 7446 | #define TUNTIL 25 |
| 7447 | #define TWHILE 26 |
| 7448 | #define TBEGIN 27 |
| 7449 | #define TEND 28 |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7450 | typedef smallint token_id_t; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7451 | |
| 7452 | /* first char is indicating which tokens mark the end of a list */ |
| 7453 | static const char *const tokname_array[] = { |
| 7454 | "\1end of file", |
| 7455 | "\0newline", |
| 7456 | "\0redirection", |
| 7457 | "\0word", |
| 7458 | "\0;", |
| 7459 | "\0&", |
| 7460 | "\0&&", |
| 7461 | "\0||", |
| 7462 | "\0|", |
| 7463 | "\0(", |
| 7464 | "\1)", |
| 7465 | "\1;;", |
| 7466 | "\1`", |
| 7467 | #define KWDOFFSET 13 |
| 7468 | /* the following are keywords */ |
| 7469 | "\0!", |
| 7470 | "\0case", |
| 7471 | "\1do", |
| 7472 | "\1done", |
| 7473 | "\1elif", |
| 7474 | "\1else", |
| 7475 | "\1esac", |
| 7476 | "\1fi", |
| 7477 | "\0for", |
| 7478 | "\0if", |
| 7479 | "\0in", |
| 7480 | "\1then", |
| 7481 | "\0until", |
| 7482 | "\0while", |
| 7483 | "\0{", |
| 7484 | "\1}", |
| 7485 | }; |
| 7486 | |
| 7487 | static const char * |
| 7488 | tokname(int tok) |
| 7489 | { |
| 7490 | static char buf[16]; |
| 7491 | |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7492 | //try this: |
| 7493 | //if (tok < TSEMI) return tokname_array[tok] + 1; |
| 7494 | //sprintf(buf, "\"%s\"", tokname_array[tok] + 1); |
| 7495 | //return buf; |
| 7496 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7497 | if (tok >= TSEMI) |
| 7498 | buf[0] = '"'; |
| 7499 | sprintf(buf + (tok >= TSEMI), "%s%c", |
| 7500 | tokname_array[tok] + 1, (tok >= TSEMI ? '"' : 0)); |
| 7501 | return buf; |
| 7502 | } |
| 7503 | |
| 7504 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 7505 | static int |
| 7506 | pstrcmp(const void *a, const void *b) |
| 7507 | { |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7508 | return strcmp((char*) a, (*(char**) b) + 1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7509 | } |
| 7510 | |
| 7511 | static const char *const * |
| 7512 | findkwd(const char *s) |
| 7513 | { |
| 7514 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 7515 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 7516 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7517 | } |
| 7518 | |
| 7519 | /* |
| 7520 | * Locate and print what a word is... |
| 7521 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7522 | static int |
| 7523 | describe_command(char *command, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7524 | { |
| 7525 | struct cmdentry entry; |
| 7526 | struct tblentry *cmdp; |
| 7527 | #if ENABLE_ASH_ALIAS |
| 7528 | const struct alias *ap; |
| 7529 | #endif |
| 7530 | const char *path = pathval(); |
| 7531 | |
| 7532 | if (describe_command_verbose) { |
| 7533 | out1str(command); |
| 7534 | } |
| 7535 | |
| 7536 | /* First look at the keywords */ |
| 7537 | if (findkwd(command)) { |
| 7538 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 7539 | goto out; |
| 7540 | } |
| 7541 | |
| 7542 | #if ENABLE_ASH_ALIAS |
| 7543 | /* Then look at the aliases */ |
| 7544 | ap = lookupalias(command, 0); |
| 7545 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7546 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7547 | out1str("alias "); |
| 7548 | printalias(ap); |
| 7549 | return 0; |
| 7550 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7551 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7552 | goto out; |
| 7553 | } |
| 7554 | #endif |
| 7555 | /* Then check if it is a tracked alias */ |
| 7556 | cmdp = cmdlookup(command, 0); |
| 7557 | if (cmdp != NULL) { |
| 7558 | entry.cmdtype = cmdp->cmdtype; |
| 7559 | entry.u = cmdp->param; |
| 7560 | } else { |
| 7561 | /* Finally use brute force */ |
| 7562 | find_command(command, &entry, DO_ABS, path); |
| 7563 | } |
| 7564 | |
| 7565 | switch (entry.cmdtype) { |
| 7566 | case CMDNORMAL: { |
| 7567 | int j = entry.u.index; |
| 7568 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7569 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7570 | p = command; |
| 7571 | } else { |
| 7572 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7573 | p = path_advance(&path, command); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7574 | stunalloc(p); |
| 7575 | } while (--j >= 0); |
| 7576 | } |
| 7577 | if (describe_command_verbose) { |
| 7578 | out1fmt(" is%s %s", |
| 7579 | (cmdp ? " a tracked alias for" : nullstr), p |
| 7580 | ); |
| 7581 | } else { |
| 7582 | out1str(p); |
| 7583 | } |
| 7584 | break; |
| 7585 | } |
| 7586 | |
| 7587 | case CMDFUNCTION: |
| 7588 | if (describe_command_verbose) { |
| 7589 | out1str(" is a shell function"); |
| 7590 | } else { |
| 7591 | out1str(command); |
| 7592 | } |
| 7593 | break; |
| 7594 | |
| 7595 | case CMDBUILTIN: |
| 7596 | if (describe_command_verbose) { |
| 7597 | out1fmt(" is a %sshell builtin", |
| 7598 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 7599 | "special " : nullstr |
| 7600 | ); |
| 7601 | } else { |
| 7602 | out1str(command); |
| 7603 | } |
| 7604 | break; |
| 7605 | |
| 7606 | default: |
| 7607 | if (describe_command_verbose) { |
| 7608 | out1str(": not found\n"); |
| 7609 | } |
| 7610 | return 127; |
| 7611 | } |
| 7612 | out: |
| 7613 | outstr("\n", stdout); |
| 7614 | return 0; |
| 7615 | } |
| 7616 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7617 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7618 | typecmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7619 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7620 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7621 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7622 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7623 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7624 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 7625 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7626 | i++; |
| 7627 | verbose = 0; |
| 7628 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7629 | while (argv[i]) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7630 | err |= describe_command(argv[i++], verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7631 | } |
| 7632 | return err; |
| 7633 | } |
| 7634 | |
| 7635 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7636 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7637 | commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7638 | { |
| 7639 | int c; |
| 7640 | enum { |
| 7641 | VERIFY_BRIEF = 1, |
| 7642 | VERIFY_VERBOSE = 2, |
| 7643 | } verify = 0; |
| 7644 | |
| 7645 | while ((c = nextopt("pvV")) != '\0') |
| 7646 | if (c == 'V') |
| 7647 | verify |= VERIFY_VERBOSE; |
| 7648 | else if (c == 'v') |
| 7649 | verify |= VERIFY_BRIEF; |
| 7650 | #if DEBUG |
| 7651 | else if (c != 'p') |
| 7652 | abort(); |
| 7653 | #endif |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7654 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
| 7655 | if (verify && (*argptr != NULL)) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7656 | return describe_command(*argptr, verify - VERIFY_BRIEF); |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7657 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7658 | |
| 7659 | return 0; |
| 7660 | } |
| 7661 | #endif |
| 7662 | |
| 7663 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7664 | /* ============ eval.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7665 | |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7666 | static int funcblocksize; /* size of structures in function */ |
| 7667 | static int funcstringsize; /* size of strings in node */ |
| 7668 | static void *funcblock; /* block to allocate function from */ |
| 7669 | static char *funcstring; /* block to allocate strings from */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7670 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7671 | /* flags in argument to evaltree */ |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7672 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 7673 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7674 | #define EV_BACKCMD 04 /* command executing within back quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7675 | |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7676 | static const short nodesize[N_NUMBER] = { |
| 7677 | [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), |
| 7678 | [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), |
| 7679 | [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7680 | [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7681 | [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7682 | [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7683 | [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7684 | [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7685 | [NIF ] = SHELL_ALIGN(sizeof(struct nif)), |
| 7686 | [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7687 | [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7688 | [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)), |
| 7689 | [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)), |
| 7690 | [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)), |
| 7691 | [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7692 | [NARG ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7693 | [NTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7694 | #if ENABLE_ASH_BASH_COMPAT |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7695 | [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7696 | #endif |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7697 | [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7698 | [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7699 | [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7700 | [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7701 | [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7702 | [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7703 | [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7704 | [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7705 | [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7706 | }; |
| 7707 | |
| 7708 | static void calcsize(union node *n); |
| 7709 | |
| 7710 | static void |
| 7711 | sizenodelist(struct nodelist *lp) |
| 7712 | { |
| 7713 | while (lp) { |
| 7714 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
| 7715 | calcsize(lp->n); |
| 7716 | lp = lp->next; |
| 7717 | } |
| 7718 | } |
| 7719 | |
| 7720 | static void |
| 7721 | calcsize(union node *n) |
| 7722 | { |
| 7723 | if (n == NULL) |
| 7724 | return; |
| 7725 | funcblocksize += nodesize[n->type]; |
| 7726 | switch (n->type) { |
| 7727 | case NCMD: |
| 7728 | calcsize(n->ncmd.redirect); |
| 7729 | calcsize(n->ncmd.args); |
| 7730 | calcsize(n->ncmd.assign); |
| 7731 | break; |
| 7732 | case NPIPE: |
| 7733 | sizenodelist(n->npipe.cmdlist); |
| 7734 | break; |
| 7735 | case NREDIR: |
| 7736 | case NBACKGND: |
| 7737 | case NSUBSHELL: |
| 7738 | calcsize(n->nredir.redirect); |
| 7739 | calcsize(n->nredir.n); |
| 7740 | break; |
| 7741 | case NAND: |
| 7742 | case NOR: |
| 7743 | case NSEMI: |
| 7744 | case NWHILE: |
| 7745 | case NUNTIL: |
| 7746 | calcsize(n->nbinary.ch2); |
| 7747 | calcsize(n->nbinary.ch1); |
| 7748 | break; |
| 7749 | case NIF: |
| 7750 | calcsize(n->nif.elsepart); |
| 7751 | calcsize(n->nif.ifpart); |
| 7752 | calcsize(n->nif.test); |
| 7753 | break; |
| 7754 | case NFOR: |
| 7755 | funcstringsize += strlen(n->nfor.var) + 1; |
| 7756 | calcsize(n->nfor.body); |
| 7757 | calcsize(n->nfor.args); |
| 7758 | break; |
| 7759 | case NCASE: |
| 7760 | calcsize(n->ncase.cases); |
| 7761 | calcsize(n->ncase.expr); |
| 7762 | break; |
| 7763 | case NCLIST: |
| 7764 | calcsize(n->nclist.body); |
| 7765 | calcsize(n->nclist.pattern); |
| 7766 | calcsize(n->nclist.next); |
| 7767 | break; |
| 7768 | case NDEFUN: |
| 7769 | case NARG: |
| 7770 | sizenodelist(n->narg.backquote); |
| 7771 | funcstringsize += strlen(n->narg.text) + 1; |
| 7772 | calcsize(n->narg.next); |
| 7773 | break; |
| 7774 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 7775 | #if ENABLE_ASH_BASH_COMPAT |
| 7776 | case NTO2: |
| 7777 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7778 | case NCLOBBER: |
| 7779 | case NFROM: |
| 7780 | case NFROMTO: |
| 7781 | case NAPPEND: |
| 7782 | calcsize(n->nfile.fname); |
| 7783 | calcsize(n->nfile.next); |
| 7784 | break; |
| 7785 | case NTOFD: |
| 7786 | case NFROMFD: |
| 7787 | calcsize(n->ndup.vname); |
| 7788 | calcsize(n->ndup.next); |
| 7789 | break; |
| 7790 | case NHERE: |
| 7791 | case NXHERE: |
| 7792 | calcsize(n->nhere.doc); |
| 7793 | calcsize(n->nhere.next); |
| 7794 | break; |
| 7795 | case NNOT: |
| 7796 | calcsize(n->nnot.com); |
| 7797 | break; |
| 7798 | }; |
| 7799 | } |
| 7800 | |
| 7801 | static char * |
| 7802 | nodeckstrdup(char *s) |
| 7803 | { |
| 7804 | char *rtn = funcstring; |
| 7805 | |
| 7806 | strcpy(funcstring, s); |
| 7807 | funcstring += strlen(s) + 1; |
| 7808 | return rtn; |
| 7809 | } |
| 7810 | |
| 7811 | static union node *copynode(union node *); |
| 7812 | |
| 7813 | static struct nodelist * |
| 7814 | copynodelist(struct nodelist *lp) |
| 7815 | { |
| 7816 | struct nodelist *start; |
| 7817 | struct nodelist **lpp; |
| 7818 | |
| 7819 | lpp = &start; |
| 7820 | while (lp) { |
| 7821 | *lpp = funcblock; |
| 7822 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 7823 | (*lpp)->n = copynode(lp->n); |
| 7824 | lp = lp->next; |
| 7825 | lpp = &(*lpp)->next; |
| 7826 | } |
| 7827 | *lpp = NULL; |
| 7828 | return start; |
| 7829 | } |
| 7830 | |
| 7831 | static union node * |
| 7832 | copynode(union node *n) |
| 7833 | { |
| 7834 | union node *new; |
| 7835 | |
| 7836 | if (n == NULL) |
| 7837 | return NULL; |
| 7838 | new = funcblock; |
| 7839 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 7840 | |
| 7841 | switch (n->type) { |
| 7842 | case NCMD: |
| 7843 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 7844 | new->ncmd.args = copynode(n->ncmd.args); |
| 7845 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 7846 | break; |
| 7847 | case NPIPE: |
| 7848 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7849 | new->npipe.pipe_backgnd = n->npipe.pipe_backgnd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7850 | break; |
| 7851 | case NREDIR: |
| 7852 | case NBACKGND: |
| 7853 | case NSUBSHELL: |
| 7854 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 7855 | new->nredir.n = copynode(n->nredir.n); |
| 7856 | break; |
| 7857 | case NAND: |
| 7858 | case NOR: |
| 7859 | case NSEMI: |
| 7860 | case NWHILE: |
| 7861 | case NUNTIL: |
| 7862 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 7863 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 7864 | break; |
| 7865 | case NIF: |
| 7866 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 7867 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 7868 | new->nif.test = copynode(n->nif.test); |
| 7869 | break; |
| 7870 | case NFOR: |
| 7871 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 7872 | new->nfor.body = copynode(n->nfor.body); |
| 7873 | new->nfor.args = copynode(n->nfor.args); |
| 7874 | break; |
| 7875 | case NCASE: |
| 7876 | new->ncase.cases = copynode(n->ncase.cases); |
| 7877 | new->ncase.expr = copynode(n->ncase.expr); |
| 7878 | break; |
| 7879 | case NCLIST: |
| 7880 | new->nclist.body = copynode(n->nclist.body); |
| 7881 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 7882 | new->nclist.next = copynode(n->nclist.next); |
| 7883 | break; |
| 7884 | case NDEFUN: |
| 7885 | case NARG: |
| 7886 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 7887 | new->narg.text = nodeckstrdup(n->narg.text); |
| 7888 | new->narg.next = copynode(n->narg.next); |
| 7889 | break; |
| 7890 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 7891 | #if ENABLE_ASH_BASH_COMPAT |
| 7892 | case NTO2: |
| 7893 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7894 | case NCLOBBER: |
| 7895 | case NFROM: |
| 7896 | case NFROMTO: |
| 7897 | case NAPPEND: |
| 7898 | new->nfile.fname = copynode(n->nfile.fname); |
| 7899 | new->nfile.fd = n->nfile.fd; |
| 7900 | new->nfile.next = copynode(n->nfile.next); |
| 7901 | break; |
| 7902 | case NTOFD: |
| 7903 | case NFROMFD: |
| 7904 | new->ndup.vname = copynode(n->ndup.vname); |
| 7905 | new->ndup.dupfd = n->ndup.dupfd; |
| 7906 | new->ndup.fd = n->ndup.fd; |
| 7907 | new->ndup.next = copynode(n->ndup.next); |
| 7908 | break; |
| 7909 | case NHERE: |
| 7910 | case NXHERE: |
| 7911 | new->nhere.doc = copynode(n->nhere.doc); |
| 7912 | new->nhere.fd = n->nhere.fd; |
| 7913 | new->nhere.next = copynode(n->nhere.next); |
| 7914 | break; |
| 7915 | case NNOT: |
| 7916 | new->nnot.com = copynode(n->nnot.com); |
| 7917 | break; |
| 7918 | }; |
| 7919 | new->type = n->type; |
| 7920 | return new; |
| 7921 | } |
| 7922 | |
| 7923 | /* |
| 7924 | * Make a copy of a parse tree. |
| 7925 | */ |
| 7926 | static struct funcnode * |
| 7927 | copyfunc(union node *n) |
| 7928 | { |
| 7929 | struct funcnode *f; |
| 7930 | size_t blocksize; |
| 7931 | |
| 7932 | funcblocksize = offsetof(struct funcnode, n); |
| 7933 | funcstringsize = 0; |
| 7934 | calcsize(n); |
| 7935 | blocksize = funcblocksize; |
| 7936 | f = ckmalloc(blocksize + funcstringsize); |
| 7937 | funcblock = (char *) f + offsetof(struct funcnode, n); |
| 7938 | funcstring = (char *) f + blocksize; |
| 7939 | copynode(n); |
| 7940 | f->count = 0; |
| 7941 | return f; |
| 7942 | } |
| 7943 | |
| 7944 | /* |
| 7945 | * Define a shell function. |
| 7946 | */ |
| 7947 | static void |
| 7948 | defun(char *name, union node *func) |
| 7949 | { |
| 7950 | struct cmdentry entry; |
| 7951 | |
| 7952 | INT_OFF; |
| 7953 | entry.cmdtype = CMDFUNCTION; |
| 7954 | entry.u.func = copyfunc(func); |
| 7955 | addcmdentry(name, &entry); |
| 7956 | INT_ON; |
| 7957 | } |
| 7958 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 7959 | /* Reasons for skipping commands (see comment on breakcmd routine) */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7960 | #define SKIPBREAK (1 << 0) |
| 7961 | #define SKIPCONT (1 << 1) |
| 7962 | #define SKIPFUNC (1 << 2) |
| 7963 | #define SKIPFILE (1 << 3) |
| 7964 | #define SKIPEVAL (1 << 4) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 7965 | static smallint evalskip; /* set to SKIPxxx if we are skipping commands */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7966 | static int skipcount; /* number of levels to skip */ |
| 7967 | static int funcnest; /* depth of function calls */ |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 7968 | static int loopnest; /* current loop nesting level */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7969 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 7970 | /* Forward decl way out to parsing code - dotrap needs it */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 7971 | static int evalstring(char *s, int mask); |
| 7972 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 7973 | /* Called to execute a trap. |
| 7974 | * Single callsite - at the end of evaltree(). |
| 7975 | * If we return non-zero, exaltree raises EXEXIT exception. |
| 7976 | * |
| 7977 | * Perhaps we should avoid entering new trap handlers |
| 7978 | * while we are executing a trap handler. [is it a TODO?] |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 7979 | */ |
| 7980 | static int |
| 7981 | dotrap(void) |
| 7982 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 7983 | uint8_t *g; |
| 7984 | int sig; |
| 7985 | uint8_t savestatus; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 7986 | |
| 7987 | savestatus = exitstatus; |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 7988 | pendingsig = 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 7989 | xbarrier(); |
| 7990 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 7991 | TRACE(("dotrap entered\n")); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 7992 | for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) { |
| 7993 | int want_exexit; |
| 7994 | char *t; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 7995 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 7996 | if (*g == 0) |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 7997 | continue; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 7998 | t = trap[sig]; |
| 7999 | /* non-trapped SIGINT is handled separately by raise_interrupt, |
| 8000 | * don't upset it by resetting gotsig[SIGINT-1] */ |
| 8001 | if (sig == SIGINT && !t) |
| 8002 | continue; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8003 | |
| 8004 | TRACE(("sig %d is active, will run handler '%s'\n", sig, t)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8005 | *g = 0; |
| 8006 | if (!t) |
| 8007 | continue; |
| 8008 | want_exexit = evalstring(t, SKIPEVAL); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8009 | exitstatus = savestatus; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8010 | if (want_exexit) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8011 | TRACE(("dotrap returns %d\n", want_exexit)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8012 | return want_exexit; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8013 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8014 | } |
| 8015 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8016 | TRACE(("dotrap returns 0\n")); |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8017 | return 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8018 | } |
| 8019 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8020 | /* forward declarations - evaluation is fairly recursive business... */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8021 | static void evalloop(union node *, int); |
| 8022 | static void evalfor(union node *, int); |
| 8023 | static void evalcase(union node *, int); |
| 8024 | static void evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8025 | static void expredir(union node *); |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8026 | static void evalpipe(union node *, int); |
| 8027 | static void evalcommand(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8028 | static int evalbltin(const struct builtincmd *, int, char **); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8029 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8030 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8031 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8032 | * Evaluate a parse tree. The value is left in the global variable |
| 8033 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8034 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8035 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8036 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8037 | { |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8038 | struct jmploc *volatile savehandler = exception_handler; |
| 8039 | struct jmploc jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8040 | int checkexit = 0; |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8041 | void (*evalfn)(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8042 | int status; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8043 | int int_level; |
| 8044 | |
| 8045 | SAVE_INT(int_level); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8046 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8047 | if (n == NULL) { |
| 8048 | TRACE(("evaltree(NULL) called\n")); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8049 | goto out1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8050 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8051 | TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8052 | |
| 8053 | exception_handler = &jmploc; |
| 8054 | { |
| 8055 | int err = setjmp(jmploc.loc); |
| 8056 | if (err) { |
| 8057 | /* if it was a signal, check for trap handlers */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8058 | if (exception_type == EXSIG) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8059 | TRACE(("exception %d (EXSIG) in evaltree, err=%d\n", |
| 8060 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8061 | goto out; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8062 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8063 | /* continue on the way out */ |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8064 | TRACE(("exception %d in evaltree, propagating err=%d\n", |
| 8065 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8066 | exception_handler = savehandler; |
| 8067 | longjmp(exception_handler->loc, err); |
| 8068 | } |
| 8069 | } |
| 8070 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8071 | switch (n->type) { |
| 8072 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 8073 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8074 | out1fmt("Node type = %d\n", n->type); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8075 | fflush(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8076 | break; |
| 8077 | #endif |
| 8078 | case NNOT: |
| 8079 | evaltree(n->nnot.com, EV_TESTED); |
| 8080 | status = !exitstatus; |
| 8081 | goto setstatus; |
| 8082 | case NREDIR: |
| 8083 | expredir(n->nredir.redirect); |
| 8084 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 8085 | if (!status) { |
| 8086 | evaltree(n->nredir.n, flags & EV_TESTED); |
| 8087 | status = exitstatus; |
| 8088 | } |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 8089 | popredir(/*drop:*/ 0, /*restore:*/ 0 /* not sure */); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8090 | goto setstatus; |
| 8091 | case NCMD: |
| 8092 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8093 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8094 | if (eflag && !(flags & EV_TESTED)) |
| 8095 | checkexit = ~0; |
| 8096 | goto calleval; |
| 8097 | case NFOR: |
| 8098 | evalfn = evalfor; |
| 8099 | goto calleval; |
| 8100 | case NWHILE: |
| 8101 | case NUNTIL: |
| 8102 | evalfn = evalloop; |
| 8103 | goto calleval; |
| 8104 | case NSUBSHELL: |
| 8105 | case NBACKGND: |
| 8106 | evalfn = evalsubshell; |
| 8107 | goto calleval; |
| 8108 | case NPIPE: |
| 8109 | evalfn = evalpipe; |
| 8110 | goto checkexit; |
| 8111 | case NCASE: |
| 8112 | evalfn = evalcase; |
| 8113 | goto calleval; |
| 8114 | case NAND: |
| 8115 | case NOR: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8116 | case NSEMI: { |
| 8117 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8118 | #if NAND + 1 != NOR |
| 8119 | #error NAND + 1 != NOR |
| 8120 | #endif |
| 8121 | #if NOR + 1 != NSEMI |
| 8122 | #error NOR + 1 != NSEMI |
| 8123 | #endif |
Denis Vlasenko | 87d5fd9 | 2008-07-26 13:48:35 +0000 | [diff] [blame] | 8124 | unsigned is_or = n->type - NAND; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8125 | evaltree( |
| 8126 | n->nbinary.ch1, |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8127 | (flags | ((is_or >> 1) - 1)) & EV_TESTED |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8128 | ); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8129 | if (!exitstatus == is_or) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8130 | break; |
| 8131 | if (!evalskip) { |
| 8132 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8133 | evaln: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8134 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8135 | calleval: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8136 | evalfn(n, flags); |
| 8137 | break; |
| 8138 | } |
| 8139 | break; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8140 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8141 | case NIF: |
| 8142 | evaltree(n->nif.test, EV_TESTED); |
| 8143 | if (evalskip) |
| 8144 | break; |
| 8145 | if (exitstatus == 0) { |
| 8146 | n = n->nif.ifpart; |
| 8147 | goto evaln; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8148 | } |
| 8149 | if (n->nif.elsepart) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8150 | n = n->nif.elsepart; |
| 8151 | goto evaln; |
| 8152 | } |
| 8153 | goto success; |
| 8154 | case NDEFUN: |
| 8155 | defun(n->narg.text, n->narg.next); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8156 | success: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8157 | status = 0; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8158 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8159 | exitstatus = status; |
| 8160 | break; |
| 8161 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8162 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8163 | out: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8164 | exception_handler = savehandler; |
| 8165 | out1: |
| 8166 | if (checkexit & exitstatus) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8167 | evalskip |= SKIPEVAL; |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 8168 | else if (pendingsig && dotrap()) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8169 | goto exexit; |
| 8170 | |
| 8171 | if (flags & EV_EXIT) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8172 | exexit: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8173 | raise_exception(EXEXIT); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8174 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8175 | |
| 8176 | RESTORE_INT(int_level); |
| 8177 | TRACE(("leaving evaltree (no interrupts)\n")); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8178 | } |
| 8179 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8180 | #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3) |
| 8181 | static |
| 8182 | #endif |
| 8183 | void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__)); |
| 8184 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8185 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8186 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8187 | { |
| 8188 | int status; |
| 8189 | |
| 8190 | loopnest++; |
| 8191 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8192 | flags &= EV_TESTED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8193 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8194 | int i; |
| 8195 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8196 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 8197 | if (evalskip) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8198 | skipping: |
| 8199 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8200 | evalskip = 0; |
| 8201 | continue; |
| 8202 | } |
| 8203 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8204 | evalskip = 0; |
| 8205 | break; |
| 8206 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8207 | i = exitstatus; |
| 8208 | if (n->type != NWHILE) |
| 8209 | i = !i; |
| 8210 | if (i != 0) |
| 8211 | break; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8212 | evaltree(n->nbinary.ch2, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8213 | status = exitstatus; |
| 8214 | if (evalskip) |
| 8215 | goto skipping; |
| 8216 | } |
| 8217 | loopnest--; |
| 8218 | exitstatus = status; |
| 8219 | } |
| 8220 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8221 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8222 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8223 | { |
| 8224 | struct arglist arglist; |
| 8225 | union node *argp; |
| 8226 | struct strlist *sp; |
| 8227 | struct stackmark smark; |
| 8228 | |
| 8229 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8230 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8231 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8232 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8233 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8234 | /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8235 | if (evalskip) |
| 8236 | goto out; |
| 8237 | } |
| 8238 | *arglist.lastp = NULL; |
| 8239 | |
| 8240 | exitstatus = 0; |
| 8241 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8242 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8243 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8244 | setvar(n->nfor.var, sp->text, 0); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8245 | evaltree(n->nfor.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8246 | if (evalskip) { |
| 8247 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 8248 | evalskip = 0; |
| 8249 | continue; |
| 8250 | } |
| 8251 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8252 | evalskip = 0; |
| 8253 | break; |
| 8254 | } |
| 8255 | } |
| 8256 | loopnest--; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8257 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8258 | popstackmark(&smark); |
| 8259 | } |
| 8260 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8261 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8262 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8263 | { |
| 8264 | union node *cp; |
| 8265 | union node *patp; |
| 8266 | struct arglist arglist; |
| 8267 | struct stackmark smark; |
| 8268 | |
| 8269 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8270 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8271 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8272 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8273 | exitstatus = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8274 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 8275 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8276 | if (casematch(patp, arglist.list->text)) { |
| 8277 | if (evalskip == 0) { |
| 8278 | evaltree(cp->nclist.body, flags); |
| 8279 | } |
| 8280 | goto out; |
| 8281 | } |
| 8282 | } |
| 8283 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8284 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8285 | popstackmark(&smark); |
| 8286 | } |
| 8287 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8288 | /* |
| 8289 | * Kick off a subshell to evaluate a tree. |
| 8290 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8291 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8292 | evalsubshell(union node *n, int flags) |
| 8293 | { |
| 8294 | struct job *jp; |
| 8295 | int backgnd = (n->type == NBACKGND); |
| 8296 | int status; |
| 8297 | |
| 8298 | expredir(n->nredir.redirect); |
| 8299 | if (!backgnd && flags & EV_EXIT && !trap[0]) |
| 8300 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8301 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8302 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8303 | if (forkshell(jp, n, backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8304 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8305 | flags |= EV_EXIT; |
| 8306 | if (backgnd) |
| 8307 | flags &=~ EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 8308 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8309 | redirect(n->nredir.redirect, 0); |
| 8310 | evaltreenr(n->nredir.n, flags); |
| 8311 | /* never returns */ |
| 8312 | } |
| 8313 | status = 0; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8314 | if (!backgnd) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8315 | status = waitforjob(jp); |
| 8316 | exitstatus = status; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8317 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8318 | } |
| 8319 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8320 | /* |
| 8321 | * Compute the names of the files in a redirection list. |
| 8322 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 8323 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8324 | static void |
| 8325 | expredir(union node *n) |
| 8326 | { |
| 8327 | union node *redir; |
| 8328 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8329 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8330 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8331 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8332 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8333 | fn.lastp = &fn.list; |
| 8334 | switch (redir->type) { |
| 8335 | case NFROMTO: |
| 8336 | case NFROM: |
| 8337 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8338 | #if ENABLE_ASH_BASH_COMPAT |
| 8339 | case NTO2: |
| 8340 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8341 | case NCLOBBER: |
| 8342 | case NAPPEND: |
| 8343 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8344 | #if ENABLE_ASH_BASH_COMPAT |
| 8345 | store_expfname: |
| 8346 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8347 | redir->nfile.expfname = fn.list->text; |
| 8348 | break; |
| 8349 | case NFROMFD: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8350 | case NTOFD: /* >& */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8351 | if (redir->ndup.vname) { |
| 8352 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8353 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8354 | ash_msg_and_raise_error("redir error"); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8355 | #if ENABLE_ASH_BASH_COMPAT |
| 8356 | //FIXME: we used expandarg with different args! |
| 8357 | if (!isdigit_str9(fn.list->text)) { |
| 8358 | /* >&file, not >&fd */ |
| 8359 | if (redir->nfile.fd != 1) /* 123>&file - BAD */ |
| 8360 | ash_msg_and_raise_error("redir error"); |
| 8361 | redir->type = NTO2; |
| 8362 | goto store_expfname; |
| 8363 | } |
| 8364 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8365 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8366 | } |
| 8367 | break; |
| 8368 | } |
| 8369 | } |
| 8370 | } |
| 8371 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8372 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8373 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 8374 | * of the process creating the pipeline. (This differs from some versions |
| 8375 | * of the shell, which make the last process in a pipeline the parent |
| 8376 | * of all the rest.) |
| 8377 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8378 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8379 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8380 | { |
| 8381 | struct job *jp; |
| 8382 | struct nodelist *lp; |
| 8383 | int pipelen; |
| 8384 | int prevfd; |
| 8385 | int pip[2]; |
| 8386 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8387 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8388 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8389 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8390 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8391 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8392 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8393 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8394 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8395 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8396 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8397 | pip[1] = -1; |
| 8398 | if (lp->next) { |
| 8399 | if (pipe(pip) < 0) { |
| 8400 | close(prevfd); |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 8401 | ash_msg_and_raise_error("pipe call failed"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8402 | } |
| 8403 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8404 | if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8405 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8406 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8407 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8408 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8409 | if (prevfd > 0) { |
| 8410 | dup2(prevfd, 0); |
| 8411 | close(prevfd); |
| 8412 | } |
| 8413 | if (pip[1] > 1) { |
| 8414 | dup2(pip[1], 1); |
| 8415 | close(pip[1]); |
| 8416 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8417 | evaltreenr(lp->n, flags); |
| 8418 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8419 | } |
| 8420 | if (prevfd >= 0) |
| 8421 | close(prevfd); |
| 8422 | prevfd = pip[0]; |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 8423 | /* Don't want to trigger debugging */ |
| 8424 | if (pip[1] != -1) |
| 8425 | close(pip[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8426 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8427 | if (n->npipe.pipe_backgnd == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8428 | exitstatus = waitforjob(jp); |
| 8429 | TRACE(("evalpipe: job done exit status %d\n", exitstatus)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8430 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8431 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8432 | } |
| 8433 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8434 | /* |
| 8435 | * Controls whether the shell is interactive or not. |
| 8436 | */ |
| 8437 | static void |
| 8438 | setinteractive(int on) |
| 8439 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8440 | static smallint is_interactive; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8441 | |
| 8442 | if (++on == is_interactive) |
| 8443 | return; |
| 8444 | is_interactive = on; |
| 8445 | setsignal(SIGINT); |
| 8446 | setsignal(SIGQUIT); |
| 8447 | setsignal(SIGTERM); |
| 8448 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8449 | if (is_interactive > 1) { |
| 8450 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8451 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8452 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8453 | if (!did_banner) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8454 | out1fmt( |
| 8455 | "\n\n" |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8456 | "%s built-in shell (ash)\n" |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8457 | "Enter 'help' for a list of built-in commands." |
| 8458 | "\n\n", |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8459 | bb_banner); |
| 8460 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8461 | } |
| 8462 | } |
| 8463 | #endif |
| 8464 | } |
| 8465 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8466 | static void |
| 8467 | optschanged(void) |
| 8468 | { |
| 8469 | #if DEBUG |
| 8470 | opentrace(); |
| 8471 | #endif |
| 8472 | setinteractive(iflag); |
| 8473 | setjobctl(mflag); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8474 | #if ENABLE_FEATURE_EDITING_VI |
| 8475 | if (viflag) |
| 8476 | line_input_state->flags |= VI_MODE; |
| 8477 | else |
| 8478 | line_input_state->flags &= ~VI_MODE; |
| 8479 | #else |
| 8480 | viflag = 0; /* forcibly keep the option off */ |
| 8481 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8482 | } |
| 8483 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8484 | static struct localvar *localvars; |
| 8485 | |
| 8486 | /* |
| 8487 | * Called after a function returns. |
| 8488 | * Interrupts must be off. |
| 8489 | */ |
| 8490 | static void |
| 8491 | poplocalvars(void) |
| 8492 | { |
| 8493 | struct localvar *lvp; |
| 8494 | struct var *vp; |
| 8495 | |
| 8496 | while ((lvp = localvars) != NULL) { |
| 8497 | localvars = lvp->next; |
| 8498 | vp = lvp->vp; |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame^] | 8499 | TRACE(("poplocalvar %s\n", vp ? vp->text : "-")); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8500 | if (vp == NULL) { /* $- saved */ |
| 8501 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 8502 | free((char*)lvp->text); |
| 8503 | optschanged(); |
| 8504 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
| 8505 | unsetvar(vp->text); |
| 8506 | } else { |
| 8507 | if (vp->func) |
| 8508 | (*vp->func)(strchrnul(lvp->text, '=') + 1); |
| 8509 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 8510 | free((char*)vp->text); |
| 8511 | vp->flags = lvp->flags; |
| 8512 | vp->text = lvp->text; |
| 8513 | } |
| 8514 | free(lvp); |
| 8515 | } |
| 8516 | } |
| 8517 | |
| 8518 | static int |
| 8519 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 8520 | { |
| 8521 | volatile struct shparam saveparam; |
| 8522 | struct localvar *volatile savelocalvars; |
| 8523 | struct jmploc *volatile savehandler; |
| 8524 | struct jmploc jmploc; |
| 8525 | int e; |
| 8526 | |
| 8527 | saveparam = shellparam; |
| 8528 | savelocalvars = localvars; |
| 8529 | e = setjmp(jmploc.loc); |
| 8530 | if (e) { |
| 8531 | goto funcdone; |
| 8532 | } |
| 8533 | INT_OFF; |
| 8534 | savehandler = exception_handler; |
| 8535 | exception_handler = &jmploc; |
| 8536 | localvars = NULL; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8537 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8538 | func->count++; |
| 8539 | funcnest++; |
| 8540 | INT_ON; |
| 8541 | shellparam.nparam = argc - 1; |
| 8542 | shellparam.p = argv + 1; |
| 8543 | #if ENABLE_ASH_GETOPTS |
| 8544 | shellparam.optind = 1; |
| 8545 | shellparam.optoff = -1; |
| 8546 | #endif |
| 8547 | evaltree(&func->n, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8548 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8549 | INT_OFF; |
| 8550 | funcnest--; |
| 8551 | freefunc(func); |
| 8552 | poplocalvars(); |
| 8553 | localvars = savelocalvars; |
| 8554 | freeparam(&shellparam); |
| 8555 | shellparam = saveparam; |
| 8556 | exception_handler = savehandler; |
| 8557 | INT_ON; |
| 8558 | evalskip &= ~SKIPFUNC; |
| 8559 | return e; |
| 8560 | } |
| 8561 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8562 | #if ENABLE_ASH_CMDCMD |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8563 | static char ** |
| 8564 | parse_command_args(char **argv, const char **path) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8565 | { |
| 8566 | char *cp, c; |
| 8567 | |
| 8568 | for (;;) { |
| 8569 | cp = *++argv; |
| 8570 | if (!cp) |
| 8571 | return 0; |
| 8572 | if (*cp++ != '-') |
| 8573 | break; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8574 | c = *cp++; |
| 8575 | if (!c) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8576 | break; |
| 8577 | if (c == '-' && !*cp) { |
| 8578 | argv++; |
| 8579 | break; |
| 8580 | } |
| 8581 | do { |
| 8582 | switch (c) { |
| 8583 | case 'p': |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 8584 | *path = bb_default_path; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8585 | break; |
| 8586 | default: |
| 8587 | /* run 'typecmd' for other options */ |
| 8588 | return 0; |
| 8589 | } |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 8590 | c = *cp++; |
| 8591 | } while (c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8592 | } |
| 8593 | return argv; |
| 8594 | } |
| 8595 | #endif |
| 8596 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8597 | /* |
| 8598 | * Make a variable a local variable. When a variable is made local, it's |
| 8599 | * value and flags are saved in a localvar structure. The saved values |
| 8600 | * will be restored when the shell function returns. We handle the name |
| 8601 | * "-" as a special case. |
| 8602 | */ |
| 8603 | static void |
| 8604 | mklocal(char *name) |
| 8605 | { |
| 8606 | struct localvar *lvp; |
| 8607 | struct var **vpp; |
| 8608 | struct var *vp; |
| 8609 | |
| 8610 | INT_OFF; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 8611 | lvp = ckzalloc(sizeof(struct localvar)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8612 | if (LONE_DASH(name)) { |
| 8613 | char *p; |
| 8614 | p = ckmalloc(sizeof(optlist)); |
| 8615 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 8616 | vp = NULL; |
| 8617 | } else { |
| 8618 | char *eq; |
| 8619 | |
| 8620 | vpp = hashvar(name); |
| 8621 | vp = *findvar(vpp, name); |
| 8622 | eq = strchr(name, '='); |
| 8623 | if (vp == NULL) { |
| 8624 | if (eq) |
| 8625 | setvareq(name, VSTRFIXED); |
| 8626 | else |
| 8627 | setvar(name, NULL, VSTRFIXED); |
| 8628 | vp = *vpp; /* the new variable */ |
| 8629 | lvp->flags = VUNSET; |
| 8630 | } else { |
| 8631 | lvp->text = vp->text; |
| 8632 | lvp->flags = vp->flags; |
| 8633 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 8634 | if (eq) |
| 8635 | setvareq(name, 0); |
| 8636 | } |
| 8637 | } |
| 8638 | lvp->vp = vp; |
| 8639 | lvp->next = localvars; |
| 8640 | localvars = lvp; |
| 8641 | INT_ON; |
| 8642 | } |
| 8643 | |
| 8644 | /* |
| 8645 | * The "local" command. |
| 8646 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8647 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8648 | localcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8649 | { |
| 8650 | char *name; |
| 8651 | |
| 8652 | argv = argptr; |
| 8653 | while ((name = *argv++) != NULL) { |
| 8654 | mklocal(name); |
| 8655 | } |
| 8656 | return 0; |
| 8657 | } |
| 8658 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8659 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8660 | falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8661 | { |
| 8662 | return 1; |
| 8663 | } |
| 8664 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8665 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8666 | truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8667 | { |
| 8668 | return 0; |
| 8669 | } |
| 8670 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8671 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8672 | execcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8673 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8674 | if (argv[1]) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8675 | iflag = 0; /* exit on error */ |
| 8676 | mflag = 0; |
| 8677 | optschanged(); |
| 8678 | shellexec(argv + 1, pathval(), 0); |
| 8679 | } |
| 8680 | return 0; |
| 8681 | } |
| 8682 | |
| 8683 | /* |
| 8684 | * The return command. |
| 8685 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8686 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8687 | returncmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8688 | { |
| 8689 | /* |
| 8690 | * If called outside a function, do what ksh does; |
| 8691 | * skip the rest of the file. |
| 8692 | */ |
| 8693 | evalskip = funcnest ? SKIPFUNC : SKIPFILE; |
| 8694 | return argv[1] ? number(argv[1]) : exitstatus; |
| 8695 | } |
| 8696 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8697 | /* Forward declarations for builtintab[] */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8698 | static int breakcmd(int, char **) FAST_FUNC; |
| 8699 | static int dotcmd(int, char **) FAST_FUNC; |
| 8700 | static int evalcmd(int, char **) FAST_FUNC; |
| 8701 | static int exitcmd(int, char **) FAST_FUNC; |
| 8702 | static int exportcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8703 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8704 | static int getoptscmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8705 | #endif |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8706 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8707 | static int helpcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8708 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 8709 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8710 | static int letcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8711 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8712 | static int readcmd(int, char **) FAST_FUNC; |
| 8713 | static int setcmd(int, char **) FAST_FUNC; |
| 8714 | static int shiftcmd(int, char **) FAST_FUNC; |
| 8715 | static int timescmd(int, char **) FAST_FUNC; |
| 8716 | static int trapcmd(int, char **) FAST_FUNC; |
| 8717 | static int umaskcmd(int, char **) FAST_FUNC; |
| 8718 | static int unsetcmd(int, char **) FAST_FUNC; |
| 8719 | static int ulimitcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8720 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8721 | #define BUILTIN_NOSPEC "0" |
| 8722 | #define BUILTIN_SPECIAL "1" |
| 8723 | #define BUILTIN_REGULAR "2" |
| 8724 | #define BUILTIN_SPEC_REG "3" |
| 8725 | #define BUILTIN_ASSIGN "4" |
| 8726 | #define BUILTIN_SPEC_ASSG "5" |
| 8727 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8728 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 8729 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8730 | /* Stubs for calling non-FAST_FUNC's */ |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8731 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8732 | static int FAST_FUNC echocmd(int argc, char **argv) { return echo_main(argc, argv); } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8733 | #endif |
| 8734 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8735 | static int FAST_FUNC printfcmd(int argc, char **argv) { return printf_main(argc, argv); } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8736 | #endif |
| 8737 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8738 | static int FAST_FUNC testcmd(int argc, char **argv) { return test_main(argc, argv); } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8739 | #endif |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 8740 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 8741 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8742 | static const struct builtincmd builtintab[] = { |
| 8743 | { BUILTIN_SPEC_REG ".", dotcmd }, |
| 8744 | { BUILTIN_SPEC_REG ":", truecmd }, |
| 8745 | #if ENABLE_ASH_BUILTIN_TEST |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8746 | { BUILTIN_REGULAR "[", testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8747 | #if ENABLE_ASH_BASH_COMPAT |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8748 | { BUILTIN_REGULAR "[[", testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8749 | #endif |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8750 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8751 | #if ENABLE_ASH_ALIAS |
| 8752 | { BUILTIN_REG_ASSG "alias", aliascmd }, |
| 8753 | #endif |
| 8754 | #if JOBS |
| 8755 | { BUILTIN_REGULAR "bg", fg_bgcmd }, |
| 8756 | #endif |
| 8757 | { BUILTIN_SPEC_REG "break", breakcmd }, |
| 8758 | { BUILTIN_REGULAR "cd", cdcmd }, |
| 8759 | { BUILTIN_NOSPEC "chdir", cdcmd }, |
| 8760 | #if ENABLE_ASH_CMDCMD |
| 8761 | { BUILTIN_REGULAR "command", commandcmd }, |
| 8762 | #endif |
| 8763 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
| 8764 | #if ENABLE_ASH_BUILTIN_ECHO |
| 8765 | { BUILTIN_REGULAR "echo", echocmd }, |
| 8766 | #endif |
| 8767 | { BUILTIN_SPEC_REG "eval", evalcmd }, |
| 8768 | { BUILTIN_SPEC_REG "exec", execcmd }, |
| 8769 | { BUILTIN_SPEC_REG "exit", exitcmd }, |
| 8770 | { BUILTIN_SPEC_REG_ASSG "export", exportcmd }, |
| 8771 | { BUILTIN_REGULAR "false", falsecmd }, |
| 8772 | #if JOBS |
| 8773 | { BUILTIN_REGULAR "fg", fg_bgcmd }, |
| 8774 | #endif |
| 8775 | #if ENABLE_ASH_GETOPTS |
| 8776 | { BUILTIN_REGULAR "getopts", getoptscmd }, |
| 8777 | #endif |
| 8778 | { BUILTIN_NOSPEC "hash", hashcmd }, |
| 8779 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8780 | { BUILTIN_NOSPEC "help", helpcmd }, |
| 8781 | #endif |
| 8782 | #if JOBS |
| 8783 | { BUILTIN_REGULAR "jobs", jobscmd }, |
| 8784 | { BUILTIN_REGULAR "kill", killcmd }, |
| 8785 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 8786 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8787 | { BUILTIN_NOSPEC "let", letcmd }, |
| 8788 | #endif |
| 8789 | { BUILTIN_ASSIGN "local", localcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8790 | #if ENABLE_ASH_BUILTIN_PRINTF |
| 8791 | { BUILTIN_REGULAR "printf", printfcmd }, |
| 8792 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8793 | { BUILTIN_NOSPEC "pwd", pwdcmd }, |
| 8794 | { BUILTIN_REGULAR "read", readcmd }, |
| 8795 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 8796 | { BUILTIN_SPEC_REG "return", returncmd }, |
| 8797 | { BUILTIN_SPEC_REG "set", setcmd }, |
| 8798 | { BUILTIN_SPEC_REG "shift", shiftcmd }, |
| 8799 | { BUILTIN_SPEC_REG "source", dotcmd }, |
| 8800 | #if ENABLE_ASH_BUILTIN_TEST |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8801 | { BUILTIN_REGULAR "test", testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8802 | #endif |
| 8803 | { BUILTIN_SPEC_REG "times", timescmd }, |
| 8804 | { BUILTIN_SPEC_REG "trap", trapcmd }, |
| 8805 | { BUILTIN_REGULAR "true", truecmd }, |
| 8806 | { BUILTIN_NOSPEC "type", typecmd }, |
| 8807 | { BUILTIN_NOSPEC "ulimit", ulimitcmd }, |
| 8808 | { BUILTIN_REGULAR "umask", umaskcmd }, |
| 8809 | #if ENABLE_ASH_ALIAS |
| 8810 | { BUILTIN_REGULAR "unalias", unaliascmd }, |
| 8811 | #endif |
| 8812 | { BUILTIN_SPEC_REG "unset", unsetcmd }, |
| 8813 | { BUILTIN_REGULAR "wait", waitcmd }, |
| 8814 | }; |
| 8815 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8816 | /* Should match the above table! */ |
| 8817 | #define COMMANDCMD (builtintab + \ |
| 8818 | 2 + \ |
| 8819 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 8820 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 8821 | 1 * ENABLE_ASH_ALIAS + \ |
| 8822 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 8823 | 3) |
| 8824 | #define EXECCMD (builtintab + \ |
| 8825 | 2 + \ |
| 8826 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 8827 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 8828 | 1 * ENABLE_ASH_ALIAS + \ |
| 8829 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 8830 | 3 + \ |
| 8831 | 1 * ENABLE_ASH_CMDCMD + \ |
| 8832 | 1 + \ |
| 8833 | ENABLE_ASH_BUILTIN_ECHO + \ |
| 8834 | 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8835 | |
| 8836 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8837 | * Search the table of builtin commands. |
| 8838 | */ |
| 8839 | static struct builtincmd * |
| 8840 | find_builtin(const char *name) |
| 8841 | { |
| 8842 | struct builtincmd *bp; |
| 8843 | |
| 8844 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 8845 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8846 | pstrcmp |
| 8847 | ); |
| 8848 | return bp; |
| 8849 | } |
| 8850 | |
| 8851 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8852 | * Execute a simple command. |
| 8853 | */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8854 | static int |
| 8855 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8856 | { |
| 8857 | const char *q = endofname(p); |
| 8858 | if (p == q) |
| 8859 | return 0; |
| 8860 | return *q == '='; |
| 8861 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8862 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8863 | bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8864 | { |
| 8865 | /* Preserve exitstatus of a previous possible redirection |
| 8866 | * as POSIX mandates */ |
| 8867 | return back_exitstatus; |
| 8868 | } |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8869 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8870 | evalcommand(union node *cmd, int flags) |
| 8871 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 8872 | static const struct builtincmd null_bltin = { |
| 8873 | "\0\0", bltincmd /* why three NULs? */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8874 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8875 | struct stackmark smark; |
| 8876 | union node *argp; |
| 8877 | struct arglist arglist; |
| 8878 | struct arglist varlist; |
| 8879 | char **argv; |
| 8880 | int argc; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8881 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8882 | struct cmdentry cmdentry; |
| 8883 | struct job *jp; |
| 8884 | char *lastarg; |
| 8885 | const char *path; |
| 8886 | int spclbltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8887 | int status; |
| 8888 | char **nargv; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8889 | struct builtincmd *bcmd; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 8890 | smallint cmd_is_exec; |
| 8891 | smallint pseudovarflag = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8892 | |
| 8893 | /* First expand the arguments. */ |
| 8894 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 8895 | setstackmark(&smark); |
| 8896 | back_exitstatus = 0; |
| 8897 | |
| 8898 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 8899 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8900 | varlist.lastp = &varlist.list; |
| 8901 | *varlist.lastp = NULL; |
| 8902 | arglist.lastp = &arglist.list; |
| 8903 | *arglist.lastp = NULL; |
| 8904 | |
| 8905 | argc = 0; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8906 | if (cmd->ncmd.args) { |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8907 | bcmd = find_builtin(cmd->ncmd.args->narg.text); |
| 8908 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
| 8909 | } |
| 8910 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8911 | for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) { |
| 8912 | struct strlist **spp; |
| 8913 | |
| 8914 | spp = arglist.lastp; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 8915 | if (pseudovarflag && isassignment(argp->narg.text)) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8916 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 8917 | else |
| 8918 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 8919 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8920 | for (sp = *spp; sp; sp = sp->next) |
| 8921 | argc++; |
| 8922 | } |
| 8923 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8924 | argv = nargv = stalloc(sizeof(char *) * (argc + 1)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8925 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8926 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 8927 | *nargv++ = sp->text; |
| 8928 | } |
| 8929 | *nargv = NULL; |
| 8930 | |
| 8931 | lastarg = NULL; |
| 8932 | if (iflag && funcnest == 0 && argc > 0) |
| 8933 | lastarg = nargv[-1]; |
| 8934 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8935 | preverrout_fd = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8936 | expredir(cmd->ncmd.redirect); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8937 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8938 | |
| 8939 | path = vpath.text; |
| 8940 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 8941 | struct strlist **spp; |
| 8942 | char *p; |
| 8943 | |
| 8944 | spp = varlist.lastp; |
| 8945 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 8946 | |
| 8947 | /* |
| 8948 | * Modify the command lookup path, if a PATH= assignment |
| 8949 | * is present |
| 8950 | */ |
| 8951 | p = (*spp)->text; |
| 8952 | if (varequal(p, path)) |
| 8953 | path = p; |
| 8954 | } |
| 8955 | |
| 8956 | /* Print the command if xflag is set. */ |
| 8957 | if (xflag) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8958 | int n; |
| 8959 | const char *p = " %s"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8960 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8961 | p++; |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 8962 | fdprintf(preverrout_fd, p, expandstr(ps4val())); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8963 | |
| 8964 | sp = varlist.list; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8965 | for (n = 0; n < 2; n++) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8966 | while (sp) { |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 8967 | fdprintf(preverrout_fd, p, sp->text); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8968 | sp = sp->next; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 8969 | if (*p == '%') { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 8970 | p--; |
| 8971 | } |
| 8972 | } |
| 8973 | sp = arglist.list; |
| 8974 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 8975 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8976 | } |
| 8977 | |
| 8978 | cmd_is_exec = 0; |
| 8979 | spclbltin = -1; |
| 8980 | |
| 8981 | /* Now locate the command. */ |
| 8982 | if (argc) { |
| 8983 | const char *oldpath; |
| 8984 | int cmd_flag = DO_ERR; |
| 8985 | |
| 8986 | path += 5; |
| 8987 | oldpath = path; |
| 8988 | for (;;) { |
| 8989 | find_command(argv[0], &cmdentry, cmd_flag, path); |
| 8990 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8991 | flush_stderr(); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 8992 | status = 127; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8993 | goto bail; |
| 8994 | } |
| 8995 | |
| 8996 | /* implement bltin and command here */ |
| 8997 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 8998 | break; |
| 8999 | if (spclbltin < 0) |
| 9000 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
| 9001 | if (cmdentry.u.cmd == EXECCMD) |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9002 | cmd_is_exec = 1; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9003 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9004 | if (cmdentry.u.cmd == COMMANDCMD) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9005 | path = oldpath; |
| 9006 | nargv = parse_command_args(argv, &path); |
| 9007 | if (!nargv) |
| 9008 | break; |
| 9009 | argc -= nargv - argv; |
| 9010 | argv = nargv; |
| 9011 | cmd_flag |= DO_NOFUNC; |
| 9012 | } else |
| 9013 | #endif |
| 9014 | break; |
| 9015 | } |
| 9016 | } |
| 9017 | |
| 9018 | if (status) { |
| 9019 | /* We have a redirection error. */ |
| 9020 | if (spclbltin > 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9021 | raise_exception(EXERROR); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9022 | bail: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9023 | exitstatus = status; |
| 9024 | goto out; |
| 9025 | } |
| 9026 | |
| 9027 | /* Execute the command. */ |
| 9028 | switch (cmdentry.cmdtype) { |
| 9029 | default: |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9030 | |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9031 | #if ENABLE_FEATURE_SH_NOFORK |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9032 | /* Hmmm... shouldn't it happen somewhere in forkshell() instead? |
| 9033 | * Why "fork off a child process if necessary" doesn't apply to NOFORK? */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9034 | { |
| 9035 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 9036 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9037 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9038 | listsetvar(varlist.list, VEXPORT|VSTACK); |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9039 | /* run <applet>_main() */ |
| 9040 | exitstatus = run_nofork_applet(applet_no, argv); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9041 | break; |
| 9042 | } |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9043 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9044 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9045 | /* Fork off a child process if necessary. */ |
| 9046 | if (!(flags & EV_EXIT) || trap[0]) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9047 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9048 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9049 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
| 9050 | exitstatus = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9051 | INT_ON; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9052 | TRACE(("forked child exited with %d\n", exitstatus)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9053 | break; |
| 9054 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9055 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9056 | } |
| 9057 | listsetvar(varlist.list, VEXPORT|VSTACK); |
| 9058 | shellexec(argv, path, cmdentry.u.index); |
| 9059 | /* NOTREACHED */ |
| 9060 | |
| 9061 | case CMDBUILTIN: |
| 9062 | cmdenviron = varlist.list; |
| 9063 | if (cmdenviron) { |
| 9064 | struct strlist *list = cmdenviron; |
| 9065 | int i = VNOSET; |
| 9066 | if (spclbltin > 0 || argc == 0) { |
| 9067 | i = 0; |
| 9068 | if (cmd_is_exec && argc > 1) |
| 9069 | i = VEXPORT; |
| 9070 | } |
| 9071 | listsetvar(list, i); |
| 9072 | } |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9073 | /* Tight loop with builtins only: |
| 9074 | * "while kill -0 $child; do true; done" |
| 9075 | * will never exit even if $child died, unless we do this |
| 9076 | * to reap the zombie and make kill detect that it's gone: */ |
| 9077 | dowait(DOWAIT_NONBLOCK, NULL); |
| 9078 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9079 | if (evalbltin(cmdentry.u.cmd, argc, argv)) { |
| 9080 | int exit_status; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 9081 | int i = exception_type; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9082 | if (i == EXEXIT) |
| 9083 | goto raise; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9084 | exit_status = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9085 | if (i == EXINT) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 9086 | exit_status = 128 + SIGINT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9087 | if (i == EXSIG) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 9088 | exit_status = 128 + pendingsig; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9089 | exitstatus = exit_status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9090 | if (i == EXINT || spclbltin > 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9091 | raise: |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9092 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9093 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9094 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9095 | } |
| 9096 | break; |
| 9097 | |
| 9098 | case CMDFUNCTION: |
| 9099 | listsetvar(varlist.list, 0); |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9100 | /* See above for the rationale */ |
| 9101 | dowait(DOWAIT_NONBLOCK, NULL); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9102 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 9103 | goto raise; |
| 9104 | break; |
| 9105 | } |
| 9106 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9107 | out: |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9108 | popredir(/*drop:*/ cmd_is_exec, /*restore:*/ cmd_is_exec); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9109 | if (lastarg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9110 | /* dsl: I think this is intended to be used to support |
| 9111 | * '_' in 'vi' command mode during line editing... |
| 9112 | * However I implemented that within libedit itself. |
| 9113 | */ |
| 9114 | setvar("_", lastarg, 0); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9115 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9116 | popstackmark(&smark); |
| 9117 | } |
| 9118 | |
| 9119 | static int |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9120 | evalbltin(const struct builtincmd *cmd, int argc, char **argv) |
| 9121 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9122 | char *volatile savecmdname; |
| 9123 | struct jmploc *volatile savehandler; |
| 9124 | struct jmploc jmploc; |
| 9125 | int i; |
| 9126 | |
| 9127 | savecmdname = commandname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9128 | i = setjmp(jmploc.loc); |
| 9129 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9130 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9131 | savehandler = exception_handler; |
| 9132 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9133 | commandname = argv[0]; |
| 9134 | argptr = argv + 1; |
| 9135 | optptr = NULL; /* initialize nextopt */ |
| 9136 | exitstatus = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9137 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9138 | cmddone: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9139 | exitstatus |= ferror(stdout); |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 9140 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9141 | commandname = savecmdname; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 9142 | // exsig = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9143 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9144 | |
| 9145 | return i; |
| 9146 | } |
| 9147 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9148 | static int |
| 9149 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9150 | { |
| 9151 | return !*endofname(p); |
| 9152 | } |
| 9153 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9154 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9155 | /* |
| 9156 | * Search for a command. This is called before we fork so that the |
| 9157 | * 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] | 9158 | * the child. The check for "goodname" is an overly conservative |
| 9159 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9160 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9161 | static void |
| 9162 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9163 | { |
| 9164 | struct cmdentry entry; |
| 9165 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9166 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 9167 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9168 | } |
| 9169 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9170 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9171 | /* ============ Builtin commands |
| 9172 | * |
| 9173 | * Builtin commands whose functions are closely tied to evaluation |
| 9174 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9175 | */ |
| 9176 | |
| 9177 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9178 | * Handle break and continue commands. Break, continue, and return are |
| 9179 | * all handled by setting the evalskip flag. The evaluation routines |
| 9180 | * above all check this flag, and if it is set they start skipping |
| 9181 | * commands rather than executing them. The variable skipcount is |
| 9182 | * the number of loops to break/continue, or the number of function |
| 9183 | * levels to return. (The latter is always 1.) It should probably |
| 9184 | * be an error to break out of more loops than exist, but it isn't |
| 9185 | * in the standard shell so we don't make it one here. |
| 9186 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9187 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9188 | breakcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9189 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9190 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9191 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 9192 | if (n <= 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9193 | ash_msg_and_raise_error(illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9194 | if (n > loopnest) |
| 9195 | n = loopnest; |
| 9196 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9197 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9198 | skipcount = n; |
| 9199 | } |
| 9200 | return 0; |
| 9201 | } |
| 9202 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9203 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9204 | /* ============ input.c |
| 9205 | * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 9206 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9207 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9208 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9209 | enum { |
| 9210 | INPUT_PUSH_FILE = 1, |
| 9211 | INPUT_NOFILE_OK = 2, |
| 9212 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9213 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9214 | static smallint checkkwd; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9215 | /* values of checkkwd variable */ |
| 9216 | #define CHKALIAS 0x1 |
| 9217 | #define CHKKWD 0x2 |
| 9218 | #define CHKNL 0x4 |
| 9219 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9220 | /* |
| 9221 | * Push a string back onto the input at this current parsefile level. |
| 9222 | * We handle aliases this way. |
| 9223 | */ |
| 9224 | #if !ENABLE_ASH_ALIAS |
| 9225 | #define pushstring(s, ap) pushstring(s) |
| 9226 | #endif |
| 9227 | static void |
| 9228 | pushstring(char *s, struct alias *ap) |
| 9229 | { |
| 9230 | struct strpush *sp; |
| 9231 | int len; |
| 9232 | |
| 9233 | len = strlen(s); |
| 9234 | INT_OFF; |
| 9235 | if (g_parsefile->strpush) { |
| 9236 | sp = ckzalloc(sizeof(*sp)); |
| 9237 | sp->prev = g_parsefile->strpush; |
| 9238 | } else { |
| 9239 | sp = &(g_parsefile->basestrpush); |
| 9240 | } |
| 9241 | g_parsefile->strpush = sp; |
| 9242 | sp->prev_string = g_parsefile->next_to_pgetc; |
| 9243 | sp->prev_left_in_line = g_parsefile->left_in_line; |
| 9244 | #if ENABLE_ASH_ALIAS |
| 9245 | sp->ap = ap; |
| 9246 | if (ap) { |
| 9247 | ap->flag |= ALIASINUSE; |
| 9248 | sp->string = s; |
| 9249 | } |
| 9250 | #endif |
| 9251 | g_parsefile->next_to_pgetc = s; |
| 9252 | g_parsefile->left_in_line = len; |
| 9253 | INT_ON; |
| 9254 | } |
| 9255 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9256 | static void |
| 9257 | popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9258 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9259 | struct strpush *sp = g_parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9260 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9261 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9262 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9263 | if (sp->ap) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9264 | if (g_parsefile->next_to_pgetc[-1] == ' ' |
| 9265 | || g_parsefile->next_to_pgetc[-1] == '\t' |
| 9266 | ) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9267 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9268 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9269 | if (sp->string != sp->ap->val) { |
| 9270 | free(sp->string); |
| 9271 | } |
| 9272 | sp->ap->flag &= ~ALIASINUSE; |
| 9273 | if (sp->ap->flag & ALIASDEAD) { |
| 9274 | unalias(sp->ap->name); |
| 9275 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9276 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9277 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9278 | g_parsefile->next_to_pgetc = sp->prev_string; |
| 9279 | g_parsefile->left_in_line = sp->prev_left_in_line; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9280 | g_parsefile->strpush = sp->prev; |
| 9281 | if (sp != &(g_parsefile->basestrpush)) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9282 | free(sp); |
| 9283 | INT_ON; |
| 9284 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9285 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9286 | //FIXME: BASH_COMPAT with "...&" does TWO pungetc(): |
| 9287 | //it peeks whether it is &>, and then pushes back both chars. |
| 9288 | //This function needs to save last *next_to_pgetc to buf[0] |
| 9289 | //to make two pungetc() reliable. Currently, |
| 9290 | // pgetc (out of buf: does preadfd), pgetc, pungetc, pungetc won't work... |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9291 | static int |
| 9292 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9293 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9294 | int nr; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 9295 | char *buf = g_parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9296 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9297 | g_parsefile->next_to_pgetc = buf; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9298 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9299 | retry: |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9300 | if (!iflag || g_parsefile->fd != STDIN_FILENO) |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9301 | nr = nonblock_safe_read(g_parsefile->fd, buf, BUFSIZ - 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9302 | else { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9303 | #if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9304 | line_input_state->path_lookup = pathval(); |
Eric Andersen | 4a79c0e | 2004-09-08 10:01:07 +0000 | [diff] [blame] | 9305 | #endif |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9306 | nr = read_line_input(cmdedit_prompt, buf, BUFSIZ, line_input_state); |
| 9307 | if (nr == 0) { |
| 9308 | /* Ctrl+C pressed */ |
| 9309 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9310 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9311 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9312 | raise(SIGINT); |
| 9313 | return 1; |
| 9314 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9315 | goto retry; |
| 9316 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9317 | if (nr < 0 && errno == 0) { |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9318 | /* Ctrl+D pressed */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9319 | nr = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9320 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9321 | } |
| 9322 | #else |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 9323 | nr = nonblock_safe_read(g_parsefile->fd, buf, BUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9324 | #endif |
| 9325 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9326 | #if 0 |
| 9327 | /* nonblock_safe_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9328 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9329 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 9330 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9331 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 9332 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9333 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 9334 | out2str("sh: turning off NDELAY mode\n"); |
| 9335 | goto retry; |
| 9336 | } |
| 9337 | } |
| 9338 | } |
| 9339 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9340 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9341 | return nr; |
| 9342 | } |
| 9343 | |
| 9344 | /* |
| 9345 | * Refill the input buffer and return the next input character: |
| 9346 | * |
| 9347 | * 1) If a string was pushed back on the input, pop it; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9348 | * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM) |
| 9349 | * or we are reading from a string so we can't refill the buffer, |
| 9350 | * return EOF. |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame^] | 9351 | * 3) If there is more stuff in this buffer, use it else call read to fill it. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9352 | * 4) Process input up to the next newline, deleting nul characters. |
| 9353 | */ |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9354 | //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__) |
| 9355 | #define pgetc_debug(...) ((void)0) |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 9356 | /* |
| 9357 | * NB: due to SIT(c) internals (syntax_index_table[] vector), |
| 9358 | * pgetc() and related functions must return chars SIGN-EXTENDED into ints, |
| 9359 | * not zero-extended. Seems fragile to me. Affects only !USE_SIT_FUNCTION case, |
| 9360 | * so we can fix it by ditching !USE_SIT_FUNCTION if Unicode requires that. |
| 9361 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9362 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9363 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9364 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9365 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9366 | int more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9367 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9368 | while (g_parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9369 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9370 | if (g_parsefile->left_in_line == -1 |
| 9371 | && g_parsefile->strpush->ap |
| 9372 | && g_parsefile->next_to_pgetc[-1] != ' ' |
| 9373 | && g_parsefile->next_to_pgetc[-1] != '\t' |
Denis Vlasenko | 1689840 | 2008-11-25 01:34:52 +0000 | [diff] [blame] | 9374 | ) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9375 | pgetc_debug("preadbuffer PEOA"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9376 | return PEOA; |
| 9377 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9378 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9379 | popstring(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9380 | /* try "pgetc" now: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9381 | pgetc_debug("preadbuffer internal pgetc at %d:%p'%s'", |
| 9382 | g_parsefile->left_in_line, |
| 9383 | g_parsefile->next_to_pgetc, |
| 9384 | g_parsefile->next_to_pgetc); |
| 9385 | if (--g_parsefile->left_in_line >= 0) |
| 9386 | return (unsigned char)(*g_parsefile->next_to_pgetc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9387 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9388 | /* on both branches above g_parsefile->left_in_line < 0. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9389 | * "pgetc" needs refilling. |
| 9390 | */ |
| 9391 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9392 | /* -90 is our -BIGNUM. Below we use -99 to mark "EOF on read", |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9393 | * pungetc() may increment it a few times. |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9394 | * Assuming it won't increment it to less than -90. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9395 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9396 | if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9397 | pgetc_debug("preadbuffer PEOF1"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9398 | /* even in failure keep left_in_line and next_to_pgetc |
| 9399 | * in lock step, for correct multi-layer pungetc. |
| 9400 | * left_in_line was decremented before preadbuffer(), |
| 9401 | * must inc next_to_pgetc: */ |
| 9402 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9403 | return PEOF; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9404 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9405 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9406 | more = g_parsefile->left_in_buffer; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9407 | if (more <= 0) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9408 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9409 | again: |
| 9410 | more = preadfd(); |
| 9411 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9412 | /* don't try reading again */ |
| 9413 | g_parsefile->left_in_line = -99; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9414 | pgetc_debug("preadbuffer PEOF2"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9415 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9416 | return PEOF; |
| 9417 | } |
| 9418 | } |
| 9419 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9420 | /* Find out where's the end of line. |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9421 | * Set g_parsefile->left_in_line |
| 9422 | * and g_parsefile->left_in_buffer acordingly. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9423 | * NUL chars are deleted. |
| 9424 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9425 | q = g_parsefile->next_to_pgetc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9426 | for (;;) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9427 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9428 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9429 | more--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9430 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9431 | c = *q; |
| 9432 | if (c == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9433 | memmove(q, q + 1, more); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9434 | } else { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9435 | q++; |
| 9436 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9437 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9438 | break; |
| 9439 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9440 | } |
| 9441 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9442 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9443 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
| 9444 | if (g_parsefile->left_in_line < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9445 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9446 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9447 | } |
| 9448 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9449 | g_parsefile->left_in_buffer = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9450 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9451 | if (vflag) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9452 | char save = *q; |
| 9453 | *q = '\0'; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9454 | out2str(g_parsefile->next_to_pgetc); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9455 | *q = save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9456 | } |
| 9457 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9458 | pgetc_debug("preadbuffer at %d:%p'%s'", |
| 9459 | g_parsefile->left_in_line, |
| 9460 | g_parsefile->next_to_pgetc, |
| 9461 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 9462 | return signed_char2int(*g_parsefile->next_to_pgetc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9463 | } |
| 9464 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9465 | #define pgetc_as_macro() \ |
| 9466 | (--g_parsefile->left_in_line >= 0 \ |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 9467 | ? signed_char2int(*g_parsefile->next_to_pgetc++) \ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9468 | : preadbuffer() \ |
| 9469 | ) |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9470 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9471 | static int |
| 9472 | pgetc(void) |
| 9473 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9474 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9475 | g_parsefile->left_in_line, |
| 9476 | g_parsefile->next_to_pgetc, |
| 9477 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9478 | return pgetc_as_macro(); |
| 9479 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9480 | |
| 9481 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9482 | #define pgetc_fast() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9483 | #else |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9484 | #define pgetc_fast() pgetc_as_macro() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9485 | #endif |
| 9486 | |
| 9487 | /* |
| 9488 | * Same as pgetc(), but ignores PEOA. |
| 9489 | */ |
| 9490 | #if ENABLE_ASH_ALIAS |
| 9491 | static int |
| 9492 | pgetc2(void) |
| 9493 | { |
| 9494 | int c; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9495 | do { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9496 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9497 | g_parsefile->left_in_line, |
| 9498 | g_parsefile->next_to_pgetc, |
| 9499 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9500 | c = pgetc_fast(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9501 | } while (c == PEOA); |
| 9502 | return c; |
| 9503 | } |
| 9504 | #else |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9505 | #define pgetc2() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9506 | #endif |
| 9507 | |
| 9508 | /* |
| 9509 | * Read a line from the script. |
| 9510 | */ |
| 9511 | static char * |
| 9512 | pfgets(char *line, int len) |
| 9513 | { |
| 9514 | char *p = line; |
| 9515 | int nleft = len; |
| 9516 | int c; |
| 9517 | |
| 9518 | while (--nleft > 0) { |
| 9519 | c = pgetc2(); |
| 9520 | if (c == PEOF) { |
| 9521 | if (p == line) |
| 9522 | return NULL; |
| 9523 | break; |
| 9524 | } |
| 9525 | *p++ = c; |
| 9526 | if (c == '\n') |
| 9527 | break; |
| 9528 | } |
| 9529 | *p = '\0'; |
| 9530 | return line; |
| 9531 | } |
| 9532 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9533 | /* |
| 9534 | * Undo the last call to pgetc. Only one character may be pushed back. |
| 9535 | * PEOF may be pushed back. |
| 9536 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9537 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9538 | pungetc(void) |
| 9539 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9540 | g_parsefile->left_in_line++; |
| 9541 | g_parsefile->next_to_pgetc--; |
| 9542 | pgetc_debug("pushed back to %d:%p'%s'", |
| 9543 | g_parsefile->left_in_line, |
| 9544 | g_parsefile->next_to_pgetc, |
| 9545 | g_parsefile->next_to_pgetc); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9546 | } |
| 9547 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9548 | /* |
| 9549 | * To handle the "." command, a stack of input files is used. Pushfile |
| 9550 | * adds a new entry to the stack and popfile restores the previous level. |
| 9551 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9552 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9553 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9554 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9555 | struct parsefile *pf; |
| 9556 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9557 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9558 | pf->prev = g_parsefile; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9559 | pf->fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9560 | /*pf->strpush = NULL; - ckzalloc did it */ |
| 9561 | /*pf->basestrpush.prev = NULL;*/ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9562 | g_parsefile = pf; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9563 | } |
| 9564 | |
| 9565 | static void |
| 9566 | popfile(void) |
| 9567 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9568 | struct parsefile *pf = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9569 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9570 | INT_OFF; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9571 | if (pf->fd >= 0) |
| 9572 | close(pf->fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 9573 | free(pf->buf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9574 | while (pf->strpush) |
| 9575 | popstring(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9576 | g_parsefile = pf->prev; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9577 | free(pf); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9578 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9579 | } |
| 9580 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9581 | /* |
| 9582 | * Return to top level. |
| 9583 | */ |
| 9584 | static void |
| 9585 | popallfiles(void) |
| 9586 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9587 | while (g_parsefile != &basepf) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9588 | popfile(); |
| 9589 | } |
| 9590 | |
| 9591 | /* |
| 9592 | * Close the file(s) that the shell is reading commands from. Called |
| 9593 | * after a fork is done. |
| 9594 | */ |
| 9595 | static void |
| 9596 | closescript(void) |
| 9597 | { |
| 9598 | popallfiles(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9599 | if (g_parsefile->fd > 0) { |
| 9600 | close(g_parsefile->fd); |
| 9601 | g_parsefile->fd = 0; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9602 | } |
| 9603 | } |
| 9604 | |
| 9605 | /* |
| 9606 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 9607 | * interrupts off. |
| 9608 | */ |
| 9609 | static void |
| 9610 | setinputfd(int fd, int push) |
| 9611 | { |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 9612 | close_on_exec_on(fd); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9613 | if (push) { |
| 9614 | pushfile(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9615 | g_parsefile->buf = NULL; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9616 | } |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9617 | g_parsefile->fd = fd; |
| 9618 | if (g_parsefile->buf == NULL) |
| 9619 | g_parsefile->buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9620 | g_parsefile->left_in_buffer = 0; |
| 9621 | g_parsefile->left_in_line = 0; |
| 9622 | g_parsefile->linno = 1; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9623 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9624 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9625 | /* |
| 9626 | * Set the input to take input from a file. If push is set, push the |
| 9627 | * old input onto the stack first. |
| 9628 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9629 | static int |
| 9630 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9631 | { |
| 9632 | int fd; |
| 9633 | int fd2; |
| 9634 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9635 | INT_OFF; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9636 | fd = open(fname, O_RDONLY); |
| 9637 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9638 | if (flags & INPUT_NOFILE_OK) |
| 9639 | goto out; |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 9640 | ash_msg_and_raise_error("can't open '%s'", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9641 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9642 | if (fd < 10) { |
| 9643 | fd2 = copyfd(fd, 10); |
| 9644 | close(fd); |
| 9645 | if (fd2 < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 9646 | ash_msg_and_raise_error("out of file descriptors"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9647 | fd = fd2; |
| 9648 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9649 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9650 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9651 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9652 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9653 | } |
| 9654 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9655 | /* |
| 9656 | * Like setinputfile, but takes input from a string. |
| 9657 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9658 | static void |
| 9659 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9660 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9661 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9662 | pushfile(); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9663 | g_parsefile->next_to_pgetc = string; |
| 9664 | g_parsefile->left_in_line = strlen(string); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9665 | g_parsefile->buf = NULL; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9666 | g_parsefile->linno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9667 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9668 | } |
| 9669 | |
| 9670 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9671 | /* ============ mail.c |
| 9672 | * |
| 9673 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9674 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9675 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9676 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9677 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9678 | #define MAXMBOXES 10 |
| 9679 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9680 | /* times of mailboxes */ |
| 9681 | static time_t mailtime[MAXMBOXES]; |
| 9682 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9683 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9684 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9685 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9686 | * Print appropriate message(s) if mail has arrived. |
| 9687 | * If mail_var_path_changed is set, |
| 9688 | * then the value of MAIL has mail_var_path_changed, |
| 9689 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9690 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9691 | static void |
| 9692 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9693 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9694 | const char *mpath; |
| 9695 | char *p; |
| 9696 | char *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9697 | time_t *mtp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9698 | struct stackmark smark; |
| 9699 | struct stat statb; |
| 9700 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9701 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9702 | mpath = mpathset() ? mpathval() : mailval(); |
| 9703 | for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 9704 | p = path_advance(&mpath, nullstr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9705 | if (p == NULL) |
| 9706 | break; |
| 9707 | if (*p == '\0') |
| 9708 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9709 | for (q = p; *q; q++) |
| 9710 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 9711 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9712 | if (q[-1] != '/') |
| 9713 | abort(); |
| 9714 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9715 | q[-1] = '\0'; /* delete trailing '/' */ |
| 9716 | if (stat(p, &statb) < 0) { |
| 9717 | *mtp = 0; |
| 9718 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9719 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9720 | if (!mail_var_path_changed && statb.st_mtime != *mtp) { |
| 9721 | fprintf( |
| 9722 | stderr, snlfmt, |
| 9723 | pathopt ? pathopt : "you have mail" |
| 9724 | ); |
| 9725 | } |
| 9726 | *mtp = statb.st_mtime; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9727 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9728 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9729 | popstackmark(&smark); |
| 9730 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9731 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9732 | static void FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9733 | changemail(const char *val UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9734 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9735 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9736 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9737 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9738 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9739 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9740 | |
| 9741 | /* ============ ??? */ |
| 9742 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9743 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9744 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9745 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9746 | static void |
| 9747 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9748 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9749 | char **newparam; |
| 9750 | char **ap; |
| 9751 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9752 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9753 | for (nparam = 0; argv[nparam]; nparam++) |
| 9754 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9755 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 9756 | while (*argv) { |
| 9757 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9758 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9759 | *ap = NULL; |
| 9760 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9761 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9762 | shellparam.nparam = nparam; |
| 9763 | shellparam.p = newparam; |
| 9764 | #if ENABLE_ASH_GETOPTS |
| 9765 | shellparam.optind = 1; |
| 9766 | shellparam.optoff = -1; |
| 9767 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9768 | } |
| 9769 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9770 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9771 | * Process shell options. The global variable argptr contains a pointer |
| 9772 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 9773 | * |
| 9774 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 9775 | * For a non-interactive shell, an error condition encountered |
| 9776 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 9777 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 9778 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 9779 | * ... |
| 9780 | * Utility syntax error (option or operand error) Shall exit |
| 9781 | * ... |
| 9782 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 9783 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 9784 | * and shell continues), and people rely on this behavior! |
| 9785 | * Testcase: |
| 9786 | * set -o barfoo 2>/dev/null |
| 9787 | * echo $? |
| 9788 | * |
| 9789 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9790 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9791 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9792 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9793 | { |
| 9794 | int i; |
| 9795 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9796 | if (name) { |
| 9797 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 9798 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9799 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9800 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9801 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9802 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9803 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9804 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9805 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9806 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9807 | if (val) { |
| 9808 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 9809 | } else { |
| 9810 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 9811 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9812 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9813 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9814 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9815 | static void |
| 9816 | setoption(int flag, int val) |
| 9817 | { |
| 9818 | int i; |
| 9819 | |
| 9820 | for (i = 0; i < NOPTS; i++) { |
| 9821 | if (optletters(i) == flag) { |
| 9822 | optlist[i] = val; |
| 9823 | return; |
| 9824 | } |
| 9825 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9826 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9827 | /* NOTREACHED */ |
| 9828 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9829 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9830 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9831 | { |
| 9832 | char *p; |
| 9833 | int val; |
| 9834 | int c; |
| 9835 | |
| 9836 | if (cmdline) |
| 9837 | minusc = NULL; |
| 9838 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9839 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9840 | if (c != '-' && c != '+') |
| 9841 | break; |
| 9842 | argptr++; |
| 9843 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9844 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9845 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 9846 | if (p[0] == '\0' || LONE_DASH(p)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9847 | if (!cmdline) { |
| 9848 | /* "-" means turn off -x and -v */ |
| 9849 | if (p[0] == '\0') |
| 9850 | xflag = vflag = 0; |
| 9851 | /* "--" means reset params */ |
| 9852 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9853 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9854 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9855 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9856 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9857 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9858 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9859 | while ((c = *p++) != '\0') { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9860 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9861 | if (c == 'c' && cmdline) { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9862 | minusc = p; /* command is after shell args */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9863 | } else if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9864 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9865 | /* it already printed err message */ |
| 9866 | return 1; /* error */ |
| 9867 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9868 | if (*argptr) |
| 9869 | argptr++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9870 | } else if (cmdline && (c == 'l')) { /* -l or +l == --login */ |
| 9871 | isloginsh = 1; |
| 9872 | /* bash does not accept +-login, we also won't */ |
| 9873 | } else if (cmdline && val && (c == '-')) { /* long options */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9874 | if (strcmp(p, "login") == 0) |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 9875 | isloginsh = 1; |
| 9876 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9877 | } else { |
| 9878 | setoption(c, val); |
| 9879 | } |
| 9880 | } |
| 9881 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9882 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9883 | } |
| 9884 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9885 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9886 | * The shift builtin command. |
| 9887 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9888 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9889 | shiftcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9890 | { |
| 9891 | int n; |
| 9892 | char **ap1, **ap2; |
| 9893 | |
| 9894 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9895 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9896 | n = number(argv[1]); |
| 9897 | if (n > shellparam.nparam) |
Denis Vlasenko | c90e1be | 2008-07-30 15:35:05 +0000 | [diff] [blame] | 9898 | n = 0; /* bash compat, was = shellparam.nparam; */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9899 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9900 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9901 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9902 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9903 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9904 | } |
| 9905 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9906 | while ((*ap2++ = *ap1++) != NULL) |
| 9907 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9908 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9909 | shellparam.optind = 1; |
| 9910 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9911 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9912 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9913 | return 0; |
| 9914 | } |
| 9915 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9916 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9917 | * POSIX requires that 'set' (but not export or readonly) output the |
| 9918 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 9919 | * Maybe we could keep them in an ordered balanced binary tree |
| 9920 | * instead of hashed lists. |
| 9921 | * For now just roll 'em through qsort for printing... |
| 9922 | */ |
| 9923 | static int |
| 9924 | showvars(const char *sep_prefix, int on, int off) |
| 9925 | { |
| 9926 | const char *sep; |
| 9927 | char **ep, **epend; |
| 9928 | |
| 9929 | ep = listvars(on, off, &epend); |
| 9930 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 9931 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9932 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9933 | |
| 9934 | for (; ep < epend; ep++) { |
| 9935 | const char *p; |
| 9936 | const char *q; |
| 9937 | |
| 9938 | p = strchrnul(*ep, '='); |
| 9939 | q = nullstr; |
| 9940 | if (*p) |
| 9941 | q = single_quote(++p); |
| 9942 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 9943 | } |
| 9944 | return 0; |
| 9945 | } |
| 9946 | |
| 9947 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9948 | * The set command builtin. |
| 9949 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9950 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9951 | setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9952 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9953 | int retval; |
| 9954 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9955 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9956 | return showvars(nullstr, 0, VUNSET); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9957 | INT_OFF; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9958 | retval = 1; |
| 9959 | if (!options(0)) { /* if no parse error... */ |
| 9960 | retval = 0; |
| 9961 | optschanged(); |
| 9962 | if (*argptr != NULL) { |
| 9963 | setparam(argptr); |
| 9964 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9965 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9966 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9967 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9968 | } |
| 9969 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9970 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9971 | static void FAST_FUNC |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 9972 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 9973 | { |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 9974 | /* Galois LFSR parameter */ |
| 9975 | /* Taps at 32 31 29 1: */ |
| 9976 | enum { MASK = 0x8000000b }; |
| 9977 | /* Another example - taps at 32 31 30 10: */ |
| 9978 | /* MASK = 0x00400007 */ |
| 9979 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9980 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 9981 | /* "get", generate */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 9982 | uint32_t t; |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 9983 | |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 9984 | /* LCG has period of 2^32 and alternating lowest bit */ |
| 9985 | random_LCG = 1664525 * random_LCG + 1013904223; |
| 9986 | /* Galois LFSR has period of 2^32-1 = 3 * 5 * 17 * 257 * 65537 */ |
| 9987 | t = (random_galois_LFSR << 1); |
| 9988 | if (random_galois_LFSR < 0) /* if we just shifted 1 out of msb... */ |
| 9989 | t ^= MASK; |
| 9990 | random_galois_LFSR = t; |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 9991 | /* Both are weak, combining them gives better randomness |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 9992 | * and ~2^64 period. & 0x7fff is probably bash compat |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 9993 | * for $RANDOM range. Combining with subtraction is |
| 9994 | * just for fun. + and ^ would work equally well. */ |
| 9995 | t = (t - random_LCG) & 0x7fff; |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 9996 | /* set without recursion */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 9997 | setvar(vrandom.text, utoa(t), VNOFUNC); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 9998 | vrandom.flags &= ~VNOFUNC; |
| 9999 | } else { |
| 10000 | /* set/reset */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 10001 | random_galois_LFSR = random_LCG = strtoul(value, (char **)NULL, 10); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10002 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10003 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10004 | #endif |
| 10005 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10006 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10007 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10008 | getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10009 | { |
| 10010 | char *p, *q; |
| 10011 | char c = '?'; |
| 10012 | int done = 0; |
| 10013 | int err = 0; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10014 | char s[12]; |
| 10015 | char **optnext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10016 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10017 | if (*param_optind < 1) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10018 | return 1; |
| 10019 | optnext = optfirst + *param_optind - 1; |
| 10020 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10021 | if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10022 | p = NULL; |
| 10023 | else |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10024 | p = optnext[-1] + *optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10025 | if (p == NULL || *p == '\0') { |
| 10026 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10027 | p = *optnext; |
| 10028 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10029 | atend: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10030 | p = NULL; |
| 10031 | done = 1; |
| 10032 | goto out; |
| 10033 | } |
| 10034 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10035 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10036 | goto atend; |
| 10037 | } |
| 10038 | |
| 10039 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 10040 | for (q = optstr; *q != c;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10041 | if (*q == '\0') { |
| 10042 | if (optstr[0] == ':') { |
| 10043 | s[0] = c; |
| 10044 | s[1] = '\0'; |
| 10045 | err |= setvarsafe("OPTARG", s, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10046 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10047 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10048 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10049 | } |
| 10050 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10051 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10052 | } |
| 10053 | if (*++q == ':') |
| 10054 | q++; |
| 10055 | } |
| 10056 | |
| 10057 | if (*++q == ':') { |
| 10058 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 10059 | if (optstr[0] == ':') { |
| 10060 | s[0] = c; |
| 10061 | s[1] = '\0'; |
| 10062 | err |= setvarsafe("OPTARG", s, 0); |
| 10063 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10064 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10065 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10066 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10067 | c = '?'; |
| 10068 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10069 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10070 | } |
| 10071 | |
| 10072 | if (p == *optnext) |
| 10073 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10074 | err |= setvarsafe("OPTARG", p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10075 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10076 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10077 | err |= setvarsafe("OPTARG", nullstr, 0); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10078 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10079 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10080 | *param_optind = optnext - optfirst + 1; |
| 10081 | fmtstr(s, sizeof(s), "%d", *param_optind); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10082 | err |= setvarsafe("OPTIND", s, VNOFUNC); |
| 10083 | s[0] = c; |
| 10084 | s[1] = '\0'; |
| 10085 | err |= setvarsafe(optvar, s, 0); |
| 10086 | if (err) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10087 | *param_optind = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10088 | *optoff = -1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10089 | flush_stdout_stderr(); |
| 10090 | raise_exception(EXERROR); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10091 | } |
| 10092 | return done; |
| 10093 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10094 | |
| 10095 | /* |
| 10096 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 10097 | * to be processed. Shellparam.optptr points to the next character to |
| 10098 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 10099 | * then it's the first time getopts has been called. |
| 10100 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10101 | static int FAST_FUNC |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10102 | getoptscmd(int argc, char **argv) |
| 10103 | { |
| 10104 | char **optbase; |
| 10105 | |
| 10106 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 10107 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10108 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10109 | optbase = shellparam.p; |
| 10110 | if (shellparam.optind > shellparam.nparam + 1) { |
| 10111 | shellparam.optind = 1; |
| 10112 | shellparam.optoff = -1; |
| 10113 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10114 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10115 | optbase = &argv[3]; |
| 10116 | if (shellparam.optind > argc - 2) { |
| 10117 | shellparam.optind = 1; |
| 10118 | shellparam.optoff = -1; |
| 10119 | } |
| 10120 | } |
| 10121 | |
| 10122 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10123 | &shellparam.optoff); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10124 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10125 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10126 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10127 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10128 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10129 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10130 | struct heredoc { |
| 10131 | struct heredoc *next; /* next here document in list */ |
| 10132 | union node *here; /* redirection node */ |
| 10133 | char *eofmark; /* string indicating end of input */ |
| 10134 | smallint striptabs; /* if set, strip leading tabs */ |
| 10135 | }; |
| 10136 | |
| 10137 | static smallint tokpushback; /* last token pushed back */ |
| 10138 | static smallint parsebackquote; /* nonzero if we are inside backquotes */ |
| 10139 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
| 10140 | static token_id_t lasttoken; /* last token read (integer id Txxx) */ |
| 10141 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 10142 | static char *wordtext; /* text of last word returned by readtoken */ |
| 10143 | static struct nodelist *backquotelist; |
| 10144 | static union node *redirnode; |
| 10145 | static struct heredoc *heredoc; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10146 | /* |
| 10147 | * NEOF is returned by parsecmd when it encounters an end of file. It |
| 10148 | * must be distinct from NULL, so we use the address of a variable that |
| 10149 | * happens to be handy. |
| 10150 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10151 | #define NEOF ((union node *)&tokpushback) |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10152 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10153 | /* |
| 10154 | * Called when an unexpected token is read during the parse. The argument |
| 10155 | * is the token that is expected, or -1 if more than one type of token can |
| 10156 | * occur at this point. |
| 10157 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10158 | static void raise_error_unexpected_syntax(int) NORETURN; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10159 | static void |
| 10160 | raise_error_unexpected_syntax(int token) |
| 10161 | { |
| 10162 | char msg[64]; |
| 10163 | int l; |
| 10164 | |
Denis Vlasenko | 7b2294e | 2008-11-28 03:50:46 +0000 | [diff] [blame] | 10165 | l = sprintf(msg, "unexpected %s", tokname(lasttoken)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10166 | if (token >= 0) |
| 10167 | sprintf(msg + l, " (expecting %s)", tokname(token)); |
| 10168 | raise_error_syntax(msg); |
| 10169 | /* NOTREACHED */ |
| 10170 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10171 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10172 | #define EOFMARKLEN 79 |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10173 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10174 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 10175 | static union node *andor(void); |
| 10176 | static union node *pipeline(void); |
| 10177 | static union node *parse_command(void); |
| 10178 | static void parseheredoc(void); |
| 10179 | static char peektoken(void); |
| 10180 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10181 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10182 | static union node * |
| 10183 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10184 | { |
| 10185 | union node *n1, *n2, *n3; |
| 10186 | int tok; |
| 10187 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10188 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10189 | if (nlflag == 2 && peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10190 | return NULL; |
| 10191 | n1 = NULL; |
| 10192 | for (;;) { |
| 10193 | n2 = andor(); |
| 10194 | tok = readtoken(); |
| 10195 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10196 | if (n2->type == NPIPE) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10197 | n2->npipe.pipe_backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10198 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10199 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10200 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10201 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10202 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10203 | n2 = n3; |
| 10204 | } |
| 10205 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10206 | } |
| 10207 | } |
| 10208 | if (n1 == NULL) { |
| 10209 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10210 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10211 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10212 | n3->type = NSEMI; |
| 10213 | n3->nbinary.ch1 = n1; |
| 10214 | n3->nbinary.ch2 = n2; |
| 10215 | n1 = n3; |
| 10216 | } |
| 10217 | switch (tok) { |
| 10218 | case TBACKGND: |
| 10219 | case TSEMI: |
| 10220 | tok = readtoken(); |
| 10221 | /* fall through */ |
| 10222 | case TNL: |
| 10223 | if (tok == TNL) { |
| 10224 | parseheredoc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10225 | if (nlflag == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10226 | return n1; |
| 10227 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10228 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10229 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10230 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10231 | if (peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10232 | return n1; |
| 10233 | break; |
| 10234 | case TEOF: |
| 10235 | if (heredoclist) |
| 10236 | parseheredoc(); |
| 10237 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10238 | pungetc(); /* push back EOF on input */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10239 | return n1; |
| 10240 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10241 | if (nlflag == 1) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10242 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10243 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10244 | return n1; |
| 10245 | } |
| 10246 | } |
| 10247 | } |
| 10248 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10249 | static union node * |
| 10250 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10251 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10252 | union node *n1, *n2, *n3; |
| 10253 | int t; |
| 10254 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10255 | n1 = pipeline(); |
| 10256 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10257 | t = readtoken(); |
| 10258 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10259 | t = NAND; |
| 10260 | } else if (t == TOR) { |
| 10261 | t = NOR; |
| 10262 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10263 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10264 | return n1; |
| 10265 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10266 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10267 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10268 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10269 | n3->type = t; |
| 10270 | n3->nbinary.ch1 = n1; |
| 10271 | n3->nbinary.ch2 = n2; |
| 10272 | n1 = n3; |
| 10273 | } |
| 10274 | } |
| 10275 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10276 | static union node * |
| 10277 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10278 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10279 | union node *n1, *n2, *pipenode; |
| 10280 | struct nodelist *lp, *prev; |
| 10281 | int negate; |
| 10282 | |
| 10283 | negate = 0; |
| 10284 | TRACE(("pipeline: entered\n")); |
| 10285 | if (readtoken() == TNOT) { |
| 10286 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10287 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10288 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10289 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10290 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10291 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10292 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10293 | pipenode->type = NPIPE; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10294 | /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10295 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10296 | pipenode->npipe.cmdlist = lp; |
| 10297 | lp->n = n1; |
| 10298 | do { |
| 10299 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10300 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10301 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10302 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10303 | prev->next = lp; |
| 10304 | } while (readtoken() == TPIPE); |
| 10305 | lp->next = NULL; |
| 10306 | n1 = pipenode; |
| 10307 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10308 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10309 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10310 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10311 | n2->type = NNOT; |
| 10312 | n2->nnot.com = n1; |
| 10313 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10314 | } |
| 10315 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10316 | } |
| 10317 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10318 | static union node * |
| 10319 | makename(void) |
| 10320 | { |
| 10321 | union node *n; |
| 10322 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10323 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10324 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10325 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10326 | n->narg.text = wordtext; |
| 10327 | n->narg.backquote = backquotelist; |
| 10328 | return n; |
| 10329 | } |
| 10330 | |
| 10331 | static void |
| 10332 | fixredir(union node *n, const char *text, int err) |
| 10333 | { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10334 | int fd; |
| 10335 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10336 | TRACE(("Fix redir %s %d\n", text, err)); |
| 10337 | if (!err) |
| 10338 | n->ndup.vname = NULL; |
| 10339 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10340 | fd = bb_strtou(text, NULL, 10); |
| 10341 | if (!errno && fd >= 0) |
| 10342 | n->ndup.dupfd = fd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10343 | else if (LONE_DASH(text)) |
| 10344 | n->ndup.dupfd = -1; |
| 10345 | else { |
| 10346 | if (err) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10347 | raise_error_syntax("bad fd number"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10348 | n->ndup.vname = makename(); |
| 10349 | } |
| 10350 | } |
| 10351 | |
| 10352 | /* |
| 10353 | * Returns true if the text contains nothing to expand (no dollar signs |
| 10354 | * or backquotes). |
| 10355 | */ |
| 10356 | static int |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10357 | noexpand(const char *text) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10358 | { |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10359 | const char *p; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10360 | char c; |
| 10361 | |
| 10362 | p = text; |
| 10363 | while ((c = *p++) != '\0') { |
| 10364 | if (c == CTLQUOTEMARK) |
| 10365 | continue; |
| 10366 | if (c == CTLESC) |
| 10367 | p++; |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10368 | else if (SIT((signed char)c, BASESYNTAX) == CCTL) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10369 | return 0; |
| 10370 | } |
| 10371 | return 1; |
| 10372 | } |
| 10373 | |
| 10374 | static void |
| 10375 | parsefname(void) |
| 10376 | { |
| 10377 | union node *n = redirnode; |
| 10378 | |
| 10379 | if (readtoken() != TWORD) |
| 10380 | raise_error_unexpected_syntax(-1); |
| 10381 | if (n->type == NHERE) { |
| 10382 | struct heredoc *here = heredoc; |
| 10383 | struct heredoc *p; |
| 10384 | int i; |
| 10385 | |
| 10386 | if (quoteflag == 0) |
| 10387 | n->type = NXHERE; |
| 10388 | TRACE(("Here document %d\n", n->type)); |
| 10389 | if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10390 | raise_error_syntax("illegal eof marker for << redirection"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10391 | rmescapes(wordtext); |
| 10392 | here->eofmark = wordtext; |
| 10393 | here->next = NULL; |
| 10394 | if (heredoclist == NULL) |
| 10395 | heredoclist = here; |
| 10396 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10397 | for (p = heredoclist; p->next; p = p->next) |
| 10398 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10399 | p->next = here; |
| 10400 | } |
| 10401 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 10402 | fixredir(n, wordtext, 0); |
| 10403 | } else { |
| 10404 | n->nfile.fname = makename(); |
| 10405 | } |
| 10406 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10407 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10408 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10409 | simplecmd(void) |
| 10410 | { |
| 10411 | union node *args, **app; |
| 10412 | union node *n = NULL; |
| 10413 | union node *vars, **vpp; |
| 10414 | union node **rpp, *redir; |
| 10415 | int savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10416 | #if ENABLE_ASH_BASH_COMPAT |
| 10417 | smallint double_brackets_flag = 0; |
| 10418 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10419 | |
| 10420 | args = NULL; |
| 10421 | app = &args; |
| 10422 | vars = NULL; |
| 10423 | vpp = &vars; |
| 10424 | redir = NULL; |
| 10425 | rpp = &redir; |
| 10426 | |
| 10427 | savecheckkwd = CHKALIAS; |
| 10428 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10429 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10430 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10431 | t = readtoken(); |
| 10432 | switch (t) { |
| 10433 | #if ENABLE_ASH_BASH_COMPAT |
| 10434 | case TAND: /* "&&" */ |
| 10435 | case TOR: /* "||" */ |
| 10436 | if (!double_brackets_flag) { |
| 10437 | tokpushback = 1; |
| 10438 | goto out; |
| 10439 | } |
| 10440 | wordtext = (char *) (t == TAND ? "-a" : "-o"); |
| 10441 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10442 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10443 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10444 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10445 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10446 | n->narg.text = wordtext; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10447 | #if ENABLE_ASH_BASH_COMPAT |
| 10448 | if (strcmp("[[", wordtext) == 0) |
| 10449 | double_brackets_flag = 1; |
| 10450 | else if (strcmp("]]", wordtext) == 0) |
| 10451 | double_brackets_flag = 0; |
| 10452 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10453 | n->narg.backquote = backquotelist; |
| 10454 | if (savecheckkwd && isassignment(wordtext)) { |
| 10455 | *vpp = n; |
| 10456 | vpp = &n->narg.next; |
| 10457 | } else { |
| 10458 | *app = n; |
| 10459 | app = &n->narg.next; |
| 10460 | savecheckkwd = 0; |
| 10461 | } |
| 10462 | break; |
| 10463 | case TREDIR: |
| 10464 | *rpp = n = redirnode; |
| 10465 | rpp = &n->nfile.next; |
| 10466 | parsefname(); /* read name of redirection file */ |
| 10467 | break; |
| 10468 | case TLP: |
| 10469 | if (args && app == &args->narg.next |
| 10470 | && !vars && !redir |
| 10471 | ) { |
| 10472 | struct builtincmd *bcmd; |
| 10473 | const char *name; |
| 10474 | |
| 10475 | /* We have a function */ |
| 10476 | if (readtoken() != TRP) |
| 10477 | raise_error_unexpected_syntax(TRP); |
| 10478 | name = n->narg.text; |
| 10479 | if (!goodname(name) |
| 10480 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 10481 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10482 | raise_error_syntax("bad function name"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10483 | } |
| 10484 | n->type = NDEFUN; |
| 10485 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10486 | n->narg.next = parse_command(); |
| 10487 | return n; |
| 10488 | } |
| 10489 | /* fall through */ |
| 10490 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10491 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10492 | goto out; |
| 10493 | } |
| 10494 | } |
| 10495 | out: |
| 10496 | *app = NULL; |
| 10497 | *vpp = NULL; |
| 10498 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10499 | n = stzalloc(sizeof(struct ncmd)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10500 | n->type = NCMD; |
| 10501 | n->ncmd.args = args; |
| 10502 | n->ncmd.assign = vars; |
| 10503 | n->ncmd.redirect = redir; |
| 10504 | return n; |
| 10505 | } |
| 10506 | |
| 10507 | static union node * |
| 10508 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10509 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10510 | union node *n1, *n2; |
| 10511 | union node *ap, **app; |
| 10512 | union node *cp, **cpp; |
| 10513 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10514 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10515 | int t; |
| 10516 | |
| 10517 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10518 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 10519 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10520 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10521 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10522 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10523 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10524 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10525 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10526 | n1->type = NIF; |
| 10527 | n1->nif.test = list(0); |
| 10528 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10529 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10530 | n1->nif.ifpart = list(0); |
| 10531 | n2 = n1; |
| 10532 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10533 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10534 | n2 = n2->nif.elsepart; |
| 10535 | n2->type = NIF; |
| 10536 | n2->nif.test = list(0); |
| 10537 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10538 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10539 | n2->nif.ifpart = list(0); |
| 10540 | } |
| 10541 | if (lasttoken == TELSE) |
| 10542 | n2->nif.elsepart = list(0); |
| 10543 | else { |
| 10544 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10545 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10546 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10547 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10548 | break; |
| 10549 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10550 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10551 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10552 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10553 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10554 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10555 | got = readtoken(); |
| 10556 | if (got != TDO) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10557 | TRACE(("expecting DO got %s %s\n", tokname(got), |
| 10558 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10559 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10560 | } |
| 10561 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10562 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10563 | break; |
| 10564 | } |
| 10565 | case TFOR: |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10566 | if (readtoken() != TWORD || quoteflag || !goodname(wordtext)) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10567 | raise_error_syntax("bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10568 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10569 | n1->type = NFOR; |
| 10570 | n1->nfor.var = wordtext; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10571 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10572 | if (readtoken() == TIN) { |
| 10573 | app = ≈ |
| 10574 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10575 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10576 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10577 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10578 | n2->narg.text = wordtext; |
| 10579 | n2->narg.backquote = backquotelist; |
| 10580 | *app = n2; |
| 10581 | app = &n2->narg.next; |
| 10582 | } |
| 10583 | *app = NULL; |
| 10584 | n1->nfor.args = ap; |
| 10585 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10586 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10587 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10588 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10589 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10590 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10591 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10592 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10593 | n1->nfor.args = n2; |
| 10594 | /* |
| 10595 | * Newline or semicolon here is optional (but note |
| 10596 | * that the original Bourne shell only allowed NL). |
| 10597 | */ |
| 10598 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10599 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10600 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10601 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10602 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10603 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10604 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10605 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10606 | break; |
| 10607 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10608 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10609 | n1->type = NCASE; |
| 10610 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10611 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10612 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10613 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10614 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10615 | n2->narg.text = wordtext; |
| 10616 | n2->narg.backquote = backquotelist; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10617 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10618 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10619 | } while (readtoken() == TNL); |
| 10620 | if (lasttoken != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10621 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10622 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10623 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10624 | checkkwd = CHKNL | CHKKWD; |
| 10625 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10626 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10627 | if (lasttoken == TLP) |
| 10628 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10629 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10630 | cp->type = NCLIST; |
| 10631 | app = &cp->nclist.pattern; |
| 10632 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10633 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10634 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10635 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10636 | ap->narg.text = wordtext; |
| 10637 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10638 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10639 | break; |
| 10640 | app = &ap->narg.next; |
| 10641 | readtoken(); |
| 10642 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10643 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10644 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10645 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10646 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10647 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10648 | cpp = &cp->nclist.next; |
| 10649 | |
| 10650 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10651 | t = readtoken(); |
| 10652 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10653 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10654 | raise_error_unexpected_syntax(TENDCASE); |
| 10655 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10656 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10657 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10658 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10659 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10660 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10661 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10662 | n1->type = NSUBSHELL; |
| 10663 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10664 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10665 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10666 | break; |
| 10667 | case TBEGIN: |
| 10668 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10669 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10670 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10671 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10672 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10673 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10674 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10675 | } |
| 10676 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10677 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10678 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10679 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10680 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10681 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10682 | checkkwd = CHKKWD | CHKALIAS; |
| 10683 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10684 | while (readtoken() == TREDIR) { |
| 10685 | *rpp = n2 = redirnode; |
| 10686 | rpp = &n2->nfile.next; |
| 10687 | parsefname(); |
| 10688 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10689 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10690 | *rpp = NULL; |
| 10691 | if (redir) { |
| 10692 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10693 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10694 | n2->type = NREDIR; |
| 10695 | n2->nredir.n = n1; |
| 10696 | n1 = n2; |
| 10697 | } |
| 10698 | n1->nredir.redirect = redir; |
| 10699 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10700 | return n1; |
| 10701 | } |
| 10702 | |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10703 | #if ENABLE_ASH_BASH_COMPAT |
| 10704 | static int decode_dollar_squote(void) |
| 10705 | { |
| 10706 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 10707 | int c, cnt; |
| 10708 | char *p; |
| 10709 | char buf[4]; |
| 10710 | |
| 10711 | c = pgetc(); |
| 10712 | p = strchr(C_escapes, c); |
| 10713 | if (p) { |
| 10714 | buf[0] = c; |
| 10715 | p = buf; |
| 10716 | cnt = 3; |
| 10717 | if ((unsigned char)(c - '0') <= 7) { /* \ooo */ |
| 10718 | do { |
| 10719 | c = pgetc(); |
| 10720 | *++p = c; |
| 10721 | } while ((unsigned char)(c - '0') <= 7 && --cnt); |
| 10722 | pungetc(); |
| 10723 | } else if (c == 'x') { /* \xHH */ |
| 10724 | do { |
| 10725 | c = pgetc(); |
| 10726 | *++p = c; |
| 10727 | } while (isxdigit(c) && --cnt); |
| 10728 | pungetc(); |
| 10729 | if (cnt == 3) { /* \x but next char is "bad" */ |
| 10730 | c = 'x'; |
| 10731 | goto unrecognized; |
| 10732 | } |
| 10733 | } else { /* simple seq like \\ or \t */ |
| 10734 | p++; |
| 10735 | } |
| 10736 | *p = '\0'; |
| 10737 | p = buf; |
| 10738 | c = bb_process_escape_sequence((void*)&p); |
| 10739 | } else { /* unrecognized "\z": print both chars unless ' or " */ |
| 10740 | if (c != '\'' && c != '"') { |
| 10741 | unrecognized: |
| 10742 | c |= 0x100; /* "please encode \, then me" */ |
| 10743 | } |
| 10744 | } |
| 10745 | return c; |
| 10746 | } |
| 10747 | #endif |
| 10748 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10749 | /* |
| 10750 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 10751 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 10752 | * word which marks the end of the document and striptabs is true if |
| 10753 | * leading tabs should be stripped from the document. The argument firstc |
| 10754 | * is the first character of the input token or document. |
| 10755 | * |
| 10756 | * Because C does not have internal subroutines, I have simulated them |
| 10757 | * using goto's to implement the subroutine linkage. The following macros |
| 10758 | * will run code that appears at the end of readtoken1. |
| 10759 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10760 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 10761 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 10762 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 10763 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 10764 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 10765 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10766 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10767 | readtoken1(int firstc, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10768 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10769 | /* NB: syntax parameter fits into smallint */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10770 | int c = firstc; |
| 10771 | char *out; |
| 10772 | int len; |
| 10773 | char line[EOFMARKLEN + 1]; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10774 | struct nodelist *bqlist; |
| 10775 | smallint quotef; |
| 10776 | smallint dblquote; |
| 10777 | smallint oldstyle; |
| 10778 | smallint prevsyntax; /* syntax before arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10779 | #if ENABLE_ASH_EXPAND_PRMT |
| 10780 | smallint pssyntax; /* we are expanding a prompt string */ |
| 10781 | #endif |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10782 | int varnest; /* levels of variables expansion */ |
| 10783 | int arinest; /* levels of arithmetic expansion */ |
| 10784 | int parenlevel; /* levels of parens in arithmetic */ |
| 10785 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 10786 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 10787 | IF_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10788 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10789 | #if __GNUC__ |
| 10790 | /* Avoid longjmp clobbering */ |
| 10791 | (void) &out; |
| 10792 | (void) "ef; |
| 10793 | (void) &dblquote; |
| 10794 | (void) &varnest; |
| 10795 | (void) &arinest; |
| 10796 | (void) &parenlevel; |
| 10797 | (void) &dqvarnest; |
| 10798 | (void) &oldstyle; |
| 10799 | (void) &prevsyntax; |
| 10800 | (void) &syntax; |
| 10801 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10802 | startlinno = g_parsefile->linno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10803 | bqlist = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10804 | quotef = 0; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10805 | oldstyle = 0; |
| 10806 | prevsyntax = 0; |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10807 | #if ENABLE_ASH_EXPAND_PRMT |
| 10808 | pssyntax = (syntax == PSSYNTAX); |
| 10809 | if (pssyntax) |
| 10810 | syntax = DQSYNTAX; |
| 10811 | #endif |
| 10812 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10813 | varnest = 0; |
| 10814 | arinest = 0; |
| 10815 | parenlevel = 0; |
| 10816 | dqvarnest = 0; |
| 10817 | |
| 10818 | STARTSTACKSTR(out); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 10819 | loop: |
| 10820 | /* For each line, until end of word */ |
| 10821 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10822 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 10823 | for (;;) { /* until end of line or end of word */ |
| 10824 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 10825 | switch (SIT(c, syntax)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10826 | case CNL: /* '\n' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10827 | if (syntax == BASESYNTAX) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10828 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10829 | USTPUTC(c, out); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10830 | g_parsefile->linno++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10831 | if (doprompt) |
| 10832 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10833 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10834 | goto loop; /* continue outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10835 | case CWORD: |
| 10836 | USTPUTC(c, out); |
| 10837 | break; |
| 10838 | case CCTL: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10839 | if (eofmark == NULL || dblquote) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10840 | USTPUTC(CTLESC, out); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10841 | #if ENABLE_ASH_BASH_COMPAT |
| 10842 | if (c == '\\' && bash_dollar_squote) { |
| 10843 | c = decode_dollar_squote(); |
| 10844 | if (c & 0x100) { |
| 10845 | USTPUTC('\\', out); |
| 10846 | c = (unsigned char)c; |
| 10847 | } |
| 10848 | } |
| 10849 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10850 | USTPUTC(c, out); |
| 10851 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10852 | case CBACK: /* backslash */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10853 | c = pgetc2(); |
| 10854 | if (c == PEOF) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10855 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10856 | USTPUTC('\\', out); |
| 10857 | pungetc(); |
| 10858 | } else if (c == '\n') { |
| 10859 | if (doprompt) |
| 10860 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10861 | } else { |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10862 | #if ENABLE_ASH_EXPAND_PRMT |
| 10863 | if (c == '$' && pssyntax) { |
| 10864 | USTPUTC(CTLESC, out); |
| 10865 | USTPUTC('\\', out); |
| 10866 | } |
| 10867 | #endif |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10868 | if (dblquote && c != '\\' |
| 10869 | && c != '`' && c != '$' |
| 10870 | && (c != '"' || eofmark != NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10871 | ) { |
| 10872 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10873 | USTPUTC('\\', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10874 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10875 | if (SIT(c, SQSYNTAX) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10876 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10877 | USTPUTC(c, out); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10878 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10879 | } |
| 10880 | break; |
| 10881 | case CSQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10882 | syntax = SQSYNTAX; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10883 | quotemark: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10884 | if (eofmark == NULL) { |
| 10885 | USTPUTC(CTLQUOTEMARK, out); |
| 10886 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10887 | break; |
| 10888 | case CDQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10889 | syntax = DQSYNTAX; |
| 10890 | dblquote = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10891 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10892 | case CENDQUOTE: |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 10893 | IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10894 | if (eofmark != NULL && arinest == 0 |
| 10895 | && varnest == 0 |
| 10896 | ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10897 | USTPUTC(c, out); |
| 10898 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10899 | if (dqvarnest == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10900 | syntax = BASESYNTAX; |
| 10901 | dblquote = 0; |
| 10902 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10903 | quotef = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10904 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10905 | } |
| 10906 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10907 | case CVAR: /* '$' */ |
| 10908 | PARSESUB(); /* parse substitution */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10909 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10910 | case CENDVAR: /* '}' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10911 | if (varnest > 0) { |
| 10912 | varnest--; |
| 10913 | if (dqvarnest > 0) { |
| 10914 | dqvarnest--; |
| 10915 | } |
| 10916 | USTPUTC(CTLENDVAR, out); |
| 10917 | } else { |
| 10918 | USTPUTC(c, out); |
| 10919 | } |
| 10920 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 10921 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10922 | case CLP: /* '(' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10923 | parenlevel++; |
| 10924 | USTPUTC(c, out); |
| 10925 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10926 | case CRP: /* ')' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10927 | if (parenlevel > 0) { |
| 10928 | USTPUTC(c, out); |
| 10929 | --parenlevel; |
| 10930 | } else { |
| 10931 | if (pgetc() == ')') { |
| 10932 | if (--arinest == 0) { |
| 10933 | USTPUTC(CTLENDARI, out); |
| 10934 | syntax = prevsyntax; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10935 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10936 | } else |
| 10937 | USTPUTC(')', out); |
| 10938 | } else { |
| 10939 | /* |
| 10940 | * unbalanced parens |
| 10941 | * (don't 2nd guess - no error) |
| 10942 | */ |
| 10943 | pungetc(); |
| 10944 | USTPUTC(')', out); |
| 10945 | } |
| 10946 | } |
| 10947 | break; |
| 10948 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10949 | case CBQUOTE: /* '`' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10950 | PARSEBACKQOLD(); |
| 10951 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10952 | case CENDFILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10953 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10954 | case CIGN: |
| 10955 | break; |
| 10956 | default: |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 10957 | if (varnest == 0) { |
| 10958 | #if ENABLE_ASH_BASH_COMPAT |
| 10959 | if (c == '&') { |
| 10960 | if (pgetc() == '>') |
| 10961 | c = 0x100 + '>'; /* flag &> */ |
| 10962 | pungetc(); |
| 10963 | } |
| 10964 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10965 | goto endword; /* exit outer loop */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 10966 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10967 | #if ENABLE_ASH_ALIAS |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 10968 | if (c != PEOA) |
| 10969 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10970 | USTPUTC(c, out); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 10971 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10972 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 10973 | c = pgetc_fast(); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10974 | } /* for (;;) */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10975 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10976 | endword: |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 10977 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10978 | if (syntax == ARISYNTAX) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10979 | raise_error_syntax("missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10980 | #endif |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10981 | if (syntax != BASESYNTAX && !parsebackquote && eofmark == NULL) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10982 | raise_error_syntax("unterminated quoted string"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10983 | if (varnest != 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10984 | startlinno = g_parsefile->linno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10985 | /* { */ |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10986 | raise_error_syntax("missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10987 | } |
| 10988 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10989 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10990 | out = stackblock(); |
| 10991 | if (eofmark == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 10992 | if ((c == '>' || c == '<' IF_ASH_BASH_COMPAT( || c == 0x100 + '>')) |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 10993 | && quotef == 0 |
| 10994 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10995 | if (isdigit_str9(out)) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10996 | PARSEREDIR(); /* passed as params: out, c */ |
| 10997 | lasttoken = TREDIR; |
| 10998 | return lasttoken; |
| 10999 | } |
| 11000 | /* else: non-number X seen, interpret it |
| 11001 | * as "NNNX>file" = "NNNX >file" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11002 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11003 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11004 | } |
| 11005 | quoteflag = quotef; |
| 11006 | backquotelist = bqlist; |
| 11007 | grabstackblock(len); |
| 11008 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11009 | lasttoken = TWORD; |
| 11010 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11011 | /* end of readtoken routine */ |
| 11012 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11013 | /* |
| 11014 | * Check to see whether we are at the end of the here document. When this |
| 11015 | * is called, c is set to the first character of the next input line. If |
| 11016 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 11017 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11018 | checkend: { |
| 11019 | if (eofmark) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11020 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11021 | if (c == PEOA) { |
| 11022 | c = pgetc2(); |
| 11023 | } |
| 11024 | #endif |
| 11025 | if (striptabs) { |
| 11026 | while (c == '\t') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11027 | c = pgetc2(); |
| 11028 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11029 | } |
| 11030 | if (c == *eofmark) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11031 | if (pfgets(line, sizeof(line)) != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11032 | char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11033 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11034 | p = line; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11035 | for (q = eofmark + 1; *q && *p == *q; p++, q++) |
| 11036 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11037 | if (*p == '\n' && *q == '\0') { |
| 11038 | c = PEOF; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11039 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11040 | needprompt = doprompt; |
| 11041 | } else { |
| 11042 | pushstring(line, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11043 | } |
| 11044 | } |
| 11045 | } |
| 11046 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11047 | goto checkend_return; |
| 11048 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11049 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11050 | /* |
| 11051 | * Parse a redirection operator. The variable "out" points to a string |
| 11052 | * specifying the fd to be redirected. The variable "c" contains the |
| 11053 | * first character of the redirection operator. |
| 11054 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11055 | parseredir: { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11056 | /* out is already checked to be a valid number or "" */ |
| 11057 | int fd = (*out == '\0' ? -1 : atoi(out)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11058 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11059 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11060 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11061 | if (c == '>') { |
| 11062 | np->nfile.fd = 1; |
| 11063 | c = pgetc(); |
| 11064 | if (c == '>') |
| 11065 | np->type = NAPPEND; |
| 11066 | else if (c == '|') |
| 11067 | np->type = NCLOBBER; |
| 11068 | else if (c == '&') |
| 11069 | np->type = NTOFD; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11070 | /* it also can be NTO2 (>&file), but we can't figure it out yet */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11071 | else { |
| 11072 | np->type = NTO; |
| 11073 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11074 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11075 | } |
| 11076 | #if ENABLE_ASH_BASH_COMPAT |
| 11077 | else if (c == 0x100 + '>') { /* this flags &> redirection */ |
| 11078 | np->nfile.fd = 1; |
| 11079 | pgetc(); /* this is '>', no need to check */ |
| 11080 | np->type = NTO2; |
| 11081 | } |
| 11082 | #endif |
| 11083 | else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11084 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11085 | c = pgetc(); |
| 11086 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11087 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11088 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11089 | np = stzalloc(sizeof(struct nhere)); |
| 11090 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11091 | } |
| 11092 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11093 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11094 | heredoc->here = np; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11095 | c = pgetc(); |
| 11096 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11097 | heredoc->striptabs = 1; |
| 11098 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11099 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11100 | pungetc(); |
| 11101 | } |
| 11102 | break; |
| 11103 | |
| 11104 | case '&': |
| 11105 | np->type = NFROMFD; |
| 11106 | break; |
| 11107 | |
| 11108 | case '>': |
| 11109 | np->type = NFROMTO; |
| 11110 | break; |
| 11111 | |
| 11112 | default: |
| 11113 | np->type = NFROM; |
| 11114 | pungetc(); |
| 11115 | break; |
| 11116 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11117 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11118 | if (fd >= 0) |
| 11119 | np->nfile.fd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11120 | redirnode = np; |
| 11121 | goto parseredir_return; |
| 11122 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11123 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11124 | /* |
| 11125 | * Parse a substitution. At this point, we have read the dollar sign |
| 11126 | * and nothing else. |
| 11127 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11128 | |
| 11129 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 11130 | * (assuming ascii char codes, as the original implementation did) */ |
| 11131 | #define is_special(c) \ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11132 | (((unsigned)(c) - 33 < 32) \ |
| 11133 | && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11134 | parsesub: { |
| 11135 | int subtype; |
| 11136 | int typeloc; |
| 11137 | int flags; |
| 11138 | char *p; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11139 | static const char types[] ALIGN1 = "}-+?="; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11140 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11141 | c = pgetc(); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11142 | if (c <= PEOA_OR_PEOF |
| 11143 | || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11144 | ) { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11145 | #if ENABLE_ASH_BASH_COMPAT |
| 11146 | if (c == '\'') |
| 11147 | bash_dollar_squote = 1; |
| 11148 | else |
| 11149 | #endif |
| 11150 | USTPUTC('$', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11151 | pungetc(); |
| 11152 | } else if (c == '(') { /* $(command) or $((arith)) */ |
| 11153 | if (pgetc() == '(') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11154 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11155 | PARSEARITH(); |
| 11156 | #else |
Mike Frysinger | 98a6f56 | 2008-06-09 09:38:45 +0000 | [diff] [blame] | 11157 | raise_error_syntax("you disabled math support for $((arith)) syntax"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11158 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11159 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11160 | pungetc(); |
| 11161 | PARSEBACKQNEW(); |
| 11162 | } |
| 11163 | } else { |
| 11164 | USTPUTC(CTLVAR, out); |
| 11165 | typeloc = out - (char *)stackblock(); |
| 11166 | USTPUTC(VSNORMAL, out); |
| 11167 | subtype = VSNORMAL; |
| 11168 | if (c == '{') { |
| 11169 | c = pgetc(); |
| 11170 | if (c == '#') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11171 | c = pgetc(); |
| 11172 | if (c == '}') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11173 | c = '#'; |
| 11174 | else |
| 11175 | subtype = VSLENGTH; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11176 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11177 | subtype = 0; |
| 11178 | } |
| 11179 | if (c > PEOA_OR_PEOF && is_name(c)) { |
| 11180 | do { |
| 11181 | STPUTC(c, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11182 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11183 | } while (c > PEOA_OR_PEOF && is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11184 | } else if (isdigit(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11185 | do { |
| 11186 | STPUTC(c, out); |
| 11187 | c = pgetc(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11188 | } while (isdigit(c)); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11189 | } else if (is_special(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11190 | USTPUTC(c, out); |
| 11191 | c = pgetc(); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11192 | } else { |
| 11193 | badsub: |
| 11194 | raise_error_syntax("bad substitution"); |
| 11195 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11196 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11197 | STPUTC('=', out); |
| 11198 | flags = 0; |
| 11199 | if (subtype == 0) { |
| 11200 | switch (c) { |
| 11201 | case ':': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11202 | c = pgetc(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11203 | #if ENABLE_ASH_BASH_COMPAT |
| 11204 | if (c == ':' || c == '$' || isdigit(c)) { |
| 11205 | pungetc(); |
| 11206 | subtype = VSSUBSTR; |
| 11207 | break; |
| 11208 | } |
| 11209 | #endif |
| 11210 | flags = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11211 | /*FALLTHROUGH*/ |
| 11212 | default: |
| 11213 | p = strchr(types, c); |
| 11214 | if (p == NULL) |
| 11215 | goto badsub; |
| 11216 | subtype = p - types + VSNORMAL; |
| 11217 | break; |
| 11218 | case '%': |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11219 | case '#': { |
| 11220 | int cc = c; |
| 11221 | subtype = c == '#' ? VSTRIMLEFT : VSTRIMRIGHT; |
| 11222 | c = pgetc(); |
| 11223 | if (c == cc) |
| 11224 | subtype++; |
| 11225 | else |
| 11226 | pungetc(); |
| 11227 | break; |
| 11228 | } |
| 11229 | #if ENABLE_ASH_BASH_COMPAT |
| 11230 | case '/': |
| 11231 | subtype = VSREPLACE; |
| 11232 | c = pgetc(); |
| 11233 | if (c == '/') |
| 11234 | subtype++; /* VSREPLACEALL */ |
| 11235 | else |
| 11236 | pungetc(); |
| 11237 | break; |
| 11238 | #endif |
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 | } else { |
| 11241 | pungetc(); |
| 11242 | } |
| 11243 | if (dblquote || arinest) |
| 11244 | flags |= VSQUOTE; |
| 11245 | *((char *)stackblock() + typeloc) = subtype | flags; |
| 11246 | if (subtype != VSNORMAL) { |
| 11247 | varnest++; |
| 11248 | if (dblquote || arinest) { |
| 11249 | dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11250 | } |
| 11251 | } |
| 11252 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11253 | goto parsesub_return; |
| 11254 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11255 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11256 | /* |
| 11257 | * Called to parse command substitutions. Newstyle is set if the command |
| 11258 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 11259 | * list of commands (passed by reference), and savelen is the number of |
| 11260 | * characters on the top of the stack which must be preserved. |
| 11261 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11262 | parsebackq: { |
| 11263 | struct nodelist **nlpp; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11264 | smallint savepbq; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11265 | union node *n; |
| 11266 | char *volatile str; |
| 11267 | struct jmploc jmploc; |
| 11268 | struct jmploc *volatile savehandler; |
| 11269 | size_t savelen; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11270 | smallint saveprompt = 0; |
| 11271 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11272 | #ifdef __GNUC__ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11273 | (void) &saveprompt; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11274 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11275 | savepbq = parsebackquote; |
| 11276 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 11277 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11278 | parsebackquote = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11279 | exception_handler = savehandler; |
| 11280 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11281 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11282 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11283 | str = NULL; |
| 11284 | savelen = out - (char *)stackblock(); |
| 11285 | if (savelen > 0) { |
| 11286 | str = ckmalloc(savelen); |
| 11287 | memcpy(str, stackblock(), savelen); |
| 11288 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11289 | savehandler = exception_handler; |
| 11290 | exception_handler = &jmploc; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11291 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11292 | if (oldstyle) { |
| 11293 | /* We must read until the closing backquote, giving special |
| 11294 | treatment to some slashes, and then push the string and |
| 11295 | reread it as input, interpreting it normally. */ |
| 11296 | char *pout; |
| 11297 | int pc; |
| 11298 | size_t psavelen; |
| 11299 | char *pstr; |
| 11300 | |
| 11301 | |
| 11302 | STARTSTACKSTR(pout); |
| 11303 | for (;;) { |
| 11304 | if (needprompt) { |
| 11305 | setprompt(2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11306 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11307 | pc = pgetc(); |
| 11308 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11309 | case '`': |
| 11310 | goto done; |
| 11311 | |
| 11312 | case '\\': |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11313 | pc = pgetc(); |
| 11314 | if (pc == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11315 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11316 | if (doprompt) |
| 11317 | setprompt(2); |
| 11318 | /* |
| 11319 | * If eating a newline, avoid putting |
| 11320 | * the newline into the new character |
| 11321 | * stream (via the STPUTC after the |
| 11322 | * switch). |
| 11323 | */ |
| 11324 | continue; |
| 11325 | } |
| 11326 | if (pc != '\\' && pc != '`' && pc != '$' |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11327 | && (!dblquote || pc != '"')) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11328 | STPUTC('\\', pout); |
| 11329 | if (pc > PEOA_OR_PEOF) { |
| 11330 | break; |
| 11331 | } |
| 11332 | /* fall through */ |
| 11333 | |
| 11334 | case PEOF: |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11335 | #if ENABLE_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11336 | case PEOA: |
| 11337 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11338 | startlinno = g_parsefile->linno; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11339 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11340 | |
| 11341 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11342 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11343 | needprompt = doprompt; |
| 11344 | break; |
| 11345 | |
| 11346 | default: |
| 11347 | break; |
| 11348 | } |
| 11349 | STPUTC(pc, pout); |
| 11350 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11351 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11352 | STPUTC('\0', pout); |
| 11353 | psavelen = pout - (char *)stackblock(); |
| 11354 | if (psavelen > 0) { |
| 11355 | pstr = grabstackstr(pout); |
| 11356 | setinputstring(pstr); |
| 11357 | } |
| 11358 | } |
| 11359 | nlpp = &bqlist; |
| 11360 | while (*nlpp) |
| 11361 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11362 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 11363 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11364 | parsebackquote = oldstyle; |
| 11365 | |
| 11366 | if (oldstyle) { |
| 11367 | saveprompt = doprompt; |
| 11368 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11369 | } |
| 11370 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11371 | n = list(2); |
| 11372 | |
| 11373 | if (oldstyle) |
| 11374 | doprompt = saveprompt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11375 | else if (readtoken() != TRP) |
| 11376 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11377 | |
| 11378 | (*nlpp)->n = n; |
| 11379 | if (oldstyle) { |
| 11380 | /* |
| 11381 | * Start reading from old file again, ignoring any pushed back |
| 11382 | * tokens left from the backquote parsing |
| 11383 | */ |
| 11384 | popfile(); |
| 11385 | tokpushback = 0; |
| 11386 | } |
| 11387 | while (stackblocksize() <= savelen) |
| 11388 | growstackblock(); |
| 11389 | STARTSTACKSTR(out); |
| 11390 | if (str) { |
| 11391 | memcpy(out, str, savelen); |
| 11392 | STADJUST(savelen, out); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11393 | INT_OFF; |
| 11394 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11395 | str = NULL; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11396 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11397 | } |
| 11398 | parsebackquote = savepbq; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11399 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11400 | if (arinest || dblquote) |
| 11401 | USTPUTC(CTLBACKQ | CTLQUOTE, out); |
| 11402 | else |
| 11403 | USTPUTC(CTLBACKQ, out); |
| 11404 | if (oldstyle) |
| 11405 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11406 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11407 | } |
| 11408 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11409 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11410 | /* |
| 11411 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 11412 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11413 | parsearith: { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11414 | if (++arinest == 1) { |
| 11415 | prevsyntax = syntax; |
| 11416 | syntax = ARISYNTAX; |
| 11417 | USTPUTC(CTLARI, out); |
| 11418 | if (dblquote) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11419 | USTPUTC('"', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11420 | else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11421 | USTPUTC(' ', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11422 | } else { |
| 11423 | /* |
| 11424 | * we collapse embedded arithmetic expansion to |
| 11425 | * parenthesis, which should be equivalent |
| 11426 | */ |
| 11427 | USTPUTC('(', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11428 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11429 | goto parsearith_return; |
| 11430 | } |
| 11431 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11432 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11433 | } /* end of readtoken */ |
| 11434 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11435 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11436 | * Read the next input token. |
| 11437 | * If the token is a word, we set backquotelist to the list of cmds in |
| 11438 | * backquotes. We set quoteflag to true if any part of the word was |
| 11439 | * quoted. |
| 11440 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 11441 | * the redirection. |
| 11442 | * In all cases, the variable startlinno is set to the number of the line |
| 11443 | * on which the token starts. |
| 11444 | * |
| 11445 | * [Change comment: here documents and internal procedures] |
| 11446 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 11447 | * word parsing code into a separate routine. In this case, readtoken |
| 11448 | * doesn't need to have any internal procedures, but parseword does. |
| 11449 | * We could also make parseoperator in essence the main routine, and |
| 11450 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11451 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11452 | #define NEW_xxreadtoken |
| 11453 | #ifdef NEW_xxreadtoken |
| 11454 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11455 | static const char xxreadtoken_chars[7] ALIGN1 = { |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11456 | '\n', '(', ')', /* singles */ |
| 11457 | '&', '|', ';', /* doubles */ |
| 11458 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11459 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11460 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11461 | #define xxreadtoken_singles 3 |
| 11462 | #define xxreadtoken_doubles 3 |
| 11463 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11464 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11465 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 11466 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 11467 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11468 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11469 | }; |
| 11470 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11471 | static int |
| 11472 | xxreadtoken(void) |
| 11473 | { |
| 11474 | int c; |
| 11475 | |
| 11476 | if (tokpushback) { |
| 11477 | tokpushback = 0; |
| 11478 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11479 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11480 | if (needprompt) { |
| 11481 | setprompt(2); |
| 11482 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11483 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11484 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11485 | c = pgetc_fast(); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11486 | if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA)) |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11487 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11488 | |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11489 | if (c == '#') { |
| 11490 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11491 | continue; |
| 11492 | pungetc(); |
| 11493 | } else if (c == '\\') { |
| 11494 | if (pgetc() != '\n') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11495 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11496 | break; /* return readtoken1(...) */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11497 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11498 | startlinno = ++g_parsefile->linno; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11499 | if (doprompt) |
| 11500 | setprompt(2); |
| 11501 | } else { |
| 11502 | const char *p; |
| 11503 | |
| 11504 | p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 11505 | if (c != PEOF) { |
| 11506 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11507 | g_parsefile->linno++; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11508 | needprompt = doprompt; |
| 11509 | } |
| 11510 | |
| 11511 | p = strchr(xxreadtoken_chars, c); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11512 | if (p == NULL) |
| 11513 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11514 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11515 | if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
| 11516 | int cc = pgetc(); |
| 11517 | if (cc == c) { /* double occurrence? */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11518 | p += xxreadtoken_doubles + 1; |
| 11519 | } else { |
| 11520 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11521 | #if ENABLE_ASH_BASH_COMPAT |
| 11522 | if (c == '&' && cc == '>') /* &> */ |
| 11523 | break; /* return readtoken1(...) */ |
| 11524 | #endif |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11525 | } |
| 11526 | } |
| 11527 | } |
| 11528 | lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 11529 | return lasttoken; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11530 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11531 | } /* for (;;) */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11532 | |
| 11533 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11534 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11535 | #else /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11536 | #define RETURN(token) return lasttoken = token |
| 11537 | static int |
| 11538 | xxreadtoken(void) |
| 11539 | { |
| 11540 | int c; |
| 11541 | |
| 11542 | if (tokpushback) { |
| 11543 | tokpushback = 0; |
| 11544 | return lasttoken; |
| 11545 | } |
| 11546 | if (needprompt) { |
| 11547 | setprompt(2); |
| 11548 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11549 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11550 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11551 | c = pgetc_fast(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11552 | switch (c) { |
| 11553 | case ' ': case '\t': |
| 11554 | #if ENABLE_ASH_ALIAS |
| 11555 | case PEOA: |
| 11556 | #endif |
| 11557 | continue; |
| 11558 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11559 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11560 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11561 | pungetc(); |
| 11562 | continue; |
| 11563 | case '\\': |
| 11564 | if (pgetc() == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11565 | startlinno = ++g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11566 | if (doprompt) |
| 11567 | setprompt(2); |
| 11568 | continue; |
| 11569 | } |
| 11570 | pungetc(); |
| 11571 | goto breakloop; |
| 11572 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11573 | g_parsefile->linno++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11574 | needprompt = doprompt; |
| 11575 | RETURN(TNL); |
| 11576 | case PEOF: |
| 11577 | RETURN(TEOF); |
| 11578 | case '&': |
| 11579 | if (pgetc() == '&') |
| 11580 | RETURN(TAND); |
| 11581 | pungetc(); |
| 11582 | RETURN(TBACKGND); |
| 11583 | case '|': |
| 11584 | if (pgetc() == '|') |
| 11585 | RETURN(TOR); |
| 11586 | pungetc(); |
| 11587 | RETURN(TPIPE); |
| 11588 | case ';': |
| 11589 | if (pgetc() == ';') |
| 11590 | RETURN(TENDCASE); |
| 11591 | pungetc(); |
| 11592 | RETURN(TSEMI); |
| 11593 | case '(': |
| 11594 | RETURN(TLP); |
| 11595 | case ')': |
| 11596 | RETURN(TRP); |
| 11597 | default: |
| 11598 | goto breakloop; |
| 11599 | } |
| 11600 | } |
| 11601 | breakloop: |
| 11602 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 11603 | #undef RETURN |
| 11604 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11605 | #endif /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11606 | |
| 11607 | static int |
| 11608 | readtoken(void) |
| 11609 | { |
| 11610 | int t; |
| 11611 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11612 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11613 | #endif |
| 11614 | |
| 11615 | #if ENABLE_ASH_ALIAS |
| 11616 | top: |
| 11617 | #endif |
| 11618 | |
| 11619 | t = xxreadtoken(); |
| 11620 | |
| 11621 | /* |
| 11622 | * eat newlines |
| 11623 | */ |
| 11624 | if (checkkwd & CHKNL) { |
| 11625 | while (t == TNL) { |
| 11626 | parseheredoc(); |
| 11627 | t = xxreadtoken(); |
| 11628 | } |
| 11629 | } |
| 11630 | |
| 11631 | if (t != TWORD || quoteflag) { |
| 11632 | goto out; |
| 11633 | } |
| 11634 | |
| 11635 | /* |
| 11636 | * check for keywords |
| 11637 | */ |
| 11638 | if (checkkwd & CHKKWD) { |
| 11639 | const char *const *pp; |
| 11640 | |
| 11641 | pp = findkwd(wordtext); |
| 11642 | if (pp) { |
| 11643 | lasttoken = t = pp - tokname_array; |
| 11644 | TRACE(("keyword %s recognized\n", tokname(t))); |
| 11645 | goto out; |
| 11646 | } |
| 11647 | } |
| 11648 | |
| 11649 | if (checkkwd & CHKALIAS) { |
| 11650 | #if ENABLE_ASH_ALIAS |
| 11651 | struct alias *ap; |
| 11652 | ap = lookupalias(wordtext, 1); |
| 11653 | if (ap != NULL) { |
| 11654 | if (*ap->val) { |
| 11655 | pushstring(ap->val, ap); |
| 11656 | } |
| 11657 | goto top; |
| 11658 | } |
| 11659 | #endif |
| 11660 | } |
| 11661 | out: |
| 11662 | checkkwd = 0; |
| 11663 | #if DEBUG |
| 11664 | if (!alreadyseen) |
| 11665 | TRACE(("token %s %s\n", tokname(t), t == TWORD ? wordtext : "")); |
| 11666 | else |
| 11667 | TRACE(("reread token %s %s\n", tokname(t), t == TWORD ? wordtext : "")); |
| 11668 | #endif |
| 11669 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11670 | } |
| 11671 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11672 | static char |
| 11673 | peektoken(void) |
| 11674 | { |
| 11675 | int t; |
| 11676 | |
| 11677 | t = readtoken(); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11678 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11679 | return tokname_array[t][0]; |
| 11680 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11681 | |
| 11682 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11683 | * Read and parse a command. Returns NEOF on end of file. (NULL is a |
| 11684 | * valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11685 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11686 | static union node * |
| 11687 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11688 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11689 | int t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11690 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11691 | tokpushback = 0; |
| 11692 | doprompt = interact; |
| 11693 | if (doprompt) |
| 11694 | setprompt(doprompt); |
| 11695 | needprompt = 0; |
| 11696 | t = readtoken(); |
| 11697 | if (t == TEOF) |
| 11698 | return NEOF; |
| 11699 | if (t == TNL) |
| 11700 | return NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11701 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11702 | return list(1); |
| 11703 | } |
| 11704 | |
| 11705 | /* |
| 11706 | * Input any here documents. |
| 11707 | */ |
| 11708 | static void |
| 11709 | parseheredoc(void) |
| 11710 | { |
| 11711 | struct heredoc *here; |
| 11712 | union node *n; |
| 11713 | |
| 11714 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11715 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11716 | |
| 11717 | while (here) { |
| 11718 | if (needprompt) { |
| 11719 | setprompt(2); |
| 11720 | } |
| 11721 | readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, |
| 11722 | here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11723 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11724 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11725 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11726 | n->narg.text = wordtext; |
| 11727 | n->narg.backquote = backquotelist; |
| 11728 | here->here->nhere.doc = n; |
| 11729 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11730 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11731 | } |
| 11732 | |
| 11733 | |
| 11734 | /* |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 11735 | * called by editline -- any expansions to the prompt should be added here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11736 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11737 | #if ENABLE_ASH_EXPAND_PRMT |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11738 | static const char * |
| 11739 | expandstr(const char *ps) |
| 11740 | { |
| 11741 | union node n; |
| 11742 | |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 11743 | /* XXX Fix (char *) cast. It _is_ a bug. ps is variable's value, |
| 11744 | * and token processing _can_ alter it (delete NULs etc). */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11745 | setinputstring((char *)ps); |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11746 | readtoken1(pgetc(), PSSYNTAX, nullstr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11747 | popfile(); |
| 11748 | |
| 11749 | n.narg.type = NARG; |
| 11750 | n.narg.next = NULL; |
| 11751 | n.narg.text = wordtext; |
| 11752 | n.narg.backquote = backquotelist; |
| 11753 | |
| 11754 | expandarg(&n, NULL, 0); |
| 11755 | return stackblock(); |
| 11756 | } |
| 11757 | #endif |
| 11758 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11759 | /* |
| 11760 | * Execute a command or commands contained in a string. |
| 11761 | */ |
| 11762 | static int |
| 11763 | evalstring(char *s, int mask) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11764 | { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11765 | union node *n; |
| 11766 | struct stackmark smark; |
| 11767 | int skip; |
| 11768 | |
| 11769 | setinputstring(s); |
| 11770 | setstackmark(&smark); |
| 11771 | |
| 11772 | skip = 0; |
| 11773 | while ((n = parsecmd(0)) != NEOF) { |
| 11774 | evaltree(n, 0); |
| 11775 | popstackmark(&smark); |
| 11776 | skip = evalskip; |
| 11777 | if (skip) |
| 11778 | break; |
| 11779 | } |
| 11780 | popfile(); |
| 11781 | |
| 11782 | skip &= mask; |
| 11783 | evalskip = skip; |
| 11784 | return skip; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11785 | } |
| 11786 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11787 | /* |
| 11788 | * The eval command. |
| 11789 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11790 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11791 | evalcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11792 | { |
| 11793 | char *p; |
| 11794 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11795 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11796 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11797 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11798 | argv += 2; |
| 11799 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11800 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11801 | for (;;) { |
| 11802 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11803 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11804 | if (p == NULL) |
| 11805 | break; |
| 11806 | STPUTC(' ', concat); |
| 11807 | } |
| 11808 | STPUTC('\0', concat); |
| 11809 | p = grabstackstr(concat); |
| 11810 | } |
| 11811 | evalstring(p, ~SKIPEVAL); |
| 11812 | |
| 11813 | } |
| 11814 | return exitstatus; |
| 11815 | } |
| 11816 | |
| 11817 | /* |
| 11818 | * Read and execute commands. "Top" is nonzero for the top level command |
| 11819 | * loop; it turns on prompting if the shell is interactive. |
| 11820 | */ |
| 11821 | static int |
| 11822 | cmdloop(int top) |
| 11823 | { |
| 11824 | union node *n; |
| 11825 | struct stackmark smark; |
| 11826 | int inter; |
| 11827 | int numeof = 0; |
| 11828 | |
| 11829 | TRACE(("cmdloop(%d) called\n", top)); |
| 11830 | for (;;) { |
| 11831 | int skip; |
| 11832 | |
| 11833 | setstackmark(&smark); |
| 11834 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11835 | if (doing_jobctl) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11836 | showjobs(stderr, SHOW_CHANGED); |
| 11837 | #endif |
| 11838 | inter = 0; |
| 11839 | if (iflag && top) { |
| 11840 | inter++; |
| 11841 | #if ENABLE_ASH_MAIL |
| 11842 | chkmail(); |
| 11843 | #endif |
| 11844 | } |
| 11845 | n = parsecmd(inter); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame^] | 11846 | #if DEBUG > 2 |
| 11847 | if (debug && (n != NEOF)) |
| 11848 | showtree(n); |
Denis Vlasenko | 135cecb | 2009-04-12 00:00:57 +0000 | [diff] [blame] | 11849 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11850 | if (n == NEOF) { |
| 11851 | if (!top || numeof >= 50) |
| 11852 | break; |
| 11853 | if (!stoppedjobs()) { |
| 11854 | if (!Iflag) |
| 11855 | break; |
| 11856 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 11857 | } |
| 11858 | numeof++; |
| 11859 | } else if (nflag == 0) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 11860 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 11861 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11862 | numeof = 0; |
| 11863 | evaltree(n, 0); |
| 11864 | } |
| 11865 | popstackmark(&smark); |
| 11866 | skip = evalskip; |
| 11867 | |
| 11868 | if (skip) { |
| 11869 | evalskip = 0; |
| 11870 | return skip & SKIPEVAL; |
| 11871 | } |
| 11872 | } |
| 11873 | return 0; |
| 11874 | } |
| 11875 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11876 | /* |
| 11877 | * Take commands from a file. To be compatible we should do a path |
| 11878 | * search for the file, which is necessary to find sub-commands. |
| 11879 | */ |
| 11880 | static char * |
| 11881 | find_dot_file(char *name) |
| 11882 | { |
| 11883 | char *fullname; |
| 11884 | const char *path = pathval(); |
| 11885 | struct stat statb; |
| 11886 | |
| 11887 | /* don't try this for absolute or relative paths */ |
| 11888 | if (strchr(name, '/')) |
| 11889 | return name; |
| 11890 | |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 11891 | /* IIRC standards do not say whether . is to be searched. |
| 11892 | * And it is even smaller this way, making it unconditional for now: |
| 11893 | */ |
| 11894 | if (1) { /* ENABLE_ASH_BASH_COMPAT */ |
| 11895 | fullname = name; |
| 11896 | goto try_cur_dir; |
| 11897 | } |
| 11898 | |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 11899 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 11900 | try_cur_dir: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11901 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 11902 | /* |
| 11903 | * Don't bother freeing here, since it will |
| 11904 | * be freed by the caller. |
| 11905 | */ |
| 11906 | return fullname; |
| 11907 | } |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 11908 | if (fullname != name) |
| 11909 | stunalloc(fullname); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11910 | } |
| 11911 | |
| 11912 | /* not found in the PATH */ |
| 11913 | ash_msg_and_raise_error("%s: not found", name); |
| 11914 | /* NOTREACHED */ |
| 11915 | } |
| 11916 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11917 | static int FAST_FUNC |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11918 | dotcmd(int argc, char **argv) |
| 11919 | { |
| 11920 | struct strlist *sp; |
| 11921 | volatile struct shparam saveparam; |
| 11922 | int status = 0; |
| 11923 | |
| 11924 | for (sp = cmdenviron; sp; sp = sp->next) |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 11925 | setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11926 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11927 | if (argv[1]) { /* That's what SVR2 does */ |
| 11928 | char *fullname = find_dot_file(argv[1]); |
| 11929 | argv += 2; |
| 11930 | argc -= 2; |
| 11931 | if (argc) { /* argc > 0, argv[0] != NULL */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11932 | saveparam = shellparam; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 11933 | shellparam.malloced = 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11934 | shellparam.nparam = argc; |
| 11935 | shellparam.p = argv; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11936 | }; |
| 11937 | |
| 11938 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 11939 | commandname = fullname; |
| 11940 | cmdloop(0); |
| 11941 | popfile(); |
| 11942 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11943 | if (argc) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11944 | freeparam(&shellparam); |
| 11945 | shellparam = saveparam; |
| 11946 | }; |
| 11947 | status = exitstatus; |
| 11948 | } |
| 11949 | return status; |
| 11950 | } |
| 11951 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11952 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11953 | exitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11954 | { |
| 11955 | if (stoppedjobs()) |
| 11956 | return 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11957 | if (argv[1]) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11958 | exitstatus = number(argv[1]); |
| 11959 | raise_exception(EXEXIT); |
| 11960 | /* NOTREACHED */ |
| 11961 | } |
| 11962 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11963 | /* |
| 11964 | * Read a file containing shell functions. |
| 11965 | */ |
| 11966 | static void |
| 11967 | readcmdfile(char *name) |
| 11968 | { |
| 11969 | setinputfile(name, INPUT_PUSH_FILE); |
| 11970 | cmdloop(0); |
| 11971 | popfile(); |
| 11972 | } |
| 11973 | |
| 11974 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11975 | /* ============ find_command inplementation */ |
| 11976 | |
| 11977 | /* |
| 11978 | * Resolve a command name. If you change this routine, you may have to |
| 11979 | * change the shellexec routine as well. |
| 11980 | */ |
| 11981 | static void |
| 11982 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 11983 | { |
| 11984 | struct tblentry *cmdp; |
| 11985 | int idx; |
| 11986 | int prev; |
| 11987 | char *fullname; |
| 11988 | struct stat statb; |
| 11989 | int e; |
| 11990 | int updatetbl; |
| 11991 | struct builtincmd *bcmd; |
| 11992 | |
| 11993 | /* If name contains a slash, don't use PATH or hash table */ |
| 11994 | if (strchr(name, '/') != NULL) { |
| 11995 | entry->u.index = -1; |
| 11996 | if (act & DO_ABS) { |
| 11997 | while (stat(name, &statb) < 0) { |
| 11998 | #ifdef SYSV |
| 11999 | if (errno == EINTR) |
| 12000 | continue; |
| 12001 | #endif |
| 12002 | entry->cmdtype = CMDUNKNOWN; |
| 12003 | return; |
| 12004 | } |
| 12005 | } |
| 12006 | entry->cmdtype = CMDNORMAL; |
| 12007 | return; |
| 12008 | } |
| 12009 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12010 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12011 | |
| 12012 | updatetbl = (path == pathval()); |
| 12013 | if (!updatetbl) { |
| 12014 | act |= DO_ALTPATH; |
| 12015 | if (strstr(path, "%builtin") != NULL) |
| 12016 | act |= DO_ALTBLTIN; |
| 12017 | } |
| 12018 | |
| 12019 | /* If name is in the table, check answer will be ok */ |
| 12020 | cmdp = cmdlookup(name, 0); |
| 12021 | if (cmdp != NULL) { |
| 12022 | int bit; |
| 12023 | |
| 12024 | switch (cmdp->cmdtype) { |
| 12025 | default: |
| 12026 | #if DEBUG |
| 12027 | abort(); |
| 12028 | #endif |
| 12029 | case CMDNORMAL: |
| 12030 | bit = DO_ALTPATH; |
| 12031 | break; |
| 12032 | case CMDFUNCTION: |
| 12033 | bit = DO_NOFUNC; |
| 12034 | break; |
| 12035 | case CMDBUILTIN: |
| 12036 | bit = DO_ALTBLTIN; |
| 12037 | break; |
| 12038 | } |
| 12039 | if (act & bit) { |
| 12040 | updatetbl = 0; |
| 12041 | cmdp = NULL; |
| 12042 | } else if (cmdp->rehash == 0) |
| 12043 | /* if not invalidated by cd, we're done */ |
| 12044 | goto success; |
| 12045 | } |
| 12046 | |
| 12047 | /* If %builtin not in path, check for builtin next */ |
| 12048 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12049 | if (bcmd) { |
| 12050 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 12051 | goto builtin_success; |
| 12052 | if (act & DO_ALTPATH) { |
| 12053 | if (!(act & DO_ALTBLTIN)) |
| 12054 | goto builtin_success; |
| 12055 | } else if (builtinloc <= 0) { |
| 12056 | goto builtin_success; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 12057 | } |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12058 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12059 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12060 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 12061 | { |
| 12062 | int applet_no = find_applet_by_name(name); |
| 12063 | if (applet_no >= 0) { |
| 12064 | entry->cmdtype = CMDNORMAL; |
| 12065 | entry->u.index = -2 - applet_no; |
| 12066 | return; |
| 12067 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12068 | } |
| 12069 | #endif |
| 12070 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12071 | /* We have to search path. */ |
| 12072 | prev = -1; /* where to start */ |
| 12073 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 12074 | if (cmdp->cmdtype == CMDBUILTIN) |
| 12075 | prev = builtinloc; |
| 12076 | else |
| 12077 | prev = cmdp->param.index; |
| 12078 | } |
| 12079 | |
| 12080 | e = ENOENT; |
| 12081 | idx = -1; |
| 12082 | loop: |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12083 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12084 | stunalloc(fullname); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12085 | /* NB: code below will still use fullname |
| 12086 | * despite it being "unallocated" */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12087 | idx++; |
| 12088 | if (pathopt) { |
| 12089 | if (prefix(pathopt, "builtin")) { |
| 12090 | if (bcmd) |
| 12091 | goto builtin_success; |
| 12092 | continue; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 12093 | } |
| 12094 | if ((act & DO_NOFUNC) |
| 12095 | || !prefix(pathopt, "func") |
| 12096 | ) { /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12097 | continue; |
| 12098 | } |
| 12099 | } |
| 12100 | /* if rehash, don't redo absolute path names */ |
| 12101 | if (fullname[0] == '/' && idx <= prev) { |
| 12102 | if (idx < prev) |
| 12103 | continue; |
| 12104 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 12105 | goto success; |
| 12106 | } |
| 12107 | while (stat(fullname, &statb) < 0) { |
| 12108 | #ifdef SYSV |
| 12109 | if (errno == EINTR) |
| 12110 | continue; |
| 12111 | #endif |
| 12112 | if (errno != ENOENT && errno != ENOTDIR) |
| 12113 | e = errno; |
| 12114 | goto loop; |
| 12115 | } |
| 12116 | e = EACCES; /* if we fail, this will be the error */ |
| 12117 | if (!S_ISREG(statb.st_mode)) |
| 12118 | continue; |
| 12119 | if (pathopt) { /* this is a %func directory */ |
| 12120 | stalloc(strlen(fullname) + 1); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12121 | /* NB: stalloc will return space pointed by fullname |
| 12122 | * (because we don't have any intervening allocations |
| 12123 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12124 | readcmdfile(fullname); |
| 12125 | cmdp = cmdlookup(name, 0); |
| 12126 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 12127 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 12128 | stunalloc(fullname); |
| 12129 | goto success; |
| 12130 | } |
| 12131 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 12132 | if (!updatetbl) { |
| 12133 | entry->cmdtype = CMDNORMAL; |
| 12134 | entry->u.index = idx; |
| 12135 | return; |
| 12136 | } |
| 12137 | INT_OFF; |
| 12138 | cmdp = cmdlookup(name, 1); |
| 12139 | cmdp->cmdtype = CMDNORMAL; |
| 12140 | cmdp->param.index = idx; |
| 12141 | INT_ON; |
| 12142 | goto success; |
| 12143 | } |
| 12144 | |
| 12145 | /* We failed. If there was an entry for this command, delete it */ |
| 12146 | if (cmdp && updatetbl) |
| 12147 | delete_cmd_entry(); |
| 12148 | if (act & DO_ERR) |
| 12149 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
| 12150 | entry->cmdtype = CMDUNKNOWN; |
| 12151 | return; |
| 12152 | |
| 12153 | builtin_success: |
| 12154 | if (!updatetbl) { |
| 12155 | entry->cmdtype = CMDBUILTIN; |
| 12156 | entry->u.cmd = bcmd; |
| 12157 | return; |
| 12158 | } |
| 12159 | INT_OFF; |
| 12160 | cmdp = cmdlookup(name, 1); |
| 12161 | cmdp->cmdtype = CMDBUILTIN; |
| 12162 | cmdp->param.cmd = bcmd; |
| 12163 | INT_ON; |
| 12164 | success: |
| 12165 | cmdp->rehash = 0; |
| 12166 | entry->cmdtype = cmdp->cmdtype; |
| 12167 | entry->u = cmdp->param; |
| 12168 | } |
| 12169 | |
| 12170 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12171 | /* ============ trap.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12172 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12173 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12174 | * The trap builtin. |
| 12175 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12176 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12177 | trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12178 | { |
| 12179 | char *action; |
| 12180 | char **ap; |
| 12181 | int signo; |
| 12182 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12183 | nextopt(nullstr); |
| 12184 | ap = argptr; |
| 12185 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12186 | for (signo = 0; signo < NSIG; signo++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12187 | if (trap[signo] != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12188 | out1fmt("trap -- %s %s\n", |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12189 | single_quote(trap[signo]), |
| 12190 | get_signame(signo)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12191 | } |
| 12192 | } |
| 12193 | return 0; |
| 12194 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12195 | action = NULL; |
| 12196 | if (ap[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12197 | action = *ap++; |
| 12198 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12199 | signo = get_signum(*ap); |
| 12200 | if (signo < 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12201 | ash_msg_and_raise_error("%s: bad trap", *ap); |
| 12202 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12203 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 12204 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12205 | action = NULL; |
| 12206 | else |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12207 | action = ckstrdup(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12208 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 12209 | free(trap[signo]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12210 | trap[signo] = action; |
| 12211 | if (signo != 0) |
| 12212 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12213 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12214 | ap++; |
| 12215 | } |
| 12216 | return 0; |
| 12217 | } |
| 12218 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12219 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12220 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12221 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 12222 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12223 | /* |
| 12224 | * Lists available builtins |
| 12225 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12226 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12227 | helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12228 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 12229 | unsigned col; |
| 12230 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12231 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 12232 | out1fmt( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 12233 | "Built-in commands:\n" |
| 12234 | "------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 12235 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12236 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 12237 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12238 | if (col > 60) { |
| 12239 | out1fmt("\n"); |
| 12240 | col = 0; |
| 12241 | } |
| 12242 | } |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 12243 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 12244 | { |
| 12245 | const char *a = applet_names; |
| 12246 | while (*a) { |
| 12247 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 12248 | if (col > 60) { |
| 12249 | out1fmt("\n"); |
| 12250 | col = 0; |
| 12251 | } |
| 12252 | a += strlen(a) + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12253 | } |
| 12254 | } |
| 12255 | #endif |
| 12256 | out1fmt("\n\n"); |
| 12257 | return EXIT_SUCCESS; |
| 12258 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12259 | #endif /* FEATURE_SH_EXTRA_QUIET */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12260 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12261 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12262 | * The export and readonly commands. |
| 12263 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12264 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12265 | exportcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12266 | { |
| 12267 | struct var *vp; |
| 12268 | char *name; |
| 12269 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12270 | char **aptr; |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 12271 | int flag = argv[0][0] == 'r' ? VREADONLY : VEXPORT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12272 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12273 | if (nextopt("p") != 'p') { |
| 12274 | aptr = argptr; |
| 12275 | name = *aptr; |
| 12276 | if (name) { |
| 12277 | do { |
| 12278 | p = strchr(name, '='); |
| 12279 | if (p != NULL) { |
| 12280 | p++; |
| 12281 | } else { |
| 12282 | vp = *findvar(hashvar(name), name); |
| 12283 | if (vp) { |
| 12284 | vp->flags |= flag; |
| 12285 | continue; |
| 12286 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12287 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12288 | setvar(name, p, flag); |
| 12289 | } while ((name = *++aptr) != NULL); |
| 12290 | return 0; |
| 12291 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12292 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12293 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12294 | return 0; |
| 12295 | } |
| 12296 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12297 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12298 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12299 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12300 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12301 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12302 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12303 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12304 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12305 | cmdp = cmdlookup(name, 0); |
| 12306 | if (cmdp!= NULL && cmdp->cmdtype == CMDFUNCTION) |
| 12307 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12308 | } |
| 12309 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12310 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12311 | * The unset builtin command. We unset the function before we unset the |
| 12312 | * variable to allow a function to be unset when there is a readonly variable |
| 12313 | * with the same name. |
| 12314 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12315 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12316 | unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12317 | { |
| 12318 | char **ap; |
| 12319 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12320 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12321 | int ret = 0; |
| 12322 | |
| 12323 | while ((i = nextopt("vf")) != '\0') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12324 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12325 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12326 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12327 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12328 | if (flag != 'f') { |
| 12329 | i = unsetvar(*ap); |
| 12330 | ret |= i; |
| 12331 | if (!(i & 2)) |
| 12332 | continue; |
| 12333 | } |
| 12334 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12335 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12336 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12337 | return ret & 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12338 | } |
| 12339 | |
| 12340 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12341 | /* setmode.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12342 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12343 | #include <sys/times.h> |
| 12344 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12345 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12346 | ' ', offsetof(struct tms, tms_utime), |
| 12347 | '\n', offsetof(struct tms, tms_stime), |
| 12348 | ' ', offsetof(struct tms, tms_cutime), |
| 12349 | '\n', offsetof(struct tms, tms_cstime), |
| 12350 | 0 |
| 12351 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12352 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12353 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12354 | timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12355 | { |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12356 | long clk_tck, s, t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12357 | const unsigned char *p; |
| 12358 | struct tms buf; |
| 12359 | |
| 12360 | clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12361 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12362 | |
| 12363 | p = timescmd_str; |
| 12364 | do { |
| 12365 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 12366 | s = t / clk_tck; |
| 12367 | out1fmt("%ldm%ld.%.3lds%c", |
| 12368 | s/60, s%60, |
| 12369 | ((t - s * clk_tck) * 1000) / clk_tck, |
| 12370 | p[0]); |
| 12371 | } while (*(p += 2)); |
| 12372 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12373 | return 0; |
| 12374 | } |
| 12375 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12376 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12377 | /* |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12378 | * The let builtin. partial stolen from GNU Bash, the Bourne Again SHell. |
| 12379 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12380 | * |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12381 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12382 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12383 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12384 | letcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12385 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12386 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12387 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12388 | argv++; |
| 12389 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12390 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12391 | do { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12392 | i = ash_arith(*argv); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12393 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12394 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 12395 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12396 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12397 | #endif /* SH_MATH_SUPPORT */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12398 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12399 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12400 | /* ============ miscbltin.c |
| 12401 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12402 | * Miscellaneous builtins. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12403 | */ |
| 12404 | |
| 12405 | #undef rflag |
| 12406 | |
Denis Vlasenko | 83e5d6f | 2006-12-18 21:49:06 +0000 | [diff] [blame] | 12407 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 1 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12408 | typedef enum __rlimit_resource rlim_t; |
| 12409 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12410 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12411 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12412 | * The read builtin. Options: |
| 12413 | * -r Do not interpret '\' specially |
| 12414 | * -s Turn off echo (tty only) |
| 12415 | * -n NCHARS Read NCHARS max |
| 12416 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 12417 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 12418 | * -u FD Read from given FD instead of fd 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12419 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12420 | * TODO: bash also has: |
| 12421 | * -a ARRAY Read into array[0],[1],etc |
| 12422 | * -d DELIM End on DELIM char, not newline |
| 12423 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12424 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12425 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12426 | readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12427 | { |
Denis Vlasenko | 9cd4c76 | 2008-06-18 19:22:19 +0000 | [diff] [blame] | 12428 | static const char *const arg_REPLY[] = { "REPLY", NULL }; |
| 12429 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12430 | char **ap; |
| 12431 | int backslash; |
| 12432 | char c; |
| 12433 | int rflag; |
| 12434 | char *prompt; |
| 12435 | const char *ifs; |
| 12436 | char *p; |
| 12437 | int startword; |
| 12438 | int status; |
| 12439 | int i; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12440 | int fd = 0; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12441 | #if ENABLE_ASH_READ_NCHARS |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12442 | int nchars = 0; /* if != 0, -n is in effect */ |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12443 | int silent = 0; |
| 12444 | struct termios tty, old_tty; |
| 12445 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12446 | #if ENABLE_ASH_READ_TIMEOUT |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12447 | unsigned end_ms = 0; |
| 12448 | unsigned timeout = 0; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12449 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12450 | |
| 12451 | rflag = 0; |
| 12452 | prompt = NULL; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12453 | while ((i = nextopt("p:u:r" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 12454 | IF_ASH_READ_TIMEOUT("t:") |
| 12455 | IF_ASH_READ_NCHARS("n:s") |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12456 | )) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 12457 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12458 | case 'p': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12459 | prompt = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12460 | break; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12461 | #if ENABLE_ASH_READ_NCHARS |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12462 | case 'n': |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12463 | nchars = bb_strtou(optionarg, NULL, 10); |
| 12464 | if (nchars < 0 || errno) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12465 | ash_msg_and_raise_error("invalid count"); |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12466 | /* nchars == 0: off (bash 3.2 does this too) */ |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12467 | break; |
| 12468 | case 's': |
| 12469 | silent = 1; |
| 12470 | break; |
Ned Ludd | 2123b7c | 2005-02-09 21:07:23 +0000 | [diff] [blame] | 12471 | #endif |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12472 | #if ENABLE_ASH_READ_TIMEOUT |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12473 | case 't': |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12474 | timeout = bb_strtou(optionarg, NULL, 10); |
| 12475 | if (errno || timeout > UINT_MAX / 2048) |
| 12476 | ash_msg_and_raise_error("invalid timeout"); |
| 12477 | timeout *= 1000; |
| 12478 | #if 0 /* even bash have no -t N.NNN support */ |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12479 | ts.tv_sec = bb_strtou(optionarg, &p, 10); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12480 | ts.tv_usec = 0; |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12481 | /* EINVAL means number is ok, but not terminated by NUL */ |
| 12482 | if (*p == '.' && errno == EINVAL) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12483 | char *p2; |
| 12484 | if (*++p) { |
| 12485 | int scale; |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12486 | ts.tv_usec = bb_strtou(p, &p2, 10); |
| 12487 | if (errno) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12488 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12489 | scale = p2 - p; |
| 12490 | /* normalize to usec */ |
| 12491 | if (scale > 6) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12492 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12493 | while (scale++ < 6) |
| 12494 | ts.tv_usec *= 10; |
| 12495 | } |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12496 | } else if (ts.tv_sec < 0 || errno) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12497 | ash_msg_and_raise_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12498 | } |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12499 | if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12500 | ash_msg_and_raise_error("invalid timeout"); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12501 | } |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12502 | #endif /* if 0 */ |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12503 | break; |
| 12504 | #endif |
| 12505 | case 'r': |
| 12506 | rflag = 1; |
| 12507 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12508 | case 'u': |
| 12509 | fd = bb_strtou(optionarg, NULL, 10); |
| 12510 | if (fd < 0 || errno) |
| 12511 | ash_msg_and_raise_error("invalid file descriptor"); |
| 12512 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12513 | default: |
| 12514 | break; |
| 12515 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12516 | } |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12517 | if (prompt && isatty(fd)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12518 | out2str(prompt); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12519 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12520 | ap = argptr; |
| 12521 | if (*ap == NULL) |
Denis Vlasenko | 9cd4c76 | 2008-06-18 19:22:19 +0000 | [diff] [blame] | 12522 | ap = (char**)arg_REPLY; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12523 | ifs = bltinlookup("IFS"); |
| 12524 | if (ifs == NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12525 | ifs = defifs; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12526 | #if ENABLE_ASH_READ_NCHARS |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12527 | tcgetattr(fd, &tty); |
| 12528 | old_tty = tty; |
| 12529 | if (nchars || silent) { |
| 12530 | if (nchars) { |
| 12531 | tty.c_lflag &= ~ICANON; |
| 12532 | tty.c_cc[VMIN] = nchars < 256 ? nchars : 255; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12533 | } |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12534 | if (silent) { |
| 12535 | tty.c_lflag &= ~(ECHO | ECHOK | ECHONL); |
| 12536 | } |
| 12537 | /* if tcgetattr failed, tcsetattr will fail too. |
| 12538 | * Ignoring, it's harmless. */ |
| 12539 | tcsetattr(fd, TCSANOW, &tty); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12540 | } |
| 12541 | #endif |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12542 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12543 | status = 0; |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12544 | startword = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12545 | backslash = 0; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12546 | #if ENABLE_ASH_READ_TIMEOUT |
| 12547 | if (timeout) /* NB: ensuring end_ms is nonzero */ |
| 12548 | end_ms = ((unsigned)(monotonic_us() / 1000) + timeout) | 1; |
| 12549 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12550 | STARTSTACKSTR(p); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12551 | do { |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12552 | const char *is_ifs; |
| 12553 | |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12554 | #if ENABLE_ASH_READ_TIMEOUT |
| 12555 | if (end_ms) { |
| 12556 | struct pollfd pfd[1]; |
| 12557 | pfd[0].fd = fd; |
| 12558 | pfd[0].events = POLLIN; |
| 12559 | timeout = end_ms - (unsigned)(monotonic_us() / 1000); |
| 12560 | if ((int)timeout <= 0 /* already late? */ |
| 12561 | || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */ |
| 12562 | ) { /* timed out! */ |
| 12563 | #if ENABLE_ASH_READ_NCHARS |
| 12564 | tcsetattr(fd, TCSANOW, &old_tty); |
| 12565 | #endif |
| 12566 | return 1; |
| 12567 | } |
| 12568 | } |
| 12569 | #endif |
| 12570 | if (nonblock_safe_read(fd, &c, 1) != 1) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12571 | status = 1; |
| 12572 | break; |
| 12573 | } |
| 12574 | if (c == '\0') |
| 12575 | continue; |
| 12576 | if (backslash) { |
| 12577 | backslash = 0; |
| 12578 | if (c != '\n') |
| 12579 | goto put; |
| 12580 | continue; |
| 12581 | } |
| 12582 | if (!rflag && c == '\\') { |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12583 | backslash = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12584 | continue; |
| 12585 | } |
| 12586 | if (c == '\n') |
| 12587 | break; |
Denis Vlasenko | f6fbd62 | 2009-03-31 19:36:58 +0000 | [diff] [blame] | 12588 | /* $IFS splitting */ |
Denis Vlasenko | 551ffdc | 2009-04-01 19:48:05 +0000 | [diff] [blame] | 12589 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */ |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12590 | is_ifs = strchr(ifs, c); |
| 12591 | if (startword && is_ifs) { |
| 12592 | if (isspace(c)) |
| 12593 | continue; |
Denis Vlasenko | f6fbd62 | 2009-03-31 19:36:58 +0000 | [diff] [blame] | 12594 | /* it is a non-space ifs char */ |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12595 | startword--; |
| 12596 | if (startword == 1) /* first one? */ |
Denis Vlasenko | f6fbd62 | 2009-03-31 19:36:58 +0000 | [diff] [blame] | 12597 | continue; /* yes, it is not next word yet */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12598 | } |
| 12599 | startword = 0; |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12600 | if (ap[1] != NULL && is_ifs) { |
| 12601 | const char *beg; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12602 | STACKSTRNUL(p); |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12603 | beg = stackblock(); |
| 12604 | setvar(*ap, beg, 0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12605 | ap++; |
Denis Vlasenko | f6fbd62 | 2009-03-31 19:36:58 +0000 | [diff] [blame] | 12606 | /* can we skip one non-space ifs char? (2: yes) */ |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12607 | startword = isspace(c) ? 2 : 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12608 | STARTSTACKSTR(p); |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12609 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12610 | } |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12611 | put: |
| 12612 | STPUTC(c, p); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12613 | } |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12614 | /* end of do {} while: */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12615 | #if ENABLE_ASH_READ_NCHARS |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12616 | while (--nchars); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12617 | #else |
| 12618 | while (1); |
| 12619 | #endif |
| 12620 | |
| 12621 | #if ENABLE_ASH_READ_NCHARS |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12622 | tcsetattr(fd, TCSANOW, &old_tty); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12623 | #endif |
| 12624 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12625 | STACKSTRNUL(p); |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12626 | /* Remove trailing space ifs chars */ |
| 12627 | while ((char *)stackblock() <= --p && isspace(*p) && strchr(ifs, *p) != NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12628 | *p = '\0'; |
| 12629 | setvar(*ap, stackblock(), 0); |
| 12630 | while (*++ap != NULL) |
| 12631 | setvar(*ap, nullstr, 0); |
| 12632 | return status; |
| 12633 | } |
| 12634 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12635 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12636 | umaskcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12637 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12638 | static const char permuser[3] ALIGN1 = "ugo"; |
| 12639 | static const char permmode[3] ALIGN1 = "rwx"; |
| 12640 | static const short permmask[] ALIGN2 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12641 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 12642 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 12643 | S_IROTH, S_IWOTH, S_IXOTH |
| 12644 | }; |
| 12645 | |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 12646 | /* TODO: use bb_parse_mode() instead */ |
| 12647 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12648 | char *ap; |
| 12649 | mode_t mask; |
| 12650 | int i; |
| 12651 | int symbolic_mode = 0; |
| 12652 | |
| 12653 | while (nextopt("S") != '\0') { |
| 12654 | symbolic_mode = 1; |
| 12655 | } |
| 12656 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12657 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12658 | mask = umask(0); |
| 12659 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12660 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12661 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12662 | ap = *argptr; |
| 12663 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12664 | if (symbolic_mode) { |
| 12665 | char buf[18]; |
| 12666 | char *p = buf; |
| 12667 | |
| 12668 | for (i = 0; i < 3; i++) { |
| 12669 | int j; |
| 12670 | |
| 12671 | *p++ = permuser[i]; |
| 12672 | *p++ = '='; |
| 12673 | for (j = 0; j < 3; j++) { |
| 12674 | if ((mask & permmask[3 * i + j]) == 0) { |
| 12675 | *p++ = permmode[j]; |
| 12676 | } |
| 12677 | } |
| 12678 | *p++ = ','; |
| 12679 | } |
| 12680 | *--p = 0; |
| 12681 | puts(buf); |
| 12682 | } else { |
| 12683 | out1fmt("%.4o\n", mask); |
| 12684 | } |
| 12685 | } else { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12686 | if (isdigit((unsigned char) *ap)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12687 | mask = 0; |
| 12688 | do { |
| 12689 | if (*ap >= '8' || *ap < '0') |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12690 | ash_msg_and_raise_error(illnum, argv[1]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12691 | mask = (mask << 3) + (*ap - '0'); |
| 12692 | } while (*++ap != '\0'); |
| 12693 | umask(mask); |
| 12694 | } else { |
| 12695 | mask = ~mask & 0777; |
| 12696 | if (!bb_parse_mode(ap, &mask)) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 12697 | ash_msg_and_raise_error("illegal mode: %s", ap); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12698 | } |
| 12699 | umask(~mask & 0777); |
| 12700 | } |
| 12701 | } |
| 12702 | return 0; |
| 12703 | } |
| 12704 | |
| 12705 | /* |
| 12706 | * ulimit builtin |
| 12707 | * |
| 12708 | * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and |
| 12709 | * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with |
| 12710 | * ash by J.T. Conklin. |
| 12711 | * |
| 12712 | * Public domain. |
| 12713 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12714 | struct limits { |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12715 | uint8_t cmd; /* RLIMIT_xxx fit into it */ |
| 12716 | uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12717 | char option; |
| 12718 | }; |
| 12719 | |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12720 | static const struct limits limits_tbl[] = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12721 | #ifdef RLIMIT_CPU |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12722 | { RLIMIT_CPU, 0, 't' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12723 | #endif |
| 12724 | #ifdef RLIMIT_FSIZE |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12725 | { RLIMIT_FSIZE, 9, 'f' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12726 | #endif |
| 12727 | #ifdef RLIMIT_DATA |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12728 | { RLIMIT_DATA, 10, 'd' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12729 | #endif |
| 12730 | #ifdef RLIMIT_STACK |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12731 | { RLIMIT_STACK, 10, 's' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12732 | #endif |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12733 | #ifdef RLIMIT_CORE |
| 12734 | { RLIMIT_CORE, 9, 'c' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12735 | #endif |
| 12736 | #ifdef RLIMIT_RSS |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12737 | { RLIMIT_RSS, 10, 'm' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12738 | #endif |
| 12739 | #ifdef RLIMIT_MEMLOCK |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12740 | { RLIMIT_MEMLOCK, 10, 'l' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12741 | #endif |
| 12742 | #ifdef RLIMIT_NPROC |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12743 | { RLIMIT_NPROC, 0, 'p' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12744 | #endif |
| 12745 | #ifdef RLIMIT_NOFILE |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12746 | { RLIMIT_NOFILE, 0, 'n' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12747 | #endif |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12748 | #ifdef RLIMIT_AS |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12749 | { RLIMIT_AS, 10, 'v' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12750 | #endif |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12751 | #ifdef RLIMIT_LOCKS |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12752 | { RLIMIT_LOCKS, 0, 'w' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12753 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12754 | }; |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12755 | static const char limits_name[] = |
| 12756 | #ifdef RLIMIT_CPU |
| 12757 | "time(seconds)" "\0" |
| 12758 | #endif |
| 12759 | #ifdef RLIMIT_FSIZE |
| 12760 | "file(blocks)" "\0" |
| 12761 | #endif |
| 12762 | #ifdef RLIMIT_DATA |
| 12763 | "data(kb)" "\0" |
| 12764 | #endif |
| 12765 | #ifdef RLIMIT_STACK |
| 12766 | "stack(kb)" "\0" |
| 12767 | #endif |
| 12768 | #ifdef RLIMIT_CORE |
| 12769 | "coredump(blocks)" "\0" |
| 12770 | #endif |
| 12771 | #ifdef RLIMIT_RSS |
| 12772 | "memory(kb)" "\0" |
| 12773 | #endif |
| 12774 | #ifdef RLIMIT_MEMLOCK |
| 12775 | "locked memory(kb)" "\0" |
| 12776 | #endif |
| 12777 | #ifdef RLIMIT_NPROC |
| 12778 | "process" "\0" |
| 12779 | #endif |
| 12780 | #ifdef RLIMIT_NOFILE |
| 12781 | "nofiles" "\0" |
| 12782 | #endif |
| 12783 | #ifdef RLIMIT_AS |
| 12784 | "vmemory(kb)" "\0" |
| 12785 | #endif |
| 12786 | #ifdef RLIMIT_LOCKS |
| 12787 | "locks" "\0" |
| 12788 | #endif |
| 12789 | ; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12790 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12791 | enum limtype { SOFT = 0x1, HARD = 0x2 }; |
| 12792 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12793 | static void |
| 12794 | printlim(enum limtype how, const struct rlimit *limit, |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12795 | const struct limits *l) |
| 12796 | { |
| 12797 | rlim_t val; |
| 12798 | |
| 12799 | val = limit->rlim_max; |
| 12800 | if (how & SOFT) |
| 12801 | val = limit->rlim_cur; |
| 12802 | |
| 12803 | if (val == RLIM_INFINITY) |
| 12804 | out1fmt("unlimited\n"); |
| 12805 | else { |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12806 | val >>= l->factor_shift; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12807 | out1fmt("%lld\n", (long long) val); |
| 12808 | } |
| 12809 | } |
| 12810 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12811 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12812 | ulimitcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12813 | { |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12814 | int c; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12815 | rlim_t val = 0; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12816 | enum limtype how = SOFT | HARD; |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12817 | const struct limits *l; |
| 12818 | int set, all = 0; |
| 12819 | int optc, what; |
| 12820 | struct rlimit limit; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12821 | |
| 12822 | what = 'f'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 12823 | while ((optc = nextopt("HSa" |
| 12824 | #ifdef RLIMIT_CPU |
| 12825 | "t" |
| 12826 | #endif |
| 12827 | #ifdef RLIMIT_FSIZE |
| 12828 | "f" |
| 12829 | #endif |
| 12830 | #ifdef RLIMIT_DATA |
| 12831 | "d" |
| 12832 | #endif |
| 12833 | #ifdef RLIMIT_STACK |
| 12834 | "s" |
| 12835 | #endif |
| 12836 | #ifdef RLIMIT_CORE |
| 12837 | "c" |
| 12838 | #endif |
| 12839 | #ifdef RLIMIT_RSS |
| 12840 | "m" |
| 12841 | #endif |
| 12842 | #ifdef RLIMIT_MEMLOCK |
| 12843 | "l" |
| 12844 | #endif |
| 12845 | #ifdef RLIMIT_NPROC |
| 12846 | "p" |
| 12847 | #endif |
| 12848 | #ifdef RLIMIT_NOFILE |
| 12849 | "n" |
| 12850 | #endif |
| 12851 | #ifdef RLIMIT_AS |
| 12852 | "v" |
| 12853 | #endif |
| 12854 | #ifdef RLIMIT_LOCKS |
| 12855 | "w" |
| 12856 | #endif |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12857 | )) != '\0') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12858 | switch (optc) { |
| 12859 | case 'H': |
| 12860 | how = HARD; |
| 12861 | break; |
| 12862 | case 'S': |
| 12863 | how = SOFT; |
| 12864 | break; |
| 12865 | case 'a': |
| 12866 | all = 1; |
| 12867 | break; |
| 12868 | default: |
| 12869 | what = optc; |
| 12870 | } |
| 12871 | |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12872 | for (l = limits_tbl; l->option != what; l++) |
| 12873 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12874 | |
| 12875 | set = *argptr ? 1 : 0; |
| 12876 | if (set) { |
| 12877 | char *p = *argptr; |
| 12878 | |
| 12879 | if (all || argptr[1]) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12880 | ash_msg_and_raise_error("too many arguments"); |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 12881 | if (strncmp(p, "unlimited\n", 9) == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12882 | val = RLIM_INFINITY; |
| 12883 | else { |
| 12884 | val = (rlim_t) 0; |
| 12885 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12886 | while ((c = *p++) >= '0' && c <= '9') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12887 | val = (val * 10) + (long)(c - '0'); |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 12888 | // val is actually 'unsigned long int' and can't get < 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12889 | if (val < (rlim_t) 0) |
| 12890 | break; |
| 12891 | } |
| 12892 | if (c) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12893 | ash_msg_and_raise_error("bad number"); |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12894 | val <<= l->factor_shift; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12895 | } |
| 12896 | } |
| 12897 | if (all) { |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12898 | const char *lname = limits_name; |
| 12899 | for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12900 | getrlimit(l->cmd, &limit); |
Denis Vlasenko | a59f435 | 2007-10-29 19:17:29 +0000 | [diff] [blame] | 12901 | out1fmt("%-20s ", lname); |
| 12902 | lname += strlen(lname) + 1; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12903 | printlim(how, &limit, l); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12904 | } |
| 12905 | return 0; |
| 12906 | } |
| 12907 | |
| 12908 | getrlimit(l->cmd, &limit); |
| 12909 | if (set) { |
| 12910 | if (how & HARD) |
| 12911 | limit.rlim_max = val; |
| 12912 | if (how & SOFT) |
| 12913 | limit.rlim_cur = val; |
| 12914 | if (setrlimit(l->cmd, &limit) < 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12915 | ash_msg_and_raise_error("error setting limit (%m)"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12916 | } else { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12917 | printlim(how, &limit, l); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12918 | } |
| 12919 | return 0; |
| 12920 | } |
| 12921 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12922 | /* ============ main() and helpers */ |
| 12923 | |
| 12924 | /* |
| 12925 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12926 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12927 | static void exitshell(void) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12928 | static void |
| 12929 | exitshell(void) |
| 12930 | { |
| 12931 | struct jmploc loc; |
| 12932 | char *p; |
| 12933 | int status; |
| 12934 | |
| 12935 | status = exitstatus; |
| 12936 | TRACE(("pid %d, exitshell(%d)\n", getpid(), status)); |
| 12937 | if (setjmp(loc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12938 | if (exception_type == EXEXIT) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12939 | /* dash bug: it just does _exit(exitstatus) here |
| 12940 | * but we have to do setjobctl(0) first! |
| 12941 | * (bug is still not fixed in dash-0.5.3 - if you run dash |
| 12942 | * under Midnight Commander, on exit from dash MC is backgrounded) */ |
| 12943 | status = exitstatus; |
| 12944 | goto out; |
| 12945 | } |
| 12946 | exception_handler = &loc; |
| 12947 | p = trap[0]; |
| 12948 | if (p) { |
| 12949 | trap[0] = NULL; |
| 12950 | evalstring(p, 0); |
| 12951 | } |
| 12952 | flush_stdout_stderr(); |
| 12953 | out: |
| 12954 | setjobctl(0); |
| 12955 | _exit(status); |
| 12956 | /* NOTREACHED */ |
| 12957 | } |
| 12958 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12959 | static void |
| 12960 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12961 | { |
| 12962 | /* from input.c: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 12963 | basepf.next_to_pgetc = basepf.buf = basebuf; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12964 | |
| 12965 | /* from trap.c: */ |
| 12966 | signal(SIGCHLD, SIG_DFL); |
| 12967 | |
| 12968 | /* from var.c: */ |
| 12969 | { |
| 12970 | char **envp; |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12971 | char ppid[sizeof(int)*3 + 1]; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12972 | const char *p; |
| 12973 | struct stat st1, st2; |
| 12974 | |
| 12975 | initvar(); |
| 12976 | for (envp = environ; envp && *envp; envp++) { |
| 12977 | if (strchr(*envp, '=')) { |
| 12978 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 12979 | } |
| 12980 | } |
| 12981 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12982 | snprintf(ppid, sizeof(ppid), "%u", (unsigned) getppid()); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12983 | setvar("PPID", ppid, 0); |
| 12984 | |
| 12985 | p = lookupvar("PWD"); |
| 12986 | if (p) |
| 12987 | if (*p != '/' || stat(p, &st1) || stat(".", &st2) |
| 12988 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) |
| 12989 | p = '\0'; |
| 12990 | setpwd(p, 0); |
| 12991 | } |
| 12992 | } |
| 12993 | |
| 12994 | /* |
| 12995 | * Process the shell command line arguments. |
| 12996 | */ |
| 12997 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12998 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12999 | { |
| 13000 | int i; |
| 13001 | const char *xminusc; |
| 13002 | char **xargv; |
| 13003 | |
| 13004 | xargv = argv; |
| 13005 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13006 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13007 | xargv++; |
| 13008 | for (i = 0; i < NOPTS; i++) |
| 13009 | optlist[i] = 2; |
| 13010 | argptr = xargv; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 13011 | if (options(1)) { |
| 13012 | /* it already printed err message */ |
| 13013 | raise_exception(EXERROR); |
| 13014 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13015 | xargv = argptr; |
| 13016 | xminusc = minusc; |
| 13017 | if (*xargv == NULL) { |
| 13018 | if (xminusc) |
| 13019 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 13020 | sflag = 1; |
| 13021 | } |
| 13022 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 13023 | iflag = 1; |
| 13024 | if (mflag == 2) |
| 13025 | mflag = iflag; |
| 13026 | for (i = 0; i < NOPTS; i++) |
| 13027 | if (optlist[i] == 2) |
| 13028 | optlist[i] = 0; |
| 13029 | #if DEBUG == 2 |
| 13030 | debug = 1; |
| 13031 | #endif |
| 13032 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 13033 | if (xminusc) { |
| 13034 | minusc = *xargv++; |
| 13035 | if (*xargv) |
| 13036 | goto setarg0; |
| 13037 | } else if (!sflag) { |
| 13038 | setinputfile(*xargv, 0); |
| 13039 | setarg0: |
| 13040 | arg0 = *xargv++; |
| 13041 | commandname = arg0; |
| 13042 | } |
| 13043 | |
| 13044 | shellparam.p = xargv; |
| 13045 | #if ENABLE_ASH_GETOPTS |
| 13046 | shellparam.optind = 1; |
| 13047 | shellparam.optoff = -1; |
| 13048 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13049 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13050 | while (*xargv) { |
| 13051 | shellparam.nparam++; |
| 13052 | xargv++; |
| 13053 | } |
| 13054 | optschanged(); |
| 13055 | } |
| 13056 | |
| 13057 | /* |
| 13058 | * Read /etc/profile or .profile. |
| 13059 | */ |
| 13060 | static void |
| 13061 | read_profile(const char *name) |
| 13062 | { |
| 13063 | int skip; |
| 13064 | |
| 13065 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 13066 | return; |
| 13067 | skip = cmdloop(0); |
| 13068 | popfile(); |
| 13069 | if (skip) |
| 13070 | exitshell(); |
| 13071 | } |
| 13072 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13073 | /* |
| 13074 | * This routine is called when an error or an interrupt occurs in an |
| 13075 | * interactive shell and control is returned to the main command loop. |
| 13076 | */ |
| 13077 | static void |
| 13078 | reset(void) |
| 13079 | { |
| 13080 | /* from eval.c: */ |
| 13081 | evalskip = 0; |
| 13082 | loopnest = 0; |
| 13083 | /* from input.c: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 13084 | g_parsefile->left_in_buffer = 0; |
| 13085 | g_parsefile->left_in_line = 0; /* clear input buffer */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13086 | popallfiles(); |
| 13087 | /* from parser.c: */ |
| 13088 | tokpushback = 0; |
| 13089 | checkkwd = 0; |
| 13090 | /* from redir.c: */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 13091 | clearredir(/*drop:*/ 0); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13092 | } |
| 13093 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13094 | #if PROFILE |
| 13095 | static short profile_buf[16384]; |
| 13096 | extern int etext(); |
| 13097 | #endif |
| 13098 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13099 | /* |
| 13100 | * Main routine. We initialize things, parse the arguments, execute |
| 13101 | * profiles if we're a login shell, and then call cmdloop to execute |
| 13102 | * commands. The setjmp call sets up the location to jump to when an |
| 13103 | * exception occurs. When an exception occurs the variable "state" |
| 13104 | * is used to figure out how far we had gotten. |
| 13105 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 13106 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13107 | int ash_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13108 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 13109 | const char *shinit; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13110 | volatile smallint state; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13111 | struct jmploc jmploc; |
| 13112 | struct stackmark smark; |
| 13113 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13114 | /* Initialize global data */ |
| 13115 | INIT_G_misc(); |
| 13116 | INIT_G_memstack(); |
| 13117 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 13118 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13119 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 13120 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13121 | INIT_G_cmdtable(); |
| 13122 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13123 | #if PROFILE |
| 13124 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 13125 | #endif |
| 13126 | |
| 13127 | #if ENABLE_FEATURE_EDITING |
| 13128 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
| 13129 | #endif |
| 13130 | state = 0; |
| 13131 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13132 | smallint e; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13133 | smallint s; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13134 | |
| 13135 | reset(); |
| 13136 | |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13137 | e = exception_type; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13138 | if (e == EXERROR) |
| 13139 | exitstatus = 2; |
| 13140 | s = state; |
| 13141 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) |
| 13142 | exitshell(); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13143 | if (e == EXINT) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13144 | outcslow('\n', stderr); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13145 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13146 | popstackmark(&smark); |
| 13147 | FORCE_INT_ON; /* enable interrupts */ |
| 13148 | if (s == 1) |
| 13149 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13150 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13151 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13152 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13153 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13154 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13155 | } |
| 13156 | exception_handler = &jmploc; |
| 13157 | #if DEBUG |
| 13158 | opentrace(); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 13159 | TRACE(("Shell args: ")); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13160 | trace_puts_args(argv); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13161 | #endif |
| 13162 | rootpid = getpid(); |
| 13163 | |
| 13164 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 13165 | /* Can use monotonic_ns() for better randomness but for now it is |
| 13166 | * not used anywhere else in busybox... so avoid bloat */ |
| 13167 | random_galois_LFSR = random_LCG = rootpid + monotonic_us(); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13168 | #endif |
| 13169 | init(); |
| 13170 | setstackmark(&smark); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13171 | procargs(argv); |
| 13172 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13173 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
| 13174 | if (iflag) { |
| 13175 | const char *hp = lookupvar("HISTFILE"); |
| 13176 | |
| 13177 | if (hp == NULL) { |
| 13178 | hp = lookupvar("HOME"); |
| 13179 | if (hp != NULL) { |
| 13180 | char *defhp = concat_path_file(hp, ".ash_history"); |
| 13181 | setvar("HISTFILE", defhp, 0); |
| 13182 | free(defhp); |
| 13183 | } |
| 13184 | } |
| 13185 | } |
| 13186 | #endif |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13187 | if (/* argv[0] && */ argv[0][0] == '-') |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13188 | isloginsh = 1; |
| 13189 | if (isloginsh) { |
| 13190 | state = 1; |
| 13191 | read_profile("/etc/profile"); |
| 13192 | state1: |
| 13193 | state = 2; |
| 13194 | read_profile(".profile"); |
| 13195 | } |
| 13196 | state2: |
| 13197 | state = 3; |
| 13198 | if ( |
| 13199 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13200 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13201 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13202 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13203 | ) { |
| 13204 | shinit = lookupvar("ENV"); |
| 13205 | if (shinit != NULL && *shinit != '\0') { |
| 13206 | read_profile(shinit); |
| 13207 | } |
| 13208 | } |
| 13209 | state3: |
| 13210 | state = 4; |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13211 | if (minusc) { |
| 13212 | /* evalstring pushes parsefile stack. |
| 13213 | * Ensure we don't falsely claim that 0 (stdin) |
Denis Vlasenko | 5368ad5 | 2009-03-20 10:20:08 +0000 | [diff] [blame] | 13214 | * is one of stacked source fds. |
| 13215 | * Testcase: ash -c 'exec 1>&0' must not complain. */ |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13216 | if (!sflag) |
| 13217 | g_parsefile->fd = -1; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13218 | evalstring(minusc, 0); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13219 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13220 | |
| 13221 | if (sflag || minusc == NULL) { |
| 13222 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 13223 | if (iflag) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13224 | const char *hp = lookupvar("HISTFILE"); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13225 | if (hp) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13226 | line_input_state->hist_file = hp; |
| 13227 | } |
| 13228 | #endif |
| 13229 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 13230 | cmdloop(1); |
| 13231 | } |
| 13232 | #if PROFILE |
| 13233 | monitor(0); |
| 13234 | #endif |
| 13235 | #ifdef GPROF |
| 13236 | { |
| 13237 | extern void _mcleanup(void); |
| 13238 | _mcleanup(); |
| 13239 | } |
| 13240 | #endif |
| 13241 | exitshell(); |
| 13242 | /* NOTREACHED */ |
| 13243 | } |
| 13244 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13245 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13246 | /*- |
| 13247 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 13248 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13249 | * |
| 13250 | * This code is derived from software contributed to Berkeley by |
| 13251 | * Kenneth Almquist. |
| 13252 | * |
| 13253 | * Redistribution and use in source and binary forms, with or without |
| 13254 | * modification, are permitted provided that the following conditions |
| 13255 | * are met: |
| 13256 | * 1. Redistributions of source code must retain the above copyright |
| 13257 | * notice, this list of conditions and the following disclaimer. |
| 13258 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13259 | * notice, this list of conditions and the following disclaimer in the |
| 13260 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13261 | * 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] | 13262 | * may be used to endorse or promote products derived from this software |
| 13263 | * without specific prior written permission. |
| 13264 | * |
| 13265 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 13266 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13267 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 13268 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13269 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13270 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 13271 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13272 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 13273 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 13274 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 13275 | * SUCH DAMAGE. |
| 13276 | */ |