Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * ash shell port for busybox |
| 4 | * |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * Kenneth Almquist. |
| 7 | * |
| 8 | * Original BSD copyright notice is retained at the end of this file. |
| 9 | * |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 10 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12 | * |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13 | * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au> |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 14 | * was re-ported from NetBSD and debianized. |
| 15 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 16 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 19 | /* |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 20 | * The following should be set to reflect the type of system you have: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 21 | * JOBS -> 1 if you have Berkeley job control, 0 otherwise. |
| 22 | * define SYSV if you are running under System V. |
| 23 | * define DEBUG=1 to compile in debugging ('set -o debug' to turn on) |
| 24 | * define DEBUG=2 to compile in and turn on debugging. |
| 25 | * |
| 26 | * When debugging is on, debugging info will be written to ./trace and |
| 27 | * a quit signal will generate a core dump. |
| 28 | */ |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 29 | #define DEBUG 0 |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 30 | /* Tweak debug output verbosity here */ |
| 31 | #define DEBUG_TIME 0 |
| 32 | #define DEBUG_PID 1 |
| 33 | #define DEBUG_SIG 1 |
| 34 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 35 | #define PROFILE 0 |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 37 | #define JOBS ENABLE_ASH_JOB_CONTROL |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 39 | #if DEBUG |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 40 | # ifndef _GNU_SOURCE |
| 41 | # define _GNU_SOURCE |
| 42 | # endif |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 43 | #endif |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 44 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 45 | #include "busybox.h" /* for applet_names */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 46 | #include <paths.h> |
| 47 | #include <setjmp.h> |
| 48 | #include <fnmatch.h> |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 49 | |
| 50 | #include "shell_common.h" |
| 51 | #include "builtin_read.h" |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 52 | #include "builtin_ulimit.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 53 | #include "math.h" |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 54 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 55 | # include "random.h" |
Denys Vlasenko | 36df048 | 2009-10-19 16:07:28 +0200 | [diff] [blame] | 56 | #else |
| 57 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 58 | #endif |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 59 | |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 60 | #define SKIP_definitions 1 |
| 61 | #include "applet_tables.h" |
| 62 | #undef SKIP_definitions |
| 63 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 64 | /* STANDALONE does not make sense, and won't compile */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 65 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 66 | # undef ENABLE_FEATURE_SH_STANDALONE |
| 67 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 68 | # undef IF_NOT_FEATURE_SH_STANDALONE |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 69 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
| 70 | # define IF_FEATURE_SH_STANDALONE(...) |
| 71 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 74 | #ifndef PIPE_BUF |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 75 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 76 | #endif |
| 77 | |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 78 | #if !BB_MMU |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 79 | # error "Do not even bother, ash will not run on NOMMU machine" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 83 | /* ============ Hash table sizes. Configurable. */ |
| 84 | |
| 85 | #define VTABSIZE 39 |
| 86 | #define ATABSIZE 39 |
| 87 | #define CMDTABLESIZE 31 /* should be prime */ |
| 88 | |
| 89 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 90 | /* ============ Shell options */ |
| 91 | |
| 92 | static const char *const optletters_optnames[] = { |
| 93 | "e" "errexit", |
| 94 | "f" "noglob", |
| 95 | "I" "ignoreeof", |
| 96 | "i" "interactive", |
| 97 | "m" "monitor", |
| 98 | "n" "noexec", |
| 99 | "s" "stdin", |
| 100 | "x" "xtrace", |
| 101 | "v" "verbose", |
| 102 | "C" "noclobber", |
| 103 | "a" "allexport", |
| 104 | "b" "notify", |
| 105 | "u" "nounset", |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 106 | "\0" "vi" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 107 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 108 | ,"\0" "pipefail" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 109 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 110 | #if DEBUG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 111 | ,"\0" "nolog" |
| 112 | ,"\0" "debug" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 113 | #endif |
| 114 | }; |
| 115 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 116 | #define optletters(n) optletters_optnames[n][0] |
| 117 | #define optnames(n) (optletters_optnames[n] + 1) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 118 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 119 | enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 120 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 121 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 122 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 123 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 124 | static const char homestr[] ALIGN1 = "HOME"; |
| 125 | static const char snlfmt[] ALIGN1 = "%s\n"; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 126 | static const char msg_illnum[] ALIGN1 = "Illegal number: %s"; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 128 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 129 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 130 | * jump locations. The global variable handler contains the location to |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 131 | * jump to when an exception occurs, and the global variable exception_type |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 132 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 133 | * exception handlers, the user should save the value of handler on entry |
| 134 | * to an inner scope, set handler to point to a jmploc structure for the |
| 135 | * inner scope, and restore handler on exit from the scope. |
| 136 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 137 | struct jmploc { |
| 138 | jmp_buf loc; |
| 139 | }; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 140 | |
| 141 | struct globals_misc { |
| 142 | /* pid of main shell */ |
| 143 | int rootpid; |
| 144 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 145 | int shlvl; |
| 146 | #define rootshell (!shlvl) |
| 147 | char *minusc; /* argument to -c option */ |
| 148 | |
| 149 | char *curdir; // = nullstr; /* current working directory */ |
| 150 | char *physdir; // = nullstr; /* physical working directory */ |
| 151 | |
| 152 | char *arg0; /* value of $0 */ |
| 153 | |
| 154 | struct jmploc *exception_handler; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 155 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 156 | volatile int suppress_int; /* counter */ |
| 157 | volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 158 | /* last pending signal */ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 159 | volatile /*sig_atomic_t*/ smallint pending_sig; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 160 | smallint exception_type; /* kind of exception (0..5) */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 161 | /* exceptions */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 162 | #define EXINT 0 /* SIGINT received */ |
| 163 | #define EXERROR 1 /* a generic error */ |
| 164 | #define EXSHELLPROC 2 /* execute a shell procedure */ |
| 165 | #define EXEXEC 3 /* command execution failed */ |
| 166 | #define EXEXIT 4 /* exit the shell */ |
| 167 | #define EXSIG 5 /* trapped signal in wait(1) */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 168 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 169 | smallint isloginsh; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 170 | char nullstr[1]; /* zero length string */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 171 | |
| 172 | char optlist[NOPTS]; |
| 173 | #define eflag optlist[0] |
| 174 | #define fflag optlist[1] |
| 175 | #define Iflag optlist[2] |
| 176 | #define iflag optlist[3] |
| 177 | #define mflag optlist[4] |
| 178 | #define nflag optlist[5] |
| 179 | #define sflag optlist[6] |
| 180 | #define xflag optlist[7] |
| 181 | #define vflag optlist[8] |
| 182 | #define Cflag optlist[9] |
| 183 | #define aflag optlist[10] |
| 184 | #define bflag optlist[11] |
| 185 | #define uflag optlist[12] |
| 186 | #define viflag optlist[13] |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 187 | #if ENABLE_ASH_BASH_COMPAT |
| 188 | # define pipefail optlist[14] |
| 189 | #else |
| 190 | # define pipefail 0 |
| 191 | #endif |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 192 | #if DEBUG |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 193 | # define nolog optlist[14 + ENABLE_ASH_BASH_COMPAT] |
| 194 | # define debug optlist[15 + ENABLE_ASH_BASH_COMPAT] |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 195 | #endif |
| 196 | |
| 197 | /* trap handler commands */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 198 | /* |
| 199 | * Sigmode records the current value of the signal handlers for the various |
| 200 | * modes. A value of zero means that the current handler is not known. |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 201 | * S_HARD_IGN indicates that the signal was ignored on entry to the shell. |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 202 | */ |
| 203 | char sigmode[NSIG - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 204 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 205 | #define S_CATCH 2 /* signal is caught */ |
| 206 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 207 | #define S_HARD_IGN 4 /* signal is ignored permenantly */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 208 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 209 | /* indicates specified signal received */ |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 210 | uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 211 | uint8_t may_have_traps; /* 0: definitely no traps are set, 1: some traps may be set */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 212 | char *trap[NSIG]; |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 213 | char **trap_ptr; /* used only by "trap hack" */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 214 | |
| 215 | /* Rarely referenced stuff */ |
| 216 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 217 | random_t random_gen; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 218 | #endif |
| 219 | pid_t backgndpid; /* pid of last background process */ |
| 220 | 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] | 221 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 222 | extern struct globals_misc *const ash_ptr_to_globals_misc; |
| 223 | #define G_misc (*ash_ptr_to_globals_misc) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 224 | #define rootpid (G_misc.rootpid ) |
| 225 | #define shlvl (G_misc.shlvl ) |
| 226 | #define minusc (G_misc.minusc ) |
| 227 | #define curdir (G_misc.curdir ) |
| 228 | #define physdir (G_misc.physdir ) |
| 229 | #define arg0 (G_misc.arg0 ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 230 | #define exception_handler (G_misc.exception_handler) |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 231 | #define exception_type (G_misc.exception_type ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 232 | #define suppress_int (G_misc.suppress_int ) |
| 233 | #define pending_int (G_misc.pending_int ) |
| 234 | #define pending_sig (G_misc.pending_sig ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 235 | #define isloginsh (G_misc.isloginsh ) |
| 236 | #define nullstr (G_misc.nullstr ) |
| 237 | #define optlist (G_misc.optlist ) |
| 238 | #define sigmode (G_misc.sigmode ) |
| 239 | #define gotsig (G_misc.gotsig ) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 240 | #define may_have_traps (G_misc.may_have_traps ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 241 | #define trap (G_misc.trap ) |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 242 | #define trap_ptr (G_misc.trap_ptr ) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 243 | #define random_gen (G_misc.random_gen ) |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 244 | #define backgndpid (G_misc.backgndpid ) |
| 245 | #define job_warning (G_misc.job_warning) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 246 | #define INIT_G_misc() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 247 | (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ |
| 248 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 249 | curdir = nullstr; \ |
| 250 | physdir = nullstr; \ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 251 | trap_ptr = trap; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 252 | } while (0) |
| 253 | |
| 254 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 255 | /* ============ DEBUG */ |
| 256 | #if DEBUG |
| 257 | static void trace_printf(const char *fmt, ...); |
| 258 | static void trace_vprintf(const char *fmt, va_list va); |
| 259 | # define TRACE(param) trace_printf param |
| 260 | # define TRACEV(param) trace_vprintf param |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 261 | # define close(fd) do { \ |
| 262 | int dfd = (fd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 263 | if (close(dfd) < 0) \ |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 264 | bb_error_msg("bug on %d: closing %d(0x%x)", \ |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 265 | __LINE__, dfd, dfd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 266 | } while (0) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 267 | #else |
| 268 | # define TRACE(param) |
| 269 | # define TRACEV(param) |
| 270 | #endif |
| 271 | |
| 272 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 273 | /* ============ Utility functions */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 274 | #define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0) |
| 275 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 276 | static int isdigit_str9(const char *str) |
| 277 | { |
| 278 | int maxlen = 9 + 1; /* max 9 digits: 999999999 */ |
| 279 | while (--maxlen && isdigit(*str)) |
| 280 | str++; |
| 281 | return (*str == '\0'); |
| 282 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 283 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 284 | |
| 285 | /* ============ Interrupts / exceptions */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 286 | /* |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 287 | * These macros allow the user to suspend the handling of interrupt signals |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 288 | * 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] | 289 | * much more efficient and portable. (But hacking the kernel is so much |
| 290 | * more fun than worrying about efficiency and portability. :-)) |
| 291 | */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 292 | #define INT_OFF do { \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 293 | suppress_int++; \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 294 | xbarrier(); \ |
| 295 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 296 | |
| 297 | /* |
| 298 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 299 | * 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] | 300 | * stored in the global variable "exception_type". |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 301 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 302 | static void raise_exception(int) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 303 | static void |
| 304 | raise_exception(int e) |
| 305 | { |
| 306 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 307 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 308 | abort(); |
| 309 | #endif |
| 310 | INT_OFF; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 311 | exception_type = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 312 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 313 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 314 | #if DEBUG |
| 315 | #define raise_exception(e) do { \ |
| 316 | TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \ |
| 317 | raise_exception(e); \ |
| 318 | } while (0) |
| 319 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | * Called from trap.c when a SIGINT is received. (If the user specifies |
| 323 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 324 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 325 | * are held using the INT_OFF macro. (The test for iflag is just |
| 326 | * defensive programming.) |
| 327 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 328 | static void raise_interrupt(void) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 329 | static void |
| 330 | raise_interrupt(void) |
| 331 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 332 | int ex_type; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 333 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 334 | pending_int = 0; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 335 | /* Signal is not automatically unmasked after it is raised, |
| 336 | * do it ourself - unmask all signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 337 | sigprocmask_allsigs(SIG_UNBLOCK); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 338 | /* pending_sig = 0; - now done in signal_handler() */ |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 339 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 340 | ex_type = EXSIG; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 341 | if (gotsig[SIGINT - 1] && !trap[SIGINT]) { |
| 342 | if (!(rootshell && iflag)) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 343 | /* Kill ourself with SIGINT */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 344 | signal(SIGINT, SIG_DFL); |
| 345 | raise(SIGINT); |
| 346 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 347 | ex_type = EXINT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 348 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 349 | raise_exception(ex_type); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 350 | /* NOTREACHED */ |
| 351 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 352 | #if DEBUG |
| 353 | #define raise_interrupt() do { \ |
| 354 | TRACE(("raising interrupt on line %d\n", __LINE__)); \ |
| 355 | raise_interrupt(); \ |
| 356 | } while (0) |
| 357 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 358 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 359 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 360 | int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 361 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 362 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 363 | if (--suppress_int == 0 && pending_int) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 364 | raise_interrupt(); |
| 365 | } |
| 366 | } |
| 367 | #define INT_ON int_on() |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 368 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 369 | force_int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 370 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 371 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 372 | suppress_int = 0; |
| 373 | if (pending_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 374 | raise_interrupt(); |
| 375 | } |
| 376 | #define FORCE_INT_ON force_int_on() |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 377 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 378 | #define SAVE_INT(v) ((v) = suppress_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 379 | |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 380 | #define RESTORE_INT(v) do { \ |
| 381 | xbarrier(); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 382 | suppress_int = (v); \ |
| 383 | if (suppress_int == 0 && pending_int) \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 384 | raise_interrupt(); \ |
| 385 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 386 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 387 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 388 | /* ============ Stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 389 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 390 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 391 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 392 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 393 | INT_OFF; |
| 394 | fputs(p, file); |
| 395 | INT_ON; |
| 396 | } |
| 397 | |
| 398 | static void |
| 399 | flush_stdout_stderr(void) |
| 400 | { |
| 401 | INT_OFF; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 402 | fflush_all(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 403 | INT_ON; |
| 404 | } |
| 405 | |
| 406 | static void |
| 407 | outcslow(int c, FILE *dest) |
| 408 | { |
| 409 | INT_OFF; |
| 410 | putc(c, dest); |
| 411 | fflush(dest); |
| 412 | INT_ON; |
| 413 | } |
| 414 | |
| 415 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 416 | static int |
| 417 | out1fmt(const char *fmt, ...) |
| 418 | { |
| 419 | va_list ap; |
| 420 | int r; |
| 421 | |
| 422 | INT_OFF; |
| 423 | va_start(ap, fmt); |
| 424 | r = vprintf(fmt, ap); |
| 425 | va_end(ap); |
| 426 | INT_ON; |
| 427 | return r; |
| 428 | } |
| 429 | |
| 430 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 431 | static int |
| 432 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 433 | { |
| 434 | va_list ap; |
| 435 | int ret; |
| 436 | |
| 437 | va_start(ap, fmt); |
| 438 | INT_OFF; |
| 439 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 440 | va_end(ap); |
| 441 | INT_ON; |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | static void |
| 446 | out1str(const char *p) |
| 447 | { |
| 448 | outstr(p, stdout); |
| 449 | } |
| 450 | |
| 451 | static void |
| 452 | out2str(const char *p) |
| 453 | { |
| 454 | outstr(p, stderr); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 455 | flush_stdout_stderr(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 459 | /* ============ Parser structures */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 460 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 461 | /* control characters in argument strings */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 462 | #define CTL_FIRST CTLESC |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 463 | #define CTLESC ((unsigned char)'\201') /* escape next character */ |
| 464 | #define CTLVAR ((unsigned char)'\202') /* variable defn */ |
| 465 | #define CTLENDVAR ((unsigned char)'\203') |
| 466 | #define CTLBACKQ ((unsigned char)'\204') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 467 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ |
| 468 | /* CTLBACKQ | CTLQUOTE == '\205' */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 469 | #define CTLARI ((unsigned char)'\206') /* arithmetic expression */ |
| 470 | #define CTLENDARI ((unsigned char)'\207') |
| 471 | #define CTLQUOTEMARK ((unsigned char)'\210') |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 472 | #define CTL_LAST CTLQUOTEMARK |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 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 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 643 | /* |
| 644 | * NODE_EOF is returned by parsecmd when it encounters an end of file. |
| 645 | * It must be distinct from NULL. |
| 646 | */ |
| 647 | #define NODE_EOF ((union node *) -1L) |
| 648 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 649 | struct nodelist { |
| 650 | struct nodelist *next; |
| 651 | union node *n; |
| 652 | }; |
| 653 | |
| 654 | struct funcnode { |
| 655 | int count; |
| 656 | union node n; |
| 657 | }; |
| 658 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 659 | /* |
| 660 | * Free a parse tree. |
| 661 | */ |
| 662 | static void |
| 663 | freefunc(struct funcnode *f) |
| 664 | { |
| 665 | if (f && --f->count < 0) |
| 666 | free(f); |
| 667 | } |
| 668 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 669 | |
| 670 | /* ============ Debugging output */ |
| 671 | |
| 672 | #if DEBUG |
| 673 | |
| 674 | static FILE *tracefile; |
| 675 | |
| 676 | static void |
| 677 | trace_printf(const char *fmt, ...) |
| 678 | { |
| 679 | va_list va; |
| 680 | |
| 681 | if (debug != 1) |
| 682 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 683 | if (DEBUG_TIME) |
| 684 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 685 | if (DEBUG_PID) |
| 686 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 687 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 688 | fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 689 | va_start(va, fmt); |
| 690 | vfprintf(tracefile, fmt, va); |
| 691 | va_end(va); |
| 692 | } |
| 693 | |
| 694 | static void |
| 695 | trace_vprintf(const char *fmt, va_list va) |
| 696 | { |
| 697 | if (debug != 1) |
| 698 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 699 | if (DEBUG_TIME) |
| 700 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 701 | if (DEBUG_PID) |
| 702 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 703 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 704 | fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 705 | vfprintf(tracefile, fmt, va); |
| 706 | } |
| 707 | |
| 708 | static void |
| 709 | trace_puts(const char *s) |
| 710 | { |
| 711 | if (debug != 1) |
| 712 | return; |
| 713 | fputs(s, tracefile); |
| 714 | } |
| 715 | |
| 716 | static void |
| 717 | trace_puts_quoted(char *s) |
| 718 | { |
| 719 | char *p; |
| 720 | char c; |
| 721 | |
| 722 | if (debug != 1) |
| 723 | return; |
| 724 | putc('"', tracefile); |
| 725 | for (p = s; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 726 | switch ((unsigned char)*p) { |
| 727 | case '\n': c = 'n'; goto backslash; |
| 728 | case '\t': c = 't'; goto backslash; |
| 729 | case '\r': c = 'r'; goto backslash; |
| 730 | case '\"': c = '\"'; goto backslash; |
| 731 | case '\\': c = '\\'; goto backslash; |
| 732 | case CTLESC: c = 'e'; goto backslash; |
| 733 | case CTLVAR: c = 'v'; goto backslash; |
| 734 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash; |
| 735 | case CTLBACKQ: c = 'q'; goto backslash; |
| 736 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 737 | backslash: |
| 738 | putc('\\', tracefile); |
| 739 | putc(c, tracefile); |
| 740 | break; |
| 741 | default: |
| 742 | if (*p >= ' ' && *p <= '~') |
| 743 | putc(*p, tracefile); |
| 744 | else { |
| 745 | putc('\\', tracefile); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 746 | putc((*p >> 6) & 03, tracefile); |
| 747 | putc((*p >> 3) & 07, tracefile); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 748 | putc(*p & 07, tracefile); |
| 749 | } |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | putc('"', tracefile); |
| 754 | } |
| 755 | |
| 756 | static void |
| 757 | trace_puts_args(char **ap) |
| 758 | { |
| 759 | if (debug != 1) |
| 760 | return; |
| 761 | if (!*ap) |
| 762 | return; |
| 763 | while (1) { |
| 764 | trace_puts_quoted(*ap); |
| 765 | if (!*++ap) { |
| 766 | putc('\n', tracefile); |
| 767 | break; |
| 768 | } |
| 769 | putc(' ', tracefile); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | static void |
| 774 | opentrace(void) |
| 775 | { |
| 776 | char s[100]; |
| 777 | #ifdef O_APPEND |
| 778 | int flags; |
| 779 | #endif |
| 780 | |
| 781 | if (debug != 1) { |
| 782 | if (tracefile) |
| 783 | fflush(tracefile); |
| 784 | /* leave open because libedit might be using it */ |
| 785 | return; |
| 786 | } |
| 787 | strcpy(s, "./trace"); |
| 788 | if (tracefile) { |
| 789 | if (!freopen(s, "a", tracefile)) { |
| 790 | fprintf(stderr, "Can't re-open %s\n", s); |
| 791 | debug = 0; |
| 792 | return; |
| 793 | } |
| 794 | } else { |
| 795 | tracefile = fopen(s, "a"); |
| 796 | if (tracefile == NULL) { |
| 797 | fprintf(stderr, "Can't open %s\n", s); |
| 798 | debug = 0; |
| 799 | return; |
| 800 | } |
| 801 | } |
| 802 | #ifdef O_APPEND |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 803 | flags = fcntl(fileno(tracefile), F_GETFL); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 804 | if (flags >= 0) |
| 805 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 806 | #endif |
| 807 | setlinebuf(tracefile); |
| 808 | fputs("\nTracing started.\n", tracefile); |
| 809 | } |
| 810 | |
| 811 | static void |
| 812 | indent(int amount, char *pfx, FILE *fp) |
| 813 | { |
| 814 | int i; |
| 815 | |
| 816 | for (i = 0; i < amount; i++) { |
| 817 | if (pfx && i == amount - 1) |
| 818 | fputs(pfx, fp); |
| 819 | putc('\t', fp); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | /* little circular references here... */ |
| 824 | static void shtree(union node *n, int ind, char *pfx, FILE *fp); |
| 825 | |
| 826 | static void |
| 827 | sharg(union node *arg, FILE *fp) |
| 828 | { |
| 829 | char *p; |
| 830 | struct nodelist *bqlist; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 831 | unsigned char subtype; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 832 | |
| 833 | if (arg->type != NARG) { |
| 834 | out1fmt("<node type %d>\n", arg->type); |
| 835 | abort(); |
| 836 | } |
| 837 | bqlist = arg->narg.backquote; |
| 838 | for (p = arg->narg.text; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 839 | switch ((unsigned char)*p) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 840 | case CTLESC: |
| 841 | putc(*++p, fp); |
| 842 | break; |
| 843 | case CTLVAR: |
| 844 | putc('$', fp); |
| 845 | putc('{', fp); |
| 846 | subtype = *++p; |
| 847 | if (subtype == VSLENGTH) |
| 848 | putc('#', fp); |
| 849 | |
| 850 | while (*p != '=') |
| 851 | putc(*p++, fp); |
| 852 | |
| 853 | if (subtype & VSNUL) |
| 854 | putc(':', fp); |
| 855 | |
| 856 | switch (subtype & VSTYPE) { |
| 857 | case VSNORMAL: |
| 858 | putc('}', fp); |
| 859 | break; |
| 860 | case VSMINUS: |
| 861 | putc('-', fp); |
| 862 | break; |
| 863 | case VSPLUS: |
| 864 | putc('+', fp); |
| 865 | break; |
| 866 | case VSQUESTION: |
| 867 | putc('?', fp); |
| 868 | break; |
| 869 | case VSASSIGN: |
| 870 | putc('=', fp); |
| 871 | break; |
| 872 | case VSTRIMLEFT: |
| 873 | putc('#', fp); |
| 874 | break; |
| 875 | case VSTRIMLEFTMAX: |
| 876 | putc('#', fp); |
| 877 | putc('#', fp); |
| 878 | break; |
| 879 | case VSTRIMRIGHT: |
| 880 | putc('%', fp); |
| 881 | break; |
| 882 | case VSTRIMRIGHTMAX: |
| 883 | putc('%', fp); |
| 884 | putc('%', fp); |
| 885 | break; |
| 886 | case VSLENGTH: |
| 887 | break; |
| 888 | default: |
| 889 | out1fmt("<subtype %d>", subtype); |
| 890 | } |
| 891 | break; |
| 892 | case CTLENDVAR: |
| 893 | putc('}', fp); |
| 894 | break; |
| 895 | case CTLBACKQ: |
| 896 | case CTLBACKQ|CTLQUOTE: |
| 897 | putc('$', fp); |
| 898 | putc('(', fp); |
| 899 | shtree(bqlist->n, -1, NULL, fp); |
| 900 | putc(')', fp); |
| 901 | break; |
| 902 | default: |
| 903 | putc(*p, fp); |
| 904 | break; |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 909 | static void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 910 | shcmd(union node *cmd, FILE *fp) |
| 911 | { |
| 912 | union node *np; |
| 913 | int first; |
| 914 | const char *s; |
| 915 | int dftfd; |
| 916 | |
| 917 | first = 1; |
| 918 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 919 | if (!first) |
| 920 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 921 | sharg(np, fp); |
| 922 | first = 0; |
| 923 | } |
| 924 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 925 | if (!first) |
| 926 | putc(' ', fp); |
| 927 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 928 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 929 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 930 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 931 | case NAPPEND: s = ">>"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 932 | #if ENABLE_ASH_BASH_COMPAT |
| 933 | case NTO2: |
| 934 | #endif |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 935 | case NTOFD: s = ">&"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 936 | case NFROM: s = "<"; break; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 937 | case NFROMFD: s = "<&"; break; |
| 938 | case NFROMTO: s = "<>"; break; |
| 939 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 940 | } |
| 941 | if (np->nfile.fd != dftfd) |
| 942 | fprintf(fp, "%d", np->nfile.fd); |
| 943 | fputs(s, fp); |
| 944 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 945 | fprintf(fp, "%d", np->ndup.dupfd); |
| 946 | } else { |
| 947 | sharg(np->nfile.fname, fp); |
| 948 | } |
| 949 | first = 0; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | static void |
| 954 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 955 | { |
| 956 | struct nodelist *lp; |
| 957 | const char *s; |
| 958 | |
| 959 | if (n == NULL) |
| 960 | return; |
| 961 | |
| 962 | indent(ind, pfx, fp); |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 963 | |
| 964 | if (n == NODE_EOF) { |
| 965 | fputs("<EOF>", fp); |
| 966 | return; |
| 967 | } |
| 968 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 969 | switch (n->type) { |
| 970 | case NSEMI: |
| 971 | s = "; "; |
| 972 | goto binop; |
| 973 | case NAND: |
| 974 | s = " && "; |
| 975 | goto binop; |
| 976 | case NOR: |
| 977 | s = " || "; |
| 978 | binop: |
| 979 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 980 | /* if (ind < 0) */ |
| 981 | fputs(s, fp); |
| 982 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 983 | break; |
| 984 | case NCMD: |
| 985 | shcmd(n, fp); |
| 986 | if (ind >= 0) |
| 987 | putc('\n', fp); |
| 988 | break; |
| 989 | case NPIPE: |
| 990 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 991 | shtree(lp->n, 0, NULL, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 992 | if (lp->next) |
| 993 | fputs(" | ", fp); |
| 994 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 995 | if (n->npipe.pipe_backgnd) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 996 | fputs(" &", fp); |
| 997 | if (ind >= 0) |
| 998 | putc('\n', fp); |
| 999 | break; |
| 1000 | default: |
| 1001 | fprintf(fp, "<node type %d>", n->type); |
| 1002 | if (ind >= 0) |
| 1003 | putc('\n', fp); |
| 1004 | break; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | static void |
| 1009 | showtree(union node *n) |
| 1010 | { |
| 1011 | trace_puts("showtree called\n"); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 1012 | shtree(n, 1, NULL, stderr); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1015 | #endif /* DEBUG */ |
| 1016 | |
| 1017 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1018 | /* ============ Parser data */ |
| 1019 | |
| 1020 | /* |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1021 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 1022 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1023 | struct strlist { |
| 1024 | struct strlist *next; |
| 1025 | char *text; |
| 1026 | }; |
| 1027 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1028 | struct alias; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1029 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1030 | struct strpush { |
| 1031 | struct strpush *prev; /* preceding string on stack */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1032 | char *prev_string; |
| 1033 | int prev_left_in_line; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1034 | #if ENABLE_ASH_ALIAS |
| 1035 | struct alias *ap; /* if push was associated with an alias */ |
| 1036 | #endif |
| 1037 | char *string; /* remember the string since it may change */ |
| 1038 | }; |
| 1039 | |
| 1040 | struct parsefile { |
| 1041 | struct parsefile *prev; /* preceding file on stack */ |
| 1042 | int linno; /* current line */ |
| 1043 | int fd; /* file descriptor (or -1 if string) */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1044 | int left_in_line; /* number of chars left in this line */ |
| 1045 | int left_in_buffer; /* number of chars left in this buffer past the line */ |
| 1046 | char *next_to_pgetc; /* next char in buffer */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1047 | char *buf; /* input buffer */ |
| 1048 | struct strpush *strpush; /* for pushing strings at this level */ |
| 1049 | struct strpush basestrpush; /* so pushing one is fast */ |
| 1050 | }; |
| 1051 | |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1052 | static struct parsefile basepf; /* top level input file */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1053 | static struct parsefile *g_parsefile = &basepf; /* current input file */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1054 | static int startlinno; /* line # where last token started */ |
| 1055 | static char *commandname; /* currently executing command */ |
| 1056 | static struct strlist *cmdenviron; /* environment for builtin command */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1057 | static uint8_t exitstatus; /* exit status of last command */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1058 | |
| 1059 | |
| 1060 | /* ============ Message printing */ |
| 1061 | |
| 1062 | static void |
| 1063 | ash_vmsg(const char *msg, va_list ap) |
| 1064 | { |
| 1065 | fprintf(stderr, "%s: ", arg0); |
| 1066 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1067 | if (strcmp(arg0, commandname)) |
| 1068 | fprintf(stderr, "%s: ", commandname); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1069 | if (!iflag || g_parsefile->fd) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1070 | fprintf(stderr, "line %d: ", startlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1071 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1072 | vfprintf(stderr, msg, ap); |
| 1073 | outcslow('\n', stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1074 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1075 | |
| 1076 | /* |
| 1077 | * Exverror is called to raise the error exception. If the second argument |
| 1078 | * is not NULL then error prints an error message using printf style |
| 1079 | * formatting. It then raises the error exception. |
| 1080 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1081 | static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1082 | static void |
| 1083 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1084 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1085 | #if DEBUG |
| 1086 | if (msg) { |
| 1087 | TRACE(("ash_vmsg_and_raise(%d, \"", cond)); |
| 1088 | TRACEV((msg, ap)); |
| 1089 | TRACE(("\") pid=%d\n", getpid())); |
| 1090 | } else |
| 1091 | TRACE(("ash_vmsg_and_raise(%d, NULL) pid=%d\n", cond, getpid())); |
| 1092 | if (msg) |
| 1093 | #endif |
| 1094 | ash_vmsg(msg, ap); |
| 1095 | |
| 1096 | flush_stdout_stderr(); |
| 1097 | raise_exception(cond); |
| 1098 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1099 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1100 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1101 | static void ash_msg_and_raise_error(const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1102 | static void |
| 1103 | ash_msg_and_raise_error(const char *msg, ...) |
| 1104 | { |
| 1105 | va_list ap; |
| 1106 | |
| 1107 | va_start(ap, msg); |
| 1108 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1109 | /* NOTREACHED */ |
| 1110 | va_end(ap); |
| 1111 | } |
| 1112 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1113 | static void raise_error_syntax(const char *) NORETURN; |
| 1114 | static void |
| 1115 | raise_error_syntax(const char *msg) |
| 1116 | { |
| 1117 | ash_msg_and_raise_error("syntax error: %s", msg); |
| 1118 | /* NOTREACHED */ |
| 1119 | } |
| 1120 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1121 | static void ash_msg_and_raise(int, const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1122 | static void |
| 1123 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1124 | { |
| 1125 | va_list ap; |
| 1126 | |
| 1127 | va_start(ap, msg); |
| 1128 | ash_vmsg_and_raise(cond, msg, ap); |
| 1129 | /* NOTREACHED */ |
| 1130 | va_end(ap); |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * error/warning routines for external builtins |
| 1135 | */ |
| 1136 | static void |
| 1137 | ash_msg(const char *fmt, ...) |
| 1138 | { |
| 1139 | va_list ap; |
| 1140 | |
| 1141 | va_start(ap, fmt); |
| 1142 | ash_vmsg(fmt, ap); |
| 1143 | va_end(ap); |
| 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * Return a string describing an error. The returned string may be a |
| 1148 | * pointer to a static buffer that will be overwritten on the next call. |
| 1149 | * Action describes the operation that got the error. |
| 1150 | */ |
| 1151 | static const char * |
| 1152 | errmsg(int e, const char *em) |
| 1153 | { |
| 1154 | if (e == ENOENT || e == ENOTDIR) { |
| 1155 | return em; |
| 1156 | } |
| 1157 | return strerror(e); |
| 1158 | } |
| 1159 | |
| 1160 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1161 | /* ============ Memory allocation */ |
| 1162 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1163 | #if 0 |
| 1164 | /* I consider these wrappers nearly useless: |
| 1165 | * ok, they return you to nearest exception handler, but |
| 1166 | * how much memory do you leak in the process, making |
| 1167 | * memory starvation worse? |
| 1168 | */ |
| 1169 | static void * |
| 1170 | ckrealloc(void * p, size_t nbytes) |
| 1171 | { |
| 1172 | p = realloc(p, nbytes); |
| 1173 | if (!p) |
| 1174 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1175 | return p; |
| 1176 | } |
| 1177 | |
| 1178 | static void * |
| 1179 | ckmalloc(size_t nbytes) |
| 1180 | { |
| 1181 | return ckrealloc(NULL, nbytes); |
| 1182 | } |
| 1183 | |
| 1184 | static void * |
| 1185 | ckzalloc(size_t nbytes) |
| 1186 | { |
| 1187 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1188 | } |
| 1189 | |
| 1190 | static char * |
| 1191 | ckstrdup(const char *s) |
| 1192 | { |
| 1193 | char *p = strdup(s); |
| 1194 | if (!p) |
| 1195 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1196 | return p; |
| 1197 | } |
| 1198 | #else |
| 1199 | /* Using bbox equivalents. They exit if out of memory */ |
| 1200 | # define ckrealloc xrealloc |
| 1201 | # define ckmalloc xmalloc |
| 1202 | # define ckzalloc xzalloc |
| 1203 | # define ckstrdup xstrdup |
| 1204 | #endif |
| 1205 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1206 | /* |
| 1207 | * It appears that grabstackstr() will barf with such alignments |
| 1208 | * because stalloc() will return a string allocated in a new stackblock. |
| 1209 | */ |
| 1210 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1211 | enum { |
| 1212 | /* Most machines require the value returned from malloc to be aligned |
| 1213 | * in some way. The following macro will get this right |
| 1214 | * on many machines. */ |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 1215 | SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1, |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1216 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1217 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1218 | }; |
| 1219 | |
| 1220 | struct stack_block { |
| 1221 | struct stack_block *prev; |
| 1222 | char space[MINSIZE]; |
| 1223 | }; |
| 1224 | |
| 1225 | struct stackmark { |
| 1226 | struct stack_block *stackp; |
| 1227 | char *stacknxt; |
| 1228 | size_t stacknleft; |
| 1229 | struct stackmark *marknext; |
| 1230 | }; |
| 1231 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1232 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1233 | struct globals_memstack { |
| 1234 | struct stack_block *g_stackp; // = &stackbase; |
| 1235 | struct stackmark *markp; |
| 1236 | char *g_stacknxt; // = stackbase.space; |
| 1237 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1238 | size_t g_stacknleft; // = MINSIZE; |
| 1239 | int herefd; // = -1; |
| 1240 | struct stack_block stackbase; |
| 1241 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1242 | extern struct globals_memstack *const ash_ptr_to_globals_memstack; |
| 1243 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1244 | #define g_stackp (G_memstack.g_stackp ) |
| 1245 | #define markp (G_memstack.markp ) |
| 1246 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1247 | #define sstrend (G_memstack.sstrend ) |
| 1248 | #define g_stacknleft (G_memstack.g_stacknleft) |
| 1249 | #define herefd (G_memstack.herefd ) |
| 1250 | #define stackbase (G_memstack.stackbase ) |
| 1251 | #define INIT_G_memstack() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1252 | (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \ |
| 1253 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1254 | g_stackp = &stackbase; \ |
| 1255 | g_stacknxt = stackbase.space; \ |
| 1256 | g_stacknleft = MINSIZE; \ |
| 1257 | sstrend = stackbase.space + MINSIZE; \ |
| 1258 | herefd = -1; \ |
| 1259 | } while (0) |
| 1260 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1261 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1262 | #define stackblock() ((void *)g_stacknxt) |
| 1263 | #define stackblocksize() g_stacknleft |
| 1264 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1265 | /* |
| 1266 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1267 | * to make this more efficient, and also to avoid all sorts of exception |
| 1268 | * handling code to handle interrupts in the middle of a parse. |
| 1269 | * |
| 1270 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1271 | * well. |
| 1272 | */ |
| 1273 | static void * |
| 1274 | stalloc(size_t nbytes) |
| 1275 | { |
| 1276 | char *p; |
| 1277 | size_t aligned; |
| 1278 | |
| 1279 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1280 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1281 | size_t len; |
| 1282 | size_t blocksize; |
| 1283 | struct stack_block *sp; |
| 1284 | |
| 1285 | blocksize = aligned; |
| 1286 | if (blocksize < MINSIZE) |
| 1287 | blocksize = MINSIZE; |
| 1288 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1289 | if (len < blocksize) |
| 1290 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1291 | INT_OFF; |
| 1292 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1293 | sp->prev = g_stackp; |
| 1294 | g_stacknxt = sp->space; |
| 1295 | g_stacknleft = blocksize; |
| 1296 | sstrend = g_stacknxt + blocksize; |
| 1297 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1298 | INT_ON; |
| 1299 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1300 | p = g_stacknxt; |
| 1301 | g_stacknxt += aligned; |
| 1302 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1303 | return p; |
| 1304 | } |
| 1305 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1306 | static void * |
| 1307 | stzalloc(size_t nbytes) |
| 1308 | { |
| 1309 | return memset(stalloc(nbytes), 0, nbytes); |
| 1310 | } |
| 1311 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1312 | static void |
| 1313 | stunalloc(void *p) |
| 1314 | { |
| 1315 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1316 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1317 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1318 | abort(); |
| 1319 | } |
| 1320 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1321 | g_stacknleft += g_stacknxt - (char *)p; |
| 1322 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1325 | /* |
| 1326 | * Like strdup but works with the ash stack. |
| 1327 | */ |
| 1328 | static char * |
| 1329 | ststrdup(const char *p) |
| 1330 | { |
| 1331 | size_t len = strlen(p) + 1; |
| 1332 | return memcpy(stalloc(len), p, len); |
| 1333 | } |
| 1334 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1335 | static void |
| 1336 | setstackmark(struct stackmark *mark) |
| 1337 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1338 | mark->stackp = g_stackp; |
| 1339 | mark->stacknxt = g_stacknxt; |
| 1340 | mark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1341 | mark->marknext = markp; |
| 1342 | markp = mark; |
| 1343 | } |
| 1344 | |
| 1345 | static void |
| 1346 | popstackmark(struct stackmark *mark) |
| 1347 | { |
| 1348 | struct stack_block *sp; |
| 1349 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1350 | if (!mark->stackp) |
| 1351 | return; |
| 1352 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1353 | INT_OFF; |
| 1354 | markp = mark->marknext; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1355 | while (g_stackp != mark->stackp) { |
| 1356 | sp = g_stackp; |
| 1357 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1358 | free(sp); |
| 1359 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1360 | g_stacknxt = mark->stacknxt; |
| 1361 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1362 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1363 | INT_ON; |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | * When the parser reads in a string, it wants to stick the string on the |
| 1368 | * stack and only adjust the stack pointer when it knows how big the |
| 1369 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1370 | * of space on top of the stack and stackblocklen returns the length of |
| 1371 | * this block. Growstackblock will grow this space by at least one byte, |
| 1372 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1373 | * part of the block that has been used. |
| 1374 | */ |
| 1375 | static void |
| 1376 | growstackblock(void) |
| 1377 | { |
| 1378 | size_t newlen; |
| 1379 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1380 | newlen = g_stacknleft * 2; |
| 1381 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1382 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1383 | if (newlen < 128) |
| 1384 | newlen += 128; |
| 1385 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1386 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1387 | struct stack_block *oldstackp; |
| 1388 | struct stackmark *xmark; |
| 1389 | struct stack_block *sp; |
| 1390 | struct stack_block *prevstackp; |
| 1391 | size_t grosslen; |
| 1392 | |
| 1393 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1394 | oldstackp = g_stackp; |
| 1395 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1396 | prevstackp = sp->prev; |
| 1397 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1398 | sp = ckrealloc(sp, grosslen); |
| 1399 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1400 | g_stackp = sp; |
| 1401 | g_stacknxt = sp->space; |
| 1402 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1403 | sstrend = sp->space + newlen; |
| 1404 | |
| 1405 | /* |
| 1406 | * Stack marks pointing to the start of the old block |
| 1407 | * must be relocated to point to the new block |
| 1408 | */ |
| 1409 | xmark = markp; |
| 1410 | while (xmark != NULL && xmark->stackp == oldstackp) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1411 | xmark->stackp = g_stackp; |
| 1412 | xmark->stacknxt = g_stacknxt; |
| 1413 | xmark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1414 | xmark = xmark->marknext; |
| 1415 | } |
| 1416 | INT_ON; |
| 1417 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1418 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1419 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1420 | char *p = stalloc(newlen); |
| 1421 | |
| 1422 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1423 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1424 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | static void |
| 1429 | grabstackblock(size_t len) |
| 1430 | { |
| 1431 | len = SHELL_ALIGN(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1432 | g_stacknxt += len; |
| 1433 | g_stacknleft -= len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | /* |
| 1437 | * The following routines are somewhat easier to use than the above. |
| 1438 | * The user declares a variable of type STACKSTR, which may be declared |
| 1439 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1440 | * the user uses the macro STPUTC to add characters to the string. In |
| 1441 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1442 | * grown as necessary. When the user is done, she can just leave the |
| 1443 | * string there and refer to it using stackblock(). Or she can allocate |
| 1444 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1445 | * someone else to use the stack temporarily and then continue to grow |
| 1446 | * the string, the user should use grabstack to allocate the space, and |
| 1447 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1448 | * |
| 1449 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1450 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1451 | * is space for at least one character. |
| 1452 | */ |
| 1453 | static void * |
| 1454 | growstackstr(void) |
| 1455 | { |
| 1456 | size_t len = stackblocksize(); |
| 1457 | if (herefd >= 0 && len >= 1024) { |
| 1458 | full_write(herefd, stackblock(), len); |
| 1459 | return stackblock(); |
| 1460 | } |
| 1461 | growstackblock(); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1462 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * Called from CHECKSTRSPACE. |
| 1467 | */ |
| 1468 | static char * |
| 1469 | makestrspace(size_t newlen, char *p) |
| 1470 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1471 | size_t len = p - g_stacknxt; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1472 | size_t size = stackblocksize(); |
| 1473 | |
| 1474 | for (;;) { |
| 1475 | size_t nleft; |
| 1476 | |
| 1477 | size = stackblocksize(); |
| 1478 | nleft = size - len; |
| 1479 | if (nleft >= newlen) |
| 1480 | break; |
| 1481 | growstackblock(); |
| 1482 | } |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1483 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | static char * |
| 1487 | stack_nputstr(const char *s, size_t n, char *p) |
| 1488 | { |
| 1489 | p = makestrspace(n, p); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1490 | p = (char *)memcpy(p, s, n) + n; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1491 | return p; |
| 1492 | } |
| 1493 | |
| 1494 | static char * |
| 1495 | stack_putstr(const char *s, char *p) |
| 1496 | { |
| 1497 | return stack_nputstr(s, strlen(s), p); |
| 1498 | } |
| 1499 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1500 | static char * |
| 1501 | _STPUTC(int c, char *p) |
| 1502 | { |
| 1503 | if (p == sstrend) |
| 1504 | p = growstackstr(); |
| 1505 | *p++ = c; |
| 1506 | return p; |
| 1507 | } |
| 1508 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1509 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1510 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1511 | #define CHECKSTRSPACE(n, p) do { \ |
| 1512 | char *q = (p); \ |
| 1513 | size_t l = (n); \ |
| 1514 | size_t m = sstrend - q; \ |
| 1515 | if (l > m) \ |
| 1516 | (p) = makestrspace(l, q); \ |
| 1517 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1518 | #define USTPUTC(c, p) (*(p)++ = (c)) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1519 | #define STACKSTRNUL(p) do { \ |
| 1520 | if ((p) == sstrend) \ |
| 1521 | (p) = growstackstr(); \ |
| 1522 | *(p) = '\0'; \ |
| 1523 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1524 | #define STUNPUTC(p) (--(p)) |
| 1525 | #define STTOPC(p) ((p)[-1]) |
| 1526 | #define STADJUST(amount, p) ((p) += (amount)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1527 | |
| 1528 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1529 | #define ungrabstackstr(s, p) stunalloc(s) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1530 | #define stackstrend() ((void *)sstrend) |
| 1531 | |
| 1532 | |
| 1533 | /* ============ String helpers */ |
| 1534 | |
| 1535 | /* |
| 1536 | * prefix -- see if pfx is a prefix of string. |
| 1537 | */ |
| 1538 | static char * |
| 1539 | prefix(const char *string, const char *pfx) |
| 1540 | { |
| 1541 | while (*pfx) { |
| 1542 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1543 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1544 | } |
| 1545 | return (char *) string; |
| 1546 | } |
| 1547 | |
| 1548 | /* |
| 1549 | * Check for a valid number. This should be elsewhere. |
| 1550 | */ |
| 1551 | static int |
| 1552 | is_number(const char *p) |
| 1553 | { |
| 1554 | do { |
| 1555 | if (!isdigit(*p)) |
| 1556 | return 0; |
| 1557 | } while (*++p != '\0'); |
| 1558 | return 1; |
| 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * Convert a string of digits to an integer, printing an error message on |
| 1563 | * failure. |
| 1564 | */ |
| 1565 | static int |
| 1566 | number(const char *s) |
| 1567 | { |
| 1568 | if (!is_number(s)) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1569 | ash_msg_and_raise_error(msg_illnum, s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1570 | return atoi(s); |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * Produce a possibly single quoted string suitable as input to the shell. |
| 1575 | * The return string is allocated on the stack. |
| 1576 | */ |
| 1577 | static char * |
| 1578 | single_quote(const char *s) |
| 1579 | { |
| 1580 | char *p; |
| 1581 | |
| 1582 | STARTSTACKSTR(p); |
| 1583 | |
| 1584 | do { |
| 1585 | char *q; |
| 1586 | size_t len; |
| 1587 | |
| 1588 | len = strchrnul(s, '\'') - s; |
| 1589 | |
| 1590 | q = p = makestrspace(len + 3, p); |
| 1591 | |
| 1592 | *q++ = '\''; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1593 | q = (char *)memcpy(q, s, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1594 | *q++ = '\''; |
| 1595 | s += len; |
| 1596 | |
| 1597 | STADJUST(q - p, p); |
| 1598 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1599 | if (*s != '\'') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1600 | break; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1601 | len = 0; |
| 1602 | do len++; while (*++s == '\''); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1603 | |
| 1604 | q = p = makestrspace(len + 3, p); |
| 1605 | |
| 1606 | *q++ = '"'; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1607 | q = (char *)memcpy(q, s - len, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1608 | *q++ = '"'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1609 | |
| 1610 | STADJUST(q - p, p); |
| 1611 | } while (*s); |
| 1612 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1613 | USTPUTC('\0', p); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1614 | |
| 1615 | return stackblock(); |
| 1616 | } |
| 1617 | |
| 1618 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1619 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1620 | |
| 1621 | static char **argptr; /* argument list for builtin commands */ |
| 1622 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1623 | static char *optptr; /* used by nextopt */ |
| 1624 | |
| 1625 | /* |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1626 | * XXX - should get rid of. Have all builtins use getopt(3). |
| 1627 | * The library getopt must have the BSD extension static variable |
| 1628 | * "optreset", otherwise it can't be used within the shell safely. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1629 | * |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1630 | * Standard option processing (a la getopt) for builtin routines. |
| 1631 | * The only argument that is passed to nextopt is the option string; |
| 1632 | * the other arguments are unnecessary. It returns the character, |
| 1633 | * or '\0' on end of input. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1634 | */ |
| 1635 | static int |
| 1636 | nextopt(const char *optstring) |
| 1637 | { |
| 1638 | char *p; |
| 1639 | const char *q; |
| 1640 | char c; |
| 1641 | |
| 1642 | p = optptr; |
| 1643 | if (p == NULL || *p == '\0') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1644 | /* We ate entire "-param", take next one */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1645 | p = *argptr; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1646 | if (p == NULL) |
| 1647 | return '\0'; |
| 1648 | if (*p != '-') |
| 1649 | return '\0'; |
| 1650 | if (*++p == '\0') /* just "-" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1651 | return '\0'; |
| 1652 | argptr++; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1653 | if (LONE_DASH(p)) /* "--" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1654 | return '\0'; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1655 | /* p => next "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1656 | } |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1657 | /* p => some option char in the middle of a "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1658 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 1659 | for (q = optstring; *q != c;) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1660 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1661 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1662 | if (*++q == ':') |
| 1663 | q++; |
| 1664 | } |
| 1665 | if (*++q == ':') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1666 | if (*p == '\0') { |
| 1667 | p = *argptr++; |
| 1668 | if (p == NULL) |
| 1669 | ash_msg_and_raise_error("no arg for -%c option", c); |
| 1670 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1671 | optionarg = p; |
| 1672 | p = NULL; |
| 1673 | } |
| 1674 | optptr = p; |
| 1675 | return c; |
| 1676 | } |
| 1677 | |
| 1678 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1679 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1680 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1681 | /* |
| 1682 | * The parsefile structure pointed to by the global variable parsefile |
| 1683 | * contains information about the current file being read. |
| 1684 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1685 | struct shparam { |
| 1686 | int nparam; /* # of positional parameters (without $0) */ |
| 1687 | #if ENABLE_ASH_GETOPTS |
| 1688 | int optind; /* next parameter to be processed by getopts */ |
| 1689 | int optoff; /* used by getopts */ |
| 1690 | #endif |
| 1691 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 1692 | char **p; /* parameter list */ |
| 1693 | }; |
| 1694 | |
| 1695 | /* |
| 1696 | * Free the list of positional parameters. |
| 1697 | */ |
| 1698 | static void |
| 1699 | freeparam(volatile struct shparam *param) |
| 1700 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1701 | if (param->malloced) { |
Denis Vlasenko | 3177ba0 | 2008-07-13 20:39:23 +0000 | [diff] [blame] | 1702 | char **ap, **ap1; |
| 1703 | ap = ap1 = param->p; |
| 1704 | while (*ap) |
| 1705 | free(*ap++); |
| 1706 | free(ap1); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1711 | static void FAST_FUNC getoptsreset(const char *value); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1712 | #endif |
| 1713 | |
| 1714 | struct var { |
| 1715 | struct var *next; /* next entry in hash list */ |
| 1716 | int flags; /* flags are defined above */ |
| 1717 | const char *text; /* name=value */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1718 | void (*func)(const char *) FAST_FUNC; /* function to be called when */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1719 | /* the variable gets set/unset */ |
| 1720 | }; |
| 1721 | |
| 1722 | struct localvar { |
| 1723 | struct localvar *next; /* next local variable in list */ |
| 1724 | struct var *vp; /* the variable that was made local */ |
| 1725 | int flags; /* saved flags */ |
| 1726 | const char *text; /* saved text */ |
| 1727 | }; |
| 1728 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1729 | /* flags */ |
| 1730 | #define VEXPORT 0x01 /* variable is exported */ |
| 1731 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 1732 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 1733 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 1734 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 1735 | #define VUNSET 0x20 /* the variable is not set */ |
| 1736 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 1737 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 1738 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1739 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1740 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 1741 | #else |
| 1742 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1743 | #endif |
| 1744 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1745 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1746 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1747 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1748 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1749 | change_lc_all(const char *value) |
| 1750 | { |
| 1751 | if (value && *value != '\0') |
| 1752 | setlocale(LC_ALL, value); |
| 1753 | } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1754 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1755 | change_lc_ctype(const char *value) |
| 1756 | { |
| 1757 | if (value && *value != '\0') |
| 1758 | setlocale(LC_CTYPE, value); |
| 1759 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1760 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1761 | #if ENABLE_ASH_MAIL |
| 1762 | static void chkmail(void); |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1763 | static void changemail(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1764 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1765 | static void changepath(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1766 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1767 | static void change_random(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1768 | #endif |
| 1769 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1770 | static const struct { |
| 1771 | int flags; |
| 1772 | const char *text; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1773 | void (*func)(const char *) FAST_FUNC; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1774 | } varinit_data[] = { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1775 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1776 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1777 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL\0" , changemail }, |
| 1778 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH\0", changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1779 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1780 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 1781 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 1782 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 1783 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1784 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1785 | { VSTRFIXED|VTEXTFIXED , "OPTIND=1" , getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1786 | #endif |
| 1787 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1788 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM\0", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1789 | #endif |
| 1790 | #if ENABLE_LOCALE_SUPPORT |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1791 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL\0" , change_lc_all }, |
| 1792 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE\0", change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1793 | #endif |
| 1794 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1795 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE\0", NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1796 | #endif |
| 1797 | }; |
| 1798 | |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1799 | struct redirtab; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1800 | |
| 1801 | struct globals_var { |
| 1802 | struct shparam shellparam; /* $@ current positional parameters */ |
| 1803 | struct redirtab *redirlist; |
| 1804 | int g_nullredirs; |
| 1805 | int preverrout_fd; /* save fd2 before print debug if xflag is set. */ |
| 1806 | struct var *vartab[VTABSIZE]; |
| 1807 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
| 1808 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1809 | extern struct globals_var *const ash_ptr_to_globals_var; |
| 1810 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1811 | #define shellparam (G_var.shellparam ) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1812 | //#define redirlist (G_var.redirlist ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1813 | #define g_nullredirs (G_var.g_nullredirs ) |
| 1814 | #define preverrout_fd (G_var.preverrout_fd) |
| 1815 | #define vartab (G_var.vartab ) |
| 1816 | #define varinit (G_var.varinit ) |
| 1817 | #define INIT_G_var() do { \ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1818 | unsigned i; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1819 | (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \ |
| 1820 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1821 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
| 1822 | varinit[i].flags = varinit_data[i].flags; \ |
| 1823 | varinit[i].text = varinit_data[i].text; \ |
| 1824 | varinit[i].func = varinit_data[i].func; \ |
| 1825 | } \ |
| 1826 | } while (0) |
| 1827 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1828 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1829 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1830 | # define vmail (&vifs)[1] |
| 1831 | # define vmpath (&vmail)[1] |
| 1832 | # define vpath (&vmpath)[1] |
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 | # define vpath (&vifs)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1835 | #endif |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1836 | #define vps1 (&vpath)[1] |
| 1837 | #define vps2 (&vps1)[1] |
| 1838 | #define vps4 (&vps2)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1839 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1840 | # define voptind (&vps4)[1] |
| 1841 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1842 | # define vrandom (&voptind)[1] |
| 1843 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1844 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1845 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1846 | # define vrandom (&vps4)[1] |
| 1847 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1848 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1849 | |
| 1850 | /* |
| 1851 | * The following macros access the values of the above variables. |
| 1852 | * They have to skip over the name. They return the null string |
| 1853 | * for unset variables. |
| 1854 | */ |
| 1855 | #define ifsval() (vifs.text + 4) |
| 1856 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1857 | #if ENABLE_ASH_MAIL |
| 1858 | # define mailval() (vmail.text + 5) |
| 1859 | # define mpathval() (vmpath.text + 9) |
| 1860 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1861 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1862 | #define pathval() (vpath.text + 5) |
| 1863 | #define ps1val() (vps1.text + 4) |
| 1864 | #define ps2val() (vps2.text + 4) |
| 1865 | #define ps4val() (vps4.text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1866 | #if ENABLE_ASH_GETOPTS |
| 1867 | # define optindval() (voptind.text + 7) |
| 1868 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1869 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1870 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1871 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1872 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1873 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1874 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1875 | static void FAST_FUNC |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1876 | getoptsreset(const char *value) |
| 1877 | { |
| 1878 | shellparam.optind = number(value); |
| 1879 | shellparam.optoff = -1; |
| 1880 | } |
| 1881 | #endif |
| 1882 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1883 | /* |
| 1884 | * Return of a legal variable name (a letter or underscore followed by zero or |
| 1885 | * more letters, underscores, and digits). |
| 1886 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1887 | static char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1888 | endofname(const char *name) |
| 1889 | { |
| 1890 | char *p; |
| 1891 | |
| 1892 | p = (char *) name; |
| 1893 | if (!is_name(*p)) |
| 1894 | return p; |
| 1895 | while (*++p) { |
| 1896 | if (!is_in_name(*p)) |
| 1897 | break; |
| 1898 | } |
| 1899 | return p; |
| 1900 | } |
| 1901 | |
| 1902 | /* |
| 1903 | * Compares two strings up to the first = or '\0'. The first |
| 1904 | * string must be terminated by '='; the second may be terminated by |
| 1905 | * either '=' or '\0'. |
| 1906 | */ |
| 1907 | static int |
| 1908 | varcmp(const char *p, const char *q) |
| 1909 | { |
| 1910 | int c, d; |
| 1911 | |
| 1912 | while ((c = *p) == (d = *q)) { |
| 1913 | if (!c || c == '=') |
| 1914 | goto out; |
| 1915 | p++; |
| 1916 | q++; |
| 1917 | } |
| 1918 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 1919 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1920 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 1921 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1922 | out: |
| 1923 | return c - d; |
| 1924 | } |
| 1925 | |
| 1926 | static int |
| 1927 | varequal(const char *a, const char *b) |
| 1928 | { |
| 1929 | return !varcmp(a, b); |
| 1930 | } |
| 1931 | |
| 1932 | /* |
| 1933 | * Find the appropriate entry in the hash table from the name. |
| 1934 | */ |
| 1935 | static struct var ** |
| 1936 | hashvar(const char *p) |
| 1937 | { |
| 1938 | unsigned hashval; |
| 1939 | |
| 1940 | hashval = ((unsigned char) *p) << 4; |
| 1941 | while (*p && *p != '=') |
| 1942 | hashval += (unsigned char) *p++; |
| 1943 | return &vartab[hashval % VTABSIZE]; |
| 1944 | } |
| 1945 | |
| 1946 | static int |
| 1947 | vpcmp(const void *a, const void *b) |
| 1948 | { |
| 1949 | return varcmp(*(const char **)a, *(const char **)b); |
| 1950 | } |
| 1951 | |
| 1952 | /* |
| 1953 | * This routine initializes the builtin variables. |
| 1954 | */ |
| 1955 | static void |
| 1956 | initvar(void) |
| 1957 | { |
| 1958 | struct var *vp; |
| 1959 | struct var *end; |
| 1960 | struct var **vpp; |
| 1961 | |
| 1962 | /* |
| 1963 | * PS1 depends on uid |
| 1964 | */ |
| 1965 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 1966 | vps1.text = "PS1=\\w \\$ "; |
| 1967 | #else |
| 1968 | if (!geteuid()) |
| 1969 | vps1.text = "PS1=# "; |
| 1970 | #endif |
| 1971 | vp = varinit; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 1972 | end = vp + ARRAY_SIZE(varinit); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1973 | do { |
| 1974 | vpp = hashvar(vp->text); |
| 1975 | vp->next = *vpp; |
| 1976 | *vpp = vp; |
| 1977 | } while (++vp < end); |
| 1978 | } |
| 1979 | |
| 1980 | static struct var ** |
| 1981 | findvar(struct var **vpp, const char *name) |
| 1982 | { |
| 1983 | for (; *vpp; vpp = &(*vpp)->next) { |
| 1984 | if (varequal((*vpp)->text, name)) { |
| 1985 | break; |
| 1986 | } |
| 1987 | } |
| 1988 | return vpp; |
| 1989 | } |
| 1990 | |
| 1991 | /* |
| 1992 | * Find the value of a variable. Returns NULL if not set. |
| 1993 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1994 | static const char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1995 | lookupvar(const char *name) |
| 1996 | { |
| 1997 | struct var *v; |
| 1998 | |
| 1999 | v = *findvar(hashvar(name), name); |
| 2000 | if (v) { |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2001 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2002 | /* |
| 2003 | * Dynamic variables are implemented roughly the same way they are |
| 2004 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 2005 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 2006 | * lookup will no longer happen at that point. -- PFM. |
| 2007 | */ |
| 2008 | if ((v->flags & VDYNAMIC)) |
| 2009 | (*v->func)(NULL); |
| 2010 | #endif |
| 2011 | if (!(v->flags & VUNSET)) |
| 2012 | return strchrnul(v->text, '=') + 1; |
| 2013 | } |
| 2014 | return NULL; |
| 2015 | } |
| 2016 | |
| 2017 | /* |
| 2018 | * Search the environment of a builtin command. |
| 2019 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2020 | static const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2021 | bltinlookup(const char *name) |
| 2022 | { |
| 2023 | struct strlist *sp; |
| 2024 | |
| 2025 | for (sp = cmdenviron; sp; sp = sp->next) { |
| 2026 | if (varequal(sp->text, name)) |
| 2027 | return strchrnul(sp->text, '=') + 1; |
| 2028 | } |
| 2029 | return lookupvar(name); |
| 2030 | } |
| 2031 | |
| 2032 | /* |
| 2033 | * Same as setvar except that the variable and value are passed in |
| 2034 | * the first argument as name=value. Since the first argument will |
| 2035 | * be actually stored in the table, it should not be a string that |
| 2036 | * will go away. |
| 2037 | * Called with interrupts off. |
| 2038 | */ |
| 2039 | static void |
| 2040 | setvareq(char *s, int flags) |
| 2041 | { |
| 2042 | struct var *vp, **vpp; |
| 2043 | |
| 2044 | vpp = hashvar(s); |
| 2045 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
| 2046 | vp = *findvar(vpp, s); |
| 2047 | if (vp) { |
| 2048 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 2049 | const char *n; |
| 2050 | |
| 2051 | if (flags & VNOSAVE) |
| 2052 | free(s); |
| 2053 | n = vp->text; |
| 2054 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2055 | } |
| 2056 | |
| 2057 | if (flags & VNOSET) |
| 2058 | return; |
| 2059 | |
| 2060 | if (vp->func && (flags & VNOFUNC) == 0) |
| 2061 | (*vp->func)(strchrnul(s, '=') + 1); |
| 2062 | |
| 2063 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 2064 | free((char*)vp->text); |
| 2065 | |
| 2066 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
| 2067 | } else { |
| 2068 | if (flags & VNOSET) |
| 2069 | return; |
| 2070 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2071 | vp = ckzalloc(sizeof(*vp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2072 | vp->next = *vpp; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2073 | /*vp->func = NULL; - ckzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2074 | *vpp = vp; |
| 2075 | } |
| 2076 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 2077 | s = ckstrdup(s); |
| 2078 | vp->text = s; |
| 2079 | vp->flags = flags; |
| 2080 | } |
| 2081 | |
| 2082 | /* |
| 2083 | * Set the value of a variable. The flags argument is ored with the |
| 2084 | * flags of the variable. If val is NULL, the variable is unset. |
| 2085 | */ |
| 2086 | static void |
| 2087 | setvar(const char *name, const char *val, int flags) |
| 2088 | { |
| 2089 | char *p, *q; |
| 2090 | size_t namelen; |
| 2091 | char *nameeq; |
| 2092 | size_t vallen; |
| 2093 | |
| 2094 | q = endofname(name); |
| 2095 | p = strchrnul(q, '='); |
| 2096 | namelen = p - name; |
| 2097 | if (!namelen || p != q) |
| 2098 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 2099 | vallen = 0; |
| 2100 | if (val == NULL) { |
| 2101 | flags |= VUNSET; |
| 2102 | } else { |
| 2103 | vallen = strlen(val); |
| 2104 | } |
| 2105 | INT_OFF; |
| 2106 | nameeq = ckmalloc(namelen + vallen + 2); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2107 | p = (char *)memcpy(nameeq, name, namelen) + namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2108 | if (val) { |
| 2109 | *p++ = '='; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2110 | p = (char *)memcpy(p, val, vallen) + vallen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2111 | } |
| 2112 | *p = '\0'; |
| 2113 | setvareq(nameeq, flags | VNOSAVE); |
| 2114 | INT_ON; |
| 2115 | } |
| 2116 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2117 | static void FAST_FUNC |
| 2118 | setvar2(const char *name, const char *val) |
| 2119 | { |
| 2120 | setvar(name, val, 0); |
| 2121 | } |
| 2122 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2123 | #if ENABLE_ASH_GETOPTS |
| 2124 | /* |
| 2125 | * Safe version of setvar, returns 1 on success 0 on failure. |
| 2126 | */ |
| 2127 | static int |
| 2128 | setvarsafe(const char *name, const char *val, int flags) |
| 2129 | { |
| 2130 | int err; |
| 2131 | volatile int saveint; |
| 2132 | struct jmploc *volatile savehandler = exception_handler; |
| 2133 | struct jmploc jmploc; |
| 2134 | |
| 2135 | SAVE_INT(saveint); |
| 2136 | if (setjmp(jmploc.loc)) |
| 2137 | err = 1; |
| 2138 | else { |
| 2139 | exception_handler = &jmploc; |
| 2140 | setvar(name, val, flags); |
| 2141 | err = 0; |
| 2142 | } |
| 2143 | exception_handler = savehandler; |
| 2144 | RESTORE_INT(saveint); |
| 2145 | return err; |
| 2146 | } |
| 2147 | #endif |
| 2148 | |
| 2149 | /* |
| 2150 | * Unset the specified variable. |
| 2151 | */ |
| 2152 | static int |
| 2153 | unsetvar(const char *s) |
| 2154 | { |
| 2155 | struct var **vpp; |
| 2156 | struct var *vp; |
| 2157 | int retval; |
| 2158 | |
| 2159 | vpp = findvar(hashvar(s), s); |
| 2160 | vp = *vpp; |
| 2161 | retval = 2; |
| 2162 | if (vp) { |
| 2163 | int flags = vp->flags; |
| 2164 | |
| 2165 | retval = 1; |
| 2166 | if (flags & VREADONLY) |
| 2167 | goto out; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2168 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2169 | vp->flags &= ~VDYNAMIC; |
| 2170 | #endif |
| 2171 | if (flags & VUNSET) |
| 2172 | goto ok; |
| 2173 | if ((flags & VSTRFIXED) == 0) { |
| 2174 | INT_OFF; |
| 2175 | if ((flags & (VTEXTFIXED|VSTACK)) == 0) |
| 2176 | free((char*)vp->text); |
| 2177 | *vpp = vp->next; |
| 2178 | free(vp); |
| 2179 | INT_ON; |
| 2180 | } else { |
| 2181 | setvar(s, 0, 0); |
| 2182 | vp->flags &= ~VEXPORT; |
| 2183 | } |
| 2184 | ok: |
| 2185 | retval = 0; |
| 2186 | } |
| 2187 | out: |
| 2188 | return retval; |
| 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * Process a linked list of variable assignments. |
| 2193 | */ |
| 2194 | static void |
| 2195 | listsetvar(struct strlist *list_set_var, int flags) |
| 2196 | { |
| 2197 | struct strlist *lp = list_set_var; |
| 2198 | |
| 2199 | if (!lp) |
| 2200 | return; |
| 2201 | INT_OFF; |
| 2202 | do { |
| 2203 | setvareq(lp->text, flags); |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2204 | lp = lp->next; |
| 2205 | } while (lp); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2206 | INT_ON; |
| 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * Generate a list of variables satisfying the given conditions. |
| 2211 | */ |
| 2212 | static char ** |
| 2213 | listvars(int on, int off, char ***end) |
| 2214 | { |
| 2215 | struct var **vpp; |
| 2216 | struct var *vp; |
| 2217 | char **ep; |
| 2218 | int mask; |
| 2219 | |
| 2220 | STARTSTACKSTR(ep); |
| 2221 | vpp = vartab; |
| 2222 | mask = on | off; |
| 2223 | do { |
| 2224 | for (vp = *vpp; vp; vp = vp->next) { |
| 2225 | if ((vp->flags & mask) == on) { |
| 2226 | if (ep == stackstrend()) |
| 2227 | ep = growstackstr(); |
| 2228 | *ep++ = (char *) vp->text; |
| 2229 | } |
| 2230 | } |
| 2231 | } while (++vpp < vartab + VTABSIZE); |
| 2232 | if (ep == stackstrend()) |
| 2233 | ep = growstackstr(); |
| 2234 | if (end) |
| 2235 | *end = ep; |
| 2236 | *ep++ = NULL; |
| 2237 | return grabstackstr(ep); |
| 2238 | } |
| 2239 | |
| 2240 | |
| 2241 | /* ============ Path search helper |
| 2242 | * |
| 2243 | * The variable path (passed by reference) should be set to the start |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2244 | * of the path before the first call; path_advance will update |
| 2245 | * this value as it proceeds. Successive calls to path_advance will return |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2246 | * the possible path expansions in sequence. If an option (indicated by |
| 2247 | * a percent sign) appears in the path entry then the global variable |
| 2248 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 2249 | * NULL. |
| 2250 | */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2251 | static const char *pathopt; /* set by path_advance */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2252 | |
| 2253 | static char * |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2254 | path_advance(const char **path, const char *name) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2255 | { |
| 2256 | const char *p; |
| 2257 | char *q; |
| 2258 | const char *start; |
| 2259 | size_t len; |
| 2260 | |
| 2261 | if (*path == NULL) |
| 2262 | return NULL; |
| 2263 | start = *path; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2264 | for (p = start; *p && *p != ':' && *p != '%'; p++) |
| 2265 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2266 | len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ |
| 2267 | while (stackblocksize() < len) |
| 2268 | growstackblock(); |
| 2269 | q = stackblock(); |
| 2270 | if (p != start) { |
| 2271 | memcpy(q, start, p - start); |
| 2272 | q += p - start; |
| 2273 | *q++ = '/'; |
| 2274 | } |
| 2275 | strcpy(q, name); |
| 2276 | pathopt = NULL; |
| 2277 | if (*p == '%') { |
| 2278 | pathopt = ++p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2279 | while (*p && *p != ':') |
| 2280 | p++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2281 | } |
| 2282 | if (*p == ':') |
| 2283 | *path = p + 1; |
| 2284 | else |
| 2285 | *path = NULL; |
| 2286 | return stalloc(len); |
| 2287 | } |
| 2288 | |
| 2289 | |
| 2290 | /* ============ Prompt */ |
| 2291 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2292 | static smallint doprompt; /* if set, prompt the user */ |
| 2293 | static smallint needprompt; /* true if interactive and at start of line */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2294 | |
| 2295 | #if ENABLE_FEATURE_EDITING |
| 2296 | static line_input_t *line_input_state; |
| 2297 | static const char *cmdedit_prompt; |
| 2298 | static void |
| 2299 | putprompt(const char *s) |
| 2300 | { |
| 2301 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 2302 | free((char*)cmdedit_prompt); |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 2303 | cmdedit_prompt = ckstrdup(s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2304 | return; |
| 2305 | } |
| 2306 | cmdedit_prompt = s; |
| 2307 | } |
| 2308 | #else |
| 2309 | static void |
| 2310 | putprompt(const char *s) |
| 2311 | { |
| 2312 | out2str(s); |
| 2313 | } |
| 2314 | #endif |
| 2315 | |
| 2316 | #if ENABLE_ASH_EXPAND_PRMT |
| 2317 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
| 2318 | static const char *expandstr(const char *ps); |
| 2319 | #else |
| 2320 | #define expandstr(s) s |
| 2321 | #endif |
| 2322 | |
| 2323 | static void |
| 2324 | setprompt(int whichprompt) |
| 2325 | { |
| 2326 | const char *prompt; |
| 2327 | #if ENABLE_ASH_EXPAND_PRMT |
| 2328 | struct stackmark smark; |
| 2329 | #endif |
| 2330 | |
| 2331 | needprompt = 0; |
| 2332 | |
| 2333 | switch (whichprompt) { |
| 2334 | case 1: |
| 2335 | prompt = ps1val(); |
| 2336 | break; |
| 2337 | case 2: |
| 2338 | prompt = ps2val(); |
| 2339 | break; |
| 2340 | default: /* 0 */ |
| 2341 | prompt = nullstr; |
| 2342 | } |
| 2343 | #if ENABLE_ASH_EXPAND_PRMT |
| 2344 | setstackmark(&smark); |
| 2345 | stalloc(stackblocksize()); |
| 2346 | #endif |
| 2347 | putprompt(expandstr(prompt)); |
| 2348 | #if ENABLE_ASH_EXPAND_PRMT |
| 2349 | popstackmark(&smark); |
| 2350 | #endif |
| 2351 | } |
| 2352 | |
| 2353 | |
| 2354 | /* ============ The cd and pwd commands */ |
| 2355 | |
| 2356 | #define CD_PHYSICAL 1 |
| 2357 | #define CD_PRINT 2 |
| 2358 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2359 | static int |
| 2360 | cdopt(void) |
| 2361 | { |
| 2362 | int flags = 0; |
| 2363 | int i, j; |
| 2364 | |
| 2365 | j = 'L'; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 2366 | while ((i = nextopt("LP")) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2367 | if (i != j) { |
| 2368 | flags ^= CD_PHYSICAL; |
| 2369 | j = i; |
| 2370 | } |
| 2371 | } |
| 2372 | |
| 2373 | return flags; |
| 2374 | } |
| 2375 | |
| 2376 | /* |
| 2377 | * Update curdir (the name of the current directory) in response to a |
| 2378 | * cd command. |
| 2379 | */ |
| 2380 | static const char * |
| 2381 | updatepwd(const char *dir) |
| 2382 | { |
| 2383 | char *new; |
| 2384 | char *p; |
| 2385 | char *cdcomppath; |
| 2386 | const char *lim; |
| 2387 | |
| 2388 | cdcomppath = ststrdup(dir); |
| 2389 | STARTSTACKSTR(new); |
| 2390 | if (*dir != '/') { |
| 2391 | if (curdir == nullstr) |
| 2392 | return 0; |
| 2393 | new = stack_putstr(curdir, new); |
| 2394 | } |
| 2395 | new = makestrspace(strlen(dir) + 2, new); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2396 | lim = (char *)stackblock() + 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2397 | if (*dir != '/') { |
| 2398 | if (new[-1] != '/') |
| 2399 | USTPUTC('/', new); |
| 2400 | if (new > lim && *lim == '/') |
| 2401 | lim++; |
| 2402 | } else { |
| 2403 | USTPUTC('/', new); |
| 2404 | cdcomppath++; |
| 2405 | if (dir[1] == '/' && dir[2] != '/') { |
| 2406 | USTPUTC('/', new); |
| 2407 | cdcomppath++; |
| 2408 | lim++; |
| 2409 | } |
| 2410 | } |
| 2411 | p = strtok(cdcomppath, "/"); |
| 2412 | while (p) { |
| 2413 | switch (*p) { |
| 2414 | case '.': |
| 2415 | if (p[1] == '.' && p[2] == '\0') { |
| 2416 | while (new > lim) { |
| 2417 | STUNPUTC(new); |
| 2418 | if (new[-1] == '/') |
| 2419 | break; |
| 2420 | } |
| 2421 | break; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 2422 | } |
| 2423 | if (p[1] == '\0') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2424 | break; |
| 2425 | /* fall through */ |
| 2426 | default: |
| 2427 | new = stack_putstr(p, new); |
| 2428 | USTPUTC('/', new); |
| 2429 | } |
| 2430 | p = strtok(0, "/"); |
| 2431 | } |
| 2432 | if (new > lim) |
| 2433 | STUNPUTC(new); |
| 2434 | *new = 0; |
| 2435 | return stackblock(); |
| 2436 | } |
| 2437 | |
| 2438 | /* |
| 2439 | * Find out what the current directory is. If we already know the current |
| 2440 | * directory, this routine returns immediately. |
| 2441 | */ |
| 2442 | static char * |
| 2443 | getpwd(void) |
| 2444 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2445 | char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2446 | return dir ? dir : nullstr; |
| 2447 | } |
| 2448 | |
| 2449 | static void |
| 2450 | setpwd(const char *val, int setold) |
| 2451 | { |
| 2452 | char *oldcur, *dir; |
| 2453 | |
| 2454 | oldcur = dir = curdir; |
| 2455 | |
| 2456 | if (setold) { |
| 2457 | setvar("OLDPWD", oldcur, VEXPORT); |
| 2458 | } |
| 2459 | INT_OFF; |
| 2460 | if (physdir != nullstr) { |
| 2461 | if (physdir != oldcur) |
| 2462 | free(physdir); |
| 2463 | physdir = nullstr; |
| 2464 | } |
| 2465 | if (oldcur == val || !val) { |
| 2466 | char *s = getpwd(); |
| 2467 | physdir = s; |
| 2468 | if (!val) |
| 2469 | dir = s; |
| 2470 | } else |
| 2471 | dir = ckstrdup(val); |
| 2472 | if (oldcur != dir && oldcur != nullstr) { |
| 2473 | free(oldcur); |
| 2474 | } |
| 2475 | curdir = dir; |
| 2476 | INT_ON; |
| 2477 | setvar("PWD", dir, VEXPORT); |
| 2478 | } |
| 2479 | |
| 2480 | static void hashcd(void); |
| 2481 | |
| 2482 | /* |
| 2483 | * Actually do the chdir. We also call hashcd to let the routines in exec.c |
| 2484 | * know that the current directory has changed. |
| 2485 | */ |
| 2486 | static int |
| 2487 | docd(const char *dest, int flags) |
| 2488 | { |
Denys Vlasenko | 4b1100e | 2010-03-05 14:10:54 +0100 | [diff] [blame] | 2489 | const char *dir = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2490 | int err; |
| 2491 | |
| 2492 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 2493 | |
| 2494 | INT_OFF; |
| 2495 | if (!(flags & CD_PHYSICAL)) { |
| 2496 | dir = updatepwd(dest); |
| 2497 | if (dir) |
| 2498 | dest = dir; |
| 2499 | } |
| 2500 | err = chdir(dest); |
| 2501 | if (err) |
| 2502 | goto out; |
| 2503 | setpwd(dir, 1); |
| 2504 | hashcd(); |
| 2505 | out: |
| 2506 | INT_ON; |
| 2507 | return err; |
| 2508 | } |
| 2509 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2510 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2511 | cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2512 | { |
| 2513 | const char *dest; |
| 2514 | const char *path; |
| 2515 | const char *p; |
| 2516 | char c; |
| 2517 | struct stat statb; |
| 2518 | int flags; |
| 2519 | |
| 2520 | flags = cdopt(); |
| 2521 | dest = *argptr; |
| 2522 | if (!dest) |
| 2523 | dest = bltinlookup(homestr); |
| 2524 | else if (LONE_DASH(dest)) { |
| 2525 | dest = bltinlookup("OLDPWD"); |
| 2526 | flags |= CD_PRINT; |
| 2527 | } |
| 2528 | if (!dest) |
| 2529 | dest = nullstr; |
| 2530 | if (*dest == '/') |
| 2531 | goto step7; |
| 2532 | if (*dest == '.') { |
| 2533 | c = dest[1]; |
| 2534 | dotdot: |
| 2535 | switch (c) { |
| 2536 | case '\0': |
| 2537 | case '/': |
| 2538 | goto step6; |
| 2539 | case '.': |
| 2540 | c = dest[2]; |
| 2541 | if (c != '.') |
| 2542 | goto dotdot; |
| 2543 | } |
| 2544 | } |
| 2545 | if (!*dest) |
| 2546 | dest = "."; |
| 2547 | path = bltinlookup("CDPATH"); |
| 2548 | if (!path) { |
| 2549 | step6: |
| 2550 | step7: |
| 2551 | p = dest; |
| 2552 | goto docd; |
| 2553 | } |
| 2554 | do { |
| 2555 | c = *path; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2556 | p = path_advance(&path, dest); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2557 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 2558 | if (c && c != ':') |
| 2559 | flags |= CD_PRINT; |
| 2560 | docd: |
| 2561 | if (!docd(p, flags)) |
| 2562 | goto out; |
| 2563 | break; |
| 2564 | } |
| 2565 | } while (path); |
| 2566 | ash_msg_and_raise_error("can't cd to %s", dest); |
| 2567 | /* NOTREACHED */ |
| 2568 | out: |
| 2569 | if (flags & CD_PRINT) |
| 2570 | out1fmt(snlfmt, curdir); |
| 2571 | return 0; |
| 2572 | } |
| 2573 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2574 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2575 | pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2576 | { |
| 2577 | int flags; |
| 2578 | const char *dir = curdir; |
| 2579 | |
| 2580 | flags = cdopt(); |
| 2581 | if (flags) { |
| 2582 | if (physdir == nullstr) |
| 2583 | setpwd(dir, 0); |
| 2584 | dir = physdir; |
| 2585 | } |
| 2586 | out1fmt(snlfmt, dir); |
| 2587 | return 0; |
| 2588 | } |
| 2589 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2590 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 2591 | /* ============ ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2592 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2593 | |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 2594 | #define IBUFSIZ (ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 1024) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2595 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2596 | /* Syntax classes */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2597 | #define CWORD 0 /* character is nothing special */ |
| 2598 | #define CNL 1 /* newline character */ |
| 2599 | #define CBACK 2 /* a backslash character */ |
| 2600 | #define CSQUOTE 3 /* single quote */ |
| 2601 | #define CDQUOTE 4 /* double quote */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2602 | #define CENDQUOTE 5 /* a terminating quote */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2603 | #define CBQUOTE 6 /* backwards single quote */ |
| 2604 | #define CVAR 7 /* a dollar sign */ |
| 2605 | #define CENDVAR 8 /* a '}' character */ |
| 2606 | #define CLP 9 /* a left paren in arithmetic */ |
| 2607 | #define CRP 10 /* a right paren in arithmetic */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2608 | #define CENDFILE 11 /* end of file */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2609 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 2610 | #define CSPCL 13 /* these terminate a word */ |
| 2611 | #define CIGN 14 /* character should be ignored */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2612 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2613 | #define PEOF 256 |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2614 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2615 | # define PEOA 257 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2616 | #endif |
| 2617 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2618 | #define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2619 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2620 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2621 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8) | (d << 12)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2622 | #else |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2623 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8)) |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2624 | #endif |
Denys Vlasenko | 068d386 | 2009-11-29 01:41:11 +0100 | [diff] [blame] | 2625 | static const uint16_t S_I_T[] = { |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2626 | #if ENABLE_ASH_ALIAS |
| 2627 | SIT_ITEM(CSPCL , CIGN , CIGN , CIGN ), /* 0, PEOA */ |
| 2628 | #endif |
| 2629 | SIT_ITEM(CSPCL , CWORD , CWORD, CWORD ), /* 1, ' ' */ |
| 2630 | SIT_ITEM(CNL , CNL , CNL , CNL ), /* 2, \n */ |
| 2631 | SIT_ITEM(CWORD , CCTL , CCTL , CWORD ), /* 3, !*-/:=?[]~ */ |
| 2632 | SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD ), /* 4, '"' */ |
| 2633 | SIT_ITEM(CVAR , CVAR , CWORD, CVAR ), /* 5, $ */ |
| 2634 | SIT_ITEM(CSQUOTE , CWORD , CENDQUOTE, CWORD), /* 6, "'" */ |
| 2635 | SIT_ITEM(CSPCL , CWORD , CWORD, CLP ), /* 7, ( */ |
| 2636 | SIT_ITEM(CSPCL , CWORD , CWORD, CRP ), /* 8, ) */ |
| 2637 | SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 9, \ */ |
| 2638 | SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 10, ` */ |
| 2639 | SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 11, } */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2640 | #if !USE_SIT_FUNCTION |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2641 | SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */ |
| 2642 | SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 13, 0-9A-Za-z */ |
| 2643 | SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 14, CTLESC ... */ |
| 2644 | #endif |
| 2645 | #undef SIT_ITEM |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2646 | }; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2647 | /* Constants below must match table above */ |
| 2648 | enum { |
| 2649 | #if ENABLE_ASH_ALIAS |
| 2650 | CSPCL_CIGN_CIGN_CIGN , /* 0 */ |
| 2651 | #endif |
| 2652 | CSPCL_CWORD_CWORD_CWORD , /* 1 */ |
| 2653 | CNL_CNL_CNL_CNL , /* 2 */ |
| 2654 | CWORD_CCTL_CCTL_CWORD , /* 3 */ |
| 2655 | CDQUOTE_CENDQUOTE_CWORD_CWORD , /* 4 */ |
| 2656 | CVAR_CVAR_CWORD_CVAR , /* 5 */ |
| 2657 | CSQUOTE_CWORD_CENDQUOTE_CWORD , /* 6 */ |
| 2658 | CSPCL_CWORD_CWORD_CLP , /* 7 */ |
| 2659 | CSPCL_CWORD_CWORD_CRP , /* 8 */ |
| 2660 | CBACK_CBACK_CCTL_CBACK , /* 9 */ |
| 2661 | CBQUOTE_CBQUOTE_CWORD_CBQUOTE , /* 10 */ |
| 2662 | CENDVAR_CENDVAR_CWORD_CENDVAR , /* 11 */ |
| 2663 | CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 12 */ |
| 2664 | CWORD_CWORD_CWORD_CWORD , /* 13 */ |
| 2665 | CCTL_CCTL_CCTL_CCTL , /* 14 */ |
| 2666 | }; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2667 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2668 | /* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF, |
| 2669 | * caller must ensure proper cast on it if c is *char_ptr! |
| 2670 | */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2671 | /* Values for syntax param */ |
| 2672 | #define BASESYNTAX 0 /* not in quotes */ |
| 2673 | #define DQSYNTAX 1 /* in double quotes */ |
| 2674 | #define SQSYNTAX 2 /* in single quotes */ |
| 2675 | #define ARISYNTAX 3 /* in arithmetic */ |
| 2676 | #define PSSYNTAX 4 /* prompt. never passed to SIT() */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2677 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2678 | #if USE_SIT_FUNCTION |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2679 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2680 | static int |
| 2681 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2682 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2683 | static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-/:;<=>?[\\]`|}~"; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2684 | # if ENABLE_ASH_ALIAS |
| 2685 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2686 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
| 2687 | 7, 8, 3, 3, 3, 3, 1, 1, /* "()*-/:;<" */ |
| 2688 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 2689 | 11, 3 /* "}~" */ |
| 2690 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2691 | # else |
| 2692 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2693 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
| 2694 | 6, 7, 2, 2, 2, 2, 0, 0, /* "()*-/:;<" */ |
| 2695 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 2696 | 10, 2 /* "}~" */ |
| 2697 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2698 | # endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2699 | const char *s; |
| 2700 | int indx; |
| 2701 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2702 | if (c == PEOF) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2703 | return CENDFILE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2704 | # if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2705 | if (c == PEOA) |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2706 | indx = 0; |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2707 | else |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2708 | # endif |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2709 | { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2710 | /* Cast is purely for paranoia here, |
| 2711 | * just in case someone passed signed char to us */ |
| 2712 | if ((unsigned char)c >= CTL_FIRST |
| 2713 | && (unsigned char)c <= CTL_LAST |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2714 | ) { |
| 2715 | return CCTL; |
| 2716 | } |
| 2717 | s = strchrnul(spec_symbls, c); |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2718 | if (*s == '\0') |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2719 | return CWORD; |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2720 | indx = syntax_index_table[s - spec_symbls]; |
| 2721 | } |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2722 | return (S_I_T[indx] >> (syntax*4)) & 0xf; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2725 | #else /* !USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2726 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2727 | static const uint8_t syntax_index_table[] = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2728 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2729 | /* 0 */ CWORD_CWORD_CWORD_CWORD, |
| 2730 | /* 1 */ CWORD_CWORD_CWORD_CWORD, |
| 2731 | /* 2 */ CWORD_CWORD_CWORD_CWORD, |
| 2732 | /* 3 */ CWORD_CWORD_CWORD_CWORD, |
| 2733 | /* 4 */ CWORD_CWORD_CWORD_CWORD, |
| 2734 | /* 5 */ CWORD_CWORD_CWORD_CWORD, |
| 2735 | /* 6 */ CWORD_CWORD_CWORD_CWORD, |
| 2736 | /* 7 */ CWORD_CWORD_CWORD_CWORD, |
| 2737 | /* 8 */ CWORD_CWORD_CWORD_CWORD, |
| 2738 | /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 2739 | /* 10 "\n" */ CNL_CNL_CNL_CNL, |
| 2740 | /* 11 */ CWORD_CWORD_CWORD_CWORD, |
| 2741 | /* 12 */ CWORD_CWORD_CWORD_CWORD, |
| 2742 | /* 13 */ CWORD_CWORD_CWORD_CWORD, |
| 2743 | /* 14 */ CWORD_CWORD_CWORD_CWORD, |
| 2744 | /* 15 */ CWORD_CWORD_CWORD_CWORD, |
| 2745 | /* 16 */ CWORD_CWORD_CWORD_CWORD, |
| 2746 | /* 17 */ CWORD_CWORD_CWORD_CWORD, |
| 2747 | /* 18 */ CWORD_CWORD_CWORD_CWORD, |
| 2748 | /* 19 */ CWORD_CWORD_CWORD_CWORD, |
| 2749 | /* 20 */ CWORD_CWORD_CWORD_CWORD, |
| 2750 | /* 21 */ CWORD_CWORD_CWORD_CWORD, |
| 2751 | /* 22 */ CWORD_CWORD_CWORD_CWORD, |
| 2752 | /* 23 */ CWORD_CWORD_CWORD_CWORD, |
| 2753 | /* 24 */ CWORD_CWORD_CWORD_CWORD, |
| 2754 | /* 25 */ CWORD_CWORD_CWORD_CWORD, |
| 2755 | /* 26 */ CWORD_CWORD_CWORD_CWORD, |
| 2756 | /* 27 */ CWORD_CWORD_CWORD_CWORD, |
| 2757 | /* 28 */ CWORD_CWORD_CWORD_CWORD, |
| 2758 | /* 29 */ CWORD_CWORD_CWORD_CWORD, |
| 2759 | /* 30 */ CWORD_CWORD_CWORD_CWORD, |
| 2760 | /* 31 */ CWORD_CWORD_CWORD_CWORD, |
| 2761 | /* 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 2762 | /* 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
| 2763 | /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
| 2764 | /* 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 2765 | /* 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 2766 | /* 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 2767 | /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
| 2768 | /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
| 2769 | /* 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 2770 | /* 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 2771 | /* 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 2772 | /* 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 2773 | /* 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 2774 | /* 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 2775 | /* 46 "." */ CWORD_CWORD_CWORD_CWORD, |
| 2776 | /* 47 "/" */ CWORD_CCTL_CCTL_CWORD, |
| 2777 | /* 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 2778 | /* 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 2779 | /* 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 2780 | /* 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 2781 | /* 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 2782 | /* 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 2783 | /* 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 2784 | /* 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 2785 | /* 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 2786 | /* 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 2787 | /* 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 2788 | /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 2789 | /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 2790 | /* 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 2791 | /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 2792 | /* 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 2793 | /* 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 2794 | /* 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 2795 | /* 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 2796 | /* 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 2797 | /* 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 2798 | /* 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 2799 | /* 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 2800 | /* 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 2801 | /* 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 2802 | /* 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 2803 | /* 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 2804 | /* 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 2805 | /* 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 2806 | /* 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 2807 | /* 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 2808 | /* 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 2809 | /* 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 2810 | /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 2811 | /* 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 2812 | /* 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 2813 | /* 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 2814 | /* 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 2815 | /* 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 2816 | /* 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 2817 | /* 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 2818 | /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 2819 | /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 2820 | /* 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 2821 | /* 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 2822 | /* 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 2823 | /* 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 2824 | /* 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 2825 | /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 2826 | /* 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 2827 | /* 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 2828 | /* 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 2829 | /* 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 2830 | /* 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 2831 | /* 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 2832 | /* 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 2833 | /* 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 2834 | /* 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 2835 | /* 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 2836 | /* 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 2837 | /* 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 2838 | /* 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 2839 | /* 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 2840 | /* 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 2841 | /* 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 2842 | /* 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 2843 | /* 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 2844 | /* 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 2845 | /* 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 2846 | /* 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 2847 | /* 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 2848 | /* 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 2849 | /* 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 2850 | /* 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 2851 | /* 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 2852 | /* 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 2853 | /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 2854 | /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 2855 | /* 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 2856 | /* 127 del */ CWORD_CWORD_CWORD_CWORD, |
| 2857 | /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 2858 | /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 2859 | /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2860 | /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2861 | /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 2862 | /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 2863 | /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 2864 | /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 2865 | /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
| 2866 | /* 137 */ CWORD_CWORD_CWORD_CWORD, |
| 2867 | /* 138 */ CWORD_CWORD_CWORD_CWORD, |
| 2868 | /* 139 */ CWORD_CWORD_CWORD_CWORD, |
| 2869 | /* 140 */ CWORD_CWORD_CWORD_CWORD, |
| 2870 | /* 141 */ CWORD_CWORD_CWORD_CWORD, |
| 2871 | /* 142 */ CWORD_CWORD_CWORD_CWORD, |
| 2872 | /* 143 */ CWORD_CWORD_CWORD_CWORD, |
| 2873 | /* 144 */ CWORD_CWORD_CWORD_CWORD, |
| 2874 | /* 145 */ CWORD_CWORD_CWORD_CWORD, |
| 2875 | /* 146 */ CWORD_CWORD_CWORD_CWORD, |
| 2876 | /* 147 */ CWORD_CWORD_CWORD_CWORD, |
| 2877 | /* 148 */ CWORD_CWORD_CWORD_CWORD, |
| 2878 | /* 149 */ CWORD_CWORD_CWORD_CWORD, |
| 2879 | /* 150 */ CWORD_CWORD_CWORD_CWORD, |
| 2880 | /* 151 */ CWORD_CWORD_CWORD_CWORD, |
| 2881 | /* 152 */ CWORD_CWORD_CWORD_CWORD, |
| 2882 | /* 153 */ CWORD_CWORD_CWORD_CWORD, |
| 2883 | /* 154 */ CWORD_CWORD_CWORD_CWORD, |
| 2884 | /* 155 */ CWORD_CWORD_CWORD_CWORD, |
| 2885 | /* 156 */ CWORD_CWORD_CWORD_CWORD, |
| 2886 | /* 157 */ CWORD_CWORD_CWORD_CWORD, |
| 2887 | /* 158 */ CWORD_CWORD_CWORD_CWORD, |
| 2888 | /* 159 */ CWORD_CWORD_CWORD_CWORD, |
| 2889 | /* 160 */ CWORD_CWORD_CWORD_CWORD, |
| 2890 | /* 161 */ CWORD_CWORD_CWORD_CWORD, |
| 2891 | /* 162 */ CWORD_CWORD_CWORD_CWORD, |
| 2892 | /* 163 */ CWORD_CWORD_CWORD_CWORD, |
| 2893 | /* 164 */ CWORD_CWORD_CWORD_CWORD, |
| 2894 | /* 165 */ CWORD_CWORD_CWORD_CWORD, |
| 2895 | /* 166 */ CWORD_CWORD_CWORD_CWORD, |
| 2896 | /* 167 */ CWORD_CWORD_CWORD_CWORD, |
| 2897 | /* 168 */ CWORD_CWORD_CWORD_CWORD, |
| 2898 | /* 169 */ CWORD_CWORD_CWORD_CWORD, |
| 2899 | /* 170 */ CWORD_CWORD_CWORD_CWORD, |
| 2900 | /* 171 */ CWORD_CWORD_CWORD_CWORD, |
| 2901 | /* 172 */ CWORD_CWORD_CWORD_CWORD, |
| 2902 | /* 173 */ CWORD_CWORD_CWORD_CWORD, |
| 2903 | /* 174 */ CWORD_CWORD_CWORD_CWORD, |
| 2904 | /* 175 */ CWORD_CWORD_CWORD_CWORD, |
| 2905 | /* 176 */ CWORD_CWORD_CWORD_CWORD, |
| 2906 | /* 177 */ CWORD_CWORD_CWORD_CWORD, |
| 2907 | /* 178 */ CWORD_CWORD_CWORD_CWORD, |
| 2908 | /* 179 */ CWORD_CWORD_CWORD_CWORD, |
| 2909 | /* 180 */ CWORD_CWORD_CWORD_CWORD, |
| 2910 | /* 181 */ CWORD_CWORD_CWORD_CWORD, |
| 2911 | /* 182 */ CWORD_CWORD_CWORD_CWORD, |
| 2912 | /* 183 */ CWORD_CWORD_CWORD_CWORD, |
| 2913 | /* 184 */ CWORD_CWORD_CWORD_CWORD, |
| 2914 | /* 185 */ CWORD_CWORD_CWORD_CWORD, |
| 2915 | /* 186 */ CWORD_CWORD_CWORD_CWORD, |
| 2916 | /* 187 */ CWORD_CWORD_CWORD_CWORD, |
| 2917 | /* 188 */ CWORD_CWORD_CWORD_CWORD, |
| 2918 | /* 189 */ CWORD_CWORD_CWORD_CWORD, |
| 2919 | /* 190 */ CWORD_CWORD_CWORD_CWORD, |
| 2920 | /* 191 */ CWORD_CWORD_CWORD_CWORD, |
| 2921 | /* 192 */ CWORD_CWORD_CWORD_CWORD, |
| 2922 | /* 193 */ CWORD_CWORD_CWORD_CWORD, |
| 2923 | /* 194 */ CWORD_CWORD_CWORD_CWORD, |
| 2924 | /* 195 */ CWORD_CWORD_CWORD_CWORD, |
| 2925 | /* 196 */ CWORD_CWORD_CWORD_CWORD, |
| 2926 | /* 197 */ CWORD_CWORD_CWORD_CWORD, |
| 2927 | /* 198 */ CWORD_CWORD_CWORD_CWORD, |
| 2928 | /* 199 */ CWORD_CWORD_CWORD_CWORD, |
| 2929 | /* 200 */ CWORD_CWORD_CWORD_CWORD, |
| 2930 | /* 201 */ CWORD_CWORD_CWORD_CWORD, |
| 2931 | /* 202 */ CWORD_CWORD_CWORD_CWORD, |
| 2932 | /* 203 */ CWORD_CWORD_CWORD_CWORD, |
| 2933 | /* 204 */ CWORD_CWORD_CWORD_CWORD, |
| 2934 | /* 205 */ CWORD_CWORD_CWORD_CWORD, |
| 2935 | /* 206 */ CWORD_CWORD_CWORD_CWORD, |
| 2936 | /* 207 */ CWORD_CWORD_CWORD_CWORD, |
| 2937 | /* 208 */ CWORD_CWORD_CWORD_CWORD, |
| 2938 | /* 209 */ CWORD_CWORD_CWORD_CWORD, |
| 2939 | /* 210 */ CWORD_CWORD_CWORD_CWORD, |
| 2940 | /* 211 */ CWORD_CWORD_CWORD_CWORD, |
| 2941 | /* 212 */ CWORD_CWORD_CWORD_CWORD, |
| 2942 | /* 213 */ CWORD_CWORD_CWORD_CWORD, |
| 2943 | /* 214 */ CWORD_CWORD_CWORD_CWORD, |
| 2944 | /* 215 */ CWORD_CWORD_CWORD_CWORD, |
| 2945 | /* 216 */ CWORD_CWORD_CWORD_CWORD, |
| 2946 | /* 217 */ CWORD_CWORD_CWORD_CWORD, |
| 2947 | /* 218 */ CWORD_CWORD_CWORD_CWORD, |
| 2948 | /* 219 */ CWORD_CWORD_CWORD_CWORD, |
| 2949 | /* 220 */ CWORD_CWORD_CWORD_CWORD, |
| 2950 | /* 221 */ CWORD_CWORD_CWORD_CWORD, |
| 2951 | /* 222 */ CWORD_CWORD_CWORD_CWORD, |
| 2952 | /* 223 */ CWORD_CWORD_CWORD_CWORD, |
| 2953 | /* 224 */ CWORD_CWORD_CWORD_CWORD, |
| 2954 | /* 225 */ CWORD_CWORD_CWORD_CWORD, |
| 2955 | /* 226 */ CWORD_CWORD_CWORD_CWORD, |
| 2956 | /* 227 */ CWORD_CWORD_CWORD_CWORD, |
| 2957 | /* 228 */ CWORD_CWORD_CWORD_CWORD, |
| 2958 | /* 229 */ CWORD_CWORD_CWORD_CWORD, |
| 2959 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 2960 | /* 231 */ CWORD_CWORD_CWORD_CWORD, |
| 2961 | /* 232 */ CWORD_CWORD_CWORD_CWORD, |
| 2962 | /* 233 */ CWORD_CWORD_CWORD_CWORD, |
| 2963 | /* 234 */ CWORD_CWORD_CWORD_CWORD, |
| 2964 | /* 235 */ CWORD_CWORD_CWORD_CWORD, |
| 2965 | /* 236 */ CWORD_CWORD_CWORD_CWORD, |
| 2966 | /* 237 */ CWORD_CWORD_CWORD_CWORD, |
| 2967 | /* 238 */ CWORD_CWORD_CWORD_CWORD, |
| 2968 | /* 239 */ CWORD_CWORD_CWORD_CWORD, |
| 2969 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 2970 | /* 241 */ CWORD_CWORD_CWORD_CWORD, |
| 2971 | /* 242 */ CWORD_CWORD_CWORD_CWORD, |
| 2972 | /* 243 */ CWORD_CWORD_CWORD_CWORD, |
| 2973 | /* 244 */ CWORD_CWORD_CWORD_CWORD, |
| 2974 | /* 245 */ CWORD_CWORD_CWORD_CWORD, |
| 2975 | /* 246 */ CWORD_CWORD_CWORD_CWORD, |
| 2976 | /* 247 */ CWORD_CWORD_CWORD_CWORD, |
| 2977 | /* 248 */ CWORD_CWORD_CWORD_CWORD, |
| 2978 | /* 249 */ CWORD_CWORD_CWORD_CWORD, |
| 2979 | /* 250 */ CWORD_CWORD_CWORD_CWORD, |
| 2980 | /* 251 */ CWORD_CWORD_CWORD_CWORD, |
| 2981 | /* 252 */ CWORD_CWORD_CWORD_CWORD, |
| 2982 | /* 253 */ CWORD_CWORD_CWORD_CWORD, |
| 2983 | /* 254 */ CWORD_CWORD_CWORD_CWORD, |
| 2984 | /* 255 */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2985 | /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2986 | # if ENABLE_ASH_ALIAS |
| 2987 | /* PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 2988 | # endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2989 | }; |
| 2990 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2991 | # define SIT(c, syntax) ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2992 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2993 | #endif /* !USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2994 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2995 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2996 | /* ============ Alias handling */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 2997 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2998 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2999 | |
| 3000 | #define ALIASINUSE 1 |
| 3001 | #define ALIASDEAD 2 |
| 3002 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3003 | struct alias { |
| 3004 | struct alias *next; |
| 3005 | char *name; |
| 3006 | char *val; |
| 3007 | int flag; |
| 3008 | }; |
| 3009 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 3010 | |
| 3011 | static struct alias **atab; // [ATABSIZE]; |
| 3012 | #define INIT_G_alias() do { \ |
| 3013 | atab = xzalloc(ATABSIZE * sizeof(atab[0])); \ |
| 3014 | } while (0) |
| 3015 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3016 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3017 | static struct alias ** |
| 3018 | __lookupalias(const char *name) { |
| 3019 | unsigned int hashval; |
| 3020 | struct alias **app; |
| 3021 | const char *p; |
| 3022 | unsigned int ch; |
| 3023 | |
| 3024 | p = name; |
| 3025 | |
| 3026 | ch = (unsigned char)*p; |
| 3027 | hashval = ch << 4; |
| 3028 | while (ch) { |
| 3029 | hashval += ch; |
| 3030 | ch = (unsigned char)*++p; |
| 3031 | } |
| 3032 | app = &atab[hashval % ATABSIZE]; |
| 3033 | |
| 3034 | for (; *app; app = &(*app)->next) { |
| 3035 | if (strcmp(name, (*app)->name) == 0) { |
| 3036 | break; |
| 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | return app; |
| 3041 | } |
| 3042 | |
| 3043 | static struct alias * |
| 3044 | lookupalias(const char *name, int check) |
| 3045 | { |
| 3046 | struct alias *ap = *__lookupalias(name); |
| 3047 | |
| 3048 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 3049 | return NULL; |
| 3050 | return ap; |
| 3051 | } |
| 3052 | |
| 3053 | static struct alias * |
| 3054 | freealias(struct alias *ap) |
| 3055 | { |
| 3056 | struct alias *next; |
| 3057 | |
| 3058 | if (ap->flag & ALIASINUSE) { |
| 3059 | ap->flag |= ALIASDEAD; |
| 3060 | return ap; |
| 3061 | } |
| 3062 | |
| 3063 | next = ap->next; |
| 3064 | free(ap->name); |
| 3065 | free(ap->val); |
| 3066 | free(ap); |
| 3067 | return next; |
| 3068 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3069 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3070 | static void |
| 3071 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3072 | { |
| 3073 | struct alias *ap, **app; |
| 3074 | |
| 3075 | app = __lookupalias(name); |
| 3076 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3077 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3078 | if (ap) { |
| 3079 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3080 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3081 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3082 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3083 | ap->flag &= ~ALIASDEAD; |
| 3084 | } else { |
| 3085 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3086 | ap = ckzalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3087 | ap->name = ckstrdup(name); |
| 3088 | ap->val = ckstrdup(val); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3089 | /*ap->flag = 0; - ckzalloc did it */ |
| 3090 | /*ap->next = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3091 | *app = ap; |
| 3092 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3093 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3096 | static int |
| 3097 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3098 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3099 | struct alias **app; |
| 3100 | |
| 3101 | app = __lookupalias(name); |
| 3102 | |
| 3103 | if (*app) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3104 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3105 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3106 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3107 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3110 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3113 | static void |
| 3114 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3115 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3116 | struct alias *ap, **app; |
| 3117 | int i; |
| 3118 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3119 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3120 | for (i = 0; i < ATABSIZE; i++) { |
| 3121 | app = &atab[i]; |
| 3122 | for (ap = *app; ap; ap = *app) { |
| 3123 | *app = freealias(*app); |
| 3124 | if (ap == *app) { |
| 3125 | app = &ap->next; |
| 3126 | } |
| 3127 | } |
| 3128 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3129 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3132 | static void |
| 3133 | printalias(const struct alias *ap) |
| 3134 | { |
| 3135 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3136 | } |
| 3137 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3138 | /* |
| 3139 | * TODO - sort output |
| 3140 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3141 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3142 | aliascmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3143 | { |
| 3144 | char *n, *v; |
| 3145 | int ret = 0; |
| 3146 | struct alias *ap; |
| 3147 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3148 | if (!argv[1]) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3149 | int i; |
| 3150 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3151 | for (i = 0; i < ATABSIZE; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3152 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3153 | printalias(ap); |
| 3154 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3155 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3156 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3157 | } |
| 3158 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3159 | v = strchr(n+1, '='); |
| 3160 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3161 | ap = *__lookupalias(n); |
| 3162 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3163 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3164 | ret = 1; |
| 3165 | } else |
| 3166 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3167 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3168 | *v++ = '\0'; |
| 3169 | setalias(n, v); |
| 3170 | } |
| 3171 | } |
| 3172 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3173 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3176 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3177 | unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3178 | { |
| 3179 | int i; |
| 3180 | |
| 3181 | while ((i = nextopt("a")) != '\0') { |
| 3182 | if (i == 'a') { |
| 3183 | rmaliases(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3184 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3185 | } |
| 3186 | } |
| 3187 | for (i = 0; *argptr; argptr++) { |
| 3188 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3189 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3190 | i = 1; |
| 3191 | } |
| 3192 | } |
| 3193 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3194 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3195 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3196 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3197 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3198 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3199 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3200 | /* ============ jobs.c */ |
| 3201 | |
| 3202 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3203 | #define FORK_FG 0 |
| 3204 | #define FORK_BG 1 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3205 | #define FORK_NOJOB 2 |
| 3206 | |
| 3207 | /* mode flags for showjob(s) */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3208 | #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */ |
| 3209 | #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */ |
| 3210 | #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3211 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3212 | /* |
| 3213 | * A job structure contains information about a job. A job is either a |
| 3214 | * single process or a set of processes contained in a pipeline. In the |
| 3215 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 3216 | * array of pids. |
| 3217 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3218 | struct procstat { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3219 | pid_t ps_pid; /* process id */ |
| 3220 | int ps_status; /* last process status from wait() */ |
| 3221 | char *ps_cmd; /* text of command being run */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3222 | }; |
| 3223 | |
| 3224 | struct job { |
| 3225 | struct procstat ps0; /* status of process */ |
| 3226 | struct procstat *ps; /* status or processes when more than one */ |
| 3227 | #if JOBS |
| 3228 | int stopstatus; /* status of a stopped job */ |
| 3229 | #endif |
| 3230 | uint32_t |
| 3231 | nprocs: 16, /* number of processes */ |
| 3232 | state: 8, |
| 3233 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3234 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3235 | #define JOBDONE 2 /* all procs are completed */ |
| 3236 | #if JOBS |
| 3237 | sigint: 1, /* job was killed by SIGINT */ |
| 3238 | jobctl: 1, /* job running under job control */ |
| 3239 | #endif |
| 3240 | waited: 1, /* true if this entry has been waited for */ |
| 3241 | used: 1, /* true if this entry is in used */ |
| 3242 | changed: 1; /* true if status has changed */ |
| 3243 | struct job *prev_job; /* previous job */ |
| 3244 | }; |
| 3245 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3246 | static struct job *makejob(/*union node *,*/ int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3247 | static int forkshell(struct job *, union node *, int); |
| 3248 | static int waitforjob(struct job *); |
| 3249 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3250 | #if !JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3251 | enum { doing_jobctl = 0 }; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3252 | #define setjobctl(on) do {} while (0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3253 | #else |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3254 | static smallint doing_jobctl; //references:8 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3255 | static void setjobctl(int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3256 | #endif |
| 3257 | |
| 3258 | /* |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3259 | * Ignore a signal. |
| 3260 | */ |
| 3261 | static void |
| 3262 | ignoresig(int signo) |
| 3263 | { |
| 3264 | /* Avoid unnecessary system calls. Is it already SIG_IGNed? */ |
| 3265 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 3266 | /* No, need to do it */ |
| 3267 | signal(signo, SIG_IGN); |
| 3268 | } |
| 3269 | sigmode[signo - 1] = S_HARD_IGN; |
| 3270 | } |
| 3271 | |
| 3272 | /* |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3273 | * Only one usage site - in setsignal() |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3274 | */ |
| 3275 | static void |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3276 | signal_handler(int signo) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3277 | { |
| 3278 | gotsig[signo - 1] = 1; |
| 3279 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3280 | if (signo == SIGINT && !trap[SIGINT]) { |
| 3281 | if (!suppress_int) { |
| 3282 | pending_sig = 0; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3283 | raise_interrupt(); /* does not return */ |
| 3284 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3285 | pending_int = 1; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3286 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3287 | pending_sig = signo; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | /* |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3292 | * Set the signal handler for the specified signal. The routine figures |
| 3293 | * out what it should be set to. |
| 3294 | */ |
| 3295 | static void |
| 3296 | setsignal(int signo) |
| 3297 | { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3298 | char *t; |
| 3299 | char cur_act, new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3300 | struct sigaction act; |
| 3301 | |
| 3302 | t = trap[signo]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3303 | new_act = S_DFL; |
| 3304 | if (t != NULL) { /* trap for this sig is set */ |
| 3305 | new_act = S_CATCH; |
| 3306 | if (t[0] == '\0') /* trap is "": ignore this sig */ |
| 3307 | new_act = S_IGN; |
| 3308 | } |
| 3309 | |
| 3310 | if (rootshell && new_act == S_DFL) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3311 | switch (signo) { |
| 3312 | case SIGINT: |
| 3313 | if (iflag || minusc || sflag == 0) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3314 | new_act = S_CATCH; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3315 | break; |
| 3316 | case SIGQUIT: |
| 3317 | #if DEBUG |
| 3318 | if (debug) |
| 3319 | break; |
| 3320 | #endif |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3321 | /* man bash: |
| 3322 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 3323 | * commands run by bash have signal handlers |
| 3324 | * set to the values inherited by the shell |
| 3325 | * from its parent". */ |
| 3326 | new_act = S_IGN; |
| 3327 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3328 | case SIGTERM: |
| 3329 | if (iflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3330 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3331 | break; |
| 3332 | #if JOBS |
| 3333 | case SIGTSTP: |
| 3334 | case SIGTTOU: |
| 3335 | if (mflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3336 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3337 | break; |
| 3338 | #endif |
| 3339 | } |
| 3340 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3341 | //TODO: if !rootshell, we reset SIGQUIT to DFL, |
| 3342 | //whereas we have to restore it to what shell got on entry |
| 3343 | //from the parent. See comment above |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3344 | |
| 3345 | t = &sigmode[signo - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3346 | cur_act = *t; |
| 3347 | if (cur_act == 0) { |
| 3348 | /* current setting is not yet known */ |
| 3349 | if (sigaction(signo, NULL, &act)) { |
| 3350 | /* pretend it worked; maybe we should give a warning, |
| 3351 | * but other shells don't. We don't alter sigmode, |
| 3352 | * so we retry every time. |
| 3353 | * btw, in Linux it never fails. --vda */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3354 | return; |
| 3355 | } |
| 3356 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3357 | cur_act = S_HARD_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3358 | if (mflag |
| 3359 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
| 3360 | ) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3361 | cur_act = S_IGN; /* don't hard ignore these */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3362 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3363 | } |
| 3364 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3365 | if (cur_act == S_HARD_IGN || cur_act == new_act) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3366 | return; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3367 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3368 | act.sa_handler = SIG_DFL; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3369 | switch (new_act) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3370 | case S_CATCH: |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3371 | act.sa_handler = signal_handler; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3372 | act.sa_flags = 0; /* matters only if !DFL and !IGN */ |
| 3373 | sigfillset(&act.sa_mask); /* ditto */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3374 | break; |
| 3375 | case S_IGN: |
| 3376 | act.sa_handler = SIG_IGN; |
| 3377 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3378 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3379 | sigaction_set(signo, &act); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3380 | |
| 3381 | *t = new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | /* mode flags for set_curjob */ |
| 3385 | #define CUR_DELETE 2 |
| 3386 | #define CUR_RUNNING 1 |
| 3387 | #define CUR_STOPPED 0 |
| 3388 | |
| 3389 | /* mode flags for dowait */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3390 | #define DOWAIT_NONBLOCK WNOHANG |
| 3391 | #define DOWAIT_BLOCK 0 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3392 | |
| 3393 | #if JOBS |
| 3394 | /* pgrp of shell on invocation */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3395 | static int initialpgrp; //references:2 |
| 3396 | static int ttyfd = -1; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3397 | #endif |
| 3398 | /* array of jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3399 | static struct job *jobtab; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3400 | /* size of array */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3401 | static unsigned njobs; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3402 | /* current job */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3403 | static struct job *curjob; //lots |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3404 | /* number of presumed living untracked jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3405 | static int jobless; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3406 | |
| 3407 | static void |
| 3408 | set_curjob(struct job *jp, unsigned mode) |
| 3409 | { |
| 3410 | struct job *jp1; |
| 3411 | struct job **jpp, **curp; |
| 3412 | |
| 3413 | /* first remove from list */ |
| 3414 | jpp = curp = &curjob; |
| 3415 | do { |
| 3416 | jp1 = *jpp; |
| 3417 | if (jp1 == jp) |
| 3418 | break; |
| 3419 | jpp = &jp1->prev_job; |
| 3420 | } while (1); |
| 3421 | *jpp = jp1->prev_job; |
| 3422 | |
| 3423 | /* Then re-insert in correct position */ |
| 3424 | jpp = curp; |
| 3425 | switch (mode) { |
| 3426 | default: |
| 3427 | #if DEBUG |
| 3428 | abort(); |
| 3429 | #endif |
| 3430 | case CUR_DELETE: |
| 3431 | /* job being deleted */ |
| 3432 | break; |
| 3433 | case CUR_RUNNING: |
| 3434 | /* newly created job or backgrounded job, |
| 3435 | put after all stopped jobs. */ |
| 3436 | do { |
| 3437 | jp1 = *jpp; |
| 3438 | #if JOBS |
| 3439 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3440 | #endif |
| 3441 | break; |
| 3442 | jpp = &jp1->prev_job; |
| 3443 | } while (1); |
| 3444 | /* FALLTHROUGH */ |
| 3445 | #if JOBS |
| 3446 | case CUR_STOPPED: |
| 3447 | #endif |
| 3448 | /* newly stopped job - becomes curjob */ |
| 3449 | jp->prev_job = *jpp; |
| 3450 | *jpp = jp; |
| 3451 | break; |
| 3452 | } |
| 3453 | } |
| 3454 | |
| 3455 | #if JOBS || DEBUG |
| 3456 | static int |
| 3457 | jobno(const struct job *jp) |
| 3458 | { |
| 3459 | return jp - jobtab + 1; |
| 3460 | } |
| 3461 | #endif |
| 3462 | |
| 3463 | /* |
| 3464 | * Convert a job name to a job structure. |
| 3465 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3466 | #if !JOBS |
| 3467 | #define getjob(name, getctl) getjob(name) |
| 3468 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3469 | static struct job * |
| 3470 | getjob(const char *name, int getctl) |
| 3471 | { |
| 3472 | struct job *jp; |
| 3473 | struct job *found; |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3474 | const char *err_msg = "%s: no such job"; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3475 | unsigned num; |
| 3476 | int c; |
| 3477 | const char *p; |
| 3478 | char *(*match)(const char *, const char *); |
| 3479 | |
| 3480 | jp = curjob; |
| 3481 | p = name; |
| 3482 | if (!p) |
| 3483 | goto currentjob; |
| 3484 | |
| 3485 | if (*p != '%') |
| 3486 | goto err; |
| 3487 | |
| 3488 | c = *++p; |
| 3489 | if (!c) |
| 3490 | goto currentjob; |
| 3491 | |
| 3492 | if (!p[1]) { |
| 3493 | if (c == '+' || c == '%') { |
| 3494 | currentjob: |
| 3495 | err_msg = "No current job"; |
| 3496 | goto check; |
| 3497 | } |
| 3498 | if (c == '-') { |
| 3499 | if (jp) |
| 3500 | jp = jp->prev_job; |
| 3501 | err_msg = "No previous job"; |
| 3502 | check: |
| 3503 | if (!jp) |
| 3504 | goto err; |
| 3505 | goto gotit; |
| 3506 | } |
| 3507 | } |
| 3508 | |
| 3509 | if (is_number(p)) { |
| 3510 | num = atoi(p); |
| 3511 | if (num < njobs) { |
| 3512 | jp = jobtab + num - 1; |
| 3513 | if (jp->used) |
| 3514 | goto gotit; |
| 3515 | goto err; |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | match = prefix; |
| 3520 | if (*p == '?') { |
| 3521 | match = strstr; |
| 3522 | p++; |
| 3523 | } |
| 3524 | |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3525 | found = NULL; |
| 3526 | while (jp) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3527 | if (match(jp->ps[0].ps_cmd, p)) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3528 | if (found) |
| 3529 | goto err; |
| 3530 | found = jp; |
| 3531 | err_msg = "%s: ambiguous"; |
| 3532 | } |
| 3533 | jp = jp->prev_job; |
| 3534 | } |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3535 | if (!found) |
| 3536 | goto err; |
| 3537 | jp = found; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3538 | |
| 3539 | gotit: |
| 3540 | #if JOBS |
| 3541 | err_msg = "job %s not created under job control"; |
| 3542 | if (getctl && jp->jobctl == 0) |
| 3543 | goto err; |
| 3544 | #endif |
| 3545 | return jp; |
| 3546 | err: |
| 3547 | ash_msg_and_raise_error(err_msg, name); |
| 3548 | } |
| 3549 | |
| 3550 | /* |
| 3551 | * Mark a job structure as unused. |
| 3552 | */ |
| 3553 | static void |
| 3554 | freejob(struct job *jp) |
| 3555 | { |
| 3556 | struct procstat *ps; |
| 3557 | int i; |
| 3558 | |
| 3559 | INT_OFF; |
| 3560 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3561 | if (ps->ps_cmd != nullstr) |
| 3562 | free(ps->ps_cmd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3563 | } |
| 3564 | if (jp->ps != &jp->ps0) |
| 3565 | free(jp->ps); |
| 3566 | jp->used = 0; |
| 3567 | set_curjob(jp, CUR_DELETE); |
| 3568 | INT_ON; |
| 3569 | } |
| 3570 | |
| 3571 | #if JOBS |
| 3572 | static void |
| 3573 | xtcsetpgrp(int fd, pid_t pgrp) |
| 3574 | { |
| 3575 | if (tcsetpgrp(fd, pgrp)) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 3576 | ash_msg_and_raise_error("can't set tty process group (%m)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | /* |
| 3580 | * Turn job control on and off. |
| 3581 | * |
| 3582 | * Note: This code assumes that the third arg to ioctl is a character |
| 3583 | * pointer, which is true on Berkeley systems but not System V. Since |
| 3584 | * System V doesn't have job control yet, this isn't a problem now. |
| 3585 | * |
| 3586 | * Called with interrupts off. |
| 3587 | */ |
| 3588 | static void |
| 3589 | setjobctl(int on) |
| 3590 | { |
| 3591 | int fd; |
| 3592 | int pgrp; |
| 3593 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3594 | if (on == doing_jobctl || rootshell == 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3595 | return; |
| 3596 | if (on) { |
| 3597 | int ofd; |
| 3598 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 3599 | if (fd < 0) { |
| 3600 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 3601 | * That sometimes helps to acquire controlling tty. |
| 3602 | * Obviously, a workaround for bugs when someone |
| 3603 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3604 | fd = 2; |
| 3605 | while (!isatty(fd)) |
| 3606 | if (--fd < 0) |
| 3607 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3608 | } |
| 3609 | fd = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3610 | if (ofd >= 0) |
| 3611 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3612 | if (fd < 0) |
| 3613 | goto out; |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3614 | /* fd is a tty at this point */ |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 3615 | close_on_exec_on(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3616 | do { /* while we are in the background */ |
| 3617 | pgrp = tcgetpgrp(fd); |
| 3618 | if (pgrp < 0) { |
| 3619 | out: |
| 3620 | ash_msg("can't access tty; job control turned off"); |
| 3621 | mflag = on = 0; |
| 3622 | goto close; |
| 3623 | } |
| 3624 | if (pgrp == getpgrp()) |
| 3625 | break; |
| 3626 | killpg(0, SIGTTIN); |
| 3627 | } while (1); |
| 3628 | initialpgrp = pgrp; |
| 3629 | |
| 3630 | setsignal(SIGTSTP); |
| 3631 | setsignal(SIGTTOU); |
| 3632 | setsignal(SIGTTIN); |
| 3633 | pgrp = rootpid; |
| 3634 | setpgid(0, pgrp); |
| 3635 | xtcsetpgrp(fd, pgrp); |
| 3636 | } else { |
| 3637 | /* turning job control off */ |
| 3638 | fd = ttyfd; |
| 3639 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3640 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3641 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3642 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3643 | setpgid(0, pgrp); |
| 3644 | setsignal(SIGTSTP); |
| 3645 | setsignal(SIGTTOU); |
| 3646 | setsignal(SIGTTIN); |
| 3647 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3648 | if (fd >= 0) |
| 3649 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3650 | fd = -1; |
| 3651 | } |
| 3652 | ttyfd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3653 | doing_jobctl = on; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3656 | static int FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3657 | killcmd(int argc, char **argv) |
| 3658 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3659 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3660 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3661 | do { |
| 3662 | if (argv[i][0] == '%') { |
| 3663 | struct job *jp = getjob(argv[i], 0); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3664 | unsigned pid = jp->ps[0].ps_pid; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3665 | /* Enough space for ' -NNN<nul>' */ |
| 3666 | argv[i] = alloca(sizeof(int)*3 + 3); |
| 3667 | /* kill_main has matching code to expect |
| 3668 | * leading space. Needed to not confuse |
| 3669 | * negative pids with "kill -SIGNAL_NO" syntax */ |
| 3670 | sprintf(argv[i], " -%u", pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3671 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3672 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3673 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3674 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | static void |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3678 | showpipe(struct job *jp /*, FILE *out*/) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3679 | { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3680 | struct procstat *ps; |
| 3681 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3682 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3683 | psend = jp->ps + jp->nprocs; |
| 3684 | for (ps = jp->ps + 1; ps < psend; ps++) |
| 3685 | printf(" | %s", ps->ps_cmd); |
| 3686 | outcslow('\n', stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3687 | flush_stdout_stderr(); |
| 3688 | } |
| 3689 | |
| 3690 | |
| 3691 | static int |
| 3692 | restartjob(struct job *jp, int mode) |
| 3693 | { |
| 3694 | struct procstat *ps; |
| 3695 | int i; |
| 3696 | int status; |
| 3697 | pid_t pgid; |
| 3698 | |
| 3699 | INT_OFF; |
| 3700 | if (jp->state == JOBDONE) |
| 3701 | goto out; |
| 3702 | jp->state = JOBRUNNING; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3703 | pgid = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3704 | if (mode == FORK_FG) |
| 3705 | xtcsetpgrp(ttyfd, pgid); |
| 3706 | killpg(pgid, SIGCONT); |
| 3707 | ps = jp->ps; |
| 3708 | i = jp->nprocs; |
| 3709 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3710 | if (WIFSTOPPED(ps->ps_status)) { |
| 3711 | ps->ps_status = -1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3712 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3713 | ps++; |
| 3714 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3715 | out: |
| 3716 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 3717 | INT_ON; |
| 3718 | return status; |
| 3719 | } |
| 3720 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3721 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3722 | fg_bgcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3723 | { |
| 3724 | struct job *jp; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3725 | int mode; |
| 3726 | int retval; |
| 3727 | |
| 3728 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 3729 | nextopt(nullstr); |
| 3730 | argv = argptr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3731 | do { |
| 3732 | jp = getjob(*argv, 1); |
| 3733 | if (mode == FORK_BG) { |
| 3734 | set_curjob(jp, CUR_RUNNING); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3735 | printf("[%d] ", jobno(jp)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3736 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3737 | out1str(jp->ps[0].ps_cmd); |
| 3738 | showpipe(jp /*, stdout*/); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3739 | retval = restartjob(jp, mode); |
| 3740 | } while (*argv && *++argv); |
| 3741 | return retval; |
| 3742 | } |
| 3743 | #endif |
| 3744 | |
| 3745 | static int |
| 3746 | sprint_status(char *s, int status, int sigonly) |
| 3747 | { |
| 3748 | int col; |
| 3749 | int st; |
| 3750 | |
| 3751 | col = 0; |
| 3752 | if (!WIFEXITED(status)) { |
| 3753 | #if JOBS |
| 3754 | if (WIFSTOPPED(status)) |
| 3755 | st = WSTOPSIG(status); |
| 3756 | else |
| 3757 | #endif |
| 3758 | st = WTERMSIG(status); |
| 3759 | if (sigonly) { |
| 3760 | if (st == SIGINT || st == SIGPIPE) |
| 3761 | goto out; |
| 3762 | #if JOBS |
| 3763 | if (WIFSTOPPED(status)) |
| 3764 | goto out; |
| 3765 | #endif |
| 3766 | } |
| 3767 | st &= 0x7f; |
| 3768 | col = fmtstr(s, 32, strsignal(st)); |
| 3769 | if (WCOREDUMP(status)) { |
| 3770 | col += fmtstr(s + col, 16, " (core dumped)"); |
| 3771 | } |
| 3772 | } else if (!sigonly) { |
| 3773 | st = WEXITSTATUS(status); |
| 3774 | if (st) |
| 3775 | col = fmtstr(s, 16, "Done(%d)", st); |
| 3776 | else |
| 3777 | col = fmtstr(s, 16, "Done"); |
| 3778 | } |
| 3779 | out: |
| 3780 | return col; |
| 3781 | } |
| 3782 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3783 | static int |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3784 | dowait(int wait_flags, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3785 | { |
| 3786 | int pid; |
| 3787 | int status; |
| 3788 | struct job *jp; |
| 3789 | struct job *thisjob; |
| 3790 | int state; |
| 3791 | |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 3792 | TRACE(("dowait(0x%x) called\n", wait_flags)); |
| 3793 | |
| 3794 | /* Do a wait system call. If job control is compiled in, we accept |
| 3795 | * stopped processes. wait_flags may have WNOHANG, preventing blocking. |
| 3796 | * NB: _not_ safe_waitpid, we need to detect EINTR */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3797 | if (doing_jobctl) |
| 3798 | wait_flags |= WUNTRACED; |
| 3799 | pid = waitpid(-1, &status, wait_flags); |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 3800 | TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n", |
| 3801 | pid, status, errno, strerror(errno))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3802 | if (pid <= 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3803 | return pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3804 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3805 | INT_OFF; |
| 3806 | thisjob = NULL; |
| 3807 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3808 | struct procstat *ps; |
| 3809 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3810 | if (jp->state == JOBDONE) |
| 3811 | continue; |
| 3812 | state = JOBDONE; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3813 | ps = jp->ps; |
| 3814 | psend = ps + jp->nprocs; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3815 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3816 | if (ps->ps_pid == pid) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3817 | TRACE(("Job %d: changing status of proc %d " |
| 3818 | "from 0x%x to 0x%x\n", |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3819 | jobno(jp), pid, ps->ps_status, status)); |
| 3820 | ps->ps_status = status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3821 | thisjob = jp; |
| 3822 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3823 | if (ps->ps_status == -1) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3824 | state = JOBRUNNING; |
| 3825 | #if JOBS |
| 3826 | if (state == JOBRUNNING) |
| 3827 | continue; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3828 | if (WIFSTOPPED(ps->ps_status)) { |
| 3829 | jp->stopstatus = ps->ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3830 | state = JOBSTOPPED; |
| 3831 | } |
| 3832 | #endif |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3833 | } while (++ps < psend); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3834 | if (thisjob) |
| 3835 | goto gotjob; |
| 3836 | } |
| 3837 | #if JOBS |
| 3838 | if (!WIFSTOPPED(status)) |
| 3839 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3840 | jobless--; |
| 3841 | goto out; |
| 3842 | |
| 3843 | gotjob: |
| 3844 | if (state != JOBRUNNING) { |
| 3845 | thisjob->changed = 1; |
| 3846 | |
| 3847 | if (thisjob->state != state) { |
| 3848 | TRACE(("Job %d: changing state from %d to %d\n", |
| 3849 | jobno(thisjob), thisjob->state, state)); |
| 3850 | thisjob->state = state; |
| 3851 | #if JOBS |
| 3852 | if (state == JOBSTOPPED) { |
| 3853 | set_curjob(thisjob, CUR_STOPPED); |
| 3854 | } |
| 3855 | #endif |
| 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | out: |
| 3860 | INT_ON; |
| 3861 | |
| 3862 | if (thisjob && thisjob == job) { |
| 3863 | char s[48 + 1]; |
| 3864 | int len; |
| 3865 | |
| 3866 | len = sprint_status(s, status, 1); |
| 3867 | if (len) { |
| 3868 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3869 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3870 | out2str(s); |
| 3871 | } |
| 3872 | } |
| 3873 | return pid; |
| 3874 | } |
| 3875 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3876 | static int |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 3877 | blocking_wait_with_raise_on_sig(void) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3878 | { |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 3879 | pid_t pid = dowait(DOWAIT_BLOCK, NULL); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3880 | if (pid <= 0 && pending_sig) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3881 | raise_exception(EXSIG); |
| 3882 | return pid; |
| 3883 | } |
| 3884 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3885 | #if JOBS |
| 3886 | static void |
| 3887 | showjob(FILE *out, struct job *jp, int mode) |
| 3888 | { |
| 3889 | struct procstat *ps; |
| 3890 | struct procstat *psend; |
| 3891 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3892 | int indent_col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3893 | char s[80]; |
| 3894 | |
| 3895 | ps = jp->ps; |
| 3896 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3897 | if (mode & SHOW_ONLY_PGID) { /* jobs -p */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3898 | /* just output process (group) id of pipeline */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3899 | fprintf(out, "%d\n", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3900 | return; |
| 3901 | } |
| 3902 | |
| 3903 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3904 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3905 | |
| 3906 | if (jp == curjob) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3907 | s[col - 3] = '+'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3908 | else if (curjob && jp == curjob->prev_job) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3909 | s[col - 3] = '-'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3910 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3911 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3912 | col += fmtstr(s + col, 16, "%d ", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3913 | |
| 3914 | psend = ps + jp->nprocs; |
| 3915 | |
| 3916 | if (jp->state == JOBRUNNING) { |
| 3917 | strcpy(s + col, "Running"); |
| 3918 | col += sizeof("Running") - 1; |
| 3919 | } else { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3920 | int status = psend[-1].ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3921 | if (jp->state == JOBSTOPPED) |
| 3922 | status = jp->stopstatus; |
| 3923 | col += sprint_status(s + col, status, 0); |
| 3924 | } |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3925 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3926 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3927 | /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line |
| 3928 | * or prints several "PID | <cmdN>" lines, |
| 3929 | * depending on SHOW_PIDS bit. |
| 3930 | * We do not print status of individual processes |
| 3931 | * between PID and <cmdN>. bash does it, but not very well: |
| 3932 | * first line shows overall job status, not process status, |
| 3933 | * making it impossible to know 1st process status. |
| 3934 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3935 | goto start; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3936 | do { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3937 | /* for each process */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3938 | s[0] = '\0'; |
| 3939 | col = 33; |
| 3940 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3941 | col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->ps_pid) - 1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3942 | start: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3943 | fprintf(out, "%s%*c%s%s", |
| 3944 | s, |
| 3945 | 33 - col >= 0 ? 33 - col : 0, ' ', |
| 3946 | ps == jp->ps ? "" : "| ", |
| 3947 | ps->ps_cmd |
| 3948 | ); |
| 3949 | } while (++ps != psend); |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3950 | outcslow('\n', out); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3951 | |
| 3952 | jp->changed = 0; |
| 3953 | |
| 3954 | if (jp->state == JOBDONE) { |
| 3955 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 3956 | freejob(jp); |
| 3957 | } |
| 3958 | } |
| 3959 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3960 | /* |
| 3961 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 3962 | * statuses have changed since the last call to showjobs. |
| 3963 | */ |
| 3964 | static void |
| 3965 | showjobs(FILE *out, int mode) |
| 3966 | { |
| 3967 | struct job *jp; |
| 3968 | |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 3969 | TRACE(("showjobs(0x%x) called\n", mode)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3970 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3971 | /* Handle all finished jobs */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3972 | while (dowait(DOWAIT_NONBLOCK, NULL) > 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3973 | continue; |
| 3974 | |
| 3975 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3976 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3977 | showjob(out, jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3978 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3979 | } |
| 3980 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3981 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3982 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3983 | jobscmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3984 | { |
| 3985 | int mode, m; |
| 3986 | |
| 3987 | mode = 0; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3988 | while ((m = nextopt("lp")) != '\0') { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3989 | if (m == 'l') |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3990 | mode |= SHOW_PIDS; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3991 | else |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3992 | mode |= SHOW_ONLY_PGID; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
| 3995 | argv = argptr; |
| 3996 | if (*argv) { |
| 3997 | do |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3998 | showjob(stdout, getjob(*argv, 0), mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3999 | while (*++argv); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4000 | } else { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4001 | showjobs(stdout, mode); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4002 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4003 | |
| 4004 | return 0; |
| 4005 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4006 | #endif /* JOBS */ |
| 4007 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4008 | /* Called only on finished or stopped jobs (no members are running) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4009 | static int |
| 4010 | getstatus(struct job *job) |
| 4011 | { |
| 4012 | int status; |
| 4013 | int retval; |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4014 | struct procstat *ps; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4015 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4016 | /* Fetch last member's status */ |
| 4017 | ps = job->ps + job->nprocs - 1; |
| 4018 | status = ps->ps_status; |
| 4019 | if (pipefail) { |
| 4020 | /* "set -o pipefail" mode: use last _nonzero_ status */ |
| 4021 | while (status == 0 && --ps >= job->ps) |
| 4022 | status = ps->ps_status; |
| 4023 | } |
| 4024 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4025 | retval = WEXITSTATUS(status); |
| 4026 | if (!WIFEXITED(status)) { |
| 4027 | #if JOBS |
| 4028 | retval = WSTOPSIG(status); |
| 4029 | if (!WIFSTOPPED(status)) |
| 4030 | #endif |
| 4031 | { |
| 4032 | /* XXX: limits number of signals */ |
| 4033 | retval = WTERMSIG(status); |
| 4034 | #if JOBS |
| 4035 | if (retval == SIGINT) |
| 4036 | job->sigint = 1; |
| 4037 | #endif |
| 4038 | } |
| 4039 | retval += 128; |
| 4040 | } |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4041 | TRACE(("getstatus: job %d, nproc %d, status 0x%x, retval 0x%x\n", |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4042 | jobno(job), job->nprocs, status, retval)); |
| 4043 | return retval; |
| 4044 | } |
| 4045 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4046 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4047 | waitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4048 | { |
| 4049 | struct job *job; |
| 4050 | int retval; |
| 4051 | struct job *jp; |
| 4052 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4053 | if (pending_sig) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4054 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4055 | |
| 4056 | nextopt(nullstr); |
| 4057 | retval = 0; |
| 4058 | |
| 4059 | argv = argptr; |
| 4060 | if (!*argv) { |
| 4061 | /* wait for all jobs */ |
| 4062 | for (;;) { |
| 4063 | jp = curjob; |
| 4064 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4065 | if (!jp) /* no running procs */ |
| 4066 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4067 | if (jp->state == JOBRUNNING) |
| 4068 | break; |
| 4069 | jp->waited = 1; |
| 4070 | jp = jp->prev_job; |
| 4071 | } |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4072 | blocking_wait_with_raise_on_sig(); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4073 | /* man bash: |
| 4074 | * "When bash is waiting for an asynchronous command via |
| 4075 | * the wait builtin, the reception of a signal for which a trap |
| 4076 | * has been set will cause the wait builtin to return immediately |
| 4077 | * with an exit status greater than 128, immediately after which |
| 4078 | * the trap is executed." |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4079 | * |
| 4080 | * blocking_wait_with_raise_on_sig raises signal handlers |
| 4081 | * if it gets no pid (pid < 0). However, |
| 4082 | * if child sends us a signal *and immediately exits*, |
| 4083 | * blocking_wait_with_raise_on_sig gets pid > 0 |
| 4084 | * and does not handle pending_sig. Check this case: */ |
| 4085 | if (pending_sig) |
| 4086 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | retval = 127; |
| 4091 | do { |
| 4092 | if (**argv != '%') { |
| 4093 | pid_t pid = number(*argv); |
| 4094 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4095 | while (1) { |
| 4096 | if (!job) |
| 4097 | goto repeat; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4098 | if (job->ps[job->nprocs - 1].ps_pid == pid) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4099 | break; |
| 4100 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4101 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4102 | } else |
| 4103 | job = getjob(*argv, 0); |
| 4104 | /* loop until process terminated or stopped */ |
| 4105 | while (job->state == JOBRUNNING) |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4106 | blocking_wait_with_raise_on_sig(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4107 | job->waited = 1; |
| 4108 | retval = getstatus(job); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4109 | repeat: ; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4110 | } while (*++argv); |
| 4111 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4112 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4113 | return retval; |
| 4114 | } |
| 4115 | |
| 4116 | static struct job * |
| 4117 | growjobtab(void) |
| 4118 | { |
| 4119 | size_t len; |
| 4120 | ptrdiff_t offset; |
| 4121 | struct job *jp, *jq; |
| 4122 | |
| 4123 | len = njobs * sizeof(*jp); |
| 4124 | jq = jobtab; |
| 4125 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4126 | |
| 4127 | offset = (char *)jp - (char *)jq; |
| 4128 | if (offset) { |
| 4129 | /* Relocate pointers */ |
| 4130 | size_t l = len; |
| 4131 | |
| 4132 | jq = (struct job *)((char *)jq + l); |
| 4133 | while (l) { |
| 4134 | l -= sizeof(*jp); |
| 4135 | jq--; |
| 4136 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4137 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4138 | if (joff(jp)->ps == &jq->ps0) |
| 4139 | jmove(joff(jp)->ps); |
| 4140 | if (joff(jp)->prev_job) |
| 4141 | jmove(joff(jp)->prev_job); |
| 4142 | } |
| 4143 | if (curjob) |
| 4144 | jmove(curjob); |
| 4145 | #undef joff |
| 4146 | #undef jmove |
| 4147 | } |
| 4148 | |
| 4149 | njobs += 4; |
| 4150 | jobtab = jp; |
| 4151 | jp = (struct job *)((char *)jp + len); |
| 4152 | jq = jp + 3; |
| 4153 | do { |
| 4154 | jq->used = 0; |
| 4155 | } while (--jq >= jp); |
| 4156 | return jp; |
| 4157 | } |
| 4158 | |
| 4159 | /* |
| 4160 | * Return a new job structure. |
| 4161 | * Called with interrupts off. |
| 4162 | */ |
| 4163 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4164 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4165 | { |
| 4166 | int i; |
| 4167 | struct job *jp; |
| 4168 | |
| 4169 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4170 | if (--i < 0) { |
| 4171 | jp = growjobtab(); |
| 4172 | break; |
| 4173 | } |
| 4174 | if (jp->used == 0) |
| 4175 | break; |
| 4176 | if (jp->state != JOBDONE || !jp->waited) |
| 4177 | continue; |
| 4178 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4179 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4180 | continue; |
| 4181 | #endif |
| 4182 | freejob(jp); |
| 4183 | break; |
| 4184 | } |
| 4185 | memset(jp, 0, sizeof(*jp)); |
| 4186 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4187 | /* jp->jobctl is a bitfield. |
| 4188 | * "jp->jobctl |= jobctl" likely to give awful code */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4189 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4190 | jp->jobctl = 1; |
| 4191 | #endif |
| 4192 | jp->prev_job = curjob; |
| 4193 | curjob = jp; |
| 4194 | jp->used = 1; |
| 4195 | jp->ps = &jp->ps0; |
| 4196 | if (nprocs > 1) { |
| 4197 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4198 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4199 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4200 | jobno(jp))); |
| 4201 | return jp; |
| 4202 | } |
| 4203 | |
| 4204 | #if JOBS |
| 4205 | /* |
| 4206 | * Return a string identifying a command (to be printed by the |
| 4207 | * jobs command). |
| 4208 | */ |
| 4209 | static char *cmdnextc; |
| 4210 | |
| 4211 | static void |
| 4212 | cmdputs(const char *s) |
| 4213 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4214 | static const char vstype[VSTYPE + 1][3] = { |
| 4215 | "", "}", "-", "+", "?", "=", |
| 4216 | "%", "%%", "#", "##" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4217 | IF_ASH_BASH_COMPAT(, ":", "/", "//") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4218 | }; |
| 4219 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4220 | const char *p, *str; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4221 | char cc[2]; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4222 | char *nextc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 4223 | unsigned char c; |
| 4224 | unsigned char subtype = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4225 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4226 | |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4227 | cc[1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4228 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4229 | p = s; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4230 | while ((c = *p++) != '\0') { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 4231 | str = NULL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4232 | switch (c) { |
| 4233 | case CTLESC: |
| 4234 | c = *p++; |
| 4235 | break; |
| 4236 | case CTLVAR: |
| 4237 | subtype = *p++; |
| 4238 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4239 | str = "${#"; |
| 4240 | else |
| 4241 | str = "${"; |
| 4242 | if (!(subtype & VSQUOTE) == !(quoted & 1)) |
| 4243 | goto dostr; |
| 4244 | quoted ^= 1; |
| 4245 | c = '"'; |
| 4246 | break; |
| 4247 | case CTLENDVAR: |
| 4248 | str = "\"}" + !(quoted & 1); |
| 4249 | quoted >>= 1; |
| 4250 | subtype = 0; |
| 4251 | goto dostr; |
| 4252 | case CTLBACKQ: |
| 4253 | str = "$(...)"; |
| 4254 | goto dostr; |
| 4255 | case CTLBACKQ+CTLQUOTE: |
| 4256 | str = "\"$(...)\""; |
| 4257 | goto dostr; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4258 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4259 | case CTLARI: |
| 4260 | str = "$(("; |
| 4261 | goto dostr; |
| 4262 | case CTLENDARI: |
| 4263 | str = "))"; |
| 4264 | goto dostr; |
| 4265 | #endif |
| 4266 | case CTLQUOTEMARK: |
| 4267 | quoted ^= 1; |
| 4268 | c = '"'; |
| 4269 | break; |
| 4270 | case '=': |
| 4271 | if (subtype == 0) |
| 4272 | break; |
| 4273 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4274 | quoted <<= 1; |
| 4275 | str = vstype[subtype & VSTYPE]; |
| 4276 | if (subtype & VSNUL) |
| 4277 | c = ':'; |
| 4278 | else |
| 4279 | goto checkstr; |
| 4280 | break; |
| 4281 | case '\'': |
| 4282 | case '\\': |
| 4283 | case '"': |
| 4284 | case '$': |
| 4285 | /* These can only happen inside quotes */ |
| 4286 | cc[0] = c; |
| 4287 | str = cc; |
| 4288 | c = '\\'; |
| 4289 | break; |
| 4290 | default: |
| 4291 | break; |
| 4292 | } |
| 4293 | USTPUTC(c, nextc); |
| 4294 | checkstr: |
| 4295 | if (!str) |
| 4296 | continue; |
| 4297 | dostr: |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4298 | while ((c = *str++) != '\0') { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4299 | USTPUTC(c, nextc); |
| 4300 | } |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4301 | } /* while *p++ not NUL */ |
| 4302 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4303 | if (quoted & 1) { |
| 4304 | USTPUTC('"', nextc); |
| 4305 | } |
| 4306 | *nextc = 0; |
| 4307 | cmdnextc = nextc; |
| 4308 | } |
| 4309 | |
| 4310 | /* cmdtxt() and cmdlist() call each other */ |
| 4311 | static void cmdtxt(union node *n); |
| 4312 | |
| 4313 | static void |
| 4314 | cmdlist(union node *np, int sep) |
| 4315 | { |
| 4316 | for (; np; np = np->narg.next) { |
| 4317 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4318 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4319 | cmdtxt(np); |
| 4320 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4321 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | static void |
| 4326 | cmdtxt(union node *n) |
| 4327 | { |
| 4328 | union node *np; |
| 4329 | struct nodelist *lp; |
| 4330 | const char *p; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4331 | |
| 4332 | if (!n) |
| 4333 | return; |
| 4334 | switch (n->type) { |
| 4335 | default: |
| 4336 | #if DEBUG |
| 4337 | abort(); |
| 4338 | #endif |
| 4339 | case NPIPE: |
| 4340 | lp = n->npipe.cmdlist; |
| 4341 | for (;;) { |
| 4342 | cmdtxt(lp->n); |
| 4343 | lp = lp->next; |
| 4344 | if (!lp) |
| 4345 | break; |
| 4346 | cmdputs(" | "); |
| 4347 | } |
| 4348 | break; |
| 4349 | case NSEMI: |
| 4350 | p = "; "; |
| 4351 | goto binop; |
| 4352 | case NAND: |
| 4353 | p = " && "; |
| 4354 | goto binop; |
| 4355 | case NOR: |
| 4356 | p = " || "; |
| 4357 | binop: |
| 4358 | cmdtxt(n->nbinary.ch1); |
| 4359 | cmdputs(p); |
| 4360 | n = n->nbinary.ch2; |
| 4361 | goto donode; |
| 4362 | case NREDIR: |
| 4363 | case NBACKGND: |
| 4364 | n = n->nredir.n; |
| 4365 | goto donode; |
| 4366 | case NNOT: |
| 4367 | cmdputs("!"); |
| 4368 | n = n->nnot.com; |
| 4369 | donode: |
| 4370 | cmdtxt(n); |
| 4371 | break; |
| 4372 | case NIF: |
| 4373 | cmdputs("if "); |
| 4374 | cmdtxt(n->nif.test); |
| 4375 | cmdputs("; then "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4376 | if (n->nif.elsepart) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4377 | cmdtxt(n->nif.ifpart); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4378 | cmdputs("; else "); |
| 4379 | n = n->nif.elsepart; |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4380 | } else { |
| 4381 | n = n->nif.ifpart; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4382 | } |
| 4383 | p = "; fi"; |
| 4384 | goto dotail; |
| 4385 | case NSUBSHELL: |
| 4386 | cmdputs("("); |
| 4387 | n = n->nredir.n; |
| 4388 | p = ")"; |
| 4389 | goto dotail; |
| 4390 | case NWHILE: |
| 4391 | p = "while "; |
| 4392 | goto until; |
| 4393 | case NUNTIL: |
| 4394 | p = "until "; |
| 4395 | until: |
| 4396 | cmdputs(p); |
| 4397 | cmdtxt(n->nbinary.ch1); |
| 4398 | n = n->nbinary.ch2; |
| 4399 | p = "; done"; |
| 4400 | dodo: |
| 4401 | cmdputs("; do "); |
| 4402 | dotail: |
| 4403 | cmdtxt(n); |
| 4404 | goto dotail2; |
| 4405 | case NFOR: |
| 4406 | cmdputs("for "); |
| 4407 | cmdputs(n->nfor.var); |
| 4408 | cmdputs(" in "); |
| 4409 | cmdlist(n->nfor.args, 1); |
| 4410 | n = n->nfor.body; |
| 4411 | p = "; done"; |
| 4412 | goto dodo; |
| 4413 | case NDEFUN: |
| 4414 | cmdputs(n->narg.text); |
| 4415 | p = "() { ... }"; |
| 4416 | goto dotail2; |
| 4417 | case NCMD: |
| 4418 | cmdlist(n->ncmd.args, 1); |
| 4419 | cmdlist(n->ncmd.redirect, 0); |
| 4420 | break; |
| 4421 | case NARG: |
| 4422 | p = n->narg.text; |
| 4423 | dotail2: |
| 4424 | cmdputs(p); |
| 4425 | break; |
| 4426 | case NHERE: |
| 4427 | case NXHERE: |
| 4428 | p = "<<..."; |
| 4429 | goto dotail2; |
| 4430 | case NCASE: |
| 4431 | cmdputs("case "); |
| 4432 | cmdputs(n->ncase.expr->narg.text); |
| 4433 | cmdputs(" in "); |
| 4434 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 4435 | cmdtxt(np->nclist.pattern); |
| 4436 | cmdputs(") "); |
| 4437 | cmdtxt(np->nclist.body); |
| 4438 | cmdputs(";; "); |
| 4439 | } |
| 4440 | p = "esac"; |
| 4441 | goto dotail2; |
| 4442 | case NTO: |
| 4443 | p = ">"; |
| 4444 | goto redir; |
| 4445 | case NCLOBBER: |
| 4446 | p = ">|"; |
| 4447 | goto redir; |
| 4448 | case NAPPEND: |
| 4449 | p = ">>"; |
| 4450 | goto redir; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4451 | #if ENABLE_ASH_BASH_COMPAT |
| 4452 | case NTO2: |
| 4453 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4454 | case NTOFD: |
| 4455 | p = ">&"; |
| 4456 | goto redir; |
| 4457 | case NFROM: |
| 4458 | p = "<"; |
| 4459 | goto redir; |
| 4460 | case NFROMFD: |
| 4461 | p = "<&"; |
| 4462 | goto redir; |
| 4463 | case NFROMTO: |
| 4464 | p = "<>"; |
| 4465 | redir: |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4466 | cmdputs(utoa(n->nfile.fd)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4467 | cmdputs(p); |
| 4468 | if (n->type == NTOFD || n->type == NFROMFD) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4469 | cmdputs(utoa(n->ndup.dupfd)); |
| 4470 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4471 | } |
| 4472 | n = n->nfile.fname; |
| 4473 | goto donode; |
| 4474 | } |
| 4475 | } |
| 4476 | |
| 4477 | static char * |
| 4478 | commandtext(union node *n) |
| 4479 | { |
| 4480 | char *name; |
| 4481 | |
| 4482 | STARTSTACKSTR(cmdnextc); |
| 4483 | cmdtxt(n); |
| 4484 | name = stackblock(); |
| 4485 | TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n", |
| 4486 | name, cmdnextc, cmdnextc)); |
| 4487 | return ckstrdup(name); |
| 4488 | } |
| 4489 | #endif /* JOBS */ |
| 4490 | |
| 4491 | /* |
| 4492 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 4493 | * own process group. Jp is a job structure that the job is to be added to. |
| 4494 | * N is the command that will be evaluated by the child. Both jp and n may |
| 4495 | * be NULL. The mode parameter can be one of the following: |
| 4496 | * FORK_FG - Fork off a foreground process. |
| 4497 | * FORK_BG - Fork off a background process. |
| 4498 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 4499 | * process group even if job control is on. |
| 4500 | * |
| 4501 | * When job control is turned off, background processes have their standard |
| 4502 | * input redirected to /dev/null (except for the second and later processes |
| 4503 | * in a pipeline). |
| 4504 | * |
| 4505 | * Called with interrupts off. |
| 4506 | */ |
| 4507 | /* |
| 4508 | * Clear traps on a fork. |
| 4509 | */ |
| 4510 | static void |
| 4511 | clear_traps(void) |
| 4512 | { |
| 4513 | char **tp; |
| 4514 | |
| 4515 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4516 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4517 | INT_OFF; |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4518 | if (trap_ptr == trap) |
| 4519 | free(*tp); |
| 4520 | /* else: it "belongs" to trap_ptr vector, don't free */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4521 | *tp = NULL; |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 4522 | if ((tp - trap) != 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4523 | setsignal(tp - trap); |
| 4524 | INT_ON; |
| 4525 | } |
| 4526 | } |
| 4527 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4528 | |
| 4529 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4530 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 4531 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4532 | /* Called after fork(), in child */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4533 | static NOINLINE void |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4534 | forkchild(struct job *jp, union node *n, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4535 | { |
| 4536 | int oldlvl; |
| 4537 | |
| 4538 | TRACE(("Child shell %d\n", getpid())); |
| 4539 | oldlvl = shlvl; |
| 4540 | shlvl++; |
| 4541 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4542 | /* man bash: "Non-builtin commands run by bash have signal handlers |
| 4543 | * set to the values inherited by the shell from its parent". |
| 4544 | * Do we do it correctly? */ |
| 4545 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4546 | closescript(); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4547 | |
| 4548 | if (mode == FORK_NOJOB /* is it `xxx` ? */ |
| 4549 | && n && n->type == NCMD /* is it single cmd? */ |
| 4550 | /* && n->ncmd.args->type == NARG - always true? */ |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4551 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4552 | && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */ |
| 4553 | /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */ |
| 4554 | ) { |
| 4555 | TRACE(("Trap hack\n")); |
| 4556 | /* Awful hack for `trap` or $(trap). |
| 4557 | * |
| 4558 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 4559 | * contains an example where "trap" is executed in a subshell: |
| 4560 | * |
| 4561 | * save_traps=$(trap) |
| 4562 | * ... |
| 4563 | * eval "$save_traps" |
| 4564 | * |
| 4565 | * Standard does not say that "trap" in subshell shall print |
| 4566 | * parent shell's traps. It only says that its output |
| 4567 | * must have suitable form, but then, in the above example |
| 4568 | * (which is not supposed to be normative), it implies that. |
| 4569 | * |
| 4570 | * bash (and probably other shell) does implement it |
| 4571 | * (traps are reset to defaults, but "trap" still shows them), |
| 4572 | * but as a result, "trap" logic is hopelessly messed up: |
| 4573 | * |
| 4574 | * # trap |
| 4575 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 4576 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 4577 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 4578 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 4579 | * trap -- 'echo Ho' SIGWINCH |
| 4580 | * # echo `(trap)` <--- in subshell in subshell - output |
| 4581 | * trap -- 'echo Ho' SIGWINCH |
| 4582 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 4583 | * trap -- 'echo Ho' SIGWINCH |
| 4584 | * |
| 4585 | * The rules when to forget and when to not forget traps |
| 4586 | * get really complex and nonsensical. |
| 4587 | * |
| 4588 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 4589 | */ |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4590 | /* Save trap handler strings for trap builtin to print */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4591 | trap_ptr = memcpy(xmalloc(sizeof(trap)), trap, sizeof(trap)); |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4592 | /* Fall through into clearing traps */ |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4593 | } |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4594 | clear_traps(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4595 | #if JOBS |
| 4596 | /* do job control only in root shell */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4597 | doing_jobctl = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4598 | if (mode != FORK_NOJOB && jp->jobctl && !oldlvl) { |
| 4599 | pid_t pgrp; |
| 4600 | |
| 4601 | if (jp->nprocs == 0) |
| 4602 | pgrp = getpid(); |
| 4603 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4604 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4605 | /* this can fail because we are doing it in the parent also */ |
| 4606 | setpgid(0, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4607 | if (mode == FORK_FG) |
| 4608 | xtcsetpgrp(ttyfd, pgrp); |
| 4609 | setsignal(SIGTSTP); |
| 4610 | setsignal(SIGTTOU); |
| 4611 | } else |
| 4612 | #endif |
| 4613 | if (mode == FORK_BG) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4614 | /* man bash: "When job control is not in effect, |
| 4615 | * asynchronous commands ignore SIGINT and SIGQUIT" */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4616 | ignoresig(SIGINT); |
| 4617 | ignoresig(SIGQUIT); |
| 4618 | if (jp->nprocs == 0) { |
| 4619 | close(0); |
| 4620 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 4621 | ash_msg_and_raise_error("can't open '%s'", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4622 | } |
| 4623 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4624 | if (!oldlvl) { |
| 4625 | if (iflag) { /* why if iflag only? */ |
| 4626 | setsignal(SIGINT); |
| 4627 | setsignal(SIGTERM); |
| 4628 | } |
| 4629 | /* man bash: |
| 4630 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 4631 | * commands run by bash have signal handlers |
| 4632 | * set to the values inherited by the shell |
| 4633 | * from its parent". |
| 4634 | * Take care of the second rule: */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4635 | setsignal(SIGQUIT); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4636 | } |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4637 | #if JOBS |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4638 | if (n && n->type == NCMD |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4639 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4640 | ) { |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4641 | TRACE(("Job hack\n")); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4642 | /* "jobs": we do not want to clear job list for it, |
| 4643 | * instead we remove only _its_ own_ job from job list. |
| 4644 | * This makes "jobs .... | cat" more useful. |
| 4645 | */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4646 | freejob(curjob); |
| 4647 | return; |
| 4648 | } |
| 4649 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4650 | for (jp = curjob; jp; jp = jp->prev_job) |
| 4651 | freejob(jp); |
| 4652 | jobless = 0; |
| 4653 | } |
| 4654 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4655 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 4656 | #if !JOBS |
| 4657 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 4658 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4659 | static void |
| 4660 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 4661 | { |
| 4662 | TRACE(("In parent shell: child = %d\n", pid)); |
| 4663 | if (!jp) { |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4664 | while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0) |
| 4665 | continue; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4666 | jobless++; |
| 4667 | return; |
| 4668 | } |
| 4669 | #if JOBS |
| 4670 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 4671 | int pgrp; |
| 4672 | |
| 4673 | if (jp->nprocs == 0) |
| 4674 | pgrp = pid; |
| 4675 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4676 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4677 | /* This can fail because we are doing it in the child also */ |
| 4678 | setpgid(pid, pgrp); |
| 4679 | } |
| 4680 | #endif |
| 4681 | if (mode == FORK_BG) { |
| 4682 | backgndpid = pid; /* set $! */ |
| 4683 | set_curjob(jp, CUR_RUNNING); |
| 4684 | } |
| 4685 | if (jp) { |
| 4686 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4687 | ps->ps_pid = pid; |
| 4688 | ps->ps_status = -1; |
| 4689 | ps->ps_cmd = nullstr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4690 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4691 | if (doing_jobctl && n) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4692 | ps->ps_cmd = commandtext(n); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4693 | #endif |
| 4694 | } |
| 4695 | } |
| 4696 | |
| 4697 | static int |
| 4698 | forkshell(struct job *jp, union node *n, int mode) |
| 4699 | { |
| 4700 | int pid; |
| 4701 | |
| 4702 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 4703 | pid = fork(); |
| 4704 | if (pid < 0) { |
| 4705 | TRACE(("Fork failed, errno=%d", errno)); |
| 4706 | if (jp) |
| 4707 | freejob(jp); |
Denis Vlasenko | fa0b56d | 2008-07-01 16:09:07 +0000 | [diff] [blame] | 4708 | ash_msg_and_raise_error("can't fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4709 | } |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4710 | if (pid == 0) { |
| 4711 | CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4712 | forkchild(jp, n, mode); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4713 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4714 | forkparent(jp, n, mode, pid); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4715 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4716 | return pid; |
| 4717 | } |
| 4718 | |
| 4719 | /* |
| 4720 | * Wait for job to finish. |
| 4721 | * |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4722 | * Under job control we have the problem that while a child process |
| 4723 | * is running interrupts generated by the user are sent to the child |
| 4724 | * but not to the shell. This means that an infinite loop started by |
| 4725 | * an interactive user may be hard to kill. With job control turned off, |
| 4726 | * an interactive user may place an interactive program inside a loop. |
| 4727 | * If the interactive program catches interrupts, the user doesn't want |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4728 | * these interrupts to also abort the loop. The approach we take here |
| 4729 | * is to have the shell ignore interrupt signals while waiting for a |
| 4730 | * foreground process to terminate, and then send itself an interrupt |
| 4731 | * signal if the child process was terminated by an interrupt signal. |
| 4732 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 4733 | * exit on interrupt; unless these processes terminate themselves by |
| 4734 | * sending a signal to themselves (instead of calling exit) they will |
| 4735 | * confuse this approach. |
| 4736 | * |
| 4737 | * Called with interrupts off. |
| 4738 | */ |
| 4739 | static int |
| 4740 | waitforjob(struct job *jp) |
| 4741 | { |
| 4742 | int st; |
| 4743 | |
| 4744 | TRACE(("waitforjob(%%%d) called\n", jobno(jp))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4745 | |
| 4746 | INT_OFF; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4747 | while (jp->state == JOBRUNNING) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4748 | /* In non-interactive shells, we _can_ get |
| 4749 | * a keyboard signal here and be EINTRed, |
| 4750 | * but we just loop back, waiting for command to complete. |
| 4751 | * |
| 4752 | * man bash: |
| 4753 | * "If bash is waiting for a command to complete and receives |
| 4754 | * a signal for which a trap has been set, the trap |
| 4755 | * will not be executed until the command completes." |
| 4756 | * |
| 4757 | * Reality is that even if trap is not set, bash |
| 4758 | * will not act on the signal until command completes. |
| 4759 | * Try this. sleep5intoff.c: |
| 4760 | * #include <signal.h> |
| 4761 | * #include <unistd.h> |
| 4762 | * int main() { |
| 4763 | * sigset_t set; |
| 4764 | * sigemptyset(&set); |
| 4765 | * sigaddset(&set, SIGINT); |
| 4766 | * sigaddset(&set, SIGQUIT); |
| 4767 | * sigprocmask(SIG_BLOCK, &set, NULL); |
| 4768 | * sleep(5); |
| 4769 | * return 0; |
| 4770 | * } |
| 4771 | * $ bash -c './sleep5intoff; echo hi' |
| 4772 | * ^C^C^C^C <--- pressing ^C once a second |
| 4773 | * $ _ |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4774 | * $ bash -c './sleep5intoff; echo hi' |
| 4775 | * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT) |
| 4776 | * $ _ |
| 4777 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4778 | dowait(DOWAIT_BLOCK, jp); |
| 4779 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4780 | INT_ON; |
| 4781 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4782 | st = getstatus(jp); |
| 4783 | #if JOBS |
| 4784 | if (jp->jobctl) { |
| 4785 | xtcsetpgrp(ttyfd, rootpid); |
| 4786 | /* |
| 4787 | * This is truly gross. |
| 4788 | * If we're doing job control, then we did a TIOCSPGRP which |
| 4789 | * caused us (the shell) to no longer be in the controlling |
| 4790 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 4791 | * intuit from the subprocess exit status whether a SIGINT |
| 4792 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 4793 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4794 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 4795 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4796 | } |
| 4797 | if (jp->state == JOBDONE) |
| 4798 | #endif |
| 4799 | freejob(jp); |
| 4800 | return st; |
| 4801 | } |
| 4802 | |
| 4803 | /* |
| 4804 | * return 1 if there are stopped jobs, otherwise 0 |
| 4805 | */ |
| 4806 | static int |
| 4807 | stoppedjobs(void) |
| 4808 | { |
| 4809 | struct job *jp; |
| 4810 | int retval; |
| 4811 | |
| 4812 | retval = 0; |
| 4813 | if (job_warning) |
| 4814 | goto out; |
| 4815 | jp = curjob; |
| 4816 | if (jp && jp->state == JOBSTOPPED) { |
| 4817 | out2str("You have stopped jobs.\n"); |
| 4818 | job_warning = 2; |
| 4819 | retval++; |
| 4820 | } |
| 4821 | out: |
| 4822 | return retval; |
| 4823 | } |
| 4824 | |
| 4825 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4826 | /* ============ redir.c |
| 4827 | * |
| 4828 | * Code for dealing with input/output redirection. |
| 4829 | */ |
| 4830 | |
| 4831 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4832 | #define CLOSED -3 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4833 | |
| 4834 | /* |
| 4835 | * Open a file in noclobber mode. |
| 4836 | * The code was copied from bash. |
| 4837 | */ |
| 4838 | static int |
| 4839 | noclobberopen(const char *fname) |
| 4840 | { |
| 4841 | int r, fd; |
| 4842 | struct stat finfo, finfo2; |
| 4843 | |
| 4844 | /* |
| 4845 | * If the file exists and is a regular file, return an error |
| 4846 | * immediately. |
| 4847 | */ |
| 4848 | r = stat(fname, &finfo); |
| 4849 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 4850 | errno = EEXIST; |
| 4851 | return -1; |
| 4852 | } |
| 4853 | |
| 4854 | /* |
| 4855 | * If the file was not present (r != 0), make sure we open it |
| 4856 | * exclusively so that if it is created before we open it, our open |
| 4857 | * will fail. Make sure that we do not truncate an existing file. |
| 4858 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 4859 | * file was not a regular file, we leave O_EXCL off. |
| 4860 | */ |
| 4861 | if (r != 0) |
| 4862 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 4863 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
| 4864 | |
| 4865 | /* If the open failed, return the file descriptor right away. */ |
| 4866 | if (fd < 0) |
| 4867 | return fd; |
| 4868 | |
| 4869 | /* |
| 4870 | * OK, the open succeeded, but the file may have been changed from a |
| 4871 | * non-regular file to a regular file between the stat and the open. |
| 4872 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 4873 | * did not exist and is symlinked to an existing file between the stat |
| 4874 | * and open. |
| 4875 | */ |
| 4876 | |
| 4877 | /* |
| 4878 | * If we can open it and fstat the file descriptor, and neither check |
| 4879 | * revealed that it was a regular file, and the file has not been |
| 4880 | * replaced, return the file descriptor. |
| 4881 | */ |
| 4882 | if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode) |
| 4883 | && finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino) |
| 4884 | return fd; |
| 4885 | |
| 4886 | /* The file has been replaced. badness. */ |
| 4887 | close(fd); |
| 4888 | errno = EEXIST; |
| 4889 | return -1; |
| 4890 | } |
| 4891 | |
| 4892 | /* |
| 4893 | * Handle here documents. Normally we fork off a process to write the |
| 4894 | * data to a pipe. If the document is short, we can stuff the data in |
| 4895 | * the pipe without forking. |
| 4896 | */ |
| 4897 | /* openhere needs this forward reference */ |
| 4898 | static void expandhere(union node *arg, int fd); |
| 4899 | static int |
| 4900 | openhere(union node *redir) |
| 4901 | { |
| 4902 | int pip[2]; |
| 4903 | size_t len = 0; |
| 4904 | |
| 4905 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 4906 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4907 | if (redir->type == NHERE) { |
| 4908 | len = strlen(redir->nhere.doc->narg.text); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4909 | if (len <= PIPE_BUF) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4910 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
| 4911 | goto out; |
| 4912 | } |
| 4913 | } |
| 4914 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4915 | /* child */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4916 | close(pip[0]); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4917 | ignoresig(SIGINT); //signal(SIGINT, SIG_IGN); |
| 4918 | ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN); |
| 4919 | ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN); |
| 4920 | ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4921 | signal(SIGPIPE, SIG_DFL); |
| 4922 | if (redir->type == NHERE) |
| 4923 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4924 | else /* NXHERE */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4925 | expandhere(redir->nhere.doc, pip[1]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 4926 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4927 | } |
| 4928 | out: |
| 4929 | close(pip[1]); |
| 4930 | return pip[0]; |
| 4931 | } |
| 4932 | |
| 4933 | static int |
| 4934 | openredirect(union node *redir) |
| 4935 | { |
| 4936 | char *fname; |
| 4937 | int f; |
| 4938 | |
| 4939 | switch (redir->nfile.type) { |
| 4940 | case NFROM: |
| 4941 | fname = redir->nfile.expfname; |
| 4942 | f = open(fname, O_RDONLY); |
| 4943 | if (f < 0) |
| 4944 | goto eopen; |
| 4945 | break; |
| 4946 | case NFROMTO: |
| 4947 | fname = redir->nfile.expfname; |
| 4948 | f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666); |
| 4949 | if (f < 0) |
| 4950 | goto ecreate; |
| 4951 | break; |
| 4952 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4953 | #if ENABLE_ASH_BASH_COMPAT |
| 4954 | case NTO2: |
| 4955 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4956 | /* Take care of noclobber mode. */ |
| 4957 | if (Cflag) { |
| 4958 | fname = redir->nfile.expfname; |
| 4959 | f = noclobberopen(fname); |
| 4960 | if (f < 0) |
| 4961 | goto ecreate; |
| 4962 | break; |
| 4963 | } |
| 4964 | /* FALLTHROUGH */ |
| 4965 | case NCLOBBER: |
| 4966 | fname = redir->nfile.expfname; |
| 4967 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 4968 | if (f < 0) |
| 4969 | goto ecreate; |
| 4970 | break; |
| 4971 | case NAPPEND: |
| 4972 | fname = redir->nfile.expfname; |
| 4973 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 4974 | if (f < 0) |
| 4975 | goto ecreate; |
| 4976 | break; |
| 4977 | default: |
| 4978 | #if DEBUG |
| 4979 | abort(); |
| 4980 | #endif |
| 4981 | /* Fall through to eliminate warning. */ |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 4982 | /* Our single caller does this itself */ |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4983 | // case NTOFD: |
| 4984 | // case NFROMFD: |
| 4985 | // f = -1; |
| 4986 | // break; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4987 | case NHERE: |
| 4988 | case NXHERE: |
| 4989 | f = openhere(redir); |
| 4990 | break; |
| 4991 | } |
| 4992 | |
| 4993 | return f; |
| 4994 | ecreate: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 4995 | 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] | 4996 | eopen: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 4997 | 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] | 4998 | } |
| 4999 | |
| 5000 | /* |
| 5001 | * Copy a file descriptor to be >= to. Returns -1 |
| 5002 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 5003 | * file descriptors left. |
| 5004 | */ |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5005 | /* 0x800..00: bit to set in "to" to request dup2 instead of fcntl(F_DUPFD). |
| 5006 | * 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] | 5007 | enum { |
| 5008 | COPYFD_EXACT = (int)~(INT_MAX), |
| 5009 | COPYFD_RESTORE = (int)((unsigned)COPYFD_EXACT >> 1), |
| 5010 | }; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5011 | static int |
| 5012 | copyfd(int from, int to) |
| 5013 | { |
| 5014 | int newfd; |
| 5015 | |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5016 | if (to & COPYFD_EXACT) { |
| 5017 | to &= ~COPYFD_EXACT; |
| 5018 | /*if (from != to)*/ |
| 5019 | newfd = dup2(from, to); |
| 5020 | } else { |
| 5021 | newfd = fcntl(from, F_DUPFD, to); |
| 5022 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5023 | if (newfd < 0) { |
| 5024 | if (errno == EMFILE) |
| 5025 | return EMPTY; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5026 | /* Happens when source fd is not open: try "echo >&99" */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5027 | ash_msg_and_raise_error("%d: %m", from); |
| 5028 | } |
| 5029 | return newfd; |
| 5030 | } |
| 5031 | |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5032 | /* Struct def and variable are moved down to the first usage site */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5033 | struct two_fd_t { |
| 5034 | int orig, copy; |
| 5035 | }; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5036 | struct redirtab { |
| 5037 | struct redirtab *next; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5038 | int nullredirs; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5039 | int pair_count; |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 5040 | struct two_fd_t two_fd[]; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5041 | }; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5042 | #define redirlist (G_var.redirlist) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5043 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5044 | static int need_to_remember(struct redirtab *rp, int fd) |
| 5045 | { |
| 5046 | int i; |
| 5047 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5048 | if (!rp) /* remembering was not requested */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5049 | return 0; |
| 5050 | |
| 5051 | for (i = 0; i < rp->pair_count; i++) { |
| 5052 | if (rp->two_fd[i].orig == fd) { |
| 5053 | /* already remembered */ |
| 5054 | return 0; |
| 5055 | } |
| 5056 | } |
| 5057 | return 1; |
| 5058 | } |
| 5059 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5060 | /* "hidden" fd is a fd used to read scripts, or a copy of such */ |
| 5061 | static int is_hidden_fd(struct redirtab *rp, int fd) |
| 5062 | { |
| 5063 | int i; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5064 | struct parsefile *pf; |
| 5065 | |
| 5066 | if (fd == -1) |
| 5067 | return 0; |
| 5068 | pf = g_parsefile; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5069 | while (pf) { |
| 5070 | if (fd == pf->fd) { |
| 5071 | return 1; |
| 5072 | } |
| 5073 | pf = pf->prev; |
| 5074 | } |
| 5075 | if (!rp) |
| 5076 | return 0; |
| 5077 | fd |= COPYFD_RESTORE; |
| 5078 | for (i = 0; i < rp->pair_count; i++) { |
| 5079 | if (rp->two_fd[i].copy == fd) { |
| 5080 | return 1; |
| 5081 | } |
| 5082 | } |
| 5083 | return 0; |
| 5084 | } |
| 5085 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5086 | /* |
| 5087 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 5088 | * old file descriptors are stashed away so that the redirection can be |
| 5089 | * undone by calling popredir. If the REDIR_BACKQ flag is set, then the |
| 5090 | * standard output, and the standard error if it becomes a duplicate of |
| 5091 | * stdout, is saved in memory. |
| 5092 | */ |
| 5093 | /* flags passed to redirect */ |
| 5094 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
| 5095 | #define REDIR_SAVEFD2 03 /* set preverrout */ |
| 5096 | static void |
| 5097 | redirect(union node *redir, int flags) |
| 5098 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5099 | struct redirtab *sv; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5100 | int sv_pos; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5101 | int i; |
| 5102 | int fd; |
| 5103 | int newfd; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5104 | int copied_fd2 = -1; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5105 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5106 | g_nullredirs++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5107 | if (!redir) { |
| 5108 | return; |
| 5109 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5110 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5111 | sv = NULL; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5112 | sv_pos = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5113 | INT_OFF; |
| 5114 | if (flags & REDIR_PUSH) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5115 | union node *tmp = redir; |
| 5116 | do { |
| 5117 | sv_pos++; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5118 | #if ENABLE_ASH_BASH_COMPAT |
Chris Metcalf | c3c1fb6 | 2010-01-08 13:18:06 +0100 | [diff] [blame] | 5119 | if (tmp->nfile.type == NTO2) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5120 | sv_pos++; |
| 5121 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5122 | tmp = tmp->nfile.next; |
| 5123 | } while (tmp); |
| 5124 | sv = ckmalloc(sizeof(*sv) + sv_pos * sizeof(sv->two_fd[0])); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5125 | sv->next = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5126 | sv->pair_count = sv_pos; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5127 | redirlist = sv; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5128 | sv->nullredirs = g_nullredirs - 1; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5129 | g_nullredirs = 0; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5130 | while (sv_pos > 0) { |
| 5131 | sv_pos--; |
| 5132 | sv->two_fd[sv_pos].orig = sv->two_fd[sv_pos].copy = EMPTY; |
| 5133 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5134 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5135 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5136 | do { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5137 | fd = redir->nfile.fd; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5138 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5139 | int right_fd = redir->ndup.dupfd; |
| 5140 | /* redirect from/to same file descriptor? */ |
| 5141 | if (right_fd == fd) |
| 5142 | continue; |
| 5143 | /* echo >&10 and 10 is a fd opened to the sh script? */ |
| 5144 | if (is_hidden_fd(sv, right_fd)) { |
| 5145 | errno = EBADF; /* as if it is closed */ |
| 5146 | ash_msg_and_raise_error("%d: %m", right_fd); |
| 5147 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5148 | newfd = -1; |
| 5149 | } else { |
| 5150 | newfd = openredirect(redir); /* always >= 0 */ |
| 5151 | if (fd == newfd) { |
| 5152 | /* Descriptor wasn't open before redirect. |
| 5153 | * Mark it for close in the future */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5154 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5155 | goto remember_to_close; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5156 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5157 | continue; |
| 5158 | } |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5159 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5160 | #if ENABLE_ASH_BASH_COMPAT |
| 5161 | redirect_more: |
| 5162 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5163 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5164 | /* Copy old descriptor */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5165 | i = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5166 | /* You'd expect copy to be CLOEXECed. Currently these extra "saved" fds |
| 5167 | * are closed in popredir() in the child, preventing them from leaking |
| 5168 | * into child. (popredir() also cleans up the mess in case of failures) |
| 5169 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5170 | if (i == -1) { |
| 5171 | i = errno; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5172 | if (i != EBADF) { |
| 5173 | /* Strange error (e.g. "too many files" EMFILE?) */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5174 | if (newfd >= 0) |
| 5175 | close(newfd); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5176 | errno = i; |
| 5177 | ash_msg_and_raise_error("%d: %m", fd); |
| 5178 | /* NOTREACHED */ |
| 5179 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5180 | /* EBADF: it is not open - good, remember to close it */ |
| 5181 | remember_to_close: |
| 5182 | i = CLOSED; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5183 | } else { /* fd is open, save its copy */ |
| 5184 | /* "exec fd>&-" should not close fds |
| 5185 | * which point to script file(s). |
| 5186 | * Force them to be restored afterwards */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5187 | if (is_hidden_fd(sv, fd)) |
| 5188 | i |= COPYFD_RESTORE; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5189 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5190 | if (fd == 2) |
| 5191 | copied_fd2 = i; |
| 5192 | sv->two_fd[sv_pos].orig = fd; |
| 5193 | sv->two_fd[sv_pos].copy = i; |
| 5194 | sv_pos++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5195 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5196 | if (newfd < 0) { |
| 5197 | /* NTOFD/NFROMFD: copy redir->ndup.dupfd to fd */ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5198 | if (redir->ndup.dupfd < 0) { /* "fd>&-" */ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5199 | /* Don't want to trigger debugging */ |
| 5200 | if (fd != -1) |
| 5201 | close(fd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5202 | } else { |
| 5203 | copyfd(redir->ndup.dupfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5204 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5205 | } else if (fd != newfd) { /* move newfd to fd */ |
| 5206 | copyfd(newfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5207 | #if ENABLE_ASH_BASH_COMPAT |
| 5208 | if (!(redir->nfile.type == NTO2 && fd == 2)) |
| 5209 | #endif |
| 5210 | close(newfd); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5211 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5212 | #if ENABLE_ASH_BASH_COMPAT |
| 5213 | if (redir->nfile.type == NTO2 && fd == 1) { |
| 5214 | /* We already redirected it to fd 1, now copy it to 2 */ |
| 5215 | newfd = 1; |
| 5216 | fd = 2; |
| 5217 | goto redirect_more; |
| 5218 | } |
| 5219 | #endif |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5220 | } while ((redir = redir->nfile.next) != NULL); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5221 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5222 | INT_ON; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5223 | if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0) |
| 5224 | preverrout_fd = copied_fd2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | /* |
| 5228 | * Undo the effects of the last redirection. |
| 5229 | */ |
| 5230 | static void |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5231 | popredir(int drop, int restore) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5232 | { |
| 5233 | struct redirtab *rp; |
| 5234 | int i; |
| 5235 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5236 | if (--g_nullredirs >= 0) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5237 | return; |
| 5238 | INT_OFF; |
| 5239 | rp = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5240 | for (i = 0; i < rp->pair_count; i++) { |
| 5241 | int fd = rp->two_fd[i].orig; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5242 | int copy = rp->two_fd[i].copy; |
| 5243 | if (copy == CLOSED) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5244 | if (!drop) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5245 | close(fd); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5246 | continue; |
| 5247 | } |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5248 | if (copy != EMPTY) { |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5249 | if (!drop || (restore && (copy & COPYFD_RESTORE))) { |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5250 | copy &= ~COPYFD_RESTORE; |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5251 | /*close(fd);*/ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5252 | copyfd(copy, fd | COPYFD_EXACT); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5253 | } |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5254 | close(copy & ~COPYFD_RESTORE); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5255 | } |
| 5256 | } |
| 5257 | redirlist = rp->next; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5258 | g_nullredirs = rp->nullredirs; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5259 | free(rp); |
| 5260 | INT_ON; |
| 5261 | } |
| 5262 | |
| 5263 | /* |
| 5264 | * Undo all redirections. Called on error or interrupt. |
| 5265 | */ |
| 5266 | |
| 5267 | /* |
| 5268 | * Discard all saved file descriptors. |
| 5269 | */ |
| 5270 | static void |
| 5271 | clearredir(int drop) |
| 5272 | { |
| 5273 | for (;;) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5274 | g_nullredirs = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5275 | if (!redirlist) |
| 5276 | break; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5277 | popredir(drop, /*restore:*/ 0); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5278 | } |
| 5279 | } |
| 5280 | |
| 5281 | static int |
| 5282 | redirectsafe(union node *redir, int flags) |
| 5283 | { |
| 5284 | int err; |
| 5285 | volatile int saveint; |
| 5286 | struct jmploc *volatile savehandler = exception_handler; |
| 5287 | struct jmploc jmploc; |
| 5288 | |
| 5289 | SAVE_INT(saveint); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5290 | /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */ |
| 5291 | err = setjmp(jmploc.loc); // huh?? was = setjmp(jmploc.loc) * 2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5292 | if (!err) { |
| 5293 | exception_handler = &jmploc; |
| 5294 | redirect(redir, flags); |
| 5295 | } |
| 5296 | exception_handler = savehandler; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 5297 | if (err && exception_type != EXERROR) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5298 | longjmp(exception_handler->loc, 1); |
| 5299 | RESTORE_INT(saveint); |
| 5300 | return err; |
| 5301 | } |
| 5302 | |
| 5303 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5304 | /* ============ Routines to expand arguments to commands |
| 5305 | * |
| 5306 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 5307 | */ |
| 5308 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5309 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5310 | static arith_t |
| 5311 | ash_arith(const char *s) |
| 5312 | { |
| 5313 | arith_eval_hooks_t math_hooks; |
| 5314 | arith_t result; |
| 5315 | int errcode = 0; |
| 5316 | |
| 5317 | math_hooks.lookupvar = lookupvar; |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 5318 | math_hooks.setvar = setvar2; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5319 | math_hooks.endofname = endofname; |
| 5320 | |
| 5321 | INT_OFF; |
| 5322 | result = arith(s, &errcode, &math_hooks); |
| 5323 | if (errcode < 0) { |
| 5324 | if (errcode == -3) |
| 5325 | ash_msg_and_raise_error("exponent less than 0"); |
| 5326 | if (errcode == -2) |
| 5327 | ash_msg_and_raise_error("divide by zero"); |
| 5328 | if (errcode == -5) |
| 5329 | ash_msg_and_raise_error("expression recursion loop detected"); |
| 5330 | raise_error_syntax(s); |
| 5331 | } |
| 5332 | INT_ON; |
| 5333 | |
| 5334 | return result; |
| 5335 | } |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5336 | #endif |
| 5337 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5338 | /* |
| 5339 | * expandarg flags |
| 5340 | */ |
| 5341 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 5342 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 5343 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 5344 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 5345 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
| 5346 | #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */ |
| 5347 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ |
| 5348 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ |
| 5349 | #define EXP_QWORD 0x100 /* expand word in quoted parameter expansion */ |
| 5350 | /* |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5351 | * rmescape() flags |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5352 | */ |
| 5353 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 5354 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
| 5355 | #define RMESCAPE_QUOTED 0x4 /* Remove CTLESC unless in quotes */ |
| 5356 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 5357 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
| 5358 | |
| 5359 | /* |
| 5360 | * Structure specifying which parts of the string should be searched |
| 5361 | * for IFS characters. |
| 5362 | */ |
| 5363 | struct ifsregion { |
| 5364 | struct ifsregion *next; /* next region in list */ |
| 5365 | int begoff; /* offset of start of region */ |
| 5366 | int endoff; /* offset of end of region */ |
| 5367 | int nulonly; /* search for nul bytes only */ |
| 5368 | }; |
| 5369 | |
| 5370 | struct arglist { |
| 5371 | struct strlist *list; |
| 5372 | struct strlist **lastp; |
| 5373 | }; |
| 5374 | |
| 5375 | /* output of current string */ |
| 5376 | static char *expdest; |
| 5377 | /* list of back quote expressions */ |
| 5378 | static struct nodelist *argbackq; |
| 5379 | /* first struct in list of ifs regions */ |
| 5380 | static struct ifsregion ifsfirst; |
| 5381 | /* last struct in list */ |
| 5382 | static struct ifsregion *ifslastp; |
| 5383 | /* holds expanded arg list */ |
| 5384 | static struct arglist exparg; |
| 5385 | |
| 5386 | /* |
| 5387 | * Our own itoa(). |
| 5388 | */ |
| 5389 | static int |
| 5390 | cvtnum(arith_t num) |
| 5391 | { |
| 5392 | int len; |
| 5393 | |
| 5394 | expdest = makestrspace(32, expdest); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5395 | len = fmtstr(expdest, 32, arith_t_fmt, num); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5396 | STADJUST(len, expdest); |
| 5397 | return len; |
| 5398 | } |
| 5399 | |
| 5400 | static size_t |
| 5401 | esclen(const char *start, const char *p) |
| 5402 | { |
| 5403 | size_t esc = 0; |
| 5404 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5405 | while (p > start && (unsigned char)*--p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5406 | esc++; |
| 5407 | } |
| 5408 | return esc; |
| 5409 | } |
| 5410 | |
| 5411 | /* |
| 5412 | * Remove any CTLESC characters from a string. |
| 5413 | */ |
| 5414 | static char * |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5415 | rmescapes(char *str, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5416 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5417 | static const char qchars[] ALIGN1 = { CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 5418 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5419 | char *p, *q, *r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5420 | unsigned inquotes; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5421 | unsigned protect_against_glob; |
| 5422 | unsigned globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5423 | |
| 5424 | p = strpbrk(str, qchars); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5425 | if (!p) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5426 | return str; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5427 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5428 | q = p; |
| 5429 | r = str; |
| 5430 | if (flag & RMESCAPE_ALLOC) { |
| 5431 | size_t len = p - str; |
| 5432 | size_t fulllen = len + strlen(p) + 1; |
| 5433 | |
| 5434 | if (flag & RMESCAPE_GROW) { |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5435 | int strloc = str - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5436 | r = makestrspace(fulllen, expdest); |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5437 | /* p and str may be invalidated by makestrspace */ |
| 5438 | str = (char *)stackblock() + strloc; |
| 5439 | p = str + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5440 | } else if (flag & RMESCAPE_HEAP) { |
| 5441 | r = ckmalloc(fulllen); |
| 5442 | } else { |
| 5443 | r = stalloc(fulllen); |
| 5444 | } |
| 5445 | q = r; |
| 5446 | if (len > 0) { |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5447 | q = (char *)memcpy(q, str, len) + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5448 | } |
| 5449 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5450 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5451 | inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED; |
| 5452 | globbing = flag & RMESCAPE_GLOB; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5453 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5454 | while (*p) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5455 | if ((unsigned char)*p == CTLQUOTEMARK) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5456 | // TODO: if no RMESCAPE_QUOTED in flags, inquotes never becomes 0 |
| 5457 | // (alternates between RMESCAPE_QUOTED and ~RMESCAPE_QUOTED). Is it ok? |
| 5458 | // Note: both inquotes and protect_against_glob only affect whether |
| 5459 | // CTLESC,<ch> gets converted to <ch> or to \<ch> |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5460 | inquotes = ~inquotes; |
| 5461 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5462 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5463 | continue; |
| 5464 | } |
| 5465 | if (*p == '\\') { |
| 5466 | /* naked back slash */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5467 | protect_against_glob = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5468 | goto copy; |
| 5469 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5470 | if ((unsigned char)*p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5471 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5472 | if (protect_against_glob && inquotes && *p != '/') { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5473 | *q++ = '\\'; |
| 5474 | } |
| 5475 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5476 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5477 | copy: |
| 5478 | *q++ = *p++; |
| 5479 | } |
| 5480 | *q = '\0'; |
| 5481 | if (flag & RMESCAPE_GROW) { |
| 5482 | expdest = r; |
| 5483 | STADJUST(q - r + 1, expdest); |
| 5484 | } |
| 5485 | return r; |
| 5486 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5487 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 5488 | |
| 5489 | /* |
| 5490 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 5491 | * |
| 5492 | * Returns an stalloced string. |
| 5493 | */ |
| 5494 | static char * |
| 5495 | preglob(const char *pattern, int quoted, int flag) |
| 5496 | { |
| 5497 | flag |= RMESCAPE_GLOB; |
| 5498 | if (quoted) { |
| 5499 | flag |= RMESCAPE_QUOTED; |
| 5500 | } |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5501 | return rmescapes((char *)pattern, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5502 | } |
| 5503 | |
| 5504 | /* |
| 5505 | * Put a string on the stack. |
| 5506 | */ |
| 5507 | static void |
| 5508 | memtodest(const char *p, size_t len, int syntax, int quotes) |
| 5509 | { |
| 5510 | char *q = expdest; |
| 5511 | |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5512 | q = makestrspace(quotes ? len * 2 : len, q); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5513 | |
| 5514 | while (len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5515 | unsigned char c = *p++; |
| 5516 | if (c == '\0') |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5517 | continue; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5518 | if (quotes) { |
| 5519 | int n = SIT(c, syntax); |
| 5520 | if (n == CCTL || n == CBACK) |
| 5521 | USTPUTC(CTLESC, q); |
| 5522 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5523 | USTPUTC(c, q); |
| 5524 | } |
| 5525 | |
| 5526 | expdest = q; |
| 5527 | } |
| 5528 | |
| 5529 | static void |
| 5530 | strtodest(const char *p, int syntax, int quotes) |
| 5531 | { |
| 5532 | memtodest(p, strlen(p), syntax, quotes); |
| 5533 | } |
| 5534 | |
| 5535 | /* |
| 5536 | * Record the fact that we have to scan this region of the |
| 5537 | * string for IFS characters. |
| 5538 | */ |
| 5539 | static void |
| 5540 | recordregion(int start, int end, int nulonly) |
| 5541 | { |
| 5542 | struct ifsregion *ifsp; |
| 5543 | |
| 5544 | if (ifslastp == NULL) { |
| 5545 | ifsp = &ifsfirst; |
| 5546 | } else { |
| 5547 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 5548 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 5549 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5550 | ifslastp->next = ifsp; |
| 5551 | INT_ON; |
| 5552 | } |
| 5553 | ifslastp = ifsp; |
| 5554 | ifslastp->begoff = start; |
| 5555 | ifslastp->endoff = end; |
| 5556 | ifslastp->nulonly = nulonly; |
| 5557 | } |
| 5558 | |
| 5559 | static void |
| 5560 | removerecordregions(int endoff) |
| 5561 | { |
| 5562 | if (ifslastp == NULL) |
| 5563 | return; |
| 5564 | |
| 5565 | if (ifsfirst.endoff > endoff) { |
| 5566 | while (ifsfirst.next != NULL) { |
| 5567 | struct ifsregion *ifsp; |
| 5568 | INT_OFF; |
| 5569 | ifsp = ifsfirst.next->next; |
| 5570 | free(ifsfirst.next); |
| 5571 | ifsfirst.next = ifsp; |
| 5572 | INT_ON; |
| 5573 | } |
| 5574 | if (ifsfirst.begoff > endoff) |
| 5575 | ifslastp = NULL; |
| 5576 | else { |
| 5577 | ifslastp = &ifsfirst; |
| 5578 | ifsfirst.endoff = endoff; |
| 5579 | } |
| 5580 | return; |
| 5581 | } |
| 5582 | |
| 5583 | ifslastp = &ifsfirst; |
| 5584 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
| 5585 | ifslastp=ifslastp->next; |
| 5586 | while (ifslastp->next != NULL) { |
| 5587 | struct ifsregion *ifsp; |
| 5588 | INT_OFF; |
| 5589 | ifsp = ifslastp->next->next; |
| 5590 | free(ifslastp->next); |
| 5591 | ifslastp->next = ifsp; |
| 5592 | INT_ON; |
| 5593 | } |
| 5594 | if (ifslastp->endoff > endoff) |
| 5595 | ifslastp->endoff = endoff; |
| 5596 | } |
| 5597 | |
| 5598 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5599 | exptilde(char *startp, char *p, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5600 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5601 | unsigned char c; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5602 | char *name; |
| 5603 | struct passwd *pw; |
| 5604 | const char *home; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 5605 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5606 | int startloc; |
| 5607 | |
| 5608 | name = p + 1; |
| 5609 | |
| 5610 | while ((c = *++p) != '\0') { |
| 5611 | switch (c) { |
| 5612 | case CTLESC: |
| 5613 | return startp; |
| 5614 | case CTLQUOTEMARK: |
| 5615 | return startp; |
| 5616 | case ':': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5617 | if (flags & EXP_VARTILDE) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5618 | goto done; |
| 5619 | break; |
| 5620 | case '/': |
| 5621 | case CTLENDVAR: |
| 5622 | goto done; |
| 5623 | } |
| 5624 | } |
| 5625 | done: |
| 5626 | *p = '\0'; |
| 5627 | if (*name == '\0') { |
| 5628 | home = lookupvar(homestr); |
| 5629 | } else { |
| 5630 | pw = getpwnam(name); |
| 5631 | if (pw == NULL) |
| 5632 | goto lose; |
| 5633 | home = pw->pw_dir; |
| 5634 | } |
| 5635 | if (!home || !*home) |
| 5636 | goto lose; |
| 5637 | *p = c; |
| 5638 | startloc = expdest - (char *)stackblock(); |
| 5639 | strtodest(home, SQSYNTAX, quotes); |
| 5640 | recordregion(startloc, expdest - (char *)stackblock(), 0); |
| 5641 | return p; |
| 5642 | lose: |
| 5643 | *p = c; |
| 5644 | return startp; |
| 5645 | } |
| 5646 | |
| 5647 | /* |
| 5648 | * Execute a command inside back quotes. If it's a builtin command, we |
| 5649 | * want to save its output in a block obtained from malloc. Otherwise |
| 5650 | * we fork off a subprocess and get the output of the command via a pipe. |
| 5651 | * Should be called with interrupts off. |
| 5652 | */ |
| 5653 | struct backcmd { /* result of evalbackcmd */ |
| 5654 | int fd; /* file descriptor to read from */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5655 | int nleft; /* number of chars in buffer */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5656 | char *buf; /* buffer */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5657 | struct job *jp; /* job structure for command */ |
| 5658 | }; |
| 5659 | |
| 5660 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5661 | static uint8_t back_exitstatus; /* exit status of backquoted command */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5662 | #define EV_EXIT 01 /* exit after evaluating tree */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 5663 | static void evaltree(union node *, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5664 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 5665 | static void FAST_FUNC |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5666 | evalbackcmd(union node *n, struct backcmd *result) |
| 5667 | { |
| 5668 | int saveherefd; |
| 5669 | |
| 5670 | result->fd = -1; |
| 5671 | result->buf = NULL; |
| 5672 | result->nleft = 0; |
| 5673 | result->jp = NULL; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5674 | if (n == NULL) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5675 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5676 | |
| 5677 | saveherefd = herefd; |
| 5678 | herefd = -1; |
| 5679 | |
| 5680 | { |
| 5681 | int pip[2]; |
| 5682 | struct job *jp; |
| 5683 | |
| 5684 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5685 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 5686 | jp = makejob(/*n,*/ 1); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5687 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
| 5688 | FORCE_INT_ON; |
| 5689 | close(pip[0]); |
| 5690 | if (pip[1] != 1) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5691 | /*close(1);*/ |
| 5692 | copyfd(pip[1], 1 | COPYFD_EXACT); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5693 | close(pip[1]); |
| 5694 | } |
| 5695 | eflag = 0; |
| 5696 | evaltree(n, EV_EXIT); /* actually evaltreenr... */ |
| 5697 | /* NOTREACHED */ |
| 5698 | } |
| 5699 | close(pip[1]); |
| 5700 | result->fd = pip[0]; |
| 5701 | result->jp = jp; |
| 5702 | } |
| 5703 | herefd = saveherefd; |
| 5704 | out: |
| 5705 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 5706 | result->fd, result->buf, result->nleft, result->jp)); |
| 5707 | } |
| 5708 | |
| 5709 | /* |
| 5710 | * Expand stuff in backwards quotes. |
| 5711 | */ |
| 5712 | static void |
| 5713 | expbackq(union node *cmd, int quoted, int quotes) |
| 5714 | { |
| 5715 | struct backcmd in; |
| 5716 | int i; |
| 5717 | char buf[128]; |
| 5718 | char *p; |
| 5719 | char *dest; |
| 5720 | int startloc; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5721 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5722 | struct stackmark smark; |
| 5723 | |
| 5724 | INT_OFF; |
| 5725 | setstackmark(&smark); |
| 5726 | dest = expdest; |
| 5727 | startloc = dest - (char *)stackblock(); |
| 5728 | grabstackstr(dest); |
| 5729 | evalbackcmd(cmd, &in); |
| 5730 | popstackmark(&smark); |
| 5731 | |
| 5732 | p = in.buf; |
| 5733 | i = in.nleft; |
| 5734 | if (i == 0) |
| 5735 | goto read; |
| 5736 | for (;;) { |
| 5737 | memtodest(p, i, syntax, quotes); |
| 5738 | read: |
| 5739 | if (in.fd < 0) |
| 5740 | break; |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 5741 | i = nonblock_safe_read(in.fd, buf, sizeof(buf)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5742 | TRACE(("expbackq: read returns %d\n", i)); |
| 5743 | if (i <= 0) |
| 5744 | break; |
| 5745 | p = buf; |
| 5746 | } |
| 5747 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 5748 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5749 | if (in.fd >= 0) { |
| 5750 | close(in.fd); |
| 5751 | back_exitstatus = waitforjob(in.jp); |
| 5752 | } |
| 5753 | INT_ON; |
| 5754 | |
| 5755 | /* Eat all trailing newlines */ |
| 5756 | dest = expdest; |
| 5757 | for (; dest > (char *)stackblock() && dest[-1] == '\n';) |
| 5758 | STUNPUTC(dest); |
| 5759 | expdest = dest; |
| 5760 | |
| 5761 | if (quoted == 0) |
| 5762 | recordregion(startloc, dest - (char *)stackblock(), 0); |
| 5763 | TRACE(("evalbackq: size=%d: \"%.*s\"\n", |
| 5764 | (dest - (char *)stackblock()) - startloc, |
| 5765 | (dest - (char *)stackblock()) - startloc, |
| 5766 | stackblock() + startloc)); |
| 5767 | } |
| 5768 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5769 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5770 | /* |
| 5771 | * Expand arithmetic expression. Backup to start of expression, |
| 5772 | * evaluate, place result in (backed up) result, adjust string position. |
| 5773 | */ |
| 5774 | static void |
| 5775 | expari(int quotes) |
| 5776 | { |
| 5777 | char *p, *start; |
| 5778 | int begoff; |
| 5779 | int flag; |
| 5780 | int len; |
| 5781 | |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5782 | /* ifsfree(); */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5783 | |
| 5784 | /* |
| 5785 | * This routine is slightly over-complicated for |
| 5786 | * efficiency. Next we scan backwards looking for the |
| 5787 | * start of arithmetic. |
| 5788 | */ |
| 5789 | start = stackblock(); |
| 5790 | p = expdest - 1; |
| 5791 | *p = '\0'; |
| 5792 | p--; |
| 5793 | do { |
| 5794 | int esc; |
| 5795 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5796 | while ((unsigned char)*p != CTLARI) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5797 | p--; |
| 5798 | #if DEBUG |
| 5799 | if (p < start) { |
| 5800 | ash_msg_and_raise_error("missing CTLARI (shouldn't happen)"); |
| 5801 | } |
| 5802 | #endif |
| 5803 | } |
| 5804 | |
| 5805 | esc = esclen(start, p); |
| 5806 | if (!(esc % 2)) { |
| 5807 | break; |
| 5808 | } |
| 5809 | |
| 5810 | p -= esc + 1; |
| 5811 | } while (1); |
| 5812 | |
| 5813 | begoff = p - start; |
| 5814 | |
| 5815 | removerecordregions(begoff); |
| 5816 | |
| 5817 | flag = p[1]; |
| 5818 | |
| 5819 | expdest = p; |
| 5820 | |
| 5821 | if (quotes) |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5822 | rmescapes(p + 2, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5823 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5824 | len = cvtnum(ash_arith(p + 2)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5825 | |
| 5826 | if (flag != '"') |
| 5827 | recordregion(begoff, begoff + len, 0); |
| 5828 | } |
| 5829 | #endif |
| 5830 | |
| 5831 | /* argstr needs it */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5832 | static char *evalvar(char *p, int flags, struct strlist *var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5833 | |
| 5834 | /* |
| 5835 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 5836 | * characters to allow for further processing. Otherwise treat |
| 5837 | * $@ like $* since no splitting will be performed. |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5838 | * |
| 5839 | * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence |
| 5840 | * over shell varables. Needed for "A=a B=$A; echo $B" case - we use it |
| 5841 | * for correct expansion of "B=$A" word. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5842 | */ |
| 5843 | static void |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5844 | argstr(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5845 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5846 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5847 | '=', |
| 5848 | ':', |
| 5849 | CTLQUOTEMARK, |
| 5850 | CTLENDVAR, |
| 5851 | CTLESC, |
| 5852 | CTLVAR, |
| 5853 | CTLBACKQ, |
| 5854 | CTLBACKQ | CTLQUOTE, |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5855 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5856 | CTLENDARI, |
| 5857 | #endif |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5858 | '\0' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5859 | }; |
| 5860 | const char *reject = spclchars; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5861 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */ |
| 5862 | int breakall = flags & EXP_WORD; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5863 | int inquotes; |
| 5864 | size_t length; |
| 5865 | int startloc; |
| 5866 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5867 | if (!(flags & EXP_VARTILDE)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5868 | reject += 2; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5869 | } else if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5870 | reject++; |
| 5871 | } |
| 5872 | inquotes = 0; |
| 5873 | length = 0; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5874 | if (flags & EXP_TILDE) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5875 | char *q; |
| 5876 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5877 | flags &= ~EXP_TILDE; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5878 | tilde: |
| 5879 | q = p; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5880 | if (*q == CTLESC && (flags & EXP_QWORD)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5881 | q++; |
| 5882 | if (*q == '~') |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5883 | p = exptilde(p, q, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5884 | } |
| 5885 | start: |
| 5886 | startloc = expdest - (char *)stackblock(); |
| 5887 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5888 | unsigned char c; |
| 5889 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5890 | length += strcspn(p + length, reject); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5891 | c = p[length]; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5892 | if (c) { |
| 5893 | if (!(c & 0x80) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5894 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5895 | || c == CTLENDARI |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5896 | #endif |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5897 | ) { |
| 5898 | /* c == '=' || c == ':' || c == CTLENDARI */ |
| 5899 | length++; |
| 5900 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5901 | } |
| 5902 | if (length > 0) { |
| 5903 | int newloc; |
| 5904 | expdest = stack_nputstr(p, length, expdest); |
| 5905 | newloc = expdest - (char *)stackblock(); |
| 5906 | if (breakall && !inquotes && newloc > startloc) { |
| 5907 | recordregion(startloc, newloc, 0); |
| 5908 | } |
| 5909 | startloc = newloc; |
| 5910 | } |
| 5911 | p += length + 1; |
| 5912 | length = 0; |
| 5913 | |
| 5914 | switch (c) { |
| 5915 | case '\0': |
| 5916 | goto breakloop; |
| 5917 | case '=': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5918 | if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5919 | p--; |
| 5920 | continue; |
| 5921 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5922 | flags |= EXP_VARTILDE2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5923 | reject++; |
| 5924 | /* fall through */ |
| 5925 | case ':': |
| 5926 | /* |
| 5927 | * sort of a hack - expand tildes in variable |
| 5928 | * assignments (after the first '=' and after ':'s). |
| 5929 | */ |
| 5930 | if (*--p == '~') { |
| 5931 | goto tilde; |
| 5932 | } |
| 5933 | continue; |
| 5934 | } |
| 5935 | |
| 5936 | switch (c) { |
| 5937 | case CTLENDVAR: /* ??? */ |
| 5938 | goto breakloop; |
| 5939 | case CTLQUOTEMARK: |
| 5940 | /* "$@" syntax adherence hack */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5941 | if (!inquotes |
| 5942 | && memcmp(p, dolatstr, 4) == 0 |
| 5943 | && ( p[4] == CTLQUOTEMARK |
| 5944 | || (p[4] == CTLENDVAR && p[5] == CTLQUOTEMARK) |
| 5945 | ) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5946 | ) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5947 | p = evalvar(p + 1, flags, /* var_str_list: */ NULL) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5948 | goto start; |
| 5949 | } |
| 5950 | inquotes = !inquotes; |
| 5951 | addquote: |
| 5952 | if (quotes) { |
| 5953 | p--; |
| 5954 | length++; |
| 5955 | startloc++; |
| 5956 | } |
| 5957 | break; |
| 5958 | case CTLESC: |
| 5959 | startloc++; |
| 5960 | length++; |
| 5961 | goto addquote; |
| 5962 | case CTLVAR: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5963 | p = evalvar(p, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5964 | goto start; |
| 5965 | case CTLBACKQ: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5966 | c = '\0'; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5967 | case CTLBACKQ|CTLQUOTE: |
| 5968 | expbackq(argbackq->n, c, quotes); |
| 5969 | argbackq = argbackq->next; |
| 5970 | goto start; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5971 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5972 | case CTLENDARI: |
| 5973 | p--; |
| 5974 | expari(quotes); |
| 5975 | goto start; |
| 5976 | #endif |
| 5977 | } |
| 5978 | } |
| 5979 | breakloop: |
| 5980 | ; |
| 5981 | } |
| 5982 | |
| 5983 | static char * |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 5984 | 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] | 5985 | int zero) |
| 5986 | { |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5987 | // This commented out code was added by James Simmons <jsimmons@infradead.org> |
| 5988 | // as part of a larger change when he added support for ${var/a/b}. |
| 5989 | // However, it broke # and % operators: |
| 5990 | // |
| 5991 | //var=ababcdcd |
| 5992 | // ok bad |
| 5993 | //echo ${var#ab} abcdcd abcdcd |
| 5994 | //echo ${var##ab} abcdcd abcdcd |
| 5995 | //echo ${var#a*b} abcdcd ababcdcd (!) |
| 5996 | //echo ${var##a*b} cdcd cdcd |
| 5997 | //echo ${var#?} babcdcd ababcdcd (!) |
| 5998 | //echo ${var##?} babcdcd babcdcd |
| 5999 | //echo ${var#*} ababcdcd babcdcd (!) |
| 6000 | //echo ${var##*} |
| 6001 | //echo ${var%cd} ababcd ababcd |
| 6002 | //echo ${var%%cd} ababcd abab (!) |
| 6003 | //echo ${var%c*d} ababcd ababcd |
| 6004 | //echo ${var%%c*d} abab ababcdcd (!) |
| 6005 | //echo ${var%?} ababcdc ababcdc |
| 6006 | //echo ${var%%?} ababcdc ababcdcd (!) |
| 6007 | //echo ${var%*} ababcdcd ababcdcd |
| 6008 | //echo ${var%%*} |
| 6009 | // |
| 6010 | // Commenting it back out helped. Remove it completely if it really |
| 6011 | // is not needed. |
| 6012 | |
| 6013 | char *loc, *loc2; //, *full; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6014 | char c; |
| 6015 | |
| 6016 | loc = startp; |
| 6017 | loc2 = rmesc; |
| 6018 | do { |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6019 | int match; // = strlen(str); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6020 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6021 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6022 | c = *loc2; |
| 6023 | if (zero) { |
| 6024 | *loc2 = '\0'; |
| 6025 | s = rmesc; |
| 6026 | } |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6027 | match = pmatch(str, s); // this line was deleted |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6028 | |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6029 | // // chop off end if its '*' |
| 6030 | // full = strrchr(str, '*'); |
| 6031 | // if (full && full != str) |
| 6032 | // match--; |
| 6033 | // |
| 6034 | // // If str starts with '*' replace with s. |
| 6035 | // if ((*str == '*') && strlen(s) >= match) { |
| 6036 | // full = xstrdup(s); |
| 6037 | // strncpy(full+strlen(s)-match+1, str+1, match-1); |
| 6038 | // } else |
| 6039 | // full = xstrndup(str, match); |
| 6040 | // match = strncmp(s, full, strlen(full)); |
| 6041 | // free(full); |
| 6042 | // |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6043 | *loc2 = c; |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6044 | if (match) // if (!match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6045 | return loc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6046 | if (quotes && (unsigned char)*loc == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6047 | loc++; |
| 6048 | loc++; |
| 6049 | loc2++; |
| 6050 | } while (c); |
| 6051 | return 0; |
| 6052 | } |
| 6053 | |
| 6054 | static char * |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6055 | scanright(char *startp, char *rmesc, char *rmescend, char *pattern, int quotes, int match_at_start) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6056 | { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6057 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6058 | int try2optimize = match_at_start; |
| 6059 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6060 | int esc = 0; |
| 6061 | char *loc; |
| 6062 | char *loc2; |
| 6063 | |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6064 | /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}": |
| 6065 | * startp="escaped_value_of_v" rmesc="raw_value_of_v" |
| 6066 | * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1 |
| 6067 | * Logic: |
| 6068 | * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc, |
| 6069 | * and on each iteration they go back two/one char until they reach the beginning. |
| 6070 | * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc. |
| 6071 | */ |
| 6072 | /* TODO: document in what other circumstances we are called. */ |
| 6073 | |
| 6074 | for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6075 | int match; |
| 6076 | char c = *loc2; |
| 6077 | const char *s = loc2; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6078 | if (match_at_start) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6079 | *loc2 = '\0'; |
| 6080 | s = rmesc; |
| 6081 | } |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6082 | match = pmatch(pattern, s); |
| 6083 | //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6084 | *loc2 = c; |
| 6085 | if (match) |
| 6086 | return loc; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6087 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6088 | if (try2optimize) { |
| 6089 | /* Maybe we can optimize this: |
| 6090 | * if pattern ends with unescaped *, we can avoid checking |
| 6091 | * shorter strings: if "foo*" doesnt match "raw_value_of_v", |
| 6092 | * it wont match truncated "raw_value_of_" strings too. |
| 6093 | */ |
| 6094 | unsigned plen = strlen(pattern); |
| 6095 | /* Does it end with "*"? */ |
| 6096 | if (plen != 0 && pattern[--plen] == '*') { |
| 6097 | /* "xxxx*" is not escaped */ |
| 6098 | /* "xxx\*" is escaped */ |
| 6099 | /* "xx\\*" is not escaped */ |
| 6100 | /* "x\\\*" is escaped */ |
| 6101 | int slashes = 0; |
| 6102 | while (plen != 0 && pattern[--plen] == '\\') |
| 6103 | slashes++; |
| 6104 | if (!(slashes & 1)) |
| 6105 | break; /* ends with unescaped "*" */ |
| 6106 | } |
| 6107 | try2optimize = 0; |
| 6108 | } |
| 6109 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6110 | loc--; |
| 6111 | if (quotes) { |
| 6112 | if (--esc < 0) { |
| 6113 | esc = esclen(startp, loc); |
| 6114 | } |
| 6115 | if (esc % 2) { |
| 6116 | esc--; |
| 6117 | loc--; |
| 6118 | } |
| 6119 | } |
| 6120 | } |
| 6121 | return 0; |
| 6122 | } |
| 6123 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6124 | static void varunset(const char *, const char *, const char *, int) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6125 | static void |
| 6126 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 6127 | { |
| 6128 | const char *msg; |
| 6129 | const char *tail; |
| 6130 | |
| 6131 | tail = nullstr; |
| 6132 | msg = "parameter not set"; |
| 6133 | if (umsg) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6134 | if ((unsigned char)*end == CTLENDVAR) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6135 | if (varflags & VSNUL) |
| 6136 | tail = " or null"; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6137 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6138 | msg = umsg; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6139 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6140 | } |
| 6141 | ash_msg_and_raise_error("%.*s: %s%s", end - var - 1, var, msg, tail); |
| 6142 | } |
| 6143 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6144 | #if ENABLE_ASH_BASH_COMPAT |
| 6145 | static char * |
| 6146 | parse_sub_pattern(char *arg, int inquotes) |
| 6147 | { |
| 6148 | char *idx, *repl = NULL; |
| 6149 | unsigned char c; |
| 6150 | |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6151 | idx = arg; |
| 6152 | while (1) { |
| 6153 | c = *arg; |
| 6154 | if (!c) |
| 6155 | break; |
| 6156 | if (c == '/') { |
| 6157 | /* Only the first '/' seen is our separator */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6158 | if (!repl) { |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6159 | repl = idx + 1; |
| 6160 | c = '\0'; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6161 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6162 | } |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6163 | *idx++ = c; |
| 6164 | if (!inquotes && c == '\\' && arg[1] == '\\') |
| 6165 | arg++; /* skip both \\, not just first one */ |
| 6166 | arg++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6167 | } |
Denis Vlasenko | 29038c0 | 2008-06-14 06:14:02 +0000 | [diff] [blame] | 6168 | *idx = c; /* NUL */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6169 | |
| 6170 | return repl; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6171 | } |
| 6172 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6173 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6174 | static const char * |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6175 | subevalvar(char *p, char *str, int strloc, int subtype, |
| 6176 | int startloc, int varflags, int quotes, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6177 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6178 | struct nodelist *saveargbackq = argbackq; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6179 | char *startp; |
| 6180 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6181 | char *rmesc, *rmescend; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6182 | IF_ASH_BASH_COMPAT(char *repl = NULL;) |
| 6183 | IF_ASH_BASH_COMPAT(char null = '\0';) |
| 6184 | IF_ASH_BASH_COMPAT(int pos, len, orig_len;) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6185 | int saveherefd = herefd; |
| 6186 | int amount, workloc, resetloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6187 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6188 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6189 | |
| 6190 | herefd = -1; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6191 | argstr(p, (subtype != VSASSIGN && subtype != VSQUESTION) ? EXP_CASE : 0, |
| 6192 | var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6193 | STPUTC('\0', expdest); |
| 6194 | herefd = saveherefd; |
| 6195 | argbackq = saveargbackq; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6196 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6197 | |
| 6198 | switch (subtype) { |
| 6199 | case VSASSIGN: |
| 6200 | setvar(str, startp, 0); |
| 6201 | amount = startp - expdest; |
| 6202 | STADJUST(amount, expdest); |
| 6203 | return startp; |
| 6204 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6205 | #if ENABLE_ASH_BASH_COMPAT |
| 6206 | case VSSUBSTR: |
| 6207 | loc = str = stackblock() + strloc; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6208 | /* Read POS in ${var:POS:LEN} */ |
| 6209 | pos = atoi(loc); /* number(loc) errors out on "1:4" */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6210 | len = str - startp - 1; |
| 6211 | |
| 6212 | /* *loc != '\0', guaranteed by parser */ |
| 6213 | if (quotes) { |
| 6214 | char *ptr; |
| 6215 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6216 | /* Adjust the length by the number of escapes */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6217 | for (ptr = startp; ptr < (str - 1); ptr++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6218 | if ((unsigned char)*ptr == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6219 | len--; |
| 6220 | ptr++; |
| 6221 | } |
| 6222 | } |
| 6223 | } |
| 6224 | orig_len = len; |
| 6225 | |
| 6226 | if (*loc++ == ':') { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6227 | /* ${var::LEN} */ |
| 6228 | len = number(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6229 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6230 | /* Skip POS in ${var:POS:LEN} */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6231 | len = orig_len; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6232 | while (*loc && *loc != ':') { |
| 6233 | /* TODO? |
| 6234 | * bash complains on: var=qwe; echo ${var:1a:123} |
| 6235 | if (!isdigit(*loc)) |
| 6236 | ash_msg_and_raise_error(msg_illnum, str); |
| 6237 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6238 | loc++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6239 | } |
| 6240 | if (*loc++ == ':') { |
| 6241 | len = number(loc); |
| 6242 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6243 | } |
| 6244 | if (pos >= orig_len) { |
| 6245 | pos = 0; |
| 6246 | len = 0; |
| 6247 | } |
| 6248 | if (len > (orig_len - pos)) |
| 6249 | len = orig_len - pos; |
| 6250 | |
| 6251 | for (str = startp; pos; str++, pos--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6252 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6253 | str++; |
| 6254 | } |
| 6255 | for (loc = startp; len; len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6256 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6257 | *loc++ = *str++; |
| 6258 | *loc++ = *str++; |
| 6259 | } |
| 6260 | *loc = '\0'; |
| 6261 | amount = loc - expdest; |
| 6262 | STADJUST(amount, expdest); |
| 6263 | return loc; |
| 6264 | #endif |
| 6265 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6266 | case VSQUESTION: |
| 6267 | varunset(p, str, startp, varflags); |
| 6268 | /* NOTREACHED */ |
| 6269 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6270 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6271 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6272 | /* We'll comeback here if we grow the stack while handling |
| 6273 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 6274 | * stack will need rebasing, and we'll need to remove our work |
| 6275 | * areas each time |
| 6276 | */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6277 | IF_ASH_BASH_COMPAT(restart:) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6278 | |
| 6279 | amount = expdest - ((char *)stackblock() + resetloc); |
| 6280 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6281 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6282 | |
| 6283 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6284 | rmescend = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6285 | if (quotes) { |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 6286 | rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6287 | if (rmesc != startp) { |
| 6288 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6289 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6290 | } |
| 6291 | } |
| 6292 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6293 | str = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6294 | preglob(str, varflags & VSQUOTE, 0); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6295 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6296 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6297 | #if ENABLE_ASH_BASH_COMPAT |
| 6298 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6299 | char *idx, *end; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6300 | |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 6301 | if (!repl) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6302 | repl = parse_sub_pattern(str, varflags & VSQUOTE); |
| 6303 | if (!repl) |
| 6304 | repl = &null; |
| 6305 | } |
| 6306 | |
| 6307 | /* If there's no pattern to match, return the expansion unmolested */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6308 | if (str[0] == '\0') |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6309 | return 0; |
| 6310 | |
| 6311 | len = 0; |
| 6312 | idx = startp; |
| 6313 | end = str - 1; |
| 6314 | while (idx < end) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6315 | try_to_match: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6316 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); |
| 6317 | if (!loc) { |
| 6318 | /* No match, advance */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6319 | char *restart_detect = stackblock(); |
| 6320 | skip_matching: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6321 | STPUTC(*idx, expdest); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6322 | if (quotes && (unsigned char)*idx == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6323 | idx++; |
| 6324 | len++; |
| 6325 | STPUTC(*idx, expdest); |
| 6326 | } |
| 6327 | if (stackblock() != restart_detect) |
| 6328 | goto restart; |
| 6329 | idx++; |
| 6330 | len++; |
| 6331 | rmesc++; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6332 | /* continue; - prone to quadratic behavior, smarter code: */ |
| 6333 | if (idx >= end) |
| 6334 | break; |
| 6335 | if (str[0] == '*') { |
| 6336 | /* Pattern is "*foo". If "*foo" does not match "long_string", |
| 6337 | * it would never match "ong_string" etc, no point in trying. |
| 6338 | */ |
| 6339 | goto skip_matching; |
| 6340 | } |
| 6341 | goto try_to_match; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
| 6344 | if (subtype == VSREPLACEALL) { |
| 6345 | while (idx < loc) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6346 | if (quotes && (unsigned char)*idx == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6347 | idx++; |
| 6348 | idx++; |
| 6349 | rmesc++; |
| 6350 | } |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6351 | } else { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6352 | idx = loc; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6353 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6354 | |
| 6355 | for (loc = repl; *loc; loc++) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6356 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6357 | STPUTC(*loc, expdest); |
| 6358 | if (stackblock() != restart_detect) |
| 6359 | goto restart; |
| 6360 | len++; |
| 6361 | } |
| 6362 | |
| 6363 | if (subtype == VSREPLACE) { |
| 6364 | while (*idx) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6365 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6366 | STPUTC(*idx, expdest); |
| 6367 | if (stackblock() != restart_detect) |
| 6368 | goto restart; |
| 6369 | len++; |
| 6370 | idx++; |
| 6371 | } |
| 6372 | break; |
| 6373 | } |
| 6374 | } |
| 6375 | |
| 6376 | /* We've put the replaced text into a buffer at workloc, now |
| 6377 | * move it to the right place and adjust the stack. |
| 6378 | */ |
| 6379 | startp = stackblock() + startloc; |
| 6380 | STPUTC('\0', expdest); |
| 6381 | memmove(startp, stackblock() + workloc, len); |
| 6382 | startp[len++] = '\0'; |
| 6383 | amount = expdest - ((char *)stackblock() + startloc + len - 1); |
| 6384 | STADJUST(-amount, expdest); |
| 6385 | return startp; |
| 6386 | } |
| 6387 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6388 | |
| 6389 | subtype -= VSTRIMRIGHT; |
| 6390 | #if DEBUG |
| 6391 | if (subtype < 0 || subtype > 7) |
| 6392 | abort(); |
| 6393 | #endif |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6394 | /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6395 | zero = subtype >> 1; |
| 6396 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 6397 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 6398 | |
| 6399 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 6400 | if (loc) { |
| 6401 | if (zero) { |
| 6402 | memmove(startp, loc, str - loc); |
| 6403 | loc = startp + (str - loc) - 1; |
| 6404 | } |
| 6405 | *loc = '\0'; |
| 6406 | amount = loc - expdest; |
| 6407 | STADJUST(amount, expdest); |
| 6408 | } |
| 6409 | return loc; |
| 6410 | } |
| 6411 | |
| 6412 | /* |
| 6413 | * Add the value of a specialized variable to the stack string. |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6414 | * name parameter (examples): |
| 6415 | * ash -c 'echo $1' name:'1=' |
| 6416 | * ash -c 'echo $qwe' name:'qwe=' |
| 6417 | * ash -c 'echo $$' name:'$=' |
| 6418 | * ash -c 'echo ${$}' name:'$=' |
| 6419 | * ash -c 'echo ${$##q}' name:'$=q' |
| 6420 | * ash -c 'echo ${#$}' name:'$=' |
| 6421 | * note: examples with bad shell syntax: |
| 6422 | * ash -c 'echo ${#$1}' name:'$=1' |
| 6423 | * ash -c 'echo ${#1#}' name:'1=#' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6424 | */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 6425 | static NOINLINE ssize_t |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6426 | varvalue(char *name, int varflags, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6427 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6428 | const char *p; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6429 | int num; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6430 | int i; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6431 | int sepq = 0; |
| 6432 | ssize_t len = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6433 | int subtype = varflags & VSTYPE; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6434 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6435 | int quoted = varflags & VSQUOTE; |
| 6436 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6437 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6438 | switch (*name) { |
| 6439 | case '$': |
| 6440 | num = rootpid; |
| 6441 | goto numvar; |
| 6442 | case '?': |
| 6443 | num = exitstatus; |
| 6444 | goto numvar; |
| 6445 | case '#': |
| 6446 | num = shellparam.nparam; |
| 6447 | goto numvar; |
| 6448 | case '!': |
| 6449 | num = backgndpid; |
| 6450 | if (num == 0) |
| 6451 | return -1; |
| 6452 | numvar: |
| 6453 | len = cvtnum(num); |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6454 | goto check_1char_name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6455 | case '-': |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6456 | expdest = makestrspace(NOPTS, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6457 | for (i = NOPTS - 1; i >= 0; i--) { |
| 6458 | if (optlist[i]) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6459 | USTPUTC(optletters(i), expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6460 | len++; |
| 6461 | } |
| 6462 | } |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6463 | check_1char_name: |
| 6464 | #if 0 |
| 6465 | /* handles cases similar to ${#$1} */ |
| 6466 | if (name[2] != '\0') |
| 6467 | raise_error_syntax("bad substitution"); |
| 6468 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6469 | break; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6470 | case '@': { |
| 6471 | char **ap; |
| 6472 | int sep; |
| 6473 | |
| 6474 | if (quoted && (flags & EXP_FULL)) { |
| 6475 | /* note: this is not meant as PEOF value */ |
| 6476 | sep = 1 << CHAR_BIT; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6477 | goto param; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6478 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6479 | /* fall through */ |
| 6480 | case '*': |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6481 | sep = ifsset() ? (unsigned char)(ifsval()[0]) : ' '; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6482 | i = SIT(sep, syntax); |
| 6483 | if (quotes && (i == CCTL || i == CBACK)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6484 | sepq = 1; |
| 6485 | param: |
| 6486 | ap = shellparam.p; |
| 6487 | if (!ap) |
| 6488 | return -1; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6489 | while ((p = *ap++) != NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6490 | size_t partlen; |
| 6491 | |
| 6492 | partlen = strlen(p); |
| 6493 | len += partlen; |
| 6494 | |
| 6495 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6496 | memtodest(p, partlen, syntax, quotes); |
| 6497 | |
| 6498 | if (*ap && sep) { |
| 6499 | char *q; |
| 6500 | |
| 6501 | len++; |
| 6502 | if (subtype == VSPLUS || subtype == VSLENGTH) { |
| 6503 | continue; |
| 6504 | } |
| 6505 | q = expdest; |
| 6506 | if (sepq) |
| 6507 | STPUTC(CTLESC, q); |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6508 | /* note: may put NUL despite sep != 0 |
| 6509 | * (see sep = 1 << CHAR_BIT above) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6510 | STPUTC(sep, q); |
| 6511 | expdest = q; |
| 6512 | } |
| 6513 | } |
| 6514 | return len; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6515 | } /* case '@' and '*' */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6516 | case '0': |
| 6517 | case '1': |
| 6518 | case '2': |
| 6519 | case '3': |
| 6520 | case '4': |
| 6521 | case '5': |
| 6522 | case '6': |
| 6523 | case '7': |
| 6524 | case '8': |
| 6525 | case '9': |
Denys Vlasenko | a00329c | 2009-08-30 20:05:10 +0200 | [diff] [blame] | 6526 | num = atoi(name); /* number(name) fails on ${N#str} etc */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6527 | if (num < 0 || num > shellparam.nparam) |
| 6528 | return -1; |
| 6529 | p = num ? shellparam.p[num - 1] : arg0; |
| 6530 | goto value; |
| 6531 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6532 | /* NB: name has form "VAR=..." */ |
| 6533 | |
| 6534 | /* "A=a B=$A" case: var_str_list is a list of "A=a" strings |
| 6535 | * which should be considered before we check variables. */ |
| 6536 | if (var_str_list) { |
| 6537 | unsigned name_len = (strchrnul(name, '=') - name) + 1; |
| 6538 | p = NULL; |
| 6539 | do { |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 6540 | char *str, *eq; |
| 6541 | str = var_str_list->text; |
| 6542 | eq = strchr(str, '='); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6543 | if (!eq) /* stop at first non-assignment */ |
| 6544 | break; |
| 6545 | eq++; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 6546 | if (name_len == (unsigned)(eq - str) |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6547 | && strncmp(str, name, name_len) == 0 |
| 6548 | ) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6549 | p = eq; |
| 6550 | /* goto value; - WRONG! */ |
| 6551 | /* think "A=1 A=2 B=$A" */ |
| 6552 | } |
| 6553 | var_str_list = var_str_list->next; |
| 6554 | } while (var_str_list); |
| 6555 | if (p) |
| 6556 | goto value; |
| 6557 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6558 | p = lookupvar(name); |
| 6559 | value: |
| 6560 | if (!p) |
| 6561 | return -1; |
| 6562 | |
| 6563 | len = strlen(p); |
| 6564 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6565 | memtodest(p, len, syntax, quotes); |
| 6566 | return len; |
| 6567 | } |
| 6568 | |
| 6569 | if (subtype == VSPLUS || subtype == VSLENGTH) |
| 6570 | STADJUST(-len, expdest); |
| 6571 | return len; |
| 6572 | } |
| 6573 | |
| 6574 | /* |
| 6575 | * Expand a variable, and return a pointer to the next character in the |
| 6576 | * input string. |
| 6577 | */ |
| 6578 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6579 | evalvar(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6580 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6581 | char varflags; |
| 6582 | char subtype; |
| 6583 | char quoted; |
| 6584 | char easy; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6585 | char *var; |
| 6586 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6587 | int startloc; |
| 6588 | ssize_t varlen; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6589 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6590 | varflags = (unsigned char) *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6591 | subtype = varflags & VSTYPE; |
| 6592 | quoted = varflags & VSQUOTE; |
| 6593 | var = p; |
| 6594 | easy = (!quoted || (*var == '@' && shellparam.nparam)); |
| 6595 | startloc = expdest - (char *)stackblock(); |
| 6596 | p = strchr(p, '=') + 1; |
| 6597 | |
| 6598 | again: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6599 | varlen = varvalue(var, varflags, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6600 | if (varflags & VSNUL) |
| 6601 | varlen--; |
| 6602 | |
| 6603 | if (subtype == VSPLUS) { |
| 6604 | varlen = -1 - varlen; |
| 6605 | goto vsplus; |
| 6606 | } |
| 6607 | |
| 6608 | if (subtype == VSMINUS) { |
| 6609 | vsplus: |
| 6610 | if (varlen < 0) { |
| 6611 | argstr( |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6612 | p, flags | EXP_TILDE | |
| 6613 | (quoted ? EXP_QWORD : EXP_WORD), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6614 | var_str_list |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6615 | ); |
| 6616 | goto end; |
| 6617 | } |
| 6618 | if (easy) |
| 6619 | goto record; |
| 6620 | goto end; |
| 6621 | } |
| 6622 | |
| 6623 | if (subtype == VSASSIGN || subtype == VSQUESTION) { |
| 6624 | if (varlen < 0) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6625 | if (subevalvar(p, var, /* strloc: */ 0, |
| 6626 | subtype, startloc, varflags, |
| 6627 | /* quotes: */ 0, |
| 6628 | var_str_list) |
| 6629 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6630 | varflags &= ~VSNUL; |
| 6631 | /* |
| 6632 | * Remove any recorded regions beyond |
| 6633 | * start of variable |
| 6634 | */ |
| 6635 | removerecordregions(startloc); |
| 6636 | goto again; |
| 6637 | } |
| 6638 | goto end; |
| 6639 | } |
| 6640 | if (easy) |
| 6641 | goto record; |
| 6642 | goto end; |
| 6643 | } |
| 6644 | |
| 6645 | if (varlen < 0 && uflag) |
| 6646 | varunset(p, var, 0, 0); |
| 6647 | |
| 6648 | if (subtype == VSLENGTH) { |
| 6649 | cvtnum(varlen > 0 ? varlen : 0); |
| 6650 | goto record; |
| 6651 | } |
| 6652 | |
| 6653 | if (subtype == VSNORMAL) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6654 | if (easy) |
| 6655 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6656 | goto end; |
| 6657 | } |
| 6658 | |
| 6659 | #if DEBUG |
| 6660 | switch (subtype) { |
| 6661 | case VSTRIMLEFT: |
| 6662 | case VSTRIMLEFTMAX: |
| 6663 | case VSTRIMRIGHT: |
| 6664 | case VSTRIMRIGHTMAX: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6665 | #if ENABLE_ASH_BASH_COMPAT |
| 6666 | case VSSUBSTR: |
| 6667 | case VSREPLACE: |
| 6668 | case VSREPLACEALL: |
| 6669 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6670 | break; |
| 6671 | default: |
| 6672 | abort(); |
| 6673 | } |
| 6674 | #endif |
| 6675 | |
| 6676 | if (varlen >= 0) { |
| 6677 | /* |
| 6678 | * Terminate the string and start recording the pattern |
| 6679 | * right after it |
| 6680 | */ |
| 6681 | STPUTC('\0', expdest); |
| 6682 | patloc = expdest - (char *)stackblock(); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6683 | if (0 == subevalvar(p, /* str: */ NULL, patloc, subtype, |
| 6684 | startloc, varflags, |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6685 | //TODO: | EXP_REDIR too? All other such places do it too |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6686 | /* quotes: */ flags & (EXP_FULL | EXP_CASE), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6687 | var_str_list) |
| 6688 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6689 | int amount = expdest - ( |
| 6690 | (char *)stackblock() + patloc - 1 |
| 6691 | ); |
| 6692 | STADJUST(-amount, expdest); |
| 6693 | } |
| 6694 | /* Remove any recorded regions beyond start of variable */ |
| 6695 | removerecordregions(startloc); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6696 | record: |
| 6697 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6698 | } |
| 6699 | |
| 6700 | end: |
| 6701 | if (subtype != VSNORMAL) { /* skip to end of alternative */ |
| 6702 | int nesting = 1; |
| 6703 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6704 | unsigned char c = *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6705 | if (c == CTLESC) |
| 6706 | p++; |
| 6707 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { |
| 6708 | if (varlen >= 0) |
| 6709 | argbackq = argbackq->next; |
| 6710 | } else if (c == CTLVAR) { |
| 6711 | if ((*p++ & VSTYPE) != VSNORMAL) |
| 6712 | nesting++; |
| 6713 | } else if (c == CTLENDVAR) { |
| 6714 | if (--nesting == 0) |
| 6715 | break; |
| 6716 | } |
| 6717 | } |
| 6718 | } |
| 6719 | return p; |
| 6720 | } |
| 6721 | |
| 6722 | /* |
| 6723 | * Break the argument string into pieces based upon IFS and add the |
| 6724 | * strings to the argument list. The regions of the string to be |
| 6725 | * searched for IFS characters have been stored by recordregion. |
| 6726 | */ |
| 6727 | static void |
| 6728 | ifsbreakup(char *string, struct arglist *arglist) |
| 6729 | { |
| 6730 | struct ifsregion *ifsp; |
| 6731 | struct strlist *sp; |
| 6732 | char *start; |
| 6733 | char *p; |
| 6734 | char *q; |
| 6735 | const char *ifs, *realifs; |
| 6736 | int ifsspc; |
| 6737 | int nulonly; |
| 6738 | |
| 6739 | start = string; |
| 6740 | if (ifslastp != NULL) { |
| 6741 | ifsspc = 0; |
| 6742 | nulonly = 0; |
| 6743 | realifs = ifsset() ? ifsval() : defifs; |
| 6744 | ifsp = &ifsfirst; |
| 6745 | do { |
| 6746 | p = string + ifsp->begoff; |
| 6747 | nulonly = ifsp->nulonly; |
| 6748 | ifs = nulonly ? nullstr : realifs; |
| 6749 | ifsspc = 0; |
| 6750 | while (p < string + ifsp->endoff) { |
| 6751 | q = p; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6752 | if ((unsigned char)*p == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6753 | p++; |
| 6754 | if (!strchr(ifs, *p)) { |
| 6755 | p++; |
| 6756 | continue; |
| 6757 | } |
| 6758 | if (!nulonly) |
| 6759 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6760 | /* Ignore IFS whitespace at start */ |
| 6761 | if (q == start && ifsspc) { |
| 6762 | p++; |
| 6763 | start = p; |
| 6764 | continue; |
| 6765 | } |
| 6766 | *q = '\0'; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6767 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6768 | sp->text = start; |
| 6769 | *arglist->lastp = sp; |
| 6770 | arglist->lastp = &sp->next; |
| 6771 | p++; |
| 6772 | if (!nulonly) { |
| 6773 | for (;;) { |
| 6774 | if (p >= string + ifsp->endoff) { |
| 6775 | break; |
| 6776 | } |
| 6777 | q = p; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6778 | if ((unsigned char)*p == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6779 | p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6780 | if (strchr(ifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6781 | p = q; |
| 6782 | break; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6783 | } |
| 6784 | if (strchr(defifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6785 | if (ifsspc) { |
| 6786 | p++; |
| 6787 | ifsspc = 0; |
| 6788 | } else { |
| 6789 | p = q; |
| 6790 | break; |
| 6791 | } |
| 6792 | } else |
| 6793 | p++; |
| 6794 | } |
| 6795 | } |
| 6796 | start = p; |
| 6797 | } /* while */ |
| 6798 | ifsp = ifsp->next; |
| 6799 | } while (ifsp != NULL); |
| 6800 | if (nulonly) |
| 6801 | goto add; |
| 6802 | } |
| 6803 | |
| 6804 | if (!*start) |
| 6805 | return; |
| 6806 | |
| 6807 | add: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6808 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6809 | sp->text = start; |
| 6810 | *arglist->lastp = sp; |
| 6811 | arglist->lastp = &sp->next; |
| 6812 | } |
| 6813 | |
| 6814 | static void |
| 6815 | ifsfree(void) |
| 6816 | { |
| 6817 | struct ifsregion *p; |
| 6818 | |
| 6819 | INT_OFF; |
| 6820 | p = ifsfirst.next; |
| 6821 | do { |
| 6822 | struct ifsregion *ifsp; |
| 6823 | ifsp = p->next; |
| 6824 | free(p); |
| 6825 | p = ifsp; |
| 6826 | } while (p); |
| 6827 | ifslastp = NULL; |
| 6828 | ifsfirst.next = NULL; |
| 6829 | INT_ON; |
| 6830 | } |
| 6831 | |
| 6832 | /* |
| 6833 | * Add a file name to the list. |
| 6834 | */ |
| 6835 | static void |
| 6836 | addfname(const char *name) |
| 6837 | { |
| 6838 | struct strlist *sp; |
| 6839 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6840 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6841 | sp->text = ststrdup(name); |
| 6842 | *exparg.lastp = sp; |
| 6843 | exparg.lastp = &sp->next; |
| 6844 | } |
| 6845 | |
| 6846 | static char *expdir; |
| 6847 | |
| 6848 | /* |
| 6849 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 6850 | */ |
| 6851 | static void |
| 6852 | expmeta(char *enddir, char *name) |
| 6853 | { |
| 6854 | char *p; |
| 6855 | const char *cp; |
| 6856 | char *start; |
| 6857 | char *endname; |
| 6858 | int metaflag; |
| 6859 | struct stat statb; |
| 6860 | DIR *dirp; |
| 6861 | struct dirent *dp; |
| 6862 | int atend; |
| 6863 | int matchdot; |
| 6864 | |
| 6865 | metaflag = 0; |
| 6866 | start = name; |
| 6867 | for (p = name; *p; p++) { |
| 6868 | if (*p == '*' || *p == '?') |
| 6869 | metaflag = 1; |
| 6870 | else if (*p == '[') { |
| 6871 | char *q = p + 1; |
| 6872 | if (*q == '!') |
| 6873 | q++; |
| 6874 | for (;;) { |
| 6875 | if (*q == '\\') |
| 6876 | q++; |
| 6877 | if (*q == '/' || *q == '\0') |
| 6878 | break; |
| 6879 | if (*++q == ']') { |
| 6880 | metaflag = 1; |
| 6881 | break; |
| 6882 | } |
| 6883 | } |
| 6884 | } else if (*p == '\\') |
| 6885 | p++; |
| 6886 | else if (*p == '/') { |
| 6887 | if (metaflag) |
| 6888 | goto out; |
| 6889 | start = p + 1; |
| 6890 | } |
| 6891 | } |
| 6892 | out: |
| 6893 | if (metaflag == 0) { /* we've reached the end of the file name */ |
| 6894 | if (enddir != expdir) |
| 6895 | metaflag++; |
| 6896 | p = name; |
| 6897 | do { |
| 6898 | if (*p == '\\') |
| 6899 | p++; |
| 6900 | *enddir++ = *p; |
| 6901 | } while (*p++); |
| 6902 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
| 6903 | addfname(expdir); |
| 6904 | return; |
| 6905 | } |
| 6906 | endname = p; |
| 6907 | if (name < start) { |
| 6908 | p = name; |
| 6909 | do { |
| 6910 | if (*p == '\\') |
| 6911 | p++; |
| 6912 | *enddir++ = *p++; |
| 6913 | } while (p < start); |
| 6914 | } |
| 6915 | if (enddir == expdir) { |
| 6916 | cp = "."; |
| 6917 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 6918 | cp = "/"; |
| 6919 | } else { |
| 6920 | cp = expdir; |
| 6921 | enddir[-1] = '\0'; |
| 6922 | } |
| 6923 | dirp = opendir(cp); |
| 6924 | if (dirp == NULL) |
| 6925 | return; |
| 6926 | if (enddir != expdir) |
| 6927 | enddir[-1] = '/'; |
| 6928 | if (*endname == 0) { |
| 6929 | atend = 1; |
| 6930 | } else { |
| 6931 | atend = 0; |
| 6932 | *endname++ = '\0'; |
| 6933 | } |
| 6934 | matchdot = 0; |
| 6935 | p = start; |
| 6936 | if (*p == '\\') |
| 6937 | p++; |
| 6938 | if (*p == '.') |
| 6939 | matchdot++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6940 | while (!pending_int && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 6941 | if (dp->d_name[0] == '.' && !matchdot) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6942 | continue; |
| 6943 | if (pmatch(start, dp->d_name)) { |
| 6944 | if (atend) { |
| 6945 | strcpy(enddir, dp->d_name); |
| 6946 | addfname(expdir); |
| 6947 | } else { |
| 6948 | for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';) |
| 6949 | continue; |
| 6950 | p[-1] = '/'; |
| 6951 | expmeta(p, endname); |
| 6952 | } |
| 6953 | } |
| 6954 | } |
| 6955 | closedir(dirp); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 6956 | if (!atend) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6957 | endname[-1] = '/'; |
| 6958 | } |
| 6959 | |
| 6960 | static struct strlist * |
| 6961 | msort(struct strlist *list, int len) |
| 6962 | { |
| 6963 | struct strlist *p, *q = NULL; |
| 6964 | struct strlist **lpp; |
| 6965 | int half; |
| 6966 | int n; |
| 6967 | |
| 6968 | if (len <= 1) |
| 6969 | return list; |
| 6970 | half = len >> 1; |
| 6971 | p = list; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6972 | for (n = half; --n >= 0;) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6973 | q = p; |
| 6974 | p = p->next; |
| 6975 | } |
| 6976 | q->next = NULL; /* terminate first half of list */ |
| 6977 | q = msort(list, half); /* sort first half of list */ |
| 6978 | p = msort(p, len - half); /* sort second half */ |
| 6979 | lpp = &list; |
| 6980 | for (;;) { |
| 6981 | #if ENABLE_LOCALE_SUPPORT |
| 6982 | if (strcoll(p->text, q->text) < 0) |
| 6983 | #else |
| 6984 | if (strcmp(p->text, q->text) < 0) |
| 6985 | #endif |
| 6986 | { |
| 6987 | *lpp = p; |
| 6988 | lpp = &p->next; |
| 6989 | p = *lpp; |
| 6990 | if (p == NULL) { |
| 6991 | *lpp = q; |
| 6992 | break; |
| 6993 | } |
| 6994 | } else { |
| 6995 | *lpp = q; |
| 6996 | lpp = &q->next; |
| 6997 | q = *lpp; |
| 6998 | if (q == NULL) { |
| 6999 | *lpp = p; |
| 7000 | break; |
| 7001 | } |
| 7002 | } |
| 7003 | } |
| 7004 | return list; |
| 7005 | } |
| 7006 | |
| 7007 | /* |
| 7008 | * Sort the results of file name expansion. It calculates the number of |
| 7009 | * strings to sort and then calls msort (short for merge sort) to do the |
| 7010 | * work. |
| 7011 | */ |
| 7012 | static struct strlist * |
| 7013 | expsort(struct strlist *str) |
| 7014 | { |
| 7015 | int len; |
| 7016 | struct strlist *sp; |
| 7017 | |
| 7018 | len = 0; |
| 7019 | for (sp = str; sp; sp = sp->next) |
| 7020 | len++; |
| 7021 | return msort(str, len); |
| 7022 | } |
| 7023 | |
| 7024 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7025 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7026 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 7027 | static const char metachars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7028 | '*', '?', '[', 0 |
| 7029 | }; |
| 7030 | /* TODO - EXP_REDIR */ |
| 7031 | |
| 7032 | while (str) { |
| 7033 | struct strlist **savelastp; |
| 7034 | struct strlist *sp; |
| 7035 | char *p; |
| 7036 | |
| 7037 | if (fflag) |
| 7038 | goto nometa; |
| 7039 | if (!strpbrk(str->text, metachars)) |
| 7040 | goto nometa; |
| 7041 | savelastp = exparg.lastp; |
| 7042 | |
| 7043 | INT_OFF; |
| 7044 | p = preglob(str->text, 0, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
| 7045 | { |
| 7046 | int i = strlen(str->text); |
| 7047 | expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ |
| 7048 | } |
| 7049 | |
| 7050 | expmeta(expdir, p); |
| 7051 | free(expdir); |
| 7052 | if (p != str->text) |
| 7053 | free(p); |
| 7054 | INT_ON; |
| 7055 | if (exparg.lastp == savelastp) { |
| 7056 | /* |
| 7057 | * no matches |
| 7058 | */ |
| 7059 | nometa: |
| 7060 | *exparg.lastp = str; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7061 | rmescapes(str->text, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7062 | exparg.lastp = &str->next; |
| 7063 | } else { |
| 7064 | *exparg.lastp = NULL; |
| 7065 | *savelastp = sp = expsort(*savelastp); |
| 7066 | while (sp->next != NULL) |
| 7067 | sp = sp->next; |
| 7068 | exparg.lastp = &sp->next; |
| 7069 | } |
| 7070 | str = str->next; |
| 7071 | } |
| 7072 | } |
| 7073 | |
| 7074 | /* |
| 7075 | * Perform variable substitution and command substitution on an argument, |
| 7076 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 7077 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 7078 | * here document expansion. |
| 7079 | */ |
| 7080 | static void |
| 7081 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 7082 | { |
| 7083 | struct strlist *sp; |
| 7084 | char *p; |
| 7085 | |
| 7086 | argbackq = arg->narg.backquote; |
| 7087 | STARTSTACKSTR(expdest); |
| 7088 | ifsfirst.next = NULL; |
| 7089 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7090 | argstr(arg->narg.text, flag, |
| 7091 | /* var_str_list: */ arglist ? arglist->list : NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7092 | p = _STPUTC('\0', expdest); |
| 7093 | expdest = p - 1; |
| 7094 | if (arglist == NULL) { |
| 7095 | return; /* here document expanded */ |
| 7096 | } |
| 7097 | p = grabstackstr(p); |
| 7098 | exparg.lastp = &exparg.list; |
| 7099 | /* |
| 7100 | * TODO - EXP_REDIR |
| 7101 | */ |
| 7102 | if (flag & EXP_FULL) { |
| 7103 | ifsbreakup(p, &exparg); |
| 7104 | *exparg.lastp = NULL; |
| 7105 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7106 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7107 | } else { |
| 7108 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7109 | rmescapes(p, 0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7110 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7111 | sp->text = p; |
| 7112 | *exparg.lastp = sp; |
| 7113 | exparg.lastp = &sp->next; |
| 7114 | } |
| 7115 | if (ifsfirst.next) |
| 7116 | ifsfree(); |
| 7117 | *exparg.lastp = NULL; |
| 7118 | if (exparg.list) { |
| 7119 | *arglist->lastp = exparg.list; |
| 7120 | arglist->lastp = exparg.lastp; |
| 7121 | } |
| 7122 | } |
| 7123 | |
| 7124 | /* |
| 7125 | * Expand shell variables and backquotes inside a here document. |
| 7126 | */ |
| 7127 | static void |
| 7128 | expandhere(union node *arg, int fd) |
| 7129 | { |
| 7130 | herefd = fd; |
| 7131 | expandarg(arg, (struct arglist *)NULL, 0); |
| 7132 | full_write(fd, stackblock(), expdest - (char *)stackblock()); |
| 7133 | } |
| 7134 | |
| 7135 | /* |
| 7136 | * Returns true if the pattern matches the string. |
| 7137 | */ |
| 7138 | static int |
| 7139 | patmatch(char *pattern, const char *string) |
| 7140 | { |
| 7141 | return pmatch(preglob(pattern, 0, 0), string); |
| 7142 | } |
| 7143 | |
| 7144 | /* |
| 7145 | * See if a pattern matches in a case statement. |
| 7146 | */ |
| 7147 | static int |
| 7148 | casematch(union node *pattern, char *val) |
| 7149 | { |
| 7150 | struct stackmark smark; |
| 7151 | int result; |
| 7152 | |
| 7153 | setstackmark(&smark); |
| 7154 | argbackq = pattern->narg.backquote; |
| 7155 | STARTSTACKSTR(expdest); |
| 7156 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7157 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE, |
| 7158 | /* var_str_list: */ NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7159 | STACKSTRNUL(expdest); |
| 7160 | result = patmatch(stackblock(), val); |
| 7161 | popstackmark(&smark); |
| 7162 | return result; |
| 7163 | } |
| 7164 | |
| 7165 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7166 | /* ============ find_command */ |
| 7167 | |
| 7168 | struct builtincmd { |
| 7169 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7170 | int (*builtin)(int, char **) FAST_FUNC; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7171 | /* unsigned flags; */ |
| 7172 | }; |
| 7173 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 7174 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7175 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7176 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7177 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7178 | |
| 7179 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7180 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7181 | union param { |
| 7182 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7183 | /* index >= 0 for commands without path (slashes) */ |
| 7184 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 7185 | /* index == -1 for commands with slashes */ |
| 7186 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7187 | const struct builtincmd *cmd; |
| 7188 | struct funcnode *func; |
| 7189 | } u; |
| 7190 | }; |
| 7191 | /* values of cmdtype */ |
| 7192 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 7193 | #define CMDNORMAL 0 /* command is an executable program */ |
| 7194 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 7195 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 7196 | |
| 7197 | /* action to find_command() */ |
| 7198 | #define DO_ERR 0x01 /* prints errors */ |
| 7199 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 7200 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 7201 | #define DO_ALTPATH 0x08 /* using alternate path */ |
| 7202 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ |
| 7203 | |
| 7204 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 7205 | |
| 7206 | |
| 7207 | /* ============ Hashing commands */ |
| 7208 | |
| 7209 | /* |
| 7210 | * When commands are first encountered, they are entered in a hash table. |
| 7211 | * This ensures that a full path search will not have to be done for them |
| 7212 | * on each invocation. |
| 7213 | * |
| 7214 | * We should investigate converting to a linear search, even though that |
| 7215 | * would make the command name "hash" a misnomer. |
| 7216 | */ |
| 7217 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7218 | struct tblentry { |
| 7219 | struct tblentry *next; /* next entry in hash chain */ |
| 7220 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7221 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7222 | char rehash; /* if set, cd done since entry created */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7223 | char cmdname[1]; /* name of command */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7224 | }; |
| 7225 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 7226 | static struct tblentry **cmdtable; |
| 7227 | #define INIT_G_cmdtable() do { \ |
| 7228 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 7229 | } while (0) |
| 7230 | |
| 7231 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 7232 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7233 | |
| 7234 | static void |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7235 | 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] | 7236 | { |
| 7237 | int repeated = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7238 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7239 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7240 | if (applet_no >= 0) { |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7241 | if (APPLET_IS_NOEXEC(applet_no)) { |
| 7242 | while (*envp) |
| 7243 | putenv(*envp++); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7244 | run_applet_no_and_exit(applet_no, argv); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7245 | } |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7246 | /* re-exec ourselves with the new arguments */ |
| 7247 | execve(bb_busybox_exec_path, argv, envp); |
| 7248 | /* If they called chroot or otherwise made the binary no longer |
| 7249 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7250 | } |
| 7251 | #endif |
| 7252 | |
| 7253 | repeat: |
| 7254 | #ifdef SYSV |
| 7255 | do { |
| 7256 | execve(cmd, argv, envp); |
| 7257 | } while (errno == EINTR); |
| 7258 | #else |
| 7259 | execve(cmd, argv, envp); |
| 7260 | #endif |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7261 | if (repeated) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7262 | free(argv); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7263 | return; |
| 7264 | } |
| 7265 | if (errno == ENOEXEC) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7266 | char **ap; |
| 7267 | char **new; |
| 7268 | |
| 7269 | for (ap = argv; *ap; ap++) |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7270 | continue; |
| 7271 | ap = new = ckmalloc((ap - argv + 2) * sizeof(ap[0])); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7272 | ap[1] = cmd; |
Denis Vlasenko | c44ab01 | 2007-04-09 03:11:58 +0000 | [diff] [blame] | 7273 | ap[0] = cmd = (char *)DEFAULT_SHELL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7274 | ap += 2; |
| 7275 | argv++; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7276 | while ((*ap++ = *argv++) != NULL) |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7277 | continue; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7278 | argv = new; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7279 | repeated++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7280 | goto repeat; |
| 7281 | } |
| 7282 | } |
| 7283 | |
| 7284 | /* |
| 7285 | * Exec a program. Never returns. If you change this routine, you may |
| 7286 | * have to change the find_command routine as well. |
| 7287 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7288 | static void shellexec(char **, const char *, int) NORETURN; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7289 | static void |
| 7290 | shellexec(char **argv, const char *path, int idx) |
| 7291 | { |
| 7292 | char *cmdname; |
| 7293 | int e; |
| 7294 | char **envp; |
| 7295 | int exerrno; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7296 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7297 | int applet_no = -1; |
| 7298 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7299 | |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 7300 | clearredir(/*drop:*/ 1); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7301 | envp = listvars(VEXPORT, VUNSET, 0); |
| 7302 | if (strchr(argv[0], '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7303 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7304 | || (applet_no = find_applet_by_name(argv[0])) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7305 | #endif |
| 7306 | ) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7307 | tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7308 | e = errno; |
| 7309 | } else { |
| 7310 | e = ENOENT; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7311 | while ((cmdname = path_advance(&path, argv[0])) != NULL) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7312 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7313 | tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7314 | if (errno != ENOENT && errno != ENOTDIR) |
| 7315 | e = errno; |
| 7316 | } |
| 7317 | stunalloc(cmdname); |
| 7318 | } |
| 7319 | } |
| 7320 | |
| 7321 | /* Map to POSIX errors */ |
| 7322 | switch (e) { |
| 7323 | case EACCES: |
| 7324 | exerrno = 126; |
| 7325 | break; |
| 7326 | case ENOENT: |
| 7327 | exerrno = 127; |
| 7328 | break; |
| 7329 | default: |
| 7330 | exerrno = 2; |
| 7331 | break; |
| 7332 | } |
| 7333 | exitstatus = exerrno; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7334 | TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n", |
| 7335 | argv[0], e, suppress_int)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7336 | ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found")); |
| 7337 | /* NOTREACHED */ |
| 7338 | } |
| 7339 | |
| 7340 | static void |
| 7341 | printentry(struct tblentry *cmdp) |
| 7342 | { |
| 7343 | int idx; |
| 7344 | const char *path; |
| 7345 | char *name; |
| 7346 | |
| 7347 | idx = cmdp->param.index; |
| 7348 | path = pathval(); |
| 7349 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7350 | name = path_advance(&path, cmdp->cmdname); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7351 | stunalloc(name); |
| 7352 | } while (--idx >= 0); |
| 7353 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 7354 | } |
| 7355 | |
| 7356 | /* |
| 7357 | * Clear out command entries. The argument specifies the first entry in |
| 7358 | * PATH which has changed. |
| 7359 | */ |
| 7360 | static void |
| 7361 | clearcmdentry(int firstchange) |
| 7362 | { |
| 7363 | struct tblentry **tblp; |
| 7364 | struct tblentry **pp; |
| 7365 | struct tblentry *cmdp; |
| 7366 | |
| 7367 | INT_OFF; |
| 7368 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 7369 | pp = tblp; |
| 7370 | while ((cmdp = *pp) != NULL) { |
| 7371 | if ((cmdp->cmdtype == CMDNORMAL && |
| 7372 | cmdp->param.index >= firstchange) |
| 7373 | || (cmdp->cmdtype == CMDBUILTIN && |
| 7374 | builtinloc >= firstchange) |
| 7375 | ) { |
| 7376 | *pp = cmdp->next; |
| 7377 | free(cmdp); |
| 7378 | } else { |
| 7379 | pp = &cmdp->next; |
| 7380 | } |
| 7381 | } |
| 7382 | } |
| 7383 | INT_ON; |
| 7384 | } |
| 7385 | |
| 7386 | /* |
| 7387 | * Locate a command in the command hash table. If "add" is nonzero, |
| 7388 | * add the command to the table if it is not already present. The |
| 7389 | * variable "lastcmdentry" is set to point to the address of the link |
| 7390 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 7391 | * entry. |
| 7392 | * |
| 7393 | * Interrupts must be off if called with add != 0. |
| 7394 | */ |
| 7395 | static struct tblentry **lastcmdentry; |
| 7396 | |
| 7397 | static struct tblentry * |
| 7398 | cmdlookup(const char *name, int add) |
| 7399 | { |
| 7400 | unsigned int hashval; |
| 7401 | const char *p; |
| 7402 | struct tblentry *cmdp; |
| 7403 | struct tblentry **pp; |
| 7404 | |
| 7405 | p = name; |
| 7406 | hashval = (unsigned char)*p << 4; |
| 7407 | while (*p) |
| 7408 | hashval += (unsigned char)*p++; |
| 7409 | hashval &= 0x7FFF; |
| 7410 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 7411 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7412 | if (strcmp(cmdp->cmdname, name) == 0) |
| 7413 | break; |
| 7414 | pp = &cmdp->next; |
| 7415 | } |
| 7416 | if (add && cmdp == NULL) { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7417 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) |
| 7418 | + strlen(name) |
| 7419 | /* + 1 - already done because |
| 7420 | * tblentry::cmdname is char[1] */); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7421 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7422 | cmdp->cmdtype = CMDUNKNOWN; |
| 7423 | strcpy(cmdp->cmdname, name); |
| 7424 | } |
| 7425 | lastcmdentry = pp; |
| 7426 | return cmdp; |
| 7427 | } |
| 7428 | |
| 7429 | /* |
| 7430 | * Delete the command entry returned on the last lookup. |
| 7431 | */ |
| 7432 | static void |
| 7433 | delete_cmd_entry(void) |
| 7434 | { |
| 7435 | struct tblentry *cmdp; |
| 7436 | |
| 7437 | INT_OFF; |
| 7438 | cmdp = *lastcmdentry; |
| 7439 | *lastcmdentry = cmdp->next; |
| 7440 | if (cmdp->cmdtype == CMDFUNCTION) |
| 7441 | freefunc(cmdp->param.func); |
| 7442 | free(cmdp); |
| 7443 | INT_ON; |
| 7444 | } |
| 7445 | |
| 7446 | /* |
| 7447 | * Add a new command entry, replacing any existing command entry for |
| 7448 | * the same name - except special builtins. |
| 7449 | */ |
| 7450 | static void |
| 7451 | addcmdentry(char *name, struct cmdentry *entry) |
| 7452 | { |
| 7453 | struct tblentry *cmdp; |
| 7454 | |
| 7455 | cmdp = cmdlookup(name, 1); |
| 7456 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 7457 | freefunc(cmdp->param.func); |
| 7458 | } |
| 7459 | cmdp->cmdtype = entry->cmdtype; |
| 7460 | cmdp->param = entry->u; |
| 7461 | cmdp->rehash = 0; |
| 7462 | } |
| 7463 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7464 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7465 | hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7466 | { |
| 7467 | struct tblentry **pp; |
| 7468 | struct tblentry *cmdp; |
| 7469 | int c; |
| 7470 | struct cmdentry entry; |
| 7471 | char *name; |
| 7472 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7473 | if (nextopt("r") != '\0') { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7474 | clearcmdentry(0); |
| 7475 | return 0; |
| 7476 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7477 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7478 | if (*argptr == NULL) { |
| 7479 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7480 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7481 | if (cmdp->cmdtype == CMDNORMAL) |
| 7482 | printentry(cmdp); |
| 7483 | } |
| 7484 | } |
| 7485 | return 0; |
| 7486 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7487 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7488 | c = 0; |
| 7489 | while ((name = *argptr) != NULL) { |
| 7490 | cmdp = cmdlookup(name, 0); |
| 7491 | if (cmdp != NULL |
| 7492 | && (cmdp->cmdtype == CMDNORMAL |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7493 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)) |
| 7494 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7495 | delete_cmd_entry(); |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7496 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7497 | find_command(name, &entry, DO_ERR, pathval()); |
| 7498 | if (entry.cmdtype == CMDUNKNOWN) |
| 7499 | c = 1; |
| 7500 | argptr++; |
| 7501 | } |
| 7502 | return c; |
| 7503 | } |
| 7504 | |
| 7505 | /* |
| 7506 | * Called when a cd is done. Marks all commands so the next time they |
| 7507 | * are executed they will be rehashed. |
| 7508 | */ |
| 7509 | static void |
| 7510 | hashcd(void) |
| 7511 | { |
| 7512 | struct tblentry **pp; |
| 7513 | struct tblentry *cmdp; |
| 7514 | |
| 7515 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7516 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7517 | if (cmdp->cmdtype == CMDNORMAL |
| 7518 | || (cmdp->cmdtype == CMDBUILTIN |
| 7519 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 7520 | && builtinloc > 0) |
| 7521 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7522 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7523 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7524 | } |
| 7525 | } |
| 7526 | } |
| 7527 | |
| 7528 | /* |
| 7529 | * Fix command hash table when PATH changed. |
| 7530 | * Called before PATH is changed. The argument is the new value of PATH; |
| 7531 | * pathval() still returns the old value at this point. |
| 7532 | * Called with interrupts off. |
| 7533 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7534 | static void FAST_FUNC |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7535 | changepath(const char *new) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7536 | { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7537 | const char *old; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7538 | int firstchange; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7539 | int idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7540 | int idx_bltin; |
| 7541 | |
| 7542 | old = pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7543 | firstchange = 9999; /* assume no change */ |
| 7544 | idx = 0; |
| 7545 | idx_bltin = -1; |
| 7546 | for (;;) { |
| 7547 | if (*old != *new) { |
| 7548 | firstchange = idx; |
| 7549 | if ((*old == '\0' && *new == ':') |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7550 | || (*old == ':' && *new == '\0') |
| 7551 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7552 | firstchange++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7553 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7554 | old = new; /* ignore subsequent differences */ |
| 7555 | } |
| 7556 | if (*new == '\0') |
| 7557 | break; |
| 7558 | if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin")) |
| 7559 | idx_bltin = idx; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7560 | if (*new == ':') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7561 | idx++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7562 | new++; |
| 7563 | old++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7564 | } |
| 7565 | if (builtinloc < 0 && idx_bltin >= 0) |
| 7566 | builtinloc = idx_bltin; /* zap builtins */ |
| 7567 | if (builtinloc >= 0 && idx_bltin < 0) |
| 7568 | firstchange = 0; |
| 7569 | clearcmdentry(firstchange); |
| 7570 | builtinloc = idx_bltin; |
| 7571 | } |
| 7572 | |
| 7573 | #define TEOF 0 |
| 7574 | #define TNL 1 |
| 7575 | #define TREDIR 2 |
| 7576 | #define TWORD 3 |
| 7577 | #define TSEMI 4 |
| 7578 | #define TBACKGND 5 |
| 7579 | #define TAND 6 |
| 7580 | #define TOR 7 |
| 7581 | #define TPIPE 8 |
| 7582 | #define TLP 9 |
| 7583 | #define TRP 10 |
| 7584 | #define TENDCASE 11 |
| 7585 | #define TENDBQUOTE 12 |
| 7586 | #define TNOT 13 |
| 7587 | #define TCASE 14 |
| 7588 | #define TDO 15 |
| 7589 | #define TDONE 16 |
| 7590 | #define TELIF 17 |
| 7591 | #define TELSE 18 |
| 7592 | #define TESAC 19 |
| 7593 | #define TFI 20 |
| 7594 | #define TFOR 21 |
| 7595 | #define TIF 22 |
| 7596 | #define TIN 23 |
| 7597 | #define TTHEN 24 |
| 7598 | #define TUNTIL 25 |
| 7599 | #define TWHILE 26 |
| 7600 | #define TBEGIN 27 |
| 7601 | #define TEND 28 |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7602 | typedef smallint token_id_t; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7603 | |
| 7604 | /* first char is indicating which tokens mark the end of a list */ |
| 7605 | static const char *const tokname_array[] = { |
| 7606 | "\1end of file", |
| 7607 | "\0newline", |
| 7608 | "\0redirection", |
| 7609 | "\0word", |
| 7610 | "\0;", |
| 7611 | "\0&", |
| 7612 | "\0&&", |
| 7613 | "\0||", |
| 7614 | "\0|", |
| 7615 | "\0(", |
| 7616 | "\1)", |
| 7617 | "\1;;", |
| 7618 | "\1`", |
| 7619 | #define KWDOFFSET 13 |
| 7620 | /* the following are keywords */ |
| 7621 | "\0!", |
| 7622 | "\0case", |
| 7623 | "\1do", |
| 7624 | "\1done", |
| 7625 | "\1elif", |
| 7626 | "\1else", |
| 7627 | "\1esac", |
| 7628 | "\1fi", |
| 7629 | "\0for", |
| 7630 | "\0if", |
| 7631 | "\0in", |
| 7632 | "\1then", |
| 7633 | "\0until", |
| 7634 | "\0while", |
| 7635 | "\0{", |
| 7636 | "\1}", |
| 7637 | }; |
| 7638 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7639 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 7640 | static int |
| 7641 | pstrcmp(const void *a, const void *b) |
| 7642 | { |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7643 | return strcmp((char*) a, (*(char**) b) + 1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7644 | } |
| 7645 | |
| 7646 | static const char *const * |
| 7647 | findkwd(const char *s) |
| 7648 | { |
| 7649 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 7650 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 7651 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7652 | } |
| 7653 | |
| 7654 | /* |
| 7655 | * Locate and print what a word is... |
| 7656 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7657 | static int |
| 7658 | describe_command(char *command, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7659 | { |
| 7660 | struct cmdentry entry; |
| 7661 | struct tblentry *cmdp; |
| 7662 | #if ENABLE_ASH_ALIAS |
| 7663 | const struct alias *ap; |
| 7664 | #endif |
| 7665 | const char *path = pathval(); |
| 7666 | |
| 7667 | if (describe_command_verbose) { |
| 7668 | out1str(command); |
| 7669 | } |
| 7670 | |
| 7671 | /* First look at the keywords */ |
| 7672 | if (findkwd(command)) { |
| 7673 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 7674 | goto out; |
| 7675 | } |
| 7676 | |
| 7677 | #if ENABLE_ASH_ALIAS |
| 7678 | /* Then look at the aliases */ |
| 7679 | ap = lookupalias(command, 0); |
| 7680 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7681 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7682 | out1str("alias "); |
| 7683 | printalias(ap); |
| 7684 | return 0; |
| 7685 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7686 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7687 | goto out; |
| 7688 | } |
| 7689 | #endif |
| 7690 | /* Then check if it is a tracked alias */ |
| 7691 | cmdp = cmdlookup(command, 0); |
| 7692 | if (cmdp != NULL) { |
| 7693 | entry.cmdtype = cmdp->cmdtype; |
| 7694 | entry.u = cmdp->param; |
| 7695 | } else { |
| 7696 | /* Finally use brute force */ |
| 7697 | find_command(command, &entry, DO_ABS, path); |
| 7698 | } |
| 7699 | |
| 7700 | switch (entry.cmdtype) { |
| 7701 | case CMDNORMAL: { |
| 7702 | int j = entry.u.index; |
| 7703 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7704 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7705 | p = command; |
| 7706 | } else { |
| 7707 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7708 | p = path_advance(&path, command); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7709 | stunalloc(p); |
| 7710 | } while (--j >= 0); |
| 7711 | } |
| 7712 | if (describe_command_verbose) { |
| 7713 | out1fmt(" is%s %s", |
| 7714 | (cmdp ? " a tracked alias for" : nullstr), p |
| 7715 | ); |
| 7716 | } else { |
| 7717 | out1str(p); |
| 7718 | } |
| 7719 | break; |
| 7720 | } |
| 7721 | |
| 7722 | case CMDFUNCTION: |
| 7723 | if (describe_command_verbose) { |
| 7724 | out1str(" is a shell function"); |
| 7725 | } else { |
| 7726 | out1str(command); |
| 7727 | } |
| 7728 | break; |
| 7729 | |
| 7730 | case CMDBUILTIN: |
| 7731 | if (describe_command_verbose) { |
| 7732 | out1fmt(" is a %sshell builtin", |
| 7733 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 7734 | "special " : nullstr |
| 7735 | ); |
| 7736 | } else { |
| 7737 | out1str(command); |
| 7738 | } |
| 7739 | break; |
| 7740 | |
| 7741 | default: |
| 7742 | if (describe_command_verbose) { |
| 7743 | out1str(": not found\n"); |
| 7744 | } |
| 7745 | return 127; |
| 7746 | } |
| 7747 | out: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 7748 | out1str("\n"); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7749 | return 0; |
| 7750 | } |
| 7751 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7752 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7753 | typecmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7754 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7755 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7756 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7757 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7758 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7759 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 7760 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7761 | i++; |
| 7762 | verbose = 0; |
| 7763 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7764 | while (argv[i]) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7765 | err |= describe_command(argv[i++], verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7766 | } |
| 7767 | return err; |
| 7768 | } |
| 7769 | |
| 7770 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7771 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7772 | commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7773 | { |
| 7774 | int c; |
| 7775 | enum { |
| 7776 | VERIFY_BRIEF = 1, |
| 7777 | VERIFY_VERBOSE = 2, |
| 7778 | } verify = 0; |
| 7779 | |
| 7780 | while ((c = nextopt("pvV")) != '\0') |
| 7781 | if (c == 'V') |
| 7782 | verify |= VERIFY_VERBOSE; |
| 7783 | else if (c == 'v') |
| 7784 | verify |= VERIFY_BRIEF; |
| 7785 | #if DEBUG |
| 7786 | else if (c != 'p') |
| 7787 | abort(); |
| 7788 | #endif |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7789 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
| 7790 | if (verify && (*argptr != NULL)) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7791 | return describe_command(*argptr, verify - VERIFY_BRIEF); |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7792 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7793 | |
| 7794 | return 0; |
| 7795 | } |
| 7796 | #endif |
| 7797 | |
| 7798 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7799 | /* ============ eval.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7800 | |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7801 | static int funcblocksize; /* size of structures in function */ |
| 7802 | static int funcstringsize; /* size of strings in node */ |
| 7803 | static void *funcblock; /* block to allocate function from */ |
| 7804 | static char *funcstring; /* block to allocate strings from */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7805 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7806 | /* flags in argument to evaltree */ |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7807 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 7808 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7809 | #define EV_BACKCMD 04 /* command executing within back quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7810 | |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 7811 | static const uint8_t nodesize[N_NUMBER] = { |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7812 | [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), |
| 7813 | [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), |
| 7814 | [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7815 | [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7816 | [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7817 | [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7818 | [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7819 | [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7820 | [NIF ] = SHELL_ALIGN(sizeof(struct nif)), |
| 7821 | [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7822 | [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7823 | [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)), |
| 7824 | [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)), |
| 7825 | [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)), |
| 7826 | [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7827 | [NARG ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7828 | [NTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7829 | #if ENABLE_ASH_BASH_COMPAT |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7830 | [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7831 | #endif |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7832 | [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7833 | [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7834 | [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7835 | [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7836 | [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7837 | [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7838 | [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7839 | [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7840 | [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7841 | }; |
| 7842 | |
| 7843 | static void calcsize(union node *n); |
| 7844 | |
| 7845 | static void |
| 7846 | sizenodelist(struct nodelist *lp) |
| 7847 | { |
| 7848 | while (lp) { |
| 7849 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
| 7850 | calcsize(lp->n); |
| 7851 | lp = lp->next; |
| 7852 | } |
| 7853 | } |
| 7854 | |
| 7855 | static void |
| 7856 | calcsize(union node *n) |
| 7857 | { |
| 7858 | if (n == NULL) |
| 7859 | return; |
| 7860 | funcblocksize += nodesize[n->type]; |
| 7861 | switch (n->type) { |
| 7862 | case NCMD: |
| 7863 | calcsize(n->ncmd.redirect); |
| 7864 | calcsize(n->ncmd.args); |
| 7865 | calcsize(n->ncmd.assign); |
| 7866 | break; |
| 7867 | case NPIPE: |
| 7868 | sizenodelist(n->npipe.cmdlist); |
| 7869 | break; |
| 7870 | case NREDIR: |
| 7871 | case NBACKGND: |
| 7872 | case NSUBSHELL: |
| 7873 | calcsize(n->nredir.redirect); |
| 7874 | calcsize(n->nredir.n); |
| 7875 | break; |
| 7876 | case NAND: |
| 7877 | case NOR: |
| 7878 | case NSEMI: |
| 7879 | case NWHILE: |
| 7880 | case NUNTIL: |
| 7881 | calcsize(n->nbinary.ch2); |
| 7882 | calcsize(n->nbinary.ch1); |
| 7883 | break; |
| 7884 | case NIF: |
| 7885 | calcsize(n->nif.elsepart); |
| 7886 | calcsize(n->nif.ifpart); |
| 7887 | calcsize(n->nif.test); |
| 7888 | break; |
| 7889 | case NFOR: |
| 7890 | funcstringsize += strlen(n->nfor.var) + 1; |
| 7891 | calcsize(n->nfor.body); |
| 7892 | calcsize(n->nfor.args); |
| 7893 | break; |
| 7894 | case NCASE: |
| 7895 | calcsize(n->ncase.cases); |
| 7896 | calcsize(n->ncase.expr); |
| 7897 | break; |
| 7898 | case NCLIST: |
| 7899 | calcsize(n->nclist.body); |
| 7900 | calcsize(n->nclist.pattern); |
| 7901 | calcsize(n->nclist.next); |
| 7902 | break; |
| 7903 | case NDEFUN: |
| 7904 | case NARG: |
| 7905 | sizenodelist(n->narg.backquote); |
| 7906 | funcstringsize += strlen(n->narg.text) + 1; |
| 7907 | calcsize(n->narg.next); |
| 7908 | break; |
| 7909 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 7910 | #if ENABLE_ASH_BASH_COMPAT |
| 7911 | case NTO2: |
| 7912 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7913 | case NCLOBBER: |
| 7914 | case NFROM: |
| 7915 | case NFROMTO: |
| 7916 | case NAPPEND: |
| 7917 | calcsize(n->nfile.fname); |
| 7918 | calcsize(n->nfile.next); |
| 7919 | break; |
| 7920 | case NTOFD: |
| 7921 | case NFROMFD: |
| 7922 | calcsize(n->ndup.vname); |
| 7923 | calcsize(n->ndup.next); |
| 7924 | break; |
| 7925 | case NHERE: |
| 7926 | case NXHERE: |
| 7927 | calcsize(n->nhere.doc); |
| 7928 | calcsize(n->nhere.next); |
| 7929 | break; |
| 7930 | case NNOT: |
| 7931 | calcsize(n->nnot.com); |
| 7932 | break; |
| 7933 | }; |
| 7934 | } |
| 7935 | |
| 7936 | static char * |
| 7937 | nodeckstrdup(char *s) |
| 7938 | { |
| 7939 | char *rtn = funcstring; |
| 7940 | |
| 7941 | strcpy(funcstring, s); |
| 7942 | funcstring += strlen(s) + 1; |
| 7943 | return rtn; |
| 7944 | } |
| 7945 | |
| 7946 | static union node *copynode(union node *); |
| 7947 | |
| 7948 | static struct nodelist * |
| 7949 | copynodelist(struct nodelist *lp) |
| 7950 | { |
| 7951 | struct nodelist *start; |
| 7952 | struct nodelist **lpp; |
| 7953 | |
| 7954 | lpp = &start; |
| 7955 | while (lp) { |
| 7956 | *lpp = funcblock; |
| 7957 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 7958 | (*lpp)->n = copynode(lp->n); |
| 7959 | lp = lp->next; |
| 7960 | lpp = &(*lpp)->next; |
| 7961 | } |
| 7962 | *lpp = NULL; |
| 7963 | return start; |
| 7964 | } |
| 7965 | |
| 7966 | static union node * |
| 7967 | copynode(union node *n) |
| 7968 | { |
| 7969 | union node *new; |
| 7970 | |
| 7971 | if (n == NULL) |
| 7972 | return NULL; |
| 7973 | new = funcblock; |
| 7974 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 7975 | |
| 7976 | switch (n->type) { |
| 7977 | case NCMD: |
| 7978 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 7979 | new->ncmd.args = copynode(n->ncmd.args); |
| 7980 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 7981 | break; |
| 7982 | case NPIPE: |
| 7983 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7984 | new->npipe.pipe_backgnd = n->npipe.pipe_backgnd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7985 | break; |
| 7986 | case NREDIR: |
| 7987 | case NBACKGND: |
| 7988 | case NSUBSHELL: |
| 7989 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 7990 | new->nredir.n = copynode(n->nredir.n); |
| 7991 | break; |
| 7992 | case NAND: |
| 7993 | case NOR: |
| 7994 | case NSEMI: |
| 7995 | case NWHILE: |
| 7996 | case NUNTIL: |
| 7997 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 7998 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 7999 | break; |
| 8000 | case NIF: |
| 8001 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 8002 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 8003 | new->nif.test = copynode(n->nif.test); |
| 8004 | break; |
| 8005 | case NFOR: |
| 8006 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 8007 | new->nfor.body = copynode(n->nfor.body); |
| 8008 | new->nfor.args = copynode(n->nfor.args); |
| 8009 | break; |
| 8010 | case NCASE: |
| 8011 | new->ncase.cases = copynode(n->ncase.cases); |
| 8012 | new->ncase.expr = copynode(n->ncase.expr); |
| 8013 | break; |
| 8014 | case NCLIST: |
| 8015 | new->nclist.body = copynode(n->nclist.body); |
| 8016 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 8017 | new->nclist.next = copynode(n->nclist.next); |
| 8018 | break; |
| 8019 | case NDEFUN: |
| 8020 | case NARG: |
| 8021 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 8022 | new->narg.text = nodeckstrdup(n->narg.text); |
| 8023 | new->narg.next = copynode(n->narg.next); |
| 8024 | break; |
| 8025 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8026 | #if ENABLE_ASH_BASH_COMPAT |
| 8027 | case NTO2: |
| 8028 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8029 | case NCLOBBER: |
| 8030 | case NFROM: |
| 8031 | case NFROMTO: |
| 8032 | case NAPPEND: |
| 8033 | new->nfile.fname = copynode(n->nfile.fname); |
| 8034 | new->nfile.fd = n->nfile.fd; |
| 8035 | new->nfile.next = copynode(n->nfile.next); |
| 8036 | break; |
| 8037 | case NTOFD: |
| 8038 | case NFROMFD: |
| 8039 | new->ndup.vname = copynode(n->ndup.vname); |
| 8040 | new->ndup.dupfd = n->ndup.dupfd; |
| 8041 | new->ndup.fd = n->ndup.fd; |
| 8042 | new->ndup.next = copynode(n->ndup.next); |
| 8043 | break; |
| 8044 | case NHERE: |
| 8045 | case NXHERE: |
| 8046 | new->nhere.doc = copynode(n->nhere.doc); |
| 8047 | new->nhere.fd = n->nhere.fd; |
| 8048 | new->nhere.next = copynode(n->nhere.next); |
| 8049 | break; |
| 8050 | case NNOT: |
| 8051 | new->nnot.com = copynode(n->nnot.com); |
| 8052 | break; |
| 8053 | }; |
| 8054 | new->type = n->type; |
| 8055 | return new; |
| 8056 | } |
| 8057 | |
| 8058 | /* |
| 8059 | * Make a copy of a parse tree. |
| 8060 | */ |
| 8061 | static struct funcnode * |
| 8062 | copyfunc(union node *n) |
| 8063 | { |
| 8064 | struct funcnode *f; |
| 8065 | size_t blocksize; |
| 8066 | |
| 8067 | funcblocksize = offsetof(struct funcnode, n); |
| 8068 | funcstringsize = 0; |
| 8069 | calcsize(n); |
| 8070 | blocksize = funcblocksize; |
| 8071 | f = ckmalloc(blocksize + funcstringsize); |
| 8072 | funcblock = (char *) f + offsetof(struct funcnode, n); |
| 8073 | funcstring = (char *) f + blocksize; |
| 8074 | copynode(n); |
| 8075 | f->count = 0; |
| 8076 | return f; |
| 8077 | } |
| 8078 | |
| 8079 | /* |
| 8080 | * Define a shell function. |
| 8081 | */ |
| 8082 | static void |
| 8083 | defun(char *name, union node *func) |
| 8084 | { |
| 8085 | struct cmdentry entry; |
| 8086 | |
| 8087 | INT_OFF; |
| 8088 | entry.cmdtype = CMDFUNCTION; |
| 8089 | entry.u.func = copyfunc(func); |
| 8090 | addcmdentry(name, &entry); |
| 8091 | INT_ON; |
| 8092 | } |
| 8093 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8094 | /* Reasons for skipping commands (see comment on breakcmd routine) */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8095 | #define SKIPBREAK (1 << 0) |
| 8096 | #define SKIPCONT (1 << 1) |
| 8097 | #define SKIPFUNC (1 << 2) |
| 8098 | #define SKIPFILE (1 << 3) |
| 8099 | #define SKIPEVAL (1 << 4) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8100 | static smallint evalskip; /* set to SKIPxxx if we are skipping commands */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8101 | static int skipcount; /* number of levels to skip */ |
| 8102 | static int funcnest; /* depth of function calls */ |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 8103 | static int loopnest; /* current loop nesting level */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8104 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8105 | /* Forward decl way out to parsing code - dotrap needs it */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8106 | static int evalstring(char *s, int mask); |
| 8107 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8108 | /* Called to execute a trap. |
| 8109 | * Single callsite - at the end of evaltree(). |
| 8110 | * If we return non-zero, exaltree raises EXEXIT exception. |
| 8111 | * |
| 8112 | * Perhaps we should avoid entering new trap handlers |
| 8113 | * while we are executing a trap handler. [is it a TODO?] |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8114 | */ |
| 8115 | static int |
| 8116 | dotrap(void) |
| 8117 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8118 | uint8_t *g; |
| 8119 | int sig; |
| 8120 | uint8_t savestatus; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8121 | |
| 8122 | savestatus = exitstatus; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8123 | pending_sig = 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8124 | xbarrier(); |
| 8125 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8126 | TRACE(("dotrap entered\n")); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8127 | for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) { |
| 8128 | int want_exexit; |
| 8129 | char *t; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8130 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8131 | if (*g == 0) |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8132 | continue; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8133 | t = trap[sig]; |
| 8134 | /* non-trapped SIGINT is handled separately by raise_interrupt, |
| 8135 | * don't upset it by resetting gotsig[SIGINT-1] */ |
| 8136 | if (sig == SIGINT && !t) |
| 8137 | continue; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8138 | |
| 8139 | TRACE(("sig %d is active, will run handler '%s'\n", sig, t)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8140 | *g = 0; |
| 8141 | if (!t) |
| 8142 | continue; |
| 8143 | want_exexit = evalstring(t, SKIPEVAL); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8144 | exitstatus = savestatus; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8145 | if (want_exexit) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8146 | TRACE(("dotrap returns %d\n", want_exexit)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8147 | return want_exexit; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8148 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8149 | } |
| 8150 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8151 | TRACE(("dotrap returns 0\n")); |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8152 | return 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8153 | } |
| 8154 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8155 | /* forward declarations - evaluation is fairly recursive business... */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8156 | static void evalloop(union node *, int); |
| 8157 | static void evalfor(union node *, int); |
| 8158 | static void evalcase(union node *, int); |
| 8159 | static void evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8160 | static void expredir(union node *); |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8161 | static void evalpipe(union node *, int); |
| 8162 | static void evalcommand(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8163 | static int evalbltin(const struct builtincmd *, int, char **); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8164 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8165 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8166 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8167 | * Evaluate a parse tree. The value is left in the global variable |
| 8168 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8169 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8170 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8171 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8172 | { |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8173 | struct jmploc *volatile savehandler = exception_handler; |
| 8174 | struct jmploc jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8175 | int checkexit = 0; |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8176 | void (*evalfn)(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8177 | int status; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8178 | int int_level; |
| 8179 | |
| 8180 | SAVE_INT(int_level); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8181 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8182 | if (n == NULL) { |
| 8183 | TRACE(("evaltree(NULL) called\n")); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8184 | goto out1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8185 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8186 | TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8187 | |
| 8188 | exception_handler = &jmploc; |
| 8189 | { |
| 8190 | int err = setjmp(jmploc.loc); |
| 8191 | if (err) { |
| 8192 | /* if it was a signal, check for trap handlers */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8193 | if (exception_type == EXSIG) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8194 | TRACE(("exception %d (EXSIG) in evaltree, err=%d\n", |
| 8195 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8196 | goto out; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8197 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8198 | /* continue on the way out */ |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8199 | TRACE(("exception %d in evaltree, propagating err=%d\n", |
| 8200 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8201 | exception_handler = savehandler; |
| 8202 | longjmp(exception_handler->loc, err); |
| 8203 | } |
| 8204 | } |
| 8205 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8206 | switch (n->type) { |
| 8207 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 8208 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8209 | out1fmt("Node type = %d\n", n->type); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8210 | fflush_all(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8211 | break; |
| 8212 | #endif |
| 8213 | case NNOT: |
| 8214 | evaltree(n->nnot.com, EV_TESTED); |
| 8215 | status = !exitstatus; |
| 8216 | goto setstatus; |
| 8217 | case NREDIR: |
| 8218 | expredir(n->nredir.redirect); |
| 8219 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 8220 | if (!status) { |
| 8221 | evaltree(n->nredir.n, flags & EV_TESTED); |
| 8222 | status = exitstatus; |
| 8223 | } |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 8224 | popredir(/*drop:*/ 0, /*restore:*/ 0 /* not sure */); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8225 | goto setstatus; |
| 8226 | case NCMD: |
| 8227 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8228 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8229 | if (eflag && !(flags & EV_TESTED)) |
| 8230 | checkexit = ~0; |
| 8231 | goto calleval; |
| 8232 | case NFOR: |
| 8233 | evalfn = evalfor; |
| 8234 | goto calleval; |
| 8235 | case NWHILE: |
| 8236 | case NUNTIL: |
| 8237 | evalfn = evalloop; |
| 8238 | goto calleval; |
| 8239 | case NSUBSHELL: |
| 8240 | case NBACKGND: |
| 8241 | evalfn = evalsubshell; |
| 8242 | goto calleval; |
| 8243 | case NPIPE: |
| 8244 | evalfn = evalpipe; |
| 8245 | goto checkexit; |
| 8246 | case NCASE: |
| 8247 | evalfn = evalcase; |
| 8248 | goto calleval; |
| 8249 | case NAND: |
| 8250 | case NOR: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8251 | case NSEMI: { |
| 8252 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8253 | #if NAND + 1 != NOR |
| 8254 | #error NAND + 1 != NOR |
| 8255 | #endif |
| 8256 | #if NOR + 1 != NSEMI |
| 8257 | #error NOR + 1 != NSEMI |
| 8258 | #endif |
Denis Vlasenko | 87d5fd9 | 2008-07-26 13:48:35 +0000 | [diff] [blame] | 8259 | unsigned is_or = n->type - NAND; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8260 | evaltree( |
| 8261 | n->nbinary.ch1, |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8262 | (flags | ((is_or >> 1) - 1)) & EV_TESTED |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8263 | ); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8264 | if (!exitstatus == is_or) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8265 | break; |
| 8266 | if (!evalskip) { |
| 8267 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8268 | evaln: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8269 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8270 | calleval: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8271 | evalfn(n, flags); |
| 8272 | break; |
| 8273 | } |
| 8274 | break; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8275 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8276 | case NIF: |
| 8277 | evaltree(n->nif.test, EV_TESTED); |
| 8278 | if (evalskip) |
| 8279 | break; |
| 8280 | if (exitstatus == 0) { |
| 8281 | n = n->nif.ifpart; |
| 8282 | goto evaln; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8283 | } |
| 8284 | if (n->nif.elsepart) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8285 | n = n->nif.elsepart; |
| 8286 | goto evaln; |
| 8287 | } |
| 8288 | goto success; |
| 8289 | case NDEFUN: |
| 8290 | defun(n->narg.text, n->narg.next); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8291 | success: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8292 | status = 0; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8293 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8294 | exitstatus = status; |
| 8295 | break; |
| 8296 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8297 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8298 | out: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8299 | exception_handler = savehandler; |
| 8300 | out1: |
| 8301 | if (checkexit & exitstatus) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8302 | evalskip |= SKIPEVAL; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8303 | else if (pending_sig && dotrap()) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8304 | goto exexit; |
| 8305 | |
| 8306 | if (flags & EV_EXIT) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8307 | exexit: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8308 | raise_exception(EXEXIT); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8309 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8310 | |
| 8311 | RESTORE_INT(int_level); |
| 8312 | TRACE(("leaving evaltree (no interrupts)\n")); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8313 | } |
| 8314 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8315 | #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3) |
| 8316 | static |
| 8317 | #endif |
| 8318 | void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__)); |
| 8319 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8320 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8321 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8322 | { |
| 8323 | int status; |
| 8324 | |
| 8325 | loopnest++; |
| 8326 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8327 | flags &= EV_TESTED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8328 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8329 | int i; |
| 8330 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8331 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 8332 | if (evalskip) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8333 | skipping: |
| 8334 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8335 | evalskip = 0; |
| 8336 | continue; |
| 8337 | } |
| 8338 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8339 | evalskip = 0; |
| 8340 | break; |
| 8341 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8342 | i = exitstatus; |
| 8343 | if (n->type != NWHILE) |
| 8344 | i = !i; |
| 8345 | if (i != 0) |
| 8346 | break; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8347 | evaltree(n->nbinary.ch2, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8348 | status = exitstatus; |
| 8349 | if (evalskip) |
| 8350 | goto skipping; |
| 8351 | } |
| 8352 | loopnest--; |
| 8353 | exitstatus = status; |
| 8354 | } |
| 8355 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8356 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8357 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8358 | { |
| 8359 | struct arglist arglist; |
| 8360 | union node *argp; |
| 8361 | struct strlist *sp; |
| 8362 | struct stackmark smark; |
| 8363 | |
| 8364 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8365 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8366 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8367 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8368 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8369 | /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8370 | if (evalskip) |
| 8371 | goto out; |
| 8372 | } |
| 8373 | *arglist.lastp = NULL; |
| 8374 | |
| 8375 | exitstatus = 0; |
| 8376 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8377 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8378 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8379 | setvar(n->nfor.var, sp->text, 0); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8380 | evaltree(n->nfor.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8381 | if (evalskip) { |
| 8382 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 8383 | evalskip = 0; |
| 8384 | continue; |
| 8385 | } |
| 8386 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8387 | evalskip = 0; |
| 8388 | break; |
| 8389 | } |
| 8390 | } |
| 8391 | loopnest--; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8392 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8393 | popstackmark(&smark); |
| 8394 | } |
| 8395 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8396 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8397 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8398 | { |
| 8399 | union node *cp; |
| 8400 | union node *patp; |
| 8401 | struct arglist arglist; |
| 8402 | struct stackmark smark; |
| 8403 | |
| 8404 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8405 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8406 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8407 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8408 | exitstatus = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8409 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 8410 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8411 | if (casematch(patp, arglist.list->text)) { |
| 8412 | if (evalskip == 0) { |
| 8413 | evaltree(cp->nclist.body, flags); |
| 8414 | } |
| 8415 | goto out; |
| 8416 | } |
| 8417 | } |
| 8418 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8419 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8420 | popstackmark(&smark); |
| 8421 | } |
| 8422 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8423 | /* |
| 8424 | * Kick off a subshell to evaluate a tree. |
| 8425 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8426 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8427 | evalsubshell(union node *n, int flags) |
| 8428 | { |
| 8429 | struct job *jp; |
| 8430 | int backgnd = (n->type == NBACKGND); |
| 8431 | int status; |
| 8432 | |
| 8433 | expredir(n->nredir.redirect); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8434 | if (!backgnd && (flags & EV_EXIT) && !may_have_traps) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8435 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8436 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8437 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8438 | if (forkshell(jp, n, backgnd) == 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8439 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8440 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8441 | flags |= EV_EXIT; |
| 8442 | if (backgnd) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8443 | flags &= ~EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 8444 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8445 | redirect(n->nredir.redirect, 0); |
| 8446 | evaltreenr(n->nredir.n, flags); |
| 8447 | /* never returns */ |
| 8448 | } |
| 8449 | status = 0; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8450 | if (!backgnd) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8451 | status = waitforjob(jp); |
| 8452 | exitstatus = status; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8453 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8454 | } |
| 8455 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8456 | /* |
| 8457 | * Compute the names of the files in a redirection list. |
| 8458 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 8459 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8460 | static void |
| 8461 | expredir(union node *n) |
| 8462 | { |
| 8463 | union node *redir; |
| 8464 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8465 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8466 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8467 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8468 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8469 | fn.lastp = &fn.list; |
| 8470 | switch (redir->type) { |
| 8471 | case NFROMTO: |
| 8472 | case NFROM: |
| 8473 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8474 | #if ENABLE_ASH_BASH_COMPAT |
| 8475 | case NTO2: |
| 8476 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8477 | case NCLOBBER: |
| 8478 | case NAPPEND: |
| 8479 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8480 | #if ENABLE_ASH_BASH_COMPAT |
| 8481 | store_expfname: |
| 8482 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8483 | redir->nfile.expfname = fn.list->text; |
| 8484 | break; |
| 8485 | case NFROMFD: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8486 | case NTOFD: /* >& */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8487 | if (redir->ndup.vname) { |
| 8488 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8489 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8490 | ash_msg_and_raise_error("redir error"); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8491 | #if ENABLE_ASH_BASH_COMPAT |
| 8492 | //FIXME: we used expandarg with different args! |
| 8493 | if (!isdigit_str9(fn.list->text)) { |
| 8494 | /* >&file, not >&fd */ |
| 8495 | if (redir->nfile.fd != 1) /* 123>&file - BAD */ |
| 8496 | ash_msg_and_raise_error("redir error"); |
| 8497 | redir->type = NTO2; |
| 8498 | goto store_expfname; |
| 8499 | } |
| 8500 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8501 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8502 | } |
| 8503 | break; |
| 8504 | } |
| 8505 | } |
| 8506 | } |
| 8507 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8508 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8509 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 8510 | * of the process creating the pipeline. (This differs from some versions |
| 8511 | * of the shell, which make the last process in a pipeline the parent |
| 8512 | * of all the rest.) |
| 8513 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8514 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8515 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8516 | { |
| 8517 | struct job *jp; |
| 8518 | struct nodelist *lp; |
| 8519 | int pipelen; |
| 8520 | int prevfd; |
| 8521 | int pip[2]; |
| 8522 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8523 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8524 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8525 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8526 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8527 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8528 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8529 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8530 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8531 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8532 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8533 | pip[1] = -1; |
| 8534 | if (lp->next) { |
| 8535 | if (pipe(pip) < 0) { |
| 8536 | close(prevfd); |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 8537 | ash_msg_and_raise_error("pipe call failed"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8538 | } |
| 8539 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8540 | if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8541 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8542 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8543 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8544 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8545 | if (prevfd > 0) { |
| 8546 | dup2(prevfd, 0); |
| 8547 | close(prevfd); |
| 8548 | } |
| 8549 | if (pip[1] > 1) { |
| 8550 | dup2(pip[1], 1); |
| 8551 | close(pip[1]); |
| 8552 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8553 | evaltreenr(lp->n, flags); |
| 8554 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8555 | } |
| 8556 | if (prevfd >= 0) |
| 8557 | close(prevfd); |
| 8558 | prevfd = pip[0]; |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 8559 | /* Don't want to trigger debugging */ |
| 8560 | if (pip[1] != -1) |
| 8561 | close(pip[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8562 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8563 | if (n->npipe.pipe_backgnd == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8564 | exitstatus = waitforjob(jp); |
| 8565 | TRACE(("evalpipe: job done exit status %d\n", exitstatus)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8566 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8567 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8568 | } |
| 8569 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8570 | /* |
| 8571 | * Controls whether the shell is interactive or not. |
| 8572 | */ |
| 8573 | static void |
| 8574 | setinteractive(int on) |
| 8575 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8576 | static smallint is_interactive; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8577 | |
| 8578 | if (++on == is_interactive) |
| 8579 | return; |
| 8580 | is_interactive = on; |
| 8581 | setsignal(SIGINT); |
| 8582 | setsignal(SIGQUIT); |
| 8583 | setsignal(SIGTERM); |
| 8584 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8585 | if (is_interactive > 1) { |
| 8586 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8587 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8588 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8589 | if (!did_banner) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8590 | /* note: ash and hush share this string */ |
| 8591 | out1fmt("\n\n%s %s\n" |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8592 | "Enter 'help' for a list of built-in commands." |
| 8593 | "\n\n", |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8594 | bb_banner, |
| 8595 | "built-in shell (ash)" |
| 8596 | ); |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8597 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8598 | } |
| 8599 | } |
| 8600 | #endif |
| 8601 | } |
| 8602 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8603 | static void |
| 8604 | optschanged(void) |
| 8605 | { |
| 8606 | #if DEBUG |
| 8607 | opentrace(); |
| 8608 | #endif |
| 8609 | setinteractive(iflag); |
| 8610 | setjobctl(mflag); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8611 | #if ENABLE_FEATURE_EDITING_VI |
| 8612 | if (viflag) |
| 8613 | line_input_state->flags |= VI_MODE; |
| 8614 | else |
| 8615 | line_input_state->flags &= ~VI_MODE; |
| 8616 | #else |
| 8617 | viflag = 0; /* forcibly keep the option off */ |
| 8618 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8619 | } |
| 8620 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8621 | static struct localvar *localvars; |
| 8622 | |
| 8623 | /* |
| 8624 | * Called after a function returns. |
| 8625 | * Interrupts must be off. |
| 8626 | */ |
| 8627 | static void |
| 8628 | poplocalvars(void) |
| 8629 | { |
| 8630 | struct localvar *lvp; |
| 8631 | struct var *vp; |
| 8632 | |
| 8633 | while ((lvp = localvars) != NULL) { |
| 8634 | localvars = lvp->next; |
| 8635 | vp = lvp->vp; |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 8636 | TRACE(("poplocalvar %s\n", vp ? vp->text : "-")); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8637 | if (vp == NULL) { /* $- saved */ |
| 8638 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 8639 | free((char*)lvp->text); |
| 8640 | optschanged(); |
| 8641 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
| 8642 | unsetvar(vp->text); |
| 8643 | } else { |
| 8644 | if (vp->func) |
| 8645 | (*vp->func)(strchrnul(lvp->text, '=') + 1); |
| 8646 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 8647 | free((char*)vp->text); |
| 8648 | vp->flags = lvp->flags; |
| 8649 | vp->text = lvp->text; |
| 8650 | } |
| 8651 | free(lvp); |
| 8652 | } |
| 8653 | } |
| 8654 | |
| 8655 | static int |
| 8656 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 8657 | { |
| 8658 | volatile struct shparam saveparam; |
| 8659 | struct localvar *volatile savelocalvars; |
| 8660 | struct jmploc *volatile savehandler; |
| 8661 | struct jmploc jmploc; |
| 8662 | int e; |
| 8663 | |
| 8664 | saveparam = shellparam; |
| 8665 | savelocalvars = localvars; |
| 8666 | e = setjmp(jmploc.loc); |
| 8667 | if (e) { |
| 8668 | goto funcdone; |
| 8669 | } |
| 8670 | INT_OFF; |
| 8671 | savehandler = exception_handler; |
| 8672 | exception_handler = &jmploc; |
| 8673 | localvars = NULL; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8674 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8675 | func->count++; |
| 8676 | funcnest++; |
| 8677 | INT_ON; |
| 8678 | shellparam.nparam = argc - 1; |
| 8679 | shellparam.p = argv + 1; |
| 8680 | #if ENABLE_ASH_GETOPTS |
| 8681 | shellparam.optind = 1; |
| 8682 | shellparam.optoff = -1; |
| 8683 | #endif |
| 8684 | evaltree(&func->n, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8685 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8686 | INT_OFF; |
| 8687 | funcnest--; |
| 8688 | freefunc(func); |
| 8689 | poplocalvars(); |
| 8690 | localvars = savelocalvars; |
| 8691 | freeparam(&shellparam); |
| 8692 | shellparam = saveparam; |
| 8693 | exception_handler = savehandler; |
| 8694 | INT_ON; |
| 8695 | evalskip &= ~SKIPFUNC; |
| 8696 | return e; |
| 8697 | } |
| 8698 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8699 | #if ENABLE_ASH_CMDCMD |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8700 | static char ** |
| 8701 | parse_command_args(char **argv, const char **path) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8702 | { |
| 8703 | char *cp, c; |
| 8704 | |
| 8705 | for (;;) { |
| 8706 | cp = *++argv; |
| 8707 | if (!cp) |
| 8708 | return 0; |
| 8709 | if (*cp++ != '-') |
| 8710 | break; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8711 | c = *cp++; |
| 8712 | if (!c) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8713 | break; |
| 8714 | if (c == '-' && !*cp) { |
| 8715 | argv++; |
| 8716 | break; |
| 8717 | } |
| 8718 | do { |
| 8719 | switch (c) { |
| 8720 | case 'p': |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 8721 | *path = bb_default_path; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8722 | break; |
| 8723 | default: |
| 8724 | /* run 'typecmd' for other options */ |
| 8725 | return 0; |
| 8726 | } |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 8727 | c = *cp++; |
| 8728 | } while (c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8729 | } |
| 8730 | return argv; |
| 8731 | } |
| 8732 | #endif |
| 8733 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8734 | /* |
| 8735 | * Make a variable a local variable. When a variable is made local, it's |
| 8736 | * value and flags are saved in a localvar structure. The saved values |
| 8737 | * will be restored when the shell function returns. We handle the name |
| 8738 | * "-" as a special case. |
| 8739 | */ |
| 8740 | static void |
| 8741 | mklocal(char *name) |
| 8742 | { |
| 8743 | struct localvar *lvp; |
| 8744 | struct var **vpp; |
| 8745 | struct var *vp; |
| 8746 | |
| 8747 | INT_OFF; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 8748 | lvp = ckzalloc(sizeof(struct localvar)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8749 | if (LONE_DASH(name)) { |
| 8750 | char *p; |
| 8751 | p = ckmalloc(sizeof(optlist)); |
| 8752 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 8753 | vp = NULL; |
| 8754 | } else { |
| 8755 | char *eq; |
| 8756 | |
| 8757 | vpp = hashvar(name); |
| 8758 | vp = *findvar(vpp, name); |
| 8759 | eq = strchr(name, '='); |
| 8760 | if (vp == NULL) { |
| 8761 | if (eq) |
| 8762 | setvareq(name, VSTRFIXED); |
| 8763 | else |
| 8764 | setvar(name, NULL, VSTRFIXED); |
| 8765 | vp = *vpp; /* the new variable */ |
| 8766 | lvp->flags = VUNSET; |
| 8767 | } else { |
| 8768 | lvp->text = vp->text; |
| 8769 | lvp->flags = vp->flags; |
| 8770 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 8771 | if (eq) |
| 8772 | setvareq(name, 0); |
| 8773 | } |
| 8774 | } |
| 8775 | lvp->vp = vp; |
| 8776 | lvp->next = localvars; |
| 8777 | localvars = lvp; |
| 8778 | INT_ON; |
| 8779 | } |
| 8780 | |
| 8781 | /* |
| 8782 | * The "local" command. |
| 8783 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8784 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8785 | localcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8786 | { |
| 8787 | char *name; |
| 8788 | |
| 8789 | argv = argptr; |
| 8790 | while ((name = *argv++) != NULL) { |
| 8791 | mklocal(name); |
| 8792 | } |
| 8793 | return 0; |
| 8794 | } |
| 8795 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8796 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8797 | falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8798 | { |
| 8799 | return 1; |
| 8800 | } |
| 8801 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8802 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8803 | truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8804 | { |
| 8805 | return 0; |
| 8806 | } |
| 8807 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8808 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8809 | execcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8810 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8811 | if (argv[1]) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8812 | iflag = 0; /* exit on error */ |
| 8813 | mflag = 0; |
| 8814 | optschanged(); |
| 8815 | shellexec(argv + 1, pathval(), 0); |
| 8816 | } |
| 8817 | return 0; |
| 8818 | } |
| 8819 | |
| 8820 | /* |
| 8821 | * The return command. |
| 8822 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8823 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8824 | returncmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8825 | { |
| 8826 | /* |
| 8827 | * If called outside a function, do what ksh does; |
| 8828 | * skip the rest of the file. |
| 8829 | */ |
| 8830 | evalskip = funcnest ? SKIPFUNC : SKIPFILE; |
| 8831 | return argv[1] ? number(argv[1]) : exitstatus; |
| 8832 | } |
| 8833 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8834 | /* Forward declarations for builtintab[] */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8835 | static int breakcmd(int, char **) FAST_FUNC; |
| 8836 | static int dotcmd(int, char **) FAST_FUNC; |
| 8837 | static int evalcmd(int, char **) FAST_FUNC; |
| 8838 | static int exitcmd(int, char **) FAST_FUNC; |
| 8839 | static int exportcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8840 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8841 | static int getoptscmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8842 | #endif |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8843 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8844 | static int helpcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8845 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 8846 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8847 | static int letcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8848 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8849 | static int readcmd(int, char **) FAST_FUNC; |
| 8850 | static int setcmd(int, char **) FAST_FUNC; |
| 8851 | static int shiftcmd(int, char **) FAST_FUNC; |
| 8852 | static int timescmd(int, char **) FAST_FUNC; |
| 8853 | static int trapcmd(int, char **) FAST_FUNC; |
| 8854 | static int umaskcmd(int, char **) FAST_FUNC; |
| 8855 | static int unsetcmd(int, char **) FAST_FUNC; |
| 8856 | static int ulimitcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8857 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8858 | #define BUILTIN_NOSPEC "0" |
| 8859 | #define BUILTIN_SPECIAL "1" |
| 8860 | #define BUILTIN_REGULAR "2" |
| 8861 | #define BUILTIN_SPEC_REG "3" |
| 8862 | #define BUILTIN_ASSIGN "4" |
| 8863 | #define BUILTIN_SPEC_ASSG "5" |
| 8864 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8865 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 8866 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8867 | /* Stubs for calling non-FAST_FUNC's */ |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8868 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8869 | 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] | 8870 | #endif |
| 8871 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8872 | 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] | 8873 | #endif |
| 8874 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8875 | 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] | 8876 | #endif |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 8877 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 8878 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8879 | static const struct builtincmd builtintab[] = { |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8880 | { BUILTIN_SPEC_REG "." , dotcmd }, |
| 8881 | { BUILTIN_SPEC_REG ":" , truecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8882 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8883 | { BUILTIN_REGULAR "[" , testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8884 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8885 | { BUILTIN_REGULAR "[[" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8886 | #endif |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8887 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8888 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8889 | { BUILTIN_REG_ASSG "alias" , aliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8890 | #endif |
| 8891 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8892 | { BUILTIN_REGULAR "bg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8893 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8894 | { BUILTIN_SPEC_REG "break" , breakcmd }, |
| 8895 | { BUILTIN_REGULAR "cd" , cdcmd }, |
| 8896 | { BUILTIN_NOSPEC "chdir" , cdcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8897 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8898 | { BUILTIN_REGULAR "command" , commandcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8899 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8900 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8901 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8902 | { BUILTIN_REGULAR "echo" , echocmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8903 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8904 | { BUILTIN_SPEC_REG "eval" , evalcmd }, |
| 8905 | { BUILTIN_SPEC_REG "exec" , execcmd }, |
| 8906 | { BUILTIN_SPEC_REG "exit" , exitcmd }, |
| 8907 | { BUILTIN_SPEC_REG_ASSG "export" , exportcmd }, |
| 8908 | { BUILTIN_REGULAR "false" , falsecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8909 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8910 | { BUILTIN_REGULAR "fg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8911 | #endif |
| 8912 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8913 | { BUILTIN_REGULAR "getopts" , getoptscmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8914 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8915 | { BUILTIN_NOSPEC "hash" , hashcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8916 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8917 | { BUILTIN_NOSPEC "help" , helpcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8918 | #endif |
| 8919 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8920 | { BUILTIN_REGULAR "jobs" , jobscmd }, |
| 8921 | { BUILTIN_REGULAR "kill" , killcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8922 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 8923 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8924 | { BUILTIN_NOSPEC "let" , letcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8925 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8926 | { BUILTIN_ASSIGN "local" , localcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8927 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8928 | { BUILTIN_REGULAR "printf" , printfcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8929 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8930 | { BUILTIN_NOSPEC "pwd" , pwdcmd }, |
| 8931 | { BUILTIN_REGULAR "read" , readcmd }, |
| 8932 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 8933 | { BUILTIN_SPEC_REG "return" , returncmd }, |
| 8934 | { BUILTIN_SPEC_REG "set" , setcmd }, |
| 8935 | { BUILTIN_SPEC_REG "shift" , shiftcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 8936 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8937 | { BUILTIN_SPEC_REG "source" , dotcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 8938 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8939 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8940 | { BUILTIN_REGULAR "test" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8941 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8942 | { BUILTIN_SPEC_REG "times" , timescmd }, |
| 8943 | { BUILTIN_SPEC_REG "trap" , trapcmd }, |
| 8944 | { BUILTIN_REGULAR "true" , truecmd }, |
| 8945 | { BUILTIN_NOSPEC "type" , typecmd }, |
| 8946 | { BUILTIN_NOSPEC "ulimit" , ulimitcmd }, |
| 8947 | { BUILTIN_REGULAR "umask" , umaskcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8948 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8949 | { BUILTIN_REGULAR "unalias" , unaliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8950 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8951 | { BUILTIN_SPEC_REG "unset" , unsetcmd }, |
| 8952 | { BUILTIN_REGULAR "wait" , waitcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8953 | }; |
| 8954 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8955 | /* Should match the above table! */ |
| 8956 | #define COMMANDCMD (builtintab + \ |
| 8957 | 2 + \ |
| 8958 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 8959 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 8960 | 1 * ENABLE_ASH_ALIAS + \ |
| 8961 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 8962 | 3) |
| 8963 | #define EXECCMD (builtintab + \ |
| 8964 | 2 + \ |
| 8965 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 8966 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 8967 | 1 * ENABLE_ASH_ALIAS + \ |
| 8968 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 8969 | 3 + \ |
| 8970 | 1 * ENABLE_ASH_CMDCMD + \ |
| 8971 | 1 + \ |
| 8972 | ENABLE_ASH_BUILTIN_ECHO + \ |
| 8973 | 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8974 | |
| 8975 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8976 | * Search the table of builtin commands. |
| 8977 | */ |
| 8978 | static struct builtincmd * |
| 8979 | find_builtin(const char *name) |
| 8980 | { |
| 8981 | struct builtincmd *bp; |
| 8982 | |
| 8983 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 8984 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8985 | pstrcmp |
| 8986 | ); |
| 8987 | return bp; |
| 8988 | } |
| 8989 | |
| 8990 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8991 | * Execute a simple command. |
| 8992 | */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8993 | static int |
| 8994 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 8995 | { |
| 8996 | const char *q = endofname(p); |
| 8997 | if (p == q) |
| 8998 | return 0; |
| 8999 | return *q == '='; |
| 9000 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9001 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9002 | bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9003 | { |
| 9004 | /* Preserve exitstatus of a previous possible redirection |
| 9005 | * as POSIX mandates */ |
| 9006 | return back_exitstatus; |
| 9007 | } |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 9008 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9009 | evalcommand(union node *cmd, int flags) |
| 9010 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9011 | static const struct builtincmd null_bltin = { |
| 9012 | "\0\0", bltincmd /* why three NULs? */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9013 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9014 | struct stackmark smark; |
| 9015 | union node *argp; |
| 9016 | struct arglist arglist; |
| 9017 | struct arglist varlist; |
| 9018 | char **argv; |
| 9019 | int argc; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9020 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9021 | struct cmdentry cmdentry; |
| 9022 | struct job *jp; |
| 9023 | char *lastarg; |
| 9024 | const char *path; |
| 9025 | int spclbltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9026 | int status; |
| 9027 | char **nargv; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9028 | struct builtincmd *bcmd; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9029 | smallint cmd_is_exec; |
| 9030 | smallint pseudovarflag = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9031 | |
| 9032 | /* First expand the arguments. */ |
| 9033 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 9034 | setstackmark(&smark); |
| 9035 | back_exitstatus = 0; |
| 9036 | |
| 9037 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9038 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9039 | varlist.lastp = &varlist.list; |
| 9040 | *varlist.lastp = NULL; |
| 9041 | arglist.lastp = &arglist.list; |
| 9042 | *arglist.lastp = NULL; |
| 9043 | |
| 9044 | argc = 0; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9045 | if (cmd->ncmd.args) { |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9046 | bcmd = find_builtin(cmd->ncmd.args->narg.text); |
| 9047 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
| 9048 | } |
| 9049 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9050 | for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) { |
| 9051 | struct strlist **spp; |
| 9052 | |
| 9053 | spp = arglist.lastp; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 9054 | if (pseudovarflag && isassignment(argp->narg.text)) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9055 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 9056 | else |
| 9057 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 9058 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9059 | for (sp = *spp; sp; sp = sp->next) |
| 9060 | argc++; |
| 9061 | } |
| 9062 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9063 | argv = nargv = stalloc(sizeof(char *) * (argc + 1)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9064 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9065 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 9066 | *nargv++ = sp->text; |
| 9067 | } |
| 9068 | *nargv = NULL; |
| 9069 | |
| 9070 | lastarg = NULL; |
| 9071 | if (iflag && funcnest == 0 && argc > 0) |
| 9072 | lastarg = nargv[-1]; |
| 9073 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9074 | preverrout_fd = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9075 | expredir(cmd->ncmd.redirect); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9076 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9077 | |
| 9078 | path = vpath.text; |
| 9079 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 9080 | struct strlist **spp; |
| 9081 | char *p; |
| 9082 | |
| 9083 | spp = varlist.lastp; |
| 9084 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 9085 | |
| 9086 | /* |
| 9087 | * Modify the command lookup path, if a PATH= assignment |
| 9088 | * is present |
| 9089 | */ |
| 9090 | p = (*spp)->text; |
| 9091 | if (varequal(p, path)) |
| 9092 | path = p; |
| 9093 | } |
| 9094 | |
| 9095 | /* Print the command if xflag is set. */ |
| 9096 | if (xflag) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9097 | int n; |
| 9098 | const char *p = " %s"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9099 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9100 | p++; |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9101 | fdprintf(preverrout_fd, p, expandstr(ps4val())); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9102 | |
| 9103 | sp = varlist.list; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9104 | for (n = 0; n < 2; n++) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9105 | while (sp) { |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9106 | fdprintf(preverrout_fd, p, sp->text); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9107 | sp = sp->next; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9108 | if (*p == '%') { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9109 | p--; |
| 9110 | } |
| 9111 | } |
| 9112 | sp = arglist.list; |
| 9113 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9114 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9115 | } |
| 9116 | |
| 9117 | cmd_is_exec = 0; |
| 9118 | spclbltin = -1; |
| 9119 | |
| 9120 | /* Now locate the command. */ |
| 9121 | if (argc) { |
| 9122 | const char *oldpath; |
| 9123 | int cmd_flag = DO_ERR; |
| 9124 | |
| 9125 | path += 5; |
| 9126 | oldpath = path; |
| 9127 | for (;;) { |
| 9128 | find_command(argv[0], &cmdentry, cmd_flag, path); |
| 9129 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9130 | flush_stdout_stderr(); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9131 | status = 127; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9132 | goto bail; |
| 9133 | } |
| 9134 | |
| 9135 | /* implement bltin and command here */ |
| 9136 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 9137 | break; |
| 9138 | if (spclbltin < 0) |
| 9139 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
| 9140 | if (cmdentry.u.cmd == EXECCMD) |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9141 | cmd_is_exec = 1; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9142 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9143 | if (cmdentry.u.cmd == COMMANDCMD) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9144 | path = oldpath; |
| 9145 | nargv = parse_command_args(argv, &path); |
| 9146 | if (!nargv) |
| 9147 | break; |
| 9148 | argc -= nargv - argv; |
| 9149 | argv = nargv; |
| 9150 | cmd_flag |= DO_NOFUNC; |
| 9151 | } else |
| 9152 | #endif |
| 9153 | break; |
| 9154 | } |
| 9155 | } |
| 9156 | |
| 9157 | if (status) { |
| 9158 | /* We have a redirection error. */ |
| 9159 | if (spclbltin > 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9160 | raise_exception(EXERROR); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9161 | bail: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9162 | exitstatus = status; |
| 9163 | goto out; |
| 9164 | } |
| 9165 | |
| 9166 | /* Execute the command. */ |
| 9167 | switch (cmdentry.cmdtype) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9168 | default: { |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9169 | |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9170 | #if ENABLE_FEATURE_SH_NOFORK |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9171 | /* (1) BUG: if variables are set, we need to fork, or save/restore them |
| 9172 | * around run_nofork_applet() call. |
| 9173 | * (2) Should this check also be done in forkshell()? |
| 9174 | * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...) |
| 9175 | */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9176 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 9177 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9178 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9179 | listsetvar(varlist.list, VEXPORT|VSTACK); |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9180 | /* run <applet>_main() */ |
| 9181 | exitstatus = run_nofork_applet(applet_no, argv); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9182 | break; |
| 9183 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9184 | #endif |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9185 | /* Can we avoid forking off? For example, very last command |
| 9186 | * in a script or a subshell does not need forking, |
| 9187 | * we can just exec it. |
| 9188 | */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9189 | if (!(flags & EV_EXIT) || may_have_traps) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9190 | /* No, forking off a child is necessary */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9191 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9192 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9193 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9194 | /* parent */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9195 | exitstatus = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9196 | INT_ON; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9197 | TRACE(("forked child exited with %d\n", exitstatus)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9198 | break; |
| 9199 | } |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9200 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9201 | FORCE_INT_ON; |
Denys Vlasenko | c7f95d2 | 2010-05-18 15:52:23 +0200 | [diff] [blame] | 9202 | /* fall through to exec'ing external program */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9203 | } |
| 9204 | listsetvar(varlist.list, VEXPORT|VSTACK); |
| 9205 | shellexec(argv, path, cmdentry.u.index); |
| 9206 | /* NOTREACHED */ |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9207 | } /* default */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9208 | case CMDBUILTIN: |
| 9209 | cmdenviron = varlist.list; |
| 9210 | if (cmdenviron) { |
| 9211 | struct strlist *list = cmdenviron; |
| 9212 | int i = VNOSET; |
| 9213 | if (spclbltin > 0 || argc == 0) { |
| 9214 | i = 0; |
| 9215 | if (cmd_is_exec && argc > 1) |
| 9216 | i = VEXPORT; |
| 9217 | } |
| 9218 | listsetvar(list, i); |
| 9219 | } |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9220 | /* Tight loop with builtins only: |
| 9221 | * "while kill -0 $child; do true; done" |
| 9222 | * will never exit even if $child died, unless we do this |
| 9223 | * to reap the zombie and make kill detect that it's gone: */ |
| 9224 | dowait(DOWAIT_NONBLOCK, NULL); |
| 9225 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9226 | if (evalbltin(cmdentry.u.cmd, argc, argv)) { |
| 9227 | int exit_status; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 9228 | int i = exception_type; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9229 | if (i == EXEXIT) |
| 9230 | goto raise; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9231 | exit_status = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9232 | if (i == EXINT) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 9233 | exit_status = 128 + SIGINT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9234 | if (i == EXSIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9235 | exit_status = 128 + pending_sig; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9236 | exitstatus = exit_status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9237 | if (i == EXINT || spclbltin > 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9238 | raise: |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9239 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9240 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9241 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9242 | } |
| 9243 | break; |
| 9244 | |
| 9245 | case CMDFUNCTION: |
| 9246 | listsetvar(varlist.list, 0); |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9247 | /* See above for the rationale */ |
| 9248 | dowait(DOWAIT_NONBLOCK, NULL); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9249 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 9250 | goto raise; |
| 9251 | break; |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9252 | |
| 9253 | } /* switch */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9254 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9255 | out: |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9256 | popredir(/*drop:*/ cmd_is_exec, /*restore:*/ cmd_is_exec); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9257 | if (lastarg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9258 | /* dsl: I think this is intended to be used to support |
| 9259 | * '_' in 'vi' command mode during line editing... |
| 9260 | * However I implemented that within libedit itself. |
| 9261 | */ |
| 9262 | setvar("_", lastarg, 0); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9263 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9264 | popstackmark(&smark); |
| 9265 | } |
| 9266 | |
| 9267 | static int |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9268 | evalbltin(const struct builtincmd *cmd, int argc, char **argv) |
| 9269 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9270 | char *volatile savecmdname; |
| 9271 | struct jmploc *volatile savehandler; |
| 9272 | struct jmploc jmploc; |
| 9273 | int i; |
| 9274 | |
| 9275 | savecmdname = commandname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9276 | i = setjmp(jmploc.loc); |
| 9277 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9278 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9279 | savehandler = exception_handler; |
| 9280 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9281 | commandname = argv[0]; |
| 9282 | argptr = argv + 1; |
| 9283 | optptr = NULL; /* initialize nextopt */ |
| 9284 | exitstatus = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9285 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9286 | cmddone: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9287 | exitstatus |= ferror(stdout); |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 9288 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9289 | commandname = savecmdname; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9290 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9291 | |
| 9292 | return i; |
| 9293 | } |
| 9294 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9295 | static int |
| 9296 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9297 | { |
| 9298 | return !*endofname(p); |
| 9299 | } |
| 9300 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9301 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9302 | /* |
| 9303 | * Search for a command. This is called before we fork so that the |
| 9304 | * 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] | 9305 | * the child. The check for "goodname" is an overly conservative |
| 9306 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9307 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9308 | static void |
| 9309 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9310 | { |
| 9311 | struct cmdentry entry; |
| 9312 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9313 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 9314 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9315 | } |
| 9316 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9317 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9318 | /* ============ Builtin commands |
| 9319 | * |
| 9320 | * Builtin commands whose functions are closely tied to evaluation |
| 9321 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9322 | */ |
| 9323 | |
| 9324 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9325 | * Handle break and continue commands. Break, continue, and return are |
| 9326 | * all handled by setting the evalskip flag. The evaluation routines |
| 9327 | * above all check this flag, and if it is set they start skipping |
| 9328 | * commands rather than executing them. The variable skipcount is |
| 9329 | * the number of loops to break/continue, or the number of function |
| 9330 | * levels to return. (The latter is always 1.) It should probably |
| 9331 | * be an error to break out of more loops than exist, but it isn't |
| 9332 | * in the standard shell so we don't make it one here. |
| 9333 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9334 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9335 | breakcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9336 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9337 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9338 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 9339 | if (n <= 0) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9340 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9341 | if (n > loopnest) |
| 9342 | n = loopnest; |
| 9343 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9344 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9345 | skipcount = n; |
| 9346 | } |
| 9347 | return 0; |
| 9348 | } |
| 9349 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9350 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9351 | /* ============ input.c |
| 9352 | * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 9353 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9354 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9355 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9356 | enum { |
| 9357 | INPUT_PUSH_FILE = 1, |
| 9358 | INPUT_NOFILE_OK = 2, |
| 9359 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9360 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9361 | static smallint checkkwd; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9362 | /* values of checkkwd variable */ |
| 9363 | #define CHKALIAS 0x1 |
| 9364 | #define CHKKWD 0x2 |
| 9365 | #define CHKNL 0x4 |
| 9366 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9367 | /* |
| 9368 | * Push a string back onto the input at this current parsefile level. |
| 9369 | * We handle aliases this way. |
| 9370 | */ |
| 9371 | #if !ENABLE_ASH_ALIAS |
| 9372 | #define pushstring(s, ap) pushstring(s) |
| 9373 | #endif |
| 9374 | static void |
| 9375 | pushstring(char *s, struct alias *ap) |
| 9376 | { |
| 9377 | struct strpush *sp; |
| 9378 | int len; |
| 9379 | |
| 9380 | len = strlen(s); |
| 9381 | INT_OFF; |
| 9382 | if (g_parsefile->strpush) { |
| 9383 | sp = ckzalloc(sizeof(*sp)); |
| 9384 | sp->prev = g_parsefile->strpush; |
| 9385 | } else { |
| 9386 | sp = &(g_parsefile->basestrpush); |
| 9387 | } |
| 9388 | g_parsefile->strpush = sp; |
| 9389 | sp->prev_string = g_parsefile->next_to_pgetc; |
| 9390 | sp->prev_left_in_line = g_parsefile->left_in_line; |
| 9391 | #if ENABLE_ASH_ALIAS |
| 9392 | sp->ap = ap; |
| 9393 | if (ap) { |
| 9394 | ap->flag |= ALIASINUSE; |
| 9395 | sp->string = s; |
| 9396 | } |
| 9397 | #endif |
| 9398 | g_parsefile->next_to_pgetc = s; |
| 9399 | g_parsefile->left_in_line = len; |
| 9400 | INT_ON; |
| 9401 | } |
| 9402 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9403 | static void |
| 9404 | popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9405 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9406 | struct strpush *sp = g_parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9407 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9408 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9409 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9410 | if (sp->ap) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9411 | if (g_parsefile->next_to_pgetc[-1] == ' ' |
| 9412 | || g_parsefile->next_to_pgetc[-1] == '\t' |
| 9413 | ) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9414 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9415 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9416 | if (sp->string != sp->ap->val) { |
| 9417 | free(sp->string); |
| 9418 | } |
| 9419 | sp->ap->flag &= ~ALIASINUSE; |
| 9420 | if (sp->ap->flag & ALIASDEAD) { |
| 9421 | unalias(sp->ap->name); |
| 9422 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9423 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9424 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9425 | g_parsefile->next_to_pgetc = sp->prev_string; |
| 9426 | g_parsefile->left_in_line = sp->prev_left_in_line; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9427 | g_parsefile->strpush = sp->prev; |
| 9428 | if (sp != &(g_parsefile->basestrpush)) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9429 | free(sp); |
| 9430 | INT_ON; |
| 9431 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9432 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9433 | //FIXME: BASH_COMPAT with "...&" does TWO pungetc(): |
| 9434 | //it peeks whether it is &>, and then pushes back both chars. |
| 9435 | //This function needs to save last *next_to_pgetc to buf[0] |
| 9436 | //to make two pungetc() reliable. Currently, |
| 9437 | // pgetc (out of buf: does preadfd), pgetc, pungetc, pungetc won't work... |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9438 | static int |
| 9439 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9440 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9441 | int nr; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 9442 | char *buf = g_parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9443 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9444 | g_parsefile->next_to_pgetc = buf; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9445 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9446 | retry: |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9447 | if (!iflag || g_parsefile->fd != STDIN_FILENO) |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 9448 | nr = nonblock_safe_read(g_parsefile->fd, buf, IBUFSIZ - 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9449 | else { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9450 | #if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9451 | line_input_state->path_lookup = pathval(); |
Eric Andersen | 4a79c0e | 2004-09-08 10:01:07 +0000 | [diff] [blame] | 9452 | #endif |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 9453 | nr = read_line_input(cmdedit_prompt, buf, IBUFSIZ, line_input_state); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9454 | if (nr == 0) { |
| 9455 | /* Ctrl+C pressed */ |
| 9456 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9457 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9458 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9459 | raise(SIGINT); |
| 9460 | return 1; |
| 9461 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9462 | goto retry; |
| 9463 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9464 | if (nr < 0 && errno == 0) { |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9465 | /* Ctrl+D pressed */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9466 | nr = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9467 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9468 | } |
| 9469 | #else |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 9470 | nr = nonblock_safe_read(g_parsefile->fd, buf, IBUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9471 | #endif |
| 9472 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9473 | #if 0 |
| 9474 | /* nonblock_safe_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9475 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9476 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 9477 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9478 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 9479 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9480 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 9481 | out2str("sh: turning off NDELAY mode\n"); |
| 9482 | goto retry; |
| 9483 | } |
| 9484 | } |
| 9485 | } |
| 9486 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9487 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9488 | return nr; |
| 9489 | } |
| 9490 | |
| 9491 | /* |
| 9492 | * Refill the input buffer and return the next input character: |
| 9493 | * |
| 9494 | * 1) If a string was pushed back on the input, pop it; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9495 | * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM) |
| 9496 | * or we are reading from a string so we can't refill the buffer, |
| 9497 | * return EOF. |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 9498 | * 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] | 9499 | * 4) Process input up to the next newline, deleting nul characters. |
| 9500 | */ |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9501 | //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__) |
| 9502 | #define pgetc_debug(...) ((void)0) |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9503 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9504 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9505 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9506 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9507 | int more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9508 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9509 | while (g_parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9510 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9511 | if (g_parsefile->left_in_line == -1 |
| 9512 | && g_parsefile->strpush->ap |
| 9513 | && g_parsefile->next_to_pgetc[-1] != ' ' |
| 9514 | && g_parsefile->next_to_pgetc[-1] != '\t' |
Denis Vlasenko | 1689840 | 2008-11-25 01:34:52 +0000 | [diff] [blame] | 9515 | ) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9516 | pgetc_debug("preadbuffer PEOA"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9517 | return PEOA; |
| 9518 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9519 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9520 | popstring(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9521 | /* try "pgetc" now: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9522 | pgetc_debug("preadbuffer internal pgetc at %d:%p'%s'", |
| 9523 | g_parsefile->left_in_line, |
| 9524 | g_parsefile->next_to_pgetc, |
| 9525 | g_parsefile->next_to_pgetc); |
| 9526 | if (--g_parsefile->left_in_line >= 0) |
| 9527 | return (unsigned char)(*g_parsefile->next_to_pgetc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9528 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9529 | /* on both branches above g_parsefile->left_in_line < 0. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9530 | * "pgetc" needs refilling. |
| 9531 | */ |
| 9532 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9533 | /* -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] | 9534 | * pungetc() may increment it a few times. |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9535 | * Assuming it won't increment it to less than -90. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9536 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9537 | if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9538 | pgetc_debug("preadbuffer PEOF1"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9539 | /* even in failure keep left_in_line and next_to_pgetc |
| 9540 | * in lock step, for correct multi-layer pungetc. |
| 9541 | * left_in_line was decremented before preadbuffer(), |
| 9542 | * must inc next_to_pgetc: */ |
| 9543 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9544 | return PEOF; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9545 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9546 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9547 | more = g_parsefile->left_in_buffer; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9548 | if (more <= 0) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9549 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9550 | again: |
| 9551 | more = preadfd(); |
| 9552 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9553 | /* don't try reading again */ |
| 9554 | g_parsefile->left_in_line = -99; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9555 | pgetc_debug("preadbuffer PEOF2"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9556 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9557 | return PEOF; |
| 9558 | } |
| 9559 | } |
| 9560 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9561 | /* Find out where's the end of line. |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9562 | * Set g_parsefile->left_in_line |
| 9563 | * and g_parsefile->left_in_buffer acordingly. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9564 | * NUL chars are deleted. |
| 9565 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9566 | q = g_parsefile->next_to_pgetc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9567 | for (;;) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9568 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9569 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9570 | more--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9571 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9572 | c = *q; |
| 9573 | if (c == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9574 | memmove(q, q + 1, more); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9575 | } else { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9576 | q++; |
| 9577 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9578 | 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] | 9579 | break; |
| 9580 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9581 | } |
| 9582 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9583 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9584 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
| 9585 | if (g_parsefile->left_in_line < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9586 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9587 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9588 | } |
| 9589 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9590 | g_parsefile->left_in_buffer = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9591 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9592 | if (vflag) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9593 | char save = *q; |
| 9594 | *q = '\0'; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9595 | out2str(g_parsefile->next_to_pgetc); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9596 | *q = save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9597 | } |
| 9598 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9599 | pgetc_debug("preadbuffer at %d:%p'%s'", |
| 9600 | g_parsefile->left_in_line, |
| 9601 | g_parsefile->next_to_pgetc, |
| 9602 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9603 | return (unsigned char)*g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9604 | } |
| 9605 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9606 | #define pgetc_as_macro() \ |
| 9607 | (--g_parsefile->left_in_line >= 0 \ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9608 | ? (unsigned char)*g_parsefile->next_to_pgetc++ \ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9609 | : preadbuffer() \ |
| 9610 | ) |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9611 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9612 | static int |
| 9613 | pgetc(void) |
| 9614 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9615 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9616 | g_parsefile->left_in_line, |
| 9617 | g_parsefile->next_to_pgetc, |
| 9618 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9619 | return pgetc_as_macro(); |
| 9620 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9621 | |
| 9622 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9623 | # define pgetc_fast() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9624 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9625 | # define pgetc_fast() pgetc_as_macro() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9626 | #endif |
| 9627 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9628 | #if ENABLE_ASH_ALIAS |
| 9629 | static int |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9630 | pgetc_without_PEOA(void) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9631 | { |
| 9632 | int c; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9633 | do { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9634 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9635 | g_parsefile->left_in_line, |
| 9636 | g_parsefile->next_to_pgetc, |
| 9637 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9638 | c = pgetc_fast(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9639 | } while (c == PEOA); |
| 9640 | return c; |
| 9641 | } |
| 9642 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9643 | # define pgetc_without_PEOA() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9644 | #endif |
| 9645 | |
| 9646 | /* |
| 9647 | * Read a line from the script. |
| 9648 | */ |
| 9649 | static char * |
| 9650 | pfgets(char *line, int len) |
| 9651 | { |
| 9652 | char *p = line; |
| 9653 | int nleft = len; |
| 9654 | int c; |
| 9655 | |
| 9656 | while (--nleft > 0) { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9657 | c = pgetc_without_PEOA(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9658 | if (c == PEOF) { |
| 9659 | if (p == line) |
| 9660 | return NULL; |
| 9661 | break; |
| 9662 | } |
| 9663 | *p++ = c; |
| 9664 | if (c == '\n') |
| 9665 | break; |
| 9666 | } |
| 9667 | *p = '\0'; |
| 9668 | return line; |
| 9669 | } |
| 9670 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9671 | /* |
| 9672 | * Undo the last call to pgetc. Only one character may be pushed back. |
| 9673 | * PEOF may be pushed back. |
| 9674 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9675 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9676 | pungetc(void) |
| 9677 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9678 | g_parsefile->left_in_line++; |
| 9679 | g_parsefile->next_to_pgetc--; |
| 9680 | pgetc_debug("pushed back to %d:%p'%s'", |
| 9681 | g_parsefile->left_in_line, |
| 9682 | g_parsefile->next_to_pgetc, |
| 9683 | g_parsefile->next_to_pgetc); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9684 | } |
| 9685 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9686 | /* |
| 9687 | * To handle the "." command, a stack of input files is used. Pushfile |
| 9688 | * adds a new entry to the stack and popfile restores the previous level. |
| 9689 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9690 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9691 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9692 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9693 | struct parsefile *pf; |
| 9694 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9695 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9696 | pf->prev = g_parsefile; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9697 | pf->fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9698 | /*pf->strpush = NULL; - ckzalloc did it */ |
| 9699 | /*pf->basestrpush.prev = NULL;*/ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9700 | g_parsefile = pf; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9701 | } |
| 9702 | |
| 9703 | static void |
| 9704 | popfile(void) |
| 9705 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9706 | struct parsefile *pf = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9707 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9708 | INT_OFF; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9709 | if (pf->fd >= 0) |
| 9710 | close(pf->fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 9711 | free(pf->buf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9712 | while (pf->strpush) |
| 9713 | popstring(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9714 | g_parsefile = pf->prev; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9715 | free(pf); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9716 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9717 | } |
| 9718 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9719 | /* |
| 9720 | * Return to top level. |
| 9721 | */ |
| 9722 | static void |
| 9723 | popallfiles(void) |
| 9724 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9725 | while (g_parsefile != &basepf) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9726 | popfile(); |
| 9727 | } |
| 9728 | |
| 9729 | /* |
| 9730 | * Close the file(s) that the shell is reading commands from. Called |
| 9731 | * after a fork is done. |
| 9732 | */ |
| 9733 | static void |
| 9734 | closescript(void) |
| 9735 | { |
| 9736 | popallfiles(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9737 | if (g_parsefile->fd > 0) { |
| 9738 | close(g_parsefile->fd); |
| 9739 | g_parsefile->fd = 0; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9740 | } |
| 9741 | } |
| 9742 | |
| 9743 | /* |
| 9744 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 9745 | * interrupts off. |
| 9746 | */ |
| 9747 | static void |
| 9748 | setinputfd(int fd, int push) |
| 9749 | { |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 9750 | close_on_exec_on(fd); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9751 | if (push) { |
| 9752 | pushfile(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9753 | g_parsefile->buf = NULL; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9754 | } |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9755 | g_parsefile->fd = fd; |
| 9756 | if (g_parsefile->buf == NULL) |
| 9757 | g_parsefile->buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9758 | g_parsefile->left_in_buffer = 0; |
| 9759 | g_parsefile->left_in_line = 0; |
| 9760 | g_parsefile->linno = 1; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9761 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9762 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9763 | /* |
| 9764 | * Set the input to take input from a file. If push is set, push the |
| 9765 | * old input onto the stack first. |
| 9766 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9767 | static int |
| 9768 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9769 | { |
| 9770 | int fd; |
| 9771 | int fd2; |
| 9772 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9773 | INT_OFF; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9774 | fd = open(fname, O_RDONLY); |
| 9775 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9776 | if (flags & INPUT_NOFILE_OK) |
| 9777 | goto out; |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 9778 | ash_msg_and_raise_error("can't open '%s'", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9779 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9780 | if (fd < 10) { |
| 9781 | fd2 = copyfd(fd, 10); |
| 9782 | close(fd); |
| 9783 | if (fd2 < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 9784 | ash_msg_and_raise_error("out of file descriptors"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9785 | fd = fd2; |
| 9786 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9787 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9788 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9789 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9790 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9791 | } |
| 9792 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9793 | /* |
| 9794 | * Like setinputfile, but takes input from a string. |
| 9795 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9796 | static void |
| 9797 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9798 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9799 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9800 | pushfile(); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9801 | g_parsefile->next_to_pgetc = string; |
| 9802 | g_parsefile->left_in_line = strlen(string); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9803 | g_parsefile->buf = NULL; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9804 | g_parsefile->linno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9805 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9806 | } |
| 9807 | |
| 9808 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9809 | /* ============ mail.c |
| 9810 | * |
| 9811 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9812 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9813 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9814 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9815 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9816 | #define MAXMBOXES 10 |
| 9817 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9818 | /* times of mailboxes */ |
| 9819 | static time_t mailtime[MAXMBOXES]; |
| 9820 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9821 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9822 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9823 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9824 | * Print appropriate message(s) if mail has arrived. |
| 9825 | * If mail_var_path_changed is set, |
| 9826 | * then the value of MAIL has mail_var_path_changed, |
| 9827 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9828 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9829 | static void |
| 9830 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9831 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9832 | const char *mpath; |
| 9833 | char *p; |
| 9834 | char *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9835 | time_t *mtp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9836 | struct stackmark smark; |
| 9837 | struct stat statb; |
| 9838 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9839 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9840 | mpath = mpathset() ? mpathval() : mailval(); |
| 9841 | for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 9842 | p = path_advance(&mpath, nullstr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9843 | if (p == NULL) |
| 9844 | break; |
| 9845 | if (*p == '\0') |
| 9846 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9847 | for (q = p; *q; q++) |
| 9848 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 9849 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9850 | if (q[-1] != '/') |
| 9851 | abort(); |
| 9852 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9853 | q[-1] = '\0'; /* delete trailing '/' */ |
| 9854 | if (stat(p, &statb) < 0) { |
| 9855 | *mtp = 0; |
| 9856 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9857 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9858 | if (!mail_var_path_changed && statb.st_mtime != *mtp) { |
| 9859 | fprintf( |
| 9860 | stderr, snlfmt, |
| 9861 | pathopt ? pathopt : "you have mail" |
| 9862 | ); |
| 9863 | } |
| 9864 | *mtp = statb.st_mtime; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9865 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9866 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9867 | popstackmark(&smark); |
| 9868 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9869 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9870 | static void FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9871 | changemail(const char *val UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9872 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9873 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9874 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9875 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9876 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9877 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9878 | |
| 9879 | /* ============ ??? */ |
| 9880 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9881 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9882 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9883 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9884 | static void |
| 9885 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9886 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9887 | char **newparam; |
| 9888 | char **ap; |
| 9889 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9890 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9891 | for (nparam = 0; argv[nparam]; nparam++) |
| 9892 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9893 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 9894 | while (*argv) { |
| 9895 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9896 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9897 | *ap = NULL; |
| 9898 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9899 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9900 | shellparam.nparam = nparam; |
| 9901 | shellparam.p = newparam; |
| 9902 | #if ENABLE_ASH_GETOPTS |
| 9903 | shellparam.optind = 1; |
| 9904 | shellparam.optoff = -1; |
| 9905 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9906 | } |
| 9907 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9908 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9909 | * Process shell options. The global variable argptr contains a pointer |
| 9910 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 9911 | * |
| 9912 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 9913 | * For a non-interactive shell, an error condition encountered |
| 9914 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 9915 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 9916 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 9917 | * ... |
| 9918 | * Utility syntax error (option or operand error) Shall exit |
| 9919 | * ... |
| 9920 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 9921 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 9922 | * and shell continues), and people rely on this behavior! |
| 9923 | * Testcase: |
| 9924 | * set -o barfoo 2>/dev/null |
| 9925 | * echo $? |
| 9926 | * |
| 9927 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9928 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9929 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9930 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9931 | { |
| 9932 | int i; |
| 9933 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9934 | if (name) { |
| 9935 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 9936 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9937 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9938 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9939 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9940 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9941 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9942 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9943 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9944 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9945 | if (val) { |
| 9946 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 9947 | } else { |
| 9948 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 9949 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9950 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9951 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9952 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9953 | static void |
| 9954 | setoption(int flag, int val) |
| 9955 | { |
| 9956 | int i; |
| 9957 | |
| 9958 | for (i = 0; i < NOPTS; i++) { |
| 9959 | if (optletters(i) == flag) { |
| 9960 | optlist[i] = val; |
| 9961 | return; |
| 9962 | } |
| 9963 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9964 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9965 | /* NOTREACHED */ |
| 9966 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9967 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9968 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9969 | { |
| 9970 | char *p; |
| 9971 | int val; |
| 9972 | int c; |
| 9973 | |
| 9974 | if (cmdline) |
| 9975 | minusc = NULL; |
| 9976 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9977 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9978 | if (c != '-' && c != '+') |
| 9979 | break; |
| 9980 | argptr++; |
| 9981 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9982 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9983 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 9984 | if (p[0] == '\0' || LONE_DASH(p)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9985 | if (!cmdline) { |
| 9986 | /* "-" means turn off -x and -v */ |
| 9987 | if (p[0] == '\0') |
| 9988 | xflag = vflag = 0; |
| 9989 | /* "--" means reset params */ |
| 9990 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9991 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9992 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9993 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9994 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9995 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9996 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9997 | while ((c = *p++) != '\0') { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9998 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9999 | if (c == 'c' && cmdline) { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10000 | minusc = p; /* command is after shell args */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10001 | } else if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10002 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10003 | /* it already printed err message */ |
| 10004 | return 1; /* error */ |
| 10005 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10006 | if (*argptr) |
| 10007 | argptr++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10008 | } else if (cmdline && (c == 'l')) { /* -l or +l == --login */ |
| 10009 | isloginsh = 1; |
| 10010 | /* bash does not accept +-login, we also won't */ |
| 10011 | } else if (cmdline && val && (c == '-')) { /* long options */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10012 | if (strcmp(p, "login") == 0) |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 10013 | isloginsh = 1; |
| 10014 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10015 | } else { |
| 10016 | setoption(c, val); |
| 10017 | } |
| 10018 | } |
| 10019 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10020 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10021 | } |
| 10022 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10023 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10024 | * The shift builtin command. |
| 10025 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10026 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10027 | shiftcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10028 | { |
| 10029 | int n; |
| 10030 | char **ap1, **ap2; |
| 10031 | |
| 10032 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10033 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10034 | n = number(argv[1]); |
| 10035 | if (n > shellparam.nparam) |
Denis Vlasenko | c90e1be | 2008-07-30 15:35:05 +0000 | [diff] [blame] | 10036 | n = 0; /* bash compat, was = shellparam.nparam; */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10037 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10038 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10039 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 10040 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10041 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10042 | } |
| 10043 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10044 | while ((*ap2++ = *ap1++) != NULL) |
| 10045 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10046 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10047 | shellparam.optind = 1; |
| 10048 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10049 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10050 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10051 | return 0; |
| 10052 | } |
| 10053 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10054 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10055 | * POSIX requires that 'set' (but not export or readonly) output the |
| 10056 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 10057 | * Maybe we could keep them in an ordered balanced binary tree |
| 10058 | * instead of hashed lists. |
| 10059 | * For now just roll 'em through qsort for printing... |
| 10060 | */ |
| 10061 | static int |
| 10062 | showvars(const char *sep_prefix, int on, int off) |
| 10063 | { |
| 10064 | const char *sep; |
| 10065 | char **ep, **epend; |
| 10066 | |
| 10067 | ep = listvars(on, off, &epend); |
| 10068 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 10069 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10070 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10071 | |
| 10072 | for (; ep < epend; ep++) { |
| 10073 | const char *p; |
| 10074 | const char *q; |
| 10075 | |
| 10076 | p = strchrnul(*ep, '='); |
| 10077 | q = nullstr; |
| 10078 | if (*p) |
| 10079 | q = single_quote(++p); |
| 10080 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 10081 | } |
| 10082 | return 0; |
| 10083 | } |
| 10084 | |
| 10085 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10086 | * The set command builtin. |
| 10087 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10088 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10089 | setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10090 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10091 | int retval; |
| 10092 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10093 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10094 | return showvars(nullstr, 0, VUNSET); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10095 | INT_OFF; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10096 | retval = 1; |
| 10097 | if (!options(0)) { /* if no parse error... */ |
| 10098 | retval = 0; |
| 10099 | optschanged(); |
| 10100 | if (*argptr != NULL) { |
| 10101 | setparam(argptr); |
| 10102 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10103 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10104 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10105 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10106 | } |
| 10107 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10108 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10109 | static void FAST_FUNC |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 10110 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10111 | { |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10112 | uint32_t t; |
| 10113 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10114 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10115 | /* "get", generate */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10116 | t = next_random(&random_gen); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10117 | /* set without recursion */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 10118 | setvar(vrandom.text, utoa(t), VNOFUNC); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10119 | vrandom.flags &= ~VNOFUNC; |
| 10120 | } else { |
| 10121 | /* set/reset */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10122 | t = strtoul(value, NULL, 10); |
| 10123 | INIT_RANDOM_T(&random_gen, (t ? t : 1), t); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10124 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10125 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10126 | #endif |
| 10127 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10128 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10129 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10130 | getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10131 | { |
| 10132 | char *p, *q; |
| 10133 | char c = '?'; |
| 10134 | int done = 0; |
| 10135 | int err = 0; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10136 | char s[12]; |
| 10137 | char **optnext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10138 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10139 | if (*param_optind < 1) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10140 | return 1; |
| 10141 | optnext = optfirst + *param_optind - 1; |
| 10142 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10143 | if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10144 | p = NULL; |
| 10145 | else |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10146 | p = optnext[-1] + *optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10147 | if (p == NULL || *p == '\0') { |
| 10148 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10149 | p = *optnext; |
| 10150 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10151 | atend: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10152 | p = NULL; |
| 10153 | done = 1; |
| 10154 | goto out; |
| 10155 | } |
| 10156 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10157 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10158 | goto atend; |
| 10159 | } |
| 10160 | |
| 10161 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 10162 | for (q = optstr; *q != c;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10163 | if (*q == '\0') { |
| 10164 | if (optstr[0] == ':') { |
| 10165 | s[0] = c; |
| 10166 | s[1] = '\0'; |
| 10167 | err |= setvarsafe("OPTARG", s, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10168 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10169 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10170 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10171 | } |
| 10172 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10173 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10174 | } |
| 10175 | if (*++q == ':') |
| 10176 | q++; |
| 10177 | } |
| 10178 | |
| 10179 | if (*++q == ':') { |
| 10180 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 10181 | if (optstr[0] == ':') { |
| 10182 | s[0] = c; |
| 10183 | s[1] = '\0'; |
| 10184 | err |= setvarsafe("OPTARG", s, 0); |
| 10185 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10186 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10187 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10188 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10189 | c = '?'; |
| 10190 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10191 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10192 | } |
| 10193 | |
| 10194 | if (p == *optnext) |
| 10195 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10196 | err |= setvarsafe("OPTARG", p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10197 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10198 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10199 | err |= setvarsafe("OPTARG", nullstr, 0); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10200 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10201 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10202 | *param_optind = optnext - optfirst + 1; |
| 10203 | fmtstr(s, sizeof(s), "%d", *param_optind); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10204 | err |= setvarsafe("OPTIND", s, VNOFUNC); |
| 10205 | s[0] = c; |
| 10206 | s[1] = '\0'; |
| 10207 | err |= setvarsafe(optvar, s, 0); |
| 10208 | if (err) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10209 | *param_optind = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10210 | *optoff = -1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10211 | flush_stdout_stderr(); |
| 10212 | raise_exception(EXERROR); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10213 | } |
| 10214 | return done; |
| 10215 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10216 | |
| 10217 | /* |
| 10218 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 10219 | * to be processed. Shellparam.optptr points to the next character to |
| 10220 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 10221 | * then it's the first time getopts has been called. |
| 10222 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10223 | static int FAST_FUNC |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10224 | getoptscmd(int argc, char **argv) |
| 10225 | { |
| 10226 | char **optbase; |
| 10227 | |
| 10228 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 10229 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10230 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10231 | optbase = shellparam.p; |
| 10232 | if (shellparam.optind > shellparam.nparam + 1) { |
| 10233 | shellparam.optind = 1; |
| 10234 | shellparam.optoff = -1; |
| 10235 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10236 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10237 | optbase = &argv[3]; |
| 10238 | if (shellparam.optind > argc - 2) { |
| 10239 | shellparam.optind = 1; |
| 10240 | shellparam.optoff = -1; |
| 10241 | } |
| 10242 | } |
| 10243 | |
| 10244 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10245 | &shellparam.optoff); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10246 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10247 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10248 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10249 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10250 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10251 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10252 | struct heredoc { |
| 10253 | struct heredoc *next; /* next here document in list */ |
| 10254 | union node *here; /* redirection node */ |
| 10255 | char *eofmark; /* string indicating end of input */ |
| 10256 | smallint striptabs; /* if set, strip leading tabs */ |
| 10257 | }; |
| 10258 | |
| 10259 | static smallint tokpushback; /* last token pushed back */ |
| 10260 | static smallint parsebackquote; /* nonzero if we are inside backquotes */ |
| 10261 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
| 10262 | static token_id_t lasttoken; /* last token read (integer id Txxx) */ |
| 10263 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 10264 | static char *wordtext; /* text of last word returned by readtoken */ |
| 10265 | static struct nodelist *backquotelist; |
| 10266 | static union node *redirnode; |
| 10267 | static struct heredoc *heredoc; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10268 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10269 | static const char * |
| 10270 | tokname(char *buf, int tok) |
| 10271 | { |
| 10272 | if (tok < TSEMI) |
| 10273 | return tokname_array[tok] + 1; |
| 10274 | sprintf(buf, "\"%s\"", tokname_array[tok] + 1); |
| 10275 | return buf; |
| 10276 | } |
| 10277 | |
| 10278 | /* raise_error_unexpected_syntax: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10279 | * Called when an unexpected token is read during the parse. The argument |
| 10280 | * is the token that is expected, or -1 if more than one type of token can |
| 10281 | * occur at this point. |
| 10282 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10283 | static void raise_error_unexpected_syntax(int) NORETURN; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10284 | static void |
| 10285 | raise_error_unexpected_syntax(int token) |
| 10286 | { |
| 10287 | char msg[64]; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10288 | char buf[16]; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10289 | int l; |
| 10290 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10291 | l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10292 | if (token >= 0) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10293 | sprintf(msg + l, " (expecting %s)", tokname(buf, token)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10294 | raise_error_syntax(msg); |
| 10295 | /* NOTREACHED */ |
| 10296 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10297 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10298 | #define EOFMARKLEN 79 |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10299 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10300 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 10301 | static union node *andor(void); |
| 10302 | static union node *pipeline(void); |
| 10303 | static union node *parse_command(void); |
| 10304 | static void parseheredoc(void); |
| 10305 | static char peektoken(void); |
| 10306 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10307 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10308 | static union node * |
| 10309 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10310 | { |
| 10311 | union node *n1, *n2, *n3; |
| 10312 | int tok; |
| 10313 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10314 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10315 | if (nlflag == 2 && peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10316 | return NULL; |
| 10317 | n1 = NULL; |
| 10318 | for (;;) { |
| 10319 | n2 = andor(); |
| 10320 | tok = readtoken(); |
| 10321 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10322 | if (n2->type == NPIPE) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10323 | n2->npipe.pipe_backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10324 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10325 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10326 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10327 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10328 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10329 | n2 = n3; |
| 10330 | } |
| 10331 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10332 | } |
| 10333 | } |
| 10334 | if (n1 == NULL) { |
| 10335 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10336 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10337 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10338 | n3->type = NSEMI; |
| 10339 | n3->nbinary.ch1 = n1; |
| 10340 | n3->nbinary.ch2 = n2; |
| 10341 | n1 = n3; |
| 10342 | } |
| 10343 | switch (tok) { |
| 10344 | case TBACKGND: |
| 10345 | case TSEMI: |
| 10346 | tok = readtoken(); |
| 10347 | /* fall through */ |
| 10348 | case TNL: |
| 10349 | if (tok == TNL) { |
| 10350 | parseheredoc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10351 | if (nlflag == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10352 | return n1; |
| 10353 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10354 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10355 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10356 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10357 | if (peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10358 | return n1; |
| 10359 | break; |
| 10360 | case TEOF: |
| 10361 | if (heredoclist) |
| 10362 | parseheredoc(); |
| 10363 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10364 | pungetc(); /* push back EOF on input */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10365 | return n1; |
| 10366 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10367 | if (nlflag == 1) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10368 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10369 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10370 | return n1; |
| 10371 | } |
| 10372 | } |
| 10373 | } |
| 10374 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10375 | static union node * |
| 10376 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10377 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10378 | union node *n1, *n2, *n3; |
| 10379 | int t; |
| 10380 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10381 | n1 = pipeline(); |
| 10382 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10383 | t = readtoken(); |
| 10384 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10385 | t = NAND; |
| 10386 | } else if (t == TOR) { |
| 10387 | t = NOR; |
| 10388 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10389 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10390 | return n1; |
| 10391 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10392 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10393 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10394 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10395 | n3->type = t; |
| 10396 | n3->nbinary.ch1 = n1; |
| 10397 | n3->nbinary.ch2 = n2; |
| 10398 | n1 = n3; |
| 10399 | } |
| 10400 | } |
| 10401 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10402 | static union node * |
| 10403 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10404 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10405 | union node *n1, *n2, *pipenode; |
| 10406 | struct nodelist *lp, *prev; |
| 10407 | int negate; |
| 10408 | |
| 10409 | negate = 0; |
| 10410 | TRACE(("pipeline: entered\n")); |
| 10411 | if (readtoken() == TNOT) { |
| 10412 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10413 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10414 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10415 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10416 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10417 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10418 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10419 | pipenode->type = NPIPE; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10420 | /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10421 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10422 | pipenode->npipe.cmdlist = lp; |
| 10423 | lp->n = n1; |
| 10424 | do { |
| 10425 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10426 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10427 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10428 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10429 | prev->next = lp; |
| 10430 | } while (readtoken() == TPIPE); |
| 10431 | lp->next = NULL; |
| 10432 | n1 = pipenode; |
| 10433 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10434 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10435 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10436 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10437 | n2->type = NNOT; |
| 10438 | n2->nnot.com = n1; |
| 10439 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10440 | } |
| 10441 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10442 | } |
| 10443 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10444 | static union node * |
| 10445 | makename(void) |
| 10446 | { |
| 10447 | union node *n; |
| 10448 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10449 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10450 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10451 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10452 | n->narg.text = wordtext; |
| 10453 | n->narg.backquote = backquotelist; |
| 10454 | return n; |
| 10455 | } |
| 10456 | |
| 10457 | static void |
| 10458 | fixredir(union node *n, const char *text, int err) |
| 10459 | { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10460 | int fd; |
| 10461 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10462 | TRACE(("Fix redir %s %d\n", text, err)); |
| 10463 | if (!err) |
| 10464 | n->ndup.vname = NULL; |
| 10465 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10466 | fd = bb_strtou(text, NULL, 10); |
| 10467 | if (!errno && fd >= 0) |
| 10468 | n->ndup.dupfd = fd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10469 | else if (LONE_DASH(text)) |
| 10470 | n->ndup.dupfd = -1; |
| 10471 | else { |
| 10472 | if (err) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10473 | raise_error_syntax("bad fd number"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10474 | n->ndup.vname = makename(); |
| 10475 | } |
| 10476 | } |
| 10477 | |
| 10478 | /* |
| 10479 | * Returns true if the text contains nothing to expand (no dollar signs |
| 10480 | * or backquotes). |
| 10481 | */ |
| 10482 | static int |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10483 | noexpand(const char *text) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10484 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10485 | unsigned char c; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10486 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10487 | while ((c = *text++) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10488 | if (c == CTLQUOTEMARK) |
| 10489 | continue; |
| 10490 | if (c == CTLESC) |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10491 | text++; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 10492 | else if (SIT(c, BASESYNTAX) == CCTL) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10493 | return 0; |
| 10494 | } |
| 10495 | return 1; |
| 10496 | } |
| 10497 | |
| 10498 | static void |
| 10499 | parsefname(void) |
| 10500 | { |
| 10501 | union node *n = redirnode; |
| 10502 | |
| 10503 | if (readtoken() != TWORD) |
| 10504 | raise_error_unexpected_syntax(-1); |
| 10505 | if (n->type == NHERE) { |
| 10506 | struct heredoc *here = heredoc; |
| 10507 | struct heredoc *p; |
| 10508 | int i; |
| 10509 | |
| 10510 | if (quoteflag == 0) |
| 10511 | n->type = NXHERE; |
| 10512 | TRACE(("Here document %d\n", n->type)); |
| 10513 | if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10514 | raise_error_syntax("illegal eof marker for << redirection"); |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 10515 | rmescapes(wordtext, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10516 | here->eofmark = wordtext; |
| 10517 | here->next = NULL; |
| 10518 | if (heredoclist == NULL) |
| 10519 | heredoclist = here; |
| 10520 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10521 | for (p = heredoclist; p->next; p = p->next) |
| 10522 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10523 | p->next = here; |
| 10524 | } |
| 10525 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 10526 | fixredir(n, wordtext, 0); |
| 10527 | } else { |
| 10528 | n->nfile.fname = makename(); |
| 10529 | } |
| 10530 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10531 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10532 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10533 | simplecmd(void) |
| 10534 | { |
| 10535 | union node *args, **app; |
| 10536 | union node *n = NULL; |
| 10537 | union node *vars, **vpp; |
| 10538 | union node **rpp, *redir; |
| 10539 | int savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10540 | #if ENABLE_ASH_BASH_COMPAT |
| 10541 | smallint double_brackets_flag = 0; |
| 10542 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10543 | |
| 10544 | args = NULL; |
| 10545 | app = &args; |
| 10546 | vars = NULL; |
| 10547 | vpp = &vars; |
| 10548 | redir = NULL; |
| 10549 | rpp = &redir; |
| 10550 | |
| 10551 | savecheckkwd = CHKALIAS; |
| 10552 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10553 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10554 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10555 | t = readtoken(); |
| 10556 | switch (t) { |
| 10557 | #if ENABLE_ASH_BASH_COMPAT |
| 10558 | case TAND: /* "&&" */ |
| 10559 | case TOR: /* "||" */ |
| 10560 | if (!double_brackets_flag) { |
| 10561 | tokpushback = 1; |
| 10562 | goto out; |
| 10563 | } |
| 10564 | wordtext = (char *) (t == TAND ? "-a" : "-o"); |
| 10565 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10566 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10567 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10568 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10569 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10570 | n->narg.text = wordtext; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10571 | #if ENABLE_ASH_BASH_COMPAT |
| 10572 | if (strcmp("[[", wordtext) == 0) |
| 10573 | double_brackets_flag = 1; |
| 10574 | else if (strcmp("]]", wordtext) == 0) |
| 10575 | double_brackets_flag = 0; |
| 10576 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10577 | n->narg.backquote = backquotelist; |
| 10578 | if (savecheckkwd && isassignment(wordtext)) { |
| 10579 | *vpp = n; |
| 10580 | vpp = &n->narg.next; |
| 10581 | } else { |
| 10582 | *app = n; |
| 10583 | app = &n->narg.next; |
| 10584 | savecheckkwd = 0; |
| 10585 | } |
| 10586 | break; |
| 10587 | case TREDIR: |
| 10588 | *rpp = n = redirnode; |
| 10589 | rpp = &n->nfile.next; |
| 10590 | parsefname(); /* read name of redirection file */ |
| 10591 | break; |
| 10592 | case TLP: |
| 10593 | if (args && app == &args->narg.next |
| 10594 | && !vars && !redir |
| 10595 | ) { |
| 10596 | struct builtincmd *bcmd; |
| 10597 | const char *name; |
| 10598 | |
| 10599 | /* We have a function */ |
| 10600 | if (readtoken() != TRP) |
| 10601 | raise_error_unexpected_syntax(TRP); |
| 10602 | name = n->narg.text; |
| 10603 | if (!goodname(name) |
| 10604 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 10605 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10606 | raise_error_syntax("bad function name"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10607 | } |
| 10608 | n->type = NDEFUN; |
| 10609 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10610 | n->narg.next = parse_command(); |
| 10611 | return n; |
| 10612 | } |
| 10613 | /* fall through */ |
| 10614 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10615 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10616 | goto out; |
| 10617 | } |
| 10618 | } |
| 10619 | out: |
| 10620 | *app = NULL; |
| 10621 | *vpp = NULL; |
| 10622 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10623 | n = stzalloc(sizeof(struct ncmd)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10624 | n->type = NCMD; |
| 10625 | n->ncmd.args = args; |
| 10626 | n->ncmd.assign = vars; |
| 10627 | n->ncmd.redirect = redir; |
| 10628 | return n; |
| 10629 | } |
| 10630 | |
| 10631 | static union node * |
| 10632 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10633 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10634 | union node *n1, *n2; |
| 10635 | union node *ap, **app; |
| 10636 | union node *cp, **cpp; |
| 10637 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10638 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10639 | int t; |
| 10640 | |
| 10641 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10642 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 10643 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10644 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10645 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10646 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10647 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10648 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10649 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10650 | n1->type = NIF; |
| 10651 | n1->nif.test = list(0); |
| 10652 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10653 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10654 | n1->nif.ifpart = list(0); |
| 10655 | n2 = n1; |
| 10656 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10657 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10658 | n2 = n2->nif.elsepart; |
| 10659 | n2->type = NIF; |
| 10660 | n2->nif.test = list(0); |
| 10661 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10662 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10663 | n2->nif.ifpart = list(0); |
| 10664 | } |
| 10665 | if (lasttoken == TELSE) |
| 10666 | n2->nif.elsepart = list(0); |
| 10667 | else { |
| 10668 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10669 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10670 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10671 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10672 | break; |
| 10673 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10674 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10675 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10676 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10677 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10678 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10679 | got = readtoken(); |
| 10680 | if (got != TDO) { |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10681 | TRACE(("expecting DO got '%s' %s\n", tokname_array[got] + 1, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10682 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10683 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10684 | } |
| 10685 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10686 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10687 | break; |
| 10688 | } |
| 10689 | case TFOR: |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10690 | if (readtoken() != TWORD || quoteflag || !goodname(wordtext)) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10691 | raise_error_syntax("bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10692 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10693 | n1->type = NFOR; |
| 10694 | n1->nfor.var = wordtext; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10695 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10696 | if (readtoken() == TIN) { |
| 10697 | app = ≈ |
| 10698 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10699 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10700 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10701 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10702 | n2->narg.text = wordtext; |
| 10703 | n2->narg.backquote = backquotelist; |
| 10704 | *app = n2; |
| 10705 | app = &n2->narg.next; |
| 10706 | } |
| 10707 | *app = NULL; |
| 10708 | n1->nfor.args = ap; |
| 10709 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10710 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10711 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10712 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10713 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10714 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10715 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10716 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10717 | n1->nfor.args = n2; |
| 10718 | /* |
| 10719 | * Newline or semicolon here is optional (but note |
| 10720 | * that the original Bourne shell only allowed NL). |
| 10721 | */ |
| 10722 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10723 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10724 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10725 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10726 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10727 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10728 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10729 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10730 | break; |
| 10731 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10732 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10733 | n1->type = NCASE; |
| 10734 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10735 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10736 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10737 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10738 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10739 | n2->narg.text = wordtext; |
| 10740 | n2->narg.backquote = backquotelist; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10741 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10742 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10743 | } while (readtoken() == TNL); |
| 10744 | if (lasttoken != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10745 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10746 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10747 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10748 | checkkwd = CHKNL | CHKKWD; |
| 10749 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10750 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10751 | if (lasttoken == TLP) |
| 10752 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10753 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10754 | cp->type = NCLIST; |
| 10755 | app = &cp->nclist.pattern; |
| 10756 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10757 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10758 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10759 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10760 | ap->narg.text = wordtext; |
| 10761 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10762 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10763 | break; |
| 10764 | app = &ap->narg.next; |
| 10765 | readtoken(); |
| 10766 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10767 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10768 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10769 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10770 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10771 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10772 | cpp = &cp->nclist.next; |
| 10773 | |
| 10774 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10775 | t = readtoken(); |
| 10776 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10777 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10778 | raise_error_unexpected_syntax(TENDCASE); |
| 10779 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10780 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10781 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10782 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10783 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10784 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10785 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10786 | n1->type = NSUBSHELL; |
| 10787 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10788 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10789 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10790 | break; |
| 10791 | case TBEGIN: |
| 10792 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10793 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10794 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10795 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10796 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10797 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10798 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10799 | } |
| 10800 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10801 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10802 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10803 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10804 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10805 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10806 | checkkwd = CHKKWD | CHKALIAS; |
| 10807 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10808 | while (readtoken() == TREDIR) { |
| 10809 | *rpp = n2 = redirnode; |
| 10810 | rpp = &n2->nfile.next; |
| 10811 | parsefname(); |
| 10812 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10813 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10814 | *rpp = NULL; |
| 10815 | if (redir) { |
| 10816 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10817 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10818 | n2->type = NREDIR; |
| 10819 | n2->nredir.n = n1; |
| 10820 | n1 = n2; |
| 10821 | } |
| 10822 | n1->nredir.redirect = redir; |
| 10823 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10824 | return n1; |
| 10825 | } |
| 10826 | |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10827 | #if ENABLE_ASH_BASH_COMPAT |
| 10828 | static int decode_dollar_squote(void) |
| 10829 | { |
| 10830 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 10831 | int c, cnt; |
| 10832 | char *p; |
| 10833 | char buf[4]; |
| 10834 | |
| 10835 | c = pgetc(); |
| 10836 | p = strchr(C_escapes, c); |
| 10837 | if (p) { |
| 10838 | buf[0] = c; |
| 10839 | p = buf; |
| 10840 | cnt = 3; |
| 10841 | if ((unsigned char)(c - '0') <= 7) { /* \ooo */ |
| 10842 | do { |
| 10843 | c = pgetc(); |
| 10844 | *++p = c; |
| 10845 | } while ((unsigned char)(c - '0') <= 7 && --cnt); |
| 10846 | pungetc(); |
| 10847 | } else if (c == 'x') { /* \xHH */ |
| 10848 | do { |
| 10849 | c = pgetc(); |
| 10850 | *++p = c; |
| 10851 | } while (isxdigit(c) && --cnt); |
| 10852 | pungetc(); |
| 10853 | if (cnt == 3) { /* \x but next char is "bad" */ |
| 10854 | c = 'x'; |
| 10855 | goto unrecognized; |
| 10856 | } |
| 10857 | } else { /* simple seq like \\ or \t */ |
| 10858 | p++; |
| 10859 | } |
| 10860 | *p = '\0'; |
| 10861 | p = buf; |
| 10862 | c = bb_process_escape_sequence((void*)&p); |
| 10863 | } else { /* unrecognized "\z": print both chars unless ' or " */ |
| 10864 | if (c != '\'' && c != '"') { |
| 10865 | unrecognized: |
| 10866 | c |= 0x100; /* "please encode \, then me" */ |
| 10867 | } |
| 10868 | } |
| 10869 | return c; |
| 10870 | } |
| 10871 | #endif |
| 10872 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10873 | /* |
| 10874 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 10875 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 10876 | * word which marks the end of the document and striptabs is true if |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10877 | * leading tabs should be stripped from the document. The argument c |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10878 | * is the first character of the input token or document. |
| 10879 | * |
| 10880 | * Because C does not have internal subroutines, I have simulated them |
| 10881 | * using goto's to implement the subroutine linkage. The following macros |
| 10882 | * will run code that appears at the end of readtoken1. |
| 10883 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10884 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 10885 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 10886 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 10887 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 10888 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 10889 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10890 | static int |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10891 | readtoken1(int c, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10892 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10893 | /* NB: syntax parameter fits into smallint */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10894 | /* c parameter is an unsigned char or PEOF or PEOA */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10895 | char *out; |
| 10896 | int len; |
| 10897 | char line[EOFMARKLEN + 1]; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10898 | struct nodelist *bqlist; |
| 10899 | smallint quotef; |
| 10900 | smallint dblquote; |
| 10901 | smallint oldstyle; |
| 10902 | smallint prevsyntax; /* syntax before arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10903 | #if ENABLE_ASH_EXPAND_PRMT |
| 10904 | smallint pssyntax; /* we are expanding a prompt string */ |
| 10905 | #endif |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10906 | int varnest; /* levels of variables expansion */ |
| 10907 | int arinest; /* levels of arithmetic expansion */ |
| 10908 | int parenlevel; /* levels of parens in arithmetic */ |
| 10909 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 10910 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 10911 | IF_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10912 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10913 | #if __GNUC__ |
| 10914 | /* Avoid longjmp clobbering */ |
| 10915 | (void) &out; |
| 10916 | (void) "ef; |
| 10917 | (void) &dblquote; |
| 10918 | (void) &varnest; |
| 10919 | (void) &arinest; |
| 10920 | (void) &parenlevel; |
| 10921 | (void) &dqvarnest; |
| 10922 | (void) &oldstyle; |
| 10923 | (void) &prevsyntax; |
| 10924 | (void) &syntax; |
| 10925 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10926 | startlinno = g_parsefile->linno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10927 | bqlist = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10928 | quotef = 0; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10929 | oldstyle = 0; |
| 10930 | prevsyntax = 0; |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10931 | #if ENABLE_ASH_EXPAND_PRMT |
| 10932 | pssyntax = (syntax == PSSYNTAX); |
| 10933 | if (pssyntax) |
| 10934 | syntax = DQSYNTAX; |
| 10935 | #endif |
| 10936 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10937 | varnest = 0; |
| 10938 | arinest = 0; |
| 10939 | parenlevel = 0; |
| 10940 | dqvarnest = 0; |
| 10941 | |
| 10942 | STARTSTACKSTR(out); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 10943 | loop: |
| 10944 | /* For each line, until end of word */ |
| 10945 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10946 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 10947 | for (;;) { /* until end of line or end of word */ |
| 10948 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 10949 | switch (SIT(c, syntax)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10950 | case CNL: /* '\n' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10951 | if (syntax == BASESYNTAX) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10952 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10953 | USTPUTC(c, out); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10954 | g_parsefile->linno++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10955 | if (doprompt) |
| 10956 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10957 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10958 | goto loop; /* continue outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10959 | case CWORD: |
| 10960 | USTPUTC(c, out); |
| 10961 | break; |
| 10962 | case CCTL: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10963 | if (eofmark == NULL || dblquote) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10964 | USTPUTC(CTLESC, out); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10965 | #if ENABLE_ASH_BASH_COMPAT |
| 10966 | if (c == '\\' && bash_dollar_squote) { |
| 10967 | c = decode_dollar_squote(); |
| 10968 | if (c & 0x100) { |
| 10969 | USTPUTC('\\', out); |
| 10970 | c = (unsigned char)c; |
| 10971 | } |
| 10972 | } |
| 10973 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10974 | USTPUTC(c, out); |
| 10975 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10976 | case CBACK: /* backslash */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 10977 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10978 | if (c == PEOF) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10979 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10980 | USTPUTC('\\', out); |
| 10981 | pungetc(); |
| 10982 | } else if (c == '\n') { |
| 10983 | if (doprompt) |
| 10984 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10985 | } else { |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10986 | #if ENABLE_ASH_EXPAND_PRMT |
| 10987 | if (c == '$' && pssyntax) { |
| 10988 | USTPUTC(CTLESC, out); |
| 10989 | USTPUTC('\\', out); |
| 10990 | } |
| 10991 | #endif |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10992 | if (dblquote && c != '\\' |
| 10993 | && c != '`' && c != '$' |
| 10994 | && (c != '"' || eofmark != NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10995 | ) { |
| 10996 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10997 | USTPUTC('\\', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10998 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10999 | if (SIT(c, SQSYNTAX) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11000 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11001 | USTPUTC(c, out); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11002 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11003 | } |
| 11004 | break; |
| 11005 | case CSQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11006 | syntax = SQSYNTAX; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11007 | quotemark: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11008 | if (eofmark == NULL) { |
| 11009 | USTPUTC(CTLQUOTEMARK, out); |
| 11010 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11011 | break; |
| 11012 | case CDQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11013 | syntax = DQSYNTAX; |
| 11014 | dblquote = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11015 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11016 | case CENDQUOTE: |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11017 | IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11018 | if (eofmark != NULL && arinest == 0 |
| 11019 | && varnest == 0 |
| 11020 | ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11021 | USTPUTC(c, out); |
| 11022 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11023 | if (dqvarnest == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11024 | syntax = BASESYNTAX; |
| 11025 | dblquote = 0; |
| 11026 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11027 | quotef = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11028 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11029 | } |
| 11030 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11031 | case CVAR: /* '$' */ |
| 11032 | PARSESUB(); /* parse substitution */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11033 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11034 | case CENDVAR: /* '}' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11035 | if (varnest > 0) { |
| 11036 | varnest--; |
| 11037 | if (dqvarnest > 0) { |
| 11038 | dqvarnest--; |
| 11039 | } |
| 11040 | USTPUTC(CTLENDVAR, out); |
| 11041 | } else { |
| 11042 | USTPUTC(c, out); |
| 11043 | } |
| 11044 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11045 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11046 | case CLP: /* '(' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11047 | parenlevel++; |
| 11048 | USTPUTC(c, out); |
| 11049 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11050 | case CRP: /* ')' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11051 | if (parenlevel > 0) { |
| 11052 | USTPUTC(c, out); |
| 11053 | --parenlevel; |
| 11054 | } else { |
| 11055 | if (pgetc() == ')') { |
| 11056 | if (--arinest == 0) { |
| 11057 | USTPUTC(CTLENDARI, out); |
| 11058 | syntax = prevsyntax; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11059 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11060 | } else |
| 11061 | USTPUTC(')', out); |
| 11062 | } else { |
| 11063 | /* |
| 11064 | * unbalanced parens |
| 11065 | * (don't 2nd guess - no error) |
| 11066 | */ |
| 11067 | pungetc(); |
| 11068 | USTPUTC(')', out); |
| 11069 | } |
| 11070 | } |
| 11071 | break; |
| 11072 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11073 | case CBQUOTE: /* '`' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11074 | PARSEBACKQOLD(); |
| 11075 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11076 | case CENDFILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11077 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11078 | case CIGN: |
| 11079 | break; |
| 11080 | default: |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11081 | if (varnest == 0) { |
| 11082 | #if ENABLE_ASH_BASH_COMPAT |
| 11083 | if (c == '&') { |
| 11084 | if (pgetc() == '>') |
| 11085 | c = 0x100 + '>'; /* flag &> */ |
| 11086 | pungetc(); |
| 11087 | } |
| 11088 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11089 | goto endword; /* exit outer loop */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11090 | } |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11091 | IF_ASH_ALIAS(if (c != PEOA)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11092 | USTPUTC(c, out); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 11093 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11094 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11095 | c = pgetc_fast(); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11096 | } /* for (;;) */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11097 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11098 | endword: |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11099 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11100 | if (syntax == ARISYNTAX) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11101 | raise_error_syntax("missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11102 | #endif |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 11103 | if (syntax != BASESYNTAX && !parsebackquote && eofmark == NULL) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11104 | raise_error_syntax("unterminated quoted string"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11105 | if (varnest != 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11106 | startlinno = g_parsefile->linno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11107 | /* { */ |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11108 | raise_error_syntax("missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11109 | } |
| 11110 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11111 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11112 | out = stackblock(); |
| 11113 | if (eofmark == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11114 | if ((c == '>' || c == '<' IF_ASH_BASH_COMPAT( || c == 0x100 + '>')) |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11115 | && quotef == 0 |
| 11116 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11117 | if (isdigit_str9(out)) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11118 | PARSEREDIR(); /* passed as params: out, c */ |
| 11119 | lasttoken = TREDIR; |
| 11120 | return lasttoken; |
| 11121 | } |
| 11122 | /* else: non-number X seen, interpret it |
| 11123 | * as "NNNX>file" = "NNNX >file" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11124 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11125 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11126 | } |
| 11127 | quoteflag = quotef; |
| 11128 | backquotelist = bqlist; |
| 11129 | grabstackblock(len); |
| 11130 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11131 | lasttoken = TWORD; |
| 11132 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11133 | /* end of readtoken routine */ |
| 11134 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11135 | /* |
| 11136 | * Check to see whether we are at the end of the here document. When this |
| 11137 | * is called, c is set to the first character of the next input line. If |
| 11138 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 11139 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11140 | checkend: { |
| 11141 | if (eofmark) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11142 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11143 | if (c == PEOA) |
| 11144 | c = pgetc_without_PEOA(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11145 | #endif |
| 11146 | if (striptabs) { |
| 11147 | while (c == '\t') { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11148 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11149 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11150 | } |
| 11151 | if (c == *eofmark) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11152 | if (pfgets(line, sizeof(line)) != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11153 | char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11154 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11155 | p = line; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11156 | for (q = eofmark + 1; *q && *p == *q; p++, q++) |
| 11157 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11158 | if (*p == '\n' && *q == '\0') { |
| 11159 | c = PEOF; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11160 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11161 | needprompt = doprompt; |
| 11162 | } else { |
| 11163 | pushstring(line, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11164 | } |
| 11165 | } |
| 11166 | } |
| 11167 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11168 | goto checkend_return; |
| 11169 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11170 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11171 | /* |
| 11172 | * Parse a redirection operator. The variable "out" points to a string |
| 11173 | * specifying the fd to be redirected. The variable "c" contains the |
| 11174 | * first character of the redirection operator. |
| 11175 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11176 | parseredir: { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11177 | /* out is already checked to be a valid number or "" */ |
| 11178 | int fd = (*out == '\0' ? -1 : atoi(out)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11179 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11180 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11181 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11182 | if (c == '>') { |
| 11183 | np->nfile.fd = 1; |
| 11184 | c = pgetc(); |
| 11185 | if (c == '>') |
| 11186 | np->type = NAPPEND; |
| 11187 | else if (c == '|') |
| 11188 | np->type = NCLOBBER; |
| 11189 | else if (c == '&') |
| 11190 | np->type = NTOFD; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11191 | /* 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] | 11192 | else { |
| 11193 | np->type = NTO; |
| 11194 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11195 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11196 | } |
| 11197 | #if ENABLE_ASH_BASH_COMPAT |
| 11198 | else if (c == 0x100 + '>') { /* this flags &> redirection */ |
| 11199 | np->nfile.fd = 1; |
| 11200 | pgetc(); /* this is '>', no need to check */ |
| 11201 | np->type = NTO2; |
| 11202 | } |
| 11203 | #endif |
| 11204 | else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11205 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11206 | c = pgetc(); |
| 11207 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11208 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11209 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11210 | np = stzalloc(sizeof(struct nhere)); |
| 11211 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11212 | } |
| 11213 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11214 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11215 | heredoc->here = np; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11216 | c = pgetc(); |
| 11217 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11218 | heredoc->striptabs = 1; |
| 11219 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11220 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11221 | pungetc(); |
| 11222 | } |
| 11223 | break; |
| 11224 | |
| 11225 | case '&': |
| 11226 | np->type = NFROMFD; |
| 11227 | break; |
| 11228 | |
| 11229 | case '>': |
| 11230 | np->type = NFROMTO; |
| 11231 | break; |
| 11232 | |
| 11233 | default: |
| 11234 | np->type = NFROM; |
| 11235 | pungetc(); |
| 11236 | break; |
| 11237 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11238 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11239 | if (fd >= 0) |
| 11240 | np->nfile.fd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11241 | redirnode = np; |
| 11242 | goto parseredir_return; |
| 11243 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11244 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11245 | /* |
| 11246 | * Parse a substitution. At this point, we have read the dollar sign |
| 11247 | * and nothing else. |
| 11248 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11249 | |
| 11250 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 11251 | * (assuming ascii char codes, as the original implementation did) */ |
| 11252 | #define is_special(c) \ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11253 | (((unsigned)(c) - 33 < 32) \ |
| 11254 | && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11255 | parsesub: { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11256 | unsigned char subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11257 | int typeloc; |
| 11258 | int flags; |
| 11259 | char *p; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11260 | static const char types[] ALIGN1 = "}-+?="; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11261 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11262 | c = pgetc(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11263 | if (c > 255 /* PEOA or PEOF */ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11264 | || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11265 | ) { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11266 | #if ENABLE_ASH_BASH_COMPAT |
| 11267 | if (c == '\'') |
| 11268 | bash_dollar_squote = 1; |
| 11269 | else |
| 11270 | #endif |
| 11271 | USTPUTC('$', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11272 | pungetc(); |
| 11273 | } else if (c == '(') { /* $(command) or $((arith)) */ |
| 11274 | if (pgetc() == '(') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11275 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11276 | PARSEARITH(); |
| 11277 | #else |
Mike Frysinger | 98a6f56 | 2008-06-09 09:38:45 +0000 | [diff] [blame] | 11278 | raise_error_syntax("you disabled math support for $((arith)) syntax"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11279 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11280 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11281 | pungetc(); |
| 11282 | PARSEBACKQNEW(); |
| 11283 | } |
| 11284 | } else { |
| 11285 | USTPUTC(CTLVAR, out); |
| 11286 | typeloc = out - (char *)stackblock(); |
| 11287 | USTPUTC(VSNORMAL, out); |
| 11288 | subtype = VSNORMAL; |
| 11289 | if (c == '{') { |
| 11290 | c = pgetc(); |
| 11291 | if (c == '#') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11292 | c = pgetc(); |
| 11293 | if (c == '}') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11294 | c = '#'; |
| 11295 | else |
| 11296 | subtype = VSLENGTH; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11297 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11298 | subtype = 0; |
| 11299 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11300 | if (c <= 255 /* not PEOA or PEOF */ && is_name(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11301 | do { |
| 11302 | STPUTC(c, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11303 | c = pgetc(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11304 | } while (c <= 255 /* not PEOA or PEOF */ && is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11305 | } else if (isdigit(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11306 | do { |
| 11307 | STPUTC(c, out); |
| 11308 | c = pgetc(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11309 | } while (isdigit(c)); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11310 | } else if (is_special(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11311 | USTPUTC(c, out); |
| 11312 | c = pgetc(); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11313 | } else { |
| 11314 | badsub: |
| 11315 | raise_error_syntax("bad substitution"); |
| 11316 | } |
Cristian Ionescu-Idbohrn | 301f5ec | 2009-10-05 02:07:23 +0200 | [diff] [blame] | 11317 | if (c != '}' && subtype == VSLENGTH) |
| 11318 | goto badsub; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11319 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11320 | STPUTC('=', out); |
| 11321 | flags = 0; |
| 11322 | if (subtype == 0) { |
| 11323 | switch (c) { |
| 11324 | case ':': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11325 | c = pgetc(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11326 | #if ENABLE_ASH_BASH_COMPAT |
| 11327 | if (c == ':' || c == '$' || isdigit(c)) { |
| 11328 | pungetc(); |
| 11329 | subtype = VSSUBSTR; |
| 11330 | break; |
| 11331 | } |
| 11332 | #endif |
| 11333 | flags = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11334 | /*FALLTHROUGH*/ |
| 11335 | default: |
| 11336 | p = strchr(types, c); |
| 11337 | if (p == NULL) |
| 11338 | goto badsub; |
| 11339 | subtype = p - types + VSNORMAL; |
| 11340 | break; |
| 11341 | case '%': |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11342 | case '#': { |
| 11343 | int cc = c; |
| 11344 | subtype = c == '#' ? VSTRIMLEFT : VSTRIMRIGHT; |
| 11345 | c = pgetc(); |
| 11346 | if (c == cc) |
| 11347 | subtype++; |
| 11348 | else |
| 11349 | pungetc(); |
| 11350 | break; |
| 11351 | } |
| 11352 | #if ENABLE_ASH_BASH_COMPAT |
| 11353 | case '/': |
| 11354 | subtype = VSREPLACE; |
| 11355 | c = pgetc(); |
| 11356 | if (c == '/') |
| 11357 | subtype++; /* VSREPLACEALL */ |
| 11358 | else |
| 11359 | pungetc(); |
| 11360 | break; |
| 11361 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11362 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11363 | } else { |
| 11364 | pungetc(); |
| 11365 | } |
| 11366 | if (dblquote || arinest) |
| 11367 | flags |= VSQUOTE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11368 | ((unsigned char *)stackblock())[typeloc] = subtype | flags; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11369 | if (subtype != VSNORMAL) { |
| 11370 | varnest++; |
| 11371 | if (dblquote || arinest) { |
| 11372 | dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11373 | } |
| 11374 | } |
| 11375 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11376 | goto parsesub_return; |
| 11377 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11378 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11379 | /* |
| 11380 | * Called to parse command substitutions. Newstyle is set if the command |
| 11381 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 11382 | * list of commands (passed by reference), and savelen is the number of |
| 11383 | * characters on the top of the stack which must be preserved. |
| 11384 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11385 | parsebackq: { |
| 11386 | struct nodelist **nlpp; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11387 | smallint savepbq; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11388 | union node *n; |
| 11389 | char *volatile str; |
| 11390 | struct jmploc jmploc; |
| 11391 | struct jmploc *volatile savehandler; |
| 11392 | size_t savelen; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11393 | smallint saveprompt = 0; |
| 11394 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11395 | #ifdef __GNUC__ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11396 | (void) &saveprompt; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11397 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11398 | savepbq = parsebackquote; |
| 11399 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 11400 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11401 | parsebackquote = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11402 | exception_handler = savehandler; |
| 11403 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11404 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11405 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11406 | str = NULL; |
| 11407 | savelen = out - (char *)stackblock(); |
| 11408 | if (savelen > 0) { |
| 11409 | str = ckmalloc(savelen); |
| 11410 | memcpy(str, stackblock(), savelen); |
| 11411 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11412 | savehandler = exception_handler; |
| 11413 | exception_handler = &jmploc; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11414 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11415 | if (oldstyle) { |
| 11416 | /* We must read until the closing backquote, giving special |
| 11417 | treatment to some slashes, and then push the string and |
| 11418 | reread it as input, interpreting it normally. */ |
| 11419 | char *pout; |
| 11420 | int pc; |
| 11421 | size_t psavelen; |
| 11422 | char *pstr; |
| 11423 | |
| 11424 | |
| 11425 | STARTSTACKSTR(pout); |
| 11426 | for (;;) { |
| 11427 | if (needprompt) { |
| 11428 | setprompt(2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11429 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11430 | pc = pgetc(); |
| 11431 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11432 | case '`': |
| 11433 | goto done; |
| 11434 | |
| 11435 | case '\\': |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11436 | pc = pgetc(); |
| 11437 | if (pc == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11438 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11439 | if (doprompt) |
| 11440 | setprompt(2); |
| 11441 | /* |
| 11442 | * If eating a newline, avoid putting |
| 11443 | * the newline into the new character |
| 11444 | * stream (via the STPUTC after the |
| 11445 | * switch). |
| 11446 | */ |
| 11447 | continue; |
| 11448 | } |
| 11449 | if (pc != '\\' && pc != '`' && pc != '$' |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11450 | && (!dblquote || pc != '"') |
| 11451 | ) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11452 | STPUTC('\\', pout); |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11453 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11454 | if (pc <= 255 /* not PEOA or PEOF */) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11455 | break; |
| 11456 | } |
| 11457 | /* fall through */ |
| 11458 | |
| 11459 | case PEOF: |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11460 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11461 | startlinno = g_parsefile->linno; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11462 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11463 | |
| 11464 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11465 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11466 | needprompt = doprompt; |
| 11467 | break; |
| 11468 | |
| 11469 | default: |
| 11470 | break; |
| 11471 | } |
| 11472 | STPUTC(pc, pout); |
| 11473 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11474 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11475 | STPUTC('\0', pout); |
| 11476 | psavelen = pout - (char *)stackblock(); |
| 11477 | if (psavelen > 0) { |
| 11478 | pstr = grabstackstr(pout); |
| 11479 | setinputstring(pstr); |
| 11480 | } |
| 11481 | } |
| 11482 | nlpp = &bqlist; |
| 11483 | while (*nlpp) |
| 11484 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11485 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 11486 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11487 | parsebackquote = oldstyle; |
| 11488 | |
| 11489 | if (oldstyle) { |
| 11490 | saveprompt = doprompt; |
| 11491 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11492 | } |
| 11493 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11494 | n = list(2); |
| 11495 | |
| 11496 | if (oldstyle) |
| 11497 | doprompt = saveprompt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11498 | else if (readtoken() != TRP) |
| 11499 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11500 | |
| 11501 | (*nlpp)->n = n; |
| 11502 | if (oldstyle) { |
| 11503 | /* |
| 11504 | * Start reading from old file again, ignoring any pushed back |
| 11505 | * tokens left from the backquote parsing |
| 11506 | */ |
| 11507 | popfile(); |
| 11508 | tokpushback = 0; |
| 11509 | } |
| 11510 | while (stackblocksize() <= savelen) |
| 11511 | growstackblock(); |
| 11512 | STARTSTACKSTR(out); |
| 11513 | if (str) { |
| 11514 | memcpy(out, str, savelen); |
| 11515 | STADJUST(savelen, out); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11516 | INT_OFF; |
| 11517 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11518 | str = NULL; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11519 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11520 | } |
| 11521 | parsebackquote = savepbq; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11522 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11523 | if (arinest || dblquote) |
| 11524 | USTPUTC(CTLBACKQ | CTLQUOTE, out); |
| 11525 | else |
| 11526 | USTPUTC(CTLBACKQ, out); |
| 11527 | if (oldstyle) |
| 11528 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11529 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11530 | } |
| 11531 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11532 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11533 | /* |
| 11534 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 11535 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11536 | parsearith: { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11537 | if (++arinest == 1) { |
| 11538 | prevsyntax = syntax; |
| 11539 | syntax = ARISYNTAX; |
| 11540 | USTPUTC(CTLARI, out); |
| 11541 | if (dblquote) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11542 | USTPUTC('"', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11543 | else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11544 | USTPUTC(' ', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11545 | } else { |
| 11546 | /* |
| 11547 | * we collapse embedded arithmetic expansion to |
| 11548 | * parenthesis, which should be equivalent |
| 11549 | */ |
| 11550 | USTPUTC('(', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11551 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11552 | goto parsearith_return; |
| 11553 | } |
| 11554 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11555 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11556 | } /* end of readtoken */ |
| 11557 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11558 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11559 | * Read the next input token. |
| 11560 | * If the token is a word, we set backquotelist to the list of cmds in |
| 11561 | * backquotes. We set quoteflag to true if any part of the word was |
| 11562 | * quoted. |
| 11563 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 11564 | * the redirection. |
| 11565 | * In all cases, the variable startlinno is set to the number of the line |
| 11566 | * on which the token starts. |
| 11567 | * |
| 11568 | * [Change comment: here documents and internal procedures] |
| 11569 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 11570 | * word parsing code into a separate routine. In this case, readtoken |
| 11571 | * doesn't need to have any internal procedures, but parseword does. |
| 11572 | * We could also make parseoperator in essence the main routine, and |
| 11573 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11574 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11575 | #define NEW_xxreadtoken |
| 11576 | #ifdef NEW_xxreadtoken |
| 11577 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11578 | static const char xxreadtoken_chars[7] ALIGN1 = { |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11579 | '\n', '(', ')', /* singles */ |
| 11580 | '&', '|', ';', /* doubles */ |
| 11581 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11582 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11583 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11584 | #define xxreadtoken_singles 3 |
| 11585 | #define xxreadtoken_doubles 3 |
| 11586 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11587 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11588 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 11589 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 11590 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11591 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11592 | }; |
| 11593 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11594 | static int |
| 11595 | xxreadtoken(void) |
| 11596 | { |
| 11597 | int c; |
| 11598 | |
| 11599 | if (tokpushback) { |
| 11600 | tokpushback = 0; |
| 11601 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11602 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11603 | if (needprompt) { |
| 11604 | setprompt(2); |
| 11605 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11606 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11607 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11608 | c = pgetc_fast(); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11609 | if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA)) |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11610 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11611 | |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11612 | if (c == '#') { |
| 11613 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11614 | continue; |
| 11615 | pungetc(); |
| 11616 | } else if (c == '\\') { |
| 11617 | if (pgetc() != '\n') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11618 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11619 | break; /* return readtoken1(...) */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11620 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11621 | startlinno = ++g_parsefile->linno; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11622 | if (doprompt) |
| 11623 | setprompt(2); |
| 11624 | } else { |
| 11625 | const char *p; |
| 11626 | |
| 11627 | p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 11628 | if (c != PEOF) { |
| 11629 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11630 | g_parsefile->linno++; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11631 | needprompt = doprompt; |
| 11632 | } |
| 11633 | |
| 11634 | p = strchr(xxreadtoken_chars, c); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11635 | if (p == NULL) |
| 11636 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11637 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11638 | if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
| 11639 | int cc = pgetc(); |
| 11640 | if (cc == c) { /* double occurrence? */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11641 | p += xxreadtoken_doubles + 1; |
| 11642 | } else { |
| 11643 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11644 | #if ENABLE_ASH_BASH_COMPAT |
| 11645 | if (c == '&' && cc == '>') /* &> */ |
| 11646 | break; /* return readtoken1(...) */ |
| 11647 | #endif |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11648 | } |
| 11649 | } |
| 11650 | } |
| 11651 | lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 11652 | return lasttoken; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11653 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11654 | } /* for (;;) */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11655 | |
| 11656 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11657 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11658 | #else /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11659 | #define RETURN(token) return lasttoken = token |
| 11660 | static int |
| 11661 | xxreadtoken(void) |
| 11662 | { |
| 11663 | int c; |
| 11664 | |
| 11665 | if (tokpushback) { |
| 11666 | tokpushback = 0; |
| 11667 | return lasttoken; |
| 11668 | } |
| 11669 | if (needprompt) { |
| 11670 | setprompt(2); |
| 11671 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11672 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11673 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11674 | c = pgetc_fast(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11675 | switch (c) { |
| 11676 | case ' ': case '\t': |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11677 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11678 | continue; |
| 11679 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11680 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11681 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11682 | pungetc(); |
| 11683 | continue; |
| 11684 | case '\\': |
| 11685 | if (pgetc() == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11686 | startlinno = ++g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11687 | if (doprompt) |
| 11688 | setprompt(2); |
| 11689 | continue; |
| 11690 | } |
| 11691 | pungetc(); |
| 11692 | goto breakloop; |
| 11693 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11694 | g_parsefile->linno++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11695 | needprompt = doprompt; |
| 11696 | RETURN(TNL); |
| 11697 | case PEOF: |
| 11698 | RETURN(TEOF); |
| 11699 | case '&': |
| 11700 | if (pgetc() == '&') |
| 11701 | RETURN(TAND); |
| 11702 | pungetc(); |
| 11703 | RETURN(TBACKGND); |
| 11704 | case '|': |
| 11705 | if (pgetc() == '|') |
| 11706 | RETURN(TOR); |
| 11707 | pungetc(); |
| 11708 | RETURN(TPIPE); |
| 11709 | case ';': |
| 11710 | if (pgetc() == ';') |
| 11711 | RETURN(TENDCASE); |
| 11712 | pungetc(); |
| 11713 | RETURN(TSEMI); |
| 11714 | case '(': |
| 11715 | RETURN(TLP); |
| 11716 | case ')': |
| 11717 | RETURN(TRP); |
| 11718 | default: |
| 11719 | goto breakloop; |
| 11720 | } |
| 11721 | } |
| 11722 | breakloop: |
| 11723 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 11724 | #undef RETURN |
| 11725 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11726 | #endif /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11727 | |
| 11728 | static int |
| 11729 | readtoken(void) |
| 11730 | { |
| 11731 | int t; |
| 11732 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11733 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11734 | #endif |
| 11735 | |
| 11736 | #if ENABLE_ASH_ALIAS |
| 11737 | top: |
| 11738 | #endif |
| 11739 | |
| 11740 | t = xxreadtoken(); |
| 11741 | |
| 11742 | /* |
| 11743 | * eat newlines |
| 11744 | */ |
| 11745 | if (checkkwd & CHKNL) { |
| 11746 | while (t == TNL) { |
| 11747 | parseheredoc(); |
| 11748 | t = xxreadtoken(); |
| 11749 | } |
| 11750 | } |
| 11751 | |
| 11752 | if (t != TWORD || quoteflag) { |
| 11753 | goto out; |
| 11754 | } |
| 11755 | |
| 11756 | /* |
| 11757 | * check for keywords |
| 11758 | */ |
| 11759 | if (checkkwd & CHKKWD) { |
| 11760 | const char *const *pp; |
| 11761 | |
| 11762 | pp = findkwd(wordtext); |
| 11763 | if (pp) { |
| 11764 | lasttoken = t = pp - tokname_array; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11765 | TRACE(("keyword '%s' recognized\n", tokname_array[t] + 1)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11766 | goto out; |
| 11767 | } |
| 11768 | } |
| 11769 | |
| 11770 | if (checkkwd & CHKALIAS) { |
| 11771 | #if ENABLE_ASH_ALIAS |
| 11772 | struct alias *ap; |
| 11773 | ap = lookupalias(wordtext, 1); |
| 11774 | if (ap != NULL) { |
| 11775 | if (*ap->val) { |
| 11776 | pushstring(ap->val, ap); |
| 11777 | } |
| 11778 | goto top; |
| 11779 | } |
| 11780 | #endif |
| 11781 | } |
| 11782 | out: |
| 11783 | checkkwd = 0; |
| 11784 | #if DEBUG |
| 11785 | if (!alreadyseen) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11786 | TRACE(("token '%s' %s\n", tokname_array[t] + 1, t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11787 | else |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11788 | TRACE(("reread token '%s' %s\n", tokname_array[t] + 1, t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11789 | #endif |
| 11790 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11791 | } |
| 11792 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11793 | static char |
| 11794 | peektoken(void) |
| 11795 | { |
| 11796 | int t; |
| 11797 | |
| 11798 | t = readtoken(); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11799 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11800 | return tokname_array[t][0]; |
| 11801 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11802 | |
| 11803 | /* |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11804 | * Read and parse a command. Returns NODE_EOF on end of file. |
| 11805 | * (NULL is a valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11806 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11807 | static union node * |
| 11808 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11809 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11810 | int t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11811 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11812 | tokpushback = 0; |
| 11813 | doprompt = interact; |
| 11814 | if (doprompt) |
| 11815 | setprompt(doprompt); |
| 11816 | needprompt = 0; |
| 11817 | t = readtoken(); |
| 11818 | if (t == TEOF) |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11819 | return NODE_EOF; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11820 | if (t == TNL) |
| 11821 | return NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11822 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11823 | return list(1); |
| 11824 | } |
| 11825 | |
| 11826 | /* |
| 11827 | * Input any here documents. |
| 11828 | */ |
| 11829 | static void |
| 11830 | parseheredoc(void) |
| 11831 | { |
| 11832 | struct heredoc *here; |
| 11833 | union node *n; |
| 11834 | |
| 11835 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11836 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11837 | |
| 11838 | while (here) { |
| 11839 | if (needprompt) { |
| 11840 | setprompt(2); |
| 11841 | } |
| 11842 | readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, |
| 11843 | here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11844 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11845 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11846 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11847 | n->narg.text = wordtext; |
| 11848 | n->narg.backquote = backquotelist; |
| 11849 | here->here->nhere.doc = n; |
| 11850 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11851 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11852 | } |
| 11853 | |
| 11854 | |
| 11855 | /* |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 11856 | * called by editline -- any expansions to the prompt should be added here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11857 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11858 | #if ENABLE_ASH_EXPAND_PRMT |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11859 | static const char * |
| 11860 | expandstr(const char *ps) |
| 11861 | { |
| 11862 | union node n; |
| 11863 | |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 11864 | /* XXX Fix (char *) cast. It _is_ a bug. ps is variable's value, |
| 11865 | * and token processing _can_ alter it (delete NULs etc). */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11866 | setinputstring((char *)ps); |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11867 | readtoken1(pgetc(), PSSYNTAX, nullstr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11868 | popfile(); |
| 11869 | |
| 11870 | n.narg.type = NARG; |
| 11871 | n.narg.next = NULL; |
| 11872 | n.narg.text = wordtext; |
| 11873 | n.narg.backquote = backquotelist; |
| 11874 | |
| 11875 | expandarg(&n, NULL, 0); |
| 11876 | return stackblock(); |
| 11877 | } |
| 11878 | #endif |
| 11879 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11880 | /* |
| 11881 | * Execute a command or commands contained in a string. |
| 11882 | */ |
| 11883 | static int |
| 11884 | evalstring(char *s, int mask) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11885 | { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11886 | union node *n; |
| 11887 | struct stackmark smark; |
| 11888 | int skip; |
| 11889 | |
| 11890 | setinputstring(s); |
| 11891 | setstackmark(&smark); |
| 11892 | |
| 11893 | skip = 0; |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11894 | while ((n = parsecmd(0)) != NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11895 | evaltree(n, 0); |
| 11896 | popstackmark(&smark); |
| 11897 | skip = evalskip; |
| 11898 | if (skip) |
| 11899 | break; |
| 11900 | } |
| 11901 | popfile(); |
| 11902 | |
| 11903 | skip &= mask; |
| 11904 | evalskip = skip; |
| 11905 | return skip; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11906 | } |
| 11907 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11908 | /* |
| 11909 | * The eval command. |
| 11910 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11911 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11912 | evalcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11913 | { |
| 11914 | char *p; |
| 11915 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11916 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11917 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11918 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11919 | argv += 2; |
| 11920 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11921 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11922 | for (;;) { |
| 11923 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11924 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11925 | if (p == NULL) |
| 11926 | break; |
| 11927 | STPUTC(' ', concat); |
| 11928 | } |
| 11929 | STPUTC('\0', concat); |
| 11930 | p = grabstackstr(concat); |
| 11931 | } |
| 11932 | evalstring(p, ~SKIPEVAL); |
| 11933 | |
| 11934 | } |
| 11935 | return exitstatus; |
| 11936 | } |
| 11937 | |
| 11938 | /* |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 11939 | * Read and execute commands. |
| 11940 | * "Top" is nonzero for the top level command loop; |
| 11941 | * it turns on prompting if the shell is interactive. |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11942 | */ |
| 11943 | static int |
| 11944 | cmdloop(int top) |
| 11945 | { |
| 11946 | union node *n; |
| 11947 | struct stackmark smark; |
| 11948 | int inter; |
| 11949 | int numeof = 0; |
| 11950 | |
| 11951 | TRACE(("cmdloop(%d) called\n", top)); |
| 11952 | for (;;) { |
| 11953 | int skip; |
| 11954 | |
| 11955 | setstackmark(&smark); |
| 11956 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11957 | if (doing_jobctl) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11958 | showjobs(stderr, SHOW_CHANGED); |
| 11959 | #endif |
| 11960 | inter = 0; |
| 11961 | if (iflag && top) { |
| 11962 | inter++; |
| 11963 | #if ENABLE_ASH_MAIL |
| 11964 | chkmail(); |
| 11965 | #endif |
| 11966 | } |
| 11967 | n = parsecmd(inter); |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 11968 | #if DEBUG |
| 11969 | if (DEBUG > 2 && debug && (n != NODE_EOF)) |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 11970 | showtree(n); |
Denis Vlasenko | 135cecb | 2009-04-12 00:00:57 +0000 | [diff] [blame] | 11971 | #endif |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11972 | if (n == NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11973 | if (!top || numeof >= 50) |
| 11974 | break; |
| 11975 | if (!stoppedjobs()) { |
| 11976 | if (!Iflag) |
| 11977 | break; |
| 11978 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 11979 | } |
| 11980 | numeof++; |
| 11981 | } else if (nflag == 0) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 11982 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 11983 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11984 | numeof = 0; |
| 11985 | evaltree(n, 0); |
| 11986 | } |
| 11987 | popstackmark(&smark); |
| 11988 | skip = evalskip; |
| 11989 | |
| 11990 | if (skip) { |
| 11991 | evalskip = 0; |
| 11992 | return skip & SKIPEVAL; |
| 11993 | } |
| 11994 | } |
| 11995 | return 0; |
| 11996 | } |
| 11997 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11998 | /* |
| 11999 | * Take commands from a file. To be compatible we should do a path |
| 12000 | * search for the file, which is necessary to find sub-commands. |
| 12001 | */ |
| 12002 | static char * |
| 12003 | find_dot_file(char *name) |
| 12004 | { |
| 12005 | char *fullname; |
| 12006 | const char *path = pathval(); |
| 12007 | struct stat statb; |
| 12008 | |
| 12009 | /* don't try this for absolute or relative paths */ |
| 12010 | if (strchr(name, '/')) |
| 12011 | return name; |
| 12012 | |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12013 | /* IIRC standards do not say whether . is to be searched. |
| 12014 | * And it is even smaller this way, making it unconditional for now: |
| 12015 | */ |
| 12016 | if (1) { /* ENABLE_ASH_BASH_COMPAT */ |
| 12017 | fullname = name; |
| 12018 | goto try_cur_dir; |
| 12019 | } |
| 12020 | |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12021 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12022 | try_cur_dir: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12023 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 12024 | /* |
| 12025 | * Don't bother freeing here, since it will |
| 12026 | * be freed by the caller. |
| 12027 | */ |
| 12028 | return fullname; |
| 12029 | } |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12030 | if (fullname != name) |
| 12031 | stunalloc(fullname); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12032 | } |
| 12033 | |
| 12034 | /* not found in the PATH */ |
| 12035 | ash_msg_and_raise_error("%s: not found", name); |
| 12036 | /* NOTREACHED */ |
| 12037 | } |
| 12038 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12039 | static int FAST_FUNC |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12040 | dotcmd(int argc, char **argv) |
| 12041 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12042 | char *fullname; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12043 | struct strlist *sp; |
| 12044 | volatile struct shparam saveparam; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12045 | |
| 12046 | for (sp = cmdenviron; sp; sp = sp->next) |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 12047 | setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12048 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12049 | if (!argv[1]) { |
| 12050 | /* bash says: "bash: .: filename argument required" */ |
| 12051 | return 2; /* bash compat */ |
| 12052 | } |
| 12053 | |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12054 | /* "false; . empty_file; echo $?" should print 0, not 1: */ |
| 12055 | exitstatus = 0; |
| 12056 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12057 | fullname = find_dot_file(argv[1]); |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12058 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12059 | argv += 2; |
| 12060 | argc -= 2; |
| 12061 | if (argc) { /* argc > 0, argv[0] != NULL */ |
| 12062 | saveparam = shellparam; |
| 12063 | shellparam.malloced = 0; |
| 12064 | shellparam.nparam = argc; |
| 12065 | shellparam.p = argv; |
| 12066 | }; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12067 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12068 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 12069 | commandname = fullname; |
| 12070 | cmdloop(0); |
| 12071 | popfile(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12072 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12073 | if (argc) { |
| 12074 | freeparam(&shellparam); |
| 12075 | shellparam = saveparam; |
| 12076 | }; |
| 12077 | |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12078 | return exitstatus; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12079 | } |
| 12080 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12081 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12082 | exitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12083 | { |
| 12084 | if (stoppedjobs()) |
| 12085 | return 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12086 | if (argv[1]) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12087 | exitstatus = number(argv[1]); |
| 12088 | raise_exception(EXEXIT); |
| 12089 | /* NOTREACHED */ |
| 12090 | } |
| 12091 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12092 | /* |
| 12093 | * Read a file containing shell functions. |
| 12094 | */ |
| 12095 | static void |
| 12096 | readcmdfile(char *name) |
| 12097 | { |
| 12098 | setinputfile(name, INPUT_PUSH_FILE); |
| 12099 | cmdloop(0); |
| 12100 | popfile(); |
| 12101 | } |
| 12102 | |
| 12103 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12104 | /* ============ find_command inplementation */ |
| 12105 | |
| 12106 | /* |
| 12107 | * Resolve a command name. If you change this routine, you may have to |
| 12108 | * change the shellexec routine as well. |
| 12109 | */ |
| 12110 | static void |
| 12111 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 12112 | { |
| 12113 | struct tblentry *cmdp; |
| 12114 | int idx; |
| 12115 | int prev; |
| 12116 | char *fullname; |
| 12117 | struct stat statb; |
| 12118 | int e; |
| 12119 | int updatetbl; |
| 12120 | struct builtincmd *bcmd; |
| 12121 | |
| 12122 | /* If name contains a slash, don't use PATH or hash table */ |
| 12123 | if (strchr(name, '/') != NULL) { |
| 12124 | entry->u.index = -1; |
| 12125 | if (act & DO_ABS) { |
| 12126 | while (stat(name, &statb) < 0) { |
| 12127 | #ifdef SYSV |
| 12128 | if (errno == EINTR) |
| 12129 | continue; |
| 12130 | #endif |
| 12131 | entry->cmdtype = CMDUNKNOWN; |
| 12132 | return; |
| 12133 | } |
| 12134 | } |
| 12135 | entry->cmdtype = CMDNORMAL; |
| 12136 | return; |
| 12137 | } |
| 12138 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12139 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12140 | |
| 12141 | updatetbl = (path == pathval()); |
| 12142 | if (!updatetbl) { |
| 12143 | act |= DO_ALTPATH; |
| 12144 | if (strstr(path, "%builtin") != NULL) |
| 12145 | act |= DO_ALTBLTIN; |
| 12146 | } |
| 12147 | |
| 12148 | /* If name is in the table, check answer will be ok */ |
| 12149 | cmdp = cmdlookup(name, 0); |
| 12150 | if (cmdp != NULL) { |
| 12151 | int bit; |
| 12152 | |
| 12153 | switch (cmdp->cmdtype) { |
| 12154 | default: |
| 12155 | #if DEBUG |
| 12156 | abort(); |
| 12157 | #endif |
| 12158 | case CMDNORMAL: |
| 12159 | bit = DO_ALTPATH; |
| 12160 | break; |
| 12161 | case CMDFUNCTION: |
| 12162 | bit = DO_NOFUNC; |
| 12163 | break; |
| 12164 | case CMDBUILTIN: |
| 12165 | bit = DO_ALTBLTIN; |
| 12166 | break; |
| 12167 | } |
| 12168 | if (act & bit) { |
| 12169 | updatetbl = 0; |
| 12170 | cmdp = NULL; |
| 12171 | } else if (cmdp->rehash == 0) |
| 12172 | /* if not invalidated by cd, we're done */ |
| 12173 | goto success; |
| 12174 | } |
| 12175 | |
| 12176 | /* If %builtin not in path, check for builtin next */ |
| 12177 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12178 | if (bcmd) { |
| 12179 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 12180 | goto builtin_success; |
| 12181 | if (act & DO_ALTPATH) { |
| 12182 | if (!(act & DO_ALTBLTIN)) |
| 12183 | goto builtin_success; |
| 12184 | } else if (builtinloc <= 0) { |
| 12185 | goto builtin_success; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 12186 | } |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12187 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12188 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12189 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 12190 | { |
| 12191 | int applet_no = find_applet_by_name(name); |
| 12192 | if (applet_no >= 0) { |
| 12193 | entry->cmdtype = CMDNORMAL; |
| 12194 | entry->u.index = -2 - applet_no; |
| 12195 | return; |
| 12196 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12197 | } |
| 12198 | #endif |
| 12199 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12200 | /* We have to search path. */ |
| 12201 | prev = -1; /* where to start */ |
| 12202 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 12203 | if (cmdp->cmdtype == CMDBUILTIN) |
| 12204 | prev = builtinloc; |
| 12205 | else |
| 12206 | prev = cmdp->param.index; |
| 12207 | } |
| 12208 | |
| 12209 | e = ENOENT; |
| 12210 | idx = -1; |
| 12211 | loop: |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12212 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12213 | stunalloc(fullname); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12214 | /* NB: code below will still use fullname |
| 12215 | * despite it being "unallocated" */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12216 | idx++; |
| 12217 | if (pathopt) { |
| 12218 | if (prefix(pathopt, "builtin")) { |
| 12219 | if (bcmd) |
| 12220 | goto builtin_success; |
| 12221 | continue; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 12222 | } |
| 12223 | if ((act & DO_NOFUNC) |
| 12224 | || !prefix(pathopt, "func") |
| 12225 | ) { /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12226 | continue; |
| 12227 | } |
| 12228 | } |
| 12229 | /* if rehash, don't redo absolute path names */ |
| 12230 | if (fullname[0] == '/' && idx <= prev) { |
| 12231 | if (idx < prev) |
| 12232 | continue; |
| 12233 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 12234 | goto success; |
| 12235 | } |
| 12236 | while (stat(fullname, &statb) < 0) { |
| 12237 | #ifdef SYSV |
| 12238 | if (errno == EINTR) |
| 12239 | continue; |
| 12240 | #endif |
| 12241 | if (errno != ENOENT && errno != ENOTDIR) |
| 12242 | e = errno; |
| 12243 | goto loop; |
| 12244 | } |
| 12245 | e = EACCES; /* if we fail, this will be the error */ |
| 12246 | if (!S_ISREG(statb.st_mode)) |
| 12247 | continue; |
| 12248 | if (pathopt) { /* this is a %func directory */ |
| 12249 | stalloc(strlen(fullname) + 1); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12250 | /* NB: stalloc will return space pointed by fullname |
| 12251 | * (because we don't have any intervening allocations |
| 12252 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12253 | readcmdfile(fullname); |
| 12254 | cmdp = cmdlookup(name, 0); |
| 12255 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 12256 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 12257 | stunalloc(fullname); |
| 12258 | goto success; |
| 12259 | } |
| 12260 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 12261 | if (!updatetbl) { |
| 12262 | entry->cmdtype = CMDNORMAL; |
| 12263 | entry->u.index = idx; |
| 12264 | return; |
| 12265 | } |
| 12266 | INT_OFF; |
| 12267 | cmdp = cmdlookup(name, 1); |
| 12268 | cmdp->cmdtype = CMDNORMAL; |
| 12269 | cmdp->param.index = idx; |
| 12270 | INT_ON; |
| 12271 | goto success; |
| 12272 | } |
| 12273 | |
| 12274 | /* We failed. If there was an entry for this command, delete it */ |
| 12275 | if (cmdp && updatetbl) |
| 12276 | delete_cmd_entry(); |
| 12277 | if (act & DO_ERR) |
| 12278 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
| 12279 | entry->cmdtype = CMDUNKNOWN; |
| 12280 | return; |
| 12281 | |
| 12282 | builtin_success: |
| 12283 | if (!updatetbl) { |
| 12284 | entry->cmdtype = CMDBUILTIN; |
| 12285 | entry->u.cmd = bcmd; |
| 12286 | return; |
| 12287 | } |
| 12288 | INT_OFF; |
| 12289 | cmdp = cmdlookup(name, 1); |
| 12290 | cmdp->cmdtype = CMDBUILTIN; |
| 12291 | cmdp->param.cmd = bcmd; |
| 12292 | INT_ON; |
| 12293 | success: |
| 12294 | cmdp->rehash = 0; |
| 12295 | entry->cmdtype = cmdp->cmdtype; |
| 12296 | entry->u = cmdp->param; |
| 12297 | } |
| 12298 | |
| 12299 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12300 | /* ============ trap.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12301 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12302 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12303 | * The trap builtin. |
| 12304 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12305 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12306 | trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12307 | { |
| 12308 | char *action; |
| 12309 | char **ap; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12310 | int signo, exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12311 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12312 | nextopt(nullstr); |
| 12313 | ap = argptr; |
| 12314 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12315 | for (signo = 0; signo < NSIG; signo++) { |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12316 | char *tr = trap_ptr[signo]; |
| 12317 | if (tr) { |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12318 | /* note: bash adds "SIG", but only if invoked |
| 12319 | * as "bash". If called as "sh", or if set -o posix, |
| 12320 | * then it prints short signal names. |
| 12321 | * We are printing short names: */ |
| 12322 | out1fmt("trap -- %s %s\n", |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12323 | single_quote(tr), |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12324 | get_signame(signo)); |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12325 | /* trap_ptr != trap only if we are in special-cased `trap` code. |
| 12326 | * In this case, we will exit very soon, no need to free(). */ |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12327 | /* if (trap_ptr != trap && tp[0]) */ |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12328 | /* free(tr); */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12329 | } |
| 12330 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12331 | /* |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12332 | if (trap_ptr != trap) { |
| 12333 | free(trap_ptr); |
| 12334 | trap_ptr = trap; |
| 12335 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12336 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12337 | return 0; |
| 12338 | } |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12339 | |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12340 | action = NULL; |
| 12341 | if (ap[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12342 | action = *ap++; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12343 | exitcode = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12344 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12345 | signo = get_signum(*ap); |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12346 | if (signo < 0) { |
| 12347 | /* Mimic bash message exactly */ |
| 12348 | ash_msg("%s: invalid signal specification", *ap); |
| 12349 | exitcode = 1; |
| 12350 | goto next; |
| 12351 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12352 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12353 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 12354 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12355 | action = NULL; |
| 12356 | else |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12357 | action = ckstrdup(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12358 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 12359 | free(trap[signo]); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 12360 | if (action) |
| 12361 | may_have_traps = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12362 | trap[signo] = action; |
| 12363 | if (signo != 0) |
| 12364 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12365 | INT_ON; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12366 | next: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12367 | ap++; |
| 12368 | } |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12369 | return exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12370 | } |
| 12371 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12372 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12373 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12374 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 12375 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12376 | /* |
| 12377 | * Lists available builtins |
| 12378 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12379 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12380 | helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12381 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 12382 | unsigned col; |
| 12383 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12384 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 12385 | out1fmt( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 12386 | "Built-in commands:\n" |
| 12387 | "------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 12388 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12389 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 12390 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12391 | if (col > 60) { |
| 12392 | out1fmt("\n"); |
| 12393 | col = 0; |
| 12394 | } |
| 12395 | } |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 12396 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 12397 | { |
| 12398 | const char *a = applet_names; |
| 12399 | while (*a) { |
| 12400 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 12401 | if (col > 60) { |
| 12402 | out1fmt("\n"); |
| 12403 | col = 0; |
| 12404 | } |
| 12405 | a += strlen(a) + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12406 | } |
| 12407 | } |
| 12408 | #endif |
| 12409 | out1fmt("\n\n"); |
| 12410 | return EXIT_SUCCESS; |
| 12411 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12412 | #endif /* FEATURE_SH_EXTRA_QUIET */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12413 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12414 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12415 | * The export and readonly commands. |
| 12416 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12417 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12418 | exportcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12419 | { |
| 12420 | struct var *vp; |
| 12421 | char *name; |
| 12422 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12423 | char **aptr; |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 12424 | int flag = argv[0][0] == 'r' ? VREADONLY : VEXPORT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12425 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12426 | if (nextopt("p") != 'p') { |
| 12427 | aptr = argptr; |
| 12428 | name = *aptr; |
| 12429 | if (name) { |
| 12430 | do { |
| 12431 | p = strchr(name, '='); |
| 12432 | if (p != NULL) { |
| 12433 | p++; |
| 12434 | } else { |
| 12435 | vp = *findvar(hashvar(name), name); |
| 12436 | if (vp) { |
| 12437 | vp->flags |= flag; |
| 12438 | continue; |
| 12439 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12440 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12441 | setvar(name, p, flag); |
| 12442 | } while ((name = *++aptr) != NULL); |
| 12443 | return 0; |
| 12444 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12445 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12446 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12447 | return 0; |
| 12448 | } |
| 12449 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12450 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12451 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12452 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12453 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12454 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12455 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12456 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12457 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12458 | cmdp = cmdlookup(name, 0); |
| 12459 | if (cmdp!= NULL && cmdp->cmdtype == CMDFUNCTION) |
| 12460 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12461 | } |
| 12462 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12463 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12464 | * The unset builtin command. We unset the function before we unset the |
| 12465 | * variable to allow a function to be unset when there is a readonly variable |
| 12466 | * with the same name. |
| 12467 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12468 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12469 | unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12470 | { |
| 12471 | char **ap; |
| 12472 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12473 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12474 | int ret = 0; |
| 12475 | |
| 12476 | while ((i = nextopt("vf")) != '\0') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12477 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12478 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12479 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12480 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12481 | if (flag != 'f') { |
| 12482 | i = unsetvar(*ap); |
| 12483 | ret |= i; |
| 12484 | if (!(i & 2)) |
| 12485 | continue; |
| 12486 | } |
| 12487 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12488 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12489 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12490 | return ret & 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12491 | } |
| 12492 | |
| 12493 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12494 | /* setmode.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12495 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12496 | #include <sys/times.h> |
| 12497 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12498 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12499 | ' ', offsetof(struct tms, tms_utime), |
| 12500 | '\n', offsetof(struct tms, tms_stime), |
| 12501 | ' ', offsetof(struct tms, tms_cutime), |
| 12502 | '\n', offsetof(struct tms, tms_cstime), |
| 12503 | 0 |
| 12504 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12505 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12506 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12507 | timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12508 | { |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12509 | long clk_tck, s, t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12510 | const unsigned char *p; |
| 12511 | struct tms buf; |
| 12512 | |
| 12513 | clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12514 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12515 | |
| 12516 | p = timescmd_str; |
| 12517 | do { |
| 12518 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 12519 | s = t / clk_tck; |
| 12520 | out1fmt("%ldm%ld.%.3lds%c", |
| 12521 | s/60, s%60, |
| 12522 | ((t - s * clk_tck) * 1000) / clk_tck, |
| 12523 | p[0]); |
| 12524 | } while (*(p += 2)); |
| 12525 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12526 | return 0; |
| 12527 | } |
| 12528 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12529 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12530 | /* |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12531 | * The let builtin. partial stolen from GNU Bash, the Bourne Again SHell. |
| 12532 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12533 | * |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12534 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12535 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12536 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12537 | letcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12538 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12539 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12540 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12541 | argv++; |
| 12542 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12543 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12544 | do { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12545 | i = ash_arith(*argv); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12546 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12547 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 12548 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12549 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12550 | #endif /* SH_MATH_SUPPORT */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12551 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12552 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12553 | /* ============ miscbltin.c |
| 12554 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12555 | * Miscellaneous builtins. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12556 | */ |
| 12557 | |
| 12558 | #undef rflag |
| 12559 | |
Denis Vlasenko | 83e5d6f | 2006-12-18 21:49:06 +0000 | [diff] [blame] | 12560 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 1 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12561 | typedef enum __rlimit_resource rlim_t; |
| 12562 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12563 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12564 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12565 | * The read builtin. Options: |
| 12566 | * -r Do not interpret '\' specially |
| 12567 | * -s Turn off echo (tty only) |
| 12568 | * -n NCHARS Read NCHARS max |
| 12569 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 12570 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 12571 | * -u FD Read from given FD instead of fd 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12572 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12573 | * TODO: bash also has: |
| 12574 | * -a ARRAY Read into array[0],[1],etc |
| 12575 | * -d DELIM End on DELIM char, not newline |
| 12576 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12577 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12578 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12579 | readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12580 | { |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12581 | char *opt_n = NULL; |
| 12582 | char *opt_p = NULL; |
| 12583 | char *opt_t = NULL; |
| 12584 | char *opt_u = NULL; |
| 12585 | int read_flags = 0; |
| 12586 | const char *r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12587 | int i; |
| 12588 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12589 | while ((i = nextopt("p:u:rt:n:s")) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 12590 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12591 | case 'p': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12592 | opt_p = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12593 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12594 | case 'n': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12595 | opt_n = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12596 | break; |
| 12597 | case 's': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12598 | read_flags |= BUILTIN_READ_SILENT; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12599 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12600 | case 't': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12601 | opt_t = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12602 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12603 | case 'r': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12604 | read_flags |= BUILTIN_READ_RAW; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12605 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12606 | case 'u': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12607 | opt_u = optionarg; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12608 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12609 | default: |
| 12610 | break; |
| 12611 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12612 | } |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12613 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 12614 | r = shell_builtin_read(setvar2, |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12615 | argptr, |
| 12616 | bltinlookup("IFS"), /* can be NULL */ |
| 12617 | read_flags, |
| 12618 | opt_n, |
| 12619 | opt_p, |
| 12620 | opt_t, |
| 12621 | opt_u |
| 12622 | ); |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12623 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12624 | if ((uintptr_t)r > 1) |
| 12625 | ash_msg_and_raise_error(r); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12626 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12627 | return (uintptr_t)r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12628 | } |
| 12629 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12630 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12631 | umaskcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12632 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12633 | static const char permuser[3] ALIGN1 = "ugo"; |
| 12634 | static const char permmode[3] ALIGN1 = "rwx"; |
| 12635 | static const short permmask[] ALIGN2 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12636 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 12637 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 12638 | S_IROTH, S_IWOTH, S_IXOTH |
| 12639 | }; |
| 12640 | |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 12641 | /* TODO: use bb_parse_mode() instead */ |
| 12642 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12643 | char *ap; |
| 12644 | mode_t mask; |
| 12645 | int i; |
| 12646 | int symbolic_mode = 0; |
| 12647 | |
| 12648 | while (nextopt("S") != '\0') { |
| 12649 | symbolic_mode = 1; |
| 12650 | } |
| 12651 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12652 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12653 | mask = umask(0); |
| 12654 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12655 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12656 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12657 | ap = *argptr; |
| 12658 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12659 | if (symbolic_mode) { |
| 12660 | char buf[18]; |
| 12661 | char *p = buf; |
| 12662 | |
| 12663 | for (i = 0; i < 3; i++) { |
| 12664 | int j; |
| 12665 | |
| 12666 | *p++ = permuser[i]; |
| 12667 | *p++ = '='; |
| 12668 | for (j = 0; j < 3; j++) { |
| 12669 | if ((mask & permmask[3 * i + j]) == 0) { |
| 12670 | *p++ = permmode[j]; |
| 12671 | } |
| 12672 | } |
| 12673 | *p++ = ','; |
| 12674 | } |
| 12675 | *--p = 0; |
| 12676 | puts(buf); |
| 12677 | } else { |
| 12678 | out1fmt("%.4o\n", mask); |
| 12679 | } |
| 12680 | } else { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12681 | if (isdigit((unsigned char) *ap)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12682 | mask = 0; |
| 12683 | do { |
| 12684 | if (*ap >= '8' || *ap < '0') |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 12685 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12686 | mask = (mask << 3) + (*ap - '0'); |
| 12687 | } while (*++ap != '\0'); |
| 12688 | umask(mask); |
| 12689 | } else { |
| 12690 | mask = ~mask & 0777; |
| 12691 | if (!bb_parse_mode(ap, &mask)) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 12692 | ash_msg_and_raise_error("illegal mode: %s", ap); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12693 | } |
| 12694 | umask(~mask & 0777); |
| 12695 | } |
| 12696 | } |
| 12697 | return 0; |
| 12698 | } |
| 12699 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12700 | static int FAST_FUNC |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 12701 | ulimitcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12702 | { |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 12703 | return shell_builtin_ulimit(argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12704 | } |
| 12705 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12706 | /* ============ main() and helpers */ |
| 12707 | |
| 12708 | /* |
| 12709 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12710 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12711 | static void exitshell(void) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12712 | static void |
| 12713 | exitshell(void) |
| 12714 | { |
| 12715 | struct jmploc loc; |
| 12716 | char *p; |
| 12717 | int status; |
| 12718 | |
| 12719 | status = exitstatus; |
| 12720 | TRACE(("pid %d, exitshell(%d)\n", getpid(), status)); |
| 12721 | if (setjmp(loc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12722 | if (exception_type == EXEXIT) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12723 | /* dash bug: it just does _exit(exitstatus) here |
| 12724 | * but we have to do setjobctl(0) first! |
| 12725 | * (bug is still not fixed in dash-0.5.3 - if you run dash |
| 12726 | * under Midnight Commander, on exit from dash MC is backgrounded) */ |
| 12727 | status = exitstatus; |
| 12728 | goto out; |
| 12729 | } |
| 12730 | exception_handler = &loc; |
| 12731 | p = trap[0]; |
| 12732 | if (p) { |
| 12733 | trap[0] = NULL; |
| 12734 | evalstring(p, 0); |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 12735 | free(p); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12736 | } |
| 12737 | flush_stdout_stderr(); |
| 12738 | out: |
| 12739 | setjobctl(0); |
| 12740 | _exit(status); |
| 12741 | /* NOTREACHED */ |
| 12742 | } |
| 12743 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12744 | static void |
| 12745 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12746 | { |
| 12747 | /* from input.c: */ |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 12748 | /* we will never free this */ |
| 12749 | basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12750 | |
| 12751 | /* from trap.c: */ |
| 12752 | signal(SIGCHLD, SIG_DFL); |
Denys Vlasenko | 7a7b034 | 2009-12-04 04:18:31 +0100 | [diff] [blame] | 12753 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 12754 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 12755 | */ |
| 12756 | signal(SIGHUP, SIG_DFL); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12757 | |
| 12758 | /* from var.c: */ |
| 12759 | { |
| 12760 | char **envp; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12761 | const char *p; |
| 12762 | struct stat st1, st2; |
| 12763 | |
| 12764 | initvar(); |
| 12765 | for (envp = environ; envp && *envp; envp++) { |
| 12766 | if (strchr(*envp, '=')) { |
| 12767 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 12768 | } |
| 12769 | } |
| 12770 | |
Denys Vlasenko | 7bb346f | 2009-10-06 22:09:50 +0200 | [diff] [blame] | 12771 | setvar("PPID", utoa(getppid()), 0); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12772 | |
| 12773 | p = lookupvar("PWD"); |
| 12774 | if (p) |
| 12775 | if (*p != '/' || stat(p, &st1) || stat(".", &st2) |
| 12776 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) |
| 12777 | p = '\0'; |
| 12778 | setpwd(p, 0); |
| 12779 | } |
| 12780 | } |
| 12781 | |
| 12782 | /* |
| 12783 | * Process the shell command line arguments. |
| 12784 | */ |
| 12785 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12786 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12787 | { |
| 12788 | int i; |
| 12789 | const char *xminusc; |
| 12790 | char **xargv; |
| 12791 | |
| 12792 | xargv = argv; |
| 12793 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12794 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12795 | xargv++; |
| 12796 | for (i = 0; i < NOPTS; i++) |
| 12797 | optlist[i] = 2; |
| 12798 | argptr = xargv; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 12799 | if (options(1)) { |
| 12800 | /* it already printed err message */ |
| 12801 | raise_exception(EXERROR); |
| 12802 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12803 | xargv = argptr; |
| 12804 | xminusc = minusc; |
| 12805 | if (*xargv == NULL) { |
| 12806 | if (xminusc) |
| 12807 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 12808 | sflag = 1; |
| 12809 | } |
| 12810 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 12811 | iflag = 1; |
| 12812 | if (mflag == 2) |
| 12813 | mflag = iflag; |
| 12814 | for (i = 0; i < NOPTS; i++) |
| 12815 | if (optlist[i] == 2) |
| 12816 | optlist[i] = 0; |
| 12817 | #if DEBUG == 2 |
| 12818 | debug = 1; |
| 12819 | #endif |
| 12820 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 12821 | if (xminusc) { |
| 12822 | minusc = *xargv++; |
| 12823 | if (*xargv) |
| 12824 | goto setarg0; |
| 12825 | } else if (!sflag) { |
| 12826 | setinputfile(*xargv, 0); |
| 12827 | setarg0: |
| 12828 | arg0 = *xargv++; |
| 12829 | commandname = arg0; |
| 12830 | } |
| 12831 | |
| 12832 | shellparam.p = xargv; |
| 12833 | #if ENABLE_ASH_GETOPTS |
| 12834 | shellparam.optind = 1; |
| 12835 | shellparam.optoff = -1; |
| 12836 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12837 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12838 | while (*xargv) { |
| 12839 | shellparam.nparam++; |
| 12840 | xargv++; |
| 12841 | } |
| 12842 | optschanged(); |
| 12843 | } |
| 12844 | |
| 12845 | /* |
| 12846 | * Read /etc/profile or .profile. |
| 12847 | */ |
| 12848 | static void |
| 12849 | read_profile(const char *name) |
| 12850 | { |
| 12851 | int skip; |
| 12852 | |
| 12853 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 12854 | return; |
| 12855 | skip = cmdloop(0); |
| 12856 | popfile(); |
| 12857 | if (skip) |
| 12858 | exitshell(); |
| 12859 | } |
| 12860 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12861 | /* |
| 12862 | * This routine is called when an error or an interrupt occurs in an |
| 12863 | * interactive shell and control is returned to the main command loop. |
| 12864 | */ |
| 12865 | static void |
| 12866 | reset(void) |
| 12867 | { |
| 12868 | /* from eval.c: */ |
| 12869 | evalskip = 0; |
| 12870 | loopnest = 0; |
| 12871 | /* from input.c: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 12872 | g_parsefile->left_in_buffer = 0; |
| 12873 | g_parsefile->left_in_line = 0; /* clear input buffer */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12874 | popallfiles(); |
| 12875 | /* from parser.c: */ |
| 12876 | tokpushback = 0; |
| 12877 | checkkwd = 0; |
| 12878 | /* from redir.c: */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 12879 | clearredir(/*drop:*/ 0); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12880 | } |
| 12881 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12882 | #if PROFILE |
| 12883 | static short profile_buf[16384]; |
| 12884 | extern int etext(); |
| 12885 | #endif |
| 12886 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12887 | /* |
| 12888 | * Main routine. We initialize things, parse the arguments, execute |
| 12889 | * profiles if we're a login shell, and then call cmdloop to execute |
| 12890 | * commands. The setjmp call sets up the location to jump to when an |
| 12891 | * exception occurs. When an exception occurs the variable "state" |
| 12892 | * is used to figure out how far we had gotten. |
| 12893 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 12894 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12895 | int ash_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12896 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12897 | const char *shinit; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 12898 | volatile smallint state; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12899 | struct jmploc jmploc; |
| 12900 | struct stackmark smark; |
| 12901 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12902 | /* Initialize global data */ |
| 12903 | INIT_G_misc(); |
| 12904 | INIT_G_memstack(); |
| 12905 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 12906 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12907 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 12908 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12909 | INIT_G_cmdtable(); |
| 12910 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12911 | #if PROFILE |
| 12912 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 12913 | #endif |
| 12914 | |
| 12915 | #if ENABLE_FEATURE_EDITING |
| 12916 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
| 12917 | #endif |
| 12918 | state = 0; |
| 12919 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12920 | smallint e; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 12921 | smallint s; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12922 | |
| 12923 | reset(); |
| 12924 | |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12925 | e = exception_type; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12926 | if (e == EXERROR) |
| 12927 | exitstatus = 2; |
| 12928 | s = state; |
| 12929 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) |
| 12930 | exitshell(); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12931 | if (e == EXINT) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12932 | outcslow('\n', stderr); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12933 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12934 | popstackmark(&smark); |
| 12935 | FORCE_INT_ON; /* enable interrupts */ |
| 12936 | if (s == 1) |
| 12937 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 12938 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12939 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 12940 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12941 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 12942 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12943 | } |
| 12944 | exception_handler = &jmploc; |
| 12945 | #if DEBUG |
| 12946 | opentrace(); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 12947 | TRACE(("Shell args: ")); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 12948 | trace_puts_args(argv); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12949 | #endif |
| 12950 | rootpid = getpid(); |
| 12951 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12952 | init(); |
| 12953 | setstackmark(&smark); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12954 | procargs(argv); |
| 12955 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12956 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
| 12957 | if (iflag) { |
| 12958 | const char *hp = lookupvar("HISTFILE"); |
| 12959 | |
| 12960 | if (hp == NULL) { |
| 12961 | hp = lookupvar("HOME"); |
| 12962 | if (hp != NULL) { |
| 12963 | char *defhp = concat_path_file(hp, ".ash_history"); |
| 12964 | setvar("HISTFILE", defhp, 0); |
| 12965 | free(defhp); |
| 12966 | } |
| 12967 | } |
| 12968 | } |
| 12969 | #endif |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 12970 | if (/* argv[0] && */ argv[0][0] == '-') |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12971 | isloginsh = 1; |
| 12972 | if (isloginsh) { |
| 12973 | state = 1; |
| 12974 | read_profile("/etc/profile"); |
| 12975 | state1: |
| 12976 | state = 2; |
| 12977 | read_profile(".profile"); |
| 12978 | } |
| 12979 | state2: |
| 12980 | state = 3; |
| 12981 | if ( |
| 12982 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12983 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12984 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12985 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12986 | ) { |
| 12987 | shinit = lookupvar("ENV"); |
| 12988 | if (shinit != NULL && *shinit != '\0') { |
| 12989 | read_profile(shinit); |
| 12990 | } |
| 12991 | } |
| 12992 | state3: |
| 12993 | state = 4; |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 12994 | if (minusc) { |
| 12995 | /* evalstring pushes parsefile stack. |
| 12996 | * Ensure we don't falsely claim that 0 (stdin) |
Denis Vlasenko | 5368ad5 | 2009-03-20 10:20:08 +0000 | [diff] [blame] | 12997 | * is one of stacked source fds. |
| 12998 | * Testcase: ash -c 'exec 1>&0' must not complain. */ |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 12999 | if (!sflag) |
| 13000 | g_parsefile->fd = -1; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13001 | evalstring(minusc, 0); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13002 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13003 | |
| 13004 | if (sflag || minusc == NULL) { |
Denys Vlasenko | 0337e03 | 2009-11-29 00:12:30 +0100 | [diff] [blame] | 13005 | #if defined MAX_HISTORY && MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 13006 | if (iflag) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13007 | const char *hp = lookupvar("HISTFILE"); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13008 | if (hp) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13009 | line_input_state->hist_file = hp; |
| 13010 | } |
| 13011 | #endif |
| 13012 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 13013 | cmdloop(1); |
| 13014 | } |
| 13015 | #if PROFILE |
| 13016 | monitor(0); |
| 13017 | #endif |
| 13018 | #ifdef GPROF |
| 13019 | { |
| 13020 | extern void _mcleanup(void); |
| 13021 | _mcleanup(); |
| 13022 | } |
| 13023 | #endif |
| 13024 | exitshell(); |
| 13025 | /* NOTREACHED */ |
| 13026 | } |
| 13027 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13028 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13029 | /*- |
| 13030 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 13031 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13032 | * |
| 13033 | * This code is derived from software contributed to Berkeley by |
| 13034 | * Kenneth Almquist. |
| 13035 | * |
| 13036 | * Redistribution and use in source and binary forms, with or without |
| 13037 | * modification, are permitted provided that the following conditions |
| 13038 | * are met: |
| 13039 | * 1. Redistributions of source code must retain the above copyright |
| 13040 | * notice, this list of conditions and the following disclaimer. |
| 13041 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13042 | * notice, this list of conditions and the following disclaimer in the |
| 13043 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13044 | * 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] | 13045 | * may be used to endorse or promote products derived from this software |
| 13046 | * without specific prior written permission. |
| 13047 | * |
| 13048 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 13049 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13050 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 13051 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13052 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13053 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 13054 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13055 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 13056 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 13057 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 13058 | * SUCH DAMAGE. |
| 13059 | */ |