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 | */ |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 15 | #include <sys/times.h> |
| 16 | #include <setjmp.h> |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 17 | |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 18 | #ifdef STANDALONE |
| 19 | # ifndef _GNU_SOURCE |
| 20 | # define _GNU_SOURCE |
| 21 | # endif |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 22 | # include <sys/types.h> |
| 23 | # include <sys/stat.h> |
| 24 | # include <sys/wait.h> |
| 25 | # include <signal.h> |
| 26 | # include <stdio.h> |
| 27 | # include <stdlib.h> |
| 28 | # include <unistd.h> |
| 29 | # include <string.h> |
| 30 | # include <errno.h> |
| 31 | # include <dirent.h> |
| 32 | # include <fcntl.h> |
| 33 | # include <ctype.h> |
| 34 | # include <assert.h> |
| 35 | # define bb_dev_null "/dev/null" |
| 36 | # define DEFAULT_SHELL "/proc/self/exe" |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 37 | # define bb_banner "busybox standalone" |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 38 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 39 | # define bb_msg_memory_exhausted "memory exhausted" |
| 40 | # define xmalloc(size) malloc(size) |
| 41 | # define msh_main(argc,argv) main(argc,argv) |
| 42 | # define safe_read(fd,buf,count) read(fd,buf,count) |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 43 | # define nonblock_safe_read(fd,buf,count) read(fd,buf,count) |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 44 | # define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1]) |
| 45 | # define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1]) |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 46 | # define NORETURN __attribute__ ((__noreturn__)) |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 47 | static int find_applet_by_name(const char *applet) |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 48 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 49 | return -1; |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 50 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 51 | static char *utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 52 | { |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 53 | unsigned i, out, res; |
| 54 | assert(sizeof(unsigned) == 4); |
| 55 | if (buflen) { |
| 56 | out = 0; |
| 57 | for (i = 1000000000; i; i /= 10) { |
| 58 | res = n / i; |
| 59 | if (res || out || i == 1) { |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 60 | if (!--buflen) break; |
| 61 | out++; |
| 62 | n -= res*i; |
| 63 | *buf++ = '0' + res; |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 66 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 67 | return buf; |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 68 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 69 | static char *itoa_to_buf(int n, char *buf, unsigned buflen) |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 70 | { |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 71 | if (buflen && n < 0) { |
| 72 | n = -n; |
| 73 | *buf++ = '-'; |
| 74 | buflen--; |
| 75 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 76 | return utoa_to_buf((unsigned)n, buf, buflen); |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 77 | } |
| 78 | static char local_buf[12]; |
| 79 | static char *itoa(int n) |
| 80 | { |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 81 | *(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0'; |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 82 | return local_buf; |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 83 | } |
| 84 | #else |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 85 | # include "busybox.h" /* for applet_names */ |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 86 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 87 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 88 | //#define MSHDEBUG 4 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 89 | |
| 90 | #ifdef MSHDEBUG |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 91 | static int mshdbg = MSHDEBUG; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | ed9d621 | 2008-06-09 07:44:19 +0000 | [diff] [blame] | 93 | #define DBGPRINTF(x) if (mshdbg > 0) printf x |
| 94 | #define DBGPRINTF0(x) if (mshdbg > 0) printf x |
| 95 | #define DBGPRINTF1(x) if (mshdbg > 1) printf x |
| 96 | #define DBGPRINTF2(x) if (mshdbg > 2) printf x |
| 97 | #define DBGPRINTF3(x) if (mshdbg > 3) printf x |
| 98 | #define DBGPRINTF4(x) if (mshdbg > 4) printf x |
| 99 | #define DBGPRINTF5(x) if (mshdbg > 5) printf x |
| 100 | #define DBGPRINTF6(x) if (mshdbg > 6) printf x |
| 101 | #define DBGPRINTF7(x) if (mshdbg > 7) printf x |
| 102 | #define DBGPRINTF8(x) if (mshdbg > 8) printf x |
| 103 | #define DBGPRINTF9(x) if (mshdbg > 9) printf x |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 104 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 105 | static int mshdbg_rc = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 106 | |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 107 | #define RCPRINTF(x) if (mshdbg_rc) printf x |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 108 | |
| 109 | #else |
| 110 | |
| 111 | #define DBGPRINTF(x) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 112 | #define DBGPRINTF0(x) ((void)0) |
| 113 | #define DBGPRINTF1(x) ((void)0) |
| 114 | #define DBGPRINTF2(x) ((void)0) |
| 115 | #define DBGPRINTF3(x) ((void)0) |
| 116 | #define DBGPRINTF4(x) ((void)0) |
| 117 | #define DBGPRINTF5(x) ((void)0) |
| 118 | #define DBGPRINTF6(x) ((void)0) |
| 119 | #define DBGPRINTF7(x) ((void)0) |
| 120 | #define DBGPRINTF8(x) ((void)0) |
| 121 | #define DBGPRINTF9(x) ((void)0) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 123 | #define RCPRINTF(x) ((void)0) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 124 | |
Denis Vlasenko | ed9d621 | 2008-06-09 07:44:19 +0000 | [diff] [blame] | 125 | #endif /* MSHDEBUG */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 126 | |
| 127 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 128 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 2a13175 | 2006-06-06 06:26:12 +0000 | [diff] [blame] | 129 | # define DEFAULT_ROOT_PROMPT "\\u:\\w> " |
| 130 | # define DEFAULT_USER_PROMPT "\\u:\\w$ " |
| 131 | #else |
| 132 | # define DEFAULT_ROOT_PROMPT "# " |
| 133 | # define DEFAULT_USER_PROMPT "$ " |
| 134 | #endif |
| 135 | |
| 136 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 137 | /* -------- sh.h -------- */ |
| 138 | /* |
| 139 | * shell |
| 140 | */ |
| 141 | |
Denis Vlasenko | ed9d621 | 2008-06-09 07:44:19 +0000 | [diff] [blame] | 142 | #define LINELIM 2100 |
| 143 | #define NPUSH 8 /* limit to input nesting */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 144 | |
Eric Andersen | 392947c | 2002-12-11 07:42:46 +0000 | [diff] [blame] | 145 | #undef NOFILE |
Denis Vlasenko | ed9d621 | 2008-06-09 07:44:19 +0000 | [diff] [blame] | 146 | #define NOFILE 20 /* Number of open files */ |
| 147 | #define NUFILE 10 /* Number of user-accessible files */ |
| 148 | #define FDBASE 10 /* First file usable by Shell */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * values returned by wait |
| 152 | */ |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 153 | #define WAITSIG(s) ((s) & 0177) |
| 154 | #define WAITVAL(s) (((s) >> 8) & 0377) |
| 155 | #define WAITCORE(s) (((s) & 0200) != 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 156 | |
| 157 | /* |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 158 | * library and system definitions |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 159 | */ |
Denis Vlasenko | ed9d621 | 2008-06-09 07:44:19 +0000 | [diff] [blame] | 160 | typedef void xint; /* base type of jmp_buf, for not broken compilers */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * shell components |
| 164 | */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 165 | #define NOBLOCK ((struct op *)NULL) |
| 166 | #define NOWORD ((char *)NULL) |
| 167 | #define NOWORDS ((char **)NULL) |
| 168 | #define NOPIPE ((int *)NULL) |
| 169 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 170 | /* |
| 171 | * redirection |
| 172 | */ |
| 173 | struct ioword { |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 174 | smallint io_flag; /* action (below) */ |
| 175 | int io_fd; /* fd affected */ |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 176 | char *io_name; /* file name */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 179 | #define IOREAD 1 /* < */ |
| 180 | #define IOHERE 2 /* << (here file) */ |
| 181 | #define IOWRITE 4 /* > */ |
| 182 | #define IOCAT 8 /* >> */ |
| 183 | #define IOXHERE 16 /* ${}, ` in << */ |
| 184 | #define IODUP 32 /* >&digit */ |
| 185 | #define IOCLOSE 64 /* >&- */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 186 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 187 | #define IODEFAULT (-1) /* "default" IO fd */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 188 | |
| 189 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 190 | /* |
| 191 | * Description of a command or an operation on commands. |
| 192 | * Might eventually use a union. |
| 193 | */ |
| 194 | struct op { |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 195 | smallint op_type; /* operation type, see Txxxx below */ |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 196 | char **op_words; /* arguments to a command */ |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 197 | struct ioword **ioact; /* IO actions (eg, < > >>) */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 198 | struct op *left; |
| 199 | struct op *right; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 200 | char *str; /* identifier for case and for */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 203 | #define TCOM 1 /* command */ |
| 204 | #define TPAREN 2 /* (c-list) */ |
| 205 | #define TPIPE 3 /* a | b */ |
| 206 | #define TLIST 4 /* a [&;] b */ |
| 207 | #define TOR 5 /* || */ |
| 208 | #define TAND 6 /* && */ |
| 209 | #define TFOR 7 |
| 210 | #define TDO 8 |
| 211 | #define TCASE 9 |
| 212 | #define TIF 10 |
| 213 | #define TWHILE 11 |
| 214 | #define TUNTIL 12 |
| 215 | #define TELIF 13 |
| 216 | #define TPAT 14 /* pattern in case */ |
| 217 | #define TBRACE 15 /* {c-list} */ |
| 218 | #define TASYNC 16 /* c & */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 219 | /* Added to support "." file expansion */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 220 | #define TDOT 17 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 221 | |
| 222 | /* Strings for names to make debug easier */ |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 223 | #ifdef MSHDEBUG |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 224 | static const char *const T_CMD_NAMES[] = { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 225 | "PLACEHOLDER", |
| 226 | "TCOM", |
| 227 | "TPAREN", |
| 228 | "TPIPE", |
| 229 | "TLIST", |
| 230 | "TOR", |
| 231 | "TAND", |
| 232 | "TFOR", |
| 233 | "TDO", |
| 234 | "TCASE", |
| 235 | "TIF", |
| 236 | "TWHILE", |
| 237 | "TUNTIL", |
| 238 | "TELIF", |
| 239 | "TPAT", |
| 240 | "TBRACE", |
| 241 | "TASYNC", |
| 242 | "TDOT", |
| 243 | }; |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 244 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 245 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 246 | #define AREASIZE (90000) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 247 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 248 | /* |
| 249 | * flags to control evaluation of words |
| 250 | */ |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 251 | #define DOSUB 1 /* interpret $, `, and quotes */ |
| 252 | #define DOBLANK 2 /* perform blank interpretation */ |
| 253 | #define DOGLOB 4 /* interpret [?* */ |
| 254 | #define DOKEY 8 /* move words with `=' to 2nd arg. list */ |
| 255 | #define DOTRIM 16 /* trim resulting string */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 256 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 257 | #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 258 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 259 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 260 | struct brkcon { |
| 261 | jmp_buf brkpt; |
| 262 | struct brkcon *nextlev; |
| 263 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 264 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 265 | |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 266 | static smallint trapset; /* trap pending (signal number) */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 267 | |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 268 | static smallint yynerrs; /* yacc (flag) */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 269 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 270 | /* moved to G: static char line[LINELIM]; */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 271 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 272 | #if ENABLE_FEATURE_EDITING |
| 273 | static char *current_prompt; |
| 274 | static line_input_t *line_input_state; |
| 275 | #endif |
| 276 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 277 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 278 | /* |
| 279 | * other functions |
| 280 | */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 281 | static const char *rexecve(char *c, char **v, char **envp); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 282 | static char *evalstr(char *cp, int f); |
| 283 | static char *putn(int n); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 284 | static char *unquote(char *as); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 285 | static int rlookup(char *n); |
| 286 | static struct wdblock *glob(char *cp, struct wdblock *wb); |
| 287 | static int my_getc(int ec); |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 288 | static int subgetc(char ec, int quoted); |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 289 | static char **makenv(int all, struct wdblock *wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 290 | static char **eval(char **ap, int f); |
| 291 | static int setstatus(int s); |
| 292 | static int waitfor(int lastpid, int canintr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 293 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 294 | static void onintr(int s); /* SIGINT handler */ |
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 int newenv(int f); |
| 297 | static void quitenv(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 298 | static void next(int f); |
| 299 | static void setdash(void); |
| 300 | static void onecommand(void); |
| 301 | static void runtrap(int i); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 302 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 303 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 304 | /* -------- area stuff -------- */ |
| 305 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 306 | #define REGSIZE sizeof(struct region) |
| 307 | #define GROWBY (256) |
| 308 | /* #define SHRINKBY (64) */ |
| 309 | #undef SHRINKBY |
| 310 | #define FREE (32767) |
| 311 | #define BUSY (0) |
| 312 | #define ALIGN (sizeof(int)-1) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 313 | |
| 314 | |
| 315 | struct region { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 316 | struct region *next; |
| 317 | int area; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 321 | /* -------- grammar stuff -------- */ |
| 322 | typedef union { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 323 | char *cp; |
| 324 | char **wp; |
| 325 | int i; |
| 326 | struct op *o; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 327 | } YYSTYPE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 328 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 329 | #define WORD 256 |
| 330 | #define LOGAND 257 |
| 331 | #define LOGOR 258 |
| 332 | #define BREAK 259 |
| 333 | #define IF 260 |
| 334 | #define THEN 261 |
| 335 | #define ELSE 262 |
| 336 | #define ELIF 263 |
| 337 | #define FI 264 |
| 338 | #define CASE 265 |
| 339 | #define ESAC 266 |
| 340 | #define FOR 267 |
| 341 | #define WHILE 268 |
| 342 | #define UNTIL 269 |
| 343 | #define DO 270 |
| 344 | #define DONE 271 |
| 345 | #define IN 272 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 346 | /* Added for "." file expansion */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 347 | #define DOT 273 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 348 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 349 | #define YYERRCODE 300 |
| 350 | |
| 351 | /* flags to yylex */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 352 | #define CONTIN 01 /* skip new lines to complete command */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 353 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 354 | static struct op *pipeline(int cf); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 355 | static struct op *andor(void); |
| 356 | static struct op *c_list(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 357 | static int synio(int cf); |
| 358 | static void musthave(int c, int cf); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 359 | static struct op *simple(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 360 | static struct op *nested(int type, int mark); |
| 361 | static struct op *command(int cf); |
| 362 | static struct op *dogroup(int onlydone); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 363 | static struct op *thenpart(void); |
| 364 | static struct op *elsepart(void); |
| 365 | static struct op *caselist(void); |
| 366 | static struct op *casepart(void); |
| 367 | static char **pattern(void); |
| 368 | static char **wordlist(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 369 | static struct op *list(struct op *t1, struct op *t2); |
| 370 | 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] | 371 | static struct op *newtp(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 372 | static struct op *namelist(struct op *t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 373 | static char **copyw(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 374 | static void word(char *cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 375 | static struct ioword **copyio(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 376 | static struct ioword *io(int u, int f, char *cp); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 377 | static int yylex(int cf); |
| 378 | static int collect(int c, int c1); |
| 379 | static int dual(int c); |
| 380 | static void diag(int ec); |
| 381 | static char *tree(unsigned size); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 382 | |
| 383 | /* -------- var.h -------- */ |
| 384 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 385 | struct var { |
| 386 | char *value; |
| 387 | char *name; |
| 388 | struct var *next; |
| 389 | char status; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 390 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 391 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 392 | #define COPYV 1 /* flag to setval, suggesting copy */ |
| 393 | #define RONLY 01 /* variable is read-only */ |
| 394 | #define EXPORT 02 /* variable is to be exported */ |
| 395 | #define GETCELL 04 /* name & value space was got with getcell */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 396 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 397 | static int yyparse(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 398 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 399 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 400 | /* -------- io.h -------- */ |
| 401 | /* io buffer */ |
| 402 | struct iobuf { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 403 | unsigned id; /* buffer id */ |
| 404 | char buf[512]; /* buffer */ |
| 405 | char *bufp; /* pointer into buffer */ |
| 406 | char *ebufp; /* pointer to end of buffer */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 407 | }; |
| 408 | |
| 409 | /* possible arguments to an IO function */ |
| 410 | struct ioarg { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 411 | const char *aword; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 412 | char **awordlist; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 413 | int afile; /* file descriptor */ |
| 414 | unsigned afid; /* buffer id */ |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 415 | off_t afpos; /* file position */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 416 | struct iobuf *afbuf; /* buffer for this file */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 417 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 418 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 419 | /* an input generator's state */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 420 | struct io { |
| 421 | int (*iofn) (struct ioarg *, struct io *); |
| 422 | struct ioarg *argp; |
| 423 | int peekc; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 424 | char prev; /* previous character read by readc() */ |
| 425 | char nlcount; /* for `'s */ |
| 426 | char xchar; /* for `'s */ |
| 427 | char task; /* reason for pushed IO */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 428 | }; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 429 | /* ->task: */ |
| 430 | #define XOTHER 0 /* none of the below */ |
| 431 | #define XDOLL 1 /* expanding ${} */ |
| 432 | #define XGRAVE 2 /* expanding `'s */ |
| 433 | #define XIO 3 /* file IO */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 434 | |
| 435 | |
| 436 | /* |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 437 | * input generators for IO structure |
| 438 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 439 | static int nlchar(struct ioarg *ap); |
| 440 | static int strchar(struct ioarg *ap); |
| 441 | static int qstrchar(struct ioarg *ap); |
| 442 | static int filechar(struct ioarg *ap); |
| 443 | static int herechar(struct ioarg *ap); |
| 444 | static int linechar(struct ioarg *ap); |
| 445 | static int gravechar(struct ioarg *ap, struct io *iop); |
| 446 | static int qgravechar(struct ioarg *ap, struct io *iop); |
| 447 | static int dolchar(struct ioarg *ap); |
| 448 | static int wdchar(struct ioarg *ap); |
| 449 | static void scraphere(void); |
| 450 | static void freehere(int area); |
| 451 | static void gethere(void); |
| 452 | static void markhere(char *s, struct ioword *iop); |
| 453 | static int herein(char *hname, int xdoll); |
| 454 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 455 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 456 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 457 | static int eofc(void); |
| 458 | static int readc(void); |
| 459 | static void unget(int c); |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 460 | static void ioecho(char c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 461 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 462 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 463 | /* |
| 464 | * IO control |
| 465 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 466 | static void pushio(struct ioarg *argp, int (*f) (struct ioarg *)); |
Denys Vlasenko | 083e172 | 2010-01-28 12:30:24 +0100 | [diff] [blame] | 467 | #define PUSHIO(what,arg,gen) ((temparg.what = (arg)), pushio(&temparg, (gen))) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 468 | static int remap(int fd); |
| 469 | static int openpipe(int *pv); |
| 470 | static void closepipe(int *pv); |
| 471 | static struct io *setbase(struct io *ip); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 472 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 473 | /* -------- word.h -------- */ |
| 474 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 475 | #define NSTART 16 /* default number of words to allow for initially */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 476 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 477 | struct wdblock { |
| 478 | short w_bsize; |
| 479 | short w_nword; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 480 | /* bounds are arbitrary */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 481 | char *w_words[1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 482 | }; |
| 483 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 484 | static struct wdblock *addword(char *wd, struct wdblock *wb); |
| 485 | static struct wdblock *newword(int nw); |
| 486 | static char **getwords(struct wdblock *wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 487 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 488 | /* -------- misc stuff -------- */ |
| 489 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 490 | static int dolabel(struct op *t, char **args); |
| 491 | static int dohelp(struct op *t, char **args); |
| 492 | static int dochdir(struct op *t, char **args); |
| 493 | static int doshift(struct op *t, char **args); |
| 494 | static int dologin(struct op *t, char **args); |
| 495 | static int doumask(struct op *t, char **args); |
| 496 | static int doexec(struct op *t, char **args); |
| 497 | static int dodot(struct op *t, char **args); |
| 498 | static int dowait(struct op *t, char **args); |
| 499 | static int doread(struct op *t, char **args); |
| 500 | static int doeval(struct op *t, char **args); |
| 501 | static int dotrap(struct op *t, char **args); |
| 502 | static int dobreak(struct op *t, char **args); |
| 503 | static int doexit(struct op *t, char **args); |
| 504 | static int doexport(struct op *t, char **args); |
| 505 | static int doreadonly(struct op *t, char **args); |
| 506 | static int doset(struct op *t, char **args); |
| 507 | static int dotimes(struct op *t, char **args); |
| 508 | static int docontinue(struct op *t, char **args); |
| 509 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 510 | static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp); |
| 511 | 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] | 512 | static int iosetup(struct ioword *iop, int pipein, int pipeout); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 513 | static void brkset(struct brkcon *bc); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 514 | static int getsig(char *s); |
| 515 | static void setsig(int n, sighandler_t f); |
| 516 | static int getn(char *as); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 517 | static int brkcontin(char *cp, int val); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 518 | static void rdexp(char **wp, void (*f) (struct var *), int key); |
| 519 | static void badid(char *s); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 520 | static void varput(char *s, int out); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 521 | static int expand(const char *cp, struct wdblock **wbp, int f); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 522 | static char *blank(int f); |
| 523 | static int dollar(int quoted); |
| 524 | static int grave(int quoted); |
| 525 | static void globname(char *we, char *pp); |
| 526 | static char *generate(char *start1, char *end1, char *middle, char *end); |
| 527 | static int anyspcl(struct wdblock *wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 528 | static void readhere(char **name, char *s, int ec); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 529 | static int xxchar(struct ioarg *ap); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 530 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 531 | struct here { |
| 532 | char *h_tag; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 533 | char h_dosub; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 534 | struct ioword *h_iop; |
| 535 | struct here *h_next; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 536 | }; |
| 537 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 538 | static const char *const signame[] = { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 539 | "Signal 0", |
| 540 | "Hangup", |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 541 | NULL, /* interrupt */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 542 | "Quit", |
| 543 | "Illegal instruction", |
| 544 | "Trace/BPT trap", |
| 545 | "Abort", |
| 546 | "Bus error", |
| 547 | "Floating Point Exception", |
| 548 | "Killed", |
| 549 | "SIGUSR1", |
| 550 | "SIGSEGV", |
| 551 | "SIGUSR2", |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 552 | NULL, /* broken pipe */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 553 | "Alarm clock", |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 554 | "Terminated" |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 555 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 556 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 557 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 558 | typedef int (*builtin_func_ptr)(struct op *, char **); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 559 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 560 | struct builtincmd { |
| 561 | const char *name; |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 562 | builtin_func_ptr builtinfunc; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 563 | }; |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 564 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 565 | static const struct builtincmd builtincmds[] = { |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 566 | { "." , dodot }, |
| 567 | { ":" , dolabel }, |
| 568 | { "break" , dobreak }, |
| 569 | { "cd" , dochdir }, |
| 570 | { "continue", docontinue }, |
| 571 | { "eval" , doeval }, |
| 572 | { "exec" , doexec }, |
| 573 | { "exit" , doexit }, |
| 574 | { "export" , doexport }, |
| 575 | { "help" , dohelp }, |
| 576 | { "login" , dologin }, |
| 577 | { "newgrp" , dologin }, |
| 578 | { "read" , doread }, |
| 579 | { "readonly", doreadonly }, |
| 580 | { "set" , doset }, |
| 581 | { "shift" , doshift }, |
| 582 | { "times" , dotimes }, |
| 583 | { "trap" , dotrap }, |
| 584 | { "umask" , doumask }, |
| 585 | { "wait" , dowait }, |
| 586 | { NULL , NULL }, |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 587 | }; |
| 588 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 589 | static struct op *dowholefile(int /*, int*/); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 590 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 591 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 592 | /* Globals */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 593 | static char **dolv; |
| 594 | static int dolc; |
Denis Vlasenko | 447bd66 | 2008-05-30 22:28:32 +0000 | [diff] [blame] | 595 | static uint8_t exstat; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 596 | static smallint gflg; /* (seems to be a parse error indicator) */ |
| 597 | static smallint interactive; /* Is this an interactive shell */ |
| 598 | static smallint execflg; |
| 599 | static smallint isbreak; /* "break" statement was seen */ |
| 600 | static int multiline; /* '\n' changed to ';' (counter) */ |
| 601 | static struct op *outtree; /* result from parser */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 602 | static xint *failpt; |
| 603 | static xint *errpt; |
| 604 | static struct brkcon *brklist; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 605 | static struct wdblock *wdlist; |
| 606 | static struct wdblock *iolist; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 607 | |
| 608 | #ifdef MSHDEBUG |
| 609 | static struct var *mshdbg_var; |
| 610 | #endif |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 611 | static struct var *vlist; /* dictionary */ |
| 612 | static struct var *homedir; /* home directory */ |
| 613 | static struct var *prompt; /* main prompt */ |
| 614 | static struct var *cprompt; /* continuation prompt */ |
| 615 | static struct var *path; /* search path for commands */ |
| 616 | static struct var *shell; /* shell to interpret command files */ |
| 617 | static struct var *ifs; /* field separators */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 618 | |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 619 | static int areanum; /* current allocation area */ |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 620 | static smallint intr; /* interrupt pending (bool) */ |
| 621 | static smallint heedint = 1; /* heed interrupt signals (bool) */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 622 | static int inparse; |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 623 | static char *null = (char*)""; /* null value for variable */ |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 624 | static void (*qflag)(int) = SIG_IGN; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 625 | static int startl; |
| 626 | static int peeksym; |
| 627 | static int nlseen; |
| 628 | static int iounit = IODEFAULT; |
| 629 | static YYSTYPE yylval; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 630 | static char *elinep; /* done in main(): = line + sizeof(line) - 5 */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 631 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 632 | static struct here *inhere; /* list of hear docs while parsing */ |
| 633 | static struct here *acthere; /* list of active here documents */ |
| 634 | static struct region *areabot; /* bottom of area */ |
| 635 | static struct region *areatop; /* top of area */ |
| 636 | static struct region *areanxt; /* starting point of scan */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 637 | static void *brktop; |
| 638 | static void *brkaddr; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 639 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 640 | #define AFID_NOBUF (~0) |
| 641 | #define AFID_ID 0 |
| 642 | |
| 643 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 644 | /* |
| 645 | * parsing & execution environment |
| 646 | */ |
| 647 | struct env { |
| 648 | char *linep; |
| 649 | struct io *iobase; |
| 650 | struct io *iop; |
| 651 | xint *errpt; /* void * */ |
| 652 | int iofd; |
| 653 | struct env *oenv; |
| 654 | }; |
| 655 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 656 | |
| 657 | struct globals { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 658 | struct env global_env; |
| 659 | struct ioarg temparg; // = { .afid = AFID_NOBUF }; /* temporary for PUSHIO */ |
| 660 | unsigned bufid; // = AFID_ID; /* buffer id counter */ |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 661 | char ourtrap[_NSIG + 1]; |
| 662 | char *trap[_NSIG + 1]; |
| 663 | struct iobuf sharedbuf; /* in main(): set to { AFID_NOBUF } */ |
| 664 | struct iobuf mainbuf; /* in main(): set to { AFID_NOBUF } */ |
| 665 | struct ioarg ioargstack[NPUSH]; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 666 | /* |
| 667 | * flags: |
| 668 | * -e: quit on error |
| 669 | * -k: look for name=value everywhere on command line |
| 670 | * -n: no execution |
| 671 | * -t: exit after reading and executing one command |
| 672 | * -v: echo as read |
| 673 | * -x: trace |
| 674 | * -u: unset variables net diagnostic |
| 675 | */ |
| 676 | char flags['z' - 'a' + 1]; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 677 | char filechar_cmdbuf[BUFSIZ]; |
| 678 | char line[LINELIM]; |
| 679 | char child_cmd[LINELIM]; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 680 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 681 | struct io iostack[NPUSH]; |
| 682 | |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 683 | char grave__var_name[LINELIM]; |
| 684 | char grave__alt_value[LINELIM]; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 685 | }; |
| 686 | |
| 687 | #define G (*ptr_to_globals) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 688 | #define global_env (G.global_env ) |
| 689 | #define temparg (G.temparg ) |
| 690 | #define bufid (G.bufid ) |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 691 | #define ourtrap (G.ourtrap ) |
| 692 | #define trap (G.trap ) |
| 693 | #define sharedbuf (G.sharedbuf ) |
| 694 | #define mainbuf (G.mainbuf ) |
| 695 | #define ioargstack (G.ioargstack ) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 696 | /* this looks weird, but is OK ... we index FLAG with 'a'...'z' */ |
| 697 | #define FLAG (G.flags - 'a' ) |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 698 | #define filechar_cmdbuf (G.filechar_cmdbuf) |
| 699 | #define line (G.line ) |
| 700 | #define child_cmd (G.child_cmd ) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 701 | #define iostack (G.iostack ) |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 702 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 703 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 704 | global_env.linep = line; \ |
| 705 | global_env.iobase = iostack; \ |
| 706 | global_env.iop = iostack - 1; \ |
| 707 | global_env.iofd = FDBASE; \ |
| 708 | temparg.afid = AFID_NOBUF; \ |
| 709 | bufid = AFID_ID; \ |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 710 | } while (0) |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 711 | |
| 712 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 713 | /* in substitution */ |
| 714 | #define INSUB() (global_env.iop->task == XGRAVE || global_env.iop->task == XDOLL) |
| 715 | |
| 716 | #define RUN(what, arg, gen) ((temparg.what = (arg)), run(&temparg, (gen))) |
| 717 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 718 | #ifdef MSHDEBUG |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 719 | static void print_tree(struct op *head) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 720 | { |
| 721 | if (head == NULL) { |
| 722 | DBGPRINTF(("PRINT_TREE: no tree\n")); |
| 723 | return; |
| 724 | } |
| 725 | |
Denis Vlasenko | ed9d621 | 2008-06-09 07:44:19 +0000 | [diff] [blame] | 726 | DBGPRINTF(("NODE: %p, left %p, right %p\n", head, head->left, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 727 | head->right)); |
| 728 | |
| 729 | if (head->left) |
| 730 | print_tree(head->left); |
| 731 | |
| 732 | if (head->right) |
| 733 | print_tree(head->right); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 734 | } |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 735 | #endif /* MSHDEBUG */ |
| 736 | |
| 737 | |
| 738 | /* |
| 739 | * IO functions |
| 740 | */ |
| 741 | static void prs(const char *s) |
| 742 | { |
| 743 | if (*s) |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 744 | xwrite_str(STDERR_FILENO, s); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static void prn(unsigned u) |
| 748 | { |
| 749 | prs(itoa(u)); |
| 750 | } |
| 751 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 752 | static void echo(char **wp) |
| 753 | { |
| 754 | int i; |
| 755 | |
| 756 | prs("+"); |
| 757 | for (i = 0; wp[i]; i++) { |
| 758 | if (i) |
| 759 | prs(" "); |
| 760 | prs(wp[i]); |
| 761 | } |
| 762 | prs("\n"); |
| 763 | } |
| 764 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 765 | static void closef(int i) |
| 766 | { |
| 767 | if (i > 2) |
| 768 | close(i); |
| 769 | } |
| 770 | |
| 771 | static void closeall(void) |
| 772 | { |
| 773 | int u; |
| 774 | |
| 775 | for (u = NUFILE; u < NOFILE;) |
| 776 | close(u++); |
| 777 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 778 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 779 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 780 | /* fail but return to process next command */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 781 | static void fail(void) NORETURN; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 782 | static void fail(void) |
| 783 | { |
| 784 | longjmp(failpt, 1); |
| 785 | /* NOTREACHED */ |
| 786 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 787 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 788 | /* abort shell (or fail in subshell) */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 789 | static void leave(void) NORETURN; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 790 | static void leave(void) |
| 791 | { |
| 792 | DBGPRINTF(("LEAVE: leave called!\n")); |
| 793 | |
| 794 | if (execflg) |
| 795 | fail(); |
| 796 | scraphere(); |
| 797 | freehere(1); |
| 798 | runtrap(0); |
| 799 | _exit(exstat); |
| 800 | /* NOTREACHED */ |
| 801 | } |
| 802 | |
| 803 | static void warn(const char *s) |
| 804 | { |
| 805 | if (*s) { |
| 806 | prs(s); |
Denis Vlasenko | 447bd66 | 2008-05-30 22:28:32 +0000 | [diff] [blame] | 807 | if (!exstat) |
| 808 | exstat = 255; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 809 | } |
| 810 | prs("\n"); |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 811 | if (FLAG['e']) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 812 | leave(); |
| 813 | } |
| 814 | |
| 815 | static void err(const char *s) |
| 816 | { |
| 817 | warn(s); |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 818 | if (FLAG['n']) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 819 | return; |
| 820 | if (!interactive) |
| 821 | leave(); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 822 | if (global_env.errpt) |
| 823 | longjmp(global_env.errpt, 1); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 824 | closeall(); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 825 | global_env.iop = global_env.iobase = iostack; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 828 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 829 | /* -------- area.c -------- */ |
| 830 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 831 | /* |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 832 | * All memory between (char *)areabot and (char *)(areatop+1) is |
| 833 | * exclusively administered by the area management routines. |
| 834 | * It is assumed that sbrk() and brk() manipulate the high end. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 835 | */ |
| 836 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 837 | #define sbrk(X) ({ \ |
| 838 | void * __q = (void *)-1; \ |
| 839 | if (brkaddr + (int)(X) < brktop) { \ |
| 840 | __q = brkaddr; \ |
| 841 | brkaddr += (int)(X); \ |
| 842 | } \ |
| 843 | __q; \ |
| 844 | }) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 845 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 846 | static void initarea(void) |
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 | brkaddr = xmalloc(AREASIZE); |
| 849 | brktop = brkaddr + AREASIZE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 850 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 851 | while ((long) sbrk(0) & ALIGN) |
| 852 | sbrk(1); |
| 853 | areabot = (struct region *) sbrk(REGSIZE); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 854 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 855 | areabot->next = areabot; |
| 856 | areabot->area = BUSY; |
| 857 | areatop = areabot; |
| 858 | areanxt = areabot; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 861 | static char *getcell(unsigned nbytes) |
| 862 | { |
| 863 | int nregio; |
| 864 | struct region *p, *q; |
| 865 | int i; |
| 866 | |
| 867 | if (nbytes == 0) { |
| 868 | puts("getcell(0)"); |
| 869 | abort(); |
| 870 | } |
| 871 | /* silly and defeats the algorithm */ |
| 872 | /* |
| 873 | * round upwards and add administration area |
| 874 | */ |
| 875 | nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1; |
| 876 | p = areanxt; |
| 877 | for (;;) { |
| 878 | if (p->area > areanum) { |
| 879 | /* |
| 880 | * merge free cells |
| 881 | */ |
| 882 | while ((q = p->next)->area > areanum && q != areanxt) |
| 883 | p->next = q->next; |
| 884 | /* |
| 885 | * exit loop if cell big enough |
| 886 | */ |
| 887 | if (q >= p + nregio) |
| 888 | goto found; |
| 889 | } |
| 890 | p = p->next; |
| 891 | if (p == areanxt) |
| 892 | break; |
| 893 | } |
| 894 | i = nregio >= GROWBY ? nregio : GROWBY; |
| 895 | p = (struct region *) sbrk(i * REGSIZE); |
| 896 | if (p == (struct region *) -1) |
| 897 | return NULL; |
| 898 | p--; |
| 899 | if (p != areatop) { |
| 900 | puts("not contig"); |
| 901 | abort(); /* allocated areas are contiguous */ |
| 902 | } |
| 903 | q = p + i; |
| 904 | p->next = q; |
| 905 | p->area = FREE; |
| 906 | q->next = areabot; |
| 907 | q->area = BUSY; |
| 908 | areatop = q; |
| 909 | found: |
| 910 | /* |
| 911 | * we found a FREE area big enough, pointed to by 'p', and up to 'q' |
| 912 | */ |
| 913 | areanxt = p + nregio; |
| 914 | if (areanxt < q) { |
| 915 | /* |
| 916 | * split into requested area and rest |
| 917 | */ |
| 918 | if (areanxt + 1 > q) { |
| 919 | puts("OOM"); |
| 920 | abort(); /* insufficient space left for admin */ |
| 921 | } |
| 922 | areanxt->next = q; |
| 923 | areanxt->area = FREE; |
| 924 | p->next = areanxt; |
| 925 | } |
| 926 | p->area = areanum; |
| 927 | return (char *) (p + 1); |
| 928 | } |
| 929 | |
| 930 | static void freecell(char *cp) |
| 931 | { |
| 932 | struct region *p; |
| 933 | |
| 934 | p = (struct region *) cp; |
| 935 | if (p != NULL) { |
| 936 | p--; |
| 937 | if (p < areanxt) |
| 938 | areanxt = p; |
| 939 | p->area = FREE; |
| 940 | } |
| 941 | } |
| 942 | #define DELETE(obj) freecell((char *)obj) |
| 943 | |
| 944 | static void freearea(int a) |
| 945 | { |
| 946 | struct region *p, *top; |
| 947 | |
| 948 | top = areatop; |
| 949 | for (p = areabot; p != top; p = p->next) |
| 950 | if (p->area >= a) |
| 951 | p->area = FREE; |
| 952 | } |
| 953 | |
| 954 | static void setarea(char *cp, int a) |
| 955 | { |
| 956 | struct region *p; |
| 957 | |
| 958 | p = (struct region *) cp; |
| 959 | if (p != NULL) |
| 960 | (p - 1)->area = a; |
| 961 | } |
| 962 | |
| 963 | static int getarea(char *cp) |
| 964 | { |
| 965 | return ((struct region *) cp - 1)->area; |
| 966 | } |
| 967 | |
| 968 | static void garbage(void) |
| 969 | { |
| 970 | struct region *p, *q, *top; |
| 971 | |
| 972 | top = areatop; |
| 973 | for (p = areabot; p != top; p = p->next) { |
| 974 | if (p->area > areanum) { |
| 975 | while ((q = p->next)->area > areanum) |
| 976 | p->next = q->next; |
| 977 | areanxt = p; |
| 978 | } |
| 979 | } |
| 980 | #ifdef SHRINKBY |
| 981 | if (areatop >= q + SHRINKBY && q->area > areanum) { |
| 982 | brk((char *) (q + 1)); |
| 983 | q->next = areabot; |
| 984 | q->area = BUSY; |
| 985 | areatop = q; |
| 986 | } |
| 987 | #endif |
| 988 | } |
| 989 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 990 | static void *get_space(int n) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 991 | { |
| 992 | char *cp; |
| 993 | |
| 994 | cp = getcell(n); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 995 | if (cp == NULL) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 996 | err("out of string space"); |
| 997 | return cp; |
| 998 | } |
| 999 | |
| 1000 | static char *strsave(const char *s, int a) |
| 1001 | { |
| 1002 | char *cp; |
| 1003 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1004 | cp = get_space(strlen(s) + 1); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1005 | if (cp == NULL) { |
| 1006 | // FIXME: I highly doubt this is good. |
| 1007 | return (char*)""; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1008 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1009 | setarea(cp, a); |
| 1010 | strcpy(cp, s); |
| 1011 | return cp; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1014 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1015 | /* -------- var.c -------- */ |
| 1016 | |
| 1017 | static int eqname(const char *n1, const char *n2) |
| 1018 | { |
| 1019 | for (; *n1 != '=' && *n1 != '\0'; n1++) |
| 1020 | if (*n2++ != *n1) |
| 1021 | return 0; |
| 1022 | return *n2 == '\0' || *n2 == '='; |
| 1023 | } |
| 1024 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1025 | static const char *findeq(const char *cp) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1026 | { |
| 1027 | while (*cp != '\0' && *cp != '=') |
| 1028 | cp++; |
| 1029 | return cp; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * Find the given name in the dictionary |
| 1034 | * and return its value. If the name was |
| 1035 | * not previously there, enter it now and |
| 1036 | * return a null value. |
| 1037 | */ |
| 1038 | static struct var *lookup(const char *n) |
| 1039 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1040 | // FIXME: dirty hack |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1041 | static struct var dummy; |
| 1042 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1043 | struct var *vp; |
| 1044 | const char *cp; |
| 1045 | char *xp; |
| 1046 | int c; |
| 1047 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1048 | if (isdigit(*n)) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1049 | dummy.name = (char*)n; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1050 | for (c = 0; isdigit(*n) && c < 1000; n++) |
| 1051 | c = c * 10 + *n - '0'; |
| 1052 | dummy.status = RONLY; |
| 1053 | dummy.value = (c <= dolc ? dolv[c] : null); |
| 1054 | return &dummy; |
| 1055 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1056 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1057 | for (vp = vlist; vp; vp = vp->next) |
| 1058 | if (eqname(vp->name, n)) |
| 1059 | return vp; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1060 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1061 | cp = findeq(n); |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1062 | vp = get_space(sizeof(*vp)); |
| 1063 | if (vp == 0 || (vp->name = get_space((int) (cp - n) + 2)) == NULL) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1064 | dummy.name = dummy.value = (char*)""; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1065 | return &dummy; |
| 1066 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1067 | |
| 1068 | xp = vp->name; |
| 1069 | while ((*xp = *n++) != '\0' && *xp != '=') |
| 1070 | xp++; |
| 1071 | *xp++ = '='; |
| 1072 | *xp = '\0'; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1073 | setarea((char *) vp, 0); |
| 1074 | setarea((char *) vp->name, 0); |
| 1075 | vp->value = null; |
| 1076 | vp->next = vlist; |
| 1077 | vp->status = GETCELL; |
| 1078 | vlist = vp; |
| 1079 | return vp; |
| 1080 | } |
| 1081 | |
| 1082 | /* |
| 1083 | * if name is not NULL, it must be |
| 1084 | * a prefix of the space `val', |
| 1085 | * and end with `='. |
| 1086 | * this is all so that exporting |
| 1087 | * values is reasonably painless. |
| 1088 | */ |
| 1089 | static void nameval(struct var *vp, const char *val, const char *name) |
| 1090 | { |
| 1091 | const char *cp; |
| 1092 | char *xp; |
| 1093 | int fl; |
| 1094 | |
| 1095 | if (vp->status & RONLY) { |
| 1096 | xp = vp->name; |
| 1097 | while (*xp && *xp != '=') |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1098 | fputc(*xp++, stderr); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1099 | err(" is read-only"); |
| 1100 | return; |
| 1101 | } |
| 1102 | fl = 0; |
| 1103 | if (name == NULL) { |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1104 | xp = get_space(strlen(vp->name) + strlen(val) + 2); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1105 | if (xp == NULL) |
| 1106 | return; |
| 1107 | /* make string: name=value */ |
| 1108 | setarea(xp, 0); |
| 1109 | name = xp; |
| 1110 | cp = vp->name; |
| 1111 | while ((*xp = *cp++) != '\0' && *xp != '=') |
| 1112 | xp++; |
| 1113 | *xp++ = '='; |
| 1114 | strcpy(xp, val); |
| 1115 | val = xp; |
| 1116 | fl = GETCELL; |
| 1117 | } |
| 1118 | if (vp->status & GETCELL) |
| 1119 | freecell(vp->name); /* form new string `name=value' */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1120 | vp->name = (char*)name; |
| 1121 | vp->value = (char*)val; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1122 | vp->status |= fl; |
| 1123 | } |
| 1124 | |
| 1125 | /* |
| 1126 | * give variable at `vp' the value `val'. |
| 1127 | */ |
| 1128 | static void setval(struct var *vp, const char *val) |
| 1129 | { |
| 1130 | nameval(vp, val, NULL); |
| 1131 | } |
| 1132 | |
| 1133 | static void export(struct var *vp) |
| 1134 | { |
| 1135 | vp->status |= EXPORT; |
| 1136 | } |
| 1137 | |
| 1138 | static void ronly(struct var *vp) |
| 1139 | { |
| 1140 | if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */ |
| 1141 | vp->status |= RONLY; |
| 1142 | } |
| 1143 | |
| 1144 | static int isassign(const char *s) |
| 1145 | { |
| 1146 | unsigned char c; |
| 1147 | DBGPRINTF7(("ISASSIGN: enter, s=%s\n", s)); |
| 1148 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1149 | c = *s; |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 1150 | /* no isalpha() - we shouldn't use locale */ |
| 1151 | /* c | 0x20 - lowercase (Latin) letters */ |
| 1152 | if (c != '_' && (unsigned)((c|0x20) - 'a') > 25) |
| 1153 | /* not letter */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1154 | return 0; |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 1155 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1156 | while (1) { |
| 1157 | c = *++s; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1158 | if (c == '=') |
| 1159 | return 1; |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 1160 | if (c == '\0') |
| 1161 | return 0; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1162 | if (c != '_' |
| 1163 | && (unsigned)(c - '0') > 9 /* not number */ |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 1164 | && (unsigned)((c|0x20) - 'a') > 25 /* not letter */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1165 | ) { |
| 1166 | return 0; |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | static int assign(const char *s, int cf) |
| 1172 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1173 | const char *cp; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1174 | struct var *vp; |
| 1175 | |
| 1176 | DBGPRINTF7(("ASSIGN: enter, s=%s, cf=%d\n", s, cf)); |
| 1177 | |
| 1178 | if (!isalpha(*s) && *s != '_') |
| 1179 | return 0; |
| 1180 | for (cp = s; *cp != '='; cp++) |
| 1181 | if (*cp == '\0' || (!isalnum(*cp) && *cp != '_')) |
| 1182 | return 0; |
| 1183 | vp = lookup(s); |
| 1184 | nameval(vp, ++cp, cf == COPYV ? NULL : s); |
| 1185 | if (cf != COPYV) |
| 1186 | vp->status &= ~GETCELL; |
| 1187 | return 1; |
| 1188 | } |
| 1189 | |
| 1190 | static int checkname(char *cp) |
| 1191 | { |
| 1192 | DBGPRINTF7(("CHECKNAME: enter, cp=%s\n", cp)); |
| 1193 | |
| 1194 | if (!isalpha(*cp++) && *(cp - 1) != '_') |
| 1195 | return 0; |
| 1196 | while (*cp) |
| 1197 | if (!isalnum(*cp++) && *(cp - 1) != '_') |
| 1198 | return 0; |
| 1199 | return 1; |
| 1200 | } |
| 1201 | |
| 1202 | static void putvlist(int f, int out) |
| 1203 | { |
| 1204 | struct var *vp; |
| 1205 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1206 | for (vp = vlist; vp; vp = vp->next) { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1207 | if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) { |
| 1208 | if (vp->status & EXPORT) |
| 1209 | write(out, "export ", 7); |
| 1210 | if (vp->status & RONLY) |
| 1211 | write(out, "readonly ", 9); |
| 1212 | write(out, vp->name, (int) (findeq(vp->name) - vp->name)); |
| 1213 | write(out, "\n", 1); |
| 1214 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1215 | } |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | |
| 1219 | /* |
| 1220 | * trap handling |
| 1221 | */ |
| 1222 | static void sig(int i) |
| 1223 | { |
| 1224 | trapset = i; |
| 1225 | signal(i, sig); |
| 1226 | } |
| 1227 | |
| 1228 | static void runtrap(int i) |
| 1229 | { |
| 1230 | char *trapstr; |
| 1231 | |
| 1232 | trapstr = trap[i]; |
| 1233 | if (trapstr == NULL) |
| 1234 | return; |
| 1235 | |
| 1236 | if (i == 0) |
| 1237 | trap[i] = NULL; |
| 1238 | |
| 1239 | RUN(aword, trapstr, nlchar); |
| 1240 | } |
| 1241 | |
| 1242 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1243 | static void setdash(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1244 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1245 | char *cp; |
| 1246 | int c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1247 | char m['z' - 'a' + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1248 | |
| 1249 | cp = m; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1250 | for (c = 'a'; c <= 'z'; c++) |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 1251 | if (FLAG[c]) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1252 | *cp++ = c; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1253 | *cp = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1254 | setval(lookup("-"), m); |
| 1255 | } |
| 1256 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1257 | static int newfile(char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1258 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1259 | int f; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1260 | |
| 1261 | DBGPRINTF7(("NEWFILE: opening %s\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1262 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1263 | f = 0; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 1264 | if (NOT_LONE_DASH(s)) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1265 | DBGPRINTF(("NEWFILE: s is %s\n", s)); |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 1266 | f = open(s, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1267 | if (f < 0) { |
| 1268 | prs(s); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 1269 | err(": can't open"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1270 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1271 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1272 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1273 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1274 | next(remap(f)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1275 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1278 | |
Denis Vlasenko | bc1918a | 2008-04-15 01:17:50 +0000 | [diff] [blame] | 1279 | #ifdef UNUSED |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1280 | struct op *scantree(struct op *head) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1281 | { |
| 1282 | struct op *dotnode; |
| 1283 | |
| 1284 | if (head == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1285 | return NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1286 | |
| 1287 | if (head->left != NULL) { |
| 1288 | dotnode = scantree(head->left); |
| 1289 | if (dotnode) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1290 | return dotnode; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | if (head->right != NULL) { |
| 1294 | dotnode = scantree(head->right); |
| 1295 | if (dotnode) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1296 | return dotnode; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1299 | if (head->op_words == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1300 | return NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1301 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1302 | DBGPRINTF5(("SCANTREE: checking node %p\n", head)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1303 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1304 | if ((head->op_type != TDOT) && LONE_CHAR(head->op_words[0], '.')) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1305 | DBGPRINTF5(("SCANTREE: dot found in node %p\n", head)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1306 | return head; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1309 | return NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1310 | } |
Denis Vlasenko | bc1918a | 2008-04-15 01:17:50 +0000 | [diff] [blame] | 1311 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1312 | |
| 1313 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1314 | static void onecommand(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1315 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1316 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1317 | jmp_buf m1; |
| 1318 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1319 | DBGPRINTF(("ONECOMMAND: enter, outtree=%p\n", outtree)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1320 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1321 | while (global_env.oenv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1322 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1323 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1324 | areanum = 1; |
| 1325 | freehere(areanum); |
| 1326 | freearea(areanum); |
| 1327 | garbage(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1328 | wdlist = NULL; |
| 1329 | iolist = NULL; |
| 1330 | global_env.errpt = NULL; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1331 | global_env.linep = line; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1332 | yynerrs = 0; |
| 1333 | multiline = 0; |
| 1334 | inparse = 1; |
| 1335 | intr = 0; |
| 1336 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1337 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1338 | failpt = m1; |
| 1339 | setjmp(failpt); /* Bruce Evans' fix */ |
| 1340 | failpt = m1; |
| 1341 | if (setjmp(failpt) || yyparse() || intr) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1342 | DBGPRINTF(("ONECOMMAND: this is not good.\n")); |
| 1343 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1344 | while (global_env.oenv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1345 | quitenv(); |
| 1346 | scraphere(); |
| 1347 | if (!interactive && intr) |
| 1348 | leave(); |
| 1349 | inparse = 0; |
| 1350 | intr = 0; |
| 1351 | return; |
| 1352 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1353 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1354 | inparse = 0; |
| 1355 | brklist = 0; |
| 1356 | intr = 0; |
| 1357 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1358 | |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 1359 | if (!FLAG['n']) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1360 | DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=%p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1361 | outtree)); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 1362 | execute(outtree, NOPIPE, NOPIPE, /* no_fork: */ 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1365 | if (!interactive && intr) { |
| 1366 | execflg = 0; |
| 1367 | leave(); |
| 1368 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1369 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1370 | i = trapset; |
| 1371 | if (i != 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1372 | trapset = 0; |
| 1373 | runtrap(i); |
| 1374 | } |
| 1375 | } |
| 1376 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1377 | static int newenv(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1378 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1379 | struct env *ep; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1380 | |
| 1381 | DBGPRINTF(("NEWENV: f=%d (indicates quitenv and return)\n", f)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1382 | |
| 1383 | if (f) { |
| 1384 | quitenv(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1385 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1386 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1387 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1388 | ep = get_space(sizeof(*ep)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1389 | if (ep == NULL) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1390 | while (global_env.oenv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1391 | quitenv(); |
| 1392 | fail(); |
| 1393 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1394 | *ep = global_env; |
| 1395 | global_env.oenv = ep; |
| 1396 | global_env.errpt = errpt; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1397 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1398 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1401 | static void quitenv(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1402 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1403 | struct env *ep; |
| 1404 | int fd; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1405 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1406 | DBGPRINTF(("QUITENV: global_env.oenv=%p\n", global_env.oenv)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1407 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1408 | ep = global_env.oenv; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1409 | if (ep != NULL) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1410 | fd = global_env.iofd; |
| 1411 | global_env = *ep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1412 | /* should close `'d files */ |
| 1413 | DELETE(ep); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1414 | while (--fd >= global_env.iofd) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1415 | close(fd); |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | /* |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1420 | * Is character c in s? |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1421 | */ |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1422 | static int any(int c, const char *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 | while (*s) |
| 1425 | if (*s++ == c) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1426 | return 1; |
| 1427 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | /* |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1431 | * Is any character from s1 in s2? |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1432 | */ |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1433 | static int anys(const char *s1, const char *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 | while (*s1) |
| 1436 | if (any(*s1++, s2)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1437 | return 1; |
| 1438 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1441 | static char *putn(int n) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1442 | { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1443 | return itoa(n); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1446 | static void next(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1447 | { |
| 1448 | PUSHIO(afile, f, filechar); |
| 1449 | } |
| 1450 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1451 | static void onintr(int s UNUSED_PARAM) /* ANSI C requires a parameter */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1452 | { |
| 1453 | signal(SIGINT, onintr); |
| 1454 | intr = 1; |
| 1455 | if (interactive) { |
| 1456 | if (inparse) { |
| 1457 | prs("\n"); |
| 1458 | fail(); |
| 1459 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1460 | } else if (heedint) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1461 | execflg = 0; |
| 1462 | leave(); |
| 1463 | } |
| 1464 | } |
| 1465 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1466 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1467 | /* -------- gmatch.c -------- */ |
| 1468 | /* |
| 1469 | * int gmatch(string, pattern) |
| 1470 | * char *string, *pattern; |
| 1471 | * |
| 1472 | * Match a pattern as in sh(1). |
| 1473 | */ |
| 1474 | |
| 1475 | #define CMASK 0377 |
| 1476 | #define QUOTE 0200 |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 1477 | #define QMASK (CMASK & ~QUOTE) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1478 | #define NOT '!' /* might use ^ */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1479 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1480 | static const char *cclass(const char *p, int sub) |
| 1481 | { |
| 1482 | int c, d, not, found; |
| 1483 | |
| 1484 | not = (*p == NOT); |
| 1485 | if (not != 0) |
| 1486 | p++; |
| 1487 | found = not; |
| 1488 | do { |
| 1489 | if (*p == '\0') |
| 1490 | return NULL; |
| 1491 | c = *p & CMASK; |
| 1492 | if (p[1] == '-' && p[2] != ']') { |
| 1493 | d = p[2] & CMASK; |
| 1494 | p++; |
| 1495 | } else |
| 1496 | d = c; |
| 1497 | if (c == sub || (c <= sub && sub <= d)) |
| 1498 | found = !not; |
| 1499 | } while (*++p != ']'); |
| 1500 | return found ? p + 1 : NULL; |
| 1501 | } |
| 1502 | |
| 1503 | static int gmatch(const char *s, const char *p) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1504 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1505 | int sc, pc; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1506 | |
| 1507 | if (s == NULL || p == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1508 | return 0; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1509 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1510 | while ((pc = *p++ & CMASK) != '\0') { |
| 1511 | sc = *s++ & QMASK; |
| 1512 | switch (pc) { |
| 1513 | case '[': |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1514 | p = cclass(p, sc); |
| 1515 | if (p == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1516 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1517 | break; |
| 1518 | |
| 1519 | case '?': |
| 1520 | if (sc == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1521 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1522 | break; |
| 1523 | |
| 1524 | case '*': |
| 1525 | s--; |
| 1526 | do { |
| 1527 | if (*p == '\0' || gmatch(s, p)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1528 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1529 | } while (*s++ != '\0'); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1530 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1531 | |
| 1532 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1533 | if (sc != (pc & ~QUOTE)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1534 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1535 | } |
| 1536 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1537 | return *s == '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1540 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1541 | /* -------- csyn.c -------- */ |
| 1542 | /* |
| 1543 | * shell: syntax (C version) |
| 1544 | */ |
| 1545 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1546 | static void yyerror(const char *s) NORETURN; |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1547 | static void yyerror(const char *s) |
| 1548 | { |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 1549 | yynerrs = 1; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1550 | if (interactive && global_env.iop <= iostack) { |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1551 | multiline = 0; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 1552 | while (eofc() == 0 && yylex(0) != '\n') |
| 1553 | continue; |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1554 | } |
| 1555 | err(s); |
| 1556 | fail(); |
| 1557 | } |
| 1558 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1559 | static void zzerr(void) NORETURN; |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1560 | static void zzerr(void) |
| 1561 | { |
| 1562 | yyerror("syntax error"); |
| 1563 | } |
| 1564 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1565 | int yyparse(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1566 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1567 | DBGPRINTF7(("YYPARSE: enter...\n")); |
| 1568 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1569 | startl = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1570 | peeksym = 0; |
| 1571 | yynerrs = 0; |
| 1572 | outtree = c_list(); |
| 1573 | musthave('\n', 0); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 1574 | return yynerrs; /* 0/1 */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1577 | static struct op *pipeline(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1578 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1579 | struct op *t, *p; |
| 1580 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1581 | |
| 1582 | DBGPRINTF7(("PIPELINE: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1583 | |
| 1584 | t = command(cf); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1585 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1586 | DBGPRINTF9(("PIPELINE: t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1587 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1588 | if (t != NULL) { |
| 1589 | while ((c = yylex(0)) == '|') { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1590 | p = command(CONTIN); |
| 1591 | if (p == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1592 | DBGPRINTF8(("PIPELINE: error!\n")); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1593 | zzerr(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1596 | if (t->op_type != TPAREN && t->op_type != TCOM) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1597 | /* shell statement */ |
| 1598 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 1599 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1600 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1601 | t = block(TPIPE, t, p, NOWORDS); |
| 1602 | } |
| 1603 | peeksym = c; |
| 1604 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1605 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1606 | DBGPRINTF7(("PIPELINE: returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1607 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1610 | static struct op *andor(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1611 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1612 | struct op *t, *p; |
| 1613 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1614 | |
| 1615 | DBGPRINTF7(("ANDOR: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1616 | |
| 1617 | t = pipeline(0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1618 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1619 | DBGPRINTF9(("ANDOR: t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1620 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1621 | if (t != NULL) { |
| 1622 | while ((c = yylex(0)) == LOGAND || c == LOGOR) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1623 | p = pipeline(CONTIN); |
| 1624 | if (p == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1625 | DBGPRINTF8(("ANDOR: error!\n")); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1626 | zzerr(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1629 | t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1630 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1631 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1632 | peeksym = c; |
| 1633 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1634 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1635 | DBGPRINTF7(("ANDOR: returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1636 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1639 | static struct op *c_list(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1640 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1641 | struct op *t, *p; |
| 1642 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1643 | |
| 1644 | DBGPRINTF7(("C_LIST: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1645 | |
| 1646 | t = andor(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1647 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1648 | if (t != NULL) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1649 | peeksym = yylex(0); |
| 1650 | if (peeksym == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1651 | t = block(TASYNC, t, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1652 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1653 | while ((c = yylex(0)) == ';' || c == '&' |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1654 | || (multiline && c == '\n') |
| 1655 | ) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1656 | p = andor(); |
| 1657 | if (p== NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1658 | return t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1659 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1660 | peeksym = yylex(0); |
| 1661 | if (peeksym == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1662 | p = block(TASYNC, p, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1663 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1664 | t = list(t, p); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1665 | } /* WHILE */ |
| 1666 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1667 | peeksym = c; |
| 1668 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1669 | /* IF */ |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1670 | DBGPRINTF7(("C_LIST: returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1671 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1674 | static int synio(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1675 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1676 | struct ioword *iop; |
| 1677 | int i; |
| 1678 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1679 | |
| 1680 | DBGPRINTF7(("SYNIO: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1681 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1682 | c = yylex(cf); |
| 1683 | if (c != '<' && c != '>') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1684 | peeksym = c; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1685 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1686 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1687 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1688 | i = yylval.i; |
| 1689 | musthave(WORD, 0); |
| 1690 | iop = io(iounit, i, yylval.cp); |
| 1691 | iounit = IODEFAULT; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1692 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1693 | if (i & IOHERE) |
| 1694 | markhere(yylval.cp, iop); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1695 | |
| 1696 | DBGPRINTF7(("SYNIO: returning 1\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1697 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1700 | static void musthave(int c, int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1701 | { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1702 | peeksym = yylex(cf); |
| 1703 | if (peeksym != c) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1704 | DBGPRINTF7(("MUSTHAVE: error!\n")); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1705 | zzerr(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1708 | peeksym = 0; |
| 1709 | } |
| 1710 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1711 | static struct op *simple(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1712 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1713 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1714 | |
| 1715 | t = NULL; |
| 1716 | for (;;) { |
| 1717 | switch (peeksym = yylex(0)) { |
| 1718 | case '<': |
| 1719 | case '>': |
| 1720 | (void) synio(0); |
| 1721 | break; |
| 1722 | |
| 1723 | case WORD: |
| 1724 | if (t == NULL) { |
| 1725 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1726 | t->op_type = TCOM; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1727 | } |
| 1728 | peeksym = 0; |
| 1729 | word(yylval.cp); |
| 1730 | break; |
| 1731 | |
| 1732 | default: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1733 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1734 | } |
| 1735 | } |
| 1736 | } |
| 1737 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1738 | static struct op *nested(int type, int mark) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1739 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1740 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1741 | |
| 1742 | DBGPRINTF3(("NESTED: enter, type=%d, mark=%d\n", type, mark)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1743 | |
| 1744 | multiline++; |
| 1745 | t = c_list(); |
| 1746 | musthave(mark, 0); |
| 1747 | multiline--; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1748 | return block(type, t, NOBLOCK, NOWORDS); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1751 | static struct op *command(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1752 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1753 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1754 | struct wdblock *iosave; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1755 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1756 | |
| 1757 | DBGPRINTF(("COMMAND: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1758 | |
| 1759 | iosave = iolist; |
| 1760 | iolist = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1761 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1762 | if (multiline) |
| 1763 | cf |= CONTIN; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1764 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1765 | while (synio(cf)) |
| 1766 | cf = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1767 | |
| 1768 | c = yylex(cf); |
| 1769 | |
| 1770 | switch (c) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1771 | default: |
| 1772 | peeksym = c; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1773 | t = simple(); |
| 1774 | if (t == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1775 | if (iolist == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1776 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1777 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1778 | t->op_type = TCOM; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1779 | } |
| 1780 | break; |
| 1781 | |
| 1782 | case '(': |
| 1783 | t = nested(TPAREN, ')'); |
| 1784 | break; |
| 1785 | |
| 1786 | case '{': |
| 1787 | t = nested(TBRACE, '}'); |
| 1788 | break; |
| 1789 | |
| 1790 | case FOR: |
| 1791 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1792 | t->op_type = TFOR; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1793 | musthave(WORD, 0); |
| 1794 | startl = 1; |
| 1795 | t->str = yylval.cp; |
| 1796 | multiline++; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1797 | t->op_words = wordlist(); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1798 | c = yylex(0); |
| 1799 | if (c != '\n' && c != ';') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1800 | peeksym = c; |
| 1801 | t->left = dogroup(0); |
| 1802 | multiline--; |
| 1803 | break; |
| 1804 | |
| 1805 | case WHILE: |
| 1806 | case UNTIL: |
| 1807 | multiline++; |
| 1808 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1809 | t->op_type = (c == WHILE ? TWHILE : TUNTIL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1810 | t->left = c_list(); |
| 1811 | t->right = dogroup(1); |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1812 | /* t->op_words = NULL; - newtp() did this */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1813 | multiline--; |
| 1814 | break; |
| 1815 | |
| 1816 | case CASE: |
| 1817 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1818 | t->op_type = TCASE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1819 | musthave(WORD, 0); |
| 1820 | t->str = yylval.cp; |
| 1821 | startl++; |
| 1822 | multiline++; |
| 1823 | musthave(IN, CONTIN); |
| 1824 | startl++; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1825 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1826 | t->left = caselist(); |
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 | musthave(ESAC, 0); |
| 1829 | multiline--; |
| 1830 | break; |
| 1831 | |
| 1832 | case IF: |
| 1833 | multiline++; |
| 1834 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1835 | t->op_type = TIF; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1836 | t->left = c_list(); |
| 1837 | t->right = thenpart(); |
| 1838 | musthave(FI, 0); |
| 1839 | multiline--; |
| 1840 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1841 | |
| 1842 | case DOT: |
| 1843 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1844 | t->op_type = TDOT; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1845 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1846 | musthave(WORD, 0); /* gets name of file */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1847 | DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp)); |
| 1848 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1849 | word(yylval.cp); /* add word to wdlist */ |
| 1850 | word(NOWORD); /* terminate wdlist */ |
| 1851 | t->op_words = copyw(); /* dup wdlist */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1852 | break; |
| 1853 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1854 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1855 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1856 | while (synio(0)) |
| 1857 | continue; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1858 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1859 | t = namelist(t); |
| 1860 | iolist = iosave; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1861 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1862 | DBGPRINTF(("COMMAND: returning %p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1863 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1864 | return t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 1867 | static struct op *dowholefile(int type /*, int mark*/) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1868 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1869 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1870 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 1871 | DBGPRINTF(("DOWHOLEFILE: enter, type=%d\n", type /*, mark*/)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1872 | |
| 1873 | multiline++; |
| 1874 | t = c_list(); |
| 1875 | multiline--; |
| 1876 | t = block(type, t, NOBLOCK, NOWORDS); |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1877 | DBGPRINTF(("DOWHOLEFILE: return t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1878 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1881 | static struct op *dogroup(int onlydone) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1882 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1883 | int c; |
| 1884 | struct op *mylist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1885 | |
| 1886 | c = yylex(CONTIN); |
| 1887 | if (c == DONE && onlydone) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1888 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1889 | if (c != DO) |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1890 | zzerr(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1891 | mylist = c_list(); |
| 1892 | musthave(DONE, 0); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1893 | return mylist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1896 | static struct op *thenpart(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1897 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1898 | int c; |
| 1899 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1900 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1901 | c = yylex(0); |
| 1902 | if (c != THEN) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1903 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1904 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1905 | } |
| 1906 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1907 | /*t->op_type = 0; - newtp() did this */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1908 | t->left = c_list(); |
| 1909 | if (t->left == NULL) |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1910 | zzerr(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1911 | t->right = elsepart(); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1912 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1915 | static struct op *elsepart(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1916 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1917 | int c; |
| 1918 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1919 | |
| 1920 | switch (c = yylex(0)) { |
| 1921 | case ELSE: |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1922 | t = c_list(); |
| 1923 | if (t == NULL) |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1924 | zzerr(); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1925 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1926 | |
| 1927 | case ELIF: |
| 1928 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1929 | t->op_type = TELIF; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1930 | t->left = c_list(); |
| 1931 | t->right = thenpart(); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1932 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1933 | |
| 1934 | default: |
| 1935 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1936 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1940 | static struct op *caselist(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1941 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1942 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1943 | |
| 1944 | t = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1945 | while ((peeksym = yylex(CONTIN)) != ESAC) { |
| 1946 | DBGPRINTF(("CASELIST, doing yylex, peeksym=%d\n", peeksym)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1947 | t = list(t, casepart()); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1950 | DBGPRINTF(("CASELIST, returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1951 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1954 | static struct op *casepart(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1955 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1956 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1957 | |
| 1958 | DBGPRINTF7(("CASEPART: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1959 | |
| 1960 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 1961 | t->op_type = TPAT; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 1962 | t->op_words = pattern(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1963 | musthave(')', 0); |
| 1964 | t->left = c_list(); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1965 | peeksym = yylex(CONTIN); |
| 1966 | if (peeksym != ESAC) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1967 | musthave(BREAK, CONTIN); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1968 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1969 | DBGPRINTF7(("CASEPART: made newtp(TPAT, t=%p)\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1970 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1971 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1974 | static char **pattern(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1975 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1976 | int c, cf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1977 | |
| 1978 | cf = CONTIN; |
| 1979 | do { |
| 1980 | musthave(WORD, cf); |
| 1981 | word(yylval.cp); |
| 1982 | cf = 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1983 | c = yylex(0); |
| 1984 | } while (c == '|'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1985 | peeksym = c; |
| 1986 | word(NOWORD); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1987 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1988 | return copyw(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1991 | static char **wordlist(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1992 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1993 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1994 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1995 | c = yylex(0); |
| 1996 | if (c != IN) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1997 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1998 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1999 | } |
| 2000 | startl = 0; |
| 2001 | while ((c = yylex(0)) == WORD) |
| 2002 | word(yylval.cp); |
| 2003 | word(NOWORD); |
| 2004 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2005 | return copyw(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * supporting functions |
| 2010 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2011 | static struct op *list(struct op *t1, struct op *t2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2012 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2013 | DBGPRINTF7(("LIST: enter, t1=%p, t2=%p\n", t1, t2)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2014 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2015 | if (t1 == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2016 | return t2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2017 | if (t2 == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2018 | return t1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2019 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2020 | return block(TLIST, t1, t2, NOWORDS); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2023 | 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] | 2024 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2025 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2026 | |
| 2027 | DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type])); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2028 | |
| 2029 | t = newtp(); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2030 | t->op_type = type; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2031 | t->left = t1; |
| 2032 | t->right = t2; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2033 | t->op_words = wp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2034 | |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 2035 | DBGPRINTF7(("BLOCK: inserted %p between %p and %p\n", t, t1, t2)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2036 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2037 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2040 | /* 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] | 2041 | static int rlookup(char *n) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2042 | { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2043 | struct res { |
| 2044 | char r_name[6]; |
| 2045 | int16_t r_val; |
| 2046 | }; |
| 2047 | static const struct res restab[] = { |
| 2048 | { "for" , FOR }, |
| 2049 | { "case" , CASE }, |
| 2050 | { "esac" , ESAC }, |
| 2051 | { "while", WHILE }, |
| 2052 | { "do" , DO }, |
| 2053 | { "done" , DONE }, |
| 2054 | { "if" , IF }, |
| 2055 | { "in" , IN }, |
| 2056 | { "then" , THEN }, |
| 2057 | { "else" , ELSE }, |
| 2058 | { "elif" , ELIF }, |
| 2059 | { "until", UNTIL }, |
| 2060 | { "fi" , FI }, |
| 2061 | { ";;" , BREAK }, |
| 2062 | { "||" , LOGOR }, |
| 2063 | { "&&" , LOGAND }, |
| 2064 | { "{" , '{' }, |
| 2065 | { "}" , '}' }, |
| 2066 | { "." , DOT }, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 2067 | { "" , 0 }, |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2068 | }; |
| 2069 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2070 | const struct res *rp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2071 | |
| 2072 | DBGPRINTF7(("RLOOKUP: enter, n is %s\n", n)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2073 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2074 | for (rp = restab; rp->r_name[0]; rp++) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2075 | if (strcmp(rp->r_name, n) == 0) { |
| 2076 | DBGPRINTF7(("RLOOKUP: match, returning %d\n", rp->r_val)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2077 | return rp->r_val; /* Return numeric code for shell multiline */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | DBGPRINTF7(("RLOOKUP: NO match, returning 0\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2081 | return 0; /* Not a shell multiline */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2084 | static struct op *newtp(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2085 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2086 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2087 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2088 | t = (struct op *) tree(sizeof(*t)); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 2089 | memset(t, 0, sizeof(*t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2090 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2091 | DBGPRINTF3(("NEWTP: allocated %p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2092 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2093 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2096 | static struct op *namelist(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2097 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2098 | DBGPRINTF7(("NAMELIST: enter, t=%p, type %s, iolist=%p\n", t, |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2099 | T_CMD_NAMES[t->op_type], iolist)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2100 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2101 | if (iolist) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2102 | iolist = addword((char *) NULL, iolist); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2103 | t->ioact = copyio(); |
| 2104 | } else |
| 2105 | t->ioact = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2106 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2107 | if (t->op_type != TCOM) { |
| 2108 | if (t->op_type != TPAREN && t->ioact != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2109 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 2110 | t->ioact = t->left->ioact; |
| 2111 | t->left->ioact = NULL; |
| 2112 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2113 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2114 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2115 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2116 | word(NOWORD); |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2117 | t->op_words = copyw(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2118 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2119 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2122 | static char **copyw(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2123 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2124 | char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2125 | |
| 2126 | wd = getwords(wdlist); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2127 | wdlist = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2128 | return wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2131 | static void word(char *cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2132 | { |
| 2133 | wdlist = addword(cp, wdlist); |
| 2134 | } |
| 2135 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2136 | static struct ioword **copyio(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2137 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2138 | struct ioword **iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2139 | |
| 2140 | iop = (struct ioword **) getwords(iolist); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2141 | iolist = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2142 | return iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2145 | static struct ioword *io(int u, int f, char *cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2146 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2147 | struct ioword *iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2148 | |
| 2149 | iop = (struct ioword *) tree(sizeof(*iop)); |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2150 | iop->io_fd = u; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2151 | iop->io_flag = f; |
| 2152 | iop->io_name = cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2153 | iolist = addword((char *) iop, iolist); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2154 | return iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2157 | static int yylex(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2158 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2159 | int c, c1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2160 | int atstart; |
| 2161 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2162 | c = peeksym; |
| 2163 | if (c > 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2164 | peeksym = 0; |
| 2165 | if (c == '\n') |
| 2166 | startl = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2167 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2168 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2169 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2170 | nlseen = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2171 | atstart = startl; |
| 2172 | startl = 0; |
| 2173 | yylval.i = 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2174 | global_env.linep = line; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2175 | |
| 2176 | /* MALAMO */ |
| 2177 | line[LINELIM - 1] = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2178 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2179 | loop: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2180 | while ((c = my_getc(0)) == ' ' || c == '\t') /* Skip whitespace */ |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2181 | continue; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2182 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2183 | switch (c) { |
| 2184 | default: |
| 2185 | if (any(c, "0123456789")) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2186 | c1 = my_getc(0); |
| 2187 | unget(c1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2188 | if (c1 == '<' || c1 == '>') { |
| 2189 | iounit = c - '0'; |
| 2190 | goto loop; |
| 2191 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2192 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2193 | c = c1; |
| 2194 | } |
| 2195 | break; |
| 2196 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2197 | case '#': /* Comment, skip to next newline or End-of-string */ |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2198 | while ((c = my_getc(0)) != '\0' && c != '\n') |
| 2199 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2200 | unget(c); |
| 2201 | goto loop; |
| 2202 | |
| 2203 | case 0: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2204 | DBGPRINTF5(("YYLEX: return 0, c=%d\n", c)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2205 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2206 | |
| 2207 | case '$': |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2208 | DBGPRINTF9(("YYLEX: found $\n")); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2209 | *global_env.linep++ = c; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2210 | c = my_getc(0); |
| 2211 | if (c == '{') { |
| 2212 | c = collect(c, '}'); |
| 2213 | if (c != '\0') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2214 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2215 | goto pack; |
| 2216 | } |
| 2217 | break; |
| 2218 | |
| 2219 | case '`': |
| 2220 | case '\'': |
| 2221 | case '"': |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2222 | c = collect(c, c); |
| 2223 | if (c != '\0') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2224 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2225 | goto pack; |
| 2226 | |
| 2227 | case '|': |
| 2228 | case '&': |
| 2229 | case ';': |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2230 | startl = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2231 | /* If more chars process them, else return NULL char */ |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2232 | c1 = dual(c); |
| 2233 | if (c1 != '\0') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2234 | return c1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2235 | return c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2236 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2237 | case '^': |
| 2238 | startl = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2239 | return '|'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2240 | case '>': |
| 2241 | case '<': |
| 2242 | diag(c); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2243 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2244 | |
| 2245 | case '\n': |
| 2246 | nlseen++; |
| 2247 | gethere(); |
| 2248 | startl = 1; |
| 2249 | if (multiline || cf & CONTIN) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2250 | if (interactive && global_env.iop <= iostack) { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 2251 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2252 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2253 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2254 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2255 | #endif |
| 2256 | } |
| 2257 | if (cf & CONTIN) |
| 2258 | goto loop; |
| 2259 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2260 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2261 | |
| 2262 | case '(': |
| 2263 | case ')': |
| 2264 | startl = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2265 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | unget(c); |
| 2269 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2270 | pack: |
| 2271 | while ((c = my_getc(0)) != '\0' && !any(c, "`$ '\"\t;&<>()|^\n")) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2272 | if (global_env.linep >= elinep) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2273 | err("word too long"); |
| 2274 | else |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2275 | *global_env.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2276 | }; |
| 2277 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2278 | unget(c); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2279 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2280 | if (any(c, "\"'`$")) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2281 | goto loop; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2282 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2283 | *global_env.linep++ = '\0'; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2284 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2285 | if (atstart) { |
| 2286 | c = rlookup(line); |
| 2287 | if (c != 0) { |
| 2288 | startl = 1; |
| 2289 | return c; |
| 2290 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2291 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2292 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2293 | yylval.cp = strsave(line, areanum); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2294 | return WORD; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2297 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2298 | static int collect(int c, int c1) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2299 | { |
| 2300 | char s[2]; |
| 2301 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2302 | DBGPRINTF8(("COLLECT: enter, c=%d, c1=%d\n", c, c1)); |
| 2303 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2304 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2305 | while ((c = my_getc(c1)) != c1) { |
| 2306 | if (c == 0) { |
| 2307 | unget(c); |
| 2308 | s[0] = c1; |
| 2309 | s[1] = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2310 | prs("no closing "); |
| 2311 | yyerror(s); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2312 | return YYERRCODE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2313 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2314 | if (interactive && c == '\n' && global_env.iop <= iostack) { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 2315 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2316 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2317 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2318 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2319 | #endif |
| 2320 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2321 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2322 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2323 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2324 | *global_env.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2325 | |
| 2326 | DBGPRINTF8(("COLLECT: return 0, line is %s\n", line)); |
| 2327 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2328 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2331 | /* "multiline commands" helper func */ |
| 2332 | /* see if next 2 chars form a shell multiline */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2333 | static int dual(int c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2334 | { |
| 2335 | char s[3]; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2336 | char *cp = s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2337 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2338 | DBGPRINTF8(("DUAL: enter, c=%d\n", c)); |
| 2339 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2340 | *cp++ = c; /* c is the given "peek" char */ |
| 2341 | *cp++ = my_getc(0); /* get next char of input */ |
| 2342 | *cp = '\0'; /* add EOS marker */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2343 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2344 | c = rlookup(s); /* see if 2 chars form a shell multiline */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2345 | if (c == 0) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2346 | unget(*--cp); /* String is not a shell multiline, put peek char back */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2347 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2348 | return c; /* String is multiline, return numeric multiline (restab) code */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2351 | static void diag(int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2352 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2353 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2354 | |
| 2355 | DBGPRINTF8(("DIAG: enter, ec=%d\n", ec)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2356 | |
| 2357 | c = my_getc(0); |
| 2358 | if (c == '>' || c == '<') { |
| 2359 | if (c != ec) |
| 2360 | zzerr(); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2361 | yylval.i = (ec == '>' ? IOWRITE | IOCAT : IOHERE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2362 | c = my_getc(0); |
| 2363 | } else |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2364 | yylval.i = (ec == '>' ? IOWRITE : IOREAD); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2365 | if (c != '&' || yylval.i == IOHERE) |
| 2366 | unget(c); |
| 2367 | else |
| 2368 | yylval.i |= IODUP; |
| 2369 | } |
| 2370 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2371 | static char *tree(unsigned size) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2372 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2373 | char *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2374 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2375 | t = getcell(size); |
| 2376 | if (t == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2377 | DBGPRINTF2(("TREE: getcell(%d) failed!\n", size)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2378 | prs("command line too complicated\n"); |
| 2379 | fail(); |
| 2380 | /* NOTREACHED */ |
| 2381 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2382 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2385 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2386 | /* VARARGS1 */ |
| 2387 | /* ARGSUSED */ |
| 2388 | |
| 2389 | /* -------- exec.c -------- */ |
| 2390 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2391 | static struct op **find1case(struct op *t, const char *w) |
| 2392 | { |
| 2393 | struct op *t1; |
| 2394 | struct op **tp; |
| 2395 | char **wp; |
| 2396 | char *cp; |
| 2397 | |
| 2398 | if (t == NULL) { |
| 2399 | DBGPRINTF3(("FIND1CASE: enter, t==NULL, returning.\n")); |
| 2400 | return NULL; |
| 2401 | } |
| 2402 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2403 | DBGPRINTF3(("FIND1CASE: enter, t->op_type=%d (%s)\n", t->op_type, |
| 2404 | T_CMD_NAMES[t->op_type])); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2405 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2406 | if (t->op_type == TLIST) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2407 | tp = find1case(t->left, w); |
| 2408 | if (tp != NULL) { |
| 2409 | DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=%p\n", tp)); |
| 2410 | return tp; |
| 2411 | } |
| 2412 | t1 = t->right; /* TPAT */ |
| 2413 | } else |
| 2414 | t1 = t; |
| 2415 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2416 | for (wp = t1->op_words; *wp;) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2417 | cp = evalstr(*wp++, DOSUB); |
| 2418 | if (cp && gmatch(w, cp)) { |
| 2419 | DBGPRINTF3(("FIND1CASE: returning &t1->left= %p.\n", |
| 2420 | &t1->left)); |
| 2421 | return &t1->left; |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | DBGPRINTF(("FIND1CASE: returning NULL\n")); |
| 2426 | return NULL; |
| 2427 | } |
| 2428 | |
| 2429 | static struct op *findcase(struct op *t, const char *w) |
| 2430 | { |
| 2431 | struct op **tp; |
| 2432 | |
| 2433 | tp = find1case(t, w); |
| 2434 | return tp != NULL ? *tp : NULL; |
| 2435 | } |
| 2436 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2437 | /* |
| 2438 | * execute tree |
| 2439 | */ |
| 2440 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2441 | 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] | 2442 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2443 | struct op *t1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2444 | volatile int i, rv, a; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2445 | const char *cp; |
| 2446 | char **wp, **wp2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2447 | struct var *vp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2448 | struct op *outtree_save; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2449 | struct brkcon bc; |
| 2450 | |
| 2451 | #if __GNUC__ |
| 2452 | /* Avoid longjmp clobbering */ |
| 2453 | (void) ℘ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2454 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2455 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2456 | if (t == NULL) { |
| 2457 | DBGPRINTF4(("EXECUTE: enter, t==null, returning.\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2458 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2461 | DBGPRINTF(("EXECUTE: t=%p, t->op_type=%d (%s), t->op_words is %s\n", t, |
| 2462 | t->op_type, T_CMD_NAMES[t->op_type], |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2463 | ((t->op_words == NULL) ? "NULL" : t->op_words[0]))); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2464 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2465 | rv = 0; |
| 2466 | a = areanum++; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2467 | wp2 = t->op_words; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 2468 | wp = (wp2 != NULL) |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2469 | ? eval(wp2, t->op_type == TCOM ? DOALL : DOALL & ~DOKEY) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2470 | : NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2471 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2472 | switch (t->op_type) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2473 | case TDOT: |
| 2474 | DBGPRINTF3(("EXECUTE: TDOT\n")); |
| 2475 | |
| 2476 | outtree_save = outtree; |
| 2477 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2478 | newfile(evalstr(t->op_words[0], DOALL)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2479 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 2480 | t->left = dowholefile(TLIST /*, 0*/); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2481 | t->right = NULL; |
| 2482 | |
| 2483 | outtree = outtree_save; |
| 2484 | |
| 2485 | if (t->left) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2486 | rv = execute(t->left, pin, pout, /* no_fork: */ 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2487 | if (t->right) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2488 | rv = execute(t->right, pin, pout, /* no_fork: */ 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2489 | break; |
| 2490 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2491 | case TPAREN: |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2492 | rv = execute(t->left, pin, pout, /* no_fork: */ 0); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 2493 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2494 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2495 | case TCOM: |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2496 | rv = forkexec(t, pin, pout, no_fork, wp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2497 | break; |
| 2498 | |
| 2499 | case TPIPE: |
| 2500 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2501 | int pv[2]; |
| 2502 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2503 | rv = openpipe(pv); |
| 2504 | if (rv < 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2505 | break; |
| 2506 | pv[0] = remap(pv[0]); |
| 2507 | pv[1] = remap(pv[1]); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2508 | (void) execute(t->left, pin, pv, /* no_fork: */ 0); |
| 2509 | rv = execute(t->right, pv, pout, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2510 | } |
| 2511 | break; |
| 2512 | |
| 2513 | case TLIST: |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2514 | (void) execute(t->left, pin, pout, /* no_fork: */ 0); |
| 2515 | rv = execute(t->right, pin, pout, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2516 | break; |
| 2517 | |
| 2518 | case TASYNC: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2519 | { |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 2520 | smallint hinteractive = interactive; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2521 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2522 | DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n")); |
| 2523 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2524 | i = vfork(); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2525 | if (i == 0) { /* child */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2526 | signal(SIGINT, SIG_IGN); |
| 2527 | signal(SIGQUIT, SIG_IGN); |
| 2528 | if (interactive) |
| 2529 | signal(SIGTERM, SIG_DFL); |
| 2530 | interactive = 0; |
| 2531 | if (pin == NULL) { |
| 2532 | close(0); |
Denis Vlasenko | 2b54aaa | 2007-05-09 22:16:08 +0000 | [diff] [blame] | 2533 | xopen(bb_dev_null, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2534 | } |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2535 | _exit(execute(t->left, pin, pout, /* no_fork: */ 1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2536 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2537 | interactive = hinteractive; |
| 2538 | if (i != -1) { |
| 2539 | setval(lookup("!"), putn(i)); |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2540 | closepipe(pin); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2541 | if (interactive) { |
| 2542 | prs(putn(i)); |
| 2543 | prs("\n"); |
| 2544 | } |
| 2545 | } else |
| 2546 | rv = -1; |
| 2547 | setstatus(rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2548 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2549 | break; |
| 2550 | |
| 2551 | case TOR: |
| 2552 | case TAND: |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2553 | rv = execute(t->left, pin, pout, /* no_fork: */ 0); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2554 | t1 = t->right; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2555 | if (t1 != NULL && (rv == 0) == (t->op_type == TAND)) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2556 | rv = execute(t1, pin, pout, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2557 | break; |
| 2558 | |
| 2559 | case TFOR: |
| 2560 | if (wp == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2561 | wp = dolv + 1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2562 | i = dolc; |
| 2563 | if (i < 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2564 | i = 0; |
| 2565 | } else { |
| 2566 | i = -1; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2567 | while (*wp++ != NULL) |
| 2568 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2569 | } |
| 2570 | vp = lookup(t->str); |
| 2571 | while (setjmp(bc.brkpt)) |
| 2572 | if (isbreak) |
| 2573 | goto broken; |
Denis Vlasenko | 7ae1cc1 | 2008-07-20 23:03:23 +0000 | [diff] [blame] | 2574 | /* Restore areanum value. It may be incremented by execute() |
| 2575 | * below, and then "continue" may jump back to setjmp above */ |
| 2576 | areanum = a + 1; |
| 2577 | freearea(areanum + 1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2578 | brkset(&bc); |
| 2579 | for (t1 = t->left; i-- && *wp != NULL;) { |
| 2580 | setval(vp, *wp++); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2581 | rv = execute(t1, pin, pout, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2582 | } |
| 2583 | brklist = brklist->nextlev; |
| 2584 | break; |
| 2585 | |
| 2586 | case TWHILE: |
| 2587 | case TUNTIL: |
| 2588 | while (setjmp(bc.brkpt)) |
| 2589 | if (isbreak) |
| 2590 | goto broken; |
Denis Vlasenko | 7ae1cc1 | 2008-07-20 23:03:23 +0000 | [diff] [blame] | 2591 | /* Restore areanum value. It may be incremented by execute() |
| 2592 | * below, and then "continue" may jump back to setjmp above */ |
| 2593 | areanum = a + 1; |
| 2594 | freearea(areanum + 1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2595 | brkset(&bc); |
| 2596 | t1 = t->left; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2597 | 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] | 2598 | rv = execute(t->right, pin, pout, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2599 | brklist = brklist->nextlev; |
| 2600 | break; |
| 2601 | |
| 2602 | case TIF: |
| 2603 | case TELIF: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2604 | if (t->right != NULL) { |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2605 | rv = !execute(t->left, pin, pout, /* no_fork: */ 0) ? |
| 2606 | execute(t->right->left, pin, pout, /* no_fork: */ 0) : |
| 2607 | execute(t->right->right, pin, pout, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2608 | } |
| 2609 | break; |
| 2610 | |
| 2611 | case TCASE: |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2612 | cp = evalstr(t->str, DOSUB | DOTRIM); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2613 | if (cp == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2614 | cp = ""; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2615 | |
| 2616 | DBGPRINTF7(("EXECUTE: TCASE, t->str is %s, cp is %s\n", |
| 2617 | ((t->str == NULL) ? "NULL" : t->str), |
| 2618 | ((cp == NULL) ? "NULL" : cp))); |
| 2619 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2620 | t1 = findcase(t->left, cp); |
| 2621 | if (t1 != NULL) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2622 | DBGPRINTF7(("EXECUTE: TCASE, calling execute(t=%p, t1=%p)...\n", t, t1)); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2623 | rv = execute(t1, pin, pout, /* no_fork: */ 0); |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2624 | 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] | 2625 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2626 | break; |
| 2627 | |
| 2628 | case TBRACE: |
| 2629 | /* |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2630 | iopp = t->ioact; |
| 2631 | if (i) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2632 | while (*iopp) |
| 2633 | if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) { |
| 2634 | rv = -1; |
| 2635 | break; |
| 2636 | } |
| 2637 | */ |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2638 | if (rv >= 0) { |
| 2639 | t1 = t->left; |
| 2640 | if (t1) { |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2641 | rv = execute(t1, pin, pout, /* no_fork: */ 0); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2642 | } |
| 2643 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2644 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2645 | |
| 2646 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2647 | |
Denis Vlasenko | e471275 | 2007-04-14 15:08:41 +0000 | [diff] [blame] | 2648 | broken: |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2649 | // Restoring op_words is most likely not needed now: see comment in forkexec() |
| 2650 | // (also take a look at exec builtin (doexec) - it touches t->op_words) |
| 2651 | t->op_words = wp2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2652 | isbreak = 0; |
| 2653 | freehere(areanum); |
| 2654 | freearea(areanum); |
| 2655 | areanum = a; |
| 2656 | if (interactive && intr) { |
| 2657 | closeall(); |
| 2658 | fail(); |
| 2659 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2660 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2661 | i = trapset; |
| 2662 | if (i != 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2663 | trapset = 0; |
| 2664 | runtrap(i); |
| 2665 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2666 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2667 | DBGPRINTF(("EXECUTE: returning from t=%p, rv=%d\n", t, rv)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2668 | return rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
Denis Vlasenko | e471275 | 2007-04-14 15:08:41 +0000 | [diff] [blame] | 2671 | static builtin_func_ptr inbuilt(const char *s) |
| 2672 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2673 | const struct builtincmd *bp; |
| 2674 | |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 2675 | for (bp = builtincmds; bp->name; bp++) |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2676 | if (strcmp(bp->name, s) == 0) |
| 2677 | return bp->builtinfunc; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2678 | return NULL; |
| 2679 | } |
| 2680 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2681 | 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] | 2682 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2683 | pid_t newpid; |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2684 | int i; |
| 2685 | builtin_func_ptr bltin = NULL; |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2686 | const char *bltin_name = NULL; |
| 2687 | const char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2688 | struct ioword **iopp; |
| 2689 | int resetsig; |
| 2690 | char **owp; |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2691 | int forked; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2692 | |
| 2693 | int *hpin = pin; |
| 2694 | int *hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2695 | char *hwp; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 2696 | smallint hinteractive; |
| 2697 | smallint hintr; |
| 2698 | smallint hexecflg; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2699 | struct brkcon *hbrklist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2700 | |
| 2701 | #if __GNUC__ |
| 2702 | /* Avoid longjmp clobbering */ |
| 2703 | (void) &pin; |
| 2704 | (void) &pout; |
| 2705 | (void) ℘ |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2706 | (void) &bltin; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2707 | (void) &cp; |
| 2708 | (void) &resetsig; |
| 2709 | (void) &owp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2710 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2711 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2712 | DBGPRINTF(("FORKEXEC: t=%p, pin %p, pout %p, no_fork %d\n", t, pin, |
| 2713 | pout, no_fork)); |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2714 | DBGPRINTF7(("FORKEXEC: t->op_words is %s\n", |
| 2715 | ((t->op_words == NULL) ? "NULL" : t->op_words[0]))); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2716 | owp = wp; |
| 2717 | resetsig = 0; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2718 | if (t->op_type == TCOM) { |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 2719 | while (*wp++ != NULL) |
| 2720 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2721 | cp = *wp; |
| 2722 | |
| 2723 | /* strip all initial assignments */ |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2724 | /* FIXME: not correct wrt PATH=yyy command etc */ |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 2725 | if (FLAG['x']) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2726 | DBGPRINTF9(("FORKEXEC: echo'ing, cp=%p, wp=%p, owp=%p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2727 | cp, wp, owp)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2728 | echo(cp ? wp : owp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2729 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2730 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2731 | if (cp == NULL) { |
| 2732 | if (t->ioact == NULL) { |
| 2733 | while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
| 2734 | continue; |
| 2735 | DBGPRINTF(("FORKEXEC: returning setstatus(0)\n")); |
| 2736 | return setstatus(0); |
| 2737 | } |
| 2738 | } else { /* cp != NULL */ |
| 2739 | bltin_name = cp; |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2740 | bltin = inbuilt(cp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2741 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2742 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2743 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2744 | forked = 0; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2745 | // We were pointing t->op_words to temporary (expanded) arg list: |
| 2746 | // t->op_words = wp; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 2747 | // and restored it later (in execute()), but "break" |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2748 | // longjmps away (at "Run builtin" below), leaving t->op_words clobbered! |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 2749 | // See http://bugs.busybox.net/view.php?id=846. |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 2750 | // 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] | 2751 | // to builtins |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2752 | DBGPRINTF(("FORKEXEC: bltin %p, no_fork %d, owp %p\n", bltin, |
| 2753 | no_fork, owp)); |
| 2754 | /* Don't fork if it is a lone builtin (not in pipe) |
| 2755 | * OR we are told to _not_ fork */ |
| 2756 | if ((!bltin || pin || pout) /* not lone bltin AND */ |
| 2757 | && !no_fork /* not told to avoid fork */ |
| 2758 | ) { |
| 2759 | /* Save values in case child alters them after vfork */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2760 | hpin = pin; |
| 2761 | hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2762 | hwp = *wp; |
| 2763 | hinteractive = interactive; |
| 2764 | hintr = intr; |
| 2765 | hbrklist = brklist; |
| 2766 | hexecflg = execflg; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2767 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2768 | DBGPRINTF3(("FORKEXEC: calling vfork()...\n")); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2769 | newpid = vfork(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2770 | if (newpid == -1) { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 2771 | DBGPRINTF(("FORKEXEC: ERROR, can't vfork()!\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2772 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 2775 | if (newpid > 0) { /* Parent */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2776 | /* Restore values */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2777 | pin = hpin; |
| 2778 | pout = hpout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2779 | *wp = hwp; |
| 2780 | interactive = hinteractive; |
| 2781 | intr = hintr; |
| 2782 | brklist = hbrklist; |
| 2783 | execflg = hexecflg; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2784 | |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2785 | closepipe(pin); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2786 | return (pout == NULL ? setstatus(waitfor(newpid, 0)) : 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2789 | /* Child */ |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2790 | DBGPRINTF(("FORKEXEC: child process, bltin=%p (%s)\n", bltin, bltin_name)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2791 | if (interactive) { |
| 2792 | signal(SIGINT, SIG_IGN); |
| 2793 | signal(SIGQUIT, SIG_IGN); |
| 2794 | resetsig = 1; |
| 2795 | } |
| 2796 | interactive = 0; |
| 2797 | intr = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2798 | forked = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2799 | brklist = 0; |
| 2800 | execflg = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2801 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2802 | |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2803 | if (owp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2804 | while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2805 | if (!bltin) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2806 | export(lookup(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2807 | |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 2808 | if (pin) { /* NB: close _first_, then move fds! */ |
| 2809 | close(pin[1]); |
Denis Vlasenko | 2b54aaa | 2007-05-09 22:16:08 +0000 | [diff] [blame] | 2810 | xmove_fd(pin[0], 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2811 | } |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2812 | if (pout) { |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 2813 | close(pout[0]); |
Denis Vlasenko | 2b54aaa | 2007-05-09 22:16:08 +0000 | [diff] [blame] | 2814 | xmove_fd(pout[1], 1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2815 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2816 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2817 | iopp = t->ioact; |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2818 | if (iopp) { |
| 2819 | if (bltin && bltin != doexec) { |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2820 | prs(bltin_name); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 2821 | err(": can't redirect shell command"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2822 | if (forked) |
| 2823 | _exit(-1); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2824 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2825 | } |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2826 | while (*iopp) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2827 | if (iosetup(*iopp++, pin != NULL, pout != NULL)) { |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2828 | /* system-detected error */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2829 | if (forked) |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2830 | _exit(-1); |
| 2831 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2832 | } |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2833 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2834 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2835 | |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2836 | if (bltin) { |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2837 | if (forked || pin || pout) { |
| 2838 | /* Builtin in pipe: disallowed */ |
| 2839 | /* TODO: allow "exec"? */ |
| 2840 | prs(bltin_name); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 2841 | err(": can't run builtin as part of pipe"); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2842 | if (forked) |
| 2843 | _exit(-1); |
| 2844 | return -1; |
| 2845 | } |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 2846 | /* Run builtin */ |
| 2847 | i = setstatus(bltin(t, wp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2848 | if (forked) |
| 2849 | _exit(i); |
| 2850 | DBGPRINTF(("FORKEXEC: returning i=%d\n", i)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2851 | return i; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2854 | /* should use FIOCEXCL */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2855 | for (i = FDBASE; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2856 | close(i); |
| 2857 | if (resetsig) { |
| 2858 | signal(SIGINT, SIG_DFL); |
| 2859 | signal(SIGQUIT, SIG_DFL); |
| 2860 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2861 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2862 | if (t->op_type == TPAREN) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2863 | _exit(execute(t->left, NOPIPE, NOPIPE, /* no_fork: */ 1)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2864 | if (wp[0] == NULL) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 2865 | _exit(EXIT_SUCCESS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2866 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 2867 | cp = rexecve(wp[0], wp, makenv(0, NULL)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2868 | prs(wp[0]); |
| 2869 | prs(": "); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2870 | err(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2871 | if (!execflg) |
| 2872 | trap[0] = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2873 | |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2874 | DBGPRINTF(("FORKEXEC: calling leave(), pid=%d\n", getpid())); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2875 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2876 | leave(); |
| 2877 | /* NOTREACHED */ |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 2878 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | /* |
| 2882 | * 0< 1> are ignored as required |
| 2883 | * within pipelines. |
| 2884 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2885 | static int iosetup(struct ioword *iop, int pipein, int pipeout) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2886 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2887 | int u = -1; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2888 | char *cp = NULL; |
| 2889 | const char *msg; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2890 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2891 | DBGPRINTF(("IOSETUP: iop %p, pipein %i, pipeout %i\n", iop, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2892 | pipein, pipeout)); |
| 2893 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2894 | if (iop->io_fd == IODEFAULT) /* take default */ |
| 2895 | iop->io_fd = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2896 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2897 | if (pipein && iop->io_fd == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2898 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2899 | |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2900 | if (pipeout && iop->io_fd == 1) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2901 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2902 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2903 | msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2904 | if ((iop->io_flag & IOHERE) == 0) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2905 | cp = iop->io_name; /* huh?? */ |
| 2906 | cp = evalstr(cp, DOSUB | DOTRIM); |
| 2907 | if (cp == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2908 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2909 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2910 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2911 | if (iop->io_flag & IODUP) { |
| 2912 | if (cp[1] || (!isdigit(*cp) && *cp != '-')) { |
| 2913 | prs(cp); |
| 2914 | err(": illegal >& argument"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2915 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2916 | } |
| 2917 | if (*cp == '-') |
| 2918 | iop->io_flag = IOCLOSE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2919 | iop->io_flag &= ~(IOREAD | IOWRITE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2920 | } |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2921 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2922 | switch (iop->io_flag) { |
| 2923 | case IOREAD: |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 2924 | u = open(cp, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2925 | break; |
| 2926 | |
| 2927 | case IOHERE: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2928 | case IOHERE | IOXHERE: |
| 2929 | u = herein(iop->io_name, iop->io_flag & IOXHERE); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2930 | cp = (char*)"here file"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2931 | break; |
| 2932 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2933 | case IOWRITE | IOCAT: |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 2934 | u = open(cp, O_WRONLY); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2935 | if (u >= 0) { |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 2936 | lseek(u, (long) 0, SEEK_END); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2937 | break; |
| 2938 | } |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2939 | /* fall through to creation if >>file doesn't exist */ |
| 2940 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2941 | case IOWRITE: |
| 2942 | u = creat(cp, 0666); |
| 2943 | break; |
| 2944 | |
| 2945 | case IODUP: |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2946 | u = dup2(*cp - '0', iop->io_fd); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2947 | break; |
| 2948 | |
| 2949 | case IOCLOSE: |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2950 | close(iop->io_fd); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2951 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2952 | } |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 2953 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2954 | if (u < 0) { |
| 2955 | prs(cp); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 2956 | prs(": can't "); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2957 | warn(msg); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2958 | return 1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2959 | } |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 2960 | xmove_fd(u, iop->io_fd); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2961 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2962 | } |
| 2963 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2964 | /* |
| 2965 | * Enter a new loop level (marked for break/continue). |
| 2966 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2967 | static void brkset(struct brkcon *bc) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2968 | { |
| 2969 | bc->nextlev = brklist; |
| 2970 | brklist = bc; |
| 2971 | } |
| 2972 | |
| 2973 | /* |
| 2974 | * Wait for the last process created. |
| 2975 | * Print a message for each process found |
| 2976 | * that was killed by a signal. |
| 2977 | * Ignore interrupt signals while waiting |
| 2978 | * unless `canintr' is true. |
| 2979 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2980 | static int waitfor(int lastpid, int canintr) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2981 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2982 | int pid, rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2983 | int s; |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 2984 | smallint oheedint = heedint; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2985 | |
| 2986 | heedint = 0; |
| 2987 | rv = 0; |
| 2988 | do { |
| 2989 | pid = wait(&s); |
| 2990 | if (pid == -1) { |
| 2991 | if (errno != EINTR || canintr) |
| 2992 | break; |
| 2993 | } else { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2994 | rv = WAITSIG(s); |
| 2995 | if (rv != 0) { |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2996 | if (rv < ARRAY_SIZE(signame)) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2997 | if (signame[rv] != NULL) { |
| 2998 | if (pid != lastpid) { |
| 2999 | prn(pid); |
| 3000 | prs(": "); |
| 3001 | } |
| 3002 | prs(signame[rv]); |
| 3003 | } |
| 3004 | } else { |
| 3005 | if (pid != lastpid) { |
| 3006 | prn(pid); |
| 3007 | prs(": "); |
| 3008 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3009 | prs("Signal "); |
| 3010 | prn(rv); |
| 3011 | prs(" "); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3012 | } |
| 3013 | if (WAITCORE(s)) |
| 3014 | prs(" - core dumped"); |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 3015 | if (rv >= ARRAY_SIZE(signame) || signame[rv]) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3016 | prs("\n"); |
Mike Frysinger | b81f97b | 2008-05-14 11:51:04 +0000 | [diff] [blame] | 3017 | rv |= 0x80; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3018 | } else |
| 3019 | rv = WAITVAL(s); |
| 3020 | } |
| 3021 | } while (pid != lastpid); |
| 3022 | heedint = oheedint; |
| 3023 | if (intr) { |
| 3024 | if (interactive) { |
| 3025 | if (canintr) |
| 3026 | intr = 0; |
| 3027 | } else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3028 | if (exstat == 0) |
| 3029 | exstat = rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3030 | onintr(0); |
| 3031 | } |
| 3032 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3033 | return rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3036 | static int setstatus(int s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3037 | { |
| 3038 | exstat = s; |
| 3039 | setval(lookup("?"), putn(s)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3040 | return s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | /* |
| 3044 | * PATH-searching interface to execve. |
| 3045 | * If getenv("PATH") were kept up-to-date, |
| 3046 | * execvp might be used. |
| 3047 | */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3048 | static const char *rexecve(char *c, char **v, char **envp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3049 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3050 | const char *sp; |
| 3051 | char *tp; |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 3052 | int asis = 0; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3053 | char *name = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3054 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3055 | if (ENABLE_FEATURE_SH_STANDALONE) { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3056 | if (find_applet_by_name(name) >= 0) { |
Rob Landley | a299efb | 2006-08-10 21:46:43 +0000 | [diff] [blame] | 3057 | /* We have to exec here since we vforked. Running |
Denis Vlasenko | e4f2d06 | 2007-04-11 17:03:19 +0000 | [diff] [blame] | 3058 | * run_applet_and_exit() won't work and bad things |
Rob Landley | a299efb | 2006-08-10 21:46:43 +0000 | [diff] [blame] | 3059 | * will happen. */ |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 3060 | execve(bb_busybox_exec_path, v, envp); |
Rob Landley | a299efb | 2006-08-10 21:46:43 +0000 | [diff] [blame] | 3061 | } |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3062 | } |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3063 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3064 | DBGPRINTF(("REXECVE: c=%p, v=%p, envp=%p\n", c, v, envp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3065 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3066 | sp = any('/', c) ? "" : path->value; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3067 | asis = (*sp == '\0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3068 | while (asis || *sp != '\0') { |
| 3069 | asis = 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3070 | tp = global_env.linep; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3071 | for (; *sp != '\0'; tp++) { |
| 3072 | *tp = *sp++; |
| 3073 | if (*tp == ':') { |
| 3074 | asis = (*sp == '\0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3075 | break; |
| 3076 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3077 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3078 | if (tp != global_env.linep) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3079 | *tp++ = '/'; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3080 | strcpy(tp, c); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3081 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3082 | DBGPRINTF3(("REXECVE: global_env.linep is %s\n", global_env.linep)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3083 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3084 | execve(global_env.linep, v, envp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3085 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3086 | switch (errno) { |
| 3087 | case ENOEXEC: |
Denis Vlasenko | 447bd66 | 2008-05-30 22:28:32 +0000 | [diff] [blame] | 3088 | /* File is executable but file format isnt recognized */ |
| 3089 | /* Run it as a shell script */ |
| 3090 | /* (execve above didnt do it itself, unlike execvp) */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3091 | *v = global_env.linep; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3092 | v--; |
| 3093 | tp = *v; |
Denis Vlasenko | 447bd66 | 2008-05-30 22:28:32 +0000 | [diff] [blame] | 3094 | *v = (char*)DEFAULT_SHELL; |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 3095 | execve(DEFAULT_SHELL, v, envp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3096 | *v = tp; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3097 | return "no shell"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3098 | |
| 3099 | case ENOMEM: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3100 | return (char *) bb_msg_memory_exhausted; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3101 | |
| 3102 | case E2BIG: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3103 | return "argument list too long"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3104 | } |
| 3105 | } |
Denis Vlasenko | 447bd66 | 2008-05-30 22:28:32 +0000 | [diff] [blame] | 3106 | if (errno == ENOENT) { |
| 3107 | exstat = 127; /* standards require this */ |
| 3108 | return "not found"; |
| 3109 | } |
| 3110 | exstat = 126; /* mimic bash */ |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 3111 | return "can't execute"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | /* |
| 3115 | * Run the command produced by generator `f' |
| 3116 | * applied to stream `arg'. |
| 3117 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3118 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3119 | { |
| 3120 | struct op *otree; |
| 3121 | struct wdblock *swdlist; |
| 3122 | struct wdblock *siolist; |
| 3123 | jmp_buf ev, rt; |
| 3124 | xint *ofail; |
| 3125 | int rv; |
| 3126 | |
| 3127 | #if __GNUC__ |
| 3128 | /* Avoid longjmp clobbering */ |
| 3129 | (void) &rv; |
| 3130 | #endif |
| 3131 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3132 | DBGPRINTF(("RUN: enter, areanum %d, outtree %p, failpt %p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3133 | areanum, outtree, failpt)); |
| 3134 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3135 | areanum++; |
| 3136 | swdlist = wdlist; |
| 3137 | siolist = iolist; |
| 3138 | otree = outtree; |
| 3139 | ofail = failpt; |
| 3140 | rv = -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3141 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3142 | errpt = ev; |
| 3143 | if (newenv(setjmp(errpt)) == 0) { |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 3144 | wdlist = NULL; |
| 3145 | iolist = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3146 | pushio(argp, f); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3147 | global_env.iobase = global_env.iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3148 | yynerrs = 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3149 | failpt = rt; |
| 3150 | if (setjmp(failpt) == 0 && yyparse() == 0) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 3151 | rv = execute(outtree, NOPIPE, NOPIPE, /* no_fork: */ 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3152 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3153 | } else { |
| 3154 | DBGPRINTF(("RUN: error from newenv()!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3155 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3156 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3157 | wdlist = swdlist; |
| 3158 | iolist = siolist; |
| 3159 | failpt = ofail; |
| 3160 | outtree = otree; |
| 3161 | freearea(areanum--); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3162 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3163 | return rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | /* -------- do.c -------- */ |
| 3167 | |
| 3168 | /* |
| 3169 | * built-in commands: doX |
| 3170 | */ |
| 3171 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3172 | static int dohelp(struct op *t UNUSED_PARAM, char **args UNUSED_PARAM) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3173 | { |
| 3174 | int col; |
| 3175 | const struct builtincmd *x; |
| 3176 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3177 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3178 | "Built-in commands:\n" |
| 3179 | "------------------\n"); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3180 | |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 3181 | col = 0; |
| 3182 | x = builtincmds; |
| 3183 | while (x->name) { |
| 3184 | col += printf("%c%s", ((col == 0) ? '\t' : ' '), x->name); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3185 | if (col > 60) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 3186 | bb_putchar('\n'); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3187 | col = 0; |
| 3188 | } |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 3189 | x++; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3190 | } |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3191 | #if ENABLE_FEATURE_SH_STANDALONE |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3192 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3193 | const char *applet = applet_names; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3194 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3195 | while (*applet) { |
| 3196 | col += printf("%c%s", ((col == 0) ? '\t' : ' '), applet); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3197 | if (col > 60) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 3198 | bb_putchar('\n'); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3199 | col = 0; |
| 3200 | } |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3201 | applet += strlen(applet) + 1; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3202 | } |
| 3203 | } |
| 3204 | #endif |
Denis Vlasenko | 0ee3999 | 2006-12-24 15:23:28 +0000 | [diff] [blame] | 3205 | puts("\n"); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3206 | return EXIT_SUCCESS; |
| 3207 | } |
| 3208 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3209 | static int dolabel(struct op *t UNUSED_PARAM, char **args UNUSED_PARAM) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3210 | { |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3211 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3214 | static int dochdir(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3215 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3216 | const char *cp, *er; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3217 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3218 | cp = args[1]; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3219 | if (cp == NULL) { |
| 3220 | cp = homedir->value; |
Denis Vlasenko | d244c5e | 2007-02-09 17:30:14 +0000 | [diff] [blame] | 3221 | if (cp != NULL) |
| 3222 | goto do_cd; |
| 3223 | er = ": no home directory"; |
| 3224 | } else { |
| 3225 | do_cd: |
| 3226 | if (chdir(cp) >= 0) |
| 3227 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3228 | er = ": bad directory"; |
Denis Vlasenko | d244c5e | 2007-02-09 17:30:14 +0000 | [diff] [blame] | 3229 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3230 | prs(cp != NULL ? cp : "cd"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3231 | err(er); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3232 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3235 | static int doshift(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3236 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3237 | int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3238 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3239 | n = args[1] ? getn(args[1]) : 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3240 | if (dolc < n) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3241 | err("nothing to shift"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3242 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3243 | } |
| 3244 | dolv[n] = dolv[0]; |
| 3245 | dolv += n; |
| 3246 | dolc -= n; |
| 3247 | setval(lookup("#"), putn(dolc)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3248 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | /* |
| 3252 | * execute login and newgrp directly |
| 3253 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3254 | static int dologin(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3255 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3256 | const char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3257 | |
| 3258 | if (interactive) { |
| 3259 | signal(SIGINT, SIG_DFL); |
| 3260 | signal(SIGQUIT, SIG_DFL); |
| 3261 | } |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3262 | cp = rexecve(args[0], args, makenv(0, NULL)); |
| 3263 | prs(args[0]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3264 | prs(": "); |
| 3265 | err(cp); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3266 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3267 | } |
| 3268 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3269 | static int doumask(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3270 | { |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 3271 | int i; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3272 | char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3273 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3274 | cp = args[1]; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3275 | if (cp == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3276 | i = umask(0); |
| 3277 | umask(i); |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 3278 | printf("%04o\n", i); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3279 | } else { |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 3280 | i = bb_strtou(cp, NULL, 8); |
| 3281 | if (errno) { |
| 3282 | err("umask: bad octal number"); |
| 3283 | return 1; |
| 3284 | } |
| 3285 | umask(i); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3286 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3287 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3290 | static int doexec(struct op *t, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3291 | { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3292 | jmp_buf ex; |
| 3293 | xint *ofail; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3294 | char **sv_words; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3295 | |
| 3296 | t->ioact = NULL; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3297 | if (!args[1]) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3298 | return 1; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3299 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3300 | execflg = 1; |
| 3301 | ofail = failpt; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3302 | failpt = ex; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3303 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3304 | sv_words = t->op_words; |
| 3305 | t->op_words = args + 1; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3306 | // TODO: test what will happen with "exec break" - |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3307 | // will it leave t->op_words pointing to garbage? |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3308 | // (see http://bugs.busybox.net/view.php?id=846) |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3309 | if (setjmp(failpt) == 0) |
Denis Vlasenko | 7e49752 | 2008-02-12 09:51:03 +0000 | [diff] [blame] | 3310 | execute(t, NOPIPE, NOPIPE, /* no_fork: */ 1); |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3311 | t->op_words = sv_words; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3312 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3313 | failpt = ofail; |
| 3314 | execflg = 0; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3315 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3316 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3319 | static int dodot(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3320 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3321 | int i; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3322 | const char *sp; |
| 3323 | char *tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3324 | char *cp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3325 | int maltmp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3326 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3327 | DBGPRINTF(("DODOT: enter, t=%p, tleft %p, tright %p, global_env.linep is %s\n", |
| 3328 | 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] | 3329 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3330 | cp = args[1]; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3331 | if (cp == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3332 | DBGPRINTF(("DODOT: bad args, ret 0\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3333 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3334 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3335 | DBGPRINTF(("DODOT: cp is %s\n", cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3336 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3337 | sp = any('/', cp) ? ":" : path->value; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3338 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3339 | DBGPRINTF(("DODOT: sp is %s, global_env.linep is %s\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3340 | ((sp == NULL) ? "NULL" : sp), |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3341 | ((global_env.linep == NULL) ? "NULL" : global_env.linep))); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3342 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3343 | while (*sp) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3344 | tp = global_env.linep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3345 | while (*sp && (*tp = *sp++) != ':') |
| 3346 | tp++; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3347 | if (tp != global_env.linep) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3348 | *tp++ = '/'; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3349 | strcpy(tp, cp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3350 | |
| 3351 | /* Original code */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3352 | i = open(global_env.linep, O_RDONLY); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3353 | if (i >= 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3354 | exstat = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3355 | maltmp = remap(i); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3356 | DBGPRINTF(("DODOT: remap=%d, exstat=%d, global_env.iofd %d, i %d, global_env.linep is %s\n", |
| 3357 | maltmp, exstat, global_env.iofd, i, global_env.linep)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3358 | |
| 3359 | next(maltmp); /* Basically a PUSHIO */ |
| 3360 | |
| 3361 | DBGPRINTF(("DODOT: returning exstat=%d\n", exstat)); |
| 3362 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3363 | return exstat; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3364 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3365 | } /* while */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3366 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3367 | prs(cp); |
| 3368 | err(": not found"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3369 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3370 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3373 | static int dowait(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3374 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3375 | int i; |
| 3376 | char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3377 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3378 | cp = args[1]; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3379 | if (cp != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3380 | i = getn(cp); |
| 3381 | if (i == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3382 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3383 | } else |
| 3384 | i = -1; |
| 3385 | setstatus(waitfor(i, 1)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3386 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3387 | } |
| 3388 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3389 | static int doread(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3390 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3391 | char *cp, **wp; |
| 3392 | int nb = 0; |
| 3393 | int nl = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3394 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3395 | if (args[1] == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3396 | err("Usage: read name ..."); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3397 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3398 | } |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3399 | for (wp = args + 1; *wp; wp++) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3400 | for (cp = global_env.linep; !nl && cp < elinep - 1; cp++) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 3401 | nb = nonblock_safe_read(STDIN_FILENO, cp, sizeof(*cp)); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3402 | if (nb != sizeof(*cp)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3403 | break; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3404 | nl = (*cp == '\n'); |
| 3405 | if (nl || (wp[1] && any(*cp, ifs->value))) |
| 3406 | break; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3407 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3408 | *cp = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3409 | if (nb <= 0) |
| 3410 | break; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3411 | setval(lookup(*wp), global_env.linep); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3412 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3413 | return nb <= 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3416 | static int doeval(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3417 | { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3418 | return RUN(awordlist, args + 1, wdchar); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3421 | static int dotrap(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3422 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3423 | int n, i; |
| 3424 | int resetsig; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3425 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3426 | if (args[1] == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3427 | for (i = 0; i <= _NSIG; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3428 | if (trap[i]) { |
| 3429 | prn(i); |
| 3430 | prs(": "); |
| 3431 | prs(trap[i]); |
| 3432 | prs("\n"); |
| 3433 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3434 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3435 | } |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3436 | resetsig = isdigit(args[1][0]); |
| 3437 | for (i = resetsig ? 1 : 2; args[i] != NULL; ++i) { |
| 3438 | n = getsig(args[i]); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3439 | freecell(trap[n]); |
| 3440 | trap[n] = 0; |
| 3441 | if (!resetsig) { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3442 | if (args[1][0] != '\0') { |
| 3443 | trap[n] = strsave(args[1], 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3444 | setsig(n, sig); |
| 3445 | } else |
| 3446 | setsig(n, SIG_IGN); |
| 3447 | } else { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 3448 | if (interactive) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3449 | if (n == SIGINT) |
| 3450 | setsig(n, onintr); |
| 3451 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3452 | setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 3453 | } else |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3454 | setsig(n, SIG_DFL); |
| 3455 | } |
| 3456 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3457 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3460 | static int getsig(char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3461 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3462 | int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3463 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3464 | n = getn(s); |
| 3465 | if (n < 0 || n > _NSIG) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3466 | err("trap: bad signal number"); |
| 3467 | n = 0; |
| 3468 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3469 | return n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3472 | static void setsig(int n, sighandler_t f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3473 | { |
| 3474 | if (n == 0) |
| 3475 | return; |
| 3476 | if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) { |
| 3477 | ourtrap[n] = 1; |
| 3478 | signal(n, f); |
| 3479 | } |
| 3480 | } |
| 3481 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3482 | static int getn(char *as) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3483 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3484 | char *s; |
| 3485 | int n, m; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3486 | |
| 3487 | s = as; |
| 3488 | m = 1; |
| 3489 | if (*s == '-') { |
| 3490 | m = -1; |
| 3491 | s++; |
| 3492 | } |
| 3493 | for (n = 0; isdigit(*s); s++) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3494 | n = (n * 10) + (*s - '0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3495 | if (*s) { |
| 3496 | prs(as); |
| 3497 | err(": bad number"); |
| 3498 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3499 | return n * m; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3502 | static int dobreak(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3503 | { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3504 | return brkcontin(args[1], 1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3507 | static int docontinue(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3508 | { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3509 | return brkcontin(args[1], 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3512 | static int brkcontin(char *cp, int val) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3513 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3514 | struct brkcon *bc; |
| 3515 | int nl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3516 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3517 | nl = cp == NULL ? 1 : getn(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3518 | if (nl <= 0) |
| 3519 | nl = 999; |
| 3520 | do { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3521 | bc = brklist; |
| 3522 | if (bc == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3523 | break; |
| 3524 | brklist = bc->nextlev; |
| 3525 | } while (--nl); |
| 3526 | if (nl) { |
| 3527 | err("bad break/continue level"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3528 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3529 | } |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 3530 | isbreak = (val != 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3531 | longjmp(bc->brkpt, 1); |
| 3532 | /* NOTREACHED */ |
| 3533 | } |
| 3534 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3535 | static int doexit(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3536 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3537 | char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3538 | |
| 3539 | execflg = 0; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3540 | cp = args[1]; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3541 | if (cp != NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3542 | setstatus(getn(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3543 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3544 | DBGPRINTF(("DOEXIT: calling leave(), t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3545 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3546 | leave(); |
| 3547 | /* NOTREACHED */ |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3548 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3551 | static int doexport(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3552 | { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3553 | rdexp(args + 1, export, EXPORT); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3554 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3555 | } |
| 3556 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3557 | static int doreadonly(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3558 | { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3559 | rdexp(args + 1, ronly, RONLY); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3560 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3563 | static void rdexp(char **wp, void (*f) (struct var *), int key) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3564 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3565 | 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] | 3566 | DBGPRINTF6(("RDEXP: *wp=%s\n", *wp)); |
| 3567 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3568 | if (*wp != NULL) { |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3569 | for (; *wp != NULL; wp++) { |
| 3570 | if (isassign(*wp)) { |
| 3571 | char *cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3572 | |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3573 | assign(*wp, COPYV); |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 3574 | for (cp = *wp; *cp != '='; cp++) |
| 3575 | continue; |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3576 | *cp = '\0'; |
| 3577 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3578 | if (checkname(*wp)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3579 | (*f) (lookup(*wp)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3580 | else |
| 3581 | badid(*wp); |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3582 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3583 | } else |
| 3584 | putvlist(key, 1); |
| 3585 | } |
| 3586 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3587 | static void badid(char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3588 | { |
| 3589 | prs(s); |
| 3590 | err(": bad identifier"); |
| 3591 | } |
| 3592 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3593 | static int doset(struct op *t UNUSED_PARAM, char **args) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3594 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3595 | struct var *vp; |
| 3596 | char *cp; |
| 3597 | int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3598 | |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3599 | cp = args[1]; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3600 | if (cp == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3601 | for (vp = vlist; vp; vp = vp->next) |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 3602 | varput(vp->name, STDOUT_FILENO); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3603 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3604 | } |
| 3605 | if (*cp == '-') { |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3606 | args++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3607 | if (*++cp == 0) |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3608 | FLAG['x'] = FLAG['v'] = 0; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3609 | else { |
| 3610 | for (; *cp; cp++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3611 | switch (*cp) { |
| 3612 | case 'e': |
| 3613 | if (!interactive) |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3614 | FLAG['e']++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3615 | break; |
| 3616 | |
| 3617 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3618 | if (*cp >= 'a' && *cp <= 'z') |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3619 | FLAG[(int) *cp]++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3620 | break; |
| 3621 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3622 | } |
| 3623 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3624 | setdash(); |
| 3625 | } |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3626 | if (args[1]) { |
| 3627 | args[0] = dolv[0]; |
| 3628 | for (n = 1; args[n]; n++) |
| 3629 | setarea((char *) args[n], 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3630 | dolc = n - 1; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3631 | dolv = args; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3632 | setval(lookup("#"), putn(dolc)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3633 | setarea((char *) (dolv - 1), 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3634 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3635 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3636 | } |
| 3637 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3638 | static void varput(char *s, int out) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3639 | { |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3640 | if (isalnum(*s) || *s == '_') { |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 3641 | xwrite_str(out, s); |
| 3642 | xwrite(out, "\n", 1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | |
| 3647 | /* |
| 3648 | * Copyright (c) 1999 Herbert Xu <herbert@debian.org> |
| 3649 | * This file contains code for the times builtin. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3650 | */ |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3651 | static void times_fmt(char *buf, clock_t val, unsigned clk_tck) |
| 3652 | { |
| 3653 | unsigned min, sec; |
| 3654 | if (sizeof(val) > sizeof(int)) |
| 3655 | sec = ((unsigned long)val) / clk_tck; |
| 3656 | else |
| 3657 | sec = ((unsigned)val) / clk_tck; |
| 3658 | min = sec / 60; |
| 3659 | #if ENABLE_DESKTOP |
| 3660 | sprintf(buf, "%um%u.%03us", min, (sec - min * 60), |
| 3661 | /* msec: */ ((unsigned)(val - (clock_t)sec * clk_tck)) * 1000 / clk_tck |
| 3662 | ); |
| 3663 | #else |
| 3664 | sprintf(buf, "%um%us", min, (sec - min * 60)); |
| 3665 | #endif |
| 3666 | } |
| 3667 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3668 | static int dotimes(struct op *t UNUSED_PARAM, char **args UNUSED_PARAM) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3669 | { |
| 3670 | struct tms buf; |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3671 | unsigned clk_tck = sysconf(_SC_CLK_TCK); |
| 3672 | /* How much do we need for "NmN.NNNs" ? */ |
| 3673 | enum { TIMEBUF_SIZE = sizeof(int)*3 + sizeof(int)*3 + 6 }; |
| 3674 | char u[TIMEBUF_SIZE], s[TIMEBUF_SIZE]; |
| 3675 | char cu[TIMEBUF_SIZE], cs[TIMEBUF_SIZE]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3676 | |
| 3677 | times(&buf); |
Denis Vlasenko | fe21883 | 2008-03-01 09:35:39 +0000 | [diff] [blame] | 3678 | |
| 3679 | times_fmt(u, buf.tms_utime, clk_tck); |
| 3680 | times_fmt(s, buf.tms_stime, clk_tck); |
| 3681 | times_fmt(cu, buf.tms_cutime, clk_tck); |
| 3682 | times_fmt(cs, buf.tms_cstime, clk_tck); |
| 3683 | |
| 3684 | printf("%s %s\n%s %s\n", u, s, cu, cs); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3685 | return 0; |
| 3686 | } |
| 3687 | |
| 3688 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3689 | /* -------- eval.c -------- */ |
| 3690 | |
| 3691 | /* |
| 3692 | * ${} |
| 3693 | * `command` |
| 3694 | * blank interpretation |
| 3695 | * quoting |
| 3696 | * glob |
| 3697 | */ |
| 3698 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3699 | static char **eval(char **ap, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3700 | { |
| 3701 | struct wdblock *wb; |
| 3702 | char **wp; |
| 3703 | char **wf; |
| 3704 | jmp_buf ev; |
| 3705 | |
| 3706 | #if __GNUC__ |
| 3707 | /* Avoid longjmp clobbering */ |
| 3708 | (void) ℘ |
| 3709 | (void) ≈ |
| 3710 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3711 | |
| 3712 | DBGPRINTF4(("EVAL: enter, f=%d\n", f)); |
| 3713 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3714 | wp = NULL; |
| 3715 | wb = NULL; |
| 3716 | wf = NULL; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3717 | errpt = ev; |
| 3718 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3719 | while (*ap && isassign(*ap)) |
| 3720 | expand(*ap++, &wb, f & ~DOGLOB); |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3721 | if (FLAG['k']) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3722 | for (wf = ap; *wf; wf++) { |
| 3723 | if (isassign(*wf)) |
| 3724 | expand(*wf, &wb, f & ~DOGLOB); |
| 3725 | } |
| 3726 | } |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 3727 | for (wb = addword((char *) NULL, wb); *ap; ap++) { |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3728 | if (!FLAG['k'] || !isassign(*ap)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3729 | expand(*ap, &wb, f & ~DOKEY); |
| 3730 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3731 | wb = addword((char *) 0, wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3732 | wp = getwords(wb); |
| 3733 | quitenv(); |
| 3734 | } else |
| 3735 | gflg = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3736 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3737 | return gflg ? (char **) NULL : wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3740 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3741 | /* |
| 3742 | * Make the exported environment from the exported |
| 3743 | * names in the dictionary. Keyword assignments |
| 3744 | * will already have been done. |
| 3745 | */ |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3746 | static char **makenv(int all, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3747 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3748 | struct var *vp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3749 | |
| 3750 | DBGPRINTF5(("MAKENV: enter, all=%d\n", all)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3751 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3752 | for (vp = vlist; vp; vp = vp->next) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3753 | if (all || vp->status & EXPORT) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3754 | wb = addword(vp->name, wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3755 | wb = addword((char *) 0, wb); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3756 | return getwords(wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3759 | static int expand(const char *cp, struct wdblock **wbp, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3760 | { |
| 3761 | jmp_buf ev; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3762 | char *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3763 | |
| 3764 | #if __GNUC__ |
| 3765 | /* Avoid longjmp clobbering */ |
| 3766 | (void) &cp; |
| 3767 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3768 | |
| 3769 | DBGPRINTF3(("EXPAND: enter, f=%d\n", f)); |
| 3770 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3771 | gflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3772 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3773 | if (cp == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3774 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3775 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3776 | if (!anys("$`'\"", cp) && !anys(ifs->value, cp) |
| 3777 | && ((f & DOGLOB) == 0 || !anys("[*?", cp)) |
| 3778 | ) { |
| 3779 | xp = strsave(cp, areanum); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3780 | if (f & DOTRIM) |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3781 | unquote(xp); |
| 3782 | *wbp = addword(xp, *wbp); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3783 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3784 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3785 | errpt = ev; |
| 3786 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3787 | PUSHIO(aword, cp, strchar); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3788 | global_env.iobase = global_env.iop; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3789 | while ((xp = blank(f)) && gflg == 0) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3790 | global_env.linep = xp; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3791 | xp = strsave(xp, areanum); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3792 | if ((f & DOGLOB) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3793 | if (f & DOTRIM) |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3794 | unquote(xp); |
| 3795 | *wbp = addword(xp, *wbp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3796 | } else |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3797 | *wbp = glob(xp, *wbp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3798 | } |
| 3799 | quitenv(); |
| 3800 | } else |
| 3801 | gflg = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3802 | return gflg == 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3805 | static char *evalstr(char *cp, int f) |
| 3806 | { |
| 3807 | struct wdblock *wb; |
| 3808 | |
| 3809 | DBGPRINTF6(("EVALSTR: enter, cp=%p, f=%d\n", cp, f)); |
| 3810 | |
| 3811 | wb = NULL; |
| 3812 | if (expand(cp, &wb, f)) { |
| 3813 | if (wb == NULL || wb->w_nword == 0 |
| 3814 | || (cp = wb->w_words[0]) == NULL |
| 3815 | ) { |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 3816 | // TODO: I suspect that |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3817 | // char *evalstr(char *cp, int f) is actually |
| 3818 | // const char *evalstr(const char *cp, int f)! |
| 3819 | cp = (char*)""; |
| 3820 | } |
| 3821 | DELETE(wb); |
| 3822 | } else |
| 3823 | cp = NULL; |
| 3824 | return cp; |
| 3825 | } |
| 3826 | |
| 3827 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3828 | /* |
| 3829 | * Blank interpretation and quoting |
| 3830 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3831 | static char *blank(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3832 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3833 | int c, c1; |
| 3834 | char *sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3835 | int scanequals, foundequals; |
| 3836 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3837 | DBGPRINTF3(("BLANK: enter, f=%d\n", f)); |
| 3838 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3839 | sp = global_env.linep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3840 | scanequals = f & DOKEY; |
| 3841 | foundequals = 0; |
| 3842 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3843 | loop: |
| 3844 | c = subgetc('"', foundequals); |
| 3845 | switch (c) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3846 | case 0: |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3847 | if (sp == global_env.linep) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3848 | return 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3849 | *global_env.linep++ = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3850 | return sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3851 | |
| 3852 | default: |
| 3853 | if (f & DOBLANK && any(c, ifs->value)) |
| 3854 | goto loop; |
| 3855 | break; |
| 3856 | |
| 3857 | case '"': |
| 3858 | case '\'': |
| 3859 | scanequals = 0; |
| 3860 | if (INSUB()) |
| 3861 | break; |
| 3862 | for (c1 = c; (c = subgetc(c1, 1)) != c1;) { |
| 3863 | if (c == 0) |
| 3864 | break; |
| 3865 | if (c == '\'' || !any(c, "$`\"")) |
| 3866 | c |= QUOTE; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3867 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3868 | } |
| 3869 | c = 0; |
| 3870 | } |
| 3871 | unget(c); |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3872 | if (!isalpha(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3873 | scanequals = 0; |
| 3874 | for (;;) { |
| 3875 | c = subgetc('"', foundequals); |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 3876 | if (c == 0 |
| 3877 | || f & (DOBLANK && any(c, ifs->value)) |
| 3878 | || (!INSUB() && any(c, "\"'")) |
| 3879 | ) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3880 | scanequals = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3881 | unget(c); |
| 3882 | if (any(c, "\"'")) |
| 3883 | goto loop; |
| 3884 | break; |
| 3885 | } |
| 3886 | if (scanequals) { |
| 3887 | if (c == '=') { |
| 3888 | foundequals = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3889 | scanequals = 0; |
| 3890 | } else if (!isalnum(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3891 | scanequals = 0; |
| 3892 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3893 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3894 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3895 | *global_env.linep++ = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3896 | return sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3897 | } |
| 3898 | |
| 3899 | /* |
| 3900 | * Get characters, substituting for ` and $ |
| 3901 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3902 | static int subgetc(char ec, int quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3903 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3904 | char c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3905 | |
| 3906 | DBGPRINTF3(("SUBGETC: enter, quoted=%d\n", quoted)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3907 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3908 | again: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3909 | c = my_getc(ec); |
| 3910 | if (!INSUB() && ec != '\'') { |
| 3911 | if (c == '`') { |
| 3912 | if (grave(quoted) == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3913 | return 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3914 | global_env.iop->task = XGRAVE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3915 | goto again; |
| 3916 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3917 | if (c == '$') { |
| 3918 | c = dollar(quoted); |
| 3919 | if (c == 0) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3920 | global_env.iop->task = XDOLL; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3921 | goto again; |
| 3922 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3923 | } |
| 3924 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3925 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
| 3928 | /* |
| 3929 | * Prepare to generate the string returned by ${} substitution. |
| 3930 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3931 | static int dollar(int quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3932 | { |
| 3933 | int otask; |
| 3934 | struct io *oiop; |
| 3935 | char *dolp; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3936 | char *s, c, *cp = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3937 | struct var *vp; |
| 3938 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3939 | DBGPRINTF3(("DOLLAR: enter, quoted=%d\n", quoted)); |
| 3940 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3941 | c = readc(); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3942 | s = global_env.linep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3943 | if (c != '{') { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3944 | *global_env.linep++ = c; |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3945 | if (isalpha(c) || c == '_') { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3946 | while ((c = readc()) != 0 && (isalnum(c) || c == '_')) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3947 | if (global_env.linep < elinep) |
| 3948 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3949 | unget(c); |
| 3950 | } |
| 3951 | c = 0; |
| 3952 | } else { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3953 | oiop = global_env.iop; |
| 3954 | otask = global_env.iop->task; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3955 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3956 | global_env.iop->task = XOTHER; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3957 | while ((c = subgetc('"', 0)) != 0 && c != '}' && c != '\n') |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3958 | if (global_env.linep < elinep) |
| 3959 | *global_env.linep++ = c; |
| 3960 | if (oiop == global_env.iop) |
| 3961 | global_env.iop->task = otask; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3962 | if (c != '}') { |
| 3963 | err("unclosed ${"); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 3964 | gflg = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3965 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3966 | } |
| 3967 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3968 | if (global_env.linep >= elinep) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3969 | err("string in ${} too long"); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 3970 | gflg = 1; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3971 | global_env.linep -= 10; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3972 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3973 | *global_env.linep = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3974 | if (*s) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3975 | for (cp = s + 1; *cp; cp++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3976 | if (any(*cp, "=-+?")) { |
| 3977 | c = *cp; |
| 3978 | *cp++ = 0; |
| 3979 | break; |
| 3980 | } |
| 3981 | if (s[1] == 0 && (*s == '*' || *s == '@')) { |
| 3982 | if (dolc > 1) { |
| 3983 | /* currently this does not distinguish $* and $@ */ |
| 3984 | /* should check dollar */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3985 | global_env.linep = s; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3986 | PUSHIO(awordlist, dolv + 1, dolchar); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3987 | return 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3988 | } else { /* trap the nasty ${=} */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3989 | s[0] = '1'; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3990 | s[1] = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3991 | } |
| 3992 | } |
| 3993 | vp = lookup(s); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3994 | dolp = vp->value; |
| 3995 | if (dolp == null) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3996 | switch (c) { |
| 3997 | case '=': |
| 3998 | if (isdigit(*s)) { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 3999 | err("can't use ${...=...} with $n"); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 4000 | gflg = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4001 | break; |
| 4002 | } |
| 4003 | setval(vp, cp); |
| 4004 | dolp = vp->value; |
| 4005 | break; |
| 4006 | |
| 4007 | case '-': |
| 4008 | dolp = strsave(cp, areanum); |
| 4009 | break; |
| 4010 | |
| 4011 | case '?': |
| 4012 | if (*cp == 0) { |
| 4013 | prs("missing value for "); |
| 4014 | err(s); |
| 4015 | } else |
| 4016 | err(cp); |
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 | break; |
| 4019 | } |
| 4020 | } else if (c == '+') |
| 4021 | dolp = strsave(cp, areanum); |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 4022 | if (FLAG['u'] && dolp == null) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4023 | prs("unset variable: "); |
| 4024 | err(s); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 4025 | gflg = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4026 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4027 | global_env.linep = s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4028 | PUSHIO(aword, dolp, quoted ? qstrchar : strchar); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4029 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
| 4032 | /* |
| 4033 | * Run the command in `...` and read its output. |
| 4034 | */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4035 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4036 | static int grave(int quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4037 | { |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 4038 | /* moved to G: static char child_cmd[LINELIM]; */ |
| 4039 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4040 | const char *cp; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4041 | int i; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4042 | int j; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4043 | int pf[2]; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4044 | const char *src; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4045 | char *dest; |
| 4046 | int count; |
| 4047 | int ignore; |
| 4048 | int ignore_once; |
| 4049 | char *argument_list[4]; |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4050 | struct wdblock *wb = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4051 | |
| 4052 | #if __GNUC__ |
| 4053 | /* Avoid longjmp clobbering */ |
| 4054 | (void) &cp; |
| 4055 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4056 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4057 | for (cp = global_env.iop->argp->aword; *cp != '`'; cp++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4058 | if (*cp == 0) { |
| 4059 | err("no closing `"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4060 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4061 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4062 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4063 | |
| 4064 | /* string copy with dollar expansion */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4065 | src = global_env.iop->argp->aword; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4066 | dest = child_cmd; |
| 4067 | count = 0; |
| 4068 | ignore = 0; |
| 4069 | ignore_once = 0; |
| 4070 | while ((*src != '`') && (count < LINELIM)) { |
| 4071 | if (*src == '\'') |
| 4072 | ignore = !ignore; |
| 4073 | if (*src == '\\') |
| 4074 | ignore_once = 1; |
| 4075 | if (*src == '$' && !ignore && !ignore_once) { |
| 4076 | struct var *vp; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 4077 | /* moved to G to reduce stack usage |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4078 | char var_name[LINELIM]; |
| 4079 | char alt_value[LINELIM]; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 4080 | */ |
| 4081 | #define var_name (G.grave__var_name) |
| 4082 | #define alt_value (G.grave__alt_value) |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4083 | int var_index = 0; |
| 4084 | int alt_index = 0; |
| 4085 | char operator = 0; |
| 4086 | int braces = 0; |
| 4087 | char *value; |
| 4088 | |
| 4089 | src++; |
| 4090 | if (*src == '{') { |
| 4091 | braces = 1; |
| 4092 | src++; |
| 4093 | } |
| 4094 | |
| 4095 | var_name[var_index++] = *src++; |
Paul Fox | 54690dc | 2005-07-20 18:33:12 +0000 | [diff] [blame] | 4096 | while (isalnum(*src) || *src=='_') |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4097 | var_name[var_index++] = *src++; |
| 4098 | var_name[var_index] = 0; |
| 4099 | |
| 4100 | if (braces) { |
| 4101 | switch (*src) { |
| 4102 | case '}': |
| 4103 | break; |
| 4104 | case '-': |
| 4105 | case '=': |
| 4106 | case '+': |
| 4107 | case '?': |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4108 | operator = * src; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4109 | break; |
| 4110 | default: |
| 4111 | err("unclosed ${\n"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4112 | return 0; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4113 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4114 | if (operator) { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4115 | src++; |
| 4116 | while (*src && (*src != '}')) { |
| 4117 | alt_value[alt_index++] = *src++; |
| 4118 | } |
| 4119 | alt_value[alt_index] = 0; |
| 4120 | if (*src != '}') { |
| 4121 | err("unclosed ${\n"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4122 | return 0; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4123 | } |
| 4124 | } |
| 4125 | src++; |
| 4126 | } |
| 4127 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4128 | if (isalpha(*var_name)) { |
| 4129 | /* let subshell handle it instead */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4130 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4131 | char *namep = var_name; |
| 4132 | |
| 4133 | *dest++ = '$'; |
| 4134 | if (braces) |
| 4135 | *dest++ = '{'; |
| 4136 | while (*namep) |
| 4137 | *dest++ = *namep++; |
| 4138 | if (operator) { |
| 4139 | char *altp = alt_value; |
| 4140 | *dest++ = operator; |
| 4141 | while (*altp) |
| 4142 | *dest++ = *altp++; |
| 4143 | } |
| 4144 | if (braces) |
| 4145 | *dest++ = '}'; |
| 4146 | |
| 4147 | wb = addword(lookup(var_name)->name, wb); |
| 4148 | } else { |
| 4149 | /* expand */ |
| 4150 | |
| 4151 | vp = lookup(var_name); |
| 4152 | if (vp->value != null) |
| 4153 | value = (operator == '+') ? |
| 4154 | alt_value : vp->value; |
| 4155 | else if (operator == '?') { |
| 4156 | err(alt_value); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4157 | return 0; |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4158 | } else if (alt_index && (operator != '+')) { |
| 4159 | value = alt_value; |
| 4160 | if (operator == '=') |
| 4161 | setval(vp, value); |
| 4162 | } else |
| 4163 | continue; |
| 4164 | |
| 4165 | while (*value && (count < LINELIM)) { |
| 4166 | *dest++ = *value++; |
| 4167 | count++; |
| 4168 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4169 | } |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 4170 | #undef var_name |
| 4171 | #undef alt_value |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4172 | } else { |
| 4173 | *dest++ = *src++; |
| 4174 | count++; |
| 4175 | ignore_once = 0; |
| 4176 | } |
| 4177 | } |
| 4178 | *dest = '\0'; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4179 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4180 | if (openpipe(pf) < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4181 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4182 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4183 | while ((i = vfork()) == -1 && errno == EAGAIN) |
| 4184 | continue; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4185 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4186 | DBGPRINTF3(("GRAVE: i is %p\n", io)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4187 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4188 | if (i < 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4189 | closepipe(pf); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4190 | err((char *) bb_msg_memory_exhausted); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4191 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4192 | } |
| 4193 | if (i != 0) { |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 4194 | waitpid(i, NULL, 0); // safe_waitpid? |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4195 | global_env.iop->argp->aword = ++cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4196 | close(pf[1]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4197 | PUSHIO(afile, remap(pf[0]), |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4198 | (int (*)(struct ioarg *)) ((quoted) ? qgravechar : gravechar)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4199 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4200 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4201 | /* allow trapped signals */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4202 | /* XXX - Maybe this signal stuff should go as well? */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4203 | for (j = 0; j <= _NSIG; j++) |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4204 | if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN) |
| 4205 | signal(j, SIG_DFL); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4206 | |
Denis Vlasenko | 2b54aaa | 2007-05-09 22:16:08 +0000 | [diff] [blame] | 4207 | /* Testcase where below checks are needed: |
| 4208 | * close stdout & run this script: |
| 4209 | * files=`ls` |
| 4210 | * echo "$files" >zz |
| 4211 | */ |
| 4212 | xmove_fd(pf[1], 1); |
Denis Vlasenko | 847fa77 | 2008-01-28 22:45:43 +0000 | [diff] [blame] | 4213 | if (pf[0] != 1) |
| 4214 | close(pf[0]); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4215 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4216 | argument_list[0] = (char *) DEFAULT_SHELL; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4217 | argument_list[1] = (char *) "-c"; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4218 | argument_list[2] = child_cmd; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4219 | argument_list[3] = NULL; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4220 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4221 | cp = rexecve(argument_list[0], argument_list, makenv(1, wb)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4222 | prs(argument_list[0]); |
| 4223 | prs(": "); |
| 4224 | err(cp); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 4225 | _exit(EXIT_FAILURE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4226 | } |
| 4227 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4228 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4229 | static char *unquote(char *as) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4230 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4231 | char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4232 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4233 | s = as; |
| 4234 | if (s != NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4235 | while (*s) |
| 4236 | *s++ &= ~QUOTE; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4237 | return as; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
| 4240 | /* -------- glob.c -------- */ |
| 4241 | |
| 4242 | /* |
| 4243 | * glob |
| 4244 | */ |
| 4245 | |
| 4246 | #define scopy(x) strsave((x), areanum) |
| 4247 | #define BLKSIZ 512 |
| 4248 | #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent)) |
| 4249 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4250 | static struct wdblock *cl, *nl; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 4251 | static const char spcl[] ALIGN1= "[?*"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4252 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4253 | static struct wdblock *glob(char *cp, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4254 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4255 | int i; |
| 4256 | char *pp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4257 | |
| 4258 | if (cp == 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4259 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4260 | i = 0; |
| 4261 | for (pp = cp; *pp; pp++) |
| 4262 | if (any(*pp, spcl)) |
| 4263 | i++; |
| 4264 | else if (!any(*pp & ~QUOTE, spcl)) |
| 4265 | *pp &= ~QUOTE; |
| 4266 | if (i != 0) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4267 | for (cl = addword(scopy(cp), NULL); anyspcl(cl); cl = nl) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4268 | nl = newword(cl->w_nword * 2); |
| 4269 | for (i = 0; i < cl->w_nword; i++) { /* for each argument */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4270 | for (pp = cl->w_words[i]; *pp; pp++) |
| 4271 | if (any(*pp, spcl)) { |
| 4272 | globname(cl->w_words[i], pp); |
| 4273 | break; |
| 4274 | } |
| 4275 | if (*pp == '\0') |
| 4276 | nl = addword(scopy(cl->w_words[i]), nl); |
| 4277 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4278 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4279 | DELETE(cl->w_words[i]); |
| 4280 | DELETE(cl); |
| 4281 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4282 | if (cl->w_nword) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4283 | for (i = 0; i < cl->w_nword; i++) |
Denis Vlasenko | fb29038 | 2008-03-02 12:51:26 +0000 | [diff] [blame] | 4284 | unquote(cl->w_words[i]); |
| 4285 | qsort_string_vector(cl->w_words, cl->w_nword); |
| 4286 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4287 | wb = addword(cl->w_words[i], wb); |
| 4288 | DELETE(cl); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4289 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4290 | } |
| 4291 | } |
| 4292 | wb = addword(unquote(cp), wb); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4293 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4296 | static void globname(char *we, char *pp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4297 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4298 | char *np, *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4299 | char *name, *gp, *dp; |
| 4300 | int k; |
| 4301 | DIR *dirp; |
| 4302 | struct dirent *de; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4303 | char dname[NAME_MAX + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4304 | struct stat dbuf; |
| 4305 | |
| 4306 | for (np = we; np != pp; pp--) |
| 4307 | if (pp[-1] == '/') |
| 4308 | break; |
Denis Vlasenko | fb29038 | 2008-03-02 12:51:26 +0000 | [diff] [blame] | 4309 | dp = cp = get_space((int) (pp - np) + 3); |
| 4310 | while (np < pp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4311 | *cp++ = *np++; |
| 4312 | *cp++ = '.'; |
| 4313 | *cp = '\0'; |
Denis Vlasenko | fb29038 | 2008-03-02 12:51:26 +0000 | [diff] [blame] | 4314 | gp = cp = get_space(strlen(pp) + 1); |
| 4315 | while (*np && *np != '/') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4316 | *cp++ = *np++; |
| 4317 | *cp = '\0'; |
| 4318 | dirp = opendir(dp); |
| 4319 | if (dirp == 0) { |
| 4320 | DELETE(dp); |
| 4321 | DELETE(gp); |
| 4322 | return; |
| 4323 | } |
| 4324 | dname[NAME_MAX] = '\0'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4325 | while ((de = readdir(dirp)) != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4326 | /* XXX Hmmm... What this could be? (abial) */ |
Denis Vlasenko | ed9d621 | 2008-06-09 07:44:19 +0000 | [diff] [blame] | 4327 | /* if (ent[j].d_ino == 0) continue; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4328 | */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4329 | strncpy(dname, de->d_name, NAME_MAX); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4330 | if (dname[0] == '.') |
| 4331 | if (*gp != '.') |
| 4332 | continue; |
| 4333 | for (k = 0; k < NAME_MAX; k++) |
| 4334 | if (any(dname[k], spcl)) |
| 4335 | dname[k] |= QUOTE; |
| 4336 | if (gmatch(dname, gp)) { |
| 4337 | name = generate(we, pp, dname, np); |
| 4338 | if (*np && !anys(np, spcl)) { |
| 4339 | if (stat(name, &dbuf)) { |
| 4340 | DELETE(name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4341 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4342 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4343 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4344 | nl = addword(name, nl); |
| 4345 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4346 | } |
| 4347 | closedir(dirp); |
| 4348 | DELETE(dp); |
| 4349 | DELETE(gp); |
| 4350 | } |
| 4351 | |
| 4352 | /* |
| 4353 | * generate a pathname as below. |
| 4354 | * start..end1 / middle end |
| 4355 | * the slashes come for free |
| 4356 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4357 | static char *generate(char *start1, char *end1, char *middle, char *end) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4358 | { |
| 4359 | char *p; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4360 | char *op, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4361 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4362 | p = op = get_space((int)(end1 - start1) + strlen(middle) + strlen(end) + 2); |
| 4363 | xp = start1; |
| 4364 | while (xp != end1) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4365 | *op++ = *xp++; |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4366 | xp = middle; |
| 4367 | while (*xp != '\0') |
| 4368 | *op++ = *xp++; |
| 4369 | strcpy(op, end); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4370 | return p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4373 | static int anyspcl(struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4374 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4375 | int i; |
| 4376 | char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4377 | |
| 4378 | wd = wb->w_words; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4379 | for (i = 0; i < wb->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4380 | if (anys(spcl, *wd++)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4381 | return 1; |
| 4382 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4385 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4386 | /* -------- word.c -------- */ |
| 4387 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4388 | static struct wdblock *newword(int nw) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4389 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4390 | struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4391 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4392 | wb = get_space(sizeof(*wb) + nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4393 | wb->w_bsize = nw; |
| 4394 | wb->w_nword = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4395 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4398 | static struct wdblock *addword(char *wd, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4399 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4400 | struct wdblock *wb2; |
| 4401 | int nw; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4402 | |
| 4403 | if (wb == NULL) |
| 4404 | wb = newword(NSTART); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4405 | nw = wb->w_nword; |
| 4406 | if (nw >= wb->w_bsize) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4407 | wb2 = newword(nw * 2); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4408 | memcpy((char *) wb2->w_words, (char *) wb->w_words, |
| 4409 | nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4410 | wb2->w_nword = nw; |
| 4411 | DELETE(wb); |
| 4412 | wb = wb2; |
| 4413 | } |
| 4414 | wb->w_words[wb->w_nword++] = wd; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4415 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4416 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4417 | |
Denis Vlasenko | e471275 | 2007-04-14 15:08:41 +0000 | [diff] [blame] | 4418 | static char **getwords(struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4419 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4420 | char **wd; |
| 4421 | int nb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4422 | |
| 4423 | if (wb == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4424 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4425 | if (wb->w_nword == 0) { |
| 4426 | DELETE(wb); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4427 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4428 | } |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4429 | nb = sizeof(*wd) * wb->w_nword; |
| 4430 | wd = get_space(nb); |
Denis Vlasenko | fb29038 | 2008-03-02 12:51:26 +0000 | [diff] [blame] | 4431 | memcpy(wd, wb->w_words, nb); |
| 4432 | DELETE(wb); /* perhaps should done by caller */ |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4433 | return wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4436 | |
| 4437 | /* -------- io.c -------- */ |
| 4438 | |
| 4439 | /* |
| 4440 | * shell IO |
| 4441 | */ |
| 4442 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4443 | static int my_getc(int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4444 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4445 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4446 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4447 | if (global_env.linep > elinep) { |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4448 | while ((c = readc()) != '\n' && c) |
| 4449 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4450 | err("input line too long"); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 4451 | gflg = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4452 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4453 | } |
| 4454 | c = readc(); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4455 | if ((ec != '\'') && (ec != '`') && (global_env.iop->task != XGRAVE)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4456 | if (c == '\\') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4457 | c = readc(); |
| 4458 | if (c == '\n' && ec != '\"') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4459 | return my_getc(ec); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4460 | c |= QUOTE; |
| 4461 | } |
| 4462 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4463 | return 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 void unget(int c) |
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 | if (global_env.iop >= global_env.iobase) |
| 4469 | global_env.iop->peekc = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4472 | static int eofc(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4473 | { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4474 | 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] | 4475 | } |
| 4476 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4477 | static int readc(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4478 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4479 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4480 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4481 | 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] | 4482 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4483 | for (; global_env.iop >= global_env.iobase; global_env.iop--) { |
| 4484 | RCPRINTF(("READC: global_env.iop %p, peekc 0x%x\n", global_env.iop, global_env.iop->peekc)); |
| 4485 | c = global_env.iop->peekc; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4486 | if (c != '\0') { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4487 | global_env.iop->peekc = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4488 | return c; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4489 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4490 | if (global_env.iop->prev != 0) { |
| 4491 | c = (*global_env.iop->iofn)(global_env.iop->argp, global_env.iop); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4492 | if (c != '\0') { |
| 4493 | if (c == -1) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4494 | global_env.iop++; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4495 | continue; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4496 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4497 | if (global_env.iop == iostack) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4498 | ioecho(c); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4499 | global_env.iop->prev = c; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4500 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4501 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4502 | if (global_env.iop->task == XIO && global_env.iop->prev != '\n') { |
| 4503 | global_env.iop->prev = 0; |
| 4504 | if (global_env.iop == iostack) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4505 | ioecho('\n'); |
| 4506 | return '\n'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4507 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4508 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4509 | if (global_env.iop->task == XIO) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4510 | if (multiline) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4511 | global_env.iop->prev = 0; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4512 | return 0; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4513 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4514 | if (interactive && global_env.iop == iostack + 1) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4515 | #if ENABLE_FEATURE_EDITING |
| 4516 | current_prompt = prompt->value; |
| 4517 | #else |
| 4518 | prs(prompt->value); |
| 4519 | #endif |
| 4520 | } |
| 4521 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4522 | } /* FOR */ |
| 4523 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4524 | if (global_env.iop >= iostack) { |
| 4525 | RCPRINTF(("READC: return 0, global_env.iop %p\n", global_env.iop)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4526 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | DBGPRINTF(("READC: leave()...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4530 | leave(); |
| 4531 | /* NOTREACHED */ |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4532 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4535 | static void ioecho(char c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4536 | { |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 4537 | if (FLAG['v']) |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 4538 | write(STDERR_FILENO, &c, sizeof c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4541 | static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4542 | { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4543 | DBGPRINTF(("PUSHIO: argp %p, argp->afid 0x%x, global_env.iop %p\n", argp, |
| 4544 | argp->afid, global_env.iop)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4545 | |
| 4546 | /* 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] | 4547 | if (++global_env.iop >= &iostack[NPUSH]) { |
| 4548 | global_env.iop--; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4549 | err("Shell input nested too deeply"); |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 4550 | gflg = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4551 | return; |
| 4552 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4553 | |
| 4554 | /* We did not overflow the NPUSH array spots so setup data structs */ |
| 4555 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4556 | 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] | 4557 | |
| 4558 | if (argp->afid != AFID_NOBUF) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4559 | global_env.iop->argp = argp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4560 | else { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4561 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4562 | global_env.iop->argp = ioargstack + (global_env.iop - iostack); /* MAL - index into stack */ |
| 4563 | *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] | 4564 | |
| 4565 | /* MAL - mainbuf is for 1st data source (command line?) and all nested use a single shared buffer? */ |
| 4566 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4567 | if (global_env.iop == &iostack[0]) |
| 4568 | global_env.iop->argp->afbuf = &mainbuf; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4569 | else |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4570 | global_env.iop->argp->afbuf = &sharedbuf; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4571 | |
| 4572 | /* MAL - if not a termimal AND (commandline OR readable file) then give it a buffer id? */ |
| 4573 | /* This line appears to be active when running scripts from command line */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4574 | if ((isatty(global_env.iop->argp->afile) == 0) |
| 4575 | && (global_env.iop == &iostack[0] |
| 4576 | || lseek(global_env.iop->argp->afile, 0L, SEEK_CUR) != -1)) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4577 | if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */ |
| 4578 | bufid = AFID_ID; /* AFID_ID = 0 */ |
| 4579 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4580 | global_env.iop->argp->afid = bufid; /* assign buffer id */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4581 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4582 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4583 | DBGPRINTF(("PUSHIO: iostack %p, global_env.iop %p, afbuf %p\n", |
| 4584 | iostack, global_env.iop, global_env.iop->argp->afbuf)); |
| 4585 | DBGPRINTF(("PUSHIO: mbuf %p, sbuf %p, bid %d, global_env.iop %p\n", |
| 4586 | &mainbuf, &sharedbuf, bufid, global_env.iop)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4587 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4590 | global_env.iop->prev = ~'\n'; |
| 4591 | global_env.iop->peekc = 0; |
| 4592 | global_env.iop->xchar = 0; |
| 4593 | global_env.iop->nlcount = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4594 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4595 | if (fn == filechar || fn == linechar) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4596 | global_env.iop->task = XIO; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4597 | else if (fn == (int (*)(struct ioarg *)) gravechar |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4598 | || fn == (int (*)(struct ioarg *)) qgravechar) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4599 | global_env.iop->task = XGRAVE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4600 | else |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4601 | global_env.iop->task = XOTHER; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4604 | static struct io *setbase(struct io *ip) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4605 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4606 | struct io *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4607 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4608 | xp = global_env.iobase; |
| 4609 | global_env.iobase = ip; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4610 | return xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
| 4613 | /* |
| 4614 | * Input generating functions |
| 4615 | */ |
| 4616 | |
| 4617 | /* |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4618 | * Produce the characters of a string, then a newline, then NUL. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4619 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4620 | static int nlchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4621 | { |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4622 | char c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4623 | |
| 4624 | if (ap->aword == NULL) |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4625 | return '\0'; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4626 | c = *ap->aword++; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 4627 | if (c == '\0') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4628 | ap->aword = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4629 | return '\n'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4630 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4631 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | /* |
| 4635 | * Given a list of words, produce the characters |
| 4636 | * in them, with a space after each word. |
| 4637 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4638 | static int wdchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4639 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4640 | char c; |
| 4641 | char **wl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4642 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4643 | wl = ap->awordlist; |
| 4644 | if (wl == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4645 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4646 | if (*wl != NULL) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4647 | c = *(*wl)++; |
| 4648 | if (c != 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4649 | return c & 0177; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4650 | ap->awordlist++; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4651 | return ' '; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4652 | } |
| 4653 | ap->awordlist = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4654 | return '\n'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4655 | } |
| 4656 | |
| 4657 | /* |
| 4658 | * Return the characters of a list of words, |
| 4659 | * producing a space between them. |
| 4660 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4661 | static int dolchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4662 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4663 | char *wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4664 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4665 | wp = *ap->awordlist++; |
| 4666 | if (wp != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4667 | PUSHIO(aword, wp, *ap->awordlist == NULL ? strchar : xxchar); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4668 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4669 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4670 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4673 | static int xxchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4674 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4675 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4676 | |
| 4677 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4678 | return 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4679 | c = *ap->aword++; |
| 4680 | if (c == '\0') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4681 | ap->aword = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4682 | return ' '; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4683 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4684 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | /* |
| 4688 | * Produce the characters from a single word (string). |
| 4689 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4690 | static int strchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4691 | { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4692 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4693 | return 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4694 | return *ap->aword++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
| 4697 | /* |
| 4698 | * Produce quoted characters from a single word (string). |
| 4699 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4700 | static int qstrchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4701 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4702 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4703 | |
Denis Vlasenko | b2abef3 | 2007-01-01 18:18:04 +0000 | [diff] [blame] | 4704 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4705 | return 0; |
Denis Vlasenko | b2abef3 | 2007-01-01 18:18:04 +0000 | [diff] [blame] | 4706 | c = *ap->aword++; |
| 4707 | if (c) |
| 4708 | c |= QUOTE; |
| 4709 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | /* |
| 4713 | * Return the characters from a file. |
| 4714 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4715 | static int filechar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4716 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4717 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4718 | char c; |
| 4719 | struct iobuf *bp = ap->afbuf; |
| 4720 | |
| 4721 | if (ap->afid != AFID_NOBUF) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4722 | i = (ap->afid != bp->id); |
| 4723 | if (i || bp->bufp == bp->ebufp) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4724 | if (i) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 4725 | lseek(ap->afile, ap->afpos, SEEK_SET); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4726 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 4727 | i = nonblock_safe_read(ap->afile, bp->buf, sizeof(bp->buf)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4728 | if (i <= 0) { |
| 4729 | closef(ap->afile); |
| 4730 | return 0; |
| 4731 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4732 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4733 | bp->id = ap->afid; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4734 | bp->bufp = bp->buf; |
| 4735 | bp->ebufp = bp->bufp + i; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4736 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4737 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4738 | ap->afpos++; |
| 4739 | return *bp->bufp++ & 0177; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4740 | } |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4741 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4742 | if (interactive && isatty(ap->afile)) { |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 4743 | /* moved to G: static char filechar_cmdbuf[BUFSIZ]; */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4744 | static int position = 0, size = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4745 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4746 | while (size == 0 || position >= size) { |
Denis Vlasenko | 6e602c4 | 2008-02-02 18:50:50 +0000 | [diff] [blame] | 4747 | size = read_line_input(current_prompt, filechar_cmdbuf, BUFSIZ, line_input_state); |
| 4748 | if (size < 0) /* Error/EOF */ |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 4749 | exit(EXIT_SUCCESS); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4750 | position = 0; |
Denis Vlasenko | 6e602c4 | 2008-02-02 18:50:50 +0000 | [diff] [blame] | 4751 | /* if Ctrl-C, size == 0 and loop will repeat */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4752 | } |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 4753 | c = filechar_cmdbuf[position]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4754 | position++; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4755 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4756 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4757 | #endif |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 4758 | i = nonblock_safe_read(ap->afile, &c, sizeof(c)); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4759 | return i == sizeof(c) ? (c & 0x7f) : (closef(ap->afile), 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | /* |
| 4763 | * Return the characters from a here temp file. |
| 4764 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4765 | static int herechar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4766 | { |
| 4767 | char c; |
| 4768 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 4769 | if (nonblock_safe_read(ap->afile, &c, sizeof(c)) != sizeof(c)) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4770 | close(ap->afile); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4771 | c = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4772 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4773 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | /* |
| 4777 | * Return the characters produced by a process (`...`). |
| 4778 | * Quote them if required, and remove any trailing newline characters. |
| 4779 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4780 | static int gravechar(struct ioarg *ap, struct io *iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4781 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4782 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4783 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4784 | c = qgravechar(ap, iop) & ~QUOTE; |
| 4785 | if (c == '\n') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4786 | c = ' '; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4787 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4790 | static int qgravechar(struct ioarg *ap, struct io *iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4791 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4792 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4793 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4794 | DBGPRINTF3(("QGRAVECHAR: enter, ap=%p, iop=%p\n", ap, iop)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4795 | |
| 4796 | if (iop->xchar) { |
| 4797 | if (iop->nlcount) { |
| 4798 | iop->nlcount--; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4799 | return '\n' | QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4800 | } |
| 4801 | c = iop->xchar; |
| 4802 | iop->xchar = 0; |
| 4803 | } else if ((c = filechar(ap)) == '\n') { |
| 4804 | iop->nlcount = 1; |
| 4805 | while ((c = filechar(ap)) == '\n') |
| 4806 | iop->nlcount++; |
| 4807 | iop->xchar = c; |
| 4808 | if (c == 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4809 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4810 | iop->nlcount--; |
| 4811 | c = '\n'; |
| 4812 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4813 | return c != 0 ? c | QUOTE : 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | /* |
| 4817 | * Return a single command (usually the first line) from a file. |
| 4818 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4819 | static int linechar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4820 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4821 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4822 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4823 | c = filechar(ap); |
| 4824 | if (c == '\n') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4825 | if (!multiline) { |
| 4826 | closef(ap->afile); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4827 | ap->afile = -1; /* illegal value */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4828 | } |
| 4829 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4830 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4831 | } |
| 4832 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4833 | /* |
Denis Vlasenko | ed9d621 | 2008-06-09 07:44:19 +0000 | [diff] [blame] | 4834 | * Remap fd into shell's fd space |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4835 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4836 | static int remap(int fd) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4837 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4838 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4839 | int map[NOFILE]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4840 | int newfd; |
| 4841 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4842 | 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] | 4843 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4844 | if (fd < global_env.iofd) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4845 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4846 | map[i] = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4847 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4848 | do { |
| 4849 | map[fd] = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4850 | newfd = dup(fd); |
| 4851 | fd = newfd; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4852 | } while (fd >= 0 && fd < global_env.iofd); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4853 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4854 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4855 | if (map[i]) |
| 4856 | close(i); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4857 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4858 | if (fd < 0) |
| 4859 | err("too many files open in shell"); |
| 4860 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4861 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4862 | return fd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4865 | static int openpipe(int *pv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4866 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4867 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4868 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4869 | i = pipe(pv); |
| 4870 | if (i < 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4871 | err("can't create pipe - try again"); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4872 | return i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4873 | } |
| 4874 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4875 | static void closepipe(int *pv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4876 | { |
| 4877 | if (pv != NULL) { |
Denis Vlasenko | a5f2cd3 | 2008-02-11 18:10:06 +0000 | [diff] [blame] | 4878 | close(pv[0]); |
| 4879 | close(pv[1]); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4880 | } |
| 4881 | } |
| 4882 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4883 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4884 | /* -------- here.c -------- */ |
| 4885 | |
| 4886 | /* |
| 4887 | * here documents |
| 4888 | */ |
| 4889 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4890 | static void markhere(char *s, struct ioword *iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4891 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4892 | struct here *h, *lh; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4893 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4894 | DBGPRINTF7(("MARKHERE: enter, s=%p\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4895 | |
Denis Vlasenko | 509697f | 2008-03-02 12:49:39 +0000 | [diff] [blame] | 4896 | h = get_space(sizeof(struct here)); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4897 | if (h == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4898 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4899 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4900 | h->h_tag = evalstr(s, DOSUB); |
| 4901 | if (h->h_tag == 0) |
| 4902 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4903 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4904 | h->h_iop = iop; |
| 4905 | iop->io_name = 0; |
| 4906 | h->h_next = NULL; |
| 4907 | if (inhere == 0) |
| 4908 | inhere = h; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4909 | else { |
| 4910 | for (lh = inhere; lh != NULL; lh = lh->h_next) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4911 | if (lh->h_next == 0) { |
| 4912 | lh->h_next = h; |
| 4913 | break; |
| 4914 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4915 | } |
| 4916 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4917 | iop->io_flag |= IOHERE | IOXHERE; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4918 | for (s = h->h_tag; *s; s++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4919 | if (*s & QUOTE) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4920 | iop->io_flag &= ~IOXHERE; |
| 4921 | *s &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4922 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4923 | } |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 4924 | h->h_dosub = ((iop->io_flag & IOXHERE) ? '\0' : '\''); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4925 | } |
| 4926 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4927 | static void gethere(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4928 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4929 | struct here *h, *hp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4930 | |
| 4931 | DBGPRINTF7(("GETHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4932 | |
| 4933 | /* Scan here files first leaving inhere list in place */ |
| 4934 | for (hp = h = inhere; h != NULL; hp = h, h = h->h_next) |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 4935 | 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] | 4936 | |
| 4937 | /* Make inhere list active - keep list intact for scraphere */ |
| 4938 | if (hp != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4939 | hp->h_next = acthere; |
| 4940 | acthere = inhere; |
| 4941 | inhere = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4942 | } |
| 4943 | } |
| 4944 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4945 | static void readhere(char **name, char *s, int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4946 | { |
| 4947 | int tf; |
| 4948 | char tname[30] = ".msh_XXXXXX"; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4949 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4950 | jmp_buf ev; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4951 | char myline[LINELIM + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4952 | char *thenext; |
| 4953 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4954 | DBGPRINTF7(("READHERE: enter, name=%p, s=%p\n", name, s)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4955 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4956 | tf = mkstemp(tname); |
| 4957 | if (tf < 0) |
| 4958 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4959 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4960 | *name = strsave(tname, areanum); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4961 | errpt = ev; |
| 4962 | if (newenv(setjmp(errpt)) != 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4963 | unlink(tname); |
| 4964 | else { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4965 | pushio(global_env.iop->argp, (int (*)(struct ioarg *)) global_env.iop->iofn); |
| 4966 | global_env.iobase = global_env.iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4967 | for (;;) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4968 | if (interactive && global_env.iop <= iostack) { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4969 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4970 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4971 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4972 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4973 | #endif |
| 4974 | } |
| 4975 | thenext = myline; |
| 4976 | while ((c = my_getc(ec)) != '\n' && c) { |
| 4977 | if (ec == '\'') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4978 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4979 | if (thenext >= &myline[LINELIM]) { |
| 4980 | c = 0; |
| 4981 | break; |
| 4982 | } |
| 4983 | *thenext++ = c; |
| 4984 | } |
| 4985 | *thenext = 0; |
| 4986 | if (strcmp(s, myline) == 0 || c == 0) |
| 4987 | break; |
| 4988 | *thenext++ = '\n'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4989 | write(tf, myline, (int) (thenext - myline)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4990 | } |
| 4991 | if (c == 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4992 | prs("here document `"); |
| 4993 | prs(s); |
| 4994 | err("' unclosed"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4995 | } |
| 4996 | quitenv(); |
| 4997 | } |
| 4998 | close(tf); |
| 4999 | } |
| 5000 | |
| 5001 | /* |
| 5002 | * open here temp file. |
| 5003 | * if unquoted here, expand here temp file into second temp file. |
| 5004 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 5005 | static int herein(char *hname, int xdoll) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5006 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5007 | int hf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5008 | int tf; |
| 5009 | |
| 5010 | #if __GNUC__ |
| 5011 | /* Avoid longjmp clobbering */ |
| 5012 | (void) &tf; |
| 5013 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5014 | if (hname == NULL) |
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 | |
| 5017 | DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll)); |
| 5018 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5019 | hf = open(hname, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5020 | if (hf < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 5021 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5022 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5023 | if (xdoll) { |
| 5024 | char c; |
| 5025 | char tname[30] = ".msh_XXXXXX"; |
| 5026 | jmp_buf ev; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5027 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5028 | tf = mkstemp(tname); |
| 5029 | if (tf < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 5030 | return -1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 5031 | errpt = ev; |
| 5032 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5033 | PUSHIO(afile, hf, herechar); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 5034 | setbase(global_env.iop); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5035 | while ((c = subgetc(0, 0)) != 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5036 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5037 | write(tf, &c, sizeof c); |
| 5038 | } |
| 5039 | quitenv(); |
| 5040 | } else |
| 5041 | unlink(tname); |
| 5042 | close(tf); |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5043 | tf = open(tname, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5044 | unlink(tname); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 5045 | return tf; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 5046 | } |
| 5047 | return hf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 5050 | static void scraphere(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5051 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5052 | struct here *h; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5053 | |
| 5054 | DBGPRINTF7(("SCRAPHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5055 | |
| 5056 | for (h = inhere; h != NULL; h = h->h_next) { |
| 5057 | if (h->h_iop && h->h_iop->io_name) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5058 | unlink(h->h_iop->io_name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5059 | } |
| 5060 | inhere = NULL; |
| 5061 | } |
| 5062 | |
| 5063 | /* unlink here temp files before a freearea(area) */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 5064 | static void freehere(int area) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5065 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5066 | struct here *h, *hl; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5067 | |
| 5068 | DBGPRINTF6(("FREEHERE: enter, area=%d\n", area)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5069 | |
| 5070 | hl = NULL; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 5071 | for (h = acthere; h != NULL; h = h->h_next) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5072 | if (getarea((char *) h) >= area) { |
| 5073 | if (h->h_iop->io_name != NULL) |
| 5074 | unlink(h->h_iop->io_name); |
| 5075 | if (hl == NULL) |
| 5076 | acthere = h->h_next; |
| 5077 | else |
| 5078 | hl->h_next = h->h_next; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 5079 | } else { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5080 | hl = h; |
Denis Vlasenko | 4aafd5f | 2008-03-02 14:33:26 +0000 | [diff] [blame] | 5081 | } |
| 5082 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
| 5085 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5086 | /* -------- sh.c -------- */ |
| 5087 | /* |
| 5088 | * shell |
| 5089 | */ |
| 5090 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 5091 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5092 | int msh_main(int argc, char **argv) |
| 5093 | { |
| 5094 | int f; |
| 5095 | char *s; |
| 5096 | int cflag; |
| 5097 | char *name, **ap; |
| 5098 | int (*iof) (struct ioarg *); |
| 5099 | |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 5100 | INIT_G(); |
| 5101 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 5102 | sharedbuf.id = AFID_NOBUF; |
| 5103 | mainbuf.id = AFID_NOBUF; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 5104 | elinep = line + sizeof(line) - 5; |
| 5105 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5106 | #if ENABLE_FEATURE_EDITING |
| 5107 | line_input_state = new_line_input_t(FOR_SHELL); |
| 5108 | #endif |
| 5109 | |
| 5110 | DBGPRINTF(("MSH_MAIN: argc %d, environ %p\n", argc, environ)); |
| 5111 | |
| 5112 | initarea(); |
| 5113 | ap = environ; |
| 5114 | if (ap != NULL) { |
| 5115 | while (*ap) |
| 5116 | assign(*ap++, !COPYV); |
| 5117 | for (ap = environ; *ap;) |
| 5118 | export(lookup(*ap++)); |
| 5119 | } |
| 5120 | closeall(); |
| 5121 | areanum = 1; |
| 5122 | |
| 5123 | shell = lookup("SHELL"); |
| 5124 | if (shell->value == null) |
| 5125 | setval(shell, (char *)DEFAULT_SHELL); |
| 5126 | export(shell); |
| 5127 | |
| 5128 | homedir = lookup("HOME"); |
| 5129 | if (homedir->value == null) |
| 5130 | setval(homedir, "/"); |
| 5131 | export(homedir); |
| 5132 | |
| 5133 | setval(lookup("$"), putn(getpid())); |
| 5134 | |
| 5135 | path = lookup("PATH"); |
| 5136 | if (path->value == null) { |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5137 | /* Can be merged with same string elsewhere in bbox */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5138 | if (geteuid() == 0) |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 5139 | setval(path, bb_default_root_path); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5140 | else |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 5141 | setval(path, bb_default_path); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5142 | } |
| 5143 | export(path); |
| 5144 | |
| 5145 | ifs = lookup("IFS"); |
| 5146 | if (ifs->value == null) |
| 5147 | setval(ifs, " \t\n"); |
| 5148 | |
| 5149 | #ifdef MSHDEBUG |
| 5150 | mshdbg_var = lookup("MSHDEBUG"); |
| 5151 | if (mshdbg_var->value == null) |
| 5152 | setval(mshdbg_var, "0"); |
| 5153 | #endif |
| 5154 | |
| 5155 | prompt = lookup("PS1"); |
| 5156 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 5157 | if (prompt->value == null) |
| 5158 | #endif |
| 5159 | setval(prompt, DEFAULT_USER_PROMPT); |
| 5160 | if (geteuid() == 0) { |
| 5161 | setval(prompt, DEFAULT_ROOT_PROMPT); |
| 5162 | prompt->status &= ~EXPORT; |
| 5163 | } |
| 5164 | cprompt = lookup("PS2"); |
| 5165 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 5166 | if (cprompt->value == null) |
| 5167 | #endif |
| 5168 | setval(cprompt, "> "); |
| 5169 | |
| 5170 | iof = filechar; |
| 5171 | cflag = 0; |
| 5172 | name = *argv++; |
| 5173 | if (--argc >= 1) { |
| 5174 | if (argv[0][0] == '-' && argv[0][1] != '\0') { |
| 5175 | for (s = argv[0] + 1; *s; s++) |
| 5176 | switch (*s) { |
| 5177 | case 'c': |
| 5178 | prompt->status &= ~EXPORT; |
| 5179 | cprompt->status &= ~EXPORT; |
| 5180 | setval(prompt, ""); |
| 5181 | setval(cprompt, ""); |
| 5182 | cflag = 1; |
| 5183 | if (--argc > 0) |
| 5184 | PUSHIO(aword, *++argv, iof = nlchar); |
| 5185 | break; |
| 5186 | |
| 5187 | case 'q': |
| 5188 | qflag = SIG_DFL; |
| 5189 | break; |
| 5190 | |
| 5191 | case 's': |
| 5192 | /* standard input */ |
| 5193 | break; |
| 5194 | |
| 5195 | case 't': |
| 5196 | prompt->status &= ~EXPORT; |
| 5197 | setval(prompt, ""); |
| 5198 | iof = linechar; |
| 5199 | break; |
| 5200 | |
| 5201 | case 'i': |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 5202 | interactive = 1; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5203 | default: |
| 5204 | if (*s >= 'a' && *s <= 'z') |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 5205 | FLAG[(int) *s]++; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5206 | } |
| 5207 | } else { |
| 5208 | argv--; |
| 5209 | argc++; |
| 5210 | } |
| 5211 | |
| 5212 | if (iof == filechar && --argc > 0) { |
| 5213 | setval(prompt, ""); |
| 5214 | setval(cprompt, ""); |
| 5215 | prompt->status &= ~EXPORT; |
| 5216 | cprompt->status &= ~EXPORT; |
| 5217 | |
| 5218 | /* Shell is non-interactive, activate printf-based debug */ |
| 5219 | #ifdef MSHDEBUG |
Denis Vlasenko | ed9d621 | 2008-06-09 07:44:19 +0000 | [diff] [blame] | 5220 | mshdbg = mshdbg_var->value[0] - '0'; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5221 | if (mshdbg < 0) |
| 5222 | mshdbg = 0; |
| 5223 | #endif |
| 5224 | DBGPRINTF(("MSH_MAIN: calling newfile()\n")); |
| 5225 | |
| 5226 | name = *++argv; |
| 5227 | if (newfile(name)) |
Denis Vlasenko | ed9d621 | 2008-06-09 07:44:19 +0000 | [diff] [blame] | 5228 | exit(EXIT_FAILURE); /* Exit on error */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5229 | } |
| 5230 | } |
| 5231 | |
| 5232 | setdash(); |
| 5233 | |
| 5234 | /* 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] | 5235 | if (global_env.iop < iostack) { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5236 | PUSHIO(afile, 0, iof); |
| 5237 | if (isatty(0) && isatty(1) && !cflag) { |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 5238 | interactive = 1; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5239 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 5240 | #ifdef MSHDEBUG |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 5241 | 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] | 5242 | #else |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 5243 | printf("\n\n%s built-in shell (msh)\n", bb_banner); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5244 | #endif |
| 5245 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 5246 | #endif |
| 5247 | } |
| 5248 | } |
| 5249 | |
| 5250 | signal(SIGQUIT, qflag); |
| 5251 | if (name && name[0] == '-') { |
Denis Vlasenko | 648b44f | 2008-02-12 06:04:06 +0000 | [diff] [blame] | 5252 | interactive = 1; |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5253 | f = open(".profile", O_RDONLY); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5254 | if (f >= 0) |
| 5255 | next(remap(f)); |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5256 | f = open("/etc/profile", O_RDONLY); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5257 | if (f >= 0) |
| 5258 | next(remap(f)); |
| 5259 | } |
| 5260 | if (interactive) |
| 5261 | signal(SIGTERM, sig); |
| 5262 | |
| 5263 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 5264 | signal(SIGINT, onintr); |
Denis Vlasenko | fee2d0c | 2008-02-12 10:12:18 +0000 | [diff] [blame] | 5265 | |
| 5266 | /* Handle "msh SCRIPT VAR=val params..." */ |
| 5267 | /* Disabled: bash does not do it! */ |
| 5268 | #if 0 |
| 5269 | argv++; |
| 5270 | /* skip leading args of the form VAR=val */ |
| 5271 | while (*argv && assign(*argv, !COPYV)) { |
| 5272 | argc--; |
| 5273 | argv++; |
| 5274 | } |
| 5275 | argv--; |
| 5276 | #endif |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5277 | dolv = argv; |
| 5278 | dolc = argc; |
| 5279 | dolv[0] = name; |
Denis Vlasenko | fee2d0c | 2008-02-12 10:12:18 +0000 | [diff] [blame] | 5280 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5281 | setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc)); |
| 5282 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 5283 | 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] | 5284 | |
| 5285 | for (;;) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 5286 | if (interactive && global_env.iop <= iostack) { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5287 | #if ENABLE_FEATURE_EDITING |
| 5288 | current_prompt = prompt->value; |
| 5289 | #else |
| 5290 | prs(prompt->value); |
| 5291 | #endif |
| 5292 | } |
| 5293 | onecommand(); |
| 5294 | /* Ensure that getenv("PATH") stays current */ |
| 5295 | setenv("PATH", path->value, 1); |
| 5296 | } |
| 5297 | |
| 5298 | DBGPRINTF(("MSH_MAIN: returning.\n")); |
| 5299 | } |
| 5300 | |
| 5301 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5302 | /* |
| 5303 | * Copyright (c) 1987,1997, Prentice Hall |
| 5304 | * All rights reserved. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5305 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5306 | * Redistribution and use of the MINIX operating system in source and |
| 5307 | * binary forms, with or without modification, are permitted provided |
| 5308 | * that the following conditions are met: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5309 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5310 | * Redistributions of source code must retain the above copyright |
| 5311 | * notice, this list of conditions and the following disclaimer. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5312 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5313 | * Redistributions in binary form must reproduce the above |
| 5314 | * copyright notice, this list of conditions and the following |
| 5315 | * disclaimer in the documentation and/or other materials provided |
| 5316 | * with the distribution. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5317 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5318 | * Neither the name of Prentice Hall nor the names of the software |
| 5319 | * authors or contributors may be used to endorse or promote |
| 5320 | * products derived from this software without specific prior |
| 5321 | * written permission. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5322 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5323 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND |
| 5324 | * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 5325 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 5326 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 5327 | * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE |
| 5328 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 5329 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 5330 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 5331 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 5332 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 5333 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 5334 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 5335 | * |
| 5336 | */ |