Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Minix shell port for busybox |
| 4 | * |
| 5 | * This version of the Minix shell was adapted for use in busybox |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 6 | * by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 8 | * - backtick expansion did not work properly |
| 9 | * Jonas Holmberg <jonas.holmberg@axis.com> |
| 10 | * Robert Schwebel <r.schwebel@pengutronix.de> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 11 | * Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 12 | * |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 13 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 16 | #include <sys/times.h> |
| 17 | #include <setjmp.h> |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 18 | |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 19 | #ifdef STANDALONE |
| 20 | # ifndef _GNU_SOURCE |
| 21 | # define _GNU_SOURCE |
| 22 | # endif |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 23 | # include <sys/types.h> |
| 24 | # include <sys/stat.h> |
| 25 | # include <sys/wait.h> |
| 26 | # include <signal.h> |
| 27 | # include <stdio.h> |
| 28 | # include <stdlib.h> |
| 29 | # include <unistd.h> |
| 30 | # include <string.h> |
| 31 | # include <errno.h> |
| 32 | # include <dirent.h> |
| 33 | # include <fcntl.h> |
| 34 | # include <ctype.h> |
| 35 | # include <assert.h> |
| 36 | # define bb_dev_null "/dev/null" |
| 37 | # define DEFAULT_SHELL "/proc/self/exe" |
| 38 | # define CONFIG_BUSYBOX_EXEC_PATH "/proc/self/exe" |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 39 | # define bb_banner "busybox standalone" |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 40 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 41 | # define bb_msg_memory_exhausted "memory exhausted" |
| 42 | # define xmalloc(size) malloc(size) |
| 43 | # define msh_main(argc,argv) main(argc,argv) |
| 44 | # define safe_read(fd,buf,count) read(fd,buf,count) |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 45 | # define nonblock_safe_read(fd,buf,count) read(fd,buf,count) |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 46 | # define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1]) |
| 47 | # define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1]) |
| 48 | # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 49 | static int find_applet_by_name(const char *applet) |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 50 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 51 | return -1; |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 52 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 53 | static char *utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 54 | { |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 55 | unsigned i, out, res; |
| 56 | assert(sizeof(unsigned) == 4); |
| 57 | if (buflen) { |
| 58 | out = 0; |
| 59 | for (i = 1000000000; i; i /= 10) { |
| 60 | res = n / i; |
| 61 | if (res || out || i == 1) { |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 62 | if (!--buflen) break; |
| 63 | out++; |
| 64 | n -= res*i; |
| 65 | *buf++ = '0' + res; |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 68 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 69 | return buf; |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 70 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 71 | static char *itoa_to_buf(int n, char *buf, unsigned buflen) |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 72 | { |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 73 | if (buflen && n < 0) { |
| 74 | n = -n; |
| 75 | *buf++ = '-'; |
| 76 | buflen--; |
| 77 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 78 | return utoa_to_buf((unsigned)n, buf, buflen); |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 79 | } |
| 80 | static char local_buf[12]; |
| 81 | static char *itoa(int n) |
| 82 | { |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 83 | *(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0'; |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 84 | return local_buf; |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 85 | } |
| 86 | #else |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 87 | # include "busybox.h" /* for applet_names */ |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 88 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 89 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 90 | //#define MSHDEBUG 4 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 91 | |
| 92 | #ifdef MSHDEBUG |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 93 | static int mshdbg = MSHDEBUG; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 95 | #define DBGPRINTF(x) if (mshdbg>0) printf x |
| 96 | #define DBGPRINTF0(x) if (mshdbg>0) printf x |
| 97 | #define DBGPRINTF1(x) if (mshdbg>1) printf x |
| 98 | #define DBGPRINTF2(x) if (mshdbg>2) printf x |
| 99 | #define DBGPRINTF3(x) if (mshdbg>3) printf x |
| 100 | #define DBGPRINTF4(x) if (mshdbg>4) printf x |
| 101 | #define DBGPRINTF5(x) if (mshdbg>5) printf x |
| 102 | #define DBGPRINTF6(x) if (mshdbg>6) printf x |
| 103 | #define DBGPRINTF7(x) if (mshdbg>7) printf x |
| 104 | #define DBGPRINTF8(x) if (mshdbg>8) printf x |
| 105 | #define DBGPRINTF9(x) if (mshdbg>9) printf x |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 106 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 107 | static int mshdbg_rc = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 108 | |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 109 | #define RCPRINTF(x) if (mshdbg_rc) printf x |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 110 | |
| 111 | #else |
| 112 | |
| 113 | #define DBGPRINTF(x) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 114 | #define DBGPRINTF0(x) ((void)0) |
| 115 | #define DBGPRINTF1(x) ((void)0) |
| 116 | #define DBGPRINTF2(x) ((void)0) |
| 117 | #define DBGPRINTF3(x) ((void)0) |
| 118 | #define DBGPRINTF4(x) ((void)0) |
| 119 | #define DBGPRINTF5(x) ((void)0) |
| 120 | #define DBGPRINTF6(x) ((void)0) |
| 121 | #define DBGPRINTF7(x) ((void)0) |
| 122 | #define DBGPRINTF8(x) ((void)0) |
| 123 | #define DBGPRINTF9(x) ((void)0) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 124 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 125 | #define RCPRINTF(x) ((void)0) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 126 | |
| 127 | #endif /* MSHDEBUG */ |
| 128 | |
| 129 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 130 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 2a13175 | 2006-06-06 06:26:12 +0000 | [diff] [blame] | 131 | # define DEFAULT_ROOT_PROMPT "\\u:\\w> " |
| 132 | # define DEFAULT_USER_PROMPT "\\u:\\w$ " |
| 133 | #else |
| 134 | # define DEFAULT_ROOT_PROMPT "# " |
| 135 | # define DEFAULT_USER_PROMPT "$ " |
| 136 | #endif |
| 137 | |
| 138 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 139 | /* -------- sh.h -------- */ |
| 140 | /* |
| 141 | * shell |
| 142 | */ |
| 143 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 144 | #define LINELIM 2100 |
| 145 | #define NPUSH 8 /* limit to input nesting */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 146 | |
Eric Andersen | 392947c | 2002-12-11 07:42:46 +0000 | [diff] [blame] | 147 | #undef NOFILE |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 148 | #define NOFILE 20 /* Number of open files */ |
| 149 | #define NUFILE 10 /* Number of user-accessible files */ |
| 150 | #define FDBASE 10 /* First file usable by Shell */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * values returned by wait |
| 154 | */ |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 155 | #define WAITSIG(s) ((s) & 0177) |
| 156 | #define WAITVAL(s) (((s) >> 8) & 0377) |
| 157 | #define WAITCORE(s) (((s) & 0200) != 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 158 | |
| 159 | /* |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 160 | * library and system definitions |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 161 | */ |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 162 | typedef void xint; /* base type of jmp_buf, for not broken compilers */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 163 | |
| 164 | /* |
| 165 | * shell components |
| 166 | */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 167 | #define NOBLOCK ((struct op *)NULL) |
| 168 | #define NOWORD ((char *)NULL) |
| 169 | #define NOWORDS ((char **)NULL) |
| 170 | #define NOPIPE ((int *)NULL) |
| 171 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 172 | /* |
| 173 | * redirection |
| 174 | */ |
| 175 | struct ioword { |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 176 | smallint io_flag; /* action (below) */ |
| 177 | int io_fd; /* fd affected */ |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 178 | char *io_name; /* file name */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 181 | #define IOREAD 1 /* < */ |
| 182 | #define IOHERE 2 /* << (here file) */ |
| 183 | #define IOWRITE 4 /* > */ |
| 184 | #define IOCAT 8 /* >> */ |
| 185 | #define IOXHERE 16 /* ${}, ` in << */ |
| 186 | #define IODUP 32 /* >&digit */ |
| 187 | #define IOCLOSE 64 /* >&- */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 188 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 189 | #define IODEFAULT (-1) /* "default" IO fd */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 190 | |
| 191 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 192 | /* |
| 193 | * Description of a command or an operation on commands. |
| 194 | * Might eventually use a union. |
| 195 | */ |
| 196 | struct op { |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 197 | smallint op_type; /* operation type, see Txxxx below */ |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 198 | char **op_words; /* arguments to a command */ |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 199 | struct ioword **ioact; /* IO actions (eg, < > >>) */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 200 | struct op *left; |
| 201 | struct op *right; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 202 | char *str; /* identifier for case and for */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 205 | #define TCOM 1 /* command */ |
| 206 | #define TPAREN 2 /* (c-list) */ |
| 207 | #define TPIPE 3 /* a | b */ |
| 208 | #define TLIST 4 /* a [&;] b */ |
| 209 | #define TOR 5 /* || */ |
| 210 | #define TAND 6 /* && */ |
| 211 | #define TFOR 7 |
| 212 | #define TDO 8 |
| 213 | #define TCASE 9 |
| 214 | #define TIF 10 |
| 215 | #define TWHILE 11 |
| 216 | #define TUNTIL 12 |
| 217 | #define TELIF 13 |
| 218 | #define TPAT 14 /* pattern in case */ |
| 219 | #define TBRACE 15 /* {c-list} */ |
| 220 | #define TASYNC 16 /* c & */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 221 | /* Added to support "." file expansion */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 222 | #define TDOT 17 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 223 | |
| 224 | /* Strings for names to make debug easier */ |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 225 | #ifdef MSHDEBUG |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 226 | static const char *const T_CMD_NAMES[] = { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 227 | "PLACEHOLDER", |
| 228 | "TCOM", |
| 229 | "TPAREN", |
| 230 | "TPIPE", |
| 231 | "TLIST", |
| 232 | "TOR", |
| 233 | "TAND", |
| 234 | "TFOR", |
| 235 | "TDO", |
| 236 | "TCASE", |
| 237 | "TIF", |
| 238 | "TWHILE", |
| 239 | "TUNTIL", |
| 240 | "TELIF", |
| 241 | "TPAT", |
| 242 | "TBRACE", |
| 243 | "TASYNC", |
| 244 | "TDOT", |
| 245 | }; |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 246 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 247 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 248 | #define AREASIZE (90000) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 249 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 250 | /* |
| 251 | * flags to control evaluation of words |
| 252 | */ |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 253 | #define DOSUB 1 /* interpret $, `, and quotes */ |
| 254 | #define DOBLANK 2 /* perform blank interpretation */ |
| 255 | #define DOGLOB 4 /* interpret [?* */ |
| 256 | #define DOKEY 8 /* move words with `=' to 2nd arg. list */ |
| 257 | #define DOTRIM 16 /* trim resulting string */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 258 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 259 | #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 260 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 261 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 262 | struct brkcon { |
| 263 | jmp_buf brkpt; |
| 264 | struct brkcon *nextlev; |
| 265 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 266 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 267 | |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 268 | static smallint trapset; /* trap pending (signal number) */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 269 | |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 270 | static smallint yynerrs; /* yacc (flag) */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 271 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 272 | /* moved to G: static char line[LINELIM]; */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 273 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 274 | #if ENABLE_FEATURE_EDITING |
| 275 | static char *current_prompt; |
| 276 | static line_input_t *line_input_state; |
| 277 | #endif |
| 278 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 279 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 280 | /* |
| 281 | * other functions |
| 282 | */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 283 | static const char *rexecve(char *c, char **v, char **envp); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 284 | static char *evalstr(char *cp, int f); |
| 285 | static char *putn(int n); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 286 | static char *unquote(char *as); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 287 | static int rlookup(char *n); |
| 288 | static struct wdblock *glob(char *cp, struct wdblock *wb); |
| 289 | static int my_getc(int ec); |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 290 | static int subgetc(char ec, int quoted); |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 291 | static char **makenv(int all, struct wdblock *wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 292 | static char **eval(char **ap, int f); |
| 293 | static int setstatus(int s); |
| 294 | static int waitfor(int lastpid, int canintr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 295 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 296 | static void onintr(int s); /* SIGINT handler */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 297 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 298 | static int newenv(int f); |
| 299 | static void quitenv(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 300 | static void next(int f); |
| 301 | static void setdash(void); |
| 302 | static void onecommand(void); |
| 303 | static void runtrap(int i); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 304 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 305 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 306 | /* -------- area stuff -------- */ |
| 307 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 308 | #define REGSIZE sizeof(struct region) |
| 309 | #define GROWBY (256) |
| 310 | /* #define SHRINKBY (64) */ |
| 311 | #undef SHRINKBY |
| 312 | #define FREE (32767) |
| 313 | #define BUSY (0) |
| 314 | #define ALIGN (sizeof(int)-1) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 315 | |
| 316 | |
| 317 | struct region { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 318 | struct region *next; |
| 319 | int area; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 323 | /* -------- grammar stuff -------- */ |
| 324 | typedef union { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 325 | char *cp; |
| 326 | char **wp; |
| 327 | int i; |
| 328 | struct op *o; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 329 | } YYSTYPE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 330 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 331 | #define WORD 256 |
| 332 | #define LOGAND 257 |
| 333 | #define LOGOR 258 |
| 334 | #define BREAK 259 |
| 335 | #define IF 260 |
| 336 | #define THEN 261 |
| 337 | #define ELSE 262 |
| 338 | #define ELIF 263 |
| 339 | #define FI 264 |
| 340 | #define CASE 265 |
| 341 | #define ESAC 266 |
| 342 | #define FOR 267 |
| 343 | #define WHILE 268 |
| 344 | #define UNTIL 269 |
| 345 | #define DO 270 |
| 346 | #define DONE 271 |
| 347 | #define IN 272 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 348 | /* Added for "." file expansion */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 349 | #define DOT 273 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 350 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 351 | #define YYERRCODE 300 |
| 352 | |
| 353 | /* flags to yylex */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 354 | #define CONTIN 01 /* skip new lines to complete command */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 355 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 356 | static struct op *pipeline(int cf); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 357 | static struct op *andor(void); |
| 358 | static struct op *c_list(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 359 | static int synio(int cf); |
| 360 | static void musthave(int c, int cf); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 361 | static struct op *simple(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 362 | static struct op *nested(int type, int mark); |
| 363 | static struct op *command(int cf); |
| 364 | static struct op *dogroup(int onlydone); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 365 | static struct op *thenpart(void); |
| 366 | static struct op *elsepart(void); |
| 367 | static struct op *caselist(void); |
| 368 | static struct op *casepart(void); |
| 369 | static char **pattern(void); |
| 370 | static char **wordlist(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 371 | static struct op *list(struct op *t1, struct op *t2); |
| 372 | static struct op *block(int type, struct op *t1, struct op *t2, char **wp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 373 | static struct op *newtp(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 374 | static struct op *namelist(struct op *t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 375 | static char **copyw(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 376 | static void word(char *cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 377 | static struct ioword **copyio(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 378 | static struct ioword *io(int u, int f, char *cp); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 379 | static int yylex(int cf); |
| 380 | static int collect(int c, int c1); |
| 381 | static int dual(int c); |
| 382 | static void diag(int ec); |
| 383 | static char *tree(unsigned size); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 384 | |
| 385 | /* -------- var.h -------- */ |
| 386 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 387 | struct var { |
| 388 | char *value; |
| 389 | char *name; |
| 390 | struct var *next; |
| 391 | char status; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 392 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 393 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 394 | #define COPYV 1 /* flag to setval, suggesting copy */ |
| 395 | #define RONLY 01 /* variable is read-only */ |
| 396 | #define EXPORT 02 /* variable is to be exported */ |
| 397 | #define GETCELL 04 /* name & value space was got with getcell */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 398 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 399 | static int yyparse(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 400 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 401 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 402 | /* -------- io.h -------- */ |
| 403 | /* io buffer */ |
| 404 | struct iobuf { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 405 | unsigned id; /* buffer id */ |
| 406 | char buf[512]; /* buffer */ |
| 407 | char *bufp; /* pointer into buffer */ |
| 408 | char *ebufp; /* pointer to end of buffer */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | /* possible arguments to an IO function */ |
| 412 | struct ioarg { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 413 | const char *aword; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 414 | char **awordlist; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 415 | int afile; /* file descriptor */ |
| 416 | unsigned afid; /* buffer id */ |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 417 | off_t afpos; /* file position */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 418 | struct iobuf *afbuf; /* buffer for this file */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 419 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 420 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 421 | /* an input generator's state */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 422 | struct io { |
| 423 | int (*iofn) (struct ioarg *, struct io *); |
| 424 | struct ioarg *argp; |
| 425 | int peekc; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 426 | char prev; /* previous character read by readc() */ |
| 427 | char nlcount; /* for `'s */ |
| 428 | char xchar; /* for `'s */ |
| 429 | char task; /* reason for pushed IO */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 430 | }; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 431 | /* ->task: */ |
| 432 | #define XOTHER 0 /* none of the below */ |
| 433 | #define XDOLL 1 /* expanding ${} */ |
| 434 | #define XGRAVE 2 /* expanding `'s */ |
| 435 | #define XIO 3 /* file IO */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 436 | |
| 437 | |
| 438 | /* |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 439 | * input generators for IO structure |
| 440 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 441 | static int nlchar(struct ioarg *ap); |
| 442 | static int strchar(struct ioarg *ap); |
| 443 | static int qstrchar(struct ioarg *ap); |
| 444 | static int filechar(struct ioarg *ap); |
| 445 | static int herechar(struct ioarg *ap); |
| 446 | static int linechar(struct ioarg *ap); |
| 447 | static int gravechar(struct ioarg *ap, struct io *iop); |
| 448 | static int qgravechar(struct ioarg *ap, struct io *iop); |
| 449 | static int dolchar(struct ioarg *ap); |
| 450 | static int wdchar(struct ioarg *ap); |
| 451 | static void scraphere(void); |
| 452 | static void freehere(int area); |
| 453 | static void gethere(void); |
| 454 | static void markhere(char *s, struct ioword *iop); |
| 455 | static int herein(char *hname, int xdoll); |
| 456 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 457 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 458 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 459 | static int eofc(void); |
| 460 | static int readc(void); |
| 461 | static void unget(int c); |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 462 | static void ioecho(char c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 463 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 464 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 465 | /* |
| 466 | * IO control |
| 467 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 468 | static void pushio(struct ioarg *argp, int (*f) (struct ioarg *)); |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 469 | #define PUSHIO(what,arg,gen) ((temparg.what = (arg)), pushio(&temparg,(gen))) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 470 | static int remap(int fd); |
| 471 | static int openpipe(int *pv); |
| 472 | static void closepipe(int *pv); |
| 473 | static struct io *setbase(struct io *ip); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 474 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 475 | /* -------- word.h -------- */ |
| 476 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 477 | #define NSTART 16 /* default number of words to allow for initially */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 478 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 479 | struct wdblock { |
| 480 | short w_bsize; |
| 481 | short w_nword; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 482 | /* bounds are arbitrary */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 483 | char *w_words[1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 484 | }; |
| 485 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 486 | static struct wdblock *addword(char *wd, struct wdblock *wb); |
| 487 | static struct wdblock *newword(int nw); |
| 488 | static char **getwords(struct wdblock *wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 489 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 490 | /* -------- misc stuff -------- */ |
| 491 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 492 | static int dolabel(struct op *t, char **args); |
| 493 | static int dohelp(struct op *t, char **args); |
| 494 | static int dochdir(struct op *t, char **args); |
| 495 | static int doshift(struct op *t, char **args); |
| 496 | static int dologin(struct op *t, char **args); |
| 497 | static int doumask(struct op *t, char **args); |
| 498 | static int doexec(struct op *t, char **args); |
| 499 | static int dodot(struct op *t, char **args); |
| 500 | static int dowait(struct op *t, char **args); |
| 501 | static int doread(struct op *t, char **args); |
| 502 | static int doeval(struct op *t, char **args); |
| 503 | static int dotrap(struct op *t, char **args); |
| 504 | static int dobreak(struct op *t, char **args); |
| 505 | static int doexit(struct op *t, char **args); |
| 506 | static int doexport(struct op *t, char **args); |
| 507 | static int doreadonly(struct op *t, char **args); |
| 508 | static int doset(struct op *t, char **args); |
| 509 | static int dotimes(struct op *t, char **args); |
| 510 | static int docontinue(struct op *t, char **args); |
| 511 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 512 | static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp); |
| 513 | static int execute(struct op *t, int *pin, int *pout, int no_fork); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 514 | static int iosetup(struct ioword *iop, int pipein, int pipeout); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 515 | static void brkset(struct brkcon *bc); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 516 | static int getsig(char *s); |
| 517 | static void setsig(int n, sighandler_t f); |
| 518 | static int getn(char *as); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 519 | static int brkcontin(char *cp, int val); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 520 | static void rdexp(char **wp, void (*f) (struct var *), int key); |
| 521 | static void badid(char *s); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 522 | static void varput(char *s, int out); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 523 | static int expand(const char *cp, struct wdblock **wbp, int f); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 524 | static char *blank(int f); |
| 525 | static int dollar(int quoted); |
| 526 | static int grave(int quoted); |
| 527 | static void globname(char *we, char *pp); |
| 528 | static char *generate(char *start1, char *end1, char *middle, char *end); |
| 529 | static int anyspcl(struct wdblock *wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 530 | static void readhere(char **name, char *s, int ec); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 531 | static int xxchar(struct ioarg *ap); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 532 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 533 | struct here { |
| 534 | char *h_tag; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 535 | char h_dosub; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 536 | struct ioword *h_iop; |
| 537 | struct here *h_next; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 538 | }; |
| 539 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 540 | static const char *const signame[] = { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 541 | "Signal 0", |
| 542 | "Hangup", |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 543 | NULL, /* interrupt */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 544 | "Quit", |
| 545 | "Illegal instruction", |
| 546 | "Trace/BPT trap", |
| 547 | "Abort", |
| 548 | "Bus error", |
| 549 | "Floating Point Exception", |
| 550 | "Killed", |
| 551 | "SIGUSR1", |
| 552 | "SIGSEGV", |
| 553 | "SIGUSR2", |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 554 | NULL, /* broken pipe */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 555 | "Alarm clock", |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 556 | "Terminated" |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 557 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 558 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 559 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 560 | typedef int (*builtin_func_ptr)(struct op *, char **); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 561 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 562 | struct builtincmd { |
| 563 | const char *name; |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 564 | builtin_func_ptr builtinfunc; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 565 | }; |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 566 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 567 | static const struct builtincmd builtincmds[] = { |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 568 | { "." , dodot }, |
| 569 | { ":" , dolabel }, |
| 570 | { "break" , dobreak }, |
| 571 | { "cd" , dochdir }, |
| 572 | { "continue", docontinue }, |
| 573 | { "eval" , doeval }, |
| 574 | { "exec" , doexec }, |
| 575 | { "exit" , doexit }, |
| 576 | { "export" , doexport }, |
| 577 | { "help" , dohelp }, |
| 578 | { "login" , dologin }, |
| 579 | { "newgrp" , dologin }, |
| 580 | { "read" , doread }, |
| 581 | { "readonly", doreadonly }, |
| 582 | { "set" , doset }, |
| 583 | { "shift" , doshift }, |
| 584 | { "times" , dotimes }, |
| 585 | { "trap" , dotrap }, |
| 586 | { "umask" , doumask }, |
| 587 | { "wait" , dowait }, |
| 588 | { NULL , NULL }, |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 589 | }; |
| 590 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 591 | static struct op *dowholefile(int /*, int*/); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 592 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 593 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 594 | /* Globals */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 595 | static char **dolv; |
| 596 | static int dolc; |
Denis Vlasenko | 447bd66 | 2008-05-30 22:28:32 +0000 | [diff] [blame] | 597 | static uint8_t exstat; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 598 | static smallint gflg; /* (seems to be a parse error indicator) */ |
| 599 | static smallint interactive; /* Is this an interactive shell */ |
| 600 | static smallint execflg; |
| 601 | static smallint isbreak; /* "break" statement was seen */ |
| 602 | static int multiline; /* '\n' changed to ';' (counter) */ |
| 603 | static struct op *outtree; /* result from parser */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 604 | static xint *failpt; |
| 605 | static xint *errpt; |
| 606 | static struct brkcon *brklist; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 607 | static struct wdblock *wdlist; |
| 608 | static struct wdblock *iolist; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 609 | |
| 610 | #ifdef MSHDEBUG |
| 611 | static struct var *mshdbg_var; |
| 612 | #endif |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 613 | static struct var *vlist; /* dictionary */ |
| 614 | static struct var *homedir; /* home directory */ |
| 615 | static struct var *prompt; /* main prompt */ |
| 616 | static struct var *cprompt; /* continuation prompt */ |
| 617 | static struct var *path; /* search path for commands */ |
| 618 | static struct var *shell; /* shell to interpret command files */ |
| 619 | static struct var *ifs; /* field separators */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 620 | |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 621 | static int areanum; /* current allocation area */ |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 622 | static smallint intr; /* interrupt pending (bool) */ |
| 623 | static smallint heedint = 1; /* heed interrupt signals (bool) */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 624 | static int inparse; |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 625 | static char *null = (char*)""; /* null value for variable */ |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 626 | static void (*qflag)(int) = SIG_IGN; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 627 | static int startl; |
| 628 | static int peeksym; |
| 629 | static int nlseen; |
| 630 | static int iounit = IODEFAULT; |
| 631 | static YYSTYPE yylval; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 632 | static char *elinep; /* done in main(): = line + sizeof(line) - 5 */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 633 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 634 | static struct here *inhere; /* list of hear docs while parsing */ |
| 635 | static struct here *acthere; /* list of active here documents */ |
| 636 | static struct region *areabot; /* bottom of area */ |
| 637 | static struct region *areatop; /* top of area */ |
| 638 | static struct region *areanxt; /* starting point of scan */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 639 | static void *brktop; |
| 640 | static void *brkaddr; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 641 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 642 | #define AFID_NOBUF (~0) |
| 643 | #define AFID_ID 0 |
| 644 | |
| 645 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 646 | /* |
| 647 | * parsing & execution environment |
| 648 | */ |
| 649 | struct env { |
| 650 | char *linep; |
| 651 | struct io *iobase; |
| 652 | struct io *iop; |
| 653 | xint *errpt; /* void * */ |
| 654 | int iofd; |
| 655 | struct env *oenv; |
| 656 | }; |
| 657 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 658 | |
| 659 | struct globals { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 660 | struct env global_env; |
| 661 | struct ioarg temparg; // = { .afid = AFID_NOBUF }; /* temporary for PUSHIO */ |
| 662 | unsigned bufid; // = AFID_ID; /* buffer id counter */ |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 663 | char ourtrap[_NSIG + 1]; |
| 664 | char *trap[_NSIG + 1]; |
| 665 | struct iobuf sharedbuf; /* in main(): set to { AFID_NOBUF } */ |
| 666 | struct iobuf mainbuf; /* in main(): set to { AFID_NOBUF } */ |
| 667 | struct ioarg ioargstack[NPUSH]; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 668 | /* |
| 669 | * flags: |
| 670 | * -e: quit on error |
| 671 | * -k: look for name=value everywhere on command line |
| 672 | * -n: no execution |
| 673 | * -t: exit after reading and executing one command |
| 674 | * -v: echo as read |
| 675 | * -x: trace |
| 676 | * -u: unset variables net diagnostic |
| 677 | */ |
| 678 | char flags['z' - 'a' + 1]; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 679 | char filechar_cmdbuf[BUFSIZ]; |
| 680 | char line[LINELIM]; |
| 681 | char child_cmd[LINELIM]; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 682 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 683 | struct io iostack[NPUSH]; |
| 684 | |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 685 | char grave__var_name[LINELIM]; |
| 686 | char grave__alt_value[LINELIM]; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 687 | }; |
| 688 | |
| 689 | #define G (*ptr_to_globals) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 690 | #define global_env (G.global_env ) |
| 691 | #define temparg (G.temparg ) |
| 692 | #define bufid (G.bufid ) |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 693 | #define ourtrap (G.ourtrap ) |
| 694 | #define trap (G.trap ) |
| 695 | #define sharedbuf (G.sharedbuf ) |
| 696 | #define mainbuf (G.mainbuf ) |
| 697 | #define ioargstack (G.ioargstack ) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 698 | /* this looks weird, but is OK ... we index FLAG with 'a'...'z' */ |
| 699 | #define FLAG (G.flags - 'a' ) |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 700 | #define filechar_cmdbuf (G.filechar_cmdbuf) |
| 701 | #define line (G.line ) |
| 702 | #define child_cmd (G.child_cmd ) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 703 | #define iostack (G.iostack ) |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 704 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 705 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 706 | global_env.linep = line; \ |
| 707 | global_env.iobase = iostack; \ |
| 708 | global_env.iop = iostack - 1; \ |
| 709 | global_env.iofd = FDBASE; \ |
| 710 | temparg.afid = AFID_NOBUF; \ |
| 711 | bufid = AFID_ID; \ |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 712 | } while (0) |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 713 | |
| 714 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 715 | /* in substitution */ |
| 716 | #define INSUB() (global_env.iop->task == XGRAVE || global_env.iop->task == XDOLL) |
| 717 | |
| 718 | #define RUN(what, arg, gen) ((temparg.what = (arg)), run(&temparg, (gen))) |
| 719 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 720 | #ifdef MSHDEBUG |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 721 | static void print_tree(struct op *head) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 722 | { |
| 723 | if (head == NULL) { |
| 724 | DBGPRINTF(("PRINT_TREE: no tree\n")); |
| 725 | return; |
| 726 | } |
| 727 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 728 | DBGPRINTF(("NODE: %p, left %p, right %p\n", head, head->left, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 729 | head->right)); |
| 730 | |
| 731 | if (head->left) |
| 732 | print_tree(head->left); |
| 733 | |
| 734 | if (head->right) |
| 735 | print_tree(head->right); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 736 | } |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 737 | #endif /* MSHDEBUG */ |
| 738 | |
| 739 | |
| 740 | /* |
| 741 | * IO functions |
| 742 | */ |
| 743 | static void prs(const char *s) |
| 744 | { |
| 745 | if (*s) |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 746 | write(STDERR_FILENO, s, strlen(s)); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | static void prn(unsigned u) |
| 750 | { |
| 751 | prs(itoa(u)); |
| 752 | } |
| 753 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 754 | static void echo(char **wp) |
| 755 | { |
| 756 | int i; |
| 757 | |
| 758 | prs("+"); |
| 759 | for (i = 0; wp[i]; i++) { |
| 760 | if (i) |
| 761 | prs(" "); |
| 762 | prs(wp[i]); |
| 763 | } |
| 764 | prs("\n"); |
| 765 | } |
| 766 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 767 | static void closef(int i) |
| 768 | { |
| 769 | if (i > 2) |
| 770 | close(i); |
| 771 | } |
| 772 | |
| 773 | static void closeall(void) |
| 774 | { |
| 775 | int u; |
| 776 | |
| 777 | for (u = NUFILE; u < NOFILE;) |
| 778 | close(u++); |
| 779 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 780 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 781 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 782 | /* fail but return to process next command */ |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 783 | static void fail(void) ATTRIBUTE_NORETURN; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 784 | static void fail(void) |
| 785 | { |
| 786 | longjmp(failpt, 1); |
| 787 | /* NOTREACHED */ |
| 788 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 789 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 790 | /* abort shell (or fail in subshell) */ |
| 791 | static void leave(void) ATTRIBUTE_NORETURN; |
| 792 | static void leave(void) |
| 793 | { |
| 794 | DBGPRINTF(("LEAVE: leave called!\n")); |
| 795 | |
| 796 | if (execflg) |
| 797 | fail(); |
| 798 | scraphere(); |
| 799 | freehere(1); |
| 800 | runtrap(0); |
| 801 | _exit(exstat); |
| 802 | /* NOTREACHED */ |
| 803 | } |
| 804 | |
| 805 | static void warn(const char *s) |
| 806 | { |
| 807 | if (*s) { |
| 808 | prs(s); |
Denis Vlasenko | 447bd66 | 2008-05-30 22:28:32 +0000 | [diff] [blame] | 809 | if (!exstat) |
| 810 | exstat = 255; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 811 | } |
| 812 | prs("\n"); |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 813 | if (FLAG['e']) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 814 | leave(); |
| 815 | } |
| 816 | |
| 817 | static void err(const char *s) |
| 818 | { |
| 819 | warn(s); |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 820 | if (FLAG['n']) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 821 | return; |
| 822 | if (!interactive) |
| 823 | leave(); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 824 | if (global_env.errpt) |
| 825 | longjmp(global_env.errpt, 1); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 826 | closeall(); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 827 | global_env.iop = global_env.iobase = iostack; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 830 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 831 | /* -------- area.c -------- */ |
| 832 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 833 | /* |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 834 | * All memory between (char *)areabot and (char *)(areatop+1) is |
| 835 | * exclusively administered by the area management routines. |
| 836 | * It is assumed that sbrk() and brk() manipulate the high end. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 837 | */ |
| 838 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 839 | #define sbrk(X) ({ \ |
| 840 | void * __q = (void *)-1; \ |
| 841 | if (brkaddr + (int)(X) < brktop) { \ |
| 842 | __q = brkaddr; \ |
| 843 | brkaddr += (int)(X); \ |
| 844 | } \ |
| 845 | __q; \ |
| 846 | }) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 847 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 848 | static void initarea(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 849 | { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 850 | brkaddr = xmalloc(AREASIZE); |
| 851 | brktop = brkaddr + AREASIZE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 852 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 853 | while ((long) sbrk(0) & ALIGN) |
| 854 | sbrk(1); |
| 855 | areabot = (struct region *) sbrk(REGSIZE); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 856 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 857 | areabot->next = areabot; |
| 858 | areabot->area = BUSY; |
| 859 | areatop = areabot; |
| 860 | areanxt = areabot; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 863 | static char *getcell(unsigned nbytes) |
| 864 | { |
| 865 | int nregio; |
| 866 | struct region *p, *q; |
| 867 | int i; |
| 868 | |
| 869 | if (nbytes == 0) { |
| 870 | puts("getcell(0)"); |
| 871 | abort(); |
| 872 | } |
| 873 | /* silly and defeats the algorithm */ |
| 874 | /* |
| 875 | * round upwards and add administration area |
| 876 | */ |
| 877 | nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1; |
| 878 | p = areanxt; |
| 879 | for (;;) { |
| 880 | if (p->area > areanum) { |
| 881 | /* |
| 882 | * merge free cells |
| 883 | */ |
| 884 | while ((q = p->next)->area > areanum && q != areanxt) |
| 885 | p->next = q->next; |
| 886 | /* |
| 887 | * exit loop if cell big enough |
| 888 | */ |
| 889 | if (q >= p + nregio) |
| 890 | goto found; |
| 891 | } |
| 892 | p = p->next; |
| 893 | if (p == areanxt) |
| 894 | break; |
| 895 | } |
| 896 | i = nregio >= GROWBY ? nregio : GROWBY; |
| 897 | p = (struct region *) sbrk(i * REGSIZE); |
| 898 | if (p == (struct region *) -1) |
| 899 | return NULL; |
| 900 | p--; |
| 901 | if (p != areatop) { |
| 902 | puts("not contig"); |
| 903 | abort(); /* allocated areas are contiguous */ |
| 904 | } |
| 905 | q = p + i; |
| 906 | p->next = q; |
| 907 | p->area = FREE; |
| 908 | q->next = areabot; |
| 909 | q->area = BUSY; |
| 910 | areatop = q; |
| 911 | found: |
| 912 | /* |
| 913 | * we found a FREE area big enough, pointed to by 'p', and up to 'q' |
| 914 | */ |
| 915 | areanxt = p + nregio; |
| 916 | if (areanxt < q) { |
| 917 | /* |
| 918 | * split into requested area and rest |
| 919 | */ |
| 920 | if (areanxt + 1 > q) { |
| 921 | puts("OOM"); |
| 922 | abort(); /* insufficient space left for admin */ |
| 923 | } |
| 924 | areanxt->next = q; |
| 925 | areanxt->area = FREE; |
| 926 | p->next = areanxt; |
| 927 | } |
| 928 | p->area = areanum; |
| 929 | return (char *) (p + 1); |
| 930 | } |
| 931 | |
| 932 | static void freecell(char *cp) |
| 933 | { |
| 934 | struct region *p; |
| 935 | |
| 936 | p = (struct region *) cp; |
| 937 | if (p != NULL) { |
| 938 | p--; |
| 939 | if (p < areanxt) |
| 940 | areanxt = p; |
| 941 | p->area = FREE; |
| 942 | } |
| 943 | } |
| 944 | #define DELETE(obj) freecell((char *)obj) |
| 945 | |
| 946 | static void freearea(int a) |
| 947 | { |
| 948 | struct region *p, *top; |
| 949 | |
| 950 | top = areatop; |
| 951 | for (p = areabot; p != top; p = p->next) |
| 952 | if (p->area >= a) |
| 953 | p->area = FREE; |
| 954 | } |
| 955 | |
| 956 | static void setarea(char *cp, int a) |
| 957 | { |
| 958 | struct region *p; |
| 959 | |
| 960 | p = (struct region *) cp; |
| 961 | if (p != NULL) |
| 962 | (p - 1)->area = a; |
| 963 | } |
| 964 | |
| 965 | static int getarea(char *cp) |
| 966 | { |
| 967 | return ((struct region *) cp - 1)->area; |
| 968 | } |
| 969 | |
| 970 | static void garbage(void) |
| 971 | { |
| 972 | struct region *p, *q, *top; |
| 973 | |
| 974 | top = areatop; |
| 975 | for (p = areabot; p != top; p = p->next) { |
| 976 | if (p->area > areanum) { |
| 977 | while ((q = p->next)->area > areanum) |
| 978 | p->next = q->next; |
| 979 | areanxt = p; |
| 980 | } |
| 981 | } |
| 982 | #ifdef SHRINKBY |
| 983 | if (areatop >= q + SHRINKBY && q->area > areanum) { |
| 984 | brk((char *) (q + 1)); |
| 985 | q->next = areabot; |
| 986 | q->area = BUSY; |
| 987 | areatop = q; |
| 988 | } |
| 989 | #endif |
| 990 | } |
| 991 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 992 | static void *get_space(int n) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 993 | { |
| 994 | char *cp; |
| 995 | |
| 996 | cp = getcell(n); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 997 | if (cp == NULL) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 998 | err("out of string space"); |
| 999 | return cp; |
| 1000 | } |
| 1001 | |
| 1002 | static char *strsave(const char *s, int a) |
| 1003 | { |
| 1004 | char *cp; |
| 1005 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1006 | cp = get_space(strlen(s) + 1); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1007 | if (cp == NULL) { |
| 1008 | // FIXME: I highly doubt this is good. |
| 1009 | return (char*)""; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1010 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1011 | setarea(cp, a); |
| 1012 | strcpy(cp, s); |
| 1013 | return cp; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1016 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1017 | /* -------- var.c -------- */ |
| 1018 | |
| 1019 | static int eqname(const char *n1, const char *n2) |
| 1020 | { |
| 1021 | for (; *n1 != '=' && *n1 != '\0'; n1++) |
| 1022 | if (*n2++ != *n1) |
| 1023 | return 0; |
| 1024 | return *n2 == '\0' || *n2 == '='; |
| 1025 | } |
| 1026 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1027 | static const char *findeq(const char *cp) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1028 | { |
| 1029 | while (*cp != '\0' && *cp != '=') |
| 1030 | cp++; |
| 1031 | return cp; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * Find the given name in the dictionary |
| 1036 | * and return its value. If the name was |
| 1037 | * not previously there, enter it now and |
| 1038 | * return a null value. |
| 1039 | */ |
| 1040 | static struct var *lookup(const char *n) |
| 1041 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1042 | // FIXME: dirty hack |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1043 | static struct var dummy; |
| 1044 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1045 | struct var *vp; |
| 1046 | const char *cp; |
| 1047 | char *xp; |
| 1048 | int c; |
| 1049 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1050 | if (isdigit(*n)) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1051 | dummy.name = (char*)n; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1052 | for (c = 0; isdigit(*n) && c < 1000; n++) |
| 1053 | c = c * 10 + *n - '0'; |
| 1054 | dummy.status = RONLY; |
| 1055 | dummy.value = (c <= dolc ? dolv[c] : null); |
| 1056 | return &dummy; |
| 1057 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1058 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1059 | for (vp = vlist; vp; vp = vp->next) |
| 1060 | if (eqname(vp->name, n)) |
| 1061 | return vp; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1062 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1063 | cp = findeq(n); |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1064 | vp = get_space(sizeof(*vp)); |
| 1065 | if (vp == 0 || (vp->name = get_space((int) (cp - n) + 2)) == NULL) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1066 | dummy.name = dummy.value = (char*)""; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1067 | return &dummy; |
| 1068 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1069 | |
| 1070 | xp = vp->name; |
| 1071 | while ((*xp = *n++) != '\0' && *xp != '=') |
| 1072 | xp++; |
| 1073 | *xp++ = '='; |
| 1074 | *xp = '\0'; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1075 | setarea((char *) vp, 0); |
| 1076 | setarea((char *) vp->name, 0); |
| 1077 | vp->value = null; |
| 1078 | vp->next = vlist; |
| 1079 | vp->status = GETCELL; |
| 1080 | vlist = vp; |
| 1081 | return vp; |
| 1082 | } |
| 1083 | |
| 1084 | /* |
| 1085 | * if name is not NULL, it must be |
| 1086 | * a prefix of the space `val', |
| 1087 | * and end with `='. |
| 1088 | * this is all so that exporting |
| 1089 | * values is reasonably painless. |
| 1090 | */ |
| 1091 | static void nameval(struct var *vp, const char *val, const char *name) |
| 1092 | { |
| 1093 | const char *cp; |
| 1094 | char *xp; |
| 1095 | int fl; |
| 1096 | |
| 1097 | if (vp->status & RONLY) { |
| 1098 | xp = vp->name; |
| 1099 | while (*xp && *xp != '=') |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1100 | fputc(*xp++, stderr); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1101 | err(" is read-only"); |
| 1102 | return; |
| 1103 | } |
| 1104 | fl = 0; |
| 1105 | if (name == NULL) { |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1106 | xp = get_space(strlen(vp->name) + strlen(val) + 2); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1107 | if (xp == NULL) |
| 1108 | return; |
| 1109 | /* make string: name=value */ |
| 1110 | setarea(xp, 0); |
| 1111 | name = xp; |
| 1112 | cp = vp->name; |
| 1113 | while ((*xp = *cp++) != '\0' && *xp != '=') |
| 1114 | xp++; |
| 1115 | *xp++ = '='; |
| 1116 | strcpy(xp, val); |
| 1117 | val = xp; |
| 1118 | fl = GETCELL; |
| 1119 | } |
| 1120 | if (vp->status & GETCELL) |
| 1121 | freecell(vp->name); /* form new string `name=value' */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1122 | vp->name = (char*)name; |
| 1123 | vp->value = (char*)val; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1124 | vp->status |= fl; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * give variable at `vp' the value `val'. |
| 1129 | */ |
| 1130 | static void setval(struct var *vp, const char *val) |
| 1131 | { |
| 1132 | nameval(vp, val, NULL); |
| 1133 | } |
| 1134 | |
| 1135 | static void export(struct var *vp) |
| 1136 | { |
| 1137 | vp->status |= EXPORT; |
| 1138 | } |
| 1139 | |
| 1140 | static void ronly(struct var *vp) |
| 1141 | { |
| 1142 | if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */ |
| 1143 | vp->status |= RONLY; |
| 1144 | } |
| 1145 | |
| 1146 | static int isassign(const char *s) |
| 1147 | { |
| 1148 | unsigned char c; |
| 1149 | DBGPRINTF7(("ISASSIGN: enter, s=%s\n", s)); |
| 1150 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1151 | c = *s; |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 1152 | /* no isalpha() - we shouldn't use locale */ |
| 1153 | /* c | 0x20 - lowercase (Latin) letters */ |
| 1154 | if (c != '_' && (unsigned)((c|0x20) - 'a') > 25) |
| 1155 | /* not letter */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1156 | return 0; |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 1157 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1158 | while (1) { |
| 1159 | c = *++s; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1160 | if (c == '=') |
| 1161 | return 1; |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 1162 | if (c == '\0') |
| 1163 | return 0; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1164 | if (c != '_' |
| 1165 | && (unsigned)(c - '0') > 9 /* not number */ |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 1166 | && (unsigned)((c|0x20) - 'a') > 25 /* not letter */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1167 | ) { |
| 1168 | return 0; |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | static int assign(const char *s, int cf) |
| 1174 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1175 | const char *cp; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1176 | struct var *vp; |
| 1177 | |
| 1178 | DBGPRINTF7(("ASSIGN: enter, s=%s, cf=%d\n", s, cf)); |
| 1179 | |
| 1180 | if (!isalpha(*s) && *s != '_') |
| 1181 | return 0; |
| 1182 | for (cp = s; *cp != '='; cp++) |
| 1183 | if (*cp == '\0' || (!isalnum(*cp) && *cp != '_')) |
| 1184 | return 0; |
| 1185 | vp = lookup(s); |
| 1186 | nameval(vp, ++cp, cf == COPYV ? NULL : s); |
| 1187 | if (cf != COPYV) |
| 1188 | vp->status &= ~GETCELL; |
| 1189 | return 1; |
| 1190 | } |
| 1191 | |
| 1192 | static int checkname(char *cp) |
| 1193 | { |
| 1194 | DBGPRINTF7(("CHECKNAME: enter, cp=%s\n", cp)); |
| 1195 | |
| 1196 | if (!isalpha(*cp++) && *(cp - 1) != '_') |
| 1197 | return 0; |
| 1198 | while (*cp) |
| 1199 | if (!isalnum(*cp++) && *(cp - 1) != '_') |
| 1200 | return 0; |
| 1201 | return 1; |
| 1202 | } |
| 1203 | |
| 1204 | static void putvlist(int f, int out) |
| 1205 | { |
| 1206 | struct var *vp; |
| 1207 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1208 | for (vp = vlist; vp; vp = vp->next) { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1209 | if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) { |
| 1210 | if (vp->status & EXPORT) |
| 1211 | write(out, "export ", 7); |
| 1212 | if (vp->status & RONLY) |
| 1213 | write(out, "readonly ", 9); |
| 1214 | write(out, vp->name, (int) (findeq(vp->name) - vp->name)); |
| 1215 | write(out, "\n", 1); |
| 1216 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1217 | } |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | /* |
| 1222 | * trap handling |
| 1223 | */ |
| 1224 | static void sig(int i) |
| 1225 | { |
| 1226 | trapset = i; |
| 1227 | signal(i, sig); |
| 1228 | } |
| 1229 | |
| 1230 | static void runtrap(int i) |
| 1231 | { |
| 1232 | char *trapstr; |
| 1233 | |
| 1234 | trapstr = trap[i]; |
| 1235 | if (trapstr == NULL) |
| 1236 | return; |
| 1237 | |
| 1238 | if (i == 0) |
| 1239 | trap[i] = NULL; |
| 1240 | |
| 1241 | RUN(aword, trapstr, nlchar); |
| 1242 | } |
| 1243 | |
| 1244 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1245 | static void setdash(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1246 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1247 | char *cp; |
| 1248 | int c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1249 | char m['z' - 'a' + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1250 | |
| 1251 | cp = m; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1252 | for (c = 'a'; c <= 'z'; c++) |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 1253 | if (FLAG[c]) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1254 | *cp++ = c; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1255 | *cp = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1256 | setval(lookup("-"), m); |
| 1257 | } |
| 1258 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1259 | static int newfile(char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1260 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1261 | int f; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1262 | |
| 1263 | DBGPRINTF7(("NEWFILE: opening %s\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1264 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1265 | f = 0; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 1266 | if (NOT_LONE_DASH(s)) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1267 | DBGPRINTF(("NEWFILE: s is %s\n", s)); |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 1268 | f = open(s, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1269 | if (f < 0) { |
| 1270 | prs(s); |
| 1271 | err(": cannot open"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1272 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1273 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1274 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1275 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1276 | next(remap(f)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1277 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1280 | |
Denis Vlasenko | bc1918a | 2008-04-15 01:17:50 +0000 | [diff] [blame] | 1281 | #ifdef UNUSED |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1282 | struct op *scantree(struct op *head) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1283 | { |
| 1284 | struct op *dotnode; |
| 1285 | |
| 1286 | if (head == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1287 | return NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1288 | |
| 1289 | if (head->left != NULL) { |
| 1290 | dotnode = scantree(head->left); |
| 1291 | if (dotnode) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1292 | return dotnode; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | if (head->right != NULL) { |
| 1296 | dotnode = scantree(head->right); |
| 1297 | if (dotnode) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1298 | return dotnode; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1301 | if (head->op_words == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1302 | return NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1303 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1304 | DBGPRINTF5(("SCANTREE: checking node %p\n", head)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1305 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1306 | if ((head->op_type != TDOT) && LONE_CHAR(head->op_words[0], '.')) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1307 | DBGPRINTF5(("SCANTREE: dot found in node %p\n", head)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1308 | return head; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1311 | return NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1312 | } |
Denis Vlasenko | bc1918a | 2008-04-15 01:17:50 +0000 | [diff] [blame] | 1313 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1314 | |
| 1315 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1316 | static void onecommand(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1317 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1318 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1319 | jmp_buf m1; |
| 1320 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1321 | DBGPRINTF(("ONECOMMAND: enter, outtree=%p\n", outtree)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1322 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1323 | while (global_env.oenv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1324 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1325 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1326 | areanum = 1; |
| 1327 | freehere(areanum); |
| 1328 | freearea(areanum); |
| 1329 | garbage(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1330 | wdlist = NULL; |
| 1331 | iolist = NULL; |
| 1332 | global_env.errpt = NULL; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1333 | global_env.linep = line; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1334 | yynerrs = 0; |
| 1335 | multiline = 0; |
| 1336 | inparse = 1; |
| 1337 | intr = 0; |
| 1338 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1339 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1340 | failpt = m1; |
| 1341 | setjmp(failpt); /* Bruce Evans' fix */ |
| 1342 | failpt = m1; |
| 1343 | if (setjmp(failpt) || yyparse() || intr) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1344 | DBGPRINTF(("ONECOMMAND: this is not good.\n")); |
| 1345 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1346 | while (global_env.oenv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1347 | quitenv(); |
| 1348 | scraphere(); |
| 1349 | if (!interactive && intr) |
| 1350 | leave(); |
| 1351 | inparse = 0; |
| 1352 | intr = 0; |
| 1353 | return; |
| 1354 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1355 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1356 | inparse = 0; |
| 1357 | brklist = 0; |
| 1358 | intr = 0; |
| 1359 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1360 | |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 1361 | if (!FLAG['n']) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1362 | DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=%p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1363 | outtree)); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 1364 | execute(outtree, NOPIPE, NOPIPE, /* no_fork: */ 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1367 | if (!interactive && intr) { |
| 1368 | execflg = 0; |
| 1369 | leave(); |
| 1370 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1371 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1372 | i = trapset; |
| 1373 | if (i != 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1374 | trapset = 0; |
| 1375 | runtrap(i); |
| 1376 | } |
| 1377 | } |
| 1378 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1379 | static int newenv(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1380 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1381 | struct env *ep; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1382 | |
| 1383 | DBGPRINTF(("NEWENV: f=%d (indicates quitenv and return)\n", f)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1384 | |
| 1385 | if (f) { |
| 1386 | quitenv(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1387 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1388 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1389 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1390 | ep = get_space(sizeof(*ep)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1391 | if (ep == NULL) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1392 | while (global_env.oenv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1393 | quitenv(); |
| 1394 | fail(); |
| 1395 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1396 | *ep = global_env; |
| 1397 | global_env.oenv = ep; |
| 1398 | global_env.errpt = errpt; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1399 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1400 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1403 | static void quitenv(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1404 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1405 | struct env *ep; |
| 1406 | int fd; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1407 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1408 | DBGPRINTF(("QUITENV: global_env.oenv=%p\n", global_env.oenv)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1409 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1410 | ep = global_env.oenv; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1411 | if (ep != NULL) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1412 | fd = global_env.iofd; |
| 1413 | global_env = *ep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1414 | /* should close `'d files */ |
| 1415 | DELETE(ep); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1416 | while (--fd >= global_env.iofd) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1417 | close(fd); |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | /* |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1422 | * Is character c in s? |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1423 | */ |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1424 | static int any(int c, const char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1425 | { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1426 | while (*s) |
| 1427 | if (*s++ == c) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1428 | return 1; |
| 1429 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | /* |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1433 | * Is any character from s1 in s2? |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1434 | */ |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1435 | static int anys(const char *s1, const char *s2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1436 | { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1437 | while (*s1) |
| 1438 | if (any(*s1++, s2)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1439 | return 1; |
| 1440 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1443 | static char *putn(int n) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1444 | { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1445 | return itoa(n); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1448 | static void next(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1449 | { |
| 1450 | PUSHIO(afile, f, filechar); |
| 1451 | } |
| 1452 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 1453 | static void onintr(int s ATTRIBUTE_UNUSED) /* ANSI C requires a parameter */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1454 | { |
| 1455 | signal(SIGINT, onintr); |
| 1456 | intr = 1; |
| 1457 | if (interactive) { |
| 1458 | if (inparse) { |
| 1459 | prs("\n"); |
| 1460 | fail(); |
| 1461 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1462 | } else if (heedint) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1463 | execflg = 0; |
| 1464 | leave(); |
| 1465 | } |
| 1466 | } |
| 1467 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1468 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1469 | /* -------- gmatch.c -------- */ |
| 1470 | /* |
| 1471 | * int gmatch(string, pattern) |
| 1472 | * char *string, *pattern; |
| 1473 | * |
| 1474 | * Match a pattern as in sh(1). |
| 1475 | */ |
| 1476 | |
| 1477 | #define CMASK 0377 |
| 1478 | #define QUOTE 0200 |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 1479 | #define QMASK (CMASK & ~QUOTE) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1480 | #define NOT '!' /* might use ^ */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1481 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1482 | static const char *cclass(const char *p, int sub) |
| 1483 | { |
| 1484 | int c, d, not, found; |
| 1485 | |
| 1486 | not = (*p == NOT); |
| 1487 | if (not != 0) |
| 1488 | p++; |
| 1489 | found = not; |
| 1490 | do { |
| 1491 | if (*p == '\0') |
| 1492 | return NULL; |
| 1493 | c = *p & CMASK; |
| 1494 | if (p[1] == '-' && p[2] != ']') { |
| 1495 | d = p[2] & CMASK; |
| 1496 | p++; |
| 1497 | } else |
| 1498 | d = c; |
| 1499 | if (c == sub || (c <= sub && sub <= d)) |
| 1500 | found = !not; |
| 1501 | } while (*++p != ']'); |
| 1502 | return found ? p + 1 : NULL; |
| 1503 | } |
| 1504 | |
| 1505 | static int gmatch(const char *s, const char *p) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1506 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1507 | int sc, pc; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1508 | |
| 1509 | if (s == NULL || p == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1510 | return 0; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1511 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1512 | while ((pc = *p++ & CMASK) != '\0') { |
| 1513 | sc = *s++ & QMASK; |
| 1514 | switch (pc) { |
| 1515 | case '[': |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1516 | p = cclass(p, sc); |
| 1517 | if (p == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1518 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1519 | break; |
| 1520 | |
| 1521 | case '?': |
| 1522 | if (sc == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1523 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1524 | break; |
| 1525 | |
| 1526 | case '*': |
| 1527 | s--; |
| 1528 | do { |
| 1529 | if (*p == '\0' || gmatch(s, p)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1530 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1531 | } while (*s++ != '\0'); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1532 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1533 | |
| 1534 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1535 | if (sc != (pc & ~QUOTE)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1536 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1537 | } |
| 1538 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1539 | return *s == '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1542 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1543 | /* -------- csyn.c -------- */ |
| 1544 | /* |
| 1545 | * shell: syntax (C version) |
| 1546 | */ |
| 1547 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1548 | static void yyerror(const char *s) ATTRIBUTE_NORETURN; |
| 1549 | static void yyerror(const char *s) |
| 1550 | { |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 1551 | yynerrs = 1; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1552 | if (interactive && global_env.iop <= iostack) { |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1553 | multiline = 0; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 1554 | while (eofc() == 0 && yylex(0) != '\n') |
| 1555 | continue; |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1556 | } |
| 1557 | err(s); |
| 1558 | fail(); |
| 1559 | } |
| 1560 | |
| 1561 | static void zzerr(void) ATTRIBUTE_NORETURN; |
| 1562 | static void zzerr(void) |
| 1563 | { |
| 1564 | yyerror("syntax error"); |
| 1565 | } |
| 1566 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1567 | int yyparse(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1568 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1569 | DBGPRINTF7(("YYPARSE: enter...\n")); |
| 1570 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1571 | startl = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1572 | peeksym = 0; |
| 1573 | yynerrs = 0; |
| 1574 | outtree = c_list(); |
| 1575 | musthave('\n', 0); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 1576 | return yynerrs; /* 0/1 */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1579 | static struct op *pipeline(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1580 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1581 | struct op *t, *p; |
| 1582 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1583 | |
| 1584 | DBGPRINTF7(("PIPELINE: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1585 | |
| 1586 | t = command(cf); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1587 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1588 | DBGPRINTF9(("PIPELINE: t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1589 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1590 | if (t != NULL) { |
| 1591 | while ((c = yylex(0)) == '|') { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1592 | p = command(CONTIN); |
| 1593 | if (p == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1594 | DBGPRINTF8(("PIPELINE: error!\n")); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1595 | zzerr(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1598 | if (t->op_type != TPAREN && t->op_type != TCOM) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1599 | /* shell statement */ |
| 1600 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 1601 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1602 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1603 | t = block(TPIPE, t, p, NOWORDS); |
| 1604 | } |
| 1605 | peeksym = c; |
| 1606 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1607 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1608 | DBGPRINTF7(("PIPELINE: returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1609 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1612 | static struct op *andor(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1613 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1614 | struct op *t, *p; |
| 1615 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1616 | |
| 1617 | DBGPRINTF7(("ANDOR: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1618 | |
| 1619 | t = pipeline(0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1620 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1621 | DBGPRINTF9(("ANDOR: t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1622 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1623 | if (t != NULL) { |
| 1624 | while ((c = yylex(0)) == LOGAND || c == LOGOR) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1625 | p = pipeline(CONTIN); |
| 1626 | if (p == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1627 | DBGPRINTF8(("ANDOR: error!\n")); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1628 | zzerr(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1631 | t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1632 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1633 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1634 | peeksym = c; |
| 1635 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1636 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1637 | DBGPRINTF7(("ANDOR: returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1638 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1641 | static struct op *c_list(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1642 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1643 | struct op *t, *p; |
| 1644 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1645 | |
| 1646 | DBGPRINTF7(("C_LIST: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1647 | |
| 1648 | t = andor(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1649 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1650 | if (t != NULL) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1651 | peeksym = yylex(0); |
| 1652 | if (peeksym == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1653 | t = block(TASYNC, t, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1654 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1655 | while ((c = yylex(0)) == ';' || c == '&' |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1656 | || (multiline && c == '\n') |
| 1657 | ) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1658 | p = andor(); |
| 1659 | if (p== NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1660 | return t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1661 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1662 | peeksym = yylex(0); |
| 1663 | if (peeksym == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1664 | p = block(TASYNC, p, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1665 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1666 | t = list(t, p); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1667 | } /* WHILE */ |
| 1668 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1669 | peeksym = c; |
| 1670 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1671 | /* IF */ |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1672 | DBGPRINTF7(("C_LIST: returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1673 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1676 | static int synio(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1677 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1678 | struct ioword *iop; |
| 1679 | int i; |
| 1680 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1681 | |
| 1682 | DBGPRINTF7(("SYNIO: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1683 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1684 | c = yylex(cf); |
| 1685 | if (c != '<' && c != '>') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1686 | peeksym = c; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1687 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1688 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1689 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1690 | i = yylval.i; |
| 1691 | musthave(WORD, 0); |
| 1692 | iop = io(iounit, i, yylval.cp); |
| 1693 | iounit = IODEFAULT; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1694 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1695 | if (i & IOHERE) |
| 1696 | markhere(yylval.cp, iop); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1697 | |
| 1698 | DBGPRINTF7(("SYNIO: returning 1\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1699 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1702 | static void musthave(int c, int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1703 | { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1704 | peeksym = yylex(cf); |
| 1705 | if (peeksym != c) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1706 | DBGPRINTF7(("MUSTHAVE: error!\n")); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1707 | zzerr(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1710 | peeksym = 0; |
| 1711 | } |
| 1712 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1713 | static struct op *simple(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1714 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1715 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1716 | |
| 1717 | t = NULL; |
| 1718 | for (;;) { |
| 1719 | switch (peeksym = yylex(0)) { |
| 1720 | case '<': |
| 1721 | case '>': |
| 1722 | (void) synio(0); |
| 1723 | break; |
| 1724 | |
| 1725 | case WORD: |
| 1726 | if (t == NULL) { |
| 1727 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1728 | t->op_type = TCOM; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1729 | } |
| 1730 | peeksym = 0; |
| 1731 | word(yylval.cp); |
| 1732 | break; |
| 1733 | |
| 1734 | default: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1735 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1736 | } |
| 1737 | } |
| 1738 | } |
| 1739 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1740 | static struct op *nested(int type, int mark) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1741 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1742 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1743 | |
| 1744 | DBGPRINTF3(("NESTED: enter, type=%d, mark=%d\n", type, mark)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1745 | |
| 1746 | multiline++; |
| 1747 | t = c_list(); |
| 1748 | musthave(mark, 0); |
| 1749 | multiline--; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1750 | return block(type, t, NOBLOCK, NOWORDS); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1753 | static struct op *command(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1754 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1755 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1756 | struct wdblock *iosave; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1757 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1758 | |
| 1759 | DBGPRINTF(("COMMAND: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1760 | |
| 1761 | iosave = iolist; |
| 1762 | iolist = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1763 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1764 | if (multiline) |
| 1765 | cf |= CONTIN; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1766 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1767 | while (synio(cf)) |
| 1768 | cf = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1769 | |
| 1770 | c = yylex(cf); |
| 1771 | |
| 1772 | switch (c) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1773 | default: |
| 1774 | peeksym = c; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1775 | t = simple(); |
| 1776 | if (t == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1777 | if (iolist == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1778 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1779 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1780 | t->op_type = TCOM; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1781 | } |
| 1782 | break; |
| 1783 | |
| 1784 | case '(': |
| 1785 | t = nested(TPAREN, ')'); |
| 1786 | break; |
| 1787 | |
| 1788 | case '{': |
| 1789 | t = nested(TBRACE, '}'); |
| 1790 | break; |
| 1791 | |
| 1792 | case FOR: |
| 1793 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1794 | t->op_type = TFOR; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1795 | musthave(WORD, 0); |
| 1796 | startl = 1; |
| 1797 | t->str = yylval.cp; |
| 1798 | multiline++; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1799 | t->op_words = wordlist(); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1800 | c = yylex(0); |
| 1801 | if (c != '\n' && c != ';') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1802 | peeksym = c; |
| 1803 | t->left = dogroup(0); |
| 1804 | multiline--; |
| 1805 | break; |
| 1806 | |
| 1807 | case WHILE: |
| 1808 | case UNTIL: |
| 1809 | multiline++; |
| 1810 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1811 | t->op_type = (c == WHILE ? TWHILE : TUNTIL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1812 | t->left = c_list(); |
| 1813 | t->right = dogroup(1); |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1814 | /* t->op_words = NULL; - newtp() did this */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1815 | multiline--; |
| 1816 | break; |
| 1817 | |
| 1818 | case CASE: |
| 1819 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1820 | t->op_type = TCASE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1821 | musthave(WORD, 0); |
| 1822 | t->str = yylval.cp; |
| 1823 | startl++; |
| 1824 | multiline++; |
| 1825 | musthave(IN, CONTIN); |
| 1826 | startl++; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1827 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1828 | t->left = caselist(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1829 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1830 | musthave(ESAC, 0); |
| 1831 | multiline--; |
| 1832 | break; |
| 1833 | |
| 1834 | case IF: |
| 1835 | multiline++; |
| 1836 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1837 | t->op_type = TIF; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1838 | t->left = c_list(); |
| 1839 | t->right = thenpart(); |
| 1840 | musthave(FI, 0); |
| 1841 | multiline--; |
| 1842 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1843 | |
| 1844 | case DOT: |
| 1845 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1846 | t->op_type = TDOT; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1847 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1848 | musthave(WORD, 0); /* gets name of file */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1849 | DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp)); |
| 1850 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1851 | word(yylval.cp); /* add word to wdlist */ |
| 1852 | word(NOWORD); /* terminate wdlist */ |
| 1853 | t->op_words = copyw(); /* dup wdlist */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1854 | break; |
| 1855 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1856 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1857 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1858 | while (synio(0)) |
| 1859 | continue; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1860 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1861 | t = namelist(t); |
| 1862 | iolist = iosave; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1863 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1864 | DBGPRINTF(("COMMAND: returning %p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1865 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1866 | return t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 1869 | static struct op *dowholefile(int type /*, int mark*/) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1870 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1871 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1872 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 1873 | DBGPRINTF(("DOWHOLEFILE: enter, type=%d\n", type /*, mark*/)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1874 | |
| 1875 | multiline++; |
| 1876 | t = c_list(); |
| 1877 | multiline--; |
| 1878 | t = block(type, t, NOBLOCK, NOWORDS); |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1879 | DBGPRINTF(("DOWHOLEFILE: return t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1880 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1883 | static struct op *dogroup(int onlydone) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1884 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1885 | int c; |
| 1886 | struct op *mylist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1887 | |
| 1888 | c = yylex(CONTIN); |
| 1889 | if (c == DONE && onlydone) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1890 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1891 | if (c != DO) |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1892 | zzerr(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1893 | mylist = c_list(); |
| 1894 | musthave(DONE, 0); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1895 | return mylist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1898 | static struct op *thenpart(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1899 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1900 | int c; |
| 1901 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1902 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1903 | c = yylex(0); |
| 1904 | if (c != THEN) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1905 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1906 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1907 | } |
| 1908 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1909 | /*t->op_type = 0; - newtp() did this */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1910 | t->left = c_list(); |
| 1911 | if (t->left == NULL) |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1912 | zzerr(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1913 | t->right = elsepart(); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1914 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1917 | static struct op *elsepart(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1918 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1919 | int c; |
| 1920 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1921 | |
| 1922 | switch (c = yylex(0)) { |
| 1923 | case ELSE: |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1924 | t = c_list(); |
| 1925 | if (t == NULL) |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1926 | zzerr(); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1927 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1928 | |
| 1929 | case ELIF: |
| 1930 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1931 | t->op_type = TELIF; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1932 | t->left = c_list(); |
| 1933 | t->right = thenpart(); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1934 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1935 | |
| 1936 | default: |
| 1937 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1938 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1939 | } |
| 1940 | } |
| 1941 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1942 | static struct op *caselist(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1943 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1944 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1945 | |
| 1946 | t = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1947 | while ((peeksym = yylex(CONTIN)) != ESAC) { |
| 1948 | DBGPRINTF(("CASELIST, doing yylex, peeksym=%d\n", peeksym)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1949 | t = list(t, casepart()); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1952 | DBGPRINTF(("CASELIST, returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1953 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1956 | static struct op *casepart(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1957 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1958 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1959 | |
| 1960 | DBGPRINTF7(("CASEPART: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1961 | |
| 1962 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1963 | t->op_type = TPAT; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1964 | t->op_words = pattern(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1965 | musthave(')', 0); |
| 1966 | t->left = c_list(); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1967 | peeksym = yylex(CONTIN); |
| 1968 | if (peeksym != ESAC) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1969 | musthave(BREAK, CONTIN); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1970 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1971 | DBGPRINTF7(("CASEPART: made newtp(TPAT, t=%p)\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1972 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1973 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1976 | static char **pattern(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1977 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1978 | int c, cf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1979 | |
| 1980 | cf = CONTIN; |
| 1981 | do { |
| 1982 | musthave(WORD, cf); |
| 1983 | word(yylval.cp); |
| 1984 | cf = 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1985 | c = yylex(0); |
| 1986 | } while (c == '|'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1987 | peeksym = c; |
| 1988 | word(NOWORD); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1989 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1990 | return copyw(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1993 | static char **wordlist(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1994 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1995 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1996 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1997 | c = yylex(0); |
| 1998 | if (c != IN) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1999 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2000 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2001 | } |
| 2002 | startl = 0; |
| 2003 | while ((c = yylex(0)) == WORD) |
| 2004 | word(yylval.cp); |
| 2005 | word(NOWORD); |
| 2006 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2007 | return copyw(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | /* |
| 2011 | * supporting functions |
| 2012 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2013 | static struct op *list(struct op *t1, struct op *t2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2014 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2015 | DBGPRINTF7(("LIST: enter, t1=%p, t2=%p\n", t1, t2)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2016 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2017 | if (t1 == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2018 | return t2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2019 | if (t2 == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2020 | return t1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2021 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2022 | return block(TLIST, t1, t2, NOWORDS); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2025 | static struct op *block(int type, struct op *t1, struct op *t2, char **wp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2026 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2027 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2028 | |
| 2029 | DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type])); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2030 | |
| 2031 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2032 | t->op_type = type; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2033 | t->left = t1; |
| 2034 | t->right = t2; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2035 | t->op_words = wp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2036 | |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 2037 | DBGPRINTF7(("BLOCK: inserted %p between %p and %p\n", t, t1, t2)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2038 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2039 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2042 | /* See if given string is a shell multiline (FOR, IF, etc) */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2043 | static int rlookup(char *n) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2044 | { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2045 | struct res { |
| 2046 | char r_name[6]; |
| 2047 | int16_t r_val; |
| 2048 | }; |
| 2049 | static const struct res restab[] = { |
| 2050 | { "for" , FOR }, |
| 2051 | { "case" , CASE }, |
| 2052 | { "esac" , ESAC }, |
| 2053 | { "while", WHILE }, |
| 2054 | { "do" , DO }, |
| 2055 | { "done" , DONE }, |
| 2056 | { "if" , IF }, |
| 2057 | { "in" , IN }, |
| 2058 | { "then" , THEN }, |
| 2059 | { "else" , ELSE }, |
| 2060 | { "elif" , ELIF }, |
| 2061 | { "until", UNTIL }, |
| 2062 | { "fi" , FI }, |
| 2063 | { ";;" , BREAK }, |
| 2064 | { "||" , LOGOR }, |
| 2065 | { "&&" , LOGAND }, |
| 2066 | { "{" , '{' }, |
| 2067 | { "}" , '}' }, |
| 2068 | { "." , DOT }, |
| 2069 | { }, |
| 2070 | }; |
| 2071 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2072 | const struct res *rp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2073 | |
| 2074 | DBGPRINTF7(("RLOOKUP: enter, n is %s\n", n)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2075 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2076 | for (rp = restab; rp->r_name[0]; rp++) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2077 | if (strcmp(rp->r_name, n) == 0) { |
| 2078 | DBGPRINTF7(("RLOOKUP: match, returning %d\n", rp->r_val)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2079 | return rp->r_val; /* Return numeric code for shell multiline */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | DBGPRINTF7(("RLOOKUP: NO match, returning 0\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2083 | return 0; /* Not a shell multiline */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2086 | static struct op *newtp(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2087 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2088 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2089 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2090 | t = (struct op *) tree(sizeof(*t)); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 2091 | memset(t, 0, sizeof(*t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2092 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2093 | DBGPRINTF3(("NEWTP: allocated %p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2094 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2095 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2098 | static struct op *namelist(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2099 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2100 | DBGPRINTF7(("NAMELIST: enter, t=%p, type %s, iolist=%p\n", t, |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2101 | T_CMD_NAMES[t->op_type], iolist)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2102 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2103 | if (iolist) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2104 | iolist = addword((char *) NULL, iolist); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2105 | t->ioact = copyio(); |
| 2106 | } else |
| 2107 | t->ioact = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2108 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2109 | if (t->op_type != TCOM) { |
| 2110 | if (t->op_type != TPAREN && t->ioact != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2111 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 2112 | t->ioact = t->left->ioact; |
| 2113 | t->left->ioact = NULL; |
| 2114 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2115 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2116 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2117 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2118 | word(NOWORD); |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2119 | t->op_words = copyw(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2120 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2121 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2124 | static char **copyw(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2125 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2126 | char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2127 | |
| 2128 | wd = getwords(wdlist); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2129 | wdlist = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2130 | return wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2133 | static void word(char *cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2134 | { |
| 2135 | wdlist = addword(cp, wdlist); |
| 2136 | } |
| 2137 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2138 | static struct ioword **copyio(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2139 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2140 | struct ioword **iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2141 | |
| 2142 | iop = (struct ioword **) getwords(iolist); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2143 | iolist = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2144 | return iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2147 | static struct ioword *io(int u, int f, char *cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2148 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2149 | struct ioword *iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2150 | |
| 2151 | iop = (struct ioword *) tree(sizeof(*iop)); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2152 | iop->io_fd = u; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2153 | iop->io_flag = f; |
| 2154 | iop->io_name = cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2155 | iolist = addword((char *) iop, iolist); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2156 | return iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2159 | static int yylex(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2160 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2161 | int c, c1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2162 | int atstart; |
| 2163 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2164 | c = peeksym; |
| 2165 | if (c > 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2166 | peeksym = 0; |
| 2167 | if (c == '\n') |
| 2168 | startl = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2169 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2170 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2171 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2172 | nlseen = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2173 | atstart = startl; |
| 2174 | startl = 0; |
| 2175 | yylval.i = 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2176 | global_env.linep = line; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2177 | |
| 2178 | /* MALAMO */ |
| 2179 | line[LINELIM - 1] = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2180 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2181 | loop: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2182 | while ((c = my_getc(0)) == ' ' || c == '\t') /* Skip whitespace */ |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2183 | continue; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2184 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2185 | switch (c) { |
| 2186 | default: |
| 2187 | if (any(c, "0123456789")) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2188 | c1 = my_getc(0); |
| 2189 | unget(c1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2190 | if (c1 == '<' || c1 == '>') { |
| 2191 | iounit = c - '0'; |
| 2192 | goto loop; |
| 2193 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2194 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2195 | c = c1; |
| 2196 | } |
| 2197 | break; |
| 2198 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2199 | case '#': /* Comment, skip to next newline or End-of-string */ |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2200 | while ((c = my_getc(0)) != '\0' && c != '\n') |
| 2201 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2202 | unget(c); |
| 2203 | goto loop; |
| 2204 | |
| 2205 | case 0: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2206 | DBGPRINTF5(("YYLEX: return 0, c=%d\n", c)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2207 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2208 | |
| 2209 | case '$': |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2210 | DBGPRINTF9(("YYLEX: found $\n")); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2211 | *global_env.linep++ = c; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2212 | c = my_getc(0); |
| 2213 | if (c == '{') { |
| 2214 | c = collect(c, '}'); |
| 2215 | if (c != '\0') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2216 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2217 | goto pack; |
| 2218 | } |
| 2219 | break; |
| 2220 | |
| 2221 | case '`': |
| 2222 | case '\'': |
| 2223 | case '"': |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2224 | c = collect(c, c); |
| 2225 | if (c != '\0') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2226 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2227 | goto pack; |
| 2228 | |
| 2229 | case '|': |
| 2230 | case '&': |
| 2231 | case ';': |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2232 | startl = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2233 | /* If more chars process them, else return NULL char */ |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2234 | c1 = dual(c); |
| 2235 | if (c1 != '\0') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2236 | return c1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2237 | return c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2238 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2239 | case '^': |
| 2240 | startl = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2241 | return '|'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2242 | case '>': |
| 2243 | case '<': |
| 2244 | diag(c); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2245 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2246 | |
| 2247 | case '\n': |
| 2248 | nlseen++; |
| 2249 | gethere(); |
| 2250 | startl = 1; |
| 2251 | if (multiline || cf & CONTIN) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2252 | if (interactive && global_env.iop <= iostack) { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 2253 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2254 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2255 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2256 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2257 | #endif |
| 2258 | } |
| 2259 | if (cf & CONTIN) |
| 2260 | goto loop; |
| 2261 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2262 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2263 | |
| 2264 | case '(': |
| 2265 | case ')': |
| 2266 | startl = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2267 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | unget(c); |
| 2271 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2272 | pack: |
| 2273 | while ((c = my_getc(0)) != '\0' && !any(c, "`$ '\"\t;&<>()|^\n")) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2274 | if (global_env.linep >= elinep) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2275 | err("word too long"); |
| 2276 | else |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2277 | *global_env.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2278 | }; |
| 2279 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2280 | unget(c); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2281 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2282 | if (any(c, "\"'`$")) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2283 | goto loop; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2284 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2285 | *global_env.linep++ = '\0'; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2286 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2287 | if (atstart) { |
| 2288 | c = rlookup(line); |
| 2289 | if (c != 0) { |
| 2290 | startl = 1; |
| 2291 | return c; |
| 2292 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2293 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2294 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2295 | yylval.cp = strsave(line, areanum); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2296 | return WORD; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2299 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2300 | static int collect(int c, int c1) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2301 | { |
| 2302 | char s[2]; |
| 2303 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2304 | DBGPRINTF8(("COLLECT: enter, c=%d, c1=%d\n", c, c1)); |
| 2305 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2306 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2307 | while ((c = my_getc(c1)) != c1) { |
| 2308 | if (c == 0) { |
| 2309 | unget(c); |
| 2310 | s[0] = c1; |
| 2311 | s[1] = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2312 | prs("no closing "); |
| 2313 | yyerror(s); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2314 | return YYERRCODE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2315 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2316 | if (interactive && c == '\n' && global_env.iop <= iostack) { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 2317 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2318 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2319 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2320 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2321 | #endif |
| 2322 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2323 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2324 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2325 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2326 | *global_env.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2327 | |
| 2328 | DBGPRINTF8(("COLLECT: return 0, line is %s\n", line)); |
| 2329 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2330 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2333 | /* "multiline commands" helper func */ |
| 2334 | /* see if next 2 chars form a shell multiline */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2335 | static int dual(int c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2336 | { |
| 2337 | char s[3]; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2338 | char *cp = s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2339 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2340 | DBGPRINTF8(("DUAL: enter, c=%d\n", c)); |
| 2341 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2342 | *cp++ = c; /* c is the given "peek" char */ |
| 2343 | *cp++ = my_getc(0); /* get next char of input */ |
| 2344 | *cp = '\0'; /* add EOS marker */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2345 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2346 | c = rlookup(s); /* see if 2 chars form a shell multiline */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2347 | if (c == 0) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2348 | unget(*--cp); /* String is not a shell multiline, put peek char back */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2349 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2350 | return c; /* String is multiline, return numeric multiline (restab) code */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2353 | static void diag(int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2354 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2355 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2356 | |
| 2357 | DBGPRINTF8(("DIAG: enter, ec=%d\n", ec)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2358 | |
| 2359 | c = my_getc(0); |
| 2360 | if (c == '>' || c == '<') { |
| 2361 | if (c != ec) |
| 2362 | zzerr(); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2363 | yylval.i = (ec == '>' ? IOWRITE | IOCAT : IOHERE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2364 | c = my_getc(0); |
| 2365 | } else |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2366 | yylval.i = (ec == '>' ? IOWRITE : IOREAD); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2367 | if (c != '&' || yylval.i == IOHERE) |
| 2368 | unget(c); |
| 2369 | else |
| 2370 | yylval.i |= IODUP; |
| 2371 | } |
| 2372 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2373 | static char *tree(unsigned size) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2374 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2375 | char *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2376 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2377 | t = getcell(size); |
| 2378 | if (t == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2379 | DBGPRINTF2(("TREE: getcell(%d) failed!\n", size)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2380 | prs("command line too complicated\n"); |
| 2381 | fail(); |
| 2382 | /* NOTREACHED */ |
| 2383 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2384 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2387 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2388 | /* VARARGS1 */ |
| 2389 | /* ARGSUSED */ |
| 2390 | |
| 2391 | /* -------- exec.c -------- */ |
| 2392 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2393 | static struct op **find1case(struct op *t, const char *w) |
| 2394 | { |
| 2395 | struct op *t1; |
| 2396 | struct op **tp; |
| 2397 | char **wp; |
| 2398 | char *cp; |
| 2399 | |
| 2400 | if (t == NULL) { |
| 2401 | DBGPRINTF3(("FIND1CASE: enter, t==NULL, returning.\n")); |
| 2402 | return NULL; |
| 2403 | } |
| 2404 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2405 | DBGPRINTF3(("FIND1CASE: enter, t->op_type=%d (%s)\n", t->op_type, |
| 2406 | T_CMD_NAMES[t->op_type])); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2407 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2408 | if (t->op_type == TLIST) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2409 | tp = find1case(t->left, w); |
| 2410 | if (tp != NULL) { |
| 2411 | DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=%p\n", tp)); |
| 2412 | return tp; |
| 2413 | } |
| 2414 | t1 = t->right; /* TPAT */ |
| 2415 | } else |
| 2416 | t1 = t; |
| 2417 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2418 | for (wp = t1->op_words; *wp;) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2419 | cp = evalstr(*wp++, DOSUB); |
| 2420 | if (cp && gmatch(w, cp)) { |
| 2421 | DBGPRINTF3(("FIND1CASE: returning &t1->left= %p.\n", |
| 2422 | &t1->left)); |
| 2423 | return &t1->left; |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | DBGPRINTF(("FIND1CASE: returning NULL\n")); |
| 2428 | return NULL; |
| 2429 | } |
| 2430 | |
| 2431 | static struct op *findcase(struct op *t, const char *w) |
| 2432 | { |
| 2433 | struct op **tp; |
| 2434 | |
| 2435 | tp = find1case(t, w); |
| 2436 | return tp != NULL ? *tp : NULL; |
| 2437 | } |
| 2438 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2439 | /* |
| 2440 | * execute tree |
| 2441 | */ |
| 2442 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2443 | static int execute(struct op *t, int *pin, int *pout, int no_fork) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2444 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2445 | struct op *t1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2446 | volatile int i, rv, a; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2447 | const char *cp; |
| 2448 | char **wp, **wp2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2449 | struct var *vp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2450 | struct op *outtree_save; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2451 | struct brkcon bc; |
| 2452 | |
| 2453 | #if __GNUC__ |
| 2454 | /* Avoid longjmp clobbering */ |
| 2455 | (void) ℘ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2456 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2457 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2458 | if (t == NULL) { |
| 2459 | DBGPRINTF4(("EXECUTE: enter, t==null, returning.\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2460 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2463 | DBGPRINTF(("EXECUTE: t=%p, t->op_type=%d (%s), t->op_words is %s\n", t, |
| 2464 | t->op_type, T_CMD_NAMES[t->op_type], |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2465 | ((t->op_words == NULL) ? "NULL" : t->op_words[0]))); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2466 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2467 | rv = 0; |
| 2468 | a = areanum++; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2469 | wp2 = t->op_words; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 2470 | wp = (wp2 != NULL) |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2471 | ? eval(wp2, t->op_type == TCOM ? DOALL : DOALL & ~DOKEY) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2472 | : NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2473 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2474 | switch (t->op_type) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2475 | case TDOT: |
| 2476 | DBGPRINTF3(("EXECUTE: TDOT\n")); |
| 2477 | |
| 2478 | outtree_save = outtree; |
| 2479 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2480 | newfile(evalstr(t->op_words[0], DOALL)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2481 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 2482 | t->left = dowholefile(TLIST /*, 0*/); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2483 | t->right = NULL; |
| 2484 | |
| 2485 | outtree = outtree_save; |
| 2486 | |
| 2487 | if (t->left) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2488 | rv = execute(t->left, pin, pout, /* no_fork: */ 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2489 | if (t->right) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2490 | rv = execute(t->right, pin, pout, /* no_fork: */ 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2491 | break; |
| 2492 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2493 | case TPAREN: |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2494 | rv = execute(t->left, pin, pout, /* no_fork: */ 0); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 2495 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2496 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2497 | case TCOM: |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2498 | rv = forkexec(t, pin, pout, no_fork, wp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2499 | break; |
| 2500 | |
| 2501 | case TPIPE: |
| 2502 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2503 | int pv[2]; |
| 2504 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2505 | rv = openpipe(pv); |
| 2506 | if (rv < 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2507 | break; |
| 2508 | pv[0] = remap(pv[0]); |
| 2509 | pv[1] = remap(pv[1]); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2510 | (void) execute(t->left, pin, pv, /* no_fork: */ 0); |
| 2511 | rv = execute(t->right, pv, pout, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2512 | } |
| 2513 | break; |
| 2514 | |
| 2515 | case TLIST: |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2516 | (void) execute(t->left, pin, pout, /* no_fork: */ 0); |
| 2517 | rv = execute(t->right, pin, pout, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2518 | break; |
| 2519 | |
| 2520 | case TASYNC: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2521 | { |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 2522 | smallint hinteractive = interactive; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2523 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2524 | DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n")); |
| 2525 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2526 | i = vfork(); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2527 | if (i == 0) { /* child */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2528 | signal(SIGINT, SIG_IGN); |
| 2529 | signal(SIGQUIT, SIG_IGN); |
| 2530 | if (interactive) |
| 2531 | signal(SIGTERM, SIG_DFL); |
| 2532 | interactive = 0; |
| 2533 | if (pin == NULL) { |
| 2534 | close(0); |
Denis Vlasenko | 2b54aaa | 2007-05-09 22:16:08 +0000 | [diff] [blame] | 2535 | xopen(bb_dev_null, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2536 | } |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2537 | _exit(execute(t->left, pin, pout, /* no_fork: */ 1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2538 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2539 | interactive = hinteractive; |
| 2540 | if (i != -1) { |
| 2541 | setval(lookup("!"), putn(i)); |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2542 | closepipe(pin); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2543 | if (interactive) { |
| 2544 | prs(putn(i)); |
| 2545 | prs("\n"); |
| 2546 | } |
| 2547 | } else |
| 2548 | rv = -1; |
| 2549 | setstatus(rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2550 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2551 | break; |
| 2552 | |
| 2553 | case TOR: |
| 2554 | case TAND: |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2555 | rv = execute(t->left, pin, pout, /* no_fork: */ 0); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2556 | t1 = t->right; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2557 | if (t1 != NULL && (rv == 0) == (t->op_type == TAND)) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2558 | rv = execute(t1, pin, pout, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2559 | break; |
| 2560 | |
| 2561 | case TFOR: |
| 2562 | if (wp == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2563 | wp = dolv + 1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2564 | i = dolc; |
| 2565 | if (i < 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2566 | i = 0; |
| 2567 | } else { |
| 2568 | i = -1; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2569 | while (*wp++ != NULL) |
| 2570 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2571 | } |
| 2572 | vp = lookup(t->str); |
| 2573 | while (setjmp(bc.brkpt)) |
| 2574 | if (isbreak) |
| 2575 | goto broken; |
| 2576 | brkset(&bc); |
| 2577 | for (t1 = t->left; i-- && *wp != NULL;) { |
| 2578 | setval(vp, *wp++); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2579 | rv = execute(t1, pin, pout, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2580 | } |
| 2581 | brklist = brklist->nextlev; |
| 2582 | break; |
| 2583 | |
| 2584 | case TWHILE: |
| 2585 | case TUNTIL: |
| 2586 | while (setjmp(bc.brkpt)) |
| 2587 | if (isbreak) |
| 2588 | goto broken; |
| 2589 | brkset(&bc); |
| 2590 | t1 = t->left; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2591 | while ((execute(t1, pin, pout, /* no_fork: */ 0) == 0) == (t->op_type == TWHILE)) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2592 | rv = execute(t->right, pin, pout, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2593 | brklist = brklist->nextlev; |
| 2594 | break; |
| 2595 | |
| 2596 | case TIF: |
| 2597 | case TELIF: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2598 | if (t->right != NULL) { |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2599 | rv = !execute(t->left, pin, pout, /* no_fork: */ 0) ? |
| 2600 | execute(t->right->left, pin, pout, /* no_fork: */ 0) : |
| 2601 | execute(t->right->right, pin, pout, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2602 | } |
| 2603 | break; |
| 2604 | |
| 2605 | case TCASE: |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2606 | cp = evalstr(t->str, DOSUB | DOTRIM); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2607 | if (cp == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2608 | cp = ""; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2609 | |
| 2610 | DBGPRINTF7(("EXECUTE: TCASE, t->str is %s, cp is %s\n", |
| 2611 | ((t->str == NULL) ? "NULL" : t->str), |
| 2612 | ((cp == NULL) ? "NULL" : cp))); |
| 2613 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2614 | t1 = findcase(t->left, cp); |
| 2615 | if (t1 != NULL) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2616 | DBGPRINTF7(("EXECUTE: TCASE, calling execute(t=%p, t1=%p)...\n", t, t1)); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2617 | rv = execute(t1, pin, pout, /* no_fork: */ 0); |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2618 | DBGPRINTF7(("EXECUTE: TCASE, back from execute(t=%p, t1=%p)...\n", t, t1)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2619 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2620 | break; |
| 2621 | |
| 2622 | case TBRACE: |
| 2623 | /* |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2624 | iopp = t->ioact; |
| 2625 | if (i) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2626 | while (*iopp) |
| 2627 | if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) { |
| 2628 | rv = -1; |
| 2629 | break; |
| 2630 | } |
| 2631 | */ |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2632 | if (rv >= 0) { |
| 2633 | t1 = t->left; |
| 2634 | if (t1) { |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2635 | rv = execute(t1, pin, pout, /* no_fork: */ 0); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2636 | } |
| 2637 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2638 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2639 | |
| 2640 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2641 | |
Denis Vlasenko | e471275 | 2007-04-14 15:08:41 +0000 | [diff] [blame] | 2642 | broken: |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2643 | // Restoring op_words is most likely not needed now: see comment in forkexec() |
| 2644 | // (also take a look at exec builtin (doexec) - it touches t->op_words) |
| 2645 | t->op_words = wp2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2646 | isbreak = 0; |
| 2647 | freehere(areanum); |
| 2648 | freearea(areanum); |
| 2649 | areanum = a; |
| 2650 | if (interactive && intr) { |
| 2651 | closeall(); |
| 2652 | fail(); |
| 2653 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2654 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2655 | i = trapset; |
| 2656 | if (i != 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2657 | trapset = 0; |
| 2658 | runtrap(i); |
| 2659 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2660 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2661 | DBGPRINTF(("EXECUTE: returning from t=%p, rv=%d\n", t, rv)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2662 | return rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
Denis Vlasenko | e471275 | 2007-04-14 15:08:41 +0000 | [diff] [blame] | 2665 | static builtin_func_ptr inbuilt(const char *s) |
| 2666 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2667 | const struct builtincmd *bp; |
| 2668 | |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 2669 | for (bp = builtincmds; bp->name; bp++) |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2670 | if (strcmp(bp->name, s) == 0) |
| 2671 | return bp->builtinfunc; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2672 | return NULL; |
| 2673 | } |
| 2674 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2675 | static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2676 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2677 | pid_t newpid; |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2678 | int i; |
| 2679 | builtin_func_ptr bltin = NULL; |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2680 | const char *bltin_name = NULL; |
| 2681 | const char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2682 | struct ioword **iopp; |
| 2683 | int resetsig; |
| 2684 | char **owp; |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2685 | int forked; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2686 | |
| 2687 | int *hpin = pin; |
| 2688 | int *hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2689 | char *hwp; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 2690 | smallint hinteractive; |
| 2691 | smallint hintr; |
| 2692 | smallint hexecflg; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2693 | struct brkcon *hbrklist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2694 | |
| 2695 | #if __GNUC__ |
| 2696 | /* Avoid longjmp clobbering */ |
| 2697 | (void) &pin; |
| 2698 | (void) &pout; |
| 2699 | (void) ℘ |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2700 | (void) &bltin; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2701 | (void) &cp; |
| 2702 | (void) &resetsig; |
| 2703 | (void) &owp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2704 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2705 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2706 | DBGPRINTF(("FORKEXEC: t=%p, pin %p, pout %p, no_fork %d\n", t, pin, |
| 2707 | pout, no_fork)); |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2708 | DBGPRINTF7(("FORKEXEC: t->op_words is %s\n", |
| 2709 | ((t->op_words == NULL) ? "NULL" : t->op_words[0]))); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2710 | owp = wp; |
| 2711 | resetsig = 0; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2712 | if (t->op_type == TCOM) { |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 2713 | while (*wp++ != NULL) |
| 2714 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2715 | cp = *wp; |
| 2716 | |
| 2717 | /* strip all initial assignments */ |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2718 | /* FIXME: not correct wrt PATH=yyy command etc */ |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 2719 | if (FLAG['x']) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2720 | DBGPRINTF9(("FORKEXEC: echo'ing, cp=%p, wp=%p, owp=%p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2721 | cp, wp, owp)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2722 | echo(cp ? wp : owp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2723 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2724 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2725 | if (cp == NULL) { |
| 2726 | if (t->ioact == NULL) { |
| 2727 | while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
| 2728 | continue; |
| 2729 | DBGPRINTF(("FORKEXEC: returning setstatus(0)\n")); |
| 2730 | return setstatus(0); |
| 2731 | } |
| 2732 | } else { /* cp != NULL */ |
| 2733 | bltin_name = cp; |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2734 | bltin = inbuilt(cp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2735 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2736 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2737 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2738 | forked = 0; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2739 | // We were pointing t->op_words to temporary (expanded) arg list: |
| 2740 | // t->op_words = wp; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 2741 | // and restored it later (in execute()), but "break" |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2742 | // longjmps away (at "Run builtin" below), leaving t->op_words clobbered! |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 2743 | // See http://bugs.busybox.net/view.php?id=846. |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2744 | // Now we do not touch t->op_words, but separately pass wp as param list |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 2745 | // to builtins |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2746 | DBGPRINTF(("FORKEXEC: bltin %p, no_fork %d, owp %p\n", bltin, |
| 2747 | no_fork, owp)); |
| 2748 | /* Don't fork if it is a lone builtin (not in pipe) |
| 2749 | * OR we are told to _not_ fork */ |
| 2750 | if ((!bltin || pin || pout) /* not lone bltin AND */ |
| 2751 | && !no_fork /* not told to avoid fork */ |
| 2752 | ) { |
| 2753 | /* Save values in case child alters them after vfork */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2754 | hpin = pin; |
| 2755 | hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2756 | hwp = *wp; |
| 2757 | hinteractive = interactive; |
| 2758 | hintr = intr; |
| 2759 | hbrklist = brklist; |
| 2760 | hexecflg = execflg; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2761 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2762 | DBGPRINTF3(("FORKEXEC: calling vfork()...\n")); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2763 | newpid = vfork(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2764 | if (newpid == -1) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 2765 | DBGPRINTF(("FORKEXEC: ERROR, cannot vfork()!\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2766 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 2769 | if (newpid > 0) { /* Parent */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2770 | /* Restore values */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2771 | pin = hpin; |
| 2772 | pout = hpout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2773 | *wp = hwp; |
| 2774 | interactive = hinteractive; |
| 2775 | intr = hintr; |
| 2776 | brklist = hbrklist; |
| 2777 | execflg = hexecflg; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2778 | |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2779 | closepipe(pin); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2780 | return (pout == NULL ? setstatus(waitfor(newpid, 0)) : 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2783 | /* Child */ |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2784 | DBGPRINTF(("FORKEXEC: child process, bltin=%p (%s)\n", bltin, bltin_name)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2785 | if (interactive) { |
| 2786 | signal(SIGINT, SIG_IGN); |
| 2787 | signal(SIGQUIT, SIG_IGN); |
| 2788 | resetsig = 1; |
| 2789 | } |
| 2790 | interactive = 0; |
| 2791 | intr = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2792 | forked = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2793 | brklist = 0; |
| 2794 | execflg = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2795 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2796 | |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2797 | if (owp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2798 | while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2799 | if (!bltin) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2800 | export(lookup(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2801 | |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 2802 | if (pin) { /* NB: close _first_, then move fds! */ |
| 2803 | close(pin[1]); |
Denis Vlasenko | 2b54aaa | 2007-05-09 22:16:08 +0000 | [diff] [blame] | 2804 | xmove_fd(pin[0], 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2805 | } |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2806 | if (pout) { |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 2807 | close(pout[0]); |
Denis Vlasenko | 2b54aaa | 2007-05-09 22:16:08 +0000 | [diff] [blame] | 2808 | xmove_fd(pout[1], 1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2809 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2810 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2811 | iopp = t->ioact; |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2812 | if (iopp) { |
| 2813 | if (bltin && bltin != doexec) { |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2814 | prs(bltin_name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2815 | err(": cannot redirect shell command"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2816 | if (forked) |
| 2817 | _exit(-1); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2818 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2819 | } |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2820 | while (*iopp) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2821 | if (iosetup(*iopp++, pin != NULL, pout != NULL)) { |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2822 | /* system-detected error */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2823 | if (forked) |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2824 | _exit(-1); |
| 2825 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2826 | } |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2827 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2828 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2829 | |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2830 | if (bltin) { |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2831 | if (forked || pin || pout) { |
| 2832 | /* Builtin in pipe: disallowed */ |
| 2833 | /* TODO: allow "exec"? */ |
| 2834 | prs(bltin_name); |
| 2835 | err(": cannot run builtin as part of pipe"); |
| 2836 | if (forked) |
| 2837 | _exit(-1); |
| 2838 | return -1; |
| 2839 | } |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 2840 | /* Run builtin */ |
| 2841 | i = setstatus(bltin(t, wp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2842 | if (forked) |
| 2843 | _exit(i); |
| 2844 | DBGPRINTF(("FORKEXEC: returning i=%d\n", i)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2845 | return i; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2848 | /* should use FIOCEXCL */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2849 | for (i = FDBASE; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2850 | close(i); |
| 2851 | if (resetsig) { |
| 2852 | signal(SIGINT, SIG_DFL); |
| 2853 | signal(SIGQUIT, SIG_DFL); |
| 2854 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2855 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2856 | if (t->op_type == TPAREN) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2857 | _exit(execute(t->left, NOPIPE, NOPIPE, /* no_fork: */ 1)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2858 | if (wp[0] == NULL) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 2859 | _exit(EXIT_SUCCESS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2860 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 2861 | cp = rexecve(wp[0], wp, makenv(0, NULL)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2862 | prs(wp[0]); |
| 2863 | prs(": "); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2864 | err(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2865 | if (!execflg) |
| 2866 | trap[0] = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2867 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2868 | DBGPRINTF(("FORKEXEC: calling leave(), pid=%d\n", getpid())); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2869 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2870 | leave(); |
| 2871 | /* NOTREACHED */ |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2872 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | /* |
| 2876 | * 0< 1> are ignored as required |
| 2877 | * within pipelines. |
| 2878 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2879 | static int iosetup(struct ioword *iop, int pipein, int pipeout) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2880 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2881 | int u = -1; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2882 | char *cp = NULL; |
| 2883 | const char *msg; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2884 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2885 | DBGPRINTF(("IOSETUP: iop %p, pipein %i, pipeout %i\n", iop, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2886 | pipein, pipeout)); |
| 2887 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2888 | if (iop->io_fd == IODEFAULT) /* take default */ |
| 2889 | iop->io_fd = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2890 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2891 | if (pipein && iop->io_fd == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2892 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2893 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2894 | if (pipeout && iop->io_fd == 1) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2895 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2896 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2897 | msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2898 | if ((iop->io_flag & IOHERE) == 0) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2899 | cp = iop->io_name; /* huh?? */ |
| 2900 | cp = evalstr(cp, DOSUB | DOTRIM); |
| 2901 | if (cp == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2902 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2903 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2904 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2905 | if (iop->io_flag & IODUP) { |
| 2906 | if (cp[1] || (!isdigit(*cp) && *cp != '-')) { |
| 2907 | prs(cp); |
| 2908 | err(": illegal >& argument"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2909 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2910 | } |
| 2911 | if (*cp == '-') |
| 2912 | iop->io_flag = IOCLOSE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2913 | iop->io_flag &= ~(IOREAD | IOWRITE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2914 | } |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2915 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2916 | switch (iop->io_flag) { |
| 2917 | case IOREAD: |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 2918 | u = open(cp, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2919 | break; |
| 2920 | |
| 2921 | case IOHERE: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2922 | case IOHERE | IOXHERE: |
| 2923 | u = herein(iop->io_name, iop->io_flag & IOXHERE); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2924 | cp = (char*)"here file"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2925 | break; |
| 2926 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2927 | case IOWRITE | IOCAT: |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 2928 | u = open(cp, O_WRONLY); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2929 | if (u >= 0) { |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 2930 | lseek(u, (long) 0, SEEK_END); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2931 | break; |
| 2932 | } |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2933 | /* fall through to creation if >>file doesn't exist */ |
| 2934 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2935 | case IOWRITE: |
| 2936 | u = creat(cp, 0666); |
| 2937 | break; |
| 2938 | |
| 2939 | case IODUP: |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2940 | u = dup2(*cp - '0', iop->io_fd); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2941 | break; |
| 2942 | |
| 2943 | case IOCLOSE: |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2944 | close(iop->io_fd); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2945 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2946 | } |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2947 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2948 | if (u < 0) { |
| 2949 | prs(cp); |
| 2950 | prs(": cannot "); |
| 2951 | warn(msg); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2952 | return 1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2953 | } |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2954 | xmove_fd(u, iop->io_fd); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2955 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2958 | /* |
| 2959 | * Enter a new loop level (marked for break/continue). |
| 2960 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2961 | static void brkset(struct brkcon *bc) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2962 | { |
| 2963 | bc->nextlev = brklist; |
| 2964 | brklist = bc; |
| 2965 | } |
| 2966 | |
| 2967 | /* |
| 2968 | * Wait for the last process created. |
| 2969 | * Print a message for each process found |
| 2970 | * that was killed by a signal. |
| 2971 | * Ignore interrupt signals while waiting |
| 2972 | * unless `canintr' is true. |
| 2973 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2974 | static int waitfor(int lastpid, int canintr) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2975 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2976 | int pid, rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2977 | int s; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 2978 | smallint oheedint = heedint; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2979 | |
| 2980 | heedint = 0; |
| 2981 | rv = 0; |
| 2982 | do { |
| 2983 | pid = wait(&s); |
| 2984 | if (pid == -1) { |
| 2985 | if (errno != EINTR || canintr) |
| 2986 | break; |
| 2987 | } else { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2988 | rv = WAITSIG(s); |
| 2989 | if (rv != 0) { |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2990 | if (rv < ARRAY_SIZE(signame)) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2991 | if (signame[rv] != NULL) { |
| 2992 | if (pid != lastpid) { |
| 2993 | prn(pid); |
| 2994 | prs(": "); |
| 2995 | } |
| 2996 | prs(signame[rv]); |
| 2997 | } |
| 2998 | } else { |
| 2999 | if (pid != lastpid) { |
| 3000 | prn(pid); |
| 3001 | prs(": "); |
| 3002 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3003 | prs("Signal "); |
| 3004 | prn(rv); |
| 3005 | prs(" "); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3006 | } |
| 3007 | if (WAITCORE(s)) |
| 3008 | prs(" - core dumped"); |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 3009 | if (rv >= ARRAY_SIZE(signame) || signame[rv]) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3010 | prs("\n"); |
Mike Frysinger | b81f97b | 2008-05-14 11:51:04 +0000 | [diff] [blame] | 3011 | rv |= 0x80; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3012 | } else |
| 3013 | rv = WAITVAL(s); |
| 3014 | } |
| 3015 | } while (pid != lastpid); |
| 3016 | heedint = oheedint; |
| 3017 | if (intr) { |
| 3018 | if (interactive) { |
| 3019 | if (canintr) |
| 3020 | intr = 0; |
| 3021 | } else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3022 | if (exstat == 0) |
| 3023 | exstat = rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3024 | onintr(0); |
| 3025 | } |
| 3026 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3027 | return rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3030 | static int setstatus(int s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3031 | { |
| 3032 | exstat = s; |
| 3033 | setval(lookup("?"), putn(s)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3034 | return s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | /* |
| 3038 | * PATH-searching interface to execve. |
| 3039 | * If getenv("PATH") were kept up-to-date, |
| 3040 | * execvp might be used. |
| 3041 | */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3042 | static const char *rexecve(char *c, char **v, char **envp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3043 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3044 | const char *sp; |
| 3045 | char *tp; |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 3046 | int asis = 0; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3047 | char *name = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3048 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3049 | if (ENABLE_FEATURE_SH_STANDALONE) { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3050 | if (find_applet_by_name(name) >= 0) { |
Rob Landley | a299efb | 2006-08-10 21:46:43 +0000 | [diff] [blame] | 3051 | /* We have to exec here since we vforked. Running |
Denis Vlasenko | e4f2d06 | 2007-04-11 17:03:19 +0000 | [diff] [blame] | 3052 | * run_applet_and_exit() won't work and bad things |
Rob Landley | a299efb | 2006-08-10 21:46:43 +0000 | [diff] [blame] | 3053 | * will happen. */ |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 3054 | execve(bb_busybox_exec_path, v, envp); |
Rob Landley | a299efb | 2006-08-10 21:46:43 +0000 | [diff] [blame] | 3055 | } |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3056 | } |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3057 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3058 | DBGPRINTF(("REXECVE: c=%p, v=%p, envp=%p\n", c, v, envp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3059 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3060 | sp = any('/', c) ? "" : path->value; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3061 | asis = (*sp == '\0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3062 | while (asis || *sp != '\0') { |
| 3063 | asis = 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3064 | tp = global_env.linep; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3065 | for (; *sp != '\0'; tp++) { |
| 3066 | *tp = *sp++; |
| 3067 | if (*tp == ':') { |
| 3068 | asis = (*sp == '\0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3069 | break; |
| 3070 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3071 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3072 | if (tp != global_env.linep) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3073 | *tp++ = '/'; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3074 | strcpy(tp, c); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3075 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3076 | DBGPRINTF3(("REXECVE: global_env.linep is %s\n", global_env.linep)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3077 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3078 | execve(global_env.linep, v, envp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3079 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3080 | switch (errno) { |
| 3081 | case ENOEXEC: |
Denis Vlasenko | 447bd66 | 2008-05-30 22:28:32 +0000 | [diff] [blame] | 3082 | /* File is executable but file format isnt recognized */ |
| 3083 | /* Run it as a shell script */ |
| 3084 | /* (execve above didnt do it itself, unlike execvp) */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3085 | *v = global_env.linep; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3086 | v--; |
| 3087 | tp = *v; |
Denis Vlasenko | 447bd66 | 2008-05-30 22:28:32 +0000 | [diff] [blame] | 3088 | *v = (char*)DEFAULT_SHELL; |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 3089 | execve(DEFAULT_SHELL, v, envp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3090 | *v = tp; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3091 | return "no shell"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3092 | |
| 3093 | case ENOMEM: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3094 | return (char *) bb_msg_memory_exhausted; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3095 | |
| 3096 | case E2BIG: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3097 | return "argument list too long"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3098 | } |
| 3099 | } |
Denis Vlasenko | 447bd66 | 2008-05-30 22:28:32 +0000 | [diff] [blame] | 3100 | if (errno == ENOENT) { |
| 3101 | exstat = 127; /* standards require this */ |
| 3102 | return "not found"; |
| 3103 | } |
| 3104 | exstat = 126; /* mimic bash */ |
| 3105 | return "cannot execute"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | /* |
| 3109 | * Run the command produced by generator `f' |
| 3110 | * applied to stream `arg'. |
| 3111 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3112 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3113 | { |
| 3114 | struct op *otree; |
| 3115 | struct wdblock *swdlist; |
| 3116 | struct wdblock *siolist; |
| 3117 | jmp_buf ev, rt; |
| 3118 | xint *ofail; |
| 3119 | int rv; |
| 3120 | |
| 3121 | #if __GNUC__ |
| 3122 | /* Avoid longjmp clobbering */ |
| 3123 | (void) &rv; |
| 3124 | #endif |
| 3125 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3126 | DBGPRINTF(("RUN: enter, areanum %d, outtree %p, failpt %p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3127 | areanum, outtree, failpt)); |
| 3128 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3129 | areanum++; |
| 3130 | swdlist = wdlist; |
| 3131 | siolist = iolist; |
| 3132 | otree = outtree; |
| 3133 | ofail = failpt; |
| 3134 | rv = -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3135 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3136 | errpt = ev; |
| 3137 | if (newenv(setjmp(errpt)) == 0) { |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 3138 | wdlist = NULL; |
| 3139 | iolist = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3140 | pushio(argp, f); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3141 | global_env.iobase = global_env.iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3142 | yynerrs = 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3143 | failpt = rt; |
| 3144 | if (setjmp(failpt) == 0 && yyparse() == 0) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 3145 | rv = execute(outtree, NOPIPE, NOPIPE, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3146 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3147 | } else { |
| 3148 | DBGPRINTF(("RUN: error from newenv()!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3149 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3150 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3151 | wdlist = swdlist; |
| 3152 | iolist = siolist; |
| 3153 | failpt = ofail; |
| 3154 | outtree = otree; |
| 3155 | freearea(areanum--); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3156 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3157 | return rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | /* -------- do.c -------- */ |
| 3161 | |
| 3162 | /* |
| 3163 | * built-in commands: doX |
| 3164 | */ |
| 3165 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3166 | static int dohelp(struct op *t ATTRIBUTE_UNUSED, char **args ATTRIBUTE_UNUSED) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3167 | { |
| 3168 | int col; |
| 3169 | const struct builtincmd *x; |
| 3170 | |
Denis Vlasenko | 0ee3999 | 2006-12-24 15:23:28 +0000 | [diff] [blame] | 3171 | puts("\nBuilt-in commands:\n" |
| 3172 | "-------------------"); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3173 | |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 3174 | col = 0; |
| 3175 | x = builtincmds; |
| 3176 | while (x->name) { |
| 3177 | col += printf("%c%s", ((col == 0) ? '\t' : ' '), x->name); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3178 | if (col > 60) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 3179 | bb_putchar('\n'); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3180 | col = 0; |
| 3181 | } |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 3182 | x++; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3183 | } |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3184 | #if ENABLE_FEATURE_SH_STANDALONE |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3185 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3186 | const char *applet = applet_names; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3187 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3188 | while (*applet) { |
| 3189 | col += printf("%c%s", ((col == 0) ? '\t' : ' '), applet); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3190 | if (col > 60) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 3191 | bb_putchar('\n'); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3192 | col = 0; |
| 3193 | } |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3194 | applet += strlen(applet) + 1; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3195 | } |
| 3196 | } |
| 3197 | #endif |
Denis Vlasenko | 0ee3999 | 2006-12-24 15:23:28 +0000 | [diff] [blame] | 3198 | puts("\n"); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3199 | return EXIT_SUCCESS; |
| 3200 | } |
| 3201 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3202 | static int dolabel(struct op *t ATTRIBUTE_UNUSED, char **args ATTRIBUTE_UNUSED) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3203 | { |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3204 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3207 | static int dochdir(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3208 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3209 | const char *cp, *er; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3210 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3211 | cp = args[1]; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3212 | if (cp == NULL) { |
| 3213 | cp = homedir->value; |
Denis Vlasenko | d244c5e | 2007-02-09 17:30:14 +0000 | [diff] [blame] | 3214 | if (cp != NULL) |
| 3215 | goto do_cd; |
| 3216 | er = ": no home directory"; |
| 3217 | } else { |
| 3218 | do_cd: |
| 3219 | if (chdir(cp) >= 0) |
| 3220 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3221 | er = ": bad directory"; |
Denis Vlasenko | d244c5e | 2007-02-09 17:30:14 +0000 | [diff] [blame] | 3222 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3223 | prs(cp != NULL ? cp : "cd"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3224 | err(er); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3225 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3228 | static int doshift(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3229 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3230 | int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3231 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3232 | n = args[1] ? getn(args[1]) : 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3233 | if (dolc < n) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3234 | err("nothing to shift"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3235 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3236 | } |
| 3237 | dolv[n] = dolv[0]; |
| 3238 | dolv += n; |
| 3239 | dolc -= n; |
| 3240 | setval(lookup("#"), putn(dolc)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3241 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | /* |
| 3245 | * execute login and newgrp directly |
| 3246 | */ |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3247 | static int dologin(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3248 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3249 | const char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3250 | |
| 3251 | if (interactive) { |
| 3252 | signal(SIGINT, SIG_DFL); |
| 3253 | signal(SIGQUIT, SIG_DFL); |
| 3254 | } |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3255 | cp = rexecve(args[0], args, makenv(0, NULL)); |
| 3256 | prs(args[0]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3257 | prs(": "); |
| 3258 | err(cp); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3259 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3262 | static int doumask(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3263 | { |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 3264 | int i; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3265 | char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3266 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3267 | cp = args[1]; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3268 | if (cp == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3269 | i = umask(0); |
| 3270 | umask(i); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 3271 | printf("%04o\n", i); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3272 | } else { |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 3273 | i = bb_strtou(cp, NULL, 8); |
| 3274 | if (errno) { |
| 3275 | err("umask: bad octal number"); |
| 3276 | return 1; |
| 3277 | } |
| 3278 | umask(i); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3279 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3280 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3283 | static int doexec(struct op *t, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3284 | { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3285 | jmp_buf ex; |
| 3286 | xint *ofail; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3287 | char **sv_words; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3288 | |
| 3289 | t->ioact = NULL; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3290 | if (!args[1]) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3291 | return 1; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3292 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3293 | execflg = 1; |
| 3294 | ofail = failpt; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3295 | failpt = ex; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3296 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3297 | sv_words = t->op_words; |
| 3298 | t->op_words = args + 1; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3299 | // TODO: test what will happen with "exec break" - |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3300 | // will it leave t->op_words pointing to garbage? |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3301 | // (see http://bugs.busybox.net/view.php?id=846) |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3302 | if (setjmp(failpt) == 0) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 3303 | execute(t, NOPIPE, NOPIPE, /* no_fork: */ 1); |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3304 | t->op_words = sv_words; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3305 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3306 | failpt = ofail; |
| 3307 | execflg = 0; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3308 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3309 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3312 | static int dodot(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3313 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3314 | int i; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3315 | const char *sp; |
| 3316 | char *tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3317 | char *cp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3318 | int maltmp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3319 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3320 | DBGPRINTF(("DODOT: enter, t=%p, tleft %p, tright %p, global_env.linep is %s\n", |
| 3321 | t, t->left, t->right, ((global_env.linep == NULL) ? "NULL" : global_env.linep))); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3322 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3323 | cp = args[1]; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3324 | if (cp == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3325 | DBGPRINTF(("DODOT: bad args, ret 0\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3326 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3327 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3328 | DBGPRINTF(("DODOT: cp is %s\n", cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3329 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3330 | sp = any('/', cp) ? ":" : path->value; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3331 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3332 | DBGPRINTF(("DODOT: sp is %s, global_env.linep is %s\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3333 | ((sp == NULL) ? "NULL" : sp), |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3334 | ((global_env.linep == NULL) ? "NULL" : global_env.linep))); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3335 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3336 | while (*sp) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3337 | tp = global_env.linep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3338 | while (*sp && (*tp = *sp++) != ':') |
| 3339 | tp++; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3340 | if (tp != global_env.linep) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3341 | *tp++ = '/'; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3342 | strcpy(tp, cp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3343 | |
| 3344 | /* Original code */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3345 | i = open(global_env.linep, O_RDONLY); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3346 | if (i >= 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3347 | exstat = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3348 | maltmp = remap(i); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3349 | DBGPRINTF(("DODOT: remap=%d, exstat=%d, global_env.iofd %d, i %d, global_env.linep is %s\n", |
| 3350 | maltmp, exstat, global_env.iofd, i, global_env.linep)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3351 | |
| 3352 | next(maltmp); /* Basically a PUSHIO */ |
| 3353 | |
| 3354 | DBGPRINTF(("DODOT: returning exstat=%d\n", exstat)); |
| 3355 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3356 | return exstat; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3357 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3358 | } /* while */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3359 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3360 | prs(cp); |
| 3361 | err(": not found"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3362 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3363 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3366 | static int dowait(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3367 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3368 | int i; |
| 3369 | char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3370 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3371 | cp = args[1]; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3372 | if (cp != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3373 | i = getn(cp); |
| 3374 | if (i == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3375 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3376 | } else |
| 3377 | i = -1; |
| 3378 | setstatus(waitfor(i, 1)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3379 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3382 | static int doread(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3383 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3384 | char *cp, **wp; |
| 3385 | int nb = 0; |
| 3386 | int nl = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3387 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3388 | if (args[1] == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3389 | err("Usage: read name ..."); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3390 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3391 | } |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3392 | for (wp = args + 1; *wp; wp++) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3393 | for (cp = global_env.linep; !nl && cp < elinep - 1; cp++) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 3394 | nb = nonblock_safe_read(STDIN_FILENO, cp, sizeof(*cp)); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3395 | if (nb != sizeof(*cp)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3396 | break; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3397 | nl = (*cp == '\n'); |
| 3398 | if (nl || (wp[1] && any(*cp, ifs->value))) |
| 3399 | break; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3400 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3401 | *cp = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3402 | if (nb <= 0) |
| 3403 | break; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3404 | setval(lookup(*wp), global_env.linep); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3405 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3406 | return nb <= 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3409 | static int doeval(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3410 | { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3411 | return RUN(awordlist, args + 1, wdchar); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3414 | static int dotrap(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3415 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3416 | int n, i; |
| 3417 | int resetsig; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3418 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3419 | if (args[1] == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3420 | for (i = 0; i <= _NSIG; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3421 | if (trap[i]) { |
| 3422 | prn(i); |
| 3423 | prs(": "); |
| 3424 | prs(trap[i]); |
| 3425 | prs("\n"); |
| 3426 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3427 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3428 | } |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3429 | resetsig = isdigit(args[1][0]); |
| 3430 | for (i = resetsig ? 1 : 2; args[i] != NULL; ++i) { |
| 3431 | n = getsig(args[i]); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3432 | freecell(trap[n]); |
| 3433 | trap[n] = 0; |
| 3434 | if (!resetsig) { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3435 | if (args[1][0] != '\0') { |
| 3436 | trap[n] = strsave(args[1], 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3437 | setsig(n, sig); |
| 3438 | } else |
| 3439 | setsig(n, SIG_IGN); |
| 3440 | } else { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 3441 | if (interactive) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3442 | if (n == SIGINT) |
| 3443 | setsig(n, onintr); |
| 3444 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3445 | setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 3446 | } else |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3447 | setsig(n, SIG_DFL); |
| 3448 | } |
| 3449 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3450 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3453 | static int getsig(char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3454 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3455 | int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3456 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3457 | n = getn(s); |
| 3458 | if (n < 0 || n > _NSIG) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3459 | err("trap: bad signal number"); |
| 3460 | n = 0; |
| 3461 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3462 | return n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3465 | static void setsig(int n, sighandler_t f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3466 | { |
| 3467 | if (n == 0) |
| 3468 | return; |
| 3469 | if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) { |
| 3470 | ourtrap[n] = 1; |
| 3471 | signal(n, f); |
| 3472 | } |
| 3473 | } |
| 3474 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3475 | static int getn(char *as) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3476 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3477 | char *s; |
| 3478 | int n, m; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3479 | |
| 3480 | s = as; |
| 3481 | m = 1; |
| 3482 | if (*s == '-') { |
| 3483 | m = -1; |
| 3484 | s++; |
| 3485 | } |
| 3486 | for (n = 0; isdigit(*s); s++) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3487 | n = (n * 10) + (*s - '0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3488 | if (*s) { |
| 3489 | prs(as); |
| 3490 | err(": bad number"); |
| 3491 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3492 | return n * m; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3495 | static int dobreak(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3496 | { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3497 | return brkcontin(args[1], 1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3500 | static int docontinue(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3501 | { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3502 | return brkcontin(args[1], 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3505 | static int brkcontin(char *cp, int val) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3506 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3507 | struct brkcon *bc; |
| 3508 | int nl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3509 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3510 | nl = cp == NULL ? 1 : getn(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3511 | if (nl <= 0) |
| 3512 | nl = 999; |
| 3513 | do { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3514 | bc = brklist; |
| 3515 | if (bc == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3516 | break; |
| 3517 | brklist = bc->nextlev; |
| 3518 | } while (--nl); |
| 3519 | if (nl) { |
| 3520 | err("bad break/continue level"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3521 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3522 | } |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 3523 | isbreak = (val != 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3524 | longjmp(bc->brkpt, 1); |
| 3525 | /* NOTREACHED */ |
| 3526 | } |
| 3527 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3528 | static int doexit(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3529 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3530 | char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3531 | |
| 3532 | execflg = 0; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3533 | cp = args[1]; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3534 | if (cp != NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3535 | setstatus(getn(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3536 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3537 | DBGPRINTF(("DOEXIT: calling leave(), t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3538 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3539 | leave(); |
| 3540 | /* NOTREACHED */ |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3541 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3544 | static int doexport(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3545 | { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3546 | rdexp(args + 1, export, EXPORT); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3547 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3550 | static int doreadonly(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3551 | { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3552 | rdexp(args + 1, ronly, RONLY); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3553 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3556 | static void rdexp(char **wp, void (*f) (struct var *), int key) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3557 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3558 | DBGPRINTF6(("RDEXP: enter, wp=%p, func=%p, key=%d\n", wp, f, key)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3559 | DBGPRINTF6(("RDEXP: *wp=%s\n", *wp)); |
| 3560 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3561 | if (*wp != NULL) { |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3562 | for (; *wp != NULL; wp++) { |
| 3563 | if (isassign(*wp)) { |
| 3564 | char *cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3565 | |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3566 | assign(*wp, COPYV); |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3567 | for (cp = *wp; *cp != '='; cp++) |
| 3568 | continue; |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3569 | *cp = '\0'; |
| 3570 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3571 | if (checkname(*wp)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3572 | (*f) (lookup(*wp)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3573 | else |
| 3574 | badid(*wp); |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3575 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3576 | } else |
| 3577 | putvlist(key, 1); |
| 3578 | } |
| 3579 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3580 | static void badid(char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3581 | { |
| 3582 | prs(s); |
| 3583 | err(": bad identifier"); |
| 3584 | } |
| 3585 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3586 | static int doset(struct op *t ATTRIBUTE_UNUSED, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3587 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3588 | struct var *vp; |
| 3589 | char *cp; |
| 3590 | int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3591 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3592 | cp = args[1]; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3593 | if (cp == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3594 | for (vp = vlist; vp; vp = vp->next) |
| 3595 | varput(vp->name, 1); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3596 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3597 | } |
| 3598 | if (*cp == '-') { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3599 | args++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3600 | if (*++cp == 0) |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3601 | FLAG['x'] = FLAG['v'] = 0; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3602 | else { |
| 3603 | for (; *cp; cp++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3604 | switch (*cp) { |
| 3605 | case 'e': |
| 3606 | if (!interactive) |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3607 | FLAG['e']++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3608 | break; |
| 3609 | |
| 3610 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3611 | if (*cp >= 'a' && *cp <= 'z') |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3612 | FLAG[(int) *cp]++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3613 | break; |
| 3614 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3615 | } |
| 3616 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3617 | setdash(); |
| 3618 | } |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3619 | if (args[1]) { |
| 3620 | args[0] = dolv[0]; |
| 3621 | for (n = 1; args[n]; n++) |
| 3622 | setarea((char *) args[n], 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3623 | dolc = n - 1; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3624 | dolv = args; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3625 | setval(lookup("#"), putn(dolc)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3626 | setarea((char *) (dolv - 1), 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3627 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3628 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3631 | static void varput(char *s, int out) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3632 | { |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3633 | if (isalnum(*s) || *s == '_') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3634 | write(out, s, strlen(s)); |
| 3635 | write(out, "\n", 1); |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | |
| 3640 | /* |
| 3641 | * Copyright (c) 1999 Herbert Xu <herbert@debian.org> |
| 3642 | * This file contains code for the times builtin. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3643 | */ |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3644 | static void times_fmt(char *buf, clock_t val, unsigned clk_tck) |
| 3645 | { |
| 3646 | unsigned min, sec; |
| 3647 | if (sizeof(val) > sizeof(int)) |
| 3648 | sec = ((unsigned long)val) / clk_tck; |
| 3649 | else |
| 3650 | sec = ((unsigned)val) / clk_tck; |
| 3651 | min = sec / 60; |
| 3652 | #if ENABLE_DESKTOP |
| 3653 | sprintf(buf, "%um%u.%03us", min, (sec - min * 60), |
| 3654 | /* msec: */ ((unsigned)(val - (clock_t)sec * clk_tck)) * 1000 / clk_tck |
| 3655 | ); |
| 3656 | #else |
| 3657 | sprintf(buf, "%um%us", min, (sec - min * 60)); |
| 3658 | #endif |
| 3659 | } |
| 3660 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3661 | static int dotimes(struct op *t ATTRIBUTE_UNUSED, char **args ATTRIBUTE_UNUSED) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3662 | { |
| 3663 | struct tms buf; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3664 | unsigned clk_tck = sysconf(_SC_CLK_TCK); |
| 3665 | /* How much do we need for "NmN.NNNs" ? */ |
| 3666 | enum { TIMEBUF_SIZE = sizeof(int)*3 + sizeof(int)*3 + 6 }; |
| 3667 | char u[TIMEBUF_SIZE], s[TIMEBUF_SIZE]; |
| 3668 | char cu[TIMEBUF_SIZE], cs[TIMEBUF_SIZE]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3669 | |
| 3670 | times(&buf); |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3671 | |
| 3672 | times_fmt(u, buf.tms_utime, clk_tck); |
| 3673 | times_fmt(s, buf.tms_stime, clk_tck); |
| 3674 | times_fmt(cu, buf.tms_cutime, clk_tck); |
| 3675 | times_fmt(cs, buf.tms_cstime, clk_tck); |
| 3676 | |
| 3677 | printf("%s %s\n%s %s\n", u, s, cu, cs); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3678 | return 0; |
| 3679 | } |
| 3680 | |
| 3681 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3682 | /* -------- eval.c -------- */ |
| 3683 | |
| 3684 | /* |
| 3685 | * ${} |
| 3686 | * `command` |
| 3687 | * blank interpretation |
| 3688 | * quoting |
| 3689 | * glob |
| 3690 | */ |
| 3691 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3692 | static char **eval(char **ap, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3693 | { |
| 3694 | struct wdblock *wb; |
| 3695 | char **wp; |
| 3696 | char **wf; |
| 3697 | jmp_buf ev; |
| 3698 | |
| 3699 | #if __GNUC__ |
| 3700 | /* Avoid longjmp clobbering */ |
| 3701 | (void) ℘ |
| 3702 | (void) ≈ |
| 3703 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3704 | |
| 3705 | DBGPRINTF4(("EVAL: enter, f=%d\n", f)); |
| 3706 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3707 | wp = NULL; |
| 3708 | wb = NULL; |
| 3709 | wf = NULL; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3710 | errpt = ev; |
| 3711 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3712 | while (*ap && isassign(*ap)) |
| 3713 | expand(*ap++, &wb, f & ~DOGLOB); |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3714 | if (FLAG['k']) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3715 | for (wf = ap; *wf; wf++) { |
| 3716 | if (isassign(*wf)) |
| 3717 | expand(*wf, &wb, f & ~DOGLOB); |
| 3718 | } |
| 3719 | } |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 3720 | for (wb = addword((char *) NULL, wb); *ap; ap++) { |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3721 | if (!FLAG['k'] || !isassign(*ap)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3722 | expand(*ap, &wb, f & ~DOKEY); |
| 3723 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3724 | wb = addword((char *) 0, wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3725 | wp = getwords(wb); |
| 3726 | quitenv(); |
| 3727 | } else |
| 3728 | gflg = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3729 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3730 | return gflg ? (char **) NULL : wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3733 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3734 | /* |
| 3735 | * Make the exported environment from the exported |
| 3736 | * names in the dictionary. Keyword assignments |
| 3737 | * will already have been done. |
| 3738 | */ |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3739 | static char **makenv(int all, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3740 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3741 | struct var *vp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3742 | |
| 3743 | DBGPRINTF5(("MAKENV: enter, all=%d\n", all)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3744 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3745 | for (vp = vlist; vp; vp = vp->next) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3746 | if (all || vp->status & EXPORT) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3747 | wb = addword(vp->name, wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3748 | wb = addword((char *) 0, wb); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3749 | return getwords(wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3752 | static int expand(const char *cp, struct wdblock **wbp, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3753 | { |
| 3754 | jmp_buf ev; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3755 | char *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3756 | |
| 3757 | #if __GNUC__ |
| 3758 | /* Avoid longjmp clobbering */ |
| 3759 | (void) &cp; |
| 3760 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3761 | |
| 3762 | DBGPRINTF3(("EXPAND: enter, f=%d\n", f)); |
| 3763 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3764 | gflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3765 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3766 | if (cp == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3767 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3768 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3769 | if (!anys("$`'\"", cp) && !anys(ifs->value, cp) |
| 3770 | && ((f & DOGLOB) == 0 || !anys("[*?", cp)) |
| 3771 | ) { |
| 3772 | xp = strsave(cp, areanum); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3773 | if (f & DOTRIM) |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3774 | unquote(xp); |
| 3775 | *wbp = addword(xp, *wbp); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3776 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3777 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3778 | errpt = ev; |
| 3779 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3780 | PUSHIO(aword, cp, strchar); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3781 | global_env.iobase = global_env.iop; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3782 | while ((xp = blank(f)) && gflg == 0) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3783 | global_env.linep = xp; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3784 | xp = strsave(xp, areanum); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3785 | if ((f & DOGLOB) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3786 | if (f & DOTRIM) |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3787 | unquote(xp); |
| 3788 | *wbp = addword(xp, *wbp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3789 | } else |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3790 | *wbp = glob(xp, *wbp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3791 | } |
| 3792 | quitenv(); |
| 3793 | } else |
| 3794 | gflg = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3795 | return gflg == 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3796 | } |
| 3797 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3798 | static char *evalstr(char *cp, int f) |
| 3799 | { |
| 3800 | struct wdblock *wb; |
| 3801 | |
| 3802 | DBGPRINTF6(("EVALSTR: enter, cp=%p, f=%d\n", cp, f)); |
| 3803 | |
| 3804 | wb = NULL; |
| 3805 | if (expand(cp, &wb, f)) { |
| 3806 | if (wb == NULL || wb->w_nword == 0 |
| 3807 | || (cp = wb->w_words[0]) == NULL |
| 3808 | ) { |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 3809 | // TODO: I suspect that |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3810 | // char *evalstr(char *cp, int f) is actually |
| 3811 | // const char *evalstr(const char *cp, int f)! |
| 3812 | cp = (char*)""; |
| 3813 | } |
| 3814 | DELETE(wb); |
| 3815 | } else |
| 3816 | cp = NULL; |
| 3817 | return cp; |
| 3818 | } |
| 3819 | |
| 3820 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3821 | /* |
| 3822 | * Blank interpretation and quoting |
| 3823 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3824 | static char *blank(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3825 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3826 | int c, c1; |
| 3827 | char *sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3828 | int scanequals, foundequals; |
| 3829 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3830 | DBGPRINTF3(("BLANK: enter, f=%d\n", f)); |
| 3831 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3832 | sp = global_env.linep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3833 | scanequals = f & DOKEY; |
| 3834 | foundequals = 0; |
| 3835 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3836 | loop: |
| 3837 | c = subgetc('"', foundequals); |
| 3838 | switch (c) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3839 | case 0: |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3840 | if (sp == global_env.linep) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3841 | return 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3842 | *global_env.linep++ = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3843 | return sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3844 | |
| 3845 | default: |
| 3846 | if (f & DOBLANK && any(c, ifs->value)) |
| 3847 | goto loop; |
| 3848 | break; |
| 3849 | |
| 3850 | case '"': |
| 3851 | case '\'': |
| 3852 | scanequals = 0; |
| 3853 | if (INSUB()) |
| 3854 | break; |
| 3855 | for (c1 = c; (c = subgetc(c1, 1)) != c1;) { |
| 3856 | if (c == 0) |
| 3857 | break; |
| 3858 | if (c == '\'' || !any(c, "$`\"")) |
| 3859 | c |= QUOTE; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3860 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3861 | } |
| 3862 | c = 0; |
| 3863 | } |
| 3864 | unget(c); |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3865 | if (!isalpha(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3866 | scanequals = 0; |
| 3867 | for (;;) { |
| 3868 | c = subgetc('"', foundequals); |
| 3869 | if (c == 0 || |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3870 | f & (DOBLANK && any(c, ifs->value)) || |
| 3871 | (!INSUB() && any(c, "\"'"))) { |
| 3872 | scanequals = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3873 | unget(c); |
| 3874 | if (any(c, "\"'")) |
| 3875 | goto loop; |
| 3876 | break; |
| 3877 | } |
| 3878 | if (scanequals) { |
| 3879 | if (c == '=') { |
| 3880 | foundequals = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3881 | scanequals = 0; |
| 3882 | } else if (!isalnum(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3883 | scanequals = 0; |
| 3884 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3885 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3886 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3887 | *global_env.linep++ = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3888 | return sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | /* |
| 3892 | * Get characters, substituting for ` and $ |
| 3893 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3894 | static int subgetc(char ec, int quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3895 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3896 | char c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3897 | |
| 3898 | DBGPRINTF3(("SUBGETC: enter, quoted=%d\n", quoted)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3899 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3900 | again: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3901 | c = my_getc(ec); |
| 3902 | if (!INSUB() && ec != '\'') { |
| 3903 | if (c == '`') { |
| 3904 | if (grave(quoted) == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3905 | return 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3906 | global_env.iop->task = XGRAVE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3907 | goto again; |
| 3908 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3909 | if (c == '$') { |
| 3910 | c = dollar(quoted); |
| 3911 | if (c == 0) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3912 | global_env.iop->task = XDOLL; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3913 | goto again; |
| 3914 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3915 | } |
| 3916 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3917 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
| 3920 | /* |
| 3921 | * Prepare to generate the string returned by ${} substitution. |
| 3922 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3923 | static int dollar(int quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3924 | { |
| 3925 | int otask; |
| 3926 | struct io *oiop; |
| 3927 | char *dolp; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3928 | char *s, c, *cp = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3929 | struct var *vp; |
| 3930 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3931 | DBGPRINTF3(("DOLLAR: enter, quoted=%d\n", quoted)); |
| 3932 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3933 | c = readc(); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3934 | s = global_env.linep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3935 | if (c != '{') { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3936 | *global_env.linep++ = c; |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3937 | if (isalpha(c) || c == '_') { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3938 | while ((c = readc()) != 0 && (isalnum(c) || c == '_')) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3939 | if (global_env.linep < elinep) |
| 3940 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3941 | unget(c); |
| 3942 | } |
| 3943 | c = 0; |
| 3944 | } else { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3945 | oiop = global_env.iop; |
| 3946 | otask = global_env.iop->task; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3947 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3948 | global_env.iop->task = XOTHER; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3949 | while ((c = subgetc('"', 0)) != 0 && c != '}' && c != '\n') |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3950 | if (global_env.linep < elinep) |
| 3951 | *global_env.linep++ = c; |
| 3952 | if (oiop == global_env.iop) |
| 3953 | global_env.iop->task = otask; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3954 | if (c != '}') { |
| 3955 | err("unclosed ${"); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 3956 | gflg = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3957 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3958 | } |
| 3959 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3960 | if (global_env.linep >= elinep) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3961 | err("string in ${} too long"); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 3962 | gflg = 1; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3963 | global_env.linep -= 10; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3964 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3965 | *global_env.linep = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3966 | if (*s) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3967 | for (cp = s + 1; *cp; cp++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3968 | if (any(*cp, "=-+?")) { |
| 3969 | c = *cp; |
| 3970 | *cp++ = 0; |
| 3971 | break; |
| 3972 | } |
| 3973 | if (s[1] == 0 && (*s == '*' || *s == '@')) { |
| 3974 | if (dolc > 1) { |
| 3975 | /* currently this does not distinguish $* and $@ */ |
| 3976 | /* should check dollar */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3977 | global_env.linep = s; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3978 | PUSHIO(awordlist, dolv + 1, dolchar); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3979 | return 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3980 | } else { /* trap the nasty ${=} */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3981 | s[0] = '1'; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3982 | s[1] = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3983 | } |
| 3984 | } |
| 3985 | vp = lookup(s); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3986 | dolp = vp->value; |
| 3987 | if (dolp == null) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3988 | switch (c) { |
| 3989 | case '=': |
| 3990 | if (isdigit(*s)) { |
| 3991 | err("cannot use ${...=...} with $n"); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 3992 | gflg = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3993 | break; |
| 3994 | } |
| 3995 | setval(vp, cp); |
| 3996 | dolp = vp->value; |
| 3997 | break; |
| 3998 | |
| 3999 | case '-': |
| 4000 | dolp = strsave(cp, areanum); |
| 4001 | break; |
| 4002 | |
| 4003 | case '?': |
| 4004 | if (*cp == 0) { |
| 4005 | prs("missing value for "); |
| 4006 | err(s); |
| 4007 | } else |
| 4008 | err(cp); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 4009 | gflg = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4010 | break; |
| 4011 | } |
| 4012 | } else if (c == '+') |
| 4013 | dolp = strsave(cp, areanum); |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 4014 | if (FLAG['u'] && dolp == null) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4015 | prs("unset variable: "); |
| 4016 | err(s); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 4017 | gflg = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4018 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4019 | global_env.linep = s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4020 | PUSHIO(aword, dolp, quoted ? qstrchar : strchar); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4021 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
| 4024 | /* |
| 4025 | * Run the command in `...` and read its output. |
| 4026 | */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4027 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4028 | static int grave(int quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4029 | { |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 4030 | /* moved to G: static char child_cmd[LINELIM]; */ |
| 4031 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4032 | const char *cp; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4033 | int i; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4034 | int j; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4035 | int pf[2]; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4036 | const char *src; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4037 | char *dest; |
| 4038 | int count; |
| 4039 | int ignore; |
| 4040 | int ignore_once; |
| 4041 | char *argument_list[4]; |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4042 | struct wdblock *wb = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4043 | |
| 4044 | #if __GNUC__ |
| 4045 | /* Avoid longjmp clobbering */ |
| 4046 | (void) &cp; |
| 4047 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4048 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4049 | for (cp = global_env.iop->argp->aword; *cp != '`'; cp++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4050 | if (*cp == 0) { |
| 4051 | err("no closing `"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4052 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4053 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4054 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4055 | |
| 4056 | /* string copy with dollar expansion */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4057 | src = global_env.iop->argp->aword; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4058 | dest = child_cmd; |
| 4059 | count = 0; |
| 4060 | ignore = 0; |
| 4061 | ignore_once = 0; |
| 4062 | while ((*src != '`') && (count < LINELIM)) { |
| 4063 | if (*src == '\'') |
| 4064 | ignore = !ignore; |
| 4065 | if (*src == '\\') |
| 4066 | ignore_once = 1; |
| 4067 | if (*src == '$' && !ignore && !ignore_once) { |
| 4068 | struct var *vp; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 4069 | /* moved to G to reduce stack usage |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4070 | char var_name[LINELIM]; |
| 4071 | char alt_value[LINELIM]; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 4072 | */ |
| 4073 | #define var_name (G.grave__var_name) |
| 4074 | #define alt_value (G.grave__alt_value) |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4075 | int var_index = 0; |
| 4076 | int alt_index = 0; |
| 4077 | char operator = 0; |
| 4078 | int braces = 0; |
| 4079 | char *value; |
| 4080 | |
| 4081 | src++; |
| 4082 | if (*src == '{') { |
| 4083 | braces = 1; |
| 4084 | src++; |
| 4085 | } |
| 4086 | |
| 4087 | var_name[var_index++] = *src++; |
Paul Fox | 54690dc | 2005-07-20 18:33:12 +0000 | [diff] [blame] | 4088 | while (isalnum(*src) || *src=='_') |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4089 | var_name[var_index++] = *src++; |
| 4090 | var_name[var_index] = 0; |
| 4091 | |
| 4092 | if (braces) { |
| 4093 | switch (*src) { |
| 4094 | case '}': |
| 4095 | break; |
| 4096 | case '-': |
| 4097 | case '=': |
| 4098 | case '+': |
| 4099 | case '?': |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4100 | operator = * src; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4101 | break; |
| 4102 | default: |
| 4103 | err("unclosed ${\n"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4104 | return 0; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4105 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4106 | if (operator) { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4107 | src++; |
| 4108 | while (*src && (*src != '}')) { |
| 4109 | alt_value[alt_index++] = *src++; |
| 4110 | } |
| 4111 | alt_value[alt_index] = 0; |
| 4112 | if (*src != '}') { |
| 4113 | err("unclosed ${\n"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4114 | return 0; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4115 | } |
| 4116 | } |
| 4117 | src++; |
| 4118 | } |
| 4119 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4120 | if (isalpha(*var_name)) { |
| 4121 | /* let subshell handle it instead */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4122 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4123 | char *namep = var_name; |
| 4124 | |
| 4125 | *dest++ = '$'; |
| 4126 | if (braces) |
| 4127 | *dest++ = '{'; |
| 4128 | while (*namep) |
| 4129 | *dest++ = *namep++; |
| 4130 | if (operator) { |
| 4131 | char *altp = alt_value; |
| 4132 | *dest++ = operator; |
| 4133 | while (*altp) |
| 4134 | *dest++ = *altp++; |
| 4135 | } |
| 4136 | if (braces) |
| 4137 | *dest++ = '}'; |
| 4138 | |
| 4139 | wb = addword(lookup(var_name)->name, wb); |
| 4140 | } else { |
| 4141 | /* expand */ |
| 4142 | |
| 4143 | vp = lookup(var_name); |
| 4144 | if (vp->value != null) |
| 4145 | value = (operator == '+') ? |
| 4146 | alt_value : vp->value; |
| 4147 | else if (operator == '?') { |
| 4148 | err(alt_value); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4149 | return 0; |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4150 | } else if (alt_index && (operator != '+')) { |
| 4151 | value = alt_value; |
| 4152 | if (operator == '=') |
| 4153 | setval(vp, value); |
| 4154 | } else |
| 4155 | continue; |
| 4156 | |
| 4157 | while (*value && (count < LINELIM)) { |
| 4158 | *dest++ = *value++; |
| 4159 | count++; |
| 4160 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4161 | } |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 4162 | #undef var_name |
| 4163 | #undef alt_value |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4164 | } else { |
| 4165 | *dest++ = *src++; |
| 4166 | count++; |
| 4167 | ignore_once = 0; |
| 4168 | } |
| 4169 | } |
| 4170 | *dest = '\0'; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4171 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4172 | if (openpipe(pf) < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4173 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4174 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4175 | while ((i = vfork()) == -1 && errno == EAGAIN) |
| 4176 | continue; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4177 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4178 | DBGPRINTF3(("GRAVE: i is %p\n", io)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4179 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4180 | if (i < 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4181 | closepipe(pf); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4182 | err((char *) bb_msg_memory_exhausted); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4183 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4184 | } |
| 4185 | if (i != 0) { |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 4186 | waitpid(i, NULL, 0); // safe_waitpid? |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4187 | global_env.iop->argp->aword = ++cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4188 | close(pf[1]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4189 | PUSHIO(afile, remap(pf[0]), |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4190 | (int (*)(struct ioarg *)) ((quoted) ? qgravechar : gravechar)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4191 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4192 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4193 | /* allow trapped signals */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4194 | /* XXX - Maybe this signal stuff should go as well? */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4195 | for (j = 0; j <= _NSIG; j++) |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4196 | if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN) |
| 4197 | signal(j, SIG_DFL); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4198 | |
Denis Vlasenko | 2b54aaa | 2007-05-09 22:16:08 +0000 | [diff] [blame] | 4199 | /* Testcase where below checks are needed: |
| 4200 | * close stdout & run this script: |
| 4201 | * files=`ls` |
| 4202 | * echo "$files" >zz |
| 4203 | */ |
| 4204 | xmove_fd(pf[1], 1); |
Denis Vlasenko | 847fa77 | 2008-01-28 22:45:43 +0000 | [diff] [blame] | 4205 | if (pf[0] != 1) |
| 4206 | close(pf[0]); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4207 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4208 | argument_list[0] = (char *) DEFAULT_SHELL; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4209 | argument_list[1] = (char *) "-c"; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4210 | argument_list[2] = child_cmd; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4211 | argument_list[3] = NULL; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4212 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4213 | cp = rexecve(argument_list[0], argument_list, makenv(1, wb)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4214 | prs(argument_list[0]); |
| 4215 | prs(": "); |
| 4216 | err(cp); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 4217 | _exit(EXIT_FAILURE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4220 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4221 | static char *unquote(char *as) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4222 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4223 | char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4224 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4225 | s = as; |
| 4226 | if (s != NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4227 | while (*s) |
| 4228 | *s++ &= ~QUOTE; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4229 | return as; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | /* -------- glob.c -------- */ |
| 4233 | |
| 4234 | /* |
| 4235 | * glob |
| 4236 | */ |
| 4237 | |
| 4238 | #define scopy(x) strsave((x), areanum) |
| 4239 | #define BLKSIZ 512 |
| 4240 | #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent)) |
| 4241 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4242 | static struct wdblock *cl, *nl; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 4243 | static const char spcl[] ALIGN1= "[?*"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4244 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4245 | static struct wdblock *glob(char *cp, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4246 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4247 | int i; |
| 4248 | char *pp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4249 | |
| 4250 | if (cp == 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4251 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4252 | i = 0; |
| 4253 | for (pp = cp; *pp; pp++) |
| 4254 | if (any(*pp, spcl)) |
| 4255 | i++; |
| 4256 | else if (!any(*pp & ~QUOTE, spcl)) |
| 4257 | *pp &= ~QUOTE; |
| 4258 | if (i != 0) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4259 | for (cl = addword(scopy(cp), NULL); anyspcl(cl); cl = nl) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4260 | nl = newword(cl->w_nword * 2); |
| 4261 | for (i = 0; i < cl->w_nword; i++) { /* for each argument */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4262 | for (pp = cl->w_words[i]; *pp; pp++) |
| 4263 | if (any(*pp, spcl)) { |
| 4264 | globname(cl->w_words[i], pp); |
| 4265 | break; |
| 4266 | } |
| 4267 | if (*pp == '\0') |
| 4268 | nl = addword(scopy(cl->w_words[i]), nl); |
| 4269 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4270 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4271 | DELETE(cl->w_words[i]); |
| 4272 | DELETE(cl); |
| 4273 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4274 | if (cl->w_nword) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4275 | for (i = 0; i < cl->w_nword; i++) |
Denis Vlasenko | fb29038 | 2008-03-02 12:51:26 +0000 | [diff] [blame] | 4276 | unquote(cl->w_words[i]); |
| 4277 | qsort_string_vector(cl->w_words, cl->w_nword); |
| 4278 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4279 | wb = addword(cl->w_words[i], wb); |
| 4280 | DELETE(cl); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4281 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4282 | } |
| 4283 | } |
| 4284 | wb = addword(unquote(cp), wb); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4285 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4288 | static void globname(char *we, char *pp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4289 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4290 | char *np, *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4291 | char *name, *gp, *dp; |
| 4292 | int k; |
| 4293 | DIR *dirp; |
| 4294 | struct dirent *de; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4295 | char dname[NAME_MAX + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4296 | struct stat dbuf; |
| 4297 | |
| 4298 | for (np = we; np != pp; pp--) |
| 4299 | if (pp[-1] == '/') |
| 4300 | break; |
Denis Vlasenko | fb29038 | 2008-03-02 12:51:26 +0000 | [diff] [blame] | 4301 | dp = cp = get_space((int) (pp - np) + 3); |
| 4302 | while (np < pp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4303 | *cp++ = *np++; |
| 4304 | *cp++ = '.'; |
| 4305 | *cp = '\0'; |
Denis Vlasenko | fb29038 | 2008-03-02 12:51:26 +0000 | [diff] [blame] | 4306 | gp = cp = get_space(strlen(pp) + 1); |
| 4307 | while (*np && *np != '/') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4308 | *cp++ = *np++; |
| 4309 | *cp = '\0'; |
| 4310 | dirp = opendir(dp); |
| 4311 | if (dirp == 0) { |
| 4312 | DELETE(dp); |
| 4313 | DELETE(gp); |
| 4314 | return; |
| 4315 | } |
| 4316 | dname[NAME_MAX] = '\0'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4317 | while ((de = readdir(dirp)) != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4318 | /* XXX Hmmm... What this could be? (abial) */ |
| 4319 | /* |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4320 | if (ent[j].d_ino == 0) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4321 | continue; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4322 | */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4323 | strncpy(dname, de->d_name, NAME_MAX); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4324 | if (dname[0] == '.') |
| 4325 | if (*gp != '.') |
| 4326 | continue; |
| 4327 | for (k = 0; k < NAME_MAX; k++) |
| 4328 | if (any(dname[k], spcl)) |
| 4329 | dname[k] |= QUOTE; |
| 4330 | if (gmatch(dname, gp)) { |
| 4331 | name = generate(we, pp, dname, np); |
| 4332 | if (*np && !anys(np, spcl)) { |
| 4333 | if (stat(name, &dbuf)) { |
| 4334 | DELETE(name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4335 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4336 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4337 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4338 | nl = addword(name, nl); |
| 4339 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4340 | } |
| 4341 | closedir(dirp); |
| 4342 | DELETE(dp); |
| 4343 | DELETE(gp); |
| 4344 | } |
| 4345 | |
| 4346 | /* |
| 4347 | * generate a pathname as below. |
| 4348 | * start..end1 / middle end |
| 4349 | * the slashes come for free |
| 4350 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4351 | static char *generate(char *start1, char *end1, char *middle, char *end) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4352 | { |
| 4353 | char *p; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4354 | char *op, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4355 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4356 | p = op = get_space((int)(end1 - start1) + strlen(middle) + strlen(end) + 2); |
| 4357 | xp = start1; |
| 4358 | while (xp != end1) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4359 | *op++ = *xp++; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4360 | xp = middle; |
| 4361 | while (*xp != '\0') |
| 4362 | *op++ = *xp++; |
| 4363 | strcpy(op, end); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4364 | return p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4367 | static int anyspcl(struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4368 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4369 | int i; |
| 4370 | char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4371 | |
| 4372 | wd = wb->w_words; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4373 | for (i = 0; i < wb->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4374 | if (anys(spcl, *wd++)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4375 | return 1; |
| 4376 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4379 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4380 | /* -------- word.c -------- */ |
| 4381 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4382 | static struct wdblock *newword(int nw) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4383 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4384 | struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4385 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4386 | wb = get_space(sizeof(*wb) + nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4387 | wb->w_bsize = nw; |
| 4388 | wb->w_nword = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4389 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4390 | } |
| 4391 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4392 | static struct wdblock *addword(char *wd, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4393 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4394 | struct wdblock *wb2; |
| 4395 | int nw; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4396 | |
| 4397 | if (wb == NULL) |
| 4398 | wb = newword(NSTART); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4399 | nw = wb->w_nword; |
| 4400 | if (nw >= wb->w_bsize) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4401 | wb2 = newword(nw * 2); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4402 | memcpy((char *) wb2->w_words, (char *) wb->w_words, |
| 4403 | nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4404 | wb2->w_nword = nw; |
| 4405 | DELETE(wb); |
| 4406 | wb = wb2; |
| 4407 | } |
| 4408 | wb->w_words[wb->w_nword++] = wd; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4409 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4410 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4411 | |
Denis Vlasenko | e471275 | 2007-04-14 15:08:41 +0000 | [diff] [blame] | 4412 | static char **getwords(struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4413 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4414 | char **wd; |
| 4415 | int nb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4416 | |
| 4417 | if (wb == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4418 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4419 | if (wb->w_nword == 0) { |
| 4420 | DELETE(wb); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4421 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4422 | } |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4423 | nb = sizeof(*wd) * wb->w_nword; |
| 4424 | wd = get_space(nb); |
Denis Vlasenko | fb29038 | 2008-03-02 12:51:26 +0000 | [diff] [blame] | 4425 | memcpy(wd, wb->w_words, nb); |
| 4426 | DELETE(wb); /* perhaps should done by caller */ |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4427 | return wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4428 | } |
| 4429 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4430 | |
| 4431 | /* -------- io.c -------- */ |
| 4432 | |
| 4433 | /* |
| 4434 | * shell IO |
| 4435 | */ |
| 4436 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4437 | static int my_getc(int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4438 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4439 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4440 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4441 | if (global_env.linep > elinep) { |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4442 | while ((c = readc()) != '\n' && c) |
| 4443 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4444 | err("input line too long"); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 4445 | gflg = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4446 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4447 | } |
| 4448 | c = readc(); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4449 | if ((ec != '\'') && (ec != '`') && (global_env.iop->task != XGRAVE)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4450 | if (c == '\\') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4451 | c = readc(); |
| 4452 | if (c == '\n' && ec != '\"') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4453 | return my_getc(ec); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4454 | c |= QUOTE; |
| 4455 | } |
| 4456 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4457 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4460 | static void unget(int c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4461 | { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4462 | if (global_env.iop >= global_env.iobase) |
| 4463 | global_env.iop->peekc = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4466 | static int eofc(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4467 | { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4468 | return global_env.iop < global_env.iobase || (global_env.iop->peekc == 0 && global_env.iop->prev == 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4471 | static int readc(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4472 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4473 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4474 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4475 | RCPRINTF(("READC: global_env.iop %p, global_env.iobase %p\n", global_env.iop, global_env.iobase)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4476 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4477 | for (; global_env.iop >= global_env.iobase; global_env.iop--) { |
| 4478 | RCPRINTF(("READC: global_env.iop %p, peekc 0x%x\n", global_env.iop, global_env.iop->peekc)); |
| 4479 | c = global_env.iop->peekc; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4480 | if (c != '\0') { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4481 | global_env.iop->peekc = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4482 | return c; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4483 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4484 | if (global_env.iop->prev != 0) { |
| 4485 | c = (*global_env.iop->iofn)(global_env.iop->argp, global_env.iop); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4486 | if (c != '\0') { |
| 4487 | if (c == -1) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4488 | global_env.iop++; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4489 | continue; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4490 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4491 | if (global_env.iop == iostack) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4492 | ioecho(c); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4493 | global_env.iop->prev = c; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4494 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4495 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4496 | if (global_env.iop->task == XIO && global_env.iop->prev != '\n') { |
| 4497 | global_env.iop->prev = 0; |
| 4498 | if (global_env.iop == iostack) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4499 | ioecho('\n'); |
| 4500 | return '\n'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4501 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4502 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4503 | if (global_env.iop->task == XIO) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4504 | if (multiline) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4505 | global_env.iop->prev = 0; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4506 | return 0; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4507 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4508 | if (interactive && global_env.iop == iostack + 1) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4509 | #if ENABLE_FEATURE_EDITING |
| 4510 | current_prompt = prompt->value; |
| 4511 | #else |
| 4512 | prs(prompt->value); |
| 4513 | #endif |
| 4514 | } |
| 4515 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4516 | } /* FOR */ |
| 4517 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4518 | if (global_env.iop >= iostack) { |
| 4519 | RCPRINTF(("READC: return 0, global_env.iop %p\n", global_env.iop)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4520 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | DBGPRINTF(("READC: leave()...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4524 | leave(); |
| 4525 | /* NOTREACHED */ |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4526 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4529 | static void ioecho(char c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4530 | { |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 4531 | if (FLAG['v']) |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 4532 | write(STDERR_FILENO, &c, sizeof c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4535 | static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4536 | { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4537 | DBGPRINTF(("PUSHIO: argp %p, argp->afid 0x%x, global_env.iop %p\n", argp, |
| 4538 | argp->afid, global_env.iop)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4539 | |
| 4540 | /* Set env ptr for io source to next array spot and check for array overflow */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4541 | if (++global_env.iop >= &iostack[NPUSH]) { |
| 4542 | global_env.iop--; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4543 | err("Shell input nested too deeply"); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 4544 | gflg = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4545 | return; |
| 4546 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4547 | |
| 4548 | /* We did not overflow the NPUSH array spots so setup data structs */ |
| 4549 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4550 | global_env.iop->iofn = (int (*)(struct ioarg *, struct io *)) fn; /* Store data source func ptr */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4551 | |
| 4552 | if (argp->afid != AFID_NOBUF) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4553 | global_env.iop->argp = argp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4554 | else { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4555 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4556 | global_env.iop->argp = ioargstack + (global_env.iop - iostack); /* MAL - index into stack */ |
| 4557 | *global_env.iop->argp = *argp; /* copy data from temp area into stack spot */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4558 | |
| 4559 | /* MAL - mainbuf is for 1st data source (command line?) and all nested use a single shared buffer? */ |
| 4560 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4561 | if (global_env.iop == &iostack[0]) |
| 4562 | global_env.iop->argp->afbuf = &mainbuf; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4563 | else |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4564 | global_env.iop->argp->afbuf = &sharedbuf; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4565 | |
| 4566 | /* MAL - if not a termimal AND (commandline OR readable file) then give it a buffer id? */ |
| 4567 | /* This line appears to be active when running scripts from command line */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4568 | if ((isatty(global_env.iop->argp->afile) == 0) |
| 4569 | && (global_env.iop == &iostack[0] |
| 4570 | || lseek(global_env.iop->argp->afile, 0L, SEEK_CUR) != -1)) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4571 | if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */ |
| 4572 | bufid = AFID_ID; /* AFID_ID = 0 */ |
| 4573 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4574 | global_env.iop->argp->afid = bufid; /* assign buffer id */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4575 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4576 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4577 | DBGPRINTF(("PUSHIO: iostack %p, global_env.iop %p, afbuf %p\n", |
| 4578 | iostack, global_env.iop, global_env.iop->argp->afbuf)); |
| 4579 | DBGPRINTF(("PUSHIO: mbuf %p, sbuf %p, bid %d, global_env.iop %p\n", |
| 4580 | &mainbuf, &sharedbuf, bufid, global_env.iop)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4581 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4582 | } |
| 4583 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4584 | global_env.iop->prev = ~'\n'; |
| 4585 | global_env.iop->peekc = 0; |
| 4586 | global_env.iop->xchar = 0; |
| 4587 | global_env.iop->nlcount = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4588 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4589 | if (fn == filechar || fn == linechar) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4590 | global_env.iop->task = XIO; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4591 | else if (fn == (int (*)(struct ioarg *)) gravechar |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4592 | || fn == (int (*)(struct ioarg *)) qgravechar) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4593 | global_env.iop->task = XGRAVE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4594 | else |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4595 | global_env.iop->task = XOTHER; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4598 | static struct io *setbase(struct io *ip) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4599 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4600 | struct io *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4601 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4602 | xp = global_env.iobase; |
| 4603 | global_env.iobase = ip; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4604 | return xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | /* |
| 4608 | * Input generating functions |
| 4609 | */ |
| 4610 | |
| 4611 | /* |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4612 | * Produce the characters of a string, then a newline, then NUL. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4613 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4614 | static int nlchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4615 | { |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4616 | char c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4617 | |
| 4618 | if (ap->aword == NULL) |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4619 | return '\0'; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4620 | c = *ap->aword++; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4621 | if (c == '\0') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4622 | ap->aword = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4623 | return '\n'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4624 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4625 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | /* |
| 4629 | * Given a list of words, produce the characters |
| 4630 | * in them, with a space after each word. |
| 4631 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4632 | static int wdchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4633 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4634 | char c; |
| 4635 | char **wl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4636 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4637 | wl = ap->awordlist; |
| 4638 | if (wl == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4639 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4640 | if (*wl != NULL) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4641 | c = *(*wl)++; |
| 4642 | if (c != 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4643 | return c & 0177; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4644 | ap->awordlist++; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4645 | return ' '; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4646 | } |
| 4647 | ap->awordlist = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4648 | return '\n'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4649 | } |
| 4650 | |
| 4651 | /* |
| 4652 | * Return the characters of a list of words, |
| 4653 | * producing a space between them. |
| 4654 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4655 | static int dolchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4656 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4657 | char *wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4658 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4659 | wp = *ap->awordlist++; |
| 4660 | if (wp != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4661 | PUSHIO(aword, wp, *ap->awordlist == NULL ? strchar : xxchar); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4662 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4663 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4664 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4665 | } |
| 4666 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4667 | static int xxchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4668 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4669 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4670 | |
| 4671 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4672 | return 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4673 | c = *ap->aword++; |
| 4674 | if (c == '\0') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4675 | ap->aword = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4676 | return ' '; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4677 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4678 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | /* |
| 4682 | * Produce the characters from a single word (string). |
| 4683 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4684 | static int strchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4685 | { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4686 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4687 | return 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4688 | return *ap->aword++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | /* |
| 4692 | * Produce quoted characters from a single word (string). |
| 4693 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4694 | static int qstrchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4695 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4696 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4697 | |
Denis Vlasenko | b2abef3 | 2007-01-01 18:18:04 +0000 | [diff] [blame] | 4698 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4699 | return 0; |
Denis Vlasenko | b2abef3 | 2007-01-01 18:18:04 +0000 | [diff] [blame] | 4700 | c = *ap->aword++; |
| 4701 | if (c) |
| 4702 | c |= QUOTE; |
| 4703 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
| 4706 | /* |
| 4707 | * Return the characters from a file. |
| 4708 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4709 | static int filechar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4710 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4711 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4712 | char c; |
| 4713 | struct iobuf *bp = ap->afbuf; |
| 4714 | |
| 4715 | if (ap->afid != AFID_NOBUF) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4716 | i = (ap->afid != bp->id); |
| 4717 | if (i || bp->bufp == bp->ebufp) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4718 | if (i) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 4719 | lseek(ap->afile, ap->afpos, SEEK_SET); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4720 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 4721 | i = nonblock_safe_read(ap->afile, bp->buf, sizeof(bp->buf)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4722 | if (i <= 0) { |
| 4723 | closef(ap->afile); |
| 4724 | return 0; |
| 4725 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4726 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4727 | bp->id = ap->afid; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4728 | bp->bufp = bp->buf; |
| 4729 | bp->ebufp = bp->bufp + i; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4730 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4731 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4732 | ap->afpos++; |
| 4733 | return *bp->bufp++ & 0177; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4734 | } |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4735 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4736 | if (interactive && isatty(ap->afile)) { |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 4737 | /* moved to G: static char filechar_cmdbuf[BUFSIZ]; */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4738 | static int position = 0, size = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4739 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4740 | while (size == 0 || position >= size) { |
Denis Vlasenko | 6e602c4 | 2008-02-02 18:50:50 +0000 | [diff] [blame] | 4741 | size = read_line_input(current_prompt, filechar_cmdbuf, BUFSIZ, line_input_state); |
| 4742 | if (size < 0) /* Error/EOF */ |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 4743 | exit(EXIT_SUCCESS); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4744 | position = 0; |
Denis Vlasenko | 6e602c4 | 2008-02-02 18:50:50 +0000 | [diff] [blame] | 4745 | /* if Ctrl-C, size == 0 and loop will repeat */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4746 | } |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 4747 | c = filechar_cmdbuf[position]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4748 | position++; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4749 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4750 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4751 | #endif |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 4752 | i = nonblock_safe_read(ap->afile, &c, sizeof(c)); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4753 | return i == sizeof(c) ? (c & 0x7f) : (closef(ap->afile), 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | /* |
| 4757 | * Return the characters from a here temp file. |
| 4758 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4759 | static int herechar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4760 | { |
| 4761 | char c; |
| 4762 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 4763 | if (nonblock_safe_read(ap->afile, &c, sizeof(c)) != sizeof(c)) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4764 | close(ap->afile); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4765 | c = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4766 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4767 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | /* |
| 4771 | * Return the characters produced by a process (`...`). |
| 4772 | * Quote them if required, and remove any trailing newline characters. |
| 4773 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4774 | static int gravechar(struct ioarg *ap, struct io *iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4775 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4776 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4777 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4778 | c = qgravechar(ap, iop) & ~QUOTE; |
| 4779 | if (c == '\n') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4780 | c = ' '; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4781 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4784 | static int qgravechar(struct ioarg *ap, struct io *iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4785 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4786 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4787 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4788 | DBGPRINTF3(("QGRAVECHAR: enter, ap=%p, iop=%p\n", ap, iop)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4789 | |
| 4790 | if (iop->xchar) { |
| 4791 | if (iop->nlcount) { |
| 4792 | iop->nlcount--; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4793 | return '\n' | QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4794 | } |
| 4795 | c = iop->xchar; |
| 4796 | iop->xchar = 0; |
| 4797 | } else if ((c = filechar(ap)) == '\n') { |
| 4798 | iop->nlcount = 1; |
| 4799 | while ((c = filechar(ap)) == '\n') |
| 4800 | iop->nlcount++; |
| 4801 | iop->xchar = c; |
| 4802 | if (c == 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4803 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4804 | iop->nlcount--; |
| 4805 | c = '\n'; |
| 4806 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4807 | return c != 0 ? c | QUOTE : 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4808 | } |
| 4809 | |
| 4810 | /* |
| 4811 | * Return a single command (usually the first line) from a file. |
| 4812 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4813 | static int linechar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4814 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4815 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4816 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4817 | c = filechar(ap); |
| 4818 | if (c == '\n') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4819 | if (!multiline) { |
| 4820 | closef(ap->afile); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4821 | ap->afile = -1; /* illegal value */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4822 | } |
| 4823 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4824 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4827 | /* |
| 4828 | * remap fd into Shell's fd space |
| 4829 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4830 | static int remap(int fd) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4831 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4832 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4833 | int map[NOFILE]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4834 | int newfd; |
| 4835 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4836 | DBGPRINTF(("REMAP: fd=%d, global_env.iofd=%d\n", fd, global_env.iofd)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4837 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4838 | if (fd < global_env.iofd) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4839 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4840 | map[i] = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4841 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4842 | do { |
| 4843 | map[fd] = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4844 | newfd = dup(fd); |
| 4845 | fd = newfd; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4846 | } while (fd >= 0 && fd < global_env.iofd); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4847 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4848 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4849 | if (map[i]) |
| 4850 | close(i); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4851 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4852 | if (fd < 0) |
| 4853 | err("too many files open in shell"); |
| 4854 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4855 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4856 | return fd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4859 | static int openpipe(int *pv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4860 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4861 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4862 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4863 | i = pipe(pv); |
| 4864 | if (i < 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4865 | err("can't create pipe - try again"); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4866 | return i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4867 | } |
| 4868 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4869 | static void closepipe(int *pv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4870 | { |
| 4871 | if (pv != NULL) { |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 4872 | close(pv[0]); |
| 4873 | close(pv[1]); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4874 | } |
| 4875 | } |
| 4876 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4877 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4878 | /* -------- here.c -------- */ |
| 4879 | |
| 4880 | /* |
| 4881 | * here documents |
| 4882 | */ |
| 4883 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4884 | static void markhere(char *s, struct ioword *iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4885 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4886 | struct here *h, *lh; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4887 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4888 | DBGPRINTF7(("MARKHERE: enter, s=%p\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4889 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4890 | h = get_space(sizeof(struct here)); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4891 | if (h == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4892 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4893 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4894 | h->h_tag = evalstr(s, DOSUB); |
| 4895 | if (h->h_tag == 0) |
| 4896 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4897 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4898 | h->h_iop = iop; |
| 4899 | iop->io_name = 0; |
| 4900 | h->h_next = NULL; |
| 4901 | if (inhere == 0) |
| 4902 | inhere = h; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4903 | else { |
| 4904 | for (lh = inhere; lh != NULL; lh = lh->h_next) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4905 | if (lh->h_next == 0) { |
| 4906 | lh->h_next = h; |
| 4907 | break; |
| 4908 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4909 | } |
| 4910 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4911 | iop->io_flag |= IOHERE | IOXHERE; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4912 | for (s = h->h_tag; *s; s++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4913 | if (*s & QUOTE) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4914 | iop->io_flag &= ~IOXHERE; |
| 4915 | *s &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4916 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4917 | } |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 4918 | h->h_dosub = ((iop->io_flag & IOXHERE) ? '\0' : '\''); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4921 | static void gethere(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4922 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4923 | struct here *h, *hp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4924 | |
| 4925 | DBGPRINTF7(("GETHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4926 | |
| 4927 | /* Scan here files first leaving inhere list in place */ |
| 4928 | for (hp = h = inhere; h != NULL; hp = h, h = h->h_next) |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 4929 | readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub /* NUL or ' */); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4930 | |
| 4931 | /* Make inhere list active - keep list intact for scraphere */ |
| 4932 | if (hp != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4933 | hp->h_next = acthere; |
| 4934 | acthere = inhere; |
| 4935 | inhere = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4936 | } |
| 4937 | } |
| 4938 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4939 | static void readhere(char **name, char *s, int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4940 | { |
| 4941 | int tf; |
| 4942 | char tname[30] = ".msh_XXXXXX"; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4943 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4944 | jmp_buf ev; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4945 | char myline[LINELIM + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4946 | char *thenext; |
| 4947 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4948 | DBGPRINTF7(("READHERE: enter, name=%p, s=%p\n", name, s)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4949 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4950 | tf = mkstemp(tname); |
| 4951 | if (tf < 0) |
| 4952 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4953 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4954 | *name = strsave(tname, areanum); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4955 | errpt = ev; |
| 4956 | if (newenv(setjmp(errpt)) != 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4957 | unlink(tname); |
| 4958 | else { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4959 | pushio(global_env.iop->argp, (int (*)(struct ioarg *)) global_env.iop->iofn); |
| 4960 | global_env.iobase = global_env.iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4961 | for (;;) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4962 | if (interactive && global_env.iop <= iostack) { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4963 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4964 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4965 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4966 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4967 | #endif |
| 4968 | } |
| 4969 | thenext = myline; |
| 4970 | while ((c = my_getc(ec)) != '\n' && c) { |
| 4971 | if (ec == '\'') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4972 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4973 | if (thenext >= &myline[LINELIM]) { |
| 4974 | c = 0; |
| 4975 | break; |
| 4976 | } |
| 4977 | *thenext++ = c; |
| 4978 | } |
| 4979 | *thenext = 0; |
| 4980 | if (strcmp(s, myline) == 0 || c == 0) |
| 4981 | break; |
| 4982 | *thenext++ = '\n'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4983 | write(tf, myline, (int) (thenext - myline)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4984 | } |
| 4985 | if (c == 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4986 | prs("here document `"); |
| 4987 | prs(s); |
| 4988 | err("' unclosed"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4989 | } |
| 4990 | quitenv(); |
| 4991 | } |
| 4992 | close(tf); |
| 4993 | } |
| 4994 | |
| 4995 | /* |
| 4996 | * open here temp file. |
| 4997 | * if unquoted here, expand here temp file into second temp file. |
| 4998 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4999 | static int herein(char *hname, int xdoll) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5000 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5001 | int hf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5002 | int tf; |
| 5003 | |
| 5004 | #if __GNUC__ |
| 5005 | /* Avoid longjmp clobbering */ |
| 5006 | (void) &tf; |
| 5007 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5008 | if (hname == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 5009 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5010 | |
| 5011 | DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll)); |
| 5012 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5013 | hf = open(hname, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5014 | if (hf < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 5015 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5016 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5017 | if (xdoll) { |
| 5018 | char c; |
| 5019 | char tname[30] = ".msh_XXXXXX"; |
| 5020 | jmp_buf ev; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5021 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5022 | tf = mkstemp(tname); |
| 5023 | if (tf < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 5024 | return -1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 5025 | errpt = ev; |
| 5026 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5027 | PUSHIO(afile, hf, herechar); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 5028 | setbase(global_env.iop); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5029 | while ((c = subgetc(0, 0)) != 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5030 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5031 | write(tf, &c, sizeof c); |
| 5032 | } |
| 5033 | quitenv(); |
| 5034 | } else |
| 5035 | unlink(tname); |
| 5036 | close(tf); |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5037 | tf = open(tname, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5038 | unlink(tname); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 5039 | return tf; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 5040 | } |
| 5041 | return hf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5042 | } |
| 5043 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 5044 | static void scraphere(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5045 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5046 | struct here *h; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5047 | |
| 5048 | DBGPRINTF7(("SCRAPHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5049 | |
| 5050 | for (h = inhere; h != NULL; h = h->h_next) { |
| 5051 | if (h->h_iop && h->h_iop->io_name) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5052 | unlink(h->h_iop->io_name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5053 | } |
| 5054 | inhere = NULL; |
| 5055 | } |
| 5056 | |
| 5057 | /* unlink here temp files before a freearea(area) */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 5058 | static void freehere(int area) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5059 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5060 | struct here *h, *hl; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5061 | |
| 5062 | DBGPRINTF6(("FREEHERE: enter, area=%d\n", area)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5063 | |
| 5064 | hl = NULL; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 5065 | for (h = acthere; h != NULL; h = h->h_next) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5066 | if (getarea((char *) h) >= area) { |
| 5067 | if (h->h_iop->io_name != NULL) |
| 5068 | unlink(h->h_iop->io_name); |
| 5069 | if (hl == NULL) |
| 5070 | acthere = h->h_next; |
| 5071 | else |
| 5072 | hl->h_next = h->h_next; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 5073 | } else { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5074 | hl = h; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 5075 | } |
| 5076 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5080 | /* -------- sh.c -------- */ |
| 5081 | /* |
| 5082 | * shell |
| 5083 | */ |
| 5084 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 5085 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5086 | int msh_main(int argc, char **argv) |
| 5087 | { |
| 5088 | int f; |
| 5089 | char *s; |
| 5090 | int cflag; |
| 5091 | char *name, **ap; |
| 5092 | int (*iof) (struct ioarg *); |
| 5093 | |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 5094 | INIT_G(); |
| 5095 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 5096 | sharedbuf.id = AFID_NOBUF; |
| 5097 | mainbuf.id = AFID_NOBUF; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 5098 | elinep = line + sizeof(line) - 5; |
| 5099 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5100 | #if ENABLE_FEATURE_EDITING |
| 5101 | line_input_state = new_line_input_t(FOR_SHELL); |
| 5102 | #endif |
| 5103 | |
| 5104 | DBGPRINTF(("MSH_MAIN: argc %d, environ %p\n", argc, environ)); |
| 5105 | |
| 5106 | initarea(); |
| 5107 | ap = environ; |
| 5108 | if (ap != NULL) { |
| 5109 | while (*ap) |
| 5110 | assign(*ap++, !COPYV); |
| 5111 | for (ap = environ; *ap;) |
| 5112 | export(lookup(*ap++)); |
| 5113 | } |
| 5114 | closeall(); |
| 5115 | areanum = 1; |
| 5116 | |
| 5117 | shell = lookup("SHELL"); |
| 5118 | if (shell->value == null) |
| 5119 | setval(shell, (char *)DEFAULT_SHELL); |
| 5120 | export(shell); |
| 5121 | |
| 5122 | homedir = lookup("HOME"); |
| 5123 | if (homedir->value == null) |
| 5124 | setval(homedir, "/"); |
| 5125 | export(homedir); |
| 5126 | |
| 5127 | setval(lookup("$"), putn(getpid())); |
| 5128 | |
| 5129 | path = lookup("PATH"); |
| 5130 | if (path->value == null) { |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5131 | /* Can be merged with same string elsewhere in bbox */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5132 | if (geteuid() == 0) |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 5133 | setval(path, bb_default_root_path); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5134 | else |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 5135 | setval(path, bb_default_path); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5136 | } |
| 5137 | export(path); |
| 5138 | |
| 5139 | ifs = lookup("IFS"); |
| 5140 | if (ifs->value == null) |
| 5141 | setval(ifs, " \t\n"); |
| 5142 | |
| 5143 | #ifdef MSHDEBUG |
| 5144 | mshdbg_var = lookup("MSHDEBUG"); |
| 5145 | if (mshdbg_var->value == null) |
| 5146 | setval(mshdbg_var, "0"); |
| 5147 | #endif |
| 5148 | |
| 5149 | prompt = lookup("PS1"); |
| 5150 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 5151 | if (prompt->value == null) |
| 5152 | #endif |
| 5153 | setval(prompt, DEFAULT_USER_PROMPT); |
| 5154 | if (geteuid() == 0) { |
| 5155 | setval(prompt, DEFAULT_ROOT_PROMPT); |
| 5156 | prompt->status &= ~EXPORT; |
| 5157 | } |
| 5158 | cprompt = lookup("PS2"); |
| 5159 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 5160 | if (cprompt->value == null) |
| 5161 | #endif |
| 5162 | setval(cprompt, "> "); |
| 5163 | |
| 5164 | iof = filechar; |
| 5165 | cflag = 0; |
| 5166 | name = *argv++; |
| 5167 | if (--argc >= 1) { |
| 5168 | if (argv[0][0] == '-' && argv[0][1] != '\0') { |
| 5169 | for (s = argv[0] + 1; *s; s++) |
| 5170 | switch (*s) { |
| 5171 | case 'c': |
| 5172 | prompt->status &= ~EXPORT; |
| 5173 | cprompt->status &= ~EXPORT; |
| 5174 | setval(prompt, ""); |
| 5175 | setval(cprompt, ""); |
| 5176 | cflag = 1; |
| 5177 | if (--argc > 0) |
| 5178 | PUSHIO(aword, *++argv, iof = nlchar); |
| 5179 | break; |
| 5180 | |
| 5181 | case 'q': |
| 5182 | qflag = SIG_DFL; |
| 5183 | break; |
| 5184 | |
| 5185 | case 's': |
| 5186 | /* standard input */ |
| 5187 | break; |
| 5188 | |
| 5189 | case 't': |
| 5190 | prompt->status &= ~EXPORT; |
| 5191 | setval(prompt, ""); |
| 5192 | iof = linechar; |
| 5193 | break; |
| 5194 | |
| 5195 | case 'i': |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 5196 | interactive = 1; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5197 | default: |
| 5198 | if (*s >= 'a' && *s <= 'z') |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 5199 | FLAG[(int) *s]++; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5200 | } |
| 5201 | } else { |
| 5202 | argv--; |
| 5203 | argc++; |
| 5204 | } |
| 5205 | |
| 5206 | if (iof == filechar && --argc > 0) { |
| 5207 | setval(prompt, ""); |
| 5208 | setval(cprompt, ""); |
| 5209 | prompt->status &= ~EXPORT; |
| 5210 | cprompt->status &= ~EXPORT; |
| 5211 | |
| 5212 | /* Shell is non-interactive, activate printf-based debug */ |
| 5213 | #ifdef MSHDEBUG |
| 5214 | mshdbg = (int) (((char) (mshdbg_var->value[0])) - '0'); |
| 5215 | if (mshdbg < 0) |
| 5216 | mshdbg = 0; |
| 5217 | #endif |
| 5218 | DBGPRINTF(("MSH_MAIN: calling newfile()\n")); |
| 5219 | |
| 5220 | name = *++argv; |
| 5221 | if (newfile(name)) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 5222 | exit(EXIT_FAILURE); /* Exit on error */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5223 | } |
| 5224 | } |
| 5225 | |
| 5226 | setdash(); |
| 5227 | |
| 5228 | /* This won't be true if PUSHIO has been called, say from newfile() above */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 5229 | if (global_env.iop < iostack) { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5230 | PUSHIO(afile, 0, iof); |
| 5231 | if (isatty(0) && isatty(1) && !cflag) { |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 5232 | interactive = 1; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5233 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 5234 | #ifdef MSHDEBUG |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 5235 | printf("\n\n%s built-in shell (msh with debug)\n", bb_banner); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5236 | #else |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 5237 | printf("\n\n%s built-in shell (msh)\n", bb_banner); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5238 | #endif |
| 5239 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 5240 | #endif |
| 5241 | } |
| 5242 | } |
| 5243 | |
| 5244 | signal(SIGQUIT, qflag); |
| 5245 | if (name && name[0] == '-') { |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 5246 | interactive = 1; |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5247 | f = open(".profile", O_RDONLY); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5248 | if (f >= 0) |
| 5249 | next(remap(f)); |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5250 | f = open("/etc/profile", O_RDONLY); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5251 | if (f >= 0) |
| 5252 | next(remap(f)); |
| 5253 | } |
| 5254 | if (interactive) |
| 5255 | signal(SIGTERM, sig); |
| 5256 | |
| 5257 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 5258 | signal(SIGINT, onintr); |
Denis Vlasenko | fee2d0c | 2008-02-12 10:12:18 +0000 | [diff] [blame] | 5259 | |
| 5260 | /* Handle "msh SCRIPT VAR=val params..." */ |
| 5261 | /* Disabled: bash does not do it! */ |
| 5262 | #if 0 |
| 5263 | argv++; |
| 5264 | /* skip leading args of the form VAR=val */ |
| 5265 | while (*argv && assign(*argv, !COPYV)) { |
| 5266 | argc--; |
| 5267 | argv++; |
| 5268 | } |
| 5269 | argv--; |
| 5270 | #endif |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5271 | dolv = argv; |
| 5272 | dolc = argc; |
| 5273 | dolv[0] = name; |
Denis Vlasenko | fee2d0c | 2008-02-12 10:12:18 +0000 | [diff] [blame] | 5274 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5275 | setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc)); |
| 5276 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 5277 | DBGPRINTF(("MSH_MAIN: begin FOR loop, interactive %d, global_env.iop %p, iostack %p\n", interactive, global_env.iop, iostack)); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5278 | |
| 5279 | for (;;) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 5280 | if (interactive && global_env.iop <= iostack) { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5281 | #if ENABLE_FEATURE_EDITING |
| 5282 | current_prompt = prompt->value; |
| 5283 | #else |
| 5284 | prs(prompt->value); |
| 5285 | #endif |
| 5286 | } |
| 5287 | onecommand(); |
| 5288 | /* Ensure that getenv("PATH") stays current */ |
| 5289 | setenv("PATH", path->value, 1); |
| 5290 | } |
| 5291 | |
| 5292 | DBGPRINTF(("MSH_MAIN: returning.\n")); |
| 5293 | } |
| 5294 | |
| 5295 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5296 | /* |
| 5297 | * Copyright (c) 1987,1997, Prentice Hall |
| 5298 | * All rights reserved. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5299 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5300 | * Redistribution and use of the MINIX operating system in source and |
| 5301 | * binary forms, with or without modification, are permitted provided |
| 5302 | * that the following conditions are met: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5303 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5304 | * Redistributions of source code must retain the above copyright |
| 5305 | * notice, this list of conditions and the following disclaimer. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5306 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5307 | * Redistributions in binary form must reproduce the above |
| 5308 | * copyright notice, this list of conditions and the following |
| 5309 | * disclaimer in the documentation and/or other materials provided |
| 5310 | * with the distribution. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5311 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5312 | * Neither the name of Prentice Hall nor the names of the software |
| 5313 | * authors or contributors may be used to endorse or promote |
| 5314 | * products derived from this software without specific prior |
| 5315 | * written permission. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5316 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5317 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND |
| 5318 | * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 5319 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 5320 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 5321 | * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE |
| 5322 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 5323 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 5324 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 5325 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 5326 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 5327 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 5328 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 5329 | * |
| 5330 | */ |