Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * ash shell port for busybox |
| 4 | * |
| 5 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7 | * |
| 8 | * This code is derived from software contributed to Berkeley by |
| 9 | * Kenneth Almquist. |
| 10 | * |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 15 | * |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | * |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 25 | * This version of ash is adapted from the source in Debian's ash 0.3.8-5 |
| 26 | * package. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 27 | * |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 28 | * Modified by Erik Andersen <andersee@debian.org> and |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 29 | * Vladimir Oleynik <dzo@simtreas.ru> to be used in busybox |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 30 | * |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 31 | * |
| 32 | * Original copyright notice is retained at the end of this file. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 33 | */ |
| 34 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 35 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 36 | /* Enable this to compile in extra debugging noise. When debugging is |
| 37 | * on, debugging info will be written to $HOME/trace and a quit signal |
| 38 | * will generate a core dump. */ |
| 39 | #undef DEBUG |
| 40 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 41 | /* These are here to work with glibc -- Don't change these... */ |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 42 | #undef FNMATCH_BROKEN |
| 43 | #undef GLOB_BROKEN |
Eric Andersen | 5bb1677 | 2001-09-06 18:00:41 +0000 | [diff] [blame] | 44 | #define IFS_BROKEN |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 45 | |
| 46 | #include <assert.h> |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 47 | #include <stddef.h> |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 48 | #include <ctype.h> |
| 49 | #include <dirent.h> |
| 50 | #include <errno.h> |
| 51 | #include <fcntl.h> |
| 52 | #include <limits.h> |
| 53 | #include <paths.h> |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 54 | #include <setjmp.h> |
| 55 | #include <signal.h> |
| 56 | #include <stdarg.h> |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 57 | #include <stdio.h> |
| 58 | #include <stdlib.h> |
| 59 | #include <string.h> |
| 60 | #include <sysexits.h> |
| 61 | #include <unistd.h> |
| 62 | #include <sys/stat.h> |
| 63 | #include <sys/cdefs.h> |
| 64 | #include <sys/ioctl.h> |
| 65 | #include <sys/param.h> |
| 66 | #include <sys/resource.h> |
| 67 | #include <sys/time.h> |
| 68 | #include <sys/times.h> |
| 69 | #include <sys/types.h> |
| 70 | #include <sys/wait.h> |
Robert Griebl | ea1a63a | 2002-06-04 20:10:23 +0000 | [diff] [blame] | 71 | #include "busybox.h" |
Eric Andersen | 887ca79 | 2002-07-03 23:19:26 +0000 | [diff] [blame] | 72 | #include "pwd_.h" |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | #if !defined(FNMATCH_BROKEN) |
| 76 | #include <fnmatch.h> |
| 77 | #endif |
| 78 | #if !defined(GLOB_BROKEN) |
| 79 | #include <glob.h> |
| 80 | #endif |
| 81 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 82 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 83 | #include <termios.h> |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 86 | #include "cmdedit.h" |
| 87 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 88 | /* |
| 89 | * This file was generated by the mksyntax program. |
| 90 | */ |
| 91 | |
| 92 | /* Syntax classes */ |
| 93 | #define CWORD 0 /* character is nothing special */ |
| 94 | #define CNL 1 /* newline character */ |
| 95 | #define CBACK 2 /* a backslash character */ |
| 96 | #define CSQUOTE 3 /* single quote */ |
| 97 | #define CDQUOTE 4 /* double quote */ |
| 98 | #define CENDQUOTE 5 /* a terminating quote */ |
| 99 | #define CBQUOTE 6 /* backwards single quote */ |
| 100 | #define CVAR 7 /* a dollar sign */ |
| 101 | #define CENDVAR 8 /* a '}' character */ |
| 102 | #define CLP 9 /* a left paren in arithmetic */ |
| 103 | #define CRP 10 /* a right paren in arithmetic */ |
| 104 | #define CENDFILE 11 /* end of file */ |
| 105 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 106 | #define CSPCL 13 /* these terminate a word */ |
| 107 | #define CIGN 14 /* character should be ignored */ |
| 108 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 109 | #define SYNBASE 130 |
| 110 | #define PEOF -130 |
| 111 | |
| 112 | #define PEOA -129 |
| 113 | |
| 114 | #define TEOF 0 |
| 115 | #define TNL 1 |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 116 | #define TREDIR 2 |
| 117 | #define TWORD 3 |
| 118 | #define TASSIGN 4 |
| 119 | #define TSEMI 5 |
| 120 | #define TBACKGND 6 |
| 121 | #define TAND 7 |
| 122 | #define TOR 8 |
| 123 | #define TPIPE 9 |
| 124 | #define TLP 10 |
| 125 | #define TRP 11 |
| 126 | #define TENDCASE 12 |
| 127 | #define TENDBQUOTE 13 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 128 | #define TNOT 14 |
| 129 | #define TCASE 15 |
| 130 | #define TDO 16 |
| 131 | #define TDONE 17 |
| 132 | #define TELIF 18 |
| 133 | #define TELSE 19 |
| 134 | #define TESAC 20 |
| 135 | #define TFI 21 |
| 136 | #define TFOR 22 |
| 137 | #define TIF 23 |
| 138 | #define TIN 24 |
| 139 | #define TTHEN 25 |
| 140 | #define TUNTIL 26 |
| 141 | #define TWHILE 27 |
| 142 | #define TBEGIN 28 |
| 143 | #define TEND 29 |
| 144 | |
| 145 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 146 | |
| 147 | /* control characters in argument strings */ |
| 148 | #define CTLESC '\201' |
| 149 | #define CTLVAR '\202' |
| 150 | #define CTLENDVAR '\203' |
| 151 | #define CTLBACKQ '\204' |
| 152 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ |
| 153 | /* CTLBACKQ | CTLQUOTE == '\205' */ |
| 154 | #define CTLARI '\206' |
| 155 | #define CTLENDARI '\207' |
| 156 | #define CTLQUOTEMARK '\210' |
| 157 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 158 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 159 | #define is_digit(c) ((c)>='0' && (c)<='9') |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 160 | #define is_name(c) (((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalpha((unsigned char) (c)))) |
| 161 | #define is_in_name(c) (((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalnum((unsigned char) (c)))) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 165 | * (assuming ascii char codes, as the original implementation did) |
| 166 | */ |
| 167 | #define is_special(c) \ |
| 168 | ( (((unsigned int)c) - 33 < 32) \ |
| 169 | && ((0xc1ff920dUL >> (((unsigned int)c) - 33)) & 1)) |
| 170 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 171 | #define digit_val(c) ((c) - '0') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 172 | |
| 173 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 174 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 175 | #define S_CATCH 2 /* signal is caught */ |
| 176 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
| 177 | #define S_HARD_IGN 4 /* signal is ignored permenantly */ |
| 178 | #define S_RESET 5 /* temporary - to reset a hard ignored sig */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 179 | |
| 180 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 181 | /* variable substitution byte (follows CTLVAR) */ |
| 182 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 183 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
| 184 | #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 185 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 186 | /* values of VSTYPE field */ |
| 187 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 188 | #define VSMINUS 0x2 /* ${var-text} */ |
| 189 | #define VSPLUS 0x3 /* ${var+text} */ |
| 190 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 191 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 192 | #define VSTRIMLEFT 0x6 /* ${var#pattern} */ |
| 193 | #define VSTRIMLEFTMAX 0x7 /* ${var##pattern} */ |
| 194 | #define VSTRIMRIGHT 0x8 /* ${var%pattern} */ |
| 195 | #define VSTRIMRIGHTMAX 0x9 /* ${var%%pattern} */ |
| 196 | #define VSLENGTH 0xa /* ${#var} */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 197 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 198 | /* flags passed to redirect */ |
| 199 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 200 | #define REDIR_BACKQ 02 /* save the command output to pipe */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 201 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 202 | /* |
| 203 | * BSD setjmp saves the signal mask, which violates ANSI C and takes time, |
| 204 | * so we use _setjmp instead. |
| 205 | */ |
| 206 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 207 | #if defined(BSD) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 208 | #define setjmp(jmploc) _setjmp(jmploc) |
| 209 | #define longjmp(jmploc, val) _longjmp(jmploc, val) |
| 210 | #endif |
| 211 | |
| 212 | /* |
| 213 | * Most machines require the value returned from malloc to be aligned |
| 214 | * in some way. The following macro will get this right on many machines. |
| 215 | */ |
| 216 | |
| 217 | #ifndef ALIGN |
| 218 | union align { |
| 219 | int i; |
| 220 | char *cp; |
| 221 | }; |
| 222 | |
| 223 | #define ALIGN(nbytes) (((nbytes) + sizeof(union align) - 1) & ~(sizeof(union align) - 1)) |
| 224 | #endif |
| 225 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 226 | #ifdef CONFIG_LOCALE_SUPPORT |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 227 | #include <locale.h> |
| 228 | static void change_lc_all(const char *value); |
| 229 | static void change_lc_ctype(const char *value); |
| 230 | #endif |
| 231 | |
| 232 | /* |
| 233 | * These macros allow the user to suspend the handling of interrupt signals |
| 234 | * over a period of time. This is similar to SIGHOLD to or sigblock, but |
| 235 | * much more efficient and portable. (But hacking the kernel is so much |
| 236 | * more fun than worrying about efficiency and portability. :-)) |
| 237 | */ |
| 238 | |
| 239 | static void onint (void); |
| 240 | static volatile int suppressint; |
| 241 | static volatile int intpending; |
| 242 | |
| 243 | #define INTOFF suppressint++ |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 244 | #ifndef CONFIG_ASH_OPTIMIZE_FOR_SIZE |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 245 | #define INTON { if (--suppressint == 0 && intpending) onint(); } |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 246 | #define FORCEINTON {suppressint = 0; if (intpending) onint();} |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 247 | #else |
| 248 | static void __inton (void); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 249 | static void forceinton (void); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 250 | #define INTON __inton() |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 251 | #define FORCEINTON forceinton() |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 252 | #endif |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 253 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 254 | #define CLEAR_PENDING_INT intpending = 0 |
| 255 | #define int_pending() intpending |
| 256 | |
| 257 | |
| 258 | typedef void *pointer; |
| 259 | #ifndef NULL |
| 260 | #define NULL (void *)0 |
| 261 | #endif |
| 262 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 263 | static pointer stalloc (int); |
| 264 | static void stunalloc (pointer); |
| 265 | static void ungrabstackstr (char *, char *); |
| 266 | static char * growstackstr(void); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 267 | static char * makestrspace(size_t newlen); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 268 | static char *sstrdup (const char *); |
| 269 | |
| 270 | /* |
| 271 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 272 | * to make this more efficient, and also to avoid all sorts of exception |
| 273 | * handling code to handle interrupts in the middle of a parse. |
| 274 | * |
| 275 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 276 | * well. |
| 277 | */ |
| 278 | |
| 279 | #define MINSIZE 504 /* minimum size of a block */ |
| 280 | |
| 281 | |
| 282 | struct stack_block { |
| 283 | struct stack_block *prev; |
| 284 | char space[MINSIZE]; |
| 285 | }; |
| 286 | |
| 287 | static struct stack_block stackbase; |
| 288 | static struct stack_block *stackp = &stackbase; |
| 289 | static struct stackmark *markp; |
| 290 | static char *stacknxt = stackbase.space; |
| 291 | static int stacknleft = MINSIZE; |
| 292 | |
| 293 | |
| 294 | #define equal(s1, s2) (strcmp(s1, s2) == 0) |
| 295 | |
| 296 | #define stackblock() stacknxt |
| 297 | #define stackblocksize() stacknleft |
| 298 | #define STARTSTACKSTR(p) p = stackblock(), sstrnleft = stackblocksize() |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 299 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 300 | #define STPUTC(c, p) (--sstrnleft >= 0? (*p++ = (c)) : (p = growstackstr(), *p++ = (c))) |
| 301 | #define CHECKSTRSPACE(n, p) { if (sstrnleft < n) p = makestrspace(n); } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 302 | #define STACKSTRNUL(p) (sstrnleft == 0? (p = growstackstr(), *p = '\0') : (*p = '\0')) |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 303 | |
| 304 | |
| 305 | #define USTPUTC(c, p) (--sstrnleft, *p++ = (c)) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 306 | #define STUNPUTC(p) (++sstrnleft, --p) |
| 307 | #define STTOPC(p) p[-1] |
| 308 | #define STADJUST(amount, p) (p += (amount), sstrnleft -= (amount)) |
| 309 | #define grabstackstr(p) stalloc(stackblocksize() - sstrnleft) |
| 310 | |
| 311 | #define ckfree(p) free((pointer)(p)) |
| 312 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 313 | |
| 314 | #ifdef DEBUG |
| 315 | #define TRACE(param) trace param |
Eric Andersen | 69a20f0 | 2001-10-31 10:40:37 +0000 | [diff] [blame] | 316 | typedef union node unode; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 317 | static void trace (const char *, ...); |
| 318 | static void trargs (char **); |
Eric Andersen | 69a20f0 | 2001-10-31 10:40:37 +0000 | [diff] [blame] | 319 | static void showtree (unode *); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 320 | static void trputc (int); |
| 321 | static void trputs (const char *); |
| 322 | static void opentrace (void); |
| 323 | #else |
| 324 | #define TRACE(param) |
| 325 | #endif |
| 326 | |
| 327 | #define NSEMI 0 |
| 328 | #define NCMD 1 |
| 329 | #define NPIPE 2 |
| 330 | #define NREDIR 3 |
| 331 | #define NBACKGND 4 |
| 332 | #define NSUBSHELL 5 |
| 333 | #define NAND 6 |
| 334 | #define NOR 7 |
| 335 | #define NIF 8 |
| 336 | #define NWHILE 9 |
| 337 | #define NUNTIL 10 |
| 338 | #define NFOR 11 |
| 339 | #define NCASE 12 |
| 340 | #define NCLIST 13 |
| 341 | #define NDEFUN 14 |
| 342 | #define NARG 15 |
| 343 | #define NTO 16 |
| 344 | #define NFROM 17 |
| 345 | #define NFROMTO 18 |
| 346 | #define NAPPEND 19 |
| 347 | #define NTOOV 20 |
| 348 | #define NTOFD 21 |
| 349 | #define NFROMFD 22 |
| 350 | #define NHERE 23 |
| 351 | #define NXHERE 24 |
| 352 | #define NNOT 25 |
| 353 | |
| 354 | /* |
| 355 | * expandarg() flags |
| 356 | */ |
| 357 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 358 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 359 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 360 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 361 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
| 362 | #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */ |
| 363 | |
| 364 | |
| 365 | #define NOPTS 16 |
| 366 | |
| 367 | static char optet_vals[NOPTS]; |
| 368 | |
| 369 | static const char * const optlist[NOPTS] = { |
| 370 | "e" "errexit", |
| 371 | "f" "noglob", |
| 372 | "I" "ignoreeof", |
| 373 | "i" "interactive", |
| 374 | "m" "monitor", |
| 375 | "n" "noexec", |
| 376 | "s" "stdin", |
| 377 | "x" "xtrace", |
| 378 | "v" "verbose", |
| 379 | "V" "vi", |
| 380 | "E" "emacs", |
| 381 | "C" "noclobber", |
| 382 | "a" "allexport", |
| 383 | "b" "notify", |
| 384 | "u" "nounset", |
| 385 | "q" "quietprofile" |
| 386 | }; |
| 387 | |
| 388 | #define optent_name(optent) (optent+1) |
| 389 | #define optent_letter(optent) optent[0] |
| 390 | #define optent_val(optent) optet_vals[optent] |
| 391 | |
| 392 | #define eflag optent_val(0) |
| 393 | #define fflag optent_val(1) |
| 394 | #define Iflag optent_val(2) |
| 395 | #define iflag optent_val(3) |
| 396 | #define mflag optent_val(4) |
| 397 | #define nflag optent_val(5) |
| 398 | #define sflag optent_val(6) |
| 399 | #define xflag optent_val(7) |
| 400 | #define vflag optent_val(8) |
| 401 | #define Vflag optent_val(9) |
| 402 | #define Eflag optent_val(10) |
| 403 | #define Cflag optent_val(11) |
| 404 | #define aflag optent_val(12) |
| 405 | #define bflag optent_val(13) |
| 406 | #define uflag optent_val(14) |
| 407 | #define qflag optent_val(15) |
| 408 | |
| 409 | |
| 410 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
| 411 | #define FORK_FG 0 |
| 412 | #define FORK_BG 1 |
| 413 | #define FORK_NOJOB 2 |
| 414 | |
| 415 | |
| 416 | struct nbinary { |
| 417 | int type; |
| 418 | union node *ch1; |
| 419 | union node *ch2; |
| 420 | }; |
| 421 | |
| 422 | |
| 423 | struct ncmd { |
| 424 | int type; |
| 425 | int backgnd; |
| 426 | union node *assign; |
| 427 | union node *args; |
| 428 | union node *redirect; |
| 429 | }; |
| 430 | |
| 431 | |
| 432 | struct npipe { |
| 433 | int type; |
| 434 | int backgnd; |
| 435 | struct nodelist *cmdlist; |
| 436 | }; |
| 437 | |
| 438 | |
| 439 | struct nredir { |
| 440 | int type; |
| 441 | union node *n; |
| 442 | union node *redirect; |
| 443 | }; |
| 444 | |
| 445 | |
| 446 | struct nif { |
| 447 | int type; |
| 448 | union node *test; |
| 449 | union node *ifpart; |
| 450 | union node *elsepart; |
| 451 | }; |
| 452 | |
| 453 | |
| 454 | struct nfor { |
| 455 | int type; |
| 456 | union node *args; |
| 457 | union node *body; |
| 458 | char *var; |
| 459 | }; |
| 460 | |
| 461 | |
| 462 | struct ncase { |
| 463 | int type; |
| 464 | union node *expr; |
| 465 | union node *cases; |
| 466 | }; |
| 467 | |
| 468 | |
| 469 | struct nclist { |
| 470 | int type; |
| 471 | union node *next; |
| 472 | union node *pattern; |
| 473 | union node *body; |
| 474 | }; |
| 475 | |
| 476 | |
| 477 | struct narg { |
| 478 | int type; |
| 479 | union node *next; |
| 480 | char *text; |
| 481 | struct nodelist *backquote; |
| 482 | }; |
| 483 | |
| 484 | |
| 485 | struct nfile { |
| 486 | int type; |
| 487 | union node *next; |
| 488 | int fd; |
| 489 | union node *fname; |
| 490 | char *expfname; |
| 491 | }; |
| 492 | |
| 493 | |
| 494 | struct ndup { |
| 495 | int type; |
| 496 | union node *next; |
| 497 | int fd; |
| 498 | int dupfd; |
| 499 | union node *vname; |
| 500 | }; |
| 501 | |
| 502 | |
| 503 | struct nhere { |
| 504 | int type; |
| 505 | union node *next; |
| 506 | int fd; |
| 507 | union node *doc; |
| 508 | }; |
| 509 | |
| 510 | |
| 511 | struct nnot { |
| 512 | int type; |
| 513 | union node *com; |
| 514 | }; |
| 515 | |
| 516 | |
| 517 | union node { |
| 518 | int type; |
| 519 | struct nbinary nbinary; |
| 520 | struct ncmd ncmd; |
| 521 | struct npipe npipe; |
| 522 | struct nredir nredir; |
| 523 | struct nif nif; |
| 524 | struct nfor nfor; |
| 525 | struct ncase ncase; |
| 526 | struct nclist nclist; |
| 527 | struct narg narg; |
| 528 | struct nfile nfile; |
| 529 | struct ndup ndup; |
| 530 | struct nhere nhere; |
| 531 | struct nnot nnot; |
| 532 | }; |
| 533 | |
| 534 | |
| 535 | struct nodelist { |
| 536 | struct nodelist *next; |
| 537 | union node *n; |
| 538 | }; |
| 539 | |
| 540 | struct backcmd { /* result of evalbackcmd */ |
| 541 | int fd; /* file descriptor to read from */ |
| 542 | char *buf; /* buffer */ |
| 543 | int nleft; /* number of chars in buffer */ |
| 544 | struct job *jp; /* job structure for command */ |
| 545 | }; |
| 546 | |
| 547 | struct cmdentry { |
| 548 | int cmdtype; |
| 549 | union param { |
| 550 | int index; |
| 551 | union node *func; |
| 552 | const struct builtincmd *cmd; |
| 553 | } u; |
| 554 | }; |
| 555 | |
| 556 | struct strlist { |
| 557 | struct strlist *next; |
| 558 | char *text; |
| 559 | }; |
| 560 | |
| 561 | |
| 562 | struct arglist { |
| 563 | struct strlist *list; |
| 564 | struct strlist **lastp; |
| 565 | }; |
| 566 | |
| 567 | struct strpush { |
| 568 | struct strpush *prev; /* preceding string on stack */ |
| 569 | char *prevstring; |
| 570 | int prevnleft; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 571 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 572 | struct alias *ap; /* if push was associated with an alias */ |
| 573 | #endif |
| 574 | char *string; /* remember the string since it may change */ |
| 575 | }; |
| 576 | |
| 577 | struct parsefile { |
| 578 | struct parsefile *prev; /* preceding file on stack */ |
| 579 | int linno; /* current line */ |
| 580 | int fd; /* file descriptor (or -1 if string) */ |
| 581 | int nleft; /* number of chars left in this line */ |
| 582 | int lleft; /* number of chars left in this buffer */ |
| 583 | char *nextc; /* next char in buffer */ |
| 584 | char *buf; /* input buffer */ |
| 585 | struct strpush *strpush; /* for pushing strings at this level */ |
| 586 | struct strpush basestrpush; /* so pushing one is fast */ |
| 587 | }; |
| 588 | |
| 589 | struct stackmark { |
| 590 | struct stack_block *stackp; |
| 591 | char *stacknxt; |
| 592 | int stacknleft; |
| 593 | struct stackmark *marknext; |
| 594 | }; |
| 595 | |
| 596 | struct shparam { |
| 597 | int nparam; /* # of positional parameters (without $0) */ |
| 598 | unsigned char malloc; /* if parameter list dynamically allocated */ |
| 599 | char **p; /* parameter list */ |
| 600 | int optind; /* next parameter to be processed by getopts */ |
| 601 | int optoff; /* used by getopts */ |
| 602 | }; |
| 603 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 604 | /* |
| 605 | * When commands are first encountered, they are entered in a hash table. |
| 606 | * This ensures that a full path search will not have to be done for them |
| 607 | * on each invocation. |
| 608 | * |
| 609 | * We should investigate converting to a linear search, even though that |
| 610 | * would make the command name "hash" a misnomer. |
| 611 | */ |
| 612 | #define CMDTABLESIZE 31 /* should be prime */ |
| 613 | #define ARB 1 /* actual size determined at run time */ |
| 614 | |
| 615 | |
| 616 | |
| 617 | struct tblentry { |
| 618 | struct tblentry *next; /* next entry in hash chain */ |
| 619 | union param param; /* definition of builtin function */ |
| 620 | short cmdtype; /* index identifying command */ |
| 621 | char rehash; /* if set, cd done since entry created */ |
| 622 | char cmdname[ARB]; /* name of command */ |
| 623 | }; |
| 624 | |
| 625 | |
| 626 | static struct tblentry *cmdtable[CMDTABLESIZE]; |
| 627 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 628 | static int exerrno = 0; /* Last exec error */ |
| 629 | |
| 630 | |
| 631 | static void tryexec (char *, char **, char **); |
| 632 | static void printentry (struct tblentry *, int); |
| 633 | static void clearcmdentry (int); |
| 634 | static struct tblentry *cmdlookup (const char *, int); |
| 635 | static void delete_cmd_entry (void); |
| 636 | static int path_change (const char *, int *); |
| 637 | |
| 638 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 639 | static void flushall (void); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 640 | static void out2fmt (const char *, ...) |
| 641 | __attribute__((__format__(__printf__,1,2))); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 642 | static int xwrite (int, const char *, int); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 643 | |
Manuel Novoa III | c639a35 | 2001-08-12 17:32:56 +0000 | [diff] [blame] | 644 | static inline void outstr (const char *p, FILE *file) { fputs(p, file); } |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 645 | static void out1str(const char *p) { outstr(p, stdout); } |
| 646 | static void out2str(const char *p) { outstr(p, stderr); } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 647 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 648 | #ifndef CONFIG_ASH_OPTIMIZE_FOR_SIZE |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 649 | #define out2c(c) putc((c), stderr) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 650 | #else |
| 651 | static void out2c(int c) { putc(c, stderr); } |
| 652 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 653 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 654 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 655 | #ifdef CONFIG_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 656 | #define USE_SIT_FUNCTION |
| 657 | #endif |
| 658 | |
| 659 | /* number syntax index */ |
| 660 | #define BASESYNTAX 0 /* not in quotes */ |
| 661 | #define DQSYNTAX 1 /* in double quotes */ |
| 662 | #define SQSYNTAX 2 /* in single quotes */ |
| 663 | #define ARISYNTAX 3 /* in arithmetic */ |
| 664 | |
| 665 | static const char S_I_T[][4] = { |
| 666 | /* 0 */ { CSPCL, CIGN, CIGN, CIGN }, /* PEOA */ |
| 667 | /* 1 */ { CSPCL, CWORD, CWORD, CWORD }, /* ' ' */ |
| 668 | /* 2 */ { CNL, CNL, CNL, CNL }, /* \n */ |
| 669 | /* 3 */ { CWORD, CCTL, CCTL, CWORD }, /* !*-/:=?[]~ */ |
| 670 | /* 4 */ { CDQUOTE, CENDQUOTE, CWORD, CDQUOTE }, /* '"' */ |
| 671 | /* 5 */ { CVAR, CVAR, CWORD, CVAR }, /* $ */ |
| 672 | /* 6 */ { CSQUOTE, CWORD, CENDQUOTE, CSQUOTE }, /* "'" */ |
| 673 | /* 7 */ { CSPCL, CWORD, CWORD, CLP }, /* ( */ |
| 674 | /* 8 */ { CSPCL, CWORD, CWORD, CRP }, /* ) */ |
| 675 | /* 9 */ { CBACK, CBACK, CCTL, CBACK }, /* \ */ |
| 676 | /* 10 */ { CBQUOTE, CBQUOTE, CWORD, CBQUOTE }, /* ` */ |
| 677 | /* 11 */ { CENDVAR, CENDVAR, CWORD, CENDVAR }, /* } */ |
| 678 | #ifndef USE_SIT_FUNCTION |
| 679 | /* 12 */ { CENDFILE, CENDFILE, CENDFILE, CENDFILE }, /* PEOF */ |
| 680 | /* 13 */ { CWORD, CWORD, CWORD, CWORD }, /* 0-9A-Za-z */ |
| 681 | /* 14 */ { CCTL, CCTL, CCTL, CCTL } /* CTLESC ... */ |
| 682 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 683 | }; |
| 684 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 685 | #ifdef USE_SIT_FUNCTION |
| 686 | |
| 687 | #define U_C(c) ((unsigned char)(c)) |
| 688 | |
| 689 | static int SIT(int c, int syntax) |
| 690 | { |
| 691 | static const char spec_symbls[]="\t\n !\"$&'()*-/:;<=>?[\\]`|}~"; |
| 692 | static const char syntax_index_table [] = { |
| 693 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
| 694 | 7, 8, 3, 3, 3, 3, 1, 1, /* "()*-/:;<" */ |
| 695 | 3, 1, 3, 3, 9, 3,10, 1, /* "=>?[\\]`|" */ |
| 696 | 11,3 }; /* "}~" */ |
| 697 | const char *s; |
| 698 | int indx; |
| 699 | |
| 700 | if(c==PEOF) /* 2^8+2 */ |
| 701 | return CENDFILE; |
| 702 | if(c==PEOA) /* 2^8+1 */ |
| 703 | indx = 0; |
| 704 | else if(U_C(c)>=U_C(CTLESC) && U_C(c)<=U_C(CTLQUOTEMARK)) |
| 705 | return CCTL; |
| 706 | else { |
| 707 | s = strchr(spec_symbls, c); |
| 708 | if(s==0) |
| 709 | return CWORD; |
| 710 | indx = syntax_index_table[(s-spec_symbls)]; |
| 711 | } |
| 712 | return S_I_T[indx][syntax]; |
| 713 | } |
| 714 | |
| 715 | #else /* USE_SIT_FUNCTION */ |
| 716 | |
| 717 | #define SIT(c, syntax) S_I_T[(int)syntax_index_table[((int)c)+SYNBASE]][syntax] |
| 718 | |
| 719 | #define CSPCL_CIGN_CIGN_CIGN 0 |
| 720 | #define CSPCL_CWORD_CWORD_CWORD 1 |
| 721 | #define CNL_CNL_CNL_CNL 2 |
| 722 | #define CWORD_CCTL_CCTL_CWORD 3 |
| 723 | #define CDQUOTE_CENDQUOTE_CWORD_CDQUOTE 4 |
| 724 | #define CVAR_CVAR_CWORD_CVAR 5 |
| 725 | #define CSQUOTE_CWORD_CENDQUOTE_CSQUOTE 6 |
| 726 | #define CSPCL_CWORD_CWORD_CLP 7 |
| 727 | #define CSPCL_CWORD_CWORD_CRP 8 |
| 728 | #define CBACK_CBACK_CCTL_CBACK 9 |
| 729 | #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE 10 |
| 730 | #define CENDVAR_CENDVAR_CWORD_CENDVAR 11 |
| 731 | #define CENDFILE_CENDFILE_CENDFILE_CENDFILE 12 |
| 732 | #define CWORD_CWORD_CWORD_CWORD 13 |
| 733 | #define CCTL_CCTL_CCTL_CCTL 14 |
| 734 | |
| 735 | static const char syntax_index_table[258] = { |
| 736 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
| 737 | /* 0 -130 PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
| 738 | /* 1 -129 PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 739 | /* 2 -128 0xff */ CWORD_CWORD_CWORD_CWORD, |
| 740 | /* 3 -127 */ CCTL_CCTL_CCTL_CCTL, /* CTLQUOTEMARK */ |
| 741 | /* 4 -126 */ CCTL_CCTL_CCTL_CCTL, |
| 742 | /* 5 -125 */ CCTL_CCTL_CCTL_CCTL, |
| 743 | /* 6 -124 */ CCTL_CCTL_CCTL_CCTL, |
| 744 | /* 7 -123 */ CCTL_CCTL_CCTL_CCTL, |
| 745 | /* 8 -122 */ CCTL_CCTL_CCTL_CCTL, |
| 746 | /* 9 -121 */ CCTL_CCTL_CCTL_CCTL, |
| 747 | /* 10 -120 */ CCTL_CCTL_CCTL_CCTL, /* CTLESC */ |
| 748 | /* 11 -119 */ CWORD_CWORD_CWORD_CWORD, |
| 749 | /* 12 -118 */ CWORD_CWORD_CWORD_CWORD, |
| 750 | /* 13 -117 */ CWORD_CWORD_CWORD_CWORD, |
| 751 | /* 14 -116 */ CWORD_CWORD_CWORD_CWORD, |
| 752 | /* 15 -115 */ CWORD_CWORD_CWORD_CWORD, |
| 753 | /* 16 -114 */ CWORD_CWORD_CWORD_CWORD, |
| 754 | /* 17 -113 */ CWORD_CWORD_CWORD_CWORD, |
| 755 | /* 18 -112 */ CWORD_CWORD_CWORD_CWORD, |
| 756 | /* 19 -111 */ CWORD_CWORD_CWORD_CWORD, |
| 757 | /* 20 -110 */ CWORD_CWORD_CWORD_CWORD, |
| 758 | /* 21 -109 */ CWORD_CWORD_CWORD_CWORD, |
| 759 | /* 22 -108 */ CWORD_CWORD_CWORD_CWORD, |
| 760 | /* 23 -107 */ CWORD_CWORD_CWORD_CWORD, |
| 761 | /* 24 -106 */ CWORD_CWORD_CWORD_CWORD, |
| 762 | /* 25 -105 */ CWORD_CWORD_CWORD_CWORD, |
| 763 | /* 26 -104 */ CWORD_CWORD_CWORD_CWORD, |
| 764 | /* 27 -103 */ CWORD_CWORD_CWORD_CWORD, |
| 765 | /* 28 -102 */ CWORD_CWORD_CWORD_CWORD, |
| 766 | /* 29 -101 */ CWORD_CWORD_CWORD_CWORD, |
| 767 | /* 30 -100 */ CWORD_CWORD_CWORD_CWORD, |
| 768 | /* 31 -99 */ CWORD_CWORD_CWORD_CWORD, |
| 769 | /* 32 -98 */ CWORD_CWORD_CWORD_CWORD, |
| 770 | /* 33 -97 */ CWORD_CWORD_CWORD_CWORD, |
| 771 | /* 34 -96 */ CWORD_CWORD_CWORD_CWORD, |
| 772 | /* 35 -95 */ CWORD_CWORD_CWORD_CWORD, |
| 773 | /* 36 -94 */ CWORD_CWORD_CWORD_CWORD, |
| 774 | /* 37 -93 */ CWORD_CWORD_CWORD_CWORD, |
| 775 | /* 38 -92 */ CWORD_CWORD_CWORD_CWORD, |
| 776 | /* 39 -91 */ CWORD_CWORD_CWORD_CWORD, |
| 777 | /* 40 -90 */ CWORD_CWORD_CWORD_CWORD, |
| 778 | /* 41 -89 */ CWORD_CWORD_CWORD_CWORD, |
| 779 | /* 42 -88 */ CWORD_CWORD_CWORD_CWORD, |
| 780 | /* 43 -87 */ CWORD_CWORD_CWORD_CWORD, |
| 781 | /* 44 -86 */ CWORD_CWORD_CWORD_CWORD, |
| 782 | /* 45 -85 */ CWORD_CWORD_CWORD_CWORD, |
| 783 | /* 46 -84 */ CWORD_CWORD_CWORD_CWORD, |
| 784 | /* 47 -83 */ CWORD_CWORD_CWORD_CWORD, |
| 785 | /* 48 -82 */ CWORD_CWORD_CWORD_CWORD, |
| 786 | /* 49 -81 */ CWORD_CWORD_CWORD_CWORD, |
| 787 | /* 50 -80 */ CWORD_CWORD_CWORD_CWORD, |
| 788 | /* 51 -79 */ CWORD_CWORD_CWORD_CWORD, |
| 789 | /* 52 -78 */ CWORD_CWORD_CWORD_CWORD, |
| 790 | /* 53 -77 */ CWORD_CWORD_CWORD_CWORD, |
| 791 | /* 54 -76 */ CWORD_CWORD_CWORD_CWORD, |
| 792 | /* 55 -75 */ CWORD_CWORD_CWORD_CWORD, |
| 793 | /* 56 -74 */ CWORD_CWORD_CWORD_CWORD, |
| 794 | /* 57 -73 */ CWORD_CWORD_CWORD_CWORD, |
| 795 | /* 58 -72 */ CWORD_CWORD_CWORD_CWORD, |
| 796 | /* 59 -71 */ CWORD_CWORD_CWORD_CWORD, |
| 797 | /* 60 -70 */ CWORD_CWORD_CWORD_CWORD, |
| 798 | /* 61 -69 */ CWORD_CWORD_CWORD_CWORD, |
| 799 | /* 62 -68 */ CWORD_CWORD_CWORD_CWORD, |
| 800 | /* 63 -67 */ CWORD_CWORD_CWORD_CWORD, |
| 801 | /* 64 -66 */ CWORD_CWORD_CWORD_CWORD, |
| 802 | /* 65 -65 */ CWORD_CWORD_CWORD_CWORD, |
| 803 | /* 66 -64 */ CWORD_CWORD_CWORD_CWORD, |
| 804 | /* 67 -63 */ CWORD_CWORD_CWORD_CWORD, |
| 805 | /* 68 -62 */ CWORD_CWORD_CWORD_CWORD, |
| 806 | /* 69 -61 */ CWORD_CWORD_CWORD_CWORD, |
| 807 | /* 70 -60 */ CWORD_CWORD_CWORD_CWORD, |
| 808 | /* 71 -59 */ CWORD_CWORD_CWORD_CWORD, |
| 809 | /* 72 -58 */ CWORD_CWORD_CWORD_CWORD, |
| 810 | /* 73 -57 */ CWORD_CWORD_CWORD_CWORD, |
| 811 | /* 74 -56 */ CWORD_CWORD_CWORD_CWORD, |
| 812 | /* 75 -55 */ CWORD_CWORD_CWORD_CWORD, |
| 813 | /* 76 -54 */ CWORD_CWORD_CWORD_CWORD, |
| 814 | /* 77 -53 */ CWORD_CWORD_CWORD_CWORD, |
| 815 | /* 78 -52 */ CWORD_CWORD_CWORD_CWORD, |
| 816 | /* 79 -51 */ CWORD_CWORD_CWORD_CWORD, |
| 817 | /* 80 -50 */ CWORD_CWORD_CWORD_CWORD, |
| 818 | /* 81 -49 */ CWORD_CWORD_CWORD_CWORD, |
| 819 | /* 82 -48 */ CWORD_CWORD_CWORD_CWORD, |
| 820 | /* 83 -47 */ CWORD_CWORD_CWORD_CWORD, |
| 821 | /* 84 -46 */ CWORD_CWORD_CWORD_CWORD, |
| 822 | /* 85 -45 */ CWORD_CWORD_CWORD_CWORD, |
| 823 | /* 86 -44 */ CWORD_CWORD_CWORD_CWORD, |
| 824 | /* 87 -43 */ CWORD_CWORD_CWORD_CWORD, |
| 825 | /* 88 -42 */ CWORD_CWORD_CWORD_CWORD, |
| 826 | /* 89 -41 */ CWORD_CWORD_CWORD_CWORD, |
| 827 | /* 90 -40 */ CWORD_CWORD_CWORD_CWORD, |
| 828 | /* 91 -39 */ CWORD_CWORD_CWORD_CWORD, |
| 829 | /* 92 -38 */ CWORD_CWORD_CWORD_CWORD, |
| 830 | /* 93 -37 */ CWORD_CWORD_CWORD_CWORD, |
| 831 | /* 94 -36 */ CWORD_CWORD_CWORD_CWORD, |
| 832 | /* 95 -35 */ CWORD_CWORD_CWORD_CWORD, |
| 833 | /* 96 -34 */ CWORD_CWORD_CWORD_CWORD, |
| 834 | /* 97 -33 */ CWORD_CWORD_CWORD_CWORD, |
| 835 | /* 98 -32 */ CWORD_CWORD_CWORD_CWORD, |
| 836 | /* 99 -31 */ CWORD_CWORD_CWORD_CWORD, |
| 837 | /* 100 -30 */ CWORD_CWORD_CWORD_CWORD, |
| 838 | /* 101 -29 */ CWORD_CWORD_CWORD_CWORD, |
| 839 | /* 102 -28 */ CWORD_CWORD_CWORD_CWORD, |
| 840 | /* 103 -27 */ CWORD_CWORD_CWORD_CWORD, |
| 841 | /* 104 -26 */ CWORD_CWORD_CWORD_CWORD, |
| 842 | /* 105 -25 */ CWORD_CWORD_CWORD_CWORD, |
| 843 | /* 106 -24 */ CWORD_CWORD_CWORD_CWORD, |
| 844 | /* 107 -23 */ CWORD_CWORD_CWORD_CWORD, |
| 845 | /* 108 -22 */ CWORD_CWORD_CWORD_CWORD, |
| 846 | /* 109 -21 */ CWORD_CWORD_CWORD_CWORD, |
| 847 | /* 110 -20 */ CWORD_CWORD_CWORD_CWORD, |
| 848 | /* 111 -19 */ CWORD_CWORD_CWORD_CWORD, |
| 849 | /* 112 -18 */ CWORD_CWORD_CWORD_CWORD, |
| 850 | /* 113 -17 */ CWORD_CWORD_CWORD_CWORD, |
| 851 | /* 114 -16 */ CWORD_CWORD_CWORD_CWORD, |
| 852 | /* 115 -15 */ CWORD_CWORD_CWORD_CWORD, |
| 853 | /* 116 -14 */ CWORD_CWORD_CWORD_CWORD, |
| 854 | /* 117 -13 */ CWORD_CWORD_CWORD_CWORD, |
| 855 | /* 118 -12 */ CWORD_CWORD_CWORD_CWORD, |
| 856 | /* 119 -11 */ CWORD_CWORD_CWORD_CWORD, |
| 857 | /* 120 -10 */ CWORD_CWORD_CWORD_CWORD, |
| 858 | /* 121 -9 */ CWORD_CWORD_CWORD_CWORD, |
| 859 | /* 122 -8 */ CWORD_CWORD_CWORD_CWORD, |
| 860 | /* 123 -7 */ CWORD_CWORD_CWORD_CWORD, |
| 861 | /* 124 -6 */ CWORD_CWORD_CWORD_CWORD, |
| 862 | /* 125 -5 */ CWORD_CWORD_CWORD_CWORD, |
| 863 | /* 126 -4 */ CWORD_CWORD_CWORD_CWORD, |
| 864 | /* 127 -3 */ CWORD_CWORD_CWORD_CWORD, |
| 865 | /* 128 -2 */ CWORD_CWORD_CWORD_CWORD, |
| 866 | /* 129 -1 */ CWORD_CWORD_CWORD_CWORD, |
| 867 | /* 130 0 */ CWORD_CWORD_CWORD_CWORD, |
| 868 | /* 131 1 */ CWORD_CWORD_CWORD_CWORD, |
| 869 | /* 132 2 */ CWORD_CWORD_CWORD_CWORD, |
| 870 | /* 133 3 */ CWORD_CWORD_CWORD_CWORD, |
| 871 | /* 134 4 */ CWORD_CWORD_CWORD_CWORD, |
| 872 | /* 135 5 */ CWORD_CWORD_CWORD_CWORD, |
| 873 | /* 136 6 */ CWORD_CWORD_CWORD_CWORD, |
| 874 | /* 137 7 */ CWORD_CWORD_CWORD_CWORD, |
| 875 | /* 138 8 */ CWORD_CWORD_CWORD_CWORD, |
| 876 | /* 139 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 877 | /* 140 10 "\n" */ CNL_CNL_CNL_CNL, |
| 878 | /* 141 11 */ CWORD_CWORD_CWORD_CWORD, |
| 879 | /* 142 12 */ CWORD_CWORD_CWORD_CWORD, |
| 880 | /* 143 13 */ CWORD_CWORD_CWORD_CWORD, |
| 881 | /* 144 14 */ CWORD_CWORD_CWORD_CWORD, |
| 882 | /* 145 15 */ CWORD_CWORD_CWORD_CWORD, |
| 883 | /* 146 16 */ CWORD_CWORD_CWORD_CWORD, |
| 884 | /* 147 17 */ CWORD_CWORD_CWORD_CWORD, |
| 885 | /* 148 18 */ CWORD_CWORD_CWORD_CWORD, |
| 886 | /* 149 19 */ CWORD_CWORD_CWORD_CWORD, |
| 887 | /* 150 20 */ CWORD_CWORD_CWORD_CWORD, |
| 888 | /* 151 21 */ CWORD_CWORD_CWORD_CWORD, |
| 889 | /* 152 22 */ CWORD_CWORD_CWORD_CWORD, |
| 890 | /* 153 23 */ CWORD_CWORD_CWORD_CWORD, |
| 891 | /* 154 24 */ CWORD_CWORD_CWORD_CWORD, |
| 892 | /* 155 25 */ CWORD_CWORD_CWORD_CWORD, |
| 893 | /* 156 26 */ CWORD_CWORD_CWORD_CWORD, |
| 894 | /* 157 27 */ CWORD_CWORD_CWORD_CWORD, |
| 895 | /* 158 28 */ CWORD_CWORD_CWORD_CWORD, |
| 896 | /* 159 29 */ CWORD_CWORD_CWORD_CWORD, |
| 897 | /* 160 30 */ CWORD_CWORD_CWORD_CWORD, |
| 898 | /* 161 31 */ CWORD_CWORD_CWORD_CWORD, |
| 899 | /* 162 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 900 | /* 163 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
| 901 | /* 164 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CDQUOTE, |
| 902 | /* 165 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 903 | /* 166 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 904 | /* 167 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 905 | /* 168 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
| 906 | /* 169 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CSQUOTE, |
| 907 | /* 170 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 908 | /* 171 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 909 | /* 172 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 910 | /* 173 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 911 | /* 174 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 912 | /* 175 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 913 | /* 176 46 "." */ CWORD_CWORD_CWORD_CWORD, |
| 914 | /* 177 47 "/" */ CWORD_CCTL_CCTL_CWORD, |
| 915 | /* 178 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 916 | /* 179 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 917 | /* 180 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 918 | /* 181 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 919 | /* 182 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 920 | /* 183 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 921 | /* 184 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 922 | /* 185 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 923 | /* 186 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 924 | /* 187 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 925 | /* 188 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 926 | /* 189 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 927 | /* 190 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 928 | /* 191 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 929 | /* 192 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 930 | /* 193 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 931 | /* 194 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 932 | /* 195 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 933 | /* 196 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 934 | /* 197 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 935 | /* 198 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 936 | /* 199 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 937 | /* 200 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 938 | /* 201 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 939 | /* 202 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 940 | /* 203 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 941 | /* 204 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 942 | /* 205 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 943 | /* 206 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 944 | /* 207 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 945 | /* 208 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 946 | /* 209 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 947 | /* 210 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 948 | /* 211 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 949 | /* 212 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 950 | /* 213 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 951 | /* 214 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 952 | /* 215 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 953 | /* 216 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 954 | /* 217 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 955 | /* 218 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 956 | /* 219 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 957 | /* 220 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 958 | /* 221 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 959 | /* 222 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 960 | /* 223 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 961 | /* 224 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 962 | /* 225 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 963 | /* 226 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 964 | /* 227 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 965 | /* 228 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 966 | /* 229 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 967 | /* 230 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 968 | /* 231 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 969 | /* 232 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 970 | /* 233 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 971 | /* 234 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 972 | /* 235 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 973 | /* 236 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 974 | /* 237 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 975 | /* 238 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 976 | /* 239 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 977 | /* 240 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 978 | /* 241 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 979 | /* 242 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 980 | /* 243 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 981 | /* 244 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 982 | /* 245 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 983 | /* 246 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 984 | /* 247 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 985 | /* 248 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 986 | /* 249 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 987 | /* 250 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 988 | /* 251 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 989 | /* 252 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 990 | /* 253 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 991 | /* 254 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 992 | /* 255 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 993 | /* 256 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 994 | /* 257 127 */ CWORD_CWORD_CWORD_CWORD, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 995 | }; |
| 996 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 997 | #endif /* USE_SIT_FUNCTION */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 998 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 999 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 1000 | /* first char is indicating which tokens mark the end of a list */ |
| 1001 | static const char *const tokname_array[] = { |
| 1002 | "\1end of file", |
| 1003 | "\0newline", |
| 1004 | "\0redirection", |
| 1005 | "\0word", |
| 1006 | "\0assignment", |
| 1007 | "\0;", |
| 1008 | "\0&", |
| 1009 | "\0&&", |
| 1010 | "\0||", |
| 1011 | "\0|", |
| 1012 | "\0(", |
| 1013 | "\1)", |
| 1014 | "\1;;", |
| 1015 | "\1`", |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1016 | #define KWDOFFSET 14 |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 1017 | /* the following are keywords */ |
| 1018 | "\0!", |
| 1019 | "\0case", |
| 1020 | "\1do", |
| 1021 | "\1done", |
| 1022 | "\1elif", |
| 1023 | "\1else", |
| 1024 | "\1esac", |
| 1025 | "\1fi", |
| 1026 | "\0for", |
| 1027 | "\0if", |
| 1028 | "\0in", |
| 1029 | "\1then", |
| 1030 | "\0until", |
| 1031 | "\0while", |
| 1032 | "\0{", |
| 1033 | "\1}", |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1034 | }; |
| 1035 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 1036 | static const char *tokname(int tok) |
| 1037 | { |
| 1038 | static char buf[16]; |
| 1039 | |
| 1040 | if(tok>=TSEMI) |
| 1041 | buf[0] = '"'; |
| 1042 | sprintf(buf+(tok>=TSEMI), "%s%c", |
| 1043 | tokname_array[tok]+1, (tok>=TSEMI ? '"' : 0)); |
| 1044 | return buf; |
| 1045 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1046 | |
| 1047 | static int plinno = 1; /* input line number */ |
| 1048 | |
| 1049 | static int parselleft; /* copy of parsefile->lleft */ |
| 1050 | |
| 1051 | static struct parsefile basepf; /* top level input file */ |
| 1052 | static char basebuf[BUFSIZ]; /* buffer for top level input file */ |
| 1053 | static struct parsefile *parsefile = &basepf; /* current input file */ |
| 1054 | |
| 1055 | /* |
| 1056 | * NEOF is returned by parsecmd when it encounters an end of file. It |
| 1057 | * must be distinct from NULL, so we use the address of a variable that |
| 1058 | * happens to be handy. |
| 1059 | */ |
| 1060 | |
| 1061 | static int tokpushback; /* last token pushed back */ |
| 1062 | #define NEOF ((union node *)&tokpushback) |
| 1063 | static int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */ |
| 1064 | |
| 1065 | |
| 1066 | static void error (const char *, ...) __attribute__((__noreturn__)); |
| 1067 | static void exerror (int, const char *, ...) __attribute__((__noreturn__)); |
| 1068 | static void shellexec (char **, char **, const char *, int) |
| 1069 | __attribute__((noreturn)); |
| 1070 | static void exitshell (int) __attribute__((noreturn)); |
| 1071 | |
| 1072 | static int goodname(const char *); |
| 1073 | static void ignoresig (int); |
| 1074 | static void onsig (int); |
| 1075 | static void dotrap (void); |
| 1076 | static int decode_signal (const char *, int); |
| 1077 | |
| 1078 | static void shprocvar(void); |
| 1079 | static void deletefuncs(void); |
| 1080 | static void setparam (char **); |
| 1081 | static void freeparam (volatile struct shparam *); |
| 1082 | |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 1083 | static void find_command (const char *, struct cmdentry *, int, const char *); |
| 1084 | |
| 1085 | static inline void hashcd (void); |
| 1086 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1087 | /* reasons for skipping commands (see comment on breakcmd routine) */ |
| 1088 | #define SKIPBREAK 1 |
| 1089 | #define SKIPCONT 2 |
| 1090 | #define SKIPFUNC 3 |
| 1091 | #define SKIPFILE 4 |
| 1092 | |
| 1093 | /* values of cmdtype */ |
| 1094 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 1095 | #define CMDNORMAL 0 /* command is an executable program */ |
| 1096 | #define CMDBUILTIN 1 /* command is a shell builtin */ |
| 1097 | #define CMDFUNCTION 2 /* command is a shell function */ |
| 1098 | |
| 1099 | #define DO_ERR 1 /* find_command prints errors */ |
| 1100 | #define DO_ABS 2 /* find_command checks absolute paths */ |
| 1101 | #define DO_NOFUN 4 /* find_command ignores functions */ |
| 1102 | #define DO_BRUTE 8 /* find_command ignores hash table */ |
| 1103 | |
| 1104 | /* |
| 1105 | * Shell variables. |
| 1106 | */ |
| 1107 | |
| 1108 | /* flags */ |
| 1109 | #define VEXPORT 0x01 /* variable is exported */ |
| 1110 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 1111 | #define VSTRFIXED 0x04 /* variable struct is staticly allocated */ |
| 1112 | #define VTEXTFIXED 0x08 /* text is staticly allocated */ |
| 1113 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 1114 | #define VUNSET 0x20 /* the variable is not set */ |
| 1115 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 1116 | |
| 1117 | |
| 1118 | struct var { |
| 1119 | struct var *next; /* next entry in hash list */ |
| 1120 | int flags; /* flags are defined above */ |
| 1121 | char *text; /* name=value */ |
| 1122 | void (*func) (const char *); |
| 1123 | /* function to be called when */ |
| 1124 | /* the variable gets set/unset */ |
| 1125 | }; |
| 1126 | |
| 1127 | struct localvar { |
| 1128 | struct localvar *next; /* next local variable in list */ |
| 1129 | struct var *vp; /* the variable that was made local */ |
| 1130 | int flags; /* saved flags */ |
| 1131 | char *text; /* saved text */ |
| 1132 | }; |
| 1133 | |
| 1134 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 1135 | #if defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1136 | #define rmescapes(p) _rmescapes((p), 0) |
| 1137 | static char *_rmescapes (char *, int); |
| 1138 | #else |
| 1139 | static void rmescapes (char *); |
| 1140 | #endif |
| 1141 | |
| 1142 | static int casematch (union node *, const char *); |
| 1143 | static void clearredir(void); |
| 1144 | static void popstring(void); |
| 1145 | static void readcmdfile (const char *); |
| 1146 | |
| 1147 | static int number (const char *); |
| 1148 | static int is_number (const char *, int *num); |
| 1149 | static char *single_quote (const char *); |
| 1150 | static int nextopt (const char *); |
| 1151 | |
| 1152 | static void redirect (union node *, int); |
| 1153 | static void popredir (void); |
| 1154 | static int dup_as_newfd (int, int); |
| 1155 | |
| 1156 | static void changepath(const char *newval); |
| 1157 | static void getoptsreset(const char *value); |
| 1158 | |
| 1159 | |
| 1160 | static int parsenleft; /* copy of parsefile->nleft */ |
| 1161 | static char *parsenextc; /* copy of parsefile->nextc */ |
| 1162 | static int rootpid; /* pid of main shell */ |
| 1163 | static int rootshell; /* true if we aren't a child of the main shell */ |
| 1164 | |
| 1165 | static const char spcstr[] = " "; |
| 1166 | static const char snlfmt[] = "%s\n"; |
| 1167 | |
| 1168 | static int sstrnleft; |
| 1169 | static int herefd = -1; |
| 1170 | |
| 1171 | static struct localvar *localvars; |
| 1172 | |
| 1173 | static struct var vifs; |
| 1174 | static struct var vmail; |
| 1175 | static struct var vmpath; |
| 1176 | static struct var vpath; |
| 1177 | static struct var vps1; |
| 1178 | static struct var vps2; |
| 1179 | static struct var voptind; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1180 | #ifdef CONFIG_LOCALE_SUPPORT |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1181 | static struct var vlc_all; |
| 1182 | static struct var vlc_ctype; |
| 1183 | #endif |
| 1184 | |
| 1185 | struct varinit { |
| 1186 | struct var *var; |
| 1187 | int flags; |
| 1188 | const char *text; |
| 1189 | void (*func) (const char *); |
| 1190 | }; |
| 1191 | |
| 1192 | static const char defpathvar[] = |
| 1193 | "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"; |
| 1194 | #define defpath (defpathvar + 5) |
| 1195 | |
| 1196 | #ifdef IFS_BROKEN |
| 1197 | static const char defifsvar[] = "IFS= \t\n"; |
| 1198 | #define defifs (defifsvar + 4) |
| 1199 | #else |
| 1200 | static const char defifs[] = " \t\n"; |
| 1201 | #endif |
| 1202 | |
| 1203 | static const struct varinit varinit[] = { |
| 1204 | #ifdef IFS_BROKEN |
| 1205 | { &vifs, VSTRFIXED|VTEXTFIXED, defifsvar, |
| 1206 | #else |
| 1207 | { &vifs, VSTRFIXED|VTEXTFIXED|VUNSET, "IFS=", |
| 1208 | #endif |
| 1209 | NULL }, |
| 1210 | { &vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=", |
| 1211 | NULL }, |
| 1212 | { &vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=", |
| 1213 | NULL }, |
| 1214 | { &vpath, VSTRFIXED|VTEXTFIXED, defpathvar, |
| 1215 | changepath }, |
Tim Riker | 497a885 | 2002-04-13 05:37:10 +0000 | [diff] [blame] | 1216 | #if defined(CONFIG_FEATURE_COMMAND_EDITING) && defined(CONFIG_FEATURE_SH_FANCY_PROMPT) |
| 1217 | { &vps1, VSTRFIXED|VTEXTFIXED, "PS1=\\w \\$ ", |
| 1218 | NULL }, |
| 1219 | #endif /* else vps1 depends on uid */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1220 | { &vps2, VSTRFIXED|VTEXTFIXED, "PS2=> ", |
| 1221 | NULL }, |
| 1222 | { &voptind, VSTRFIXED|VTEXTFIXED, "OPTIND=1", |
| 1223 | getoptsreset }, |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1224 | #ifdef CONFIG_LOCALE_SUPPORT |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1225 | { &vlc_all, VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL=", |
| 1226 | change_lc_all }, |
| 1227 | { &vlc_ctype, VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE=", |
| 1228 | change_lc_ctype }, |
| 1229 | #endif |
| 1230 | { NULL, 0, NULL, |
| 1231 | NULL } |
| 1232 | }; |
| 1233 | |
| 1234 | #define VTABSIZE 39 |
| 1235 | |
| 1236 | static struct var *vartab[VTABSIZE]; |
| 1237 | |
| 1238 | /* |
| 1239 | * The following macros access the values of the above variables. |
| 1240 | * They have to skip over the name. They return the null string |
| 1241 | * for unset variables. |
| 1242 | */ |
| 1243 | |
| 1244 | #define ifsval() (vifs.text + 4) |
| 1245 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
| 1246 | #define mailval() (vmail.text + 5) |
| 1247 | #define mpathval() (vmpath.text + 9) |
| 1248 | #define pathval() (vpath.text + 5) |
| 1249 | #define ps1val() (vps1.text + 4) |
| 1250 | #define ps2val() (vps2.text + 4) |
| 1251 | #define optindval() (voptind.text + 7) |
| 1252 | |
| 1253 | #define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1254 | |
| 1255 | static void initvar (void); |
| 1256 | static void setvar (const char *, const char *, int); |
| 1257 | static void setvareq (char *, int); |
| 1258 | static void listsetvar (struct strlist *); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 1259 | static const char *lookupvar (const char *); |
| 1260 | static const char *bltinlookup (const char *); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1261 | static char **environment (void); |
| 1262 | static int showvarscmd (int, char **); |
| 1263 | static void mklocal (char *); |
| 1264 | static void poplocalvars (void); |
| 1265 | static int unsetvar (const char *); |
| 1266 | static int varequal (const char *, const char *); |
| 1267 | |
| 1268 | |
| 1269 | static char *arg0; /* value of $0 */ |
| 1270 | static struct shparam shellparam; /* current positional parameters */ |
| 1271 | static char **argptr; /* argument list for builtin commands */ |
| 1272 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1273 | static char *optptr; /* used by nextopt */ |
| 1274 | static char *minusc; /* argument to -c option */ |
| 1275 | |
| 1276 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1277 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1278 | |
| 1279 | #define ALIASINUSE 1 |
| 1280 | #define ALIASDEAD 2 |
| 1281 | |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 1282 | #define ATABSIZE 39 |
| 1283 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1284 | struct alias { |
| 1285 | struct alias *next; |
| 1286 | char *name; |
| 1287 | char *val; |
| 1288 | int flag; |
| 1289 | }; |
| 1290 | |
| 1291 | static struct alias *atab[ATABSIZE]; |
| 1292 | |
| 1293 | static void setalias (char *, char *); |
| 1294 | static struct alias **hashalias (const char *); |
| 1295 | static struct alias *freealias (struct alias *); |
| 1296 | static struct alias **__lookupalias (const char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1297 | |
| 1298 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 1299 | setalias(char *name, char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1300 | { |
| 1301 | struct alias *ap, **app; |
| 1302 | |
| 1303 | app = __lookupalias(name); |
| 1304 | ap = *app; |
| 1305 | INTOFF; |
| 1306 | if (ap) { |
| 1307 | if (!(ap->flag & ALIASINUSE)) { |
| 1308 | ckfree(ap->val); |
| 1309 | } |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 1310 | ap->val = xstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1311 | ap->flag &= ~ALIASDEAD; |
| 1312 | } else { |
| 1313 | /* not found */ |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 1314 | ap = xmalloc(sizeof (struct alias)); |
| 1315 | ap->name = xstrdup(name); |
| 1316 | ap->val = xstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1317 | ap->flag = 0; |
| 1318 | ap->next = 0; |
| 1319 | *app = ap; |
| 1320 | } |
| 1321 | INTON; |
| 1322 | } |
| 1323 | |
| 1324 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1325 | unalias(char *name) |
| 1326 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1327 | struct alias **app; |
| 1328 | |
| 1329 | app = __lookupalias(name); |
| 1330 | |
| 1331 | if (*app) { |
| 1332 | INTOFF; |
| 1333 | *app = freealias(*app); |
| 1334 | INTON; |
| 1335 | return (0); |
| 1336 | } |
| 1337 | |
| 1338 | return (1); |
| 1339 | } |
| 1340 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1341 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1342 | rmaliases(void) |
| 1343 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1344 | struct alias *ap, **app; |
| 1345 | int i; |
| 1346 | |
| 1347 | INTOFF; |
| 1348 | for (i = 0; i < ATABSIZE; i++) { |
| 1349 | app = &atab[i]; |
| 1350 | for (ap = *app; ap; ap = *app) { |
| 1351 | *app = freealias(*app); |
| 1352 | if (ap == *app) { |
| 1353 | app = &ap->next; |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | INTON; |
| 1358 | } |
| 1359 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1360 | static void |
| 1361 | printalias(const struct alias *ap) { |
| 1362 | char *p; |
| 1363 | |
| 1364 | p = single_quote(ap->val); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 1365 | printf("alias %s=%s\n", ap->name, p); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1366 | stunalloc(p); |
| 1367 | } |
| 1368 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1369 | |
| 1370 | /* |
| 1371 | * TODO - sort output |
| 1372 | */ |
| 1373 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1374 | aliascmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1375 | { |
| 1376 | char *n, *v; |
| 1377 | int ret = 0; |
| 1378 | struct alias *ap; |
| 1379 | |
| 1380 | if (argc == 1) { |
| 1381 | int i; |
| 1382 | |
| 1383 | for (i = 0; i < ATABSIZE; i++) |
| 1384 | for (ap = atab[i]; ap; ap = ap->next) { |
| 1385 | printalias(ap); |
| 1386 | } |
| 1387 | return (0); |
| 1388 | } |
| 1389 | while ((n = *++argv) != NULL) { |
| 1390 | if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */ |
| 1391 | if ((ap = *__lookupalias(n)) == NULL) { |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 1392 | out2fmt("%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1393 | ret = 1; |
| 1394 | } else |
| 1395 | printalias(ap); |
| 1396 | } |
| 1397 | else { |
| 1398 | *v++ = '\0'; |
| 1399 | setalias(n, v); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | return (ret); |
| 1404 | } |
| 1405 | |
| 1406 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1407 | unaliascmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1408 | { |
| 1409 | int i; |
| 1410 | |
| 1411 | while ((i = nextopt("a")) != '\0') { |
| 1412 | if (i == 'a') { |
| 1413 | rmaliases(); |
| 1414 | return (0); |
| 1415 | } |
| 1416 | } |
| 1417 | for (i = 0; *argptr; argptr++) { |
| 1418 | if (unalias(*argptr)) { |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 1419 | out2fmt("%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1420 | i = 1; |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | return (i); |
| 1425 | } |
| 1426 | |
| 1427 | static struct alias ** |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 1428 | hashalias(const char *p) |
| 1429 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1430 | unsigned int hashval; |
| 1431 | |
| 1432 | hashval = *p << 4; |
| 1433 | while (*p) |
| 1434 | hashval+= *p++; |
| 1435 | return &atab[hashval % ATABSIZE]; |
| 1436 | } |
| 1437 | |
| 1438 | static struct alias * |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 1439 | freealias(struct alias *ap) |
| 1440 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1441 | struct alias *next; |
| 1442 | |
| 1443 | if (ap->flag & ALIASINUSE) { |
| 1444 | ap->flag |= ALIASDEAD; |
| 1445 | return ap; |
| 1446 | } |
| 1447 | |
| 1448 | next = ap->next; |
| 1449 | ckfree(ap->name); |
| 1450 | ckfree(ap->val); |
| 1451 | ckfree(ap); |
| 1452 | return next; |
| 1453 | } |
| 1454 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1455 | |
| 1456 | static struct alias ** |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 1457 | __lookupalias(const char *name) |
| 1458 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1459 | struct alias **app = hashalias(name); |
| 1460 | |
| 1461 | for (; *app; app = &(*app)->next) { |
| 1462 | if (equal(name, (*app)->name)) { |
| 1463 | break; |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | return app; |
| 1468 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1469 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1470 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1471 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 1472 | /* The generated file arith.c has been replaced with a custom hand |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 1473 | * written implementation written by Aaron Lehmann <aaronl@vitelus.com>. |
| 1474 | * This is now part of libbb, so that it can be used by all the shells |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 1475 | * in busybox. */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1476 | static void expari (int); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1477 | #endif |
| 1478 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1479 | static char *trap[NSIG]; /* trap handler commands */ |
| 1480 | static char sigmode[NSIG - 1]; /* current value of signal */ |
| 1481 | static char gotsig[NSIG - 1]; /* indicates specified signal received */ |
| 1482 | static int pendingsigs; /* indicates some signal received */ |
| 1483 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1484 | /* |
| 1485 | * This file was generated by the mkbuiltins program. |
| 1486 | */ |
| 1487 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1488 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1489 | static int bgcmd (int, char **); |
| 1490 | static int fgcmd (int, char **); |
| 1491 | static int killcmd (int, char **); |
| 1492 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1493 | static int bltincmd (int, char **); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1494 | static int cdcmd (int, char **); |
| 1495 | static int breakcmd (int, char **); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1496 | #ifdef CONFIG_ASH_CMDCMD |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1497 | static int commandcmd (int, char **); |
| 1498 | #endif |
| 1499 | static int dotcmd (int, char **); |
| 1500 | static int evalcmd (int, char **); |
| 1501 | static int execcmd (int, char **); |
| 1502 | static int exitcmd (int, char **); |
| 1503 | static int exportcmd (int, char **); |
| 1504 | static int histcmd (int, char **); |
| 1505 | static int hashcmd (int, char **); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 1506 | static int helpcmd (int, char **); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1507 | static int jobscmd (int, char **); |
| 1508 | static int localcmd (int, char **); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1509 | static int pwdcmd (int, char **); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1510 | static int readcmd (int, char **); |
| 1511 | static int returncmd (int, char **); |
| 1512 | static int setcmd (int, char **); |
| 1513 | static int setvarcmd (int, char **); |
| 1514 | static int shiftcmd (int, char **); |
| 1515 | static int trapcmd (int, char **); |
| 1516 | static int umaskcmd (int, char **); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1517 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1518 | static int aliascmd (int, char **); |
| 1519 | static int unaliascmd (int, char **); |
| 1520 | #endif |
| 1521 | static int unsetcmd (int, char **); |
| 1522 | static int waitcmd (int, char **); |
| 1523 | static int ulimitcmd (int, char **); |
| 1524 | static int timescmd (int, char **); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1525 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | fa1c5aa | 2001-07-31 21:38:23 +0000 | [diff] [blame] | 1526 | static int letcmd (int, char **); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1527 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1528 | static int typecmd (int, char **); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1529 | #ifdef CONFIG_ASH_GETOPTS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1530 | static int getoptscmd (int, char **); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1531 | #endif |
| 1532 | |
Eric Andersen | 69a20f0 | 2001-10-31 10:40:37 +0000 | [diff] [blame] | 1533 | #ifndef CONFIG_TRUE |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1534 | static int true_main (int, char **); |
Eric Andersen | 69a20f0 | 2001-10-31 10:40:37 +0000 | [diff] [blame] | 1535 | #endif |
| 1536 | #ifndef CONFIG_FALSE |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1537 | static int false_main (int, char **); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1538 | #endif |
| 1539 | |
| 1540 | static void setpwd (const char *, int); |
| 1541 | |
| 1542 | |
| 1543 | #define BUILTIN_NOSPEC "0" |
| 1544 | #define BUILTIN_SPECIAL "1" |
| 1545 | #define BUILTIN_REGULAR "2" |
| 1546 | #define BUILTIN_ASSIGN "4" |
| 1547 | #define BUILTIN_SPEC_ASSG "5" |
| 1548 | #define BUILTIN_REG_ASSG "6" |
| 1549 | |
| 1550 | #define IS_BUILTIN_SPECIAL(builtincmd) ((builtincmd)->name[0] & 1) |
| 1551 | #define IS_BUILTIN_REGULAR(builtincmd) ((builtincmd)->name[0] & 2) |
| 1552 | #define IS_BUILTIN_ASSIGN(builtincmd) ((builtincmd)->name[0] & 4) |
| 1553 | |
| 1554 | struct builtincmd { |
| 1555 | const char *name; |
| 1556 | int (*const builtinfunc) (int, char **); |
| 1557 | //unsigned flags; |
| 1558 | }; |
| 1559 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1560 | |
| 1561 | /* It is CRUCIAL that this listing be kept in ascii order, otherwise |
| 1562 | * the binary search in find_builtin() will stop working. If you value |
| 1563 | * your kneecaps, you'll be sure to *make sure* that any changes made |
| 1564 | * to this array result in the listing remaining in ascii order. You |
| 1565 | * have been warned. |
| 1566 | */ |
| 1567 | static const struct builtincmd builtincmds[] = { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 1568 | { BUILTIN_SPECIAL ".", dotcmd }, /* first, see declare DOTCMD */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1569 | { BUILTIN_SPECIAL ":", true_main }, |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1570 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1571 | { BUILTIN_REG_ASSG "alias", aliascmd }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1572 | #endif |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1573 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1574 | { BUILTIN_REGULAR "bg", bgcmd }, |
| 1575 | #endif |
| 1576 | { BUILTIN_SPECIAL "break", breakcmd }, |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 1577 | { BUILTIN_SPECIAL "builtin", bltincmd }, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1578 | { BUILTIN_REGULAR "cd", cdcmd }, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1579 | { BUILTIN_NOSPEC "chdir", cdcmd }, |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1580 | #ifdef CONFIG_ASH_CMDCMD |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1581 | { BUILTIN_REGULAR "command", commandcmd }, |
| 1582 | #endif |
| 1583 | { BUILTIN_SPECIAL "continue", breakcmd }, |
| 1584 | { BUILTIN_SPECIAL "eval", evalcmd }, |
| 1585 | { BUILTIN_SPECIAL "exec", execcmd }, |
| 1586 | { BUILTIN_SPECIAL "exit", exitcmd }, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1587 | { BUILTIN_SPEC_ASSG "export", exportcmd }, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1588 | { BUILTIN_REGULAR "false", false_main }, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1589 | { BUILTIN_REGULAR "fc", histcmd }, |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1590 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1591 | { BUILTIN_REGULAR "fg", fgcmd }, |
| 1592 | #endif |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1593 | #ifdef CONFIG_ASH_GETOPTS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1594 | { BUILTIN_REGULAR "getopts", getoptscmd }, |
| 1595 | #endif |
| 1596 | { BUILTIN_NOSPEC "hash", hashcmd }, |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 1597 | { BUILTIN_NOSPEC "help", helpcmd }, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1598 | { BUILTIN_REGULAR "jobs", jobscmd }, |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1599 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1600 | { BUILTIN_REGULAR "kill", killcmd }, |
| 1601 | #endif |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1602 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | fa1c5aa | 2001-07-31 21:38:23 +0000 | [diff] [blame] | 1603 | { BUILTIN_REGULAR "let", letcmd }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1604 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1605 | { BUILTIN_ASSIGN "local", localcmd }, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1606 | { BUILTIN_NOSPEC "pwd", pwdcmd }, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1607 | { BUILTIN_REGULAR "read", readcmd }, |
| 1608 | { BUILTIN_SPEC_ASSG "readonly", exportcmd }, |
| 1609 | { BUILTIN_SPECIAL "return", returncmd }, |
| 1610 | { BUILTIN_SPECIAL "set", setcmd }, |
| 1611 | { BUILTIN_NOSPEC "setvar", setvarcmd }, |
| 1612 | { BUILTIN_SPECIAL "shift", shiftcmd }, |
| 1613 | { BUILTIN_SPECIAL "times", timescmd }, |
| 1614 | { BUILTIN_SPECIAL "trap", trapcmd }, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1615 | { BUILTIN_REGULAR "true", true_main }, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1616 | { BUILTIN_NOSPEC "type", typecmd }, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1617 | { BUILTIN_NOSPEC "ulimit", ulimitcmd }, |
| 1618 | { BUILTIN_REGULAR "umask", umaskcmd }, |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1619 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1620 | { BUILTIN_REGULAR "unalias", unaliascmd }, |
| 1621 | #endif |
| 1622 | { BUILTIN_SPECIAL "unset", unsetcmd }, |
| 1623 | { BUILTIN_REGULAR "wait", waitcmd }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1624 | }; |
| 1625 | #define NUMBUILTINS (sizeof (builtincmds) / sizeof (struct builtincmd) ) |
| 1626 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 1627 | #define DOTCMD &builtincmds[0] |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1628 | static struct builtincmd *BLTINCMD; |
| 1629 | static struct builtincmd *EXECCMD; |
| 1630 | static struct builtincmd *EVALCMD; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1631 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1632 | /* states */ |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1633 | #define CONFIG_ASH_JOB_CONTROLTOPPED 1 /* all procs are stopped */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1634 | #define JOBDONE 2 /* all procs are completed */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1635 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1636 | /* |
| 1637 | * A job structure contains information about a job. A job is either a |
| 1638 | * single process or a set of processes contained in a pipeline. In the |
| 1639 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 1640 | * array of pids. |
| 1641 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1642 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1643 | struct procstat { |
| 1644 | pid_t pid; /* process id */ |
| 1645 | int status; /* status flags (defined above) */ |
| 1646 | char *cmd; /* text of command being run */ |
| 1647 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1648 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1649 | |
| 1650 | static int job_warning; /* user was warned about stopped jobs */ |
| 1651 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1652 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1653 | static void setjobctl(int enable); |
| 1654 | #else |
| 1655 | #define setjobctl(on) /* do nothing */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1656 | #endif |
| 1657 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1658 | |
| 1659 | struct job { |
| 1660 | struct procstat ps0; /* status of process */ |
| 1661 | struct procstat *ps; /* status or processes when more than one */ |
| 1662 | short nprocs; /* number of processes */ |
| 1663 | short pgrp; /* process group of this job */ |
| 1664 | char state; /* true if job is finished */ |
| 1665 | char used; /* true if this entry is in used */ |
| 1666 | char changed; /* true if status has changed */ |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1667 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1668 | char jobctl; /* job running under job control */ |
| 1669 | #endif |
| 1670 | }; |
| 1671 | |
| 1672 | static struct job *jobtab; /* array of jobs */ |
| 1673 | static int njobs; /* size of array */ |
| 1674 | static int backgndpid = -1; /* pid of last background process */ |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 1675 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1676 | static int initialpgrp; /* pgrp of shell on invocation */ |
| 1677 | static int curjob; /* current job */ |
| 1678 | static int jobctl; |
| 1679 | #endif |
| 1680 | static int intreceived; |
| 1681 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 1682 | static struct job *makejob (const union node *, int); |
| 1683 | static int forkshell (struct job *, const union node *, int); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1684 | static int waitforjob (struct job *); |
| 1685 | |
| 1686 | static int docd (char *, int); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1687 | static void getpwd (void); |
| 1688 | |
| 1689 | static char *padvance (const char **, const char *); |
| 1690 | |
| 1691 | static char nullstr[1]; /* zero length string */ |
| 1692 | static char *curdir = nullstr; /* current working directory */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1693 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1694 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 1695 | cdcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1696 | { |
| 1697 | const char *dest; |
| 1698 | const char *path; |
| 1699 | char *p; |
| 1700 | struct stat statb; |
| 1701 | int print = 0; |
| 1702 | |
| 1703 | nextopt(nullstr); |
| 1704 | if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME")) == NULL) |
| 1705 | error("HOME not set"); |
| 1706 | if (*dest == '\0') |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1707 | dest = "."; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1708 | if (dest[0] == '-' && dest[1] == '\0') { |
| 1709 | dest = bltinlookup("OLDPWD"); |
| 1710 | if (!dest || !*dest) { |
| 1711 | dest = curdir; |
| 1712 | } |
| 1713 | print = 1; |
| 1714 | if (dest) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1715 | print = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1716 | else |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1717 | dest = "."; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1718 | } |
| 1719 | if (*dest == '/' || (path = bltinlookup("CDPATH")) == NULL) |
| 1720 | path = nullstr; |
| 1721 | while ((p = padvance(&path, dest)) != NULL) { |
| 1722 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 1723 | if (!print) { |
| 1724 | /* |
| 1725 | * XXX - rethink |
| 1726 | */ |
| 1727 | if (p[0] == '.' && p[1] == '/' && p[2] != '\0') |
| 1728 | p += 2; |
| 1729 | print = strcmp(p, dest); |
| 1730 | } |
| 1731 | if (docd(p, print) >= 0) |
| 1732 | return 0; |
| 1733 | |
| 1734 | } |
| 1735 | } |
| 1736 | error("can't cd to %s", dest); |
| 1737 | /* NOTREACHED */ |
| 1738 | } |
| 1739 | |
| 1740 | |
| 1741 | /* |
| 1742 | * Actually do the chdir. In an interactive shell, print the |
| 1743 | * directory name if "print" is nonzero. |
| 1744 | */ |
| 1745 | |
| 1746 | static int |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 1747 | docd(char *dest, int print) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1748 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1749 | TRACE(("docd(\"%s\", %d) called\n", dest, print)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1750 | INTOFF; |
| 1751 | if (chdir(dest) < 0) { |
| 1752 | INTON; |
| 1753 | return -1; |
| 1754 | } |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 1755 | hashcd(); |
| 1756 | /* |
| 1757 | * Update curdir (the name of the current directory) in response to a |
| 1758 | * cd command. We also call hashcd to let the routines in exec.c know |
| 1759 | * that the current directory has changed. |
| 1760 | */ |
| 1761 | /* If dest is NULL, we don't know the current directory */ |
| 1762 | if (dest == NULL || curdir == nullstr) |
| 1763 | setpwd(0, 1); |
| 1764 | else |
| 1765 | setpwd(dest, 1); |
| 1766 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1767 | INTON; |
| 1768 | if (print && iflag) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 1769 | puts(curdir); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1770 | return 0; |
| 1771 | } |
| 1772 | |
| 1773 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1774 | static int |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 1775 | pwdcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1776 | { |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 1777 | puts(curdir); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1778 | return 0; |
| 1779 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1780 | |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 1781 | /* Ask system the current directory */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1782 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1783 | getpwd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1784 | { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1785 | curdir = xgetcwd(0); |
| 1786 | if(curdir==0) |
| 1787 | curdir = nullstr; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | static void |
| 1791 | setpwd(const char *val, int setold) |
| 1792 | { |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 1793 | char *cated = NULL; |
| 1794 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1795 | if (setold) { |
| 1796 | setvar("OLDPWD", curdir, VEXPORT); |
| 1797 | } |
| 1798 | INTOFF; |
| 1799 | if (curdir != nullstr) { |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 1800 | if(val!=NULL && *val != '/') |
| 1801 | val = cated = concat_path_file(curdir, val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1802 | free(curdir); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1803 | } |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 1804 | if (!val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1805 | getpwd(); |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 1806 | else |
| 1807 | curdir = simplify_path(val); |
| 1808 | free(cated); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1809 | INTON; |
| 1810 | setvar("PWD", curdir, VEXPORT); |
| 1811 | } |
| 1812 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1813 | /* |
| 1814 | * Errors and exceptions. |
| 1815 | */ |
| 1816 | |
| 1817 | /* |
| 1818 | * Code to handle exceptions in C. |
| 1819 | */ |
| 1820 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1821 | /* |
| 1822 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 1823 | * jump locations. The global variable handler contains the location to |
| 1824 | * jump to when an exception occurs, and the global variable exception |
| 1825 | * contains a code identifying the exeception. To implement nested |
| 1826 | * exception handlers, the user should save the value of handler on entry |
| 1827 | * to an inner scope, set handler to point to a jmploc structure for the |
| 1828 | * inner scope, and restore handler on exit from the scope. |
| 1829 | */ |
| 1830 | |
| 1831 | struct jmploc { |
| 1832 | jmp_buf loc; |
| 1833 | }; |
| 1834 | |
| 1835 | /* exceptions */ |
| 1836 | #define EXINT 0 /* SIGINT received */ |
| 1837 | #define EXERROR 1 /* a generic error */ |
| 1838 | #define EXSHELLPROC 2 /* execute a shell procedure */ |
| 1839 | #define EXEXEC 3 /* command execution failed */ |
| 1840 | |
| 1841 | static struct jmploc *handler; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1842 | static int exception; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1843 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1844 | static void exverror (int, const char *, va_list) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1845 | __attribute__((__noreturn__)); |
| 1846 | |
| 1847 | /* |
| 1848 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 1849 | * just do a longjmp to the exception handler. The type of exception is |
| 1850 | * stored in the global variable "exception". |
| 1851 | */ |
| 1852 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1853 | static void exraise (int) __attribute__((__noreturn__)); |
| 1854 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1855 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1856 | exraise(int e) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1857 | { |
| 1858 | #ifdef DEBUG |
| 1859 | if (handler == NULL) |
| 1860 | abort(); |
| 1861 | #endif |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 1862 | flushall(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1863 | exception = e; |
| 1864 | longjmp(handler->loc, 1); |
| 1865 | } |
| 1866 | |
| 1867 | |
| 1868 | /* |
| 1869 | * Called from trap.c when a SIGINT is received. (If the user specifies |
| 1870 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 1871 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 1872 | * are held using the INTOFF macro. The call to _exit is necessary because |
| 1873 | * there is a short period after a fork before the signal handlers are |
| 1874 | * set to the appropriate value for the child. (The test for iflag is |
| 1875 | * just defensive programming.) |
| 1876 | */ |
| 1877 | |
| 1878 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1879 | onint(void) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1880 | sigset_t mysigset; |
| 1881 | |
| 1882 | if (suppressint) { |
| 1883 | intpending++; |
| 1884 | return; |
| 1885 | } |
| 1886 | intpending = 0; |
| 1887 | sigemptyset(&mysigset); |
| 1888 | sigprocmask(SIG_SETMASK, &mysigset, NULL); |
| 1889 | if (rootshell && iflag) |
| 1890 | exraise(EXINT); |
| 1891 | else { |
| 1892 | signal(SIGINT, SIG_DFL); |
| 1893 | raise(SIGINT); |
| 1894 | } |
| 1895 | /* NOTREACHED */ |
| 1896 | } |
| 1897 | |
| 1898 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1899 | static char *commandname; /* currently executing command */ |
| 1900 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1901 | /* |
| 1902 | * Exverror is called to raise the error exception. If the first argument |
| 1903 | * is not NULL then error prints an error message using printf style |
| 1904 | * formatting. It then raises the error exception. |
| 1905 | */ |
| 1906 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1907 | exverror(int cond, const char *msg, va_list ap) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1908 | { |
| 1909 | CLEAR_PENDING_INT; |
| 1910 | INTOFF; |
| 1911 | |
| 1912 | #ifdef DEBUG |
| 1913 | if (msg) |
| 1914 | TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid())); |
| 1915 | else |
| 1916 | TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid())); |
| 1917 | #endif |
| 1918 | if (msg) { |
| 1919 | if (commandname) |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 1920 | out2fmt("%s: ", commandname); |
| 1921 | vfprintf(stderr, msg, ap); |
| 1922 | out2c('\n'); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1923 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1924 | exraise(cond); |
| 1925 | /* NOTREACHED */ |
| 1926 | } |
| 1927 | |
| 1928 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 1929 | static void |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1930 | error(const char *msg, ...) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1931 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1932 | va_list ap; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1933 | va_start(ap, msg); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1934 | exverror(EXERROR, msg, ap); |
| 1935 | /* NOTREACHED */ |
| 1936 | va_end(ap); |
| 1937 | } |
| 1938 | |
| 1939 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1940 | static void |
| 1941 | exerror(int cond, const char *msg, ...) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1942 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1943 | va_list ap; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1944 | va_start(ap, msg); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1945 | exverror(cond, msg, ap); |
| 1946 | /* NOTREACHED */ |
| 1947 | va_end(ap); |
| 1948 | } |
| 1949 | |
| 1950 | |
| 1951 | |
| 1952 | /* |
| 1953 | * Table of error messages. |
| 1954 | */ |
| 1955 | |
| 1956 | struct errname { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1957 | short errcode; /* error number */ |
Aaron Lehmann | 4388033 | 2001-12-31 06:16:54 +0000 | [diff] [blame] | 1958 | short action; /* operation which encountered the error */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1959 | }; |
| 1960 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1961 | /* |
| 1962 | * Types of operations (passed to the errmsg routine). |
| 1963 | */ |
| 1964 | |
| 1965 | #define E_OPEN 01 /* opening a file */ |
| 1966 | #define E_CREAT 02 /* creating a file */ |
| 1967 | #define E_EXEC 04 /* executing a program */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1968 | |
| 1969 | #define ALL (E_OPEN|E_CREAT|E_EXEC) |
| 1970 | |
| 1971 | static const struct errname errormsg[] = { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1972 | { EINTR, ALL }, |
| 1973 | { EACCES, ALL }, |
| 1974 | { EIO, ALL }, |
| 1975 | { ENOENT, E_OPEN }, |
| 1976 | { ENOENT, E_CREAT }, |
| 1977 | { ENOENT, E_EXEC }, |
| 1978 | { ENOTDIR, E_OPEN }, |
| 1979 | { ENOTDIR, E_CREAT }, |
| 1980 | { ENOTDIR, E_EXEC }, |
| 1981 | { EISDIR, ALL }, |
| 1982 | { EEXIST, E_CREAT }, |
| 1983 | #ifdef EMFILE |
| 1984 | { EMFILE, ALL }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1985 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1986 | { ENFILE, ALL }, |
| 1987 | { ENOSPC, ALL }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1988 | #ifdef EDQUOT |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1989 | { EDQUOT, ALL }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1990 | #endif |
| 1991 | #ifdef ENOSR |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1992 | { ENOSR, ALL }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1993 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1994 | { ENXIO, ALL }, |
| 1995 | { EROFS, ALL }, |
| 1996 | { ETXTBSY, ALL }, |
| 1997 | #ifdef EAGAIN |
| 1998 | { EAGAIN, E_EXEC }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 1999 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2000 | { ENOMEM, ALL }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2001 | #ifdef ENOLINK |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2002 | { ENOLINK, ALL }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2003 | #endif |
| 2004 | #ifdef EMULTIHOP |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2005 | { EMULTIHOP, ALL }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2006 | #endif |
| 2007 | #ifdef ECOMM |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2008 | { ECOMM, ALL }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2009 | #endif |
| 2010 | #ifdef ESTALE |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2011 | { ESTALE, ALL }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2012 | #endif |
| 2013 | #ifdef ETIMEDOUT |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2014 | { ETIMEDOUT, ALL }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2015 | #endif |
| 2016 | #ifdef ELOOP |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2017 | { ELOOP, ALL }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2018 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2019 | { E2BIG, E_EXEC }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2020 | #ifdef ELIBACC |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2021 | { ELIBACC, E_EXEC }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2022 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2023 | }; |
| 2024 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2025 | #define ERRNAME_SIZE (sizeof(errormsg)/sizeof(struct errname)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2026 | |
| 2027 | /* |
| 2028 | * Return a string describing an error. The returned string may be a |
| 2029 | * pointer to a static buffer that will be overwritten on the next call. |
| 2030 | * Action describes the operation that got the error. |
| 2031 | */ |
| 2032 | |
| 2033 | static const char * |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2034 | errmsg(int e, int action) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2035 | { |
| 2036 | struct errname const *ep; |
| 2037 | static char buf[12]; |
| 2038 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2039 | for (ep = errormsg ; ep < errormsg+ERRNAME_SIZE; ep++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2040 | if (ep->errcode == e && (ep->action & action) != 0) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2041 | return strerror(e); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2042 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2043 | |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 2044 | snprintf(buf, sizeof buf, "error %d", e); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2045 | return buf; |
| 2046 | } |
| 2047 | |
| 2048 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 2049 | #ifdef CONFIG_ASH_OPTIMIZE_FOR_SIZE |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2050 | static void |
| 2051 | __inton() { |
| 2052 | if (--suppressint == 0 && intpending) { |
| 2053 | onint(); |
| 2054 | } |
| 2055 | } |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 2056 | static void forceinton (void) { |
| 2057 | suppressint = 0; |
| 2058 | if (intpending) |
| 2059 | onint(); |
| 2060 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2061 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2062 | |
| 2063 | /* flags in argument to evaltree */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2064 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 2065 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
| 2066 | #define EV_BACKCMD 04 /* command executing within back quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2067 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2068 | static int evalskip; /* set if we are skipping commands */ |
| 2069 | static int skipcount; /* number of levels to skip */ |
| 2070 | static int loopnest; /* current loop nesting level */ |
| 2071 | static int funcnest; /* depth of function calls */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2072 | |
| 2073 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2074 | static struct strlist *cmdenviron; /* environment for builtin command */ |
| 2075 | static int exitstatus; /* exit status of last command */ |
| 2076 | static int oexitstatus; /* saved exit status */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2077 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2078 | static void evalsubshell (const union node *, int); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2079 | static void expredir (union node *); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2080 | static void eprintlist (struct strlist *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2081 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2082 | static union node *parsecmd(int); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2083 | /* |
| 2084 | * Called to reset things after an exception. |
| 2085 | */ |
| 2086 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2087 | /* |
| 2088 | * The eval commmand. |
| 2089 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2090 | static void evalstring (char *, int); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2091 | |
| 2092 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 2093 | evalcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2094 | { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2095 | char *p; |
| 2096 | char *concat; |
| 2097 | char **ap; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2098 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2099 | if (argc > 1) { |
| 2100 | p = argv[1]; |
| 2101 | if (argc > 2) { |
| 2102 | STARTSTACKSTR(concat); |
| 2103 | ap = argv + 2; |
| 2104 | for (;;) { |
| 2105 | while (*p) |
| 2106 | STPUTC(*p++, concat); |
| 2107 | if ((p = *ap++) == NULL) |
| 2108 | break; |
| 2109 | STPUTC(' ', concat); |
| 2110 | } |
| 2111 | STPUTC('\0', concat); |
| 2112 | p = grabstackstr(concat); |
| 2113 | } |
| 2114 | evalstring(p, EV_TESTED); |
| 2115 | } |
| 2116 | return exitstatus; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2119 | /* |
| 2120 | * Execute a command or commands contained in a string. |
| 2121 | */ |
| 2122 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2123 | static void evaltree (union node *, int); |
| 2124 | static void setinputstring (char *); |
| 2125 | static void popfile (void); |
| 2126 | static void setstackmark(struct stackmark *mark); |
| 2127 | static void popstackmark(struct stackmark *mark); |
| 2128 | |
| 2129 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2130 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2131 | evalstring(char *s, int flag) |
| 2132 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2133 | union node *n; |
| 2134 | struct stackmark smark; |
| 2135 | |
| 2136 | setstackmark(&smark); |
| 2137 | setinputstring(s); |
| 2138 | while ((n = parsecmd(0)) != NEOF) { |
| 2139 | evaltree(n, flag); |
| 2140 | popstackmark(&smark); |
| 2141 | } |
| 2142 | popfile(); |
| 2143 | popstackmark(&smark); |
| 2144 | } |
| 2145 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2146 | static struct builtincmd *find_builtin (const char *); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2147 | static void expandarg (union node *, struct arglist *, int); |
| 2148 | static void calcsize (const union node *); |
| 2149 | static union node *copynode (const union node *); |
| 2150 | |
| 2151 | /* |
| 2152 | * Make a copy of a parse tree. |
| 2153 | */ |
| 2154 | |
| 2155 | static int funcblocksize; /* size of structures in function */ |
| 2156 | static int funcstringsize; /* size of strings in node */ |
| 2157 | static pointer funcblock; /* block to allocate function from */ |
| 2158 | static char *funcstring; /* block to allocate strings from */ |
| 2159 | |
| 2160 | |
| 2161 | static inline union node * |
| 2162 | copyfunc(union node *n) |
| 2163 | { |
| 2164 | if (n == NULL) |
| 2165 | return NULL; |
| 2166 | funcblocksize = 0; |
| 2167 | funcstringsize = 0; |
| 2168 | calcsize(n); |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 2169 | funcblock = xmalloc(funcblocksize + funcstringsize); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2170 | funcstring = (char *) funcblock + funcblocksize; |
| 2171 | return copynode(n); |
| 2172 | } |
| 2173 | |
| 2174 | /* |
| 2175 | * Free a parse tree. |
| 2176 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2177 | |
| 2178 | static void |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2179 | freefunc(union node *n) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2180 | { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2181 | if (n) |
| 2182 | ckfree(n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2186 | /* |
| 2187 | * Add a new command entry, replacing any existing command entry for |
| 2188 | * the same name. |
| 2189 | */ |
| 2190 | |
| 2191 | static inline void |
| 2192 | addcmdentry(char *name, struct cmdentry *entry) |
| 2193 | { |
| 2194 | struct tblentry *cmdp; |
| 2195 | |
| 2196 | INTOFF; |
| 2197 | cmdp = cmdlookup(name, 1); |
| 2198 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 2199 | freefunc(cmdp->param.func); |
| 2200 | } |
| 2201 | cmdp->cmdtype = entry->cmdtype; |
| 2202 | cmdp->param = entry->u; |
| 2203 | INTON; |
| 2204 | } |
| 2205 | |
| 2206 | static inline void |
| 2207 | evalloop(const union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2208 | { |
| 2209 | int status; |
| 2210 | |
| 2211 | loopnest++; |
| 2212 | status = 0; |
| 2213 | for (;;) { |
| 2214 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 2215 | if (evalskip) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2216 | skipping: if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2217 | evalskip = 0; |
| 2218 | continue; |
| 2219 | } |
| 2220 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 2221 | evalskip = 0; |
| 2222 | break; |
| 2223 | } |
| 2224 | if (n->type == NWHILE) { |
| 2225 | if (exitstatus != 0) |
| 2226 | break; |
| 2227 | } else { |
| 2228 | if (exitstatus == 0) |
| 2229 | break; |
| 2230 | } |
| 2231 | evaltree(n->nbinary.ch2, flags & EV_TESTED); |
| 2232 | status = exitstatus; |
| 2233 | if (evalskip) |
| 2234 | goto skipping; |
| 2235 | } |
| 2236 | loopnest--; |
| 2237 | exitstatus = status; |
| 2238 | } |
| 2239 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2240 | static void |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2241 | evalfor(const union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2242 | { |
| 2243 | struct arglist arglist; |
| 2244 | union node *argp; |
| 2245 | struct strlist *sp; |
| 2246 | struct stackmark smark; |
| 2247 | |
| 2248 | setstackmark(&smark); |
| 2249 | arglist.lastp = &arglist.list; |
| 2250 | for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { |
| 2251 | oexitstatus = exitstatus; |
| 2252 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); |
| 2253 | if (evalskip) |
| 2254 | goto out; |
| 2255 | } |
| 2256 | *arglist.lastp = NULL; |
| 2257 | |
| 2258 | exitstatus = 0; |
| 2259 | loopnest++; |
| 2260 | for (sp = arglist.list ; sp ; sp = sp->next) { |
| 2261 | setvar(n->nfor.var, sp->text, 0); |
| 2262 | evaltree(n->nfor.body, flags & EV_TESTED); |
| 2263 | if (evalskip) { |
| 2264 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 2265 | evalskip = 0; |
| 2266 | continue; |
| 2267 | } |
| 2268 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 2269 | evalskip = 0; |
| 2270 | break; |
| 2271 | } |
| 2272 | } |
| 2273 | loopnest--; |
| 2274 | out: |
| 2275 | popstackmark(&smark); |
| 2276 | } |
| 2277 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2278 | static inline void |
| 2279 | evalcase(const union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2280 | { |
| 2281 | union node *cp; |
| 2282 | union node *patp; |
| 2283 | struct arglist arglist; |
| 2284 | struct stackmark smark; |
| 2285 | |
| 2286 | setstackmark(&smark); |
| 2287 | arglist.lastp = &arglist.list; |
| 2288 | oexitstatus = exitstatus; |
| 2289 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
| 2290 | for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { |
| 2291 | for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { |
| 2292 | if (casematch(patp, arglist.list->text)) { |
| 2293 | if (evalskip == 0) { |
| 2294 | evaltree(cp->nclist.body, flags); |
| 2295 | } |
| 2296 | goto out; |
| 2297 | } |
| 2298 | } |
| 2299 | } |
| 2300 | out: |
| 2301 | popstackmark(&smark); |
| 2302 | } |
| 2303 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2304 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2305 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 2306 | * of the process creating the pipeline. (This differs from some versions |
| 2307 | * of the shell, which make the last process in a pipeline the parent |
| 2308 | * of all the rest.) |
| 2309 | */ |
| 2310 | |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 2311 | static inline void evalpipe(union node *n) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2312 | { |
| 2313 | struct job *jp; |
| 2314 | struct nodelist *lp; |
| 2315 | int pipelen; |
| 2316 | int prevfd; |
| 2317 | int pip[2]; |
| 2318 | |
| 2319 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
| 2320 | pipelen = 0; |
| 2321 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) |
| 2322 | pipelen++; |
| 2323 | INTOFF; |
| 2324 | jp = makejob(n, pipelen); |
| 2325 | prevfd = -1; |
| 2326 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 2327 | /* |
| 2328 | * Search for a command. This is called before we fork so that the |
| 2329 | * location of the command will be available in the parent as well as |
| 2330 | * the child. The check for "goodname" is an overly conservative |
| 2331 | * check that the name will not be subject to expansion. |
| 2332 | */ |
| 2333 | |
| 2334 | struct cmdentry entry; |
| 2335 | union node *lpn = lp->n; |
| 2336 | if (lpn->type == NCMD && lpn->ncmd.args && goodname(lpn->ncmd.args->narg.text)) |
| 2337 | find_command(lpn->ncmd.args->narg.text, &entry, 0, pathval()); |
| 2338 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2339 | pip[1] = -1; |
| 2340 | if (lp->next) { |
| 2341 | if (pipe(pip) < 0) { |
| 2342 | close(prevfd); |
| 2343 | error("Pipe call failed"); |
| 2344 | } |
| 2345 | } |
| 2346 | if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { |
| 2347 | INTON; |
| 2348 | if (prevfd > 0) { |
| 2349 | close(0); |
| 2350 | dup_as_newfd(prevfd, 0); |
| 2351 | close(prevfd); |
| 2352 | if (pip[0] == 0) { |
| 2353 | pip[0] = -1; |
| 2354 | } |
| 2355 | } |
| 2356 | if (pip[1] >= 0) { |
| 2357 | if (pip[0] >= 0) { |
| 2358 | close(pip[0]); |
| 2359 | } |
| 2360 | if (pip[1] != 1) { |
| 2361 | close(1); |
| 2362 | dup_as_newfd(pip[1], 1); |
| 2363 | close(pip[1]); |
| 2364 | } |
| 2365 | } |
| 2366 | evaltree(lp->n, EV_EXIT); |
| 2367 | } |
| 2368 | if (prevfd >= 0) |
| 2369 | close(prevfd); |
| 2370 | prevfd = pip[0]; |
| 2371 | close(pip[1]); |
| 2372 | } |
| 2373 | INTON; |
| 2374 | if (n->npipe.backgnd == 0) { |
| 2375 | INTOFF; |
| 2376 | exitstatus = waitforjob(jp); |
| 2377 | TRACE(("evalpipe: job done exit status %d\n", exitstatus)); |
| 2378 | INTON; |
| 2379 | } |
| 2380 | } |
| 2381 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2382 | static int |
| 2383 | isassignment(const char *word) { |
| 2384 | if (!is_name(*word)) { |
| 2385 | return 0; |
| 2386 | } |
| 2387 | do { |
| 2388 | word++; |
| 2389 | } while (is_in_name(*word)); |
| 2390 | return *word == '='; |
| 2391 | } |
| 2392 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2393 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2394 | static void |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 2395 | evalcommand(union node *cmd, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2396 | { |
| 2397 | struct stackmark smark; |
| 2398 | union node *argp; |
| 2399 | struct arglist arglist; |
| 2400 | struct arglist varlist; |
| 2401 | char **argv; |
| 2402 | int argc; |
| 2403 | char **envp; |
| 2404 | struct strlist *sp; |
| 2405 | int mode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2406 | struct cmdentry cmdentry; |
| 2407 | struct job *jp; |
| 2408 | char *volatile savecmdname; |
| 2409 | volatile struct shparam saveparam; |
| 2410 | struct localvar *volatile savelocalvars; |
| 2411 | volatile int e; |
| 2412 | char *lastarg; |
| 2413 | const char *path; |
| 2414 | const struct builtincmd *firstbltin; |
| 2415 | struct jmploc *volatile savehandler; |
| 2416 | struct jmploc jmploc; |
| 2417 | #if __GNUC__ |
| 2418 | /* Avoid longjmp clobbering */ |
| 2419 | (void) &argv; |
| 2420 | (void) &argc; |
| 2421 | (void) &lastarg; |
| 2422 | (void) &flags; |
| 2423 | #endif |
| 2424 | |
| 2425 | /* First expand the arguments. */ |
| 2426 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 2427 | setstackmark(&smark); |
| 2428 | arglist.lastp = &arglist.list; |
| 2429 | varlist.lastp = &varlist.list; |
| 2430 | arglist.list = 0; |
| 2431 | oexitstatus = exitstatus; |
| 2432 | exitstatus = 0; |
| 2433 | path = pathval(); |
| 2434 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 2435 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 2436 | } |
| 2437 | for ( |
| 2438 | argp = cmd->ncmd.args; argp && !arglist.list; |
| 2439 | argp = argp->narg.next |
| 2440 | ) { |
| 2441 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 2442 | } |
| 2443 | if (argp) { |
| 2444 | struct builtincmd *bcmd; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2445 | int pseudovarflag; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2446 | bcmd = find_builtin(arglist.list->text); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2447 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2448 | for (; argp; argp = argp->narg.next) { |
| 2449 | if (pseudovarflag && isassignment(argp->narg.text)) { |
| 2450 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 2451 | continue; |
| 2452 | } |
| 2453 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 2454 | } |
| 2455 | } |
| 2456 | *arglist.lastp = NULL; |
| 2457 | *varlist.lastp = NULL; |
| 2458 | expredir(cmd->ncmd.redirect); |
| 2459 | argc = 0; |
| 2460 | for (sp = arglist.list ; sp ; sp = sp->next) |
| 2461 | argc++; |
| 2462 | argv = stalloc(sizeof (char *) * (argc + 1)); |
| 2463 | |
| 2464 | for (sp = arglist.list ; sp ; sp = sp->next) { |
| 2465 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 2466 | *argv++ = sp->text; |
| 2467 | } |
| 2468 | *argv = NULL; |
| 2469 | lastarg = NULL; |
| 2470 | if (iflag && funcnest == 0 && argc > 0) |
| 2471 | lastarg = argv[-1]; |
| 2472 | argv -= argc; |
| 2473 | |
| 2474 | /* Print the command if xflag is set. */ |
| 2475 | if (xflag) { |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 2476 | out2c('+'); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2477 | eprintlist(varlist.list); |
| 2478 | eprintlist(arglist.list); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 2479 | out2c('\n'); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | /* Now locate the command. */ |
| 2483 | if (argc == 0) { |
| 2484 | cmdentry.cmdtype = CMDBUILTIN; |
| 2485 | firstbltin = cmdentry.u.cmd = BLTINCMD; |
| 2486 | } else { |
| 2487 | const char *oldpath; |
| 2488 | int findflag = DO_ERR; |
| 2489 | int oldfindflag; |
| 2490 | |
| 2491 | /* |
| 2492 | * Modify the command lookup path, if a PATH= assignment |
| 2493 | * is present |
| 2494 | */ |
| 2495 | for (sp = varlist.list ; sp ; sp = sp->next) |
| 2496 | if (varequal(sp->text, defpathvar)) { |
| 2497 | path = sp->text + 5; |
| 2498 | findflag |= DO_BRUTE; |
| 2499 | } |
| 2500 | oldpath = path; |
| 2501 | oldfindflag = findflag; |
| 2502 | firstbltin = 0; |
| 2503 | for(;;) { |
| 2504 | find_command(argv[0], &cmdentry, findflag, path); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2505 | if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2506 | exitstatus = 127; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2507 | goto out; |
| 2508 | } |
| 2509 | /* implement bltin and command here */ |
| 2510 | if (cmdentry.cmdtype != CMDBUILTIN) { |
| 2511 | break; |
| 2512 | } |
| 2513 | if (!firstbltin) { |
| 2514 | firstbltin = cmdentry.u.cmd; |
| 2515 | } |
| 2516 | if (cmdentry.u.cmd == BLTINCMD) { |
| 2517 | for(;;) { |
| 2518 | struct builtincmd *bcmd; |
| 2519 | |
| 2520 | argv++; |
| 2521 | if (--argc == 0) |
| 2522 | goto found; |
| 2523 | if (!(bcmd = find_builtin(*argv))) { |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 2524 | out2fmt("%s: not found\n", *argv); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2525 | exitstatus = 127; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2526 | goto out; |
| 2527 | } |
| 2528 | cmdentry.u.cmd = bcmd; |
| 2529 | if (bcmd != BLTINCMD) |
| 2530 | break; |
| 2531 | } |
| 2532 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2533 | if (cmdentry.u.cmd == find_builtin("command")) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2534 | argv++; |
| 2535 | if (--argc == 0) { |
| 2536 | goto found; |
| 2537 | } |
| 2538 | if (*argv[0] == '-') { |
| 2539 | if (!equal(argv[0], "-p")) { |
| 2540 | argv--; |
| 2541 | argc++; |
| 2542 | break; |
| 2543 | } |
| 2544 | argv++; |
| 2545 | if (--argc == 0) { |
| 2546 | goto found; |
| 2547 | } |
| 2548 | path = defpath; |
| 2549 | findflag |= DO_BRUTE; |
| 2550 | } else { |
| 2551 | path = oldpath; |
| 2552 | findflag = oldfindflag; |
| 2553 | } |
| 2554 | findflag |= DO_NOFUN; |
| 2555 | continue; |
| 2556 | } |
| 2557 | found: |
| 2558 | break; |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | /* Fork off a child process if necessary. */ |
| 2563 | if (cmd->ncmd.backgnd |
| 2564 | || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2565 | ) { |
| 2566 | jp = makejob(cmd, 1); |
| 2567 | mode = cmd->ncmd.backgnd; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2568 | if (forkshell(jp, cmd, mode) != 0) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2569 | goto parent; /* at end of routine */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2570 | flags |= EV_EXIT; |
| 2571 | } |
| 2572 | |
| 2573 | /* This is the child process if a fork occurred. */ |
| 2574 | /* Execute the command. */ |
| 2575 | if (cmdentry.cmdtype == CMDFUNCTION) { |
| 2576 | #ifdef DEBUG |
| 2577 | trputs("Shell function: "); trargs(argv); |
| 2578 | #endif |
| 2579 | exitstatus = oexitstatus; |
| 2580 | redirect(cmd->ncmd.redirect, REDIR_PUSH); |
| 2581 | saveparam = shellparam; |
| 2582 | shellparam.malloc = 0; |
| 2583 | shellparam.nparam = argc - 1; |
| 2584 | shellparam.p = argv + 1; |
| 2585 | INTOFF; |
| 2586 | savelocalvars = localvars; |
| 2587 | localvars = NULL; |
| 2588 | INTON; |
| 2589 | if (setjmp(jmploc.loc)) { |
| 2590 | if (exception == EXSHELLPROC) { |
| 2591 | freeparam((volatile struct shparam *) |
| 2592 | &saveparam); |
| 2593 | } else { |
| 2594 | saveparam.optind = shellparam.optind; |
| 2595 | saveparam.optoff = shellparam.optoff; |
| 2596 | freeparam(&shellparam); |
| 2597 | shellparam = saveparam; |
| 2598 | } |
| 2599 | poplocalvars(); |
| 2600 | localvars = savelocalvars; |
| 2601 | handler = savehandler; |
| 2602 | longjmp(handler->loc, 1); |
| 2603 | } |
| 2604 | savehandler = handler; |
| 2605 | handler = &jmploc; |
| 2606 | for (sp = varlist.list ; sp ; sp = sp->next) |
| 2607 | mklocal(sp->text); |
| 2608 | funcnest++; |
| 2609 | evaltree(cmdentry.u.func, flags & EV_TESTED); |
| 2610 | funcnest--; |
| 2611 | INTOFF; |
| 2612 | poplocalvars(); |
| 2613 | localvars = savelocalvars; |
| 2614 | saveparam.optind = shellparam.optind; |
| 2615 | saveparam.optoff = shellparam.optoff; |
| 2616 | freeparam(&shellparam); |
| 2617 | shellparam = saveparam; |
| 2618 | handler = savehandler; |
| 2619 | popredir(); |
| 2620 | INTON; |
| 2621 | if (evalskip == SKIPFUNC) { |
| 2622 | evalskip = 0; |
| 2623 | skipcount = 0; |
| 2624 | } |
| 2625 | if (flags & EV_EXIT) |
| 2626 | exitshell(exitstatus); |
| 2627 | } else if (cmdentry.cmdtype == CMDBUILTIN) { |
| 2628 | #ifdef DEBUG |
| 2629 | trputs("builtin command: "); trargs(argv); |
| 2630 | #endif |
| 2631 | mode = (cmdentry.u.cmd == EXECCMD)? 0 : REDIR_PUSH; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2632 | redirect(cmd->ncmd.redirect, mode); |
| 2633 | savecmdname = commandname; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2634 | if (IS_BUILTIN_SPECIAL(firstbltin)) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2635 | listsetvar(varlist.list); |
| 2636 | } else { |
| 2637 | cmdenviron = varlist.list; |
| 2638 | } |
| 2639 | e = -1; |
| 2640 | if (setjmp(jmploc.loc)) { |
| 2641 | e = exception; |
| 2642 | exitstatus = (e == EXINT)? SIGINT+128 : 2; |
| 2643 | goto cmddone; |
| 2644 | } |
| 2645 | savehandler = handler; |
| 2646 | handler = &jmploc; |
| 2647 | commandname = argv[0]; |
| 2648 | argptr = argv + 1; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2649 | optptr = NULL; /* initialize nextopt */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2650 | exitstatus = (*cmdentry.u.cmd->builtinfunc)(argc, argv); |
| 2651 | flushall(); |
| 2652 | cmddone: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2653 | cmdenviron = NULL; |
| 2654 | if (e != EXSHELLPROC) { |
| 2655 | commandname = savecmdname; |
| 2656 | if (flags & EV_EXIT) |
| 2657 | exitshell(exitstatus); |
| 2658 | } |
| 2659 | handler = savehandler; |
| 2660 | if (e != -1) { |
| 2661 | if ((e != EXERROR && e != EXEXEC) |
| 2662 | || cmdentry.u.cmd == BLTINCMD |
| 2663 | || cmdentry.u.cmd == DOTCMD |
| 2664 | || cmdentry.u.cmd == EVALCMD |
| 2665 | || cmdentry.u.cmd == EXECCMD) |
| 2666 | exraise(e); |
| 2667 | FORCEINTON; |
| 2668 | } |
| 2669 | if (cmdentry.u.cmd != EXECCMD) |
| 2670 | popredir(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2671 | } else { |
| 2672 | #ifdef DEBUG |
| 2673 | trputs("normal command: "); trargs(argv); |
| 2674 | #endif |
| 2675 | redirect(cmd->ncmd.redirect, 0); |
| 2676 | clearredir(); |
| 2677 | for (sp = varlist.list ; sp ; sp = sp->next) |
| 2678 | setvareq(sp->text, VEXPORT|VSTACK); |
| 2679 | envp = environment(); |
| 2680 | shellexec(argv, envp, path, cmdentry.u.index); |
| 2681 | } |
| 2682 | goto out; |
| 2683 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2684 | parent: /* parent process gets here (if we forked) */ |
| 2685 | if (mode == 0) { /* argument to fork */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2686 | INTOFF; |
| 2687 | exitstatus = waitforjob(jp); |
| 2688 | INTON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
| 2691 | out: |
| 2692 | if (lastarg) |
| 2693 | setvar("_", lastarg, 0); |
| 2694 | popstackmark(&smark); |
| 2695 | } |
| 2696 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2697 | /* |
| 2698 | * Evaluate a parse tree. The value is left in the global variable |
| 2699 | * exitstatus. |
| 2700 | */ |
| 2701 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 2702 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2703 | { |
| 2704 | int checkexit = 0; |
| 2705 | if (n == NULL) { |
| 2706 | TRACE(("evaltree(NULL) called\n")); |
| 2707 | goto out; |
| 2708 | } |
| 2709 | TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type)); |
| 2710 | switch (n->type) { |
| 2711 | case NSEMI: |
| 2712 | evaltree(n->nbinary.ch1, flags & EV_TESTED); |
| 2713 | if (evalskip) |
| 2714 | goto out; |
| 2715 | evaltree(n->nbinary.ch2, flags); |
| 2716 | break; |
| 2717 | case NAND: |
| 2718 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 2719 | if (evalskip || exitstatus != 0) |
| 2720 | goto out; |
| 2721 | evaltree(n->nbinary.ch2, flags); |
| 2722 | break; |
| 2723 | case NOR: |
| 2724 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 2725 | if (evalskip || exitstatus == 0) |
| 2726 | goto out; |
| 2727 | evaltree(n->nbinary.ch2, flags); |
| 2728 | break; |
| 2729 | case NREDIR: |
| 2730 | expredir(n->nredir.redirect); |
| 2731 | redirect(n->nredir.redirect, REDIR_PUSH); |
| 2732 | evaltree(n->nredir.n, flags); |
| 2733 | popredir(); |
| 2734 | break; |
| 2735 | case NSUBSHELL: |
| 2736 | evalsubshell(n, flags); |
| 2737 | break; |
| 2738 | case NBACKGND: |
| 2739 | evalsubshell(n, flags); |
| 2740 | break; |
| 2741 | case NIF: { |
| 2742 | evaltree(n->nif.test, EV_TESTED); |
| 2743 | if (evalskip) |
| 2744 | goto out; |
| 2745 | if (exitstatus == 0) |
| 2746 | evaltree(n->nif.ifpart, flags); |
| 2747 | else if (n->nif.elsepart) |
| 2748 | evaltree(n->nif.elsepart, flags); |
| 2749 | else |
| 2750 | exitstatus = 0; |
| 2751 | break; |
| 2752 | } |
| 2753 | case NWHILE: |
| 2754 | case NUNTIL: |
| 2755 | evalloop(n, flags); |
| 2756 | break; |
| 2757 | case NFOR: |
| 2758 | evalfor(n, flags); |
| 2759 | break; |
| 2760 | case NCASE: |
| 2761 | evalcase(n, flags); |
| 2762 | break; |
| 2763 | case NDEFUN: { |
| 2764 | struct builtincmd *bcmd; |
| 2765 | struct cmdentry entry; |
| 2766 | if ( |
| 2767 | (bcmd = find_builtin(n->narg.text)) && |
| 2768 | IS_BUILTIN_SPECIAL(bcmd) |
| 2769 | ) { |
| 2770 | out2fmt("%s is a special built-in\n", n->narg.text); |
| 2771 | exitstatus = 1; |
| 2772 | break; |
| 2773 | } |
| 2774 | entry.cmdtype = CMDFUNCTION; |
| 2775 | entry.u.func = copyfunc(n->narg.next); |
| 2776 | addcmdentry(n->narg.text, &entry); |
| 2777 | exitstatus = 0; |
| 2778 | break; |
| 2779 | } |
| 2780 | case NNOT: |
| 2781 | evaltree(n->nnot.com, EV_TESTED); |
| 2782 | exitstatus = !exitstatus; |
| 2783 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2784 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2785 | case NPIPE: |
| 2786 | evalpipe(n); |
| 2787 | checkexit = 1; |
| 2788 | break; |
| 2789 | case NCMD: |
| 2790 | evalcommand(n, flags); |
| 2791 | checkexit = 1; |
| 2792 | break; |
| 2793 | #ifdef DEBUG |
| 2794 | default: |
| 2795 | printf("Node type = %d\n", n->type); |
| 2796 | break; |
| 2797 | #endif |
| 2798 | } |
| 2799 | out: |
| 2800 | if (pendingsigs) |
| 2801 | dotrap(); |
| 2802 | if ( |
| 2803 | flags & EV_EXIT || |
| 2804 | (checkexit && eflag && exitstatus && !(flags & EV_TESTED)) |
| 2805 | ) |
| 2806 | exitshell(exitstatus); |
| 2807 | } |
| 2808 | |
| 2809 | /* |
| 2810 | * Kick off a subshell to evaluate a tree. |
| 2811 | */ |
| 2812 | |
| 2813 | static void |
| 2814 | evalsubshell(const union node *n, int flags) |
| 2815 | { |
| 2816 | struct job *jp; |
| 2817 | int backgnd = (n->type == NBACKGND); |
| 2818 | |
| 2819 | expredir(n->nredir.redirect); |
| 2820 | jp = makejob(n, 1); |
| 2821 | if (forkshell(jp, n, backgnd) == 0) { |
| 2822 | if (backgnd) |
| 2823 | flags &=~ EV_TESTED; |
| 2824 | redirect(n->nredir.redirect, 0); |
| 2825 | evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ |
| 2826 | } |
| 2827 | if (! backgnd) { |
| 2828 | INTOFF; |
| 2829 | exitstatus = waitforjob(jp); |
| 2830 | INTON; |
| 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | /* |
| 2835 | * Compute the names of the files in a redirection list. |
| 2836 | */ |
| 2837 | |
| 2838 | static void fixredir(union node *n, const char *text, int err); |
| 2839 | |
| 2840 | static void |
| 2841 | expredir(union node *n) |
| 2842 | { |
| 2843 | union node *redir; |
| 2844 | |
| 2845 | for (redir = n ; redir ; redir = redir->nfile.next) { |
| 2846 | struct arglist fn; |
| 2847 | fn.lastp = &fn.list; |
| 2848 | oexitstatus = exitstatus; |
| 2849 | switch (redir->type) { |
| 2850 | case NFROMTO: |
| 2851 | case NFROM: |
| 2852 | case NTO: |
| 2853 | case NAPPEND: |
| 2854 | case NTOOV: |
| 2855 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
| 2856 | redir->nfile.expfname = fn.list->text; |
| 2857 | break; |
| 2858 | case NFROMFD: |
| 2859 | case NTOFD: |
| 2860 | if (redir->ndup.vname) { |
| 2861 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
| 2862 | fixredir(redir, fn.list->text, 1); |
| 2863 | } |
| 2864 | break; |
| 2865 | } |
| 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | |
| 2870 | /* |
| 2871 | * Execute a command inside back quotes. If it's a builtin command, we |
| 2872 | * want to save its output in a block obtained from malloc. Otherwise |
| 2873 | * we fork off a subprocess and get the output of the command via a pipe. |
| 2874 | * Should be called with interrupts off. |
| 2875 | */ |
| 2876 | |
| 2877 | static void |
| 2878 | evalbackcmd(union node *n, struct backcmd *result) |
| 2879 | { |
| 2880 | int pip[2]; |
| 2881 | struct job *jp; |
| 2882 | struct stackmark smark; /* unnecessary */ |
| 2883 | |
| 2884 | setstackmark(&smark); |
| 2885 | result->fd = -1; |
| 2886 | result->buf = NULL; |
| 2887 | result->nleft = 0; |
| 2888 | result->jp = NULL; |
| 2889 | if (n == NULL) { |
| 2890 | exitstatus = 0; |
| 2891 | goto out; |
| 2892 | } |
| 2893 | exitstatus = 0; |
| 2894 | if (pipe(pip) < 0) |
| 2895 | error("Pipe call failed"); |
| 2896 | jp = makejob(n, 1); |
| 2897 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
| 2898 | FORCEINTON; |
| 2899 | close(pip[0]); |
| 2900 | if (pip[1] != 1) { |
| 2901 | close(1); |
| 2902 | dup_as_newfd(pip[1], 1); |
| 2903 | close(pip[1]); |
| 2904 | } |
| 2905 | eflag = 0; |
| 2906 | evaltree(n, EV_EXIT); |
| 2907 | } |
| 2908 | close(pip[1]); |
| 2909 | result->fd = pip[0]; |
| 2910 | result->jp = jp; |
| 2911 | out: |
| 2912 | popstackmark(&smark); |
| 2913 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 2914 | result->fd, result->buf, result->nleft, result->jp)); |
| 2915 | } |
| 2916 | |
| 2917 | |
| 2918 | /* |
| 2919 | * Execute a simple command. |
| 2920 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2921 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2922 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2923 | /* |
| 2924 | * Builtin commands. Builtin commands whose functions are closely |
| 2925 | * tied to evaluation are implemented here. |
| 2926 | */ |
| 2927 | |
| 2928 | /* |
| 2929 | * No command given, or a bltin command with no arguments. Set the |
| 2930 | * specified variables. |
| 2931 | */ |
| 2932 | |
| 2933 | int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 2934 | bltincmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2935 | { |
| 2936 | /* |
| 2937 | * Preserve exitstatus of a previous possible redirection |
| 2938 | * as POSIX mandates |
| 2939 | */ |
| 2940 | return exitstatus; |
| 2941 | } |
| 2942 | |
| 2943 | |
| 2944 | /* |
| 2945 | * Handle break and continue commands. Break, continue, and return are |
| 2946 | * all handled by setting the evalskip flag. The evaluation routines |
| 2947 | * above all check this flag, and if it is set they start skipping |
| 2948 | * commands rather than executing them. The variable skipcount is |
| 2949 | * the number of loops to break/continue, or the number of function |
| 2950 | * levels to return. (The latter is always 1.) It should probably |
| 2951 | * be an error to break out of more loops than exist, but it isn't |
| 2952 | * in the standard shell so we don't make it one here. |
| 2953 | */ |
| 2954 | |
| 2955 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 2956 | breakcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2957 | { |
| 2958 | int n = argc > 1 ? number(argv[1]) : 1; |
| 2959 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 2960 | if (n <= 0) |
| 2961 | error("Illegal number: %s", argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2962 | if (n > loopnest) |
| 2963 | n = loopnest; |
| 2964 | if (n > 0) { |
| 2965 | evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; |
| 2966 | skipcount = n; |
| 2967 | } |
| 2968 | return 0; |
| 2969 | } |
| 2970 | |
| 2971 | |
| 2972 | /* |
| 2973 | * The return command. |
| 2974 | */ |
| 2975 | |
| 2976 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 2977 | returncmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2978 | { |
| 2979 | int ret = argc > 1 ? number(argv[1]) : oexitstatus; |
| 2980 | |
| 2981 | if (funcnest) { |
| 2982 | evalskip = SKIPFUNC; |
| 2983 | skipcount = 1; |
| 2984 | return ret; |
| 2985 | } |
| 2986 | else { |
| 2987 | /* Do what ksh does; skip the rest of the file */ |
| 2988 | evalskip = SKIPFILE; |
| 2989 | skipcount = 1; |
| 2990 | return ret; |
| 2991 | } |
| 2992 | } |
| 2993 | |
| 2994 | |
Eric Andersen | 69a20f0 | 2001-10-31 10:40:37 +0000 | [diff] [blame] | 2995 | #ifndef CONFIG_FALSE |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2996 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 2997 | false_main(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2998 | { |
| 2999 | return 1; |
| 3000 | } |
Eric Andersen | 69a20f0 | 2001-10-31 10:40:37 +0000 | [diff] [blame] | 3001 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3002 | |
Eric Andersen | 69a20f0 | 2001-10-31 10:40:37 +0000 | [diff] [blame] | 3003 | #ifndef CONFIG_TRUE |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3004 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3005 | true_main(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3006 | { |
| 3007 | return 0; |
| 3008 | } |
| 3009 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3010 | |
| 3011 | /* |
| 3012 | * Controls whether the shell is interactive or not. |
| 3013 | */ |
| 3014 | |
| 3015 | static void setsignal(int signo); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3016 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 3017 | #ifdef CONFIG_ASH_MAIL |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 3018 | static void chkmail(int silent); |
| 3019 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3020 | |
| 3021 | static void |
| 3022 | setinteractive(int on) |
| 3023 | { |
| 3024 | static int is_interactive; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3025 | static int do_banner=0; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3026 | |
| 3027 | if (on == is_interactive) |
| 3028 | return; |
| 3029 | setsignal(SIGINT); |
| 3030 | setsignal(SIGQUIT); |
| 3031 | setsignal(SIGTERM); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 3032 | #ifdef CONFIG_ASH_MAIL |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3033 | chkmail(1); |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 3034 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3035 | is_interactive = on; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3036 | if (do_banner==0 && is_interactive) { |
| 3037 | /* Looks like they want an interactive shell */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3038 | #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3039 | printf( "\n\n" BB_BANNER " Built-in shell (ash)\n"); |
| 3040 | printf( "Enter 'help' for a list of built-in commands.\n\n"); |
Eric Andersen | d63dee4 | 2001-10-19 00:22:23 +0000 | [diff] [blame] | 3041 | #endif |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3042 | do_banner=1; |
| 3043 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | static void |
| 3047 | optschanged(void) |
| 3048 | { |
| 3049 | setinteractive(iflag); |
| 3050 | setjobctl(mflag); |
| 3051 | } |
| 3052 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3053 | |
| 3054 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3055 | execcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3056 | { |
| 3057 | if (argc > 1) { |
| 3058 | struct strlist *sp; |
| 3059 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3060 | iflag = 0; /* exit on error */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3061 | mflag = 0; |
| 3062 | optschanged(); |
| 3063 | for (sp = cmdenviron; sp ; sp = sp->next) |
| 3064 | setvareq(sp->text, VEXPORT|VSTACK); |
| 3065 | shellexec(argv + 1, environment(), pathval(), 0); |
| 3066 | } |
| 3067 | return 0; |
| 3068 | } |
| 3069 | |
| 3070 | static void |
| 3071 | eprintlist(struct strlist *sp) |
| 3072 | { |
| 3073 | for (; sp; sp = sp->next) { |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 3074 | out2fmt(" %s",sp->text); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3075 | } |
| 3076 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3077 | |
| 3078 | /* |
| 3079 | * Exec a program. Never returns. If you change this routine, you may |
| 3080 | * have to change the find_command routine as well. |
| 3081 | */ |
| 3082 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3083 | static const char *pathopt; /* set by padvance */ |
| 3084 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3085 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3086 | shellexec(char **argv, char **envp, const char *path, int idx) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3087 | { |
| 3088 | char *cmdname; |
| 3089 | int e; |
| 3090 | |
| 3091 | if (strchr(argv[0], '/') != NULL) { |
| 3092 | tryexec(argv[0], argv, envp); |
| 3093 | e = errno; |
| 3094 | } else { |
| 3095 | e = ENOENT; |
| 3096 | while ((cmdname = padvance(&path, argv[0])) != NULL) { |
| 3097 | if (--idx < 0 && pathopt == NULL) { |
| 3098 | tryexec(cmdname, argv, envp); |
| 3099 | if (errno != ENOENT && errno != ENOTDIR) |
| 3100 | e = errno; |
| 3101 | } |
| 3102 | stunalloc(cmdname); |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | /* Map to POSIX errors */ |
| 3107 | switch (e) { |
| 3108 | case EACCES: |
| 3109 | exerrno = 126; |
| 3110 | break; |
| 3111 | case ENOENT: |
| 3112 | exerrno = 127; |
| 3113 | break; |
| 3114 | default: |
| 3115 | exerrno = 2; |
| 3116 | break; |
| 3117 | } |
| 3118 | exerror(EXEXEC, "%s: %s", argv[0], errmsg(e, E_EXEC)); |
| 3119 | /* NOTREACHED */ |
| 3120 | } |
| 3121 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3122 | /* |
| 3123 | * Clear traps on a fork. |
| 3124 | */ |
| 3125 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3126 | clear_traps(void) |
| 3127 | { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3128 | char **tp; |
| 3129 | |
| 3130 | for (tp = trap ; tp < &trap[NSIG] ; tp++) { |
| 3131 | if (*tp && **tp) { /* trap not NULL or SIG_IGN */ |
| 3132 | INTOFF; |
| 3133 | ckfree(*tp); |
| 3134 | *tp = NULL; |
| 3135 | if (tp != &trap[0]) |
| 3136 | setsignal(tp - trap); |
| 3137 | INTON; |
| 3138 | } |
| 3139 | } |
| 3140 | } |
| 3141 | |
| 3142 | |
| 3143 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3144 | initshellproc(void) |
| 3145 | { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3146 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 3147 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3148 | /* from alias.c: */ |
| 3149 | { |
| 3150 | rmaliases(); |
| 3151 | } |
| 3152 | #endif |
| 3153 | /* from eval.c: */ |
| 3154 | { |
| 3155 | exitstatus = 0; |
| 3156 | } |
| 3157 | |
| 3158 | /* from exec.c: */ |
| 3159 | { |
| 3160 | deletefuncs(); |
| 3161 | } |
| 3162 | |
| 3163 | /* from jobs.c: */ |
| 3164 | { |
| 3165 | backgndpid = -1; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 3166 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3167 | jobctl = 0; |
| 3168 | #endif |
| 3169 | } |
| 3170 | |
| 3171 | /* from options.c: */ |
| 3172 | { |
| 3173 | int i; |
| 3174 | |
| 3175 | for (i = 0; i < NOPTS; i++) |
| 3176 | optent_val(i) = 0; |
| 3177 | optschanged(); |
| 3178 | |
| 3179 | } |
| 3180 | |
| 3181 | /* from redir.c: */ |
| 3182 | { |
| 3183 | clearredir(); |
| 3184 | } |
| 3185 | |
| 3186 | /* from trap.c: */ |
| 3187 | { |
| 3188 | char *sm; |
| 3189 | |
| 3190 | clear_traps(); |
| 3191 | for (sm = sigmode ; sm < sigmode + NSIG - 1; sm++) { |
| 3192 | if (*sm == S_IGN) |
| 3193 | *sm = S_HARD_IGN; |
| 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | /* from var.c: */ |
| 3198 | { |
| 3199 | shprocvar(); |
| 3200 | } |
| 3201 | } |
| 3202 | |
| 3203 | static int preadbuffer(void); |
| 3204 | static void pushfile (void); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3205 | |
| 3206 | /* |
| 3207 | * Read a character from the script, returning PEOF on end of file. |
| 3208 | * Nul characters in the input are silently discarded. |
| 3209 | */ |
| 3210 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 3211 | #ifndef CONFIG_ASH_OPTIMIZE_FOR_SIZE |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3212 | #define pgetc_macro() (--parsenleft >= 0? *parsenextc++ : preadbuffer()) |
| 3213 | static int |
| 3214 | pgetc(void) |
| 3215 | { |
| 3216 | return pgetc_macro(); |
| 3217 | } |
| 3218 | #else |
| 3219 | static int |
| 3220 | pgetc_macro(void) |
| 3221 | { |
| 3222 | return --parsenleft >= 0? *parsenextc++ : preadbuffer(); |
| 3223 | } |
| 3224 | |
| 3225 | static inline int |
| 3226 | pgetc(void) |
| 3227 | { |
| 3228 | return pgetc_macro(); |
| 3229 | } |
| 3230 | #endif |
| 3231 | |
| 3232 | |
| 3233 | /* |
| 3234 | * Undo the last call to pgetc. Only one character may be pushed back. |
| 3235 | * PEOF may be pushed back. |
| 3236 | */ |
| 3237 | |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 3238 | static void pungetc(void) |
| 3239 | { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3240 | parsenleft++; |
| 3241 | parsenextc--; |
| 3242 | } |
| 3243 | |
| 3244 | |
| 3245 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3246 | popfile(void) |
| 3247 | { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3248 | struct parsefile *pf = parsefile; |
| 3249 | |
| 3250 | INTOFF; |
| 3251 | if (pf->fd >= 0) |
| 3252 | close(pf->fd); |
| 3253 | if (pf->buf) |
| 3254 | ckfree(pf->buf); |
| 3255 | while (pf->strpush) |
| 3256 | popstring(); |
| 3257 | parsefile = pf->prev; |
| 3258 | ckfree(pf); |
| 3259 | parsenleft = parsefile->nleft; |
| 3260 | parselleft = parsefile->lleft; |
| 3261 | parsenextc = parsefile->nextc; |
| 3262 | plinno = parsefile->linno; |
| 3263 | INTON; |
| 3264 | } |
| 3265 | |
| 3266 | |
| 3267 | /* |
| 3268 | * Return to top level. |
| 3269 | */ |
| 3270 | |
| 3271 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3272 | popallfiles(void) |
| 3273 | { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3274 | while (parsefile != &basepf) |
| 3275 | popfile(); |
| 3276 | } |
| 3277 | |
| 3278 | /* |
| 3279 | * Close the file(s) that the shell is reading commands from. Called |
| 3280 | * after a fork is done. |
| 3281 | */ |
| 3282 | |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 3283 | static void closescript(void) |
| 3284 | { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3285 | popallfiles(); |
| 3286 | if (parsefile->fd > 0) { |
| 3287 | close(parsefile->fd); |
| 3288 | parsefile->fd = 0; |
| 3289 | } |
| 3290 | } |
| 3291 | |
| 3292 | |
| 3293 | /* |
| 3294 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 3295 | * interrupts off. |
| 3296 | */ |
| 3297 | |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 3298 | static void setinputfd(int fd, int push) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3299 | { |
| 3300 | (void) fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 3301 | if (push) { |
| 3302 | pushfile(); |
| 3303 | parsefile->buf = 0; |
| 3304 | } else { |
| 3305 | closescript(); |
| 3306 | while (parsefile->strpush) |
| 3307 | popstring(); |
| 3308 | } |
| 3309 | parsefile->fd = fd; |
| 3310 | if (parsefile->buf == NULL) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3311 | parsefile->buf = xmalloc(BUFSIZ); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3312 | parselleft = parsenleft = 0; |
| 3313 | plinno = 1; |
| 3314 | } |
| 3315 | |
| 3316 | |
| 3317 | /* |
| 3318 | * Set the input to take input from a file. If push is set, push the |
| 3319 | * old input onto the stack first. |
| 3320 | */ |
| 3321 | |
| 3322 | static void |
| 3323 | setinputfile(const char *fname, int push) |
| 3324 | { |
| 3325 | int fd; |
| 3326 | int myfileno2; |
| 3327 | |
| 3328 | INTOFF; |
| 3329 | if ((fd = open(fname, O_RDONLY)) < 0) |
| 3330 | error("Can't open %s", fname); |
| 3331 | if (fd < 10) { |
| 3332 | myfileno2 = dup_as_newfd(fd, 10); |
| 3333 | close(fd); |
| 3334 | if (myfileno2 < 0) |
| 3335 | error("Out of file descriptors"); |
| 3336 | fd = myfileno2; |
| 3337 | } |
| 3338 | setinputfd(fd, push); |
| 3339 | INTON; |
| 3340 | } |
| 3341 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3342 | |
| 3343 | static void |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3344 | tryexec(char *cmd, char **argv, char **envp) |
| 3345 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3346 | int e; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3347 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3348 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 3349 | char *name = cmd; |
| 3350 | char** argv_l=argv; |
| 3351 | int argc_l; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3352 | #ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 3353 | name = get_last_path_component(name); |
| 3354 | #endif |
| 3355 | argv_l=envp; |
| 3356 | for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++) |
| 3357 | putenv(*argv_l); |
| 3358 | argv_l=argv; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3359 | for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++) |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 3360 | optind = 1; |
| 3361 | run_applet_by_name(name, argc_l, argv); |
| 3362 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3363 | execve(cmd, argv, envp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3364 | e = errno; |
| 3365 | if (e == ENOEXEC) { |
| 3366 | INTOFF; |
| 3367 | initshellproc(); |
| 3368 | setinputfile(cmd, 0); |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3369 | commandname = arg0 = xstrdup(argv[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3370 | setparam(argv + 1); |
| 3371 | exraise(EXSHELLPROC); |
| 3372 | } |
| 3373 | errno = e; |
| 3374 | } |
| 3375 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3376 | static char *commandtext (const union node *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3377 | |
| 3378 | /* |
| 3379 | * Do a path search. The variable path (passed by reference) should be |
| 3380 | * set to the start of the path before the first call; padvance will update |
| 3381 | * this value as it proceeds. Successive calls to padvance will return |
| 3382 | * the possible path expansions in sequence. If an option (indicated by |
| 3383 | * a percent sign) appears in the path entry then the global variable |
| 3384 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 3385 | * NULL. |
| 3386 | */ |
| 3387 | |
| 3388 | static const char *pathopt; |
| 3389 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3390 | static void growstackblock(void); |
| 3391 | |
| 3392 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3393 | static char * |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3394 | padvance(const char **path, const char *name) |
| 3395 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3396 | const char *p; |
| 3397 | char *q; |
| 3398 | const char *start; |
| 3399 | int len; |
| 3400 | |
| 3401 | if (*path == NULL) |
| 3402 | return NULL; |
| 3403 | start = *path; |
| 3404 | for (p = start ; *p && *p != ':' && *p != '%' ; p++); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3405 | len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3406 | while (stackblocksize() < len) |
| 3407 | growstackblock(); |
| 3408 | q = stackblock(); |
| 3409 | if (p != start) { |
| 3410 | memcpy(q, start, p - start); |
| 3411 | q += p - start; |
| 3412 | *q++ = '/'; |
| 3413 | } |
| 3414 | strcpy(q, name); |
| 3415 | pathopt = NULL; |
| 3416 | if (*p == '%') { |
| 3417 | pathopt = ++p; |
| 3418 | while (*p && *p != ':') p++; |
| 3419 | } |
| 3420 | if (*p == ':') |
| 3421 | *path = p + 1; |
| 3422 | else |
| 3423 | *path = NULL; |
| 3424 | return stalloc(len); |
| 3425 | } |
| 3426 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3427 | /* |
| 3428 | * Wrapper around strcmp for qsort/bsearch/... |
| 3429 | */ |
| 3430 | static int |
| 3431 | pstrcmp(const void *a, const void *b) |
| 3432 | { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3433 | return strcmp((const char *) a, (*(const char *const *) b) + 1); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | /* |
| 3437 | * Find a keyword is in a sorted array. |
| 3438 | */ |
| 3439 | |
| 3440 | static const char *const * |
| 3441 | findkwd(const char *s) |
| 3442 | { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3443 | return bsearch(s, tokname_array + KWDOFFSET, |
| 3444 | (sizeof(tokname_array)/sizeof(const char *)) - KWDOFFSET, |
| 3445 | sizeof(const char *), pstrcmp); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3446 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3447 | |
| 3448 | |
| 3449 | /*** Command hashing code ***/ |
| 3450 | |
| 3451 | |
| 3452 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3453 | hashcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3454 | { |
| 3455 | struct tblentry **pp; |
| 3456 | struct tblentry *cmdp; |
| 3457 | int c; |
| 3458 | int verbose; |
| 3459 | struct cmdentry entry; |
| 3460 | char *name; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 3461 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3462 | const struct alias *ap; |
| 3463 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3464 | |
| 3465 | verbose = 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3466 | while ((c = nextopt("rvV")) != '\0') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3467 | if (c == 'r') { |
| 3468 | clearcmdentry(0); |
| 3469 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3470 | } else if (c == 'v' || c == 'V') { |
| 3471 | verbose = c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3472 | } |
| 3473 | } |
| 3474 | if (*argptr == NULL) { |
| 3475 | for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) { |
| 3476 | for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { |
| 3477 | if (cmdp->cmdtype != CMDBUILTIN) { |
| 3478 | printentry(cmdp, verbose); |
| 3479 | } |
| 3480 | } |
| 3481 | } |
| 3482 | return 0; |
| 3483 | } |
| 3484 | c = 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3485 | while ((name = *argptr++) != NULL) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3486 | if ((cmdp = cmdlookup(name, 0)) != NULL |
| 3487 | && (cmdp->cmdtype == CMDNORMAL |
| 3488 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))) |
| 3489 | delete_cmd_entry(); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 3490 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3491 | /* Then look at the aliases */ |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 3492 | if ((ap = *__lookupalias(name)) != NULL) { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3493 | if (verbose=='v') |
| 3494 | printf("%s is an alias for %s\n", name, ap->val); |
| 3495 | else |
| 3496 | printalias(ap); |
| 3497 | continue; |
| 3498 | } |
| 3499 | #endif |
| 3500 | /* First look at the keywords */ |
| 3501 | if (findkwd(name)!=0) { |
| 3502 | if (verbose=='v') |
| 3503 | printf("%s is a shell keyword\n", name); |
| 3504 | else |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3505 | puts(name); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3506 | continue; |
| 3507 | } |
| 3508 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3509 | find_command(name, &entry, DO_ERR, pathval()); |
| 3510 | if (entry.cmdtype == CMDUNKNOWN) c = 1; |
| 3511 | else if (verbose) { |
| 3512 | cmdp = cmdlookup(name, 0); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3513 | if (cmdp) printentry(cmdp, verbose=='v'); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3514 | flushall(); |
| 3515 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3516 | } |
| 3517 | return c; |
| 3518 | } |
| 3519 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3520 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3521 | printentry(struct tblentry *cmdp, int verbose) |
| 3522 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3523 | int idx; |
| 3524 | const char *path; |
| 3525 | char *name; |
| 3526 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3527 | printf("%s%s", cmdp->cmdname, (verbose ? " is " : "")); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3528 | if (cmdp->cmdtype == CMDNORMAL) { |
| 3529 | idx = cmdp->param.index; |
| 3530 | path = pathval(); |
| 3531 | do { |
| 3532 | name = padvance(&path, cmdp->cmdname); |
| 3533 | stunalloc(name); |
| 3534 | } while (--idx >= 0); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3535 | if(verbose) |
| 3536 | out1str(name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3537 | } else if (cmdp->cmdtype == CMDBUILTIN) { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3538 | if(verbose) |
| 3539 | out1str("a shell builtin"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3540 | } else if (cmdp->cmdtype == CMDFUNCTION) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3541 | if (verbose) { |
| 3542 | INTOFF; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3543 | out1str("a function\n"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3544 | name = commandtext(cmdp->param.func); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3545 | printf("%s() {\n %s\n}", cmdp->cmdname, name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3546 | ckfree(name); |
| 3547 | INTON; |
| 3548 | } |
| 3549 | #ifdef DEBUG |
| 3550 | } else { |
| 3551 | error("internal error: cmdtype %d", cmdp->cmdtype); |
| 3552 | #endif |
| 3553 | } |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3554 | puts(cmdp->rehash ? "*" : nullstr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | |
| 3558 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3559 | /*** List the available builtins ***/ |
| 3560 | |
| 3561 | |
| 3562 | static int helpcmd(int argc, char** argv) |
| 3563 | { |
| 3564 | int col, i; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3565 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3566 | printf("\nBuilt-in commands:\n-------------------\n"); |
| 3567 | for (col=0, i=0; i < NUMBUILTINS; i++) { |
| 3568 | col += printf("%c%s", ((col == 0) ? '\t' : ' '), |
| 3569 | builtincmds[i].name+1); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3570 | if (col > 60) { |
| 3571 | printf("\n"); |
| 3572 | col = 0; |
| 3573 | } |
| 3574 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3575 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3576 | { |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3577 | extern const struct BB_applet applets[]; |
| 3578 | extern const size_t NUM_APPLETS; |
| 3579 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3580 | for (i=0; i < NUM_APPLETS; i++) { |
| 3581 | |
| 3582 | col += printf("%c%s", ((col == 0) ? '\t' : ' '), |
| 3583 | applets[i].name); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3584 | if (col > 60) { |
| 3585 | printf("\n"); |
| 3586 | col = 0; |
| 3587 | } |
| 3588 | } |
| 3589 | } |
| 3590 | #endif |
| 3591 | printf("\n\n"); |
| 3592 | return EXIT_SUCCESS; |
| 3593 | } |
| 3594 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3595 | /* |
| 3596 | * Resolve a command name. If you change this routine, you may have to |
| 3597 | * change the shellexec routine as well. |
| 3598 | */ |
| 3599 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3600 | static int prefix (const char *, const char *); |
| 3601 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3602 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3603 | find_command(const char *name, struct cmdentry *entry, int act, const char *path) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3604 | { |
| 3605 | struct tblentry *cmdp; |
| 3606 | int idx; |
| 3607 | int prev; |
| 3608 | char *fullname; |
| 3609 | struct stat statb; |
| 3610 | int e; |
| 3611 | int bltin; |
| 3612 | int firstchange; |
| 3613 | int updatetbl; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3614 | int regular; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3615 | struct builtincmd *bcmd; |
| 3616 | |
| 3617 | /* If name contains a slash, don't use the hash table */ |
| 3618 | if (strchr(name, '/') != NULL) { |
| 3619 | if (act & DO_ABS) { |
| 3620 | while (stat(name, &statb) < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3621 | if (errno != ENOENT && errno != ENOTDIR) |
| 3622 | e = errno; |
| 3623 | entry->cmdtype = CMDUNKNOWN; |
| 3624 | entry->u.index = -1; |
| 3625 | return; |
| 3626 | } |
| 3627 | entry->cmdtype = CMDNORMAL; |
| 3628 | entry->u.index = -1; |
| 3629 | return; |
| 3630 | } |
| 3631 | entry->cmdtype = CMDNORMAL; |
| 3632 | entry->u.index = 0; |
| 3633 | return; |
| 3634 | } |
| 3635 | |
| 3636 | updatetbl = 1; |
| 3637 | if (act & DO_BRUTE) { |
| 3638 | firstchange = path_change(path, &bltin); |
| 3639 | } else { |
| 3640 | bltin = builtinloc; |
| 3641 | firstchange = 9999; |
| 3642 | } |
| 3643 | |
| 3644 | /* If name is in the table, and not invalidated by cd, we're done */ |
| 3645 | if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->rehash == 0) { |
| 3646 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 3647 | if (act & DO_NOFUN) { |
| 3648 | updatetbl = 0; |
| 3649 | } else { |
| 3650 | goto success; |
| 3651 | } |
| 3652 | } else if (act & DO_BRUTE) { |
| 3653 | if ((cmdp->cmdtype == CMDNORMAL && |
| 3654 | cmdp->param.index >= firstchange) || |
| 3655 | (cmdp->cmdtype == CMDBUILTIN && |
| 3656 | ((builtinloc < 0 && bltin >= 0) ? |
| 3657 | bltin : builtinloc) >= firstchange)) { |
| 3658 | /* need to recompute the entry */ |
| 3659 | } else { |
| 3660 | goto success; |
| 3661 | } |
| 3662 | } else { |
| 3663 | goto success; |
| 3664 | } |
| 3665 | } |
| 3666 | |
| 3667 | bcmd = find_builtin(name); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3668 | regular = bcmd && IS_BUILTIN_REGULAR(bcmd); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3669 | |
| 3670 | if (regular) { |
| 3671 | if (cmdp && (cmdp->cmdtype == CMDBUILTIN)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3672 | goto success; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3673 | } |
| 3674 | } else if (act & DO_BRUTE) { |
| 3675 | if (firstchange == 0) { |
| 3676 | updatetbl = 0; |
| 3677 | } |
| 3678 | } |
| 3679 | |
| 3680 | /* If %builtin not in path, check for builtin next */ |
| 3681 | if (regular || (bltin < 0 && bcmd)) { |
| 3682 | builtin: |
| 3683 | if (!updatetbl) { |
| 3684 | entry->cmdtype = CMDBUILTIN; |
| 3685 | entry->u.cmd = bcmd; |
| 3686 | return; |
| 3687 | } |
| 3688 | INTOFF; |
| 3689 | cmdp = cmdlookup(name, 1); |
| 3690 | cmdp->cmdtype = CMDBUILTIN; |
| 3691 | cmdp->param.cmd = bcmd; |
| 3692 | INTON; |
| 3693 | goto success; |
| 3694 | } |
| 3695 | |
| 3696 | /* We have to search path. */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3697 | prev = -1; /* where to start */ |
| 3698 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3699 | if (cmdp->cmdtype == CMDBUILTIN) |
| 3700 | prev = builtinloc; |
| 3701 | else |
| 3702 | prev = cmdp->param.index; |
| 3703 | } |
| 3704 | |
| 3705 | e = ENOENT; |
| 3706 | idx = -1; |
| 3707 | loop: |
| 3708 | while ((fullname = padvance(&path, name)) != NULL) { |
| 3709 | stunalloc(fullname); |
| 3710 | idx++; |
| 3711 | if (idx >= firstchange) { |
| 3712 | updatetbl = 0; |
| 3713 | } |
| 3714 | if (pathopt) { |
| 3715 | if (prefix("builtin", pathopt)) { |
| 3716 | if ((bcmd = find_builtin(name))) { |
| 3717 | goto builtin; |
| 3718 | } |
| 3719 | continue; |
| 3720 | } else if (!(act & DO_NOFUN) && |
| 3721 | prefix("func", pathopt)) { |
| 3722 | /* handled below */ |
| 3723 | } else { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3724 | continue; /* ignore unimplemented options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3725 | } |
| 3726 | } |
| 3727 | /* if rehash, don't redo absolute path names */ |
| 3728 | if (fullname[0] == '/' && idx <= prev && |
| 3729 | idx < firstchange) { |
| 3730 | if (idx < prev) |
| 3731 | continue; |
| 3732 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 3733 | goto success; |
| 3734 | } |
| 3735 | while (stat(fullname, &statb) < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3736 | if (errno != ENOENT && errno != ENOTDIR) |
| 3737 | e = errno; |
| 3738 | goto loop; |
| 3739 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3740 | e = EACCES; /* if we fail, this will be the error */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3741 | if (!S_ISREG(statb.st_mode)) |
| 3742 | continue; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3743 | if (pathopt) { /* this is a %func directory */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3744 | stalloc(strlen(fullname) + 1); |
| 3745 | readcmdfile(fullname); |
| 3746 | if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 3747 | error("%s not defined in %s", name, fullname); |
| 3748 | stunalloc(fullname); |
| 3749 | goto success; |
| 3750 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3751 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 3752 | /* If we aren't called with DO_BRUTE and cmdp is set, it must |
| 3753 | be a function and we're being called with DO_NOFUN */ |
| 3754 | if (!updatetbl) { |
| 3755 | entry->cmdtype = CMDNORMAL; |
| 3756 | entry->u.index = idx; |
| 3757 | return; |
| 3758 | } |
| 3759 | INTOFF; |
| 3760 | cmdp = cmdlookup(name, 1); |
| 3761 | cmdp->cmdtype = CMDNORMAL; |
| 3762 | cmdp->param.index = idx; |
| 3763 | INTON; |
| 3764 | goto success; |
| 3765 | } |
| 3766 | |
| 3767 | /* We failed. If there was an entry for this command, delete it */ |
| 3768 | if (cmdp && updatetbl) |
| 3769 | delete_cmd_entry(); |
| 3770 | if (act & DO_ERR) |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 3771 | out2fmt("%s: %s\n", name, errmsg(e, E_EXEC)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3772 | entry->cmdtype = CMDUNKNOWN; |
| 3773 | return; |
| 3774 | |
| 3775 | success: |
| 3776 | cmdp->rehash = 0; |
| 3777 | entry->cmdtype = cmdp->cmdtype; |
| 3778 | entry->u = cmdp->param; |
| 3779 | } |
| 3780 | |
| 3781 | |
| 3782 | |
| 3783 | /* |
| 3784 | * Search the table of builtin commands. |
| 3785 | */ |
| 3786 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3787 | static int |
| 3788 | bstrcmp(const void *name, const void *b) |
| 3789 | { |
| 3790 | return strcmp((const char *)name, (*(const char *const *) b)+1); |
| 3791 | } |
| 3792 | |
| 3793 | static struct builtincmd * |
| 3794 | find_builtin(const char *name) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3795 | { |
| 3796 | struct builtincmd *bp; |
| 3797 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3798 | bp = bsearch(name, builtincmds, NUMBUILTINS, sizeof(struct builtincmd), |
| 3799 | bstrcmp |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3800 | ); |
| 3801 | return bp; |
| 3802 | } |
| 3803 | |
| 3804 | |
| 3805 | /* |
| 3806 | * Called when a cd is done. Marks all commands so the next time they |
| 3807 | * are executed they will be rehashed. |
| 3808 | */ |
| 3809 | |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3810 | static inline void |
| 3811 | hashcd(void) |
| 3812 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3813 | struct tblentry **pp; |
| 3814 | struct tblentry *cmdp; |
| 3815 | |
| 3816 | for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) { |
| 3817 | for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { |
| 3818 | if (cmdp->cmdtype == CMDNORMAL |
| 3819 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)) |
| 3820 | cmdp->rehash = 1; |
| 3821 | } |
| 3822 | } |
| 3823 | } |
| 3824 | |
| 3825 | |
| 3826 | |
| 3827 | /* |
| 3828 | * Called before PATH is changed. The argument is the new value of PATH; |
| 3829 | * pathval() still returns the old value at this point. Called with |
| 3830 | * interrupts off. |
| 3831 | */ |
| 3832 | |
| 3833 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3834 | changepath(const char *newval) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3835 | { |
| 3836 | int firstchange; |
| 3837 | int bltin; |
| 3838 | |
| 3839 | firstchange = path_change(newval, &bltin); |
| 3840 | if (builtinloc < 0 && bltin >= 0) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3841 | builtinloc = bltin; /* zap builtins */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3842 | clearcmdentry(firstchange); |
| 3843 | builtinloc = bltin; |
Eric Andersen | 1a92376 | 2002-06-06 12:07:28 +0000 | [diff] [blame] | 3844 | /* Ensure that getenv("PATH") stays current */ |
| 3845 | setenv("PATH", newval, 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
| 3848 | |
| 3849 | /* |
| 3850 | * Clear out command entries. The argument specifies the first entry in |
| 3851 | * PATH which has changed. |
| 3852 | */ |
| 3853 | |
| 3854 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3855 | clearcmdentry(int firstchange) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3856 | { |
| 3857 | struct tblentry **tblp; |
| 3858 | struct tblentry **pp; |
| 3859 | struct tblentry *cmdp; |
| 3860 | |
| 3861 | INTOFF; |
| 3862 | for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) { |
| 3863 | pp = tblp; |
| 3864 | while ((cmdp = *pp) != NULL) { |
| 3865 | if ((cmdp->cmdtype == CMDNORMAL && |
| 3866 | cmdp->param.index >= firstchange) |
| 3867 | || (cmdp->cmdtype == CMDBUILTIN && |
| 3868 | builtinloc >= firstchange)) { |
| 3869 | *pp = cmdp->next; |
| 3870 | ckfree(cmdp); |
| 3871 | } else { |
| 3872 | pp = &cmdp->next; |
| 3873 | } |
| 3874 | } |
| 3875 | } |
| 3876 | INTON; |
| 3877 | } |
| 3878 | |
| 3879 | |
| 3880 | /* |
| 3881 | * Delete all functions. |
| 3882 | */ |
| 3883 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3884 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3885 | deletefuncs(void) |
| 3886 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3887 | struct tblentry **tblp; |
| 3888 | struct tblentry **pp; |
| 3889 | struct tblentry *cmdp; |
| 3890 | |
| 3891 | INTOFF; |
| 3892 | for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) { |
| 3893 | pp = tblp; |
| 3894 | while ((cmdp = *pp) != NULL) { |
| 3895 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 3896 | *pp = cmdp->next; |
| 3897 | freefunc(cmdp->param.func); |
| 3898 | ckfree(cmdp); |
| 3899 | } else { |
| 3900 | pp = &cmdp->next; |
| 3901 | } |
| 3902 | } |
| 3903 | } |
| 3904 | INTON; |
| 3905 | } |
| 3906 | |
| 3907 | |
| 3908 | |
| 3909 | /* |
| 3910 | * Locate a command in the command hash table. If "add" is nonzero, |
| 3911 | * add the command to the table if it is not already present. The |
| 3912 | * variable "lastcmdentry" is set to point to the address of the link |
| 3913 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 3914 | * entry. |
| 3915 | */ |
| 3916 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3917 | static struct tblentry **lastcmdentry; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3918 | |
| 3919 | static struct tblentry * |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3920 | cmdlookup(const char *name, int add) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3921 | { |
| 3922 | int hashval; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3923 | const char *p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3924 | struct tblentry *cmdp; |
| 3925 | struct tblentry **pp; |
| 3926 | |
| 3927 | p = name; |
| 3928 | hashval = *p << 4; |
| 3929 | while (*p) |
| 3930 | hashval += *p++; |
| 3931 | hashval &= 0x7FFF; |
| 3932 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 3933 | for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { |
| 3934 | if (equal(cmdp->cmdname, name)) |
| 3935 | break; |
| 3936 | pp = &cmdp->next; |
| 3937 | } |
| 3938 | if (add && cmdp == NULL) { |
| 3939 | INTOFF; |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3940 | cmdp = *pp = xmalloc(sizeof (struct tblentry) - ARB |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3941 | + strlen(name) + 1); |
| 3942 | cmdp->next = NULL; |
| 3943 | cmdp->cmdtype = CMDUNKNOWN; |
| 3944 | cmdp->rehash = 0; |
| 3945 | strcpy(cmdp->cmdname, name); |
| 3946 | INTON; |
| 3947 | } |
| 3948 | lastcmdentry = pp; |
| 3949 | return cmdp; |
| 3950 | } |
| 3951 | |
| 3952 | /* |
| 3953 | * Delete the command entry returned on the last lookup. |
| 3954 | */ |
| 3955 | |
| 3956 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 3957 | delete_cmd_entry() |
| 3958 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3959 | struct tblentry *cmdp; |
| 3960 | |
| 3961 | INTOFF; |
| 3962 | cmdp = *lastcmdentry; |
| 3963 | *lastcmdentry = cmdp->next; |
| 3964 | ckfree(cmdp); |
| 3965 | INTON; |
| 3966 | } |
| 3967 | |
| 3968 | |
| 3969 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3970 | |
| 3971 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3972 | static const unsigned char nodesize[26] = { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3973 | ALIGN(sizeof (struct nbinary)), |
| 3974 | ALIGN(sizeof (struct ncmd)), |
| 3975 | ALIGN(sizeof (struct npipe)), |
| 3976 | ALIGN(sizeof (struct nredir)), |
| 3977 | ALIGN(sizeof (struct nredir)), |
| 3978 | ALIGN(sizeof (struct nredir)), |
| 3979 | ALIGN(sizeof (struct nbinary)), |
| 3980 | ALIGN(sizeof (struct nbinary)), |
| 3981 | ALIGN(sizeof (struct nif)), |
| 3982 | ALIGN(sizeof (struct nbinary)), |
| 3983 | ALIGN(sizeof (struct nbinary)), |
| 3984 | ALIGN(sizeof (struct nfor)), |
| 3985 | ALIGN(sizeof (struct ncase)), |
| 3986 | ALIGN(sizeof (struct nclist)), |
| 3987 | ALIGN(sizeof (struct narg)), |
| 3988 | ALIGN(sizeof (struct narg)), |
| 3989 | ALIGN(sizeof (struct nfile)), |
| 3990 | ALIGN(sizeof (struct nfile)), |
| 3991 | ALIGN(sizeof (struct nfile)), |
| 3992 | ALIGN(sizeof (struct nfile)), |
| 3993 | ALIGN(sizeof (struct nfile)), |
| 3994 | ALIGN(sizeof (struct ndup)), |
| 3995 | ALIGN(sizeof (struct ndup)), |
| 3996 | ALIGN(sizeof (struct nhere)), |
| 3997 | ALIGN(sizeof (struct nhere)), |
| 3998 | ALIGN(sizeof (struct nnot)), |
| 3999 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4000 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4001 | |
| 4002 | |
| 4003 | /* |
| 4004 | * Delete a function if it exists. |
| 4005 | */ |
| 4006 | |
| 4007 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4008 | unsetfunc(char *name) |
| 4009 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4010 | struct tblentry *cmdp; |
| 4011 | |
| 4012 | if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) { |
| 4013 | freefunc(cmdp->param.func); |
| 4014 | delete_cmd_entry(); |
| 4015 | } |
| 4016 | } |
| 4017 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4018 | |
| 4019 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4020 | * Locate and print what a word is... |
| 4021 | */ |
| 4022 | |
| 4023 | static int |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4024 | typecmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4025 | { |
| 4026 | int i; |
| 4027 | int err = 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4028 | char *argv_a[2]; |
| 4029 | |
| 4030 | argv_a[1] = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4031 | |
| 4032 | for (i = 1; i < argc; i++) { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4033 | argv_a[0] = argv[i]; |
| 4034 | argptr = argv_a; |
| 4035 | optptr = "v"; |
| 4036 | err |= hashcmd(argc, argv); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4037 | } |
| 4038 | return err; |
| 4039 | } |
| 4040 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 4041 | #ifdef CONFIG_ASH_CMDCMD |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4042 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4043 | commandcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4044 | { |
| 4045 | int c; |
| 4046 | int default_path = 0; |
| 4047 | int verify_only = 0; |
| 4048 | int verbose_verify_only = 0; |
| 4049 | |
| 4050 | while ((c = nextopt("pvV")) != '\0') |
| 4051 | switch (c) { |
| 4052 | case 'p': |
| 4053 | default_path = 1; |
| 4054 | break; |
| 4055 | case 'v': |
| 4056 | verify_only = 1; |
| 4057 | break; |
| 4058 | case 'V': |
| 4059 | verbose_verify_only = 1; |
| 4060 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4061 | } |
| 4062 | |
| 4063 | if (default_path + verify_only + verbose_verify_only > 1 || |
| 4064 | !*argptr) { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4065 | out2str( |
| 4066 | "command [-p] command [arg ...]\n" |
| 4067 | "command {-v|-V} command\n"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4068 | return EX_USAGE; |
| 4069 | } |
| 4070 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4071 | if (verify_only || verbose_verify_only) { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4072 | char *argv_a[2]; |
| 4073 | |
| 4074 | argv_a[1] = 0; |
| 4075 | argv_a[0] = *argptr; |
| 4076 | argptr = argv_a; |
| 4077 | optptr = verbose_verify_only ? "v" : "V"; /* reverse special */ |
| 4078 | return hashcmd(argc, argv); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4079 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4080 | |
| 4081 | return 0; |
| 4082 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4083 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4084 | |
| 4085 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4086 | path_change(const char *newval, int *bltin) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4087 | { |
| 4088 | const char *old, *new; |
| 4089 | int idx; |
| 4090 | int firstchange; |
| 4091 | |
| 4092 | old = pathval(); |
| 4093 | new = newval; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4094 | firstchange = 9999; /* assume no change */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4095 | idx = 0; |
| 4096 | *bltin = -1; |
| 4097 | for (;;) { |
| 4098 | if (*old != *new) { |
| 4099 | firstchange = idx; |
| 4100 | if ((*old == '\0' && *new == ':') |
| 4101 | || (*old == ':' && *new == '\0')) |
| 4102 | firstchange++; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4103 | old = new; /* ignore subsequent differences */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4104 | } |
| 4105 | if (*new == '\0') |
| 4106 | break; |
| 4107 | if (*new == '%' && *bltin < 0 && prefix("builtin", new + 1)) |
| 4108 | *bltin = idx; |
| 4109 | if (*new == ':') { |
| 4110 | idx++; |
| 4111 | } |
| 4112 | new++, old++; |
| 4113 | } |
| 4114 | if (builtinloc >= 0 && *bltin < 0) |
| 4115 | firstchange = 0; |
| 4116 | return firstchange; |
| 4117 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4118 | /* |
| 4119 | * Routines to expand arguments to commands. We have to deal with |
| 4120 | * backquotes, shell variables, and file metacharacters. |
| 4121 | */ |
| 4122 | /* |
| 4123 | * _rmescape() flags |
| 4124 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4125 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 4126 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4127 | |
| 4128 | /* |
| 4129 | * Structure specifying which parts of the string should be searched |
| 4130 | * for IFS characters. |
| 4131 | */ |
| 4132 | |
| 4133 | struct ifsregion { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4134 | struct ifsregion *next; /* next region in list */ |
| 4135 | int begoff; /* offset of start of region */ |
| 4136 | int endoff; /* offset of end of region */ |
| 4137 | int nulonly; /* search for nul bytes only */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4138 | }; |
| 4139 | |
| 4140 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4141 | static char *expdest; /* output of current string */ |
| 4142 | static struct nodelist *argbackq; /* list of back quote expressions */ |
| 4143 | static struct ifsregion ifsfirst; /* first struct in list of ifs regions */ |
| 4144 | static struct ifsregion *ifslastp; /* last struct in list */ |
| 4145 | static struct arglist exparg; /* holds expanded arg list */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4146 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4147 | static void argstr (char *, int); |
| 4148 | static char *exptilde (char *, int); |
| 4149 | static void expbackq (union node *, int, int); |
| 4150 | static int subevalvar (char *, char *, int, int, int, int, int); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4151 | static int varisset (char *, int); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 4152 | static void strtodest (const char *, int, int); |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4153 | static inline void varvalue (char *, int, int); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4154 | static void recordregion (int, int, int); |
| 4155 | static void removerecordregions (int); |
| 4156 | static void ifsbreakup (char *, struct arglist *); |
| 4157 | static void ifsfree (void); |
| 4158 | static void expandmeta (struct strlist *, int); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4159 | #if defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4160 | #define preglob(p) _rmescapes((p), RMESCAPE_ALLOC | RMESCAPE_GLOB) |
| 4161 | #if !defined(GLOB_BROKEN) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4162 | static inline void addglob (const glob_t *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4163 | #endif |
| 4164 | #endif |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4165 | #if !(defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN)) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4166 | static void expmeta (char *, char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4167 | #endif |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4168 | #if !(defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN)) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4169 | static struct strlist *expsort (struct strlist *); |
| 4170 | static struct strlist *msort (struct strlist *, int); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4171 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4172 | static int patmatch (char *, char *, int); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4173 | #if defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4174 | static int patmatch2 (char *, char *, int); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4175 | #else |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4176 | static int pmatch (char *, char *, int); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4177 | #define patmatch2 patmatch |
| 4178 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4179 | static char *cvtnum (int, char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4180 | |
| 4181 | /* |
| 4182 | * Expand shell variables and backquotes inside a here document. |
| 4183 | */ |
| 4184 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4185 | /* arg: the document, fd: where to write the expanded version */ |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4186 | static inline void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4187 | expandhere(union node *arg, int fd) |
| 4188 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4189 | herefd = fd; |
| 4190 | expandarg(arg, (struct arglist *)NULL, 0); |
| 4191 | xwrite(fd, stackblock(), expdest - stackblock()); |
| 4192 | } |
| 4193 | |
| 4194 | |
| 4195 | /* |
| 4196 | * Perform variable substitution and command substitution on an argument, |
| 4197 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 4198 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 4199 | * here document expansion. |
| 4200 | */ |
| 4201 | |
| 4202 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4203 | expandarg(union node *arg, struct arglist *arglist, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4204 | { |
| 4205 | struct strlist *sp; |
| 4206 | char *p; |
| 4207 | |
| 4208 | argbackq = arg->narg.backquote; |
| 4209 | STARTSTACKSTR(expdest); |
| 4210 | ifsfirst.next = NULL; |
| 4211 | ifslastp = NULL; |
| 4212 | argstr(arg->narg.text, flag); |
| 4213 | if (arglist == NULL) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4214 | return; /* here document expanded */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4215 | } |
| 4216 | STPUTC('\0', expdest); |
| 4217 | p = grabstackstr(expdest); |
| 4218 | exparg.lastp = &exparg.list; |
| 4219 | /* |
| 4220 | * TODO - EXP_REDIR |
| 4221 | */ |
| 4222 | if (flag & EXP_FULL) { |
| 4223 | ifsbreakup(p, &exparg); |
| 4224 | *exparg.lastp = NULL; |
| 4225 | exparg.lastp = &exparg.list; |
| 4226 | expandmeta(exparg.list, flag); |
| 4227 | } else { |
| 4228 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ |
| 4229 | rmescapes(p); |
| 4230 | sp = (struct strlist *)stalloc(sizeof (struct strlist)); |
| 4231 | sp->text = p; |
| 4232 | *exparg.lastp = sp; |
| 4233 | exparg.lastp = &sp->next; |
| 4234 | } |
| 4235 | ifsfree(); |
| 4236 | *exparg.lastp = NULL; |
| 4237 | if (exparg.list) { |
| 4238 | *arglist->lastp = exparg.list; |
| 4239 | arglist->lastp = exparg.lastp; |
| 4240 | } |
| 4241 | } |
| 4242 | |
| 4243 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4244 | /* |
| 4245 | * Expand a variable, and return a pointer to the next character in the |
| 4246 | * input string. |
| 4247 | */ |
| 4248 | |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 4249 | static inline char * evalvar(char *p, int flag) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 4250 | { |
| 4251 | int subtype; |
| 4252 | int varflags; |
| 4253 | char *var; |
| 4254 | const char *val; |
| 4255 | int patloc; |
| 4256 | int c; |
| 4257 | int set; |
| 4258 | int special; |
| 4259 | int startloc; |
| 4260 | int varlen; |
| 4261 | int easy; |
| 4262 | int quotes = flag & (EXP_FULL | EXP_CASE); |
| 4263 | |
| 4264 | varflags = *p++; |
| 4265 | subtype = varflags & VSTYPE; |
| 4266 | var = p; |
| 4267 | special = 0; |
| 4268 | if (! is_name(*p)) |
| 4269 | special = 1; |
| 4270 | p = strchr(p, '=') + 1; |
| 4271 | again: /* jump here after setting a variable with ${var=text} */ |
| 4272 | if (special) { |
| 4273 | set = varisset(var, varflags & VSNUL); |
| 4274 | val = NULL; |
| 4275 | } else { |
| 4276 | val = lookupvar(var); |
| 4277 | if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) { |
| 4278 | val = NULL; |
| 4279 | set = 0; |
| 4280 | } else |
| 4281 | set = 1; |
| 4282 | } |
| 4283 | varlen = 0; |
| 4284 | startloc = expdest - stackblock(); |
| 4285 | if (set && subtype != VSPLUS) { |
| 4286 | /* insert the value of the variable */ |
| 4287 | if (special) { |
| 4288 | varvalue(var, varflags & VSQUOTE, flag); |
| 4289 | if (subtype == VSLENGTH) { |
| 4290 | varlen = expdest - stackblock() - startloc; |
| 4291 | STADJUST(-varlen, expdest); |
| 4292 | } |
| 4293 | } else { |
| 4294 | if (subtype == VSLENGTH) { |
| 4295 | varlen = strlen(val); |
| 4296 | } else { |
| 4297 | strtodest( |
| 4298 | val, |
| 4299 | varflags & VSQUOTE ? |
| 4300 | DQSYNTAX : BASESYNTAX, |
| 4301 | quotes |
| 4302 | ); |
| 4303 | } |
| 4304 | } |
| 4305 | } |
| 4306 | |
| 4307 | if (subtype == VSPLUS) |
| 4308 | set = ! set; |
| 4309 | |
| 4310 | easy = ((varflags & VSQUOTE) == 0 || |
| 4311 | (*var == '@' && shellparam.nparam != 1)); |
| 4312 | |
| 4313 | |
| 4314 | switch (subtype) { |
| 4315 | case VSLENGTH: |
| 4316 | expdest = cvtnum(varlen, expdest); |
| 4317 | goto record; |
| 4318 | |
| 4319 | case VSNORMAL: |
| 4320 | if (!easy) |
| 4321 | break; |
| 4322 | record: |
| 4323 | recordregion(startloc, expdest - stackblock(), |
| 4324 | varflags & VSQUOTE); |
| 4325 | break; |
| 4326 | |
| 4327 | case VSPLUS: |
| 4328 | case VSMINUS: |
| 4329 | if (!set) { |
| 4330 | argstr(p, flag); |
| 4331 | break; |
| 4332 | } |
| 4333 | if (easy) |
| 4334 | goto record; |
| 4335 | break; |
| 4336 | |
| 4337 | case VSTRIMLEFT: |
| 4338 | case VSTRIMLEFTMAX: |
| 4339 | case VSTRIMRIGHT: |
| 4340 | case VSTRIMRIGHTMAX: |
| 4341 | if (!set) |
| 4342 | break; |
| 4343 | /* |
| 4344 | * Terminate the string and start recording the pattern |
| 4345 | * right after it |
| 4346 | */ |
| 4347 | STPUTC('\0', expdest); |
| 4348 | patloc = expdest - stackblock(); |
| 4349 | if (subevalvar(p, NULL, patloc, subtype, |
| 4350 | startloc, varflags, quotes) == 0) { |
| 4351 | int amount = (expdest - stackblock() - patloc) + 1; |
| 4352 | STADJUST(-amount, expdest); |
| 4353 | } |
| 4354 | /* Remove any recorded regions beyond start of variable */ |
| 4355 | removerecordregions(startloc); |
| 4356 | goto record; |
| 4357 | |
| 4358 | case VSASSIGN: |
| 4359 | case VSQUESTION: |
| 4360 | if (!set) { |
| 4361 | if (subevalvar(p, var, 0, subtype, startloc, |
| 4362 | varflags, quotes)) { |
| 4363 | varflags &= ~VSNUL; |
| 4364 | /* |
| 4365 | * Remove any recorded regions beyond |
| 4366 | * start of variable |
| 4367 | */ |
| 4368 | removerecordregions(startloc); |
| 4369 | goto again; |
| 4370 | } |
| 4371 | break; |
| 4372 | } |
| 4373 | if (easy) |
| 4374 | goto record; |
| 4375 | break; |
| 4376 | |
| 4377 | #ifdef DEBUG |
| 4378 | default: |
| 4379 | abort(); |
| 4380 | #endif |
| 4381 | } |
| 4382 | |
| 4383 | if (subtype != VSNORMAL) { /* skip to end of alternative */ |
| 4384 | int nesting = 1; |
| 4385 | for (;;) { |
| 4386 | if ((c = *p++) == CTLESC) |
| 4387 | p++; |
| 4388 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { |
| 4389 | if (set) |
| 4390 | argbackq = argbackq->next; |
| 4391 | } else if (c == CTLVAR) { |
| 4392 | if ((*p++ & VSTYPE) != VSNORMAL) |
| 4393 | nesting++; |
| 4394 | } else if (c == CTLENDVAR) { |
| 4395 | if (--nesting == 0) |
| 4396 | break; |
| 4397 | } |
| 4398 | } |
| 4399 | } |
| 4400 | return p; |
| 4401 | } |
| 4402 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4403 | |
| 4404 | /* |
| 4405 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 4406 | * characters to allow for further processing. Otherwise treat |
| 4407 | * $@ like $* since no splitting will be performed. |
| 4408 | */ |
| 4409 | |
| 4410 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4411 | argstr(char *p, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4412 | { |
| 4413 | char c; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4414 | int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4415 | int firsteq = 1; |
| 4416 | |
| 4417 | if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE))) |
| 4418 | p = exptilde(p, flag); |
| 4419 | for (;;) { |
| 4420 | switch (c = *p++) { |
| 4421 | case '\0': |
| 4422 | case CTLENDVAR: /* ??? */ |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4423 | return; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4424 | case CTLQUOTEMARK: |
| 4425 | /* "$@" syntax adherence hack */ |
| 4426 | if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=') |
| 4427 | break; |
| 4428 | if ((flag & EXP_FULL) != 0) |
| 4429 | STPUTC(c, expdest); |
| 4430 | break; |
| 4431 | case CTLESC: |
| 4432 | if (quotes) |
| 4433 | STPUTC(c, expdest); |
| 4434 | c = *p++; |
| 4435 | STPUTC(c, expdest); |
| 4436 | break; |
| 4437 | case CTLVAR: |
| 4438 | p = evalvar(p, flag); |
| 4439 | break; |
| 4440 | case CTLBACKQ: |
| 4441 | case CTLBACKQ|CTLQUOTE: |
| 4442 | expbackq(argbackq->n, c & CTLQUOTE, flag); |
| 4443 | argbackq = argbackq->next; |
| 4444 | break; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 4445 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4446 | case CTLENDARI: |
| 4447 | expari(flag); |
| 4448 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4449 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4450 | case ':': |
| 4451 | case '=': |
| 4452 | /* |
| 4453 | * sort of a hack - expand tildes in variable |
| 4454 | * assignments (after the first '=' and after ':'s). |
| 4455 | */ |
| 4456 | STPUTC(c, expdest); |
| 4457 | if (flag & EXP_VARTILDE && *p == '~') { |
| 4458 | if (c == '=') { |
| 4459 | if (firsteq) |
| 4460 | firsteq = 0; |
| 4461 | else |
| 4462 | break; |
| 4463 | } |
| 4464 | p = exptilde(p, flag); |
| 4465 | } |
| 4466 | break; |
| 4467 | default: |
| 4468 | STPUTC(c, expdest); |
| 4469 | } |
| 4470 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4471 | return; |
| 4472 | } |
| 4473 | |
| 4474 | static char * |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4475 | exptilde(char *p, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4476 | { |
| 4477 | char c, *startp = p; |
| 4478 | struct passwd *pw; |
| 4479 | const char *home; |
| 4480 | int quotes = flag & (EXP_FULL | EXP_CASE); |
| 4481 | |
| 4482 | while ((c = *p) != '\0') { |
| 4483 | switch(c) { |
| 4484 | case CTLESC: |
| 4485 | return (startp); |
| 4486 | case CTLQUOTEMARK: |
| 4487 | return (startp); |
| 4488 | case ':': |
| 4489 | if (flag & EXP_VARTILDE) |
| 4490 | goto done; |
| 4491 | break; |
| 4492 | case '/': |
| 4493 | goto done; |
| 4494 | } |
| 4495 | p++; |
| 4496 | } |
| 4497 | done: |
| 4498 | *p = '\0'; |
| 4499 | if (*(startp+1) == '\0') { |
| 4500 | if ((home = lookupvar("HOME")) == NULL) |
| 4501 | goto lose; |
| 4502 | } else { |
| 4503 | if ((pw = getpwnam(startp+1)) == NULL) |
| 4504 | goto lose; |
| 4505 | home = pw->pw_dir; |
| 4506 | } |
| 4507 | if (*home == '\0') |
| 4508 | goto lose; |
| 4509 | *p = c; |
| 4510 | strtodest(home, SQSYNTAX, quotes); |
| 4511 | return (p); |
| 4512 | lose: |
| 4513 | *p = c; |
| 4514 | return (startp); |
| 4515 | } |
| 4516 | |
| 4517 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4518 | static void |
| 4519 | removerecordregions(int endoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4520 | { |
| 4521 | if (ifslastp == NULL) |
| 4522 | return; |
| 4523 | |
| 4524 | if (ifsfirst.endoff > endoff) { |
| 4525 | while (ifsfirst.next != NULL) { |
| 4526 | struct ifsregion *ifsp; |
| 4527 | INTOFF; |
| 4528 | ifsp = ifsfirst.next->next; |
| 4529 | ckfree(ifsfirst.next); |
| 4530 | ifsfirst.next = ifsp; |
| 4531 | INTON; |
| 4532 | } |
| 4533 | if (ifsfirst.begoff > endoff) |
| 4534 | ifslastp = NULL; |
| 4535 | else { |
| 4536 | ifslastp = &ifsfirst; |
| 4537 | ifsfirst.endoff = endoff; |
| 4538 | } |
| 4539 | return; |
| 4540 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4541 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4542 | ifslastp = &ifsfirst; |
| 4543 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
| 4544 | ifslastp=ifslastp->next; |
| 4545 | while (ifslastp->next != NULL) { |
| 4546 | struct ifsregion *ifsp; |
| 4547 | INTOFF; |
| 4548 | ifsp = ifslastp->next->next; |
| 4549 | ckfree(ifslastp->next); |
| 4550 | ifslastp->next = ifsp; |
| 4551 | INTON; |
| 4552 | } |
| 4553 | if (ifslastp->endoff > endoff) |
| 4554 | ifslastp->endoff = endoff; |
| 4555 | } |
| 4556 | |
| 4557 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 4558 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4559 | /* |
| 4560 | * Expand arithmetic expression. Backup to start of expression, |
| 4561 | * evaluate, place result in (backed up) result, adjust string position. |
| 4562 | */ |
| 4563 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4564 | expari(int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4565 | { |
| 4566 | char *p, *start; |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 4567 | int errcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4568 | int result; |
| 4569 | int begoff; |
| 4570 | int quotes = flag & (EXP_FULL | EXP_CASE); |
| 4571 | int quoted; |
| 4572 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4573 | /* ifsfree(); */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4574 | |
| 4575 | /* |
| 4576 | * This routine is slightly over-complicated for |
| 4577 | * efficiency. First we make sure there is |
| 4578 | * enough space for the result, which may be bigger |
| 4579 | * than the expression if we add exponentation. Next we |
| 4580 | * scan backwards looking for the start of arithmetic. If the |
| 4581 | * next previous character is a CTLESC character, then we |
| 4582 | * have to rescan starting from the beginning since CTLESC |
| 4583 | * characters have to be processed left to right. |
| 4584 | */ |
| 4585 | CHECKSTRSPACE(10, expdest); |
| 4586 | USTPUTC('\0', expdest); |
| 4587 | start = stackblock(); |
| 4588 | p = expdest - 1; |
| 4589 | while (*p != CTLARI && p >= start) |
| 4590 | --p; |
| 4591 | if (*p != CTLARI) |
| 4592 | error("missing CTLARI (shouldn't happen)"); |
| 4593 | if (p > start && *(p-1) == CTLESC) |
| 4594 | for (p = start; *p != CTLARI; p++) |
| 4595 | if (*p == CTLESC) |
| 4596 | p++; |
| 4597 | |
| 4598 | if (p[1] == '"') |
| 4599 | quoted=1; |
| 4600 | else |
| 4601 | quoted=0; |
| 4602 | begoff = p - start; |
| 4603 | removerecordregions(begoff); |
| 4604 | if (quotes) |
| 4605 | rmescapes(p+2); |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 4606 | result = arith(p+2, &errcode); |
| 4607 | if (errcode < 0) { |
| 4608 | if(errcode == -2) |
| 4609 | error("divide by zero"); |
| 4610 | else |
| 4611 | error("syntax error: \"%s\"\n", p+2); |
| 4612 | } |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 4613 | snprintf(p, 12, "%d", result); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4614 | |
| 4615 | while (*p++) |
| 4616 | ; |
| 4617 | |
| 4618 | if (quoted == 0) |
| 4619 | recordregion(begoff, p - 1 - start, 0); |
| 4620 | result = expdest - p + 1; |
| 4621 | STADJUST(-result, expdest); |
| 4622 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4623 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4624 | |
| 4625 | /* |
| 4626 | * Expand stuff in backwards quotes. |
| 4627 | */ |
| 4628 | |
| 4629 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4630 | expbackq(union node *cmd, int quoted, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4631 | { |
| 4632 | volatile struct backcmd in; |
| 4633 | int i; |
| 4634 | char buf[128]; |
| 4635 | char *p; |
| 4636 | char *dest = expdest; |
| 4637 | volatile struct ifsregion saveifs; |
| 4638 | struct ifsregion *volatile savelastp; |
| 4639 | struct nodelist *volatile saveargbackq; |
| 4640 | char lastc; |
| 4641 | int startloc = dest - stackblock(); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 4642 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4643 | volatile int saveherefd; |
| 4644 | int quotes = flag & (EXP_FULL | EXP_CASE); |
| 4645 | struct jmploc jmploc; |
| 4646 | struct jmploc *volatile savehandler; |
| 4647 | int ex; |
| 4648 | |
| 4649 | #if __GNUC__ |
| 4650 | /* Avoid longjmp clobbering */ |
| 4651 | (void) &dest; |
| 4652 | (void) &syntax; |
| 4653 | #endif |
| 4654 | |
| 4655 | in.fd = -1; |
| 4656 | in.buf = 0; |
| 4657 | in.jp = 0; |
| 4658 | |
| 4659 | INTOFF; |
| 4660 | saveifs = ifsfirst; |
| 4661 | savelastp = ifslastp; |
| 4662 | saveargbackq = argbackq; |
| 4663 | saveherefd = herefd; |
| 4664 | herefd = -1; |
| 4665 | if ((ex = setjmp(jmploc.loc))) { |
| 4666 | goto err1; |
| 4667 | } |
| 4668 | savehandler = handler; |
| 4669 | handler = &jmploc; |
| 4670 | INTON; |
| 4671 | p = grabstackstr(dest); |
| 4672 | evalbackcmd(cmd, (struct backcmd *) &in); |
| 4673 | ungrabstackstr(p, dest); |
| 4674 | err1: |
| 4675 | INTOFF; |
| 4676 | ifsfirst = saveifs; |
| 4677 | ifslastp = savelastp; |
| 4678 | argbackq = saveargbackq; |
| 4679 | herefd = saveherefd; |
| 4680 | if (ex) { |
| 4681 | goto err2; |
| 4682 | } |
| 4683 | |
| 4684 | p = in.buf; |
| 4685 | lastc = '\0'; |
| 4686 | for (;;) { |
| 4687 | if (--in.nleft < 0) { |
| 4688 | if (in.fd < 0) |
| 4689 | break; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 4690 | i = safe_read(in.fd, buf, sizeof buf); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4691 | TRACE(("expbackq: read returns %d\n", i)); |
| 4692 | if (i <= 0) |
| 4693 | break; |
| 4694 | p = buf; |
| 4695 | in.nleft = i - 1; |
| 4696 | } |
| 4697 | lastc = *p++; |
| 4698 | if (lastc != '\0') { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 4699 | if (quotes && SIT(lastc, syntax) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4700 | STPUTC(CTLESC, dest); |
| 4701 | STPUTC(lastc, dest); |
| 4702 | } |
| 4703 | } |
| 4704 | |
| 4705 | /* Eat all trailing newlines */ |
| 4706 | for (; dest > stackblock() && dest[-1] == '\n';) |
| 4707 | STUNPUTC(dest); |
| 4708 | |
| 4709 | err2: |
| 4710 | if (in.fd >= 0) |
| 4711 | close(in.fd); |
| 4712 | if (in.buf) |
| 4713 | ckfree(in.buf); |
| 4714 | if (in.jp) |
| 4715 | exitstatus = waitforjob(in.jp); |
| 4716 | handler = savehandler; |
| 4717 | if (ex) { |
| 4718 | longjmp(handler->loc, 1); |
| 4719 | } |
| 4720 | if (quoted == 0) |
| 4721 | recordregion(startloc, dest - stackblock(), 0); |
| 4722 | TRACE(("evalbackq: size=%d: \"%.*s\"\n", |
| 4723 | (dest - stackblock()) - startloc, |
| 4724 | (dest - stackblock()) - startloc, |
| 4725 | stackblock() + startloc)); |
| 4726 | expdest = dest; |
| 4727 | INTON; |
| 4728 | } |
| 4729 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4730 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4731 | subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varflags, int quotes) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4732 | { |
| 4733 | char *startp; |
| 4734 | char *loc = NULL; |
| 4735 | char *q; |
| 4736 | int c = 0; |
| 4737 | int saveherefd = herefd; |
| 4738 | struct nodelist *saveargbackq = argbackq; |
| 4739 | int amount; |
| 4740 | |
| 4741 | herefd = -1; |
| 4742 | argstr(p, subtype != VSASSIGN && subtype != VSQUESTION ? EXP_CASE : 0); |
| 4743 | STACKSTRNUL(expdest); |
| 4744 | herefd = saveherefd; |
| 4745 | argbackq = saveargbackq; |
| 4746 | startp = stackblock() + startloc; |
| 4747 | if (str == NULL) |
| 4748 | str = stackblock() + strloc; |
| 4749 | |
| 4750 | switch (subtype) { |
| 4751 | case VSASSIGN: |
| 4752 | setvar(str, startp, 0); |
| 4753 | amount = startp - expdest; |
| 4754 | STADJUST(amount, expdest); |
| 4755 | varflags &= ~VSNUL; |
| 4756 | if (c != 0) |
| 4757 | *loc = c; |
| 4758 | return 1; |
| 4759 | |
| 4760 | case VSQUESTION: |
| 4761 | if (*p != CTLENDVAR) { |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 4762 | out2fmt(snlfmt, startp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4763 | error((char *)NULL); |
| 4764 | } |
| 4765 | error("%.*s: parameter %snot set", p - str - 1, |
| 4766 | str, (varflags & VSNUL) ? "null or " |
| 4767 | : nullstr); |
| 4768 | /* NOTREACHED */ |
| 4769 | |
| 4770 | case VSTRIMLEFT: |
| 4771 | for (loc = startp; loc < str; loc++) { |
| 4772 | c = *loc; |
| 4773 | *loc = '\0'; |
| 4774 | if (patmatch2(str, startp, quotes)) |
| 4775 | goto recordleft; |
| 4776 | *loc = c; |
| 4777 | if (quotes && *loc == CTLESC) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4778 | loc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4779 | } |
| 4780 | return 0; |
| 4781 | |
| 4782 | case VSTRIMLEFTMAX: |
| 4783 | for (loc = str - 1; loc >= startp;) { |
| 4784 | c = *loc; |
| 4785 | *loc = '\0'; |
| 4786 | if (patmatch2(str, startp, quotes)) |
| 4787 | goto recordleft; |
| 4788 | *loc = c; |
| 4789 | loc--; |
| 4790 | if (quotes && loc > startp && *(loc - 1) == CTLESC) { |
| 4791 | for (q = startp; q < loc; q++) |
| 4792 | if (*q == CTLESC) |
| 4793 | q++; |
| 4794 | if (q > loc) |
| 4795 | loc--; |
| 4796 | } |
| 4797 | } |
| 4798 | return 0; |
| 4799 | |
| 4800 | case VSTRIMRIGHT: |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4801 | for (loc = str - 1; loc >= startp;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4802 | if (patmatch2(str, loc, quotes)) |
| 4803 | goto recordright; |
| 4804 | loc--; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4805 | if (quotes && loc > startp && *(loc - 1) == CTLESC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4806 | for (q = startp; q < loc; q++) |
| 4807 | if (*q == CTLESC) |
| 4808 | q++; |
| 4809 | if (q > loc) |
| 4810 | loc--; |
| 4811 | } |
| 4812 | } |
| 4813 | return 0; |
| 4814 | |
| 4815 | case VSTRIMRIGHTMAX: |
| 4816 | for (loc = startp; loc < str - 1; loc++) { |
| 4817 | if (patmatch2(str, loc, quotes)) |
| 4818 | goto recordright; |
| 4819 | if (quotes && *loc == CTLESC) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4820 | loc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4821 | } |
| 4822 | return 0; |
| 4823 | |
| 4824 | #ifdef DEBUG |
| 4825 | default: |
| 4826 | abort(); |
| 4827 | #endif |
| 4828 | } |
| 4829 | |
| 4830 | recordleft: |
| 4831 | *loc = c; |
| 4832 | amount = ((str - 1) - (loc - startp)) - expdest; |
| 4833 | STADJUST(amount, expdest); |
| 4834 | while (loc != str - 1) |
| 4835 | *startp++ = *loc++; |
| 4836 | return 1; |
| 4837 | |
| 4838 | recordright: |
| 4839 | amount = loc - expdest; |
| 4840 | STADJUST(amount, expdest); |
| 4841 | STPUTC('\0', expdest); |
| 4842 | STADJUST(-1, expdest); |
| 4843 | return 1; |
| 4844 | } |
| 4845 | |
| 4846 | |
| 4847 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4848 | * Test whether a specialized variable is set. |
| 4849 | */ |
| 4850 | |
| 4851 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4852 | varisset(char *name, int nulok) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4853 | { |
| 4854 | if (*name == '!') |
| 4855 | return backgndpid != -1; |
| 4856 | else if (*name == '@' || *name == '*') { |
| 4857 | if (*shellparam.p == NULL) |
| 4858 | return 0; |
| 4859 | |
| 4860 | if (nulok) { |
| 4861 | char **av; |
| 4862 | |
| 4863 | for (av = shellparam.p; *av; av++) |
| 4864 | if (**av != '\0') |
| 4865 | return 1; |
| 4866 | return 0; |
| 4867 | } |
| 4868 | } else if (is_digit(*name)) { |
| 4869 | char *ap; |
| 4870 | int num = atoi(name); |
| 4871 | |
| 4872 | if (num > shellparam.nparam) |
| 4873 | return 0; |
| 4874 | |
| 4875 | if (num == 0) |
| 4876 | ap = arg0; |
| 4877 | else |
| 4878 | ap = shellparam.p[num - 1]; |
| 4879 | |
| 4880 | if (nulok && (ap == NULL || *ap == '\0')) |
| 4881 | return 0; |
| 4882 | } |
| 4883 | return 1; |
| 4884 | } |
| 4885 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4886 | /* |
| 4887 | * Put a string on the stack. |
| 4888 | */ |
| 4889 | |
| 4890 | static void |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 4891 | strtodest(const char *p, int syntax, int quotes) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4892 | { |
| 4893 | while (*p) { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 4894 | if (quotes && SIT(*p,syntax) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4895 | STPUTC(CTLESC, expdest); |
| 4896 | STPUTC(*p++, expdest); |
| 4897 | } |
| 4898 | } |
| 4899 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4900 | /* |
| 4901 | * Add the value of a specialized variable to the stack string. |
| 4902 | */ |
| 4903 | |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4904 | static inline void |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 4905 | varvalue(char *name, int quoted, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4906 | { |
| 4907 | int num; |
| 4908 | char *p; |
| 4909 | int i; |
| 4910 | int sep; |
| 4911 | int sepq = 0; |
| 4912 | char **ap; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 4913 | int syntax; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4914 | int allow_split = flags & EXP_FULL; |
| 4915 | int quotes = flags & (EXP_FULL | EXP_CASE); |
| 4916 | |
| 4917 | syntax = quoted ? DQSYNTAX : BASESYNTAX; |
| 4918 | switch (*name) { |
| 4919 | case '$': |
| 4920 | num = rootpid; |
| 4921 | goto numvar; |
| 4922 | case '?': |
| 4923 | num = oexitstatus; |
| 4924 | goto numvar; |
| 4925 | case '#': |
| 4926 | num = shellparam.nparam; |
| 4927 | goto numvar; |
| 4928 | case '!': |
| 4929 | num = backgndpid; |
| 4930 | numvar: |
| 4931 | expdest = cvtnum(num, expdest); |
| 4932 | break; |
| 4933 | case '-': |
| 4934 | for (i = 0 ; i < NOPTS ; i++) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4935 | if (optent_val(i)) |
| 4936 | STPUTC(optent_letter(optlist[i]), expdest); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4937 | } |
| 4938 | break; |
| 4939 | case '@': |
| 4940 | if (allow_split && quoted) { |
| 4941 | sep = 1 << CHAR_BIT; |
| 4942 | goto param; |
| 4943 | } |
| 4944 | /* fall through */ |
| 4945 | case '*': |
| 4946 | sep = ifsset() ? ifsval()[0] : ' '; |
| 4947 | if (quotes) { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 4948 | sepq = SIT(sep,syntax) == CCTL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4949 | } |
| 4950 | param: |
| 4951 | for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { |
| 4952 | strtodest(p, syntax, quotes); |
| 4953 | if (*ap && sep) { |
| 4954 | if (sepq) |
| 4955 | STPUTC(CTLESC, expdest); |
| 4956 | STPUTC(sep, expdest); |
| 4957 | } |
| 4958 | } |
| 4959 | break; |
| 4960 | case '0': |
| 4961 | strtodest(arg0, syntax, quotes); |
| 4962 | break; |
| 4963 | default: |
| 4964 | num = atoi(name); |
| 4965 | if (num > 0 && num <= shellparam.nparam) { |
| 4966 | strtodest(shellparam.p[num - 1], syntax, quotes); |
| 4967 | } |
| 4968 | break; |
| 4969 | } |
| 4970 | } |
| 4971 | |
| 4972 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4973 | /* |
| 4974 | * Record the fact that we have to scan this region of the |
| 4975 | * string for IFS characters. |
| 4976 | */ |
| 4977 | |
| 4978 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4979 | recordregion(int start, int end, int nulonly) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4980 | { |
| 4981 | struct ifsregion *ifsp; |
| 4982 | |
| 4983 | if (ifslastp == NULL) { |
| 4984 | ifsp = &ifsfirst; |
| 4985 | } else { |
| 4986 | INTOFF; |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4987 | ifsp = (struct ifsregion *)xmalloc(sizeof (struct ifsregion)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4988 | ifsp->next = NULL; |
| 4989 | ifslastp->next = ifsp; |
| 4990 | INTON; |
| 4991 | } |
| 4992 | ifslastp = ifsp; |
| 4993 | ifslastp->begoff = start; |
| 4994 | ifslastp->endoff = end; |
| 4995 | ifslastp->nulonly = nulonly; |
| 4996 | } |
| 4997 | |
| 4998 | |
| 4999 | |
| 5000 | /* |
| 5001 | * Break the argument string into pieces based upon IFS and add the |
| 5002 | * strings to the argument list. The regions of the string to be |
| 5003 | * searched for IFS characters have been stored by recordregion. |
| 5004 | */ |
| 5005 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5006 | ifsbreakup(char *string, struct arglist *arglist) |
| 5007 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5008 | struct ifsregion *ifsp; |
| 5009 | struct strlist *sp; |
| 5010 | char *start; |
| 5011 | char *p; |
| 5012 | char *q; |
| 5013 | const char *ifs, *realifs; |
| 5014 | int ifsspc; |
| 5015 | int nulonly; |
| 5016 | |
| 5017 | |
| 5018 | start = string; |
| 5019 | ifsspc = 0; |
| 5020 | nulonly = 0; |
| 5021 | realifs = ifsset() ? ifsval() : defifs; |
| 5022 | if (ifslastp != NULL) { |
| 5023 | ifsp = &ifsfirst; |
| 5024 | do { |
| 5025 | p = string + ifsp->begoff; |
| 5026 | nulonly = ifsp->nulonly; |
| 5027 | ifs = nulonly ? nullstr : realifs; |
| 5028 | ifsspc = 0; |
| 5029 | while (p < string + ifsp->endoff) { |
| 5030 | q = p; |
| 5031 | if (*p == CTLESC) |
| 5032 | p++; |
| 5033 | if (strchr(ifs, *p)) { |
| 5034 | if (!nulonly) |
| 5035 | ifsspc = (strchr(defifs, *p) != NULL); |
| 5036 | /* Ignore IFS whitespace at start */ |
| 5037 | if (q == start && ifsspc) { |
| 5038 | p++; |
| 5039 | start = p; |
| 5040 | continue; |
| 5041 | } |
| 5042 | *q = '\0'; |
| 5043 | sp = (struct strlist *)stalloc(sizeof *sp); |
| 5044 | sp->text = start; |
| 5045 | *arglist->lastp = sp; |
| 5046 | arglist->lastp = &sp->next; |
| 5047 | p++; |
| 5048 | if (!nulonly) { |
| 5049 | for (;;) { |
| 5050 | if (p >= string + ifsp->endoff) { |
| 5051 | break; |
| 5052 | } |
| 5053 | q = p; |
| 5054 | if (*p == CTLESC) |
| 5055 | p++; |
| 5056 | if (strchr(ifs, *p) == NULL ) { |
| 5057 | p = q; |
| 5058 | break; |
| 5059 | } else if (strchr(defifs, *p) == NULL) { |
| 5060 | if (ifsspc) { |
| 5061 | p++; |
| 5062 | ifsspc = 0; |
| 5063 | } else { |
| 5064 | p = q; |
| 5065 | break; |
| 5066 | } |
| 5067 | } else |
| 5068 | p++; |
| 5069 | } |
| 5070 | } |
| 5071 | start = p; |
| 5072 | } else |
| 5073 | p++; |
| 5074 | } |
| 5075 | } while ((ifsp = ifsp->next) != NULL); |
| 5076 | if (!(*start || (!ifsspc && start > string && nulonly))) { |
| 5077 | return; |
| 5078 | } |
| 5079 | } |
| 5080 | |
| 5081 | sp = (struct strlist *)stalloc(sizeof *sp); |
| 5082 | sp->text = start; |
| 5083 | *arglist->lastp = sp; |
| 5084 | arglist->lastp = &sp->next; |
| 5085 | } |
| 5086 | |
| 5087 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5088 | ifsfree(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5089 | { |
| 5090 | while (ifsfirst.next != NULL) { |
| 5091 | struct ifsregion *ifsp; |
| 5092 | INTOFF; |
| 5093 | ifsp = ifsfirst.next->next; |
| 5094 | ckfree(ifsfirst.next); |
| 5095 | ifsfirst.next = ifsp; |
| 5096 | INTON; |
| 5097 | } |
| 5098 | ifslastp = NULL; |
| 5099 | ifsfirst.next = NULL; |
| 5100 | } |
| 5101 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5102 | /* |
| 5103 | * Add a file name to the list. |
| 5104 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5105 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5106 | static void |
| 5107 | addfname(const char *name) |
| 5108 | { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5109 | struct strlist *sp; |
| 5110 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5111 | sp = (struct strlist *)stalloc(sizeof *sp); |
Aaron Lehmann | 95877b6 | 2001-12-31 06:00:57 +0000 | [diff] [blame] | 5112 | sp->text = sstrdup(name); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5113 | *exparg.lastp = sp; |
| 5114 | exparg.lastp = &sp->next; |
| 5115 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5116 | |
| 5117 | /* |
| 5118 | * Expand shell metacharacters. At this point, the only control characters |
| 5119 | * should be escapes. The results are stored in the list exparg. |
| 5120 | */ |
| 5121 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5122 | #if defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5123 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5124 | expandmeta(struct strlist *str, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5125 | { |
| 5126 | const char *p; |
| 5127 | glob_t pglob; |
| 5128 | /* TODO - EXP_REDIR */ |
| 5129 | |
| 5130 | while (str) { |
| 5131 | if (fflag) |
| 5132 | goto nometa; |
| 5133 | p = preglob(str->text); |
| 5134 | INTOFF; |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 5135 | switch (glob(p, 0, 0, &pglob)) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5136 | case 0: |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 5137 | if(pglob.gl_pathv[1]==0 && !strcmp(p, pglob.gl_pathv[0])) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5138 | goto nometa2; |
| 5139 | addglob(&pglob); |
| 5140 | globfree(&pglob); |
| 5141 | INTON; |
| 5142 | break; |
| 5143 | case GLOB_NOMATCH: |
| 5144 | nometa2: |
| 5145 | globfree(&pglob); |
| 5146 | INTON; |
| 5147 | nometa: |
| 5148 | *exparg.lastp = str; |
| 5149 | rmescapes(str->text); |
| 5150 | exparg.lastp = &str->next; |
| 5151 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5152 | default: /* GLOB_NOSPACE */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5153 | error("Out of space"); |
| 5154 | } |
| 5155 | str = str->next; |
| 5156 | } |
| 5157 | } |
| 5158 | |
| 5159 | |
| 5160 | /* |
| 5161 | * Add the result of glob(3) to the list. |
| 5162 | */ |
| 5163 | |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5164 | static inline void |
| 5165 | addglob(const glob_t *pglob) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5166 | { |
| 5167 | char **p = pglob->gl_pathv; |
| 5168 | |
| 5169 | do { |
| 5170 | addfname(*p); |
| 5171 | } while (*++p); |
| 5172 | } |
| 5173 | |
| 5174 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5175 | #else /* defined(__GLIBC__) && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN) */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5176 | static char *expdir; |
| 5177 | |
| 5178 | |
| 5179 | static void |
Eric Andersen | ceef50b | 2001-12-21 11:22:26 +0000 | [diff] [blame] | 5180 | expandmeta(struct strlist *str, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5181 | { |
| 5182 | char *p; |
| 5183 | struct strlist **savelastp; |
| 5184 | struct strlist *sp; |
| 5185 | char c; |
| 5186 | /* TODO - EXP_REDIR */ |
| 5187 | |
| 5188 | while (str) { |
| 5189 | if (fflag) |
| 5190 | goto nometa; |
| 5191 | p = str->text; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5192 | for (;;) { /* fast check for meta chars */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5193 | if ((c = *p++) == '\0') |
| 5194 | goto nometa; |
| 5195 | if (c == '*' || c == '?' || c == '[' || c == '!') |
| 5196 | break; |
| 5197 | } |
| 5198 | savelastp = exparg.lastp; |
| 5199 | INTOFF; |
| 5200 | if (expdir == NULL) { |
| 5201 | int i = strlen(str->text); |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5202 | expdir = xmalloc(i < 2048 ? 2048 : i); /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | expmeta(expdir, str->text); |
| 5206 | ckfree(expdir); |
| 5207 | expdir = NULL; |
| 5208 | INTON; |
| 5209 | if (exparg.lastp == savelastp) { |
| 5210 | /* |
| 5211 | * no matches |
| 5212 | */ |
| 5213 | nometa: |
| 5214 | *exparg.lastp = str; |
| 5215 | rmescapes(str->text); |
| 5216 | exparg.lastp = &str->next; |
| 5217 | } else { |
| 5218 | *exparg.lastp = NULL; |
| 5219 | *savelastp = sp = expsort(*savelastp); |
| 5220 | while (sp->next != NULL) |
| 5221 | sp = sp->next; |
| 5222 | exparg.lastp = &sp->next; |
| 5223 | } |
| 5224 | str = str->next; |
| 5225 | } |
| 5226 | } |
| 5227 | |
| 5228 | |
| 5229 | /* |
| 5230 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 5231 | */ |
| 5232 | |
| 5233 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5234 | expmeta(char *enddir, char *name) |
| 5235 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5236 | char *p; |
| 5237 | const char *cp; |
| 5238 | char *q; |
| 5239 | char *start; |
| 5240 | char *endname; |
| 5241 | int metaflag; |
| 5242 | struct stat statb; |
| 5243 | DIR *dirp; |
| 5244 | struct dirent *dp; |
| 5245 | int atend; |
| 5246 | int matchdot; |
| 5247 | |
| 5248 | metaflag = 0; |
| 5249 | start = name; |
| 5250 | for (p = name ; ; p++) { |
| 5251 | if (*p == '*' || *p == '?') |
| 5252 | metaflag = 1; |
| 5253 | else if (*p == '[') { |
| 5254 | q = p + 1; |
| 5255 | if (*q == '!') |
| 5256 | q++; |
| 5257 | for (;;) { |
| 5258 | while (*q == CTLQUOTEMARK) |
| 5259 | q++; |
| 5260 | if (*q == CTLESC) |
| 5261 | q++; |
| 5262 | if (*q == '/' || *q == '\0') |
| 5263 | break; |
| 5264 | if (*++q == ']') { |
| 5265 | metaflag = 1; |
| 5266 | break; |
| 5267 | } |
| 5268 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 5269 | } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5270 | metaflag = 1; |
| 5271 | } else if (*p == '\0') |
| 5272 | break; |
| 5273 | else if (*p == CTLQUOTEMARK) |
| 5274 | continue; |
| 5275 | else if (*p == CTLESC) |
| 5276 | p++; |
| 5277 | if (*p == '/') { |
| 5278 | if (metaflag) |
| 5279 | break; |
| 5280 | start = p + 1; |
| 5281 | } |
| 5282 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5283 | if (metaflag == 0) { /* we've reached the end of the file name */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5284 | if (enddir != expdir) |
| 5285 | metaflag++; |
| 5286 | for (p = name ; ; p++) { |
| 5287 | if (*p == CTLQUOTEMARK) |
| 5288 | continue; |
| 5289 | if (*p == CTLESC) |
| 5290 | p++; |
| 5291 | *enddir++ = *p; |
| 5292 | if (*p == '\0') |
| 5293 | break; |
| 5294 | } |
| 5295 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
| 5296 | addfname(expdir); |
| 5297 | return; |
| 5298 | } |
| 5299 | endname = p; |
| 5300 | if (start != name) { |
| 5301 | p = name; |
| 5302 | while (p < start) { |
| 5303 | while (*p == CTLQUOTEMARK) |
| 5304 | p++; |
| 5305 | if (*p == CTLESC) |
| 5306 | p++; |
| 5307 | *enddir++ = *p++; |
| 5308 | } |
| 5309 | } |
| 5310 | if (enddir == expdir) { |
| 5311 | cp = "."; |
| 5312 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 5313 | cp = "/"; |
| 5314 | } else { |
| 5315 | cp = expdir; |
| 5316 | enddir[-1] = '\0'; |
| 5317 | } |
| 5318 | if ((dirp = opendir(cp)) == NULL) |
| 5319 | return; |
| 5320 | if (enddir != expdir) |
| 5321 | enddir[-1] = '/'; |
| 5322 | if (*endname == 0) { |
| 5323 | atend = 1; |
| 5324 | } else { |
| 5325 | atend = 0; |
| 5326 | *endname++ = '\0'; |
| 5327 | } |
| 5328 | matchdot = 0; |
| 5329 | p = start; |
| 5330 | while (*p == CTLQUOTEMARK) |
| 5331 | p++; |
| 5332 | if (*p == CTLESC) |
| 5333 | p++; |
| 5334 | if (*p == '.') |
| 5335 | matchdot++; |
| 5336 | while (! int_pending() && (dp = readdir(dirp)) != NULL) { |
| 5337 | if (dp->d_name[0] == '.' && ! matchdot) |
| 5338 | continue; |
| 5339 | if (patmatch(start, dp->d_name, 0)) { |
| 5340 | if (atend) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5341 | strcpy(enddir, dp->d_name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5342 | addfname(expdir); |
| 5343 | } else { |
| 5344 | for (p = enddir, cp = dp->d_name; |
| 5345 | (*p++ = *cp++) != '\0';) |
| 5346 | continue; |
| 5347 | p[-1] = '/'; |
| 5348 | expmeta(p, endname); |
| 5349 | } |
| 5350 | } |
| 5351 | } |
| 5352 | closedir(dirp); |
| 5353 | if (! atend) |
| 5354 | endname[-1] = '/'; |
| 5355 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5356 | #endif /* defined(__GLIBC__) && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN) */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5357 | |
| 5358 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5359 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5360 | #if !(defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) && !defined(GLOB_BROKEN)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5361 | /* |
| 5362 | * Sort the results of file name expansion. It calculates the number of |
| 5363 | * strings to sort and then calls msort (short for merge sort) to do the |
| 5364 | * work. |
| 5365 | */ |
| 5366 | |
| 5367 | static struct strlist * |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5368 | expsort(struct strlist *str) |
| 5369 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5370 | int len; |
| 5371 | struct strlist *sp; |
| 5372 | |
| 5373 | len = 0; |
| 5374 | for (sp = str ; sp ; sp = sp->next) |
| 5375 | len++; |
| 5376 | return msort(str, len); |
| 5377 | } |
| 5378 | |
| 5379 | |
| 5380 | static struct strlist * |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5381 | msort(struct strlist *list, int len) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5382 | { |
| 5383 | struct strlist *p, *q = NULL; |
| 5384 | struct strlist **lpp; |
| 5385 | int half; |
| 5386 | int n; |
| 5387 | |
| 5388 | if (len <= 1) |
| 5389 | return list; |
| 5390 | half = len >> 1; |
| 5391 | p = list; |
| 5392 | for (n = half ; --n >= 0 ; ) { |
| 5393 | q = p; |
| 5394 | p = p->next; |
| 5395 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5396 | q->next = NULL; /* terminate first half of list */ |
| 5397 | q = msort(list, half); /* sort first half of list */ |
| 5398 | p = msort(p, len - half); /* sort second half */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5399 | lpp = &list; |
| 5400 | for (;;) { |
| 5401 | if (strcmp(p->text, q->text) < 0) { |
| 5402 | *lpp = p; |
| 5403 | lpp = &p->next; |
| 5404 | if ((p = *lpp) == NULL) { |
| 5405 | *lpp = q; |
| 5406 | break; |
| 5407 | } |
| 5408 | } else { |
| 5409 | *lpp = q; |
| 5410 | lpp = &q->next; |
| 5411 | if ((q = *lpp) == NULL) { |
| 5412 | *lpp = p; |
| 5413 | break; |
| 5414 | } |
| 5415 | } |
| 5416 | } |
| 5417 | return list; |
| 5418 | } |
| 5419 | #endif |
| 5420 | |
| 5421 | |
| 5422 | |
| 5423 | /* |
| 5424 | * Returns true if the pattern matches the string. |
| 5425 | */ |
| 5426 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5427 | #if defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5428 | /* squoted: string might have quote chars */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5429 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5430 | patmatch(char *pattern, char *string, int squoted) |
| 5431 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5432 | const char *p; |
| 5433 | char *q; |
| 5434 | |
| 5435 | p = preglob(pattern); |
| 5436 | q = squoted ? _rmescapes(string, RMESCAPE_ALLOC) : string; |
| 5437 | |
| 5438 | return !fnmatch(p, q, 0); |
| 5439 | } |
| 5440 | |
| 5441 | |
| 5442 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5443 | patmatch2(char *pattern, char *string, int squoted) |
| 5444 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5445 | char *p; |
| 5446 | int res; |
| 5447 | |
| 5448 | sstrnleft--; |
| 5449 | p = grabstackstr(expdest); |
| 5450 | res = patmatch(pattern, string, squoted); |
| 5451 | ungrabstackstr(p, expdest); |
| 5452 | return res; |
| 5453 | } |
| 5454 | #else |
| 5455 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5456 | patmatch(char *pattern, char *string, int squoted) { |
| 5457 | return pmatch(pattern, string, squoted); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5458 | } |
| 5459 | |
| 5460 | |
| 5461 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5462 | pmatch(char *pattern, char *string, int squoted) |
| 5463 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5464 | char *p, *q; |
| 5465 | char c; |
| 5466 | |
| 5467 | p = pattern; |
| 5468 | q = string; |
| 5469 | for (;;) { |
| 5470 | switch (c = *p++) { |
| 5471 | case '\0': |
| 5472 | goto breakloop; |
| 5473 | case CTLESC: |
| 5474 | if (squoted && *q == CTLESC) |
| 5475 | q++; |
| 5476 | if (*q++ != *p++) |
| 5477 | return 0; |
| 5478 | break; |
| 5479 | case CTLQUOTEMARK: |
| 5480 | continue; |
| 5481 | case '?': |
| 5482 | if (squoted && *q == CTLESC) |
| 5483 | q++; |
| 5484 | if (*q++ == '\0') |
| 5485 | return 0; |
| 5486 | break; |
| 5487 | case '*': |
| 5488 | c = *p; |
| 5489 | while (c == CTLQUOTEMARK || c == '*') |
| 5490 | c = *++p; |
| 5491 | if (c != CTLESC && c != CTLQUOTEMARK && |
| 5492 | c != '?' && c != '*' && c != '[') { |
| 5493 | while (*q != c) { |
| 5494 | if (squoted && *q == CTLESC && |
| 5495 | q[1] == c) |
| 5496 | break; |
| 5497 | if (*q == '\0') |
| 5498 | return 0; |
| 5499 | if (squoted && *q == CTLESC) |
| 5500 | q++; |
| 5501 | q++; |
| 5502 | } |
| 5503 | } |
| 5504 | do { |
| 5505 | if (pmatch(p, q, squoted)) |
| 5506 | return 1; |
| 5507 | if (squoted && *q == CTLESC) |
| 5508 | q++; |
| 5509 | } while (*q++ != '\0'); |
| 5510 | return 0; |
| 5511 | case '[': { |
| 5512 | char *endp; |
| 5513 | int invert, found; |
| 5514 | char chr; |
| 5515 | |
| 5516 | endp = p; |
| 5517 | if (*endp == '!') |
| 5518 | endp++; |
| 5519 | for (;;) { |
| 5520 | while (*endp == CTLQUOTEMARK) |
| 5521 | endp++; |
| 5522 | if (*endp == '\0') |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5523 | goto dft; /* no matching ] */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5524 | if (*endp == CTLESC) |
| 5525 | endp++; |
| 5526 | if (*++endp == ']') |
| 5527 | break; |
| 5528 | } |
| 5529 | invert = 0; |
| 5530 | if (*p == '!') { |
| 5531 | invert++; |
| 5532 | p++; |
| 5533 | } |
| 5534 | found = 0; |
| 5535 | chr = *q++; |
| 5536 | if (squoted && chr == CTLESC) |
| 5537 | chr = *q++; |
| 5538 | if (chr == '\0') |
| 5539 | return 0; |
| 5540 | c = *p++; |
| 5541 | do { |
| 5542 | if (c == CTLQUOTEMARK) |
| 5543 | continue; |
| 5544 | if (c == CTLESC) |
| 5545 | c = *p++; |
| 5546 | if (*p == '-' && p[1] != ']') { |
| 5547 | p++; |
| 5548 | while (*p == CTLQUOTEMARK) |
| 5549 | p++; |
| 5550 | if (*p == CTLESC) |
| 5551 | p++; |
| 5552 | if (chr >= c && chr <= *p) |
| 5553 | found = 1; |
| 5554 | p++; |
| 5555 | } else { |
| 5556 | if (chr == c) |
| 5557 | found = 1; |
| 5558 | } |
| 5559 | } while ((c = *p++) != ']'); |
| 5560 | if (found == invert) |
| 5561 | return 0; |
| 5562 | break; |
| 5563 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5564 | dft: default: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5565 | if (squoted && *q == CTLESC) |
| 5566 | q++; |
| 5567 | if (*q++ != c) |
| 5568 | return 0; |
| 5569 | break; |
| 5570 | } |
| 5571 | } |
| 5572 | breakloop: |
| 5573 | if (*q != '\0') |
| 5574 | return 0; |
| 5575 | return 1; |
| 5576 | } |
| 5577 | #endif |
| 5578 | |
| 5579 | |
| 5580 | |
| 5581 | /* |
| 5582 | * Remove any CTLESC characters from a string. |
| 5583 | */ |
| 5584 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5585 | #if defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(FNMATCH_BROKEN) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5586 | static char * |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5587 | _rmescapes(char *str, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5588 | { |
| 5589 | char *p, *q, *r; |
| 5590 | static const char qchars[] = { CTLESC, CTLQUOTEMARK, 0 }; |
| 5591 | |
| 5592 | p = strpbrk(str, qchars); |
| 5593 | if (!p) { |
| 5594 | return str; |
| 5595 | } |
| 5596 | q = p; |
| 5597 | r = str; |
| 5598 | if (flag & RMESCAPE_ALLOC) { |
| 5599 | size_t len = p - str; |
| 5600 | q = r = stalloc(strlen(p) + len + 1); |
| 5601 | if (len > 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5602 | memcpy(q, str, len); |
| 5603 | q += len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5604 | } |
| 5605 | } |
| 5606 | while (*p) { |
| 5607 | if (*p == CTLQUOTEMARK) { |
| 5608 | p++; |
| 5609 | continue; |
| 5610 | } |
| 5611 | if (*p == CTLESC) { |
| 5612 | p++; |
| 5613 | if (flag & RMESCAPE_GLOB && *p != '/') { |
| 5614 | *q++ = '\\'; |
| 5615 | } |
| 5616 | } |
| 5617 | *q++ = *p++; |
| 5618 | } |
| 5619 | *q = '\0'; |
| 5620 | return r; |
| 5621 | } |
| 5622 | #else |
| 5623 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5624 | rmescapes(char *str) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5625 | { |
| 5626 | char *p, *q; |
| 5627 | |
| 5628 | p = str; |
| 5629 | while (*p != CTLESC && *p != CTLQUOTEMARK) { |
| 5630 | if (*p++ == '\0') |
| 5631 | return; |
| 5632 | } |
| 5633 | q = p; |
| 5634 | while (*p) { |
| 5635 | if (*p == CTLQUOTEMARK) { |
| 5636 | p++; |
| 5637 | continue; |
| 5638 | } |
| 5639 | if (*p == CTLESC) |
| 5640 | p++; |
| 5641 | *q++ = *p++; |
| 5642 | } |
| 5643 | *q = '\0'; |
| 5644 | } |
| 5645 | #endif |
| 5646 | |
| 5647 | |
| 5648 | |
| 5649 | /* |
| 5650 | * See if a pattern matches in a case statement. |
| 5651 | */ |
| 5652 | |
| 5653 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5654 | casematch(union node *pattern, const char *val) |
| 5655 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5656 | struct stackmark smark; |
| 5657 | int result; |
| 5658 | char *p; |
| 5659 | |
| 5660 | setstackmark(&smark); |
| 5661 | argbackq = pattern->narg.backquote; |
| 5662 | STARTSTACKSTR(expdest); |
| 5663 | ifslastp = NULL; |
| 5664 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); |
| 5665 | STPUTC('\0', expdest); |
| 5666 | p = grabstackstr(expdest); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5667 | result = patmatch(p, (char *)val, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5668 | popstackmark(&smark); |
| 5669 | return result; |
| 5670 | } |
| 5671 | |
| 5672 | /* |
| 5673 | * Our own itoa(). |
| 5674 | */ |
| 5675 | |
| 5676 | static char * |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5677 | cvtnum(int num, char *buf) |
| 5678 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5679 | int len; |
| 5680 | |
| 5681 | CHECKSTRSPACE(32, buf); |
| 5682 | len = sprintf(buf, "%d", num); |
| 5683 | STADJUST(len, buf); |
| 5684 | return buf; |
| 5685 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5686 | /* |
| 5687 | * Editline and history functions (and glue). |
| 5688 | */ |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5689 | static int histcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5690 | { |
| 5691 | error("not compiled with history support"); |
| 5692 | /* NOTREACHED */ |
| 5693 | } |
| 5694 | |
| 5695 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5696 | struct redirtab { |
| 5697 | struct redirtab *next; |
Russ Dill | 4db3564 | 2001-07-26 05:58:40 +0000 | [diff] [blame] | 5698 | short renamed[10]; /* Current ash support only 0-9 descriptors */ |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 5699 | /* char on arm (and others) can't be negative */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5700 | }; |
| 5701 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5702 | static struct redirtab *redirlist; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5703 | |
| 5704 | extern char **environ; |
| 5705 | |
| 5706 | |
| 5707 | |
| 5708 | /* |
| 5709 | * Initialization code. |
| 5710 | */ |
| 5711 | |
| 5712 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5713 | init(void) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5714 | |
| 5715 | /* from cd.c: */ |
| 5716 | { |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 5717 | curdir = nullstr; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5718 | setpwd(0, 0); |
| 5719 | } |
| 5720 | |
| 5721 | /* from input.c: */ |
| 5722 | { |
| 5723 | basepf.nextc = basepf.buf = basebuf; |
| 5724 | } |
| 5725 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5726 | /* from var.c: */ |
| 5727 | { |
| 5728 | char **envp; |
| 5729 | char ppid[32]; |
| 5730 | |
| 5731 | initvar(); |
| 5732 | for (envp = environ ; *envp ; envp++) { |
| 5733 | if (strchr(*envp, '=')) { |
| 5734 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 5735 | } |
| 5736 | } |
| 5737 | |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 5738 | snprintf(ppid, sizeof(ppid), "%d", (int) getppid()); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5739 | setvar("PPID", ppid, 0); |
| 5740 | } |
| 5741 | } |
| 5742 | |
| 5743 | |
| 5744 | |
| 5745 | /* |
| 5746 | * This routine is called when an error or an interrupt occurs in an |
| 5747 | * interactive shell and control is returned to the main command loop. |
| 5748 | */ |
| 5749 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5750 | /* 1 == check for aliases, 2 == also check for assignments */ |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 5751 | static int checkalias; /* also used in no alias mode for check assignments */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5752 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5753 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5754 | reset(void) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5755 | |
| 5756 | /* from eval.c: */ |
| 5757 | { |
| 5758 | evalskip = 0; |
| 5759 | loopnest = 0; |
| 5760 | funcnest = 0; |
| 5761 | } |
| 5762 | |
| 5763 | /* from input.c: */ |
| 5764 | { |
| 5765 | if (exception != EXSHELLPROC) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5766 | parselleft = parsenleft = 0; /* clear input buffer */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5767 | popallfiles(); |
| 5768 | } |
| 5769 | |
| 5770 | /* from parser.c: */ |
| 5771 | { |
| 5772 | tokpushback = 0; |
| 5773 | checkkwd = 0; |
| 5774 | checkalias = 0; |
| 5775 | } |
| 5776 | |
| 5777 | /* from redir.c: */ |
| 5778 | { |
| 5779 | while (redirlist) |
| 5780 | popredir(); |
| 5781 | } |
| 5782 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
| 5785 | |
| 5786 | |
| 5787 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5788 | * This file implements the input routines used by the parser. |
| 5789 | */ |
| 5790 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5791 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5792 | static const char * cmdedit_prompt; |
| 5793 | static inline void putprompt(const char *s) { |
| 5794 | cmdedit_prompt = s; |
| 5795 | } |
| 5796 | #else |
| 5797 | static inline void putprompt(const char *s) { |
| 5798 | out2str(s); |
| 5799 | } |
| 5800 | #endif |
| 5801 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5802 | #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5803 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5804 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5805 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5806 | /* |
| 5807 | * Same as pgetc(), but ignores PEOA. |
| 5808 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5809 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 5810 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5811 | static int |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 5812 | pgetc2(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5813 | { |
| 5814 | int c; |
| 5815 | do { |
| 5816 | c = pgetc_macro(); |
| 5817 | } while (c == PEOA); |
| 5818 | return c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5819 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5820 | #else |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 5821 | static inline int pgetc2(void) { return pgetc_macro(); } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5822 | #endif |
| 5823 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5824 | /* |
| 5825 | * Read a line from the script. |
| 5826 | */ |
| 5827 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5828 | static inline char * |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5829 | pfgets(char *line, int len) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5830 | { |
| 5831 | char *p = line; |
| 5832 | int nleft = len; |
| 5833 | int c; |
| 5834 | |
| 5835 | while (--nleft > 0) { |
| 5836 | c = pgetc2(); |
| 5837 | if (c == PEOF) { |
| 5838 | if (p == line) |
| 5839 | return NULL; |
| 5840 | break; |
| 5841 | } |
| 5842 | *p++ = c; |
| 5843 | if (c == '\n') |
| 5844 | break; |
| 5845 | } |
| 5846 | *p = '\0'; |
| 5847 | return line; |
| 5848 | } |
| 5849 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5850 | static inline int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5851 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5852 | { |
| 5853 | int nr; |
| 5854 | char *buf = parsefile->buf; |
| 5855 | parsenextc = buf; |
| 5856 | |
| 5857 | retry: |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5858 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5859 | { |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 5860 | if (!iflag || parsefile->fd) |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 5861 | nr = safe_read(parsefile->fd, buf, BUFSIZ - 1); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5862 | else { |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 5863 | nr = cmdedit_read_input((char*)cmdedit_prompt, buf); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5864 | } |
| 5865 | } |
| 5866 | #else |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 5867 | nr = safe_read(parsefile->fd, buf, BUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5868 | #endif |
| 5869 | |
| 5870 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5871 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
| 5872 | int flags = fcntl(0, F_GETFL, 0); |
| 5873 | if (flags >= 0 && flags & O_NONBLOCK) { |
| 5874 | flags &=~ O_NONBLOCK; |
| 5875 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 5876 | out2str("sh: turning off NDELAY mode\n"); |
| 5877 | goto retry; |
| 5878 | } |
| 5879 | } |
| 5880 | } |
| 5881 | } |
| 5882 | return nr; |
| 5883 | } |
| 5884 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5885 | static void |
| 5886 | popstring(void) |
| 5887 | { |
| 5888 | struct strpush *sp = parsefile->strpush; |
| 5889 | |
| 5890 | INTOFF; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 5891 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5892 | if (sp->ap) { |
| 5893 | if (parsenextc[-1] == ' ' || parsenextc[-1] == '\t') { |
| 5894 | if (!checkalias) { |
| 5895 | checkalias = 1; |
| 5896 | } |
| 5897 | } |
| 5898 | if (sp->string != sp->ap->val) { |
| 5899 | ckfree(sp->string); |
| 5900 | } |
| 5901 | |
| 5902 | sp->ap->flag &= ~ALIASINUSE; |
| 5903 | if (sp->ap->flag & ALIASDEAD) { |
| 5904 | unalias(sp->ap->name); |
| 5905 | } |
| 5906 | } |
| 5907 | #endif |
| 5908 | parsenextc = sp->prevstring; |
| 5909 | parsenleft = sp->prevnleft; |
| 5910 | /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/ |
| 5911 | parsefile->strpush = sp->prev; |
| 5912 | if (sp != &(parsefile->basestrpush)) |
| 5913 | ckfree(sp); |
| 5914 | INTON; |
| 5915 | } |
| 5916 | |
| 5917 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5918 | /* |
| 5919 | * Refill the input buffer and return the next input character: |
| 5920 | * |
| 5921 | * 1) If a string was pushed back on the input, pop it; |
| 5922 | * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading |
| 5923 | * from a string so we can't refill the buffer, return EOF. |
| 5924 | * 3) If the is more stuff in this buffer, use it else call read to fill it. |
| 5925 | * 4) Process input up to the next newline, deleting nul characters. |
| 5926 | */ |
| 5927 | |
| 5928 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5929 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5930 | { |
| 5931 | char *p, *q; |
| 5932 | int more; |
| 5933 | char savec; |
| 5934 | |
| 5935 | while (parsefile->strpush) { |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 5936 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5937 | if (parsenleft == -1 && parsefile->strpush->ap && |
| 5938 | parsenextc[-1] != ' ' && parsenextc[-1] != '\t') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5939 | return PEOA; |
| 5940 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5941 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5942 | popstring(); |
| 5943 | if (--parsenleft >= 0) |
| 5944 | return (*parsenextc++); |
| 5945 | } |
| 5946 | if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) |
| 5947 | return PEOF; |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 5948 | flushall(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5949 | |
| 5950 | again: |
| 5951 | if (parselleft <= 0) { |
| 5952 | if ((parselleft = preadfd()) <= 0) { |
| 5953 | parselleft = parsenleft = EOF_NLEFT; |
| 5954 | return PEOF; |
| 5955 | } |
| 5956 | } |
| 5957 | |
| 5958 | q = p = parsenextc; |
| 5959 | |
| 5960 | /* delete nul characters */ |
| 5961 | for (more = 1; more;) { |
| 5962 | switch (*p) { |
| 5963 | case '\0': |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5964 | p++; /* Skip nul */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5965 | goto check; |
| 5966 | |
| 5967 | |
| 5968 | case '\n': |
| 5969 | parsenleft = q - parsenextc; |
| 5970 | more = 0; /* Stop processing here */ |
| 5971 | break; |
| 5972 | } |
| 5973 | |
| 5974 | *q++ = *p++; |
| 5975 | check: |
| 5976 | if (--parselleft <= 0 && more) { |
| 5977 | parsenleft = q - parsenextc - 1; |
| 5978 | if (parsenleft < 0) |
| 5979 | goto again; |
| 5980 | more = 0; |
| 5981 | } |
| 5982 | } |
| 5983 | |
| 5984 | savec = *q; |
| 5985 | *q = '\0'; |
| 5986 | |
| 5987 | if (vflag) { |
| 5988 | out2str(parsenextc); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5989 | } |
| 5990 | |
| 5991 | *q = savec; |
| 5992 | |
| 5993 | return *parsenextc++; |
| 5994 | } |
| 5995 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5996 | |
| 5997 | /* |
| 5998 | * Push a string back onto the input at this current parsefile level. |
| 5999 | * We handle aliases this way. |
| 6000 | */ |
| 6001 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6002 | pushstring(char *s, int len, void *ap) |
| 6003 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6004 | struct strpush *sp; |
| 6005 | |
| 6006 | INTOFF; |
| 6007 | /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/ |
| 6008 | if (parsefile->strpush) { |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6009 | sp = xmalloc(sizeof (struct strpush)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6010 | sp->prev = parsefile->strpush; |
| 6011 | parsefile->strpush = sp; |
| 6012 | } else |
| 6013 | sp = parsefile->strpush = &(parsefile->basestrpush); |
| 6014 | sp->prevstring = parsenextc; |
| 6015 | sp->prevnleft = parsenleft; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6016 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6017 | sp->ap = (struct alias *)ap; |
| 6018 | if (ap) { |
| 6019 | ((struct alias *)ap)->flag |= ALIASINUSE; |
| 6020 | sp->string = s; |
| 6021 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6022 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6023 | parsenextc = s; |
| 6024 | parsenleft = len; |
| 6025 | INTON; |
| 6026 | } |
| 6027 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6028 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6029 | /* |
| 6030 | * Like setinputfile, but takes input from a string. |
| 6031 | */ |
| 6032 | |
| 6033 | static void |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6034 | setinputstring(char *string) |
| 6035 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6036 | INTOFF; |
| 6037 | pushfile(); |
| 6038 | parsenextc = string; |
| 6039 | parsenleft = strlen(string); |
| 6040 | parsefile->buf = NULL; |
| 6041 | plinno = 1; |
| 6042 | INTON; |
| 6043 | } |
| 6044 | |
| 6045 | |
| 6046 | |
| 6047 | /* |
| 6048 | * To handle the "." command, a stack of input files is used. Pushfile |
| 6049 | * adds a new entry to the stack and popfile restores the previous level. |
| 6050 | */ |
| 6051 | |
| 6052 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6053 | pushfile(void) |
| 6054 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6055 | struct parsefile *pf; |
| 6056 | |
| 6057 | parsefile->nleft = parsenleft; |
| 6058 | parsefile->lleft = parselleft; |
| 6059 | parsefile->nextc = parsenextc; |
| 6060 | parsefile->linno = plinno; |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6061 | pf = (struct parsefile *)xmalloc(sizeof (struct parsefile)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6062 | pf->prev = parsefile; |
| 6063 | pf->fd = -1; |
| 6064 | pf->strpush = NULL; |
| 6065 | pf->basestrpush.prev = NULL; |
| 6066 | parsefile = pf; |
| 6067 | } |
| 6068 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6069 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6070 | static void restartjob (struct job *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6071 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6072 | static void freejob (struct job *); |
| 6073 | static struct job *getjob (const char *); |
| 6074 | static int dowait (int, struct job *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6075 | static void waitonint(int); |
| 6076 | |
| 6077 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6078 | /* |
| 6079 | * We keep track of whether or not fd0 has been redirected. This is for |
| 6080 | * background commands, where we want to redirect fd0 to /dev/null only |
| 6081 | * if it hasn't already been redirected. |
| 6082 | */ |
| 6083 | static int fd0_redirected = 0; |
| 6084 | |
| 6085 | /* Return true if fd 0 has already been redirected at least once. */ |
| 6086 | static inline int |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 6087 | fd0_redirected_p (void) |
| 6088 | { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6089 | return fd0_redirected != 0; |
| 6090 | } |
| 6091 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6092 | static void dupredirect (const union node *, int, int fd1dup); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6093 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6094 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6095 | /* |
| 6096 | * Turn job control on and off. |
| 6097 | * |
| 6098 | * Note: This code assumes that the third arg to ioctl is a character |
| 6099 | * pointer, which is true on Berkeley systems but not System V. Since |
| 6100 | * System V doesn't have job control yet, this isn't a problem now. |
| 6101 | */ |
| 6102 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6103 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6104 | |
| 6105 | static void setjobctl(int enable) |
| 6106 | { |
| 6107 | #ifdef OLD_TTY_DRIVER |
| 6108 | int ldisc; |
| 6109 | #endif |
| 6110 | |
| 6111 | if (enable == jobctl || rootshell == 0) |
| 6112 | return; |
| 6113 | if (enable) { |
| 6114 | do { /* while we are in the background */ |
| 6115 | #ifdef OLD_TTY_DRIVER |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6116 | if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6117 | #else |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6118 | initialpgrp = tcgetpgrp(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6119 | if (initialpgrp < 0) { |
| 6120 | #endif |
Eric Andersen | 8c145dc | 2001-07-10 16:57:09 +0000 | [diff] [blame] | 6121 | out2str("sh: can't access tty; job control turned off\n"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6122 | mflag = 0; |
| 6123 | return; |
| 6124 | } |
| 6125 | if (initialpgrp == -1) |
| 6126 | initialpgrp = getpgrp(); |
| 6127 | else if (initialpgrp != getpgrp()) { |
| 6128 | killpg(initialpgrp, SIGTTIN); |
| 6129 | continue; |
| 6130 | } |
| 6131 | } while (0); |
| 6132 | #ifdef OLD_TTY_DRIVER |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6133 | if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) { |
Eric Andersen | 8c145dc | 2001-07-10 16:57:09 +0000 | [diff] [blame] | 6134 | out2str("sh: need new tty driver to run job control; job control turned off\n"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6135 | mflag = 0; |
| 6136 | return; |
| 6137 | } |
| 6138 | #endif |
| 6139 | setsignal(SIGTSTP); |
| 6140 | setsignal(SIGTTOU); |
| 6141 | setsignal(SIGTTIN); |
| 6142 | setpgid(0, rootpid); |
| 6143 | #ifdef OLD_TTY_DRIVER |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6144 | ioctl(2, TIOCSPGRP, (char *)&rootpid); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6145 | #else |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6146 | tcsetpgrp(2, rootpid); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6147 | #endif |
Eric Andersen | 8c145dc | 2001-07-10 16:57:09 +0000 | [diff] [blame] | 6148 | } else { /* turning job control off */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6149 | setpgid(0, initialpgrp); |
| 6150 | #ifdef OLD_TTY_DRIVER |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6151 | ioctl(2, TIOCSPGRP, (char *)&initialpgrp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6152 | #else |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6153 | tcsetpgrp(2, initialpgrp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6154 | #endif |
| 6155 | setsignal(SIGTSTP); |
| 6156 | setsignal(SIGTTOU); |
| 6157 | setsignal(SIGTTIN); |
| 6158 | } |
| 6159 | jobctl = enable; |
| 6160 | } |
| 6161 | #endif |
| 6162 | |
| 6163 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6164 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6165 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6166 | killcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6167 | { |
| 6168 | int signo = -1; |
| 6169 | int list = 0; |
| 6170 | int i; |
| 6171 | pid_t pid; |
| 6172 | struct job *jp; |
| 6173 | |
| 6174 | if (argc <= 1) { |
| 6175 | usage: |
| 6176 | error( |
| 6177 | "Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or\n" |
| 6178 | "kill -l [exitstatus]" |
| 6179 | ); |
| 6180 | } |
| 6181 | |
| 6182 | if (*argv[1] == '-') { |
| 6183 | signo = decode_signal(argv[1] + 1, 1); |
| 6184 | if (signo < 0) { |
| 6185 | int c; |
| 6186 | |
| 6187 | while ((c = nextopt("ls:")) != '\0') |
| 6188 | switch (c) { |
| 6189 | case 'l': |
| 6190 | list = 1; |
| 6191 | break; |
| 6192 | case 's': |
| 6193 | signo = decode_signal(optionarg, 1); |
| 6194 | if (signo < 0) { |
| 6195 | error( |
| 6196 | "invalid signal number or name: %s", |
| 6197 | optionarg |
| 6198 | ); |
| 6199 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6200 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6201 | #ifdef DEBUG |
| 6202 | default: |
| 6203 | error( |
| 6204 | "nextopt returned character code 0%o", c); |
| 6205 | #endif |
| 6206 | } |
| 6207 | } else |
| 6208 | argptr++; |
| 6209 | } |
| 6210 | |
| 6211 | if (!list && signo < 0) |
| 6212 | signo = SIGTERM; |
| 6213 | |
| 6214 | if ((signo < 0 || !*argptr) ^ list) { |
| 6215 | goto usage; |
| 6216 | } |
| 6217 | |
| 6218 | if (list) { |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 6219 | const char *name; |
| 6220 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6221 | if (!*argptr) { |
| 6222 | out1str("0\n"); |
| 6223 | for (i = 1; i < NSIG; i++) { |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 6224 | name = u_signal_names(0, &i, 1); |
| 6225 | if(name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6226 | puts(name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6227 | } |
| 6228 | return 0; |
| 6229 | } |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 6230 | name = u_signal_names(*argptr, &signo, -1); |
| 6231 | if (name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6232 | puts(name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6233 | else |
| 6234 | error("invalid signal number or exit status: %s", |
| 6235 | *argptr); |
| 6236 | return 0; |
| 6237 | } |
| 6238 | |
| 6239 | do { |
| 6240 | if (**argptr == '%') { |
| 6241 | jp = getjob(*argptr); |
| 6242 | if (jp->jobctl == 0) |
| 6243 | error("job %s not created under job control", |
| 6244 | *argptr); |
| 6245 | pid = -jp->ps[0].pid; |
| 6246 | } else |
| 6247 | pid = atoi(*argptr); |
| 6248 | if (kill(pid, signo) != 0) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6249 | error("%s: %m", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6250 | } while (*++argptr); |
| 6251 | |
| 6252 | return 0; |
| 6253 | } |
| 6254 | |
| 6255 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6256 | fgcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6257 | { |
| 6258 | struct job *jp; |
| 6259 | int pgrp; |
| 6260 | int status; |
| 6261 | |
| 6262 | jp = getjob(argv[1]); |
| 6263 | if (jp->jobctl == 0) |
| 6264 | error("job not created under job control"); |
| 6265 | pgrp = jp->ps[0].pid; |
| 6266 | #ifdef OLD_TTY_DRIVER |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6267 | ioctl(2, TIOCSPGRP, (char *)&pgrp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6268 | #else |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6269 | tcsetpgrp(2, pgrp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6270 | #endif |
| 6271 | restartjob(jp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6272 | status = waitforjob(jp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6273 | return status; |
| 6274 | } |
| 6275 | |
| 6276 | |
| 6277 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6278 | bgcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6279 | { |
| 6280 | struct job *jp; |
| 6281 | |
| 6282 | do { |
| 6283 | jp = getjob(*++argv); |
| 6284 | if (jp->jobctl == 0) |
| 6285 | error("job not created under job control"); |
| 6286 | restartjob(jp); |
| 6287 | } while (--argc > 1); |
| 6288 | return 0; |
| 6289 | } |
| 6290 | |
| 6291 | |
| 6292 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6293 | restartjob(struct job *jp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6294 | { |
| 6295 | struct procstat *ps; |
| 6296 | int i; |
| 6297 | |
| 6298 | if (jp->state == JOBDONE) |
| 6299 | return; |
| 6300 | INTOFF; |
| 6301 | killpg(jp->ps[0].pid, SIGCONT); |
| 6302 | for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { |
| 6303 | if (WIFSTOPPED(ps->status)) { |
| 6304 | ps->status = -1; |
| 6305 | jp->state = 0; |
| 6306 | } |
| 6307 | } |
| 6308 | INTON; |
| 6309 | } |
| 6310 | #endif |
| 6311 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6312 | static void showjobs(int change); |
| 6313 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6314 | |
| 6315 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6316 | jobscmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6317 | { |
| 6318 | showjobs(0); |
| 6319 | return 0; |
| 6320 | } |
| 6321 | |
| 6322 | |
| 6323 | /* |
| 6324 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 6325 | * statuses have changed since the last call to showjobs. |
| 6326 | * |
| 6327 | * If the shell is interrupted in the process of creating a job, the |
| 6328 | * result may be a job structure containing zero processes. Such structures |
| 6329 | * will be freed here. |
| 6330 | */ |
| 6331 | |
| 6332 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6333 | showjobs(int change) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6334 | { |
| 6335 | int jobno; |
| 6336 | int procno; |
| 6337 | int i; |
| 6338 | struct job *jp; |
| 6339 | struct procstat *ps; |
| 6340 | int col; |
| 6341 | char s[64]; |
| 6342 | |
| 6343 | TRACE(("showjobs(%d) called\n", change)); |
| 6344 | while (dowait(0, (struct job *)NULL) > 0); |
| 6345 | for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { |
| 6346 | if (! jp->used) |
| 6347 | continue; |
| 6348 | if (jp->nprocs == 0) { |
| 6349 | freejob(jp); |
| 6350 | continue; |
| 6351 | } |
| 6352 | if (change && ! jp->changed) |
| 6353 | continue; |
| 6354 | procno = jp->nprocs; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6355 | for (ps = jp->ps ; ; ps++) { /* for each process */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6356 | if (ps == jp->ps) |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6357 | snprintf(s, 64, "[%d] %ld ", jobno, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6358 | (long)ps->pid); |
| 6359 | else |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6360 | snprintf(s, 64, " %ld ", |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6361 | (long)ps->pid); |
| 6362 | out1str(s); |
| 6363 | col = strlen(s); |
| 6364 | s[0] = '\0'; |
| 6365 | if (ps->status == -1) { |
| 6366 | /* don't print anything */ |
| 6367 | } else if (WIFEXITED(ps->status)) { |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6368 | snprintf(s, 64, "Exit %d", |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6369 | WEXITSTATUS(ps->status)); |
| 6370 | } else { |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6371 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6372 | if (WIFSTOPPED(ps->status)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6373 | i = WSTOPSIG(ps->status); |
| 6374 | else /* WIFSIGNALED(ps->status) */ |
| 6375 | #endif |
| 6376 | i = WTERMSIG(ps->status); |
| 6377 | if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F]) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6378 | strcpy(s, sys_siglist[i & 0x7F]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6379 | else |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6380 | snprintf(s, 64, "Signal %d", i & 0x7F); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6381 | if (WCOREDUMP(ps->status)) |
| 6382 | strcat(s, " (core dumped)"); |
| 6383 | } |
| 6384 | out1str(s); |
| 6385 | col += strlen(s); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6386 | printf( |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6387 | "%*c%s\n", 30 - col >= 0 ? 30 - col : 0, ' ', |
| 6388 | ps->cmd |
| 6389 | ); |
| 6390 | if (--procno <= 0) |
| 6391 | break; |
| 6392 | } |
| 6393 | jp->changed = 0; |
| 6394 | if (jp->state == JOBDONE) { |
| 6395 | freejob(jp); |
| 6396 | } |
| 6397 | } |
| 6398 | } |
| 6399 | |
| 6400 | |
| 6401 | /* |
| 6402 | * Mark a job structure as unused. |
| 6403 | */ |
| 6404 | |
| 6405 | static void |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6406 | freejob(struct job *jp) |
| 6407 | { |
| 6408 | const struct procstat *ps; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6409 | int i; |
| 6410 | |
| 6411 | INTOFF; |
| 6412 | for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { |
| 6413 | if (ps->cmd != nullstr) |
| 6414 | ckfree(ps->cmd); |
| 6415 | } |
| 6416 | if (jp->ps != &jp->ps0) |
| 6417 | ckfree(jp->ps); |
| 6418 | jp->used = 0; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6419 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6420 | if (curjob == jp - jobtab + 1) |
| 6421 | curjob = 0; |
| 6422 | #endif |
| 6423 | INTON; |
| 6424 | } |
| 6425 | |
| 6426 | |
| 6427 | |
| 6428 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6429 | waitcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6430 | { |
| 6431 | struct job *job; |
| 6432 | int status, retval; |
| 6433 | struct job *jp; |
| 6434 | |
| 6435 | if (--argc > 0) { |
| 6436 | start: |
| 6437 | job = getjob(*++argv); |
| 6438 | } else { |
| 6439 | job = NULL; |
| 6440 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6441 | for (;;) { /* loop until process terminated or stopped */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6442 | if (job != NULL) { |
| 6443 | if (job->state) { |
| 6444 | status = job->ps[job->nprocs - 1].status; |
| 6445 | if (! iflag) |
| 6446 | freejob(job); |
| 6447 | if (--argc) { |
| 6448 | goto start; |
| 6449 | } |
| 6450 | if (WIFEXITED(status)) |
| 6451 | retval = WEXITSTATUS(status); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6452 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6453 | else if (WIFSTOPPED(status)) |
| 6454 | retval = WSTOPSIG(status) + 128; |
| 6455 | #endif |
| 6456 | else { |
| 6457 | /* XXX: limits number of signals */ |
| 6458 | retval = WTERMSIG(status) + 128; |
| 6459 | } |
| 6460 | return retval; |
| 6461 | } |
| 6462 | } else { |
| 6463 | for (jp = jobtab ; ; jp++) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6464 | if (jp >= jobtab + njobs) { /* no running procs */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6465 | return 0; |
| 6466 | } |
| 6467 | if (jp->used && jp->state == 0) |
| 6468 | break; |
| 6469 | } |
| 6470 | } |
| 6471 | if (dowait(2, 0) < 0 && errno == EINTR) { |
| 6472 | return 129; |
| 6473 | } |
| 6474 | } |
| 6475 | } |
| 6476 | |
| 6477 | |
| 6478 | |
| 6479 | /* |
| 6480 | * Convert a job name to a job structure. |
| 6481 | */ |
| 6482 | |
| 6483 | static struct job * |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6484 | getjob(const char *name) |
| 6485 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6486 | int jobno; |
| 6487 | struct job *jp; |
| 6488 | int pid; |
| 6489 | int i; |
| 6490 | |
| 6491 | if (name == NULL) { |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6492 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6493 | currentjob: |
| 6494 | if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0) |
| 6495 | error("No current job"); |
| 6496 | return &jobtab[jobno - 1]; |
| 6497 | #else |
| 6498 | error("No current job"); |
| 6499 | #endif |
| 6500 | } else if (name[0] == '%') { |
| 6501 | if (is_digit(name[1])) { |
| 6502 | jobno = number(name + 1); |
| 6503 | if (jobno > 0 && jobno <= njobs |
| 6504 | && jobtab[jobno - 1].used != 0) |
| 6505 | return &jobtab[jobno - 1]; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6506 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6507 | } else if (name[1] == '%' && name[2] == '\0') { |
| 6508 | goto currentjob; |
| 6509 | #endif |
| 6510 | } else { |
| 6511 | struct job *found = NULL; |
| 6512 | for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { |
| 6513 | if (jp->used && jp->nprocs > 0 |
| 6514 | && prefix(name + 1, jp->ps[0].cmd)) { |
| 6515 | if (found) |
| 6516 | error("%s: ambiguous", name); |
| 6517 | found = jp; |
| 6518 | } |
| 6519 | } |
| 6520 | if (found) |
| 6521 | return found; |
| 6522 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6523 | } else if (is_number(name, &pid)) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6524 | for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { |
| 6525 | if (jp->used && jp->nprocs > 0 |
| 6526 | && jp->ps[jp->nprocs - 1].pid == pid) |
| 6527 | return jp; |
| 6528 | } |
| 6529 | } |
| 6530 | error("No such job: %s", name); |
| 6531 | /* NOTREACHED */ |
| 6532 | } |
| 6533 | |
| 6534 | |
| 6535 | |
| 6536 | /* |
| 6537 | * Return a new job structure, |
| 6538 | */ |
| 6539 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6540 | static struct job * |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6541 | makejob(const union node *node, int nprocs) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6542 | { |
| 6543 | int i; |
| 6544 | struct job *jp; |
| 6545 | |
| 6546 | for (i = njobs, jp = jobtab ; ; jp++) { |
| 6547 | if (--i < 0) { |
| 6548 | INTOFF; |
| 6549 | if (njobs == 0) { |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6550 | jobtab = xmalloc(4 * sizeof jobtab[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6551 | } else { |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6552 | jp = xmalloc((njobs + 4) * sizeof jobtab[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6553 | memcpy(jp, jobtab, njobs * sizeof jp[0]); |
| 6554 | /* Relocate `ps' pointers */ |
| 6555 | for (i = 0; i < njobs; i++) |
| 6556 | if (jp[i].ps == &jobtab[i].ps0) |
| 6557 | jp[i].ps = &jp[i].ps0; |
| 6558 | ckfree(jobtab); |
| 6559 | jobtab = jp; |
| 6560 | } |
| 6561 | jp = jobtab + njobs; |
| 6562 | for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); |
| 6563 | INTON; |
| 6564 | break; |
| 6565 | } |
| 6566 | if (jp->used == 0) |
| 6567 | break; |
| 6568 | } |
| 6569 | INTOFF; |
| 6570 | jp->state = 0; |
| 6571 | jp->used = 1; |
| 6572 | jp->changed = 0; |
| 6573 | jp->nprocs = 0; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6574 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6575 | jp->jobctl = jobctl; |
| 6576 | #endif |
| 6577 | if (nprocs > 1) { |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6578 | jp->ps = xmalloc(nprocs * sizeof (struct procstat)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6579 | } else { |
| 6580 | jp->ps = &jp->ps0; |
| 6581 | } |
| 6582 | INTON; |
| 6583 | TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs, |
| 6584 | jp - jobtab + 1)); |
| 6585 | return jp; |
| 6586 | } |
| 6587 | |
| 6588 | |
| 6589 | /* |
| 6590 | * Fork of a subshell. If we are doing job control, give the subshell its |
| 6591 | * own process group. Jp is a job structure that the job is to be added to. |
| 6592 | * N is the command that will be evaluated by the child. Both jp and n may |
| 6593 | * be NULL. The mode parameter can be one of the following: |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6594 | * FORK_FG - Fork off a foreground process. |
| 6595 | * FORK_BG - Fork off a background process. |
| 6596 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 6597 | * process group even if job control is on. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6598 | * |
| 6599 | * When job control is turned off, background processes have their standard |
| 6600 | * input redirected to /dev/null (except for the second and later processes |
| 6601 | * in a pipeline). |
| 6602 | */ |
| 6603 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6604 | |
| 6605 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6606 | static int |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6607 | forkshell(struct job *jp, const union node *n, int mode) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6608 | { |
| 6609 | int pid; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6610 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6611 | int pgrp; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6612 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6613 | const char *devnull = _PATH_DEVNULL; |
| 6614 | const char *nullerr = "Can't open %s"; |
| 6615 | |
| 6616 | TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n, |
| 6617 | mode)); |
| 6618 | INTOFF; |
Eric Andersen | 72f9a42 | 2001-10-28 05:12:20 +0000 | [diff] [blame] | 6619 | #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6620 | pid = fork(); |
Eric Andersen | 72f9a42 | 2001-10-28 05:12:20 +0000 | [diff] [blame] | 6621 | #else |
| 6622 | pid = vfork(); |
| 6623 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6624 | if (pid == -1) { |
| 6625 | TRACE(("Fork failed, errno=%d\n", errno)); |
| 6626 | INTON; |
| 6627 | error("Cannot fork"); |
| 6628 | } |
| 6629 | if (pid == 0) { |
| 6630 | struct job *p; |
| 6631 | int wasroot; |
| 6632 | int i; |
| 6633 | |
| 6634 | TRACE(("Child shell %d\n", getpid())); |
| 6635 | wasroot = rootshell; |
| 6636 | rootshell = 0; |
| 6637 | closescript(); |
| 6638 | INTON; |
| 6639 | clear_traps(); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6640 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6641 | jobctl = 0; /* do job control only in root shell */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6642 | if (wasroot && mode != FORK_NOJOB && mflag) { |
| 6643 | if (jp == NULL || jp->nprocs == 0) |
| 6644 | pgrp = getpid(); |
| 6645 | else |
| 6646 | pgrp = jp->ps[0].pid; |
| 6647 | setpgid(0, pgrp); |
| 6648 | if (mode == FORK_FG) { |
| 6649 | /*** this causes superfluous TIOCSPGRPS ***/ |
| 6650 | #ifdef OLD_TTY_DRIVER |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6651 | if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6652 | error("TIOCSPGRP failed, errno=%d", errno); |
| 6653 | #else |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6654 | if (tcsetpgrp(2, pgrp) < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6655 | error("tcsetpgrp failed, errno=%d", errno); |
| 6656 | #endif |
| 6657 | } |
| 6658 | setsignal(SIGTSTP); |
| 6659 | setsignal(SIGTTOU); |
| 6660 | } else if (mode == FORK_BG) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6661 | #else |
| 6662 | if (mode == FORK_BG) { |
Aaron Lehmann | 1a69866 | 2001-12-31 06:12:48 +0000 | [diff] [blame] | 6663 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6664 | ignoresig(SIGINT); |
| 6665 | ignoresig(SIGQUIT); |
| 6666 | if ((jp == NULL || jp->nprocs == 0) && |
| 6667 | ! fd0_redirected_p ()) { |
| 6668 | close(0); |
| 6669 | if (open(devnull, O_RDONLY) != 0) |
| 6670 | error(nullerr, devnull); |
| 6671 | } |
| 6672 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6673 | for (i = njobs, p = jobtab ; --i >= 0 ; p++) |
| 6674 | if (p->used) |
| 6675 | freejob(p); |
| 6676 | if (wasroot && iflag) { |
| 6677 | setsignal(SIGINT); |
| 6678 | setsignal(SIGQUIT); |
| 6679 | setsignal(SIGTERM); |
| 6680 | } |
| 6681 | return pid; |
| 6682 | } |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6683 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6684 | if (rootshell && mode != FORK_NOJOB && mflag) { |
| 6685 | if (jp == NULL || jp->nprocs == 0) |
| 6686 | pgrp = pid; |
| 6687 | else |
| 6688 | pgrp = jp->ps[0].pid; |
| 6689 | setpgid(pid, pgrp); |
| 6690 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6691 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6692 | if (mode == FORK_BG) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6693 | backgndpid = pid; /* set $! */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6694 | if (jp) { |
| 6695 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
| 6696 | ps->pid = pid; |
| 6697 | ps->status = -1; |
| 6698 | ps->cmd = nullstr; |
| 6699 | if (iflag && rootshell && n) |
| 6700 | ps->cmd = commandtext(n); |
| 6701 | } |
| 6702 | INTON; |
| 6703 | TRACE(("In parent shell: child = %d\n", pid)); |
| 6704 | return pid; |
| 6705 | } |
| 6706 | |
| 6707 | |
| 6708 | |
| 6709 | /* |
| 6710 | * Wait for job to finish. |
| 6711 | * |
| 6712 | * Under job control we have the problem that while a child process is |
| 6713 | * running interrupts generated by the user are sent to the child but not |
| 6714 | * to the shell. This means that an infinite loop started by an inter- |
| 6715 | * active user may be hard to kill. With job control turned off, an |
| 6716 | * interactive user may place an interactive program inside a loop. If |
| 6717 | * the interactive program catches interrupts, the user doesn't want |
| 6718 | * these interrupts to also abort the loop. The approach we take here |
| 6719 | * is to have the shell ignore interrupt signals while waiting for a |
| 6720 | * forground process to terminate, and then send itself an interrupt |
| 6721 | * signal if the child process was terminated by an interrupt signal. |
| 6722 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 6723 | * exit on interrupt; unless these processes terminate themselves by |
| 6724 | * sending a signal to themselves (instead of calling exit) they will |
| 6725 | * confuse this approach. |
| 6726 | */ |
| 6727 | |
| 6728 | static int |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6729 | waitforjob(struct job *jp) |
| 6730 | { |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6731 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6732 | int mypgrp = getpgrp(); |
| 6733 | #endif |
| 6734 | int status; |
| 6735 | int st; |
| 6736 | struct sigaction act, oact; |
| 6737 | |
| 6738 | INTOFF; |
| 6739 | intreceived = 0; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6740 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6741 | if (!jobctl) { |
| 6742 | #else |
| 6743 | if (!iflag) { |
| 6744 | #endif |
| 6745 | sigaction(SIGINT, 0, &act); |
| 6746 | act.sa_handler = waitonint; |
| 6747 | sigaction(SIGINT, &act, &oact); |
| 6748 | } |
| 6749 | TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); |
| 6750 | while (jp->state == 0) { |
| 6751 | dowait(1, jp); |
| 6752 | } |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6753 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6754 | if (!jobctl) { |
| 6755 | #else |
| 6756 | if (!iflag) { |
| 6757 | #endif |
| 6758 | sigaction(SIGINT, &oact, 0); |
Eric Andersen | 54c14d7 | 2002-04-24 23:14:06 +0000 | [diff] [blame] | 6759 | if (intreceived) kill(getpid(), SIGINT); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6760 | } |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6761 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6762 | if (jp->jobctl) { |
| 6763 | #ifdef OLD_TTY_DRIVER |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6764 | if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6765 | error("TIOCSPGRP failed, errno=%d\n", errno); |
| 6766 | #else |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6767 | if (tcsetpgrp(2, mypgrp) < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6768 | error("tcsetpgrp failed, errno=%d\n", errno); |
| 6769 | #endif |
| 6770 | } |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6771 | if (jp->state == CONFIG_ASH_JOB_CONTROLTOPPED) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6772 | curjob = jp - jobtab + 1; |
| 6773 | #endif |
| 6774 | status = jp->ps[jp->nprocs - 1].status; |
| 6775 | /* convert to 8 bits */ |
| 6776 | if (WIFEXITED(status)) |
| 6777 | st = WEXITSTATUS(status); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6778 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6779 | else if (WIFSTOPPED(status)) |
| 6780 | st = WSTOPSIG(status) + 128; |
| 6781 | #endif |
| 6782 | else |
| 6783 | st = WTERMSIG(status) + 128; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6784 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6785 | if (jp->jobctl) { |
| 6786 | /* |
| 6787 | * This is truly gross. |
| 6788 | * If we're doing job control, then we did a TIOCSPGRP which |
| 6789 | * caused us (the shell) to no longer be in the controlling |
| 6790 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 6791 | * intuit from the subprocess exit status whether a SIGINT |
| 6792 | * occured, and if so interrupt ourselves. Yuck. - mycroft |
| 6793 | */ |
| 6794 | if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) |
| 6795 | raise(SIGINT); |
| 6796 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6797 | if (jp->state == JOBDONE) |
| 6798 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6799 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6800 | freejob(jp); |
| 6801 | INTON; |
| 6802 | return st; |
| 6803 | } |
| 6804 | |
| 6805 | |
| 6806 | |
| 6807 | /* |
| 6808 | * Wait for a process to terminate. |
| 6809 | */ |
| 6810 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6811 | /* |
| 6812 | * Do a wait system call. If job control is compiled in, we accept |
| 6813 | * stopped processes. If block is zero, we return a value of zero |
| 6814 | * rather than blocking. |
| 6815 | * |
| 6816 | * System V doesn't have a non-blocking wait system call. It does |
| 6817 | * have a SIGCLD signal that is sent to a process when one of it's |
| 6818 | * children dies. The obvious way to use SIGCLD would be to install |
| 6819 | * a handler for SIGCLD which simply bumped a counter when a SIGCLD |
| 6820 | * was received, and have waitproc bump another counter when it got |
| 6821 | * the status of a process. Waitproc would then know that a wait |
| 6822 | * system call would not block if the two counters were different. |
| 6823 | * This approach doesn't work because if a process has children that |
| 6824 | * have not been waited for, System V will send it a SIGCLD when it |
| 6825 | * installs a signal handler for SIGCLD. What this means is that when |
| 6826 | * a child exits, the shell will be sent SIGCLD signals continuously |
| 6827 | * until is runs out of stack space, unless it does a wait call before |
| 6828 | * restoring the signal handler. The code below takes advantage of |
| 6829 | * this (mis)feature by installing a signal handler for SIGCLD and |
| 6830 | * then checking to see whether it was called. If there are any |
| 6831 | * children to be waited for, it will be. |
| 6832 | * |
| 6833 | */ |
| 6834 | |
| 6835 | static inline int |
| 6836 | waitproc(int block, int *status) |
| 6837 | { |
| 6838 | int flags; |
| 6839 | |
| 6840 | flags = 0; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6841 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6842 | if (jobctl) |
| 6843 | flags |= WUNTRACED; |
| 6844 | #endif |
| 6845 | if (block == 0) |
| 6846 | flags |= WNOHANG; |
| 6847 | return wait3(status, flags, (struct rusage *)NULL); |
| 6848 | } |
| 6849 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6850 | static int |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6851 | dowait(int block, struct job *job) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6852 | { |
| 6853 | int pid; |
| 6854 | int status; |
| 6855 | struct procstat *sp; |
| 6856 | struct job *jp; |
| 6857 | struct job *thisjob; |
| 6858 | int done; |
| 6859 | int stopped; |
| 6860 | int core; |
| 6861 | int sig; |
| 6862 | |
| 6863 | TRACE(("dowait(%d) called\n", block)); |
| 6864 | do { |
| 6865 | pid = waitproc(block, &status); |
| 6866 | TRACE(("wait returns %d, status=%d\n", pid, status)); |
| 6867 | } while (!(block & 2) && pid == -1 && errno == EINTR); |
| 6868 | if (pid <= 0) |
| 6869 | return pid; |
| 6870 | INTOFF; |
| 6871 | thisjob = NULL; |
| 6872 | for (jp = jobtab ; jp < jobtab + njobs ; jp++) { |
| 6873 | if (jp->used) { |
| 6874 | done = 1; |
| 6875 | stopped = 1; |
| 6876 | for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { |
| 6877 | if (sp->pid == -1) |
| 6878 | continue; |
| 6879 | if (sp->pid == pid) { |
| 6880 | TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", pid, sp->status, status)); |
| 6881 | sp->status = status; |
| 6882 | thisjob = jp; |
| 6883 | } |
| 6884 | if (sp->status == -1) |
| 6885 | stopped = 0; |
| 6886 | else if (WIFSTOPPED(sp->status)) |
| 6887 | done = 0; |
| 6888 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6889 | if (stopped) { /* stopped or done */ |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6890 | int state = done? JOBDONE : CONFIG_ASH_JOB_CONTROLTOPPED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6891 | if (jp->state != state) { |
| 6892 | TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); |
| 6893 | jp->state = state; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6894 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6895 | if (done && curjob == jp - jobtab + 1) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6896 | curjob = 0; /* no current job */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6897 | #endif |
| 6898 | } |
| 6899 | } |
| 6900 | } |
| 6901 | } |
| 6902 | INTON; |
| 6903 | if (! rootshell || ! iflag || (job && thisjob == job)) { |
| 6904 | core = WCOREDUMP(status); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6905 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6906 | if (WIFSTOPPED(status)) sig = WSTOPSIG(status); |
| 6907 | else |
| 6908 | #endif |
| 6909 | if (WIFEXITED(status)) sig = 0; |
| 6910 | else sig = WTERMSIG(status); |
| 6911 | |
| 6912 | if (sig != 0 && sig != SIGINT && sig != SIGPIPE) { |
| 6913 | if (thisjob != job) |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6914 | out2fmt("%d: ", pid); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6915 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6916 | if (sig == SIGTSTP && rootshell && iflag) |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6917 | out2fmt("%%%ld ", |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6918 | (long)(job - jobtab + 1)); |
| 6919 | #endif |
| 6920 | if (sig < NSIG && sys_siglist[sig]) |
| 6921 | out2str(sys_siglist[sig]); |
| 6922 | else |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6923 | out2fmt("Signal %d", sig); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6924 | if (core) |
| 6925 | out2str(" - core dumped"); |
| 6926 | out2c('\n'); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6927 | } else { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6928 | TRACE(("Not printing status: status=%d, sig=%d\n", |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6929 | status, sig)); |
| 6930 | } |
| 6931 | } else { |
| 6932 | TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job)); |
| 6933 | if (thisjob) |
| 6934 | thisjob->changed = 1; |
| 6935 | } |
| 6936 | return pid; |
| 6937 | } |
| 6938 | |
| 6939 | |
| 6940 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6941 | |
| 6942 | /* |
| 6943 | * return 1 if there are stopped jobs, otherwise 0 |
| 6944 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6945 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6946 | stoppedjobs(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6947 | { |
| 6948 | int jobno; |
| 6949 | struct job *jp; |
| 6950 | |
| 6951 | if (job_warning) |
| 6952 | return (0); |
| 6953 | for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { |
| 6954 | if (jp->used == 0) |
| 6955 | continue; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6956 | if (jp->state == CONFIG_ASH_JOB_CONTROLTOPPED) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6957 | out2str("You have stopped jobs.\n"); |
| 6958 | job_warning = 2; |
| 6959 | return (1); |
| 6960 | } |
| 6961 | } |
| 6962 | |
| 6963 | return (0); |
| 6964 | } |
| 6965 | |
| 6966 | /* |
| 6967 | * Return a string identifying a command (to be printed by the |
| 6968 | * jobs command. |
| 6969 | */ |
| 6970 | |
| 6971 | static char *cmdnextc; |
| 6972 | static int cmdnleft; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6973 | #define MAXCMDTEXT 200 |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6974 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6975 | static void |
| 6976 | cmdputs(const char *s) |
| 6977 | { |
| 6978 | const char *p; |
| 6979 | char *q; |
| 6980 | char c; |
| 6981 | int subtype = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6982 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6983 | if (cmdnleft <= 0) |
| 6984 | return; |
| 6985 | p = s; |
| 6986 | q = cmdnextc; |
| 6987 | while ((c = *p++) != '\0') { |
| 6988 | if (c == CTLESC) |
| 6989 | *q++ = *p++; |
| 6990 | else if (c == CTLVAR) { |
| 6991 | *q++ = '$'; |
| 6992 | if (--cmdnleft > 0) |
| 6993 | *q++ = '{'; |
| 6994 | subtype = *p++; |
| 6995 | } else if (c == '=' && subtype != 0) { |
| 6996 | *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; |
| 6997 | subtype = 0; |
| 6998 | } else if (c == CTLENDVAR) { |
| 6999 | *q++ = '}'; |
| 7000 | } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) |
| 7001 | cmdnleft++; /* ignore it */ |
| 7002 | else |
| 7003 | *q++ = c; |
| 7004 | if (--cmdnleft <= 0) { |
| 7005 | *q++ = '.'; |
| 7006 | *q++ = '.'; |
| 7007 | *q++ = '.'; |
| 7008 | break; |
| 7009 | } |
| 7010 | } |
| 7011 | cmdnextc = q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7012 | } |
| 7013 | |
Manuel Novoa III | 8d0afde | 2001-09-11 01:14:02 +0000 | [diff] [blame] | 7014 | #define CMDTXT_TABLE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 7015 | #ifdef CMDTXT_TABLE |
| 7016 | /* |
| 7017 | * To collect a lot of redundant code in cmdtxt() case statements, we |
| 7018 | * implement a mini language here. Each type of node struct has an |
| 7019 | * associated instruction sequence that operates on its members via |
| 7020 | * their offsets. The instruction are pack in unsigned chars with |
| 7021 | * format IIDDDDDE where the bits are |
| 7022 | * I : part of the instruction opcode, which are |
| 7023 | * 00 : member is a pointer to another node -- process it recursively |
| 7024 | * 40 : member is a pointer to a char string -- output it |
| 7025 | * 80 : output the string whose index is stored in the data field |
| 7026 | * CC : flag signaling that this case needs external processing |
| 7027 | * D : data - either the (shifted) index of a fixed string to output or |
| 7028 | * the actual offset of the member to operate on in the struct |
| 7029 | * (since we assume bit 0 is set, the offset is not shifted) |
| 7030 | * E : flag signaling end of instruction sequence |
| 7031 | * |
| 7032 | * WARNING: In order to handle larger offsets for 64bit archs, this code |
| 7033 | * assumes that no offset can be an odd number and stores the |
| 7034 | * end-of-instructions flag in bit 0. |
| 7035 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7036 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 7037 | #define CMDTXT_NOMORE 0x01 /* NOTE: no offset should be odd */ |
| 7038 | #define CMDTXT_CHARPTR 0x40 |
| 7039 | #define CMDTXT_STRING 0x80 |
| 7040 | #define CMDTXT_SPECIAL 0xC0 |
| 7041 | #define CMDTXT_OFFSETMASK 0x3E |
| 7042 | |
| 7043 | static const char * const cmdtxt_strings[] = { |
| 7044 | /* 0 1 2 3 4 5 6 7 */ |
| 7045 | "; ", "(", ")", " && ", " || ", "if ", "; then ", "...", |
| 7046 | /* 8 9 10 11 12 13 */ |
| 7047 | "while ", "; do ", "; done", "until ", "for ", " in ...", |
| 7048 | /* 14 15 16 17 */ |
| 7049 | "case ", "???", "() ...", "<<..." |
| 7050 | }; |
| 7051 | |
| 7052 | static const char * const redir_strings[] = { |
| 7053 | ">", "<", "<>", ">>", ">|", ">&", "<&" |
| 7054 | }; |
| 7055 | |
| 7056 | static const unsigned char cmdtxt_ops[] = { |
| 7057 | #define CMDTXT_NSEMI 0 |
| 7058 | offsetof(union node, nbinary.ch1), |
| 7059 | 0|CMDTXT_STRING, |
| 7060 | offsetof(union node, nbinary.ch2)|CMDTXT_NOMORE, |
| 7061 | #define CMDTXT_NCMD (CMDTXT_NSEMI + 3) |
| 7062 | #define CMDTXT_NPIPE (CMDTXT_NCMD) |
| 7063 | #define CMDTXT_NCASE (CMDTXT_NCMD) |
| 7064 | #define CMDTXT_NTO (CMDTXT_NCMD) |
| 7065 | #define CMDTXT_NFROM (CMDTXT_NCMD) |
| 7066 | #define CMDTXT_NFROMTO (CMDTXT_NCMD) |
| 7067 | #define CMDTXT_NAPPEND (CMDTXT_NCMD) |
| 7068 | #define CMDTXT_NTOOV (CMDTXT_NCMD) |
| 7069 | #define CMDTXT_NTOFD (CMDTXT_NCMD) |
| 7070 | #define CMDTXT_NFROMFD (CMDTXT_NCMD) |
| 7071 | CMDTXT_SPECIAL, |
| 7072 | #define CMDTXT_NREDIR (CMDTXT_NPIPE + 1) |
| 7073 | #define CMDTXT_NBACKGND (CMDTXT_NREDIR) |
| 7074 | offsetof(union node, nredir.n)|CMDTXT_NOMORE, |
| 7075 | #define CMDTXT_NSUBSHELL (CMDTXT_NBACKGND + 1) |
| 7076 | (1*2)|CMDTXT_STRING, |
| 7077 | offsetof(union node, nredir.n), |
| 7078 | (2*2)|CMDTXT_STRING|CMDTXT_NOMORE, |
| 7079 | #define CMDTXT_NAND (CMDTXT_NSUBSHELL + 3) |
| 7080 | offsetof(union node, nbinary.ch1), |
| 7081 | (3*2)|CMDTXT_STRING, |
| 7082 | offsetof(union node, nbinary.ch2)|CMDTXT_NOMORE, |
| 7083 | #define CMDTXT_NOR (CMDTXT_NAND + 3) |
| 7084 | offsetof(union node, nbinary.ch1), |
| 7085 | (4*2)|CMDTXT_STRING, |
| 7086 | offsetof(union node, nbinary.ch2)|CMDTXT_NOMORE, |
| 7087 | #define CMDTXT_NIF (CMDTXT_NOR + 3) |
| 7088 | (5*2)|CMDTXT_STRING, |
| 7089 | offsetof(union node, nif.test), |
| 7090 | (6*2)|CMDTXT_STRING, |
| 7091 | offsetof(union node, nif.ifpart), |
| 7092 | (7*2)|CMDTXT_STRING|CMDTXT_NOMORE, |
| 7093 | #define CMDTXT_NWHILE (CMDTXT_NIF + 5) |
| 7094 | (8*2)|CMDTXT_STRING, |
| 7095 | offsetof(union node, nbinary.ch1), |
| 7096 | (9*2)|CMDTXT_STRING, |
| 7097 | offsetof(union node, nbinary.ch2), |
| 7098 | (10*2)|CMDTXT_STRING|CMDTXT_NOMORE, |
| 7099 | #define CMDTXT_NUNTIL (CMDTXT_NWHILE + 5) |
| 7100 | (11*2)|CMDTXT_STRING, |
| 7101 | offsetof(union node, nbinary.ch1), |
| 7102 | (9*2)|CMDTXT_STRING, |
| 7103 | offsetof(union node, nbinary.ch2), |
| 7104 | (10*2)|CMDTXT_STRING|CMDTXT_NOMORE, |
| 7105 | #define CMDTXT_NFOR (CMDTXT_NUNTIL + 5) |
| 7106 | (12*2)|CMDTXT_STRING, |
| 7107 | offsetof(union node, nfor.var)|CMDTXT_CHARPTR, |
| 7108 | (13*2)|CMDTXT_STRING|CMDTXT_NOMORE, |
| 7109 | #define CMDTXT_NCLIST (CMDTXT_NFOR + 3) /* TODO: IS THIS CORRECT??? */ |
| 7110 | #define CMDTXT_NNOT (CMDTXT_NCLIST) /* TODO: IS THIS CORRECT??? */ |
| 7111 | (15*2)|CMDTXT_STRING|CMDTXT_NOMORE, |
| 7112 | #define CMDTXT_NDEFUN (CMDTXT_NCLIST + 1) |
| 7113 | offsetof(union node, narg.text)|CMDTXT_CHARPTR, |
| 7114 | (16*2)|CMDTXT_STRING|CMDTXT_NOMORE, |
| 7115 | #define CMDTXT_NARG (CMDTXT_NDEFUN + 2) |
| 7116 | offsetof(union node, narg.text)|CMDTXT_CHARPTR|CMDTXT_NOMORE, |
| 7117 | #define CMDTXT_NHERE (CMDTXT_NARG + 1) |
| 7118 | #define CMDTXT_NXHERE (CMDTXT_NHERE) |
| 7119 | (17*2)|CMDTXT_STRING|CMDTXT_NOMORE, |
| 7120 | }; |
| 7121 | |
| 7122 | #if CMDTXT_NXHERE != 36 |
| 7123 | #error CMDTXT_NXHERE |
| 7124 | #endif |
| 7125 | |
| 7126 | static const unsigned char cmdtxt_ops_index[26] = { |
| 7127 | CMDTXT_NSEMI, |
| 7128 | CMDTXT_NCMD, |
| 7129 | CMDTXT_NPIPE, |
| 7130 | CMDTXT_NREDIR, |
| 7131 | CMDTXT_NBACKGND, |
| 7132 | CMDTXT_NSUBSHELL, |
| 7133 | CMDTXT_NAND, |
| 7134 | CMDTXT_NOR, |
| 7135 | CMDTXT_NIF, |
| 7136 | CMDTXT_NWHILE, |
| 7137 | CMDTXT_NUNTIL, |
| 7138 | CMDTXT_NFOR, |
| 7139 | CMDTXT_NCASE, |
| 7140 | CMDTXT_NCLIST, |
| 7141 | CMDTXT_NDEFUN, |
| 7142 | CMDTXT_NARG, |
| 7143 | CMDTXT_NTO, |
| 7144 | CMDTXT_NFROM, |
| 7145 | CMDTXT_NFROMTO, |
| 7146 | CMDTXT_NAPPEND, |
| 7147 | CMDTXT_NTOOV, |
| 7148 | CMDTXT_NTOFD, |
| 7149 | CMDTXT_NFROMFD, |
| 7150 | CMDTXT_NHERE, |
| 7151 | CMDTXT_NXHERE, |
| 7152 | CMDTXT_NNOT, |
| 7153 | }; |
| 7154 | |
| 7155 | static void |
| 7156 | cmdtxt(const union node *n) |
| 7157 | { |
| 7158 | const char *p; |
| 7159 | |
| 7160 | if (n == NULL) |
| 7161 | return; |
| 7162 | |
| 7163 | p = cmdtxt_ops + (int) cmdtxt_ops_index[n->type]; |
| 7164 | if ((*p & CMDTXT_SPECIAL) != CMDTXT_SPECIAL) { /* normal case */ |
| 7165 | do { |
| 7166 | if (*p & CMDTXT_STRING) { /* output fixed string */ |
| 7167 | cmdputs(cmdtxt_strings[((int)(*p & CMDTXT_OFFSETMASK) >> 1)]); |
Manuel Novoa III | c639a35 | 2001-08-12 17:32:56 +0000 | [diff] [blame] | 7168 | } else { |
| 7169 | const char *pf = ((const char *) n) |
| 7170 | + ((int)(*p & CMDTXT_OFFSETMASK)); |
| 7171 | if (*p & CMDTXT_CHARPTR) { /* output dynamic string */ |
| 7172 | cmdputs(*((const char **) pf)); |
| 7173 | } else { /* output field */ |
| 7174 | cmdtxt(*((const union node **) pf)); |
| 7175 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 7176 | } |
| 7177 | } while (!(*p++ & CMDTXT_NOMORE)); |
| 7178 | } else if (n->type == NCMD) { |
| 7179 | union node *np; |
| 7180 | for (np = n->ncmd.args ; np ; np = np->narg.next) { |
| 7181 | cmdtxt(np); |
| 7182 | if (np->narg.next) |
| 7183 | cmdputs(spcstr); |
| 7184 | } |
| 7185 | for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { |
| 7186 | cmdputs(spcstr); |
| 7187 | cmdtxt(np); |
| 7188 | } |
| 7189 | } else if (n->type == NPIPE) { |
| 7190 | struct nodelist *lp; |
| 7191 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { |
| 7192 | cmdtxt(lp->n); |
| 7193 | if (lp->next) |
| 7194 | cmdputs(" | "); |
| 7195 | } |
| 7196 | } else if (n->type == NCASE) { |
| 7197 | cmdputs(cmdtxt_strings[14]); |
| 7198 | cmdputs(n->ncase.expr->narg.text); |
| 7199 | cmdputs(cmdtxt_strings[13]); |
| 7200 | } else { |
| 7201 | #if (NTO != 16) || (NFROM != 17) || (NFROMTO != 18) || (NAPPEND != 19) || (NTOOV != 20) || (NTOFD != 21) || (NFROMFD != 22) |
| 7202 | #error Assumption violated regarding range and ordering of NTO ... NFROMFD! |
| 7203 | #endif |
| 7204 | char s[2]; |
| 7205 | |
| 7206 | #ifdef DEBUG |
| 7207 | assert((n->type >= NTO) && (n->type <= NFROMFD)); |
| 7208 | #endif |
| 7209 | |
| 7210 | p = redir_strings[n->type - NTO]; |
| 7211 | if (n->nfile.fd != ('>' == *p)) { |
| 7212 | s[0] = n->nfile.fd + '0'; |
| 7213 | s[1] = '\0'; |
| 7214 | cmdputs(s); |
| 7215 | } |
| 7216 | cmdputs(p); |
| 7217 | if (n->type >= NTOFD) { |
| 7218 | s[0] = n->ndup.dupfd + '0'; |
| 7219 | s[1] = '\0'; |
| 7220 | cmdputs(s); |
| 7221 | } else { |
| 7222 | cmdtxt(n->nfile.fname); |
| 7223 | } |
| 7224 | } |
| 7225 | } |
| 7226 | #else /* CMDTXT_TABLE */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7227 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7228 | cmdtxt(const union node *n) |
| 7229 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7230 | union node *np; |
| 7231 | struct nodelist *lp; |
| 7232 | const char *p; |
| 7233 | int i; |
| 7234 | char s[2]; |
| 7235 | |
| 7236 | if (n == NULL) |
| 7237 | return; |
| 7238 | switch (n->type) { |
| 7239 | case NSEMI: |
| 7240 | cmdtxt(n->nbinary.ch1); |
| 7241 | cmdputs("; "); |
| 7242 | cmdtxt(n->nbinary.ch2); |
| 7243 | break; |
| 7244 | case NAND: |
| 7245 | cmdtxt(n->nbinary.ch1); |
| 7246 | cmdputs(" && "); |
| 7247 | cmdtxt(n->nbinary.ch2); |
| 7248 | break; |
| 7249 | case NOR: |
| 7250 | cmdtxt(n->nbinary.ch1); |
| 7251 | cmdputs(" || "); |
| 7252 | cmdtxt(n->nbinary.ch2); |
| 7253 | break; |
| 7254 | case NPIPE: |
| 7255 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { |
| 7256 | cmdtxt(lp->n); |
| 7257 | if (lp->next) |
| 7258 | cmdputs(" | "); |
| 7259 | } |
| 7260 | break; |
| 7261 | case NSUBSHELL: |
| 7262 | cmdputs("("); |
| 7263 | cmdtxt(n->nredir.n); |
| 7264 | cmdputs(")"); |
| 7265 | break; |
| 7266 | case NREDIR: |
| 7267 | case NBACKGND: |
| 7268 | cmdtxt(n->nredir.n); |
| 7269 | break; |
| 7270 | case NIF: |
| 7271 | cmdputs("if "); |
| 7272 | cmdtxt(n->nif.test); |
| 7273 | cmdputs("; then "); |
| 7274 | cmdtxt(n->nif.ifpart); |
| 7275 | cmdputs("..."); |
| 7276 | break; |
| 7277 | case NWHILE: |
| 7278 | cmdputs("while "); |
| 7279 | goto until; |
| 7280 | case NUNTIL: |
| 7281 | cmdputs("until "); |
| 7282 | until: |
| 7283 | cmdtxt(n->nbinary.ch1); |
| 7284 | cmdputs("; do "); |
| 7285 | cmdtxt(n->nbinary.ch2); |
| 7286 | cmdputs("; done"); |
| 7287 | break; |
| 7288 | case NFOR: |
| 7289 | cmdputs("for "); |
| 7290 | cmdputs(n->nfor.var); |
| 7291 | cmdputs(" in ..."); |
| 7292 | break; |
| 7293 | case NCASE: |
| 7294 | cmdputs("case "); |
| 7295 | cmdputs(n->ncase.expr->narg.text); |
| 7296 | cmdputs(" in ..."); |
| 7297 | break; |
| 7298 | case NDEFUN: |
| 7299 | cmdputs(n->narg.text); |
| 7300 | cmdputs("() ..."); |
| 7301 | break; |
| 7302 | case NCMD: |
| 7303 | for (np = n->ncmd.args ; np ; np = np->narg.next) { |
| 7304 | cmdtxt(np); |
| 7305 | if (np->narg.next) |
| 7306 | cmdputs(spcstr); |
| 7307 | } |
| 7308 | for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { |
| 7309 | cmdputs(spcstr); |
| 7310 | cmdtxt(np); |
| 7311 | } |
| 7312 | break; |
| 7313 | case NARG: |
| 7314 | cmdputs(n->narg.text); |
| 7315 | break; |
| 7316 | case NTO: |
| 7317 | p = ">"; i = 1; goto redir; |
| 7318 | case NAPPEND: |
| 7319 | p = ">>"; i = 1; goto redir; |
| 7320 | case NTOFD: |
| 7321 | p = ">&"; i = 1; goto redir; |
| 7322 | case NTOOV: |
| 7323 | p = ">|"; i = 1; goto redir; |
| 7324 | case NFROM: |
| 7325 | p = "<"; i = 0; goto redir; |
| 7326 | case NFROMFD: |
| 7327 | p = "<&"; i = 0; goto redir; |
| 7328 | case NFROMTO: |
| 7329 | p = "<>"; i = 0; goto redir; |
| 7330 | redir: |
| 7331 | if (n->nfile.fd != i) { |
| 7332 | s[0] = n->nfile.fd + '0'; |
| 7333 | s[1] = '\0'; |
| 7334 | cmdputs(s); |
| 7335 | } |
| 7336 | cmdputs(p); |
| 7337 | if (n->type == NTOFD || n->type == NFROMFD) { |
| 7338 | s[0] = n->ndup.dupfd + '0'; |
| 7339 | s[1] = '\0'; |
| 7340 | cmdputs(s); |
| 7341 | } else { |
| 7342 | cmdtxt(n->nfile.fname); |
| 7343 | } |
| 7344 | break; |
| 7345 | case NHERE: |
| 7346 | case NXHERE: |
| 7347 | cmdputs("<<..."); |
| 7348 | break; |
| 7349 | default: |
| 7350 | cmdputs("???"); |
| 7351 | break; |
| 7352 | } |
| 7353 | } |
Manuel Novoa III | c639a35 | 2001-08-12 17:32:56 +0000 | [diff] [blame] | 7354 | #endif /* CMDTXT_TABLE */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7355 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7356 | static char * |
| 7357 | commandtext(const union node *n) |
| 7358 | { |
| 7359 | char *name; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7360 | |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 7361 | cmdnextc = name = xmalloc(MAXCMDTEXT); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7362 | cmdnleft = MAXCMDTEXT - 4; |
| 7363 | cmdtxt(n); |
| 7364 | *cmdnextc = '\0'; |
| 7365 | return name; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7366 | } |
| 7367 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7368 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7369 | static void waitonint(int sig) { |
| 7370 | intreceived = 1; |
| 7371 | return; |
| 7372 | } |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 7373 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 7374 | #ifdef CONFIG_ASH_MAIL |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 7375 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7376 | /* |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 7377 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7378 | */ |
| 7379 | |
| 7380 | |
| 7381 | #define MAXMBOXES 10 |
| 7382 | |
| 7383 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7384 | static int nmboxes; /* number of mailboxes */ |
| 7385 | static time_t mailtime[MAXMBOXES]; /* times of mailboxes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7386 | |
| 7387 | |
| 7388 | |
| 7389 | /* |
| 7390 | * Print appropriate message(s) if mail has arrived. If the argument is |
| 7391 | * nozero, then the value of MAIL has changed, so we just update the |
| 7392 | * values. |
| 7393 | */ |
| 7394 | |
| 7395 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7396 | chkmail(int silent) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7397 | { |
| 7398 | int i; |
| 7399 | const char *mpath; |
| 7400 | char *p; |
| 7401 | char *q; |
| 7402 | struct stackmark smark; |
| 7403 | struct stat statb; |
| 7404 | |
| 7405 | if (silent) |
| 7406 | nmboxes = 10; |
| 7407 | if (nmboxes == 0) |
| 7408 | return; |
| 7409 | setstackmark(&smark); |
| 7410 | mpath = mpathset()? mpathval() : mailval(); |
| 7411 | for (i = 0 ; i < nmboxes ; i++) { |
| 7412 | p = padvance(&mpath, nullstr); |
| 7413 | if (p == NULL) |
| 7414 | break; |
| 7415 | if (*p == '\0') |
| 7416 | continue; |
| 7417 | for (q = p ; *q ; q++); |
| 7418 | #ifdef DEBUG |
| 7419 | if (q[-1] != '/') |
| 7420 | abort(); |
| 7421 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7422 | q[-1] = '\0'; /* delete trailing '/' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7423 | if (stat(p, &statb) < 0) |
| 7424 | statb.st_size = 0; |
| 7425 | if (statb.st_size > mailtime[i] && ! silent) { |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 7426 | out2fmt(snlfmt, |
| 7427 | pathopt? pathopt : "you have mail"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7428 | } |
| 7429 | mailtime[i] = statb.st_size; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7430 | } |
| 7431 | nmboxes = i; |
| 7432 | popstackmark(&smark); |
| 7433 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7434 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 7435 | #endif /* CONFIG_ASH_MAIL */ |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 7436 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7437 | #define PROFILE 0 |
| 7438 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7439 | #if PROFILE |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7440 | static short profile_buf[16384]; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7441 | extern int etext(); |
| 7442 | #endif |
| 7443 | |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 7444 | static int isloginsh = 0; |
| 7445 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7446 | static void read_profile (const char *); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7447 | static void cmdloop (int); |
| 7448 | static void options (int); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7449 | static void setoption (int, int); |
| 7450 | static void procargs (int, char **); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7451 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7452 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7453 | /* |
| 7454 | * Main routine. We initialize things, parse the arguments, execute |
| 7455 | * profiles if we're a login shell, and then call cmdloop to execute |
| 7456 | * commands. The setjmp call sets up the location to jump to when an |
| 7457 | * exception occurs. When an exception occurs the variable "state" |
| 7458 | * is used to figure out how far we had gotten. |
| 7459 | */ |
| 7460 | |
| 7461 | int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 7462 | ash_main(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7463 | { |
| 7464 | struct jmploc jmploc; |
| 7465 | struct stackmark smark; |
| 7466 | volatile int state; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7467 | const char *shinit; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7468 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7469 | BLTINCMD = find_builtin("builtin"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7470 | EXECCMD = find_builtin("exec"); |
| 7471 | EVALCMD = find_builtin("eval"); |
| 7472 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 7473 | #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 7474 | unsetenv("PS1"); |
| 7475 | unsetenv("PS2"); |
| 7476 | #endif |
| 7477 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7478 | #if PROFILE |
| 7479 | monitor(4, etext, profile_buf, sizeof profile_buf, 50); |
| 7480 | #endif |
| 7481 | #if defined(linux) || defined(__GNU__) |
| 7482 | signal(SIGCHLD, SIG_DFL); |
| 7483 | #endif |
| 7484 | state = 0; |
| 7485 | if (setjmp(jmploc.loc)) { |
| 7486 | INTOFF; |
| 7487 | /* |
| 7488 | * When a shell procedure is executed, we raise the |
| 7489 | * exception EXSHELLPROC to clean up before executing |
| 7490 | * the shell procedure. |
| 7491 | */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 7492 | if (exception == EXSHELLPROC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7493 | rootpid = getpid(); |
| 7494 | rootshell = 1; |
| 7495 | minusc = NULL; |
| 7496 | state = 3; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 7497 | } else { |
| 7498 | if (exception == EXEXEC) { |
| 7499 | exitstatus = exerrno; |
| 7500 | } else if (exception == EXERROR) { |
| 7501 | exitstatus = 2; |
| 7502 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7503 | if (state == 0 || iflag == 0 || ! rootshell) |
| 7504 | exitshell(exitstatus); |
| 7505 | } |
| 7506 | reset(); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7507 | if (exception == EXINT) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7508 | out2c('\n'); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7509 | } |
| 7510 | popstackmark(&smark); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7511 | FORCEINTON; /* enable interrupts */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7512 | if (state == 1) |
| 7513 | goto state1; |
| 7514 | else if (state == 2) |
| 7515 | goto state2; |
| 7516 | else if (state == 3) |
| 7517 | goto state3; |
| 7518 | else |
| 7519 | goto state4; |
| 7520 | } |
| 7521 | handler = &jmploc; |
| 7522 | #ifdef DEBUG |
| 7523 | opentrace(); |
| 7524 | trputs("Shell args: "); trargs(argv); |
| 7525 | #endif |
| 7526 | rootpid = getpid(); |
| 7527 | rootshell = 1; |
| 7528 | init(); |
| 7529 | setstackmark(&smark); |
| 7530 | procargs(argc, argv); |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 7531 | if (argv[0] && argv[0][0] == '-') |
| 7532 | isloginsh = 1; |
| 7533 | if (isloginsh) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7534 | state = 1; |
| 7535 | read_profile("/etc/profile"); |
| 7536 | state1: |
| 7537 | state = 2; |
| 7538 | read_profile(".profile"); |
| 7539 | } |
| 7540 | state2: |
| 7541 | state = 3; |
| 7542 | #ifndef linux |
| 7543 | if (getuid() == geteuid() && getgid() == getegid()) { |
| 7544 | #endif |
| 7545 | if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { |
| 7546 | state = 3; |
| 7547 | read_profile(shinit); |
| 7548 | } |
| 7549 | #ifndef linux |
| 7550 | } |
| 7551 | #endif |
| 7552 | state3: |
| 7553 | state = 4; |
| 7554 | if (sflag == 0 || minusc) { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 7555 | static const char sigs[] = { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7556 | SIGINT, SIGQUIT, SIGHUP, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7557 | #ifdef SIGTSTP |
| 7558 | SIGTSTP, |
| 7559 | #endif |
| 7560 | SIGPIPE |
| 7561 | }; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 7562 | #define SIGSSIZE ((sizeof(sigs)/sizeof(sigs[0])) - 1) /* trailing nul */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7563 | int i; |
| 7564 | |
| 7565 | for (i = 0; i < SIGSSIZE; i++) |
| 7566 | setsignal(sigs[i]); |
| 7567 | } |
| 7568 | |
| 7569 | if (minusc) |
| 7570 | evalstring(minusc, 0); |
| 7571 | |
| 7572 | if (sflag || minusc == NULL) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7573 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7574 | cmdloop(1); |
| 7575 | } |
| 7576 | #if PROFILE |
| 7577 | monitor(0); |
| 7578 | #endif |
| 7579 | exitshell(exitstatus); |
| 7580 | /* NOTREACHED */ |
| 7581 | } |
| 7582 | |
| 7583 | |
| 7584 | /* |
| 7585 | * Read and execute commands. "Top" is nonzero for the top level command |
| 7586 | * loop; it turns on prompting if the shell is interactive. |
| 7587 | */ |
| 7588 | |
| 7589 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7590 | cmdloop(int top) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7591 | { |
| 7592 | union node *n; |
| 7593 | struct stackmark smark; |
| 7594 | int inter; |
| 7595 | int numeof = 0; |
| 7596 | |
| 7597 | TRACE(("cmdloop(%d) called\n", top)); |
| 7598 | setstackmark(&smark); |
| 7599 | for (;;) { |
| 7600 | if (pendingsigs) |
| 7601 | dotrap(); |
| 7602 | inter = 0; |
| 7603 | if (iflag && top) { |
| 7604 | inter++; |
| 7605 | showjobs(1); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 7606 | #ifdef CONFIG_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7607 | chkmail(0); |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 7608 | #endif |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 7609 | flushall(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7610 | } |
| 7611 | n = parsecmd(inter); |
| 7612 | /* showtree(n); DEBUG */ |
| 7613 | if (n == NEOF) { |
| 7614 | if (!top || numeof >= 50) |
| 7615 | break; |
| 7616 | if (!stoppedjobs()) { |
| 7617 | if (!Iflag) |
| 7618 | break; |
| 7619 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 7620 | } |
| 7621 | numeof++; |
| 7622 | } else if (n != NULL && nflag == 0) { |
| 7623 | job_warning = (job_warning == 2) ? 1 : 0; |
| 7624 | numeof = 0; |
| 7625 | evaltree(n, 0); |
| 7626 | } |
| 7627 | popstackmark(&smark); |
| 7628 | setstackmark(&smark); |
| 7629 | if (evalskip == SKIPFILE) { |
| 7630 | evalskip = 0; |
| 7631 | break; |
| 7632 | } |
| 7633 | } |
| 7634 | popstackmark(&smark); |
| 7635 | } |
| 7636 | |
| 7637 | |
| 7638 | |
| 7639 | /* |
| 7640 | * Read /etc/profile or .profile. Return on error. |
| 7641 | */ |
| 7642 | |
| 7643 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 7644 | read_profile(const char *name) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7645 | { |
| 7646 | int fd; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 7647 | int xflag_save; |
| 7648 | int vflag_save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7649 | |
| 7650 | INTOFF; |
| 7651 | if ((fd = open(name, O_RDONLY)) >= 0) |
| 7652 | setinputfd(fd, 1); |
| 7653 | INTON; |
| 7654 | if (fd < 0) |
| 7655 | return; |
| 7656 | /* -q turns off -x and -v just when executing init files */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 7657 | /* Note: Might do a little redundant work, but reduces code size. */ |
| 7658 | xflag_save = xflag; |
| 7659 | vflag_save = vflag; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7660 | if (qflag) { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 7661 | vflag = xflag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7662 | } |
| 7663 | cmdloop(0); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 7664 | xflag = xflag_save; |
| 7665 | vflag = vflag_save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7666 | popfile(); |
| 7667 | } |
| 7668 | |
| 7669 | |
| 7670 | |
| 7671 | /* |
| 7672 | * Read a file containing shell functions. |
| 7673 | */ |
| 7674 | |
| 7675 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7676 | readcmdfile(const char *name) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7677 | { |
| 7678 | int fd; |
| 7679 | |
| 7680 | INTOFF; |
| 7681 | if ((fd = open(name, O_RDONLY)) >= 0) |
| 7682 | setinputfd(fd, 1); |
| 7683 | else |
| 7684 | error("Can't open %s", name); |
| 7685 | INTON; |
| 7686 | cmdloop(0); |
| 7687 | popfile(); |
| 7688 | } |
| 7689 | |
| 7690 | |
| 7691 | |
| 7692 | /* |
| 7693 | * Take commands from a file. To be compatable we should do a path |
| 7694 | * search for the file, which is necessary to find sub-commands. |
| 7695 | */ |
| 7696 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7697 | static inline char * |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 7698 | find_dot_file(char *mybasename) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7699 | { |
| 7700 | char *fullname; |
| 7701 | const char *path = pathval(); |
| 7702 | struct stat statb; |
| 7703 | |
| 7704 | /* don't try this for absolute or relative paths */ |
| 7705 | if (strchr(mybasename, '/')) |
| 7706 | return mybasename; |
| 7707 | |
| 7708 | while ((fullname = padvance(&path, mybasename)) != NULL) { |
| 7709 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 7710 | /* |
| 7711 | * Don't bother freeing here, since it will |
| 7712 | * be freed by the caller. |
| 7713 | */ |
| 7714 | return fullname; |
| 7715 | } |
| 7716 | stunalloc(fullname); |
| 7717 | } |
| 7718 | |
| 7719 | /* not found in the PATH */ |
| 7720 | error("%s: not found", mybasename); |
| 7721 | /* NOTREACHED */ |
| 7722 | } |
| 7723 | |
| 7724 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 7725 | dotcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7726 | { |
| 7727 | struct strlist *sp; |
Eric Andersen | 69a20f0 | 2001-10-31 10:40:37 +0000 | [diff] [blame] | 7728 | volatile struct shparam saveparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7729 | exitstatus = 0; |
| 7730 | |
| 7731 | for (sp = cmdenviron; sp ; sp = sp->next) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 7732 | setvareq(xstrdup(sp->text), VSTRFIXED|VTEXTFIXED); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7733 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7734 | if (argc >= 2) { /* That's what SVR2 does */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7735 | char *fullname; |
| 7736 | struct stackmark smark; |
| 7737 | |
| 7738 | setstackmark(&smark); |
| 7739 | fullname = find_dot_file(argv[1]); |
Eric Andersen | 69a20f0 | 2001-10-31 10:40:37 +0000 | [diff] [blame] | 7740 | |
| 7741 | if (argc>2) { |
| 7742 | saveparam = shellparam; |
| 7743 | shellparam.malloc = 0; |
| 7744 | shellparam.nparam = argc - 2; |
| 7745 | shellparam.p = argv + 2; |
| 7746 | }; |
| 7747 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7748 | setinputfile(fullname, 1); |
| 7749 | commandname = fullname; |
| 7750 | cmdloop(0); |
| 7751 | popfile(); |
Eric Andersen | 69a20f0 | 2001-10-31 10:40:37 +0000 | [diff] [blame] | 7752 | |
| 7753 | if (argc>2) { |
| 7754 | freeparam(&shellparam); |
| 7755 | shellparam = saveparam; |
| 7756 | }; |
| 7757 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7758 | popstackmark(&smark); |
| 7759 | } |
| 7760 | return exitstatus; |
| 7761 | } |
| 7762 | |
| 7763 | |
| 7764 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 7765 | exitcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7766 | { |
| 7767 | if (stoppedjobs()) |
| 7768 | return 0; |
| 7769 | if (argc > 1) |
| 7770 | exitstatus = number(argv[1]); |
| 7771 | else |
| 7772 | exitstatus = oexitstatus; |
| 7773 | exitshell(exitstatus); |
| 7774 | /* NOTREACHED */ |
| 7775 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7776 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7777 | static pointer |
| 7778 | stalloc(int nbytes) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7779 | { |
| 7780 | char *p; |
| 7781 | |
| 7782 | nbytes = ALIGN(nbytes); |
| 7783 | if (nbytes > stacknleft) { |
| 7784 | int blocksize; |
| 7785 | struct stack_block *sp; |
| 7786 | |
| 7787 | blocksize = nbytes; |
| 7788 | if (blocksize < MINSIZE) |
| 7789 | blocksize = MINSIZE; |
| 7790 | INTOFF; |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 7791 | sp = xmalloc(sizeof(struct stack_block) - MINSIZE + blocksize); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7792 | sp->prev = stackp; |
| 7793 | stacknxt = sp->space; |
| 7794 | stacknleft = blocksize; |
| 7795 | stackp = sp; |
| 7796 | INTON; |
| 7797 | } |
| 7798 | p = stacknxt; |
| 7799 | stacknxt += nbytes; |
| 7800 | stacknleft -= nbytes; |
| 7801 | return p; |
| 7802 | } |
| 7803 | |
| 7804 | |
| 7805 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7806 | stunalloc(pointer p) |
| 7807 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7808 | #ifdef DEBUG |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7809 | if (p == NULL) { /*DEBUG */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7810 | write(2, "stunalloc\n", 10); |
| 7811 | abort(); |
| 7812 | } |
| 7813 | #endif |
| 7814 | if (!(stacknxt >= (char *)p && (char *)p >= stackp->space)) { |
| 7815 | p = stackp->space; |
| 7816 | } |
| 7817 | stacknleft += stacknxt - (char *)p; |
| 7818 | stacknxt = p; |
| 7819 | } |
| 7820 | |
| 7821 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7822 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7823 | setstackmark(struct stackmark *mark) |
| 7824 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7825 | mark->stackp = stackp; |
| 7826 | mark->stacknxt = stacknxt; |
| 7827 | mark->stacknleft = stacknleft; |
| 7828 | mark->marknext = markp; |
| 7829 | markp = mark; |
| 7830 | } |
| 7831 | |
| 7832 | |
| 7833 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7834 | popstackmark(struct stackmark *mark) |
| 7835 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7836 | struct stack_block *sp; |
| 7837 | |
| 7838 | INTOFF; |
| 7839 | markp = mark->marknext; |
| 7840 | while (stackp != mark->stackp) { |
| 7841 | sp = stackp; |
| 7842 | stackp = sp->prev; |
| 7843 | ckfree(sp); |
| 7844 | } |
| 7845 | stacknxt = mark->stacknxt; |
| 7846 | stacknleft = mark->stacknleft; |
| 7847 | INTON; |
| 7848 | } |
| 7849 | |
| 7850 | |
| 7851 | /* |
| 7852 | * When the parser reads in a string, it wants to stick the string on the |
| 7853 | * stack and only adjust the stack pointer when it knows how big the |
| 7854 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 7855 | * of space on top of the stack and stackblocklen returns the length of |
| 7856 | * this block. Growstackblock will grow this space by at least one byte, |
| 7857 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 7858 | * part of the block that has been used. |
| 7859 | */ |
| 7860 | |
| 7861 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7862 | growstackblock(void) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7863 | char *p; |
| 7864 | int newlen = ALIGN(stacknleft * 2 + 100); |
| 7865 | char *oldspace = stacknxt; |
| 7866 | int oldlen = stacknleft; |
| 7867 | struct stack_block *sp; |
| 7868 | struct stack_block *oldstackp; |
| 7869 | |
| 7870 | if (stacknxt == stackp->space && stackp != &stackbase) { |
| 7871 | INTOFF; |
| 7872 | oldstackp = stackp; |
| 7873 | sp = stackp; |
| 7874 | stackp = sp->prev; |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 7875 | sp = xrealloc((pointer)sp, sizeof(struct stack_block) - MINSIZE + newlen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7876 | sp->prev = stackp; |
| 7877 | stackp = sp; |
| 7878 | stacknxt = sp->space; |
| 7879 | stacknleft = newlen; |
| 7880 | { |
| 7881 | /* Stack marks pointing to the start of the old block |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7882 | * must be relocated to point to the new block |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7883 | */ |
| 7884 | struct stackmark *xmark; |
| 7885 | xmark = markp; |
| 7886 | while (xmark != NULL && xmark->stackp == oldstackp) { |
| 7887 | xmark->stackp = stackp; |
| 7888 | xmark->stacknxt = stacknxt; |
| 7889 | xmark->stacknleft = stacknleft; |
| 7890 | xmark = xmark->marknext; |
| 7891 | } |
| 7892 | } |
| 7893 | INTON; |
| 7894 | } else { |
| 7895 | p = stalloc(newlen); |
| 7896 | memcpy(p, oldspace, oldlen); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7897 | stacknxt = p; /* free the space */ |
| 7898 | stacknleft += newlen; /* we just allocated */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7899 | } |
| 7900 | } |
| 7901 | |
| 7902 | |
| 7903 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7904 | static inline void |
| 7905 | grabstackblock(int len) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7906 | { |
| 7907 | len = ALIGN(len); |
| 7908 | stacknxt += len; |
| 7909 | stacknleft -= len; |
| 7910 | } |
| 7911 | |
| 7912 | |
| 7913 | |
| 7914 | /* |
| 7915 | * The following routines are somewhat easier to use that the above. |
| 7916 | * The user declares a variable of type STACKSTR, which may be declared |
| 7917 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 7918 | * the user uses the macro STPUTC to add characters to the string. In |
| 7919 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 7920 | * grown as necessary. When the user is done, she can just leave the |
| 7921 | * string there and refer to it using stackblock(). Or she can allocate |
| 7922 | * the space for it using grabstackstr(). If it is necessary to allow |
| 7923 | * someone else to use the stack temporarily and then continue to grow |
| 7924 | * the string, the user should use grabstack to allocate the space, and |
| 7925 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 7926 | * |
| 7927 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 7928 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 7929 | * is space for at least one character. |
| 7930 | */ |
| 7931 | |
| 7932 | |
| 7933 | static char * |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7934 | growstackstr(void) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7935 | int len = stackblocksize(); |
| 7936 | if (herefd >= 0 && len >= 1024) { |
| 7937 | xwrite(herefd, stackblock(), len); |
| 7938 | sstrnleft = len - 1; |
| 7939 | return stackblock(); |
| 7940 | } |
| 7941 | growstackblock(); |
| 7942 | sstrnleft = stackblocksize() - len - 1; |
| 7943 | return stackblock() + len; |
| 7944 | } |
| 7945 | |
| 7946 | |
| 7947 | /* |
| 7948 | * Called from CHECKSTRSPACE. |
| 7949 | */ |
| 7950 | |
| 7951 | static char * |
| 7952 | makestrspace(size_t newlen) { |
| 7953 | int len = stackblocksize() - sstrnleft; |
| 7954 | do { |
| 7955 | growstackblock(); |
| 7956 | sstrnleft = stackblocksize() - len; |
| 7957 | } while (sstrnleft < newlen); |
| 7958 | return stackblock() + len; |
| 7959 | } |
| 7960 | |
| 7961 | |
| 7962 | |
| 7963 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7964 | ungrabstackstr(char *s, char *p) |
| 7965 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7966 | stacknleft += stacknxt - s; |
| 7967 | stacknxt = s; |
| 7968 | sstrnleft = stacknleft - (p - s); |
| 7969 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7970 | /* |
| 7971 | * Miscelaneous builtins. |
| 7972 | */ |
| 7973 | |
| 7974 | |
| 7975 | #undef rflag |
| 7976 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7977 | #if !defined(__GLIBC__) || __GLIBC__ == 2 && __GLIBC_MINOR__ < 1 |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7978 | typedef long rlim_t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7979 | #endif |
| 7980 | |
| 7981 | |
| 7982 | |
| 7983 | /* |
| 7984 | * The read builtin. The -e option causes backslashes to escape the |
| 7985 | * following character. |
| 7986 | * |
| 7987 | * This uses unbuffered input, which may be avoidable in some cases. |
| 7988 | */ |
| 7989 | |
| 7990 | static int |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 7991 | readcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7992 | { |
| 7993 | char **ap; |
| 7994 | int backslash; |
| 7995 | char c; |
| 7996 | int rflag; |
| 7997 | char *prompt; |
| 7998 | const char *ifs; |
| 7999 | char *p; |
| 8000 | int startword; |
| 8001 | int status; |
| 8002 | int i; |
| 8003 | |
| 8004 | rflag = 0; |
| 8005 | prompt = NULL; |
| 8006 | while ((i = nextopt("p:r")) != '\0') { |
| 8007 | if (i == 'p') |
| 8008 | prompt = optionarg; |
| 8009 | else |
| 8010 | rflag = 1; |
| 8011 | } |
| 8012 | if (prompt && isatty(0)) { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8013 | out2str(prompt); /* read without cmdedit */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8014 | flushall(); |
| 8015 | } |
| 8016 | if (*(ap = argptr) == NULL) |
| 8017 | error("arg count"); |
| 8018 | if ((ifs = bltinlookup("IFS")) == NULL) |
| 8019 | ifs = defifs; |
| 8020 | status = 0; |
| 8021 | startword = 1; |
| 8022 | backslash = 0; |
| 8023 | STARTSTACKSTR(p); |
| 8024 | for (;;) { |
| 8025 | if (read(0, &c, 1) != 1) { |
| 8026 | status = 1; |
| 8027 | break; |
| 8028 | } |
| 8029 | if (c == '\0') |
| 8030 | continue; |
| 8031 | if (backslash) { |
| 8032 | backslash = 0; |
| 8033 | if (c != '\n') |
| 8034 | STPUTC(c, p); |
| 8035 | continue; |
| 8036 | } |
| 8037 | if (!rflag && c == '\\') { |
| 8038 | backslash++; |
| 8039 | continue; |
| 8040 | } |
| 8041 | if (c == '\n') |
| 8042 | break; |
| 8043 | if (startword && *ifs == ' ' && strchr(ifs, c)) { |
| 8044 | continue; |
| 8045 | } |
| 8046 | startword = 0; |
| 8047 | if (backslash && c == '\\') { |
| 8048 | if (read(0, &c, 1) != 1) { |
| 8049 | status = 1; |
| 8050 | break; |
| 8051 | } |
| 8052 | STPUTC(c, p); |
| 8053 | } else if (ap[1] != NULL && strchr(ifs, c) != NULL) { |
| 8054 | STACKSTRNUL(p); |
| 8055 | setvar(*ap, stackblock(), 0); |
| 8056 | ap++; |
| 8057 | startword = 1; |
| 8058 | STARTSTACKSTR(p); |
| 8059 | } else { |
| 8060 | STPUTC(c, p); |
| 8061 | } |
| 8062 | } |
| 8063 | STACKSTRNUL(p); |
| 8064 | /* Remove trailing blanks */ |
| 8065 | while (stackblock() <= --p && strchr(ifs, *p) != NULL) |
| 8066 | *p = '\0'; |
| 8067 | setvar(*ap, stackblock(), 0); |
| 8068 | while (*++ap != NULL) |
| 8069 | setvar(*ap, nullstr, 0); |
| 8070 | return status; |
| 8071 | } |
| 8072 | |
| 8073 | |
| 8074 | |
| 8075 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 8076 | umaskcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8077 | { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8078 | static const char permuser[3] = "ugo"; |
| 8079 | static const char permmode[3] = "rwx"; |
| 8080 | static const short int permmask[] = { |
| 8081 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 8082 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 8083 | S_IROTH, S_IWOTH, S_IXOTH |
| 8084 | }; |
| 8085 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8086 | char *ap; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8087 | mode_t mask; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8088 | int i; |
| 8089 | int symbolic_mode = 0; |
| 8090 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8091 | while (nextopt("S") != '\0') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8092 | symbolic_mode = 1; |
| 8093 | } |
| 8094 | |
| 8095 | INTOFF; |
| 8096 | mask = umask(0); |
| 8097 | umask(mask); |
| 8098 | INTON; |
| 8099 | |
| 8100 | if ((ap = *argptr) == NULL) { |
| 8101 | if (symbolic_mode) { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8102 | char buf[18]; |
| 8103 | char *p = buf; |
| 8104 | for (i=0 ; i<3 ; i++) { |
| 8105 | int j; |
| 8106 | *p++ = permuser[i]; |
| 8107 | *p++ = '='; |
| 8108 | for (j=0 ; j<3 ; j++) { |
| 8109 | if ((mask & permmask[3*i+j]) == 0) { |
| 8110 | *p++ = permmode[j]; |
| 8111 | } |
| 8112 | } |
| 8113 | *p++ = ','; |
| 8114 | } |
| 8115 | *--p = 0; |
| 8116 | puts(buf); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8117 | } else { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8118 | printf("%.4o\n", mask); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8119 | } |
| 8120 | } else { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8121 | if (is_digit((unsigned char)*ap)) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8122 | mask = 0; |
| 8123 | do { |
| 8124 | if (*ap >= '8' || *ap < '0') |
| 8125 | error("Illegal number: %s", argv[1]); |
| 8126 | mask = (mask << 3) + (*ap - '0'); |
| 8127 | } while (*++ap != '\0'); |
| 8128 | umask(mask); |
| 8129 | } else { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8130 | mask = ~mask & 0777; |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 8131 | if (! parse_mode(ap, &mask)) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8132 | error("Illegal mode: %s", ap); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8133 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8134 | umask(~mask & 0777); |
| 8135 | } |
| 8136 | } |
| 8137 | return 0; |
| 8138 | } |
| 8139 | |
| 8140 | /* |
| 8141 | * ulimit builtin |
| 8142 | * |
| 8143 | * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and |
| 8144 | * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with |
| 8145 | * ash by J.T. Conklin. |
| 8146 | * |
| 8147 | * Public domain. |
| 8148 | */ |
| 8149 | |
| 8150 | struct limits { |
| 8151 | const char *name; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8152 | short cmd; |
| 8153 | short factor; /* multiply by to get rlim_{cur,max} values */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8154 | }; |
| 8155 | |
| 8156 | static const struct limits limits[] = { |
| 8157 | #ifdef RLIMIT_CPU |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8158 | { "time(seconds)", RLIMIT_CPU, 1 }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8159 | #endif |
| 8160 | #ifdef RLIMIT_FSIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8161 | { "file(blocks)", RLIMIT_FSIZE, 512 }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8162 | #endif |
| 8163 | #ifdef RLIMIT_DATA |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8164 | { "data(kbytes)", RLIMIT_DATA, 1024 }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8165 | #endif |
| 8166 | #ifdef RLIMIT_STACK |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8167 | { "stack(kbytes)", RLIMIT_STACK, 1024 }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8168 | #endif |
| 8169 | #ifdef RLIMIT_CORE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8170 | { "coredump(blocks)", RLIMIT_CORE, 512 }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8171 | #endif |
| 8172 | #ifdef RLIMIT_RSS |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8173 | { "memory(kbytes)", RLIMIT_RSS, 1024 }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8174 | #endif |
| 8175 | #ifdef RLIMIT_MEMLOCK |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8176 | { "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024 }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8177 | #endif |
| 8178 | #ifdef RLIMIT_NPROC |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8179 | { "process(processes)", RLIMIT_NPROC, 1 }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8180 | #endif |
| 8181 | #ifdef RLIMIT_NOFILE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8182 | { "nofiles(descriptors)", RLIMIT_NOFILE, 1 }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8183 | #endif |
| 8184 | #ifdef RLIMIT_VMEM |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8185 | { "vmemory(kbytes)", RLIMIT_VMEM, 1024 }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8186 | #endif |
| 8187 | #ifdef RLIMIT_SWAP |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8188 | { "swap(kbytes)", RLIMIT_SWAP, 1024 }, |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8189 | #endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8190 | { NULL, 0, 0 } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8191 | }; |
| 8192 | |
| 8193 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 8194 | ulimitcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8195 | { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8196 | static const char unlimited_string[] = "unlimited"; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8197 | int c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8198 | rlim_t val = 0; |
| 8199 | enum { SOFT = 0x1, HARD = 0x2 } |
| 8200 | how = SOFT | HARD; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8201 | const struct limits *l; |
| 8202 | int set, all = 0; |
| 8203 | int optc, what; |
| 8204 | struct rlimit limit; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8205 | |
| 8206 | what = 'f'; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8207 | |
| 8208 | while ((optc = nextopt("HSa" |
| 8209 | #ifdef RLIMIT_CPU |
| 8210 | "t" |
| 8211 | #endif |
| 8212 | #ifdef RLIMIT_FSIZE |
| 8213 | "f" |
| 8214 | #endif |
| 8215 | #ifdef RLIMIT_DATA |
| 8216 | "d" |
| 8217 | #endif |
| 8218 | #ifdef RLIMIT_STACK |
| 8219 | "s" |
| 8220 | #endif |
| 8221 | #ifdef RLIMIT_CORE |
| 8222 | "c" |
| 8223 | #endif |
| 8224 | #ifdef RLIMIT_RSS |
| 8225 | "m" |
| 8226 | #endif |
| 8227 | #ifdef RLIMIT_MEMLOCK |
| 8228 | "l" |
| 8229 | #endif |
| 8230 | #ifdef RLIMIT_NPROC |
| 8231 | "p" |
| 8232 | #endif |
| 8233 | #ifdef RLIMIT_NOFILE |
| 8234 | "n" |
| 8235 | #endif |
| 8236 | #ifdef RLIMIT_VMEM |
| 8237 | "v" |
| 8238 | #endif |
| 8239 | #ifdef RLIMIT_SWAP |
| 8240 | "w" |
| 8241 | #endif |
| 8242 | )) != '\0') { |
| 8243 | if (optc == 'H') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8244 | how = HARD; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8245 | } else if (optc == 'S') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8246 | how = SOFT; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8247 | } else if (optc == 'a') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8248 | all = 1; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8249 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8250 | what = optc; |
| 8251 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8252 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8253 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8254 | for (l = limits; l->name; l++) { |
| 8255 | if(l->name[0] == what) |
| 8256 | break; |
| 8257 | if(l->name[1]=='w' && what=='w') |
| 8258 | break; |
| 8259 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8260 | |
| 8261 | set = *argptr ? 1 : 0; |
| 8262 | if (set) { |
| 8263 | char *p = *argptr; |
| 8264 | |
| 8265 | if (all || argptr[1]) |
| 8266 | error("too many arguments"); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8267 | if (strcmp(p, unlimited_string) == 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8268 | val = RLIM_INFINITY; |
| 8269 | else { |
| 8270 | val = (rlim_t) 0; |
| 8271 | |
| 8272 | while ((c = *p++) >= '0' && c <= '9') |
| 8273 | { |
| 8274 | val = (val * 10) + (long)(c - '0'); |
| 8275 | if (val < (rlim_t) 0) |
| 8276 | break; |
| 8277 | } |
| 8278 | if (c) |
| 8279 | error("bad number"); |
| 8280 | val *= l->factor; |
| 8281 | } |
| 8282 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8283 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8284 | if (all) { |
| 8285 | for (l = limits; l->name; l++) { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8286 | printf("%-20s ", l->name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8287 | getrlimit(l->cmd, &limit); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8288 | OUTPUT_LIMIT: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8289 | if (how & SOFT) |
| 8290 | val = limit.rlim_cur; |
| 8291 | else if (how & HARD) |
| 8292 | val = limit.rlim_max; |
| 8293 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8294 | if (val == RLIM_INFINITY) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8295 | puts(unlimited_string); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8296 | else |
| 8297 | { |
| 8298 | val /= l->factor; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8299 | printf("%lld\n", (long long) val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8300 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8301 | if (!all) { |
| 8302 | break; |
| 8303 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8304 | } |
| 8305 | return 0; |
| 8306 | } |
| 8307 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8308 | if (!set) { |
| 8309 | goto OUTPUT_LIMIT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8310 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8311 | |
| 8312 | getrlimit(l->cmd, &limit); |
| 8313 | if (how & HARD) |
| 8314 | limit.rlim_max = val; |
| 8315 | if (how & SOFT) |
| 8316 | limit.rlim_cur = val; |
| 8317 | if (setrlimit(l->cmd, &limit) < 0) |
| 8318 | error("error setting limit (%m)"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8319 | return 0; |
| 8320 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8321 | /* |
| 8322 | * prefix -- see if pfx is a prefix of string. |
| 8323 | */ |
| 8324 | |
| 8325 | static int |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8326 | prefix(char const *pfx, char const *string) |
| 8327 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8328 | while (*pfx) { |
| 8329 | if (*pfx++ != *string++) |
| 8330 | return 0; |
| 8331 | } |
| 8332 | return 1; |
| 8333 | } |
| 8334 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8335 | /* |
| 8336 | * Return true if s is a string of digits, and save munber in intptr |
| 8337 | * nagative is bad |
| 8338 | */ |
| 8339 | |
| 8340 | static int |
| 8341 | is_number(const char *p, int *intptr) |
| 8342 | { |
| 8343 | int ret = 0; |
| 8344 | |
| 8345 | do { |
| 8346 | if (! is_digit(*p)) |
| 8347 | return 0; |
| 8348 | ret *= 10; |
| 8349 | ret += digit_val(*p); |
| 8350 | p++; |
| 8351 | } while (*p != '\0'); |
| 8352 | |
| 8353 | *intptr = ret; |
| 8354 | return 1; |
| 8355 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8356 | |
| 8357 | /* |
| 8358 | * Convert a string of digits to an integer, printing an error message on |
| 8359 | * failure. |
| 8360 | */ |
| 8361 | |
| 8362 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8363 | number(const char *s) |
| 8364 | { |
| 8365 | int i; |
| 8366 | if (! is_number(s, &i)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8367 | error("Illegal number: %s", s); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8368 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8369 | } |
| 8370 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8371 | /* |
| 8372 | * Produce a possibly single quoted string suitable as input to the shell. |
| 8373 | * The return string is allocated on the stack. |
| 8374 | */ |
| 8375 | |
| 8376 | static char * |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 8377 | single_quote(const char *s) |
| 8378 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8379 | char *p; |
| 8380 | |
| 8381 | STARTSTACKSTR(p); |
| 8382 | |
| 8383 | do { |
| 8384 | char *q = p; |
| 8385 | size_t len1, len1p, len2, len2p; |
| 8386 | |
| 8387 | len1 = strcspn(s, "'"); |
| 8388 | len2 = strspn(s + len1, "'"); |
| 8389 | |
| 8390 | len1p = len1 ? len1 + 2 : len1; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8391 | len2p = len2 + ((len2 < 2) ? len2 : 2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8392 | |
| 8393 | CHECKSTRSPACE(len1p + len2p + 1, p); |
| 8394 | |
| 8395 | if (len1) { |
| 8396 | *p = '\''; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8397 | q = p + 1 + len1; |
| 8398 | memcpy(p + 1, s, len1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8399 | *q++ = '\''; |
| 8400 | s += len1; |
| 8401 | } |
| 8402 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8403 | if (len2 > 1) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8404 | *q = '"'; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8405 | q += 1 + len2; |
| 8406 | memcpy(q + 1, s, len2); |
| 8407 | *q = '"'; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8408 | s += len2; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8409 | } else if (len2 == 1) { |
| 8410 | *q++ = '\\'; |
| 8411 | *q = '\''; |
| 8412 | s++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8413 | } |
| 8414 | |
| 8415 | STADJUST(len1p + len2p, p); |
| 8416 | } while (*s); |
| 8417 | |
| 8418 | USTPUTC(0, p); |
| 8419 | |
| 8420 | return grabstackstr(p); |
| 8421 | } |
| 8422 | |
| 8423 | /* |
| 8424 | * Like strdup but works with the ash stack. |
| 8425 | */ |
| 8426 | |
| 8427 | static char * |
| 8428 | sstrdup(const char *p) |
| 8429 | { |
| 8430 | size_t len = strlen(p) + 1; |
| 8431 | return memcpy(stalloc(len), p, len); |
| 8432 | } |
| 8433 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8434 | |
| 8435 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8436 | * Routine for dealing with parsed shell commands. |
| 8437 | */ |
| 8438 | |
| 8439 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8440 | static void sizenodelist (const struct nodelist *); |
| 8441 | static struct nodelist *copynodelist (const struct nodelist *); |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 8442 | static char *nodexstrdup (const char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8443 | |
Manuel Novoa III | 8d0afde | 2001-09-11 01:14:02 +0000 | [diff] [blame] | 8444 | #define CALCSIZE_TABLE |
| 8445 | #define COPYNODE_TABLE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8446 | #if defined(CALCSIZE_TABLE) || defined(COPYNODE_TABLE) |
| 8447 | /* |
| 8448 | * To collect a lot of redundant code in case statements for copynode() |
| 8449 | * and calcsize(), we implement a mini language here. Each type of node |
| 8450 | * struct has an associated instruction sequence that operates on its |
| 8451 | * members via their offsets. The instruction are pack in unsigned chars |
| 8452 | * with format IIDDDDDE where the bits are |
| 8453 | * I : part of the instruction opcode, which are |
| 8454 | * 00 : member is a pointer to another node |
| 8455 | * 40 : member is an integer |
| 8456 | * 80 : member is a pointer to a nodelist |
| 8457 | * CC : member is a pointer to a char string |
| 8458 | * D : data - the actual offset of the member to operate on in the struct |
| 8459 | * (since we assume bit 0 is set, it is not shifted) |
| 8460 | * E : flag signaling end of instruction sequence |
| 8461 | * |
| 8462 | * WARNING: In order to handle larger offsets for 64bit archs, this code |
| 8463 | * assumes that no offset can be an odd number and stores the |
| 8464 | * end-of-instructions flag in bit 0. |
| 8465 | */ |
| 8466 | |
| 8467 | #define NODE_INTEGER 0x40 |
| 8468 | #define NODE_NODELIST 0x80 |
| 8469 | #define NODE_CHARPTR 0xC0 |
| 8470 | #define NODE_NOMORE 0x01 /* Note: no offset should be odd (aligned)*/ |
| 8471 | #define NODE_MBRMASK 0xC0 |
| 8472 | #define NODE_OFFSETMASK 0x3E |
| 8473 | |
| 8474 | static const unsigned char copynode_ops[35] = { |
| 8475 | #define COPYNODE_OPS0 0 |
| 8476 | offsetof(union node, nbinary.ch2), |
| 8477 | offsetof(union node, nbinary.ch1)|NODE_NOMORE, |
| 8478 | #define COPYNODE_OPS1 (COPYNODE_OPS0 + 2) |
| 8479 | offsetof(union node, ncmd.redirect), |
| 8480 | offsetof(union node, ncmd.args), |
| 8481 | offsetof(union node, ncmd.assign), |
| 8482 | offsetof(union node, ncmd.backgnd)|NODE_INTEGER|NODE_NOMORE, |
| 8483 | #define COPYNODE_OPS2 (COPYNODE_OPS1 + 4) |
| 8484 | offsetof(union node, npipe.cmdlist)|NODE_NODELIST, |
| 8485 | offsetof(union node, npipe.backgnd)|NODE_INTEGER|NODE_NOMORE, |
| 8486 | #define COPYNODE_OPS3 (COPYNODE_OPS2 + 2) |
| 8487 | offsetof(union node, nredir.redirect), |
| 8488 | offsetof(union node, nredir.n)|NODE_NOMORE, |
| 8489 | #define COPYNODE_OPS4 (COPYNODE_OPS3 + 2) |
| 8490 | offsetof(union node, nif.elsepart), |
| 8491 | offsetof(union node, nif.ifpart), |
| 8492 | offsetof(union node, nif.test)|NODE_NOMORE, |
| 8493 | #define COPYNODE_OPS5 (COPYNODE_OPS4 + 3) |
| 8494 | offsetof(union node, nfor.var)|NODE_CHARPTR, |
| 8495 | offsetof(union node, nfor.body), |
| 8496 | offsetof(union node, nfor.args)|NODE_NOMORE, |
| 8497 | #define COPYNODE_OPS6 (COPYNODE_OPS5 + 3) |
| 8498 | offsetof(union node, ncase.cases), |
| 8499 | offsetof(union node, ncase.expr)|NODE_NOMORE, |
| 8500 | #define COPYNODE_OPS7 (COPYNODE_OPS6 + 2) |
| 8501 | offsetof(union node, nclist.body), |
| 8502 | offsetof(union node, nclist.pattern), |
| 8503 | offsetof(union node, nclist.next)|NODE_NOMORE, |
| 8504 | #define COPYNODE_OPS8 (COPYNODE_OPS7 + 3) |
| 8505 | offsetof(union node, narg.backquote)|NODE_NODELIST, |
| 8506 | offsetof(union node, narg.text)|NODE_CHARPTR, |
| 8507 | offsetof(union node, narg.next)|NODE_NOMORE, |
| 8508 | #define COPYNODE_OPS9 (COPYNODE_OPS8 + 3) |
| 8509 | offsetof(union node, nfile.fname), |
| 8510 | offsetof(union node, nfile.fd)|NODE_INTEGER, |
| 8511 | offsetof(union node, nfile.next)|NODE_NOMORE, |
| 8512 | #define COPYNODE_OPS10 (COPYNODE_OPS9 + 3) |
| 8513 | offsetof(union node, ndup.vname), |
| 8514 | offsetof(union node, ndup.dupfd)|NODE_INTEGER, |
| 8515 | offsetof(union node, ndup.fd)|NODE_INTEGER, |
| 8516 | offsetof(union node, ndup.next)|NODE_NOMORE, |
| 8517 | #define COPYNODE_OPS11 (COPYNODE_OPS10 + 4) |
| 8518 | offsetof(union node, nhere.doc), |
| 8519 | offsetof(union node, nhere.fd)|NODE_INTEGER, |
| 8520 | offsetof(union node, nhere.next)|NODE_NOMORE, |
| 8521 | #define COPYNODE_OPS12 (COPYNODE_OPS11 + 3) |
| 8522 | offsetof(union node, nnot.com)|NODE_NOMORE, |
| 8523 | }; |
| 8524 | |
| 8525 | #if COPYNODE_OPS12 != 34 |
| 8526 | #error COPYNODE_OPS12 is incorrect |
| 8527 | #endif |
| 8528 | |
| 8529 | static const unsigned char copynode_ops_index[26] = { |
| 8530 | COPYNODE_OPS0, /* NSEMI */ |
| 8531 | COPYNODE_OPS1, /* NCMD */ |
| 8532 | COPYNODE_OPS2, /* NPIPE */ |
| 8533 | COPYNODE_OPS3, /* NREDIR */ |
| 8534 | COPYNODE_OPS3, /* NBACKGND */ |
| 8535 | COPYNODE_OPS3, /* NSUBSHELL */ |
| 8536 | COPYNODE_OPS0, /* NAND */ |
| 8537 | COPYNODE_OPS0, /* NOR */ |
| 8538 | COPYNODE_OPS4, /* NIF */ |
| 8539 | COPYNODE_OPS0, /* NWHILE */ |
| 8540 | COPYNODE_OPS0, /* NUNTIL */ |
| 8541 | COPYNODE_OPS5, /* NFOR */ |
| 8542 | COPYNODE_OPS6, /* NCASE */ |
| 8543 | COPYNODE_OPS7, /* NCLIST */ |
| 8544 | COPYNODE_OPS8, /* NDEFUN */ |
| 8545 | COPYNODE_OPS8, /* NARG */ |
| 8546 | COPYNODE_OPS9, /* NTO */ |
| 8547 | COPYNODE_OPS9, /* NFROM */ |
| 8548 | COPYNODE_OPS9, /* NFROMTO */ |
| 8549 | COPYNODE_OPS9, /* NAPPEND */ |
| 8550 | COPYNODE_OPS9, /* NTOOV */ |
| 8551 | COPYNODE_OPS10, /* NTOFD */ |
| 8552 | COPYNODE_OPS10, /* NFROMFD */ |
| 8553 | COPYNODE_OPS11, /* NHERE */ |
| 8554 | COPYNODE_OPS11, /* NXHERE */ |
| 8555 | COPYNODE_OPS12, /* NNOT */ |
| 8556 | }; |
| 8557 | |
| 8558 | #if NODE_CHARPTR != NODE_MBRMASK |
| 8559 | #error NODE_CHARPTR != NODE_MBRMASK!!! |
| 8560 | #endif |
| 8561 | #endif /* defined(CALCSIZE_TABLE) || defined(COPYNODE_TABLE) */ |
| 8562 | |
| 8563 | #ifdef COPYNODE_TABLE |
| 8564 | static union node * |
| 8565 | copynode(const union node *n) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8566 | { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8567 | union node *new; |
| 8568 | const unsigned char *p; |
| 8569 | |
Manuel Novoa III | c639a35 | 2001-08-12 17:32:56 +0000 | [diff] [blame] | 8570 | if (n == NULL) { |
| 8571 | return NULL; |
| 8572 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8573 | new = funcblock; |
| 8574 | new->type = n->type; |
| 8575 | funcblock = (char *) funcblock + (int) nodesize[n->type]; |
| 8576 | p = copynode_ops + (int) copynode_ops_index[n->type]; |
| 8577 | do { |
| 8578 | char *nn = ((char *) new) + ((int)(*p & NODE_OFFSETMASK)); |
| 8579 | const char *no = ((const char *) n) + ((int)(*p & NODE_OFFSETMASK)); |
| 8580 | |
| 8581 | if (!(*p & NODE_MBRMASK)) { /* standard node */ |
Manuel Novoa III | c639a35 | 2001-08-12 17:32:56 +0000 | [diff] [blame] | 8582 | *((union node **)nn) = copynode(*((const union node **) no)); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8583 | } else if ((*p & NODE_MBRMASK) == NODE_CHARPTR) { /* string */ |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 8584 | *((const char **)nn) = nodexstrdup(*((const char **)no)); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8585 | } else if (*p & NODE_NODELIST) { /* nodelist */ |
Manuel Novoa III | c639a35 | 2001-08-12 17:32:56 +0000 | [diff] [blame] | 8586 | *((struct nodelist **)nn) |
| 8587 | = copynodelist(*((const struct nodelist **) no)); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8588 | } else { /* integer */ |
| 8589 | *((int *) nn) = *((int *) no); |
| 8590 | } |
| 8591 | } while (!(*p++ & NODE_NOMORE)); |
| 8592 | return new; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8593 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8594 | #else /* COPYNODE_TABLE */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8595 | static union node * |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8596 | copynode(const union node *n) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8597 | { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8598 | union node *new; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8599 | |
| 8600 | if (n == NULL) |
Manuel Novoa III | c639a35 | 2001-08-12 17:32:56 +0000 | [diff] [blame] | 8601 | return NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8602 | new = funcblock; |
| 8603 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 8604 | switch (n->type) { |
| 8605 | case NSEMI: |
| 8606 | case NAND: |
| 8607 | case NOR: |
| 8608 | case NWHILE: |
| 8609 | case NUNTIL: |
| 8610 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 8611 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 8612 | break; |
| 8613 | case NCMD: |
| 8614 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 8615 | new->ncmd.args = copynode(n->ncmd.args); |
| 8616 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 8617 | new->ncmd.backgnd = n->ncmd.backgnd; |
| 8618 | break; |
| 8619 | case NPIPE: |
| 8620 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
| 8621 | new->npipe.backgnd = n->npipe.backgnd; |
| 8622 | break; |
| 8623 | case NREDIR: |
| 8624 | case NBACKGND: |
| 8625 | case NSUBSHELL: |
| 8626 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 8627 | new->nredir.n = copynode(n->nredir.n); |
| 8628 | break; |
| 8629 | case NIF: |
| 8630 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 8631 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 8632 | new->nif.test = copynode(n->nif.test); |
| 8633 | break; |
| 8634 | case NFOR: |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 8635 | new->nfor.var = nodexstrdup(n->nfor.var); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8636 | new->nfor.body = copynode(n->nfor.body); |
| 8637 | new->nfor.args = copynode(n->nfor.args); |
| 8638 | break; |
| 8639 | case NCASE: |
| 8640 | new->ncase.cases = copynode(n->ncase.cases); |
| 8641 | new->ncase.expr = copynode(n->ncase.expr); |
| 8642 | break; |
| 8643 | case NCLIST: |
| 8644 | new->nclist.body = copynode(n->nclist.body); |
| 8645 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 8646 | new->nclist.next = copynode(n->nclist.next); |
| 8647 | break; |
| 8648 | case NDEFUN: |
| 8649 | case NARG: |
| 8650 | new->narg.backquote = copynodelist(n->narg.backquote); |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 8651 | new->narg.text = nodexstrdup(n->narg.text); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8652 | new->narg.next = copynode(n->narg.next); |
| 8653 | break; |
| 8654 | case NTO: |
| 8655 | case NFROM: |
| 8656 | case NFROMTO: |
| 8657 | case NAPPEND: |
| 8658 | case NTOOV: |
| 8659 | new->nfile.fname = copynode(n->nfile.fname); |
| 8660 | new->nfile.fd = n->nfile.fd; |
| 8661 | new->nfile.next = copynode(n->nfile.next); |
| 8662 | break; |
| 8663 | case NTOFD: |
| 8664 | case NFROMFD: |
| 8665 | new->ndup.vname = copynode(n->ndup.vname); |
| 8666 | new->ndup.dupfd = n->ndup.dupfd; |
| 8667 | new->ndup.fd = n->ndup.fd; |
| 8668 | new->ndup.next = copynode(n->ndup.next); |
| 8669 | break; |
| 8670 | case NHERE: |
| 8671 | case NXHERE: |
| 8672 | new->nhere.doc = copynode(n->nhere.doc); |
| 8673 | new->nhere.fd = n->nhere.fd; |
| 8674 | new->nhere.next = copynode(n->nhere.next); |
| 8675 | break; |
| 8676 | case NNOT: |
| 8677 | new->nnot.com = copynode(n->nnot.com); |
| 8678 | break; |
| 8679 | }; |
| 8680 | new->type = n->type; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8681 | return new; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8682 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8683 | #endif /* COPYNODE_TABLE */ |
| 8684 | |
| 8685 | #ifdef CALCSIZE_TABLE |
| 8686 | static void |
| 8687 | calcsize(const union node *n) |
| 8688 | { |
| 8689 | const unsigned char *p; |
| 8690 | |
| 8691 | if (n == NULL) |
| 8692 | return; |
| 8693 | funcblocksize += (int) nodesize[n->type]; |
| 8694 | |
| 8695 | p = copynode_ops + (int) copynode_ops_index[n->type]; |
| 8696 | do { |
| 8697 | const char *no = ((const char *) n) + ((int)(*p & NODE_OFFSETMASK)); |
| 8698 | |
| 8699 | if (!(*p & NODE_MBRMASK)) { /* standard node */ |
Manuel Novoa III | c639a35 | 2001-08-12 17:32:56 +0000 | [diff] [blame] | 8700 | calcsize(*((const union node **) no)); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8701 | } else if ((*p & NODE_MBRMASK) == NODE_CHARPTR) { /* string */ |
Manuel Novoa III | c639a35 | 2001-08-12 17:32:56 +0000 | [diff] [blame] | 8702 | funcstringsize += strlen(*((const char **)no)) + 1; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8703 | } else if (*p & NODE_NODELIST) { /* nodelist */ |
Manuel Novoa III | c639a35 | 2001-08-12 17:32:56 +0000 | [diff] [blame] | 8704 | sizenodelist(*((const struct nodelist **) no)); |
| 8705 | } /* else integer -- ignore */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8706 | } while (!(*p++ & NODE_NOMORE)); |
| 8707 | } |
| 8708 | #else /* CALCSIZE_TABLE */ |
| 8709 | static void |
| 8710 | calcsize(const union node *n) |
| 8711 | { |
| 8712 | if (n == NULL) |
| 8713 | return; |
| 8714 | funcblocksize += nodesize[n->type]; |
| 8715 | switch (n->type) { |
| 8716 | case NSEMI: |
| 8717 | case NAND: |
| 8718 | case NOR: |
| 8719 | case NWHILE: |
| 8720 | case NUNTIL: |
| 8721 | calcsize(n->nbinary.ch2); |
| 8722 | calcsize(n->nbinary.ch1); |
| 8723 | break; |
| 8724 | case NCMD: |
| 8725 | calcsize(n->ncmd.redirect); |
| 8726 | calcsize(n->ncmd.args); |
| 8727 | calcsize(n->ncmd.assign); |
| 8728 | break; |
| 8729 | case NPIPE: |
| 8730 | sizenodelist(n->npipe.cmdlist); |
| 8731 | break; |
| 8732 | case NREDIR: |
| 8733 | case NBACKGND: |
| 8734 | case NSUBSHELL: |
| 8735 | calcsize(n->nredir.redirect); |
| 8736 | calcsize(n->nredir.n); |
| 8737 | break; |
| 8738 | case NIF: |
| 8739 | calcsize(n->nif.elsepart); |
| 8740 | calcsize(n->nif.ifpart); |
| 8741 | calcsize(n->nif.test); |
| 8742 | break; |
| 8743 | case NFOR: |
| 8744 | funcstringsize += strlen(n->nfor.var) + 1; |
| 8745 | calcsize(n->nfor.body); |
| 8746 | calcsize(n->nfor.args); |
| 8747 | break; |
| 8748 | case NCASE: |
| 8749 | calcsize(n->ncase.cases); |
| 8750 | calcsize(n->ncase.expr); |
| 8751 | break; |
| 8752 | case NCLIST: |
| 8753 | calcsize(n->nclist.body); |
| 8754 | calcsize(n->nclist.pattern); |
| 8755 | calcsize(n->nclist.next); |
| 8756 | break; |
| 8757 | case NDEFUN: |
| 8758 | case NARG: |
| 8759 | sizenodelist(n->narg.backquote); |
| 8760 | funcstringsize += strlen(n->narg.text) + 1; |
| 8761 | calcsize(n->narg.next); |
| 8762 | break; |
| 8763 | case NTO: |
| 8764 | case NFROM: |
| 8765 | case NFROMTO: |
| 8766 | case NAPPEND: |
| 8767 | case NTOOV: |
| 8768 | calcsize(n->nfile.fname); |
| 8769 | calcsize(n->nfile.next); |
| 8770 | break; |
| 8771 | case NTOFD: |
| 8772 | case NFROMFD: |
| 8773 | calcsize(n->ndup.vname); |
| 8774 | calcsize(n->ndup.next); |
| 8775 | break; |
| 8776 | case NHERE: |
| 8777 | case NXHERE: |
| 8778 | calcsize(n->nhere.doc); |
| 8779 | calcsize(n->nhere.next); |
| 8780 | break; |
| 8781 | case NNOT: |
| 8782 | calcsize(n->nnot.com); |
| 8783 | break; |
| 8784 | }; |
| 8785 | } |
| 8786 | #endif /* CALCSIZE_TABLE */ |
| 8787 | |
| 8788 | static void |
| 8789 | sizenodelist(const struct nodelist *lp) |
| 8790 | { |
| 8791 | while (lp) { |
| 8792 | funcblocksize += ALIGN(sizeof(struct nodelist)); |
| 8793 | calcsize(lp->n); |
| 8794 | lp = lp->next; |
| 8795 | } |
| 8796 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8797 | |
| 8798 | |
| 8799 | static struct nodelist * |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8800 | copynodelist(const struct nodelist *lp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8801 | { |
| 8802 | struct nodelist *start; |
| 8803 | struct nodelist **lpp; |
| 8804 | |
| 8805 | lpp = &start; |
| 8806 | while (lp) { |
| 8807 | *lpp = funcblock; |
| 8808 | funcblock = (char *) funcblock + ALIGN(sizeof(struct nodelist)); |
| 8809 | (*lpp)->n = copynode(lp->n); |
| 8810 | lp = lp->next; |
| 8811 | lpp = &(*lpp)->next; |
| 8812 | } |
| 8813 | *lpp = NULL; |
| 8814 | return start; |
| 8815 | } |
| 8816 | |
| 8817 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8818 | static char * |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 8819 | nodexstrdup(const char *s) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8820 | { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8821 | const char *p = s; |
| 8822 | char *q = funcstring; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8823 | char *rtn = funcstring; |
| 8824 | |
| 8825 | while ((*q++ = *p++) != '\0') |
| 8826 | continue; |
| 8827 | funcstring = q; |
| 8828 | return rtn; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8829 | } |
| 8830 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 8831 | #ifdef CONFIG_ASH_GETOPTS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8832 | static int getopts (char *, char *, char **, int *, int *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8833 | #endif |
| 8834 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8835 | /* |
| 8836 | * Process the shell command line arguments. |
| 8837 | */ |
| 8838 | |
| 8839 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 8840 | procargs(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8841 | { |
| 8842 | int i; |
| 8843 | |
| 8844 | argptr = argv; |
| 8845 | if (argc > 0) |
| 8846 | argptr++; |
| 8847 | for (i = 0; i < NOPTS; i++) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8848 | optent_val(i) = 2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8849 | options(1); |
| 8850 | if (*argptr == NULL && minusc == NULL) |
| 8851 | sflag = 1; |
| 8852 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 8853 | iflag = 1; |
| 8854 | if (mflag == 2) |
| 8855 | mflag = iflag; |
| 8856 | for (i = 0; i < NOPTS; i++) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8857 | if (optent_val(i) == 2) |
| 8858 | optent_val(i) = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8859 | arg0 = argv[0]; |
| 8860 | if (sflag == 0 && minusc == NULL) { |
| 8861 | commandname = argv[0]; |
| 8862 | arg0 = *argptr++; |
| 8863 | setinputfile(arg0, 0); |
| 8864 | commandname = arg0; |
| 8865 | } |
| 8866 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 8867 | if (argptr && minusc && *argptr) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8868 | arg0 = *argptr++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8869 | |
| 8870 | shellparam.p = argptr; |
| 8871 | shellparam.optind = 1; |
| 8872 | shellparam.optoff = -1; |
| 8873 | /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ |
| 8874 | while (*argptr) { |
| 8875 | shellparam.nparam++; |
| 8876 | argptr++; |
| 8877 | } |
| 8878 | optschanged(); |
| 8879 | } |
| 8880 | |
| 8881 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8882 | |
| 8883 | /* |
| 8884 | * Process shell options. The global variable argptr contains a pointer |
| 8885 | * to the argument list; we advance it past the options. |
| 8886 | */ |
| 8887 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8888 | static inline void |
| 8889 | minus_o(const char *name, int val) |
| 8890 | { |
| 8891 | int i; |
| 8892 | |
| 8893 | if (name == NULL) { |
| 8894 | out1str("Current option settings\n"); |
| 8895 | for (i = 0; i < NOPTS; i++) |
| 8896 | printf("%-16s%s\n", optent_name(optlist[i]), |
| 8897 | optent_val(i) ? "on" : "off"); |
| 8898 | } else { |
| 8899 | for (i = 0; i < NOPTS; i++) |
| 8900 | if (equal(name, optent_name(optlist[i]))) { |
| 8901 | setoption(optent_letter(optlist[i]), val); |
| 8902 | return; |
| 8903 | } |
| 8904 | error("Illegal option -o %s", name); |
| 8905 | } |
| 8906 | } |
| 8907 | |
| 8908 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8909 | static void |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8910 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8911 | { |
| 8912 | char *p; |
| 8913 | int val; |
| 8914 | int c; |
| 8915 | |
| 8916 | if (cmdline) |
| 8917 | minusc = NULL; |
| 8918 | while ((p = *argptr) != NULL) { |
| 8919 | argptr++; |
| 8920 | if ((c = *p++) == '-') { |
| 8921 | val = 1; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8922 | if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) { |
| 8923 | if (!cmdline) { |
| 8924 | /* "-" means turn off -x and -v */ |
| 8925 | if (p[0] == '\0') |
| 8926 | xflag = vflag = 0; |
| 8927 | /* "--" means reset params */ |
| 8928 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8929 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8930 | } |
| 8931 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8932 | } |
| 8933 | } else if (c == '+') { |
| 8934 | val = 0; |
| 8935 | } else { |
| 8936 | argptr--; |
| 8937 | break; |
| 8938 | } |
| 8939 | while ((c = *p++) != '\0') { |
| 8940 | if (c == 'c' && cmdline) { |
| 8941 | char *q; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8942 | #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8943 | if (*p == '\0') |
| 8944 | #endif |
| 8945 | q = *argptr++; |
| 8946 | if (q == NULL || minusc != NULL) |
| 8947 | error("Bad -c option"); |
| 8948 | minusc = q; |
| 8949 | #ifdef NOHACK |
| 8950 | break; |
| 8951 | #endif |
| 8952 | } else if (c == 'o') { |
| 8953 | minus_o(*argptr, val); |
| 8954 | if (*argptr) |
| 8955 | argptr++; |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 8956 | } else if (cmdline && (c == '-')) { // long options |
| 8957 | if ( strcmp ( p, "login" ) == 0 ) |
| 8958 | isloginsh = 1; |
| 8959 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8960 | } else { |
| 8961 | setoption(c, val); |
| 8962 | } |
| 8963 | } |
| 8964 | } |
| 8965 | } |
| 8966 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8967 | |
| 8968 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8969 | setoption(int flag, int val) |
| 8970 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8971 | int i; |
| 8972 | |
| 8973 | for (i = 0; i < NOPTS; i++) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8974 | if (optent_letter(optlist[i]) == flag) { |
| 8975 | optent_val(i) = val; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8976 | if (val) { |
| 8977 | /* #%$ hack for ksh semantics */ |
| 8978 | if (flag == 'V') |
| 8979 | Eflag = 0; |
| 8980 | else if (flag == 'E') |
| 8981 | Vflag = 0; |
| 8982 | } |
| 8983 | return; |
| 8984 | } |
| 8985 | error("Illegal option -%c", flag); |
| 8986 | /* NOTREACHED */ |
| 8987 | } |
| 8988 | |
| 8989 | |
| 8990 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8991 | /* |
| 8992 | * Set the shell parameters. |
| 8993 | */ |
| 8994 | |
| 8995 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8996 | setparam(char **argv) |
| 8997 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8998 | char **newparam; |
| 8999 | char **ap; |
| 9000 | int nparam; |
| 9001 | |
| 9002 | for (nparam = 0 ; argv[nparam] ; nparam++); |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 9003 | ap = newparam = xmalloc((nparam + 1) * sizeof *ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9004 | while (*argv) { |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 9005 | *ap++ = xstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9006 | } |
| 9007 | *ap = NULL; |
| 9008 | freeparam(&shellparam); |
| 9009 | shellparam.malloc = 1; |
| 9010 | shellparam.nparam = nparam; |
| 9011 | shellparam.p = newparam; |
| 9012 | shellparam.optind = 1; |
| 9013 | shellparam.optoff = -1; |
| 9014 | } |
| 9015 | |
| 9016 | |
| 9017 | /* |
| 9018 | * Free the list of positional parameters. |
| 9019 | */ |
| 9020 | |
| 9021 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9022 | freeparam(volatile struct shparam *param) |
| 9023 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9024 | char **ap; |
| 9025 | |
| 9026 | if (param->malloc) { |
| 9027 | for (ap = param->p ; *ap ; ap++) |
| 9028 | ckfree(*ap); |
| 9029 | ckfree(param->p); |
| 9030 | } |
| 9031 | } |
| 9032 | |
| 9033 | |
| 9034 | |
| 9035 | /* |
| 9036 | * The shift builtin command. |
| 9037 | */ |
| 9038 | |
| 9039 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 9040 | shiftcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9041 | { |
| 9042 | int n; |
| 9043 | char **ap1, **ap2; |
| 9044 | |
| 9045 | n = 1; |
| 9046 | if (argc > 1) |
| 9047 | n = number(argv[1]); |
| 9048 | if (n > shellparam.nparam) |
| 9049 | error("can't shift that many"); |
| 9050 | INTOFF; |
| 9051 | shellparam.nparam -= n; |
| 9052 | for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { |
| 9053 | if (shellparam.malloc) |
| 9054 | ckfree(*ap1); |
| 9055 | } |
| 9056 | ap2 = shellparam.p; |
| 9057 | while ((*ap2++ = *ap1++) != NULL); |
| 9058 | shellparam.optind = 1; |
| 9059 | shellparam.optoff = -1; |
| 9060 | INTON; |
| 9061 | return 0; |
| 9062 | } |
| 9063 | |
| 9064 | |
| 9065 | |
| 9066 | /* |
| 9067 | * The set command builtin. |
| 9068 | */ |
| 9069 | |
| 9070 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 9071 | setcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9072 | { |
| 9073 | if (argc == 1) |
| 9074 | return showvarscmd(argc, argv); |
| 9075 | INTOFF; |
| 9076 | options(0); |
| 9077 | optschanged(); |
| 9078 | if (*argptr != NULL) { |
| 9079 | setparam(argptr); |
| 9080 | } |
| 9081 | INTON; |
| 9082 | return 0; |
| 9083 | } |
| 9084 | |
| 9085 | |
| 9086 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9087 | getoptsreset(const char *value) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9088 | { |
| 9089 | shellparam.optind = number(value); |
| 9090 | shellparam.optoff = -1; |
| 9091 | } |
| 9092 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 9093 | #ifdef CONFIG_LOCALE_SUPPORT |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9094 | static void change_lc_all(const char *value) |
| 9095 | { |
| 9096 | if(value != 0 && *value != 0) |
| 9097 | setlocale(LC_ALL, value); |
| 9098 | } |
| 9099 | |
| 9100 | static void change_lc_ctype(const char *value) |
| 9101 | { |
| 9102 | if(value != 0 && *value != 0) |
| 9103 | setlocale(LC_CTYPE, value); |
| 9104 | } |
| 9105 | |
| 9106 | #endif |
| 9107 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 9108 | #ifdef CONFIG_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9109 | /* |
| 9110 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 9111 | * to be processed. Shellparam.optptr points to the next character to |
| 9112 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 9113 | * then it's the first time getopts has been called. |
| 9114 | */ |
| 9115 | |
| 9116 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 9117 | getoptscmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9118 | { |
| 9119 | char **optbase; |
| 9120 | |
| 9121 | if (argc < 3) |
| 9122 | error("Usage: getopts optstring var [arg]"); |
| 9123 | else if (argc == 3) { |
| 9124 | optbase = shellparam.p; |
| 9125 | if (shellparam.optind > shellparam.nparam + 1) { |
| 9126 | shellparam.optind = 1; |
| 9127 | shellparam.optoff = -1; |
| 9128 | } |
| 9129 | } |
| 9130 | else { |
| 9131 | optbase = &argv[3]; |
| 9132 | if (shellparam.optind > argc - 2) { |
| 9133 | shellparam.optind = 1; |
| 9134 | shellparam.optoff = -1; |
| 9135 | } |
| 9136 | } |
| 9137 | |
| 9138 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
| 9139 | &shellparam.optoff); |
| 9140 | } |
| 9141 | |
| 9142 | /* |
| 9143 | * Safe version of setvar, returns 1 on success 0 on failure. |
| 9144 | */ |
| 9145 | |
| 9146 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 9147 | setvarsafe(const char *name, const char *val, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9148 | { |
| 9149 | struct jmploc jmploc; |
| 9150 | struct jmploc *volatile savehandler = handler; |
| 9151 | int err = 0; |
| 9152 | #ifdef __GNUC__ |
| 9153 | (void) &err; |
| 9154 | #endif |
| 9155 | |
| 9156 | if (setjmp(jmploc.loc)) |
| 9157 | err = 1; |
| 9158 | else { |
| 9159 | handler = &jmploc; |
| 9160 | setvar(name, val, flags); |
| 9161 | } |
| 9162 | handler = savehandler; |
| 9163 | return err; |
| 9164 | } |
| 9165 | |
| 9166 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 9167 | getopts(char *optstr, char *optvar, char **optfirst, int *myoptind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9168 | { |
| 9169 | char *p, *q; |
| 9170 | char c = '?'; |
| 9171 | int done = 0; |
| 9172 | int err = 0; |
| 9173 | char s[10]; |
| 9174 | char **optnext = optfirst + *myoptind - 1; |
| 9175 | |
| 9176 | if (*myoptind <= 1 || *optoff < 0 || !(*(optnext - 1)) || |
| 9177 | strlen(*(optnext - 1)) < *optoff) |
| 9178 | p = NULL; |
| 9179 | else |
| 9180 | p = *(optnext - 1) + *optoff; |
| 9181 | if (p == NULL || *p == '\0') { |
| 9182 | /* Current word is done, advance */ |
| 9183 | if (optnext == NULL) |
| 9184 | return 1; |
| 9185 | p = *optnext; |
| 9186 | if (p == NULL || *p != '-' || *++p == '\0') { |
| 9187 | atend: |
| 9188 | *myoptind = optnext - optfirst + 1; |
| 9189 | p = NULL; |
| 9190 | done = 1; |
| 9191 | goto out; |
| 9192 | } |
| 9193 | optnext++; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9194 | if (p[0] == '-' && p[1] == '\0') /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9195 | goto atend; |
| 9196 | } |
| 9197 | |
| 9198 | c = *p++; |
| 9199 | for (q = optstr; *q != c; ) { |
| 9200 | if (*q == '\0') { |
| 9201 | if (optstr[0] == ':') { |
| 9202 | s[0] = c; |
| 9203 | s[1] = '\0'; |
| 9204 | err |= setvarsafe("OPTARG", s, 0); |
| 9205 | } |
| 9206 | else { |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 9207 | out2fmt("Illegal option -%c\n", c); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9208 | (void) unsetvar("OPTARG"); |
| 9209 | } |
| 9210 | c = '?'; |
| 9211 | goto bad; |
| 9212 | } |
| 9213 | if (*++q == ':') |
| 9214 | q++; |
| 9215 | } |
| 9216 | |
| 9217 | if (*++q == ':') { |
| 9218 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 9219 | if (optstr[0] == ':') { |
| 9220 | s[0] = c; |
| 9221 | s[1] = '\0'; |
| 9222 | err |= setvarsafe("OPTARG", s, 0); |
| 9223 | c = ':'; |
| 9224 | } |
| 9225 | else { |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 9226 | out2fmt("No arg for -%c option\n", c); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9227 | (void) unsetvar("OPTARG"); |
| 9228 | c = '?'; |
| 9229 | } |
| 9230 | goto bad; |
| 9231 | } |
| 9232 | |
| 9233 | if (p == *optnext) |
| 9234 | optnext++; |
| 9235 | setvarsafe("OPTARG", p, 0); |
| 9236 | p = NULL; |
| 9237 | } |
| 9238 | else |
| 9239 | setvarsafe("OPTARG", "", 0); |
| 9240 | *myoptind = optnext - optfirst + 1; |
| 9241 | goto out; |
| 9242 | |
| 9243 | bad: |
| 9244 | *myoptind = 1; |
| 9245 | p = NULL; |
| 9246 | out: |
| 9247 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 9248 | snprintf(s, sizeof(s), "%d", *myoptind); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9249 | err |= setvarsafe("OPTIND", s, VNOFUNC); |
| 9250 | s[0] = c; |
| 9251 | s[1] = '\0'; |
| 9252 | err |= setvarsafe(optvar, s, 0); |
| 9253 | if (err) { |
| 9254 | *myoptind = 1; |
| 9255 | *optoff = -1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9256 | exraise(EXERROR); |
| 9257 | } |
| 9258 | return done; |
| 9259 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9260 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9261 | |
| 9262 | /* |
| 9263 | * XXX - should get rid of. have all builtins use getopt(3). the |
| 9264 | * library getopt must have the BSD extension static variable "optreset" |
| 9265 | * otherwise it can't be used within the shell safely. |
| 9266 | * |
| 9267 | * Standard option processing (a la getopt) for builtin routines. The |
| 9268 | * only argument that is passed to nextopt is the option string; the |
| 9269 | * other arguments are unnecessary. It return the character, or '\0' on |
| 9270 | * end of input. |
| 9271 | */ |
| 9272 | |
| 9273 | static int |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9274 | nextopt(const char *optstring) |
| 9275 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9276 | char *p; |
| 9277 | const char *q; |
| 9278 | char c; |
| 9279 | |
| 9280 | if ((p = optptr) == NULL || *p == '\0') { |
| 9281 | p = *argptr; |
| 9282 | if (p == NULL || *p != '-' || *++p == '\0') |
| 9283 | return '\0'; |
| 9284 | argptr++; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9285 | if (p[0] == '-' && p[1] == '\0') /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9286 | return '\0'; |
| 9287 | } |
| 9288 | c = *p++; |
| 9289 | for (q = optstring ; *q != c ; ) { |
| 9290 | if (*q == '\0') |
| 9291 | error("Illegal option -%c", c); |
| 9292 | if (*++q == ':') |
| 9293 | q++; |
| 9294 | } |
| 9295 | if (*++q == ':') { |
| 9296 | if (*p == '\0' && (p = *argptr++) == NULL) |
| 9297 | error("No arg for -%c option", c); |
| 9298 | optionarg = p; |
| 9299 | p = NULL; |
| 9300 | } |
| 9301 | optptr = p; |
| 9302 | return c; |
| 9303 | } |
| 9304 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9305 | static void |
| 9306 | flushall() { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9307 | INTOFF; |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 9308 | fflush(stdout); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9309 | INTON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9310 | } |
| 9311 | |
| 9312 | |
| 9313 | static void |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 9314 | out2fmt(const char *fmt, ...) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9315 | { |
| 9316 | va_list ap; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9317 | va_start(ap, fmt); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 9318 | vfprintf(stderr, fmt, ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9319 | va_end(ap); |
| 9320 | } |
| 9321 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9322 | /* |
| 9323 | * Version of write which resumes after a signal is caught. |
| 9324 | */ |
| 9325 | |
| 9326 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9327 | xwrite(int fd, const char *buf, int nbytes) |
| 9328 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9329 | int ntry; |
| 9330 | int i; |
| 9331 | int n; |
| 9332 | |
| 9333 | n = nbytes; |
| 9334 | ntry = 0; |
| 9335 | for (;;) { |
| 9336 | i = write(fd, buf, n); |
| 9337 | if (i > 0) { |
| 9338 | if ((n -= i) <= 0) |
| 9339 | return nbytes; |
| 9340 | buf += i; |
| 9341 | ntry = 0; |
| 9342 | } else if (i == 0) { |
| 9343 | if (++ntry > 10) |
| 9344 | return nbytes - n; |
| 9345 | } else if (errno != EINTR) { |
| 9346 | return -1; |
| 9347 | } |
| 9348 | } |
| 9349 | } |
| 9350 | |
| 9351 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9352 | /* |
| 9353 | * Shell command parser. |
| 9354 | */ |
| 9355 | |
| 9356 | #define EOFMARKLEN 79 |
| 9357 | |
| 9358 | |
| 9359 | |
| 9360 | struct heredoc { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9361 | struct heredoc *next; /* next here document in list */ |
| 9362 | union node *here; /* redirection node */ |
| 9363 | char *eofmark; /* string indicating end of input */ |
| 9364 | int striptabs; /* if set, strip leading tabs */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9365 | }; |
| 9366 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9367 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 9368 | static int parsebackquote; /* nonzero if we are inside backquotes */ |
| 9369 | static int doprompt; /* if set, prompt the user */ |
| 9370 | static int needprompt; /* true if interactive and at start of line */ |
| 9371 | static int lasttoken; /* last token read */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9372 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9373 | static char *wordtext; /* text of last word returned by readtoken */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9374 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9375 | static struct nodelist *backquotelist; |
| 9376 | static union node *redirnode; |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 9377 | static struct heredoc *heredoc; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9378 | static int quoteflag; /* set if (part of) last token was quoted */ |
| 9379 | static int startlinno; /* line # where last token started */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9380 | |
| 9381 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9382 | static union node *list (int); |
| 9383 | static union node *andor (void); |
| 9384 | static union node *pipeline (void); |
| 9385 | static union node *command (void); |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 9386 | static union node *simplecmd(union node **rpp, union node *redir); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9387 | static void parsefname (void); |
| 9388 | static void parseheredoc (void); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 9389 | static char peektoken (void); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9390 | static int readtoken (void); |
| 9391 | static int xxreadtoken (void); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 9392 | static int readtoken1 (int, int, const char *, int); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9393 | static int noexpand (char *); |
| 9394 | static void synexpect (int) __attribute__((noreturn)); |
| 9395 | static void synerror (const char *) __attribute__((noreturn)); |
| 9396 | static void setprompt (int); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9397 | |
| 9398 | |
| 9399 | /* |
| 9400 | * Read and parse a command. Returns NEOF on end of file. (NULL is a |
| 9401 | * valid parse tree indicating a blank line.) |
| 9402 | */ |
| 9403 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9404 | static union node * |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9405 | parsecmd(int interact) |
| 9406 | { |
| 9407 | int t; |
| 9408 | |
| 9409 | tokpushback = 0; |
| 9410 | doprompt = interact; |
| 9411 | if (doprompt) |
| 9412 | setprompt(1); |
| 9413 | else |
| 9414 | setprompt(0); |
| 9415 | needprompt = 0; |
| 9416 | t = readtoken(); |
| 9417 | if (t == TEOF) |
| 9418 | return NEOF; |
| 9419 | if (t == TNL) |
| 9420 | return NULL; |
| 9421 | tokpushback++; |
| 9422 | return list(1); |
| 9423 | } |
| 9424 | |
| 9425 | |
| 9426 | static union node * |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 9427 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9428 | { |
| 9429 | union node *n1, *n2, *n3; |
| 9430 | int tok; |
| 9431 | |
| 9432 | checkkwd = 2; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 9433 | if (nlflag == 0 && peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9434 | return NULL; |
| 9435 | n1 = NULL; |
| 9436 | for (;;) { |
| 9437 | n2 = andor(); |
| 9438 | tok = readtoken(); |
| 9439 | if (tok == TBACKGND) { |
| 9440 | if (n2->type == NCMD || n2->type == NPIPE) { |
| 9441 | n2->ncmd.backgnd = 1; |
| 9442 | } else if (n2->type == NREDIR) { |
| 9443 | n2->type = NBACKGND; |
| 9444 | } else { |
| 9445 | n3 = (union node *)stalloc(sizeof (struct nredir)); |
| 9446 | n3->type = NBACKGND; |
| 9447 | n3->nredir.n = n2; |
| 9448 | n3->nredir.redirect = NULL; |
| 9449 | n2 = n3; |
| 9450 | } |
| 9451 | } |
| 9452 | if (n1 == NULL) { |
| 9453 | n1 = n2; |
| 9454 | } |
| 9455 | else { |
| 9456 | n3 = (union node *)stalloc(sizeof (struct nbinary)); |
| 9457 | n3->type = NSEMI; |
| 9458 | n3->nbinary.ch1 = n1; |
| 9459 | n3->nbinary.ch2 = n2; |
| 9460 | n1 = n3; |
| 9461 | } |
| 9462 | switch (tok) { |
| 9463 | case TBACKGND: |
| 9464 | case TSEMI: |
| 9465 | tok = readtoken(); |
| 9466 | /* fall through */ |
| 9467 | case TNL: |
| 9468 | if (tok == TNL) { |
| 9469 | parseheredoc(); |
| 9470 | if (nlflag) |
| 9471 | return n1; |
| 9472 | } else { |
| 9473 | tokpushback++; |
| 9474 | } |
| 9475 | checkkwd = 2; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 9476 | if (peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9477 | return n1; |
| 9478 | break; |
| 9479 | case TEOF: |
| 9480 | if (heredoclist) |
| 9481 | parseheredoc(); |
| 9482 | else |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9483 | pungetc(); /* push back EOF on input */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9484 | return n1; |
| 9485 | default: |
| 9486 | if (nlflag) |
| 9487 | synexpect(-1); |
| 9488 | tokpushback++; |
| 9489 | return n1; |
| 9490 | } |
| 9491 | } |
| 9492 | } |
| 9493 | |
| 9494 | |
| 9495 | |
| 9496 | static union node * |
| 9497 | andor() { |
| 9498 | union node *n1, *n2, *n3; |
| 9499 | int t; |
| 9500 | |
| 9501 | checkkwd = 1; |
| 9502 | n1 = pipeline(); |
| 9503 | for (;;) { |
| 9504 | if ((t = readtoken()) == TAND) { |
| 9505 | t = NAND; |
| 9506 | } else if (t == TOR) { |
| 9507 | t = NOR; |
| 9508 | } else { |
| 9509 | tokpushback++; |
| 9510 | return n1; |
| 9511 | } |
| 9512 | checkkwd = 2; |
| 9513 | n2 = pipeline(); |
| 9514 | n3 = (union node *)stalloc(sizeof (struct nbinary)); |
| 9515 | n3->type = t; |
| 9516 | n3->nbinary.ch1 = n1; |
| 9517 | n3->nbinary.ch2 = n2; |
| 9518 | n1 = n3; |
| 9519 | } |
| 9520 | } |
| 9521 | |
| 9522 | |
| 9523 | |
| 9524 | static union node * |
| 9525 | pipeline() { |
| 9526 | union node *n1, *n2, *pipenode; |
| 9527 | struct nodelist *lp, *prev; |
| 9528 | int negate; |
| 9529 | |
| 9530 | negate = 0; |
| 9531 | TRACE(("pipeline: entered\n")); |
| 9532 | if (readtoken() == TNOT) { |
| 9533 | negate = !negate; |
| 9534 | checkkwd = 1; |
| 9535 | } else |
| 9536 | tokpushback++; |
| 9537 | n1 = command(); |
| 9538 | if (readtoken() == TPIPE) { |
| 9539 | pipenode = (union node *)stalloc(sizeof (struct npipe)); |
| 9540 | pipenode->type = NPIPE; |
| 9541 | pipenode->npipe.backgnd = 0; |
| 9542 | lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); |
| 9543 | pipenode->npipe.cmdlist = lp; |
| 9544 | lp->n = n1; |
| 9545 | do { |
| 9546 | prev = lp; |
| 9547 | lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); |
| 9548 | checkkwd = 2; |
| 9549 | lp->n = command(); |
| 9550 | prev->next = lp; |
| 9551 | } while (readtoken() == TPIPE); |
| 9552 | lp->next = NULL; |
| 9553 | n1 = pipenode; |
| 9554 | } |
| 9555 | tokpushback++; |
| 9556 | if (negate) { |
| 9557 | n2 = (union node *)stalloc(sizeof (struct nnot)); |
| 9558 | n2->type = NNOT; |
| 9559 | n2->nnot.com = n1; |
| 9560 | return n2; |
| 9561 | } else |
| 9562 | return n1; |
| 9563 | } |
| 9564 | |
| 9565 | |
| 9566 | |
| 9567 | static union node * |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 9568 | command(void) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9569 | union node *n1, *n2; |
| 9570 | union node *ap, **app; |
| 9571 | union node *cp, **cpp; |
| 9572 | union node *redir, **rpp; |
| 9573 | int t; |
| 9574 | |
| 9575 | redir = NULL; |
| 9576 | n1 = NULL; |
| 9577 | rpp = &redir; |
| 9578 | |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 9579 | /* Check for redirection which may precede command */ |
| 9580 | while (readtoken() == TREDIR) { |
| 9581 | *rpp = n2 = redirnode; |
| 9582 | rpp = &n2->nfile.next; |
| 9583 | parsefname(); |
| 9584 | } |
| 9585 | tokpushback++; |
| 9586 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9587 | switch (readtoken()) { |
| 9588 | case TIF: |
| 9589 | n1 = (union node *)stalloc(sizeof (struct nif)); |
| 9590 | n1->type = NIF; |
| 9591 | n1->nif.test = list(0); |
| 9592 | if (readtoken() != TTHEN) |
| 9593 | synexpect(TTHEN); |
| 9594 | n1->nif.ifpart = list(0); |
| 9595 | n2 = n1; |
| 9596 | while (readtoken() == TELIF) { |
| 9597 | n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); |
| 9598 | n2 = n2->nif.elsepart; |
| 9599 | n2->type = NIF; |
| 9600 | n2->nif.test = list(0); |
| 9601 | if (readtoken() != TTHEN) |
| 9602 | synexpect(TTHEN); |
| 9603 | n2->nif.ifpart = list(0); |
| 9604 | } |
| 9605 | if (lasttoken == TELSE) |
| 9606 | n2->nif.elsepart = list(0); |
| 9607 | else { |
| 9608 | n2->nif.elsepart = NULL; |
| 9609 | tokpushback++; |
| 9610 | } |
| 9611 | if (readtoken() != TFI) |
| 9612 | synexpect(TFI); |
| 9613 | checkkwd = 1; |
| 9614 | break; |
| 9615 | case TWHILE: |
| 9616 | case TUNTIL: { |
| 9617 | int got; |
| 9618 | n1 = (union node *)stalloc(sizeof (struct nbinary)); |
| 9619 | n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; |
| 9620 | n1->nbinary.ch1 = list(0); |
| 9621 | if ((got=readtoken()) != TDO) { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 9622 | TRACE(("expecting DO got %s %s\n", tokname(got), got == TWORD ? wordtext : "")); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9623 | synexpect(TDO); |
| 9624 | } |
| 9625 | n1->nbinary.ch2 = list(0); |
| 9626 | if (readtoken() != TDONE) |
| 9627 | synexpect(TDONE); |
| 9628 | checkkwd = 1; |
| 9629 | break; |
| 9630 | } |
| 9631 | case TFOR: |
| 9632 | if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) |
| 9633 | synerror("Bad for loop variable"); |
| 9634 | n1 = (union node *)stalloc(sizeof (struct nfor)); |
| 9635 | n1->type = NFOR; |
| 9636 | n1->nfor.var = wordtext; |
| 9637 | checkkwd = 1; |
| 9638 | if (readtoken() == TIN) { |
| 9639 | app = ≈ |
| 9640 | while (readtoken() == TWORD) { |
| 9641 | n2 = (union node *)stalloc(sizeof (struct narg)); |
| 9642 | n2->type = NARG; |
| 9643 | n2->narg.text = wordtext; |
| 9644 | n2->narg.backquote = backquotelist; |
| 9645 | *app = n2; |
| 9646 | app = &n2->narg.next; |
| 9647 | } |
| 9648 | *app = NULL; |
| 9649 | n1->nfor.args = ap; |
| 9650 | if (lasttoken != TNL && lasttoken != TSEMI) |
| 9651 | synexpect(-1); |
| 9652 | } else { |
| 9653 | static char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, |
| 9654 | '@', '=', '\0'}; |
| 9655 | n2 = (union node *)stalloc(sizeof (struct narg)); |
| 9656 | n2->type = NARG; |
| 9657 | n2->narg.text = argvars; |
| 9658 | n2->narg.backquote = NULL; |
| 9659 | n2->narg.next = NULL; |
| 9660 | n1->nfor.args = n2; |
| 9661 | /* |
| 9662 | * Newline or semicolon here is optional (but note |
| 9663 | * that the original Bourne shell only allowed NL). |
| 9664 | */ |
| 9665 | if (lasttoken != TNL && lasttoken != TSEMI) |
| 9666 | tokpushback++; |
| 9667 | } |
| 9668 | checkkwd = 2; |
| 9669 | if (readtoken() != TDO) |
| 9670 | synexpect(TDO); |
| 9671 | n1->nfor.body = list(0); |
| 9672 | if (readtoken() != TDONE) |
| 9673 | synexpect(TDONE); |
| 9674 | checkkwd = 1; |
| 9675 | break; |
| 9676 | case TCASE: |
| 9677 | n1 = (union node *)stalloc(sizeof (struct ncase)); |
| 9678 | n1->type = NCASE; |
| 9679 | if (readtoken() != TWORD) |
| 9680 | synexpect(TWORD); |
| 9681 | n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); |
| 9682 | n2->type = NARG; |
| 9683 | n2->narg.text = wordtext; |
| 9684 | n2->narg.backquote = backquotelist; |
| 9685 | n2->narg.next = NULL; |
| 9686 | do { |
| 9687 | checkkwd = 1; |
| 9688 | } while (readtoken() == TNL); |
| 9689 | if (lasttoken != TIN) |
| 9690 | synerror("expecting \"in\""); |
| 9691 | cpp = &n1->ncase.cases; |
| 9692 | checkkwd = 2, readtoken(); |
| 9693 | do { |
| 9694 | if (lasttoken == TLP) |
| 9695 | readtoken(); |
| 9696 | *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); |
| 9697 | cp->type = NCLIST; |
| 9698 | app = &cp->nclist.pattern; |
| 9699 | for (;;) { |
| 9700 | *app = ap = (union node *)stalloc(sizeof (struct narg)); |
| 9701 | ap->type = NARG; |
| 9702 | ap->narg.text = wordtext; |
| 9703 | ap->narg.backquote = backquotelist; |
| 9704 | if (checkkwd = 2, readtoken() != TPIPE) |
| 9705 | break; |
| 9706 | app = &ap->narg.next; |
| 9707 | readtoken(); |
| 9708 | } |
| 9709 | ap->narg.next = NULL; |
| 9710 | if (lasttoken != TRP) |
| 9711 | synexpect(TRP); |
| 9712 | cp->nclist.body = list(0); |
| 9713 | |
| 9714 | checkkwd = 2; |
| 9715 | if ((t = readtoken()) != TESAC) { |
| 9716 | if (t != TENDCASE) |
| 9717 | synexpect(TENDCASE); |
| 9718 | else |
| 9719 | checkkwd = 2, readtoken(); |
| 9720 | } |
| 9721 | cpp = &cp->nclist.next; |
| 9722 | } while(lasttoken != TESAC); |
| 9723 | *cpp = NULL; |
| 9724 | checkkwd = 1; |
| 9725 | break; |
| 9726 | case TLP: |
| 9727 | n1 = (union node *)stalloc(sizeof (struct nredir)); |
| 9728 | n1->type = NSUBSHELL; |
| 9729 | n1->nredir.n = list(0); |
| 9730 | n1->nredir.redirect = NULL; |
| 9731 | if (readtoken() != TRP) |
| 9732 | synexpect(TRP); |
| 9733 | checkkwd = 1; |
| 9734 | break; |
| 9735 | case TBEGIN: |
| 9736 | n1 = list(0); |
| 9737 | if (readtoken() != TEND) |
| 9738 | synexpect(TEND); |
| 9739 | checkkwd = 1; |
| 9740 | break; |
| 9741 | /* Handle an empty command like other simple commands. */ |
| 9742 | case TSEMI: |
| 9743 | case TAND: |
| 9744 | case TOR: |
| 9745 | case TNL: |
| 9746 | case TEOF: |
| 9747 | case TRP: |
| 9748 | case TBACKGND: |
| 9749 | /* |
| 9750 | * An empty command before a ; doesn't make much sense, and |
| 9751 | * should certainly be disallowed in the case of `if ;'. |
| 9752 | */ |
| 9753 | if (!redir) |
| 9754 | synexpect(-1); |
| 9755 | case TWORD: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9756 | tokpushback++; |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 9757 | n1 = simplecmd(rpp, redir); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9758 | return n1; |
| 9759 | default: |
| 9760 | synexpect(-1); |
| 9761 | /* NOTREACHED */ |
| 9762 | } |
| 9763 | |
| 9764 | /* Now check for redirection which may follow command */ |
| 9765 | while (readtoken() == TREDIR) { |
| 9766 | *rpp = n2 = redirnode; |
| 9767 | rpp = &n2->nfile.next; |
| 9768 | parsefname(); |
| 9769 | } |
| 9770 | tokpushback++; |
| 9771 | *rpp = NULL; |
| 9772 | if (redir) { |
| 9773 | if (n1->type != NSUBSHELL) { |
| 9774 | n2 = (union node *)stalloc(sizeof (struct nredir)); |
| 9775 | n2->type = NREDIR; |
| 9776 | n2->nredir.n = n1; |
| 9777 | n1 = n2; |
| 9778 | } |
| 9779 | n1->nredir.redirect = redir; |
| 9780 | } |
| 9781 | |
| 9782 | return n1; |
| 9783 | } |
| 9784 | |
| 9785 | |
| 9786 | static union node * |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 9787 | simplecmd(union node **rpp, union node *redir) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9788 | union node *args, **app; |
| 9789 | union node *n = NULL; |
| 9790 | union node *vars, **vpp; |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 9791 | union node **orig_rpp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9792 | |
| 9793 | args = NULL; |
| 9794 | app = &args; |
| 9795 | vars = NULL; |
| 9796 | vpp = &vars; |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 9797 | |
| 9798 | /* If we don't have any redirections already, then we must reset |
| 9799 | rpp to be the address of the local redir variable. */ |
| 9800 | if (redir == 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9801 | rpp = &redir; |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 9802 | /* We save the incoming value, because we need this for shell |
| 9803 | functions. There can not be a redirect or an argument between |
| 9804 | the function name and the open parenthesis. */ |
| 9805 | orig_rpp = rpp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9806 | |
| 9807 | checkalias = 2; |
| 9808 | for (;;) { |
| 9809 | switch (readtoken()) { |
| 9810 | case TWORD: |
| 9811 | case TASSIGN: |
| 9812 | n = (union node *)stalloc(sizeof (struct narg)); |
| 9813 | n->type = NARG; |
| 9814 | n->narg.text = wordtext; |
| 9815 | n->narg.backquote = backquotelist; |
| 9816 | if (lasttoken == TWORD) { |
| 9817 | *app = n; |
| 9818 | app = &n->narg.next; |
| 9819 | } else { |
| 9820 | *vpp = n; |
| 9821 | vpp = &n->narg.next; |
| 9822 | } |
| 9823 | break; |
| 9824 | case TREDIR: |
| 9825 | *rpp = n = redirnode; |
| 9826 | rpp = &n->nfile.next; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9827 | parsefname(); /* read name of redirection file */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9828 | break; |
| 9829 | case TLP: |
| 9830 | if ( |
| 9831 | args && app == &args->narg.next && |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 9832 | !vars && rpp == orig_rpp |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9833 | ) { |
| 9834 | /* We have a function */ |
| 9835 | if (readtoken() != TRP) |
| 9836 | synexpect(TRP); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9837 | n->type = NDEFUN; |
| 9838 | checkkwd = 2; |
| 9839 | n->narg.next = command(); |
| 9840 | return n; |
| 9841 | } |
| 9842 | /* fall through */ |
| 9843 | default: |
| 9844 | tokpushback++; |
| 9845 | goto out; |
| 9846 | } |
| 9847 | } |
| 9848 | out: |
| 9849 | *app = NULL; |
| 9850 | *vpp = NULL; |
| 9851 | *rpp = NULL; |
| 9852 | n = (union node *)stalloc(sizeof (struct ncmd)); |
| 9853 | n->type = NCMD; |
| 9854 | n->ncmd.backgnd = 0; |
| 9855 | n->ncmd.args = args; |
| 9856 | n->ncmd.assign = vars; |
| 9857 | n->ncmd.redirect = redir; |
| 9858 | return n; |
| 9859 | } |
| 9860 | |
| 9861 | static union node * |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9862 | makename(void) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9863 | union node *n; |
| 9864 | |
| 9865 | n = (union node *)stalloc(sizeof (struct narg)); |
| 9866 | n->type = NARG; |
| 9867 | n->narg.next = NULL; |
| 9868 | n->narg.text = wordtext; |
| 9869 | n->narg.backquote = backquotelist; |
| 9870 | return n; |
| 9871 | } |
| 9872 | |
| 9873 | static void fixredir(union node *n, const char *text, int err) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9874 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9875 | TRACE(("Fix redir %s %d\n", text, err)); |
| 9876 | if (!err) |
| 9877 | n->ndup.vname = NULL; |
| 9878 | |
| 9879 | if (is_digit(text[0]) && text[1] == '\0') |
| 9880 | n->ndup.dupfd = digit_val(text[0]); |
| 9881 | else if (text[0] == '-' && text[1] == '\0') |
| 9882 | n->ndup.dupfd = -1; |
| 9883 | else { |
| 9884 | |
| 9885 | if (err) |
| 9886 | synerror("Bad fd number"); |
| 9887 | else |
| 9888 | n->ndup.vname = makename(); |
| 9889 | } |
| 9890 | } |
| 9891 | |
| 9892 | |
| 9893 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9894 | parsefname(void) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9895 | union node *n = redirnode; |
| 9896 | |
| 9897 | if (readtoken() != TWORD) |
| 9898 | synexpect(-1); |
| 9899 | if (n->type == NHERE) { |
| 9900 | struct heredoc *here = heredoc; |
| 9901 | struct heredoc *p; |
| 9902 | int i; |
| 9903 | |
| 9904 | if (quoteflag == 0) |
| 9905 | n->type = NXHERE; |
| 9906 | TRACE(("Here document %d\n", n->type)); |
| 9907 | if (here->striptabs) { |
| 9908 | while (*wordtext == '\t') |
| 9909 | wordtext++; |
| 9910 | } |
| 9911 | if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
| 9912 | synerror("Illegal eof marker for << redirection"); |
| 9913 | rmescapes(wordtext); |
| 9914 | here->eofmark = wordtext; |
| 9915 | here->next = NULL; |
| 9916 | if (heredoclist == NULL) |
| 9917 | heredoclist = here; |
| 9918 | else { |
| 9919 | for (p = heredoclist ; p->next ; p = p->next); |
| 9920 | p->next = here; |
| 9921 | } |
| 9922 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 9923 | fixredir(n, wordtext, 0); |
| 9924 | } else { |
| 9925 | n->nfile.fname = makename(); |
| 9926 | } |
| 9927 | } |
| 9928 | |
| 9929 | |
| 9930 | /* |
| 9931 | * Input any here documents. |
| 9932 | */ |
| 9933 | |
| 9934 | static void |
| 9935 | parseheredoc() { |
| 9936 | struct heredoc *here; |
| 9937 | union node *n; |
| 9938 | |
| 9939 | while (heredoclist) { |
| 9940 | here = heredoclist; |
| 9941 | heredoclist = here->next; |
| 9942 | if (needprompt) { |
| 9943 | setprompt(2); |
| 9944 | needprompt = 0; |
| 9945 | } |
| 9946 | readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, |
| 9947 | here->eofmark, here->striptabs); |
| 9948 | n = (union node *)stalloc(sizeof (struct narg)); |
| 9949 | n->narg.type = NARG; |
| 9950 | n->narg.next = NULL; |
| 9951 | n->narg.text = wordtext; |
| 9952 | n->narg.backquote = backquotelist; |
| 9953 | here->here->nhere.doc = n; |
| 9954 | } |
| 9955 | } |
| 9956 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 9957 | static char |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9958 | peektoken() { |
| 9959 | int t; |
| 9960 | |
| 9961 | t = readtoken(); |
| 9962 | tokpushback++; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 9963 | return tokname_array[t][0]; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9964 | } |
| 9965 | |
| 9966 | static int |
| 9967 | readtoken() { |
| 9968 | int t; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 9969 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9970 | int savecheckalias = checkalias; |
Eric Andersen | 8e13987 | 2002-07-04 00:19:46 +0000 | [diff] [blame] | 9971 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 9972 | int savecheckkwd = checkkwd; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9973 | struct alias *ap; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9974 | #endif |
| 9975 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9976 | #ifdef DEBUG |
| 9977 | int alreadyseen = tokpushback; |
| 9978 | #endif |
| 9979 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 9980 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9981 | top: |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9982 | #endif |
| 9983 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9984 | t = xxreadtoken(); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9985 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9986 | checkalias = savecheckalias; |
| 9987 | |
| 9988 | if (checkkwd) { |
| 9989 | /* |
| 9990 | * eat newlines |
| 9991 | */ |
| 9992 | if (checkkwd == 2) { |
| 9993 | checkkwd = 0; |
| 9994 | while (t == TNL) { |
| 9995 | parseheredoc(); |
| 9996 | t = xxreadtoken(); |
| 9997 | } |
| 9998 | } |
| 9999 | checkkwd = 0; |
| 10000 | /* |
| 10001 | * check for keywords |
| 10002 | */ |
| 10003 | if (t == TWORD && !quoteflag) |
| 10004 | { |
| 10005 | const char *const *pp; |
| 10006 | |
| 10007 | if ((pp = findkwd(wordtext))) { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10008 | lasttoken = t = pp - tokname_array; |
| 10009 | TRACE(("keyword %s recognized\n", tokname(t))); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10010 | goto out; |
| 10011 | } |
| 10012 | } |
| 10013 | } |
| 10014 | |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 10015 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10016 | if (t != TWORD) { |
| 10017 | if (t != TREDIR) { |
| 10018 | checkalias = 0; |
| 10019 | } |
| 10020 | } else if (checkalias == 2 && isassignment(wordtext)) { |
| 10021 | lasttoken = t = TASSIGN; |
| 10022 | } else if (checkalias) { |
Eric Andersen | 8e13987 | 2002-07-04 00:19:46 +0000 | [diff] [blame] | 10023 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 10024 | if (!quoteflag && (ap = *__lookupalias(wordtext)) != NULL && !(ap->flag & ALIASINUSE)) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10025 | if (*ap->val) { |
| 10026 | pushstring(ap->val, strlen(ap->val), ap); |
| 10027 | } |
| 10028 | checkkwd = savecheckkwd; |
| 10029 | goto top; |
| 10030 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10031 | #endif |
Eric Andersen | 8e13987 | 2002-07-04 00:19:46 +0000 | [diff] [blame] | 10032 | checkalias = 0; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 10033 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10034 | out: |
| 10035 | #ifdef DEBUG |
| 10036 | if (!alreadyseen) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10037 | TRACE(("token %s %s\n", tokname(t), t == TWORD || t == TASSIGN ? wordtext : "")); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10038 | else |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10039 | TRACE(("reread token %s %s\n", tokname(t), t == TWORD || t == TASSIGN ? wordtext : "")); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10040 | #endif |
| 10041 | return (t); |
| 10042 | } |
| 10043 | |
| 10044 | |
| 10045 | /* |
| 10046 | * Read the next input token. |
| 10047 | * If the token is a word, we set backquotelist to the list of cmds in |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10048 | * backquotes. We set quoteflag to true if any part of the word was |
| 10049 | * quoted. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10050 | * If the token is TREDIR, then we set redirnode to a structure containing |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10051 | * the redirection. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10052 | * In all cases, the variable startlinno is set to the number of the line |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10053 | * on which the token starts. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10054 | * |
| 10055 | * [Change comment: here documents and internal procedures] |
| 10056 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 10057 | * word parsing code into a separate routine. In this case, readtoken |
| 10058 | * doesn't need to have any internal procedures, but parseword does. |
| 10059 | * We could also make parseoperator in essence the main routine, and |
| 10060 | * have parseword (readtoken1?) handle both words and redirection.] |
| 10061 | */ |
| 10062 | |
Manuel Novoa III | 8d0afde | 2001-09-11 01:14:02 +0000 | [diff] [blame] | 10063 | #define NEW_xxreadtoken |
| 10064 | #ifdef NEW_xxreadtoken |
| 10065 | |
| 10066 | static const char xxreadtoken_chars[] = "\n()&|;"; /* singles must be first! */ |
| 10067 | static const char xxreadtoken_tokens[] = { |
| 10068 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 10069 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 10070 | TEOF, /* corresponds to trailing nul */ |
| 10071 | TAND, TOR, TENDCASE, /* if double occurrence */ |
| 10072 | }; |
| 10073 | |
| 10074 | #define xxreadtoken_doubles \ |
| 10075 | (sizeof(xxreadtoken_tokens) - sizeof(xxreadtoken_chars)) |
| 10076 | #define xxreadtoken_singles \ |
| 10077 | (sizeof(xxreadtoken_chars) - xxreadtoken_doubles - 1) |
| 10078 | |
| 10079 | static int |
| 10080 | xxreadtoken() { |
| 10081 | int c; |
| 10082 | |
| 10083 | if (tokpushback) { |
| 10084 | tokpushback = 0; |
| 10085 | return lasttoken; |
| 10086 | } |
| 10087 | if (needprompt) { |
| 10088 | setprompt(2); |
| 10089 | needprompt = 0; |
| 10090 | } |
| 10091 | startlinno = plinno; |
| 10092 | for (;;) { /* until token or start of word found */ |
| 10093 | c = pgetc_macro(); |
| 10094 | |
| 10095 | if ((c!=' ') && (c!='\t') |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 10096 | #ifdef CONFIG_ASH_ALIAS |
Manuel Novoa III | 8d0afde | 2001-09-11 01:14:02 +0000 | [diff] [blame] | 10097 | && (c!=PEOA) |
| 10098 | #endif |
| 10099 | ) { |
| 10100 | if (c=='#') { |
| 10101 | while ((c = pgetc()) != '\n' && c != PEOF); |
| 10102 | pungetc(); |
| 10103 | } else if (c=='\\') { |
| 10104 | if (pgetc() != '\n') { |
| 10105 | pungetc(); |
| 10106 | goto READTOKEN1; |
| 10107 | } |
| 10108 | startlinno = ++plinno; |
| 10109 | setprompt(doprompt ? 2 : 0); |
| 10110 | } else { |
| 10111 | const char *p |
| 10112 | = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 10113 | |
| 10114 | if (c!=PEOF) { |
| 10115 | if (c=='\n') { |
| 10116 | plinno++; |
| 10117 | needprompt = doprompt; |
| 10118 | } |
| 10119 | |
| 10120 | p = strchr(xxreadtoken_chars, c); |
| 10121 | if (p == NULL) { |
| 10122 | READTOKEN1: |
| 10123 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 10124 | } |
| 10125 | |
| 10126 | if (p-xxreadtoken_chars >= xxreadtoken_singles) { |
| 10127 | if (pgetc() == *p) { /* double occurrence? */ |
| 10128 | p += xxreadtoken_doubles + 1; |
| 10129 | } else { |
| 10130 | pungetc(); |
| 10131 | } |
| 10132 | } |
| 10133 | } |
| 10134 | |
| 10135 | return lasttoken = xxreadtoken_tokens[p-xxreadtoken_chars]; |
| 10136 | } |
| 10137 | } |
| 10138 | } |
| 10139 | } |
| 10140 | |
| 10141 | |
| 10142 | #else |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10143 | #define RETURN(token) return lasttoken = token |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10144 | |
| 10145 | static int |
| 10146 | xxreadtoken() { |
| 10147 | int c; |
| 10148 | |
| 10149 | if (tokpushback) { |
| 10150 | tokpushback = 0; |
| 10151 | return lasttoken; |
| 10152 | } |
| 10153 | if (needprompt) { |
| 10154 | setprompt(2); |
| 10155 | needprompt = 0; |
| 10156 | } |
| 10157 | startlinno = plinno; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10158 | for (;;) { /* until token or start of word found */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10159 | c = pgetc_macro(); |
| 10160 | switch (c) { |
| 10161 | case ' ': case '\t': |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 10162 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10163 | case PEOA: |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 10164 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10165 | continue; |
| 10166 | case '#': |
| 10167 | while ((c = pgetc()) != '\n' && c != PEOF); |
| 10168 | pungetc(); |
| 10169 | continue; |
| 10170 | case '\\': |
| 10171 | if (pgetc() == '\n') { |
| 10172 | startlinno = ++plinno; |
| 10173 | if (doprompt) |
| 10174 | setprompt(2); |
| 10175 | else |
| 10176 | setprompt(0); |
| 10177 | continue; |
| 10178 | } |
| 10179 | pungetc(); |
| 10180 | goto breakloop; |
| 10181 | case '\n': |
| 10182 | plinno++; |
| 10183 | needprompt = doprompt; |
| 10184 | RETURN(TNL); |
| 10185 | case PEOF: |
| 10186 | RETURN(TEOF); |
| 10187 | case '&': |
| 10188 | if (pgetc() == '&') |
| 10189 | RETURN(TAND); |
| 10190 | pungetc(); |
| 10191 | RETURN(TBACKGND); |
| 10192 | case '|': |
| 10193 | if (pgetc() == '|') |
| 10194 | RETURN(TOR); |
| 10195 | pungetc(); |
| 10196 | RETURN(TPIPE); |
| 10197 | case ';': |
| 10198 | if (pgetc() == ';') |
| 10199 | RETURN(TENDCASE); |
| 10200 | pungetc(); |
| 10201 | RETURN(TSEMI); |
| 10202 | case '(': |
| 10203 | RETURN(TLP); |
| 10204 | case ')': |
| 10205 | RETURN(TRP); |
| 10206 | default: |
| 10207 | goto breakloop; |
| 10208 | } |
| 10209 | } |
| 10210 | breakloop: |
| 10211 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 10212 | #undef RETURN |
| 10213 | } |
Manuel Novoa III | 8d0afde | 2001-09-11 01:14:02 +0000 | [diff] [blame] | 10214 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10215 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10216 | /* |
| 10217 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 10218 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 10219 | * word which marks the end of the document and striptabs is true if |
| 10220 | * leading tabs should be stripped from the document. The argument firstc |
| 10221 | * is the first character of the input token or document. |
| 10222 | * |
| 10223 | * Because C does not have internal subroutines, I have simulated them |
| 10224 | * using goto's to implement the subroutine linkage. The following macros |
| 10225 | * will run code that appears at the end of readtoken1. |
| 10226 | */ |
| 10227 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10228 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 10229 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 10230 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 10231 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 10232 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 10233 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10234 | |
| 10235 | static int |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10236 | readtoken1(int firstc, int syntax, const char *eofmark, int striptabs) |
| 10237 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10238 | int c = firstc; |
| 10239 | char *out; |
| 10240 | int len; |
| 10241 | char line[EOFMARKLEN + 1]; |
| 10242 | struct nodelist *bqlist; |
| 10243 | int quotef; |
| 10244 | int dblquote; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10245 | int varnest; /* levels of variables expansion */ |
| 10246 | int arinest; /* levels of arithmetic expansion */ |
| 10247 | int parenlevel; /* levels of parens in arithmetic */ |
| 10248 | int dqvarnest; /* levels of variables expansion within double quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10249 | int oldstyle; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10250 | int prevsyntax; /* syntax before arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10251 | #if __GNUC__ |
| 10252 | /* Avoid longjmp clobbering */ |
| 10253 | (void) &out; |
| 10254 | (void) "ef; |
| 10255 | (void) &dblquote; |
| 10256 | (void) &varnest; |
| 10257 | (void) &arinest; |
| 10258 | (void) &parenlevel; |
| 10259 | (void) &dqvarnest; |
| 10260 | (void) &oldstyle; |
| 10261 | (void) &prevsyntax; |
| 10262 | (void) &syntax; |
| 10263 | #endif |
| 10264 | |
| 10265 | startlinno = plinno; |
| 10266 | dblquote = 0; |
| 10267 | if (syntax == DQSYNTAX) |
| 10268 | dblquote = 1; |
| 10269 | quotef = 0; |
| 10270 | bqlist = NULL; |
| 10271 | varnest = 0; |
| 10272 | arinest = 0; |
| 10273 | parenlevel = 0; |
| 10274 | dqvarnest = 0; |
| 10275 | |
| 10276 | STARTSTACKSTR(out); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10277 | loop: { /* for each line, until end of word */ |
| 10278 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 10279 | for (;;) { /* until end of line or end of word */ |
| 10280 | CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10281 | switch(SIT(c,syntax)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10282 | case CNL: /* '\n' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10283 | if (syntax == BASESYNTAX) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10284 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10285 | USTPUTC(c, out); |
| 10286 | plinno++; |
| 10287 | if (doprompt) |
| 10288 | setprompt(2); |
| 10289 | else |
| 10290 | setprompt(0); |
| 10291 | c = pgetc(); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10292 | goto loop; /* continue outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10293 | case CWORD: |
| 10294 | USTPUTC(c, out); |
| 10295 | break; |
| 10296 | case CCTL: |
| 10297 | if ((eofmark == NULL || dblquote) && |
| 10298 | dqvarnest == 0) |
| 10299 | USTPUTC(CTLESC, out); |
| 10300 | USTPUTC(c, out); |
| 10301 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10302 | case CBACK: /* backslash */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10303 | c = pgetc2(); |
| 10304 | if (c == PEOF) { |
| 10305 | USTPUTC('\\', out); |
| 10306 | pungetc(); |
| 10307 | } else if (c == '\n') { |
| 10308 | if (doprompt) |
| 10309 | setprompt(2); |
| 10310 | else |
| 10311 | setprompt(0); |
| 10312 | } else { |
| 10313 | if (dblquote && c != '\\' && c != '`' && c != '$' |
| 10314 | && (c != '"' || eofmark != NULL)) |
| 10315 | USTPUTC('\\', out); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10316 | if (SIT(c,SQSYNTAX) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10317 | USTPUTC(CTLESC, out); |
| 10318 | else if (eofmark == NULL) |
| 10319 | USTPUTC(CTLQUOTEMARK, out); |
| 10320 | USTPUTC(c, out); |
| 10321 | quotef++; |
| 10322 | } |
| 10323 | break; |
| 10324 | case CSQUOTE: |
| 10325 | if (eofmark == NULL) |
| 10326 | USTPUTC(CTLQUOTEMARK, out); |
| 10327 | syntax = SQSYNTAX; |
| 10328 | break; |
| 10329 | case CDQUOTE: |
| 10330 | if (eofmark == NULL) |
| 10331 | USTPUTC(CTLQUOTEMARK, out); |
| 10332 | syntax = DQSYNTAX; |
| 10333 | dblquote = 1; |
| 10334 | break; |
| 10335 | case CENDQUOTE: |
| 10336 | if (eofmark != NULL && arinest == 0 && |
| 10337 | varnest == 0) { |
| 10338 | USTPUTC(c, out); |
| 10339 | } else { |
| 10340 | if (arinest) { |
| 10341 | syntax = ARISYNTAX; |
| 10342 | dblquote = 0; |
| 10343 | } else if (eofmark == NULL && |
| 10344 | dqvarnest == 0) { |
| 10345 | syntax = BASESYNTAX; |
| 10346 | dblquote = 0; |
| 10347 | } |
| 10348 | quotef++; |
| 10349 | } |
| 10350 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10351 | case CVAR: /* '$' */ |
| 10352 | PARSESUB(); /* parse substitution */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10353 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10354 | case CENDVAR: /* '}' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10355 | if (varnest > 0) { |
| 10356 | varnest--; |
| 10357 | if (dqvarnest > 0) { |
| 10358 | dqvarnest--; |
| 10359 | } |
| 10360 | USTPUTC(CTLENDVAR, out); |
| 10361 | } else { |
| 10362 | USTPUTC(c, out); |
| 10363 | } |
| 10364 | break; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 10365 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10366 | case CLP: /* '(' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10367 | parenlevel++; |
| 10368 | USTPUTC(c, out); |
| 10369 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10370 | case CRP: /* ')' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10371 | if (parenlevel > 0) { |
| 10372 | USTPUTC(c, out); |
| 10373 | --parenlevel; |
| 10374 | } else { |
| 10375 | if (pgetc() == ')') { |
| 10376 | if (--arinest == 0) { |
| 10377 | USTPUTC(CTLENDARI, out); |
| 10378 | syntax = prevsyntax; |
| 10379 | if (syntax == DQSYNTAX) |
| 10380 | dblquote = 1; |
| 10381 | else |
| 10382 | dblquote = 0; |
| 10383 | } else |
| 10384 | USTPUTC(')', out); |
| 10385 | } else { |
| 10386 | /* |
| 10387 | * unbalanced parens |
| 10388 | * (don't 2nd guess - no error) |
| 10389 | */ |
| 10390 | pungetc(); |
| 10391 | USTPUTC(')', out); |
| 10392 | } |
| 10393 | } |
| 10394 | break; |
| 10395 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10396 | case CBQUOTE: /* '`' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10397 | PARSEBACKQOLD(); |
| 10398 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10399 | case CENDFILE: |
| 10400 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10401 | case CIGN: |
| 10402 | break; |
| 10403 | default: |
| 10404 | if (varnest == 0) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10405 | goto endword; /* exit outer loop */ |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 10406 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 10407 | if (c != PEOA) |
| 10408 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10409 | USTPUTC(c, out); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 10410 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10411 | } |
| 10412 | c = pgetc_macro(); |
| 10413 | } |
| 10414 | } |
| 10415 | endword: |
| 10416 | if (syntax == ARISYNTAX) |
| 10417 | synerror("Missing '))'"); |
| 10418 | if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL) |
| 10419 | synerror("Unterminated quoted string"); |
| 10420 | if (varnest != 0) { |
| 10421 | startlinno = plinno; |
| 10422 | synerror("Missing '}'"); |
| 10423 | } |
| 10424 | USTPUTC('\0', out); |
| 10425 | len = out - stackblock(); |
| 10426 | out = stackblock(); |
| 10427 | if (eofmark == NULL) { |
| 10428 | if ((c == '>' || c == '<') |
| 10429 | && quotef == 0 |
| 10430 | && len <= 2 |
| 10431 | && (*out == '\0' || is_digit(*out))) { |
| 10432 | PARSEREDIR(); |
| 10433 | return lasttoken = TREDIR; |
| 10434 | } else { |
| 10435 | pungetc(); |
| 10436 | } |
| 10437 | } |
| 10438 | quoteflag = quotef; |
| 10439 | backquotelist = bqlist; |
| 10440 | grabstackblock(len); |
| 10441 | wordtext = out; |
| 10442 | return lasttoken = TWORD; |
| 10443 | /* end of readtoken routine */ |
| 10444 | |
| 10445 | |
| 10446 | |
| 10447 | /* |
| 10448 | * Check to see whether we are at the end of the here document. When this |
| 10449 | * is called, c is set to the first character of the next input line. If |
| 10450 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 10451 | */ |
| 10452 | |
| 10453 | checkend: { |
| 10454 | if (eofmark) { |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 10455 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10456 | if (c == PEOA) { |
| 10457 | c = pgetc2(); |
| 10458 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10459 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10460 | if (striptabs) { |
| 10461 | while (c == '\t') { |
| 10462 | c = pgetc2(); |
| 10463 | } |
| 10464 | } |
| 10465 | if (c == *eofmark) { |
| 10466 | if (pfgets(line, sizeof line) != NULL) { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10467 | const char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10468 | |
| 10469 | p = line; |
| 10470 | for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); |
| 10471 | if (*p == '\n' && *q == '\0') { |
| 10472 | c = PEOF; |
| 10473 | plinno++; |
| 10474 | needprompt = doprompt; |
| 10475 | } else { |
| 10476 | pushstring(line, strlen(line), NULL); |
| 10477 | } |
| 10478 | } |
| 10479 | } |
| 10480 | } |
| 10481 | goto checkend_return; |
| 10482 | } |
| 10483 | |
| 10484 | |
| 10485 | /* |
| 10486 | * Parse a redirection operator. The variable "out" points to a string |
| 10487 | * specifying the fd to be redirected. The variable "c" contains the |
| 10488 | * first character of the redirection operator. |
| 10489 | */ |
| 10490 | |
| 10491 | parseredir: { |
| 10492 | char fd = *out; |
| 10493 | union node *np; |
| 10494 | |
| 10495 | np = (union node *)stalloc(sizeof (struct nfile)); |
| 10496 | if (c == '>') { |
| 10497 | np->nfile.fd = 1; |
| 10498 | c = pgetc(); |
| 10499 | if (c == '>') |
| 10500 | np->type = NAPPEND; |
| 10501 | else if (c == '&') |
| 10502 | np->type = NTOFD; |
| 10503 | else if (c == '|') |
| 10504 | np->type = NTOOV; |
| 10505 | else { |
| 10506 | np->type = NTO; |
| 10507 | pungetc(); |
| 10508 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10509 | } else { /* c == '<' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10510 | np->nfile.fd = 0; |
| 10511 | switch (c = pgetc()) { |
| 10512 | case '<': |
| 10513 | if (sizeof (struct nfile) != sizeof (struct nhere)) { |
| 10514 | np = (union node *)stalloc(sizeof (struct nhere)); |
| 10515 | np->nfile.fd = 0; |
| 10516 | } |
| 10517 | np->type = NHERE; |
| 10518 | heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); |
| 10519 | heredoc->here = np; |
| 10520 | if ((c = pgetc()) == '-') { |
| 10521 | heredoc->striptabs = 1; |
| 10522 | } else { |
| 10523 | heredoc->striptabs = 0; |
| 10524 | pungetc(); |
| 10525 | } |
| 10526 | break; |
| 10527 | |
| 10528 | case '&': |
| 10529 | np->type = NFROMFD; |
| 10530 | break; |
| 10531 | |
| 10532 | case '>': |
| 10533 | np->type = NFROMTO; |
| 10534 | break; |
| 10535 | |
| 10536 | default: |
| 10537 | np->type = NFROM; |
| 10538 | pungetc(); |
| 10539 | break; |
| 10540 | } |
| 10541 | } |
| 10542 | if (fd != '\0') |
| 10543 | np->nfile.fd = digit_val(fd); |
| 10544 | redirnode = np; |
| 10545 | goto parseredir_return; |
| 10546 | } |
| 10547 | |
| 10548 | |
| 10549 | /* |
| 10550 | * Parse a substitution. At this point, we have read the dollar sign |
| 10551 | * and nothing else. |
| 10552 | */ |
| 10553 | |
| 10554 | parsesub: { |
| 10555 | int subtype; |
| 10556 | int typeloc; |
| 10557 | int flags; |
| 10558 | char *p; |
| 10559 | static const char types[] = "}-+?="; |
| 10560 | |
| 10561 | c = pgetc(); |
| 10562 | if ( |
| 10563 | c <= PEOA || |
| 10564 | (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
| 10565 | ) { |
| 10566 | USTPUTC('$', out); |
| 10567 | pungetc(); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10568 | } else if (c == '(') { /* $(command) or $((arith)) */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10569 | if (pgetc() == '(') { |
| 10570 | PARSEARITH(); |
| 10571 | } else { |
| 10572 | pungetc(); |
| 10573 | PARSEBACKQNEW(); |
| 10574 | } |
| 10575 | } else { |
| 10576 | USTPUTC(CTLVAR, out); |
| 10577 | typeloc = out - stackblock(); |
| 10578 | USTPUTC(VSNORMAL, out); |
| 10579 | subtype = VSNORMAL; |
| 10580 | if (c == '{') { |
| 10581 | c = pgetc(); |
| 10582 | if (c == '#') { |
| 10583 | if ((c = pgetc()) == '}') |
| 10584 | c = '#'; |
| 10585 | else |
| 10586 | subtype = VSLENGTH; |
| 10587 | } |
| 10588 | else |
| 10589 | subtype = 0; |
| 10590 | } |
| 10591 | if (c > PEOA && is_name(c)) { |
| 10592 | do { |
| 10593 | STPUTC(c, out); |
| 10594 | c = pgetc(); |
| 10595 | } while (c > PEOA && is_in_name(c)); |
| 10596 | } else if (is_digit(c)) { |
| 10597 | do { |
| 10598 | USTPUTC(c, out); |
| 10599 | c = pgetc(); |
| 10600 | } while (is_digit(c)); |
| 10601 | } |
| 10602 | else if (is_special(c)) { |
| 10603 | USTPUTC(c, out); |
| 10604 | c = pgetc(); |
| 10605 | } |
| 10606 | else |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10607 | badsub: synerror("Bad substitution"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10608 | |
| 10609 | STPUTC('=', out); |
| 10610 | flags = 0; |
| 10611 | if (subtype == 0) { |
| 10612 | switch (c) { |
| 10613 | case ':': |
| 10614 | flags = VSNUL; |
| 10615 | c = pgetc(); |
| 10616 | /*FALLTHROUGH*/ |
| 10617 | default: |
| 10618 | p = strchr(types, c); |
| 10619 | if (p == NULL) |
| 10620 | goto badsub; |
| 10621 | subtype = p - types + VSNORMAL; |
| 10622 | break; |
| 10623 | case '%': |
| 10624 | case '#': |
| 10625 | { |
| 10626 | int cc = c; |
| 10627 | subtype = c == '#' ? VSTRIMLEFT : |
| 10628 | VSTRIMRIGHT; |
| 10629 | c = pgetc(); |
| 10630 | if (c == cc) |
| 10631 | subtype++; |
| 10632 | else |
| 10633 | pungetc(); |
| 10634 | break; |
| 10635 | } |
| 10636 | } |
| 10637 | } else { |
| 10638 | pungetc(); |
| 10639 | } |
| 10640 | if (dblquote || arinest) |
| 10641 | flags |= VSQUOTE; |
| 10642 | *(stackblock() + typeloc) = subtype | flags; |
| 10643 | if (subtype != VSNORMAL) { |
| 10644 | varnest++; |
| 10645 | if (dblquote) { |
| 10646 | dqvarnest++; |
| 10647 | } |
| 10648 | } |
| 10649 | } |
| 10650 | goto parsesub_return; |
| 10651 | } |
| 10652 | |
| 10653 | |
| 10654 | /* |
| 10655 | * Called to parse command substitutions. Newstyle is set if the command |
| 10656 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 10657 | * list of commands (passed by reference), and savelen is the number of |
| 10658 | * characters on the top of the stack which must be preserved. |
| 10659 | */ |
| 10660 | |
| 10661 | parsebackq: { |
| 10662 | struct nodelist **nlpp; |
| 10663 | int savepbq; |
| 10664 | union node *n; |
| 10665 | char *volatile str; |
| 10666 | struct jmploc jmploc; |
| 10667 | struct jmploc *volatile savehandler; |
| 10668 | int savelen; |
| 10669 | int saveprompt; |
| 10670 | #ifdef __GNUC__ |
| 10671 | (void) &saveprompt; |
| 10672 | #endif |
| 10673 | |
| 10674 | savepbq = parsebackquote; |
| 10675 | if (setjmp(jmploc.loc)) { |
| 10676 | if (str) |
| 10677 | ckfree(str); |
| 10678 | parsebackquote = 0; |
| 10679 | handler = savehandler; |
| 10680 | longjmp(handler->loc, 1); |
| 10681 | } |
| 10682 | INTOFF; |
| 10683 | str = NULL; |
| 10684 | savelen = out - stackblock(); |
| 10685 | if (savelen > 0) { |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 10686 | str = xmalloc(savelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10687 | memcpy(str, stackblock(), savelen); |
| 10688 | } |
| 10689 | savehandler = handler; |
| 10690 | handler = &jmploc; |
| 10691 | INTON; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10692 | if (oldstyle) { |
| 10693 | /* We must read until the closing backquote, giving special |
| 10694 | treatment to some slashes, and then push the string and |
| 10695 | reread it as input, interpreting it normally. */ |
| 10696 | char *pout; |
| 10697 | int pc; |
| 10698 | int psavelen; |
| 10699 | char *pstr; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10700 | |
| 10701 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10702 | STARTSTACKSTR(pout); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10703 | for (;;) { |
| 10704 | if (needprompt) { |
| 10705 | setprompt(2); |
| 10706 | needprompt = 0; |
| 10707 | } |
| 10708 | switch (pc = pgetc()) { |
| 10709 | case '`': |
| 10710 | goto done; |
| 10711 | |
| 10712 | case '\\': |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10713 | if ((pc = pgetc()) == '\n') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10714 | plinno++; |
| 10715 | if (doprompt) |
| 10716 | setprompt(2); |
| 10717 | else |
| 10718 | setprompt(0); |
| 10719 | /* |
| 10720 | * If eating a newline, avoid putting |
| 10721 | * the newline into the new character |
| 10722 | * stream (via the STPUTC after the |
| 10723 | * switch). |
| 10724 | */ |
| 10725 | continue; |
| 10726 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10727 | if (pc != '\\' && pc != '`' && pc != '$' |
| 10728 | && (!dblquote || pc != '"')) |
| 10729 | STPUTC('\\', pout); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10730 | if (pc > PEOA) { |
| 10731 | break; |
| 10732 | } |
| 10733 | /* fall through */ |
| 10734 | |
| 10735 | case PEOF: |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 10736 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10737 | case PEOA: |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10738 | #endif |
| 10739 | startlinno = plinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10740 | synerror("EOF in backquote substitution"); |
| 10741 | |
| 10742 | case '\n': |
| 10743 | plinno++; |
| 10744 | needprompt = doprompt; |
| 10745 | break; |
| 10746 | |
| 10747 | default: |
| 10748 | break; |
| 10749 | } |
| 10750 | STPUTC(pc, pout); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10751 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10752 | done: |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10753 | STPUTC('\0', pout); |
| 10754 | psavelen = pout - stackblock(); |
| 10755 | if (psavelen > 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10756 | pstr = grabstackstr(pout); |
| 10757 | setinputstring(pstr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10758 | } |
| 10759 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10760 | nlpp = &bqlist; |
| 10761 | while (*nlpp) |
| 10762 | nlpp = &(*nlpp)->next; |
| 10763 | *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); |
| 10764 | (*nlpp)->next = NULL; |
| 10765 | parsebackquote = oldstyle; |
| 10766 | |
| 10767 | if (oldstyle) { |
| 10768 | saveprompt = doprompt; |
| 10769 | doprompt = 0; |
| 10770 | } |
| 10771 | |
| 10772 | n = list(0); |
| 10773 | |
| 10774 | if (oldstyle) |
| 10775 | doprompt = saveprompt; |
| 10776 | else { |
| 10777 | if (readtoken() != TRP) |
| 10778 | synexpect(TRP); |
| 10779 | } |
| 10780 | |
| 10781 | (*nlpp)->n = n; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10782 | if (oldstyle) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10783 | /* |
| 10784 | * Start reading from old file again, ignoring any pushed back |
| 10785 | * tokens left from the backquote parsing |
| 10786 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10787 | popfile(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10788 | tokpushback = 0; |
| 10789 | } |
| 10790 | while (stackblocksize() <= savelen) |
| 10791 | growstackblock(); |
| 10792 | STARTSTACKSTR(out); |
| 10793 | if (str) { |
| 10794 | memcpy(out, str, savelen); |
| 10795 | STADJUST(savelen, out); |
| 10796 | INTOFF; |
| 10797 | ckfree(str); |
| 10798 | str = NULL; |
| 10799 | INTON; |
| 10800 | } |
| 10801 | parsebackquote = savepbq; |
| 10802 | handler = savehandler; |
| 10803 | if (arinest || dblquote) |
| 10804 | USTPUTC(CTLBACKQ | CTLQUOTE, out); |
| 10805 | else |
| 10806 | USTPUTC(CTLBACKQ, out); |
| 10807 | if (oldstyle) |
| 10808 | goto parsebackq_oldreturn; |
| 10809 | else |
| 10810 | goto parsebackq_newreturn; |
| 10811 | } |
| 10812 | |
| 10813 | /* |
| 10814 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 10815 | */ |
| 10816 | parsearith: { |
| 10817 | |
| 10818 | if (++arinest == 1) { |
| 10819 | prevsyntax = syntax; |
| 10820 | syntax = ARISYNTAX; |
| 10821 | USTPUTC(CTLARI, out); |
| 10822 | if (dblquote) |
| 10823 | USTPUTC('"',out); |
| 10824 | else |
| 10825 | USTPUTC(' ',out); |
| 10826 | } else { |
| 10827 | /* |
| 10828 | * we collapse embedded arithmetic expansion to |
| 10829 | * parenthesis, which should be equivalent |
| 10830 | */ |
| 10831 | USTPUTC('(', out); |
| 10832 | } |
| 10833 | goto parsearith_return; |
| 10834 | } |
| 10835 | |
| 10836 | } /* end of readtoken */ |
| 10837 | |
| 10838 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10839 | /* |
| 10840 | * Returns true if the text contains nothing to expand (no dollar signs |
| 10841 | * or backquotes). |
| 10842 | */ |
| 10843 | |
| 10844 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 10845 | noexpand(char *text) |
| 10846 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10847 | char *p; |
| 10848 | char c; |
| 10849 | |
| 10850 | p = text; |
| 10851 | while ((c = *p++) != '\0') { |
| 10852 | if (c == CTLQUOTEMARK) |
| 10853 | continue; |
| 10854 | if (c == CTLESC) |
| 10855 | p++; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10856 | else if (SIT(c,BASESYNTAX) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10857 | return 0; |
| 10858 | } |
| 10859 | return 1; |
| 10860 | } |
| 10861 | |
| 10862 | |
| 10863 | /* |
| 10864 | * Return true if the argument is a legal variable name (a letter or |
| 10865 | * underscore followed by zero or more letters, underscores, and digits). |
| 10866 | */ |
| 10867 | |
| 10868 | static int |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10869 | goodname(const char *name) |
| 10870 | { |
| 10871 | const char *p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10872 | |
| 10873 | p = name; |
| 10874 | if (! is_name(*p)) |
| 10875 | return 0; |
| 10876 | while (*++p) { |
| 10877 | if (! is_in_name(*p)) |
| 10878 | return 0; |
| 10879 | } |
| 10880 | return 1; |
| 10881 | } |
| 10882 | |
| 10883 | |
| 10884 | /* |
| 10885 | * Called when an unexpected token is read during the parse. The argument |
| 10886 | * is the token that is expected, or -1 if more than one type of token can |
| 10887 | * occur at this point. |
| 10888 | */ |
| 10889 | |
| 10890 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 10891 | synexpect(int token) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10892 | { |
| 10893 | char msg[64]; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10894 | int l; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10895 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10896 | l = sprintf(msg, "%s unexpected", tokname(lasttoken)); |
| 10897 | if (token >= 0) |
| 10898 | sprintf(msg+l, " (expecting %s)", tokname(token)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10899 | synerror(msg); |
| 10900 | /* NOTREACHED */ |
| 10901 | } |
| 10902 | |
| 10903 | |
| 10904 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10905 | synerror(const char *msg) |
| 10906 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10907 | if (commandname) |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 10908 | out2fmt("%s: %d: ", commandname, startlinno); |
| 10909 | out2fmt("Syntax error: %s\n", msg); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10910 | error((char *)NULL); |
| 10911 | /* NOTREACHED */ |
| 10912 | } |
| 10913 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10914 | |
| 10915 | /* |
| 10916 | * called by editline -- any expansions to the prompt |
| 10917 | * should be added here. |
| 10918 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10919 | static void |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10920 | setprompt(int whichprompt) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10921 | { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10922 | char *prompt; |
| 10923 | switch (whichprompt) { |
| 10924 | case 1: |
| 10925 | prompt = ps1val(); |
| 10926 | break; |
| 10927 | case 2: |
| 10928 | prompt = ps2val(); |
| 10929 | break; |
| 10930 | default: /* 0 */ |
| 10931 | prompt = ""; |
| 10932 | } |
| 10933 | putprompt(prompt); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10934 | } |
| 10935 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10936 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10937 | /* |
| 10938 | * Code for dealing with input/output redirection. |
| 10939 | */ |
| 10940 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10941 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10942 | #ifndef PIPE_BUF |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10943 | # define PIPESIZE 4096 /* amount of buffering in a pipe */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10944 | #else |
| 10945 | # define PIPESIZE PIPE_BUF |
| 10946 | #endif |
| 10947 | |
| 10948 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10949 | /* |
| 10950 | * Open a file in noclobber mode. |
| 10951 | * The code was copied from bash. |
| 10952 | */ |
| 10953 | static inline int |
| 10954 | noclobberopen(const char *fname) |
| 10955 | { |
| 10956 | int r, fd; |
| 10957 | struct stat finfo, finfo2; |
| 10958 | |
| 10959 | /* |
| 10960 | * If the file exists and is a regular file, return an error |
| 10961 | * immediately. |
| 10962 | */ |
| 10963 | r = stat(fname, &finfo); |
| 10964 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 10965 | errno = EEXIST; |
| 10966 | return -1; |
| 10967 | } |
| 10968 | |
| 10969 | /* |
| 10970 | * If the file was not present (r != 0), make sure we open it |
| 10971 | * exclusively so that if it is created before we open it, our open |
| 10972 | * will fail. Make sure that we do not truncate an existing file. |
| 10973 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 10974 | * file was not a regular file, we leave O_EXCL off. |
| 10975 | */ |
| 10976 | if (r != 0) |
| 10977 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 10978 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
| 10979 | |
| 10980 | /* If the open failed, return the file descriptor right away. */ |
| 10981 | if (fd < 0) |
| 10982 | return fd; |
| 10983 | |
| 10984 | /* |
| 10985 | * OK, the open succeeded, but the file may have been changed from a |
| 10986 | * non-regular file to a regular file between the stat and the open. |
| 10987 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 10988 | * did not exist and is symlinked to an existing file between the stat |
| 10989 | * and open. |
| 10990 | */ |
| 10991 | |
| 10992 | /* |
| 10993 | * If we can open it and fstat the file descriptor, and neither check |
| 10994 | * revealed that it was a regular file, and the file has not been |
| 10995 | * replaced, return the file descriptor. |
| 10996 | */ |
| 10997 | if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode) && |
| 10998 | finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino) |
| 10999 | return fd; |
| 11000 | |
| 11001 | /* The file has been replaced. badness. */ |
| 11002 | close(fd); |
| 11003 | errno = EEXIST; |
| 11004 | return -1; |
| 11005 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11006 | |
| 11007 | /* |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11008 | * Handle here documents. Normally we fork off a process to write the |
| 11009 | * data to a pipe. If the document is short, we can stuff the data in |
| 11010 | * the pipe without forking. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11011 | */ |
| 11012 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11013 | static inline int |
| 11014 | openhere(const union node *redir) |
| 11015 | { |
| 11016 | int pip[2]; |
| 11017 | int len = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11018 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11019 | if (pipe(pip) < 0) |
| 11020 | error("Pipe call failed"); |
| 11021 | if (redir->type == NHERE) { |
| 11022 | len = strlen(redir->nhere.doc->narg.text); |
| 11023 | if (len <= PIPESIZE) { |
| 11024 | xwrite(pip[1], redir->nhere.doc->narg.text, len); |
| 11025 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11026 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11027 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11028 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
| 11029 | close(pip[0]); |
| 11030 | signal(SIGINT, SIG_IGN); |
| 11031 | signal(SIGQUIT, SIG_IGN); |
| 11032 | signal(SIGHUP, SIG_IGN); |
| 11033 | #ifdef SIGTSTP |
| 11034 | signal(SIGTSTP, SIG_IGN); |
| 11035 | #endif |
| 11036 | signal(SIGPIPE, SIG_DFL); |
| 11037 | if (redir->type == NHERE) |
| 11038 | xwrite(pip[1], redir->nhere.doc->narg.text, len); |
| 11039 | else |
| 11040 | expandhere(redir->nhere.doc, pip[1]); |
| 11041 | _exit(0); |
| 11042 | } |
| 11043 | out: |
| 11044 | close(pip[1]); |
| 11045 | return pip[0]; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11046 | } |
| 11047 | |
| 11048 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11049 | static inline int |
| 11050 | openredirect(const union node *redir) |
| 11051 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11052 | char *fname; |
| 11053 | int f; |
| 11054 | |
| 11055 | switch (redir->nfile.type) { |
| 11056 | case NFROM: |
| 11057 | fname = redir->nfile.expfname; |
| 11058 | if ((f = open(fname, O_RDONLY)) < 0) |
| 11059 | goto eopen; |
| 11060 | break; |
| 11061 | case NFROMTO: |
| 11062 | fname = redir->nfile.expfname; |
| 11063 | if ((f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) |
| 11064 | goto ecreate; |
| 11065 | break; |
| 11066 | case NTO: |
| 11067 | /* Take care of noclobber mode. */ |
| 11068 | if (Cflag) { |
| 11069 | fname = redir->nfile.expfname; |
| 11070 | if ((f = noclobberopen(fname)) < 0) |
| 11071 | goto ecreate; |
| 11072 | break; |
| 11073 | } |
| 11074 | case NTOOV: |
| 11075 | fname = redir->nfile.expfname; |
| 11076 | #ifdef O_CREAT |
| 11077 | if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) |
| 11078 | goto ecreate; |
| 11079 | #else |
| 11080 | if ((f = creat(fname, 0666)) < 0) |
| 11081 | goto ecreate; |
| 11082 | #endif |
| 11083 | break; |
| 11084 | case NAPPEND: |
| 11085 | fname = redir->nfile.expfname; |
| 11086 | #ifdef O_APPEND |
| 11087 | if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) |
| 11088 | goto ecreate; |
| 11089 | #else |
| 11090 | if ((f = open(fname, O_WRONLY)) < 0 |
| 11091 | && (f = creat(fname, 0666)) < 0) |
| 11092 | goto ecreate; |
| 11093 | lseek(f, (off_t)0, 2); |
| 11094 | #endif |
| 11095 | break; |
| 11096 | default: |
| 11097 | #ifdef DEBUG |
| 11098 | abort(); |
| 11099 | #endif |
| 11100 | /* Fall through to eliminate warning. */ |
| 11101 | case NTOFD: |
| 11102 | case NFROMFD: |
| 11103 | f = -1; |
| 11104 | break; |
| 11105 | case NHERE: |
| 11106 | case NXHERE: |
| 11107 | f = openhere(redir); |
| 11108 | break; |
| 11109 | } |
| 11110 | |
| 11111 | return f; |
| 11112 | ecreate: |
| 11113 | error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); |
| 11114 | eopen: |
| 11115 | error("cannot open %s: %s", fname, errmsg(errno, E_OPEN)); |
| 11116 | } |
| 11117 | |
| 11118 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11119 | /* |
| 11120 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 11121 | * old file descriptors are stashed away so that the redirection can be |
| 11122 | * undone by calling popredir. If the REDIR_BACKQ flag is set, then the |
| 11123 | * standard output, and the standard error if it becomes a duplicate of |
| 11124 | * stdout. |
| 11125 | */ |
| 11126 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11127 | static void |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11128 | redirect(union node *redir, int flags) |
| 11129 | { |
| 11130 | union node *n; |
| 11131 | struct redirtab *sv = NULL; |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 11132 | int i = EMPTY; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11133 | int fd; |
| 11134 | int newfd; |
| 11135 | int try; |
| 11136 | int fd1dup = flags & REDIR_BACKQ;; /* stdout `cmd` redir to pipe */ |
| 11137 | |
| 11138 | if (flags & REDIR_PUSH) { |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11139 | sv = xmalloc(sizeof (struct redirtab)); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11140 | for (i = 0 ; i < 10 ; i++) |
| 11141 | sv->renamed[i] = EMPTY; |
| 11142 | sv->next = redirlist; |
| 11143 | redirlist = sv; |
| 11144 | } |
| 11145 | for (n = redir ; n ; n = n->nfile.next) { |
| 11146 | fd = n->nfile.fd; |
| 11147 | try = 0; |
| 11148 | if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) && |
| 11149 | n->ndup.dupfd == fd) |
| 11150 | continue; /* redirect from/to same file descriptor */ |
| 11151 | |
| 11152 | INTOFF; |
| 11153 | newfd = openredirect(n); |
| 11154 | if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) { |
| 11155 | if (newfd == fd) { |
| 11156 | try++; |
| 11157 | } else if ((i = fcntl(fd, F_DUPFD, 10)) == -1) { |
| 11158 | switch (errno) { |
| 11159 | case EBADF: |
| 11160 | if (!try) { |
| 11161 | dupredirect(n, newfd, fd1dup); |
| 11162 | try++; |
| 11163 | break; |
| 11164 | } |
| 11165 | /* FALLTHROUGH*/ |
| 11166 | default: |
| 11167 | if (newfd >= 0) { |
| 11168 | close(newfd); |
| 11169 | } |
| 11170 | INTON; |
| 11171 | error("%d: %m", fd); |
| 11172 | /* NOTREACHED */ |
| 11173 | } |
| 11174 | } |
| 11175 | if (!try) { |
| 11176 | close(fd); |
| 11177 | if (flags & REDIR_PUSH) { |
| 11178 | sv->renamed[fd] = i; |
| 11179 | } |
| 11180 | } |
| 11181 | } else if (fd != newfd) { |
| 11182 | close(fd); |
| 11183 | } |
| 11184 | if (fd == 0) |
| 11185 | fd0_redirected++; |
| 11186 | if (!try) |
| 11187 | dupredirect(n, newfd, fd1dup); |
| 11188 | INTON; |
| 11189 | } |
| 11190 | } |
| 11191 | |
| 11192 | |
| 11193 | static void |
| 11194 | dupredirect(const union node *redir, int f, int fd1dup) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11195 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11196 | int fd = redir->nfile.fd; |
| 11197 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11198 | if(fd==1) |
| 11199 | fd1dup = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11200 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11201 | if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11202 | if (redir->ndup.dupfd!=1 || fd1dup!=1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11203 | dup_as_newfd(redir->ndup.dupfd, fd); |
| 11204 | } |
| 11205 | return; |
| 11206 | } |
| 11207 | |
| 11208 | if (f != fd) { |
| 11209 | dup_as_newfd(f, fd); |
| 11210 | close(f); |
| 11211 | } |
| 11212 | return; |
| 11213 | } |
| 11214 | |
| 11215 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11216 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11217 | /* |
| 11218 | * Undo the effects of the last redirection. |
| 11219 | */ |
| 11220 | |
| 11221 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11222 | popredir(void) |
| 11223 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11224 | struct redirtab *rp = redirlist; |
| 11225 | int i; |
| 11226 | |
| 11227 | INTOFF; |
| 11228 | for (i = 0 ; i < 10 ; i++) { |
| 11229 | if (rp->renamed[i] != EMPTY) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11230 | if (i == 0) |
| 11231 | fd0_redirected--; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11232 | close(i); |
| 11233 | if (rp->renamed[i] >= 0) { |
| 11234 | dup_as_newfd(rp->renamed[i], i); |
| 11235 | close(rp->renamed[i]); |
| 11236 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11237 | } |
| 11238 | } |
| 11239 | redirlist = rp->next; |
| 11240 | ckfree(rp); |
| 11241 | INTON; |
| 11242 | } |
| 11243 | |
| 11244 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11245 | * Discard all saved file descriptors. |
| 11246 | */ |
| 11247 | |
| 11248 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11249 | clearredir(void) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11250 | struct redirtab *rp; |
| 11251 | int i; |
| 11252 | |
| 11253 | for (rp = redirlist ; rp ; rp = rp->next) { |
| 11254 | for (i = 0 ; i < 10 ; i++) { |
| 11255 | if (rp->renamed[i] >= 0) { |
| 11256 | close(rp->renamed[i]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11257 | } |
| 11258 | rp->renamed[i] = EMPTY; |
| 11259 | } |
| 11260 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11261 | } |
| 11262 | |
| 11263 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11264 | /* |
| 11265 | * Copy a file descriptor to be >= to. Returns -1 |
| 11266 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 11267 | * file descriptors left. |
| 11268 | */ |
| 11269 | |
| 11270 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11271 | dup_as_newfd(int from, int to) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11272 | { |
| 11273 | int newfd; |
| 11274 | |
| 11275 | newfd = fcntl(from, F_DUPFD, to); |
| 11276 | if (newfd < 0) { |
| 11277 | if (errno == EMFILE) |
| 11278 | return EMPTY; |
| 11279 | else |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11280 | error("%d: %m", from); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11281 | } |
| 11282 | return newfd; |
| 11283 | } |
| 11284 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11285 | #ifdef DEBUG |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 11286 | /* |
| 11287 | * Debugging stuff. |
| 11288 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11289 | static void shtree (union node *, int, char *, FILE*); |
| 11290 | static void shcmd (union node *, FILE *); |
| 11291 | static void sharg (union node *, FILE *); |
| 11292 | static void indent (int, char *, FILE *); |
| 11293 | static void trstring (char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11294 | |
| 11295 | |
| 11296 | static void |
| 11297 | showtree(n) |
Eric Andersen | 69a20f0 | 2001-10-31 10:40:37 +0000 | [diff] [blame] | 11298 | unode *n; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11299 | { |
| 11300 | trputs("showtree called\n"); |
| 11301 | shtree(n, 1, NULL, stdout); |
| 11302 | } |
| 11303 | |
| 11304 | |
| 11305 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11306 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11307 | { |
| 11308 | struct nodelist *lp; |
| 11309 | const char *s; |
| 11310 | |
| 11311 | if (n == NULL) |
| 11312 | return; |
| 11313 | |
| 11314 | indent(ind, pfx, fp); |
| 11315 | switch(n->type) { |
| 11316 | case NSEMI: |
| 11317 | s = "; "; |
| 11318 | goto binop; |
| 11319 | case NAND: |
| 11320 | s = " && "; |
| 11321 | goto binop; |
| 11322 | case NOR: |
| 11323 | s = " || "; |
| 11324 | binop: |
| 11325 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 11326 | /* if (ind < 0) */ |
| 11327 | fputs(s, fp); |
| 11328 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 11329 | break; |
| 11330 | case NCMD: |
| 11331 | shcmd(n, fp); |
| 11332 | if (ind >= 0) |
| 11333 | putc('\n', fp); |
| 11334 | break; |
| 11335 | case NPIPE: |
| 11336 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { |
| 11337 | shcmd(lp->n, fp); |
| 11338 | if (lp->next) |
| 11339 | fputs(" | ", fp); |
| 11340 | } |
| 11341 | if (n->npipe.backgnd) |
| 11342 | fputs(" &", fp); |
| 11343 | if (ind >= 0) |
| 11344 | putc('\n', fp); |
| 11345 | break; |
| 11346 | default: |
| 11347 | fprintf(fp, "<node type %d>", n->type); |
| 11348 | if (ind >= 0) |
| 11349 | putc('\n', fp); |
| 11350 | break; |
| 11351 | } |
| 11352 | } |
| 11353 | |
| 11354 | |
| 11355 | |
| 11356 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11357 | shcmd(union node *cmd, FILE *fp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11358 | { |
| 11359 | union node *np; |
| 11360 | int first; |
| 11361 | const char *s; |
| 11362 | int dftfd; |
| 11363 | |
| 11364 | first = 1; |
| 11365 | for (np = cmd->ncmd.args ; np ; np = np->narg.next) { |
| 11366 | if (! first) |
| 11367 | putchar(' '); |
| 11368 | sharg(np, fp); |
| 11369 | first = 0; |
| 11370 | } |
| 11371 | for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) { |
| 11372 | if (! first) |
| 11373 | putchar(' '); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 11374 | #if 1 |
| 11375 | s = "*error*"; |
| 11376 | dftfd = 0; |
| 11377 | if ((np->nfile.type <= NFROMFD) && (np->nfile.type >= NTO)) { |
| 11378 | s = redir_strings[np->nfile.type - NTO]; |
| 11379 | if (*s == '>') { |
| 11380 | dftfd = 1; |
| 11381 | } |
| 11382 | } |
| 11383 | #else |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11384 | switch (np->nfile.type) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11385 | case NTO: s = ">"; dftfd = 1; break; |
| 11386 | case NAPPEND: s = ">>"; dftfd = 1; break; |
| 11387 | case NTOFD: s = ">&"; dftfd = 1; break; |
| 11388 | case NTOOV: s = ">|"; dftfd = 1; break; |
| 11389 | case NFROM: s = "<"; dftfd = 0; break; |
| 11390 | case NFROMFD: s = "<&"; dftfd = 0; break; |
| 11391 | case NFROMTO: s = "<>"; dftfd = 0; break; |
| 11392 | default: s = "*error*"; dftfd = 0; break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11393 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 11394 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11395 | if (np->nfile.fd != dftfd) |
| 11396 | fprintf(fp, "%d", np->nfile.fd); |
| 11397 | fputs(s, fp); |
| 11398 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 11399 | fprintf(fp, "%d", np->ndup.dupfd); |
| 11400 | } else { |
| 11401 | sharg(np->nfile.fname, fp); |
| 11402 | } |
| 11403 | first = 0; |
| 11404 | } |
| 11405 | } |
| 11406 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11407 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11408 | sharg(union node *arg, FILE *fp) |
| 11409 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11410 | char *p; |
| 11411 | struct nodelist *bqlist; |
| 11412 | int subtype; |
| 11413 | |
| 11414 | if (arg->type != NARG) { |
| 11415 | printf("<node type %d>\n", arg->type); |
| 11416 | fflush(stdout); |
| 11417 | abort(); |
| 11418 | } |
| 11419 | bqlist = arg->narg.backquote; |
| 11420 | for (p = arg->narg.text ; *p ; p++) { |
| 11421 | switch (*p) { |
| 11422 | case CTLESC: |
| 11423 | putc(*++p, fp); |
| 11424 | break; |
| 11425 | case CTLVAR: |
| 11426 | putc('$', fp); |
| 11427 | putc('{', fp); |
| 11428 | subtype = *++p; |
| 11429 | if (subtype == VSLENGTH) |
| 11430 | putc('#', fp); |
| 11431 | |
| 11432 | while (*p != '=') |
| 11433 | putc(*p++, fp); |
| 11434 | |
| 11435 | if (subtype & VSNUL) |
| 11436 | putc(':', fp); |
| 11437 | |
| 11438 | switch (subtype & VSTYPE) { |
| 11439 | case VSNORMAL: |
| 11440 | putc('}', fp); |
| 11441 | break; |
| 11442 | case VSMINUS: |
| 11443 | putc('-', fp); |
| 11444 | break; |
| 11445 | case VSPLUS: |
| 11446 | putc('+', fp); |
| 11447 | break; |
| 11448 | case VSQUESTION: |
| 11449 | putc('?', fp); |
| 11450 | break; |
| 11451 | case VSASSIGN: |
| 11452 | putc('=', fp); |
| 11453 | break; |
| 11454 | case VSTRIMLEFT: |
| 11455 | putc('#', fp); |
| 11456 | break; |
| 11457 | case VSTRIMLEFTMAX: |
| 11458 | putc('#', fp); |
| 11459 | putc('#', fp); |
| 11460 | break; |
| 11461 | case VSTRIMRIGHT: |
| 11462 | putc('%', fp); |
| 11463 | break; |
| 11464 | case VSTRIMRIGHTMAX: |
| 11465 | putc('%', fp); |
| 11466 | putc('%', fp); |
| 11467 | break; |
| 11468 | case VSLENGTH: |
| 11469 | break; |
| 11470 | default: |
| 11471 | printf("<subtype %d>", subtype); |
| 11472 | } |
| 11473 | break; |
| 11474 | case CTLENDVAR: |
| 11475 | putc('}', fp); |
| 11476 | break; |
| 11477 | case CTLBACKQ: |
| 11478 | case CTLBACKQ|CTLQUOTE: |
| 11479 | putc('$', fp); |
| 11480 | putc('(', fp); |
| 11481 | shtree(bqlist->n, -1, NULL, fp); |
| 11482 | putc(')', fp); |
| 11483 | break; |
| 11484 | default: |
| 11485 | putc(*p, fp); |
| 11486 | break; |
| 11487 | } |
| 11488 | } |
| 11489 | } |
| 11490 | |
| 11491 | |
| 11492 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11493 | indent(int amount, char *pfx, FILE *fp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11494 | { |
| 11495 | int i; |
| 11496 | |
| 11497 | for (i = 0 ; i < amount ; i++) { |
| 11498 | if (pfx && i == amount - 1) |
| 11499 | fputs(pfx, fp); |
| 11500 | putc('\t', fp); |
| 11501 | } |
| 11502 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11503 | |
| 11504 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11505 | FILE *tracefile; |
| 11506 | |
| 11507 | #if DEBUG == 2 |
| 11508 | static int debug = 1; |
| 11509 | #else |
| 11510 | static int debug = 0; |
| 11511 | #endif |
| 11512 | |
| 11513 | |
| 11514 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11515 | trputc(int c) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11516 | { |
| 11517 | if (tracefile == NULL) |
| 11518 | return; |
| 11519 | putc(c, tracefile); |
| 11520 | if (c == '\n') |
| 11521 | fflush(tracefile); |
| 11522 | } |
| 11523 | |
| 11524 | static void |
| 11525 | trace(const char *fmt, ...) |
| 11526 | { |
| 11527 | va_list va; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11528 | va_start(va, fmt); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11529 | if (tracefile != NULL) { |
| 11530 | (void) vfprintf(tracefile, fmt, va); |
| 11531 | if (strchr(fmt, '\n')) |
| 11532 | (void) fflush(tracefile); |
| 11533 | } |
| 11534 | va_end(va); |
| 11535 | } |
| 11536 | |
| 11537 | |
| 11538 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11539 | trputs(const char *s) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11540 | { |
| 11541 | if (tracefile == NULL) |
| 11542 | return; |
| 11543 | fputs(s, tracefile); |
| 11544 | if (strchr(s, '\n')) |
| 11545 | fflush(tracefile); |
| 11546 | } |
| 11547 | |
| 11548 | |
| 11549 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11550 | trstring(char *s) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11551 | { |
| 11552 | char *p; |
| 11553 | char c; |
| 11554 | |
| 11555 | if (tracefile == NULL) |
| 11556 | return; |
| 11557 | putc('"', tracefile); |
| 11558 | for (p = s ; *p ; p++) { |
| 11559 | switch (*p) { |
| 11560 | case '\n': c = 'n'; goto backslash; |
| 11561 | case '\t': c = 't'; goto backslash; |
| 11562 | case '\r': c = 'r'; goto backslash; |
| 11563 | case '"': c = '"'; goto backslash; |
| 11564 | case '\\': c = '\\'; goto backslash; |
| 11565 | case CTLESC: c = 'e'; goto backslash; |
| 11566 | case CTLVAR: c = 'v'; goto backslash; |
| 11567 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash; |
| 11568 | case CTLBACKQ: c = 'q'; goto backslash; |
| 11569 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11570 | backslash: putc('\\', tracefile); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11571 | putc(c, tracefile); |
| 11572 | break; |
| 11573 | default: |
| 11574 | if (*p >= ' ' && *p <= '~') |
| 11575 | putc(*p, tracefile); |
| 11576 | else { |
| 11577 | putc('\\', tracefile); |
| 11578 | putc(*p >> 6 & 03, tracefile); |
| 11579 | putc(*p >> 3 & 07, tracefile); |
| 11580 | putc(*p & 07, tracefile); |
| 11581 | } |
| 11582 | break; |
| 11583 | } |
| 11584 | } |
| 11585 | putc('"', tracefile); |
| 11586 | } |
| 11587 | |
| 11588 | |
| 11589 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11590 | trargs(char **ap) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11591 | { |
| 11592 | if (tracefile == NULL) |
| 11593 | return; |
| 11594 | while (*ap) { |
| 11595 | trstring(*ap++); |
| 11596 | if (*ap) |
| 11597 | putc(' ', tracefile); |
| 11598 | else |
| 11599 | putc('\n', tracefile); |
| 11600 | } |
| 11601 | fflush(tracefile); |
| 11602 | } |
| 11603 | |
| 11604 | |
| 11605 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11606 | opentrace() |
| 11607 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11608 | char s[100]; |
| 11609 | #ifdef O_APPEND |
| 11610 | int flags; |
| 11611 | #endif |
| 11612 | |
| 11613 | if (!debug) |
| 11614 | return; |
| 11615 | #ifdef not_this_way |
| 11616 | { |
| 11617 | char *p; |
| 11618 | if ((p = getenv("HOME")) == NULL) { |
| 11619 | if (geteuid() == 0) |
| 11620 | p = "/"; |
| 11621 | else |
| 11622 | p = "/tmp"; |
| 11623 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11624 | strcpy(s, p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11625 | strcat(s, "/trace"); |
| 11626 | } |
| 11627 | #else |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11628 | strcpy(s, "./trace"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11629 | #endif /* not_this_way */ |
Matt Kraai | a5f09c6 | 2001-11-12 16:44:55 +0000 | [diff] [blame] | 11630 | if ((tracefile = wfopen(s, "a")) == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11631 | return; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11632 | #ifdef O_APPEND |
| 11633 | if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0) |
| 11634 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 11635 | #endif |
| 11636 | fputs("\nTracing started.\n", tracefile); |
| 11637 | fflush(tracefile); |
| 11638 | } |
| 11639 | #endif /* DEBUG */ |
| 11640 | |
| 11641 | |
| 11642 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11643 | * The trap builtin. |
| 11644 | */ |
| 11645 | |
| 11646 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11647 | trapcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11648 | { |
| 11649 | char *action; |
| 11650 | char **ap; |
| 11651 | int signo; |
| 11652 | |
| 11653 | if (argc <= 1) { |
| 11654 | for (signo = 0 ; signo < NSIG ; signo++) { |
| 11655 | if (trap[signo] != NULL) { |
| 11656 | char *p; |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11657 | const char *sn; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11658 | |
| 11659 | p = single_quote(trap[signo]); |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11660 | sn = sys_siglist[signo]; |
| 11661 | if(sn==NULL) |
| 11662 | sn = u_signal_names(0, &signo, 0); |
| 11663 | if(sn==NULL) |
| 11664 | sn = "???"; |
| 11665 | printf("trap -- %s %s\n", p, sn); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11666 | stunalloc(p); |
| 11667 | } |
| 11668 | } |
| 11669 | return 0; |
| 11670 | } |
| 11671 | ap = argv + 1; |
| 11672 | if (argc == 2) |
| 11673 | action = NULL; |
| 11674 | else |
| 11675 | action = *ap++; |
| 11676 | while (*ap) { |
| 11677 | if ((signo = decode_signal(*ap, 0)) < 0) |
| 11678 | error("%s: bad trap", *ap); |
| 11679 | INTOFF; |
| 11680 | if (action) { |
| 11681 | if (action[0] == '-' && action[1] == '\0') |
| 11682 | action = NULL; |
| 11683 | else |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11684 | action = xstrdup(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11685 | } |
| 11686 | if (trap[signo]) |
| 11687 | ckfree(trap[signo]); |
| 11688 | trap[signo] = action; |
| 11689 | if (signo != 0) |
| 11690 | setsignal(signo); |
| 11691 | INTON; |
| 11692 | ap++; |
| 11693 | } |
| 11694 | return 0; |
| 11695 | } |
| 11696 | |
| 11697 | |
| 11698 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11699 | |
| 11700 | |
| 11701 | |
| 11702 | /* |
| 11703 | * Set the signal handler for the specified signal. The routine figures |
| 11704 | * out what it should be set to. |
| 11705 | */ |
| 11706 | |
| 11707 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11708 | setsignal(int signo) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11709 | { |
| 11710 | int action; |
| 11711 | char *t; |
| 11712 | struct sigaction act; |
| 11713 | |
| 11714 | if ((t = trap[signo]) == NULL) |
| 11715 | action = S_DFL; |
| 11716 | else if (*t != '\0') |
| 11717 | action = S_CATCH; |
| 11718 | else |
| 11719 | action = S_IGN; |
| 11720 | if (rootshell && action == S_DFL) { |
| 11721 | switch (signo) { |
| 11722 | case SIGINT: |
| 11723 | if (iflag || minusc || sflag == 0) |
| 11724 | action = S_CATCH; |
| 11725 | break; |
| 11726 | case SIGQUIT: |
| 11727 | #ifdef DEBUG |
| 11728 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11729 | |
| 11730 | if (debug) |
| 11731 | break; |
| 11732 | } |
| 11733 | #endif |
| 11734 | /* FALLTHROUGH */ |
| 11735 | case SIGTERM: |
| 11736 | if (iflag) |
| 11737 | action = S_IGN; |
| 11738 | break; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 11739 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11740 | case SIGTSTP: |
| 11741 | case SIGTTOU: |
| 11742 | if (mflag) |
| 11743 | action = S_IGN; |
| 11744 | break; |
| 11745 | #endif |
| 11746 | } |
| 11747 | } |
| 11748 | |
| 11749 | t = &sigmode[signo - 1]; |
| 11750 | if (*t == 0) { |
| 11751 | /* |
| 11752 | * current setting unknown |
| 11753 | */ |
| 11754 | if (sigaction(signo, 0, &act) == -1) { |
| 11755 | /* |
| 11756 | * Pretend it worked; maybe we should give a warning |
| 11757 | * here, but other shells don't. We don't alter |
| 11758 | * sigmode, so that we retry every time. |
| 11759 | */ |
| 11760 | return; |
| 11761 | } |
| 11762 | if (act.sa_handler == SIG_IGN) { |
| 11763 | if (mflag && (signo == SIGTSTP || |
| 11764 | signo == SIGTTIN || signo == SIGTTOU)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11765 | *t = S_IGN; /* don't hard ignore these */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11766 | } else |
| 11767 | *t = S_HARD_IGN; |
| 11768 | } else { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11769 | *t = S_RESET; /* force to be set */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11770 | } |
| 11771 | } |
| 11772 | if (*t == S_HARD_IGN || *t == action) |
| 11773 | return; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 11774 | act.sa_handler = ((action == S_CATCH) ? onsig |
| 11775 | : ((action == S_IGN) ? SIG_IGN : SIG_DFL)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11776 | *t = action; |
| 11777 | act.sa_flags = 0; |
| 11778 | sigemptyset(&act.sa_mask); |
| 11779 | sigaction(signo, &act, 0); |
| 11780 | } |
| 11781 | |
| 11782 | /* |
| 11783 | * Ignore a signal. |
| 11784 | */ |
| 11785 | |
| 11786 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11787 | ignoresig(int signo) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11788 | { |
| 11789 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 11790 | signal(signo, SIG_IGN); |
| 11791 | } |
| 11792 | sigmode[signo - 1] = S_HARD_IGN; |
| 11793 | } |
| 11794 | |
| 11795 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11796 | /* |
| 11797 | * Signal handler. |
| 11798 | */ |
| 11799 | |
| 11800 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11801 | onsig(int signo) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11802 | { |
| 11803 | if (signo == SIGINT && trap[SIGINT] == NULL) { |
| 11804 | onint(); |
| 11805 | return; |
| 11806 | } |
| 11807 | gotsig[signo - 1] = 1; |
| 11808 | pendingsigs++; |
| 11809 | } |
| 11810 | |
| 11811 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11812 | /* |
| 11813 | * Called to execute a trap. Perhaps we should avoid entering new trap |
| 11814 | * handlers while we are executing a trap handler. |
| 11815 | */ |
| 11816 | |
| 11817 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11818 | dotrap(void) |
| 11819 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11820 | int i; |
| 11821 | int savestatus; |
| 11822 | |
| 11823 | for (;;) { |
| 11824 | for (i = 1 ; ; i++) { |
| 11825 | if (gotsig[i - 1]) |
| 11826 | break; |
| 11827 | if (i >= NSIG - 1) |
| 11828 | goto done; |
| 11829 | } |
| 11830 | gotsig[i - 1] = 0; |
| 11831 | savestatus=exitstatus; |
| 11832 | evalstring(trap[i], 0); |
| 11833 | exitstatus=savestatus; |
| 11834 | } |
| 11835 | done: |
| 11836 | pendingsigs = 0; |
| 11837 | } |
| 11838 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11839 | /* |
| 11840 | * Called to exit the shell. |
| 11841 | */ |
| 11842 | |
| 11843 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11844 | exitshell(int status) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11845 | { |
| 11846 | struct jmploc loc1, loc2; |
| 11847 | char *p; |
| 11848 | |
| 11849 | TRACE(("exitshell(%d) pid=%d\n", status, getpid())); |
| 11850 | if (setjmp(loc1.loc)) { |
| 11851 | goto l1; |
| 11852 | } |
| 11853 | if (setjmp(loc2.loc)) { |
| 11854 | goto l2; |
| 11855 | } |
| 11856 | handler = &loc1; |
| 11857 | if ((p = trap[0]) != NULL && *p != '\0') { |
| 11858 | trap[0] = NULL; |
| 11859 | evalstring(p, 0); |
| 11860 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11861 | l1: handler = &loc2; /* probably unnecessary */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11862 | flushall(); |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 11863 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11864 | setjobctl(0); |
| 11865 | #endif |
| 11866 | l2: _exit(status); |
| 11867 | /* NOTREACHED */ |
| 11868 | } |
| 11869 | |
| 11870 | static int decode_signal(const char *string, int minsig) |
| 11871 | { |
| 11872 | int signo; |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11873 | const char *name = u_signal_names(string, &signo, minsig); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11874 | |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11875 | return name ? signo : -1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11876 | } |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11877 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11878 | static struct var **hashvar (const char *); |
| 11879 | static void showvars (const char *, int, int); |
| 11880 | static struct var **findvar (struct var **, const char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11881 | |
| 11882 | /* |
| 11883 | * Initialize the varable symbol tables and import the environment |
| 11884 | */ |
| 11885 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11886 | /* |
| 11887 | * This routine initializes the builtin variables. It is called when the |
| 11888 | * shell is initialized and again when a shell procedure is spawned. |
| 11889 | */ |
| 11890 | |
| 11891 | static void |
| 11892 | initvar() { |
| 11893 | const struct varinit *ip; |
| 11894 | struct var *vp; |
| 11895 | struct var **vpp; |
| 11896 | |
| 11897 | for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { |
| 11898 | if ((vp->flags & VEXPORT) == 0) { |
| 11899 | vpp = hashvar(ip->text); |
| 11900 | vp->next = *vpp; |
| 11901 | *vpp = vp; |
Matt Kraai | c822763 | 2001-11-12 16:57:27 +0000 | [diff] [blame] | 11902 | vp->text = xstrdup(ip->text); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11903 | vp->flags = ip->flags; |
| 11904 | vp->func = ip->func; |
| 11905 | } |
| 11906 | } |
Tim Riker | 497a885 | 2002-04-13 05:37:10 +0000 | [diff] [blame] | 11907 | #if !defined(CONFIG_FEATURE_COMMAND_EDITING) || !defined(CONFIG_FEATURE_SH_FANCY_PROMPT) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11908 | /* |
| 11909 | * PS1 depends on uid |
| 11910 | */ |
| 11911 | if ((vps1.flags & VEXPORT) == 0) { |
Aaron Lehmann | 9a218bf | 2001-12-31 06:13:38 +0000 | [diff] [blame] | 11912 | vpp = hashvar("PS1=$"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11913 | vps1.next = *vpp; |
| 11914 | *vpp = &vps1; |
Matt Kraai | c822763 | 2001-11-12 16:57:27 +0000 | [diff] [blame] | 11915 | vps1.text = xstrdup(geteuid() ? "PS1=$ " : "PS1=# "); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11916 | vps1.flags = VSTRFIXED|VTEXTFIXED; |
| 11917 | } |
Tim Riker | 497a885 | 2002-04-13 05:37:10 +0000 | [diff] [blame] | 11918 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11919 | } |
| 11920 | |
| 11921 | /* |
| 11922 | * Set the value of a variable. The flags argument is ored with the |
| 11923 | * flags of the variable. If val is NULL, the variable is unset. |
| 11924 | */ |
| 11925 | |
| 11926 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11927 | setvar(const char *name, const char *val, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11928 | { |
| 11929 | const char *p; |
| 11930 | int len; |
| 11931 | int namelen; |
| 11932 | char *nameeq; |
| 11933 | int isbad; |
| 11934 | int vallen = 0; |
| 11935 | |
| 11936 | isbad = 0; |
| 11937 | p = name; |
| 11938 | if (! is_name(*p)) |
| 11939 | isbad = 1; |
| 11940 | p++; |
| 11941 | for (;;) { |
| 11942 | if (! is_in_name(*p)) { |
| 11943 | if (*p == '\0' || *p == '=') |
| 11944 | break; |
| 11945 | isbad = 1; |
| 11946 | } |
| 11947 | p++; |
| 11948 | } |
| 11949 | namelen = p - name; |
| 11950 | if (isbad) |
| 11951 | error("%.*s: bad variable name", namelen, name); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11952 | len = namelen + 2; /* 2 is space for '=' and '\0' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11953 | if (val == NULL) { |
| 11954 | flags |= VUNSET; |
| 11955 | } else { |
| 11956 | len += vallen = strlen(val); |
| 11957 | } |
| 11958 | INTOFF; |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11959 | nameeq = xmalloc(len); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11960 | memcpy(nameeq, name, namelen); |
| 11961 | nameeq[namelen] = '='; |
| 11962 | if (val) { |
| 11963 | memcpy(nameeq + namelen + 1, val, vallen + 1); |
| 11964 | } else { |
| 11965 | nameeq[namelen + 1] = '\0'; |
| 11966 | } |
| 11967 | setvareq(nameeq, flags); |
| 11968 | INTON; |
| 11969 | } |
| 11970 | |
| 11971 | |
| 11972 | |
| 11973 | /* |
| 11974 | * Same as setvar except that the variable and value are passed in |
| 11975 | * the first argument as name=value. Since the first argument will |
| 11976 | * be actually stored in the table, it should not be a string that |
| 11977 | * will go away. |
| 11978 | */ |
| 11979 | |
| 11980 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11981 | setvareq(char *s, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11982 | { |
| 11983 | struct var *vp, **vpp; |
| 11984 | |
| 11985 | vpp = hashvar(s); |
| 11986 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
| 11987 | if ((vp = *findvar(vpp, s))) { |
| 11988 | if (vp->flags & VREADONLY) { |
| 11989 | size_t len = strchr(s, '=') - s; |
| 11990 | error("%.*s: is read only", len, s); |
| 11991 | } |
| 11992 | INTOFF; |
| 11993 | |
| 11994 | if (vp->func && (flags & VNOFUNC) == 0) |
| 11995 | (*vp->func)(strchr(s, '=') + 1); |
| 11996 | |
| 11997 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 11998 | ckfree(vp->text); |
| 11999 | |
| 12000 | vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); |
| 12001 | vp->flags |= flags; |
| 12002 | vp->text = s; |
| 12003 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 12004 | #ifdef CONFIG_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12005 | /* |
| 12006 | * We could roll this to a function, to handle it as |
| 12007 | * a regular variable function callback, but why bother? |
| 12008 | */ |
| 12009 | if (iflag && (vp == &vmpath || (vp == &vmail && !mpathset()))) |
| 12010 | chkmail(1); |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 12011 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12012 | INTON; |
| 12013 | return; |
| 12014 | } |
| 12015 | /* not found */ |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12016 | vp = xmalloc(sizeof (*vp)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12017 | vp->flags = flags; |
| 12018 | vp->text = s; |
| 12019 | vp->next = *vpp; |
| 12020 | vp->func = NULL; |
| 12021 | *vpp = vp; |
| 12022 | } |
| 12023 | |
| 12024 | |
| 12025 | |
| 12026 | /* |
| 12027 | * Process a linked list of variable assignments. |
| 12028 | */ |
| 12029 | |
| 12030 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12031 | listsetvar(struct strlist *mylist) |
| 12032 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12033 | struct strlist *lp; |
| 12034 | |
| 12035 | INTOFF; |
| 12036 | for (lp = mylist ; lp ; lp = lp->next) { |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12037 | setvareq(xstrdup(lp->text), 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12038 | } |
| 12039 | INTON; |
| 12040 | } |
| 12041 | |
| 12042 | |
| 12043 | |
| 12044 | /* |
| 12045 | * Find the value of a variable. Returns NULL if not set. |
| 12046 | */ |
| 12047 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 12048 | static const char * |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12049 | lookupvar(const char *name) |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 12050 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12051 | struct var *v; |
| 12052 | |
| 12053 | if ((v = *findvar(hashvar(name), name)) && !(v->flags & VUNSET)) { |
| 12054 | return strchr(v->text, '=') + 1; |
| 12055 | } |
| 12056 | return NULL; |
| 12057 | } |
| 12058 | |
| 12059 | |
| 12060 | |
| 12061 | /* |
| 12062 | * Search the environment of a builtin command. |
| 12063 | */ |
| 12064 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 12065 | static const char * |
| 12066 | bltinlookup(const char *name) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12067 | { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 12068 | const struct strlist *sp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12069 | |
| 12070 | for (sp = cmdenviron ; sp ; sp = sp->next) { |
| 12071 | if (varequal(sp->text, name)) |
| 12072 | return strchr(sp->text, '=') + 1; |
| 12073 | } |
| 12074 | return lookupvar(name); |
| 12075 | } |
| 12076 | |
| 12077 | |
| 12078 | |
| 12079 | /* |
| 12080 | * Generate a list of exported variables. This routine is used to construct |
| 12081 | * the third argument to execve when executing a program. |
| 12082 | */ |
| 12083 | |
| 12084 | static char ** |
| 12085 | environment() { |
| 12086 | int nenv; |
| 12087 | struct var **vpp; |
| 12088 | struct var *vp; |
| 12089 | char **env; |
| 12090 | char **ep; |
| 12091 | |
| 12092 | nenv = 0; |
| 12093 | for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { |
| 12094 | for (vp = *vpp ; vp ; vp = vp->next) |
| 12095 | if (vp->flags & VEXPORT) |
| 12096 | nenv++; |
| 12097 | } |
| 12098 | ep = env = stalloc((nenv + 1) * sizeof *env); |
| 12099 | for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { |
| 12100 | for (vp = *vpp ; vp ; vp = vp->next) |
| 12101 | if (vp->flags & VEXPORT) |
| 12102 | *ep++ = vp->text; |
| 12103 | } |
| 12104 | *ep = NULL; |
| 12105 | return env; |
| 12106 | } |
| 12107 | |
| 12108 | |
| 12109 | /* |
| 12110 | * Called when a shell procedure is invoked to clear out nonexported |
| 12111 | * variables. It is also necessary to reallocate variables of with |
| 12112 | * VSTACK set since these are currently allocated on the stack. |
| 12113 | */ |
| 12114 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12115 | static void |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 12116 | shprocvar(void) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12117 | struct var **vpp; |
| 12118 | struct var *vp, **prev; |
| 12119 | |
| 12120 | for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { |
| 12121 | for (prev = vpp ; (vp = *prev) != NULL ; ) { |
| 12122 | if ((vp->flags & VEXPORT) == 0) { |
| 12123 | *prev = vp->next; |
| 12124 | if ((vp->flags & VTEXTFIXED) == 0) |
| 12125 | ckfree(vp->text); |
| 12126 | if ((vp->flags & VSTRFIXED) == 0) |
| 12127 | ckfree(vp); |
| 12128 | } else { |
| 12129 | if (vp->flags & VSTACK) { |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12130 | vp->text = xstrdup(vp->text); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12131 | vp->flags &=~ VSTACK; |
| 12132 | } |
| 12133 | prev = &vp->next; |
| 12134 | } |
| 12135 | } |
| 12136 | } |
| 12137 | initvar(); |
| 12138 | } |
| 12139 | |
| 12140 | |
| 12141 | |
| 12142 | /* |
| 12143 | * Command to list all variables which are set. Currently this command |
| 12144 | * is invoked from the set command when the set command is called without |
| 12145 | * any variables. |
| 12146 | */ |
| 12147 | |
| 12148 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12149 | showvarscmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12150 | { |
| 12151 | showvars(nullstr, VUNSET, VUNSET); |
| 12152 | return 0; |
| 12153 | } |
| 12154 | |
| 12155 | |
| 12156 | |
| 12157 | /* |
| 12158 | * The export and readonly commands. |
| 12159 | */ |
| 12160 | |
| 12161 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12162 | exportcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12163 | { |
| 12164 | struct var *vp; |
| 12165 | char *name; |
| 12166 | const char *p; |
| 12167 | int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; |
| 12168 | int pflag; |
| 12169 | |
| 12170 | listsetvar(cmdenviron); |
| 12171 | pflag = (nextopt("p") == 'p'); |
| 12172 | if (argc > 1 && !pflag) { |
| 12173 | while ((name = *argptr++) != NULL) { |
| 12174 | if ((p = strchr(name, '=')) != NULL) { |
| 12175 | p++; |
| 12176 | } else { |
| 12177 | if ((vp = *findvar(hashvar(name), name))) { |
| 12178 | vp->flags |= flag; |
| 12179 | goto found; |
| 12180 | } |
| 12181 | } |
| 12182 | setvar(name, p, flag); |
| 12183 | found:; |
| 12184 | } |
| 12185 | } else { |
| 12186 | showvars(argv[0], flag, 0); |
| 12187 | } |
| 12188 | return 0; |
| 12189 | } |
| 12190 | |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 12191 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12192 | /* |
| 12193 | * The "local" command. |
| 12194 | */ |
| 12195 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 12196 | /* funcnest nonzero if we are currently evaluating a function */ |
| 12197 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12198 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12199 | localcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12200 | { |
| 12201 | char *name; |
| 12202 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 12203 | if (! funcnest) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12204 | error("Not in a function"); |
| 12205 | while ((name = *argptr++) != NULL) { |
| 12206 | mklocal(name); |
| 12207 | } |
| 12208 | return 0; |
| 12209 | } |
| 12210 | |
| 12211 | |
| 12212 | /* |
| 12213 | * Make a variable a local variable. When a variable is made local, it's |
| 12214 | * value and flags are saved in a localvar structure. The saved values |
| 12215 | * will be restored when the shell function returns. We handle the name |
| 12216 | * "-" as a special case. |
| 12217 | */ |
| 12218 | |
| 12219 | static void |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12220 | mklocal(char *name) |
| 12221 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12222 | struct localvar *lvp; |
| 12223 | struct var **vpp; |
| 12224 | struct var *vp; |
| 12225 | |
| 12226 | INTOFF; |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12227 | lvp = xmalloc(sizeof (struct localvar)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12228 | if (name[0] == '-' && name[1] == '\0') { |
| 12229 | char *p; |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12230 | p = xmalloc(sizeof optet_vals); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 12231 | lvp->text = memcpy(p, optet_vals, sizeof optet_vals); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12232 | vp = NULL; |
| 12233 | } else { |
| 12234 | vpp = hashvar(name); |
| 12235 | vp = *findvar(vpp, name); |
| 12236 | if (vp == NULL) { |
| 12237 | if (strchr(name, '=')) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12238 | setvareq(xstrdup(name), VSTRFIXED); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12239 | else |
| 12240 | setvar(name, NULL, VSTRFIXED); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 12241 | vp = *vpp; /* the new variable */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12242 | lvp->text = NULL; |
| 12243 | lvp->flags = VUNSET; |
| 12244 | } else { |
| 12245 | lvp->text = vp->text; |
| 12246 | lvp->flags = vp->flags; |
| 12247 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 12248 | if (strchr(name, '=')) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12249 | setvareq(xstrdup(name), 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12250 | } |
| 12251 | } |
| 12252 | lvp->vp = vp; |
| 12253 | lvp->next = localvars; |
| 12254 | localvars = lvp; |
| 12255 | INTON; |
| 12256 | } |
| 12257 | |
| 12258 | |
| 12259 | /* |
| 12260 | * Called after a function returns. |
| 12261 | */ |
| 12262 | |
| 12263 | static void |
| 12264 | poplocalvars() { |
| 12265 | struct localvar *lvp; |
| 12266 | struct var *vp; |
| 12267 | |
| 12268 | while ((lvp = localvars) != NULL) { |
| 12269 | localvars = lvp->next; |
| 12270 | vp = lvp->vp; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 12271 | if (vp == NULL) { /* $- saved */ |
| 12272 | memcpy(optet_vals, lvp->text, sizeof optet_vals); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12273 | ckfree(lvp->text); |
| 12274 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
| 12275 | (void)unsetvar(vp->text); |
| 12276 | } else { |
| 12277 | if ((vp->flags & VTEXTFIXED) == 0) |
| 12278 | ckfree(vp->text); |
| 12279 | vp->flags = lvp->flags; |
| 12280 | vp->text = lvp->text; |
| 12281 | } |
| 12282 | ckfree(lvp); |
| 12283 | } |
| 12284 | } |
| 12285 | |
| 12286 | |
| 12287 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12288 | setvarcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12289 | { |
| 12290 | if (argc <= 2) |
| 12291 | return unsetcmd(argc, argv); |
| 12292 | else if (argc == 3) |
| 12293 | setvar(argv[1], argv[2], 0); |
| 12294 | else |
| 12295 | error("List assignment not implemented"); |
| 12296 | return 0; |
| 12297 | } |
| 12298 | |
| 12299 | |
| 12300 | /* |
| 12301 | * The unset builtin command. We unset the function before we unset the |
| 12302 | * variable to allow a function to be unset when there is a readonly variable |
| 12303 | * with the same name. |
| 12304 | */ |
| 12305 | |
| 12306 | static int |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12307 | unsetcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12308 | { |
| 12309 | char **ap; |
| 12310 | int i; |
| 12311 | int flg_func = 0; |
| 12312 | int flg_var = 0; |
| 12313 | int ret = 0; |
| 12314 | |
| 12315 | while ((i = nextopt("vf")) != '\0') { |
| 12316 | if (i == 'f') |
| 12317 | flg_func = 1; |
| 12318 | else |
| 12319 | flg_var = 1; |
| 12320 | } |
| 12321 | if (flg_func == 0 && flg_var == 0) |
| 12322 | flg_var = 1; |
| 12323 | |
| 12324 | for (ap = argptr; *ap ; ap++) { |
| 12325 | if (flg_func) |
| 12326 | unsetfunc(*ap); |
| 12327 | if (flg_var) |
| 12328 | ret |= unsetvar(*ap); |
| 12329 | } |
| 12330 | return ret; |
| 12331 | } |
| 12332 | |
| 12333 | |
| 12334 | /* |
| 12335 | * Unset the specified variable. |
| 12336 | */ |
| 12337 | |
| 12338 | static int |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 12339 | unsetvar(const char *s) |
| 12340 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12341 | struct var **vpp; |
| 12342 | struct var *vp; |
| 12343 | |
| 12344 | vpp = findvar(hashvar(s), s); |
| 12345 | vp = *vpp; |
| 12346 | if (vp) { |
| 12347 | if (vp->flags & VREADONLY) |
| 12348 | return (1); |
| 12349 | INTOFF; |
| 12350 | if (*(strchr(vp->text, '=') + 1) != '\0') |
| 12351 | setvar(s, nullstr, 0); |
| 12352 | vp->flags &= ~VEXPORT; |
| 12353 | vp->flags |= VUNSET; |
| 12354 | if ((vp->flags & VSTRFIXED) == 0) { |
| 12355 | if ((vp->flags & VTEXTFIXED) == 0) |
| 12356 | ckfree(vp->text); |
| 12357 | *vpp = vp->next; |
| 12358 | ckfree(vp); |
| 12359 | } |
| 12360 | INTON; |
| 12361 | return (0); |
| 12362 | } |
| 12363 | |
| 12364 | return (0); |
| 12365 | } |
| 12366 | |
| 12367 | |
| 12368 | |
| 12369 | /* |
| 12370 | * Find the appropriate entry in the hash table from the name. |
| 12371 | */ |
| 12372 | |
| 12373 | static struct var ** |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 12374 | hashvar(const char *p) |
| 12375 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12376 | unsigned int hashval; |
| 12377 | |
| 12378 | hashval = ((unsigned char) *p) << 4; |
| 12379 | while (*p && *p != '=') |
| 12380 | hashval += (unsigned char) *p++; |
| 12381 | return &vartab[hashval % VTABSIZE]; |
| 12382 | } |
| 12383 | |
| 12384 | |
| 12385 | |
| 12386 | /* |
| 12387 | * Returns true if the two strings specify the same varable. The first |
| 12388 | * variable name is terminated by '='; the second may be terminated by |
| 12389 | * either '=' or '\0'. |
| 12390 | */ |
| 12391 | |
| 12392 | static int |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 12393 | varequal(const char *p, const char *q) |
| 12394 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12395 | while (*p == *q++) { |
| 12396 | if (*p++ == '=') |
| 12397 | return 1; |
| 12398 | } |
| 12399 | if (*p == '=' && *(q - 1) == '\0') |
| 12400 | return 1; |
| 12401 | return 0; |
| 12402 | } |
| 12403 | |
| 12404 | static void |
| 12405 | showvars(const char *myprefix, int mask, int xor) |
| 12406 | { |
| 12407 | struct var **vpp; |
| 12408 | struct var *vp; |
| 12409 | const char *sep = myprefix == nullstr ? myprefix : spcstr; |
| 12410 | |
| 12411 | for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { |
| 12412 | for (vp = *vpp ; vp ; vp = vp->next) { |
| 12413 | if ((vp->flags & mask) ^ xor) { |
| 12414 | char *p; |
| 12415 | int len; |
| 12416 | |
| 12417 | p = strchr(vp->text, '=') + 1; |
| 12418 | len = p - vp->text; |
| 12419 | p = single_quote(p); |
| 12420 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 12421 | printf("%s%s%.*s%s\n", myprefix, sep, len, |
| 12422 | vp->text, p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12423 | stunalloc(p); |
| 12424 | } |
| 12425 | } |
| 12426 | } |
| 12427 | } |
| 12428 | |
| 12429 | static struct var ** |
| 12430 | findvar(struct var **vpp, const char *name) |
| 12431 | { |
| 12432 | for (; *vpp; vpp = &(*vpp)->next) { |
| 12433 | if (varequal((*vpp)->text, name)) { |
| 12434 | break; |
| 12435 | } |
| 12436 | } |
| 12437 | return vpp; |
| 12438 | } |
| 12439 | |
| 12440 | /* |
| 12441 | * Copyright (c) 1999 Herbert Xu <herbert@debian.org> |
| 12442 | * This file contains code for the times builtin. |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 12443 | * $Id: ash.c,v 1.55 2002/07/11 11:11:51 andersen Exp $ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12444 | */ |
| 12445 | static int timescmd (int argc, char **argv) |
| 12446 | { |
| 12447 | struct tms buf; |
| 12448 | long int clk_tck = sysconf(_SC_CLK_TCK); |
| 12449 | |
| 12450 | times(&buf); |
| 12451 | printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n", |
| 12452 | (int) (buf.tms_utime / clk_tck / 60), |
| 12453 | ((double) buf.tms_utime) / clk_tck, |
| 12454 | (int) (buf.tms_stime / clk_tck / 60), |
| 12455 | ((double) buf.tms_stime) / clk_tck, |
| 12456 | (int) (buf.tms_cutime / clk_tck / 60), |
| 12457 | ((double) buf.tms_cutime) / clk_tck, |
| 12458 | (int) (buf.tms_cstime / clk_tck / 60), |
| 12459 | ((double) buf.tms_cstime) / clk_tck); |
| 12460 | return 0; |
| 12461 | } |
| 12462 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 12463 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | fa1c5aa | 2001-07-31 21:38:23 +0000 | [diff] [blame] | 12464 | /* The let builtin. */ |
| 12465 | int letcmd(int argc, char **argv) |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12466 | { |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 12467 | int errcode; |
Eric Andersen | fa1c5aa | 2001-07-31 21:38:23 +0000 | [diff] [blame] | 12468 | long result=0; |
| 12469 | if (argc == 2) { |
| 12470 | char *tmp, *expression, p[13]; |
| 12471 | expression = strchr(argv[1], '='); |
| 12472 | if (!expression) { |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 12473 | /* Cannot use 'error()' here, or the return code |
| 12474 | * will be incorrect */ |
Eric Andersen | fa1c5aa | 2001-07-31 21:38:23 +0000 | [diff] [blame] | 12475 | out2fmt("sh: let: syntax error: \"%s\"\n", argv[1]); |
| 12476 | return 0; |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12477 | } |
Eric Andersen | fa1c5aa | 2001-07-31 21:38:23 +0000 | [diff] [blame] | 12478 | *expression = '\0'; |
| 12479 | tmp = ++expression; |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 12480 | result = arith(tmp, &errcode); |
| 12481 | if (errcode < 0) { |
| 12482 | /* Cannot use 'error()' here, or the return code |
| 12483 | * will be incorrect */ |
| 12484 | out2fmt("sh: let: "); |
| 12485 | if(errcode == -2) |
| 12486 | out2fmt("divide by zero"); |
| 12487 | else |
| 12488 | out2fmt("syntax error: \"%s=%s\"\n", argv[1], expression); |
Eric Andersen | fa1c5aa | 2001-07-31 21:38:23 +0000 | [diff] [blame] | 12489 | return 0; |
| 12490 | } |
| 12491 | snprintf(p, 12, "%ld", result); |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12492 | setvar(argv[1], xstrdup(p), 0); |
Eric Andersen | fa1c5aa | 2001-07-31 21:38:23 +0000 | [diff] [blame] | 12493 | } else if (argc >= 3) |
| 12494 | synerror("invalid operand"); |
| 12495 | return !result; |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12496 | } |
| 12497 | #endif |
| 12498 | |
| 12499 | |
| 12500 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 12501 | /*- |
| 12502 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 12503 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 12504 | * |
| 12505 | * This code is derived from software contributed to Berkeley by |
| 12506 | * Kenneth Almquist. |
| 12507 | * |
| 12508 | * Redistribution and use in source and binary forms, with or without |
| 12509 | * modification, are permitted provided that the following conditions |
| 12510 | * are met: |
| 12511 | * 1. Redistributions of source code must retain the above copyright |
| 12512 | * notice, this list of conditions and the following disclaimer. |
| 12513 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12514 | * notice, this list of conditions and the following disclaimer in the |
| 12515 | * documentation and/or other materials provided with the distribution. |
| 12516 | * |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 12517 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 12518 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 12519 | * |
| 12520 | * 4. Neither the name of the University nor the names of its contributors |
| 12521 | * may be used to endorse or promote products derived from this software |
| 12522 | * without specific prior written permission. |
| 12523 | * |
| 12524 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 12525 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 12526 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 12527 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 12528 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 12529 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 12530 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 12531 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 12532 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 12533 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 12534 | * SUCH DAMAGE. |
| 12535 | */ |