Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * ash shell port for busybox |
| 4 | * |
| 5 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7 | * |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 8 | * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au> |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 9 | * was re-ported from NetBSD and debianized. |
| 10 | * |
| 11 | * |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12 | * This code is derived from software contributed to Berkeley by |
| 13 | * Kenneth Almquist. |
| 14 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 15 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 16 | * |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 17 | * Original BSD copyright notice is retained at the end of this file. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 20 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 21 | * rewrite arith.y to micro stack based cryptic algorithm by |
| 22 | * Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com> |
| 23 | * |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 24 | * Modified by Paul Mundt <lethal@linux-sh.org> (c) 2004 to support |
| 25 | * dynamic variables. |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 26 | * |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 27 | * Modified by Vladimir Oleynik <dzo@simtreas.ru> (c) 2001-2005 to be |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 28 | * used in busybox and size optimizations, |
| 29 | * rewrote arith (see notes to this), added locale support, |
| 30 | * rewrote dynamic variables. |
| 31 | * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 32 | */ |
| 33 | |
| 34 | |
| 35 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 36 | * The follow should be set to reflect the type of system you have: |
| 37 | * JOBS -> 1 if you have Berkeley job control, 0 otherwise. |
| 38 | * define SYSV if you are running under System V. |
| 39 | * define DEBUG=1 to compile in debugging ('set -o debug' to turn on) |
| 40 | * define DEBUG=2 to compile in and turn on debugging. |
| 41 | * |
| 42 | * When debugging is on, debugging info will be written to ./trace and |
| 43 | * a quit signal will generate a core dump. |
| 44 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 45 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 46 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 47 | |
Eric Andersen | 5bb1677 | 2001-09-06 18:00:41 +0000 | [diff] [blame] | 48 | #define IFS_BROKEN |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 49 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 50 | #define PROFILE 0 |
| 51 | |
| 52 | #ifdef DEBUG |
| 53 | #define _GNU_SOURCE |
| 54 | #endif |
| 55 | |
| 56 | #include <sys/types.h> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 57 | #include <sys/ioctl.h> |
| 58 | #include <sys/param.h> |
| 59 | #include <sys/resource.h> |
| 60 | #include <sys/stat.h> |
| 61 | #include <sys/time.h> |
| 62 | #include <sys/wait.h> |
| 63 | |
| 64 | #include <stdio.h> |
| 65 | #include <stdlib.h> |
| 66 | #include <string.h> |
| 67 | #include <unistd.h> |
| 68 | |
| 69 | #include <stdarg.h> |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 70 | #include <stddef.h> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 71 | #include <assert.h> |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 72 | #include <ctype.h> |
| 73 | #include <dirent.h> |
| 74 | #include <errno.h> |
| 75 | #include <fcntl.h> |
| 76 | #include <limits.h> |
| 77 | #include <paths.h> |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 78 | #include <setjmp.h> |
| 79 | #include <signal.h> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 80 | #include <stdint.h> |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 81 | #include <time.h> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 82 | #include <fnmatch.h> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 83 | |
| 84 | |
Robert Griebl | ea1a63a | 2002-06-04 20:10:23 +0000 | [diff] [blame] | 85 | #include "busybox.h" |
Eric Andersen | 887ca79 | 2002-07-03 23:19:26 +0000 | [diff] [blame] | 86 | #include "pwd_.h" |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 87 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 88 | #ifdef CONFIG_ASH_JOB_CONTROL |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 89 | #define JOBS 1 |
| 90 | #else |
Eric Andersen | ca16204 | 2003-07-29 07:15:17 +0000 | [diff] [blame] | 91 | #undef JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 94 | #if JOBS || defined(CONFIG_ASH_READ_NCHARS) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 95 | #include <termios.h> |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 96 | #endif |
| 97 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 98 | #include "cmdedit.h" |
| 99 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 100 | #ifdef __GLIBC__ |
| 101 | /* glibc sucks */ |
| 102 | static int *dash_errno; |
| 103 | #undef errno |
| 104 | #define errno (*dash_errno) |
| 105 | #endif |
| 106 | |
Eric Andersen | aa1d6cc | 2002-09-30 20:12:32 +0000 | [diff] [blame] | 107 | #if defined(__uClinux__) |
| 108 | #error "Do not even bother, ash will not run on uClinux" |
| 109 | #endif |
| 110 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 111 | #ifdef DEBUG |
| 112 | #define _DIAGASSERT(assert_expr) assert(assert_expr) |
| 113 | #else |
| 114 | #define _DIAGASSERT(assert_expr) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 115 | #endif |
| 116 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 117 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 118 | #ifdef CONFIG_ASH_ALIAS |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 119 | /* alias.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 120 | |
| 121 | #define ALIASINUSE 1 |
| 122 | #define ALIASDEAD 2 |
| 123 | |
| 124 | struct alias { |
| 125 | struct alias *next; |
| 126 | char *name; |
| 127 | char *val; |
| 128 | int flag; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 131 | static struct alias *lookupalias(const char *, int); |
| 132 | static int aliascmd(int, char **); |
| 133 | static int unaliascmd(int, char **); |
| 134 | static void rmaliases(void); |
| 135 | static int unalias(const char *); |
| 136 | static void printalias(const struct alias *); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 137 | #endif |
| 138 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 139 | /* cd.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 140 | |
| 141 | |
| 142 | static void setpwd(const char *, int); |
| 143 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 144 | /* error.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 145 | |
| 146 | |
| 147 | /* |
| 148 | * Types of operations (passed to the errmsg routine). |
| 149 | */ |
| 150 | |
| 151 | |
| 152 | static const char not_found_msg[] = "%s: not found"; |
| 153 | |
| 154 | |
| 155 | #define E_OPEN "No such file" /* opening a file */ |
| 156 | #define E_CREAT "Directory nonexistent" /* creating a file */ |
| 157 | #define E_EXEC not_found_msg+4 /* executing a program */ |
| 158 | |
| 159 | /* |
| 160 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 161 | * jump locations. The global variable handler contains the location to |
| 162 | * jump to when an exception occurs, and the global variable exception |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 163 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 164 | * exception handlers, the user should save the value of handler on entry |
| 165 | * to an inner scope, set handler to point to a jmploc structure for the |
| 166 | * inner scope, and restore handler on exit from the scope. |
| 167 | */ |
| 168 | |
| 169 | struct jmploc { |
| 170 | jmp_buf loc; |
| 171 | }; |
| 172 | |
| 173 | static struct jmploc *handler; |
| 174 | static int exception; |
| 175 | static volatile int suppressint; |
| 176 | static volatile sig_atomic_t intpending; |
| 177 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 178 | /* exceptions */ |
| 179 | #define EXINT 0 /* SIGINT received */ |
| 180 | #define EXERROR 1 /* a generic error */ |
| 181 | #define EXSHELLPROC 2 /* execute a shell procedure */ |
| 182 | #define EXEXEC 3 /* command execution failed */ |
| 183 | #define EXEXIT 4 /* exit the shell */ |
| 184 | #define EXSIG 5 /* trapped signal in wait(1) */ |
| 185 | |
| 186 | |
| 187 | /* do we generate EXSIG events */ |
| 188 | static int exsig; |
| 189 | /* last pending signal */ |
| 190 | static volatile sig_atomic_t pendingsigs; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * These macros allow the user to suspend the handling of interrupt signals |
| 194 | * over a period of time. This is similar to SIGHOLD to or sigblock, but |
| 195 | * much more efficient and portable. (But hacking the kernel is so much |
| 196 | * more fun than worrying about efficiency and portability. :-)) |
| 197 | */ |
| 198 | |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 199 | #define xbarrier() ({ __asm__ __volatile__ ("": : :"memory"); }) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 200 | #define INTOFF \ |
| 201 | ({ \ |
| 202 | suppressint++; \ |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 203 | xbarrier(); \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 204 | 0; \ |
| 205 | }) |
| 206 | #define SAVEINT(v) ((v) = suppressint) |
| 207 | #define RESTOREINT(v) \ |
| 208 | ({ \ |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 209 | xbarrier(); \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 210 | if ((suppressint = (v)) == 0 && intpending) onint(); \ |
| 211 | 0; \ |
| 212 | }) |
| 213 | #define EXSIGON() \ |
| 214 | ({ \ |
| 215 | exsig++; \ |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 216 | xbarrier(); \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 217 | if (pendingsigs) \ |
| 218 | exraise(EXSIG); \ |
| 219 | 0; \ |
| 220 | }) |
| 221 | /* EXSIG is turned off by evalbltin(). */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 222 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 223 | |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 224 | static void exraise(int) ATTRIBUTE_NORETURN; |
| 225 | static void onint(void) ATTRIBUTE_NORETURN; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 226 | |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 227 | static void sh_error(const char *, ...) ATTRIBUTE_NORETURN; |
| 228 | static void exerror(int, const char *, ...) ATTRIBUTE_NORETURN; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 229 | |
| 230 | static void sh_warnx(const char *, ...); |
| 231 | |
| 232 | #ifdef CONFIG_ASH_OPTIMIZE_FOR_SIZE |
| 233 | static void |
| 234 | inton(void) { |
| 235 | if (--suppressint == 0 && intpending) { |
| 236 | onint(); |
| 237 | } |
| 238 | } |
| 239 | #define INTON inton() |
| 240 | static void forceinton(void) |
| 241 | { |
| 242 | suppressint = 0; |
| 243 | if (intpending) |
| 244 | onint(); |
| 245 | } |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 246 | #define FORCEINTON forceinton() |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 247 | #else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 248 | #define INTON \ |
| 249 | ({ \ |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 250 | xbarrier(); \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 251 | if (--suppressint == 0 && intpending) onint(); \ |
| 252 | 0; \ |
| 253 | }) |
| 254 | #define FORCEINTON \ |
| 255 | ({ \ |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 256 | xbarrier(); \ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 257 | suppressint = 0; \ |
| 258 | if (intpending) onint(); \ |
| 259 | 0; \ |
| 260 | }) |
| 261 | #endif /* CONFIG_ASH_OPTIMIZE_FOR_SIZE */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 262 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 263 | /* expand.h */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 264 | |
| 265 | struct strlist { |
| 266 | struct strlist *next; |
| 267 | char *text; |
| 268 | }; |
| 269 | |
| 270 | |
| 271 | struct arglist { |
| 272 | struct strlist *list; |
| 273 | struct strlist **lastp; |
| 274 | }; |
| 275 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 276 | /* |
| 277 | * expandarg() flags |
| 278 | */ |
| 279 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 280 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 281 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 282 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 283 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
| 284 | #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */ |
| 285 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ |
| 286 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ |
| 287 | #define EXP_QWORD 0x100 /* expand word in quoted parameter expansion */ |
| 288 | |
| 289 | |
| 290 | union node; |
| 291 | static void expandarg(union node *, struct arglist *, int); |
| 292 | #define rmescapes(p) _rmescapes((p), 0) |
| 293 | static char *_rmescapes(char *, int); |
| 294 | static int casematch(union node *, char *); |
| 295 | |
| 296 | #ifdef CONFIG_ASH_MATH_SUPPORT |
| 297 | static void expari(int); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 298 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 299 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 300 | /* eval.h */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 301 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 302 | static char *commandname; /* currently executing command */ |
| 303 | static struct strlist *cmdenviron; /* environment for builtin command */ |
| 304 | static int exitstatus; /* exit status of last command */ |
| 305 | static int back_exitstatus; /* exit status of backquoted command */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 306 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 307 | |
| 308 | struct backcmd { /* result of evalbackcmd */ |
| 309 | int fd; /* file descriptor to read from */ |
| 310 | char *buf; /* buffer */ |
| 311 | int nleft; /* number of chars in buffer */ |
| 312 | struct job *jp; /* job structure for command */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 313 | }; |
| 314 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 315 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 316 | * This file was generated by the mknodes program. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 317 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 318 | |
| 319 | #define NCMD 0 |
| 320 | #define NPIPE 1 |
| 321 | #define NREDIR 2 |
| 322 | #define NBACKGND 3 |
| 323 | #define NSUBSHELL 4 |
| 324 | #define NAND 5 |
| 325 | #define NOR 6 |
| 326 | #define NSEMI 7 |
| 327 | #define NIF 8 |
| 328 | #define NWHILE 9 |
| 329 | #define NUNTIL 10 |
| 330 | #define NFOR 11 |
| 331 | #define NCASE 12 |
| 332 | #define NCLIST 13 |
| 333 | #define NDEFUN 14 |
| 334 | #define NARG 15 |
| 335 | #define NTO 16 |
| 336 | #define NCLOBBER 17 |
| 337 | #define NFROM 18 |
| 338 | #define NFROMTO 19 |
| 339 | #define NAPPEND 20 |
| 340 | #define NTOFD 21 |
| 341 | #define NFROMFD 22 |
| 342 | #define NHERE 23 |
| 343 | #define NXHERE 24 |
| 344 | #define NNOT 25 |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 345 | |
| 346 | |
| 347 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 348 | struct ncmd { |
| 349 | int type; |
| 350 | union node *assign; |
| 351 | union node *args; |
| 352 | union node *redirect; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 356 | struct npipe { |
| 357 | int type; |
| 358 | int backgnd; |
| 359 | struct nodelist *cmdlist; |
| 360 | }; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 361 | |
| 362 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 363 | struct nredir { |
| 364 | int type; |
| 365 | union node *n; |
| 366 | union node *redirect; |
| 367 | }; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 368 | |
| 369 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 370 | struct nbinary { |
| 371 | int type; |
| 372 | union node *ch1; |
| 373 | union node *ch2; |
| 374 | }; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 375 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 376 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 377 | struct nif { |
| 378 | int type; |
| 379 | union node *test; |
| 380 | union node *ifpart; |
| 381 | union node *elsepart; |
| 382 | }; |
| 383 | |
| 384 | |
| 385 | struct nfor { |
| 386 | int type; |
| 387 | union node *args; |
| 388 | union node *body; |
| 389 | char *var; |
| 390 | }; |
| 391 | |
| 392 | |
| 393 | struct ncase { |
| 394 | int type; |
| 395 | union node *expr; |
| 396 | union node *cases; |
| 397 | }; |
| 398 | |
| 399 | |
| 400 | struct nclist { |
| 401 | int type; |
| 402 | union node *next; |
| 403 | union node *pattern; |
| 404 | union node *body; |
| 405 | }; |
| 406 | |
| 407 | |
| 408 | struct narg { |
| 409 | int type; |
| 410 | union node *next; |
| 411 | char *text; |
| 412 | struct nodelist *backquote; |
| 413 | }; |
| 414 | |
| 415 | |
| 416 | struct nfile { |
| 417 | int type; |
| 418 | union node *next; |
| 419 | int fd; |
| 420 | union node *fname; |
| 421 | char *expfname; |
| 422 | }; |
| 423 | |
| 424 | |
| 425 | struct ndup { |
| 426 | int type; |
| 427 | union node *next; |
| 428 | int fd; |
| 429 | int dupfd; |
| 430 | union node *vname; |
| 431 | }; |
| 432 | |
| 433 | |
| 434 | struct nhere { |
| 435 | int type; |
| 436 | union node *next; |
| 437 | int fd; |
| 438 | union node *doc; |
| 439 | }; |
| 440 | |
| 441 | |
| 442 | struct nnot { |
| 443 | int type; |
| 444 | union node *com; |
| 445 | }; |
| 446 | |
| 447 | |
| 448 | union node { |
| 449 | int type; |
| 450 | struct ncmd ncmd; |
| 451 | struct npipe npipe; |
| 452 | struct nredir nredir; |
| 453 | struct nbinary nbinary; |
| 454 | struct nif nif; |
| 455 | struct nfor nfor; |
| 456 | struct ncase ncase; |
| 457 | struct nclist nclist; |
| 458 | struct narg narg; |
| 459 | struct nfile nfile; |
| 460 | struct ndup ndup; |
| 461 | struct nhere nhere; |
| 462 | struct nnot nnot; |
| 463 | }; |
| 464 | |
| 465 | |
| 466 | struct nodelist { |
| 467 | struct nodelist *next; |
| 468 | union node *n; |
| 469 | }; |
| 470 | |
| 471 | |
| 472 | struct funcnode { |
| 473 | int count; |
| 474 | union node n; |
| 475 | }; |
| 476 | |
| 477 | |
| 478 | static void freefunc(struct funcnode *); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 479 | /* parser.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 480 | |
| 481 | /* control characters in argument strings */ |
| 482 | #define CTL_FIRST '\201' /* first 'special' character */ |
| 483 | #define CTLESC '\201' /* escape next character */ |
| 484 | #define CTLVAR '\202' /* variable defn */ |
| 485 | #define CTLENDVAR '\203' |
| 486 | #define CTLBACKQ '\204' |
| 487 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ |
| 488 | /* CTLBACKQ | CTLQUOTE == '\205' */ |
| 489 | #define CTLARI '\206' /* arithmetic expression */ |
| 490 | #define CTLENDARI '\207' |
| 491 | #define CTLQUOTEMARK '\210' |
| 492 | #define CTL_LAST '\210' /* last 'special' character */ |
| 493 | |
| 494 | /* variable substitution byte (follows CTLVAR) */ |
| 495 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 496 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
| 497 | #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */ |
| 498 | |
| 499 | /* values of VSTYPE field */ |
| 500 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 501 | #define VSMINUS 0x2 /* ${var-text} */ |
| 502 | #define VSPLUS 0x3 /* ${var+text} */ |
| 503 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 504 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 505 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 506 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 507 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 508 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 509 | #define VSLENGTH 0xa /* ${#var} */ |
| 510 | |
| 511 | /* values of checkkwd variable */ |
| 512 | #define CHKALIAS 0x1 |
| 513 | #define CHKKWD 0x2 |
| 514 | #define CHKNL 0x4 |
| 515 | |
| 516 | #define IBUFSIZ (BUFSIZ + 1) |
| 517 | |
| 518 | /* |
| 519 | * NEOF is returned by parsecmd when it encounters an end of file. It |
| 520 | * must be distinct from NULL, so we use the address of a variable that |
| 521 | * happens to be handy. |
| 522 | */ |
| 523 | static int plinno = 1; /* input line number */ |
| 524 | |
| 525 | /* number of characters left in input buffer */ |
| 526 | static int parsenleft; /* copy of parsefile->nleft */ |
| 527 | static int parselleft; /* copy of parsefile->lleft */ |
| 528 | |
| 529 | /* next character in input buffer */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 530 | static char *parsenextc; /* copy of parsefile->nextc */ |
Glenn L McGrath | d361217 | 2003-09-17 00:22:26 +0000 | [diff] [blame] | 531 | |
| 532 | struct strpush { |
| 533 | struct strpush *prev; /* preceding string on stack */ |
| 534 | char *prevstring; |
| 535 | int prevnleft; |
| 536 | #ifdef CONFIG_ASH_ALIAS |
| 537 | struct alias *ap; /* if push was associated with an alias */ |
| 538 | #endif |
| 539 | char *string; /* remember the string since it may change */ |
| 540 | }; |
| 541 | |
| 542 | struct parsefile { |
| 543 | struct parsefile *prev; /* preceding file on stack */ |
| 544 | int linno; /* current line */ |
| 545 | int fd; /* file descriptor (or -1 if string) */ |
| 546 | int nleft; /* number of chars left in this line */ |
| 547 | int lleft; /* number of chars left in this buffer */ |
| 548 | char *nextc; /* next char in buffer */ |
| 549 | char *buf; /* input buffer */ |
| 550 | struct strpush *strpush; /* for pushing strings at this level */ |
| 551 | struct strpush basestrpush; /* so pushing one is fast */ |
| 552 | }; |
| 553 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 554 | static struct parsefile basepf; /* top level input file */ |
"Vladimir N. Oleynik" | 6f347ef | 2005-10-15 10:23:55 +0000 | [diff] [blame] | 555 | #define basebuf bb_common_bufsiz1 /* buffer for top level input file */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 556 | static struct parsefile *parsefile = &basepf; /* current input file */ |
| 557 | |
| 558 | |
| 559 | static int tokpushback; /* last token pushed back */ |
| 560 | #define NEOF ((union node *)&tokpushback) |
| 561 | static int parsebackquote; /* nonzero if we are inside backquotes */ |
| 562 | static int doprompt; /* if set, prompt the user */ |
| 563 | static int needprompt; /* true if interactive and at start of line */ |
| 564 | static int lasttoken; /* last token read */ |
| 565 | static char *wordtext; /* text of last word returned by readtoken */ |
| 566 | static int checkkwd; |
| 567 | static struct nodelist *backquotelist; |
| 568 | static union node *redirnode; |
| 569 | static struct heredoc *heredoc; |
| 570 | static int quoteflag; /* set if (part of) last token was quoted */ |
| 571 | static int startlinno; /* line # where last token started */ |
| 572 | |
| 573 | static union node *parsecmd(int); |
| 574 | static void fixredir(union node *, const char *, int); |
| 575 | static const char *const *findkwd(const char *); |
| 576 | static char *endofname(const char *); |
| 577 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 578 | /* shell.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 579 | |
| 580 | typedef void *pointer; |
| 581 | |
| 582 | static char nullstr[1]; /* zero length string */ |
| 583 | static const char spcstr[] = " "; |
| 584 | static const char snlfmt[] = "%s\n"; |
| 585 | static const char dolatstr[] = { CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' }; |
| 586 | static const char illnum[] = "Illegal number: %s"; |
| 587 | static const char homestr[] = "HOME"; |
| 588 | |
| 589 | #ifdef DEBUG |
| 590 | #define TRACE(param) trace param |
| 591 | #define TRACEV(param) tracev param |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 592 | #else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 593 | #define TRACE(param) |
| 594 | #define TRACEV(param) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 595 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 596 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 597 | #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96) |
| 598 | #define __builtin_expect(x, expected_value) (x) |
| 599 | #endif |
| 600 | |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 601 | #define xlikely(x) __builtin_expect((x),1) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 602 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 603 | |
| 604 | #define TEOF 0 |
| 605 | #define TNL 1 |
| 606 | #define TREDIR 2 |
| 607 | #define TWORD 3 |
| 608 | #define TSEMI 4 |
| 609 | #define TBACKGND 5 |
| 610 | #define TAND 6 |
| 611 | #define TOR 7 |
| 612 | #define TPIPE 8 |
| 613 | #define TLP 9 |
| 614 | #define TRP 10 |
| 615 | #define TENDCASE 11 |
| 616 | #define TENDBQUOTE 12 |
| 617 | #define TNOT 13 |
| 618 | #define TCASE 14 |
| 619 | #define TDO 15 |
| 620 | #define TDONE 16 |
| 621 | #define TELIF 17 |
| 622 | #define TELSE 18 |
| 623 | #define TESAC 19 |
| 624 | #define TFI 20 |
| 625 | #define TFOR 21 |
| 626 | #define TIF 22 |
| 627 | #define TIN 23 |
| 628 | #define TTHEN 24 |
| 629 | #define TUNTIL 25 |
| 630 | #define TWHILE 26 |
| 631 | #define TBEGIN 27 |
| 632 | #define TEND 28 |
| 633 | |
| 634 | /* first char is indicating which tokens mark the end of a list */ |
| 635 | static const char *const tokname_array[] = { |
| 636 | "\1end of file", |
| 637 | "\0newline", |
| 638 | "\0redirection", |
| 639 | "\0word", |
| 640 | "\0;", |
| 641 | "\0&", |
| 642 | "\0&&", |
| 643 | "\0||", |
| 644 | "\0|", |
| 645 | "\0(", |
| 646 | "\1)", |
| 647 | "\1;;", |
| 648 | "\1`", |
| 649 | #define KWDOFFSET 13 |
| 650 | /* the following are keywords */ |
| 651 | "\0!", |
| 652 | "\0case", |
| 653 | "\1do", |
| 654 | "\1done", |
| 655 | "\1elif", |
| 656 | "\1else", |
| 657 | "\1esac", |
| 658 | "\1fi", |
| 659 | "\0for", |
| 660 | "\0if", |
| 661 | "\0in", |
| 662 | "\1then", |
| 663 | "\0until", |
| 664 | "\0while", |
| 665 | "\0{", |
| 666 | "\1}", |
| 667 | }; |
| 668 | |
| 669 | static const char *tokname(int tok) |
| 670 | { |
| 671 | static char buf[16]; |
| 672 | |
| 673 | if (tok >= TSEMI) |
| 674 | buf[0] = '"'; |
| 675 | sprintf(buf + (tok >= TSEMI), "%s%c", |
| 676 | tokname_array[tok] + 1, (tok >= TSEMI ? '"' : 0)); |
| 677 | return buf; |
| 678 | } |
| 679 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 680 | /* machdep.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 681 | |
| 682 | /* |
| 683 | * Most machines require the value returned from malloc to be aligned |
| 684 | * in some way. The following macro will get this right on many machines. |
| 685 | */ |
| 686 | |
| 687 | #define SHELL_SIZE (sizeof(union {int i; char *cp; double d; }) - 1) |
| 688 | /* |
| 689 | * It appears that grabstackstr() will barf with such alignments |
| 690 | * because stalloc() will return a string allocated in a new stackblock. |
| 691 | */ |
| 692 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 693 | |
| 694 | /* |
| 695 | * This file was generated by the mksyntax program. |
| 696 | */ |
| 697 | |
| 698 | |
| 699 | /* Syntax classes */ |
| 700 | #define CWORD 0 /* character is nothing special */ |
| 701 | #define CNL 1 /* newline character */ |
| 702 | #define CBACK 2 /* a backslash character */ |
| 703 | #define CSQUOTE 3 /* single quote */ |
| 704 | #define CDQUOTE 4 /* double quote */ |
| 705 | #define CENDQUOTE 5 /* a terminating quote */ |
| 706 | #define CBQUOTE 6 /* backwards single quote */ |
| 707 | #define CVAR 7 /* a dollar sign */ |
| 708 | #define CENDVAR 8 /* a '}' character */ |
| 709 | #define CLP 9 /* a left paren in arithmetic */ |
| 710 | #define CRP 10 /* a right paren in arithmetic */ |
| 711 | #define CENDFILE 11 /* end of file */ |
| 712 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 713 | #define CSPCL 13 /* these terminate a word */ |
| 714 | #define CIGN 14 /* character should be ignored */ |
| 715 | |
| 716 | #ifdef CONFIG_ASH_ALIAS |
| 717 | #define SYNBASE 130 |
| 718 | #define PEOF -130 |
| 719 | #define PEOA -129 |
| 720 | #define PEOA_OR_PEOF PEOA |
| 721 | #else |
| 722 | #define SYNBASE 129 |
| 723 | #define PEOF -129 |
| 724 | #define PEOA_OR_PEOF PEOF |
| 725 | #endif |
| 726 | |
| 727 | #define is_digit(c) ((unsigned)((c) - '0') <= 9) |
| 728 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 729 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 730 | |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 731 | /* C99 say: "char" declaration may be signed or unsigned default */ |
| 732 | #define SC2INT(chr2may_be_negative_int) (int)((signed char)chr2may_be_negative_int) |
| 733 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 734 | /* |
| 735 | * is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 736 | * (assuming ascii char codes, as the original implementation did) |
| 737 | */ |
| 738 | #define is_special(c) \ |
| 739 | ( (((unsigned int)c) - 33 < 32) \ |
| 740 | && ((0xc1ff920dUL >> (((unsigned int)c) - 33)) & 1)) |
| 741 | |
| 742 | #define digit_val(c) ((c) - '0') |
| 743 | |
| 744 | /* |
| 745 | * This file was generated by the mksyntax program. |
| 746 | */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 747 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 748 | #ifdef CONFIG_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 749 | #define USE_SIT_FUNCTION |
| 750 | #endif |
| 751 | |
| 752 | /* number syntax index */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 753 | #define BASESYNTAX 0 /* not in quotes */ |
| 754 | #define DQSYNTAX 1 /* in double quotes */ |
| 755 | #define SQSYNTAX 2 /* in single quotes */ |
| 756 | #define ARISYNTAX 3 /* in arithmetic */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 757 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 758 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 759 | static const char S_I_T[][4] = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 760 | #ifdef CONFIG_ASH_ALIAS |
| 761 | {CSPCL, CIGN, CIGN, CIGN}, /* 0, PEOA */ |
| 762 | #endif |
| 763 | {CSPCL, CWORD, CWORD, CWORD}, /* 1, ' ' */ |
| 764 | {CNL, CNL, CNL, CNL}, /* 2, \n */ |
| 765 | {CWORD, CCTL, CCTL, CWORD}, /* 3, !*-/:=?[]~ */ |
| 766 | {CDQUOTE, CENDQUOTE, CWORD, CWORD}, /* 4, '"' */ |
| 767 | {CVAR, CVAR, CWORD, CVAR}, /* 5, $ */ |
| 768 | {CSQUOTE, CWORD, CENDQUOTE, CWORD}, /* 6, "'" */ |
| 769 | {CSPCL, CWORD, CWORD, CLP}, /* 7, ( */ |
| 770 | {CSPCL, CWORD, CWORD, CRP}, /* 8, ) */ |
| 771 | {CBACK, CBACK, CCTL, CBACK}, /* 9, \ */ |
| 772 | {CBQUOTE, CBQUOTE, CWORD, CBQUOTE}, /* 10, ` */ |
| 773 | {CENDVAR, CENDVAR, CWORD, CENDVAR}, /* 11, } */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 774 | #ifndef USE_SIT_FUNCTION |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 775 | {CENDFILE, CENDFILE, CENDFILE, CENDFILE}, /* 12, PEOF */ |
| 776 | {CWORD, CWORD, CWORD, CWORD}, /* 13, 0-9A-Za-z */ |
| 777 | {CCTL, CCTL, CCTL, CCTL} /* 14, CTLESC ... */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 778 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 779 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 780 | #else |
| 781 | static const char S_I_T[][3] = { |
| 782 | #ifdef CONFIG_ASH_ALIAS |
| 783 | {CSPCL, CIGN, CIGN}, /* 0, PEOA */ |
| 784 | #endif |
| 785 | {CSPCL, CWORD, CWORD}, /* 1, ' ' */ |
| 786 | {CNL, CNL, CNL}, /* 2, \n */ |
| 787 | {CWORD, CCTL, CCTL}, /* 3, !*-/:=?[]~ */ |
| 788 | {CDQUOTE, CENDQUOTE, CWORD}, /* 4, '"' */ |
| 789 | {CVAR, CVAR, CWORD}, /* 5, $ */ |
| 790 | {CSQUOTE, CWORD, CENDQUOTE}, /* 6, "'" */ |
| 791 | {CSPCL, CWORD, CWORD}, /* 7, ( */ |
| 792 | {CSPCL, CWORD, CWORD}, /* 8, ) */ |
| 793 | {CBACK, CBACK, CCTL}, /* 9, \ */ |
| 794 | {CBQUOTE, CBQUOTE, CWORD}, /* 10, ` */ |
| 795 | {CENDVAR, CENDVAR, CWORD}, /* 11, } */ |
| 796 | #ifndef USE_SIT_FUNCTION |
| 797 | {CENDFILE, CENDFILE, CENDFILE}, /* 12, PEOF */ |
| 798 | {CWORD, CWORD, CWORD}, /* 13, 0-9A-Za-z */ |
| 799 | {CCTL, CCTL, CCTL} /* 14, CTLESC ... */ |
| 800 | #endif |
| 801 | }; |
| 802 | #endif /* CONFIG_ASH_MATH_SUPPORT */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 803 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 804 | #ifdef USE_SIT_FUNCTION |
| 805 | |
| 806 | #define U_C(c) ((unsigned char)(c)) |
| 807 | |
| 808 | static int SIT(int c, int syntax) |
| 809 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 810 | static const char spec_symbls[] = "\t\n !\"$&'()*-/:;<=>?[\\]`|}~"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 811 | #ifdef CONFIG_ASH_ALIAS |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 812 | static const char syntax_index_table[] = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 813 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
| 814 | 7, 8, 3, 3, 3, 3, 1, 1, /* "()*-/:;<" */ |
| 815 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 816 | 11, 3 /* "}~" */ |
| 817 | }; |
| 818 | #else |
| 819 | static const char syntax_index_table[] = { |
| 820 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
| 821 | 6, 7, 2, 2, 2, 2, 0, 0, /* "()*-/:;<" */ |
| 822 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 823 | 10, 2 /* "}~" */ |
| 824 | }; |
| 825 | #endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 826 | const char *s; |
| 827 | int indx; |
| 828 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 829 | if (c == PEOF) /* 2^8+2 */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 830 | return CENDFILE; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 831 | #ifdef CONFIG_ASH_ALIAS |
| 832 | if (c == PEOA) /* 2^8+1 */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 833 | indx = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 834 | else |
| 835 | #endif |
| 836 | if (U_C(c) >= U_C(CTLESC) && U_C(c) <= U_C(CTLQUOTEMARK)) |
| 837 | return CCTL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 838 | else { |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 839 | s = strchr(spec_symbls, c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 840 | if (s == 0 || *s == 0) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 841 | return CWORD; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 842 | indx = syntax_index_table[(s - spec_symbls)]; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 843 | } |
| 844 | return S_I_T[indx][syntax]; |
| 845 | } |
| 846 | |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 847 | #else /* USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 848 | |
| 849 | #define SIT(c, syntax) S_I_T[(int)syntax_index_table[((int)c)+SYNBASE]][syntax] |
| 850 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 851 | #ifdef CONFIG_ASH_ALIAS |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 852 | #define CSPCL_CIGN_CIGN_CIGN 0 |
| 853 | #define CSPCL_CWORD_CWORD_CWORD 1 |
| 854 | #define CNL_CNL_CNL_CNL 2 |
| 855 | #define CWORD_CCTL_CCTL_CWORD 3 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 856 | #define CDQUOTE_CENDQUOTE_CWORD_CWORD 4 |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 857 | #define CVAR_CVAR_CWORD_CVAR 5 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 858 | #define CSQUOTE_CWORD_CENDQUOTE_CWORD 6 |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 859 | #define CSPCL_CWORD_CWORD_CLP 7 |
| 860 | #define CSPCL_CWORD_CWORD_CRP 8 |
| 861 | #define CBACK_CBACK_CCTL_CBACK 9 |
| 862 | #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE 10 |
| 863 | #define CENDVAR_CENDVAR_CWORD_CENDVAR 11 |
| 864 | #define CENDFILE_CENDFILE_CENDFILE_CENDFILE 12 |
| 865 | #define CWORD_CWORD_CWORD_CWORD 13 |
| 866 | #define CCTL_CCTL_CCTL_CCTL 14 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 867 | #else |
| 868 | #define CSPCL_CWORD_CWORD_CWORD 0 |
| 869 | #define CNL_CNL_CNL_CNL 1 |
| 870 | #define CWORD_CCTL_CCTL_CWORD 2 |
| 871 | #define CDQUOTE_CENDQUOTE_CWORD_CWORD 3 |
| 872 | #define CVAR_CVAR_CWORD_CVAR 4 |
| 873 | #define CSQUOTE_CWORD_CENDQUOTE_CWORD 5 |
| 874 | #define CSPCL_CWORD_CWORD_CLP 6 |
| 875 | #define CSPCL_CWORD_CWORD_CRP 7 |
| 876 | #define CBACK_CBACK_CCTL_CBACK 8 |
| 877 | #define CBQUOTE_CBQUOTE_CWORD_CBQUOTE 9 |
| 878 | #define CENDVAR_CENDVAR_CWORD_CENDVAR 10 |
| 879 | #define CENDFILE_CENDFILE_CENDFILE_CENDFILE 11 |
| 880 | #define CWORD_CWORD_CWORD_CWORD 12 |
| 881 | #define CCTL_CCTL_CCTL_CCTL 13 |
| 882 | #endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 883 | |
| 884 | static const char syntax_index_table[258] = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 885 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 886 | /* 0 PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
| 887 | #ifdef CONFIG_ASH_ALIAS |
| 888 | /* 1 PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 889 | #endif |
| 890 | /* 2 -128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 891 | /* 3 -127 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 892 | /* 4 -126 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 893 | /* 5 -125 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 894 | /* 6 -124 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 895 | /* 7 -123 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 896 | /* 8 -122 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 897 | /* 9 -121 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 898 | /* 10 -120 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 899 | /* 11 -119 */ CWORD_CWORD_CWORD_CWORD, |
| 900 | /* 12 -118 */ CWORD_CWORD_CWORD_CWORD, |
| 901 | /* 13 -117 */ CWORD_CWORD_CWORD_CWORD, |
| 902 | /* 14 -116 */ CWORD_CWORD_CWORD_CWORD, |
| 903 | /* 15 -115 */ CWORD_CWORD_CWORD_CWORD, |
| 904 | /* 16 -114 */ CWORD_CWORD_CWORD_CWORD, |
| 905 | /* 17 -113 */ CWORD_CWORD_CWORD_CWORD, |
| 906 | /* 18 -112 */ CWORD_CWORD_CWORD_CWORD, |
| 907 | /* 19 -111 */ CWORD_CWORD_CWORD_CWORD, |
| 908 | /* 20 -110 */ CWORD_CWORD_CWORD_CWORD, |
| 909 | /* 21 -109 */ CWORD_CWORD_CWORD_CWORD, |
| 910 | /* 22 -108 */ CWORD_CWORD_CWORD_CWORD, |
| 911 | /* 23 -107 */ CWORD_CWORD_CWORD_CWORD, |
| 912 | /* 24 -106 */ CWORD_CWORD_CWORD_CWORD, |
| 913 | /* 25 -105 */ CWORD_CWORD_CWORD_CWORD, |
| 914 | /* 26 -104 */ CWORD_CWORD_CWORD_CWORD, |
| 915 | /* 27 -103 */ CWORD_CWORD_CWORD_CWORD, |
| 916 | /* 28 -102 */ CWORD_CWORD_CWORD_CWORD, |
| 917 | /* 29 -101 */ CWORD_CWORD_CWORD_CWORD, |
| 918 | /* 30 -100 */ CWORD_CWORD_CWORD_CWORD, |
| 919 | /* 31 -99 */ CWORD_CWORD_CWORD_CWORD, |
| 920 | /* 32 -98 */ CWORD_CWORD_CWORD_CWORD, |
| 921 | /* 33 -97 */ CWORD_CWORD_CWORD_CWORD, |
| 922 | /* 34 -96 */ CWORD_CWORD_CWORD_CWORD, |
| 923 | /* 35 -95 */ CWORD_CWORD_CWORD_CWORD, |
| 924 | /* 36 -94 */ CWORD_CWORD_CWORD_CWORD, |
| 925 | /* 37 -93 */ CWORD_CWORD_CWORD_CWORD, |
| 926 | /* 38 -92 */ CWORD_CWORD_CWORD_CWORD, |
| 927 | /* 39 -91 */ CWORD_CWORD_CWORD_CWORD, |
| 928 | /* 40 -90 */ CWORD_CWORD_CWORD_CWORD, |
| 929 | /* 41 -89 */ CWORD_CWORD_CWORD_CWORD, |
| 930 | /* 42 -88 */ CWORD_CWORD_CWORD_CWORD, |
| 931 | /* 43 -87 */ CWORD_CWORD_CWORD_CWORD, |
| 932 | /* 44 -86 */ CWORD_CWORD_CWORD_CWORD, |
| 933 | /* 45 -85 */ CWORD_CWORD_CWORD_CWORD, |
| 934 | /* 46 -84 */ CWORD_CWORD_CWORD_CWORD, |
| 935 | /* 47 -83 */ CWORD_CWORD_CWORD_CWORD, |
| 936 | /* 48 -82 */ CWORD_CWORD_CWORD_CWORD, |
| 937 | /* 49 -81 */ CWORD_CWORD_CWORD_CWORD, |
| 938 | /* 50 -80 */ CWORD_CWORD_CWORD_CWORD, |
| 939 | /* 51 -79 */ CWORD_CWORD_CWORD_CWORD, |
| 940 | /* 52 -78 */ CWORD_CWORD_CWORD_CWORD, |
| 941 | /* 53 -77 */ CWORD_CWORD_CWORD_CWORD, |
| 942 | /* 54 -76 */ CWORD_CWORD_CWORD_CWORD, |
| 943 | /* 55 -75 */ CWORD_CWORD_CWORD_CWORD, |
| 944 | /* 56 -74 */ CWORD_CWORD_CWORD_CWORD, |
| 945 | /* 57 -73 */ CWORD_CWORD_CWORD_CWORD, |
| 946 | /* 58 -72 */ CWORD_CWORD_CWORD_CWORD, |
| 947 | /* 59 -71 */ CWORD_CWORD_CWORD_CWORD, |
| 948 | /* 60 -70 */ CWORD_CWORD_CWORD_CWORD, |
| 949 | /* 61 -69 */ CWORD_CWORD_CWORD_CWORD, |
| 950 | /* 62 -68 */ CWORD_CWORD_CWORD_CWORD, |
| 951 | /* 63 -67 */ CWORD_CWORD_CWORD_CWORD, |
| 952 | /* 64 -66 */ CWORD_CWORD_CWORD_CWORD, |
| 953 | /* 65 -65 */ CWORD_CWORD_CWORD_CWORD, |
| 954 | /* 66 -64 */ CWORD_CWORD_CWORD_CWORD, |
| 955 | /* 67 -63 */ CWORD_CWORD_CWORD_CWORD, |
| 956 | /* 68 -62 */ CWORD_CWORD_CWORD_CWORD, |
| 957 | /* 69 -61 */ CWORD_CWORD_CWORD_CWORD, |
| 958 | /* 70 -60 */ CWORD_CWORD_CWORD_CWORD, |
| 959 | /* 71 -59 */ CWORD_CWORD_CWORD_CWORD, |
| 960 | /* 72 -58 */ CWORD_CWORD_CWORD_CWORD, |
| 961 | /* 73 -57 */ CWORD_CWORD_CWORD_CWORD, |
| 962 | /* 74 -56 */ CWORD_CWORD_CWORD_CWORD, |
| 963 | /* 75 -55 */ CWORD_CWORD_CWORD_CWORD, |
| 964 | /* 76 -54 */ CWORD_CWORD_CWORD_CWORD, |
| 965 | /* 77 -53 */ CWORD_CWORD_CWORD_CWORD, |
| 966 | /* 78 -52 */ CWORD_CWORD_CWORD_CWORD, |
| 967 | /* 79 -51 */ CWORD_CWORD_CWORD_CWORD, |
| 968 | /* 80 -50 */ CWORD_CWORD_CWORD_CWORD, |
| 969 | /* 81 -49 */ CWORD_CWORD_CWORD_CWORD, |
| 970 | /* 82 -48 */ CWORD_CWORD_CWORD_CWORD, |
| 971 | /* 83 -47 */ CWORD_CWORD_CWORD_CWORD, |
| 972 | /* 84 -46 */ CWORD_CWORD_CWORD_CWORD, |
| 973 | /* 85 -45 */ CWORD_CWORD_CWORD_CWORD, |
| 974 | /* 86 -44 */ CWORD_CWORD_CWORD_CWORD, |
| 975 | /* 87 -43 */ CWORD_CWORD_CWORD_CWORD, |
| 976 | /* 88 -42 */ CWORD_CWORD_CWORD_CWORD, |
| 977 | /* 89 -41 */ CWORD_CWORD_CWORD_CWORD, |
| 978 | /* 90 -40 */ CWORD_CWORD_CWORD_CWORD, |
| 979 | /* 91 -39 */ CWORD_CWORD_CWORD_CWORD, |
| 980 | /* 92 -38 */ CWORD_CWORD_CWORD_CWORD, |
| 981 | /* 93 -37 */ CWORD_CWORD_CWORD_CWORD, |
| 982 | /* 94 -36 */ CWORD_CWORD_CWORD_CWORD, |
| 983 | /* 95 -35 */ CWORD_CWORD_CWORD_CWORD, |
| 984 | /* 96 -34 */ CWORD_CWORD_CWORD_CWORD, |
| 985 | /* 97 -33 */ CWORD_CWORD_CWORD_CWORD, |
| 986 | /* 98 -32 */ CWORD_CWORD_CWORD_CWORD, |
| 987 | /* 99 -31 */ CWORD_CWORD_CWORD_CWORD, |
| 988 | /* 100 -30 */ CWORD_CWORD_CWORD_CWORD, |
| 989 | /* 101 -29 */ CWORD_CWORD_CWORD_CWORD, |
| 990 | /* 102 -28 */ CWORD_CWORD_CWORD_CWORD, |
| 991 | /* 103 -27 */ CWORD_CWORD_CWORD_CWORD, |
| 992 | /* 104 -26 */ CWORD_CWORD_CWORD_CWORD, |
| 993 | /* 105 -25 */ CWORD_CWORD_CWORD_CWORD, |
| 994 | /* 106 -24 */ CWORD_CWORD_CWORD_CWORD, |
| 995 | /* 107 -23 */ CWORD_CWORD_CWORD_CWORD, |
| 996 | /* 108 -22 */ CWORD_CWORD_CWORD_CWORD, |
| 997 | /* 109 -21 */ CWORD_CWORD_CWORD_CWORD, |
| 998 | /* 110 -20 */ CWORD_CWORD_CWORD_CWORD, |
| 999 | /* 111 -19 */ CWORD_CWORD_CWORD_CWORD, |
| 1000 | /* 112 -18 */ CWORD_CWORD_CWORD_CWORD, |
| 1001 | /* 113 -17 */ CWORD_CWORD_CWORD_CWORD, |
| 1002 | /* 114 -16 */ CWORD_CWORD_CWORD_CWORD, |
| 1003 | /* 115 -15 */ CWORD_CWORD_CWORD_CWORD, |
| 1004 | /* 116 -14 */ CWORD_CWORD_CWORD_CWORD, |
| 1005 | /* 117 -13 */ CWORD_CWORD_CWORD_CWORD, |
| 1006 | /* 118 -12 */ CWORD_CWORD_CWORD_CWORD, |
| 1007 | /* 119 -11 */ CWORD_CWORD_CWORD_CWORD, |
| 1008 | /* 120 -10 */ CWORD_CWORD_CWORD_CWORD, |
| 1009 | /* 121 -9 */ CWORD_CWORD_CWORD_CWORD, |
| 1010 | /* 122 -8 */ CWORD_CWORD_CWORD_CWORD, |
| 1011 | /* 123 -7 */ CWORD_CWORD_CWORD_CWORD, |
| 1012 | /* 124 -6 */ CWORD_CWORD_CWORD_CWORD, |
| 1013 | /* 125 -5 */ CWORD_CWORD_CWORD_CWORD, |
| 1014 | /* 126 -4 */ CWORD_CWORD_CWORD_CWORD, |
| 1015 | /* 127 -3 */ CWORD_CWORD_CWORD_CWORD, |
| 1016 | /* 128 -2 */ CWORD_CWORD_CWORD_CWORD, |
| 1017 | /* 129 -1 */ CWORD_CWORD_CWORD_CWORD, |
| 1018 | /* 130 0 */ CWORD_CWORD_CWORD_CWORD, |
| 1019 | /* 131 1 */ CWORD_CWORD_CWORD_CWORD, |
| 1020 | /* 132 2 */ CWORD_CWORD_CWORD_CWORD, |
| 1021 | /* 133 3 */ CWORD_CWORD_CWORD_CWORD, |
| 1022 | /* 134 4 */ CWORD_CWORD_CWORD_CWORD, |
| 1023 | /* 135 5 */ CWORD_CWORD_CWORD_CWORD, |
| 1024 | /* 136 6 */ CWORD_CWORD_CWORD_CWORD, |
| 1025 | /* 137 7 */ CWORD_CWORD_CWORD_CWORD, |
| 1026 | /* 138 8 */ CWORD_CWORD_CWORD_CWORD, |
| 1027 | /* 139 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 1028 | /* 140 10 "\n" */ CNL_CNL_CNL_CNL, |
| 1029 | /* 141 11 */ CWORD_CWORD_CWORD_CWORD, |
| 1030 | /* 142 12 */ CWORD_CWORD_CWORD_CWORD, |
| 1031 | /* 143 13 */ CWORD_CWORD_CWORD_CWORD, |
| 1032 | /* 144 14 */ CWORD_CWORD_CWORD_CWORD, |
| 1033 | /* 145 15 */ CWORD_CWORD_CWORD_CWORD, |
| 1034 | /* 146 16 */ CWORD_CWORD_CWORD_CWORD, |
| 1035 | /* 147 17 */ CWORD_CWORD_CWORD_CWORD, |
| 1036 | /* 148 18 */ CWORD_CWORD_CWORD_CWORD, |
| 1037 | /* 149 19 */ CWORD_CWORD_CWORD_CWORD, |
| 1038 | /* 150 20 */ CWORD_CWORD_CWORD_CWORD, |
| 1039 | /* 151 21 */ CWORD_CWORD_CWORD_CWORD, |
| 1040 | /* 152 22 */ CWORD_CWORD_CWORD_CWORD, |
| 1041 | /* 153 23 */ CWORD_CWORD_CWORD_CWORD, |
| 1042 | /* 154 24 */ CWORD_CWORD_CWORD_CWORD, |
| 1043 | /* 155 25 */ CWORD_CWORD_CWORD_CWORD, |
| 1044 | /* 156 26 */ CWORD_CWORD_CWORD_CWORD, |
| 1045 | /* 157 27 */ CWORD_CWORD_CWORD_CWORD, |
| 1046 | /* 158 28 */ CWORD_CWORD_CWORD_CWORD, |
| 1047 | /* 159 29 */ CWORD_CWORD_CWORD_CWORD, |
| 1048 | /* 160 30 */ CWORD_CWORD_CWORD_CWORD, |
| 1049 | /* 161 31 */ CWORD_CWORD_CWORD_CWORD, |
| 1050 | /* 162 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 1051 | /* 163 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1052 | /* 164 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 1053 | /* 165 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 1054 | /* 166 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 1055 | /* 167 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 1056 | /* 168 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1057 | /* 169 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 1058 | /* 170 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 1059 | /* 171 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 1060 | /* 172 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 1061 | /* 173 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 1062 | /* 174 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 1063 | /* 175 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 1064 | /* 176 46 "." */ CWORD_CWORD_CWORD_CWORD, |
| 1065 | /* 177 47 "/" */ CWORD_CCTL_CCTL_CWORD, |
| 1066 | /* 178 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 1067 | /* 179 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 1068 | /* 180 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 1069 | /* 181 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 1070 | /* 182 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 1071 | /* 183 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 1072 | /* 184 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 1073 | /* 185 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 1074 | /* 186 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 1075 | /* 187 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 1076 | /* 188 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 1077 | /* 189 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 1078 | /* 190 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 1079 | /* 191 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 1080 | /* 192 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 1081 | /* 193 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 1082 | /* 194 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 1083 | /* 195 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 1084 | /* 196 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 1085 | /* 197 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 1086 | /* 198 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 1087 | /* 199 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 1088 | /* 200 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 1089 | /* 201 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 1090 | /* 202 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 1091 | /* 203 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 1092 | /* 204 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 1093 | /* 205 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 1094 | /* 206 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 1095 | /* 207 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 1096 | /* 208 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 1097 | /* 209 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 1098 | /* 210 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 1099 | /* 211 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 1100 | /* 212 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 1101 | /* 213 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 1102 | /* 214 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 1103 | /* 215 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 1104 | /* 216 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 1105 | /* 217 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 1106 | /* 218 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 1107 | /* 219 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 1108 | /* 220 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 1109 | /* 221 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 1110 | /* 222 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 1111 | /* 223 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 1112 | /* 224 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 1113 | /* 225 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 1114 | /* 226 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 1115 | /* 227 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 1116 | /* 228 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 1117 | /* 229 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 1118 | /* 230 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 1119 | /* 231 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 1120 | /* 232 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 1121 | /* 233 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 1122 | /* 234 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 1123 | /* 235 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 1124 | /* 236 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 1125 | /* 237 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 1126 | /* 238 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 1127 | /* 239 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 1128 | /* 240 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 1129 | /* 241 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 1130 | /* 242 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 1131 | /* 243 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 1132 | /* 244 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 1133 | /* 245 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 1134 | /* 246 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 1135 | /* 247 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 1136 | /* 248 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 1137 | /* 249 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 1138 | /* 250 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 1139 | /* 251 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 1140 | /* 252 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 1141 | /* 253 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 1142 | /* 254 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 1143 | /* 255 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 1144 | /* 256 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 1145 | /* 257 127 */ CWORD_CWORD_CWORD_CWORD, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1146 | }; |
| 1147 | |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 1148 | #endif /* USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1149 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1150 | /* alias.c */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1151 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1152 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1153 | #define ATABSIZE 39 |
| 1154 | |
| 1155 | static int funcblocksize; /* size of structures in function */ |
| 1156 | static int funcstringsize; /* size of strings in node */ |
| 1157 | static pointer funcblock; /* block to allocate function from */ |
| 1158 | static char *funcstring; /* block to allocate strings from */ |
| 1159 | |
| 1160 | static const short nodesize[26] = { |
| 1161 | SHELL_ALIGN(sizeof (struct ncmd)), |
| 1162 | SHELL_ALIGN(sizeof (struct npipe)), |
| 1163 | SHELL_ALIGN(sizeof (struct nredir)), |
| 1164 | SHELL_ALIGN(sizeof (struct nredir)), |
| 1165 | SHELL_ALIGN(sizeof (struct nredir)), |
| 1166 | SHELL_ALIGN(sizeof (struct nbinary)), |
| 1167 | SHELL_ALIGN(sizeof (struct nbinary)), |
| 1168 | SHELL_ALIGN(sizeof (struct nbinary)), |
| 1169 | SHELL_ALIGN(sizeof (struct nif)), |
| 1170 | SHELL_ALIGN(sizeof (struct nbinary)), |
| 1171 | SHELL_ALIGN(sizeof (struct nbinary)), |
| 1172 | SHELL_ALIGN(sizeof (struct nfor)), |
| 1173 | SHELL_ALIGN(sizeof (struct ncase)), |
| 1174 | SHELL_ALIGN(sizeof (struct nclist)), |
| 1175 | SHELL_ALIGN(sizeof (struct narg)), |
| 1176 | SHELL_ALIGN(sizeof (struct narg)), |
| 1177 | SHELL_ALIGN(sizeof (struct nfile)), |
| 1178 | SHELL_ALIGN(sizeof (struct nfile)), |
| 1179 | SHELL_ALIGN(sizeof (struct nfile)), |
| 1180 | SHELL_ALIGN(sizeof (struct nfile)), |
| 1181 | SHELL_ALIGN(sizeof (struct nfile)), |
| 1182 | SHELL_ALIGN(sizeof (struct ndup)), |
| 1183 | SHELL_ALIGN(sizeof (struct ndup)), |
| 1184 | SHELL_ALIGN(sizeof (struct nhere)), |
| 1185 | SHELL_ALIGN(sizeof (struct nhere)), |
| 1186 | SHELL_ALIGN(sizeof (struct nnot)), |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1187 | }; |
| 1188 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 1189 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1190 | static void calcsize(union node *); |
| 1191 | static void sizenodelist(struct nodelist *); |
| 1192 | static union node *copynode(union node *); |
| 1193 | static struct nodelist *copynodelist(struct nodelist *); |
| 1194 | static char *nodesavestr(char *); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1195 | |
| 1196 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1197 | static int evalstring(char *, int mask); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1198 | union node; /* BLETCH for ansi C */ |
| 1199 | static void evaltree(union node *, int); |
| 1200 | static void evalbackcmd(union node *, struct backcmd *); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1201 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1202 | static int evalskip; /* set if we are skipping commands */ |
| 1203 | static int skipcount; /* number of levels to skip */ |
| 1204 | static int funcnest; /* depth of function calls */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1205 | |
| 1206 | /* reasons for skipping commands (see comment on breakcmd routine) */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1207 | #define SKIPBREAK (1 << 0) |
| 1208 | #define SKIPCONT (1 << 1) |
| 1209 | #define SKIPFUNC (1 << 2) |
| 1210 | #define SKIPFILE (1 << 3) |
| 1211 | #define SKIPEVAL (1 << 4) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1212 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1213 | /* |
| 1214 | * This file was generated by the mkbuiltins program. |
| 1215 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1216 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1217 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1218 | static int bgcmd(int, char **); |
| 1219 | #endif |
| 1220 | static int breakcmd(int, char **); |
| 1221 | static int cdcmd(int, char **); |
| 1222 | #ifdef CONFIG_ASH_CMDCMD |
| 1223 | static int commandcmd(int, char **); |
| 1224 | #endif |
| 1225 | static int dotcmd(int, char **); |
| 1226 | static int evalcmd(int, char **); |
Paul Fox | 0b62158 | 2005-08-09 19:38:05 +0000 | [diff] [blame] | 1227 | #ifdef CONFIG_ASH_BUILTIN_ECHO |
| 1228 | static int echocmd(int, char **); |
| 1229 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1230 | static int execcmd(int, char **); |
| 1231 | static int exitcmd(int, char **); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1232 | static int exportcmd(int, char **); |
| 1233 | static int falsecmd(int, char **); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1234 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1235 | static int fgcmd(int, char **); |
| 1236 | #endif |
| 1237 | #ifdef CONFIG_ASH_GETOPTS |
| 1238 | static int getoptscmd(int, char **); |
| 1239 | #endif |
| 1240 | static int hashcmd(int, char **); |
| 1241 | #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET |
| 1242 | static int helpcmd(int argc, char **argv); |
| 1243 | #endif |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1244 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1245 | static int jobscmd(int, char **); |
| 1246 | #endif |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 1247 | #ifdef CONFIG_ASH_MATH_SUPPORT |
| 1248 | static int letcmd(int, char **); |
| 1249 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1250 | static int localcmd(int, char **); |
| 1251 | static int pwdcmd(int, char **); |
| 1252 | static int readcmd(int, char **); |
| 1253 | static int returncmd(int, char **); |
| 1254 | static int setcmd(int, char **); |
| 1255 | static int shiftcmd(int, char **); |
| 1256 | static int timescmd(int, char **); |
| 1257 | static int trapcmd(int, char **); |
| 1258 | static int truecmd(int, char **); |
| 1259 | static int typecmd(int, char **); |
| 1260 | static int umaskcmd(int, char **); |
| 1261 | static int unsetcmd(int, char **); |
| 1262 | static int waitcmd(int, char **); |
| 1263 | static int ulimitcmd(int, char **); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1264 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1265 | static int killcmd(int, char **); |
| 1266 | #endif |
| 1267 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1268 | /* mail.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1269 | |
| 1270 | #ifdef CONFIG_ASH_MAIL |
| 1271 | static void chkmail(void); |
| 1272 | static void changemail(const char *); |
| 1273 | #endif |
| 1274 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1275 | /* exec.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1276 | |
| 1277 | /* values of cmdtype */ |
| 1278 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 1279 | #define CMDNORMAL 0 /* command is an executable program */ |
| 1280 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 1281 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 1282 | |
| 1283 | struct builtincmd { |
| 1284 | const char *name; |
| 1285 | int (*builtin)(int, char **); |
| 1286 | /* unsigned flags; */ |
| 1287 | }; |
| 1288 | |
Paul Fox | 0b62158 | 2005-08-09 19:38:05 +0000 | [diff] [blame] | 1289 | |
| 1290 | #define COMMANDCMD (builtincmd + 5 + \ |
| 1291 | ENABLE_ASH_ALIAS + ENABLE_ASH_JOB_CONTROL) |
| 1292 | #define EXECCMD (builtincmd + 7 + \ |
| 1293 | ENABLE_ASH_CMDCMD + ENABLE_ASH_ALIAS + \ |
| 1294 | ENABLE_ASH_BUILTIN_ECHO + ENABLE_ASH_JOB_CONTROL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1295 | |
| 1296 | #define BUILTIN_NOSPEC "0" |
| 1297 | #define BUILTIN_SPECIAL "1" |
| 1298 | #define BUILTIN_REGULAR "2" |
| 1299 | #define BUILTIN_SPEC_REG "3" |
| 1300 | #define BUILTIN_ASSIGN "4" |
| 1301 | #define BUILTIN_SPEC_ASSG "5" |
| 1302 | #define BUILTIN_REG_ASSG "6" |
| 1303 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 1304 | |
| 1305 | #define IS_BUILTIN_SPECIAL(builtincmd) ((builtincmd)->name[0] & 1) |
| 1306 | #define IS_BUILTIN_REGULAR(builtincmd) ((builtincmd)->name[0] & 2) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 1307 | #define IS_BUILTIN_ASSIGN(builtincmd) ((builtincmd)->name[0] & 4) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1308 | |
| 1309 | static const struct builtincmd builtincmd[] = { |
| 1310 | { BUILTIN_SPEC_REG ".", dotcmd }, |
| 1311 | { BUILTIN_SPEC_REG ":", truecmd }, |
| 1312 | #ifdef CONFIG_ASH_ALIAS |
| 1313 | { BUILTIN_REG_ASSG "alias", aliascmd }, |
| 1314 | #endif |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1315 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1316 | { BUILTIN_REGULAR "bg", bgcmd }, |
| 1317 | #endif |
| 1318 | { BUILTIN_SPEC_REG "break", breakcmd }, |
| 1319 | { BUILTIN_REGULAR "cd", cdcmd }, |
| 1320 | { BUILTIN_NOSPEC "chdir", cdcmd }, |
| 1321 | #ifdef CONFIG_ASH_CMDCMD |
| 1322 | { BUILTIN_REGULAR "command", commandcmd }, |
| 1323 | #endif |
| 1324 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
Paul Fox | 0b62158 | 2005-08-09 19:38:05 +0000 | [diff] [blame] | 1325 | #ifdef CONFIG_ASH_BUILTIN_ECHO |
| 1326 | { BUILTIN_REGULAR "echo", echocmd }, |
| 1327 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1328 | { BUILTIN_SPEC_REG "eval", evalcmd }, |
| 1329 | { BUILTIN_SPEC_REG "exec", execcmd }, |
| 1330 | { BUILTIN_SPEC_REG "exit", exitcmd }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1331 | { BUILTIN_SPEC_REG_ASSG "export", exportcmd }, |
| 1332 | { BUILTIN_REGULAR "false", falsecmd }, |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1333 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1334 | { BUILTIN_REGULAR "fg", fgcmd }, |
| 1335 | #endif |
| 1336 | #ifdef CONFIG_ASH_GETOPTS |
| 1337 | { BUILTIN_REGULAR "getopts", getoptscmd }, |
| 1338 | #endif |
| 1339 | { BUILTIN_NOSPEC "hash", hashcmd }, |
| 1340 | #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET |
| 1341 | { BUILTIN_NOSPEC "help", helpcmd }, |
| 1342 | #endif |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1343 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1344 | { BUILTIN_REGULAR "jobs", jobscmd }, |
| 1345 | { BUILTIN_REGULAR "kill", killcmd }, |
| 1346 | #endif |
| 1347 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 1348 | { BUILTIN_NOSPEC "let", letcmd }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1349 | #endif |
| 1350 | { BUILTIN_ASSIGN "local", localcmd }, |
| 1351 | { BUILTIN_NOSPEC "pwd", pwdcmd }, |
| 1352 | { BUILTIN_REGULAR "read", readcmd }, |
| 1353 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 1354 | { BUILTIN_SPEC_REG "return", returncmd }, |
| 1355 | { BUILTIN_SPEC_REG "set", setcmd }, |
| 1356 | { BUILTIN_SPEC_REG "shift", shiftcmd }, |
| 1357 | { BUILTIN_SPEC_REG "times", timescmd }, |
| 1358 | { BUILTIN_SPEC_REG "trap", trapcmd }, |
| 1359 | { BUILTIN_REGULAR "true", truecmd }, |
| 1360 | { BUILTIN_NOSPEC "type", typecmd }, |
| 1361 | { BUILTIN_NOSPEC "ulimit", ulimitcmd }, |
| 1362 | { BUILTIN_REGULAR "umask", umaskcmd }, |
| 1363 | #ifdef CONFIG_ASH_ALIAS |
| 1364 | { BUILTIN_REGULAR "unalias", unaliascmd }, |
| 1365 | #endif |
| 1366 | { BUILTIN_SPEC_REG "unset", unsetcmd }, |
| 1367 | { BUILTIN_REGULAR "wait", waitcmd }, |
| 1368 | }; |
| 1369 | |
| 1370 | #define NUMBUILTINS (sizeof (builtincmd) / sizeof (struct builtincmd) ) |
| 1371 | |
| 1372 | |
| 1373 | |
| 1374 | struct cmdentry { |
| 1375 | int cmdtype; |
| 1376 | union param { |
| 1377 | int index; |
| 1378 | const struct builtincmd *cmd; |
| 1379 | struct funcnode *func; |
| 1380 | } u; |
| 1381 | }; |
| 1382 | |
| 1383 | |
| 1384 | /* action to find_command() */ |
| 1385 | #define DO_ERR 0x01 /* prints errors */ |
| 1386 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 1387 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 1388 | #define DO_ALTPATH 0x08 /* using alternate path */ |
| 1389 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ |
| 1390 | |
| 1391 | static const char *pathopt; /* set by padvance */ |
| 1392 | |
| 1393 | static void shellexec(char **, const char *, int) |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 1394 | ATTRIBUTE_NORETURN; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1395 | static char *padvance(const char **, const char *); |
| 1396 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 1397 | static struct builtincmd *find_builtin(const char *); |
| 1398 | static void hashcd(void); |
| 1399 | static void changepath(const char *); |
| 1400 | static void defun(char *, union node *); |
| 1401 | static void unsetfunc(const char *); |
| 1402 | |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 1403 | #ifdef CONFIG_ASH_MATH_SUPPORT_64 |
| 1404 | typedef int64_t arith_t; |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 1405 | #define arith_t_type (long long) |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 1406 | #else |
| 1407 | typedef long arith_t; |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 1408 | #define arith_t_type (long) |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 1409 | #endif |
Glenn L McGrath | 5f2a23c | 2004-06-25 07:05:13 +0000 | [diff] [blame] | 1410 | |
| 1411 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 1412 | static arith_t dash_arith(const char *); |
| 1413 | static arith_t arith(const char *expr, int *perrcode); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1414 | #endif |
| 1415 | |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1416 | #ifdef CONFIG_ASH_RANDOM_SUPPORT |
| 1417 | static unsigned long rseed; |
| 1418 | static void change_random(const char *); |
| 1419 | # ifndef DYNAMIC_VAR |
| 1420 | # define DYNAMIC_VAR |
| 1421 | # endif |
| 1422 | #endif |
| 1423 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1424 | /* init.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1425 | |
| 1426 | static void reset(void); |
| 1427 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1428 | /* var.h */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1429 | |
| 1430 | /* |
| 1431 | * Shell variables. |
| 1432 | */ |
| 1433 | |
| 1434 | /* flags */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1435 | #define VEXPORT 0x01 /* variable is exported */ |
| 1436 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 1437 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 1438 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 1439 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 1440 | #define VUNSET 0x20 /* the variable is not set */ |
| 1441 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 1442 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 1443 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1444 | #ifdef DYNAMIC_VAR |
| 1445 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 1446 | # else |
| 1447 | # define VDYNAMIC 0 |
| 1448 | #endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1449 | |
| 1450 | struct var { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1451 | struct var *next; /* next entry in hash list */ |
| 1452 | int flags; /* flags are defined above */ |
| 1453 | const char *text; /* name=value */ |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1454 | void (*func)(const char *); /* function to be called when */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1455 | /* the variable gets set/unset */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1456 | }; |
| 1457 | |
| 1458 | struct localvar { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1459 | struct localvar *next; /* next local variable in list */ |
| 1460 | struct var *vp; /* the variable that was made local */ |
| 1461 | int flags; /* saved flags */ |
| 1462 | const char *text; /* saved text */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1463 | }; |
| 1464 | |
| 1465 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1466 | static struct localvar *localvars; |
| 1467 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1468 | /* |
| 1469 | * Shell variables. |
| 1470 | */ |
| 1471 | |
| 1472 | #ifdef CONFIG_ASH_GETOPTS |
| 1473 | static void getoptsreset(const char *); |
| 1474 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 1475 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1476 | #ifdef CONFIG_LOCALE_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1477 | #include <locale.h> |
| 1478 | static void change_lc_all(const char *value); |
| 1479 | static void change_lc_ctype(const char *value); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1480 | #endif |
| 1481 | |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 1482 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1483 | #define VTABSIZE 39 |
| 1484 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 1485 | static const char defpathvar[] = "PATH=/usr/local/bin:/usr/bin:/sbin:/bin"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1486 | #ifdef IFS_BROKEN |
| 1487 | static const char defifsvar[] = "IFS= \t\n"; |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 1488 | #define defifs (defifsvar + 4) |
| 1489 | #else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1490 | static const char defifs[] = " \t\n"; |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 1491 | #endif |
| 1492 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1493 | |
| 1494 | static struct var varinit[] = { |
| 1495 | #ifdef IFS_BROKEN |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1496 | { 0, VSTRFIXED|VTEXTFIXED, defifsvar, 0 }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1497 | #else |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1498 | { 0, VSTRFIXED|VTEXTFIXED|VUNSET, "IFS\0", 0 }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1499 | #endif |
| 1500 | |
| 1501 | #ifdef CONFIG_ASH_MAIL |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1502 | { 0, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL\0", changemail }, |
| 1503 | { 0, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH\0", changemail }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1504 | #endif |
| 1505 | |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1506 | { 0, VSTRFIXED|VTEXTFIXED, defpathvar, changepath }, |
| 1507 | { 0, VSTRFIXED|VTEXTFIXED, "PS1=$ ", 0 }, |
| 1508 | { 0, VSTRFIXED|VTEXTFIXED, "PS2=> ", 0 }, |
| 1509 | { 0, VSTRFIXED|VTEXTFIXED, "PS4=+ ", 0 }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1510 | #ifdef CONFIG_ASH_GETOPTS |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1511 | { 0, VSTRFIXED|VTEXTFIXED, "OPTIND=1", getoptsreset }, |
| 1512 | #endif |
| 1513 | #ifdef CONFIG_ASH_RANDOM_SUPPORT |
| 1514 | {0, VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM\0", change_random }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1515 | #endif |
| 1516 | #ifdef CONFIG_LOCALE_SUPPORT |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1517 | {0, VSTRFIXED | VTEXTFIXED | VUNSET, "LC_ALL\0", change_lc_all }, |
| 1518 | {0, VSTRFIXED | VTEXTFIXED | VUNSET, "LC_CTYPE\0", change_lc_ctype }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1519 | #endif |
| 1520 | #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1521 | {0, VSTRFIXED | VTEXTFIXED | VUNSET, "HISTFILE\0", NULL }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1522 | #endif |
| 1523 | }; |
| 1524 | |
| 1525 | #define vifs varinit[0] |
| 1526 | #ifdef CONFIG_ASH_MAIL |
| 1527 | #define vmail (&vifs)[1] |
| 1528 | #define vmpath (&vmail)[1] |
| 1529 | #else |
| 1530 | #define vmpath vifs |
| 1531 | #endif |
| 1532 | #define vpath (&vmpath)[1] |
| 1533 | #define vps1 (&vpath)[1] |
| 1534 | #define vps2 (&vps1)[1] |
| 1535 | #define vps4 (&vps2)[1] |
| 1536 | #define voptind (&vps4)[1] |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1537 | #ifdef CONFIG_ASH_GETOPTS |
| 1538 | #define vrandom (&voptind)[1] |
| 1539 | #else |
| 1540 | #define vrandom (&vps4)[1] |
| 1541 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1542 | #define defpath (defpathvar + 5) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1543 | |
| 1544 | /* |
| 1545 | * The following macros access the values of the above variables. |
| 1546 | * They have to skip over the name. They return the null string |
| 1547 | * for unset variables. |
| 1548 | */ |
| 1549 | |
| 1550 | #define ifsval() (vifs.text + 4) |
| 1551 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
| 1552 | #define mailval() (vmail.text + 5) |
| 1553 | #define mpathval() (vmpath.text + 9) |
| 1554 | #define pathval() (vpath.text + 5) |
| 1555 | #define ps1val() (vps1.text + 4) |
| 1556 | #define ps2val() (vps2.text + 4) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1557 | #define ps4val() (vps4.text + 4) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1558 | #define optindval() (voptind.text + 7) |
| 1559 | |
| 1560 | #define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1561 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 1562 | static void setvar(const char *, const char *, int); |
| 1563 | static void setvareq(char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1564 | static void listsetvar(struct strlist *, int); |
| 1565 | static char *lookupvar(const char *); |
| 1566 | static char *bltinlookup(const char *); |
| 1567 | static char **listvars(int, int, char ***); |
| 1568 | #define environment() listvars(VEXPORT, VUNSET, 0) |
| 1569 | static int showvars(const char *, int, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 1570 | static void poplocalvars(void); |
| 1571 | static int unsetvar(const char *); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1572 | #ifdef CONFIG_ASH_GETOPTS |
| 1573 | static int setvarsafe(const char *, const char *, int); |
| 1574 | #endif |
| 1575 | static int varcmp(const char *, const char *); |
| 1576 | static struct var **hashvar(const char *); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1577 | |
| 1578 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1579 | static inline int varequal(const char *a, const char *b) { |
| 1580 | return !varcmp(a, b); |
| 1581 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 1582 | |
| 1583 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1584 | static int loopnest; /* current loop nesting level */ |
| 1585 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1586 | /* |
| 1587 | * The parsefile structure pointed to by the global variable parsefile |
| 1588 | * contains information about the current file being read. |
| 1589 | */ |
| 1590 | |
| 1591 | |
| 1592 | struct redirtab { |
| 1593 | struct redirtab *next; |
| 1594 | int renamed[10]; |
| 1595 | int nullredirs; |
| 1596 | }; |
| 1597 | |
| 1598 | static struct redirtab *redirlist; |
| 1599 | static int nullredirs; |
| 1600 | |
| 1601 | extern char **environ; |
| 1602 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1603 | /* output.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1604 | |
| 1605 | |
| 1606 | static void outstr(const char *, FILE *); |
| 1607 | static void outcslow(int, FILE *); |
| 1608 | static void flushall(void); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1609 | static void flusherr(void); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1610 | static int out1fmt(const char *, ...) |
| 1611 | __attribute__((__format__(__printf__,1,2))); |
| 1612 | static int fmtstr(char *, size_t, const char *, ...) |
| 1613 | __attribute__((__format__(__printf__,3,4))); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1614 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 1615 | static int preverrout_fd; /* save fd2 before print debug if xflag is set. */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1616 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1617 | |
| 1618 | static void out1str(const char *p) |
| 1619 | { |
| 1620 | outstr(p, stdout); |
| 1621 | } |
| 1622 | |
| 1623 | static void out2str(const char *p) |
| 1624 | { |
| 1625 | outstr(p, stderr); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 1626 | flusherr(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | /* |
| 1630 | * Initialization code. |
| 1631 | */ |
| 1632 | |
| 1633 | /* |
| 1634 | * This routine initializes the builtin variables. |
| 1635 | */ |
| 1636 | |
| 1637 | static inline void |
| 1638 | initvar(void) |
| 1639 | { |
| 1640 | struct var *vp; |
| 1641 | struct var *end; |
| 1642 | struct var **vpp; |
| 1643 | |
| 1644 | /* |
| 1645 | * PS1 depends on uid |
| 1646 | */ |
| 1647 | #if defined(CONFIG_FEATURE_COMMAND_EDITING) && defined(CONFIG_FEATURE_SH_FANCY_PROMPT) |
| 1648 | vps1.text = "PS1=\\w \\$ "; |
| 1649 | #else |
| 1650 | if (!geteuid()) |
| 1651 | vps1.text = "PS1=# "; |
| 1652 | #endif |
| 1653 | vp = varinit; |
| 1654 | end = vp + sizeof(varinit) / sizeof(varinit[0]); |
| 1655 | do { |
| 1656 | vpp = hashvar(vp->text); |
| 1657 | vp->next = *vpp; |
| 1658 | *vpp = vp; |
| 1659 | } while (++vp < end); |
| 1660 | } |
| 1661 | |
| 1662 | static inline void |
| 1663 | init(void) |
| 1664 | { |
| 1665 | |
| 1666 | /* from input.c: */ |
| 1667 | { |
| 1668 | basepf.nextc = basepf.buf = basebuf; |
| 1669 | } |
| 1670 | |
| 1671 | /* from trap.c: */ |
| 1672 | { |
| 1673 | signal(SIGCHLD, SIG_DFL); |
| 1674 | } |
| 1675 | |
| 1676 | /* from var.c: */ |
| 1677 | { |
| 1678 | char **envp; |
| 1679 | char ppid[32]; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 1680 | const char *p; |
| 1681 | struct stat st1, st2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1682 | |
| 1683 | initvar(); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 1684 | for (envp = environ ; envp && *envp ; envp++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1685 | if (strchr(*envp, '=')) { |
| 1686 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | snprintf(ppid, sizeof(ppid), "%d", (int) getppid()); |
| 1691 | setvar("PPID", ppid, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 1692 | |
| 1693 | p = lookupvar("PWD"); |
| 1694 | if (p) |
| 1695 | if (*p != '/' || stat(p, &st1) || stat(".", &st2) || |
| 1696 | st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) |
| 1697 | p = 0; |
| 1698 | setpwd(p, 0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | /* PEOF (the end of file marker) */ |
| 1703 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1704 | enum { |
| 1705 | INPUT_PUSH_FILE = 1, |
| 1706 | INPUT_NOFILE_OK = 2, |
| 1707 | }; |
| 1708 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1709 | /* |
| 1710 | * The input line number. Input.c just defines this variable, and saves |
| 1711 | * and restores it when files are pushed and popped. The user of this |
| 1712 | * package must set its value. |
| 1713 | */ |
| 1714 | |
| 1715 | static int pgetc(void); |
| 1716 | static int pgetc2(void); |
| 1717 | static int preadbuffer(void); |
| 1718 | static void pungetc(void); |
| 1719 | static void pushstring(char *, void *); |
| 1720 | static void popstring(void); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1721 | static void setinputfd(int, int); |
| 1722 | static void setinputstring(char *); |
| 1723 | static void popfile(void); |
| 1724 | static void popallfiles(void); |
| 1725 | static void closescript(void); |
| 1726 | |
| 1727 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1728 | /* jobs.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1729 | |
| 1730 | |
| 1731 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
| 1732 | #define FORK_FG 0 |
| 1733 | #define FORK_BG 1 |
| 1734 | #define FORK_NOJOB 2 |
| 1735 | |
| 1736 | /* mode flags for showjob(s) */ |
| 1737 | #define SHOW_PGID 0x01 /* only show pgid - for jobs -p */ |
| 1738 | #define SHOW_PID 0x04 /* include process pid */ |
| 1739 | #define SHOW_CHANGED 0x08 /* only jobs whose state has changed */ |
| 1740 | |
| 1741 | |
| 1742 | /* |
| 1743 | * A job structure contains information about a job. A job is either a |
| 1744 | * single process or a set of processes contained in a pipeline. In the |
| 1745 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 1746 | * array of pids. |
| 1747 | */ |
| 1748 | |
| 1749 | struct procstat { |
| 1750 | pid_t pid; /* process id */ |
| 1751 | int status; /* last process status from wait() */ |
| 1752 | char *cmd; /* text of command being run */ |
| 1753 | }; |
| 1754 | |
| 1755 | struct job { |
| 1756 | struct procstat ps0; /* status of process */ |
| 1757 | struct procstat *ps; /* status or processes when more than one */ |
| 1758 | #if JOBS |
| 1759 | int stopstatus; /* status of a stopped job */ |
| 1760 | #endif |
| 1761 | uint32_t |
| 1762 | nprocs: 16, /* number of processes */ |
| 1763 | state: 8, |
| 1764 | #define JOBRUNNING 0 /* at least one proc running */ |
| 1765 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 1766 | #define JOBDONE 2 /* all procs are completed */ |
| 1767 | #if JOBS |
| 1768 | sigint: 1, /* job was killed by SIGINT */ |
| 1769 | jobctl: 1, /* job running under job control */ |
| 1770 | #endif |
| 1771 | waited: 1, /* true if this entry has been waited for */ |
| 1772 | used: 1, /* true if this entry is in used */ |
| 1773 | changed: 1; /* true if status has changed */ |
| 1774 | struct job *prev_job; /* previous job */ |
| 1775 | }; |
| 1776 | |
| 1777 | static pid_t backgndpid; /* pid of last background process */ |
| 1778 | static int job_warning; /* user was warned about stopped jobs */ |
| 1779 | #if JOBS |
| 1780 | static int jobctl; /* true if doing job control */ |
| 1781 | #endif |
| 1782 | |
| 1783 | static struct job *makejob(union node *, int); |
| 1784 | static int forkshell(struct job *, union node *, int); |
| 1785 | static int waitforjob(struct job *); |
| 1786 | static int stoppedjobs(void); |
| 1787 | |
| 1788 | #if ! JOBS |
| 1789 | #define setjobctl(on) /* do nothing */ |
| 1790 | #else |
| 1791 | static void setjobctl(int); |
| 1792 | static void showjobs(FILE *, int); |
| 1793 | #endif |
| 1794 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1795 | /* main.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1796 | |
| 1797 | |
| 1798 | /* pid of main shell */ |
| 1799 | static int rootpid; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1800 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 1801 | static int shlvl; |
| 1802 | #define rootshell (!shlvl) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1803 | |
| 1804 | static void readcmdfile(char *); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1805 | static int cmdloop(int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1806 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1807 | /* memalloc.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1808 | |
| 1809 | |
| 1810 | struct stackmark { |
| 1811 | struct stack_block *stackp; |
| 1812 | char *stacknxt; |
| 1813 | size_t stacknleft; |
| 1814 | struct stackmark *marknext; |
| 1815 | }; |
| 1816 | |
| 1817 | /* minimum size of a block */ |
| 1818 | #define MINSIZE SHELL_ALIGN(504) |
| 1819 | |
| 1820 | struct stack_block { |
| 1821 | struct stack_block *prev; |
| 1822 | char space[MINSIZE]; |
| 1823 | }; |
| 1824 | |
| 1825 | static struct stack_block stackbase; |
| 1826 | static struct stack_block *stackp = &stackbase; |
| 1827 | static struct stackmark *markp; |
| 1828 | static char *stacknxt = stackbase.space; |
| 1829 | static size_t stacknleft = MINSIZE; |
| 1830 | static char *sstrend = stackbase.space + MINSIZE; |
| 1831 | static int herefd = -1; |
| 1832 | |
| 1833 | |
| 1834 | static pointer ckmalloc(size_t); |
| 1835 | static pointer ckrealloc(pointer, size_t); |
| 1836 | static char *savestr(const char *); |
| 1837 | static pointer stalloc(size_t); |
| 1838 | static void stunalloc(pointer); |
| 1839 | static void setstackmark(struct stackmark *); |
| 1840 | static void popstackmark(struct stackmark *); |
| 1841 | static void growstackblock(void); |
| 1842 | static void *growstackstr(void); |
| 1843 | static char *makestrspace(size_t, char *); |
| 1844 | static char *stnputs(const char *, size_t, char *); |
| 1845 | static char *stputs(const char *, char *); |
| 1846 | |
| 1847 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1848 | static inline char *_STPUTC(int c, char *p) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1849 | if (p == sstrend) |
| 1850 | p = growstackstr(); |
| 1851 | *p++ = c; |
| 1852 | return p; |
| 1853 | } |
| 1854 | |
| 1855 | #define stackblock() ((void *)stacknxt) |
| 1856 | #define stackblocksize() stacknleft |
| 1857 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1858 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
| 1859 | #define CHECKSTRSPACE(n, p) \ |
| 1860 | ({ \ |
| 1861 | char *q = (p); \ |
| 1862 | size_t l = (n); \ |
| 1863 | size_t m = sstrend - q; \ |
| 1864 | if (l > m) \ |
| 1865 | (p) = makestrspace(l, q); \ |
| 1866 | 0; \ |
| 1867 | }) |
| 1868 | #define USTPUTC(c, p) (*p++ = (c)) |
| 1869 | #define STACKSTRNUL(p) ((p) == sstrend? (p = growstackstr(), *p = '\0') : (*p = '\0')) |
| 1870 | #define STUNPUTC(p) (--p) |
| 1871 | #define STTOPC(p) p[-1] |
| 1872 | #define STADJUST(amount, p) (p += (amount)) |
| 1873 | |
| 1874 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
| 1875 | #define ungrabstackstr(s, p) stunalloc((s)) |
| 1876 | #define stackstrend() ((void *)sstrend) |
| 1877 | |
| 1878 | #define ckfree(p) free((pointer)(p)) |
| 1879 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1880 | /* mystring.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1881 | |
| 1882 | |
| 1883 | #define DOLATSTRLEN 4 |
| 1884 | |
| 1885 | static char *prefix(const char *, const char *); |
| 1886 | static int number(const char *); |
| 1887 | static int is_number(const char *); |
| 1888 | static char *single_quote(const char *); |
| 1889 | static char *sstrdup(const char *); |
| 1890 | |
| 1891 | #define equal(s1, s2) (strcmp(s1, s2) == 0) |
| 1892 | #define scopy(s1, s2) ((void)strcpy(s2, s1)) |
| 1893 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1894 | /* options.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1895 | |
| 1896 | struct shparam { |
| 1897 | int nparam; /* # of positional parameters (without $0) */ |
| 1898 | unsigned char malloc; /* if parameter list dynamically allocated */ |
| 1899 | char **p; /* parameter list */ |
| 1900 | #ifdef CONFIG_ASH_GETOPTS |
| 1901 | int optind; /* next parameter to be processed by getopts */ |
| 1902 | int optoff; /* used by getopts */ |
| 1903 | #endif |
| 1904 | }; |
| 1905 | |
| 1906 | |
| 1907 | #define eflag optlist[0] |
| 1908 | #define fflag optlist[1] |
| 1909 | #define Iflag optlist[2] |
| 1910 | #define iflag optlist[3] |
| 1911 | #define mflag optlist[4] |
| 1912 | #define nflag optlist[5] |
| 1913 | #define sflag optlist[6] |
| 1914 | #define xflag optlist[7] |
| 1915 | #define vflag optlist[8] |
| 1916 | #define Cflag optlist[9] |
| 1917 | #define aflag optlist[10] |
| 1918 | #define bflag optlist[11] |
| 1919 | #define uflag optlist[12] |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1920 | #define viflag optlist[13] |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1921 | |
| 1922 | #ifdef DEBUG |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1923 | #define nolog optlist[14] |
| 1924 | #define debug optlist[15] |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1925 | #endif |
| 1926 | |
| 1927 | #ifndef CONFIG_FEATURE_COMMAND_EDITING_VI |
| 1928 | #define setvimode(on) viflag = 0 /* forcibly keep the option off */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1929 | #endif |
| 1930 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1931 | /* options.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1932 | |
| 1933 | |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1934 | static const char *const optletters_optnames[] = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1935 | "e" "errexit", |
| 1936 | "f" "noglob", |
| 1937 | "I" "ignoreeof", |
| 1938 | "i" "interactive", |
| 1939 | "m" "monitor", |
| 1940 | "n" "noexec", |
| 1941 | "s" "stdin", |
| 1942 | "x" "xtrace", |
| 1943 | "v" "verbose", |
| 1944 | "C" "noclobber", |
| 1945 | "a" "allexport", |
| 1946 | "b" "notify", |
| 1947 | "u" "nounset", |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1948 | "\0" "vi", |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1949 | #ifdef DEBUG |
| 1950 | "\0" "nolog", |
| 1951 | "\0" "debug", |
| 1952 | #endif |
| 1953 | }; |
| 1954 | |
| 1955 | #define optletters(n) optletters_optnames[(n)][0] |
| 1956 | #define optnames(n) (&optletters_optnames[(n)][1]) |
| 1957 | |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1958 | #define NOPTS (sizeof(optletters_optnames)/sizeof(optletters_optnames[0])) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1959 | |
| 1960 | static char optlist[NOPTS]; |
| 1961 | |
| 1962 | |
| 1963 | static char *arg0; /* value of $0 */ |
| 1964 | static struct shparam shellparam; /* $@ current positional parameters */ |
| 1965 | static char **argptr; /* argument list for builtin commands */ |
| 1966 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1967 | static char *optptr; /* used by nextopt */ |
| 1968 | |
| 1969 | static char *minusc; /* argument to -c option */ |
| 1970 | |
| 1971 | |
| 1972 | static void procargs(int, char **); |
| 1973 | static void optschanged(void); |
| 1974 | static void setparam(char **); |
| 1975 | static void freeparam(volatile struct shparam *); |
| 1976 | static int shiftcmd(int, char **); |
| 1977 | static int setcmd(int, char **); |
| 1978 | static int nextopt(const char *); |
| 1979 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1980 | /* redir.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1981 | |
| 1982 | /* flags passed to redirect */ |
| 1983 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 1984 | #define REDIR_SAVEFD2 03 /* set preverrout */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1985 | |
| 1986 | union node; |
| 1987 | static void redirect(union node *, int); |
| 1988 | static void popredir(int); |
| 1989 | static void clearredir(int); |
| 1990 | static int copyfd(int, int); |
| 1991 | static int redirectsafe(union node *, int); |
| 1992 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 1993 | /* show.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1994 | |
| 1995 | |
| 1996 | #ifdef DEBUG |
| 1997 | static void showtree(union node *); |
| 1998 | static void trace(const char *, ...); |
| 1999 | static void tracev(const char *, va_list); |
| 2000 | static void trargs(char **); |
| 2001 | static void trputc(int); |
| 2002 | static void trputs(const char *); |
| 2003 | static void opentrace(void); |
| 2004 | #endif |
| 2005 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2006 | /* trap.h */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2007 | |
| 2008 | |
| 2009 | /* trap handler commands */ |
| 2010 | static char *trap[NSIG]; |
| 2011 | /* current value of signal */ |
| 2012 | static char sigmode[NSIG - 1]; |
| 2013 | /* indicates specified signal received */ |
| 2014 | static char gotsig[NSIG - 1]; |
| 2015 | |
| 2016 | static void clear_traps(void); |
| 2017 | static void setsignal(int); |
| 2018 | static void ignoresig(int); |
| 2019 | static void onsig(int); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2020 | static int dotrap(void); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2021 | static void setinteractive(int); |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 2022 | static void exitshell(void) ATTRIBUTE_NORETURN; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2023 | static int decode_signal(const char *, int); |
| 2024 | |
| 2025 | /* |
| 2026 | * This routine is called when an error or an interrupt occurs in an |
| 2027 | * interactive shell and control is returned to the main command loop. |
| 2028 | */ |
| 2029 | |
| 2030 | static void |
| 2031 | reset(void) |
| 2032 | { |
| 2033 | /* from eval.c: */ |
| 2034 | { |
| 2035 | evalskip = 0; |
| 2036 | loopnest = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | /* from input.c: */ |
| 2040 | { |
| 2041 | parselleft = parsenleft = 0; /* clear input buffer */ |
| 2042 | popallfiles(); |
| 2043 | } |
| 2044 | |
| 2045 | /* from parser.c: */ |
| 2046 | { |
| 2047 | tokpushback = 0; |
| 2048 | checkkwd = 0; |
| 2049 | } |
| 2050 | |
| 2051 | /* from redir.c: */ |
| 2052 | { |
| 2053 | clearredir(0); |
| 2054 | } |
| 2055 | |
| 2056 | } |
| 2057 | |
| 2058 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2059 | static struct alias *atab[ATABSIZE]; |
| 2060 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2061 | static void setalias(const char *, const char *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2062 | static struct alias *freealias(struct alias *); |
| 2063 | static struct alias **__lookupalias(const char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2064 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2065 | static void |
| 2066 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2067 | { |
| 2068 | struct alias *ap, **app; |
| 2069 | |
| 2070 | app = __lookupalias(name); |
| 2071 | ap = *app; |
| 2072 | INTOFF; |
| 2073 | if (ap) { |
| 2074 | if (!(ap->flag & ALIASINUSE)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2075 | ckfree(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2076 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2077 | ap->val = savestr(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2078 | ap->flag &= ~ALIASDEAD; |
| 2079 | } else { |
| 2080 | /* not found */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2081 | ap = ckmalloc(sizeof (struct alias)); |
| 2082 | ap->name = savestr(name); |
| 2083 | ap->val = savestr(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2084 | ap->flag = 0; |
| 2085 | ap->next = 0; |
| 2086 | *app = ap; |
| 2087 | } |
| 2088 | INTON; |
| 2089 | } |
| 2090 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2091 | static int |
| 2092 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2093 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2094 | struct alias **app; |
| 2095 | |
| 2096 | app = __lookupalias(name); |
| 2097 | |
| 2098 | if (*app) { |
| 2099 | INTOFF; |
| 2100 | *app = freealias(*app); |
| 2101 | INTON; |
| 2102 | return (0); |
| 2103 | } |
| 2104 | |
| 2105 | return (1); |
| 2106 | } |
| 2107 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2108 | static void |
| 2109 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2110 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2111 | struct alias *ap, **app; |
| 2112 | int i; |
| 2113 | |
| 2114 | INTOFF; |
| 2115 | for (i = 0; i < ATABSIZE; i++) { |
| 2116 | app = &atab[i]; |
| 2117 | for (ap = *app; ap; ap = *app) { |
| 2118 | *app = freealias(*app); |
| 2119 | if (ap == *app) { |
| 2120 | app = &ap->next; |
| 2121 | } |
| 2122 | } |
| 2123 | } |
| 2124 | INTON; |
| 2125 | } |
| 2126 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2127 | static struct alias * |
| 2128 | lookupalias(const char *name, int check) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2129 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2130 | struct alias *ap = *__lookupalias(name); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2131 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2132 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 2133 | return (NULL); |
| 2134 | return (ap); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2137 | /* |
| 2138 | * TODO - sort output |
| 2139 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2140 | static int |
| 2141 | aliascmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2142 | { |
| 2143 | char *n, *v; |
| 2144 | int ret = 0; |
| 2145 | struct alias *ap; |
| 2146 | |
| 2147 | if (argc == 1) { |
| 2148 | int i; |
| 2149 | |
| 2150 | for (i = 0; i < ATABSIZE; i++) |
| 2151 | for (ap = atab[i]; ap; ap = ap->next) { |
| 2152 | printalias(ap); |
| 2153 | } |
| 2154 | return (0); |
| 2155 | } |
| 2156 | while ((n = *++argv) != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2157 | if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2158 | if ((ap = *__lookupalias(n)) == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2159 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2160 | ret = 1; |
| 2161 | } else |
| 2162 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2163 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2164 | *v++ = '\0'; |
| 2165 | setalias(n, v); |
| 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | return (ret); |
| 2170 | } |
| 2171 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2172 | static int |
| 2173 | unaliascmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2174 | { |
| 2175 | int i; |
| 2176 | |
| 2177 | while ((i = nextopt("a")) != '\0') { |
| 2178 | if (i == 'a') { |
| 2179 | rmaliases(); |
| 2180 | return (0); |
| 2181 | } |
| 2182 | } |
| 2183 | for (i = 0; *argptr; argptr++) { |
| 2184 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2185 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2186 | i = 1; |
| 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | return (i); |
| 2191 | } |
| 2192 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2193 | static struct alias * |
| 2194 | freealias(struct alias *ap) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2195 | struct alias *next; |
| 2196 | |
| 2197 | if (ap->flag & ALIASINUSE) { |
| 2198 | ap->flag |= ALIASDEAD; |
| 2199 | return ap; |
| 2200 | } |
| 2201 | |
| 2202 | next = ap->next; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2203 | ckfree(ap->name); |
| 2204 | ckfree(ap->val); |
| 2205 | ckfree(ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2206 | return next; |
| 2207 | } |
| 2208 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2209 | static void |
| 2210 | printalias(const struct alias *ap) { |
| 2211 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 2212 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2213 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2214 | static struct alias ** |
| 2215 | __lookupalias(const char *name) { |
| 2216 | unsigned int hashval; |
| 2217 | struct alias **app; |
| 2218 | const char *p; |
| 2219 | unsigned int ch; |
| 2220 | |
| 2221 | p = name; |
| 2222 | |
| 2223 | ch = (unsigned char)*p; |
| 2224 | hashval = ch << 4; |
| 2225 | while (ch) { |
| 2226 | hashval += ch; |
| 2227 | ch = (unsigned char)*++p; |
| 2228 | } |
| 2229 | app = &atab[hashval % ATABSIZE]; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2230 | |
| 2231 | for (; *app; app = &(*app)->next) { |
| 2232 | if (equal(name, (*app)->name)) { |
| 2233 | break; |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | return app; |
| 2238 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2239 | #endif /* CONFIG_ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2240 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2241 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2242 | /* cd.c */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2243 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2244 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2245 | * The cd and pwd commands. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2246 | */ |
| 2247 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2248 | #define CD_PHYSICAL 1 |
| 2249 | #define CD_PRINT 2 |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2250 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2251 | static int docd(const char *, int); |
| 2252 | static int cdopt(void); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2253 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2254 | static char *curdir = nullstr; /* current working directory */ |
| 2255 | static char *physdir = nullstr; /* physical working directory */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2256 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2257 | static int |
| 2258 | cdopt(void) |
| 2259 | { |
| 2260 | int flags = 0; |
| 2261 | int i, j; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2262 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2263 | j = 'L'; |
| 2264 | while ((i = nextopt("LP"))) { |
| 2265 | if (i != j) { |
| 2266 | flags ^= CD_PHYSICAL; |
| 2267 | j = i; |
| 2268 | } |
| 2269 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2270 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2271 | return flags; |
| 2272 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2273 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2274 | static int |
| 2275 | cdcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2276 | { |
| 2277 | const char *dest; |
| 2278 | const char *path; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2279 | const char *p; |
| 2280 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2281 | struct stat statb; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2282 | int flags; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2283 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2284 | flags = cdopt(); |
| 2285 | dest = *argptr; |
| 2286 | if (!dest) |
| 2287 | dest = bltinlookup(homestr); |
| 2288 | else if (dest[0] == '-' && dest[1] == '\0') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2289 | dest = bltinlookup("OLDPWD"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2290 | flags |= CD_PRINT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2291 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2292 | if (!dest) |
| 2293 | dest = nullstr; |
| 2294 | if (*dest == '/') |
| 2295 | goto step7; |
| 2296 | if (*dest == '.') { |
| 2297 | c = dest[1]; |
| 2298 | dotdot: |
| 2299 | switch (c) { |
| 2300 | case '\0': |
| 2301 | case '/': |
| 2302 | goto step6; |
| 2303 | case '.': |
| 2304 | c = dest[2]; |
| 2305 | if (c != '.') |
| 2306 | goto dotdot; |
| 2307 | } |
| 2308 | } |
| 2309 | if (!*dest) |
| 2310 | dest = "."; |
| 2311 | if (!(path = bltinlookup("CDPATH"))) { |
| 2312 | step6: |
| 2313 | step7: |
| 2314 | p = dest; |
| 2315 | goto docd; |
| 2316 | } |
| 2317 | do { |
| 2318 | c = *path; |
| 2319 | p = padvance(&path, dest); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2320 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2321 | if (c && c != ':') |
| 2322 | flags |= CD_PRINT; |
| 2323 | docd: |
| 2324 | if (!docd(p, flags)) |
| 2325 | goto out; |
| 2326 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2327 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2328 | } while (path); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2329 | sh_error("can't cd to %s", dest); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2330 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2331 | out: |
| 2332 | if (flags & CD_PRINT) |
| 2333 | out1fmt(snlfmt, curdir); |
| 2334 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | |
| 2338 | /* |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 2339 | * Update curdir (the name of the current directory) in response to a |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2340 | * cd command. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 2341 | */ |
| 2342 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2343 | static inline const char * |
| 2344 | updatepwd(const char *dir) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 2345 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2346 | char *new; |
| 2347 | char *p; |
| 2348 | char *cdcomppath; |
| 2349 | const char *lim; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 2350 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2351 | cdcomppath = sstrdup(dir); |
| 2352 | STARTSTACKSTR(new); |
| 2353 | if (*dir != '/') { |
| 2354 | if (curdir == nullstr) |
| 2355 | return 0; |
| 2356 | new = stputs(curdir, new); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 2357 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2358 | new = makestrspace(strlen(dir) + 2, new); |
| 2359 | lim = stackblock() + 1; |
| 2360 | if (*dir != '/') { |
| 2361 | if (new[-1] != '/') |
| 2362 | USTPUTC('/', new); |
| 2363 | if (new > lim && *lim == '/') |
| 2364 | lim++; |
| 2365 | } else { |
| 2366 | USTPUTC('/', new); |
| 2367 | cdcomppath++; |
| 2368 | if (dir[1] == '/' && dir[2] != '/') { |
| 2369 | USTPUTC('/', new); |
| 2370 | cdcomppath++; |
| 2371 | lim++; |
| 2372 | } |
| 2373 | } |
| 2374 | p = strtok(cdcomppath, "/"); |
| 2375 | while (p) { |
| 2376 | switch(*p) { |
| 2377 | case '.': |
| 2378 | if (p[1] == '.' && p[2] == '\0') { |
| 2379 | while (new > lim) { |
| 2380 | STUNPUTC(new); |
| 2381 | if (new[-1] == '/') |
| 2382 | break; |
| 2383 | } |
| 2384 | break; |
| 2385 | } else if (p[1] == '\0') |
| 2386 | break; |
| 2387 | /* fall through */ |
| 2388 | default: |
| 2389 | new = stputs(p, new); |
| 2390 | USTPUTC('/', new); |
| 2391 | } |
| 2392 | p = strtok(0, "/"); |
| 2393 | } |
| 2394 | if (new > lim) |
| 2395 | STUNPUTC(new); |
| 2396 | *new = 0; |
| 2397 | return stackblock(); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2401 | * Actually do the chdir. We also call hashcd to let the routines in exec.c |
| 2402 | * know that the current directory has changed. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2403 | */ |
| 2404 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2405 | static int |
| 2406 | docd(const char *dest, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2407 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2408 | const char *dir = 0; |
| 2409 | int err; |
| 2410 | |
| 2411 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 2412 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2413 | INTOFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2414 | if (!(flags & CD_PHYSICAL)) { |
| 2415 | dir = updatepwd(dest); |
| 2416 | if (dir) |
| 2417 | dest = dir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2418 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2419 | err = chdir(dest); |
| 2420 | if (err) |
| 2421 | goto out; |
| 2422 | setpwd(dir, 1); |
| 2423 | hashcd(); |
| 2424 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2425 | INTON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2426 | return err; |
| 2427 | } |
| 2428 | |
| 2429 | /* |
| 2430 | * Find out what the current directory is. If we already know the current |
| 2431 | * directory, this routine returns immediately. |
| 2432 | */ |
| 2433 | static inline char * |
| 2434 | getpwd(void) |
| 2435 | { |
| 2436 | char *dir = getcwd(0, 0); |
| 2437 | return dir ? dir : nullstr; |
| 2438 | } |
| 2439 | |
| 2440 | static int |
| 2441 | pwdcmd(int argc, char **argv) |
| 2442 | { |
| 2443 | int flags; |
| 2444 | const char *dir = curdir; |
| 2445 | |
| 2446 | flags = cdopt(); |
| 2447 | if (flags) { |
| 2448 | if (physdir == nullstr) |
| 2449 | setpwd(dir, 0); |
| 2450 | dir = physdir; |
| 2451 | } |
| 2452 | out1fmt(snlfmt, dir); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2453 | return 0; |
| 2454 | } |
| 2455 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2456 | static void |
| 2457 | setpwd(const char *val, int setold) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2458 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2459 | char *oldcur, *dir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2460 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2461 | oldcur = dir = curdir; |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 2462 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2463 | if (setold) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2464 | setvar("OLDPWD", oldcur, VEXPORT); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2465 | } |
| 2466 | INTOFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2467 | if (physdir != nullstr) { |
| 2468 | if (physdir != oldcur) |
| 2469 | free(physdir); |
| 2470 | physdir = nullstr; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2471 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2472 | if (oldcur == val || !val) { |
| 2473 | char *s = getpwd(); |
| 2474 | physdir = s; |
| 2475 | if (!val) |
| 2476 | dir = s; |
| 2477 | } else |
| 2478 | dir = savestr(val); |
| 2479 | if (oldcur != dir && oldcur != nullstr) { |
| 2480 | free(oldcur); |
| 2481 | } |
| 2482 | curdir = dir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2483 | INTON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2484 | setvar("PWD", dir, VEXPORT); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2487 | /* error.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2488 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2489 | /* |
| 2490 | * Errors and exceptions. |
| 2491 | */ |
| 2492 | |
| 2493 | /* |
| 2494 | * Code to handle exceptions in C. |
| 2495 | */ |
| 2496 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2497 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2498 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2499 | static void exverror(int, const char *, va_list) |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 2500 | ATTRIBUTE_NORETURN; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2501 | |
| 2502 | /* |
| 2503 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 2504 | * just do a longjmp to the exception handler. The type of exception is |
| 2505 | * stored in the global variable "exception". |
| 2506 | */ |
| 2507 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2508 | static void |
| 2509 | exraise(int e) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2510 | { |
| 2511 | #ifdef DEBUG |
| 2512 | if (handler == NULL) |
| 2513 | abort(); |
| 2514 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2515 | INTOFF; |
| 2516 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2517 | exception = e; |
| 2518 | longjmp(handler->loc, 1); |
| 2519 | } |
| 2520 | |
| 2521 | |
| 2522 | /* |
| 2523 | * Called from trap.c when a SIGINT is received. (If the user specifies |
| 2524 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 2525 | * this routine is not called.) Suppressint is nonzero when interrupts |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2526 | * are held using the INTOFF macro. (The test for iflag is just |
| 2527 | * defensive programming.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2528 | */ |
| 2529 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2530 | static void |
| 2531 | onint(void) { |
| 2532 | int i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2533 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2534 | intpending = 0; |
"Vladimir N. Oleynik" | e6d88ea | 2005-12-13 13:24:23 +0000 | [diff] [blame] | 2535 | #if 0 |
| 2536 | /* comment by vodz: its strange for me, this programm don`t use other |
| 2537 | signal block */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2538 | sigsetmask(0); |
"Vladimir N. Oleynik" | e6d88ea | 2005-12-13 13:24:23 +0000 | [diff] [blame] | 2539 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2540 | i = EXSIG; |
| 2541 | if (gotsig[SIGINT - 1] && !trap[SIGINT]) { |
| 2542 | if (!(rootshell && iflag)) { |
| 2543 | signal(SIGINT, SIG_DFL); |
| 2544 | raise(SIGINT); |
| 2545 | } |
| 2546 | i = EXINT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2547 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2548 | exraise(i); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2549 | /* NOTREACHED */ |
| 2550 | } |
| 2551 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2552 | static void |
| 2553 | exvwarning(const char *msg, va_list ap) |
| 2554 | { |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 2555 | FILE *errs; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2556 | |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 2557 | errs = stderr; |
| 2558 | fprintf(errs, "%s: ", arg0); |
| 2559 | if (commandname) { |
| 2560 | const char *fmt = (!iflag || parsefile->fd) ? |
| 2561 | "%s: %d: " : "%s: "; |
| 2562 | fprintf(errs, fmt, commandname, startlinno); |
| 2563 | } |
| 2564 | vfprintf(errs, msg, ap); |
| 2565 | outcslow('\n', errs); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2566 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2567 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2568 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2569 | * Exverror is called to raise the error exception. If the second argument |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2570 | * is not NULL then error prints an error message using printf style |
| 2571 | * formatting. It then raises the error exception. |
| 2572 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2573 | static void |
| 2574 | exverror(int cond, const char *msg, va_list ap) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2575 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2576 | #ifdef DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2577 | if (msg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2578 | TRACE(("exverror(%d, \"", cond)); |
| 2579 | TRACEV((msg, ap)); |
| 2580 | TRACE(("\") pid=%d\n", getpid())); |
| 2581 | } else |
| 2582 | TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid())); |
| 2583 | if (msg) |
| 2584 | #endif |
| 2585 | exvwarning(msg, ap); |
| 2586 | |
| 2587 | flushall(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2588 | exraise(cond); |
| 2589 | /* NOTREACHED */ |
| 2590 | } |
| 2591 | |
| 2592 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2593 | static void |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2594 | sh_error(const char *msg, ...) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2595 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2596 | va_list ap; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2597 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2598 | va_start(ap, msg); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2599 | exverror(EXERROR, msg, ap); |
| 2600 | /* NOTREACHED */ |
| 2601 | va_end(ap); |
| 2602 | } |
| 2603 | |
| 2604 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2605 | static void |
| 2606 | exerror(int cond, const char *msg, ...) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2607 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2608 | va_list ap; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2609 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2610 | va_start(ap, msg); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2611 | exverror(cond, msg, ap); |
| 2612 | /* NOTREACHED */ |
| 2613 | va_end(ap); |
| 2614 | } |
| 2615 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2616 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2617 | * error/warning routines for external builtins |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2618 | */ |
| 2619 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2620 | static void |
| 2621 | sh_warnx(const char *fmt, ...) |
| 2622 | { |
| 2623 | va_list ap; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2624 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2625 | va_start(ap, fmt); |
| 2626 | exvwarning(fmt, ap); |
| 2627 | va_end(ap); |
| 2628 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2629 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2630 | |
| 2631 | /* |
| 2632 | * Return a string describing an error. The returned string may be a |
| 2633 | * pointer to a static buffer that will be overwritten on the next call. |
| 2634 | * Action describes the operation that got the error. |
| 2635 | */ |
| 2636 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2637 | static const char * |
| 2638 | errmsg(int e, const char *em) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2639 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2640 | if(e == ENOENT || e == ENOTDIR) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2641 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2642 | return em; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2643 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2644 | return strerror(e); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2648 | /* eval.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2649 | |
| 2650 | /* |
| 2651 | * Evaluate a command. |
| 2652 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2653 | |
| 2654 | /* flags in argument to evaltree */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2655 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 2656 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
| 2657 | #define EV_BACKCMD 04 /* command executing within back quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2658 | |
| 2659 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2660 | static void evalloop(union node *, int); |
| 2661 | static void evalfor(union node *, int); |
| 2662 | static void evalcase(union node *, int); |
| 2663 | static void evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2664 | static void expredir(union node *); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2665 | static void evalpipe(union node *, int); |
| 2666 | static void evalcommand(union node *, int); |
| 2667 | static int evalbltin(const struct builtincmd *, int, char **); |
| 2668 | static int evalfun(struct funcnode *, int, char **, int); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 2669 | static void prehash(union node *); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2670 | static int bltincmd(int, char **); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2671 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2672 | |
| 2673 | static const struct builtincmd bltin = { |
| 2674 | "\0\0", bltincmd |
| 2675 | }; |
| 2676 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2677 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2678 | /* |
| 2679 | * Called to reset things after an exception. |
| 2680 | */ |
| 2681 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2682 | /* |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 2683 | * The eval command. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2684 | */ |
| 2685 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2686 | static int |
| 2687 | evalcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2688 | { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2689 | char *p; |
| 2690 | char *concat; |
| 2691 | char **ap; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2692 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2693 | if (argc > 1) { |
| 2694 | p = argv[1]; |
| 2695 | if (argc > 2) { |
| 2696 | STARTSTACKSTR(concat); |
| 2697 | ap = argv + 2; |
| 2698 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2699 | concat = stputs(p, concat); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2700 | if ((p = *ap++) == NULL) |
| 2701 | break; |
| 2702 | STPUTC(' ', concat); |
| 2703 | } |
| 2704 | STPUTC('\0', concat); |
| 2705 | p = grabstackstr(concat); |
| 2706 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2707 | evalstring(p, ~SKIPEVAL); |
| 2708 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2709 | } |
| 2710 | return exitstatus; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2713 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2714 | /* |
| 2715 | * Execute a command or commands contained in a string. |
| 2716 | */ |
| 2717 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2718 | static int |
| 2719 | evalstring(char *s, int mask) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2720 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2721 | union node *n; |
| 2722 | struct stackmark smark; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2723 | int skip; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2724 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2725 | setinputstring(s); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2726 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2727 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2728 | skip = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2729 | while ((n = parsecmd(0)) != NEOF) { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 2730 | evaltree(n, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2731 | popstackmark(&smark); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2732 | skip = evalskip; |
| 2733 | if (skip) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2734 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2735 | } |
| 2736 | popfile(); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2737 | |
| 2738 | skip &= mask; |
| 2739 | evalskip = skip; |
| 2740 | return skip; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2743 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2744 | |
| 2745 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2746 | * Evaluate a parse tree. The value is left in the global variable |
| 2747 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2748 | */ |
| 2749 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2750 | static void |
| 2751 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2752 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2753 | int checkexit = 0; |
| 2754 | void (*evalfn)(union node *, int); |
| 2755 | unsigned isor; |
| 2756 | int status; |
| 2757 | if (n == NULL) { |
| 2758 | TRACE(("evaltree(NULL) called\n")); |
| 2759 | goto out; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2760 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2761 | TRACE(("pid %d, evaltree(%p: %d, %d) called\n", |
| 2762 | getpid(), n, n->type, flags)); |
| 2763 | switch (n->type) { |
| 2764 | default: |
| 2765 | #ifdef DEBUG |
| 2766 | out1fmt("Node type = %d\n", n->type); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 2767 | fflush(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2768 | break; |
| 2769 | #endif |
| 2770 | case NNOT: |
| 2771 | evaltree(n->nnot.com, EV_TESTED); |
| 2772 | status = !exitstatus; |
| 2773 | goto setstatus; |
| 2774 | case NREDIR: |
| 2775 | expredir(n->nredir.redirect); |
| 2776 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 2777 | if (!status) { |
| 2778 | evaltree(n->nredir.n, flags & EV_TESTED); |
| 2779 | status = exitstatus; |
| 2780 | } |
| 2781 | popredir(0); |
| 2782 | goto setstatus; |
| 2783 | case NCMD: |
| 2784 | evalfn = evalcommand; |
| 2785 | checkexit: |
| 2786 | if (eflag && !(flags & EV_TESTED)) |
| 2787 | checkexit = ~0; |
| 2788 | goto calleval; |
| 2789 | case NFOR: |
| 2790 | evalfn = evalfor; |
| 2791 | goto calleval; |
| 2792 | case NWHILE: |
| 2793 | case NUNTIL: |
| 2794 | evalfn = evalloop; |
| 2795 | goto calleval; |
| 2796 | case NSUBSHELL: |
| 2797 | case NBACKGND: |
| 2798 | evalfn = evalsubshell; |
| 2799 | goto calleval; |
| 2800 | case NPIPE: |
| 2801 | evalfn = evalpipe; |
| 2802 | goto checkexit; |
| 2803 | case NCASE: |
| 2804 | evalfn = evalcase; |
| 2805 | goto calleval; |
| 2806 | case NAND: |
| 2807 | case NOR: |
| 2808 | case NSEMI: |
| 2809 | #if NAND + 1 != NOR |
| 2810 | #error NAND + 1 != NOR |
| 2811 | #endif |
| 2812 | #if NOR + 1 != NSEMI |
| 2813 | #error NOR + 1 != NSEMI |
| 2814 | #endif |
| 2815 | isor = n->type - NAND; |
| 2816 | evaltree( |
| 2817 | n->nbinary.ch1, |
| 2818 | (flags | ((isor >> 1) - 1)) & EV_TESTED |
| 2819 | ); |
| 2820 | if (!exitstatus == isor) |
| 2821 | break; |
| 2822 | if (!evalskip) { |
| 2823 | n = n->nbinary.ch2; |
| 2824 | evaln: |
| 2825 | evalfn = evaltree; |
| 2826 | calleval: |
| 2827 | evalfn(n, flags); |
| 2828 | break; |
| 2829 | } |
| 2830 | break; |
| 2831 | case NIF: |
| 2832 | evaltree(n->nif.test, EV_TESTED); |
| 2833 | if (evalskip) |
| 2834 | break; |
| 2835 | if (exitstatus == 0) { |
| 2836 | n = n->nif.ifpart; |
| 2837 | goto evaln; |
| 2838 | } else if (n->nif.elsepart) { |
| 2839 | n = n->nif.elsepart; |
| 2840 | goto evaln; |
| 2841 | } |
| 2842 | goto success; |
| 2843 | case NDEFUN: |
| 2844 | defun(n->narg.text, n->narg.next); |
| 2845 | success: |
| 2846 | status = 0; |
| 2847 | setstatus: |
| 2848 | exitstatus = status; |
| 2849 | break; |
| 2850 | } |
| 2851 | out: |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2852 | if ((checkexit & exitstatus)) |
| 2853 | evalskip |= SKIPEVAL; |
| 2854 | else if (pendingsigs && dotrap()) |
| 2855 | goto exexit; |
| 2856 | |
| 2857 | if (flags & EV_EXIT) { |
| 2858 | exexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2859 | exraise(EXEXIT); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 2860 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2863 | |
| 2864 | #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3) |
| 2865 | static |
| 2866 | #endif |
| 2867 | void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__)); |
| 2868 | |
| 2869 | |
| 2870 | static void |
| 2871 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2872 | { |
| 2873 | int status; |
| 2874 | |
| 2875 | loopnest++; |
| 2876 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 2877 | flags &= EV_TESTED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2878 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2879 | int i; |
| 2880 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2881 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 2882 | if (evalskip) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2883 | skipping: if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2884 | evalskip = 0; |
| 2885 | continue; |
| 2886 | } |
| 2887 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 2888 | evalskip = 0; |
| 2889 | break; |
| 2890 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2891 | i = exitstatus; |
| 2892 | if (n->type != NWHILE) |
| 2893 | i = !i; |
| 2894 | if (i != 0) |
| 2895 | break; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 2896 | evaltree(n->nbinary.ch2, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2897 | status = exitstatus; |
| 2898 | if (evalskip) |
| 2899 | goto skipping; |
| 2900 | } |
| 2901 | loopnest--; |
| 2902 | exitstatus = status; |
| 2903 | } |
| 2904 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2905 | |
| 2906 | |
| 2907 | static void |
| 2908 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2909 | { |
| 2910 | struct arglist arglist; |
| 2911 | union node *argp; |
| 2912 | struct strlist *sp; |
| 2913 | struct stackmark smark; |
| 2914 | |
| 2915 | setstackmark(&smark); |
| 2916 | arglist.lastp = &arglist.list; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2917 | for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2918 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2919 | /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2920 | if (evalskip) |
| 2921 | goto out; |
| 2922 | } |
| 2923 | *arglist.lastp = NULL; |
| 2924 | |
| 2925 | exitstatus = 0; |
| 2926 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 2927 | flags &= EV_TESTED; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2928 | for (sp = arglist.list ; sp ; sp = sp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2929 | setvar(n->nfor.var, sp->text, 0); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 2930 | evaltree(n->nfor.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2931 | if (evalskip) { |
| 2932 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 2933 | evalskip = 0; |
| 2934 | continue; |
| 2935 | } |
| 2936 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 2937 | evalskip = 0; |
| 2938 | break; |
| 2939 | } |
| 2940 | } |
| 2941 | loopnest--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2942 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2943 | popstackmark(&smark); |
| 2944 | } |
| 2945 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2946 | |
| 2947 | |
| 2948 | static void |
| 2949 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2950 | { |
| 2951 | union node *cp; |
| 2952 | union node *patp; |
| 2953 | struct arglist arglist; |
| 2954 | struct stackmark smark; |
| 2955 | |
| 2956 | setstackmark(&smark); |
| 2957 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2958 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2959 | exitstatus = 0; |
| 2960 | for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { |
| 2961 | for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2962 | if (casematch(patp, arglist.list->text)) { |
| 2963 | if (evalskip == 0) { |
| 2964 | evaltree(cp->nclist.body, flags); |
| 2965 | } |
| 2966 | goto out; |
| 2967 | } |
| 2968 | } |
| 2969 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2970 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 2971 | popstackmark(&smark); |
| 2972 | } |
| 2973 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2974 | |
| 2975 | |
| 2976 | /* |
| 2977 | * Kick off a subshell to evaluate a tree. |
| 2978 | */ |
| 2979 | |
| 2980 | static void |
| 2981 | evalsubshell(union node *n, int flags) |
| 2982 | { |
| 2983 | struct job *jp; |
| 2984 | int backgnd = (n->type == NBACKGND); |
| 2985 | int status; |
| 2986 | |
| 2987 | expredir(n->nredir.redirect); |
| 2988 | if (!backgnd && flags & EV_EXIT && !trap[0]) |
| 2989 | goto nofork; |
| 2990 | INTOFF; |
| 2991 | jp = makejob(n, 1); |
| 2992 | if (forkshell(jp, n, backgnd) == 0) { |
| 2993 | INTON; |
| 2994 | flags |= EV_EXIT; |
| 2995 | if (backgnd) |
| 2996 | flags &=~ EV_TESTED; |
| 2997 | nofork: |
| 2998 | redirect(n->nredir.redirect, 0); |
| 2999 | evaltreenr(n->nredir.n, flags); |
| 3000 | /* never returns */ |
| 3001 | } |
| 3002 | status = 0; |
| 3003 | if (! backgnd) |
| 3004 | status = waitforjob(jp); |
| 3005 | exitstatus = status; |
| 3006 | INTON; |
| 3007 | } |
| 3008 | |
| 3009 | |
| 3010 | |
| 3011 | /* |
| 3012 | * Compute the names of the files in a redirection list. |
| 3013 | */ |
| 3014 | |
| 3015 | static void |
| 3016 | expredir(union node *n) |
| 3017 | { |
| 3018 | union node *redir; |
| 3019 | |
| 3020 | for (redir = n ; redir ; redir = redir->nfile.next) { |
| 3021 | struct arglist fn; |
| 3022 | fn.lastp = &fn.list; |
| 3023 | switch (redir->type) { |
| 3024 | case NFROMTO: |
| 3025 | case NFROM: |
| 3026 | case NTO: |
| 3027 | case NCLOBBER: |
| 3028 | case NAPPEND: |
| 3029 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
| 3030 | redir->nfile.expfname = fn.list->text; |
| 3031 | break; |
| 3032 | case NFROMFD: |
| 3033 | case NTOFD: |
| 3034 | if (redir->ndup.vname) { |
| 3035 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
| 3036 | fixredir(redir, fn.list->text, 1); |
| 3037 | } |
| 3038 | break; |
| 3039 | } |
| 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | |
| 3044 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3045 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3046 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 3047 | * of the process creating the pipeline. (This differs from some versions |
| 3048 | * of the shell, which make the last process in a pipeline the parent |
| 3049 | * of all the rest.) |
| 3050 | */ |
| 3051 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3052 | static void |
| 3053 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3054 | { |
| 3055 | struct job *jp; |
| 3056 | struct nodelist *lp; |
| 3057 | int pipelen; |
| 3058 | int prevfd; |
| 3059 | int pip[2]; |
| 3060 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3061 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3062 | pipelen = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3063 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3064 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3065 | flags |= EV_EXIT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3066 | INTOFF; |
| 3067 | jp = makejob(n, pipelen); |
| 3068 | prevfd = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3069 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3070 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3071 | pip[1] = -1; |
| 3072 | if (lp->next) { |
| 3073 | if (pipe(pip) < 0) { |
| 3074 | close(prevfd); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3075 | sh_error("Pipe call failed"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3076 | } |
| 3077 | } |
| 3078 | if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { |
| 3079 | INTON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3080 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3081 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3082 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3083 | if (prevfd > 0) { |
| 3084 | dup2(prevfd, 0); |
| 3085 | close(prevfd); |
| 3086 | } |
| 3087 | if (pip[1] > 1) { |
| 3088 | dup2(pip[1], 1); |
| 3089 | close(pip[1]); |
| 3090 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3091 | evaltreenr(lp->n, flags); |
| 3092 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3093 | } |
| 3094 | if (prevfd >= 0) |
| 3095 | close(prevfd); |
| 3096 | prevfd = pip[0]; |
| 3097 | close(pip[1]); |
| 3098 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3099 | if (n->npipe.backgnd == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3100 | exitstatus = waitforjob(jp); |
| 3101 | TRACE(("evalpipe: job done exit status %d\n", exitstatus)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3102 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3103 | INTON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3106 | |
| 3107 | |
| 3108 | /* |
| 3109 | * Execute a command inside back quotes. If it's a builtin command, we |
| 3110 | * want to save its output in a block obtained from malloc. Otherwise |
| 3111 | * we fork off a subprocess and get the output of the command via a pipe. |
| 3112 | * Should be called with interrupts off. |
| 3113 | */ |
| 3114 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3115 | static void |
| 3116 | evalbackcmd(union node *n, struct backcmd *result) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3117 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3118 | int saveherefd; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3119 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3120 | result->fd = -1; |
| 3121 | result->buf = NULL; |
| 3122 | result->nleft = 0; |
| 3123 | result->jp = NULL; |
| 3124 | if (n == NULL) { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3125 | goto out; |
| 3126 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3127 | |
| 3128 | saveherefd = herefd; |
| 3129 | herefd = -1; |
| 3130 | |
| 3131 | { |
| 3132 | int pip[2]; |
| 3133 | struct job *jp; |
| 3134 | |
| 3135 | if (pipe(pip) < 0) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3136 | sh_error("Pipe call failed"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3137 | jp = makejob(n, 1); |
| 3138 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
| 3139 | FORCEINTON; |
| 3140 | close(pip[0]); |
| 3141 | if (pip[1] != 1) { |
| 3142 | close(1); |
| 3143 | copyfd(pip[1], 1); |
| 3144 | close(pip[1]); |
| 3145 | } |
| 3146 | eflag = 0; |
| 3147 | evaltreenr(n, EV_EXIT); |
| 3148 | /* NOTREACHED */ |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3149 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3150 | close(pip[1]); |
| 3151 | result->fd = pip[0]; |
| 3152 | result->jp = jp; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3153 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3154 | herefd = saveherefd; |
| 3155 | out: |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3156 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3157 | result->fd, result->buf, result->nleft, result->jp)); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3160 | #ifdef CONFIG_ASH_CMDCMD |
| 3161 | static inline char ** |
| 3162 | parse_command_args(char **argv, const char **path) |
| 3163 | { |
| 3164 | char *cp, c; |
| 3165 | |
| 3166 | for (;;) { |
| 3167 | cp = *++argv; |
| 3168 | if (!cp) |
| 3169 | return 0; |
| 3170 | if (*cp++ != '-') |
| 3171 | break; |
| 3172 | if (!(c = *cp++)) |
| 3173 | break; |
| 3174 | if (c == '-' && !*cp) { |
| 3175 | argv++; |
| 3176 | break; |
| 3177 | } |
| 3178 | do { |
| 3179 | switch (c) { |
| 3180 | case 'p': |
| 3181 | *path = defpath; |
| 3182 | break; |
| 3183 | default: |
| 3184 | /* run 'typecmd' for other options */ |
| 3185 | return 0; |
| 3186 | } |
| 3187 | } while ((c = *cp++)); |
| 3188 | } |
| 3189 | return argv; |
| 3190 | } |
| 3191 | #endif |
| 3192 | |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 3193 | static inline int |
| 3194 | isassignment(const char *p) |
| 3195 | { |
| 3196 | const char *q = endofname(p); |
| 3197 | if (p == q) |
| 3198 | return 0; |
| 3199 | return *q == '='; |
| 3200 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3201 | |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 3202 | #ifdef CONFIG_ASH_EXPAND_PRMT |
| 3203 | static const char *expandstr(const char *ps); |
| 3204 | #else |
| 3205 | #define expandstr(s) s |
| 3206 | #endif |
| 3207 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3208 | /* |
| 3209 | * Execute a simple command. |
| 3210 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3211 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3212 | static void |
| 3213 | evalcommand(union node *cmd, int flags) |
| 3214 | { |
| 3215 | struct stackmark smark; |
| 3216 | union node *argp; |
| 3217 | struct arglist arglist; |
| 3218 | struct arglist varlist; |
| 3219 | char **argv; |
| 3220 | int argc; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3221 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3222 | struct cmdentry cmdentry; |
| 3223 | struct job *jp; |
| 3224 | char *lastarg; |
| 3225 | const char *path; |
| 3226 | int spclbltin; |
| 3227 | int cmd_is_exec; |
| 3228 | int status; |
| 3229 | char **nargv; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 3230 | struct builtincmd *bcmd; |
| 3231 | int pseudovarflag = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3232 | |
| 3233 | /* First expand the arguments. */ |
| 3234 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 3235 | setstackmark(&smark); |
| 3236 | back_exitstatus = 0; |
| 3237 | |
| 3238 | cmdentry.cmdtype = CMDBUILTIN; |
| 3239 | cmdentry.u.cmd = &bltin; |
| 3240 | varlist.lastp = &varlist.list; |
| 3241 | *varlist.lastp = NULL; |
| 3242 | arglist.lastp = &arglist.list; |
| 3243 | *arglist.lastp = NULL; |
| 3244 | |
| 3245 | argc = 0; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 3246 | if (cmd->ncmd.args) |
| 3247 | { |
| 3248 | bcmd = find_builtin(cmd->ncmd.args->narg.text); |
| 3249 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
| 3250 | } |
| 3251 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3252 | for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) { |
| 3253 | struct strlist **spp; |
| 3254 | |
| 3255 | spp = arglist.lastp; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 3256 | if (pseudovarflag && isassignment(argp->narg.text)) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 3257 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 3258 | else |
| 3259 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 3260 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3261 | for (sp = *spp; sp; sp = sp->next) |
| 3262 | argc++; |
| 3263 | } |
| 3264 | |
| 3265 | argv = nargv = stalloc(sizeof (char *) * (argc + 1)); |
| 3266 | for (sp = arglist.list ; sp ; sp = sp->next) { |
| 3267 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 3268 | *nargv++ = sp->text; |
| 3269 | } |
| 3270 | *nargv = NULL; |
| 3271 | |
| 3272 | lastarg = NULL; |
| 3273 | if (iflag && funcnest == 0 && argc > 0) |
| 3274 | lastarg = nargv[-1]; |
| 3275 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3276 | preverrout_fd = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3277 | expredir(cmd->ncmd.redirect); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3278 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH|REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3279 | |
| 3280 | path = vpath.text; |
| 3281 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 3282 | struct strlist **spp; |
| 3283 | char *p; |
| 3284 | |
| 3285 | spp = varlist.lastp; |
| 3286 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 3287 | |
| 3288 | /* |
| 3289 | * Modify the command lookup path, if a PATH= assignment |
| 3290 | * is present |
| 3291 | */ |
| 3292 | p = (*spp)->text; |
| 3293 | if (varequal(p, path)) |
| 3294 | path = p; |
| 3295 | } |
| 3296 | |
| 3297 | /* Print the command if xflag is set. */ |
| 3298 | if (xflag) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3299 | int n; |
| 3300 | const char *p = " %s"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3301 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3302 | p++; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 3303 | dprintf(preverrout_fd, p, expandstr(ps4val())); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3304 | |
| 3305 | sp = varlist.list; |
| 3306 | for(n = 0; n < 2; n++) { |
| 3307 | while (sp) { |
| 3308 | dprintf(preverrout_fd, p, sp->text); |
| 3309 | sp = sp->next; |
| 3310 | if(*p == '%') { |
| 3311 | p--; |
| 3312 | } |
| 3313 | } |
| 3314 | sp = arglist.list; |
| 3315 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 3316 | bb_full_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | cmd_is_exec = 0; |
| 3320 | spclbltin = -1; |
| 3321 | |
| 3322 | /* Now locate the command. */ |
| 3323 | if (argc) { |
| 3324 | const char *oldpath; |
| 3325 | int cmd_flag = DO_ERR; |
| 3326 | |
| 3327 | path += 5; |
| 3328 | oldpath = path; |
| 3329 | for (;;) { |
| 3330 | find_command(argv[0], &cmdentry, cmd_flag, path); |
| 3331 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
| 3332 | status = 127; |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 3333 | flusherr(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3334 | goto bail; |
| 3335 | } |
| 3336 | |
| 3337 | /* implement bltin and command here */ |
| 3338 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 3339 | break; |
| 3340 | if (spclbltin < 0) |
| 3341 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
| 3342 | if (cmdentry.u.cmd == EXECCMD) |
| 3343 | cmd_is_exec++; |
| 3344 | #ifdef CONFIG_ASH_CMDCMD |
| 3345 | if (cmdentry.u.cmd == COMMANDCMD) { |
| 3346 | |
| 3347 | path = oldpath; |
| 3348 | nargv = parse_command_args(argv, &path); |
| 3349 | if (!nargv) |
| 3350 | break; |
| 3351 | argc -= nargv - argv; |
| 3352 | argv = nargv; |
| 3353 | cmd_flag |= DO_NOFUNC; |
| 3354 | } else |
| 3355 | #endif |
| 3356 | break; |
| 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | if (status) { |
| 3361 | /* We have a redirection error. */ |
| 3362 | if (spclbltin > 0) |
| 3363 | exraise(EXERROR); |
| 3364 | bail: |
| 3365 | exitstatus = status; |
| 3366 | goto out; |
| 3367 | } |
| 3368 | |
| 3369 | /* Execute the command. */ |
| 3370 | switch (cmdentry.cmdtype) { |
| 3371 | default: |
| 3372 | /* Fork off a child process if necessary. */ |
| 3373 | if (!(flags & EV_EXIT) || trap[0]) { |
| 3374 | INTOFF; |
| 3375 | jp = makejob(cmd, 1); |
| 3376 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
| 3377 | exitstatus = waitforjob(jp); |
| 3378 | INTON; |
| 3379 | break; |
| 3380 | } |
| 3381 | FORCEINTON; |
| 3382 | } |
| 3383 | listsetvar(varlist.list, VEXPORT|VSTACK); |
| 3384 | shellexec(argv, path, cmdentry.u.index); |
| 3385 | /* NOTREACHED */ |
| 3386 | |
| 3387 | case CMDBUILTIN: |
| 3388 | cmdenviron = varlist.list; |
| 3389 | if (cmdenviron) { |
| 3390 | struct strlist *list = cmdenviron; |
| 3391 | int i = VNOSET; |
| 3392 | if (spclbltin > 0 || argc == 0) { |
| 3393 | i = 0; |
| 3394 | if (cmd_is_exec && argc > 1) |
| 3395 | i = VEXPORT; |
| 3396 | } |
| 3397 | listsetvar(list, i); |
| 3398 | } |
| 3399 | if (evalbltin(cmdentry.u.cmd, argc, argv)) { |
| 3400 | int exit_status; |
| 3401 | int i, j; |
| 3402 | |
| 3403 | i = exception; |
| 3404 | if (i == EXEXIT) |
| 3405 | goto raise; |
| 3406 | |
| 3407 | exit_status = 2; |
| 3408 | j = 0; |
| 3409 | if (i == EXINT) |
| 3410 | j = SIGINT; |
| 3411 | if (i == EXSIG) |
| 3412 | j = pendingsigs; |
| 3413 | if (j) |
| 3414 | exit_status = j + 128; |
| 3415 | exitstatus = exit_status; |
| 3416 | |
| 3417 | if (i == EXINT || spclbltin > 0) { |
| 3418 | raise: |
| 3419 | longjmp(handler->loc, 1); |
| 3420 | } |
| 3421 | FORCEINTON; |
| 3422 | } |
| 3423 | break; |
| 3424 | |
| 3425 | case CMDFUNCTION: |
| 3426 | listsetvar(varlist.list, 0); |
| 3427 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 3428 | goto raise; |
| 3429 | break; |
| 3430 | } |
| 3431 | |
| 3432 | out: |
| 3433 | popredir(cmd_is_exec); |
| 3434 | if (lastarg) |
| 3435 | /* dsl: I think this is intended to be used to support |
| 3436 | * '_' in 'vi' command mode during line editing... |
| 3437 | * However I implemented that within libedit itself. |
| 3438 | */ |
| 3439 | setvar("_", lastarg, 0); |
| 3440 | popstackmark(&smark); |
| 3441 | } |
| 3442 | |
| 3443 | static int |
| 3444 | evalbltin(const struct builtincmd *cmd, int argc, char **argv) { |
| 3445 | char *volatile savecmdname; |
| 3446 | struct jmploc *volatile savehandler; |
| 3447 | struct jmploc jmploc; |
| 3448 | int i; |
| 3449 | |
| 3450 | savecmdname = commandname; |
| 3451 | if ((i = setjmp(jmploc.loc))) |
| 3452 | goto cmddone; |
| 3453 | savehandler = handler; |
| 3454 | handler = &jmploc; |
| 3455 | commandname = argv[0]; |
| 3456 | argptr = argv + 1; |
| 3457 | optptr = NULL; /* initialize nextopt */ |
| 3458 | exitstatus = (*cmd->builtin)(argc, argv); |
| 3459 | flushall(); |
| 3460 | cmddone: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3461 | exitstatus |= ferror(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3462 | commandname = savecmdname; |
| 3463 | exsig = 0; |
| 3464 | handler = savehandler; |
| 3465 | |
| 3466 | return i; |
| 3467 | } |
| 3468 | |
| 3469 | static int |
| 3470 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 3471 | { |
| 3472 | volatile struct shparam saveparam; |
| 3473 | struct localvar *volatile savelocalvars; |
| 3474 | struct jmploc *volatile savehandler; |
| 3475 | struct jmploc jmploc; |
| 3476 | int e; |
| 3477 | |
| 3478 | saveparam = shellparam; |
| 3479 | savelocalvars = localvars; |
| 3480 | if ((e = setjmp(jmploc.loc))) { |
| 3481 | goto funcdone; |
| 3482 | } |
| 3483 | INTOFF; |
| 3484 | savehandler = handler; |
| 3485 | handler = &jmploc; |
| 3486 | localvars = NULL; |
| 3487 | shellparam.malloc = 0; |
| 3488 | func->count++; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3489 | funcnest++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3490 | INTON; |
| 3491 | shellparam.nparam = argc - 1; |
| 3492 | shellparam.p = argv + 1; |
| 3493 | #ifdef CONFIG_ASH_GETOPTS |
| 3494 | shellparam.optind = 1; |
| 3495 | shellparam.optoff = -1; |
| 3496 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3497 | evaltree(&func->n, flags & EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3498 | funcdone: |
| 3499 | INTOFF; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3500 | funcnest--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3501 | freefunc(func); |
| 3502 | poplocalvars(); |
| 3503 | localvars = savelocalvars; |
| 3504 | freeparam(&shellparam); |
| 3505 | shellparam = saveparam; |
| 3506 | handler = savehandler; |
| 3507 | INTON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3508 | evalskip &= ~SKIPFUNC; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3509 | return e; |
| 3510 | } |
| 3511 | |
| 3512 | |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 3513 | static inline int |
| 3514 | goodname(const char *p) |
| 3515 | { |
| 3516 | return !*endofname(p); |
| 3517 | } |
| 3518 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3519 | /* |
| 3520 | * Search for a command. This is called before we fork so that the |
| 3521 | * location of the command will be available in the parent as well as |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 3522 | * the child. The check for "goodname" is an overly conservative |
| 3523 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3524 | */ |
| 3525 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3526 | static void |
| 3527 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3528 | { |
| 3529 | struct cmdentry entry; |
| 3530 | |
| 3531 | if (n->type == NCMD && n->ncmd.args) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 3532 | if (goodname(n->ncmd.args->narg.text)) |
| 3533 | find_command(n->ncmd.args->narg.text, &entry, 0, |
| 3534 | pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3535 | } |
| 3536 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3537 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3538 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3539 | /* |
| 3540 | * Builtin commands. Builtin commands whose functions are closely |
| 3541 | * tied to evaluation are implemented here. |
| 3542 | */ |
| 3543 | |
| 3544 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3545 | * No command given. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3546 | */ |
| 3547 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3548 | static int |
| 3549 | bltincmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3550 | { |
| 3551 | /* |
| 3552 | * Preserve exitstatus of a previous possible redirection |
| 3553 | * as POSIX mandates |
| 3554 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3555 | return back_exitstatus; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | |
| 3559 | /* |
| 3560 | * Handle break and continue commands. Break, continue, and return are |
| 3561 | * all handled by setting the evalskip flag. The evaluation routines |
| 3562 | * above all check this flag, and if it is set they start skipping |
| 3563 | * commands rather than executing them. The variable skipcount is |
| 3564 | * the number of loops to break/continue, or the number of function |
| 3565 | * levels to return. (The latter is always 1.) It should probably |
| 3566 | * be an error to break out of more loops than exist, but it isn't |
| 3567 | * in the standard shell so we don't make it one here. |
| 3568 | */ |
| 3569 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3570 | static int |
| 3571 | breakcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3572 | { |
| 3573 | int n = argc > 1 ? number(argv[1]) : 1; |
| 3574 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 3575 | if (n <= 0) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3576 | sh_error(illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3577 | if (n > loopnest) |
| 3578 | n = loopnest; |
| 3579 | if (n > 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3580 | evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3581 | skipcount = n; |
| 3582 | } |
| 3583 | return 0; |
| 3584 | } |
| 3585 | |
| 3586 | |
| 3587 | /* |
| 3588 | * The return command. |
| 3589 | */ |
| 3590 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3591 | static int |
| 3592 | returncmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3593 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3594 | /* |
| 3595 | * If called outside a function, do what ksh does; |
| 3596 | * skip the rest of the file. |
| 3597 | */ |
| 3598 | evalskip = funcnest ? SKIPFUNC : SKIPFILE; |
| 3599 | return argv[1] ? number(argv[1]) : exitstatus; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3603 | static int |
| 3604 | falsecmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3605 | { |
| 3606 | return 1; |
| 3607 | } |
| 3608 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3609 | |
| 3610 | static int |
| 3611 | truecmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3612 | { |
| 3613 | return 0; |
| 3614 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3615 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3616 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3617 | static int |
| 3618 | execcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3619 | { |
| 3620 | if (argc > 1) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3621 | iflag = 0; /* exit on error */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3622 | mflag = 0; |
| 3623 | optschanged(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3624 | shellexec(argv + 1, pathval(), 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3625 | } |
| 3626 | return 0; |
| 3627 | } |
| 3628 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3629 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3630 | /* exec.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3631 | |
| 3632 | /* |
| 3633 | * When commands are first encountered, they are entered in a hash table. |
| 3634 | * This ensures that a full path search will not have to be done for them |
| 3635 | * on each invocation. |
| 3636 | * |
| 3637 | * We should investigate converting to a linear search, even though that |
| 3638 | * would make the command name "hash" a misnomer. |
| 3639 | */ |
| 3640 | |
| 3641 | #define CMDTABLESIZE 31 /* should be prime */ |
| 3642 | #define ARB 1 /* actual size determined at run time */ |
| 3643 | |
| 3644 | |
| 3645 | |
| 3646 | struct tblentry { |
| 3647 | struct tblentry *next; /* next entry in hash chain */ |
| 3648 | union param param; /* definition of builtin function */ |
| 3649 | short cmdtype; /* index identifying command */ |
| 3650 | char rehash; /* if set, cd done since entry created */ |
| 3651 | char cmdname[ARB]; /* name of command */ |
| 3652 | }; |
| 3653 | |
| 3654 | |
| 3655 | static struct tblentry *cmdtable[CMDTABLESIZE]; |
| 3656 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 3657 | |
| 3658 | |
| 3659 | static void tryexec(char *, char **, char **); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3660 | static void clearcmdentry(int); |
| 3661 | static struct tblentry *cmdlookup(const char *, int); |
| 3662 | static void delete_cmd_entry(void); |
| 3663 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3664 | |
| 3665 | /* |
| 3666 | * Exec a program. Never returns. If you change this routine, you may |
| 3667 | * have to change the find_command routine as well. |
| 3668 | */ |
| 3669 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3670 | static void |
| 3671 | shellexec(char **argv, const char *path, int idx) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3672 | { |
| 3673 | char *cmdname; |
| 3674 | int e; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3675 | char **envp; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3676 | int exerrno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3677 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3678 | clearredir(1); |
| 3679 | envp = environment(); |
Eric Andersen | bf8bf10 | 2002-09-17 08:41:08 +0000 | [diff] [blame] | 3680 | if (strchr(argv[0], '/') != NULL |
| 3681 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
| 3682 | || find_applet_by_name(argv[0]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3683 | #endif |
| 3684 | ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3685 | tryexec(argv[0], argv, envp); |
| 3686 | e = errno; |
| 3687 | } else { |
| 3688 | e = ENOENT; |
| 3689 | while ((cmdname = padvance(&path, argv[0])) != NULL) { |
| 3690 | if (--idx < 0 && pathopt == NULL) { |
| 3691 | tryexec(cmdname, argv, envp); |
| 3692 | if (errno != ENOENT && errno != ENOTDIR) |
| 3693 | e = errno; |
| 3694 | } |
| 3695 | stunalloc(cmdname); |
| 3696 | } |
| 3697 | } |
| 3698 | |
| 3699 | /* Map to POSIX errors */ |
| 3700 | switch (e) { |
| 3701 | case EACCES: |
| 3702 | exerrno = 126; |
| 3703 | break; |
| 3704 | case ENOENT: |
| 3705 | exerrno = 127; |
| 3706 | break; |
| 3707 | default: |
| 3708 | exerrno = 2; |
| 3709 | break; |
| 3710 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 3711 | exitstatus = exerrno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3712 | TRACE(("shellexec failed for %s, errno %d, suppressint %d\n", |
| 3713 | argv[0], e, suppressint )); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3714 | exerror(EXEXEC, "%s: %s", argv[0], errmsg(e, E_EXEC)); |
| 3715 | /* NOTREACHED */ |
| 3716 | } |
| 3717 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3718 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3719 | static void |
| 3720 | tryexec(char *cmd, char **argv, char **envp) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 3721 | { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3722 | int repeated = 0; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3723 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
Rob Landley | 0fcd943 | 2005-05-07 08:27:34 +0000 | [diff] [blame] | 3724 | if(find_applet_by_name(cmd) != NULL) { |
| 3725 | /* re-exec ourselves with the new arguments */ |
| 3726 | execve("/proc/self/exe",argv,envp); |
| 3727 | /* If proc isn't mounted, try hardcoded path to busybox binary*/ |
| 3728 | execve("/bin/busybox",argv,envp); |
| 3729 | /* If they called chroot or otherwise made the binary no longer |
| 3730 | * executable, fall through */ |
Glenn L McGrath | c00c56e | 2002-12-23 10:23:10 +0000 | [diff] [blame] | 3731 | } |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 3732 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3733 | |
| 3734 | repeat: |
| 3735 | #ifdef SYSV |
| 3736 | do { |
| 3737 | execve(cmd, argv, envp); |
| 3738 | } while (errno == EINTR); |
| 3739 | #else |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3740 | execve(cmd, argv, envp); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3741 | #endif |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3742 | if (repeated++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3743 | ckfree(argv); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3744 | } else if (errno == ENOEXEC) { |
| 3745 | char **ap; |
| 3746 | char **new; |
| 3747 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3748 | for (ap = argv; *ap; ap++) |
| 3749 | ; |
| 3750 | ap = new = ckmalloc((ap - argv + 2) * sizeof(char *)); |
Glenn L McGrath | 47e5ca1 | 2003-09-15 12:00:19 +0000 | [diff] [blame] | 3751 | ap[1] = cmd; |
| 3752 | *ap = cmd = (char *)DEFAULT_SHELL; |
| 3753 | ap += 2; |
| 3754 | argv++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3755 | while ((*ap++ = *argv++)) |
| 3756 | ; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 3757 | argv = new; |
| 3758 | goto repeat; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3759 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3760 | } |
| 3761 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3762 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3763 | |
| 3764 | /* |
| 3765 | * Do a path search. The variable path (passed by reference) should be |
| 3766 | * set to the start of the path before the first call; padvance will update |
| 3767 | * this value as it proceeds. Successive calls to padvance will return |
| 3768 | * the possible path expansions in sequence. If an option (indicated by |
| 3769 | * a percent sign) appears in the path entry then the global variable |
| 3770 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 3771 | * NULL. |
| 3772 | */ |
| 3773 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3774 | static char * |
| 3775 | padvance(const char **path, const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3776 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3777 | const char *p; |
| 3778 | char *q; |
| 3779 | const char *start; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3780 | size_t len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3781 | |
| 3782 | if (*path == NULL) |
| 3783 | return NULL; |
| 3784 | start = *path; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3785 | for (p = start ; *p && *p != ':' && *p != '%' ; p++); |
| 3786 | len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3787 | while (stackblocksize() < len) |
| 3788 | growstackblock(); |
| 3789 | q = stackblock(); |
| 3790 | if (p != start) { |
| 3791 | memcpy(q, start, p - start); |
| 3792 | q += p - start; |
| 3793 | *q++ = '/'; |
| 3794 | } |
| 3795 | strcpy(q, name); |
| 3796 | pathopt = NULL; |
| 3797 | if (*p == '%') { |
| 3798 | pathopt = ++p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3799 | while (*p && *p != ':') p++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3800 | } |
| 3801 | if (*p == ':') |
| 3802 | *path = p + 1; |
| 3803 | else |
| 3804 | *path = NULL; |
| 3805 | return stalloc(len); |
| 3806 | } |
| 3807 | |
| 3808 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3809 | /*** Command hashing code ***/ |
| 3810 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 3811 | static void |
| 3812 | printentry(struct tblentry *cmdp) |
| 3813 | { |
| 3814 | int idx; |
| 3815 | const char *path; |
| 3816 | char *name; |
| 3817 | |
| 3818 | idx = cmdp->param.index; |
| 3819 | path = pathval(); |
| 3820 | do { |
| 3821 | name = padvance(&path, cmdp->cmdname); |
| 3822 | stunalloc(name); |
| 3823 | } while (--idx >= 0); |
| 3824 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 3825 | } |
| 3826 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3827 | |
| 3828 | static int |
| 3829 | hashcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3830 | { |
| 3831 | struct tblentry **pp; |
| 3832 | struct tblentry *cmdp; |
| 3833 | int c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3834 | struct cmdentry entry; |
| 3835 | char *name; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3836 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3837 | while ((c = nextopt("r")) != '\0') { |
| 3838 | clearcmdentry(0); |
| 3839 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3840 | } |
| 3841 | if (*argptr == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3842 | for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) { |
| 3843 | for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { |
| 3844 | if (cmdp->cmdtype == CMDNORMAL) |
| 3845 | printentry(cmdp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3846 | } |
| 3847 | } |
| 3848 | return 0; |
| 3849 | } |
| 3850 | c = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3851 | while ((name = *argptr) != NULL) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3852 | if ((cmdp = cmdlookup(name, 0)) != NULL |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3853 | && (cmdp->cmdtype == CMDNORMAL |
| 3854 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3855 | delete_cmd_entry(); |
| 3856 | find_command(name, &entry, DO_ERR, pathval()); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3857 | if (entry.cmdtype == CMDUNKNOWN) |
| 3858 | c = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3859 | argptr++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3860 | } |
| 3861 | return c; |
| 3862 | } |
| 3863 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3864 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3865 | /* |
| 3866 | * Resolve a command name. If you change this routine, you may have to |
| 3867 | * change the shellexec routine as well. |
| 3868 | */ |
| 3869 | |
| 3870 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3871 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3872 | { |
| 3873 | struct tblentry *cmdp; |
| 3874 | int idx; |
| 3875 | int prev; |
| 3876 | char *fullname; |
| 3877 | struct stat statb; |
| 3878 | int e; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3879 | int updatetbl; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3880 | struct builtincmd *bcmd; |
| 3881 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3882 | /* If name contains a slash, don't use PATH or hash table */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3883 | if (strchr(name, '/') != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3884 | entry->u.index = -1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3885 | if (act & DO_ABS) { |
| 3886 | while (stat(name, &statb) < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3887 | #ifdef SYSV |
| 3888 | if (errno == EINTR) |
| 3889 | continue; |
| 3890 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3891 | entry->cmdtype = CMDUNKNOWN; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3892 | return; |
| 3893 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3894 | } |
| 3895 | entry->cmdtype = CMDNORMAL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3896 | return; |
| 3897 | } |
| 3898 | |
Eric Andersen | bf8bf10 | 2002-09-17 08:41:08 +0000 | [diff] [blame] | 3899 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
| 3900 | if (find_applet_by_name(name)) { |
| 3901 | entry->cmdtype = CMDNORMAL; |
| 3902 | entry->u.index = -1; |
| 3903 | return; |
| 3904 | } |
| 3905 | #endif |
| 3906 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3907 | updatetbl = (path == pathval()); |
| 3908 | if (!updatetbl) { |
| 3909 | act |= DO_ALTPATH; |
| 3910 | if (strstr(path, "%builtin") != NULL) |
| 3911 | act |= DO_ALTBLTIN; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3914 | /* If name is in the table, check answer will be ok */ |
| 3915 | if ((cmdp = cmdlookup(name, 0)) != NULL) { |
| 3916 | int bit; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3917 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3918 | switch (cmdp->cmdtype) { |
| 3919 | default: |
| 3920 | #if DEBUG |
| 3921 | abort(); |
| 3922 | #endif |
| 3923 | case CMDNORMAL: |
| 3924 | bit = DO_ALTPATH; |
| 3925 | break; |
| 3926 | case CMDFUNCTION: |
| 3927 | bit = DO_NOFUNC; |
| 3928 | break; |
| 3929 | case CMDBUILTIN: |
| 3930 | bit = DO_ALTBLTIN; |
| 3931 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3932 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3933 | if (act & bit) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3934 | updatetbl = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3935 | cmdp = NULL; |
| 3936 | } else if (cmdp->rehash == 0) |
| 3937 | /* if not invalidated by cd, we're done */ |
| 3938 | goto success; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | /* If %builtin not in path, check for builtin next */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3942 | bcmd = find_builtin(name); |
| 3943 | if (bcmd && (IS_BUILTIN_REGULAR(bcmd) || ( |
| 3944 | act & DO_ALTPATH ? !(act & DO_ALTBLTIN) : builtinloc <= 0 |
| 3945 | ))) |
| 3946 | goto builtin_success; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3947 | |
| 3948 | /* We have to search path. */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3949 | prev = -1; /* where to start */ |
| 3950 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3951 | if (cmdp->cmdtype == CMDBUILTIN) |
| 3952 | prev = builtinloc; |
| 3953 | else |
| 3954 | prev = cmdp->param.index; |
| 3955 | } |
| 3956 | |
| 3957 | e = ENOENT; |
| 3958 | idx = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3959 | loop: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3960 | while ((fullname = padvance(&path, name)) != NULL) { |
| 3961 | stunalloc(fullname); |
| 3962 | idx++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3963 | if (pathopt) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3964 | if (prefix(pathopt, "builtin")) { |
| 3965 | if (bcmd) |
| 3966 | goto builtin_success; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3967 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3968 | } else if (!(act & DO_NOFUNC) && |
| 3969 | prefix(pathopt, "func")) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3970 | /* handled below */ |
| 3971 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3972 | /* ignore unimplemented options */ |
| 3973 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3974 | } |
| 3975 | } |
| 3976 | /* if rehash, don't redo absolute path names */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3977 | if (fullname[0] == '/' && idx <= prev) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3978 | if (idx < prev) |
| 3979 | continue; |
| 3980 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 3981 | goto success; |
| 3982 | } |
| 3983 | while (stat(fullname, &statb) < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3984 | #ifdef SYSV |
| 3985 | if (errno == EINTR) |
| 3986 | continue; |
| 3987 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3988 | if (errno != ENOENT && errno != ENOTDIR) |
| 3989 | e = errno; |
| 3990 | goto loop; |
| 3991 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3992 | e = EACCES; /* if we fail, this will be the error */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3993 | if (!S_ISREG(statb.st_mode)) |
| 3994 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3995 | if (pathopt) { /* this is a %func directory */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3996 | stalloc(strlen(fullname) + 1); |
| 3997 | readcmdfile(fullname); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3998 | if ((cmdp = cmdlookup(name, 0)) == NULL || |
| 3999 | cmdp->cmdtype != CMDFUNCTION) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4000 | sh_error("%s not defined in %s", name, fullname); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4001 | stunalloc(fullname); |
| 4002 | goto success; |
| 4003 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4004 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4005 | if (!updatetbl) { |
| 4006 | entry->cmdtype = CMDNORMAL; |
| 4007 | entry->u.index = idx; |
| 4008 | return; |
| 4009 | } |
| 4010 | INTOFF; |
| 4011 | cmdp = cmdlookup(name, 1); |
| 4012 | cmdp->cmdtype = CMDNORMAL; |
| 4013 | cmdp->param.index = idx; |
| 4014 | INTON; |
| 4015 | goto success; |
| 4016 | } |
| 4017 | |
| 4018 | /* We failed. If there was an entry for this command, delete it */ |
| 4019 | if (cmdp && updatetbl) |
| 4020 | delete_cmd_entry(); |
| 4021 | if (act & DO_ERR) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4022 | sh_warnx("%s: %s", name, errmsg(e, E_EXEC)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4023 | entry->cmdtype = CMDUNKNOWN; |
| 4024 | return; |
| 4025 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4026 | builtin_success: |
| 4027 | if (!updatetbl) { |
| 4028 | entry->cmdtype = CMDBUILTIN; |
| 4029 | entry->u.cmd = bcmd; |
| 4030 | return; |
| 4031 | } |
| 4032 | INTOFF; |
| 4033 | cmdp = cmdlookup(name, 1); |
| 4034 | cmdp->cmdtype = CMDBUILTIN; |
| 4035 | cmdp->param.cmd = bcmd; |
| 4036 | INTON; |
| 4037 | success: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4038 | cmdp->rehash = 0; |
| 4039 | entry->cmdtype = cmdp->cmdtype; |
| 4040 | entry->u = cmdp->param; |
| 4041 | } |
| 4042 | |
| 4043 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4044 | /* |
| 4045 | * Wrapper around strcmp for qsort/bsearch/... |
| 4046 | */ |
| 4047 | static int pstrcmp(const void *a, const void *b) |
| 4048 | { |
| 4049 | return strcmp((const char *) a, (*(const char *const *) b) + 1); |
| 4050 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4051 | |
| 4052 | /* |
| 4053 | * Search the table of builtin commands. |
| 4054 | */ |
| 4055 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4056 | static struct builtincmd * |
| 4057 | find_builtin(const char *name) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4058 | { |
| 4059 | struct builtincmd *bp; |
| 4060 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4061 | bp = bsearch( |
| 4062 | name, builtincmd, NUMBUILTINS, sizeof(struct builtincmd), |
| 4063 | pstrcmp |
| 4064 | ); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4065 | return bp; |
| 4066 | } |
| 4067 | |
| 4068 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4069 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4070 | /* |
| 4071 | * Called when a cd is done. Marks all commands so the next time they |
| 4072 | * are executed they will be rehashed. |
| 4073 | */ |
| 4074 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4075 | static void |
| 4076 | hashcd(void) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4077 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4078 | struct tblentry **pp; |
| 4079 | struct tblentry *cmdp; |
| 4080 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4081 | for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) { |
| 4082 | for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { |
| 4083 | if (cmdp->cmdtype == CMDNORMAL || ( |
| 4084 | cmdp->cmdtype == CMDBUILTIN && |
| 4085 | !(IS_BUILTIN_REGULAR(cmdp->param.cmd)) && |
| 4086 | builtinloc > 0 |
| 4087 | )) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4088 | cmdp->rehash = 1; |
| 4089 | } |
| 4090 | } |
| 4091 | } |
| 4092 | |
| 4093 | |
| 4094 | |
| 4095 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4096 | * Fix command hash table when PATH changed. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4097 | * Called before PATH is changed. The argument is the new value of PATH; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4098 | * pathval() still returns the old value at this point. |
| 4099 | * Called with interrupts off. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4100 | */ |
| 4101 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4102 | static void |
| 4103 | changepath(const char *newval) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4104 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4105 | const char *old, *new; |
| 4106 | int idx; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4107 | int firstchange; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4108 | int idx_bltin; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4109 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4110 | old = pathval(); |
| 4111 | new = newval; |
| 4112 | firstchange = 9999; /* assume no change */ |
| 4113 | idx = 0; |
| 4114 | idx_bltin = -1; |
| 4115 | for (;;) { |
| 4116 | if (*old != *new) { |
| 4117 | firstchange = idx; |
| 4118 | if ((*old == '\0' && *new == ':') |
| 4119 | || (*old == ':' && *new == '\0')) |
| 4120 | firstchange++; |
| 4121 | old = new; /* ignore subsequent differences */ |
| 4122 | } |
| 4123 | if (*new == '\0') |
| 4124 | break; |
| 4125 | if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin")) |
| 4126 | idx_bltin = idx; |
| 4127 | if (*new == ':') { |
| 4128 | idx++; |
| 4129 | } |
| 4130 | new++, old++; |
| 4131 | } |
| 4132 | if (builtinloc < 0 && idx_bltin >= 0) |
| 4133 | builtinloc = idx_bltin; /* zap builtins */ |
| 4134 | if (builtinloc >= 0 && idx_bltin < 0) |
| 4135 | firstchange = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4136 | clearcmdentry(firstchange); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4137 | builtinloc = idx_bltin; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
| 4140 | |
| 4141 | /* |
| 4142 | * Clear out command entries. The argument specifies the first entry in |
| 4143 | * PATH which has changed. |
| 4144 | */ |
| 4145 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4146 | static void |
| 4147 | clearcmdentry(int firstchange) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4148 | { |
| 4149 | struct tblentry **tblp; |
| 4150 | struct tblentry **pp; |
| 4151 | struct tblentry *cmdp; |
| 4152 | |
| 4153 | INTOFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4154 | for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4155 | pp = tblp; |
| 4156 | while ((cmdp = *pp) != NULL) { |
| 4157 | if ((cmdp->cmdtype == CMDNORMAL && |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4158 | cmdp->param.index >= firstchange) |
| 4159 | || (cmdp->cmdtype == CMDBUILTIN && |
| 4160 | builtinloc >= firstchange)) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4161 | *pp = cmdp->next; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4162 | ckfree(cmdp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4163 | } else { |
| 4164 | pp = &cmdp->next; |
| 4165 | } |
| 4166 | } |
| 4167 | } |
| 4168 | INTON; |
| 4169 | } |
| 4170 | |
| 4171 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4172 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4173 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4174 | * Locate a command in the command hash table. If "add" is nonzero, |
| 4175 | * add the command to the table if it is not already present. The |
| 4176 | * variable "lastcmdentry" is set to point to the address of the link |
| 4177 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 4178 | * entry. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4179 | * |
| 4180 | * Interrupts must be off if called with add != 0. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4181 | */ |
| 4182 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4183 | static struct tblentry **lastcmdentry; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4184 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4185 | |
| 4186 | static struct tblentry * |
| 4187 | cmdlookup(const char *name, int add) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4188 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4189 | unsigned int hashval; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4190 | const char *p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4191 | struct tblentry *cmdp; |
| 4192 | struct tblentry **pp; |
| 4193 | |
| 4194 | p = name; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4195 | hashval = (unsigned char)*p << 4; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4196 | while (*p) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4197 | hashval += (unsigned char)*p++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4198 | hashval &= 0x7FFF; |
| 4199 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4200 | for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4201 | if (equal(cmdp->cmdname, name)) |
| 4202 | break; |
| 4203 | pp = &cmdp->next; |
| 4204 | } |
| 4205 | if (add && cmdp == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4206 | cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB |
| 4207 | + strlen(name) + 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4208 | cmdp->next = NULL; |
| 4209 | cmdp->cmdtype = CMDUNKNOWN; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4210 | strcpy(cmdp->cmdname, name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4211 | } |
| 4212 | lastcmdentry = pp; |
| 4213 | return cmdp; |
| 4214 | } |
| 4215 | |
| 4216 | /* |
| 4217 | * Delete the command entry returned on the last lookup. |
| 4218 | */ |
| 4219 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4220 | static void |
| 4221 | delete_cmd_entry(void) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 4222 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4223 | struct tblentry *cmdp; |
| 4224 | |
| 4225 | INTOFF; |
| 4226 | cmdp = *lastcmdentry; |
| 4227 | *lastcmdentry = cmdp->next; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4228 | if (cmdp->cmdtype == CMDFUNCTION) |
| 4229 | freefunc(cmdp->param.func); |
| 4230 | ckfree(cmdp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4231 | INTON; |
| 4232 | } |
| 4233 | |
| 4234 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4235 | /* |
| 4236 | * Add a new command entry, replacing any existing command entry for |
| 4237 | * the same name - except special builtins. |
| 4238 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4239 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4240 | static inline void |
| 4241 | addcmdentry(char *name, struct cmdentry *entry) |
| 4242 | { |
| 4243 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4244 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4245 | cmdp = cmdlookup(name, 1); |
| 4246 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 4247 | freefunc(cmdp->param.func); |
| 4248 | } |
| 4249 | cmdp->cmdtype = entry->cmdtype; |
| 4250 | cmdp->param = entry->u; |
| 4251 | cmdp->rehash = 0; |
| 4252 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4253 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4254 | /* |
| 4255 | * Make a copy of a parse tree. |
| 4256 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4257 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4258 | static inline struct funcnode * |
| 4259 | copyfunc(union node *n) |
| 4260 | { |
| 4261 | struct funcnode *f; |
| 4262 | size_t blocksize; |
| 4263 | |
| 4264 | funcblocksize = offsetof(struct funcnode, n); |
| 4265 | funcstringsize = 0; |
| 4266 | calcsize(n); |
| 4267 | blocksize = funcblocksize; |
| 4268 | f = ckmalloc(blocksize + funcstringsize); |
| 4269 | funcblock = (char *) f + offsetof(struct funcnode, n); |
| 4270 | funcstring = (char *) f + blocksize; |
| 4271 | copynode(n); |
| 4272 | f->count = 0; |
| 4273 | return f; |
| 4274 | } |
| 4275 | |
| 4276 | /* |
| 4277 | * Define a shell function. |
| 4278 | */ |
| 4279 | |
| 4280 | static void |
| 4281 | defun(char *name, union node *func) |
| 4282 | { |
| 4283 | struct cmdentry entry; |
| 4284 | |
| 4285 | INTOFF; |
| 4286 | entry.cmdtype = CMDFUNCTION; |
| 4287 | entry.u.func = copyfunc(func); |
| 4288 | addcmdentry(name, &entry); |
| 4289 | INTON; |
| 4290 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4291 | |
| 4292 | |
| 4293 | /* |
| 4294 | * Delete a function if it exists. |
| 4295 | */ |
| 4296 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4297 | static void |
| 4298 | unsetfunc(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4299 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4300 | struct tblentry *cmdp; |
| 4301 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4302 | if ((cmdp = cmdlookup(name, 0)) != NULL && |
| 4303 | cmdp->cmdtype == CMDFUNCTION) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4304 | delete_cmd_entry(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4305 | } |
| 4306 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4307 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4308 | * Locate and print what a word is... |
| 4309 | */ |
| 4310 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4311 | |
| 4312 | #ifdef CONFIG_ASH_CMDCMD |
| 4313 | static int |
| 4314 | describe_command(char *command, int describe_command_verbose) |
| 4315 | #else |
| 4316 | #define describe_command_verbose 1 |
| 4317 | static int |
| 4318 | describe_command(char *command) |
| 4319 | #endif |
| 4320 | { |
| 4321 | struct cmdentry entry; |
| 4322 | struct tblentry *cmdp; |
| 4323 | #ifdef CONFIG_ASH_ALIAS |
| 4324 | const struct alias *ap; |
| 4325 | #endif |
| 4326 | const char *path = pathval(); |
| 4327 | |
| 4328 | if (describe_command_verbose) { |
| 4329 | out1str(command); |
| 4330 | } |
| 4331 | |
| 4332 | /* First look at the keywords */ |
| 4333 | if (findkwd(command)) { |
| 4334 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 4335 | goto out; |
| 4336 | } |
| 4337 | |
| 4338 | #ifdef CONFIG_ASH_ALIAS |
| 4339 | /* Then look at the aliases */ |
| 4340 | if ((ap = lookupalias(command, 0)) != NULL) { |
| 4341 | if (describe_command_verbose) { |
| 4342 | out1fmt(" is an alias for %s", ap->val); |
| 4343 | } else { |
| 4344 | out1str("alias "); |
| 4345 | printalias(ap); |
| 4346 | return 0; |
| 4347 | } |
| 4348 | goto out; |
| 4349 | } |
| 4350 | #endif |
| 4351 | /* Then check if it is a tracked alias */ |
| 4352 | if ((cmdp = cmdlookup(command, 0)) != NULL) { |
| 4353 | entry.cmdtype = cmdp->cmdtype; |
| 4354 | entry.u = cmdp->param; |
| 4355 | } else { |
| 4356 | /* Finally use brute force */ |
| 4357 | find_command(command, &entry, DO_ABS, path); |
| 4358 | } |
| 4359 | |
| 4360 | switch (entry.cmdtype) { |
| 4361 | case CMDNORMAL: { |
| 4362 | int j = entry.u.index; |
| 4363 | char *p; |
| 4364 | if (j == -1) { |
| 4365 | p = command; |
| 4366 | } else { |
| 4367 | do { |
| 4368 | p = padvance(&path, command); |
| 4369 | stunalloc(p); |
| 4370 | } while (--j >= 0); |
| 4371 | } |
| 4372 | if (describe_command_verbose) { |
| 4373 | out1fmt(" is%s %s", |
| 4374 | (cmdp ? " a tracked alias for" : nullstr), p |
| 4375 | ); |
| 4376 | } else { |
| 4377 | out1str(p); |
| 4378 | } |
| 4379 | break; |
| 4380 | } |
| 4381 | |
| 4382 | case CMDFUNCTION: |
| 4383 | if (describe_command_verbose) { |
| 4384 | out1str(" is a shell function"); |
| 4385 | } else { |
| 4386 | out1str(command); |
| 4387 | } |
| 4388 | break; |
| 4389 | |
| 4390 | case CMDBUILTIN: |
| 4391 | if (describe_command_verbose) { |
| 4392 | out1fmt(" is a %sshell builtin", |
| 4393 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 4394 | "special " : nullstr |
| 4395 | ); |
| 4396 | } else { |
| 4397 | out1str(command); |
| 4398 | } |
| 4399 | break; |
| 4400 | |
| 4401 | default: |
| 4402 | if (describe_command_verbose) { |
| 4403 | out1str(": not found\n"); |
| 4404 | } |
| 4405 | return 127; |
| 4406 | } |
| 4407 | |
| 4408 | out: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 4409 | outstr("\n", stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4410 | return 0; |
| 4411 | } |
| 4412 | |
| 4413 | static int |
| 4414 | typecmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4415 | { |
| 4416 | int i; |
| 4417 | int err = 0; |
| 4418 | |
| 4419 | for (i = 1; i < argc; i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4420 | #ifdef CONFIG_ASH_CMDCMD |
| 4421 | err |= describe_command(argv[i], 1); |
| 4422 | #else |
| 4423 | err |= describe_command(argv[i]); |
| 4424 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4425 | } |
| 4426 | return err; |
| 4427 | } |
| 4428 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 4429 | #ifdef CONFIG_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4430 | static int |
| 4431 | commandcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4432 | { |
| 4433 | int c; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4434 | enum { |
| 4435 | VERIFY_BRIEF = 1, |
| 4436 | VERIFY_VERBOSE = 2, |
| 4437 | } verify = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4438 | |
| 4439 | while ((c = nextopt("pvV")) != '\0') |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4440 | if (c == 'V') |
| 4441 | verify |= VERIFY_VERBOSE; |
| 4442 | else if (c == 'v') |
| 4443 | verify |= VERIFY_BRIEF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4444 | #ifdef DEBUG |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4445 | else if (c != 'p') |
| 4446 | abort(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4447 | #endif |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4448 | if (verify) |
| 4449 | return describe_command(*argptr, verify - VERIFY_BRIEF); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4450 | |
| 4451 | return 0; |
| 4452 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4453 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4454 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4455 | /* expand.c */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 4456 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4457 | /* |
| 4458 | * Routines to expand arguments to commands. We have to deal with |
| 4459 | * backquotes, shell variables, and file metacharacters. |
| 4460 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4461 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4462 | /* |
| 4463 | * _rmescape() flags |
| 4464 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4465 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 4466 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
| 4467 | #define RMESCAPE_QUOTED 0x4 /* Remove CTLESC unless in quotes */ |
| 4468 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 4469 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4470 | |
| 4471 | /* |
| 4472 | * Structure specifying which parts of the string should be searched |
| 4473 | * for IFS characters. |
| 4474 | */ |
| 4475 | |
| 4476 | struct ifsregion { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4477 | struct ifsregion *next; /* next region in list */ |
| 4478 | int begoff; /* offset of start of region */ |
| 4479 | int endoff; /* offset of end of region */ |
| 4480 | int nulonly; /* search for nul bytes only */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4481 | }; |
| 4482 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4483 | /* output of current string */ |
| 4484 | static char *expdest; |
| 4485 | /* list of back quote expressions */ |
| 4486 | static struct nodelist *argbackq; |
| 4487 | /* first struct in list of ifs regions */ |
| 4488 | static struct ifsregion ifsfirst; |
| 4489 | /* last struct in list */ |
| 4490 | static struct ifsregion *ifslastp; |
| 4491 | /* holds expanded arg list */ |
| 4492 | static struct arglist exparg; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4493 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 4494 | static void argstr(char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4495 | static char *exptilde(char *, char *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 4496 | static void expbackq(union node *, int, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4497 | static const char *subevalvar(char *, char *, int, int, int, int, int); |
| 4498 | static char *evalvar(char *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 4499 | static void strtodest(const char *, int, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4500 | static void memtodest(const char *p, size_t len, int syntax, int quotes); |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 4501 | static ssize_t varvalue(char *, int, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 4502 | static void recordregion(int, int, int); |
| 4503 | static void removerecordregions(int); |
| 4504 | static void ifsbreakup(char *, struct arglist *); |
| 4505 | static void ifsfree(void); |
| 4506 | static void expandmeta(struct strlist *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4507 | static int patmatch(char *, const char *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 4508 | |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 4509 | static int cvtnum(arith_t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4510 | static size_t esclen(const char *, const char *); |
| 4511 | static char *scanleft(char *, char *, char *, char *, int, int); |
| 4512 | static char *scanright(char *, char *, char *, char *, int, int); |
| 4513 | static void varunset(const char *, const char *, const char *, int) |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 4514 | ATTRIBUTE_NORETURN; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 4515 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4516 | |
| 4517 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 4518 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 4519 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4520 | * |
| 4521 | * Returns an stalloced string. |
| 4522 | */ |
| 4523 | |
| 4524 | static inline char * |
| 4525 | preglob(const char *pattern, int quoted, int flag) { |
| 4526 | flag |= RMESCAPE_GLOB; |
| 4527 | if (quoted) { |
| 4528 | flag |= RMESCAPE_QUOTED; |
| 4529 | } |
| 4530 | return _rmescapes((char *)pattern, flag); |
| 4531 | } |
| 4532 | |
| 4533 | |
| 4534 | static size_t |
| 4535 | esclen(const char *start, const char *p) { |
| 4536 | size_t esc = 0; |
| 4537 | |
| 4538 | while (p > start && *--p == CTLESC) { |
| 4539 | esc++; |
| 4540 | } |
| 4541 | return esc; |
| 4542 | } |
| 4543 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4544 | |
| 4545 | /* |
| 4546 | * Expand shell variables and backquotes inside a here document. |
| 4547 | */ |
| 4548 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4549 | static inline void |
| 4550 | expandhere(union node *arg, int fd) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 4551 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4552 | herefd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4553 | expandarg(arg, (struct arglist *)NULL, 0); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 4554 | bb_full_write(fd, stackblock(), expdest - (char *)stackblock()); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | |
| 4558 | /* |
| 4559 | * Perform variable substitution and command substitution on an argument, |
| 4560 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 4561 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 4562 | * here document expansion. |
| 4563 | */ |
| 4564 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4565 | void |
| 4566 | expandarg(union node *arg, struct arglist *arglist, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4567 | { |
| 4568 | struct strlist *sp; |
| 4569 | char *p; |
| 4570 | |
| 4571 | argbackq = arg->narg.backquote; |
| 4572 | STARTSTACKSTR(expdest); |
| 4573 | ifsfirst.next = NULL; |
| 4574 | ifslastp = NULL; |
| 4575 | argstr(arg->narg.text, flag); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 4576 | p = _STPUTC('\0', expdest); |
| 4577 | expdest = p - 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4578 | if (arglist == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4579 | return; /* here document expanded */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4580 | } |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 4581 | p = grabstackstr(p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4582 | exparg.lastp = &exparg.list; |
| 4583 | /* |
| 4584 | * TODO - EXP_REDIR |
| 4585 | */ |
| 4586 | if (flag & EXP_FULL) { |
| 4587 | ifsbreakup(p, &exparg); |
| 4588 | *exparg.lastp = NULL; |
| 4589 | exparg.lastp = &exparg.list; |
| 4590 | expandmeta(exparg.list, flag); |
| 4591 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4592 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4593 | rmescapes(p); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4594 | sp = (struct strlist *)stalloc(sizeof (struct strlist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4595 | sp->text = p; |
| 4596 | *exparg.lastp = sp; |
| 4597 | exparg.lastp = &sp->next; |
| 4598 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4599 | if (ifsfirst.next) |
| 4600 | ifsfree(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 4601 | *exparg.lastp = NULL; |
| 4602 | if (exparg.list) { |
| 4603 | *arglist->lastp = exparg.list; |
| 4604 | arglist->lastp = exparg.lastp; |
| 4605 | } |
| 4606 | } |
| 4607 | |
| 4608 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4609 | /* |
| 4610 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 4611 | * characters to allow for further processing. Otherwise treat |
| 4612 | * $@ like $* since no splitting will be performed. |
| 4613 | */ |
| 4614 | |
| 4615 | static void |
| 4616 | argstr(char *p, int flag) |
| 4617 | { |
| 4618 | static const char spclchars[] = { |
| 4619 | '=', |
| 4620 | ':', |
| 4621 | CTLQUOTEMARK, |
| 4622 | CTLENDVAR, |
| 4623 | CTLESC, |
| 4624 | CTLVAR, |
| 4625 | CTLBACKQ, |
| 4626 | CTLBACKQ | CTLQUOTE, |
| 4627 | #ifdef CONFIG_ASH_MATH_SUPPORT |
| 4628 | CTLENDARI, |
| 4629 | #endif |
| 4630 | 0 |
| 4631 | }; |
| 4632 | const char *reject = spclchars; |
| 4633 | int c; |
| 4634 | int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */ |
| 4635 | int breakall = flag & EXP_WORD; |
| 4636 | int inquotes; |
| 4637 | size_t length; |
| 4638 | int startloc; |
| 4639 | |
| 4640 | if (!(flag & EXP_VARTILDE)) { |
| 4641 | reject += 2; |
| 4642 | } else if (flag & EXP_VARTILDE2) { |
| 4643 | reject++; |
| 4644 | } |
| 4645 | inquotes = 0; |
| 4646 | length = 0; |
| 4647 | if (flag & EXP_TILDE) { |
| 4648 | char *q; |
| 4649 | |
| 4650 | flag &= ~EXP_TILDE; |
| 4651 | tilde: |
| 4652 | q = p; |
| 4653 | if (*q == CTLESC && (flag & EXP_QWORD)) |
| 4654 | q++; |
| 4655 | if (*q == '~') |
| 4656 | p = exptilde(p, q, flag); |
| 4657 | } |
| 4658 | start: |
| 4659 | startloc = expdest - (char *)stackblock(); |
| 4660 | for (;;) { |
| 4661 | length += strcspn(p + length, reject); |
| 4662 | c = p[length]; |
| 4663 | if (c && (!(c & 0x80) |
| 4664 | #ifdef CONFIG_ASH_MATH_SUPPORT |
| 4665 | || c == CTLENDARI |
| 4666 | #endif |
| 4667 | )) { |
| 4668 | /* c == '=' || c == ':' || c == CTLENDARI */ |
| 4669 | length++; |
| 4670 | } |
| 4671 | if (length > 0) { |
| 4672 | int newloc; |
| 4673 | expdest = stnputs(p, length, expdest); |
| 4674 | newloc = expdest - (char *)stackblock(); |
| 4675 | if (breakall && !inquotes && newloc > startloc) { |
| 4676 | recordregion(startloc, newloc, 0); |
| 4677 | } |
| 4678 | startloc = newloc; |
| 4679 | } |
| 4680 | p += length + 1; |
| 4681 | length = 0; |
| 4682 | |
| 4683 | switch (c) { |
| 4684 | case '\0': |
| 4685 | goto breakloop; |
| 4686 | case '=': |
| 4687 | if (flag & EXP_VARTILDE2) { |
| 4688 | p--; |
| 4689 | continue; |
| 4690 | } |
| 4691 | flag |= EXP_VARTILDE2; |
| 4692 | reject++; |
| 4693 | /* fall through */ |
| 4694 | case ':': |
| 4695 | /* |
| 4696 | * sort of a hack - expand tildes in variable |
| 4697 | * assignments (after the first '=' and after ':'s). |
| 4698 | */ |
| 4699 | if (*--p == '~') { |
| 4700 | goto tilde; |
| 4701 | } |
| 4702 | continue; |
| 4703 | } |
| 4704 | |
| 4705 | switch (c) { |
| 4706 | case CTLENDVAR: /* ??? */ |
| 4707 | goto breakloop; |
| 4708 | case CTLQUOTEMARK: |
| 4709 | /* "$@" syntax adherence hack */ |
| 4710 | if ( |
| 4711 | !inquotes && |
| 4712 | !memcmp(p, dolatstr, DOLATSTRLEN) && |
| 4713 | (p[4] == CTLQUOTEMARK || ( |
| 4714 | p[4] == CTLENDVAR && |
| 4715 | p[5] == CTLQUOTEMARK |
| 4716 | )) |
| 4717 | ) { |
| 4718 | p = evalvar(p + 1, flag) + 1; |
| 4719 | goto start; |
| 4720 | } |
| 4721 | inquotes = !inquotes; |
| 4722 | addquote: |
| 4723 | if (quotes) { |
| 4724 | p--; |
| 4725 | length++; |
| 4726 | startloc++; |
| 4727 | } |
| 4728 | break; |
| 4729 | case CTLESC: |
| 4730 | startloc++; |
| 4731 | length++; |
| 4732 | goto addquote; |
| 4733 | case CTLVAR: |
| 4734 | p = evalvar(p, flag); |
| 4735 | goto start; |
| 4736 | case CTLBACKQ: |
| 4737 | c = 0; |
| 4738 | case CTLBACKQ|CTLQUOTE: |
| 4739 | expbackq(argbackq->n, c, quotes); |
| 4740 | argbackq = argbackq->next; |
| 4741 | goto start; |
| 4742 | #ifdef CONFIG_ASH_MATH_SUPPORT |
| 4743 | case CTLENDARI: |
| 4744 | p--; |
| 4745 | expari(quotes); |
| 4746 | goto start; |
| 4747 | #endif |
| 4748 | } |
| 4749 | } |
| 4750 | breakloop: |
| 4751 | ; |
| 4752 | } |
| 4753 | |
| 4754 | static char * |
| 4755 | exptilde(char *startp, char *p, int flag) |
| 4756 | { |
| 4757 | char c; |
| 4758 | char *name; |
| 4759 | struct passwd *pw; |
| 4760 | const char *home; |
| 4761 | int quotes = flag & (EXP_FULL | EXP_CASE); |
| 4762 | int startloc; |
| 4763 | |
| 4764 | name = p + 1; |
| 4765 | |
| 4766 | while ((c = *++p) != '\0') { |
| 4767 | switch(c) { |
| 4768 | case CTLESC: |
| 4769 | return (startp); |
| 4770 | case CTLQUOTEMARK: |
| 4771 | return (startp); |
| 4772 | case ':': |
| 4773 | if (flag & EXP_VARTILDE) |
| 4774 | goto done; |
| 4775 | break; |
| 4776 | case '/': |
| 4777 | case CTLENDVAR: |
| 4778 | goto done; |
| 4779 | } |
| 4780 | } |
| 4781 | done: |
| 4782 | *p = '\0'; |
| 4783 | if (*name == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4784 | home = lookupvar(homestr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4785 | } else { |
| 4786 | if ((pw = getpwnam(name)) == NULL) |
| 4787 | goto lose; |
| 4788 | home = pw->pw_dir; |
| 4789 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4790 | if (!home || !*home) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4791 | goto lose; |
| 4792 | *p = c; |
| 4793 | startloc = expdest - (char *)stackblock(); |
| 4794 | strtodest(home, SQSYNTAX, quotes); |
| 4795 | recordregion(startloc, expdest - (char *)stackblock(), 0); |
| 4796 | return (p); |
| 4797 | lose: |
| 4798 | *p = c; |
| 4799 | return (startp); |
| 4800 | } |
| 4801 | |
| 4802 | |
| 4803 | static void |
| 4804 | removerecordregions(int endoff) |
| 4805 | { |
| 4806 | if (ifslastp == NULL) |
| 4807 | return; |
| 4808 | |
| 4809 | if (ifsfirst.endoff > endoff) { |
| 4810 | while (ifsfirst.next != NULL) { |
| 4811 | struct ifsregion *ifsp; |
| 4812 | INTOFF; |
| 4813 | ifsp = ifsfirst.next->next; |
| 4814 | ckfree(ifsfirst.next); |
| 4815 | ifsfirst.next = ifsp; |
| 4816 | INTON; |
| 4817 | } |
| 4818 | if (ifsfirst.begoff > endoff) |
| 4819 | ifslastp = NULL; |
| 4820 | else { |
| 4821 | ifslastp = &ifsfirst; |
| 4822 | ifsfirst.endoff = endoff; |
| 4823 | } |
| 4824 | return; |
| 4825 | } |
| 4826 | |
| 4827 | ifslastp = &ifsfirst; |
| 4828 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
| 4829 | ifslastp=ifslastp->next; |
| 4830 | while (ifslastp->next != NULL) { |
| 4831 | struct ifsregion *ifsp; |
| 4832 | INTOFF; |
| 4833 | ifsp = ifslastp->next->next; |
| 4834 | ckfree(ifslastp->next); |
| 4835 | ifslastp->next = ifsp; |
| 4836 | INTON; |
| 4837 | } |
| 4838 | if (ifslastp->endoff > endoff) |
| 4839 | ifslastp->endoff = endoff; |
| 4840 | } |
| 4841 | |
| 4842 | |
| 4843 | #ifdef CONFIG_ASH_MATH_SUPPORT |
| 4844 | /* |
| 4845 | * Expand arithmetic expression. Backup to start of expression, |
| 4846 | * evaluate, place result in (backed up) result, adjust string position. |
| 4847 | */ |
| 4848 | void |
| 4849 | expari(int quotes) |
| 4850 | { |
| 4851 | char *p, *start; |
| 4852 | int begoff; |
| 4853 | int flag; |
| 4854 | int len; |
| 4855 | |
| 4856 | /* ifsfree(); */ |
| 4857 | |
| 4858 | /* |
| 4859 | * This routine is slightly over-complicated for |
| 4860 | * efficiency. Next we scan backwards looking for the |
| 4861 | * start of arithmetic. |
| 4862 | */ |
| 4863 | start = stackblock(); |
| 4864 | p = expdest - 1; |
| 4865 | *p = '\0'; |
| 4866 | p--; |
| 4867 | do { |
| 4868 | int esc; |
| 4869 | |
| 4870 | while (*p != CTLARI) { |
| 4871 | p--; |
| 4872 | #ifdef DEBUG |
| 4873 | if (p < start) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 4874 | sh_error("missing CTLARI (shouldn't happen)"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4875 | } |
| 4876 | #endif |
| 4877 | } |
| 4878 | |
| 4879 | esc = esclen(start, p); |
| 4880 | if (!(esc % 2)) { |
| 4881 | break; |
| 4882 | } |
| 4883 | |
| 4884 | p -= esc + 1; |
| 4885 | } while (1); |
| 4886 | |
| 4887 | begoff = p - start; |
| 4888 | |
| 4889 | removerecordregions(begoff); |
| 4890 | |
| 4891 | flag = p[1]; |
| 4892 | |
| 4893 | expdest = p; |
| 4894 | |
| 4895 | if (quotes) |
| 4896 | rmescapes(p + 2); |
| 4897 | |
| 4898 | len = cvtnum(dash_arith(p + 2)); |
| 4899 | |
| 4900 | if (flag != '"') |
| 4901 | recordregion(begoff, begoff + len, 0); |
| 4902 | } |
| 4903 | #endif |
| 4904 | |
| 4905 | /* |
| 4906 | * Expand stuff in backwards quotes. |
| 4907 | */ |
| 4908 | |
| 4909 | static void |
| 4910 | expbackq(union node *cmd, int quoted, int quotes) |
| 4911 | { |
| 4912 | struct backcmd in; |
| 4913 | int i; |
| 4914 | char buf[128]; |
| 4915 | char *p; |
| 4916 | char *dest; |
| 4917 | int startloc; |
| 4918 | int syntax = quoted? DQSYNTAX : BASESYNTAX; |
| 4919 | struct stackmark smark; |
| 4920 | |
| 4921 | INTOFF; |
| 4922 | setstackmark(&smark); |
| 4923 | dest = expdest; |
| 4924 | startloc = dest - (char *)stackblock(); |
| 4925 | grabstackstr(dest); |
| 4926 | evalbackcmd(cmd, (struct backcmd *) &in); |
| 4927 | popstackmark(&smark); |
| 4928 | |
| 4929 | p = in.buf; |
| 4930 | i = in.nleft; |
| 4931 | if (i == 0) |
| 4932 | goto read; |
| 4933 | for (;;) { |
| 4934 | memtodest(p, i, syntax, quotes); |
| 4935 | read: |
| 4936 | if (in.fd < 0) |
| 4937 | break; |
| 4938 | i = safe_read(in.fd, buf, sizeof buf); |
| 4939 | TRACE(("expbackq: read returns %d\n", i)); |
| 4940 | if (i <= 0) |
| 4941 | break; |
| 4942 | p = buf; |
| 4943 | } |
| 4944 | |
| 4945 | if (in.buf) |
| 4946 | ckfree(in.buf); |
| 4947 | if (in.fd >= 0) { |
| 4948 | close(in.fd); |
| 4949 | back_exitstatus = waitforjob(in.jp); |
| 4950 | } |
| 4951 | INTON; |
| 4952 | |
| 4953 | /* Eat all trailing newlines */ |
| 4954 | dest = expdest; |
| 4955 | for (; dest > (char *)stackblock() && dest[-1] == '\n';) |
| 4956 | STUNPUTC(dest); |
| 4957 | expdest = dest; |
| 4958 | |
| 4959 | if (quoted == 0) |
| 4960 | recordregion(startloc, dest - (char *)stackblock(), 0); |
| 4961 | TRACE(("evalbackq: size=%d: \"%.*s\"\n", |
| 4962 | (dest - (char *)stackblock()) - startloc, |
| 4963 | (dest - (char *)stackblock()) - startloc, |
| 4964 | stackblock() + startloc)); |
| 4965 | } |
| 4966 | |
| 4967 | |
| 4968 | static char * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 4969 | scanleft(char *startp, char *rmesc, char *rmescend, char *str, int quotes, |
| 4970 | int zero) |
| 4971 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 4972 | char *loc; |
| 4973 | char *loc2; |
| 4974 | char c; |
| 4975 | |
| 4976 | loc = startp; |
| 4977 | loc2 = rmesc; |
| 4978 | do { |
| 4979 | int match; |
| 4980 | const char *s = loc2; |
| 4981 | c = *loc2; |
| 4982 | if (zero) { |
| 4983 | *loc2 = '\0'; |
| 4984 | s = rmesc; |
| 4985 | } |
| 4986 | match = pmatch(str, s); |
| 4987 | *loc2 = c; |
| 4988 | if (match) |
| 4989 | return loc; |
| 4990 | if (quotes && *loc == CTLESC) |
| 4991 | loc++; |
| 4992 | loc++; |
| 4993 | loc2++; |
| 4994 | } while (c); |
| 4995 | return 0; |
| 4996 | } |
| 4997 | |
| 4998 | |
| 4999 | static char * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5000 | scanright(char *startp, char *rmesc, char *rmescend, char *str, int quotes, |
| 5001 | int zero) |
| 5002 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5003 | int esc = 0; |
| 5004 | char *loc; |
| 5005 | char *loc2; |
| 5006 | |
| 5007 | for (loc = str - 1, loc2 = rmescend; loc >= startp; loc2--) { |
| 5008 | int match; |
| 5009 | char c = *loc2; |
| 5010 | const char *s = loc2; |
| 5011 | if (zero) { |
| 5012 | *loc2 = '\0'; |
| 5013 | s = rmesc; |
| 5014 | } |
| 5015 | match = pmatch(str, s); |
| 5016 | *loc2 = c; |
| 5017 | if (match) |
| 5018 | return loc; |
| 5019 | loc--; |
| 5020 | if (quotes) { |
| 5021 | if (--esc < 0) { |
| 5022 | esc = esclen(startp, loc); |
| 5023 | } |
| 5024 | if (esc % 2) { |
| 5025 | esc--; |
| 5026 | loc--; |
| 5027 | } |
| 5028 | } |
| 5029 | } |
| 5030 | return 0; |
| 5031 | } |
| 5032 | |
| 5033 | static const char * |
| 5034 | subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varflags, int quotes) |
| 5035 | { |
| 5036 | char *startp; |
| 5037 | char *loc; |
| 5038 | int saveherefd = herefd; |
| 5039 | struct nodelist *saveargbackq = argbackq; |
| 5040 | int amount; |
| 5041 | char *rmesc, *rmescend; |
| 5042 | int zero; |
| 5043 | char *(*scan)(char *, char *, char *, char *, int , int); |
| 5044 | |
| 5045 | herefd = -1; |
| 5046 | argstr(p, subtype != VSASSIGN && subtype != VSQUESTION ? EXP_CASE : 0); |
| 5047 | STPUTC('\0', expdest); |
| 5048 | herefd = saveherefd; |
| 5049 | argbackq = saveargbackq; |
| 5050 | startp = stackblock() + startloc; |
| 5051 | |
| 5052 | switch (subtype) { |
| 5053 | case VSASSIGN: |
| 5054 | setvar(str, startp, 0); |
| 5055 | amount = startp - expdest; |
| 5056 | STADJUST(amount, expdest); |
| 5057 | return startp; |
| 5058 | |
| 5059 | case VSQUESTION: |
| 5060 | varunset(p, str, startp, varflags); |
| 5061 | /* NOTREACHED */ |
| 5062 | } |
| 5063 | |
| 5064 | subtype -= VSTRIMRIGHT; |
| 5065 | #ifdef DEBUG |
| 5066 | if (subtype < 0 || subtype > 3) |
| 5067 | abort(); |
| 5068 | #endif |
| 5069 | |
| 5070 | rmesc = startp; |
| 5071 | rmescend = stackblock() + strloc; |
| 5072 | if (quotes) { |
| 5073 | rmesc = _rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); |
| 5074 | if (rmesc != startp) { |
| 5075 | rmescend = expdest; |
| 5076 | startp = stackblock() + startloc; |
| 5077 | } |
| 5078 | } |
| 5079 | rmescend--; |
| 5080 | str = stackblock() + strloc; |
| 5081 | preglob(str, varflags & VSQUOTE, 0); |
| 5082 | |
| 5083 | /* zero = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX */ |
| 5084 | zero = subtype >> 1; |
| 5085 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 5086 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 5087 | |
| 5088 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 5089 | if (loc) { |
| 5090 | if (zero) { |
| 5091 | memmove(startp, loc, str - loc); |
| 5092 | loc = startp + (str - loc) - 1; |
| 5093 | } |
| 5094 | *loc = '\0'; |
| 5095 | amount = loc - expdest; |
| 5096 | STADJUST(amount, expdest); |
| 5097 | } |
| 5098 | return loc; |
| 5099 | } |
| 5100 | |
| 5101 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5102 | /* |
| 5103 | * Expand a variable, and return a pointer to the next character in the |
| 5104 | * input string. |
| 5105 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5106 | static char * |
| 5107 | evalvar(char *p, int flag) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5108 | { |
| 5109 | int subtype; |
| 5110 | int varflags; |
| 5111 | char *var; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5112 | int patloc; |
| 5113 | int c; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5114 | int startloc; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5115 | ssize_t varlen; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5116 | int easy; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5117 | int quotes; |
| 5118 | int quoted; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5119 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5120 | quotes = flag & (EXP_FULL | EXP_CASE); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5121 | varflags = *p++; |
| 5122 | subtype = varflags & VSTYPE; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5123 | quoted = varflags & VSQUOTE; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5124 | var = p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5125 | easy = (!quoted || (*var == '@' && shellparam.nparam)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5126 | startloc = expdest - (char *)stackblock(); |
| 5127 | p = strchr(p, '=') + 1; |
| 5128 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5129 | again: |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5130 | varlen = varvalue(var, varflags, flag); |
| 5131 | if (varflags & VSNUL) |
| 5132 | varlen--; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5133 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5134 | if (subtype == VSPLUS) { |
| 5135 | varlen = -1 - varlen; |
| 5136 | goto vsplus; |
| 5137 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5138 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5139 | if (subtype == VSMINUS) { |
| 5140 | vsplus: |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5141 | if (varlen < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5142 | argstr( |
| 5143 | p, flag | EXP_TILDE | |
| 5144 | (quoted ? EXP_QWORD : EXP_WORD) |
| 5145 | ); |
| 5146 | goto end; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5147 | } |
| 5148 | if (easy) |
| 5149 | goto record; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5150 | goto end; |
| 5151 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5152 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5153 | if (subtype == VSASSIGN || subtype == VSQUESTION) { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5154 | if (varlen < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5155 | if (subevalvar(p, var, 0, subtype, startloc, |
| 5156 | varflags, 0)) { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5157 | varflags &= ~VSNUL; |
| 5158 | /* |
| 5159 | * Remove any recorded regions beyond |
| 5160 | * start of variable |
| 5161 | */ |
| 5162 | removerecordregions(startloc); |
| 5163 | goto again; |
| 5164 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5165 | goto end; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5166 | } |
| 5167 | if (easy) |
| 5168 | goto record; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5169 | goto end; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5170 | } |
| 5171 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5172 | if (varlen < 0 && uflag) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5173 | varunset(p, var, 0, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 5174 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5175 | if (subtype == VSLENGTH) { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5176 | cvtnum(varlen > 0 ? varlen : 0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5177 | goto record; |
| 5178 | } |
| 5179 | |
| 5180 | if (subtype == VSNORMAL) { |
| 5181 | if (!easy) |
| 5182 | goto end; |
| 5183 | record: |
| 5184 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
| 5185 | goto end; |
| 5186 | } |
| 5187 | |
| 5188 | #ifdef DEBUG |
| 5189 | switch (subtype) { |
| 5190 | case VSTRIMLEFT: |
| 5191 | case VSTRIMLEFTMAX: |
| 5192 | case VSTRIMRIGHT: |
| 5193 | case VSTRIMRIGHTMAX: |
| 5194 | break; |
| 5195 | default: |
| 5196 | abort(); |
| 5197 | } |
| 5198 | #endif |
| 5199 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5200 | if (varlen >= 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5201 | /* |
| 5202 | * Terminate the string and start recording the pattern |
| 5203 | * right after it |
| 5204 | */ |
| 5205 | STPUTC('\0', expdest); |
| 5206 | patloc = expdest - (char *)stackblock(); |
| 5207 | if (subevalvar(p, NULL, patloc, subtype, |
| 5208 | startloc, varflags, quotes) == 0) { |
| 5209 | int amount = expdest - ( |
| 5210 | (char *)stackblock() + patloc - 1 |
| 5211 | ); |
| 5212 | STADJUST(-amount, expdest); |
| 5213 | } |
| 5214 | /* Remove any recorded regions beyond start of variable */ |
| 5215 | removerecordregions(startloc); |
| 5216 | goto record; |
| 5217 | } |
| 5218 | |
| 5219 | end: |
| 5220 | if (subtype != VSNORMAL) { /* skip to end of alternative */ |
| 5221 | int nesting = 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5222 | for (;;) { |
| 5223 | if ((c = *p++) == CTLESC) |
| 5224 | p++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5225 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5226 | if (varlen >= 0) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 5227 | argbackq = argbackq->next; |
| 5228 | } else if (c == CTLVAR) { |
| 5229 | if ((*p++ & VSTYPE) != VSNORMAL) |
| 5230 | nesting++; |
| 5231 | } else if (c == CTLENDVAR) { |
| 5232 | if (--nesting == 0) |
| 5233 | break; |
| 5234 | } |
| 5235 | } |
| 5236 | } |
| 5237 | return p; |
| 5238 | } |
| 5239 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5240 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5241 | /* |
| 5242 | * Put a string on the stack. |
| 5243 | */ |
| 5244 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5245 | static void |
| 5246 | memtodest(const char *p, size_t len, int syntax, int quotes) { |
| 5247 | char *q = expdest; |
| 5248 | |
| 5249 | q = makestrspace(len * 2, q); |
| 5250 | |
| 5251 | while (len--) { |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 5252 | int c = SC2INT(*p++); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5253 | if (!c) |
| 5254 | continue; |
| 5255 | if (quotes && (SIT(c, syntax) == CCTL || SIT(c, syntax) == CBACK)) |
| 5256 | USTPUTC(CTLESC, q); |
| 5257 | USTPUTC(c, q); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5258 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5259 | |
| 5260 | expdest = q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5261 | } |
| 5262 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5263 | |
| 5264 | static void |
| 5265 | strtodest(const char *p, int syntax, int quotes) |
| 5266 | { |
| 5267 | memtodest(p, strlen(p), syntax, quotes); |
| 5268 | } |
| 5269 | |
| 5270 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5271 | /* |
| 5272 | * Add the value of a specialized variable to the stack string. |
| 5273 | */ |
| 5274 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5275 | static ssize_t |
| 5276 | varvalue(char *name, int varflags, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5277 | { |
| 5278 | int num; |
| 5279 | char *p; |
| 5280 | int i; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5281 | int sep = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5282 | int sepq = 0; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5283 | ssize_t len = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5284 | char **ap; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 5285 | int syntax; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5286 | int quoted = varflags & VSQUOTE; |
| 5287 | int subtype = varflags & VSTYPE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5288 | int quotes = flags & (EXP_FULL | EXP_CASE); |
| 5289 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5290 | if (quoted && (flags & EXP_FULL)) |
| 5291 | sep = 1 << CHAR_BIT; |
| 5292 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5293 | syntax = quoted ? DQSYNTAX : BASESYNTAX; |
| 5294 | switch (*name) { |
| 5295 | case '$': |
| 5296 | num = rootpid; |
| 5297 | goto numvar; |
| 5298 | case '?': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5299 | num = exitstatus; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5300 | goto numvar; |
| 5301 | case '#': |
| 5302 | num = shellparam.nparam; |
| 5303 | goto numvar; |
| 5304 | case '!': |
| 5305 | num = backgndpid; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5306 | if (num == 0) |
| 5307 | return -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5308 | numvar: |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5309 | len = cvtnum(num); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5310 | break; |
| 5311 | case '-': |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5312 | p = makestrspace(NOPTS, expdest); |
| 5313 | for (i = NOPTS - 1; i >= 0; i--) { |
| 5314 | if (optlist[i]) { |
| 5315 | USTPUTC(optletters(i), p); |
| 5316 | len++; |
| 5317 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5318 | } |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5319 | expdest = p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5320 | break; |
| 5321 | case '@': |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5322 | if (sep) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5323 | goto param; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5324 | /* fall through */ |
| 5325 | case '*': |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 5326 | sep = ifsset() ? SC2INT(ifsval()[0]) : ' '; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5327 | if (quotes && (SIT(sep, syntax) == CCTL || SIT(sep, syntax) == CBACK)) |
| 5328 | sepq = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5329 | param: |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5330 | if (!(ap = shellparam.p)) |
| 5331 | return -1; |
| 5332 | while ((p = *ap++)) { |
| 5333 | size_t partlen; |
| 5334 | |
| 5335 | partlen = strlen(p); |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5336 | len += partlen; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 5337 | |
| 5338 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 5339 | memtodest(p, partlen, syntax, quotes); |
| 5340 | |
| 5341 | if (*ap && sep) { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5342 | char *q; |
| 5343 | |
| 5344 | len++; |
| 5345 | if (subtype == VSPLUS || subtype == VSLENGTH) { |
| 5346 | continue; |
| 5347 | } |
| 5348 | q = expdest; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5349 | if (sepq) |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5350 | STPUTC(CTLESC, q); |
| 5351 | STPUTC(sep, q); |
| 5352 | expdest = q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5353 | } |
| 5354 | } |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5355 | return len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5356 | case '0': |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5357 | case '1': |
| 5358 | case '2': |
| 5359 | case '3': |
| 5360 | case '4': |
| 5361 | case '5': |
| 5362 | case '6': |
| 5363 | case '7': |
| 5364 | case '8': |
| 5365 | case '9': |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5366 | num = atoi(name); |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5367 | if (num < 0 || num > shellparam.nparam) |
| 5368 | return -1; |
| 5369 | p = num ? shellparam.p[num - 1] : arg0; |
| 5370 | goto value; |
| 5371 | default: |
| 5372 | p = lookupvar(name); |
| 5373 | value: |
| 5374 | if (!p) |
| 5375 | return -1; |
| 5376 | |
| 5377 | len = strlen(p); |
| 5378 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 5379 | memtodest(p, len, syntax, quotes); |
| 5380 | return len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5381 | } |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 5382 | |
| 5383 | if (subtype == VSPLUS || subtype == VSLENGTH) |
| 5384 | STADJUST(-len, expdest); |
| 5385 | return len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5389 | /* |
| 5390 | * Record the fact that we have to scan this region of the |
| 5391 | * string for IFS characters. |
| 5392 | */ |
| 5393 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5394 | static void |
| 5395 | recordregion(int start, int end, int nulonly) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5396 | { |
| 5397 | struct ifsregion *ifsp; |
| 5398 | |
| 5399 | if (ifslastp == NULL) { |
| 5400 | ifsp = &ifsfirst; |
| 5401 | } else { |
| 5402 | INTOFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5403 | ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5404 | ifsp->next = NULL; |
| 5405 | ifslastp->next = ifsp; |
| 5406 | INTON; |
| 5407 | } |
| 5408 | ifslastp = ifsp; |
| 5409 | ifslastp->begoff = start; |
| 5410 | ifslastp->endoff = end; |
| 5411 | ifslastp->nulonly = nulonly; |
| 5412 | } |
| 5413 | |
| 5414 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5415 | /* |
| 5416 | * Break the argument string into pieces based upon IFS and add the |
| 5417 | * strings to the argument list. The regions of the string to be |
| 5418 | * searched for IFS characters have been stored by recordregion. |
| 5419 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5420 | static void |
| 5421 | ifsbreakup(char *string, struct arglist *arglist) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5422 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5423 | struct ifsregion *ifsp; |
| 5424 | struct strlist *sp; |
| 5425 | char *start; |
| 5426 | char *p; |
| 5427 | char *q; |
| 5428 | const char *ifs, *realifs; |
| 5429 | int ifsspc; |
| 5430 | int nulonly; |
| 5431 | |
| 5432 | |
| 5433 | start = string; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5434 | if (ifslastp != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5435 | ifsspc = 0; |
| 5436 | nulonly = 0; |
| 5437 | realifs = ifsset() ? ifsval() : defifs; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5438 | ifsp = &ifsfirst; |
| 5439 | do { |
| 5440 | p = string + ifsp->begoff; |
| 5441 | nulonly = ifsp->nulonly; |
| 5442 | ifs = nulonly ? nullstr : realifs; |
| 5443 | ifsspc = 0; |
| 5444 | while (p < string + ifsp->endoff) { |
| 5445 | q = p; |
| 5446 | if (*p == CTLESC) |
| 5447 | p++; |
| 5448 | if (strchr(ifs, *p)) { |
| 5449 | if (!nulonly) |
| 5450 | ifsspc = (strchr(defifs, *p) != NULL); |
| 5451 | /* Ignore IFS whitespace at start */ |
| 5452 | if (q == start && ifsspc) { |
| 5453 | p++; |
| 5454 | start = p; |
| 5455 | continue; |
| 5456 | } |
| 5457 | *q = '\0'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5458 | sp = (struct strlist *)stalloc(sizeof *sp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5459 | sp->text = start; |
| 5460 | *arglist->lastp = sp; |
| 5461 | arglist->lastp = &sp->next; |
| 5462 | p++; |
| 5463 | if (!nulonly) { |
| 5464 | for (;;) { |
| 5465 | if (p >= string + ifsp->endoff) { |
| 5466 | break; |
| 5467 | } |
| 5468 | q = p; |
| 5469 | if (*p == CTLESC) |
| 5470 | p++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5471 | if (strchr(ifs, *p) == NULL ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5472 | p = q; |
| 5473 | break; |
| 5474 | } else if (strchr(defifs, *p) == NULL) { |
| 5475 | if (ifsspc) { |
| 5476 | p++; |
| 5477 | ifsspc = 0; |
| 5478 | } else { |
| 5479 | p = q; |
| 5480 | break; |
| 5481 | } |
| 5482 | } else |
| 5483 | p++; |
| 5484 | } |
| 5485 | } |
| 5486 | start = p; |
| 5487 | } else |
| 5488 | p++; |
| 5489 | } |
| 5490 | } while ((ifsp = ifsp->next) != NULL); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5491 | if (nulonly) |
| 5492 | goto add; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5493 | } |
| 5494 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5495 | if (!*start) |
| 5496 | return; |
| 5497 | |
| 5498 | add: |
| 5499 | sp = (struct strlist *)stalloc(sizeof *sp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5500 | sp->text = start; |
| 5501 | *arglist->lastp = sp; |
| 5502 | arglist->lastp = &sp->next; |
| 5503 | } |
| 5504 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5505 | static void |
| 5506 | ifsfree(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5507 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5508 | struct ifsregion *p; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 5509 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5510 | INTOFF; |
| 5511 | p = ifsfirst.next; |
| 5512 | do { |
| 5513 | struct ifsregion *ifsp; |
| 5514 | ifsp = p->next; |
| 5515 | ckfree(p); |
| 5516 | p = ifsp; |
| 5517 | } while (p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5518 | ifslastp = NULL; |
| 5519 | ifsfirst.next = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5520 | INTON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5523 | static void expmeta(char *, char *); |
| 5524 | static struct strlist *expsort(struct strlist *); |
| 5525 | static struct strlist *msort(struct strlist *, int); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5526 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5527 | static char *expdir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5528 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5529 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5530 | static void |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5531 | expandmeta(struct strlist *str, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5532 | { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5533 | static const char metachars[] = { |
| 5534 | '*', '?', '[', 0 |
| 5535 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5536 | /* TODO - EXP_REDIR */ |
| 5537 | |
| 5538 | while (str) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5539 | struct strlist **savelastp; |
| 5540 | struct strlist *sp; |
| 5541 | char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5542 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5543 | if (fflag) |
| 5544 | goto nometa; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5545 | if (!strpbrk(str->text, metachars)) |
| 5546 | goto nometa; |
| 5547 | savelastp = exparg.lastp; |
| 5548 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5549 | INTOFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5550 | p = preglob(str->text, 0, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5551 | { |
| 5552 | int i = strlen(str->text); |
| 5553 | expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ |
| 5554 | } |
| 5555 | |
| 5556 | expmeta(expdir, p); |
| 5557 | ckfree(expdir); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5558 | if (p != str->text) |
| 5559 | ckfree(p); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5560 | INTON; |
| 5561 | if (exparg.lastp == savelastp) { |
| 5562 | /* |
| 5563 | * no matches |
| 5564 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5565 | nometa: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5566 | *exparg.lastp = str; |
| 5567 | rmescapes(str->text); |
| 5568 | exparg.lastp = &str->next; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5569 | } else { |
| 5570 | *exparg.lastp = NULL; |
| 5571 | *savelastp = sp = expsort(*savelastp); |
| 5572 | while (sp->next != NULL) |
| 5573 | sp = sp->next; |
| 5574 | exparg.lastp = &sp->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5575 | } |
| 5576 | str = str->next; |
| 5577 | } |
| 5578 | } |
| 5579 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5580 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5581 | * Add a file name to the list. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5582 | */ |
| 5583 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5584 | static void |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5585 | addfname(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5586 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5587 | struct strlist *sp; |
| 5588 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5589 | sp = (struct strlist *)stalloc(sizeof *sp); |
| 5590 | sp->text = sstrdup(name); |
| 5591 | *exparg.lastp = sp; |
| 5592 | exparg.lastp = &sp->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
| 5595 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5596 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5597 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 5598 | */ |
| 5599 | |
| 5600 | static void |
| 5601 | expmeta(char *enddir, char *name) |
| 5602 | { |
| 5603 | char *p; |
| 5604 | const char *cp; |
| 5605 | char *start; |
| 5606 | char *endname; |
| 5607 | int metaflag; |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 5608 | struct stat statb; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5609 | DIR *dirp; |
| 5610 | struct dirent *dp; |
| 5611 | int atend; |
| 5612 | int matchdot; |
| 5613 | |
| 5614 | metaflag = 0; |
| 5615 | start = name; |
| 5616 | for (p = name; *p; p++) { |
| 5617 | if (*p == '*' || *p == '?') |
| 5618 | metaflag = 1; |
| 5619 | else if (*p == '[') { |
| 5620 | char *q = p + 1; |
| 5621 | if (*q == '!') |
| 5622 | q++; |
| 5623 | for (;;) { |
| 5624 | if (*q == '\\') |
| 5625 | q++; |
| 5626 | if (*q == '/' || *q == '\0') |
| 5627 | break; |
| 5628 | if (*++q == ']') { |
| 5629 | metaflag = 1; |
| 5630 | break; |
| 5631 | } |
| 5632 | } |
| 5633 | } else if (*p == '\\') |
| 5634 | p++; |
| 5635 | else if (*p == '/') { |
| 5636 | if (metaflag) |
| 5637 | goto out; |
| 5638 | start = p + 1; |
| 5639 | } |
| 5640 | } |
| 5641 | out: |
| 5642 | if (metaflag == 0) { /* we've reached the end of the file name */ |
| 5643 | if (enddir != expdir) |
| 5644 | metaflag++; |
| 5645 | p = name; |
| 5646 | do { |
| 5647 | if (*p == '\\') |
| 5648 | p++; |
| 5649 | *enddir++ = *p; |
| 5650 | } while (*p++); |
Glenn L McGrath | 005f83a | 2003-09-01 08:53:32 +0000 | [diff] [blame] | 5651 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5652 | addfname(expdir); |
| 5653 | return; |
| 5654 | } |
| 5655 | endname = p; |
| 5656 | if (name < start) { |
| 5657 | p = name; |
| 5658 | do { |
| 5659 | if (*p == '\\') |
| 5660 | p++; |
| 5661 | *enddir++ = *p++; |
| 5662 | } while (p < start); |
| 5663 | } |
| 5664 | if (enddir == expdir) { |
| 5665 | cp = "."; |
| 5666 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 5667 | cp = "/"; |
| 5668 | } else { |
| 5669 | cp = expdir; |
| 5670 | enddir[-1] = '\0'; |
| 5671 | } |
| 5672 | if ((dirp = opendir(cp)) == NULL) |
| 5673 | return; |
| 5674 | if (enddir != expdir) |
| 5675 | enddir[-1] = '/'; |
| 5676 | if (*endname == 0) { |
| 5677 | atend = 1; |
| 5678 | } else { |
| 5679 | atend = 0; |
| 5680 | *endname++ = '\0'; |
| 5681 | } |
| 5682 | matchdot = 0; |
| 5683 | p = start; |
| 5684 | if (*p == '\\') |
| 5685 | p++; |
| 5686 | if (*p == '.') |
| 5687 | matchdot++; |
| 5688 | while (! intpending && (dp = readdir(dirp)) != NULL) { |
| 5689 | if (dp->d_name[0] == '.' && ! matchdot) |
| 5690 | continue; |
| 5691 | if (pmatch(start, dp->d_name)) { |
| 5692 | if (atend) { |
| 5693 | scopy(dp->d_name, enddir); |
| 5694 | addfname(expdir); |
| 5695 | } else { |
| 5696 | for (p = enddir, cp = dp->d_name; |
| 5697 | (*p++ = *cp++) != '\0';) |
| 5698 | continue; |
| 5699 | p[-1] = '/'; |
| 5700 | expmeta(p, endname); |
| 5701 | } |
| 5702 | } |
| 5703 | } |
| 5704 | closedir(dirp); |
| 5705 | if (! atend) |
| 5706 | endname[-1] = '/'; |
| 5707 | } |
| 5708 | |
| 5709 | /* |
| 5710 | * Sort the results of file name expansion. It calculates the number of |
| 5711 | * strings to sort and then calls msort (short for merge sort) to do the |
| 5712 | * work. |
| 5713 | */ |
| 5714 | |
| 5715 | static struct strlist * |
| 5716 | expsort(struct strlist *str) |
| 5717 | { |
| 5718 | int len; |
| 5719 | struct strlist *sp; |
| 5720 | |
| 5721 | len = 0; |
| 5722 | for (sp = str ; sp ; sp = sp->next) |
| 5723 | len++; |
| 5724 | return msort(str, len); |
| 5725 | } |
| 5726 | |
| 5727 | |
| 5728 | static struct strlist * |
| 5729 | msort(struct strlist *list, int len) |
| 5730 | { |
| 5731 | struct strlist *p, *q = NULL; |
| 5732 | struct strlist **lpp; |
| 5733 | int half; |
| 5734 | int n; |
| 5735 | |
| 5736 | if (len <= 1) |
| 5737 | return list; |
| 5738 | half = len >> 1; |
| 5739 | p = list; |
| 5740 | for (n = half ; --n >= 0 ; ) { |
| 5741 | q = p; |
| 5742 | p = p->next; |
| 5743 | } |
| 5744 | q->next = NULL; /* terminate first half of list */ |
| 5745 | q = msort(list, half); /* sort first half of list */ |
| 5746 | p = msort(p, len - half); /* sort second half */ |
| 5747 | lpp = &list; |
| 5748 | for (;;) { |
| 5749 | #ifdef CONFIG_LOCALE_SUPPORT |
| 5750 | if (strcoll(p->text, q->text) < 0) |
| 5751 | #else |
| 5752 | if (strcmp(p->text, q->text) < 0) |
| 5753 | #endif |
| 5754 | { |
| 5755 | *lpp = p; |
| 5756 | lpp = &p->next; |
| 5757 | if ((p = *lpp) == NULL) { |
| 5758 | *lpp = q; |
| 5759 | break; |
| 5760 | } |
| 5761 | } else { |
| 5762 | *lpp = q; |
| 5763 | lpp = &q->next; |
| 5764 | if ((q = *lpp) == NULL) { |
| 5765 | *lpp = p; |
| 5766 | break; |
| 5767 | } |
| 5768 | } |
| 5769 | } |
| 5770 | return list; |
| 5771 | } |
| 5772 | |
| 5773 | |
| 5774 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5775 | * Returns true if the pattern matches the string. |
| 5776 | */ |
| 5777 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5778 | static inline int |
| 5779 | patmatch(char *pattern, const char *string) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5780 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5781 | return pmatch(preglob(pattern, 0, 0), string); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5782 | } |
| 5783 | |
| 5784 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5785 | /* |
| 5786 | * Remove any CTLESC characters from a string. |
| 5787 | */ |
| 5788 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5789 | static char * |
| 5790 | _rmescapes(char *str, int flag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5791 | { |
| 5792 | char *p, *q, *r; |
| 5793 | static const char qchars[] = { CTLESC, CTLQUOTEMARK, 0 }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5794 | unsigned inquotes; |
| 5795 | int notescaped; |
| 5796 | int globbing; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5797 | |
| 5798 | p = strpbrk(str, qchars); |
| 5799 | if (!p) { |
| 5800 | return str; |
| 5801 | } |
| 5802 | q = p; |
| 5803 | r = str; |
| 5804 | if (flag & RMESCAPE_ALLOC) { |
| 5805 | size_t len = p - str; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5806 | size_t fulllen = len + strlen(p) + 1; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 5807 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5808 | if (flag & RMESCAPE_GROW) { |
| 5809 | r = makestrspace(fulllen, expdest); |
| 5810 | } else if (flag & RMESCAPE_HEAP) { |
| 5811 | r = ckmalloc(fulllen); |
| 5812 | } else { |
| 5813 | r = stalloc(fulllen); |
| 5814 | } |
| 5815 | q = r; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5816 | if (len > 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5817 | q = mempcpy(q, str, len); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5818 | } |
| 5819 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5820 | inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED; |
| 5821 | globbing = flag & RMESCAPE_GLOB; |
| 5822 | notescaped = globbing; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5823 | while (*p) { |
| 5824 | if (*p == CTLQUOTEMARK) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5825 | inquotes = ~inquotes; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5826 | p++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5827 | notescaped = globbing; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5828 | continue; |
| 5829 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5830 | if (*p == '\\') { |
| 5831 | /* naked back slash */ |
| 5832 | notescaped = 0; |
| 5833 | goto copy; |
| 5834 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5835 | if (*p == CTLESC) { |
| 5836 | p++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5837 | if (notescaped && inquotes && *p != '/') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5838 | *q++ = '\\'; |
| 5839 | } |
| 5840 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5841 | notescaped = globbing; |
| 5842 | copy: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5843 | *q++ = *p++; |
| 5844 | } |
| 5845 | *q = '\0'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5846 | if (flag & RMESCAPE_GROW) { |
| 5847 | expdest = r; |
| 5848 | STADJUST(q - r + 1, expdest); |
| 5849 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5850 | return r; |
| 5851 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5852 | |
| 5853 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5854 | /* |
| 5855 | * See if a pattern matches in a case statement. |
| 5856 | */ |
| 5857 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5858 | int |
| 5859 | casematch(union node *pattern, char *val) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 5860 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5861 | struct stackmark smark; |
| 5862 | int result; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5863 | |
| 5864 | setstackmark(&smark); |
| 5865 | argbackq = pattern->narg.backquote; |
| 5866 | STARTSTACKSTR(expdest); |
| 5867 | ifslastp = NULL; |
| 5868 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5869 | STACKSTRNUL(expdest); |
| 5870 | result = patmatch(stackblock(), val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5871 | popstackmark(&smark); |
| 5872 | return result; |
| 5873 | } |
| 5874 | |
| 5875 | /* |
| 5876 | * Our own itoa(). |
| 5877 | */ |
| 5878 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5879 | static int |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 5880 | cvtnum(arith_t num) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 5881 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5882 | int len; |
| 5883 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5884 | expdest = makestrspace(32, expdest); |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 5885 | #ifdef CONFIG_ASH_MATH_SUPPORT_64 |
| 5886 | len = fmtstr(expdest, 32, "%lld", (long long) num); |
| 5887 | #else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5888 | len = fmtstr(expdest, 32, "%ld", num); |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 5889 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5890 | STADJUST(len, expdest); |
| 5891 | return len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5892 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 5893 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5894 | static void |
| 5895 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5896 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5897 | const char *msg; |
| 5898 | const char *tail; |
| 5899 | |
| 5900 | tail = nullstr; |
| 5901 | msg = "parameter not set"; |
| 5902 | if (umsg) { |
| 5903 | if (*end == CTLENDVAR) { |
| 5904 | if (varflags & VSNUL) |
| 5905 | tail = " or null"; |
| 5906 | } else |
| 5907 | msg = umsg; |
| 5908 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 5909 | sh_error("%.*s: %s%s", end - var - 1, var, msg, tail); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5910 | } |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5911 | |
| 5912 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 5913 | /* input.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5914 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5915 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 5916 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5917 | */ |
| 5918 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5919 | #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5920 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5921 | static void pushfile(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5922 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 5923 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5924 | * Read a character from the script, returning PEOF on end of file. |
| 5925 | * Nul characters in the input are silently discarded. |
| 5926 | */ |
| 5927 | |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 5928 | |
| 5929 | #define pgetc_as_macro() (--parsenleft >= 0? SC2INT(*parsenextc++) : preadbuffer()) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5930 | |
| 5931 | #ifdef CONFIG_ASH_OPTIMIZE_FOR_SIZE |
| 5932 | #define pgetc_macro() pgetc() |
| 5933 | static int |
| 5934 | pgetc(void) |
| 5935 | { |
| 5936 | return pgetc_as_macro(); |
| 5937 | } |
| 5938 | #else |
| 5939 | #define pgetc_macro() pgetc_as_macro() |
| 5940 | static int |
| 5941 | pgetc(void) |
| 5942 | { |
| 5943 | return pgetc_macro(); |
| 5944 | } |
| 5945 | #endif |
| 5946 | |
| 5947 | |
| 5948 | /* |
| 5949 | * Same as pgetc(), but ignores PEOA. |
| 5950 | */ |
| 5951 | #ifdef CONFIG_ASH_ALIAS |
| 5952 | static int pgetc2(void) |
| 5953 | { |
| 5954 | int c; |
| 5955 | |
| 5956 | do { |
| 5957 | c = pgetc_macro(); |
| 5958 | } while (c == PEOA); |
| 5959 | return c; |
| 5960 | } |
| 5961 | #else |
| 5962 | static inline int pgetc2(void) |
| 5963 | { |
| 5964 | return pgetc_macro(); |
| 5965 | } |
| 5966 | #endif |
| 5967 | |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 5968 | /* |
| 5969 | * Read a line from the script. |
| 5970 | */ |
| 5971 | |
| 5972 | static inline char * |
| 5973 | pfgets(char *line, int len) |
| 5974 | { |
| 5975 | char *p = line; |
| 5976 | int nleft = len; |
| 5977 | int c; |
| 5978 | |
| 5979 | while (--nleft > 0) { |
| 5980 | c = pgetc2(); |
| 5981 | if (c == PEOF) { |
| 5982 | if (p == line) |
| 5983 | return NULL; |
| 5984 | break; |
| 5985 | } |
| 5986 | *p++ = c; |
| 5987 | if (c == '\n') |
| 5988 | break; |
| 5989 | } |
| 5990 | *p = '\0'; |
| 5991 | return line; |
| 5992 | } |
| 5993 | |
| 5994 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 5995 | |
| 5996 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
"Vladimir N. Oleynik" | 11d7c52 | 2005-09-26 13:24:45 +0000 | [diff] [blame] | 5997 | #ifdef CONFIG_ASH_EXPAND_PRMT |
| 5998 | static char *cmdedit_prompt; |
| 5999 | #else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6000 | static const char *cmdedit_prompt; |
"Vladimir N. Oleynik" | 11d7c52 | 2005-09-26 13:24:45 +0000 | [diff] [blame] | 6001 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6002 | static inline void putprompt(const char *s) |
| 6003 | { |
"Vladimir N. Oleynik" | 11d7c52 | 2005-09-26 13:24:45 +0000 | [diff] [blame] | 6004 | #ifdef CONFIG_ASH_EXPAND_PRMT |
| 6005 | free(cmdedit_prompt); |
| 6006 | cmdedit_prompt = bb_xstrdup(s); |
| 6007 | #else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6008 | cmdedit_prompt = s; |
"Vladimir N. Oleynik" | 11d7c52 | 2005-09-26 13:24:45 +0000 | [diff] [blame] | 6009 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6010 | } |
| 6011 | #else |
| 6012 | static inline void putprompt(const char *s) |
| 6013 | { |
| 6014 | out2str(s); |
| 6015 | } |
| 6016 | #endif |
| 6017 | |
| 6018 | static inline int |
| 6019 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6020 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 6021 | int nr; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6022 | char *buf = parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 6023 | parsenextc = buf; |
| 6024 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6025 | retry: |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 6026 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6027 | if (!iflag || parsefile->fd) |
| 6028 | nr = safe_read(parsefile->fd, buf, BUFSIZ - 1); |
| 6029 | else { |
Eric Andersen | 4a79c0e | 2004-09-08 10:01:07 +0000 | [diff] [blame] | 6030 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | fa06a77 | 2004-02-06 10:33:19 +0000 | [diff] [blame] | 6031 | cmdedit_path_lookup = pathval(); |
Eric Andersen | 4a79c0e | 2004-09-08 10:01:07 +0000 | [diff] [blame] | 6032 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6033 | nr = cmdedit_read_input((char *) cmdedit_prompt, buf); |
| 6034 | if(nr == 0) { |
| 6035 | /* Ctrl+C presend */ |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 6036 | if(trap[SIGINT]) { |
| 6037 | buf[0] = '\n'; |
| 6038 | buf[1] = 0; |
| 6039 | raise(SIGINT); |
| 6040 | return 1; |
| 6041 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6042 | goto retry; |
| 6043 | } |
Eric Andersen | cb01bb1 | 2004-08-19 18:22:13 +0000 | [diff] [blame] | 6044 | if(nr < 0 && errno == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6045 | /* Ctrl+D presend */ |
| 6046 | nr = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 6047 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6048 | } |
| 6049 | #else |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 6050 | nr = safe_read(parsefile->fd, buf, BUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6051 | #endif |
| 6052 | |
| 6053 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6054 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
| 6055 | int flags = fcntl(0, F_GETFL, 0); |
| 6056 | if (flags >= 0 && flags & O_NONBLOCK) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6057 | flags &=~ O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6058 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 6059 | out2str("sh: turning off NDELAY mode\n"); |
| 6060 | goto retry; |
| 6061 | } |
| 6062 | } |
| 6063 | } |
| 6064 | } |
| 6065 | return nr; |
| 6066 | } |
| 6067 | |
| 6068 | /* |
| 6069 | * Refill the input buffer and return the next input character: |
| 6070 | * |
| 6071 | * 1) If a string was pushed back on the input, pop it; |
| 6072 | * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading |
| 6073 | * from a string so we can't refill the buffer, return EOF. |
| 6074 | * 3) If the is more stuff in this buffer, use it else call read to fill it. |
| 6075 | * 4) Process input up to the next newline, deleting nul characters. |
| 6076 | */ |
| 6077 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6078 | int |
| 6079 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6080 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6081 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6082 | int more; |
| 6083 | char savec; |
| 6084 | |
| 6085 | while (parsefile->strpush) { |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6086 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6087 | if (parsenleft == -1 && parsefile->strpush->ap && |
| 6088 | parsenextc[-1] != ' ' && parsenextc[-1] != '\t') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6089 | return PEOA; |
| 6090 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6091 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6092 | popstring(); |
| 6093 | if (--parsenleft >= 0) |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 6094 | return SC2INT(*parsenextc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6095 | } |
| 6096 | if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) |
| 6097 | return PEOF; |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 6098 | flushall(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6099 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6100 | more = parselleft; |
| 6101 | if (more <= 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6102 | again: |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6103 | if ((more = preadfd()) <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6104 | parselleft = parsenleft = EOF_NLEFT; |
| 6105 | return PEOF; |
| 6106 | } |
| 6107 | } |
| 6108 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6109 | q = parsenextc; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6110 | |
| 6111 | /* delete nul characters */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6112 | for (;;) { |
| 6113 | int c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6114 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6115 | more--; |
| 6116 | c = *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6117 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6118 | if (!c) |
| 6119 | memmove(q, q + 1, more); |
| 6120 | else { |
| 6121 | q++; |
| 6122 | if (c == '\n') { |
| 6123 | parsenleft = q - parsenextc - 1; |
| 6124 | break; |
| 6125 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6126 | } |
| 6127 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6128 | if (more <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6129 | parsenleft = q - parsenextc - 1; |
| 6130 | if (parsenleft < 0) |
| 6131 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6132 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6133 | } |
| 6134 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6135 | parselleft = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6136 | |
| 6137 | savec = *q; |
| 6138 | *q = '\0'; |
| 6139 | |
| 6140 | if (vflag) { |
| 6141 | out2str(parsenextc); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6142 | } |
| 6143 | |
| 6144 | *q = savec; |
| 6145 | |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 6146 | return SC2INT(*parsenextc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6147 | } |
| 6148 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6149 | /* |
| 6150 | * Undo the last call to pgetc. Only one character may be pushed back. |
| 6151 | * PEOF may be pushed back. |
| 6152 | */ |
| 6153 | |
| 6154 | void |
| 6155 | pungetc(void) |
| 6156 | { |
| 6157 | parsenleft++; |
| 6158 | parsenextc--; |
| 6159 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6160 | |
| 6161 | /* |
| 6162 | * Push a string back onto the input at this current parsefile level. |
| 6163 | * We handle aliases this way. |
| 6164 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6165 | void |
| 6166 | pushstring(char *s, void *ap) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6167 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6168 | struct strpush *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6169 | size_t len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6170 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6171 | len = strlen(s); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6172 | INTOFF; |
| 6173 | /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/ |
| 6174 | if (parsefile->strpush) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6175 | sp = ckmalloc(sizeof (struct strpush)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6176 | sp->prev = parsefile->strpush; |
| 6177 | parsefile->strpush = sp; |
| 6178 | } else |
| 6179 | sp = parsefile->strpush = &(parsefile->basestrpush); |
| 6180 | sp->prevstring = parsenextc; |
| 6181 | sp->prevnleft = parsenleft; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 6182 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6183 | sp->ap = (struct alias *)ap; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6184 | if (ap) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6185 | ((struct alias *)ap)->flag |= ALIASINUSE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6186 | sp->string = s; |
| 6187 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6188 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6189 | parsenextc = s; |
| 6190 | parsenleft = len; |
| 6191 | INTON; |
| 6192 | } |
| 6193 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6194 | void |
| 6195 | popstring(void) |
| 6196 | { |
| 6197 | struct strpush *sp = parsefile->strpush; |
| 6198 | |
| 6199 | INTOFF; |
| 6200 | #ifdef CONFIG_ASH_ALIAS |
| 6201 | if (sp->ap) { |
| 6202 | if (parsenextc[-1] == ' ' || parsenextc[-1] == '\t') { |
| 6203 | checkkwd |= CHKALIAS; |
| 6204 | } |
| 6205 | if (sp->string != sp->ap->val) { |
| 6206 | ckfree(sp->string); |
| 6207 | } |
| 6208 | sp->ap->flag &= ~ALIASINUSE; |
| 6209 | if (sp->ap->flag & ALIASDEAD) { |
| 6210 | unalias(sp->ap->name); |
| 6211 | } |
| 6212 | } |
| 6213 | #endif |
| 6214 | parsenextc = sp->prevstring; |
| 6215 | parsenleft = sp->prevnleft; |
| 6216 | /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/ |
| 6217 | parsefile->strpush = sp->prev; |
| 6218 | if (sp != &(parsefile->basestrpush)) |
| 6219 | ckfree(sp); |
| 6220 | INTON; |
| 6221 | } |
| 6222 | |
| 6223 | /* |
| 6224 | * Set the input to take input from a file. If push is set, push the |
| 6225 | * old input onto the stack first. |
| 6226 | */ |
| 6227 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6228 | static int |
| 6229 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6230 | { |
| 6231 | int fd; |
| 6232 | int fd2; |
| 6233 | |
| 6234 | INTOFF; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6235 | if ((fd = open(fname, O_RDONLY)) < 0) { |
| 6236 | if (flags & INPUT_NOFILE_OK) |
| 6237 | goto out; |
| 6238 | sh_error("Can't open %s", fname); |
| 6239 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6240 | if (fd < 10) { |
| 6241 | fd2 = copyfd(fd, 10); |
| 6242 | close(fd); |
| 6243 | if (fd2 < 0) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6244 | sh_error("Out of file descriptors"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6245 | fd = fd2; |
| 6246 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6247 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
| 6248 | out: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6249 | INTON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6250 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6251 | } |
| 6252 | |
| 6253 | |
| 6254 | /* |
| 6255 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 6256 | * interrupts off. |
| 6257 | */ |
| 6258 | |
| 6259 | static void |
| 6260 | setinputfd(int fd, int push) |
| 6261 | { |
| 6262 | (void) fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 6263 | if (push) { |
| 6264 | pushfile(); |
| 6265 | parsefile->buf = 0; |
| 6266 | } |
| 6267 | parsefile->fd = fd; |
| 6268 | if (parsefile->buf == NULL) |
| 6269 | parsefile->buf = ckmalloc(IBUFSIZ); |
| 6270 | parselleft = parsenleft = 0; |
| 6271 | plinno = 1; |
| 6272 | } |
| 6273 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6274 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6275 | /* |
| 6276 | * Like setinputfile, but takes input from a string. |
| 6277 | */ |
| 6278 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6279 | static void |
| 6280 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6281 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6282 | INTOFF; |
| 6283 | pushfile(); |
| 6284 | parsenextc = string; |
| 6285 | parsenleft = strlen(string); |
| 6286 | parsefile->buf = NULL; |
| 6287 | plinno = 1; |
| 6288 | INTON; |
| 6289 | } |
| 6290 | |
| 6291 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6292 | /* |
| 6293 | * To handle the "." command, a stack of input files is used. Pushfile |
| 6294 | * adds a new entry to the stack and popfile restores the previous level. |
| 6295 | */ |
| 6296 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6297 | static void |
| 6298 | pushfile(void) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 6299 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6300 | struct parsefile *pf; |
| 6301 | |
| 6302 | parsefile->nleft = parsenleft; |
| 6303 | parsefile->lleft = parselleft; |
| 6304 | parsefile->nextc = parsenextc; |
| 6305 | parsefile->linno = plinno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6306 | pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6307 | pf->prev = parsefile; |
| 6308 | pf->fd = -1; |
| 6309 | pf->strpush = NULL; |
| 6310 | pf->basestrpush.prev = NULL; |
| 6311 | parsefile = pf; |
| 6312 | } |
| 6313 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6314 | |
| 6315 | static void |
| 6316 | popfile(void) |
| 6317 | { |
| 6318 | struct parsefile *pf = parsefile; |
| 6319 | |
| 6320 | INTOFF; |
| 6321 | if (pf->fd >= 0) |
| 6322 | close(pf->fd); |
| 6323 | if (pf->buf) |
| 6324 | ckfree(pf->buf); |
| 6325 | while (pf->strpush) |
| 6326 | popstring(); |
| 6327 | parsefile = pf->prev; |
| 6328 | ckfree(pf); |
| 6329 | parsenleft = parsefile->nleft; |
| 6330 | parselleft = parsefile->lleft; |
| 6331 | parsenextc = parsefile->nextc; |
| 6332 | plinno = parsefile->linno; |
| 6333 | INTON; |
| 6334 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6335 | |
| 6336 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6337 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6338 | * Return to top level. |
| 6339 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6340 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6341 | static void |
| 6342 | popallfiles(void) |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 6343 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6344 | while (parsefile != &basepf) |
| 6345 | popfile(); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6346 | } |
| 6347 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6348 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6349 | /* |
| 6350 | * Close the file(s) that the shell is reading commands from. Called |
| 6351 | * after a fork is done. |
| 6352 | */ |
| 6353 | |
| 6354 | static void |
| 6355 | closescript(void) |
| 6356 | { |
| 6357 | popallfiles(); |
| 6358 | if (parsefile->fd > 0) { |
| 6359 | close(parsefile->fd); |
| 6360 | parsefile->fd = 0; |
| 6361 | } |
| 6362 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6363 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6364 | /* jobs.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6365 | |
| 6366 | /* mode flags for set_curjob */ |
| 6367 | #define CUR_DELETE 2 |
| 6368 | #define CUR_RUNNING 1 |
| 6369 | #define CUR_STOPPED 0 |
| 6370 | |
| 6371 | /* mode flags for dowait */ |
| 6372 | #define DOWAIT_NORMAL 0 |
| 6373 | #define DOWAIT_BLOCK 1 |
| 6374 | |
| 6375 | /* array of jobs */ |
| 6376 | static struct job *jobtab; |
| 6377 | /* size of array */ |
| 6378 | static unsigned njobs; |
| 6379 | #if JOBS |
| 6380 | /* pgrp of shell on invocation */ |
| 6381 | static int initialpgrp; |
| 6382 | static int ttyfd = -1; |
| 6383 | #endif |
| 6384 | /* current job */ |
| 6385 | static struct job *curjob; |
| 6386 | /* number of presumed living untracked jobs */ |
| 6387 | static int jobless; |
| 6388 | |
| 6389 | static void set_curjob(struct job *, unsigned); |
| 6390 | #if JOBS |
| 6391 | static int restartjob(struct job *, int); |
| 6392 | static void xtcsetpgrp(int, pid_t); |
| 6393 | static char *commandtext(union node *); |
| 6394 | static void cmdlist(union node *, int); |
| 6395 | static void cmdtxt(union node *); |
| 6396 | static void cmdputs(const char *); |
| 6397 | static void showpipe(struct job *, FILE *); |
| 6398 | #endif |
| 6399 | static int sprint_status(char *, int, int); |
| 6400 | static void freejob(struct job *); |
| 6401 | static struct job *getjob(const char *, int); |
| 6402 | static struct job *growjobtab(void); |
| 6403 | static void forkchild(struct job *, union node *, int); |
| 6404 | static void forkparent(struct job *, union node *, int, pid_t); |
| 6405 | static int dowait(int, struct job *); |
| 6406 | static int getstatus(struct job *); |
| 6407 | |
| 6408 | static void |
| 6409 | set_curjob(struct job *jp, unsigned mode) |
| 6410 | { |
| 6411 | struct job *jp1; |
| 6412 | struct job **jpp, **curp; |
| 6413 | |
| 6414 | /* first remove from list */ |
| 6415 | jpp = curp = &curjob; |
| 6416 | do { |
| 6417 | jp1 = *jpp; |
| 6418 | if (jp1 == jp) |
| 6419 | break; |
| 6420 | jpp = &jp1->prev_job; |
| 6421 | } while (1); |
| 6422 | *jpp = jp1->prev_job; |
| 6423 | |
| 6424 | /* Then re-insert in correct position */ |
| 6425 | jpp = curp; |
| 6426 | switch (mode) { |
| 6427 | default: |
| 6428 | #ifdef DEBUG |
| 6429 | abort(); |
| 6430 | #endif |
| 6431 | case CUR_DELETE: |
| 6432 | /* job being deleted */ |
| 6433 | break; |
| 6434 | case CUR_RUNNING: |
| 6435 | /* newly created job or backgrounded job, |
| 6436 | put after all stopped jobs. */ |
| 6437 | do { |
| 6438 | jp1 = *jpp; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6439 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6440 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 6441 | #endif |
| 6442 | break; |
| 6443 | jpp = &jp1->prev_job; |
| 6444 | } while (1); |
| 6445 | /* FALLTHROUGH */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6446 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6447 | case CUR_STOPPED: |
| 6448 | #endif |
| 6449 | /* newly stopped job - becomes curjob */ |
| 6450 | jp->prev_job = *jpp; |
| 6451 | *jpp = jp; |
| 6452 | break; |
| 6453 | } |
| 6454 | } |
| 6455 | |
| 6456 | #if JOBS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6457 | /* |
| 6458 | * Turn job control on and off. |
| 6459 | * |
| 6460 | * Note: This code assumes that the third arg to ioctl is a character |
| 6461 | * pointer, which is true on Berkeley systems but not System V. Since |
| 6462 | * System V doesn't have job control yet, this isn't a problem now. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6463 | * |
| 6464 | * Called with interrupts off. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6465 | */ |
| 6466 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6467 | void |
| 6468 | setjobctl(int on) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6469 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6470 | int fd; |
| 6471 | int pgrp; |
| 6472 | |
| 6473 | if (on == jobctl || rootshell == 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6474 | return; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6475 | if (on) { |
| 6476 | int ofd; |
| 6477 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 6478 | if (fd < 0) { |
| 6479 | fd += 3; |
| 6480 | while (!isatty(fd) && --fd >= 0) |
| 6481 | ; |
| 6482 | } |
| 6483 | fd = fcntl(fd, F_DUPFD, 10); |
| 6484 | close(ofd); |
| 6485 | if (fd < 0) |
| 6486 | goto out; |
| 6487 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 6488 | do { /* while we are in the background */ |
| 6489 | if ((pgrp = tcgetpgrp(fd)) < 0) { |
| 6490 | out: |
| 6491 | sh_warnx("can't access tty; job control turned off"); |
| 6492 | mflag = on = 0; |
| 6493 | goto close; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6494 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6495 | if (pgrp == getpgrp()) |
Glenn L McGrath | 7040ecc | 2003-01-06 16:27:07 +0000 | [diff] [blame] | 6496 | break; |
| 6497 | killpg(0, SIGTTIN); |
| 6498 | } while (1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6499 | initialpgrp = pgrp; |
| 6500 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6501 | setsignal(SIGTSTP); |
| 6502 | setsignal(SIGTTOU); |
| 6503 | setsignal(SIGTTIN); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6504 | pgrp = rootpid; |
| 6505 | setpgid(0, pgrp); |
| 6506 | xtcsetpgrp(fd, pgrp); |
| 6507 | } else { |
| 6508 | /* turning job control off */ |
| 6509 | fd = ttyfd; |
| 6510 | pgrp = initialpgrp; |
| 6511 | xtcsetpgrp(fd, pgrp); |
| 6512 | setpgid(0, pgrp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6513 | setsignal(SIGTSTP); |
| 6514 | setsignal(SIGTTOU); |
| 6515 | setsignal(SIGTTIN); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6516 | close: |
| 6517 | close(fd); |
| 6518 | fd = -1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6519 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6520 | ttyfd = fd; |
| 6521 | jobctl = on; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6522 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6523 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6524 | static int |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6525 | killcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6526 | { |
| 6527 | int signo = -1; |
| 6528 | int list = 0; |
| 6529 | int i; |
| 6530 | pid_t pid; |
| 6531 | struct job *jp; |
| 6532 | |
| 6533 | if (argc <= 1) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6534 | usage: |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6535 | sh_error( |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6536 | "Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or\n" |
| 6537 | "kill -l [exitstatus]" |
| 6538 | ); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6539 | } |
| 6540 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6541 | if (**++argv == '-') { |
| 6542 | signo = decode_signal(*argv + 1, 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6543 | if (signo < 0) { |
| 6544 | int c; |
| 6545 | |
| 6546 | while ((c = nextopt("ls:")) != '\0') |
| 6547 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6548 | default: |
| 6549 | #ifdef DEBUG |
| 6550 | abort(); |
| 6551 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6552 | case 'l': |
| 6553 | list = 1; |
| 6554 | break; |
| 6555 | case 's': |
| 6556 | signo = decode_signal(optionarg, 1); |
| 6557 | if (signo < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6558 | sh_error( |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6559 | "invalid signal number or name: %s", |
| 6560 | optionarg |
| 6561 | ); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6562 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6563 | break; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 6564 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6565 | argv = argptr; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6566 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6567 | argv++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6568 | } |
| 6569 | |
| 6570 | if (!list && signo < 0) |
| 6571 | signo = SIGTERM; |
| 6572 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6573 | if ((signo < 0 || !*argv) ^ list) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6574 | goto usage; |
| 6575 | } |
| 6576 | |
| 6577 | if (list) { |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 6578 | const char *name; |
| 6579 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6580 | if (!*argv) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6581 | for (i = 1; i < NSIG; i++) { |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 6582 | name = u_signal_names(0, &i, 1); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 6583 | if (name) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6584 | out1fmt(snlfmt, name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6585 | } |
| 6586 | return 0; |
| 6587 | } |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 6588 | name = u_signal_names(*argptr, &signo, -1); |
| 6589 | if (name) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6590 | out1fmt(snlfmt, name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6591 | else |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6592 | sh_error("invalid signal number or exit status: %s", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6593 | return 0; |
| 6594 | } |
| 6595 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6596 | i = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6597 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6598 | if (**argv == '%') { |
| 6599 | jp = getjob(*argv, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6600 | pid = -jp->ps[0].pid; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 6601 | } else { |
| 6602 | pid = **argv == '-' ? |
| 6603 | -number(*argv + 1) : number(*argv); |
| 6604 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6605 | if (kill(pid, signo) != 0) { |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 6606 | sh_warnx("(%d) - %m", pid); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6607 | i = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6608 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6609 | } while (*++argv); |
| 6610 | |
| 6611 | return i; |
| 6612 | } |
| 6613 | #endif /* JOBS */ |
| 6614 | |
| 6615 | #if defined(JOBS) || defined(DEBUG) |
| 6616 | static int |
| 6617 | jobno(const struct job *jp) |
| 6618 | { |
| 6619 | return jp - jobtab + 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6620 | } |
| 6621 | #endif |
| 6622 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 6623 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6624 | static int |
| 6625 | fgcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6626 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6627 | struct job *jp; |
| 6628 | FILE *out; |
| 6629 | int mode; |
| 6630 | int retval; |
| 6631 | |
| 6632 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 6633 | nextopt(nullstr); |
| 6634 | argv = argptr; |
| 6635 | out = stdout; |
| 6636 | do { |
| 6637 | jp = getjob(*argv, 1); |
| 6638 | if (mode == FORK_BG) { |
| 6639 | set_curjob(jp, CUR_RUNNING); |
| 6640 | fprintf(out, "[%d] ", jobno(jp)); |
| 6641 | } |
| 6642 | outstr(jp->ps->cmd, out); |
| 6643 | showpipe(jp, out); |
| 6644 | retval = restartjob(jp, mode); |
| 6645 | } while (*argv && *++argv); |
| 6646 | return retval; |
| 6647 | } |
| 6648 | |
| 6649 | static int bgcmd(int, char **) __attribute__((__alias__("fgcmd"))); |
| 6650 | |
| 6651 | |
| 6652 | static int |
| 6653 | restartjob(struct job *jp, int mode) |
| 6654 | { |
| 6655 | struct procstat *ps; |
| 6656 | int i; |
| 6657 | int status; |
| 6658 | pid_t pgid; |
| 6659 | |
| 6660 | INTOFF; |
| 6661 | if (jp->state == JOBDONE) |
| 6662 | goto out; |
| 6663 | jp->state = JOBRUNNING; |
| 6664 | pgid = jp->ps->pid; |
| 6665 | if (mode == FORK_FG) |
| 6666 | xtcsetpgrp(ttyfd, pgid); |
| 6667 | killpg(pgid, SIGCONT); |
| 6668 | ps = jp->ps; |
| 6669 | i = jp->nprocs; |
| 6670 | do { |
| 6671 | if (WIFSTOPPED(ps->status)) { |
| 6672 | ps->status = -1; |
| 6673 | } |
| 6674 | } while (ps++, --i); |
| 6675 | out: |
| 6676 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 6677 | INTON; |
| 6678 | return status; |
| 6679 | } |
| 6680 | #endif |
| 6681 | |
| 6682 | static int |
| 6683 | sprint_status(char *s, int status, int sigonly) |
| 6684 | { |
| 6685 | int col; |
| 6686 | int st; |
| 6687 | |
| 6688 | col = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6689 | if (!WIFEXITED(status)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6690 | #if JOBS |
Glenn L McGrath | a3822de | 2003-09-15 14:42:39 +0000 | [diff] [blame] | 6691 | if (WIFSTOPPED(status)) |
| 6692 | st = WSTOPSIG(status); |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 6693 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6694 | #endif |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 6695 | st = WTERMSIG(status); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6696 | if (sigonly) { |
Glenn L McGrath | 4ddddd1 | 2003-11-25 20:45:38 +0000 | [diff] [blame] | 6697 | if (st == SIGINT || st == SIGPIPE) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 6698 | goto out; |
| 6699 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6700 | if (WIFSTOPPED(status)) |
| 6701 | goto out; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 6702 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6703 | } |
| 6704 | st &= 0x7f; |
Glenn L McGrath | 5c2c8ec | 2003-11-14 21:01:26 +0000 | [diff] [blame] | 6705 | col = fmtstr(s, 32, strsignal(st)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6706 | if (WCOREDUMP(status)) { |
| 6707 | col += fmtstr(s + col, 16, " (core dumped)"); |
| 6708 | } |
| 6709 | } else if (!sigonly) { |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 6710 | st = WEXITSTATUS(status); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6711 | if (st) |
| 6712 | col = fmtstr(s, 16, "Done(%d)", st); |
| 6713 | else |
| 6714 | col = fmtstr(s, 16, "Done"); |
| 6715 | } |
| 6716 | |
| 6717 | out: |
| 6718 | return col; |
| 6719 | } |
| 6720 | |
| 6721 | #if JOBS |
| 6722 | static void |
| 6723 | showjob(FILE *out, struct job *jp, int mode) |
| 6724 | { |
| 6725 | struct procstat *ps; |
| 6726 | struct procstat *psend; |
| 6727 | int col; |
| 6728 | int indent; |
| 6729 | char s[80]; |
| 6730 | |
| 6731 | ps = jp->ps; |
| 6732 | |
| 6733 | if (mode & SHOW_PGID) { |
| 6734 | /* just output process (group) id of pipeline */ |
| 6735 | fprintf(out, "%d\n", ps->pid); |
| 6736 | return; |
| 6737 | } |
| 6738 | |
| 6739 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
| 6740 | indent = col; |
| 6741 | |
| 6742 | if (jp == curjob) |
| 6743 | s[col - 2] = '+'; |
| 6744 | else if (curjob && jp == curjob->prev_job) |
| 6745 | s[col - 2] = '-'; |
| 6746 | |
| 6747 | if (mode & SHOW_PID) |
| 6748 | col += fmtstr(s + col, 16, "%d ", ps->pid); |
| 6749 | |
| 6750 | psend = ps + jp->nprocs; |
| 6751 | |
| 6752 | if (jp->state == JOBRUNNING) { |
| 6753 | scopy("Running", s + col); |
| 6754 | col += strlen("Running"); |
| 6755 | } else { |
| 6756 | int status = psend[-1].status; |
| 6757 | #if JOBS |
| 6758 | if (jp->state == JOBSTOPPED) |
| 6759 | status = jp->stopstatus; |
| 6760 | #endif |
| 6761 | col += sprint_status(s + col, status, 0); |
| 6762 | } |
| 6763 | |
| 6764 | goto start; |
| 6765 | |
| 6766 | do { |
| 6767 | /* for each process */ |
| 6768 | col = fmtstr(s, 48, " |\n%*c%d ", indent, ' ', ps->pid) - 3; |
| 6769 | |
| 6770 | start: |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 6771 | fprintf(out, "%s%*c%s", |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6772 | s, 33 - col >= 0 ? 33 - col : 0, ' ', ps->cmd |
| 6773 | ); |
| 6774 | if (!(mode & SHOW_PID)) { |
| 6775 | showpipe(jp, out); |
| 6776 | break; |
| 6777 | } |
| 6778 | if (++ps == psend) { |
| 6779 | outcslow('\n', out); |
| 6780 | break; |
| 6781 | } |
| 6782 | } while (1); |
| 6783 | |
| 6784 | jp->changed = 0; |
| 6785 | |
| 6786 | if (jp->state == JOBDONE) { |
| 6787 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 6788 | freejob(jp); |
| 6789 | } |
| 6790 | } |
| 6791 | |
| 6792 | |
| 6793 | static int |
| 6794 | jobscmd(int argc, char **argv) |
| 6795 | { |
| 6796 | int mode, m; |
| 6797 | FILE *out; |
| 6798 | |
| 6799 | mode = 0; |
| 6800 | while ((m = nextopt("lp"))) |
| 6801 | if (m == 'l') |
| 6802 | mode = SHOW_PID; |
| 6803 | else |
| 6804 | mode = SHOW_PGID; |
| 6805 | |
| 6806 | out = stdout; |
| 6807 | argv = argptr; |
| 6808 | if (*argv) |
| 6809 | do |
| 6810 | showjob(out, getjob(*argv,0), mode); |
| 6811 | while (*++argv); |
| 6812 | else |
| 6813 | showjobs(out, mode); |
| 6814 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6815 | return 0; |
| 6816 | } |
| 6817 | |
| 6818 | |
| 6819 | /* |
| 6820 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 6821 | * statuses have changed since the last call to showjobs. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6822 | */ |
| 6823 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6824 | static void |
| 6825 | showjobs(FILE *out, int mode) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6826 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6827 | struct job *jp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6828 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6829 | TRACE(("showjobs(%x) called\n", mode)); |
| 6830 | |
| 6831 | /* If not even one one job changed, there is nothing to do */ |
| 6832 | while (dowait(DOWAIT_NORMAL, NULL) > 0) |
| 6833 | continue; |
| 6834 | |
| 6835 | for (jp = curjob; jp; jp = jp->prev_job) { |
| 6836 | if (!(mode & SHOW_CHANGED) || jp->changed) |
| 6837 | showjob(out, jp, mode); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6838 | } |
| 6839 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6840 | #endif /* JOBS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6841 | |
| 6842 | /* |
| 6843 | * Mark a job structure as unused. |
| 6844 | */ |
| 6845 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6846 | static void |
| 6847 | freejob(struct job *jp) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 6848 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6849 | struct procstat *ps; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6850 | int i; |
| 6851 | |
| 6852 | INTOFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6853 | for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6854 | if (ps->cmd != nullstr) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6855 | ckfree(ps->cmd); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6856 | } |
| 6857 | if (jp->ps != &jp->ps0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6858 | ckfree(jp->ps); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6859 | jp->used = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6860 | set_curjob(jp, CUR_DELETE); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6861 | INTON; |
| 6862 | } |
| 6863 | |
| 6864 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6865 | static int |
| 6866 | waitcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6867 | { |
| 6868 | struct job *job; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6869 | int retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6870 | struct job *jp; |
| 6871 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6872 | EXSIGON(); |
| 6873 | |
| 6874 | nextopt(nullstr); |
| 6875 | retval = 0; |
| 6876 | |
| 6877 | argv = argptr; |
| 6878 | if (!*argv) { |
| 6879 | /* wait for all jobs */ |
| 6880 | for (;;) { |
| 6881 | jp = curjob; |
| 6882 | while (1) { |
| 6883 | if (!jp) { |
| 6884 | /* no running procs */ |
| 6885 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6886 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6887 | if (jp->state == JOBRUNNING) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6888 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6889 | jp->waited = 1; |
| 6890 | jp = jp->prev_job; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6891 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6892 | dowait(DOWAIT_BLOCK, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6893 | } |
| 6894 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6895 | |
| 6896 | retval = 127; |
| 6897 | do { |
| 6898 | if (**argv != '%') { |
| 6899 | pid_t pid = number(*argv); |
| 6900 | job = curjob; |
| 6901 | goto start; |
| 6902 | do { |
| 6903 | if (job->ps[job->nprocs - 1].pid == pid) |
| 6904 | break; |
| 6905 | job = job->prev_job; |
| 6906 | start: |
| 6907 | if (!job) |
| 6908 | goto repeat; |
| 6909 | } while (1); |
| 6910 | } else |
| 6911 | job = getjob(*argv, 0); |
| 6912 | /* loop until process terminated or stopped */ |
| 6913 | while (job->state == JOBRUNNING) |
| 6914 | dowait(DOWAIT_BLOCK, 0); |
| 6915 | job->waited = 1; |
| 6916 | retval = getstatus(job); |
| 6917 | repeat: |
| 6918 | ; |
| 6919 | } while (*++argv); |
| 6920 | |
| 6921 | out: |
| 6922 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6923 | } |
| 6924 | |
| 6925 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6926 | /* |
| 6927 | * Convert a job name to a job structure. |
| 6928 | */ |
| 6929 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6930 | static struct job * |
| 6931 | getjob(const char *name, int getctl) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 6932 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6933 | struct job *jp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6934 | struct job *found; |
| 6935 | const char *err_msg = "No such job: %s"; |
| 6936 | unsigned num; |
| 6937 | int c; |
| 6938 | const char *p; |
| 6939 | char *(*match)(const char *, const char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6940 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6941 | jp = curjob; |
| 6942 | p = name; |
| 6943 | if (!p) |
| 6944 | goto currentjob; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 6945 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6946 | if (*p != '%') |
| 6947 | goto err; |
| 6948 | |
| 6949 | c = *++p; |
| 6950 | if (!c) |
| 6951 | goto currentjob; |
| 6952 | |
| 6953 | if (!p[1]) { |
| 6954 | if (c == '+' || c == '%') { |
| 6955 | currentjob: |
| 6956 | err_msg = "No current job"; |
| 6957 | goto check; |
| 6958 | } else if (c == '-') { |
| 6959 | if (jp) |
| 6960 | jp = jp->prev_job; |
| 6961 | err_msg = "No previous job"; |
| 6962 | check: |
| 6963 | if (!jp) |
| 6964 | goto err; |
| 6965 | goto gotit; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 6966 | } |
| 6967 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 6968 | |
| 6969 | if (is_number(p)) { |
| 6970 | num = atoi(p); |
| 6971 | if (num < njobs) { |
| 6972 | jp = jobtab + num - 1; |
| 6973 | if (jp->used) |
| 6974 | goto gotit; |
| 6975 | goto err; |
| 6976 | } |
| 6977 | } |
| 6978 | |
| 6979 | match = prefix; |
| 6980 | if (*p == '?') { |
| 6981 | match = strstr; |
| 6982 | p++; |
| 6983 | } |
| 6984 | |
| 6985 | found = 0; |
| 6986 | while (1) { |
| 6987 | if (!jp) |
| 6988 | goto err; |
| 6989 | if (match(jp->ps[0].cmd, p)) { |
| 6990 | if (found) |
| 6991 | goto err; |
| 6992 | found = jp; |
| 6993 | err_msg = "%s: ambiguous"; |
| 6994 | } |
| 6995 | jp = jp->prev_job; |
| 6996 | } |
| 6997 | |
| 6998 | gotit: |
| 6999 | #if JOBS |
| 7000 | err_msg = "job %s not created under job control"; |
| 7001 | if (getctl && jp->jobctl == 0) |
| 7002 | goto err; |
| 7003 | #endif |
| 7004 | return jp; |
| 7005 | err: |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7006 | sh_error(err_msg, name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7007 | } |
| 7008 | |
| 7009 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7010 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7011 | * Return a new job structure. |
| 7012 | * Called with interrupts off. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7013 | */ |
| 7014 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7015 | static struct job * |
| 7016 | makejob(union node *node, int nprocs) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7017 | { |
| 7018 | int i; |
| 7019 | struct job *jp; |
| 7020 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7021 | for (i = njobs, jp = jobtab ; ; jp++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7022 | if (--i < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7023 | jp = growjobtab(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7024 | break; |
| 7025 | } |
| 7026 | if (jp->used == 0) |
| 7027 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7028 | if (jp->state != JOBDONE || !jp->waited) |
| 7029 | continue; |
| 7030 | #if JOBS |
| 7031 | if (jobctl) |
| 7032 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7033 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7034 | freejob(jp); |
| 7035 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7036 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7037 | memset(jp, 0, sizeof(*jp)); |
| 7038 | #if JOBS |
| 7039 | if (jobctl) |
| 7040 | jp->jobctl = 1; |
| 7041 | #endif |
| 7042 | jp->prev_job = curjob; |
| 7043 | curjob = jp; |
| 7044 | jp->used = 1; |
| 7045 | jp->ps = &jp->ps0; |
| 7046 | if (nprocs > 1) { |
| 7047 | jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); |
| 7048 | } |
| 7049 | TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs, |
| 7050 | jobno(jp))); |
| 7051 | return jp; |
| 7052 | } |
| 7053 | |
| 7054 | static struct job * |
| 7055 | growjobtab(void) |
| 7056 | { |
| 7057 | size_t len; |
| 7058 | ptrdiff_t offset; |
| 7059 | struct job *jp, *jq; |
| 7060 | |
| 7061 | len = njobs * sizeof(*jp); |
| 7062 | jq = jobtab; |
| 7063 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 7064 | |
| 7065 | offset = (char *)jp - (char *)jq; |
| 7066 | if (offset) { |
| 7067 | /* Relocate pointers */ |
| 7068 | size_t l = len; |
| 7069 | |
| 7070 | jq = (struct job *)((char *)jq + l); |
| 7071 | while (l) { |
| 7072 | l -= sizeof(*jp); |
| 7073 | jq--; |
| 7074 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 7075 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
Glenn L McGrath | 2f325a0 | 2004-08-06 01:49:04 +0000 | [diff] [blame] | 7076 | if (xlikely(joff(jp)->ps == &jq->ps0)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7077 | jmove(joff(jp)->ps); |
| 7078 | if (joff(jp)->prev_job) |
| 7079 | jmove(joff(jp)->prev_job); |
| 7080 | } |
| 7081 | if (curjob) |
| 7082 | jmove(curjob); |
| 7083 | #undef joff |
| 7084 | #undef jmove |
| 7085 | } |
| 7086 | |
| 7087 | njobs += 4; |
| 7088 | jobtab = jp; |
| 7089 | jp = (struct job *)((char *)jp + len); |
| 7090 | jq = jp + 3; |
| 7091 | do { |
| 7092 | jq->used = 0; |
| 7093 | } while (--jq >= jp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7094 | return jp; |
| 7095 | } |
| 7096 | |
| 7097 | |
| 7098 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7099 | * Fork off a subshell. If we are doing job control, give the subshell its |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7100 | * own process group. Jp is a job structure that the job is to be added to. |
| 7101 | * N is the command that will be evaluated by the child. Both jp and n may |
| 7102 | * be NULL. The mode parameter can be one of the following: |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7103 | * FORK_FG - Fork off a foreground process. |
| 7104 | * FORK_BG - Fork off a background process. |
| 7105 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 7106 | * process group even if job control is on. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7107 | * |
| 7108 | * When job control is turned off, background processes have their standard |
| 7109 | * input redirected to /dev/null (except for the second and later processes |
| 7110 | * in a pipeline). |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7111 | * |
| 7112 | * Called with interrupts off. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7113 | */ |
| 7114 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7115 | static inline void |
| 7116 | forkchild(struct job *jp, union node *n, int mode) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7117 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7118 | int oldlvl; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 7119 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7120 | TRACE(("Child shell %d\n", getpid())); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7121 | oldlvl = shlvl; |
| 7122 | shlvl++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7123 | |
| 7124 | closescript(); |
| 7125 | clear_traps(); |
| 7126 | #if JOBS |
| 7127 | /* do job control only in root shell */ |
| 7128 | jobctl = 0; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7129 | if (mode != FORK_NOJOB && jp->jobctl && !oldlvl) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7130 | pid_t pgrp; |
| 7131 | |
| 7132 | if (jp->nprocs == 0) |
| 7133 | pgrp = getpid(); |
| 7134 | else |
| 7135 | pgrp = jp->ps[0].pid; |
| 7136 | /* This can fail because we are doing it in the parent also */ |
| 7137 | (void)setpgid(0, pgrp); |
| 7138 | if (mode == FORK_FG) |
| 7139 | xtcsetpgrp(ttyfd, pgrp); |
| 7140 | setsignal(SIGTSTP); |
| 7141 | setsignal(SIGTTOU); |
| 7142 | } else |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7143 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7144 | if (mode == FORK_BG) { |
| 7145 | ignoresig(SIGINT); |
| 7146 | ignoresig(SIGQUIT); |
| 7147 | if (jp->nprocs == 0) { |
| 7148 | close(0); |
| 7149 | if (open(_PATH_DEVNULL, O_RDONLY) != 0) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7150 | sh_error("Can't open %s", _PATH_DEVNULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7151 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7152 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7153 | if (!oldlvl && iflag) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7154 | setsignal(SIGINT); |
| 7155 | setsignal(SIGQUIT); |
| 7156 | setsignal(SIGTERM); |
| 7157 | } |
| 7158 | for (jp = curjob; jp; jp = jp->prev_job) |
| 7159 | freejob(jp); |
| 7160 | jobless = 0; |
| 7161 | } |
| 7162 | |
| 7163 | static inline void |
| 7164 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 7165 | { |
| 7166 | TRACE(("In parent shell: child = %d\n", pid)); |
| 7167 | if (!jp) { |
| 7168 | while (jobless && dowait(DOWAIT_NORMAL, 0) > 0); |
| 7169 | jobless++; |
| 7170 | return; |
| 7171 | } |
| 7172 | #if JOBS |
| 7173 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 7174 | int pgrp; |
| 7175 | |
| 7176 | if (jp->nprocs == 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7177 | pgrp = pid; |
| 7178 | else |
| 7179 | pgrp = jp->ps[0].pid; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7180 | /* This can fail because we are doing it in the child also */ |
| 7181 | (void)setpgid(pid, pgrp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7182 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7183 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7184 | if (mode == FORK_BG) { |
| 7185 | backgndpid = pid; /* set $! */ |
| 7186 | set_curjob(jp, CUR_RUNNING); |
| 7187 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7188 | if (jp) { |
| 7189 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
| 7190 | ps->pid = pid; |
| 7191 | ps->status = -1; |
| 7192 | ps->cmd = nullstr; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7193 | #if JOBS |
| 7194 | if (jobctl && n) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7195 | ps->cmd = commandtext(n); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7196 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7197 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7198 | } |
| 7199 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7200 | static int |
| 7201 | forkshell(struct job *jp, union node *n, int mode) |
| 7202 | { |
| 7203 | int pid; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7204 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7205 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 7206 | pid = fork(); |
| 7207 | if (pid < 0) { |
| 7208 | TRACE(("Fork failed, errno=%d", errno)); |
| 7209 | if (jp) |
| 7210 | freejob(jp); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7211 | sh_error("Cannot fork"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7212 | } |
| 7213 | if (pid == 0) |
| 7214 | forkchild(jp, n, mode); |
| 7215 | else |
| 7216 | forkparent(jp, n, mode, pid); |
| 7217 | return pid; |
| 7218 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7219 | |
| 7220 | /* |
| 7221 | * Wait for job to finish. |
| 7222 | * |
| 7223 | * Under job control we have the problem that while a child process is |
| 7224 | * running interrupts generated by the user are sent to the child but not |
| 7225 | * to the shell. This means that an infinite loop started by an inter- |
| 7226 | * active user may be hard to kill. With job control turned off, an |
| 7227 | * interactive user may place an interactive program inside a loop. If |
| 7228 | * the interactive program catches interrupts, the user doesn't want |
| 7229 | * these interrupts to also abort the loop. The approach we take here |
| 7230 | * is to have the shell ignore interrupt signals while waiting for a |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 7231 | * foreground process to terminate, and then send itself an interrupt |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7232 | * signal if the child process was terminated by an interrupt signal. |
| 7233 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 7234 | * exit on interrupt; unless these processes terminate themselves by |
| 7235 | * sending a signal to themselves (instead of calling exit) they will |
| 7236 | * confuse this approach. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7237 | * |
| 7238 | * Called with interrupts off. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7239 | */ |
| 7240 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7241 | int |
| 7242 | waitforjob(struct job *jp) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7243 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7244 | int st; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7245 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7246 | TRACE(("waitforjob(%%%d) called\n", jobno(jp))); |
| 7247 | while (jp->state == JOBRUNNING) { |
| 7248 | dowait(DOWAIT_BLOCK, jp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7249 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7250 | st = getstatus(jp); |
| 7251 | #if JOBS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7252 | if (jp->jobctl) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7253 | xtcsetpgrp(ttyfd, rootpid); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7254 | /* |
| 7255 | * This is truly gross. |
| 7256 | * If we're doing job control, then we did a TIOCSPGRP which |
| 7257 | * caused us (the shell) to no longer be in the controlling |
| 7258 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 7259 | * intuit from the subprocess exit status whether a SIGINT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7260 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7261 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7262 | if (jp->sigint) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7263 | raise(SIGINT); |
| 7264 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7265 | if (jp->state == JOBDONE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7266 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7267 | freejob(jp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7268 | return st; |
| 7269 | } |
| 7270 | |
| 7271 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7272 | /* |
| 7273 | * Do a wait system call. If job control is compiled in, we accept |
| 7274 | * stopped processes. If block is zero, we return a value of zero |
| 7275 | * rather than blocking. |
| 7276 | * |
| 7277 | * System V doesn't have a non-blocking wait system call. It does |
| 7278 | * have a SIGCLD signal that is sent to a process when one of it's |
| 7279 | * children dies. The obvious way to use SIGCLD would be to install |
| 7280 | * a handler for SIGCLD which simply bumped a counter when a SIGCLD |
| 7281 | * was received, and have waitproc bump another counter when it got |
| 7282 | * the status of a process. Waitproc would then know that a wait |
| 7283 | * system call would not block if the two counters were different. |
| 7284 | * This approach doesn't work because if a process has children that |
| 7285 | * have not been waited for, System V will send it a SIGCLD when it |
| 7286 | * installs a signal handler for SIGCLD. What this means is that when |
| 7287 | * a child exits, the shell will be sent SIGCLD signals continuously |
| 7288 | * until is runs out of stack space, unless it does a wait call before |
| 7289 | * restoring the signal handler. The code below takes advantage of |
| 7290 | * this (mis)feature by installing a signal handler for SIGCLD and |
| 7291 | * then checking to see whether it was called. If there are any |
| 7292 | * children to be waited for, it will be. |
| 7293 | * |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7294 | * If neither SYSV nor BSD is defined, we don't implement nonblocking |
| 7295 | * waits at all. In this case, the user will not be informed when |
| 7296 | * a background process until the next time she runs a real program |
| 7297 | * (as opposed to running a builtin command or just typing return), |
| 7298 | * and the jobs command may give out of date information. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7299 | */ |
| 7300 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7301 | static inline int |
| 7302 | waitproc(int block, int *status) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7303 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7304 | int flags = 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7305 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7306 | #if JOBS |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7307 | if (jobctl) |
| 7308 | flags |= WUNTRACED; |
| 7309 | #endif |
| 7310 | if (block == 0) |
| 7311 | flags |= WNOHANG; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7312 | return wait3(status, flags, (struct rusage *)NULL); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 7313 | } |
| 7314 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7315 | /* |
| 7316 | * Wait for a process to terminate. |
| 7317 | */ |
| 7318 | |
| 7319 | static int |
| 7320 | dowait(int block, struct job *job) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7321 | { |
| 7322 | int pid; |
| 7323 | int status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7324 | struct job *jp; |
| 7325 | struct job *thisjob; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7326 | int state; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7327 | |
| 7328 | TRACE(("dowait(%d) called\n", block)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7329 | pid = waitproc(block, &status); |
| 7330 | TRACE(("wait returns pid %d, status=%d\n", pid, status)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7331 | if (pid <= 0) |
| 7332 | return pid; |
| 7333 | INTOFF; |
| 7334 | thisjob = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7335 | for (jp = curjob; jp; jp = jp->prev_job) { |
| 7336 | struct procstat *sp; |
| 7337 | struct procstat *spend; |
| 7338 | if (jp->state == JOBDONE) |
| 7339 | continue; |
| 7340 | state = JOBDONE; |
| 7341 | spend = jp->ps + jp->nprocs; |
| 7342 | sp = jp->ps; |
| 7343 | do { |
| 7344 | if (sp->pid == pid) { |
| 7345 | TRACE(("Job %d: changing status of proc %d from 0x%x to 0x%x\n", jobno(jp), pid, sp->status, status)); |
| 7346 | sp->status = status; |
| 7347 | thisjob = jp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7348 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7349 | if (sp->status == -1) |
| 7350 | state = JOBRUNNING; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7351 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7352 | if (state == JOBRUNNING) |
| 7353 | continue; |
| 7354 | if (WIFSTOPPED(sp->status)) { |
| 7355 | jp->stopstatus = sp->status; |
| 7356 | state = JOBSTOPPED; |
| 7357 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7358 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7359 | } while (++sp < spend); |
| 7360 | if (thisjob) |
| 7361 | goto gotjob; |
| 7362 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7363 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7364 | if (!WIFSTOPPED(status)) |
| 7365 | #endif |
| 7366 | |
| 7367 | jobless--; |
| 7368 | goto out; |
| 7369 | |
| 7370 | gotjob: |
| 7371 | if (state != JOBRUNNING) { |
| 7372 | thisjob->changed = 1; |
| 7373 | |
| 7374 | if (thisjob->state != state) { |
| 7375 | TRACE(("Job %d: changing state from %d to %d\n", jobno(thisjob), thisjob->state, state)); |
| 7376 | thisjob->state = state; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7377 | #if JOBS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7378 | if (state == JOBSTOPPED) { |
| 7379 | set_curjob(thisjob, CUR_STOPPED); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7380 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7381 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7382 | } |
| 7383 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7384 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7385 | out: |
| 7386 | INTON; |
| 7387 | |
| 7388 | if (thisjob && thisjob == job) { |
| 7389 | char s[48 + 1]; |
| 7390 | int len; |
| 7391 | |
| 7392 | len = sprint_status(s, status, 1); |
| 7393 | if (len) { |
| 7394 | s[len] = '\n'; |
| 7395 | s[len + 1] = 0; |
| 7396 | out2str(s); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7397 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7398 | } |
| 7399 | return pid; |
| 7400 | } |
| 7401 | |
| 7402 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7403 | /* |
| 7404 | * return 1 if there are stopped jobs, otherwise 0 |
| 7405 | */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 7406 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7407 | int |
| 7408 | stoppedjobs(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7409 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7410 | struct job *jp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7411 | int retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7412 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7413 | retval = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7414 | if (job_warning) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7415 | goto out; |
| 7416 | jp = curjob; |
| 7417 | if (jp && jp->state == JOBSTOPPED) { |
| 7418 | out2str("You have stopped jobs.\n"); |
| 7419 | job_warning = 2; |
| 7420 | retval++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7421 | } |
| 7422 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7423 | out: |
| 7424 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7425 | } |
| 7426 | |
| 7427 | /* |
| 7428 | * Return a string identifying a command (to be printed by the |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7429 | * jobs command). |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7430 | */ |
| 7431 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7432 | #if JOBS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7433 | static char *cmdnextc; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 7434 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7435 | static char * |
| 7436 | commandtext(union node *n) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7437 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7438 | char *name; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7439 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7440 | STARTSTACKSTR(cmdnextc); |
| 7441 | cmdtxt(n); |
| 7442 | name = stackblock(); |
| 7443 | TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n", |
| 7444 | name, cmdnextc, cmdnextc)); |
| 7445 | return savestr(name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7446 | } |
| 7447 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7448 | static void |
| 7449 | cmdtxt(union node *n) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7450 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7451 | union node *np; |
| 7452 | struct nodelist *lp; |
| 7453 | const char *p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7454 | char s[2]; |
| 7455 | |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 7456 | if (!n) |
| 7457 | return; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7458 | switch (n->type) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7459 | default: |
| 7460 | #if DEBUG |
| 7461 | abort(); |
| 7462 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7463 | case NPIPE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7464 | lp = n->npipe.cmdlist; |
| 7465 | for (;;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7466 | cmdtxt(lp->n); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7467 | lp = lp->next; |
| 7468 | if (!lp) |
| 7469 | break; |
| 7470 | cmdputs(" | "); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7471 | } |
| 7472 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7473 | case NSEMI: |
| 7474 | p = "; "; |
| 7475 | goto binop; |
| 7476 | case NAND: |
| 7477 | p = " && "; |
| 7478 | goto binop; |
| 7479 | case NOR: |
| 7480 | p = " || "; |
| 7481 | binop: |
| 7482 | cmdtxt(n->nbinary.ch1); |
| 7483 | cmdputs(p); |
| 7484 | n = n->nbinary.ch2; |
| 7485 | goto donode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7486 | case NREDIR: |
| 7487 | case NBACKGND: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7488 | n = n->nredir.n; |
| 7489 | goto donode; |
| 7490 | case NNOT: |
| 7491 | cmdputs("!"); |
| 7492 | n = n->nnot.com; |
| 7493 | donode: |
| 7494 | cmdtxt(n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7495 | break; |
| 7496 | case NIF: |
| 7497 | cmdputs("if "); |
| 7498 | cmdtxt(n->nif.test); |
| 7499 | cmdputs("; then "); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7500 | n = n->nif.ifpart; |
| 7501 | if (n->nif.elsepart) { |
| 7502 | cmdtxt(n); |
| 7503 | cmdputs("; else "); |
| 7504 | n = n->nif.elsepart; |
| 7505 | } |
| 7506 | p = "; fi"; |
| 7507 | goto dotail; |
| 7508 | case NSUBSHELL: |
| 7509 | cmdputs("("); |
| 7510 | n = n->nredir.n; |
| 7511 | p = ")"; |
| 7512 | goto dotail; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7513 | case NWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7514 | p = "while "; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7515 | goto until; |
| 7516 | case NUNTIL: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7517 | p = "until "; |
| 7518 | until: |
| 7519 | cmdputs(p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7520 | cmdtxt(n->nbinary.ch1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7521 | n = n->nbinary.ch2; |
| 7522 | p = "; done"; |
| 7523 | dodo: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7524 | cmdputs("; do "); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7525 | dotail: |
| 7526 | cmdtxt(n); |
| 7527 | goto dotail2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7528 | case NFOR: |
| 7529 | cmdputs("for "); |
| 7530 | cmdputs(n->nfor.var); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7531 | cmdputs(" in "); |
| 7532 | cmdlist(n->nfor.args, 1); |
| 7533 | n = n->nfor.body; |
| 7534 | p = "; done"; |
| 7535 | goto dodo; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7536 | case NDEFUN: |
| 7537 | cmdputs(n->narg.text); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7538 | p = "() { ... }"; |
| 7539 | goto dotail2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7540 | case NCMD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7541 | cmdlist(n->ncmd.args, 1); |
| 7542 | cmdlist(n->ncmd.redirect, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7543 | break; |
| 7544 | case NARG: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7545 | p = n->narg.text; |
| 7546 | dotail2: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7547 | cmdputs(p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7548 | break; |
| 7549 | case NHERE: |
| 7550 | case NXHERE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7551 | p = "<<..."; |
| 7552 | goto dotail2; |
| 7553 | case NCASE: |
| 7554 | cmdputs("case "); |
| 7555 | cmdputs(n->ncase.expr->narg.text); |
| 7556 | cmdputs(" in "); |
| 7557 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 7558 | cmdtxt(np->nclist.pattern); |
| 7559 | cmdputs(") "); |
| 7560 | cmdtxt(np->nclist.body); |
| 7561 | cmdputs(";; "); |
| 7562 | } |
| 7563 | p = "esac"; |
| 7564 | goto dotail2; |
| 7565 | case NTO: |
| 7566 | p = ">"; |
| 7567 | goto redir; |
| 7568 | case NCLOBBER: |
| 7569 | p = ">|"; |
| 7570 | goto redir; |
| 7571 | case NAPPEND: |
| 7572 | p = ">>"; |
| 7573 | goto redir; |
| 7574 | case NTOFD: |
| 7575 | p = ">&"; |
| 7576 | goto redir; |
| 7577 | case NFROM: |
| 7578 | p = "<"; |
| 7579 | goto redir; |
| 7580 | case NFROMFD: |
| 7581 | p = "<&"; |
| 7582 | goto redir; |
| 7583 | case NFROMTO: |
| 7584 | p = "<>"; |
| 7585 | redir: |
| 7586 | s[0] = n->nfile.fd + '0'; |
| 7587 | s[1] = '\0'; |
| 7588 | cmdputs(s); |
| 7589 | cmdputs(p); |
| 7590 | if (n->type == NTOFD || n->type == NFROMFD) { |
| 7591 | s[0] = n->ndup.dupfd + '0'; |
| 7592 | p = s; |
| 7593 | goto dotail2; |
| 7594 | } else { |
| 7595 | n = n->nfile.fname; |
| 7596 | goto donode; |
| 7597 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7598 | } |
| 7599 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7600 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7601 | static void |
| 7602 | cmdlist(union node *np, int sep) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7603 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7604 | for (; np; np = np->narg.next) { |
| 7605 | if (!sep) |
| 7606 | cmdputs(spcstr); |
| 7607 | cmdtxt(np); |
| 7608 | if (sep && np->narg.next) |
| 7609 | cmdputs(spcstr); |
| 7610 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7611 | } |
| 7612 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7613 | static void |
| 7614 | cmdputs(const char *s) |
| 7615 | { |
| 7616 | const char *p, *str; |
| 7617 | char c, cc[2] = " "; |
| 7618 | char *nextc; |
| 7619 | int subtype = 0; |
| 7620 | int quoted = 0; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 7621 | static const char vstype[VSTYPE + 1][4] = { |
| 7622 | "", "}", "-", "+", "?", "=", |
| 7623 | "%", "%%", "#", "##" |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7624 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7625 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 7626 | p = s; |
| 7627 | while ((c = *p++) != 0) { |
| 7628 | str = 0; |
| 7629 | switch (c) { |
| 7630 | case CTLESC: |
| 7631 | c = *p++; |
| 7632 | break; |
| 7633 | case CTLVAR: |
| 7634 | subtype = *p++; |
| 7635 | if ((subtype & VSTYPE) == VSLENGTH) |
| 7636 | str = "${#"; |
| 7637 | else |
| 7638 | str = "${"; |
| 7639 | if (!(subtype & VSQUOTE) != !(quoted & 1)) { |
| 7640 | quoted ^= 1; |
| 7641 | c = '"'; |
| 7642 | } else |
| 7643 | goto dostr; |
| 7644 | break; |
| 7645 | case CTLENDVAR: |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 7646 | str = "\"}" + !(quoted & 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7647 | quoted >>= 1; |
| 7648 | subtype = 0; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 7649 | goto dostr; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7650 | case CTLBACKQ: |
| 7651 | str = "$(...)"; |
| 7652 | goto dostr; |
| 7653 | case CTLBACKQ+CTLQUOTE: |
| 7654 | str = "\"$(...)\""; |
| 7655 | goto dostr; |
| 7656 | #ifdef CONFIG_ASH_MATH_SUPPORT |
| 7657 | case CTLARI: |
| 7658 | str = "$(("; |
| 7659 | goto dostr; |
| 7660 | case CTLENDARI: |
| 7661 | str = "))"; |
| 7662 | goto dostr; |
| 7663 | #endif |
| 7664 | case CTLQUOTEMARK: |
| 7665 | quoted ^= 1; |
| 7666 | c = '"'; |
| 7667 | break; |
| 7668 | case '=': |
| 7669 | if (subtype == 0) |
| 7670 | break; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 7671 | if ((subtype & VSTYPE) != VSNORMAL) |
| 7672 | quoted <<= 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7673 | str = vstype[subtype & VSTYPE]; |
| 7674 | if (subtype & VSNUL) |
| 7675 | c = ':'; |
| 7676 | else |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 7677 | goto checkstr; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7678 | break; |
| 7679 | case '\'': |
| 7680 | case '\\': |
| 7681 | case '"': |
| 7682 | case '$': |
| 7683 | /* These can only happen inside quotes */ |
| 7684 | cc[0] = c; |
| 7685 | str = cc; |
| 7686 | c = '\\'; |
| 7687 | break; |
| 7688 | default: |
| 7689 | break; |
| 7690 | } |
| 7691 | USTPUTC(c, nextc); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 7692 | checkstr: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7693 | if (!str) |
| 7694 | continue; |
| 7695 | dostr: |
| 7696 | while ((c = *str++)) { |
| 7697 | USTPUTC(c, nextc); |
| 7698 | } |
| 7699 | } |
| 7700 | if (quoted & 1) { |
| 7701 | USTPUTC('"', nextc); |
| 7702 | } |
| 7703 | *nextc = 0; |
| 7704 | cmdnextc = nextc; |
| 7705 | } |
| 7706 | |
| 7707 | |
| 7708 | static void |
| 7709 | showpipe(struct job *jp, FILE *out) |
| 7710 | { |
| 7711 | struct procstat *sp; |
| 7712 | struct procstat *spend; |
| 7713 | |
| 7714 | spend = jp->ps + jp->nprocs; |
| 7715 | for (sp = jp->ps + 1; sp < spend; sp++) |
| 7716 | fprintf(out, " | %s", sp->cmd); |
| 7717 | outcslow('\n', out); |
| 7718 | flushall(); |
| 7719 | } |
| 7720 | |
| 7721 | static void |
| 7722 | xtcsetpgrp(int fd, pid_t pgrp) |
| 7723 | { |
| 7724 | if (tcsetpgrp(fd, pgrp)) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7725 | sh_error("Cannot set tty process group (%m)"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7726 | } |
| 7727 | #endif /* JOBS */ |
| 7728 | |
| 7729 | static int |
| 7730 | getstatus(struct job *job) { |
| 7731 | int status; |
| 7732 | int retval; |
| 7733 | |
| 7734 | status = job->ps[job->nprocs - 1].status; |
| 7735 | retval = WEXITSTATUS(status); |
| 7736 | if (!WIFEXITED(status)) { |
| 7737 | #if JOBS |
| 7738 | retval = WSTOPSIG(status); |
| 7739 | if (!WIFSTOPPED(status)) |
| 7740 | #endif |
| 7741 | { |
| 7742 | /* XXX: limits number of signals */ |
| 7743 | retval = WTERMSIG(status); |
| 7744 | #if JOBS |
| 7745 | if (retval == SIGINT) |
| 7746 | job->sigint = 1; |
| 7747 | #endif |
| 7748 | } |
| 7749 | retval += 128; |
| 7750 | } |
| 7751 | TRACE(("getstatus: job %d, nproc %d, status %x, retval %x\n", |
| 7752 | jobno(job), job->nprocs, status, retval)); |
| 7753 | return retval; |
| 7754 | } |
| 7755 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 7756 | #ifdef CONFIG_ASH_MAIL |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7757 | /* mail.c */ |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 7758 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7759 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7760 | * Routines to check for mail. (Perhaps make part of main.c?) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7761 | */ |
| 7762 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7763 | #define MAXMBOXES 10 |
| 7764 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7765 | /* times of mailboxes */ |
| 7766 | static time_t mailtime[MAXMBOXES]; |
| 7767 | /* Set if MAIL or MAILPATH is changed. */ |
| 7768 | static int mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7769 | |
| 7770 | |
| 7771 | |
| 7772 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7773 | * Print appropriate message(s) if mail has arrived. |
| 7774 | * If mail_var_path_changed is set, |
| 7775 | * then the value of MAIL has mail_var_path_changed, |
| 7776 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7777 | */ |
| 7778 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7779 | static void |
| 7780 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7781 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7782 | const char *mpath; |
| 7783 | char *p; |
| 7784 | char *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7785 | time_t *mtp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7786 | struct stackmark smark; |
| 7787 | struct stat statb; |
| 7788 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7789 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7790 | mpath = mpathset() ? mpathval() : mailval(); |
| 7791 | for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7792 | p = padvance(&mpath, nullstr); |
| 7793 | if (p == NULL) |
| 7794 | break; |
| 7795 | if (*p == '\0') |
| 7796 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7797 | for (q = p ; *q ; q++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7798 | #ifdef DEBUG |
| 7799 | if (q[-1] != '/') |
| 7800 | abort(); |
| 7801 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7802 | q[-1] = '\0'; /* delete trailing '/' */ |
| 7803 | if (stat(p, &statb) < 0) { |
| 7804 | *mtp = 0; |
| 7805 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7806 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7807 | if (!mail_var_path_changed && statb.st_mtime != *mtp) { |
| 7808 | fprintf( |
| 7809 | stderr, snlfmt, |
| 7810 | pathopt ? pathopt : "you have mail" |
| 7811 | ); |
| 7812 | } |
| 7813 | *mtp = statb.st_mtime; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7814 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7815 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7816 | popstackmark(&smark); |
| 7817 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7818 | |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 7819 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7820 | static void |
| 7821 | changemail(const char *val) |
| 7822 | { |
| 7823 | mail_var_path_changed++; |
| 7824 | } |
| 7825 | |
| 7826 | #endif /* CONFIG_ASH_MAIL */ |
| 7827 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7828 | /* main.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7829 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7830 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7831 | #if PROFILE |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7832 | static short profile_buf[16384]; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7833 | extern int etext(); |
| 7834 | #endif |
| 7835 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7836 | static int isloginsh; |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 7837 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 7838 | static void read_profile(const char *); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 7839 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7840 | /* |
| 7841 | * Main routine. We initialize things, parse the arguments, execute |
| 7842 | * profiles if we're a login shell, and then call cmdloop to execute |
| 7843 | * commands. The setjmp call sets up the location to jump to when an |
| 7844 | * exception occurs. When an exception occurs the variable "state" |
| 7845 | * is used to figure out how far we had gotten. |
| 7846 | */ |
| 7847 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7848 | int |
| 7849 | ash_main(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7850 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7851 | char *shinit; |
| 7852 | volatile int state; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7853 | struct jmploc jmploc; |
| 7854 | struct stackmark smark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7855 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7856 | #ifdef __GLIBC__ |
| 7857 | dash_errno = __errno_location(); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 7858 | #endif |
| 7859 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7860 | #if PROFILE |
| 7861 | monitor(4, etext, profile_buf, sizeof profile_buf, 50); |
| 7862 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7863 | state = 0; |
| 7864 | if (setjmp(jmploc.loc)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7865 | int e; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7866 | int s; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7867 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7868 | reset(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7869 | |
| 7870 | e = exception; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7871 | if (e == EXERROR) |
| 7872 | exitstatus = 2; |
| 7873 | s = state; |
| 7874 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7875 | exitshell(); |
| 7876 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 7877 | if (e == EXINT) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7878 | outcslow('\n', stderr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7879 | } |
| 7880 | popstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7881 | FORCEINTON; /* enable interrupts */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7882 | if (s == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7883 | goto state1; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7884 | else if (s == 2) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7885 | goto state2; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7886 | else if (s == 3) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7887 | goto state3; |
| 7888 | else |
| 7889 | goto state4; |
| 7890 | } |
| 7891 | handler = &jmploc; |
| 7892 | #ifdef DEBUG |
| 7893 | opentrace(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7894 | trputs("Shell args: "); trargs(argv); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7895 | #endif |
| 7896 | rootpid = getpid(); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 7897 | |
| 7898 | #ifdef CONFIG_ASH_RANDOM_SUPPORT |
| 7899 | rseed = rootpid + ((time_t)time((time_t *)0)); |
| 7900 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7901 | init(); |
| 7902 | setstackmark(&smark); |
| 7903 | procargs(argc, argv); |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 7904 | #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY |
| 7905 | if ( iflag ) { |
| 7906 | const char *hp = lookupvar("HISTFILE"); |
| 7907 | |
| 7908 | if(hp == NULL ) { |
| 7909 | hp = lookupvar("HOME"); |
| 7910 | if(hp != NULL) { |
| 7911 | char *defhp = concat_path_file(hp, ".ash_history"); |
| 7912 | setvar("HISTFILE", defhp, 0); |
| 7913 | free(defhp); |
| 7914 | } |
| 7915 | } |
| 7916 | } |
| 7917 | #endif |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 7918 | if (argv[0] && argv[0][0] == '-') |
| 7919 | isloginsh = 1; |
| 7920 | if (isloginsh) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7921 | state = 1; |
| 7922 | read_profile("/etc/profile"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7923 | state1: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7924 | state = 2; |
| 7925 | read_profile(".profile"); |
| 7926 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7927 | state2: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7928 | state = 3; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7929 | if ( |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7930 | #ifndef linux |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7931 | getuid() == geteuid() && getgid() == getegid() && |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7932 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7933 | iflag |
| 7934 | ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7935 | if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7936 | read_profile(shinit); |
| 7937 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7938 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7939 | state3: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7940 | state = 4; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7941 | if (minusc) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7942 | evalstring(minusc, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7943 | |
| 7944 | if (sflag || minusc == NULL) { |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 7945 | #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 7946 | if ( iflag ) { |
| 7947 | const char *hp = lookupvar("HISTFILE"); |
| 7948 | |
| 7949 | if(hp != NULL ) |
| 7950 | load_history ( hp ); |
| 7951 | } |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 7952 | #endif |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 7953 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7954 | cmdloop(1); |
| 7955 | } |
| 7956 | #if PROFILE |
| 7957 | monitor(0); |
| 7958 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7959 | #if GPROF |
| 7960 | { |
| 7961 | extern void _mcleanup(void); |
| 7962 | _mcleanup(); |
| 7963 | } |
| 7964 | #endif |
| 7965 | exitshell(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7966 | /* NOTREACHED */ |
| 7967 | } |
| 7968 | |
| 7969 | |
| 7970 | /* |
| 7971 | * Read and execute commands. "Top" is nonzero for the top level command |
| 7972 | * loop; it turns on prompting if the shell is interactive. |
| 7973 | */ |
| 7974 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7975 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7976 | cmdloop(int top) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7977 | { |
| 7978 | union node *n; |
| 7979 | struct stackmark smark; |
| 7980 | int inter; |
| 7981 | int numeof = 0; |
| 7982 | |
| 7983 | TRACE(("cmdloop(%d) called\n", top)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7984 | for (;;) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 7985 | int skip; |
| 7986 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 7987 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7988 | #if JOBS |
| 7989 | if (jobctl) |
| 7990 | showjobs(stderr, SHOW_CHANGED); |
| 7991 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7992 | inter = 0; |
| 7993 | if (iflag && top) { |
| 7994 | inter++; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 7995 | #ifdef CONFIG_ASH_MAIL |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7996 | chkmail(); |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 7997 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7998 | } |
| 7999 | n = parsecmd(inter); |
| 8000 | /* showtree(n); DEBUG */ |
| 8001 | if (n == NEOF) { |
| 8002 | if (!top || numeof >= 50) |
| 8003 | break; |
| 8004 | if (!stoppedjobs()) { |
| 8005 | if (!Iflag) |
| 8006 | break; |
| 8007 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 8008 | } |
| 8009 | numeof++; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8010 | } else if (nflag == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8011 | job_warning = (job_warning == 2) ? 1 : 0; |
| 8012 | numeof = 0; |
| 8013 | evaltree(n, 0); |
| 8014 | } |
| 8015 | popstackmark(&smark); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8016 | skip = evalskip; |
| 8017 | |
| 8018 | if (skip) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8019 | evalskip = 0; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8020 | return skip & SKIPEVAL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8021 | } |
| 8022 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8023 | |
| 8024 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8025 | } |
| 8026 | |
| 8027 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8028 | /* |
| 8029 | * Read /etc/profile or .profile. Return on error. |
| 8030 | */ |
| 8031 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8032 | static void |
| 8033 | read_profile(const char *name) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8034 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8035 | int skip; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8036 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8037 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8038 | return; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8039 | |
| 8040 | skip = cmdloop(0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8041 | popfile(); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8042 | |
| 8043 | if (skip) |
| 8044 | exitshell(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8045 | } |
| 8046 | |
| 8047 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8048 | /* |
| 8049 | * Read a file containing shell functions. |
| 8050 | */ |
| 8051 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8052 | static void |
| 8053 | readcmdfile(char *name) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8054 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8055 | setinputfile(name, INPUT_PUSH_FILE); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8056 | cmdloop(0); |
| 8057 | popfile(); |
| 8058 | } |
| 8059 | |
| 8060 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8061 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8062 | * Take commands from a file. To be compatible we should do a path |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8063 | * search for the file, which is necessary to find sub-commands. |
| 8064 | */ |
| 8065 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8066 | static inline char * |
| 8067 | find_dot_file(char *name) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8068 | { |
| 8069 | char *fullname; |
| 8070 | const char *path = pathval(); |
| 8071 | struct stat statb; |
| 8072 | |
| 8073 | /* don't try this for absolute or relative paths */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8074 | if (strchr(name, '/')) |
| 8075 | return name; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8076 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8077 | while ((fullname = padvance(&path, name)) != NULL) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8078 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 8079 | /* |
| 8080 | * Don't bother freeing here, since it will |
| 8081 | * be freed by the caller. |
| 8082 | */ |
| 8083 | return fullname; |
| 8084 | } |
| 8085 | stunalloc(fullname); |
| 8086 | } |
| 8087 | |
| 8088 | /* not found in the PATH */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8089 | sh_error(not_found_msg, name); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8090 | /* NOTREACHED */ |
| 8091 | } |
| 8092 | |
Eric Andersen | 1e6aba9 | 2004-04-12 19:12:13 +0000 | [diff] [blame] | 8093 | static int dotcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8094 | { |
Eric Andersen | 1e6aba9 | 2004-04-12 19:12:13 +0000 | [diff] [blame] | 8095 | struct strlist *sp; |
| 8096 | volatile struct shparam saveparam; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8097 | int status = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8098 | |
Eric Andersen | 1e6aba9 | 2004-04-12 19:12:13 +0000 | [diff] [blame] | 8099 | for (sp = cmdenviron; sp; sp = sp->next) |
| 8100 | setvareq(bb_xstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
| 8101 | |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 8102 | if (argc >= 2) { /* That's what SVR2 does */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8103 | char *fullname; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8104 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8105 | fullname = find_dot_file(argv[1]); |
Eric Andersen | 1e6aba9 | 2004-04-12 19:12:13 +0000 | [diff] [blame] | 8106 | |
| 8107 | if (argc > 2) { |
| 8108 | saveparam = shellparam; |
| 8109 | shellparam.malloc = 0; |
| 8110 | shellparam.nparam = argc - 2; |
| 8111 | shellparam.p = argv + 2; |
| 8112 | }; |
| 8113 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8114 | setinputfile(fullname, INPUT_PUSH_FILE); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8115 | commandname = fullname; |
| 8116 | cmdloop(0); |
| 8117 | popfile(); |
Eric Andersen | 1e6aba9 | 2004-04-12 19:12:13 +0000 | [diff] [blame] | 8118 | |
| 8119 | if (argc > 2) { |
| 8120 | freeparam(&shellparam); |
| 8121 | shellparam = saveparam; |
| 8122 | }; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8123 | status = exitstatus; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8124 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8125 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8126 | } |
| 8127 | |
| 8128 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8129 | static int |
| 8130 | exitcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8131 | { |
| 8132 | if (stoppedjobs()) |
| 8133 | return 0; |
| 8134 | if (argc > 1) |
| 8135 | exitstatus = number(argv[1]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8136 | exraise(EXEXIT); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8137 | /* NOTREACHED */ |
| 8138 | } |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8139 | |
Paul Fox | 0b62158 | 2005-08-09 19:38:05 +0000 | [diff] [blame] | 8140 | #ifdef CONFIG_ASH_BUILTIN_ECHO |
| 8141 | static int |
| 8142 | echocmd(int argc, char **argv) |
| 8143 | { |
| 8144 | return bb_echo(argc, argv); |
| 8145 | } |
| 8146 | #endif |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8147 | /* memalloc.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8148 | |
| 8149 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 8150 | * Same for malloc, realloc, but returns an error when out of space. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8151 | */ |
| 8152 | |
| 8153 | static pointer |
| 8154 | ckrealloc(pointer p, size_t nbytes) |
| 8155 | { |
| 8156 | p = realloc(p, nbytes); |
| 8157 | if (p == NULL) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8158 | sh_error(bb_msg_memory_exhausted); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8159 | return p; |
| 8160 | } |
| 8161 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 8162 | static pointer |
| 8163 | ckmalloc(size_t nbytes) |
| 8164 | { |
| 8165 | return ckrealloc(NULL, nbytes); |
| 8166 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8167 | |
| 8168 | /* |
| 8169 | * Make a copy of a string in safe storage. |
| 8170 | */ |
| 8171 | |
| 8172 | static char * |
| 8173 | savestr(const char *s) |
| 8174 | { |
| 8175 | char *p = strdup(s); |
| 8176 | if (!p) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8177 | sh_error(bb_msg_memory_exhausted); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8178 | return p; |
| 8179 | } |
| 8180 | |
| 8181 | |
| 8182 | /* |
| 8183 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 8184 | * to make this more efficient, and also to avoid all sorts of exception |
| 8185 | * handling code to handle interrupts in the middle of a parse. |
| 8186 | * |
| 8187 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 8188 | * well. |
| 8189 | */ |
| 8190 | |
| 8191 | |
| 8192 | static pointer |
| 8193 | stalloc(size_t nbytes) |
| 8194 | { |
| 8195 | char *p; |
| 8196 | size_t aligned; |
| 8197 | |
| 8198 | aligned = SHELL_ALIGN(nbytes); |
| 8199 | if (aligned > stacknleft) { |
| 8200 | size_t len; |
| 8201 | size_t blocksize; |
| 8202 | struct stack_block *sp; |
| 8203 | |
| 8204 | blocksize = aligned; |
| 8205 | if (blocksize < MINSIZE) |
| 8206 | blocksize = MINSIZE; |
| 8207 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 8208 | if (len < blocksize) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8209 | sh_error(bb_msg_memory_exhausted); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8210 | INTOFF; |
| 8211 | sp = ckmalloc(len); |
| 8212 | sp->prev = stackp; |
| 8213 | stacknxt = sp->space; |
| 8214 | stacknleft = blocksize; |
| 8215 | sstrend = stacknxt + blocksize; |
| 8216 | stackp = sp; |
| 8217 | INTON; |
| 8218 | } |
| 8219 | p = stacknxt; |
| 8220 | stacknxt += aligned; |
| 8221 | stacknleft -= aligned; |
| 8222 | return p; |
| 8223 | } |
| 8224 | |
| 8225 | |
| 8226 | void |
| 8227 | stunalloc(pointer p) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8228 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8229 | #ifdef DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8230 | if (!p || (stacknxt < (char *)p) || ((char *)p < stackp->space)) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8231 | write(2, "stunalloc\n", 10); |
| 8232 | abort(); |
| 8233 | } |
| 8234 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8235 | stacknleft += stacknxt - (char *)p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8236 | stacknxt = p; |
| 8237 | } |
| 8238 | |
| 8239 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8240 | void |
| 8241 | setstackmark(struct stackmark *mark) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8242 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8243 | mark->stackp = stackp; |
| 8244 | mark->stacknxt = stacknxt; |
| 8245 | mark->stacknleft = stacknleft; |
| 8246 | mark->marknext = markp; |
| 8247 | markp = mark; |
| 8248 | } |
| 8249 | |
| 8250 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8251 | void |
| 8252 | popstackmark(struct stackmark *mark) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8253 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8254 | struct stack_block *sp; |
| 8255 | |
| 8256 | INTOFF; |
| 8257 | markp = mark->marknext; |
| 8258 | while (stackp != mark->stackp) { |
| 8259 | sp = stackp; |
| 8260 | stackp = sp->prev; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8261 | ckfree(sp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8262 | } |
| 8263 | stacknxt = mark->stacknxt; |
| 8264 | stacknleft = mark->stacknleft; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8265 | sstrend = mark->stacknxt + mark->stacknleft; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8266 | INTON; |
| 8267 | } |
| 8268 | |
| 8269 | |
| 8270 | /* |
| 8271 | * When the parser reads in a string, it wants to stick the string on the |
| 8272 | * stack and only adjust the stack pointer when it knows how big the |
| 8273 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 8274 | * of space on top of the stack and stackblocklen returns the length of |
| 8275 | * this block. Growstackblock will grow this space by at least one byte, |
| 8276 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 8277 | * part of the block that has been used. |
| 8278 | */ |
| 8279 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8280 | void |
| 8281 | growstackblock(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8282 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8283 | size_t newlen; |
| 8284 | |
| 8285 | newlen = stacknleft * 2; |
| 8286 | if (newlen < stacknleft) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8287 | sh_error(bb_msg_memory_exhausted); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8288 | if (newlen < 128) |
| 8289 | newlen += 128; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8290 | |
| 8291 | if (stacknxt == stackp->space && stackp != &stackbase) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8292 | struct stack_block *oldstackp; |
| 8293 | struct stackmark *xmark; |
| 8294 | struct stack_block *sp; |
| 8295 | struct stack_block *prevstackp; |
| 8296 | size_t grosslen; |
| 8297 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8298 | INTOFF; |
| 8299 | oldstackp = stackp; |
| 8300 | sp = stackp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8301 | prevstackp = sp->prev; |
| 8302 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 8303 | sp = ckrealloc((pointer)sp, grosslen); |
| 8304 | sp->prev = prevstackp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8305 | stackp = sp; |
| 8306 | stacknxt = sp->space; |
| 8307 | stacknleft = newlen; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8308 | sstrend = sp->space + newlen; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8309 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8310 | /* |
| 8311 | * Stack marks pointing to the start of the old block |
| 8312 | * must be relocated to point to the new block |
| 8313 | */ |
| 8314 | xmark = markp; |
| 8315 | while (xmark != NULL && xmark->stackp == oldstackp) { |
| 8316 | xmark->stackp = stackp; |
| 8317 | xmark->stacknxt = stacknxt; |
| 8318 | xmark->stacknleft = stacknleft; |
| 8319 | xmark = xmark->marknext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8320 | } |
| 8321 | INTON; |
| 8322 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8323 | char *oldspace = stacknxt; |
| 8324 | int oldlen = stacknleft; |
| 8325 | char *p = stalloc(newlen); |
| 8326 | |
| 8327 | /* free the space we just allocated */ |
| 8328 | stacknxt = memcpy(p, oldspace, oldlen); |
| 8329 | stacknleft += newlen; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8330 | } |
| 8331 | } |
| 8332 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8333 | static inline void |
| 8334 | grabstackblock(size_t len) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8335 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8336 | len = SHELL_ALIGN(len); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8337 | stacknxt += len; |
| 8338 | stacknleft -= len; |
| 8339 | } |
| 8340 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8341 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8342 | * The following routines are somewhat easier to use than the above. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8343 | * The user declares a variable of type STACKSTR, which may be declared |
| 8344 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 8345 | * the user uses the macro STPUTC to add characters to the string. In |
| 8346 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 8347 | * grown as necessary. When the user is done, she can just leave the |
| 8348 | * string there and refer to it using stackblock(). Or she can allocate |
| 8349 | * the space for it using grabstackstr(). If it is necessary to allow |
| 8350 | * someone else to use the stack temporarily and then continue to grow |
| 8351 | * the string, the user should use grabstack to allocate the space, and |
| 8352 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 8353 | * |
| 8354 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 8355 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 8356 | * is space for at least one character. |
| 8357 | */ |
| 8358 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8359 | void * |
| 8360 | growstackstr(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8361 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8362 | size_t len = stackblocksize(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8363 | if (herefd >= 0 && len >= 1024) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 8364 | bb_full_write(herefd, stackblock(), len); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8365 | return stackblock(); |
| 8366 | } |
| 8367 | growstackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8368 | return stackblock() + len; |
| 8369 | } |
| 8370 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8371 | /* |
| 8372 | * Called from CHECKSTRSPACE. |
| 8373 | */ |
| 8374 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8375 | char * |
| 8376 | makestrspace(size_t newlen, char *p) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8377 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8378 | size_t len = p - stacknxt; |
| 8379 | size_t size = stackblocksize(); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8380 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8381 | for (;;) { |
| 8382 | size_t nleft; |
| 8383 | |
| 8384 | size = stackblocksize(); |
| 8385 | nleft = size - len; |
| 8386 | if (nleft >= newlen) |
| 8387 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8388 | growstackblock(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8389 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8390 | return stackblock() + len; |
| 8391 | } |
| 8392 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8393 | char * |
| 8394 | stnputs(const char *s, size_t n, char *p) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8395 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8396 | p = makestrspace(n, p); |
| 8397 | p = mempcpy(p, s, n); |
| 8398 | return p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8399 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8400 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8401 | char * |
| 8402 | stputs(const char *s, char *p) |
| 8403 | { |
| 8404 | return stnputs(s, strlen(s), p); |
| 8405 | } |
| 8406 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8407 | /* mystring.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8408 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8409 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8410 | * String functions. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8411 | * |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8412 | * number(s) Convert a string of digits to an integer. |
| 8413 | * is_number(s) Return true if s is a string of digits. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8414 | */ |
| 8415 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8416 | /* |
| 8417 | * prefix -- see if pfx is a prefix of string. |
| 8418 | */ |
| 8419 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8420 | char * |
| 8421 | prefix(const char *string, const char *pfx) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8422 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8423 | while (*pfx) { |
| 8424 | if (*pfx++ != *string++) |
| 8425 | return 0; |
| 8426 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8427 | return (char *) string; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8428 | } |
| 8429 | |
| 8430 | |
| 8431 | /* |
| 8432 | * Convert a string of digits to an integer, printing an error message on |
| 8433 | * failure. |
| 8434 | */ |
| 8435 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8436 | int |
| 8437 | number(const char *s) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8438 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8439 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8440 | if (! is_number(s)) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8441 | sh_error(illnum, s); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8442 | return atoi(s); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8443 | } |
| 8444 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8445 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8446 | /* |
| 8447 | * Check for a valid number. This should be elsewhere. |
| 8448 | */ |
| 8449 | |
| 8450 | int |
| 8451 | is_number(const char *p) |
| 8452 | { |
| 8453 | do { |
| 8454 | if (! is_digit(*p)) |
| 8455 | return 0; |
| 8456 | } while (*++p != '\0'); |
| 8457 | return 1; |
| 8458 | } |
| 8459 | |
| 8460 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8461 | /* |
| 8462 | * Produce a possibly single quoted string suitable as input to the shell. |
| 8463 | * The return string is allocated on the stack. |
| 8464 | */ |
| 8465 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8466 | char * |
| 8467 | single_quote(const char *s) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8468 | char *p; |
| 8469 | |
| 8470 | STARTSTACKSTR(p); |
| 8471 | |
| 8472 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8473 | char *q; |
| 8474 | size_t len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8475 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8476 | len = strchrnul(s, '\'') - s; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8477 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8478 | q = p = makestrspace(len + 3, p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8479 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8480 | *q++ = '\''; |
| 8481 | q = mempcpy(q, s, len); |
| 8482 | *q++ = '\''; |
| 8483 | s += len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8484 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8485 | STADJUST(q - p, p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8486 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8487 | len = strspn(s, "'"); |
| 8488 | if (!len) |
| 8489 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8490 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8491 | q = p = makestrspace(len + 3, p); |
| 8492 | |
| 8493 | *q++ = '"'; |
| 8494 | q = mempcpy(q, s, len); |
| 8495 | *q++ = '"'; |
| 8496 | s += len; |
| 8497 | |
| 8498 | STADJUST(q - p, p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8499 | } while (*s); |
| 8500 | |
| 8501 | USTPUTC(0, p); |
| 8502 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8503 | return stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8504 | } |
| 8505 | |
| 8506 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8507 | * Like strdup but works with the ash stack. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8508 | */ |
| 8509 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8510 | char * |
| 8511 | sstrdup(const char *p) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8512 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8513 | size_t len = strlen(p) + 1; |
| 8514 | return memcpy(stalloc(len), p, len); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8515 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8516 | |
| 8517 | |
| 8518 | static void |
| 8519 | calcsize(union node *n) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8520 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8521 | if (n == NULL) |
| 8522 | return; |
| 8523 | funcblocksize += nodesize[n->type]; |
| 8524 | switch (n->type) { |
| 8525 | case NCMD: |
| 8526 | calcsize(n->ncmd.redirect); |
| 8527 | calcsize(n->ncmd.args); |
| 8528 | calcsize(n->ncmd.assign); |
| 8529 | break; |
| 8530 | case NPIPE: |
| 8531 | sizenodelist(n->npipe.cmdlist); |
| 8532 | break; |
| 8533 | case NREDIR: |
| 8534 | case NBACKGND: |
| 8535 | case NSUBSHELL: |
| 8536 | calcsize(n->nredir.redirect); |
| 8537 | calcsize(n->nredir.n); |
| 8538 | break; |
| 8539 | case NAND: |
| 8540 | case NOR: |
| 8541 | case NSEMI: |
| 8542 | case NWHILE: |
| 8543 | case NUNTIL: |
| 8544 | calcsize(n->nbinary.ch2); |
| 8545 | calcsize(n->nbinary.ch1); |
| 8546 | break; |
| 8547 | case NIF: |
| 8548 | calcsize(n->nif.elsepart); |
| 8549 | calcsize(n->nif.ifpart); |
| 8550 | calcsize(n->nif.test); |
| 8551 | break; |
| 8552 | case NFOR: |
| 8553 | funcstringsize += strlen(n->nfor.var) + 1; |
| 8554 | calcsize(n->nfor.body); |
| 8555 | calcsize(n->nfor.args); |
| 8556 | break; |
| 8557 | case NCASE: |
| 8558 | calcsize(n->ncase.cases); |
| 8559 | calcsize(n->ncase.expr); |
| 8560 | break; |
| 8561 | case NCLIST: |
| 8562 | calcsize(n->nclist.body); |
| 8563 | calcsize(n->nclist.pattern); |
| 8564 | calcsize(n->nclist.next); |
| 8565 | break; |
| 8566 | case NDEFUN: |
| 8567 | case NARG: |
| 8568 | sizenodelist(n->narg.backquote); |
| 8569 | funcstringsize += strlen(n->narg.text) + 1; |
| 8570 | calcsize(n->narg.next); |
| 8571 | break; |
| 8572 | case NTO: |
| 8573 | case NCLOBBER: |
| 8574 | case NFROM: |
| 8575 | case NFROMTO: |
| 8576 | case NAPPEND: |
| 8577 | calcsize(n->nfile.fname); |
| 8578 | calcsize(n->nfile.next); |
| 8579 | break; |
| 8580 | case NTOFD: |
| 8581 | case NFROMFD: |
| 8582 | calcsize(n->ndup.vname); |
| 8583 | calcsize(n->ndup.next); |
| 8584 | break; |
| 8585 | case NHERE: |
| 8586 | case NXHERE: |
| 8587 | calcsize(n->nhere.doc); |
| 8588 | calcsize(n->nhere.next); |
| 8589 | break; |
| 8590 | case NNOT: |
| 8591 | calcsize(n->nnot.com); |
| 8592 | break; |
| 8593 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8594 | } |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8595 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8596 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8597 | static void |
| 8598 | sizenodelist(struct nodelist *lp) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8599 | { |
| 8600 | while (lp) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8601 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 8602 | calcsize(lp->n); |
| 8603 | lp = lp->next; |
| 8604 | } |
| 8605 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8606 | |
| 8607 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8608 | static union node * |
| 8609 | copynode(union node *n) |
| 8610 | { |
| 8611 | union node *new; |
| 8612 | |
| 8613 | if (n == NULL) |
| 8614 | return NULL; |
| 8615 | new = funcblock; |
| 8616 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 8617 | switch (n->type) { |
| 8618 | case NCMD: |
| 8619 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 8620 | new->ncmd.args = copynode(n->ncmd.args); |
| 8621 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 8622 | break; |
| 8623 | case NPIPE: |
| 8624 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
| 8625 | new->npipe.backgnd = n->npipe.backgnd; |
| 8626 | break; |
| 8627 | case NREDIR: |
| 8628 | case NBACKGND: |
| 8629 | case NSUBSHELL: |
| 8630 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 8631 | new->nredir.n = copynode(n->nredir.n); |
| 8632 | break; |
| 8633 | case NAND: |
| 8634 | case NOR: |
| 8635 | case NSEMI: |
| 8636 | case NWHILE: |
| 8637 | case NUNTIL: |
| 8638 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 8639 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 8640 | break; |
| 8641 | case NIF: |
| 8642 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 8643 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 8644 | new->nif.test = copynode(n->nif.test); |
| 8645 | break; |
| 8646 | case NFOR: |
| 8647 | new->nfor.var = nodesavestr(n->nfor.var); |
| 8648 | new->nfor.body = copynode(n->nfor.body); |
| 8649 | new->nfor.args = copynode(n->nfor.args); |
| 8650 | break; |
| 8651 | case NCASE: |
| 8652 | new->ncase.cases = copynode(n->ncase.cases); |
| 8653 | new->ncase.expr = copynode(n->ncase.expr); |
| 8654 | break; |
| 8655 | case NCLIST: |
| 8656 | new->nclist.body = copynode(n->nclist.body); |
| 8657 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 8658 | new->nclist.next = copynode(n->nclist.next); |
| 8659 | break; |
| 8660 | case NDEFUN: |
| 8661 | case NARG: |
| 8662 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 8663 | new->narg.text = nodesavestr(n->narg.text); |
| 8664 | new->narg.next = copynode(n->narg.next); |
| 8665 | break; |
| 8666 | case NTO: |
| 8667 | case NCLOBBER: |
| 8668 | case NFROM: |
| 8669 | case NFROMTO: |
| 8670 | case NAPPEND: |
| 8671 | new->nfile.fname = copynode(n->nfile.fname); |
| 8672 | new->nfile.fd = n->nfile.fd; |
| 8673 | new->nfile.next = copynode(n->nfile.next); |
| 8674 | break; |
| 8675 | case NTOFD: |
| 8676 | case NFROMFD: |
| 8677 | new->ndup.vname = copynode(n->ndup.vname); |
| 8678 | new->ndup.dupfd = n->ndup.dupfd; |
| 8679 | new->ndup.fd = n->ndup.fd; |
| 8680 | new->ndup.next = copynode(n->ndup.next); |
| 8681 | break; |
| 8682 | case NHERE: |
| 8683 | case NXHERE: |
| 8684 | new->nhere.doc = copynode(n->nhere.doc); |
| 8685 | new->nhere.fd = n->nhere.fd; |
| 8686 | new->nhere.next = copynode(n->nhere.next); |
| 8687 | break; |
| 8688 | case NNOT: |
| 8689 | new->nnot.com = copynode(n->nnot.com); |
| 8690 | break; |
| 8691 | }; |
| 8692 | new->type = n->type; |
| 8693 | return new; |
| 8694 | } |
| 8695 | |
| 8696 | |
| 8697 | static struct nodelist * |
| 8698 | copynodelist(struct nodelist *lp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8699 | { |
| 8700 | struct nodelist *start; |
| 8701 | struct nodelist **lpp; |
| 8702 | |
| 8703 | lpp = &start; |
| 8704 | while (lp) { |
| 8705 | *lpp = funcblock; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8706 | funcblock = (char *) funcblock + |
| 8707 | SHELL_ALIGN(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8708 | (*lpp)->n = copynode(lp->n); |
| 8709 | lp = lp->next; |
| 8710 | lpp = &(*lpp)->next; |
| 8711 | } |
| 8712 | *lpp = NULL; |
| 8713 | return start; |
| 8714 | } |
| 8715 | |
| 8716 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8717 | static char * |
| 8718 | nodesavestr(char *s) |
| 8719 | { |
| 8720 | char *rtn = funcstring; |
| 8721 | |
| 8722 | funcstring = stpcpy(funcstring, s) + 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8723 | return rtn; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8724 | } |
| 8725 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8726 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8727 | /* |
| 8728 | * Free a parse tree. |
| 8729 | */ |
| 8730 | |
| 8731 | static void |
| 8732 | freefunc(struct funcnode *f) |
| 8733 | { |
| 8734 | if (f && --f->count < 0) |
| 8735 | ckfree(f); |
| 8736 | } |
| 8737 | |
| 8738 | |
| 8739 | static void options(int); |
| 8740 | static void setoption(int, int); |
| 8741 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8742 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8743 | /* |
| 8744 | * Process the shell command line arguments. |
| 8745 | */ |
| 8746 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8747 | void |
| 8748 | procargs(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8749 | { |
| 8750 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8751 | const char *xminusc; |
| 8752 | char **xargv; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8753 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8754 | xargv = argv; |
| 8755 | arg0 = xargv[0]; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8756 | if (argc > 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8757 | xargv++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8758 | for (i = 0; i < NOPTS; i++) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8759 | optlist[i] = 2; |
| 8760 | argptr = xargv; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8761 | options(1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8762 | xargv = argptr; |
| 8763 | xminusc = minusc; |
| 8764 | if (*xargv == NULL) { |
| 8765 | if (xminusc) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8766 | sh_error("-c requires an argument"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8767 | sflag = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8768 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8769 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 8770 | iflag = 1; |
| 8771 | if (mflag == 2) |
| 8772 | mflag = iflag; |
| 8773 | for (i = 0; i < NOPTS; i++) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8774 | if (optlist[i] == 2) |
| 8775 | optlist[i] = 0; |
| 8776 | #if DEBUG == 2 |
| 8777 | debug = 1; |
| 8778 | #endif |
| 8779 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 8780 | if (xminusc) { |
| 8781 | minusc = *xargv++; |
| 8782 | if (*xargv) |
| 8783 | goto setarg0; |
| 8784 | } else if (!sflag) { |
| 8785 | setinputfile(*xargv, 0); |
| 8786 | setarg0: |
| 8787 | arg0 = *xargv++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8788 | commandname = arg0; |
| 8789 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8790 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8791 | shellparam.p = xargv; |
| 8792 | #ifdef CONFIG_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8793 | shellparam.optind = 1; |
| 8794 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8795 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8796 | /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8797 | while (*xargv) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8798 | shellparam.nparam++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8799 | xargv++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8800 | } |
| 8801 | optschanged(); |
| 8802 | } |
| 8803 | |
| 8804 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8805 | void |
| 8806 | optschanged(void) |
| 8807 | { |
| 8808 | #ifdef DEBUG |
| 8809 | opentrace(); |
| 8810 | #endif |
| 8811 | setinteractive(iflag); |
| 8812 | setjobctl(mflag); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 8813 | setvimode(viflag); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8814 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8815 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8816 | static inline void |
| 8817 | minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8818 | { |
| 8819 | int i; |
| 8820 | |
| 8821 | if (name == NULL) { |
| 8822 | out1str("Current option settings\n"); |
| 8823 | for (i = 0; i < NOPTS; i++) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8824 | out1fmt("%-16s%s\n", optnames(i), |
| 8825 | optlist[i] ? "on" : "off"); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8826 | } else { |
| 8827 | for (i = 0; i < NOPTS; i++) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8828 | if (equal(name, optnames(i))) { |
| 8829 | optlist[i] = val; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8830 | return; |
| 8831 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8832 | sh_error("Illegal option -o %s", name); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8833 | } |
| 8834 | } |
| 8835 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8836 | /* |
| 8837 | * Process shell options. The global variable argptr contains a pointer |
| 8838 | * to the argument list; we advance it past the options. |
| 8839 | */ |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8840 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8841 | static void |
| 8842 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8843 | { |
| 8844 | char *p; |
| 8845 | int val; |
| 8846 | int c; |
| 8847 | |
| 8848 | if (cmdline) |
| 8849 | minusc = NULL; |
| 8850 | while ((p = *argptr) != NULL) { |
| 8851 | argptr++; |
| 8852 | if ((c = *p++) == '-') { |
| 8853 | val = 1; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8854 | if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) { |
| 8855 | if (!cmdline) { |
| 8856 | /* "-" means turn off -x and -v */ |
| 8857 | if (p[0] == '\0') |
| 8858 | xflag = vflag = 0; |
| 8859 | /* "--" means reset params */ |
| 8860 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8861 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8862 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8863 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8864 | } |
| 8865 | } else if (c == '+') { |
| 8866 | val = 0; |
| 8867 | } else { |
| 8868 | argptr--; |
| 8869 | break; |
| 8870 | } |
| 8871 | while ((c = *p++) != '\0') { |
| 8872 | if (c == 'c' && cmdline) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8873 | minusc = p; /* command is after shell args*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8874 | } else if (c == 'o') { |
| 8875 | minus_o(*argptr, val); |
| 8876 | if (*argptr) |
| 8877 | argptr++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8878 | } else if (cmdline && (c == '-')) { // long options |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8879 | if (strcmp(p, "login") == 0) |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 8880 | isloginsh = 1; |
| 8881 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8882 | } else { |
| 8883 | setoption(c, val); |
| 8884 | } |
| 8885 | } |
| 8886 | } |
| 8887 | } |
| 8888 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8889 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8890 | static void |
| 8891 | setoption(int flag, int val) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8892 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8893 | int i; |
| 8894 | |
| 8895 | for (i = 0; i < NOPTS; i++) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8896 | if (optletters(i) == flag) { |
| 8897 | optlist[i] = val; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8898 | return; |
| 8899 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8900 | sh_error("Illegal option -%c", flag); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8901 | /* NOTREACHED */ |
| 8902 | } |
| 8903 | |
| 8904 | |
| 8905 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8906 | /* |
| 8907 | * Set the shell parameters. |
| 8908 | */ |
| 8909 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8910 | void |
| 8911 | setparam(char **argv) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8912 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8913 | char **newparam; |
| 8914 | char **ap; |
| 8915 | int nparam; |
| 8916 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8917 | for (nparam = 0 ; argv[nparam] ; nparam++); |
| 8918 | ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8919 | while (*argv) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8920 | *ap++ = savestr(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8921 | } |
| 8922 | *ap = NULL; |
| 8923 | freeparam(&shellparam); |
| 8924 | shellparam.malloc = 1; |
| 8925 | shellparam.nparam = nparam; |
| 8926 | shellparam.p = newparam; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8927 | #ifdef CONFIG_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8928 | shellparam.optind = 1; |
| 8929 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8930 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8931 | } |
| 8932 | |
| 8933 | |
| 8934 | /* |
| 8935 | * Free the list of positional parameters. |
| 8936 | */ |
| 8937 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8938 | void |
| 8939 | freeparam(volatile struct shparam *param) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 8940 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8941 | char **ap; |
| 8942 | |
| 8943 | if (param->malloc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8944 | for (ap = param->p ; *ap ; ap++) |
| 8945 | ckfree(*ap); |
| 8946 | ckfree(param->p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8947 | } |
| 8948 | } |
| 8949 | |
| 8950 | |
| 8951 | |
| 8952 | /* |
| 8953 | * The shift builtin command. |
| 8954 | */ |
| 8955 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8956 | int |
| 8957 | shiftcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8958 | { |
| 8959 | int n; |
| 8960 | char **ap1, **ap2; |
| 8961 | |
| 8962 | n = 1; |
| 8963 | if (argc > 1) |
| 8964 | n = number(argv[1]); |
| 8965 | if (n > shellparam.nparam) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8966 | sh_error("can't shift that many"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8967 | INTOFF; |
| 8968 | shellparam.nparam -= n; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8969 | for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8970 | if (shellparam.malloc) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8971 | ckfree(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8972 | } |
| 8973 | ap2 = shellparam.p; |
| 8974 | while ((*ap2++ = *ap1++) != NULL); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8975 | #ifdef CONFIG_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8976 | shellparam.optind = 1; |
| 8977 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8978 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8979 | INTON; |
| 8980 | return 0; |
| 8981 | } |
| 8982 | |
| 8983 | |
| 8984 | |
| 8985 | /* |
| 8986 | * The set command builtin. |
| 8987 | */ |
| 8988 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8989 | int |
| 8990 | setcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8991 | { |
| 8992 | if (argc == 1) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8993 | return showvars(nullstr, 0, VUNSET); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8994 | INTOFF; |
| 8995 | options(0); |
| 8996 | optschanged(); |
| 8997 | if (*argptr != NULL) { |
| 8998 | setparam(argptr); |
| 8999 | } |
| 9000 | INTON; |
| 9001 | return 0; |
| 9002 | } |
| 9003 | |
| 9004 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9005 | #ifdef CONFIG_ASH_GETOPTS |
| 9006 | static void |
| 9007 | getoptsreset(value) |
| 9008 | const char *value; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9009 | { |
| 9010 | shellparam.optind = number(value); |
| 9011 | shellparam.optoff = -1; |
| 9012 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9013 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9014 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 9015 | #ifdef CONFIG_LOCALE_SUPPORT |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9016 | static void change_lc_all(const char *value) |
| 9017 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9018 | if (value != 0 && *value != 0) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9019 | setlocale(LC_ALL, value); |
| 9020 | } |
| 9021 | |
| 9022 | static void change_lc_ctype(const char *value) |
| 9023 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9024 | if (value != 0 && *value != 0) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9025 | setlocale(LC_CTYPE, value); |
| 9026 | } |
| 9027 | |
| 9028 | #endif |
| 9029 | |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 9030 | #ifdef CONFIG_ASH_RANDOM_SUPPORT |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 9031 | /* Roughly copied from bash.. */ |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 9032 | static void change_random(const char *value) |
| 9033 | { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 9034 | if(value == NULL) { |
| 9035 | /* "get", generate */ |
| 9036 | char buf[16]; |
| 9037 | |
| 9038 | rseed = rseed * 1103515245 + 12345; |
| 9039 | sprintf(buf, "%d", (unsigned int)((rseed & 32767))); |
| 9040 | /* set without recursion */ |
| 9041 | setvar(vrandom.text, buf, VNOFUNC); |
| 9042 | vrandom.flags &= ~VNOFUNC; |
| 9043 | } else { |
| 9044 | /* set/reset */ |
| 9045 | rseed = strtoul(value, (char **)NULL, 10); |
| 9046 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 9047 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 9048 | #endif |
| 9049 | |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 9050 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 9051 | #ifdef CONFIG_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9052 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9053 | getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9054 | { |
| 9055 | char *p, *q; |
| 9056 | char c = '?'; |
| 9057 | int done = 0; |
| 9058 | int err = 0; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 9059 | char s[12]; |
| 9060 | char **optnext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9061 | |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 9062 | if(*param_optind < 1) |
| 9063 | return 1; |
| 9064 | optnext = optfirst + *param_optind - 1; |
| 9065 | |
| 9066 | if (*param_optind <= 1 || *optoff < 0 || strlen(optnext[-1]) < *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9067 | p = NULL; |
| 9068 | else |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 9069 | p = optnext[-1] + *optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9070 | if (p == NULL || *p == '\0') { |
| 9071 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9072 | p = *optnext; |
| 9073 | if (p == NULL || *p != '-' || *++p == '\0') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9074 | atend: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9075 | p = NULL; |
| 9076 | done = 1; |
| 9077 | goto out; |
| 9078 | } |
| 9079 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9080 | if (p[0] == '-' && p[1] == '\0') /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9081 | goto atend; |
| 9082 | } |
| 9083 | |
| 9084 | c = *p++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9085 | for (q = optstr; *q != c; ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9086 | if (*q == '\0') { |
| 9087 | if (optstr[0] == ':') { |
| 9088 | s[0] = c; |
| 9089 | s[1] = '\0'; |
| 9090 | err |= setvarsafe("OPTARG", s, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9091 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9092 | fprintf(stderr, "Illegal option -%c\n", c); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9093 | (void) unsetvar("OPTARG"); |
| 9094 | } |
| 9095 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9096 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9097 | } |
| 9098 | if (*++q == ':') |
| 9099 | q++; |
| 9100 | } |
| 9101 | |
| 9102 | if (*++q == ':') { |
| 9103 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 9104 | if (optstr[0] == ':') { |
| 9105 | s[0] = c; |
| 9106 | s[1] = '\0'; |
| 9107 | err |= setvarsafe("OPTARG", s, 0); |
| 9108 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9109 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9110 | fprintf(stderr, "No arg for -%c option\n", c); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9111 | (void) unsetvar("OPTARG"); |
| 9112 | c = '?'; |
| 9113 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9114 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9115 | } |
| 9116 | |
| 9117 | if (p == *optnext) |
| 9118 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9119 | err |= setvarsafe("OPTARG", p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9120 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9121 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9122 | err |= setvarsafe("OPTARG", nullstr, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9123 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9124 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9125 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9126 | *param_optind = optnext - optfirst + 1; |
| 9127 | fmtstr(s, sizeof(s), "%d", *param_optind); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9128 | err |= setvarsafe("OPTIND", s, VNOFUNC); |
| 9129 | s[0] = c; |
| 9130 | s[1] = '\0'; |
| 9131 | err |= setvarsafe(optvar, s, 0); |
| 9132 | if (err) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9133 | *param_optind = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9134 | *optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9135 | flushall(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9136 | exraise(EXERROR); |
| 9137 | } |
| 9138 | return done; |
| 9139 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9140 | |
| 9141 | /* |
| 9142 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 9143 | * to be processed. Shellparam.optptr points to the next character to |
| 9144 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 9145 | * then it's the first time getopts has been called. |
| 9146 | */ |
| 9147 | |
| 9148 | int |
| 9149 | getoptscmd(int argc, char **argv) |
| 9150 | { |
| 9151 | char **optbase; |
| 9152 | |
| 9153 | if (argc < 3) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9154 | sh_error("Usage: getopts optstring var [arg]"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9155 | else if (argc == 3) { |
| 9156 | optbase = shellparam.p; |
| 9157 | if (shellparam.optind > shellparam.nparam + 1) { |
| 9158 | shellparam.optind = 1; |
| 9159 | shellparam.optoff = -1; |
| 9160 | } |
| 9161 | } |
| 9162 | else { |
| 9163 | optbase = &argv[3]; |
| 9164 | if (shellparam.optind > argc - 2) { |
| 9165 | shellparam.optind = 1; |
| 9166 | shellparam.optoff = -1; |
| 9167 | } |
| 9168 | } |
| 9169 | |
| 9170 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
| 9171 | &shellparam.optoff); |
| 9172 | } |
| 9173 | #endif /* CONFIG_ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9174 | |
| 9175 | /* |
| 9176 | * XXX - should get rid of. have all builtins use getopt(3). the |
| 9177 | * library getopt must have the BSD extension static variable "optreset" |
| 9178 | * otherwise it can't be used within the shell safely. |
| 9179 | * |
| 9180 | * Standard option processing (a la getopt) for builtin routines. The |
| 9181 | * only argument that is passed to nextopt is the option string; the |
| 9182 | * other arguments are unnecessary. It return the character, or '\0' on |
| 9183 | * end of input. |
| 9184 | */ |
| 9185 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9186 | static int |
| 9187 | nextopt(const char *optstring) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9188 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9189 | char *p; |
| 9190 | const char *q; |
| 9191 | char c; |
| 9192 | |
| 9193 | if ((p = optptr) == NULL || *p == '\0') { |
| 9194 | p = *argptr; |
| 9195 | if (p == NULL || *p != '-' || *++p == '\0') |
| 9196 | return '\0'; |
| 9197 | argptr++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9198 | if (p[0] == '-' && p[1] == '\0') /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9199 | return '\0'; |
| 9200 | } |
| 9201 | c = *p++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9202 | for (q = optstring ; *q != c ; ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9203 | if (*q == '\0') |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9204 | sh_error("Illegal option -%c", c); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9205 | if (*++q == ':') |
| 9206 | q++; |
| 9207 | } |
| 9208 | if (*++q == ':') { |
| 9209 | if (*p == '\0' && (p = *argptr++) == NULL) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9210 | sh_error("No arg for -%c option", c); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9211 | optionarg = p; |
| 9212 | p = NULL; |
| 9213 | } |
| 9214 | optptr = p; |
| 9215 | return c; |
| 9216 | } |
| 9217 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9218 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9219 | /* output.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9220 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9221 | void |
| 9222 | outstr(const char *p, FILE *file) |
| 9223 | { |
| 9224 | INTOFF; |
| 9225 | fputs(p, file); |
| 9226 | INTON; |
| 9227 | } |
| 9228 | |
| 9229 | void |
| 9230 | flushall(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9231 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9232 | INTOFF; |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 9233 | fflush(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9234 | fflush(stderr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9235 | INTON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9236 | } |
| 9237 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9238 | void |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 9239 | flusherr(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9240 | { |
| 9241 | INTOFF; |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 9242 | fflush(stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9243 | INTON; |
| 9244 | } |
| 9245 | |
| 9246 | static void |
| 9247 | outcslow(int c, FILE *dest) |
| 9248 | { |
| 9249 | INTOFF; |
| 9250 | putc(c, dest); |
| 9251 | fflush(dest); |
| 9252 | INTON; |
| 9253 | } |
| 9254 | |
| 9255 | |
| 9256 | static int |
| 9257 | out1fmt(const char *fmt, ...) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9258 | { |
| 9259 | va_list ap; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9260 | int r; |
| 9261 | |
| 9262 | INTOFF; |
| 9263 | va_start(ap, fmt); |
| 9264 | r = vprintf(fmt, ap); |
| 9265 | va_end(ap); |
| 9266 | INTON; |
| 9267 | return r; |
| 9268 | } |
| 9269 | |
| 9270 | |
| 9271 | int |
| 9272 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 9273 | { |
| 9274 | va_list ap; |
| 9275 | int ret; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9276 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9277 | va_start(ap, fmt); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9278 | INTOFF; |
| 9279 | ret = vsnprintf(outbuf, length, fmt, ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9280 | va_end(ap); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9281 | INTON; |
| 9282 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9283 | } |
| 9284 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9285 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9286 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9287 | /* parser.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9288 | |
| 9289 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9290 | /* |
| 9291 | * Shell command parser. |
| 9292 | */ |
| 9293 | |
| 9294 | #define EOFMARKLEN 79 |
| 9295 | |
| 9296 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9297 | struct heredoc { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9298 | struct heredoc *next; /* next here document in list */ |
| 9299 | union node *here; /* redirection node */ |
| 9300 | char *eofmark; /* string indicating end of input */ |
| 9301 | int striptabs; /* if set, strip leading tabs */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9302 | }; |
| 9303 | |
| 9304 | |
| 9305 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9306 | static struct heredoc *heredoclist; /* list of here documents to read */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9307 | |
| 9308 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9309 | static union node *list(int); |
| 9310 | static union node *andor(void); |
| 9311 | static union node *pipeline(void); |
| 9312 | static union node *command(void); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9313 | static union node *simplecmd(void); |
| 9314 | static union node *makename(void); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9315 | static void parsefname(void); |
| 9316 | static void parseheredoc(void); |
| 9317 | static char peektoken(void); |
| 9318 | static int readtoken(void); |
| 9319 | static int xxreadtoken(void); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9320 | static int readtoken1(int firstc, int syntax, char *eofmark, int striptabs); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9321 | static int noexpand(char *); |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 9322 | static void synexpect(int) ATTRIBUTE_NORETURN; |
| 9323 | static void synerror(const char *) ATTRIBUTE_NORETURN; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9324 | static void setprompt(int); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9325 | |
| 9326 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9327 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9328 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9329 | /* |
| 9330 | * Read and parse a command. Returns NEOF on end of file. (NULL is a |
| 9331 | * valid parse tree indicating a blank line.) |
| 9332 | */ |
| 9333 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9334 | union node * |
| 9335 | parsecmd(int interact) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9336 | { |
| 9337 | int t; |
| 9338 | |
| 9339 | tokpushback = 0; |
| 9340 | doprompt = interact; |
| 9341 | if (doprompt) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9342 | setprompt(doprompt); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9343 | needprompt = 0; |
| 9344 | t = readtoken(); |
| 9345 | if (t == TEOF) |
| 9346 | return NEOF; |
| 9347 | if (t == TNL) |
| 9348 | return NULL; |
| 9349 | tokpushback++; |
| 9350 | return list(1); |
| 9351 | } |
| 9352 | |
| 9353 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9354 | static union node * |
| 9355 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9356 | { |
| 9357 | union node *n1, *n2, *n3; |
| 9358 | int tok; |
| 9359 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9360 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 9361 | if (nlflag == 2 && peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9362 | return NULL; |
| 9363 | n1 = NULL; |
| 9364 | for (;;) { |
| 9365 | n2 = andor(); |
| 9366 | tok = readtoken(); |
| 9367 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9368 | if (n2->type == NPIPE) { |
| 9369 | n2->npipe.backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9370 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9371 | if (n2->type != NREDIR) { |
| 9372 | n3 = stalloc(sizeof(struct nredir)); |
| 9373 | n3->nredir.n = n2; |
| 9374 | n3->nredir.redirect = NULL; |
| 9375 | n2 = n3; |
| 9376 | } |
| 9377 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9378 | } |
| 9379 | } |
| 9380 | if (n1 == NULL) { |
| 9381 | n1 = n2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9382 | } |
| 9383 | else { |
| 9384 | n3 = (union node *)stalloc(sizeof (struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9385 | n3->type = NSEMI; |
| 9386 | n3->nbinary.ch1 = n1; |
| 9387 | n3->nbinary.ch2 = n2; |
| 9388 | n1 = n3; |
| 9389 | } |
| 9390 | switch (tok) { |
| 9391 | case TBACKGND: |
| 9392 | case TSEMI: |
| 9393 | tok = readtoken(); |
| 9394 | /* fall through */ |
| 9395 | case TNL: |
| 9396 | if (tok == TNL) { |
| 9397 | parseheredoc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9398 | if (nlflag == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9399 | return n1; |
| 9400 | } else { |
| 9401 | tokpushback++; |
| 9402 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9403 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 9404 | if (peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9405 | return n1; |
| 9406 | break; |
| 9407 | case TEOF: |
| 9408 | if (heredoclist) |
| 9409 | parseheredoc(); |
| 9410 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9411 | pungetc(); /* push back EOF on input */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9412 | return n1; |
| 9413 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9414 | if (nlflag == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9415 | synexpect(-1); |
| 9416 | tokpushback++; |
| 9417 | return n1; |
| 9418 | } |
| 9419 | } |
| 9420 | } |
| 9421 | |
| 9422 | |
| 9423 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9424 | static union node * |
| 9425 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9426 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9427 | union node *n1, *n2, *n3; |
| 9428 | int t; |
| 9429 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9430 | n1 = pipeline(); |
| 9431 | for (;;) { |
| 9432 | if ((t = readtoken()) == TAND) { |
| 9433 | t = NAND; |
| 9434 | } else if (t == TOR) { |
| 9435 | t = NOR; |
| 9436 | } else { |
| 9437 | tokpushback++; |
| 9438 | return n1; |
| 9439 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9440 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9441 | n2 = pipeline(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9442 | n3 = (union node *)stalloc(sizeof (struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9443 | n3->type = t; |
| 9444 | n3->nbinary.ch1 = n1; |
| 9445 | n3->nbinary.ch2 = n2; |
| 9446 | n1 = n3; |
| 9447 | } |
| 9448 | } |
| 9449 | |
| 9450 | |
| 9451 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9452 | static union node * |
| 9453 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9454 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9455 | union node *n1, *n2, *pipenode; |
| 9456 | struct nodelist *lp, *prev; |
| 9457 | int negate; |
| 9458 | |
| 9459 | negate = 0; |
| 9460 | TRACE(("pipeline: entered\n")); |
| 9461 | if (readtoken() == TNOT) { |
| 9462 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9463 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9464 | } else |
| 9465 | tokpushback++; |
| 9466 | n1 = command(); |
| 9467 | if (readtoken() == TPIPE) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9468 | pipenode = (union node *)stalloc(sizeof (struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9469 | pipenode->type = NPIPE; |
| 9470 | pipenode->npipe.backgnd = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9471 | lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9472 | pipenode->npipe.cmdlist = lp; |
| 9473 | lp->n = n1; |
| 9474 | do { |
| 9475 | prev = lp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9476 | lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); |
| 9477 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9478 | lp->n = command(); |
| 9479 | prev->next = lp; |
| 9480 | } while (readtoken() == TPIPE); |
| 9481 | lp->next = NULL; |
| 9482 | n1 = pipenode; |
| 9483 | } |
| 9484 | tokpushback++; |
| 9485 | if (negate) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9486 | n2 = (union node *)stalloc(sizeof (struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9487 | n2->type = NNOT; |
| 9488 | n2->nnot.com = n1; |
| 9489 | return n2; |
| 9490 | } else |
| 9491 | return n1; |
| 9492 | } |
| 9493 | |
| 9494 | |
| 9495 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9496 | static union node * |
| 9497 | command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9498 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9499 | union node *n1, *n2; |
| 9500 | union node *ap, **app; |
| 9501 | union node *cp, **cpp; |
| 9502 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9503 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9504 | int t; |
| 9505 | |
| 9506 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9507 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 9508 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9509 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9510 | default: |
| 9511 | synexpect(-1); |
| 9512 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9513 | case TIF: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9514 | n1 = (union node *)stalloc(sizeof (struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9515 | n1->type = NIF; |
| 9516 | n1->nif.test = list(0); |
| 9517 | if (readtoken() != TTHEN) |
| 9518 | synexpect(TTHEN); |
| 9519 | n1->nif.ifpart = list(0); |
| 9520 | n2 = n1; |
| 9521 | while (readtoken() == TELIF) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9522 | n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9523 | n2 = n2->nif.elsepart; |
| 9524 | n2->type = NIF; |
| 9525 | n2->nif.test = list(0); |
| 9526 | if (readtoken() != TTHEN) |
| 9527 | synexpect(TTHEN); |
| 9528 | n2->nif.ifpart = list(0); |
| 9529 | } |
| 9530 | if (lasttoken == TELSE) |
| 9531 | n2->nif.elsepart = list(0); |
| 9532 | else { |
| 9533 | n2->nif.elsepart = NULL; |
| 9534 | tokpushback++; |
| 9535 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9536 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9537 | break; |
| 9538 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9539 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9540 | int got; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9541 | n1 = (union node *)stalloc(sizeof (struct nbinary)); |
| 9542 | n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9543 | n1->nbinary.ch1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9544 | if ((got=readtoken()) != TDO) { |
| 9545 | TRACE(("expecting DO got %s %s\n", tokname(got), got == TWORD ? wordtext : "")); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9546 | synexpect(TDO); |
| 9547 | } |
| 9548 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9549 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9550 | break; |
| 9551 | } |
| 9552 | case TFOR: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9553 | if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9554 | synerror("Bad for loop variable"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9555 | n1 = (union node *)stalloc(sizeof (struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9556 | n1->type = NFOR; |
| 9557 | n1->nfor.var = wordtext; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9558 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9559 | if (readtoken() == TIN) { |
| 9560 | app = ≈ |
| 9561 | while (readtoken() == TWORD) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9562 | n2 = (union node *)stalloc(sizeof (struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9563 | n2->type = NARG; |
| 9564 | n2->narg.text = wordtext; |
| 9565 | n2->narg.backquote = backquotelist; |
| 9566 | *app = n2; |
| 9567 | app = &n2->narg.next; |
| 9568 | } |
| 9569 | *app = NULL; |
| 9570 | n1->nfor.args = ap; |
| 9571 | if (lasttoken != TNL && lasttoken != TSEMI) |
| 9572 | synexpect(-1); |
| 9573 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9574 | n2 = (union node *)stalloc(sizeof (struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9575 | n2->type = NARG; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9576 | n2->narg.text = (char *)dolatstr; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9577 | n2->narg.backquote = NULL; |
| 9578 | n2->narg.next = NULL; |
| 9579 | n1->nfor.args = n2; |
| 9580 | /* |
| 9581 | * Newline or semicolon here is optional (but note |
| 9582 | * that the original Bourne shell only allowed NL). |
| 9583 | */ |
| 9584 | if (lasttoken != TNL && lasttoken != TSEMI) |
| 9585 | tokpushback++; |
| 9586 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9587 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9588 | if (readtoken() != TDO) |
| 9589 | synexpect(TDO); |
| 9590 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9591 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9592 | break; |
| 9593 | case TCASE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9594 | n1 = (union node *)stalloc(sizeof (struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9595 | n1->type = NCASE; |
| 9596 | if (readtoken() != TWORD) |
| 9597 | synexpect(TWORD); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9598 | n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9599 | n2->type = NARG; |
| 9600 | n2->narg.text = wordtext; |
| 9601 | n2->narg.backquote = backquotelist; |
| 9602 | n2->narg.next = NULL; |
| 9603 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9604 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9605 | } while (readtoken() == TNL); |
| 9606 | if (lasttoken != TIN) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9607 | synexpect(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9608 | cpp = &n1->ncase.cases; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9609 | next_case: |
| 9610 | checkkwd = CHKNL | CHKKWD; |
| 9611 | t = readtoken(); |
| 9612 | while(t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9613 | if (lasttoken == TLP) |
| 9614 | readtoken(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9615 | *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9616 | cp->type = NCLIST; |
| 9617 | app = &cp->nclist.pattern; |
| 9618 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9619 | *app = ap = (union node *)stalloc(sizeof (struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9620 | ap->type = NARG; |
| 9621 | ap->narg.text = wordtext; |
| 9622 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9623 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9624 | break; |
| 9625 | app = &ap->narg.next; |
| 9626 | readtoken(); |
| 9627 | } |
| 9628 | ap->narg.next = NULL; |
| 9629 | if (lasttoken != TRP) |
| 9630 | synexpect(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9631 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9632 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9633 | cpp = &cp->nclist.next; |
| 9634 | |
| 9635 | checkkwd = CHKNL | CHKKWD; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9636 | if ((t = readtoken()) != TESAC) { |
| 9637 | if (t != TENDCASE) |
| 9638 | synexpect(TENDCASE); |
| 9639 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9640 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9641 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9642 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9643 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9644 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9645 | case TLP: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9646 | n1 = (union node *)stalloc(sizeof (struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9647 | n1->type = NSUBSHELL; |
| 9648 | n1->nredir.n = list(0); |
| 9649 | n1->nredir.redirect = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9650 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9651 | break; |
| 9652 | case TBEGIN: |
| 9653 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9654 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9655 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9656 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9657 | case TREDIR: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9658 | tokpushback++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9659 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9660 | } |
| 9661 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9662 | if (readtoken() != t) |
| 9663 | synexpect(t); |
| 9664 | |
| 9665 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9666 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9667 | checkkwd = CHKKWD | CHKALIAS; |
| 9668 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9669 | while (readtoken() == TREDIR) { |
| 9670 | *rpp = n2 = redirnode; |
| 9671 | rpp = &n2->nfile.next; |
| 9672 | parsefname(); |
| 9673 | } |
| 9674 | tokpushback++; |
| 9675 | *rpp = NULL; |
| 9676 | if (redir) { |
| 9677 | if (n1->type != NSUBSHELL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9678 | n2 = (union node *)stalloc(sizeof (struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9679 | n2->type = NREDIR; |
| 9680 | n2->nredir.n = n1; |
| 9681 | n1 = n2; |
| 9682 | } |
| 9683 | n1->nredir.redirect = redir; |
| 9684 | } |
| 9685 | |
| 9686 | return n1; |
| 9687 | } |
| 9688 | |
| 9689 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9690 | static union node * |
| 9691 | simplecmd(void) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9692 | union node *args, **app; |
| 9693 | union node *n = NULL; |
| 9694 | union node *vars, **vpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9695 | union node **rpp, *redir; |
| 9696 | int savecheckkwd; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9697 | |
| 9698 | args = NULL; |
| 9699 | app = &args; |
| 9700 | vars = NULL; |
| 9701 | vpp = &vars; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9702 | redir = NULL; |
| 9703 | rpp = &redir; |
Eric Andersen | a3483db | 2001-10-24 08:01:06 +0000 | [diff] [blame] | 9704 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9705 | savecheckkwd = CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9706 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9707 | checkkwd = savecheckkwd; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9708 | switch (readtoken()) { |
| 9709 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9710 | n = (union node *)stalloc(sizeof (struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9711 | n->type = NARG; |
| 9712 | n->narg.text = wordtext; |
| 9713 | n->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9714 | if (savecheckkwd && isassignment(wordtext)) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9715 | *vpp = n; |
| 9716 | vpp = &n->narg.next; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9717 | } else { |
| 9718 | *app = n; |
| 9719 | app = &n->narg.next; |
| 9720 | savecheckkwd = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9721 | } |
| 9722 | break; |
| 9723 | case TREDIR: |
| 9724 | *rpp = n = redirnode; |
| 9725 | rpp = &n->nfile.next; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9726 | parsefname(); /* read name of redirection file */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9727 | break; |
| 9728 | case TLP: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9729 | if ( |
| 9730 | args && app == &args->narg.next && |
| 9731 | !vars && !redir |
| 9732 | ) { |
| 9733 | struct builtincmd *bcmd; |
| 9734 | const char *name; |
| 9735 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9736 | /* We have a function */ |
| 9737 | if (readtoken() != TRP) |
| 9738 | synexpect(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9739 | name = n->narg.text; |
| 9740 | if ( |
| 9741 | !goodname(name) || ( |
| 9742 | (bcmd = find_builtin(name)) && |
| 9743 | IS_BUILTIN_SPECIAL(bcmd) |
| 9744 | ) |
| 9745 | ) |
| 9746 | synerror("Bad function name"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9747 | n->type = NDEFUN; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9748 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9749 | n->narg.next = command(); |
| 9750 | return n; |
| 9751 | } |
| 9752 | /* fall through */ |
| 9753 | default: |
| 9754 | tokpushback++; |
| 9755 | goto out; |
| 9756 | } |
| 9757 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9758 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9759 | *app = NULL; |
| 9760 | *vpp = NULL; |
| 9761 | *rpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9762 | n = (union node *)stalloc(sizeof (struct ncmd)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9763 | n->type = NCMD; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9764 | n->ncmd.args = args; |
| 9765 | n->ncmd.assign = vars; |
| 9766 | n->ncmd.redirect = redir; |
| 9767 | return n; |
| 9768 | } |
| 9769 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9770 | static union node * |
| 9771 | makename(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9772 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9773 | union node *n; |
| 9774 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9775 | n = (union node *)stalloc(sizeof (struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9776 | n->type = NARG; |
| 9777 | n->narg.next = NULL; |
| 9778 | n->narg.text = wordtext; |
| 9779 | n->narg.backquote = backquotelist; |
| 9780 | return n; |
| 9781 | } |
| 9782 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9783 | void fixredir(union node *n, const char *text, int err) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9784 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9785 | TRACE(("Fix redir %s %d\n", text, err)); |
| 9786 | if (!err) |
| 9787 | n->ndup.vname = NULL; |
| 9788 | |
| 9789 | if (is_digit(text[0]) && text[1] == '\0') |
| 9790 | n->ndup.dupfd = digit_val(text[0]); |
| 9791 | else if (text[0] == '-' && text[1] == '\0') |
| 9792 | n->ndup.dupfd = -1; |
| 9793 | else { |
| 9794 | |
| 9795 | if (err) |
| 9796 | synerror("Bad fd number"); |
| 9797 | else |
| 9798 | n->ndup.vname = makename(); |
| 9799 | } |
| 9800 | } |
| 9801 | |
| 9802 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9803 | static void |
| 9804 | parsefname(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9805 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9806 | union node *n = redirnode; |
| 9807 | |
| 9808 | if (readtoken() != TWORD) |
| 9809 | synexpect(-1); |
| 9810 | if (n->type == NHERE) { |
| 9811 | struct heredoc *here = heredoc; |
| 9812 | struct heredoc *p; |
| 9813 | int i; |
| 9814 | |
| 9815 | if (quoteflag == 0) |
| 9816 | n->type = NXHERE; |
| 9817 | TRACE(("Here document %d\n", n->type)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9818 | if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9819 | synerror("Illegal eof marker for << redirection"); |
| 9820 | rmescapes(wordtext); |
| 9821 | here->eofmark = wordtext; |
| 9822 | here->next = NULL; |
| 9823 | if (heredoclist == NULL) |
| 9824 | heredoclist = here; |
| 9825 | else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9826 | for (p = heredoclist ; p->next ; p = p->next); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9827 | p->next = here; |
| 9828 | } |
| 9829 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 9830 | fixredir(n, wordtext, 0); |
| 9831 | } else { |
| 9832 | n->nfile.fname = makename(); |
| 9833 | } |
| 9834 | } |
| 9835 | |
| 9836 | |
| 9837 | /* |
| 9838 | * Input any here documents. |
| 9839 | */ |
| 9840 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9841 | static void |
| 9842 | parseheredoc(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9843 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9844 | struct heredoc *here; |
| 9845 | union node *n; |
| 9846 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9847 | here = heredoclist; |
| 9848 | heredoclist = 0; |
| 9849 | |
| 9850 | while (here) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9851 | if (needprompt) { |
| 9852 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9853 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9854 | readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, |
| 9855 | here->eofmark, here->striptabs); |
| 9856 | n = (union node *)stalloc(sizeof (struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9857 | n->narg.type = NARG; |
| 9858 | n->narg.next = NULL; |
| 9859 | n->narg.text = wordtext; |
| 9860 | n->narg.backquote = backquotelist; |
| 9861 | here->here->nhere.doc = n; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9862 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9863 | } |
| 9864 | } |
| 9865 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9866 | static char peektoken(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9867 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9868 | int t; |
| 9869 | |
| 9870 | t = readtoken(); |
| 9871 | tokpushback++; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 9872 | return tokname_array[t][0]; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9873 | } |
| 9874 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9875 | static int |
| 9876 | readtoken(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9877 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9878 | int t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9879 | #ifdef DEBUG |
| 9880 | int alreadyseen = tokpushback; |
| 9881 | #endif |
| 9882 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 9883 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9884 | top: |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9885 | #endif |
| 9886 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9887 | t = xxreadtoken(); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9888 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9889 | /* |
| 9890 | * eat newlines |
| 9891 | */ |
| 9892 | if (checkkwd & CHKNL) { |
| 9893 | while (t == TNL) { |
| 9894 | parseheredoc(); |
| 9895 | t = xxreadtoken(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9896 | } |
| 9897 | } |
| 9898 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9899 | if (t != TWORD || quoteflag) { |
| 9900 | goto out; |
| 9901 | } |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 9902 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9903 | /* |
| 9904 | * check for keywords |
| 9905 | */ |
| 9906 | if (checkkwd & CHKKWD) { |
| 9907 | const char *const *pp; |
| 9908 | |
| 9909 | if ((pp = findkwd(wordtext))) { |
| 9910 | lasttoken = t = pp - tokname_array; |
| 9911 | TRACE(("keyword %s recognized\n", tokname(t))); |
| 9912 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9913 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9914 | } |
| 9915 | |
| 9916 | if (checkkwd & CHKALIAS) { |
Eric Andersen | 8e13987 | 2002-07-04 00:19:46 +0000 | [diff] [blame] | 9917 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9918 | struct alias *ap; |
| 9919 | if ((ap = lookupalias(wordtext, 1)) != NULL) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9920 | if (*ap->val) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9921 | pushstring(ap->val, ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9922 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9923 | goto top; |
| 9924 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9925 | #endif |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 9926 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9927 | out: |
| 9928 | checkkwd = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9929 | #ifdef DEBUG |
| 9930 | if (!alreadyseen) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9931 | TRACE(("token %s %s\n", tokname(t), t == TWORD ? wordtext : "")); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9932 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9933 | TRACE(("reread token %s %s\n", tokname(t), t == TWORD ? wordtext : "")); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9934 | #endif |
| 9935 | return (t); |
| 9936 | } |
| 9937 | |
| 9938 | |
| 9939 | /* |
| 9940 | * Read the next input token. |
| 9941 | * 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] | 9942 | * backquotes. We set quoteflag to true if any part of the word was |
| 9943 | * quoted. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9944 | * 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] | 9945 | * the redirection. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9946 | * 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] | 9947 | * on which the token starts. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9948 | * |
| 9949 | * [Change comment: here documents and internal procedures] |
| 9950 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 9951 | * word parsing code into a separate routine. In this case, readtoken |
| 9952 | * doesn't need to have any internal procedures, but parseword does. |
| 9953 | * We could also make parseoperator in essence the main routine, and |
| 9954 | * have parseword (readtoken1?) handle both words and redirection.] |
| 9955 | */ |
| 9956 | |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 9957 | #define NEW_xxreadtoken |
| 9958 | #ifdef NEW_xxreadtoken |
| 9959 | |
| 9960 | /* singles must be first! */ |
| 9961 | static const char xxreadtoken_chars[7] = { '\n', '(', ')', '&', '|', ';', 0 }; |
| 9962 | |
| 9963 | static const char xxreadtoken_tokens[] = { |
| 9964 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 9965 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 9966 | TEOF, /* corresponds to trailing nul */ |
| 9967 | TAND, TOR, TENDCASE, /* if double occurrence */ |
| 9968 | }; |
| 9969 | |
| 9970 | #define xxreadtoken_doubles \ |
| 9971 | (sizeof(xxreadtoken_tokens) - sizeof(xxreadtoken_chars)) |
| 9972 | #define xxreadtoken_singles \ |
| 9973 | (sizeof(xxreadtoken_chars) - xxreadtoken_doubles - 1) |
| 9974 | |
| 9975 | static int xxreadtoken() |
| 9976 | { |
| 9977 | int c; |
| 9978 | |
| 9979 | if (tokpushback) { |
| 9980 | tokpushback = 0; |
| 9981 | return lasttoken; |
| 9982 | } |
| 9983 | if (needprompt) { |
| 9984 | setprompt(2); |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 9985 | } |
| 9986 | startlinno = plinno; |
| 9987 | for (;;) { /* until token or start of word found */ |
| 9988 | c = pgetc_macro(); |
| 9989 | |
| 9990 | if ((c != ' ') && (c != '\t') |
| 9991 | #ifdef CONFIG_ASH_ALIAS |
| 9992 | && (c != PEOA) |
| 9993 | #endif |
| 9994 | ) { |
| 9995 | if (c == '#') { |
| 9996 | while ((c = pgetc()) != '\n' && c != PEOF); |
| 9997 | pungetc(); |
| 9998 | } else if (c == '\\') { |
| 9999 | if (pgetc() != '\n') { |
| 10000 | pungetc(); |
| 10001 | goto READTOKEN1; |
| 10002 | } |
| 10003 | startlinno = ++plinno; |
| 10004 | if (doprompt) |
| 10005 | setprompt(2); |
| 10006 | } else { |
| 10007 | const char *p |
| 10008 | = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 10009 | |
| 10010 | if (c != PEOF) { |
| 10011 | if (c == '\n') { |
| 10012 | plinno++; |
| 10013 | needprompt = doprompt; |
| 10014 | } |
| 10015 | |
| 10016 | p = strchr(xxreadtoken_chars, c); |
| 10017 | if (p == NULL) { |
| 10018 | READTOKEN1: |
| 10019 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
| 10020 | } |
| 10021 | |
| 10022 | if (p - xxreadtoken_chars >= xxreadtoken_singles) { |
| 10023 | if (pgetc() == *p) { /* double occurrence? */ |
| 10024 | p += xxreadtoken_doubles + 1; |
| 10025 | } else { |
| 10026 | pungetc(); |
| 10027 | } |
| 10028 | } |
| 10029 | } |
| 10030 | |
| 10031 | return lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 10032 | } |
| 10033 | } |
| 10034 | } |
| 10035 | } |
| 10036 | |
| 10037 | |
| 10038 | #else |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10039 | #define RETURN(token) return lasttoken = token |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10040 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10041 | static int |
| 10042 | xxreadtoken(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10043 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10044 | int c; |
| 10045 | |
| 10046 | if (tokpushback) { |
| 10047 | tokpushback = 0; |
| 10048 | return lasttoken; |
| 10049 | } |
| 10050 | if (needprompt) { |
| 10051 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10052 | } |
| 10053 | startlinno = plinno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10054 | for (;;) { /* until token or start of word found */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10055 | c = pgetc_macro(); |
| 10056 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10057 | case ' ': case '\t': |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 10058 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10059 | case PEOA: |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 10060 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10061 | continue; |
| 10062 | case '#': |
| 10063 | while ((c = pgetc()) != '\n' && c != PEOF); |
| 10064 | pungetc(); |
| 10065 | continue; |
| 10066 | case '\\': |
| 10067 | if (pgetc() == '\n') { |
| 10068 | startlinno = ++plinno; |
| 10069 | if (doprompt) |
| 10070 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10071 | continue; |
| 10072 | } |
| 10073 | pungetc(); |
| 10074 | goto breakloop; |
| 10075 | case '\n': |
| 10076 | plinno++; |
| 10077 | needprompt = doprompt; |
| 10078 | RETURN(TNL); |
| 10079 | case PEOF: |
| 10080 | RETURN(TEOF); |
| 10081 | case '&': |
| 10082 | if (pgetc() == '&') |
| 10083 | RETURN(TAND); |
| 10084 | pungetc(); |
| 10085 | RETURN(TBACKGND); |
| 10086 | case '|': |
| 10087 | if (pgetc() == '|') |
| 10088 | RETURN(TOR); |
| 10089 | pungetc(); |
| 10090 | RETURN(TPIPE); |
| 10091 | case ';': |
| 10092 | if (pgetc() == ';') |
| 10093 | RETURN(TENDCASE); |
| 10094 | pungetc(); |
| 10095 | RETURN(TSEMI); |
| 10096 | case '(': |
| 10097 | RETURN(TLP); |
| 10098 | case ')': |
| 10099 | RETURN(TRP); |
| 10100 | default: |
| 10101 | goto breakloop; |
| 10102 | } |
| 10103 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10104 | breakloop: |
| 10105 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10106 | #undef RETURN |
| 10107 | } |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 10108 | #endif /* NEW_xxreadtoken */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10109 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10110 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10111 | /* |
| 10112 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 10113 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 10114 | * word which marks the end of the document and striptabs is true if |
| 10115 | * leading tabs should be stripped from the document. The argument firstc |
| 10116 | * is the first character of the input token or document. |
| 10117 | * |
| 10118 | * Because C does not have internal subroutines, I have simulated them |
| 10119 | * using goto's to implement the subroutine linkage. The following macros |
| 10120 | * will run code that appears at the end of readtoken1. |
| 10121 | */ |
| 10122 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10123 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 10124 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 10125 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 10126 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 10127 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 10128 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10129 | |
| 10130 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10131 | readtoken1(int firstc, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10132 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10133 | int c = firstc; |
| 10134 | char *out; |
| 10135 | int len; |
| 10136 | char line[EOFMARKLEN + 1]; |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 10137 | struct nodelist *bqlist = 0; |
| 10138 | int quotef = 0; |
| 10139 | int dblquote = 0; |
| 10140 | int varnest = 0; /* levels of variables expansion */ |
| 10141 | int arinest = 0; /* levels of arithmetic expansion */ |
| 10142 | int parenlevel = 0; /* levels of parens in arithmetic */ |
| 10143 | int dqvarnest = 0; /* levels of variables expansion within double quotes */ |
| 10144 | int oldstyle = 0; |
| 10145 | int prevsyntax = 0; /* syntax before arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10146 | #if __GNUC__ |
| 10147 | /* Avoid longjmp clobbering */ |
| 10148 | (void) &out; |
| 10149 | (void) "ef; |
| 10150 | (void) &dblquote; |
| 10151 | (void) &varnest; |
| 10152 | (void) &arinest; |
| 10153 | (void) &parenlevel; |
| 10154 | (void) &dqvarnest; |
| 10155 | (void) &oldstyle; |
| 10156 | (void) &prevsyntax; |
| 10157 | (void) &syntax; |
| 10158 | #endif |
| 10159 | |
| 10160 | startlinno = plinno; |
| 10161 | dblquote = 0; |
| 10162 | if (syntax == DQSYNTAX) |
| 10163 | dblquote = 1; |
| 10164 | quotef = 0; |
| 10165 | bqlist = NULL; |
| 10166 | varnest = 0; |
| 10167 | arinest = 0; |
| 10168 | parenlevel = 0; |
| 10169 | dqvarnest = 0; |
| 10170 | |
| 10171 | STARTSTACKSTR(out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10172 | loop: { /* for each line, until end of word */ |
| 10173 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 10174 | for (;;) { /* until end of line or end of word */ |
| 10175 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
| 10176 | switch(SIT(c, syntax)) { |
| 10177 | case CNL: /* '\n' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10178 | if (syntax == BASESYNTAX) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10179 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10180 | USTPUTC(c, out); |
| 10181 | plinno++; |
| 10182 | if (doprompt) |
| 10183 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10184 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10185 | goto loop; /* continue outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10186 | case CWORD: |
| 10187 | USTPUTC(c, out); |
| 10188 | break; |
| 10189 | case CCTL: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10190 | if (eofmark == NULL || dblquote) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10191 | USTPUTC(CTLESC, out); |
| 10192 | USTPUTC(c, out); |
| 10193 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10194 | case CBACK: /* backslash */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10195 | c = pgetc2(); |
| 10196 | if (c == PEOF) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10197 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10198 | USTPUTC('\\', out); |
| 10199 | pungetc(); |
| 10200 | } else if (c == '\n') { |
| 10201 | if (doprompt) |
| 10202 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10203 | } else { |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 10204 | if (dblquote && |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10205 | c != '\\' && c != '`' && |
| 10206 | c != '$' && ( |
| 10207 | c != '"' || |
"Vladimir N. Oleynik" | 84005af | 2006-01-25 17:53:04 +0000 | [diff] [blame] | 10208 | eofmark != NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10209 | ) { |
| 10210 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10211 | USTPUTC('\\', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10212 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10213 | if (SIT(c, SQSYNTAX) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10214 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10215 | USTPUTC(c, out); |
| 10216 | quotef++; |
| 10217 | } |
| 10218 | break; |
| 10219 | case CSQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10220 | syntax = SQSYNTAX; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10221 | quotemark: |
| 10222 | if (eofmark == NULL) { |
| 10223 | USTPUTC(CTLQUOTEMARK, out); |
| 10224 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10225 | break; |
| 10226 | case CDQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10227 | syntax = DQSYNTAX; |
| 10228 | dblquote = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10229 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10230 | case CENDQUOTE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10231 | if (eofmark != NULL && arinest == 0 && |
| 10232 | varnest == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10233 | USTPUTC(c, out); |
| 10234 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10235 | if (dqvarnest == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10236 | syntax = BASESYNTAX; |
| 10237 | dblquote = 0; |
| 10238 | } |
| 10239 | quotef++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10240 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10241 | } |
| 10242 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10243 | case CVAR: /* '$' */ |
| 10244 | PARSESUB(); /* parse substitution */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10245 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10246 | case CENDVAR: /* '}' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10247 | if (varnest > 0) { |
| 10248 | varnest--; |
| 10249 | if (dqvarnest > 0) { |
| 10250 | dqvarnest--; |
| 10251 | } |
| 10252 | USTPUTC(CTLENDVAR, out); |
| 10253 | } else { |
| 10254 | USTPUTC(c, out); |
| 10255 | } |
| 10256 | break; |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 10257 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10258 | case CLP: /* '(' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10259 | parenlevel++; |
| 10260 | USTPUTC(c, out); |
| 10261 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10262 | case CRP: /* ')' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10263 | if (parenlevel > 0) { |
| 10264 | USTPUTC(c, out); |
| 10265 | --parenlevel; |
| 10266 | } else { |
| 10267 | if (pgetc() == ')') { |
| 10268 | if (--arinest == 0) { |
| 10269 | USTPUTC(CTLENDARI, out); |
| 10270 | syntax = prevsyntax; |
| 10271 | if (syntax == DQSYNTAX) |
| 10272 | dblquote = 1; |
| 10273 | else |
| 10274 | dblquote = 0; |
| 10275 | } else |
| 10276 | USTPUTC(')', out); |
| 10277 | } else { |
| 10278 | /* |
| 10279 | * unbalanced parens |
| 10280 | * (don't 2nd guess - no error) |
| 10281 | */ |
| 10282 | pungetc(); |
| 10283 | USTPUTC(')', out); |
| 10284 | } |
| 10285 | } |
| 10286 | break; |
| 10287 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10288 | case CBQUOTE: /* '`' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10289 | PARSEBACKQOLD(); |
| 10290 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10291 | case CENDFILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10292 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10293 | case CIGN: |
| 10294 | break; |
| 10295 | default: |
| 10296 | if (varnest == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10297 | goto endword; /* exit outer loop */ |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 10298 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 10299 | if (c != PEOA) |
| 10300 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10301 | USTPUTC(c, out); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 10302 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10303 | } |
| 10304 | c = pgetc_macro(); |
| 10305 | } |
| 10306 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10307 | endword: |
| 10308 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10309 | if (syntax == ARISYNTAX) |
| 10310 | synerror("Missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10311 | #endif |
| 10312 | if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10313 | synerror("Unterminated quoted string"); |
| 10314 | if (varnest != 0) { |
| 10315 | startlinno = plinno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10316 | /* { */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10317 | synerror("Missing '}'"); |
| 10318 | } |
| 10319 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10320 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10321 | out = stackblock(); |
| 10322 | if (eofmark == NULL) { |
| 10323 | if ((c == '>' || c == '<') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10324 | && quotef == 0 |
| 10325 | && len <= 2 |
| 10326 | && (*out == '\0' || is_digit(*out))) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10327 | PARSEREDIR(); |
| 10328 | return lasttoken = TREDIR; |
| 10329 | } else { |
| 10330 | pungetc(); |
| 10331 | } |
| 10332 | } |
| 10333 | quoteflag = quotef; |
| 10334 | backquotelist = bqlist; |
| 10335 | grabstackblock(len); |
| 10336 | wordtext = out; |
| 10337 | return lasttoken = TWORD; |
| 10338 | /* end of readtoken routine */ |
| 10339 | |
| 10340 | |
| 10341 | |
| 10342 | /* |
| 10343 | * Check to see whether we are at the end of the here document. When this |
| 10344 | * is called, c is set to the first character of the next input line. If |
| 10345 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 10346 | */ |
| 10347 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10348 | checkend: { |
| 10349 | if (eofmark) { |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 10350 | #ifdef CONFIG_ASH_ALIAS |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10351 | if (c == PEOA) { |
| 10352 | c = pgetc2(); |
| 10353 | } |
| 10354 | #endif |
| 10355 | if (striptabs) { |
| 10356 | while (c == '\t') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10357 | c = pgetc2(); |
| 10358 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10359 | } |
| 10360 | if (c == *eofmark) { |
| 10361 | if (pfgets(line, sizeof line) != NULL) { |
| 10362 | char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10363 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10364 | p = line; |
| 10365 | for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); |
| 10366 | if (*p == '\n' && *q == '\0') { |
| 10367 | c = PEOF; |
| 10368 | plinno++; |
| 10369 | needprompt = doprompt; |
| 10370 | } else { |
| 10371 | pushstring(line, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10372 | } |
| 10373 | } |
| 10374 | } |
| 10375 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10376 | goto checkend_return; |
| 10377 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10378 | |
| 10379 | |
| 10380 | /* |
| 10381 | * Parse a redirection operator. The variable "out" points to a string |
| 10382 | * specifying the fd to be redirected. The variable "c" contains the |
| 10383 | * first character of the redirection operator. |
| 10384 | */ |
| 10385 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10386 | parseredir: { |
| 10387 | char fd = *out; |
| 10388 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10389 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10390 | np = (union node *)stalloc(sizeof (struct nfile)); |
| 10391 | if (c == '>') { |
| 10392 | np->nfile.fd = 1; |
| 10393 | c = pgetc(); |
| 10394 | if (c == '>') |
| 10395 | np->type = NAPPEND; |
| 10396 | else if (c == '|') |
| 10397 | np->type = NCLOBBER; |
| 10398 | else if (c == '&') |
| 10399 | np->type = NTOFD; |
| 10400 | else { |
| 10401 | np->type = NTO; |
| 10402 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10403 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10404 | } else { /* c == '<' */ |
| 10405 | np->nfile.fd = 0; |
| 10406 | switch (c = pgetc()) { |
| 10407 | case '<': |
| 10408 | if (sizeof (struct nfile) != sizeof (struct nhere)) { |
| 10409 | np = (union node *)stalloc(sizeof (struct nhere)); |
| 10410 | np->nfile.fd = 0; |
| 10411 | } |
| 10412 | np->type = NHERE; |
| 10413 | heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); |
| 10414 | heredoc->here = np; |
| 10415 | if ((c = pgetc()) == '-') { |
| 10416 | heredoc->striptabs = 1; |
| 10417 | } else { |
| 10418 | heredoc->striptabs = 0; |
| 10419 | pungetc(); |
| 10420 | } |
| 10421 | break; |
| 10422 | |
| 10423 | case '&': |
| 10424 | np->type = NFROMFD; |
| 10425 | break; |
| 10426 | |
| 10427 | case '>': |
| 10428 | np->type = NFROMTO; |
| 10429 | break; |
| 10430 | |
| 10431 | default: |
| 10432 | np->type = NFROM; |
| 10433 | pungetc(); |
| 10434 | break; |
| 10435 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10436 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10437 | if (fd != '\0') |
| 10438 | np->nfile.fd = digit_val(fd); |
| 10439 | redirnode = np; |
| 10440 | goto parseredir_return; |
| 10441 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10442 | |
| 10443 | |
| 10444 | /* |
| 10445 | * Parse a substitution. At this point, we have read the dollar sign |
| 10446 | * and nothing else. |
| 10447 | */ |
| 10448 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10449 | parsesub: { |
| 10450 | int subtype; |
| 10451 | int typeloc; |
| 10452 | int flags; |
| 10453 | char *p; |
| 10454 | static const char types[] = "}-+?="; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10455 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10456 | c = pgetc(); |
| 10457 | if ( |
| 10458 | c <= PEOA_OR_PEOF || |
| 10459 | (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
| 10460 | ) { |
| 10461 | USTPUTC('$', out); |
| 10462 | pungetc(); |
| 10463 | } else if (c == '(') { /* $(command) or $((arith)) */ |
| 10464 | if (pgetc() == '(') { |
| 10465 | #ifdef CONFIG_ASH_MATH_SUPPORT |
| 10466 | PARSEARITH(); |
| 10467 | #else |
| 10468 | synerror("We unsupport $((arith))"); |
| 10469 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10470 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10471 | pungetc(); |
| 10472 | PARSEBACKQNEW(); |
| 10473 | } |
| 10474 | } else { |
| 10475 | USTPUTC(CTLVAR, out); |
| 10476 | typeloc = out - (char *)stackblock(); |
| 10477 | USTPUTC(VSNORMAL, out); |
| 10478 | subtype = VSNORMAL; |
| 10479 | if (c == '{') { |
| 10480 | c = pgetc(); |
| 10481 | if (c == '#') { |
| 10482 | if ((c = pgetc()) == '}') |
| 10483 | c = '#'; |
| 10484 | else |
| 10485 | subtype = VSLENGTH; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10486 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10487 | else |
| 10488 | subtype = 0; |
| 10489 | } |
| 10490 | if (c > PEOA_OR_PEOF && is_name(c)) { |
| 10491 | do { |
| 10492 | STPUTC(c, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10493 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10494 | } while (c > PEOA_OR_PEOF && is_in_name(c)); |
| 10495 | } else if (is_digit(c)) { |
| 10496 | do { |
| 10497 | STPUTC(c, out); |
| 10498 | c = pgetc(); |
| 10499 | } while (is_digit(c)); |
| 10500 | } |
| 10501 | else if (is_special(c)) { |
| 10502 | USTPUTC(c, out); |
| 10503 | c = pgetc(); |
| 10504 | } |
| 10505 | else |
| 10506 | badsub: synerror("Bad substitution"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10507 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10508 | STPUTC('=', out); |
| 10509 | flags = 0; |
| 10510 | if (subtype == 0) { |
| 10511 | switch (c) { |
| 10512 | case ':': |
| 10513 | flags = VSNUL; |
| 10514 | c = pgetc(); |
| 10515 | /*FALLTHROUGH*/ |
| 10516 | default: |
| 10517 | p = strchr(types, c); |
| 10518 | if (p == NULL) |
| 10519 | goto badsub; |
| 10520 | subtype = p - types + VSNORMAL; |
| 10521 | break; |
| 10522 | case '%': |
| 10523 | case '#': |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10524 | { |
| 10525 | int cc = c; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10526 | subtype = c == '#' ? VSTRIMLEFT : |
| 10527 | VSTRIMRIGHT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10528 | c = pgetc(); |
| 10529 | if (c == cc) |
| 10530 | subtype++; |
| 10531 | else |
| 10532 | pungetc(); |
| 10533 | break; |
| 10534 | } |
| 10535 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10536 | } else { |
| 10537 | pungetc(); |
| 10538 | } |
| 10539 | if (dblquote || arinest) |
| 10540 | flags |= VSQUOTE; |
| 10541 | *((char *)stackblock() + typeloc) = subtype | flags; |
| 10542 | if (subtype != VSNORMAL) { |
| 10543 | varnest++; |
| 10544 | if (dblquote || arinest) { |
| 10545 | dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10546 | } |
| 10547 | } |
| 10548 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10549 | goto parsesub_return; |
| 10550 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10551 | |
| 10552 | |
| 10553 | /* |
| 10554 | * Called to parse command substitutions. Newstyle is set if the command |
| 10555 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 10556 | * list of commands (passed by reference), and savelen is the number of |
| 10557 | * characters on the top of the stack which must be preserved. |
| 10558 | */ |
| 10559 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10560 | parsebackq: { |
| 10561 | struct nodelist **nlpp; |
| 10562 | int savepbq; |
| 10563 | union node *n; |
| 10564 | char *volatile str; |
| 10565 | struct jmploc jmploc; |
| 10566 | struct jmploc *volatile savehandler; |
| 10567 | size_t savelen; |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 10568 | int saveprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10569 | #ifdef __GNUC__ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10570 | (void) &saveprompt; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10571 | #endif |
| 10572 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10573 | savepbq = parsebackquote; |
| 10574 | if (setjmp(jmploc.loc)) { |
| 10575 | if (str) |
| 10576 | ckfree(str); |
| 10577 | parsebackquote = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10578 | handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10579 | longjmp(handler->loc, 1); |
| 10580 | } |
| 10581 | INTOFF; |
| 10582 | str = NULL; |
| 10583 | savelen = out - (char *)stackblock(); |
| 10584 | if (savelen > 0) { |
| 10585 | str = ckmalloc(savelen); |
| 10586 | memcpy(str, stackblock(), savelen); |
| 10587 | } |
| 10588 | savehandler = handler; |
| 10589 | handler = &jmploc; |
| 10590 | INTON; |
| 10591 | if (oldstyle) { |
| 10592 | /* We must read until the closing backquote, giving special |
| 10593 | treatment to some slashes, and then push the string and |
| 10594 | reread it as input, interpreting it normally. */ |
| 10595 | char *pout; |
| 10596 | int pc; |
| 10597 | size_t psavelen; |
| 10598 | char *pstr; |
| 10599 | |
| 10600 | |
| 10601 | STARTSTACKSTR(pout); |
| 10602 | for (;;) { |
| 10603 | if (needprompt) { |
| 10604 | setprompt(2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10605 | } |
| 10606 | switch (pc = pgetc()) { |
| 10607 | case '`': |
| 10608 | goto done; |
| 10609 | |
| 10610 | case '\\': |
| 10611 | if ((pc = pgetc()) == '\n') { |
| 10612 | plinno++; |
| 10613 | if (doprompt) |
| 10614 | setprompt(2); |
| 10615 | /* |
| 10616 | * If eating a newline, avoid putting |
| 10617 | * the newline into the new character |
| 10618 | * stream (via the STPUTC after the |
| 10619 | * switch). |
| 10620 | */ |
| 10621 | continue; |
| 10622 | } |
| 10623 | if (pc != '\\' && pc != '`' && pc != '$' |
| 10624 | && (!dblquote || pc != '"')) |
| 10625 | STPUTC('\\', pout); |
| 10626 | if (pc > PEOA_OR_PEOF) { |
| 10627 | break; |
| 10628 | } |
| 10629 | /* fall through */ |
| 10630 | |
| 10631 | case PEOF: |
| 10632 | #ifdef CONFIG_ASH_ALIAS |
| 10633 | case PEOA: |
| 10634 | #endif |
| 10635 | startlinno = plinno; |
| 10636 | synerror("EOF in backquote substitution"); |
| 10637 | |
| 10638 | case '\n': |
| 10639 | plinno++; |
| 10640 | needprompt = doprompt; |
| 10641 | break; |
| 10642 | |
| 10643 | default: |
| 10644 | break; |
| 10645 | } |
| 10646 | STPUTC(pc, pout); |
| 10647 | } |
| 10648 | done: |
| 10649 | STPUTC('\0', pout); |
| 10650 | psavelen = pout - (char *)stackblock(); |
| 10651 | if (psavelen > 0) { |
| 10652 | pstr = grabstackstr(pout); |
| 10653 | setinputstring(pstr); |
| 10654 | } |
| 10655 | } |
| 10656 | nlpp = &bqlist; |
| 10657 | while (*nlpp) |
| 10658 | nlpp = &(*nlpp)->next; |
| 10659 | *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); |
| 10660 | (*nlpp)->next = NULL; |
| 10661 | parsebackquote = oldstyle; |
| 10662 | |
| 10663 | if (oldstyle) { |
| 10664 | saveprompt = doprompt; |
| 10665 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10666 | } |
| 10667 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10668 | n = list(2); |
| 10669 | |
| 10670 | if (oldstyle) |
| 10671 | doprompt = saveprompt; |
| 10672 | else { |
| 10673 | if (readtoken() != TRP) |
| 10674 | synexpect(TRP); |
| 10675 | } |
| 10676 | |
| 10677 | (*nlpp)->n = n; |
| 10678 | if (oldstyle) { |
| 10679 | /* |
| 10680 | * Start reading from old file again, ignoring any pushed back |
| 10681 | * tokens left from the backquote parsing |
| 10682 | */ |
| 10683 | popfile(); |
| 10684 | tokpushback = 0; |
| 10685 | } |
| 10686 | while (stackblocksize() <= savelen) |
| 10687 | growstackblock(); |
| 10688 | STARTSTACKSTR(out); |
| 10689 | if (str) { |
| 10690 | memcpy(out, str, savelen); |
| 10691 | STADJUST(savelen, out); |
| 10692 | INTOFF; |
| 10693 | ckfree(str); |
| 10694 | str = NULL; |
| 10695 | INTON; |
| 10696 | } |
| 10697 | parsebackquote = savepbq; |
| 10698 | handler = savehandler; |
| 10699 | if (arinest || dblquote) |
| 10700 | USTPUTC(CTLBACKQ | CTLQUOTE, out); |
| 10701 | else |
| 10702 | USTPUTC(CTLBACKQ, out); |
| 10703 | if (oldstyle) |
| 10704 | goto parsebackq_oldreturn; |
| 10705 | else |
| 10706 | goto parsebackq_newreturn; |
| 10707 | } |
| 10708 | |
| 10709 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10710 | /* |
| 10711 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 10712 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10713 | parsearith: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10714 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10715 | if (++arinest == 1) { |
| 10716 | prevsyntax = syntax; |
| 10717 | syntax = ARISYNTAX; |
| 10718 | USTPUTC(CTLARI, out); |
| 10719 | if (dblquote) |
| 10720 | USTPUTC('"',out); |
| 10721 | else |
| 10722 | USTPUTC(' ',out); |
| 10723 | } else { |
| 10724 | /* |
| 10725 | * we collapse embedded arithmetic expansion to |
| 10726 | * parenthesis, which should be equivalent |
| 10727 | */ |
| 10728 | USTPUTC('(', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10729 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10730 | goto parsearith_return; |
| 10731 | } |
| 10732 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10733 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10734 | } /* end of readtoken */ |
| 10735 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10736 | |
| 10737 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10738 | /* |
| 10739 | * Returns true if the text contains nothing to expand (no dollar signs |
| 10740 | * or backquotes). |
| 10741 | */ |
| 10742 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10743 | static int |
| 10744 | noexpand(char *text) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 10745 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10746 | char *p; |
| 10747 | char c; |
| 10748 | |
| 10749 | p = text; |
| 10750 | while ((c = *p++) != '\0') { |
| 10751 | if (c == CTLQUOTEMARK) |
| 10752 | continue; |
| 10753 | if (c == CTLESC) |
| 10754 | p++; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10755 | else if (SIT(c, BASESYNTAX) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10756 | return 0; |
| 10757 | } |
| 10758 | return 1; |
| 10759 | } |
| 10760 | |
| 10761 | |
| 10762 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10763 | * Return of a legal variable name (a letter or underscore followed by zero or |
| 10764 | * more letters, underscores, and digits). |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10765 | */ |
| 10766 | |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10767 | static char * |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10768 | endofname(const char *name) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 10769 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10770 | char *p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10771 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10772 | p = (char *) name; |
| 10773 | if (! is_name(*p)) |
| 10774 | return p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10775 | while (*++p) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10776 | if (! is_in_name(*p)) |
| 10777 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10778 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10779 | return p; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10780 | } |
| 10781 | |
| 10782 | |
| 10783 | /* |
| 10784 | * Called when an unexpected token is read during the parse. The argument |
| 10785 | * is the token that is expected, or -1 if more than one type of token can |
| 10786 | * occur at this point. |
| 10787 | */ |
| 10788 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10789 | static void synexpect(int token) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10790 | { |
| 10791 | char msg[64]; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10792 | int l; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10793 | |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10794 | l = sprintf(msg, "%s unexpected", tokname(lasttoken)); |
| 10795 | if (token >= 0) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10796 | sprintf(msg + l, " (expecting %s)", tokname(token)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10797 | synerror(msg); |
| 10798 | /* NOTREACHED */ |
| 10799 | } |
| 10800 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10801 | static void |
| 10802 | synerror(const char *msg) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10803 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10804 | sh_error("Syntax error: %s", msg); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10805 | /* NOTREACHED */ |
| 10806 | } |
| 10807 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10808 | |
| 10809 | /* |
| 10810 | * called by editline -- any expansions to the prompt |
| 10811 | * should be added here. |
| 10812 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10813 | |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 10814 | #ifdef CONFIG_ASH_EXPAND_PRMT |
| 10815 | static const char * |
| 10816 | expandstr(const char *ps) |
| 10817 | { |
| 10818 | union node n; |
| 10819 | |
| 10820 | /* XXX Fix (char *) cast. */ |
| 10821 | setinputstring((char *)ps); |
| 10822 | readtoken1(pgetc(), DQSYNTAX, nullstr, 0); |
| 10823 | popfile(); |
| 10824 | |
| 10825 | n.narg.type = NARG; |
| 10826 | n.narg.next = NULL; |
| 10827 | n.narg.text = wordtext; |
| 10828 | n.narg.backquote = backquotelist; |
| 10829 | |
| 10830 | expandarg(&n, NULL, 0); |
| 10831 | return stackblock(); |
| 10832 | } |
| 10833 | #endif |
| 10834 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10835 | static void setprompt(int whichprompt) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10836 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10837 | const char *prompt; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 10838 | #ifdef CONFIG_ASH_EXPAND_PRMT |
| 10839 | struct stackmark smark; |
| 10840 | #endif |
| 10841 | |
| 10842 | needprompt = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10843 | |
| 10844 | switch (whichprompt) { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10845 | case 1: |
| 10846 | prompt = ps1val(); |
| 10847 | break; |
| 10848 | case 2: |
| 10849 | prompt = ps2val(); |
| 10850 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10851 | default: /* 0 */ |
| 10852 | prompt = nullstr; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10853 | } |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 10854 | #ifdef CONFIG_ASH_EXPAND_PRMT |
| 10855 | setstackmark(&smark); |
| 10856 | stalloc(stackblocksize()); |
| 10857 | #endif |
| 10858 | putprompt(expandstr(prompt)); |
| 10859 | #ifdef CONFIG_ASH_EXPAND_PRMT |
| 10860 | popstackmark(&smark); |
| 10861 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10862 | } |
| 10863 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10864 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10865 | static const char *const *findkwd(const char *s) |
| 10866 | { |
| 10867 | return bsearch(s, tokname_array + KWDOFFSET, |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 10868 | (sizeof(tokname_array) / sizeof(const char *)) - KWDOFFSET, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10869 | sizeof(const char *), pstrcmp); |
| 10870 | } |
| 10871 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10872 | /* redir.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10873 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10874 | /* |
| 10875 | * Code for dealing with input/output redirection. |
| 10876 | */ |
| 10877 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10878 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10879 | #ifndef PIPE_BUF |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10880 | # define PIPESIZE 4096 /* amount of buffering in a pipe */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10881 | #else |
| 10882 | # define PIPESIZE PIPE_BUF |
| 10883 | #endif |
| 10884 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10885 | /* |
| 10886 | * Open a file in noclobber mode. |
| 10887 | * The code was copied from bash. |
| 10888 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10889 | static inline int |
| 10890 | noclobberopen(const char *fname) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10891 | { |
| 10892 | int r, fd; |
| 10893 | struct stat finfo, finfo2; |
| 10894 | |
| 10895 | /* |
| 10896 | * If the file exists and is a regular file, return an error |
| 10897 | * immediately. |
| 10898 | */ |
| 10899 | r = stat(fname, &finfo); |
| 10900 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 10901 | errno = EEXIST; |
| 10902 | return -1; |
| 10903 | } |
| 10904 | |
| 10905 | /* |
| 10906 | * If the file was not present (r != 0), make sure we open it |
| 10907 | * exclusively so that if it is created before we open it, our open |
| 10908 | * will fail. Make sure that we do not truncate an existing file. |
| 10909 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 10910 | * file was not a regular file, we leave O_EXCL off. |
| 10911 | */ |
| 10912 | if (r != 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10913 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 10914 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10915 | |
| 10916 | /* If the open failed, return the file descriptor right away. */ |
| 10917 | if (fd < 0) |
| 10918 | return fd; |
| 10919 | |
| 10920 | /* |
| 10921 | * OK, the open succeeded, but the file may have been changed from a |
| 10922 | * non-regular file to a regular file between the stat and the open. |
| 10923 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 10924 | * did not exist and is symlinked to an existing file between the stat |
| 10925 | * and open. |
| 10926 | */ |
| 10927 | |
| 10928 | /* |
| 10929 | * If we can open it and fstat the file descriptor, and neither check |
| 10930 | * revealed that it was a regular file, and the file has not been |
| 10931 | * replaced, return the file descriptor. |
| 10932 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10933 | if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode) && |
| 10934 | finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10935 | return fd; |
| 10936 | |
| 10937 | /* The file has been replaced. badness. */ |
| 10938 | close(fd); |
| 10939 | errno = EEXIST; |
| 10940 | return -1; |
| 10941 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10942 | |
| 10943 | /* |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10944 | * Handle here documents. Normally we fork off a process to write the |
| 10945 | * data to a pipe. If the document is short, we can stuff the data in |
| 10946 | * the pipe without forking. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10947 | */ |
| 10948 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10949 | static inline int |
| 10950 | openhere(union node *redir) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10951 | { |
| 10952 | int pip[2]; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10953 | size_t len = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10954 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10955 | if (pipe(pip) < 0) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10956 | sh_error("Pipe call failed"); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10957 | if (redir->type == NHERE) { |
| 10958 | len = strlen(redir->nhere.doc->narg.text); |
| 10959 | if (len <= PIPESIZE) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10960 | bb_full_write(pip[1], redir->nhere.doc->narg.text, len); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10961 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10962 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10963 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10964 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10965 | close(pip[0]); |
| 10966 | signal(SIGINT, SIG_IGN); |
| 10967 | signal(SIGQUIT, SIG_IGN); |
| 10968 | signal(SIGHUP, SIG_IGN); |
| 10969 | #ifdef SIGTSTP |
| 10970 | signal(SIGTSTP, SIG_IGN); |
| 10971 | #endif |
| 10972 | signal(SIGPIPE, SIG_DFL); |
| 10973 | if (redir->type == NHERE) |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10974 | bb_full_write(pip[1], redir->nhere.doc->narg.text, len); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10975 | else |
| 10976 | expandhere(redir->nhere.doc, pip[1]); |
| 10977 | _exit(0); |
| 10978 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10979 | out: |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10980 | close(pip[1]); |
| 10981 | return pip[0]; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10982 | } |
| 10983 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10984 | static int |
| 10985 | openredirect(union node *redir) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10986 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10987 | char *fname; |
| 10988 | int f; |
| 10989 | |
| 10990 | switch (redir->nfile.type) { |
| 10991 | case NFROM: |
| 10992 | fname = redir->nfile.expfname; |
| 10993 | if ((f = open(fname, O_RDONLY)) < 0) |
| 10994 | goto eopen; |
| 10995 | break; |
| 10996 | case NFROMTO: |
| 10997 | fname = redir->nfile.expfname; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10998 | if ((f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10999 | goto ecreate; |
| 11000 | break; |
| 11001 | case NTO: |
| 11002 | /* Take care of noclobber mode. */ |
| 11003 | if (Cflag) { |
| 11004 | fname = redir->nfile.expfname; |
| 11005 | if ((f = noclobberopen(fname)) < 0) |
| 11006 | goto ecreate; |
| 11007 | break; |
| 11008 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11009 | /* FALLTHROUGH */ |
| 11010 | case NCLOBBER: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11011 | fname = redir->nfile.expfname; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11012 | if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11013 | goto ecreate; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11014 | break; |
| 11015 | case NAPPEND: |
| 11016 | fname = redir->nfile.expfname; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11017 | if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11018 | goto ecreate; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11019 | break; |
| 11020 | default: |
| 11021 | #ifdef DEBUG |
| 11022 | abort(); |
| 11023 | #endif |
| 11024 | /* Fall through to eliminate warning. */ |
| 11025 | case NTOFD: |
| 11026 | case NFROMFD: |
| 11027 | f = -1; |
| 11028 | break; |
| 11029 | case NHERE: |
| 11030 | case NXHERE: |
| 11031 | f = openhere(redir); |
| 11032 | break; |
| 11033 | } |
| 11034 | |
| 11035 | return f; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11036 | ecreate: |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11037 | sh_error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11038 | eopen: |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11039 | sh_error("cannot open %s: %s", fname, errmsg(errno, E_OPEN)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11040 | } |
| 11041 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11042 | static inline void |
| 11043 | dupredirect(union node *redir, int f) |
| 11044 | { |
| 11045 | int fd = redir->nfile.fd; |
| 11046 | |
| 11047 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
| 11048 | if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ |
| 11049 | copyfd(redir->ndup.dupfd, fd); |
| 11050 | } |
| 11051 | return; |
| 11052 | } |
| 11053 | |
| 11054 | if (f != fd) { |
| 11055 | copyfd(f, fd); |
| 11056 | close(f); |
| 11057 | } |
| 11058 | return; |
| 11059 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11060 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11061 | /* |
| 11062 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 11063 | * old file descriptors are stashed away so that the redirection can be |
| 11064 | * undone by calling popredir. If the REDIR_BACKQ flag is set, then the |
| 11065 | * standard output, and the standard error if it becomes a duplicate of |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11066 | * stdout, is saved in memory. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11067 | */ |
| 11068 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11069 | static void |
| 11070 | redirect(union node *redir, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11071 | { |
| 11072 | union node *n; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11073 | struct redirtab *sv; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 11074 | int i; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11075 | int fd; |
| 11076 | int newfd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11077 | int *p; |
| 11078 | nullredirs++; |
| 11079 | if (!redir) { |
| 11080 | return; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11081 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11082 | sv = NULL; |
| 11083 | INTOFF; |
| 11084 | if (flags & REDIR_PUSH) { |
| 11085 | struct redirtab *q; |
| 11086 | q = ckmalloc(sizeof (struct redirtab)); |
| 11087 | q->next = redirlist; |
| 11088 | redirlist = q; |
| 11089 | q->nullredirs = nullredirs - 1; |
| 11090 | for (i = 0 ; i < 10 ; i++) |
| 11091 | q->renamed[i] = EMPTY; |
| 11092 | nullredirs = 0; |
| 11093 | sv = q; |
| 11094 | } |
| 11095 | n = redir; |
| 11096 | do { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11097 | fd = n->nfile.fd; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11098 | if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) && |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11099 | n->ndup.dupfd == fd) |
| 11100 | continue; /* redirect from/to same file descriptor */ |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11101 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11102 | newfd = openredirect(n); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11103 | if (fd == newfd) |
| 11104 | continue; |
| 11105 | if (sv && *(p = &sv->renamed[fd]) == EMPTY) { |
| 11106 | i = fcntl(fd, F_DUPFD, 10); |
| 11107 | |
| 11108 | if (i == -1) { |
| 11109 | i = errno; |
| 11110 | if (i != EBADF) { |
| 11111 | close(newfd); |
| 11112 | errno = i; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11113 | sh_error("%d: %m", fd); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11114 | /* NOTREACHED */ |
| 11115 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11116 | } else { |
| 11117 | *p = i; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11118 | close(fd); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11119 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11120 | } else { |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11121 | close(fd); |
| 11122 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11123 | dupredirect(n, newfd); |
| 11124 | } while ((n = n->nfile.next)); |
| 11125 | INTON; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 11126 | if (flags & REDIR_SAVEFD2 && sv && sv->renamed[2] >= 0) |
| 11127 | preverrout_fd = sv->renamed[2]; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11128 | } |
| 11129 | |
| 11130 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11131 | /* |
| 11132 | * Undo the effects of the last redirection. |
| 11133 | */ |
| 11134 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11135 | void |
| 11136 | popredir(int drop) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11137 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11138 | struct redirtab *rp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11139 | int i; |
| 11140 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11141 | if (--nullredirs >= 0) |
| 11142 | return; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11143 | INTOFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11144 | rp = redirlist; |
| 11145 | for (i = 0 ; i < 10 ; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11146 | if (rp->renamed[i] != EMPTY) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11147 | if (!drop) { |
| 11148 | close(i); |
| 11149 | copyfd(rp->renamed[i], i); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11150 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11151 | close(rp->renamed[i]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11152 | } |
| 11153 | } |
| 11154 | redirlist = rp->next; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11155 | nullredirs = rp->nullredirs; |
| 11156 | ckfree(rp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11157 | INTON; |
| 11158 | } |
| 11159 | |
| 11160 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11161 | * Undo all redirections. Called on error or interrupt. |
| 11162 | */ |
| 11163 | |
| 11164 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11165 | * Discard all saved file descriptors. |
| 11166 | */ |
| 11167 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11168 | void |
| 11169 | clearredir(int drop) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11170 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11171 | for (;;) { |
| 11172 | nullredirs = 0; |
| 11173 | if (!redirlist) |
| 11174 | break; |
| 11175 | popredir(drop); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11176 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11177 | } |
| 11178 | |
| 11179 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11180 | /* |
| 11181 | * Copy a file descriptor to be >= to. Returns -1 |
| 11182 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 11183 | * file descriptors left. |
| 11184 | */ |
| 11185 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11186 | int |
| 11187 | copyfd(int from, int to) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11188 | { |
| 11189 | int newfd; |
| 11190 | |
| 11191 | newfd = fcntl(from, F_DUPFD, to); |
| 11192 | if (newfd < 0) { |
| 11193 | if (errno == EMFILE) |
| 11194 | return EMPTY; |
| 11195 | else |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11196 | sh_error("%d: %m", from); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11197 | } |
| 11198 | return newfd; |
| 11199 | } |
| 11200 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 11201 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11202 | int |
| 11203 | redirectsafe(union node *redir, int flags) |
| 11204 | { |
| 11205 | int err; |
| 11206 | volatile int saveint; |
| 11207 | struct jmploc *volatile savehandler = handler; |
| 11208 | struct jmploc jmploc; |
| 11209 | |
| 11210 | SAVEINT(saveint); |
| 11211 | if (!(err = setjmp(jmploc.loc) * 2)) { |
| 11212 | handler = &jmploc; |
| 11213 | redirect(redir, flags); |
| 11214 | } |
| 11215 | handler = savehandler; |
| 11216 | if (err && exception != EXERROR) |
| 11217 | longjmp(handler->loc, 1); |
| 11218 | RESTOREINT(saveint); |
| 11219 | return err; |
| 11220 | } |
| 11221 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11222 | /* show.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11223 | |
| 11224 | #ifdef DEBUG |
| 11225 | static void shtree(union node *, int, char *, FILE*); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11226 | static void shcmd(union node *, FILE *); |
| 11227 | static void sharg(union node *, FILE *); |
| 11228 | static void indent(int, char *, FILE *); |
| 11229 | static void trstring(char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11230 | |
| 11231 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11232 | void |
| 11233 | showtree(union node *n) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11234 | { |
| 11235 | trputs("showtree called\n"); |
| 11236 | shtree(n, 1, NULL, stdout); |
| 11237 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11238 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11239 | |
| 11240 | static void |
| 11241 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11242 | { |
| 11243 | struct nodelist *lp; |
| 11244 | const char *s; |
| 11245 | |
| 11246 | if (n == NULL) |
| 11247 | return; |
| 11248 | |
| 11249 | indent(ind, pfx, fp); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11250 | switch(n->type) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11251 | case NSEMI: |
| 11252 | s = "; "; |
| 11253 | goto binop; |
| 11254 | case NAND: |
| 11255 | s = " && "; |
| 11256 | goto binop; |
| 11257 | case NOR: |
| 11258 | s = " || "; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11259 | binop: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11260 | shtree(n->nbinary.ch1, ind, NULL, fp); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11261 | /* if (ind < 0) */ |
| 11262 | fputs(s, fp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11263 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 11264 | break; |
| 11265 | case NCMD: |
| 11266 | shcmd(n, fp); |
| 11267 | if (ind >= 0) |
| 11268 | putc('\n', fp); |
| 11269 | break; |
| 11270 | case NPIPE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11271 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11272 | shcmd(lp->n, fp); |
| 11273 | if (lp->next) |
| 11274 | fputs(" | ", fp); |
| 11275 | } |
| 11276 | if (n->npipe.backgnd) |
| 11277 | fputs(" &", fp); |
| 11278 | if (ind >= 0) |
| 11279 | putc('\n', fp); |
| 11280 | break; |
| 11281 | default: |
| 11282 | fprintf(fp, "<node type %d>", n->type); |
| 11283 | if (ind >= 0) |
| 11284 | putc('\n', fp); |
| 11285 | break; |
| 11286 | } |
| 11287 | } |
| 11288 | |
| 11289 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11290 | static void |
| 11291 | shcmd(union node *cmd, FILE *fp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11292 | { |
| 11293 | union node *np; |
| 11294 | int first; |
| 11295 | const char *s; |
| 11296 | int dftfd; |
| 11297 | |
| 11298 | first = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11299 | for (np = cmd->ncmd.args ; np ; np = np->narg.next) { |
| 11300 | if (! first) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11301 | putchar(' '); |
| 11302 | sharg(np, fp); |
| 11303 | first = 0; |
| 11304 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11305 | for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) { |
| 11306 | if (! first) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11307 | putchar(' '); |
| 11308 | switch (np->nfile.type) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11309 | case NTO: s = ">"; dftfd = 1; break; |
| 11310 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 11311 | case NAPPEND: s = ">>"; dftfd = 1; break; |
| 11312 | case NTOFD: s = ">&"; dftfd = 1; break; |
| 11313 | case NFROM: s = "<"; dftfd = 0; break; |
| 11314 | case NFROMFD: s = "<&"; dftfd = 0; break; |
| 11315 | case NFROMTO: s = "<>"; dftfd = 0; break; |
| 11316 | default: s = "*error*"; dftfd = 0; break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11317 | } |
| 11318 | if (np->nfile.fd != dftfd) |
| 11319 | fprintf(fp, "%d", np->nfile.fd); |
| 11320 | fputs(s, fp); |
| 11321 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 11322 | fprintf(fp, "%d", np->ndup.dupfd); |
| 11323 | } else { |
| 11324 | sharg(np->nfile.fname, fp); |
| 11325 | } |
| 11326 | first = 0; |
| 11327 | } |
| 11328 | } |
| 11329 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 11330 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11331 | |
| 11332 | static void |
| 11333 | sharg(union node *arg, FILE *fp) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11334 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11335 | char *p; |
| 11336 | struct nodelist *bqlist; |
| 11337 | int subtype; |
| 11338 | |
| 11339 | if (arg->type != NARG) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11340 | out1fmt("<node type %d>\n", arg->type); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11341 | abort(); |
| 11342 | } |
| 11343 | bqlist = arg->narg.backquote; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11344 | for (p = arg->narg.text ; *p ; p++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11345 | switch (*p) { |
| 11346 | case CTLESC: |
| 11347 | putc(*++p, fp); |
| 11348 | break; |
| 11349 | case CTLVAR: |
| 11350 | putc('$', fp); |
| 11351 | putc('{', fp); |
| 11352 | subtype = *++p; |
| 11353 | if (subtype == VSLENGTH) |
| 11354 | putc('#', fp); |
| 11355 | |
| 11356 | while (*p != '=') |
| 11357 | putc(*p++, fp); |
| 11358 | |
| 11359 | if (subtype & VSNUL) |
| 11360 | putc(':', fp); |
| 11361 | |
| 11362 | switch (subtype & VSTYPE) { |
| 11363 | case VSNORMAL: |
| 11364 | putc('}', fp); |
| 11365 | break; |
| 11366 | case VSMINUS: |
| 11367 | putc('-', fp); |
| 11368 | break; |
| 11369 | case VSPLUS: |
| 11370 | putc('+', fp); |
| 11371 | break; |
| 11372 | case VSQUESTION: |
| 11373 | putc('?', fp); |
| 11374 | break; |
| 11375 | case VSASSIGN: |
| 11376 | putc('=', fp); |
| 11377 | break; |
| 11378 | case VSTRIMLEFT: |
| 11379 | putc('#', fp); |
| 11380 | break; |
| 11381 | case VSTRIMLEFTMAX: |
| 11382 | putc('#', fp); |
| 11383 | putc('#', fp); |
| 11384 | break; |
| 11385 | case VSTRIMRIGHT: |
| 11386 | putc('%', fp); |
| 11387 | break; |
| 11388 | case VSTRIMRIGHTMAX: |
| 11389 | putc('%', fp); |
| 11390 | putc('%', fp); |
| 11391 | break; |
| 11392 | case VSLENGTH: |
| 11393 | break; |
| 11394 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11395 | out1fmt("<subtype %d>", subtype); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11396 | } |
| 11397 | break; |
| 11398 | case CTLENDVAR: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11399 | putc('}', fp); |
| 11400 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11401 | case CTLBACKQ: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11402 | case CTLBACKQ|CTLQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11403 | putc('$', fp); |
| 11404 | putc('(', fp); |
| 11405 | shtree(bqlist->n, -1, NULL, fp); |
| 11406 | putc(')', fp); |
| 11407 | break; |
| 11408 | default: |
| 11409 | putc(*p, fp); |
| 11410 | break; |
| 11411 | } |
| 11412 | } |
| 11413 | } |
| 11414 | |
| 11415 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11416 | static void |
| 11417 | indent(int amount, char *pfx, FILE *fp) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11418 | { |
| 11419 | int i; |
| 11420 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11421 | for (i = 0 ; i < amount ; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11422 | if (pfx && i == amount - 1) |
| 11423 | fputs(pfx, fp); |
| 11424 | putc('\t', fp); |
| 11425 | } |
| 11426 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11427 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11428 | |
| 11429 | |
| 11430 | /* |
| 11431 | * Debugging stuff. |
| 11432 | */ |
| 11433 | |
| 11434 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11435 | FILE *tracefile; |
| 11436 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11437 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11438 | void |
| 11439 | trputc(int c) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11440 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11441 | if (debug != 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11442 | return; |
| 11443 | putc(c, tracefile); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11444 | } |
| 11445 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11446 | void |
| 11447 | trace(const char *fmt, ...) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11448 | { |
| 11449 | va_list va; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11450 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11451 | if (debug != 1) |
| 11452 | return; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11453 | va_start(va, fmt); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11454 | (void) vfprintf(tracefile, fmt, va); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11455 | va_end(va); |
| 11456 | } |
| 11457 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11458 | void |
| 11459 | tracev(const char *fmt, va_list va) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11460 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11461 | if (debug != 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11462 | return; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11463 | (void) vfprintf(tracefile, fmt, va); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11464 | } |
| 11465 | |
| 11466 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11467 | void |
| 11468 | trputs(const char *s) |
| 11469 | { |
| 11470 | if (debug != 1) |
| 11471 | return; |
| 11472 | fputs(s, tracefile); |
| 11473 | } |
| 11474 | |
| 11475 | |
| 11476 | static void |
| 11477 | trstring(char *s) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11478 | { |
| 11479 | char *p; |
| 11480 | char c; |
| 11481 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11482 | if (debug != 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11483 | return; |
| 11484 | putc('"', tracefile); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11485 | for (p = s ; *p ; p++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11486 | switch (*p) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11487 | case '\n': c = 'n'; goto backslash; |
| 11488 | case '\t': c = 't'; goto backslash; |
| 11489 | case '\r': c = 'r'; goto backslash; |
| 11490 | case '"': c = '"'; goto backslash; |
| 11491 | case '\\': c = '\\'; goto backslash; |
| 11492 | case CTLESC: c = 'e'; goto backslash; |
| 11493 | case CTLVAR: c = 'v'; goto backslash; |
| 11494 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash; |
| 11495 | case CTLBACKQ: c = 'q'; goto backslash; |
| 11496 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash; |
| 11497 | backslash: putc('\\', tracefile); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11498 | putc(c, tracefile); |
| 11499 | break; |
| 11500 | default: |
| 11501 | if (*p >= ' ' && *p <= '~') |
| 11502 | putc(*p, tracefile); |
| 11503 | else { |
| 11504 | putc('\\', tracefile); |
| 11505 | putc(*p >> 6 & 03, tracefile); |
| 11506 | putc(*p >> 3 & 07, tracefile); |
| 11507 | putc(*p & 07, tracefile); |
| 11508 | } |
| 11509 | break; |
| 11510 | } |
| 11511 | } |
| 11512 | putc('"', tracefile); |
| 11513 | } |
| 11514 | |
| 11515 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11516 | void |
| 11517 | trargs(char **ap) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11518 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11519 | if (debug != 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11520 | return; |
| 11521 | while (*ap) { |
| 11522 | trstring(*ap++); |
| 11523 | if (*ap) |
| 11524 | putc(' ', tracefile); |
| 11525 | else |
| 11526 | putc('\n', tracefile); |
| 11527 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11528 | } |
| 11529 | |
| 11530 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11531 | void |
| 11532 | opentrace(void) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 11533 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11534 | char s[100]; |
| 11535 | #ifdef O_APPEND |
| 11536 | int flags; |
| 11537 | #endif |
| 11538 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11539 | if (debug != 1) { |
| 11540 | if (tracefile) |
| 11541 | fflush(tracefile); |
| 11542 | /* leave open because libedit might be using it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11543 | return; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11544 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11545 | scopy("./trace", s); |
| 11546 | if (tracefile) { |
| 11547 | if (!freopen(s, "a", tracefile)) { |
| 11548 | fprintf(stderr, "Can't re-open %s\n", s); |
| 11549 | debug = 0; |
| 11550 | return; |
| 11551 | } |
| 11552 | } else { |
| 11553 | if ((tracefile = fopen(s, "a")) == NULL) { |
| 11554 | fprintf(stderr, "Can't open %s\n", s); |
| 11555 | debug = 0; |
| 11556 | return; |
| 11557 | } |
| 11558 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11559 | #ifdef O_APPEND |
| 11560 | if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0) |
| 11561 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 11562 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11563 | setlinebuf(tracefile); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11564 | fputs("\nTracing started.\n", tracefile); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11565 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11566 | #endif /* DEBUG */ |
| 11567 | |
| 11568 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11569 | /* trap.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11570 | |
| 11571 | /* |
| 11572 | * Sigmode records the current value of the signal handlers for the various |
| 11573 | * modes. A value of zero means that the current handler is not known. |
| 11574 | * S_HARD_IGN indicates that the signal was ignored on entry to the shell, |
| 11575 | */ |
| 11576 | |
| 11577 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 11578 | #define S_CATCH 2 /* signal is caught */ |
| 11579 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
| 11580 | #define S_HARD_IGN 4 /* signal is ignored permenantly */ |
| 11581 | #define S_RESET 5 /* temporary - to reset a hard ignored sig */ |
| 11582 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11583 | |
| 11584 | |
| 11585 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11586 | * The trap builtin. |
| 11587 | */ |
| 11588 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11589 | int |
| 11590 | trapcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11591 | { |
| 11592 | char *action; |
| 11593 | char **ap; |
| 11594 | int signo; |
| 11595 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11596 | nextopt(nullstr); |
| 11597 | ap = argptr; |
| 11598 | if (!*ap) { |
| 11599 | for (signo = 0 ; signo < NSIG ; signo++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11600 | if (trap[signo] != NULL) { |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11601 | const char *sn; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11602 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11603 | sn = u_signal_names(0, &signo, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11604 | if (sn == NULL) |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11605 | sn = "???"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11606 | out1fmt("trap -- %s %s\n", |
| 11607 | single_quote(trap[signo]), sn); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11608 | } |
| 11609 | } |
| 11610 | return 0; |
| 11611 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11612 | if (!ap[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11613 | action = NULL; |
| 11614 | else |
| 11615 | action = *ap++; |
| 11616 | while (*ap) { |
| 11617 | if ((signo = decode_signal(*ap, 0)) < 0) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11618 | sh_error("%s: bad trap", *ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11619 | INTOFF; |
| 11620 | if (action) { |
| 11621 | if (action[0] == '-' && action[1] == '\0') |
| 11622 | action = NULL; |
| 11623 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11624 | action = savestr(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11625 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11626 | if (trap[signo]) |
| 11627 | ckfree(trap[signo]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11628 | trap[signo] = action; |
| 11629 | if (signo != 0) |
| 11630 | setsignal(signo); |
| 11631 | INTON; |
| 11632 | ap++; |
| 11633 | } |
| 11634 | return 0; |
| 11635 | } |
| 11636 | |
| 11637 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11638 | /* |
| 11639 | * Clear traps on a fork. |
| 11640 | */ |
| 11641 | |
| 11642 | void |
| 11643 | clear_traps(void) |
| 11644 | { |
| 11645 | char **tp; |
| 11646 | |
| 11647 | for (tp = trap ; tp < &trap[NSIG] ; tp++) { |
| 11648 | if (*tp && **tp) { /* trap not NULL or SIG_IGN */ |
| 11649 | INTOFF; |
| 11650 | ckfree(*tp); |
| 11651 | *tp = NULL; |
| 11652 | if (tp != &trap[0]) |
| 11653 | setsignal(tp - trap); |
| 11654 | INTON; |
| 11655 | } |
| 11656 | } |
| 11657 | } |
| 11658 | |
| 11659 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11660 | /* |
| 11661 | * Set the signal handler for the specified signal. The routine figures |
| 11662 | * out what it should be set to. |
| 11663 | */ |
| 11664 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11665 | void |
| 11666 | setsignal(int signo) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11667 | { |
| 11668 | int action; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11669 | char *t, tsig; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11670 | struct sigaction act; |
| 11671 | |
| 11672 | if ((t = trap[signo]) == NULL) |
| 11673 | action = S_DFL; |
| 11674 | else if (*t != '\0') |
| 11675 | action = S_CATCH; |
| 11676 | else |
| 11677 | action = S_IGN; |
| 11678 | if (rootshell && action == S_DFL) { |
| 11679 | switch (signo) { |
| 11680 | case SIGINT: |
| 11681 | if (iflag || minusc || sflag == 0) |
| 11682 | action = S_CATCH; |
| 11683 | break; |
| 11684 | case SIGQUIT: |
| 11685 | #ifdef DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11686 | if (debug) |
| 11687 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11688 | #endif |
| 11689 | /* FALLTHROUGH */ |
| 11690 | case SIGTERM: |
| 11691 | if (iflag) |
| 11692 | action = S_IGN; |
| 11693 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11694 | #if JOBS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11695 | case SIGTSTP: |
| 11696 | case SIGTTOU: |
| 11697 | if (mflag) |
| 11698 | action = S_IGN; |
| 11699 | break; |
| 11700 | #endif |
| 11701 | } |
| 11702 | } |
| 11703 | |
| 11704 | t = &sigmode[signo - 1]; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11705 | tsig = *t; |
| 11706 | if (tsig == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11707 | /* |
| 11708 | * current setting unknown |
| 11709 | */ |
| 11710 | if (sigaction(signo, 0, &act) == -1) { |
| 11711 | /* |
| 11712 | * Pretend it worked; maybe we should give a warning |
| 11713 | * here, but other shells don't. We don't alter |
| 11714 | * sigmode, so that we retry every time. |
| 11715 | */ |
| 11716 | return; |
| 11717 | } |
| 11718 | if (act.sa_handler == SIG_IGN) { |
| 11719 | if (mflag && (signo == SIGTSTP || |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11720 | signo == SIGTTIN || signo == SIGTTOU)) { |
| 11721 | tsig = S_IGN; /* don't hard ignore these */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11722 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11723 | tsig = S_HARD_IGN; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11724 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11725 | tsig = S_RESET; /* force to be set */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11726 | } |
| 11727 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11728 | if (tsig == S_HARD_IGN || tsig == action) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11729 | return; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11730 | switch (action) { |
| 11731 | case S_CATCH: |
| 11732 | act.sa_handler = onsig; |
| 11733 | break; |
| 11734 | case S_IGN: |
| 11735 | act.sa_handler = SIG_IGN; |
| 11736 | break; |
| 11737 | default: |
| 11738 | act.sa_handler = SIG_DFL; |
| 11739 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11740 | *t = action; |
| 11741 | act.sa_flags = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11742 | sigfillset(&act.sa_mask); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11743 | sigaction(signo, &act, 0); |
| 11744 | } |
| 11745 | |
| 11746 | /* |
| 11747 | * Ignore a signal. |
| 11748 | */ |
| 11749 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11750 | void |
| 11751 | ignoresig(int signo) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11752 | { |
| 11753 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 11754 | signal(signo, SIG_IGN); |
| 11755 | } |
| 11756 | sigmode[signo - 1] = S_HARD_IGN; |
| 11757 | } |
| 11758 | |
| 11759 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11760 | /* |
| 11761 | * Signal handler. |
| 11762 | */ |
| 11763 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11764 | void |
| 11765 | onsig(int signo) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11766 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11767 | gotsig[signo - 1] = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11768 | pendingsigs = signo; |
| 11769 | |
| 11770 | if (exsig || (signo == SIGINT && !trap[SIGINT])) { |
| 11771 | if (!suppressint) |
| 11772 | onint(); |
| 11773 | intpending = 1; |
| 11774 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11775 | } |
| 11776 | |
| 11777 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11778 | /* |
| 11779 | * Called to execute a trap. Perhaps we should avoid entering new trap |
| 11780 | * handlers while we are executing a trap handler. |
| 11781 | */ |
| 11782 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11783 | int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11784 | dotrap(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11785 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11786 | char *p; |
| 11787 | char *q; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11788 | int i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11789 | int savestatus; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11790 | int skip = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11791 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11792 | savestatus = exitstatus; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11793 | pendingsigs = 0; |
| 11794 | xbarrier(); |
| 11795 | |
| 11796 | for (i = 0, q = gotsig; i < NSIG - 1; i++, q++) { |
| 11797 | if (!*q) |
| 11798 | continue; |
| 11799 | *q = 0; |
| 11800 | |
| 11801 | p = trap[i + 1]; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11802 | if (!p) |
| 11803 | continue; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11804 | skip = evalstring(p, SKIPEVAL); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11805 | exitstatus = savestatus; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11806 | if (skip) |
| 11807 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11808 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11809 | |
| 11810 | return skip; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11811 | } |
| 11812 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11813 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11814 | /* |
| 11815 | * Controls whether the shell is interactive or not. |
| 11816 | */ |
| 11817 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11818 | void |
| 11819 | setinteractive(int on) |
| 11820 | { |
| 11821 | static int is_interactive; |
| 11822 | |
| 11823 | if (++on == is_interactive) |
| 11824 | return; |
| 11825 | is_interactive = on; |
| 11826 | setsignal(SIGINT); |
| 11827 | setsignal(SIGQUIT); |
| 11828 | setsignal(SIGTERM); |
| 11829 | #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET |
| 11830 | if(is_interactive > 1) { |
| 11831 | /* Looks like they want an interactive shell */ |
| 11832 | static int do_banner; |
| 11833 | |
| 11834 | if(!do_banner) { |
| 11835 | out1fmt( |
| 11836 | "\n\n" BB_BANNER " Built-in shell (ash)\n" |
| 11837 | "Enter 'help' for a list of built-in commands.\n\n"); |
| 11838 | do_banner++; |
| 11839 | } |
| 11840 | } |
| 11841 | #endif |
| 11842 | } |
| 11843 | |
| 11844 | |
| 11845 | #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET |
| 11846 | /*** List the available builtins ***/ |
| 11847 | |
| 11848 | static int helpcmd(int argc, char **argv) |
| 11849 | { |
| 11850 | int col, i; |
| 11851 | |
| 11852 | out1fmt("\nBuilt-in commands:\n-------------------\n"); |
| 11853 | for (col = 0, i = 0; i < NUMBUILTINS; i++) { |
| 11854 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
| 11855 | builtincmd[i].name + 1); |
| 11856 | if (col > 60) { |
| 11857 | out1fmt("\n"); |
| 11858 | col = 0; |
| 11859 | } |
| 11860 | } |
| 11861 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
| 11862 | { |
| 11863 | extern const struct BB_applet applets[]; |
| 11864 | extern const size_t NUM_APPLETS; |
| 11865 | |
| 11866 | for (i = 0; i < NUM_APPLETS; i++) { |
| 11867 | |
| 11868 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), applets[i].name); |
| 11869 | if (col > 60) { |
| 11870 | out1fmt("\n"); |
| 11871 | col = 0; |
| 11872 | } |
| 11873 | } |
| 11874 | } |
| 11875 | #endif |
| 11876 | out1fmt("\n\n"); |
| 11877 | return EXIT_SUCCESS; |
| 11878 | } |
| 11879 | #endif /* CONFIG_FEATURE_SH_EXTRA_QUIET */ |
| 11880 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11881 | /* |
| 11882 | * Called to exit the shell. |
| 11883 | */ |
| 11884 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11885 | void |
| 11886 | exitshell(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11887 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11888 | struct jmploc loc; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11889 | char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11890 | int status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11891 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11892 | status = exitstatus; |
| 11893 | TRACE(("pid %d, exitshell(%d)\n", getpid(), status)); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11894 | if (setjmp(loc.loc)) { |
| 11895 | if (exception == EXEXIT) |
| 11896 | _exit(exitstatus); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11897 | goto out; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11898 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11899 | handler = &loc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11900 | if ((p = trap[0])) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11901 | trap[0] = NULL; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11902 | evalstring(p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11903 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11904 | flushall(); |
Eric Andersen | 5dcf15e | 2004-07-24 12:44:13 +0000 | [diff] [blame] | 11905 | setjobctl(0); |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 11906 | #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY |
| 11907 | if (iflag && rootshell) { |
| 11908 | const char *hp = lookupvar("HISTFILE"); |
| 11909 | |
| 11910 | if(hp != NULL ) |
| 11911 | save_history ( hp ); |
| 11912 | } |
| 11913 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11914 | out: |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 11915 | _exit(status); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11916 | /* NOTREACHED */ |
| 11917 | } |
| 11918 | |
| 11919 | static int decode_signal(const char *string, int minsig) |
| 11920 | { |
| 11921 | int signo; |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11922 | const char *name = u_signal_names(string, &signo, minsig); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11923 | |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11924 | return name ? signo : -1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11925 | } |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 11926 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11927 | /* var.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11928 | |
| 11929 | static struct var *vartab[VTABSIZE]; |
| 11930 | |
| 11931 | static int vpcmp(const void *, const void *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11932 | static struct var **findvar(struct var **, const char *); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11933 | |
| 11934 | /* |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 11935 | * Initialize the variable symbol tables and import the environment |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11936 | */ |
| 11937 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11938 | |
| 11939 | #ifdef CONFIG_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11940 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11941 | * Safe version of setvar, returns 1 on success 0 on failure. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11942 | */ |
| 11943 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11944 | int |
| 11945 | setvarsafe(const char *name, const char *val, int flags) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11946 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11947 | int err; |
| 11948 | volatile int saveint; |
| 11949 | struct jmploc *volatile savehandler = handler; |
| 11950 | struct jmploc jmploc; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11951 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11952 | SAVEINT(saveint); |
| 11953 | if (setjmp(jmploc.loc)) |
| 11954 | err = 1; |
| 11955 | else { |
| 11956 | handler = &jmploc; |
| 11957 | setvar(name, val, flags); |
| 11958 | err = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11959 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11960 | handler = savehandler; |
| 11961 | RESTOREINT(saveint); |
| 11962 | return err; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11963 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11964 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11965 | |
| 11966 | /* |
| 11967 | * Set the value of a variable. The flags argument is ored with the |
| 11968 | * flags of the variable. If val is NULL, the variable is unset. |
| 11969 | */ |
| 11970 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11971 | static void |
| 11972 | setvar(const char *name, const char *val, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11973 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11974 | char *p, *q; |
| 11975 | size_t namelen; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11976 | char *nameeq; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11977 | size_t vallen; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11978 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11979 | q = endofname(name); |
| 11980 | p = strchrnul(q, '='); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11981 | namelen = p - name; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11982 | if (!namelen || p != q) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11983 | sh_error("%.*s: bad variable name", namelen, name); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11984 | vallen = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11985 | if (val == NULL) { |
| 11986 | flags |= VUNSET; |
| 11987 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11988 | vallen = strlen(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11989 | } |
| 11990 | INTOFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11991 | p = mempcpy(nameeq = ckmalloc(namelen + vallen + 2), name, namelen); |
Paul Fox | 8de331d | 2005-07-21 12:03:05 +0000 | [diff] [blame] | 11992 | if (val) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11993 | *p++ = '='; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11994 | p = mempcpy(p, val, vallen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11995 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11996 | *p = '\0'; |
| 11997 | setvareq(nameeq, flags | VNOSAVE); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11998 | INTON; |
| 11999 | } |
| 12000 | |
| 12001 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12002 | /* |
| 12003 | * Same as setvar except that the variable and value are passed in |
| 12004 | * the first argument as name=value. Since the first argument will |
| 12005 | * be actually stored in the table, it should not be a string that |
| 12006 | * will go away. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12007 | * Called with interrupts off. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12008 | */ |
| 12009 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12010 | void |
| 12011 | setvareq(char *s, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12012 | { |
| 12013 | struct var *vp, **vpp; |
| 12014 | |
| 12015 | vpp = hashvar(s); |
| 12016 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12017 | vp = *findvar(vpp, s); |
| 12018 | if (vp) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 12019 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 12020 | const char *n; |
| 12021 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12022 | if (flags & VNOSAVE) |
| 12023 | free(s); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 12024 | n = vp->text; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12025 | sh_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12026 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12027 | |
| 12028 | if (flags & VNOSET) |
| 12029 | return; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12030 | |
| 12031 | if (vp->func && (flags & VNOFUNC) == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12032 | (*vp->func)(strchrnul(s, '=') + 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12033 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12034 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 12035 | ckfree(vp->text); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12036 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12037 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
| 12038 | } else { |
| 12039 | if (flags & VNOSET) |
| 12040 | return; |
| 12041 | /* not found */ |
| 12042 | vp = ckmalloc(sizeof (*vp)); |
| 12043 | vp->next = *vpp; |
| 12044 | vp->func = NULL; |
| 12045 | *vpp = vp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12046 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12047 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 12048 | s = savestr(s); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12049 | vp->text = s; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12050 | vp->flags = flags; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12051 | } |
| 12052 | |
| 12053 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12054 | /* |
| 12055 | * Process a linked list of variable assignments. |
| 12056 | */ |
| 12057 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12058 | static void |
| 12059 | listsetvar(struct strlist *list_set_var, int flags) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12060 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12061 | struct strlist *lp = list_set_var; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12062 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12063 | if (!lp) |
| 12064 | return; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12065 | INTOFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12066 | do { |
| 12067 | setvareq(lp->text, flags); |
| 12068 | } while ((lp = lp->next)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12069 | INTON; |
| 12070 | } |
| 12071 | |
| 12072 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12073 | /* |
| 12074 | * Find the value of a variable. Returns NULL if not set. |
| 12075 | */ |
| 12076 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12077 | static char * |
| 12078 | lookupvar(const char *name) |
Eric Andersen | ec07469 | 2001-10-31 11:05:49 +0000 | [diff] [blame] | 12079 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12080 | struct var *v; |
| 12081 | |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 12082 | if ((v = *findvar(hashvar(name), name))) { |
| 12083 | #ifdef DYNAMIC_VAR |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 12084 | /* |
| 12085 | * Dynamic variables are implemented roughly the same way they are |
| 12086 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 12087 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 12088 | * lookup will no longer happen at that point. -- PFM. |
| 12089 | */ |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 12090 | if((v->flags & VDYNAMIC)) |
| 12091 | (*v->func)(NULL); |
| 12092 | #endif |
| 12093 | if(!(v->flags & VUNSET)) |
| 12094 | return strchrnul(v->text, '=') + 1; |
| 12095 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 12096 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12097 | return NULL; |
| 12098 | } |
| 12099 | |
| 12100 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12101 | /* |
| 12102 | * Search the environment of a builtin command. |
| 12103 | */ |
| 12104 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12105 | static char * |
| 12106 | bltinlookup(const char *name) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12107 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12108 | struct strlist *sp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12109 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12110 | for (sp = cmdenviron ; sp ; sp = sp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12111 | if (varequal(sp->text, name)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12112 | return strchrnul(sp->text, '=') + 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12113 | } |
| 12114 | return lookupvar(name); |
| 12115 | } |
| 12116 | |
| 12117 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12118 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12119 | * Generate a list of variables satisfying the given conditions. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12120 | */ |
| 12121 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12122 | static char ** |
| 12123 | listvars(int on, int off, char ***end) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 12124 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12125 | struct var **vpp; |
| 12126 | struct var *vp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12127 | char **ep; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12128 | int mask; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12129 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12130 | STARTSTACKSTR(ep); |
| 12131 | vpp = vartab; |
| 12132 | mask = on | off; |
| 12133 | do { |
| 12134 | for (vp = *vpp ; vp ; vp = vp->next) |
| 12135 | if ((vp->flags & mask) == on) { |
| 12136 | if (ep == stackstrend()) |
| 12137 | ep = growstackstr(); |
| 12138 | *ep++ = (char *) vp->text; |
| 12139 | } |
| 12140 | } while (++vpp < vartab + VTABSIZE); |
| 12141 | if (ep == stackstrend()) |
| 12142 | ep = growstackstr(); |
| 12143 | if (end) |
| 12144 | *end = ep; |
| 12145 | *ep++ = NULL; |
| 12146 | return grabstackstr(ep); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12147 | } |
| 12148 | |
| 12149 | |
| 12150 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12151 | * POSIX requires that 'set' (but not export or readonly) output the |
| 12152 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 12153 | * Maybe we could keep them in an ordered balanced binary tree |
| 12154 | * instead of hashed lists. |
| 12155 | * For now just roll 'em through qsort for printing... |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12156 | */ |
| 12157 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12158 | static int |
| 12159 | showvars(const char *sep_prefix, int on, int off) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12160 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12161 | const char *sep; |
| 12162 | char **ep, **epend; |
| 12163 | |
| 12164 | ep = listvars(on, off, &epend); |
| 12165 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 12166 | |
| 12167 | sep = *sep_prefix ? spcstr : sep_prefix; |
| 12168 | |
| 12169 | for (; ep < epend; ep++) { |
| 12170 | const char *p; |
| 12171 | const char *q; |
| 12172 | |
| 12173 | p = strchrnul(*ep, '='); |
| 12174 | q = nullstr; |
| 12175 | if (*p) |
| 12176 | q = single_quote(++p); |
| 12177 | |
| 12178 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 12179 | } |
| 12180 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12181 | return 0; |
| 12182 | } |
| 12183 | |
| 12184 | |
| 12185 | |
| 12186 | /* |
| 12187 | * The export and readonly commands. |
| 12188 | */ |
| 12189 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12190 | static int |
| 12191 | exportcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12192 | { |
| 12193 | struct var *vp; |
| 12194 | char *name; |
| 12195 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12196 | char **aptr; |
| 12197 | int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; |
| 12198 | int notp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12199 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12200 | notp = nextopt("p") - 'p'; |
| 12201 | if (notp && ((name = *(aptr = argptr)))) { |
| 12202 | do { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12203 | if ((p = strchr(name, '=')) != NULL) { |
| 12204 | p++; |
| 12205 | } else { |
| 12206 | if ((vp = *findvar(hashvar(name), name))) { |
| 12207 | vp->flags |= flag; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12208 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12209 | } |
| 12210 | } |
| 12211 | setvar(name, p, flag); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12212 | } while ((name = *++aptr) != NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12213 | } else { |
| 12214 | showvars(argv[0], flag, 0); |
| 12215 | } |
| 12216 | return 0; |
| 12217 | } |
| 12218 | |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 12219 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12220 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12221 | * Make a variable a local variable. When a variable is made local, it's |
| 12222 | * value and flags are saved in a localvar structure. The saved values |
| 12223 | * will be restored when the shell function returns. We handle the name |
| 12224 | * "-" as a special case. |
| 12225 | */ |
| 12226 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12227 | static inline void |
| 12228 | mklocal(char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12229 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12230 | struct localvar *lvp; |
| 12231 | struct var **vpp; |
| 12232 | struct var *vp; |
| 12233 | |
| 12234 | INTOFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12235 | lvp = ckmalloc(sizeof (struct localvar)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12236 | if (name[0] == '-' && name[1] == '\0') { |
| 12237 | char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12238 | p = ckmalloc(sizeof(optlist)); |
| 12239 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12240 | vp = NULL; |
| 12241 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12242 | char *eq; |
| 12243 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12244 | vpp = hashvar(name); |
| 12245 | vp = *findvar(vpp, name); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12246 | eq = strchr(name, '='); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12247 | if (vp == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12248 | if (eq) |
| 12249 | setvareq(name, VSTRFIXED); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12250 | else |
| 12251 | setvar(name, NULL, VSTRFIXED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12252 | vp = *vpp; /* the new variable */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12253 | lvp->flags = VUNSET; |
| 12254 | } else { |
| 12255 | lvp->text = vp->text; |
| 12256 | lvp->flags = vp->flags; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12257 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 12258 | if (eq) |
| 12259 | setvareq(name, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12260 | } |
| 12261 | } |
| 12262 | lvp->vp = vp; |
| 12263 | lvp->next = localvars; |
| 12264 | localvars = lvp; |
| 12265 | INTON; |
| 12266 | } |
| 12267 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12268 | /* |
| 12269 | * The "local" command. |
| 12270 | */ |
| 12271 | |
| 12272 | static int |
| 12273 | localcmd(int argc, char **argv) |
| 12274 | { |
| 12275 | char *name; |
| 12276 | |
| 12277 | argv = argptr; |
| 12278 | while ((name = *argv++) != NULL) { |
| 12279 | mklocal(name); |
| 12280 | } |
| 12281 | return 0; |
| 12282 | } |
| 12283 | |
| 12284 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12285 | /* |
| 12286 | * Called after a function returns. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12287 | * Interrupts must be off. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12288 | */ |
| 12289 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12290 | static void |
| 12291 | poplocalvars(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 12292 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12293 | struct localvar *lvp; |
| 12294 | struct var *vp; |
| 12295 | |
| 12296 | while ((lvp = localvars) != NULL) { |
| 12297 | localvars = lvp->next; |
| 12298 | vp = lvp->vp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12299 | TRACE(("poplocalvar %s", vp ? vp->text : "-")); |
| 12300 | if (vp == NULL) { /* $- saved */ |
| 12301 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 12302 | ckfree(lvp->text); |
| 12303 | optschanged(); |
| 12304 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
| 12305 | unsetvar(vp->text); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12306 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12307 | if (vp->func) |
| 12308 | (*vp->func)(strchrnul(lvp->text, '=') + 1); |
| 12309 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
| 12310 | ckfree(vp->text); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12311 | vp->flags = lvp->flags; |
| 12312 | vp->text = lvp->text; |
| 12313 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12314 | ckfree(lvp); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12315 | } |
| 12316 | } |
| 12317 | |
| 12318 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12319 | /* |
| 12320 | * The unset builtin command. We unset the function before we unset the |
| 12321 | * variable to allow a function to be unset when there is a readonly variable |
| 12322 | * with the same name. |
| 12323 | */ |
| 12324 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12325 | int |
| 12326 | unsetcmd(int argc, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12327 | { |
| 12328 | char **ap; |
| 12329 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12330 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12331 | int ret = 0; |
| 12332 | |
| 12333 | while ((i = nextopt("vf")) != '\0') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12334 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12335 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12336 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12337 | for (ap = argptr; *ap ; ap++) { |
| 12338 | if (flag != 'f') { |
| 12339 | i = unsetvar(*ap); |
| 12340 | ret |= i; |
| 12341 | if (!(i & 2)) |
| 12342 | continue; |
| 12343 | } |
| 12344 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12345 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12346 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12347 | return ret & 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12348 | } |
| 12349 | |
| 12350 | |
| 12351 | /* |
| 12352 | * Unset the specified variable. |
| 12353 | */ |
| 12354 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12355 | int |
| 12356 | unsetvar(const char *s) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 12357 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12358 | struct var **vpp; |
| 12359 | struct var *vp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12360 | int retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12361 | |
| 12362 | vpp = findvar(hashvar(s), s); |
| 12363 | vp = *vpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12364 | retval = 2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12365 | if (vp) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12366 | int flags = vp->flags; |
| 12367 | |
| 12368 | retval = 1; |
| 12369 | if (flags & VREADONLY) |
| 12370 | goto out; |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 12371 | #ifdef DYNAMIC_VAR |
| 12372 | vp->flags &= ~VDYNAMIC; |
| 12373 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12374 | if (flags & VUNSET) |
| 12375 | goto ok; |
| 12376 | if ((flags & VSTRFIXED) == 0) { |
| 12377 | INTOFF; |
| 12378 | if ((flags & (VTEXTFIXED|VSTACK)) == 0) |
| 12379 | ckfree(vp->text); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12380 | *vpp = vp->next; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12381 | ckfree(vp); |
| 12382 | INTON; |
| 12383 | } else { |
| 12384 | setvar(s, 0, 0); |
| 12385 | vp->flags &= ~VEXPORT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12386 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12387 | ok: |
| 12388 | retval = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12389 | } |
| 12390 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12391 | out: |
| 12392 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12393 | } |
| 12394 | |
| 12395 | |
| 12396 | |
| 12397 | /* |
| 12398 | * Find the appropriate entry in the hash table from the name. |
| 12399 | */ |
| 12400 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12401 | static struct var ** |
| 12402 | hashvar(const char *p) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 12403 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12404 | unsigned int hashval; |
| 12405 | |
| 12406 | hashval = ((unsigned char) *p) << 4; |
| 12407 | while (*p && *p != '=') |
| 12408 | hashval += (unsigned char) *p++; |
| 12409 | return &vartab[hashval % VTABSIZE]; |
| 12410 | } |
| 12411 | |
| 12412 | |
| 12413 | |
| 12414 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12415 | * Compares two strings up to the first = or '\0'. The first |
| 12416 | * string must be terminated by '='; the second may be terminated by |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12417 | * either '=' or '\0'. |
| 12418 | */ |
| 12419 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12420 | int |
| 12421 | varcmp(const char *p, const char *q) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 12422 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12423 | int c, d; |
| 12424 | |
| 12425 | while ((c = *p) == (d = *q)) { |
| 12426 | if (!c || c == '=') |
| 12427 | goto out; |
| 12428 | p++; |
| 12429 | q++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12430 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12431 | if (c == '=') |
| 12432 | c = 0; |
| 12433 | if (d == '=') |
| 12434 | d = 0; |
| 12435 | out: |
| 12436 | return c - d; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12437 | } |
| 12438 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12439 | static int |
| 12440 | vpcmp(const void *a, const void *b) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12441 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12442 | return varcmp(*(const char **)a, *(const char **)b); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12443 | } |
| 12444 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12445 | static struct var ** |
| 12446 | findvar(struct var **vpp, const char *name) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12447 | { |
| 12448 | for (; *vpp; vpp = &(*vpp)->next) { |
| 12449 | if (varequal((*vpp)->text, name)) { |
| 12450 | break; |
| 12451 | } |
| 12452 | } |
| 12453 | return vpp; |
| 12454 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12455 | /* setmode.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12456 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12457 | #include <sys/times.h> |
| 12458 | |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12459 | static const unsigned char timescmd_str[] = { |
| 12460 | ' ', offsetof(struct tms, tms_utime), |
| 12461 | '\n', offsetof(struct tms, tms_stime), |
| 12462 | ' ', offsetof(struct tms, tms_cutime), |
| 12463 | '\n', offsetof(struct tms, tms_cstime), |
| 12464 | 0 |
| 12465 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12466 | |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12467 | static int timescmd(int ac, char **av) |
| 12468 | { |
| 12469 | long int clk_tck, s, t; |
| 12470 | const unsigned char *p; |
| 12471 | struct tms buf; |
| 12472 | |
| 12473 | clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12474 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12475 | |
| 12476 | p = timescmd_str; |
| 12477 | do { |
| 12478 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 12479 | s = t / clk_tck; |
| 12480 | out1fmt("%ldm%ld.%.3lds%c", |
| 12481 | s/60, s%60, |
| 12482 | ((t - s * clk_tck) * 1000) / clk_tck, |
| 12483 | p[0]); |
| 12484 | } while (*(p += 2)); |
| 12485 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12486 | return 0; |
| 12487 | } |
| 12488 | |
Eric Andersen | d35c5df | 2002-01-09 15:37:36 +0000 | [diff] [blame] | 12489 | #ifdef CONFIG_ASH_MATH_SUPPORT |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 12490 | static arith_t |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12491 | dash_arith(const char *s) |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12492 | { |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 12493 | arith_t result; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12494 | int errcode = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 12495 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12496 | INTOFF; |
| 12497 | result = arith(s, &errcode); |
| 12498 | if (errcode < 0) { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12499 | if (errcode == -3) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12500 | sh_error("exponent less than 0"); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12501 | else if (errcode == -2) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12502 | sh_error("divide by zero"); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12503 | else if (errcode == -5) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12504 | sh_error("expression recursion loop detected"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12505 | else |
| 12506 | synerror(s); |
| 12507 | } |
| 12508 | INTON; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 12509 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12510 | return (result); |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12511 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12512 | |
| 12513 | |
| 12514 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12515 | * The let builtin. partial stolen from GNU Bash, the Bourne Again SHell. |
| 12516 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
| 12517 | * |
| 12518 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12519 | */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12520 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12521 | static int |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12522 | letcmd(int argc, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12523 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12524 | char **ap; |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 12525 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12526 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12527 | ap = argv + 1; |
| 12528 | if(!*ap) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12529 | sh_error("expression expected"); |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12530 | for (ap = argv + 1; *ap; ap++) { |
| 12531 | i = dash_arith(*ap); |
| 12532 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12533 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12534 | return (!i); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12535 | } |
| 12536 | #endif /* CONFIG_ASH_MATH_SUPPORT */ |
| 12537 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12538 | /* miscbltin.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12539 | |
| 12540 | /* |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12541 | * Miscellaneous builtins. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12542 | */ |
| 12543 | |
| 12544 | #undef rflag |
| 12545 | |
| 12546 | #ifdef __GLIBC__ |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12547 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 1 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12548 | typedef enum __rlimit_resource rlim_t; |
| 12549 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12550 | #endif |
| 12551 | |
| 12552 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12553 | /* |
| 12554 | * The read builtin. The -e option causes backslashes to escape the |
| 12555 | * following character. |
| 12556 | * |
| 12557 | * This uses unbuffered input, which may be avoidable in some cases. |
| 12558 | */ |
| 12559 | |
| 12560 | static int |
| 12561 | readcmd(int argc, char **argv) |
| 12562 | { |
| 12563 | char **ap; |
| 12564 | int backslash; |
| 12565 | char c; |
| 12566 | int rflag; |
| 12567 | char *prompt; |
| 12568 | const char *ifs; |
| 12569 | char *p; |
| 12570 | int startword; |
| 12571 | int status; |
| 12572 | int i; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12573 | #if defined(CONFIG_ASH_READ_NCHARS) |
| 12574 | int nch_flag = 0; |
| 12575 | int nchars = 0; |
| 12576 | int silent = 0; |
| 12577 | struct termios tty, old_tty; |
| 12578 | #endif |
| 12579 | #if defined(CONFIG_ASH_READ_TIMEOUT) |
| 12580 | fd_set set; |
| 12581 | struct timeval ts; |
| 12582 | |
| 12583 | ts.tv_sec = ts.tv_usec = 0; |
| 12584 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12585 | |
| 12586 | rflag = 0; |
| 12587 | prompt = NULL; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12588 | #if defined(CONFIG_ASH_READ_NCHARS) && defined(CONFIG_ASH_READ_TIMEOUT) |
| 12589 | while ((i = nextopt("p:rt:n:s")) != '\0') |
| 12590 | #elif defined(CONFIG_ASH_READ_NCHARS) |
| 12591 | while ((i = nextopt("p:rn:s")) != '\0') |
| 12592 | #elif defined(CONFIG_ASH_READ_TIMEOUT) |
Ned Ludd | 2123b7c | 2005-02-09 21:07:23 +0000 | [diff] [blame] | 12593 | while ((i = nextopt("p:rt:")) != '\0') |
| 12594 | #else |
| 12595 | while ((i = nextopt("p:r")) != '\0') |
| 12596 | #endif |
| 12597 | { |
"Vladimir N. Oleynik" | 11d7c52 | 2005-09-26 13:24:45 +0000 | [diff] [blame] | 12598 | switch(i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12599 | case 'p': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12600 | prompt = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12601 | break; |
| 12602 | #if defined(CONFIG_ASH_READ_NCHARS) |
| 12603 | case 'n': |
| 12604 | nchars = strtol(optionarg, &p, 10); |
| 12605 | if (*p) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12606 | sh_error("invalid count"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12607 | nch_flag = (nchars > 0); |
| 12608 | break; |
| 12609 | case 's': |
| 12610 | silent = 1; |
| 12611 | break; |
Ned Ludd | 2123b7c | 2005-02-09 21:07:23 +0000 | [diff] [blame] | 12612 | #endif |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12613 | #if defined(CONFIG_ASH_READ_TIMEOUT) |
| 12614 | case 't': |
"Vladimir N. Oleynik" | 11d7c52 | 2005-09-26 13:24:45 +0000 | [diff] [blame] | 12615 | ts.tv_sec = strtol(optionarg, &p, 10); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12616 | ts.tv_usec = 0; |
| 12617 | if (*p == '.') { |
| 12618 | char *p2; |
| 12619 | if (*++p) { |
| 12620 | int scale; |
"Vladimir N. Oleynik" | 11d7c52 | 2005-09-26 13:24:45 +0000 | [diff] [blame] | 12621 | ts.tv_usec = strtol(p, &p2, 10); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12622 | if (*p2) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12623 | sh_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12624 | scale = p2 - p; |
| 12625 | /* normalize to usec */ |
| 12626 | if (scale > 6) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12627 | sh_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12628 | while (scale++ < 6) |
| 12629 | ts.tv_usec *= 10; |
| 12630 | } |
| 12631 | } else if (*p) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12632 | sh_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12633 | } |
| 12634 | if ( ! ts.tv_sec && ! ts.tv_usec) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12635 | sh_error("invalid timeout"); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12636 | break; |
| 12637 | #endif |
| 12638 | case 'r': |
| 12639 | rflag = 1; |
| 12640 | break; |
| 12641 | default: |
| 12642 | break; |
| 12643 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12644 | } |
| 12645 | if (prompt && isatty(0)) { |
| 12646 | out2str(prompt); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12647 | } |
| 12648 | if (*(ap = argptr) == NULL) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12649 | sh_error("arg count"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12650 | if ((ifs = bltinlookup("IFS")) == NULL) |
| 12651 | ifs = defifs; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12652 | #if defined(CONFIG_ASH_READ_NCHARS) |
| 12653 | if (nch_flag || silent) { |
| 12654 | tcgetattr(0, &tty); |
| 12655 | old_tty = tty; |
| 12656 | if (nch_flag) { |
| 12657 | tty.c_lflag &= ~ICANON; |
| 12658 | tty.c_cc[VMIN] = nchars; |
| 12659 | } |
| 12660 | if (silent) { |
| 12661 | tty.c_lflag &= ~(ECHO|ECHOK|ECHONL); |
| 12662 | |
| 12663 | } |
| 12664 | tcsetattr(0, TCSANOW, &tty); |
| 12665 | } |
| 12666 | #endif |
| 12667 | #if defined(CONFIG_ASH_READ_TIMEOUT) |
| 12668 | if (ts.tv_sec || ts.tv_usec) { |
| 12669 | FD_ZERO (&set); |
| 12670 | FD_SET (0, &set); |
| 12671 | |
| 12672 | i = select (FD_SETSIZE, &set, NULL, NULL, &ts); |
| 12673 | if (!i) { |
| 12674 | #if defined(CONFIG_ASH_READ_NCHARS) |
| 12675 | if (nch_flag) |
| 12676 | tcsetattr(0, TCSANOW, &old_tty); |
| 12677 | #endif |
| 12678 | return 1; |
| 12679 | } |
| 12680 | } |
Ned Ludd | 2123b7c | 2005-02-09 21:07:23 +0000 | [diff] [blame] | 12681 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12682 | status = 0; |
| 12683 | startword = 1; |
| 12684 | backslash = 0; |
| 12685 | STARTSTACKSTR(p); |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12686 | #if defined(CONFIG_ASH_READ_NCHARS) |
| 12687 | while (!nch_flag || nchars--) |
Ned Ludd | 2123b7c | 2005-02-09 21:07:23 +0000 | [diff] [blame] | 12688 | #else |
| 12689 | for (;;) |
| 12690 | #endif |
| 12691 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12692 | if (read(0, &c, 1) != 1) { |
| 12693 | status = 1; |
| 12694 | break; |
| 12695 | } |
| 12696 | if (c == '\0') |
| 12697 | continue; |
| 12698 | if (backslash) { |
| 12699 | backslash = 0; |
| 12700 | if (c != '\n') |
| 12701 | goto put; |
| 12702 | continue; |
| 12703 | } |
| 12704 | if (!rflag && c == '\\') { |
| 12705 | backslash++; |
| 12706 | continue; |
| 12707 | } |
| 12708 | if (c == '\n') |
| 12709 | break; |
| 12710 | if (startword && *ifs == ' ' && strchr(ifs, c)) { |
| 12711 | continue; |
| 12712 | } |
| 12713 | startword = 0; |
| 12714 | if (ap[1] != NULL && strchr(ifs, c) != NULL) { |
| 12715 | STACKSTRNUL(p); |
| 12716 | setvar(*ap, stackblock(), 0); |
| 12717 | ap++; |
| 12718 | startword = 1; |
| 12719 | STARTSTACKSTR(p); |
| 12720 | } else { |
| 12721 | put: |
| 12722 | STPUTC(c, p); |
| 12723 | } |
| 12724 | } |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12725 | #if defined(CONFIG_ASH_READ_NCHARS) |
| 12726 | if (nch_flag || silent) |
| 12727 | tcsetattr(0, TCSANOW, &old_tty); |
| 12728 | #endif |
| 12729 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12730 | STACKSTRNUL(p); |
| 12731 | /* Remove trailing blanks */ |
| 12732 | while ((char *)stackblock() <= --p && strchr(ifs, *p) != NULL) |
| 12733 | *p = '\0'; |
| 12734 | setvar(*ap, stackblock(), 0); |
| 12735 | while (*++ap != NULL) |
| 12736 | setvar(*ap, nullstr, 0); |
| 12737 | return status; |
| 12738 | } |
| 12739 | |
| 12740 | |
| 12741 | static int umaskcmd(int argc, char **argv) |
| 12742 | { |
| 12743 | static const char permuser[3] = "ugo"; |
| 12744 | static const char permmode[3] = "rwx"; |
| 12745 | static const short int permmask[] = { |
| 12746 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 12747 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 12748 | S_IROTH, S_IWOTH, S_IXOTH |
| 12749 | }; |
| 12750 | |
| 12751 | char *ap; |
| 12752 | mode_t mask; |
| 12753 | int i; |
| 12754 | int symbolic_mode = 0; |
| 12755 | |
| 12756 | while (nextopt("S") != '\0') { |
| 12757 | symbolic_mode = 1; |
| 12758 | } |
| 12759 | |
| 12760 | INTOFF; |
| 12761 | mask = umask(0); |
| 12762 | umask(mask); |
| 12763 | INTON; |
| 12764 | |
| 12765 | if ((ap = *argptr) == NULL) { |
| 12766 | if (symbolic_mode) { |
| 12767 | char buf[18]; |
| 12768 | char *p = buf; |
| 12769 | |
| 12770 | for (i = 0; i < 3; i++) { |
| 12771 | int j; |
| 12772 | |
| 12773 | *p++ = permuser[i]; |
| 12774 | *p++ = '='; |
| 12775 | for (j = 0; j < 3; j++) { |
| 12776 | if ((mask & permmask[3 * i + j]) == 0) { |
| 12777 | *p++ = permmode[j]; |
| 12778 | } |
| 12779 | } |
| 12780 | *p++ = ','; |
| 12781 | } |
| 12782 | *--p = 0; |
| 12783 | puts(buf); |
| 12784 | } else { |
| 12785 | out1fmt("%.4o\n", mask); |
| 12786 | } |
| 12787 | } else { |
| 12788 | if (is_digit((unsigned char) *ap)) { |
| 12789 | mask = 0; |
| 12790 | do { |
| 12791 | if (*ap >= '8' || *ap < '0') |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12792 | sh_error(illnum, argv[1]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12793 | mask = (mask << 3) + (*ap - '0'); |
| 12794 | } while (*++ap != '\0'); |
| 12795 | umask(mask); |
| 12796 | } else { |
| 12797 | mask = ~mask & 0777; |
| 12798 | if (!bb_parse_mode(ap, &mask)) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12799 | sh_error("Illegal mode: %s", ap); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12800 | } |
| 12801 | umask(~mask & 0777); |
| 12802 | } |
| 12803 | } |
| 12804 | return 0; |
| 12805 | } |
| 12806 | |
| 12807 | /* |
| 12808 | * ulimit builtin |
| 12809 | * |
| 12810 | * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and |
| 12811 | * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with |
| 12812 | * ash by J.T. Conklin. |
| 12813 | * |
| 12814 | * Public domain. |
| 12815 | */ |
| 12816 | |
| 12817 | struct limits { |
| 12818 | const char *name; |
| 12819 | int cmd; |
| 12820 | int factor; /* multiply by to get rlim_{cur,max} values */ |
| 12821 | char option; |
| 12822 | }; |
| 12823 | |
| 12824 | static const struct limits limits[] = { |
| 12825 | #ifdef RLIMIT_CPU |
| 12826 | { "time(seconds)", RLIMIT_CPU, 1, 't' }, |
| 12827 | #endif |
| 12828 | #ifdef RLIMIT_FSIZE |
| 12829 | { "file(blocks)", RLIMIT_FSIZE, 512, 'f' }, |
| 12830 | #endif |
| 12831 | #ifdef RLIMIT_DATA |
| 12832 | { "data(kbytes)", RLIMIT_DATA, 1024, 'd' }, |
| 12833 | #endif |
| 12834 | #ifdef RLIMIT_STACK |
| 12835 | { "stack(kbytes)", RLIMIT_STACK, 1024, 's' }, |
| 12836 | #endif |
| 12837 | #ifdef RLIMIT_CORE |
| 12838 | { "coredump(blocks)", RLIMIT_CORE, 512, 'c' }, |
| 12839 | #endif |
| 12840 | #ifdef RLIMIT_RSS |
| 12841 | { "memory(kbytes)", RLIMIT_RSS, 1024, 'm' }, |
| 12842 | #endif |
| 12843 | #ifdef RLIMIT_MEMLOCK |
| 12844 | { "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024, 'l' }, |
| 12845 | #endif |
| 12846 | #ifdef RLIMIT_NPROC |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12847 | { "process", RLIMIT_NPROC, 1, 'p' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12848 | #endif |
| 12849 | #ifdef RLIMIT_NOFILE |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12850 | { "nofiles", RLIMIT_NOFILE, 1, 'n' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12851 | #endif |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12852 | #ifdef RLIMIT_AS |
| 12853 | { "vmemory(kbytes)", RLIMIT_AS, 1024, 'v' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12854 | #endif |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12855 | #ifdef RLIMIT_LOCKS |
| 12856 | { "locks", RLIMIT_LOCKS, 1, 'w' }, |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12857 | #endif |
| 12858 | { (char *) 0, 0, 0, '\0' } |
| 12859 | }; |
| 12860 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12861 | enum limtype { SOFT = 0x1, HARD = 0x2 }; |
| 12862 | |
| 12863 | static void printlim(enum limtype how, const struct rlimit *limit, |
| 12864 | const struct limits *l) |
| 12865 | { |
| 12866 | rlim_t val; |
| 12867 | |
| 12868 | val = limit->rlim_max; |
| 12869 | if (how & SOFT) |
| 12870 | val = limit->rlim_cur; |
| 12871 | |
| 12872 | if (val == RLIM_INFINITY) |
| 12873 | out1fmt("unlimited\n"); |
| 12874 | else { |
| 12875 | val /= l->factor; |
| 12876 | out1fmt("%lld\n", (long long) val); |
| 12877 | } |
| 12878 | } |
| 12879 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12880 | int |
| 12881 | ulimitcmd(int argc, char **argv) |
| 12882 | { |
| 12883 | int c; |
| 12884 | rlim_t val = 0; |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12885 | enum limtype how = SOFT | HARD; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12886 | const struct limits *l; |
| 12887 | int set, all = 0; |
| 12888 | int optc, what; |
| 12889 | struct rlimit limit; |
| 12890 | |
| 12891 | what = 'f'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 12892 | while ((optc = nextopt("HSa" |
| 12893 | #ifdef RLIMIT_CPU |
| 12894 | "t" |
| 12895 | #endif |
| 12896 | #ifdef RLIMIT_FSIZE |
| 12897 | "f" |
| 12898 | #endif |
| 12899 | #ifdef RLIMIT_DATA |
| 12900 | "d" |
| 12901 | #endif |
| 12902 | #ifdef RLIMIT_STACK |
| 12903 | "s" |
| 12904 | #endif |
| 12905 | #ifdef RLIMIT_CORE |
| 12906 | "c" |
| 12907 | #endif |
| 12908 | #ifdef RLIMIT_RSS |
| 12909 | "m" |
| 12910 | #endif |
| 12911 | #ifdef RLIMIT_MEMLOCK |
| 12912 | "l" |
| 12913 | #endif |
| 12914 | #ifdef RLIMIT_NPROC |
| 12915 | "p" |
| 12916 | #endif |
| 12917 | #ifdef RLIMIT_NOFILE |
| 12918 | "n" |
| 12919 | #endif |
| 12920 | #ifdef RLIMIT_AS |
| 12921 | "v" |
| 12922 | #endif |
| 12923 | #ifdef RLIMIT_LOCKS |
| 12924 | "w" |
| 12925 | #endif |
| 12926 | )) != '\0') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12927 | switch (optc) { |
| 12928 | case 'H': |
| 12929 | how = HARD; |
| 12930 | break; |
| 12931 | case 'S': |
| 12932 | how = SOFT; |
| 12933 | break; |
| 12934 | case 'a': |
| 12935 | all = 1; |
| 12936 | break; |
| 12937 | default: |
| 12938 | what = optc; |
| 12939 | } |
| 12940 | |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12941 | for (l = limits; l->option != what; l++) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12942 | ; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12943 | |
| 12944 | set = *argptr ? 1 : 0; |
| 12945 | if (set) { |
| 12946 | char *p = *argptr; |
| 12947 | |
| 12948 | if (all || argptr[1]) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12949 | sh_error("too many arguments"); |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 12950 | if (strncmp(p, "unlimited\n", 9) == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12951 | val = RLIM_INFINITY; |
| 12952 | else { |
| 12953 | val = (rlim_t) 0; |
| 12954 | |
| 12955 | while ((c = *p++) >= '0' && c <= '9') |
| 12956 | { |
| 12957 | val = (val * 10) + (long)(c - '0'); |
| 12958 | if (val < (rlim_t) 0) |
| 12959 | break; |
| 12960 | } |
| 12961 | if (c) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12962 | sh_error("bad number"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12963 | val *= l->factor; |
| 12964 | } |
| 12965 | } |
| 12966 | if (all) { |
| 12967 | for (l = limits; l->name; l++) { |
| 12968 | getrlimit(l->cmd, &limit); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12969 | out1fmt("%-20s ", l->name); |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12970 | printlim(how, &limit, l); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12971 | } |
| 12972 | return 0; |
| 12973 | } |
| 12974 | |
| 12975 | getrlimit(l->cmd, &limit); |
| 12976 | if (set) { |
| 12977 | if (how & HARD) |
| 12978 | limit.rlim_max = val; |
| 12979 | if (how & SOFT) |
| 12980 | limit.rlim_cur = val; |
| 12981 | if (setrlimit(l->cmd, &limit) < 0) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12982 | sh_error("error setting limit (%m)"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12983 | } else { |
Glenn L McGrath | 7662062 | 2004-01-13 10:19:37 +0000 | [diff] [blame] | 12984 | printlim(how, &limit, l); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12985 | } |
| 12986 | return 0; |
| 12987 | } |
| 12988 | |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12989 | |
| 12990 | #ifdef CONFIG_ASH_MATH_SUPPORT |
| 12991 | |
| 12992 | /* Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com> |
| 12993 | |
| 12994 | Permission is hereby granted, free of charge, to any person obtaining |
| 12995 | a copy of this software and associated documentation files (the |
| 12996 | "Software"), to deal in the Software without restriction, including |
| 12997 | without limitation the rights to use, copy, modify, merge, publish, |
| 12998 | distribute, sublicense, and/or sell copies of the Software, and to |
| 12999 | permit persons to whom the Software is furnished to do so, subject to |
| 13000 | the following conditions: |
| 13001 | |
| 13002 | The above copyright notice and this permission notice shall be |
| 13003 | included in all copies or substantial portions of the Software. |
| 13004 | |
| 13005 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 13006 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 13007 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 13008 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 13009 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 13010 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 13011 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 13012 | */ |
| 13013 | |
| 13014 | /* This is my infix parser/evaluator. It is optimized for size, intended |
| 13015 | * as a replacement for yacc-based parsers. However, it may well be faster |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 13016 | * than a comparable parser written in yacc. The supported operators are |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13017 | * listed in #defines below. Parens, order of operations, and error handling |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 13018 | * are supported. This code is thread safe. The exact expression format should |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13019 | * be that which POSIX specifies for shells. */ |
| 13020 | |
| 13021 | /* The code uses a simple two-stack algorithm. See |
| 13022 | * http://www.onthenet.com.au/~grahamis/int2008/week02/lect02.html |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 13023 | * for a detailed explanation of the infix-to-postfix algorithm on which |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13024 | * this is based (this code differs in that it applies operators immediately |
| 13025 | * to the stack instead of adding them to a queue to end up with an |
| 13026 | * expression). */ |
| 13027 | |
| 13028 | /* To use the routine, call it with an expression string and error return |
| 13029 | * pointer */ |
| 13030 | |
| 13031 | /* |
| 13032 | * Aug 24, 2001 Manuel Novoa III |
| 13033 | * |
| 13034 | * Reduced the generated code size by about 30% (i386) and fixed several bugs. |
| 13035 | * |
| 13036 | * 1) In arith_apply(): |
| 13037 | * a) Cached values of *numptr and &(numptr[-1]). |
| 13038 | * b) Removed redundant test for zero denominator. |
| 13039 | * |
| 13040 | * 2) In arith(): |
| 13041 | * a) Eliminated redundant code for processing operator tokens by moving |
| 13042 | * to a table-based implementation. Also folded handling of parens |
| 13043 | * into the table. |
| 13044 | * b) Combined all 3 loops which called arith_apply to reduce generated |
| 13045 | * code size at the cost of speed. |
| 13046 | * |
| 13047 | * 3) The following expressions were treated as valid by the original code: |
| 13048 | * 1() , 0! , 1 ( *3 ) . |
| 13049 | * These bugs have been fixed by internally enclosing the expression in |
| 13050 | * parens and then checking that all binary ops and right parens are |
| 13051 | * preceded by a valid expression (NUM_TOKEN). |
| 13052 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 13053 | * Note: It may be desirable to replace Aaron's test for whitespace with |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13054 | * ctype's isspace() if it is used by another busybox applet or if additional |
| 13055 | * whitespace chars should be considered. Look below the "#include"s for a |
| 13056 | * precompiler test. |
| 13057 | */ |
| 13058 | |
| 13059 | /* |
| 13060 | * Aug 26, 2001 Manuel Novoa III |
| 13061 | * |
| 13062 | * Return 0 for null expressions. Pointed out by Vladimir Oleynik. |
| 13063 | * |
| 13064 | * Merge in Aaron's comments previously posted to the busybox list, |
| 13065 | * modified slightly to take account of my changes to the code. |
| 13066 | * |
| 13067 | */ |
| 13068 | |
| 13069 | /* |
| 13070 | * (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
| 13071 | * |
| 13072 | * - allow access to variable, |
| 13073 | * used recursive find value indirection (c=2*2; a="c"; $((a+=2)) produce 6) |
| 13074 | * - realize assign syntax (VAR=expr, +=, *= etc) |
| 13075 | * - realize exponentiation (** operator) |
| 13076 | * - realize comma separated - expr, expr |
| 13077 | * - realise ++expr --expr expr++ expr-- |
| 13078 | * - realise expr ? expr : expr (but, second expr calculate always) |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 13079 | * - allow hexadecimal and octal numbers |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13080 | * - was restored loses XOR operator |
| 13081 | * - remove one goto label, added three ;-) |
| 13082 | * - protect $((num num)) as true zero expr (Manuel`s error) |
| 13083 | * - always use special isspace(), see comment from bash ;-) |
| 13084 | */ |
| 13085 | |
| 13086 | |
| 13087 | #define arith_isspace(arithval) \ |
| 13088 | (arithval == ' ' || arithval == '\n' || arithval == '\t') |
| 13089 | |
| 13090 | |
| 13091 | typedef unsigned char operator; |
| 13092 | |
| 13093 | /* An operator's token id is a bit of a bitfield. The lower 5 bits are the |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 13094 | * precedence, and 3 high bits are an ID unique across operators of that |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13095 | * precedence. The ID portion is so that multiple operators can have the |
| 13096 | * same precedence, ensuring that the leftmost one is evaluated first. |
| 13097 | * Consider * and /. */ |
| 13098 | |
| 13099 | #define tok_decl(prec,id) (((id)<<5)|(prec)) |
| 13100 | #define PREC(op) ((op) & 0x1F) |
| 13101 | |
| 13102 | #define TOK_LPAREN tok_decl(0,0) |
| 13103 | |
| 13104 | #define TOK_COMMA tok_decl(1,0) |
| 13105 | |
| 13106 | #define TOK_ASSIGN tok_decl(2,0) |
| 13107 | #define TOK_AND_ASSIGN tok_decl(2,1) |
| 13108 | #define TOK_OR_ASSIGN tok_decl(2,2) |
| 13109 | #define TOK_XOR_ASSIGN tok_decl(2,3) |
| 13110 | #define TOK_PLUS_ASSIGN tok_decl(2,4) |
| 13111 | #define TOK_MINUS_ASSIGN tok_decl(2,5) |
| 13112 | #define TOK_LSHIFT_ASSIGN tok_decl(2,6) |
| 13113 | #define TOK_RSHIFT_ASSIGN tok_decl(2,7) |
| 13114 | |
| 13115 | #define TOK_MUL_ASSIGN tok_decl(3,0) |
| 13116 | #define TOK_DIV_ASSIGN tok_decl(3,1) |
| 13117 | #define TOK_REM_ASSIGN tok_decl(3,2) |
| 13118 | |
| 13119 | /* all assign is right associativity and precedence eq, but (7+3)<<5 > 256 */ |
| 13120 | #define convert_prec_is_assing(prec) do { if(prec == 3) prec = 2; } while(0) |
| 13121 | |
| 13122 | /* conditional is right associativity too */ |
| 13123 | #define TOK_CONDITIONAL tok_decl(4,0) |
| 13124 | #define TOK_CONDITIONAL_SEP tok_decl(4,1) |
| 13125 | |
| 13126 | #define TOK_OR tok_decl(5,0) |
| 13127 | |
| 13128 | #define TOK_AND tok_decl(6,0) |
| 13129 | |
| 13130 | #define TOK_BOR tok_decl(7,0) |
| 13131 | |
| 13132 | #define TOK_BXOR tok_decl(8,0) |
| 13133 | |
| 13134 | #define TOK_BAND tok_decl(9,0) |
| 13135 | |
| 13136 | #define TOK_EQ tok_decl(10,0) |
| 13137 | #define TOK_NE tok_decl(10,1) |
| 13138 | |
| 13139 | #define TOK_LT tok_decl(11,0) |
| 13140 | #define TOK_GT tok_decl(11,1) |
| 13141 | #define TOK_GE tok_decl(11,2) |
| 13142 | #define TOK_LE tok_decl(11,3) |
| 13143 | |
| 13144 | #define TOK_LSHIFT tok_decl(12,0) |
| 13145 | #define TOK_RSHIFT tok_decl(12,1) |
| 13146 | |
| 13147 | #define TOK_ADD tok_decl(13,0) |
| 13148 | #define TOK_SUB tok_decl(13,1) |
| 13149 | |
| 13150 | #define TOK_MUL tok_decl(14,0) |
| 13151 | #define TOK_DIV tok_decl(14,1) |
| 13152 | #define TOK_REM tok_decl(14,2) |
| 13153 | |
| 13154 | /* exponent is right associativity */ |
| 13155 | #define TOK_EXPONENT tok_decl(15,1) |
| 13156 | |
| 13157 | /* For now unary operators. */ |
| 13158 | #define UNARYPREC 16 |
| 13159 | #define TOK_BNOT tok_decl(UNARYPREC,0) |
| 13160 | #define TOK_NOT tok_decl(UNARYPREC,1) |
| 13161 | |
| 13162 | #define TOK_UMINUS tok_decl(UNARYPREC+1,0) |
| 13163 | #define TOK_UPLUS tok_decl(UNARYPREC+1,1) |
| 13164 | |
| 13165 | #define PREC_PRE (UNARYPREC+2) |
| 13166 | |
| 13167 | #define TOK_PRE_INC tok_decl(PREC_PRE, 0) |
| 13168 | #define TOK_PRE_DEC tok_decl(PREC_PRE, 1) |
| 13169 | |
| 13170 | #define PREC_POST (UNARYPREC+3) |
| 13171 | |
| 13172 | #define TOK_POST_INC tok_decl(PREC_POST, 0) |
| 13173 | #define TOK_POST_DEC tok_decl(PREC_POST, 1) |
| 13174 | |
| 13175 | #define SPEC_PREC (UNARYPREC+4) |
| 13176 | |
| 13177 | #define TOK_NUM tok_decl(SPEC_PREC, 0) |
| 13178 | #define TOK_RPAREN tok_decl(SPEC_PREC, 1) |
| 13179 | |
| 13180 | #define NUMPTR (*numstackptr) |
| 13181 | |
| 13182 | static inline int tok_have_assign(operator op) |
| 13183 | { |
| 13184 | operator prec = PREC(op); |
| 13185 | |
| 13186 | convert_prec_is_assing(prec); |
| 13187 | return (prec == PREC(TOK_ASSIGN) || |
| 13188 | prec == PREC_PRE || prec == PREC_POST); |
| 13189 | } |
| 13190 | |
| 13191 | static inline int is_right_associativity(operator prec) |
| 13192 | { |
| 13193 | return (prec == PREC(TOK_ASSIGN) || prec == PREC(TOK_EXPONENT) || |
| 13194 | prec == PREC(TOK_CONDITIONAL)); |
| 13195 | } |
| 13196 | |
| 13197 | |
| 13198 | typedef struct ARITCH_VAR_NUM { |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 13199 | arith_t val; |
| 13200 | arith_t contidional_second_val; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13201 | char contidional_second_val_initialized; |
| 13202 | char *var; /* if NULL then is regular number, |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 13203 | else is variable name */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13204 | } v_n_t; |
| 13205 | |
| 13206 | |
| 13207 | typedef struct CHK_VAR_RECURSIVE_LOOPED { |
| 13208 | const char *var; |
| 13209 | struct CHK_VAR_RECURSIVE_LOOPED *next; |
| 13210 | } chk_var_recursive_looped_t; |
| 13211 | |
| 13212 | static chk_var_recursive_looped_t *prev_chk_var_recursive; |
| 13213 | |
| 13214 | |
| 13215 | static int arith_lookup_val(v_n_t *t) |
| 13216 | { |
| 13217 | if(t->var) { |
| 13218 | const char * p = lookupvar(t->var); |
| 13219 | |
| 13220 | if(p) { |
| 13221 | int errcode; |
| 13222 | |
| 13223 | /* recursive try as expression */ |
| 13224 | chk_var_recursive_looped_t *cur; |
| 13225 | chk_var_recursive_looped_t cur_save; |
| 13226 | |
| 13227 | for(cur = prev_chk_var_recursive; cur; cur = cur->next) { |
| 13228 | if(strcmp(cur->var, t->var) == 0) { |
| 13229 | /* expression recursion loop detected */ |
| 13230 | return -5; |
| 13231 | } |
| 13232 | } |
| 13233 | /* save current lookuped var name */ |
| 13234 | cur = prev_chk_var_recursive; |
| 13235 | cur_save.var = t->var; |
| 13236 | cur_save.next = cur; |
| 13237 | prev_chk_var_recursive = &cur_save; |
| 13238 | |
| 13239 | t->val = arith (p, &errcode); |
| 13240 | /* restore previous ptr after recursiving */ |
| 13241 | prev_chk_var_recursive = cur; |
| 13242 | return errcode; |
| 13243 | } else { |
| 13244 | /* allow undefined var as 0 */ |
| 13245 | t->val = 0; |
| 13246 | } |
| 13247 | } |
| 13248 | return 0; |
| 13249 | } |
| 13250 | |
| 13251 | /* "applying" a token means performing it on the top elements on the integer |
| 13252 | * stack. For a unary operator it will only change the top element, but a |
| 13253 | * binary operator will pop two arguments and push a result */ |
| 13254 | static inline int |
| 13255 | arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr) |
| 13256 | { |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13257 | v_n_t *numptr_m1; |
Eric Andersen | fac312d | 2004-06-22 20:09:40 +0000 | [diff] [blame] | 13258 | arith_t numptr_val, rez; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13259 | int ret_arith_lookup_val; |
| 13260 | |
| 13261 | if (NUMPTR == numstack) goto err; /* There is no operator that can work |
| 13262 | without arguments */ |
| 13263 | numptr_m1 = NUMPTR - 1; |
| 13264 | |
| 13265 | /* check operand is var with noninteger value */ |
| 13266 | ret_arith_lookup_val = arith_lookup_val(numptr_m1); |
| 13267 | if(ret_arith_lookup_val) |
| 13268 | return ret_arith_lookup_val; |
| 13269 | |
| 13270 | rez = numptr_m1->val; |
| 13271 | if (op == TOK_UMINUS) |
| 13272 | rez *= -1; |
| 13273 | else if (op == TOK_NOT) |
| 13274 | rez = !rez; |
| 13275 | else if (op == TOK_BNOT) |
| 13276 | rez = ~rez; |
| 13277 | else if (op == TOK_POST_INC || op == TOK_PRE_INC) |
| 13278 | rez++; |
| 13279 | else if (op == TOK_POST_DEC || op == TOK_PRE_DEC) |
| 13280 | rez--; |
| 13281 | else if (op != TOK_UPLUS) { |
| 13282 | /* Binary operators */ |
| 13283 | |
| 13284 | /* check and binary operators need two arguments */ |
| 13285 | if (numptr_m1 == numstack) goto err; |
| 13286 | |
| 13287 | /* ... and they pop one */ |
| 13288 | --NUMPTR; |
| 13289 | numptr_val = rez; |
| 13290 | if (op == TOK_CONDITIONAL) { |
| 13291 | if(! numptr_m1->contidional_second_val_initialized) { |
| 13292 | /* protect $((expr1 ? expr2)) without ": expr" */ |
| 13293 | goto err; |
| 13294 | } |
| 13295 | rez = numptr_m1->contidional_second_val; |
| 13296 | } else if(numptr_m1->contidional_second_val_initialized) { |
| 13297 | /* protect $((expr1 : expr2)) without "expr ? " */ |
| 13298 | goto err; |
| 13299 | } |
| 13300 | numptr_m1 = NUMPTR - 1; |
| 13301 | if(op != TOK_ASSIGN) { |
| 13302 | /* check operand is var with noninteger value for not '=' */ |
| 13303 | ret_arith_lookup_val = arith_lookup_val(numptr_m1); |
| 13304 | if(ret_arith_lookup_val) |
| 13305 | return ret_arith_lookup_val; |
| 13306 | } |
| 13307 | if (op == TOK_CONDITIONAL) { |
| 13308 | numptr_m1->contidional_second_val = rez; |
| 13309 | } |
| 13310 | rez = numptr_m1->val; |
| 13311 | if (op == TOK_BOR || op == TOK_OR_ASSIGN) |
| 13312 | rez |= numptr_val; |
| 13313 | else if (op == TOK_OR) |
| 13314 | rez = numptr_val || rez; |
| 13315 | else if (op == TOK_BAND || op == TOK_AND_ASSIGN) |
| 13316 | rez &= numptr_val; |
| 13317 | else if (op == TOK_BXOR || op == TOK_XOR_ASSIGN) |
| 13318 | rez ^= numptr_val; |
| 13319 | else if (op == TOK_AND) |
| 13320 | rez = rez && numptr_val; |
| 13321 | else if (op == TOK_EQ) |
| 13322 | rez = (rez == numptr_val); |
| 13323 | else if (op == TOK_NE) |
| 13324 | rez = (rez != numptr_val); |
| 13325 | else if (op == TOK_GE) |
| 13326 | rez = (rez >= numptr_val); |
| 13327 | else if (op == TOK_RSHIFT || op == TOK_RSHIFT_ASSIGN) |
| 13328 | rez >>= numptr_val; |
| 13329 | else if (op == TOK_LSHIFT || op == TOK_LSHIFT_ASSIGN) |
| 13330 | rez <<= numptr_val; |
| 13331 | else if (op == TOK_GT) |
| 13332 | rez = (rez > numptr_val); |
| 13333 | else if (op == TOK_LT) |
| 13334 | rez = (rez < numptr_val); |
| 13335 | else if (op == TOK_LE) |
| 13336 | rez = (rez <= numptr_val); |
| 13337 | else if (op == TOK_MUL || op == TOK_MUL_ASSIGN) |
| 13338 | rez *= numptr_val; |
| 13339 | else if (op == TOK_ADD || op == TOK_PLUS_ASSIGN) |
| 13340 | rez += numptr_val; |
| 13341 | else if (op == TOK_SUB || op == TOK_MINUS_ASSIGN) |
| 13342 | rez -= numptr_val; |
| 13343 | else if (op == TOK_ASSIGN || op == TOK_COMMA) |
| 13344 | rez = numptr_val; |
| 13345 | else if (op == TOK_CONDITIONAL_SEP) { |
| 13346 | if (numptr_m1 == numstack) { |
| 13347 | /* protect $((expr : expr)) without "expr ? " */ |
| 13348 | goto err; |
| 13349 | } |
| 13350 | numptr_m1->contidional_second_val_initialized = op; |
| 13351 | numptr_m1->contidional_second_val = numptr_val; |
| 13352 | } |
| 13353 | else if (op == TOK_CONDITIONAL) { |
| 13354 | rez = rez ? |
| 13355 | numptr_val : numptr_m1->contidional_second_val; |
| 13356 | } |
| 13357 | else if(op == TOK_EXPONENT) { |
| 13358 | if(numptr_val < 0) |
| 13359 | return -3; /* exponent less than 0 */ |
| 13360 | else { |
Eric Andersen | ad63cb2 | 2004-10-08 09:43:34 +0000 | [diff] [blame] | 13361 | arith_t c = 1; |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13362 | |
| 13363 | if(numptr_val) |
| 13364 | while(numptr_val--) |
| 13365 | c *= rez; |
| 13366 | rez = c; |
| 13367 | } |
| 13368 | } |
| 13369 | else if(numptr_val==0) /* zero divisor check */ |
| 13370 | return -2; |
| 13371 | else if (op == TOK_DIV || op == TOK_DIV_ASSIGN) |
| 13372 | rez /= numptr_val; |
| 13373 | else if (op == TOK_REM || op == TOK_REM_ASSIGN) |
| 13374 | rez %= numptr_val; |
| 13375 | } |
| 13376 | if(tok_have_assign(op)) { |
| 13377 | char buf[32]; |
| 13378 | |
| 13379 | if(numptr_m1->var == NULL) { |
| 13380 | /* Hmm, 1=2 ? */ |
| 13381 | goto err; |
| 13382 | } |
| 13383 | /* save to shell variable */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13384 | #ifdef CONFIG_ASH_MATH_SUPPORT_64 |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 13385 | snprintf(buf, sizeof(buf), "%lld", arith_t_type rez); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13386 | #else |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 13387 | snprintf(buf, sizeof(buf), "%ld", arith_t_type rez); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13388 | #endif |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13389 | setvar(numptr_m1->var, buf, 0); |
| 13390 | /* after saving, make previous value for v++ or v-- */ |
| 13391 | if(op == TOK_POST_INC) |
| 13392 | rez--; |
| 13393 | else if(op == TOK_POST_DEC) |
| 13394 | rez++; |
| 13395 | } |
| 13396 | numptr_m1->val = rez; |
| 13397 | /* protect geting var value, is number now */ |
| 13398 | numptr_m1->var = NULL; |
| 13399 | return 0; |
| 13400 | err: return(-1); |
| 13401 | } |
| 13402 | |
| 13403 | /* longest must first */ |
| 13404 | static const char op_tokens[] = { |
| 13405 | '<','<','=',0, TOK_LSHIFT_ASSIGN, |
| 13406 | '>','>','=',0, TOK_RSHIFT_ASSIGN, |
| 13407 | '<','<', 0, TOK_LSHIFT, |
| 13408 | '>','>', 0, TOK_RSHIFT, |
| 13409 | '|','|', 0, TOK_OR, |
| 13410 | '&','&', 0, TOK_AND, |
| 13411 | '!','=', 0, TOK_NE, |
| 13412 | '<','=', 0, TOK_LE, |
| 13413 | '>','=', 0, TOK_GE, |
| 13414 | '=','=', 0, TOK_EQ, |
| 13415 | '|','=', 0, TOK_OR_ASSIGN, |
| 13416 | '&','=', 0, TOK_AND_ASSIGN, |
| 13417 | '*','=', 0, TOK_MUL_ASSIGN, |
| 13418 | '/','=', 0, TOK_DIV_ASSIGN, |
| 13419 | '%','=', 0, TOK_REM_ASSIGN, |
| 13420 | '+','=', 0, TOK_PLUS_ASSIGN, |
| 13421 | '-','=', 0, TOK_MINUS_ASSIGN, |
| 13422 | '-','-', 0, TOK_POST_DEC, |
| 13423 | '^','=', 0, TOK_XOR_ASSIGN, |
| 13424 | '+','+', 0, TOK_POST_INC, |
| 13425 | '*','*', 0, TOK_EXPONENT, |
| 13426 | '!', 0, TOK_NOT, |
| 13427 | '<', 0, TOK_LT, |
| 13428 | '>', 0, TOK_GT, |
| 13429 | '=', 0, TOK_ASSIGN, |
| 13430 | '|', 0, TOK_BOR, |
| 13431 | '&', 0, TOK_BAND, |
| 13432 | '*', 0, TOK_MUL, |
| 13433 | '/', 0, TOK_DIV, |
| 13434 | '%', 0, TOK_REM, |
| 13435 | '+', 0, TOK_ADD, |
| 13436 | '-', 0, TOK_SUB, |
| 13437 | '^', 0, TOK_BXOR, |
| 13438 | /* uniq */ |
| 13439 | '~', 0, TOK_BNOT, |
| 13440 | ',', 0, TOK_COMMA, |
| 13441 | '?', 0, TOK_CONDITIONAL, |
| 13442 | ':', 0, TOK_CONDITIONAL_SEP, |
| 13443 | ')', 0, TOK_RPAREN, |
| 13444 | '(', 0, TOK_LPAREN, |
| 13445 | 0 |
| 13446 | }; |
| 13447 | /* ptr to ")" */ |
| 13448 | #define endexpression &op_tokens[sizeof(op_tokens)-7] |
| 13449 | |
| 13450 | |
Eric Andersen | ed9ecf7 | 2004-06-22 08:29:45 +0000 | [diff] [blame] | 13451 | static arith_t arith (const char *expr, int *perrcode) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13452 | { |
| 13453 | register char arithval; /* Current character under analysis */ |
| 13454 | operator lasttok, op; |
| 13455 | operator prec; |
| 13456 | |
| 13457 | const char *p = endexpression; |
| 13458 | int errcode; |
| 13459 | |
| 13460 | size_t datasizes = strlen(expr) + 2; |
| 13461 | |
| 13462 | /* Stack of integers */ |
| 13463 | /* The proof that there can be no more than strlen(startbuf)/2+1 integers |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 13464 | * in any given correct or incorrect expression is left as an exercise to |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13465 | * the reader. */ |
| 13466 | v_n_t *numstack = alloca(((datasizes)/2)*sizeof(v_n_t)), |
| 13467 | *numstackptr = numstack; |
| 13468 | /* Stack of operator tokens */ |
| 13469 | operator *stack = alloca((datasizes) * sizeof(operator)), |
| 13470 | *stackptr = stack; |
| 13471 | |
| 13472 | *stackptr++ = lasttok = TOK_LPAREN; /* start off with a left paren */ |
| 13473 | *perrcode = errcode = 0; |
| 13474 | |
| 13475 | while(1) { |
| 13476 | if ((arithval = *expr) == 0) { |
| 13477 | if (p == endexpression) { |
| 13478 | /* Null expression. */ |
| 13479 | return 0; |
| 13480 | } |
| 13481 | |
| 13482 | /* This is only reached after all tokens have been extracted from the |
| 13483 | * input stream. If there are still tokens on the operator stack, they |
| 13484 | * are to be applied in order. At the end, there should be a final |
| 13485 | * result on the integer stack */ |
| 13486 | |
| 13487 | if (expr != endexpression + 1) { |
| 13488 | /* If we haven't done so already, */ |
| 13489 | /* append a closing right paren */ |
| 13490 | expr = endexpression; |
| 13491 | /* and let the loop process it. */ |
| 13492 | continue; |
| 13493 | } |
| 13494 | /* At this point, we're done with the expression. */ |
| 13495 | if (numstackptr != numstack+1) { |
| 13496 | /* ... but if there isn't, it's bad */ |
| 13497 | err: |
| 13498 | return (*perrcode = -1); |
| 13499 | } |
| 13500 | if(numstack->var) { |
| 13501 | /* expression is $((var)) only, lookup now */ |
| 13502 | errcode = arith_lookup_val(numstack); |
| 13503 | } |
| 13504 | ret: |
| 13505 | *perrcode = errcode; |
| 13506 | return numstack->val; |
| 13507 | } else { |
| 13508 | /* Continue processing the expression. */ |
| 13509 | if (arith_isspace(arithval)) { |
| 13510 | /* Skip whitespace */ |
| 13511 | goto prologue; |
| 13512 | } |
| 13513 | if((p = endofname(expr)) != expr) { |
Eric Andersen | ad63cb2 | 2004-10-08 09:43:34 +0000 | [diff] [blame] | 13514 | size_t var_name_size = (p-expr) + 1; /* trailing zero */ |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13515 | |
| 13516 | numstackptr->var = alloca(var_name_size); |
| 13517 | safe_strncpy(numstackptr->var, expr, var_name_size); |
| 13518 | expr = p; |
| 13519 | num: |
| 13520 | numstackptr->contidional_second_val_initialized = 0; |
| 13521 | numstackptr++; |
| 13522 | lasttok = TOK_NUM; |
| 13523 | continue; |
| 13524 | } else if (is_digit(arithval)) { |
| 13525 | numstackptr->var = NULL; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13526 | #ifdef CONFIG_ASH_MATH_SUPPORT_64 |
Eric Andersen | ad63cb2 | 2004-10-08 09:43:34 +0000 | [diff] [blame] | 13527 | numstackptr->val = strtoll(expr, (char **) &expr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13528 | #else |
| 13529 | numstackptr->val = strtol(expr, (char **) &expr, 0); |
| 13530 | #endif |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13531 | goto num; |
| 13532 | } |
| 13533 | for(p = op_tokens; ; p++) { |
| 13534 | const char *o; |
| 13535 | |
| 13536 | if(*p == 0) { |
| 13537 | /* strange operator not found */ |
| 13538 | goto err; |
| 13539 | } |
| 13540 | for(o = expr; *p && *o == *p; p++) |
| 13541 | o++; |
| 13542 | if(! *p) { |
| 13543 | /* found */ |
| 13544 | expr = o - 1; |
| 13545 | break; |
| 13546 | } |
| 13547 | /* skip tail uncompared token */ |
| 13548 | while(*p) |
| 13549 | p++; |
| 13550 | /* skip zero delim */ |
| 13551 | p++; |
| 13552 | } |
| 13553 | op = p[1]; |
| 13554 | |
| 13555 | /* post grammar: a++ reduce to num */ |
| 13556 | if(lasttok == TOK_POST_INC || lasttok == TOK_POST_DEC) |
| 13557 | lasttok = TOK_NUM; |
| 13558 | |
| 13559 | /* Plus and minus are binary (not unary) _only_ if the last |
| 13560 | * token was as number, or a right paren (which pretends to be |
| 13561 | * a number, since it evaluates to one). Think about it. |
| 13562 | * It makes sense. */ |
| 13563 | if (lasttok != TOK_NUM) { |
| 13564 | switch(op) { |
| 13565 | case TOK_ADD: |
| 13566 | op = TOK_UPLUS; |
| 13567 | break; |
| 13568 | case TOK_SUB: |
| 13569 | op = TOK_UMINUS; |
| 13570 | break; |
| 13571 | case TOK_POST_INC: |
| 13572 | op = TOK_PRE_INC; |
| 13573 | break; |
| 13574 | case TOK_POST_DEC: |
| 13575 | op = TOK_PRE_DEC; |
| 13576 | break; |
| 13577 | } |
| 13578 | } |
| 13579 | /* We don't want a unary operator to cause recursive descent on the |
| 13580 | * stack, because there can be many in a row and it could cause an |
| 13581 | * operator to be evaluated before its argument is pushed onto the |
| 13582 | * integer stack. */ |
| 13583 | /* But for binary operators, "apply" everything on the operator |
| 13584 | * stack until we find an operator with a lesser priority than the |
| 13585 | * one we have just extracted. */ |
| 13586 | /* Left paren is given the lowest priority so it will never be |
| 13587 | * "applied" in this way. |
| 13588 | * if associativity is right and priority eq, applied also skip |
| 13589 | */ |
| 13590 | prec = PREC(op); |
| 13591 | if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) { |
| 13592 | /* not left paren or unary */ |
| 13593 | if (lasttok != TOK_NUM) { |
| 13594 | /* binary op must be preceded by a num */ |
| 13595 | goto err; |
| 13596 | } |
| 13597 | while (stackptr != stack) { |
| 13598 | if (op == TOK_RPAREN) { |
| 13599 | /* The algorithm employed here is simple: while we don't |
| 13600 | * hit an open paren nor the bottom of the stack, pop |
| 13601 | * tokens and apply them */ |
| 13602 | if (stackptr[-1] == TOK_LPAREN) { |
| 13603 | --stackptr; |
| 13604 | /* Any operator directly after a */ |
| 13605 | lasttok = TOK_NUM; |
| 13606 | /* close paren should consider itself binary */ |
| 13607 | goto prologue; |
| 13608 | } |
| 13609 | } else { |
| 13610 | operator prev_prec = PREC(stackptr[-1]); |
| 13611 | |
| 13612 | convert_prec_is_assing(prec); |
| 13613 | convert_prec_is_assing(prev_prec); |
| 13614 | if (prev_prec < prec) |
| 13615 | break; |
| 13616 | /* check right assoc */ |
| 13617 | if(prev_prec == prec && is_right_associativity(prec)) |
| 13618 | break; |
| 13619 | } |
| 13620 | errcode = arith_apply(*--stackptr, numstack, &numstackptr); |
| 13621 | if(errcode) goto ret; |
| 13622 | } |
| 13623 | if (op == TOK_RPAREN) { |
| 13624 | goto err; |
| 13625 | } |
| 13626 | } |
| 13627 | |
| 13628 | /* Push this operator to the stack and remember it. */ |
| 13629 | *stackptr++ = lasttok = op; |
| 13630 | |
| 13631 | prologue: |
| 13632 | ++expr; |
| 13633 | } |
| 13634 | } |
| 13635 | } |
| 13636 | #endif /* CONFIG_ASH_MATH_SUPPORT */ |
| 13637 | |
| 13638 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13639 | #ifdef DEBUG |
| 13640 | const char *bb_applet_name = "debug stuff usage"; |
| 13641 | int main(int argc, char **argv) |
| 13642 | { |
| 13643 | return ash_main(argc, argv); |
| 13644 | } |
| 13645 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 13646 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13647 | /*- |
| 13648 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 13649 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13650 | * |
| 13651 | * This code is derived from software contributed to Berkeley by |
| 13652 | * Kenneth Almquist. |
| 13653 | * |
| 13654 | * Redistribution and use in source and binary forms, with or without |
| 13655 | * modification, are permitted provided that the following conditions |
| 13656 | * are met: |
| 13657 | * 1. Redistributions of source code must retain the above copyright |
| 13658 | * notice, this list of conditions and the following disclaimer. |
| 13659 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13660 | * notice, this list of conditions and the following disclaimer in the |
| 13661 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13662 | * 3. Neither the name of the University nor the names of its contributors |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13663 | * may be used to endorse or promote products derived from this software |
| 13664 | * without specific prior written permission. |
| 13665 | * |
| 13666 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 13667 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13668 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 13669 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13670 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13671 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 13672 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13673 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 13674 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 13675 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 13676 | * SUCH DAMAGE. |
| 13677 | */ |