Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Minix shell port for busybox |
| 4 | * |
| 5 | * This version of the Minix shell was adapted for use in busybox |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 6 | * by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 8 | * - backtick expansion did not work properly |
| 9 | * Jonas Holmberg <jonas.holmberg@axis.com> |
| 10 | * Robert Schwebel <r.schwebel@pengutronix.de> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 11 | * Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 12 | * |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 13 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 16 | #include <sys/times.h> |
| 17 | #include <setjmp.h> |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 18 | |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 19 | #ifdef STANDALONE |
| 20 | # ifndef _GNU_SOURCE |
| 21 | # define _GNU_SOURCE |
| 22 | # endif |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 23 | # include <sys/types.h> |
| 24 | # include <sys/stat.h> |
| 25 | # include <sys/wait.h> |
| 26 | # include <signal.h> |
| 27 | # include <stdio.h> |
| 28 | # include <stdlib.h> |
| 29 | # include <unistd.h> |
| 30 | # include <string.h> |
| 31 | # include <errno.h> |
| 32 | # include <dirent.h> |
| 33 | # include <fcntl.h> |
| 34 | # include <ctype.h> |
| 35 | # include <assert.h> |
| 36 | # define bb_dev_null "/dev/null" |
| 37 | # define DEFAULT_SHELL "/proc/self/exe" |
| 38 | # define CONFIG_BUSYBOX_EXEC_PATH "/proc/self/exe" |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 39 | # define bb_banner "busybox standalone" |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 40 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 41 | # define bb_msg_memory_exhausted "memory exhausted" |
| 42 | # define xmalloc(size) malloc(size) |
| 43 | # define msh_main(argc,argv) main(argc,argv) |
| 44 | # define safe_read(fd,buf,count) read(fd,buf,count) |
| 45 | # define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1]) |
| 46 | # define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1]) |
| 47 | # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 48 | static int find_applet_by_name(const char *applet) |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 49 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 50 | return -1; |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 51 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 52 | static char *utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 53 | { |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 54 | unsigned i, out, res; |
| 55 | assert(sizeof(unsigned) == 4); |
| 56 | if (buflen) { |
| 57 | out = 0; |
| 58 | for (i = 1000000000; i; i /= 10) { |
| 59 | res = n / i; |
| 60 | if (res || out || i == 1) { |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 61 | if (!--buflen) break; |
| 62 | out++; |
| 63 | n -= res*i; |
| 64 | *buf++ = '0' + res; |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 67 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 68 | return buf; |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 69 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 70 | static char *itoa_to_buf(int n, char *buf, unsigned buflen) |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 71 | { |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 72 | if (buflen && n < 0) { |
| 73 | n = -n; |
| 74 | *buf++ = '-'; |
| 75 | buflen--; |
| 76 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 77 | return utoa_to_buf((unsigned)n, buf, buflen); |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 78 | } |
| 79 | static char local_buf[12]; |
| 80 | static char *itoa(int n) |
| 81 | { |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 82 | *(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0'; |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 83 | return local_buf; |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 84 | } |
| 85 | #else |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 86 | # include "busybox.h" /* for applet_names */ |
Mike Frysinger | 67a32ad | 2007-03-09 08:25:24 +0000 | [diff] [blame] | 87 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 88 | |
Mike Frysinger | 1781188 | 2006-05-05 20:33:07 +0000 | [diff] [blame] | 89 | /*#define MSHDEBUG 1*/ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 90 | |
| 91 | #ifdef MSHDEBUG |
Mike Frysinger | 14ff19b | 2006-06-20 20:37:01 +0000 | [diff] [blame] | 92 | int mshdbg = MSHDEBUG; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 94 | #define DBGPRINTF(x) if (mshdbg>0) printf x |
| 95 | #define DBGPRINTF0(x) if (mshdbg>0) printf x |
| 96 | #define DBGPRINTF1(x) if (mshdbg>1) printf x |
| 97 | #define DBGPRINTF2(x) if (mshdbg>2) printf x |
| 98 | #define DBGPRINTF3(x) if (mshdbg>3) printf x |
| 99 | #define DBGPRINTF4(x) if (mshdbg>4) printf x |
| 100 | #define DBGPRINTF5(x) if (mshdbg>5) printf x |
| 101 | #define DBGPRINTF6(x) if (mshdbg>6) printf x |
| 102 | #define DBGPRINTF7(x) if (mshdbg>7) printf x |
| 103 | #define DBGPRINTF8(x) if (mshdbg>8) printf x |
| 104 | #define DBGPRINTF9(x) if (mshdbg>9) printf x |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 105 | |
| 106 | int mshdbg_rc = 0; |
| 107 | |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 108 | #define RCPRINTF(x) if (mshdbg_rc) printf x |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 109 | |
| 110 | #else |
| 111 | |
| 112 | #define DBGPRINTF(x) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 113 | #define DBGPRINTF0(x) ((void)0) |
| 114 | #define DBGPRINTF1(x) ((void)0) |
| 115 | #define DBGPRINTF2(x) ((void)0) |
| 116 | #define DBGPRINTF3(x) ((void)0) |
| 117 | #define DBGPRINTF4(x) ((void)0) |
| 118 | #define DBGPRINTF5(x) ((void)0) |
| 119 | #define DBGPRINTF6(x) ((void)0) |
| 120 | #define DBGPRINTF7(x) ((void)0) |
| 121 | #define DBGPRINTF8(x) ((void)0) |
| 122 | #define DBGPRINTF9(x) ((void)0) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 123 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 124 | #define RCPRINTF(x) ((void)0) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 125 | |
| 126 | #endif /* MSHDEBUG */ |
| 127 | |
| 128 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 129 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 2a13175 | 2006-06-06 06:26:12 +0000 | [diff] [blame] | 130 | # define DEFAULT_ROOT_PROMPT "\\u:\\w> " |
| 131 | # define DEFAULT_USER_PROMPT "\\u:\\w$ " |
| 132 | #else |
| 133 | # define DEFAULT_ROOT_PROMPT "# " |
| 134 | # define DEFAULT_USER_PROMPT "$ " |
| 135 | #endif |
| 136 | |
| 137 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 138 | /* -------- sh.h -------- */ |
| 139 | /* |
| 140 | * shell |
| 141 | */ |
| 142 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 143 | #define LINELIM 2100 |
| 144 | #define NPUSH 8 /* limit to input nesting */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 145 | |
Eric Andersen | 392947c | 2002-12-11 07:42:46 +0000 | [diff] [blame] | 146 | #undef NOFILE |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 147 | #define NOFILE 20 /* Number of open files */ |
| 148 | #define NUFILE 10 /* Number of user-accessible files */ |
| 149 | #define FDBASE 10 /* First file usable by Shell */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * values returned by wait |
| 153 | */ |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 154 | #define WAITSIG(s) ((s) & 0177) |
| 155 | #define WAITVAL(s) (((s) >> 8) & 0377) |
| 156 | #define WAITCORE(s) (((s) & 0200) != 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 157 | |
| 158 | /* |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 159 | * library and system definitions |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 160 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 161 | typedef void xint; /* base type of jmp_buf, for not broken compilers */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * shell components |
| 165 | */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 166 | #define NOBLOCK ((struct op *)NULL) |
| 167 | #define NOWORD ((char *)NULL) |
| 168 | #define NOWORDS ((char **)NULL) |
| 169 | #define NOPIPE ((int *)NULL) |
| 170 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 171 | /* |
| 172 | * redirection |
| 173 | */ |
| 174 | struct ioword { |
| 175 | short io_unit; /* unit affected */ |
| 176 | short io_flag; /* action (below) */ |
| 177 | char *io_name; /* file name */ |
| 178 | }; |
| 179 | |
| 180 | #define IOREAD 1 /* < */ |
| 181 | #define IOHERE 2 /* << (here file) */ |
| 182 | #define IOWRITE 4 /* > */ |
| 183 | #define IOCAT 8 /* >> */ |
| 184 | #define IOXHERE 16 /* ${}, ` in << */ |
| 185 | #define IODUP 32 /* >&digit */ |
| 186 | #define IOCLOSE 64 /* >&- */ |
| 187 | |
| 188 | #define IODEFAULT (-1) /* token for default IO unit */ |
| 189 | |
| 190 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 191 | /* |
| 192 | * Description of a command or an operation on commands. |
| 193 | * Might eventually use a union. |
| 194 | */ |
| 195 | struct op { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 196 | int type; /* operation type, see below */ |
| 197 | char **words; /* arguments to a command */ |
| 198 | struct ioword **ioact; /* IO actions (eg, < > >>) */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 199 | struct op *left; |
| 200 | struct op *right; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 201 | char *str; /* identifier for case and for */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 204 | #define TCOM 1 /* command */ |
| 205 | #define TPAREN 2 /* (c-list) */ |
| 206 | #define TPIPE 3 /* a | b */ |
| 207 | #define TLIST 4 /* a [&;] b */ |
| 208 | #define TOR 5 /* || */ |
| 209 | #define TAND 6 /* && */ |
| 210 | #define TFOR 7 |
| 211 | #define TDO 8 |
| 212 | #define TCASE 9 |
| 213 | #define TIF 10 |
| 214 | #define TWHILE 11 |
| 215 | #define TUNTIL 12 |
| 216 | #define TELIF 13 |
| 217 | #define TPAT 14 /* pattern in case */ |
| 218 | #define TBRACE 15 /* {c-list} */ |
| 219 | #define TASYNC 16 /* c & */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 220 | /* Added to support "." file expansion */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 221 | #define TDOT 17 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 222 | |
| 223 | /* Strings for names to make debug easier */ |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 224 | #ifdef MSHDEBUG |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 225 | static const char *const T_CMD_NAMES[] = { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 226 | "PLACEHOLDER", |
| 227 | "TCOM", |
| 228 | "TPAREN", |
| 229 | "TPIPE", |
| 230 | "TLIST", |
| 231 | "TOR", |
| 232 | "TAND", |
| 233 | "TFOR", |
| 234 | "TDO", |
| 235 | "TCASE", |
| 236 | "TIF", |
| 237 | "TWHILE", |
| 238 | "TUNTIL", |
| 239 | "TELIF", |
| 240 | "TPAT", |
| 241 | "TBRACE", |
| 242 | "TASYNC", |
| 243 | "TDOT", |
| 244 | }; |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 245 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 246 | |
| 247 | /* |
| 248 | * actions determining the environment of a process |
| 249 | */ |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 250 | #define FEXEC 1 /* execute without forking */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 251 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 252 | #define AREASIZE (90000) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 253 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 254 | /* |
| 255 | * flags to control evaluation of words |
| 256 | */ |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 257 | #define DOSUB 1 /* interpret $, `, and quotes */ |
| 258 | #define DOBLANK 2 /* perform blank interpretation */ |
| 259 | #define DOGLOB 4 /* interpret [?* */ |
| 260 | #define DOKEY 8 /* move words with `=' to 2nd arg. list */ |
| 261 | #define DOTRIM 16 /* trim resulting string */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 262 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 263 | #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 264 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 265 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 266 | struct brkcon { |
| 267 | jmp_buf brkpt; |
| 268 | struct brkcon *nextlev; |
| 269 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 270 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 271 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 272 | static int trapset; /* trap pending */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 273 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 274 | static int yynerrs; /* yacc */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 275 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 276 | /* moved to G: static char line[LINELIM]; */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 277 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 278 | #if ENABLE_FEATURE_EDITING |
| 279 | static char *current_prompt; |
| 280 | static line_input_t *line_input_state; |
| 281 | #endif |
| 282 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 283 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 284 | /* |
| 285 | * other functions |
| 286 | */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 287 | static const char *rexecve(char *c, char **v, char **envp); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 288 | static char *evalstr(char *cp, int f); |
| 289 | static char *putn(int n); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 290 | static char *unquote(char *as); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 291 | static int rlookup(char *n); |
| 292 | static struct wdblock *glob(char *cp, struct wdblock *wb); |
| 293 | static int my_getc(int ec); |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 294 | static int subgetc(char ec, int quoted); |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 295 | static char **makenv(int all, struct wdblock *wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 296 | static char **eval(char **ap, int f); |
| 297 | static int setstatus(int s); |
| 298 | static int waitfor(int lastpid, int canintr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 299 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 300 | static void onintr(int s); /* SIGINT handler */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 301 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 302 | static int newenv(int f); |
| 303 | static void quitenv(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 304 | static void next(int f); |
| 305 | static void setdash(void); |
| 306 | static void onecommand(void); |
| 307 | static void runtrap(int i); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 308 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 309 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 310 | /* -------- area stuff -------- */ |
| 311 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 312 | #define REGSIZE sizeof(struct region) |
| 313 | #define GROWBY (256) |
| 314 | /* #define SHRINKBY (64) */ |
| 315 | #undef SHRINKBY |
| 316 | #define FREE (32767) |
| 317 | #define BUSY (0) |
| 318 | #define ALIGN (sizeof(int)-1) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 319 | |
| 320 | |
| 321 | struct region { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 322 | struct region *next; |
| 323 | int area; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 327 | /* -------- grammar stuff -------- */ |
| 328 | typedef union { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 329 | char *cp; |
| 330 | char **wp; |
| 331 | int i; |
| 332 | struct op *o; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 333 | } YYSTYPE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 334 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 335 | #define WORD 256 |
| 336 | #define LOGAND 257 |
| 337 | #define LOGOR 258 |
| 338 | #define BREAK 259 |
| 339 | #define IF 260 |
| 340 | #define THEN 261 |
| 341 | #define ELSE 262 |
| 342 | #define ELIF 263 |
| 343 | #define FI 264 |
| 344 | #define CASE 265 |
| 345 | #define ESAC 266 |
| 346 | #define FOR 267 |
| 347 | #define WHILE 268 |
| 348 | #define UNTIL 269 |
| 349 | #define DO 270 |
| 350 | #define DONE 271 |
| 351 | #define IN 272 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 352 | /* Added for "." file expansion */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 353 | #define DOT 273 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 354 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 355 | #define YYERRCODE 300 |
| 356 | |
| 357 | /* flags to yylex */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 358 | #define CONTIN 01 /* skip new lines to complete command */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 359 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 360 | static struct op *pipeline(int cf); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 361 | static struct op *andor(void); |
| 362 | static struct op *c_list(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 363 | static int synio(int cf); |
| 364 | static void musthave(int c, int cf); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 365 | static struct op *simple(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 366 | static struct op *nested(int type, int mark); |
| 367 | static struct op *command(int cf); |
| 368 | static struct op *dogroup(int onlydone); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 369 | static struct op *thenpart(void); |
| 370 | static struct op *elsepart(void); |
| 371 | static struct op *caselist(void); |
| 372 | static struct op *casepart(void); |
| 373 | static char **pattern(void); |
| 374 | static char **wordlist(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 375 | static struct op *list(struct op *t1, struct op *t2); |
| 376 | 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] | 377 | static struct op *newtp(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 378 | static struct op *namelist(struct op *t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 379 | static char **copyw(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 380 | static void word(char *cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 381 | static struct ioword **copyio(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 382 | static struct ioword *io(int u, int f, char *cp); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 383 | static int yylex(int cf); |
| 384 | static int collect(int c, int c1); |
| 385 | static int dual(int c); |
| 386 | static void diag(int ec); |
| 387 | static char *tree(unsigned size); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 388 | |
| 389 | /* -------- var.h -------- */ |
| 390 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 391 | struct var { |
| 392 | char *value; |
| 393 | char *name; |
| 394 | struct var *next; |
| 395 | char status; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 396 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 397 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 398 | #define COPYV 1 /* flag to setval, suggesting copy */ |
| 399 | #define RONLY 01 /* variable is read-only */ |
| 400 | #define EXPORT 02 /* variable is to be exported */ |
| 401 | #define GETCELL 04 /* name & value space was got with getcell */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 402 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 403 | static int yyparse(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 404 | |
| 405 | static int execute(struct op *t, int *pin, int *pout, int act); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 406 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 407 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 408 | /* -------- io.h -------- */ |
| 409 | /* io buffer */ |
| 410 | struct iobuf { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 411 | unsigned id; /* buffer id */ |
| 412 | char buf[512]; /* buffer */ |
| 413 | char *bufp; /* pointer into buffer */ |
| 414 | char *ebufp; /* pointer to end of buffer */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | /* possible arguments to an IO function */ |
| 418 | struct ioarg { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 419 | const char *aword; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 420 | char **awordlist; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 421 | int afile; /* file descriptor */ |
| 422 | unsigned afid; /* buffer id */ |
| 423 | long afpos; /* file position */ |
| 424 | struct iobuf *afbuf; /* buffer for this file */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 425 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 426 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 427 | /* an input generator's state */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 428 | struct io { |
| 429 | int (*iofn) (struct ioarg *, struct io *); |
| 430 | struct ioarg *argp; |
| 431 | int peekc; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 432 | char prev; /* previous character read by readc() */ |
| 433 | char nlcount; /* for `'s */ |
| 434 | char xchar; /* for `'s */ |
| 435 | char task; /* reason for pushed IO */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 436 | }; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 437 | /* ->task: */ |
| 438 | #define XOTHER 0 /* none of the below */ |
| 439 | #define XDOLL 1 /* expanding ${} */ |
| 440 | #define XGRAVE 2 /* expanding `'s */ |
| 441 | #define XIO 3 /* file IO */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 442 | |
| 443 | |
| 444 | /* |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 445 | * input generators for IO structure |
| 446 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 447 | static int nlchar(struct ioarg *ap); |
| 448 | static int strchar(struct ioarg *ap); |
| 449 | static int qstrchar(struct ioarg *ap); |
| 450 | static int filechar(struct ioarg *ap); |
| 451 | static int herechar(struct ioarg *ap); |
| 452 | static int linechar(struct ioarg *ap); |
| 453 | static int gravechar(struct ioarg *ap, struct io *iop); |
| 454 | static int qgravechar(struct ioarg *ap, struct io *iop); |
| 455 | static int dolchar(struct ioarg *ap); |
| 456 | static int wdchar(struct ioarg *ap); |
| 457 | static void scraphere(void); |
| 458 | static void freehere(int area); |
| 459 | static void gethere(void); |
| 460 | static void markhere(char *s, struct ioword *iop); |
| 461 | static int herein(char *hname, int xdoll); |
| 462 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 463 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 464 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 465 | static int eofc(void); |
| 466 | static int readc(void); |
| 467 | static void unget(int c); |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 468 | static void ioecho(char c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 469 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 470 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 471 | /* |
| 472 | * IO control |
| 473 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 474 | static void pushio(struct ioarg *argp, int (*f) (struct ioarg *)); |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 475 | #define PUSHIO(what,arg,gen) ((temparg.what = (arg)), pushio(&temparg,(gen))) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 476 | static int remap(int fd); |
| 477 | static int openpipe(int *pv); |
| 478 | static void closepipe(int *pv); |
| 479 | static struct io *setbase(struct io *ip); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 480 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 481 | /* -------- word.h -------- */ |
| 482 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 483 | #define NSTART 16 /* default number of words to allow for initially */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 484 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 485 | struct wdblock { |
| 486 | short w_bsize; |
| 487 | short w_nword; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 488 | /* bounds are arbitrary */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 489 | char *w_words[1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 490 | }; |
| 491 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 492 | static struct wdblock *addword(char *wd, struct wdblock *wb); |
| 493 | static struct wdblock *newword(int nw); |
| 494 | static char **getwords(struct wdblock *wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 495 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 496 | /* -------- misc stuff -------- */ |
| 497 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 498 | static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 499 | static int iosetup(struct ioword *iop, int pipein, int pipeout); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 500 | static void brkset(struct brkcon *bc); |
| 501 | static int dolabel(struct op *t); |
| 502 | static int dohelp(struct op *t); |
| 503 | static int dochdir(struct op *t); |
| 504 | static int doshift(struct op *t); |
| 505 | static int dologin(struct op *t); |
| 506 | static int doumask(struct op *t); |
| 507 | static int doexec(struct op *t); |
| 508 | static int dodot(struct op *t); |
| 509 | static int dowait(struct op *t); |
| 510 | static int doread(struct op *t); |
| 511 | static int doeval(struct op *t); |
| 512 | static int dotrap(struct op *t); |
| 513 | static int getsig(char *s); |
| 514 | static void setsig(int n, sighandler_t f); |
| 515 | static int getn(char *as); |
| 516 | static int dobreak(struct op *t); |
| 517 | static int docontinue(struct op *t); |
| 518 | static int brkcontin(char *cp, int val); |
| 519 | static int doexit(struct op *t); |
| 520 | static int doexport(struct op *t); |
| 521 | static int doreadonly(struct op *t); |
| 522 | static void rdexp(char **wp, void (*f) (struct var *), int key); |
| 523 | static void badid(char *s); |
| 524 | static int doset(struct op *t); |
| 525 | static void varput(char *s, int out); |
| 526 | static int dotimes(struct op *t); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 527 | static int expand(const char *cp, struct wdblock **wbp, int f); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 528 | static char *blank(int f); |
| 529 | static int dollar(int quoted); |
| 530 | static int grave(int quoted); |
| 531 | static void globname(char *we, char *pp); |
| 532 | static char *generate(char *start1, char *end1, char *middle, char *end); |
| 533 | static int anyspcl(struct wdblock *wb); |
| 534 | static int xstrcmp(char *p1, char *p2); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 535 | static void glob0(char *a0, unsigned a1, int a2, |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 536 | int (*a3) (char *, char *)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 537 | static void readhere(char **name, char *s, int ec); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 538 | static int xxchar(struct ioarg *ap); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 539 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 540 | struct here { |
| 541 | char *h_tag; |
| 542 | int h_dosub; |
| 543 | struct ioword *h_iop; |
| 544 | struct here *h_next; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 545 | }; |
| 546 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 547 | static const char *const signame[] = { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 548 | "Signal 0", |
| 549 | "Hangup", |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 550 | NULL, /* interrupt */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 551 | "Quit", |
| 552 | "Illegal instruction", |
| 553 | "Trace/BPT trap", |
| 554 | "Abort", |
| 555 | "Bus error", |
| 556 | "Floating Point Exception", |
| 557 | "Killed", |
| 558 | "SIGUSR1", |
| 559 | "SIGSEGV", |
| 560 | "SIGUSR2", |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 561 | NULL, /* broken pipe */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 562 | "Alarm clock", |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 563 | "Terminated" |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 564 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 565 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 566 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 567 | struct builtincmd { |
| 568 | const char *name; |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 569 | int (*builtinfunc)(struct op *t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 570 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 571 | static const struct builtincmd builtincmds[] = { |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 572 | { "." , dodot }, |
| 573 | { ":" , dolabel }, |
| 574 | { "break" , dobreak }, |
| 575 | { "cd" , dochdir }, |
| 576 | { "continue", docontinue }, |
| 577 | { "eval" , doeval }, |
| 578 | { "exec" , doexec }, |
| 579 | { "exit" , doexit }, |
| 580 | { "export" , doexport }, |
| 581 | { "help" , dohelp }, |
| 582 | { "login" , dologin }, |
| 583 | { "newgrp" , dologin }, |
| 584 | { "read" , doread }, |
| 585 | { "readonly", doreadonly }, |
| 586 | { "set" , doset }, |
| 587 | { "shift" , doshift }, |
| 588 | { "times" , dotimes }, |
| 589 | { "trap" , dotrap }, |
| 590 | { "umask" , doumask }, |
| 591 | { "wait" , dowait }, |
| 592 | { NULL , NULL }, |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 593 | }; |
| 594 | |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 595 | static struct op *scantree(struct op *); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 596 | static struct op *dowholefile(int, int); |
| 597 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 598 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 599 | /* Globals */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 600 | static char **dolv; |
| 601 | static int dolc; |
| 602 | static int exstat; |
| 603 | static char gflg; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 604 | static int interactive; /* Is this an interactive shell */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 605 | static int execflg; |
| 606 | static int multiline; /* \n changed to ; */ |
| 607 | static struct op *outtree; /* result from parser */ |
| 608 | static xint *failpt; |
| 609 | static xint *errpt; |
| 610 | static struct brkcon *brklist; |
| 611 | static int isbreak; |
| 612 | static struct wdblock *wdlist; |
| 613 | static struct wdblock *iolist; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 614 | |
| 615 | #ifdef MSHDEBUG |
| 616 | static struct var *mshdbg_var; |
| 617 | #endif |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 618 | static struct var *vlist; /* dictionary */ |
| 619 | static struct var *homedir; /* home directory */ |
| 620 | static struct var *prompt; /* main prompt */ |
| 621 | static struct var *cprompt; /* continuation prompt */ |
| 622 | static struct var *path; /* search path for commands */ |
| 623 | static struct var *shell; /* shell to interpret command files */ |
| 624 | static struct var *ifs; /* field separators */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 625 | |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 626 | static int areanum; /* current allocation area */ |
| 627 | static int intr; /* interrupt pending */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 628 | static int inparse; |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 629 | static char *null = (char*)""; /* null value for variable */ |
| 630 | static int heedint = 1; /* heed interrupt signals */ |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 631 | static void (*qflag)(int) = SIG_IGN; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 632 | static int startl; |
| 633 | static int peeksym; |
| 634 | static int nlseen; |
| 635 | static int iounit = IODEFAULT; |
| 636 | static YYSTYPE yylval; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 637 | static char *elinep; /* done in main(): = line + sizeof(line) - 5 */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 638 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 639 | static struct here *inhere; /* list of hear docs while parsing */ |
| 640 | static struct here *acthere; /* list of active here documents */ |
| 641 | static struct region *areabot; /* bottom of area */ |
| 642 | static struct region *areatop; /* top of area */ |
| 643 | static struct region *areanxt; /* starting point of scan */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 644 | static void *brktop; |
| 645 | static void *brkaddr; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 646 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 647 | #define AFID_NOBUF (~0) |
| 648 | #define AFID_ID 0 |
| 649 | |
| 650 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 651 | /* |
| 652 | * parsing & execution environment |
| 653 | */ |
| 654 | struct env { |
| 655 | char *linep; |
| 656 | struct io *iobase; |
| 657 | struct io *iop; |
| 658 | xint *errpt; /* void * */ |
| 659 | int iofd; |
| 660 | struct env *oenv; |
| 661 | }; |
| 662 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 663 | |
| 664 | struct globals { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 665 | struct env global_env; |
| 666 | struct ioarg temparg; // = { .afid = AFID_NOBUF }; /* temporary for PUSHIO */ |
| 667 | unsigned bufid; // = AFID_ID; /* buffer id counter */ |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 668 | char ourtrap[_NSIG + 1]; |
| 669 | char *trap[_NSIG + 1]; |
| 670 | struct iobuf sharedbuf; /* in main(): set to { AFID_NOBUF } */ |
| 671 | struct iobuf mainbuf; /* in main(): set to { AFID_NOBUF } */ |
| 672 | struct ioarg ioargstack[NPUSH]; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 673 | /* |
| 674 | * flags: |
| 675 | * -e: quit on error |
| 676 | * -k: look for name=value everywhere on command line |
| 677 | * -n: no execution |
| 678 | * -t: exit after reading and executing one command |
| 679 | * -v: echo as read |
| 680 | * -x: trace |
| 681 | * -u: unset variables net diagnostic |
| 682 | */ |
| 683 | char flags['z' - 'a' + 1]; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 684 | char filechar_cmdbuf[BUFSIZ]; |
| 685 | char line[LINELIM]; |
| 686 | char child_cmd[LINELIM]; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 687 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 688 | struct io iostack[NPUSH]; |
| 689 | |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 690 | char grave__var_name[LINELIM]; |
| 691 | char grave__alt_value[LINELIM]; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 692 | }; |
| 693 | |
| 694 | #define G (*ptr_to_globals) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 695 | #define global_env (G.global_env ) |
| 696 | #define temparg (G.temparg ) |
| 697 | #define bufid (G.bufid ) |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 698 | #define ourtrap (G.ourtrap ) |
| 699 | #define trap (G.trap ) |
| 700 | #define sharedbuf (G.sharedbuf ) |
| 701 | #define mainbuf (G.mainbuf ) |
| 702 | #define ioargstack (G.ioargstack ) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 703 | /* this looks weird, but is OK ... we index FLAG with 'a'...'z' */ |
| 704 | #define FLAG (G.flags - 'a' ) |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 705 | #define filechar_cmdbuf (G.filechar_cmdbuf) |
| 706 | #define line (G.line ) |
| 707 | #define child_cmd (G.child_cmd ) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 708 | #define iostack (G.iostack ) |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 709 | #define INIT_G() do { \ |
| 710 | PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 711 | global_env.linep = line; \ |
| 712 | global_env.iobase = iostack; \ |
| 713 | global_env.iop = iostack - 1; \ |
| 714 | global_env.iofd = FDBASE; \ |
| 715 | temparg.afid = AFID_NOBUF; \ |
| 716 | bufid = AFID_ID; \ |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 717 | } while (0) |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 718 | |
| 719 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 720 | /* in substitution */ |
| 721 | #define INSUB() (global_env.iop->task == XGRAVE || global_env.iop->task == XDOLL) |
| 722 | |
| 723 | #define RUN(what, arg, gen) ((temparg.what = (arg)), run(&temparg, (gen))) |
| 724 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 725 | #ifdef MSHDEBUG |
| 726 | void print_t(struct op *t) |
| 727 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 728 | DBGPRINTF(("T: t=%p, type %s, words=%p, IOword=%p\n", t, |
| 729 | T_CMD_NAMES[t->type], t->words, t->ioact)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 730 | |
| 731 | if (t->words) { |
| 732 | DBGPRINTF(("T: W1: %s", t->words[0])); |
| 733 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | void print_tree(struct op *head) |
| 737 | { |
| 738 | if (head == NULL) { |
| 739 | DBGPRINTF(("PRINT_TREE: no tree\n")); |
| 740 | return; |
| 741 | } |
| 742 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 743 | DBGPRINTF(("NODE: %p, left %p, right %p\n", head, head->left, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 744 | head->right)); |
| 745 | |
| 746 | if (head->left) |
| 747 | print_tree(head->left); |
| 748 | |
| 749 | if (head->right) |
| 750 | print_tree(head->right); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 751 | } |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 752 | #endif /* MSHDEBUG */ |
| 753 | |
| 754 | |
| 755 | /* |
| 756 | * IO functions |
| 757 | */ |
| 758 | static void prs(const char *s) |
| 759 | { |
| 760 | if (*s) |
| 761 | write(2, s, strlen(s)); |
| 762 | } |
| 763 | |
| 764 | static void prn(unsigned u) |
| 765 | { |
| 766 | prs(itoa(u)); |
| 767 | } |
| 768 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 769 | static void echo(char **wp) |
| 770 | { |
| 771 | int i; |
| 772 | |
| 773 | prs("+"); |
| 774 | for (i = 0; wp[i]; i++) { |
| 775 | if (i) |
| 776 | prs(" "); |
| 777 | prs(wp[i]); |
| 778 | } |
| 779 | prs("\n"); |
| 780 | } |
| 781 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 782 | static void closef(int i) |
| 783 | { |
| 784 | if (i > 2) |
| 785 | close(i); |
| 786 | } |
| 787 | |
| 788 | static void closeall(void) |
| 789 | { |
| 790 | int u; |
| 791 | |
| 792 | for (u = NUFILE; u < NOFILE;) |
| 793 | close(u++); |
| 794 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 795 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 796 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 797 | /* fail but return to process next command */ |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 798 | static void fail(void) ATTRIBUTE_NORETURN; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 799 | static void fail(void) |
| 800 | { |
| 801 | longjmp(failpt, 1); |
| 802 | /* NOTREACHED */ |
| 803 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 804 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 805 | /* abort shell (or fail in subshell) */ |
| 806 | static void leave(void) ATTRIBUTE_NORETURN; |
| 807 | static void leave(void) |
| 808 | { |
| 809 | DBGPRINTF(("LEAVE: leave called!\n")); |
| 810 | |
| 811 | if (execflg) |
| 812 | fail(); |
| 813 | scraphere(); |
| 814 | freehere(1); |
| 815 | runtrap(0); |
| 816 | _exit(exstat); |
| 817 | /* NOTREACHED */ |
| 818 | } |
| 819 | |
| 820 | static void warn(const char *s) |
| 821 | { |
| 822 | if (*s) { |
| 823 | prs(s); |
| 824 | exstat = -1; |
| 825 | } |
| 826 | prs("\n"); |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 827 | if (FLAG['e']) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 828 | leave(); |
| 829 | } |
| 830 | |
| 831 | static void err(const char *s) |
| 832 | { |
| 833 | warn(s); |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 834 | if (FLAG['n']) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 835 | return; |
| 836 | if (!interactive) |
| 837 | leave(); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 838 | if (global_env.errpt) |
| 839 | longjmp(global_env.errpt, 1); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 840 | closeall(); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 841 | global_env.iop = global_env.iobase = iostack; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 844 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 845 | /* -------- area.c -------- */ |
| 846 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 847 | /* |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 848 | * All memory between (char *)areabot and (char *)(areatop+1) is |
| 849 | * exclusively administered by the area management routines. |
| 850 | * It is assumed that sbrk() and brk() manipulate the high end. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 851 | */ |
| 852 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 853 | #define sbrk(X) ({ \ |
| 854 | void * __q = (void *)-1; \ |
| 855 | if (brkaddr + (int)(X) < brktop) { \ |
| 856 | __q = brkaddr; \ |
| 857 | brkaddr += (int)(X); \ |
| 858 | } \ |
| 859 | __q; \ |
| 860 | }) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 861 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 862 | static void initarea(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 863 | { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 864 | brkaddr = xmalloc(AREASIZE); |
| 865 | brktop = brkaddr + AREASIZE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 866 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 867 | while ((long) sbrk(0) & ALIGN) |
| 868 | sbrk(1); |
| 869 | areabot = (struct region *) sbrk(REGSIZE); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 870 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 871 | areabot->next = areabot; |
| 872 | areabot->area = BUSY; |
| 873 | areatop = areabot; |
| 874 | areanxt = areabot; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 877 | static char *getcell(unsigned nbytes) |
| 878 | { |
| 879 | int nregio; |
| 880 | struct region *p, *q; |
| 881 | int i; |
| 882 | |
| 883 | if (nbytes == 0) { |
| 884 | puts("getcell(0)"); |
| 885 | abort(); |
| 886 | } |
| 887 | /* silly and defeats the algorithm */ |
| 888 | /* |
| 889 | * round upwards and add administration area |
| 890 | */ |
| 891 | nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1; |
| 892 | p = areanxt; |
| 893 | for (;;) { |
| 894 | if (p->area > areanum) { |
| 895 | /* |
| 896 | * merge free cells |
| 897 | */ |
| 898 | while ((q = p->next)->area > areanum && q != areanxt) |
| 899 | p->next = q->next; |
| 900 | /* |
| 901 | * exit loop if cell big enough |
| 902 | */ |
| 903 | if (q >= p + nregio) |
| 904 | goto found; |
| 905 | } |
| 906 | p = p->next; |
| 907 | if (p == areanxt) |
| 908 | break; |
| 909 | } |
| 910 | i = nregio >= GROWBY ? nregio : GROWBY; |
| 911 | p = (struct region *) sbrk(i * REGSIZE); |
| 912 | if (p == (struct region *) -1) |
| 913 | return NULL; |
| 914 | p--; |
| 915 | if (p != areatop) { |
| 916 | puts("not contig"); |
| 917 | abort(); /* allocated areas are contiguous */ |
| 918 | } |
| 919 | q = p + i; |
| 920 | p->next = q; |
| 921 | p->area = FREE; |
| 922 | q->next = areabot; |
| 923 | q->area = BUSY; |
| 924 | areatop = q; |
| 925 | found: |
| 926 | /* |
| 927 | * we found a FREE area big enough, pointed to by 'p', and up to 'q' |
| 928 | */ |
| 929 | areanxt = p + nregio; |
| 930 | if (areanxt < q) { |
| 931 | /* |
| 932 | * split into requested area and rest |
| 933 | */ |
| 934 | if (areanxt + 1 > q) { |
| 935 | puts("OOM"); |
| 936 | abort(); /* insufficient space left for admin */ |
| 937 | } |
| 938 | areanxt->next = q; |
| 939 | areanxt->area = FREE; |
| 940 | p->next = areanxt; |
| 941 | } |
| 942 | p->area = areanum; |
| 943 | return (char *) (p + 1); |
| 944 | } |
| 945 | |
| 946 | static void freecell(char *cp) |
| 947 | { |
| 948 | struct region *p; |
| 949 | |
| 950 | p = (struct region *) cp; |
| 951 | if (p != NULL) { |
| 952 | p--; |
| 953 | if (p < areanxt) |
| 954 | areanxt = p; |
| 955 | p->area = FREE; |
| 956 | } |
| 957 | } |
| 958 | #define DELETE(obj) freecell((char *)obj) |
| 959 | |
| 960 | static void freearea(int a) |
| 961 | { |
| 962 | struct region *p, *top; |
| 963 | |
| 964 | top = areatop; |
| 965 | for (p = areabot; p != top; p = p->next) |
| 966 | if (p->area >= a) |
| 967 | p->area = FREE; |
| 968 | } |
| 969 | |
| 970 | static void setarea(char *cp, int a) |
| 971 | { |
| 972 | struct region *p; |
| 973 | |
| 974 | p = (struct region *) cp; |
| 975 | if (p != NULL) |
| 976 | (p - 1)->area = a; |
| 977 | } |
| 978 | |
| 979 | static int getarea(char *cp) |
| 980 | { |
| 981 | return ((struct region *) cp - 1)->area; |
| 982 | } |
| 983 | |
| 984 | static void garbage(void) |
| 985 | { |
| 986 | struct region *p, *q, *top; |
| 987 | |
| 988 | top = areatop; |
| 989 | for (p = areabot; p != top; p = p->next) { |
| 990 | if (p->area > areanum) { |
| 991 | while ((q = p->next)->area > areanum) |
| 992 | p->next = q->next; |
| 993 | areanxt = p; |
| 994 | } |
| 995 | } |
| 996 | #ifdef SHRINKBY |
| 997 | if (areatop >= q + SHRINKBY && q->area > areanum) { |
| 998 | brk((char *) (q + 1)); |
| 999 | q->next = areabot; |
| 1000 | q->area = BUSY; |
| 1001 | areatop = q; |
| 1002 | } |
| 1003 | #endif |
| 1004 | } |
| 1005 | |
| 1006 | static char *space(int n) |
| 1007 | { |
| 1008 | char *cp; |
| 1009 | |
| 1010 | cp = getcell(n); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1011 | if (cp == NULL) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1012 | err("out of string space"); |
| 1013 | return cp; |
| 1014 | } |
| 1015 | |
| 1016 | static char *strsave(const char *s, int a) |
| 1017 | { |
| 1018 | char *cp; |
| 1019 | |
| 1020 | cp = space(strlen(s) + 1); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1021 | if (cp == NULL) { |
| 1022 | // FIXME: I highly doubt this is good. |
| 1023 | return (char*)""; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1024 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1025 | setarea(cp, a); |
| 1026 | strcpy(cp, s); |
| 1027 | return cp; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1030 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1031 | /* -------- var.c -------- */ |
| 1032 | |
| 1033 | static int eqname(const char *n1, const char *n2) |
| 1034 | { |
| 1035 | for (; *n1 != '=' && *n1 != '\0'; n1++) |
| 1036 | if (*n2++ != *n1) |
| 1037 | return 0; |
| 1038 | return *n2 == '\0' || *n2 == '='; |
| 1039 | } |
| 1040 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1041 | static const char *findeq(const char *cp) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1042 | { |
| 1043 | while (*cp != '\0' && *cp != '=') |
| 1044 | cp++; |
| 1045 | return cp; |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | * Find the given name in the dictionary |
| 1050 | * and return its value. If the name was |
| 1051 | * not previously there, enter it now and |
| 1052 | * return a null value. |
| 1053 | */ |
| 1054 | static struct var *lookup(const char *n) |
| 1055 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1056 | // FIXME: dirty hack |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1057 | static struct var dummy; |
| 1058 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1059 | struct var *vp; |
| 1060 | const char *cp; |
| 1061 | char *xp; |
| 1062 | int c; |
| 1063 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1064 | if (isdigit(*n)) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1065 | dummy.name = (char*)n; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1066 | for (c = 0; isdigit(*n) && c < 1000; n++) |
| 1067 | c = c * 10 + *n - '0'; |
| 1068 | dummy.status = RONLY; |
| 1069 | dummy.value = (c <= dolc ? dolv[c] : null); |
| 1070 | return &dummy; |
| 1071 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1072 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1073 | for (vp = vlist; vp; vp = vp->next) |
| 1074 | if (eqname(vp->name, n)) |
| 1075 | return vp; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1076 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1077 | cp = findeq(n); |
| 1078 | vp = (struct var *) space(sizeof(*vp)); |
| 1079 | if (vp == 0 || (vp->name = space((int) (cp - n) + 2)) == 0) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1080 | dummy.name = dummy.value = (char*)""; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1081 | return &dummy; |
| 1082 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1083 | |
| 1084 | xp = vp->name; |
| 1085 | while ((*xp = *n++) != '\0' && *xp != '=') |
| 1086 | xp++; |
| 1087 | *xp++ = '='; |
| 1088 | *xp = '\0'; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1089 | setarea((char *) vp, 0); |
| 1090 | setarea((char *) vp->name, 0); |
| 1091 | vp->value = null; |
| 1092 | vp->next = vlist; |
| 1093 | vp->status = GETCELL; |
| 1094 | vlist = vp; |
| 1095 | return vp; |
| 1096 | } |
| 1097 | |
| 1098 | /* |
| 1099 | * if name is not NULL, it must be |
| 1100 | * a prefix of the space `val', |
| 1101 | * and end with `='. |
| 1102 | * this is all so that exporting |
| 1103 | * values is reasonably painless. |
| 1104 | */ |
| 1105 | static void nameval(struct var *vp, const char *val, const char *name) |
| 1106 | { |
| 1107 | const char *cp; |
| 1108 | char *xp; |
| 1109 | int fl; |
| 1110 | |
| 1111 | if (vp->status & RONLY) { |
| 1112 | xp = vp->name; |
| 1113 | while (*xp && *xp != '=') |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1114 | fputc(*xp++, stderr); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1115 | err(" is read-only"); |
| 1116 | return; |
| 1117 | } |
| 1118 | fl = 0; |
| 1119 | if (name == NULL) { |
| 1120 | xp = space(strlen(vp->name) + strlen(val) + 2); |
| 1121 | if (xp == NULL) |
| 1122 | return; |
| 1123 | /* make string: name=value */ |
| 1124 | setarea(xp, 0); |
| 1125 | name = xp; |
| 1126 | cp = vp->name; |
| 1127 | while ((*xp = *cp++) != '\0' && *xp != '=') |
| 1128 | xp++; |
| 1129 | *xp++ = '='; |
| 1130 | strcpy(xp, val); |
| 1131 | val = xp; |
| 1132 | fl = GETCELL; |
| 1133 | } |
| 1134 | if (vp->status & GETCELL) |
| 1135 | freecell(vp->name); /* form new string `name=value' */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1136 | vp->name = (char*)name; |
| 1137 | vp->value = (char*)val; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1138 | vp->status |= fl; |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * give variable at `vp' the value `val'. |
| 1143 | */ |
| 1144 | static void setval(struct var *vp, const char *val) |
| 1145 | { |
| 1146 | nameval(vp, val, NULL); |
| 1147 | } |
| 1148 | |
| 1149 | static void export(struct var *vp) |
| 1150 | { |
| 1151 | vp->status |= EXPORT; |
| 1152 | } |
| 1153 | |
| 1154 | static void ronly(struct var *vp) |
| 1155 | { |
| 1156 | if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */ |
| 1157 | vp->status |= RONLY; |
| 1158 | } |
| 1159 | |
| 1160 | static int isassign(const char *s) |
| 1161 | { |
| 1162 | unsigned char c; |
| 1163 | DBGPRINTF7(("ISASSIGN: enter, s=%s\n", s)); |
| 1164 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1165 | c = *s; |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 1166 | /* no isalpha() - we shouldn't use locale */ |
| 1167 | /* c | 0x20 - lowercase (Latin) letters */ |
| 1168 | if (c != '_' && (unsigned)((c|0x20) - 'a') > 25) |
| 1169 | /* not letter */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1170 | return 0; |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 1171 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1172 | while (1) { |
| 1173 | c = *++s; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1174 | if (c == '=') |
| 1175 | return 1; |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 1176 | if (c == '\0') |
| 1177 | return 0; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1178 | if (c != '_' |
| 1179 | && (unsigned)(c - '0') > 9 /* not number */ |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 1180 | && (unsigned)((c|0x20) - 'a') > 25 /* not letter */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1181 | ) { |
| 1182 | return 0; |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | static int assign(const char *s, int cf) |
| 1188 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1189 | const char *cp; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1190 | struct var *vp; |
| 1191 | |
| 1192 | DBGPRINTF7(("ASSIGN: enter, s=%s, cf=%d\n", s, cf)); |
| 1193 | |
| 1194 | if (!isalpha(*s) && *s != '_') |
| 1195 | return 0; |
| 1196 | for (cp = s; *cp != '='; cp++) |
| 1197 | if (*cp == '\0' || (!isalnum(*cp) && *cp != '_')) |
| 1198 | return 0; |
| 1199 | vp = lookup(s); |
| 1200 | nameval(vp, ++cp, cf == COPYV ? NULL : s); |
| 1201 | if (cf != COPYV) |
| 1202 | vp->status &= ~GETCELL; |
| 1203 | return 1; |
| 1204 | } |
| 1205 | |
| 1206 | static int checkname(char *cp) |
| 1207 | { |
| 1208 | DBGPRINTF7(("CHECKNAME: enter, cp=%s\n", cp)); |
| 1209 | |
| 1210 | if (!isalpha(*cp++) && *(cp - 1) != '_') |
| 1211 | return 0; |
| 1212 | while (*cp) |
| 1213 | if (!isalnum(*cp++) && *(cp - 1) != '_') |
| 1214 | return 0; |
| 1215 | return 1; |
| 1216 | } |
| 1217 | |
| 1218 | static void putvlist(int f, int out) |
| 1219 | { |
| 1220 | struct var *vp; |
| 1221 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1222 | for (vp = vlist; vp; vp = vp->next) { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1223 | if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) { |
| 1224 | if (vp->status & EXPORT) |
| 1225 | write(out, "export ", 7); |
| 1226 | if (vp->status & RONLY) |
| 1227 | write(out, "readonly ", 9); |
| 1228 | write(out, vp->name, (int) (findeq(vp->name) - vp->name)); |
| 1229 | write(out, "\n", 1); |
| 1230 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1231 | } |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | |
| 1235 | /* |
| 1236 | * trap handling |
| 1237 | */ |
| 1238 | static void sig(int i) |
| 1239 | { |
| 1240 | trapset = i; |
| 1241 | signal(i, sig); |
| 1242 | } |
| 1243 | |
| 1244 | static void runtrap(int i) |
| 1245 | { |
| 1246 | char *trapstr; |
| 1247 | |
| 1248 | trapstr = trap[i]; |
| 1249 | if (trapstr == NULL) |
| 1250 | return; |
| 1251 | |
| 1252 | if (i == 0) |
| 1253 | trap[i] = NULL; |
| 1254 | |
| 1255 | RUN(aword, trapstr, nlchar); |
| 1256 | } |
| 1257 | |
| 1258 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1259 | static void setdash(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1260 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1261 | char *cp; |
| 1262 | int c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1263 | char m['z' - 'a' + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1264 | |
| 1265 | cp = m; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1266 | for (c = 'a'; c <= 'z'; c++) |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 1267 | if (FLAG[c]) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1268 | *cp++ = c; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1269 | *cp = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1270 | setval(lookup("-"), m); |
| 1271 | } |
| 1272 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1273 | static int newfile(char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1274 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1275 | int f; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1276 | |
| 1277 | DBGPRINTF7(("NEWFILE: opening %s\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1278 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1279 | f = 0; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 1280 | if (NOT_LONE_DASH(s)) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1281 | DBGPRINTF(("NEWFILE: s is %s\n", s)); |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 1282 | f = open(s, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1283 | if (f < 0) { |
| 1284 | prs(s); |
| 1285 | err(": cannot open"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1286 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1287 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1288 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1289 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1290 | next(remap(f)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1291 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1294 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1295 | struct op *scantree(struct op *head) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1296 | { |
| 1297 | struct op *dotnode; |
| 1298 | |
| 1299 | if (head == 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 | |
| 1302 | if (head->left != NULL) { |
| 1303 | dotnode = scantree(head->left); |
| 1304 | if (dotnode) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1305 | return dotnode; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | if (head->right != NULL) { |
| 1309 | dotnode = scantree(head->right); |
| 1310 | if (dotnode) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1311 | return dotnode; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | if (head->words == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1315 | return NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1316 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1317 | DBGPRINTF5(("SCANTREE: checking node %p\n", head)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1318 | |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1319 | if ((head->type != TDOT) && LONE_CHAR(head->words[0], '.')) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1320 | DBGPRINTF5(("SCANTREE: dot found in node %p\n", head)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1321 | return head; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1324 | return NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1328 | static void onecommand(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1329 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1330 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1331 | jmp_buf m1; |
| 1332 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1333 | DBGPRINTF(("ONECOMMAND: enter, outtree=%p\n", outtree)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1334 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1335 | while (global_env.oenv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1336 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1337 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1338 | areanum = 1; |
| 1339 | freehere(areanum); |
| 1340 | freearea(areanum); |
| 1341 | garbage(); |
| 1342 | wdlist = 0; |
| 1343 | iolist = 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1344 | global_env.errpt = 0; |
| 1345 | global_env.linep = line; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1346 | yynerrs = 0; |
| 1347 | multiline = 0; |
| 1348 | inparse = 1; |
| 1349 | intr = 0; |
| 1350 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1351 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1352 | failpt = m1; |
| 1353 | setjmp(failpt); /* Bruce Evans' fix */ |
| 1354 | failpt = m1; |
| 1355 | if (setjmp(failpt) || yyparse() || intr) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1356 | DBGPRINTF(("ONECOMMAND: this is not good.\n")); |
| 1357 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1358 | while (global_env.oenv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1359 | quitenv(); |
| 1360 | scraphere(); |
| 1361 | if (!interactive && intr) |
| 1362 | leave(); |
| 1363 | inparse = 0; |
| 1364 | intr = 0; |
| 1365 | return; |
| 1366 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1367 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1368 | inparse = 0; |
| 1369 | brklist = 0; |
| 1370 | intr = 0; |
| 1371 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1372 | |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 1373 | if (!FLAG['n']) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1374 | DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=%p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1375 | outtree)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1376 | execute(outtree, NOPIPE, NOPIPE, 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1379 | if (!interactive && intr) { |
| 1380 | execflg = 0; |
| 1381 | leave(); |
| 1382 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1383 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1384 | i = trapset; |
| 1385 | if (i != 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1386 | trapset = 0; |
| 1387 | runtrap(i); |
| 1388 | } |
| 1389 | } |
| 1390 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1391 | static int newenv(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1392 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1393 | struct env *ep; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1394 | |
| 1395 | DBGPRINTF(("NEWENV: f=%d (indicates quitenv and return)\n", f)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1396 | |
| 1397 | if (f) { |
| 1398 | quitenv(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1399 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1400 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1401 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1402 | ep = (struct env *) space(sizeof(*ep)); |
| 1403 | if (ep == NULL) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1404 | while (global_env.oenv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1405 | quitenv(); |
| 1406 | fail(); |
| 1407 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1408 | *ep = global_env; |
| 1409 | global_env.oenv = ep; |
| 1410 | global_env.errpt = errpt; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1411 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1412 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1415 | static void quitenv(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1416 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1417 | struct env *ep; |
| 1418 | int fd; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1419 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1420 | DBGPRINTF(("QUITENV: global_env.oenv=%p\n", global_env.oenv)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1421 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1422 | ep = global_env.oenv; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1423 | if (ep != NULL) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1424 | fd = global_env.iofd; |
| 1425 | global_env = *ep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1426 | /* should close `'d files */ |
| 1427 | DELETE(ep); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1428 | while (--fd >= global_env.iofd) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1429 | close(fd); |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | /* |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1434 | * Is character c in s? |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1435 | */ |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1436 | static int any(int c, const char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1437 | { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1438 | while (*s) |
| 1439 | if (*s++ == c) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1440 | return 1; |
| 1441 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | /* |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1445 | * Is any character from s1 in s2? |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1446 | */ |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1447 | static int anys(const char *s1, const char *s2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1448 | { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1449 | while (*s1) |
| 1450 | if (any(*s1++, s2)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1451 | return 1; |
| 1452 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1455 | static char *putn(int n) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1456 | { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1457 | return itoa(n); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1460 | static void next(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1461 | { |
| 1462 | PUSHIO(afile, f, filechar); |
| 1463 | } |
| 1464 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1465 | static void onintr(int s) /* ANSI C requires a parameter */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1466 | { |
| 1467 | signal(SIGINT, onintr); |
| 1468 | intr = 1; |
| 1469 | if (interactive) { |
| 1470 | if (inparse) { |
| 1471 | prs("\n"); |
| 1472 | fail(); |
| 1473 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1474 | } else if (heedint) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1475 | execflg = 0; |
| 1476 | leave(); |
| 1477 | } |
| 1478 | } |
| 1479 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1480 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1481 | /* -------- gmatch.c -------- */ |
| 1482 | /* |
| 1483 | * int gmatch(string, pattern) |
| 1484 | * char *string, *pattern; |
| 1485 | * |
| 1486 | * Match a pattern as in sh(1). |
| 1487 | */ |
| 1488 | |
| 1489 | #define CMASK 0377 |
| 1490 | #define QUOTE 0200 |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 1491 | #define QMASK (CMASK & ~QUOTE) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1492 | #define NOT '!' /* might use ^ */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1493 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1494 | static const char *cclass(const char *p, int sub) |
| 1495 | { |
| 1496 | int c, d, not, found; |
| 1497 | |
| 1498 | not = (*p == NOT); |
| 1499 | if (not != 0) |
| 1500 | p++; |
| 1501 | found = not; |
| 1502 | do { |
| 1503 | if (*p == '\0') |
| 1504 | return NULL; |
| 1505 | c = *p & CMASK; |
| 1506 | if (p[1] == '-' && p[2] != ']') { |
| 1507 | d = p[2] & CMASK; |
| 1508 | p++; |
| 1509 | } else |
| 1510 | d = c; |
| 1511 | if (c == sub || (c <= sub && sub <= d)) |
| 1512 | found = !not; |
| 1513 | } while (*++p != ']'); |
| 1514 | return found ? p + 1 : NULL; |
| 1515 | } |
| 1516 | |
| 1517 | static int gmatch(const char *s, const char *p) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1518 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1519 | int sc, pc; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1520 | |
| 1521 | if (s == NULL || p == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1522 | return 0; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1523 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1524 | while ((pc = *p++ & CMASK) != '\0') { |
| 1525 | sc = *s++ & QMASK; |
| 1526 | switch (pc) { |
| 1527 | case '[': |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1528 | p = cclass(p, sc); |
| 1529 | if (p == NULL) |
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 | break; |
| 1532 | |
| 1533 | case '?': |
| 1534 | if (sc == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1535 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1536 | break; |
| 1537 | |
| 1538 | case '*': |
| 1539 | s--; |
| 1540 | do { |
| 1541 | if (*p == '\0' || gmatch(s, p)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1542 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1543 | } while (*s++ != '\0'); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1544 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1545 | |
| 1546 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1547 | if (sc != (pc & ~QUOTE)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1548 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1549 | } |
| 1550 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1551 | return *s == '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1554 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1555 | /* -------- csyn.c -------- */ |
| 1556 | /* |
| 1557 | * shell: syntax (C version) |
| 1558 | */ |
| 1559 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1560 | static void yyerror(const char *s) ATTRIBUTE_NORETURN; |
| 1561 | static void yyerror(const char *s) |
| 1562 | { |
| 1563 | yynerrs++; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 1564 | if (interactive && global_env.iop <= iostack) { |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1565 | multiline = 0; |
| 1566 | while (eofc() == 0 && yylex(0) != '\n'); |
| 1567 | } |
| 1568 | err(s); |
| 1569 | fail(); |
| 1570 | } |
| 1571 | |
| 1572 | static void zzerr(void) ATTRIBUTE_NORETURN; |
| 1573 | static void zzerr(void) |
| 1574 | { |
| 1575 | yyerror("syntax error"); |
| 1576 | } |
| 1577 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1578 | int yyparse(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1579 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1580 | DBGPRINTF7(("YYPARSE: enter...\n")); |
| 1581 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1582 | startl = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1583 | peeksym = 0; |
| 1584 | yynerrs = 0; |
| 1585 | outtree = c_list(); |
| 1586 | musthave('\n', 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1587 | return (yynerrs != 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1590 | static struct op *pipeline(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1591 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1592 | struct op *t, *p; |
| 1593 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1594 | |
| 1595 | DBGPRINTF7(("PIPELINE: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1596 | |
| 1597 | t = command(cf); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1598 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1599 | DBGPRINTF9(("PIPELINE: t=%p\n", t)); |
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 | if (t != NULL) { |
| 1602 | while ((c = yylex(0)) == '|') { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1603 | p = command(CONTIN); |
| 1604 | if (p == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1605 | DBGPRINTF8(("PIPELINE: error!\n")); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1606 | zzerr(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1609 | if (t->type != TPAREN && t->type != TCOM) { |
| 1610 | /* shell statement */ |
| 1611 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 1612 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1613 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1614 | t = block(TPIPE, t, p, NOWORDS); |
| 1615 | } |
| 1616 | peeksym = c; |
| 1617 | } |
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 | DBGPRINTF7(("PIPELINE: returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1620 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1623 | static struct op *andor(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1624 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1625 | struct op *t, *p; |
| 1626 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1627 | |
| 1628 | DBGPRINTF7(("ANDOR: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1629 | |
| 1630 | t = pipeline(0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1631 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1632 | DBGPRINTF9(("ANDOR: t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1633 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1634 | if (t != NULL) { |
| 1635 | while ((c = yylex(0)) == LOGAND || c == LOGOR) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1636 | p = pipeline(CONTIN); |
| 1637 | if (p == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1638 | DBGPRINTF8(("ANDOR: error!\n")); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1639 | zzerr(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1642 | t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1643 | } /* WHILE */ |
| 1644 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1645 | peeksym = c; |
| 1646 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1647 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1648 | DBGPRINTF7(("ANDOR: returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1649 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1652 | static struct op *c_list(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1653 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1654 | struct op *t, *p; |
| 1655 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1656 | |
| 1657 | DBGPRINTF7(("C_LIST: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1658 | |
| 1659 | t = andor(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1660 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1661 | if (t != NULL) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1662 | peeksym = yylex(0); |
| 1663 | if (peeksym == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1664 | t = block(TASYNC, t, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1665 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1666 | while ((c = yylex(0)) == ';' || c == '&' |
| 1667 | || (multiline && c == '\n')) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1668 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1669 | p = andor(); |
| 1670 | if (p== NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1671 | return t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1672 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1673 | peeksym = yylex(0); |
| 1674 | if (peeksym == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1675 | p = block(TASYNC, p, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1676 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1677 | t = list(t, p); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1678 | } /* WHILE */ |
| 1679 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1680 | peeksym = c; |
| 1681 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1682 | /* IF */ |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1683 | DBGPRINTF7(("C_LIST: returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1684 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1687 | static int synio(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1688 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1689 | struct ioword *iop; |
| 1690 | int i; |
| 1691 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1692 | |
| 1693 | DBGPRINTF7(("SYNIO: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1694 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1695 | c = yylex(cf); |
| 1696 | if (c != '<' && c != '>') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1697 | peeksym = c; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1698 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1699 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1700 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1701 | i = yylval.i; |
| 1702 | musthave(WORD, 0); |
| 1703 | iop = io(iounit, i, yylval.cp); |
| 1704 | iounit = IODEFAULT; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1705 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1706 | if (i & IOHERE) |
| 1707 | markhere(yylval.cp, iop); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1708 | |
| 1709 | DBGPRINTF7(("SYNIO: returning 1\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1710 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1713 | static void musthave(int c, int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1714 | { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1715 | peeksym = yylex(cf); |
| 1716 | if (peeksym != c) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1717 | DBGPRINTF7(("MUSTHAVE: error!\n")); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1718 | zzerr(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1721 | peeksym = 0; |
| 1722 | } |
| 1723 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1724 | static struct op *simple(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1725 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1726 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1727 | |
| 1728 | t = NULL; |
| 1729 | for (;;) { |
| 1730 | switch (peeksym = yylex(0)) { |
| 1731 | case '<': |
| 1732 | case '>': |
| 1733 | (void) synio(0); |
| 1734 | break; |
| 1735 | |
| 1736 | case WORD: |
| 1737 | if (t == NULL) { |
| 1738 | t = newtp(); |
| 1739 | t->type = TCOM; |
| 1740 | } |
| 1741 | peeksym = 0; |
| 1742 | word(yylval.cp); |
| 1743 | break; |
| 1744 | |
| 1745 | default: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1746 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1747 | } |
| 1748 | } |
| 1749 | } |
| 1750 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1751 | static struct op *nested(int type, int mark) |
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 | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1754 | |
| 1755 | DBGPRINTF3(("NESTED: enter, type=%d, mark=%d\n", type, mark)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1756 | |
| 1757 | multiline++; |
| 1758 | t = c_list(); |
| 1759 | musthave(mark, 0); |
| 1760 | multiline--; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1761 | return block(type, t, NOBLOCK, NOWORDS); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1764 | static struct op *command(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1765 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1766 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1767 | struct wdblock *iosave; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1768 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1769 | |
| 1770 | DBGPRINTF(("COMMAND: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1771 | |
| 1772 | iosave = iolist; |
| 1773 | iolist = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1774 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1775 | if (multiline) |
| 1776 | cf |= CONTIN; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1777 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1778 | while (synio(cf)) |
| 1779 | cf = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1780 | |
| 1781 | c = yylex(cf); |
| 1782 | |
| 1783 | switch (c) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1784 | default: |
| 1785 | peeksym = c; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1786 | t = simple(); |
| 1787 | if (t == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1788 | if (iolist == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1789 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1790 | t = newtp(); |
| 1791 | t->type = TCOM; |
| 1792 | } |
| 1793 | break; |
| 1794 | |
| 1795 | case '(': |
| 1796 | t = nested(TPAREN, ')'); |
| 1797 | break; |
| 1798 | |
| 1799 | case '{': |
| 1800 | t = nested(TBRACE, '}'); |
| 1801 | break; |
| 1802 | |
| 1803 | case FOR: |
| 1804 | t = newtp(); |
| 1805 | t->type = TFOR; |
| 1806 | musthave(WORD, 0); |
| 1807 | startl = 1; |
| 1808 | t->str = yylval.cp; |
| 1809 | multiline++; |
| 1810 | t->words = wordlist(); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1811 | c = yylex(0); |
| 1812 | if (c != '\n' && c != ';') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1813 | peeksym = c; |
| 1814 | t->left = dogroup(0); |
| 1815 | multiline--; |
| 1816 | break; |
| 1817 | |
| 1818 | case WHILE: |
| 1819 | case UNTIL: |
| 1820 | multiline++; |
| 1821 | t = newtp(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1822 | t->type = c == WHILE ? TWHILE : TUNTIL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1823 | t->left = c_list(); |
| 1824 | t->right = dogroup(1); |
| 1825 | t->words = NULL; |
| 1826 | multiline--; |
| 1827 | break; |
| 1828 | |
| 1829 | case CASE: |
| 1830 | t = newtp(); |
| 1831 | t->type = TCASE; |
| 1832 | musthave(WORD, 0); |
| 1833 | t->str = yylval.cp; |
| 1834 | startl++; |
| 1835 | multiline++; |
| 1836 | musthave(IN, CONTIN); |
| 1837 | startl++; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1838 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1839 | t->left = caselist(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1840 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1841 | musthave(ESAC, 0); |
| 1842 | multiline--; |
| 1843 | break; |
| 1844 | |
| 1845 | case IF: |
| 1846 | multiline++; |
| 1847 | t = newtp(); |
| 1848 | t->type = TIF; |
| 1849 | t->left = c_list(); |
| 1850 | t->right = thenpart(); |
| 1851 | musthave(FI, 0); |
| 1852 | multiline--; |
| 1853 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1854 | |
| 1855 | case DOT: |
| 1856 | t = newtp(); |
| 1857 | t->type = TDOT; |
| 1858 | |
| 1859 | musthave(WORD, 0); /* gets name of file */ |
| 1860 | DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp)); |
| 1861 | |
| 1862 | word(yylval.cp); /* add word to wdlist */ |
| 1863 | word(NOWORD); /* terminate wdlist */ |
| 1864 | t->words = copyw(); /* dup wdlist */ |
| 1865 | break; |
| 1866 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1867 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1868 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1869 | while (synio(0)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1870 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1871 | t = namelist(t); |
| 1872 | iolist = iosave; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1873 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1874 | DBGPRINTF(("COMMAND: returning %p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1875 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1876 | return t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1879 | static struct op *dowholefile(int type, int mark) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1880 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1881 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1882 | |
| 1883 | DBGPRINTF(("DOWHOLEFILE: enter, type=%d, mark=%d\n", type, mark)); |
| 1884 | |
| 1885 | multiline++; |
| 1886 | t = c_list(); |
| 1887 | multiline--; |
| 1888 | t = block(type, t, NOBLOCK, NOWORDS); |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1889 | DBGPRINTF(("DOWHOLEFILE: return t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1890 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1893 | static struct op *dogroup(int onlydone) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1894 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1895 | int c; |
| 1896 | struct op *mylist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1897 | |
| 1898 | c = yylex(CONTIN); |
| 1899 | if (c == DONE && onlydone) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1900 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1901 | if (c != DO) |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1902 | zzerr(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1903 | mylist = c_list(); |
| 1904 | musthave(DONE, 0); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1905 | return mylist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1908 | static struct op *thenpart(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1909 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1910 | int c; |
| 1911 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1912 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1913 | c = yylex(0); |
| 1914 | if (c != THEN) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1915 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1916 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1917 | } |
| 1918 | t = newtp(); |
| 1919 | t->type = 0; |
| 1920 | t->left = c_list(); |
| 1921 | if (t->left == NULL) |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1922 | zzerr(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1923 | t->right = elsepart(); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1924 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1927 | static struct op *elsepart(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1928 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1929 | int c; |
| 1930 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1931 | |
| 1932 | switch (c = yylex(0)) { |
| 1933 | case ELSE: |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1934 | t = c_list(); |
| 1935 | if (t == NULL) |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1936 | zzerr(); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1937 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1938 | |
| 1939 | case ELIF: |
| 1940 | t = newtp(); |
| 1941 | t->type = TELIF; |
| 1942 | t->left = c_list(); |
| 1943 | t->right = thenpart(); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1944 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1945 | |
| 1946 | default: |
| 1947 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1948 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1949 | } |
| 1950 | } |
| 1951 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1952 | static struct op *caselist(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1953 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1954 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1955 | |
| 1956 | t = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1957 | while ((peeksym = yylex(CONTIN)) != ESAC) { |
| 1958 | DBGPRINTF(("CASELIST, doing yylex, peeksym=%d\n", peeksym)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1959 | t = list(t, casepart()); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1962 | DBGPRINTF(("CASELIST, returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1963 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1966 | static struct op *casepart(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1967 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1968 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1969 | |
| 1970 | DBGPRINTF7(("CASEPART: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1971 | |
| 1972 | t = newtp(); |
| 1973 | t->type = TPAT; |
| 1974 | t->words = pattern(); |
| 1975 | musthave(')', 0); |
| 1976 | t->left = c_list(); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1977 | peeksym = yylex(CONTIN); |
| 1978 | if (peeksym != ESAC) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1979 | musthave(BREAK, CONTIN); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1980 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1981 | DBGPRINTF7(("CASEPART: made newtp(TPAT, t=%p)\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1982 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1983 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1986 | static char **pattern(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1987 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1988 | int c, cf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1989 | |
| 1990 | cf = CONTIN; |
| 1991 | do { |
| 1992 | musthave(WORD, cf); |
| 1993 | word(yylval.cp); |
| 1994 | cf = 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1995 | c = yylex(0); |
| 1996 | } while (c == '|'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1997 | peeksym = c; |
| 1998 | word(NOWORD); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1999 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2000 | return copyw(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2003 | static char **wordlist(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2004 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2005 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2006 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2007 | c = yylex(0); |
| 2008 | if (c != IN) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2009 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2010 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2011 | } |
| 2012 | startl = 0; |
| 2013 | while ((c = yylex(0)) == WORD) |
| 2014 | word(yylval.cp); |
| 2015 | word(NOWORD); |
| 2016 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2017 | return copyw(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * supporting functions |
| 2022 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2023 | static struct op *list(struct op *t1, struct op *t2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2024 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2025 | DBGPRINTF7(("LIST: enter, t1=%p, t2=%p\n", t1, t2)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2026 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2027 | if (t1 == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2028 | return t2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2029 | if (t2 == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2030 | return t1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2031 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2032 | return block(TLIST, t1, t2, NOWORDS); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2035 | 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] | 2036 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2037 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2038 | |
| 2039 | DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type])); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2040 | |
| 2041 | t = newtp(); |
| 2042 | t->type = type; |
| 2043 | t->left = t1; |
| 2044 | t->right = t2; |
| 2045 | t->words = wp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2046 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2047 | DBGPRINTF7(("BLOCK: inserted %p between %p and %p\n", t, t1, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2048 | t2)); |
| 2049 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2050 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2053 | /* 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] | 2054 | static int rlookup(char *n) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2055 | { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2056 | struct res { |
| 2057 | char r_name[6]; |
| 2058 | int16_t r_val; |
| 2059 | }; |
| 2060 | static const struct res restab[] = { |
| 2061 | { "for" , FOR }, |
| 2062 | { "case" , CASE }, |
| 2063 | { "esac" , ESAC }, |
| 2064 | { "while", WHILE }, |
| 2065 | { "do" , DO }, |
| 2066 | { "done" , DONE }, |
| 2067 | { "if" , IF }, |
| 2068 | { "in" , IN }, |
| 2069 | { "then" , THEN }, |
| 2070 | { "else" , ELSE }, |
| 2071 | { "elif" , ELIF }, |
| 2072 | { "until", UNTIL }, |
| 2073 | { "fi" , FI }, |
| 2074 | { ";;" , BREAK }, |
| 2075 | { "||" , LOGOR }, |
| 2076 | { "&&" , LOGAND }, |
| 2077 | { "{" , '{' }, |
| 2078 | { "}" , '}' }, |
| 2079 | { "." , DOT }, |
| 2080 | { }, |
| 2081 | }; |
| 2082 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2083 | const struct res *rp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2084 | |
| 2085 | DBGPRINTF7(("RLOOKUP: enter, n is %s\n", n)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2086 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2087 | for (rp = restab; rp->r_name[0]; rp++) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2088 | if (strcmp(rp->r_name, n) == 0) { |
| 2089 | DBGPRINTF7(("RLOOKUP: match, returning %d\n", rp->r_val)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2090 | return rp->r_val; /* Return numeric code for shell multiline */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | DBGPRINTF7(("RLOOKUP: NO match, returning 0\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2094 | return 0; /* Not a shell multiline */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2097 | static struct op *newtp(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2098 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2099 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2100 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2101 | t = (struct op *) tree(sizeof(*t)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2102 | t->type = 0; |
| 2103 | t->words = NULL; |
| 2104 | t->ioact = NULL; |
| 2105 | t->left = NULL; |
| 2106 | t->right = NULL; |
| 2107 | t->str = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2108 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2109 | DBGPRINTF3(("NEWTP: allocated %p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2110 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2111 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2114 | static struct op *namelist(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2115 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2116 | DBGPRINTF7(("NAMELIST: enter, t=%p, type %s, iolist=%p\n", t, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2117 | T_CMD_NAMES[t->type], iolist)); |
| 2118 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2119 | if (iolist) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2120 | iolist = addword((char *) NULL, iolist); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2121 | t->ioact = copyio(); |
| 2122 | } else |
| 2123 | t->ioact = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2124 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2125 | if (t->type != TCOM) { |
| 2126 | if (t->type != TPAREN && t->ioact != NULL) { |
| 2127 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 2128 | t->ioact = t->left->ioact; |
| 2129 | t->left->ioact = NULL; |
| 2130 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2131 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2132 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2133 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2134 | word(NOWORD); |
| 2135 | t->words = copyw(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2136 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2137 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2140 | static char **copyw(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2141 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2142 | char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2143 | |
| 2144 | wd = getwords(wdlist); |
| 2145 | wdlist = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2146 | return wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2149 | static void word(char *cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2150 | { |
| 2151 | wdlist = addword(cp, wdlist); |
| 2152 | } |
| 2153 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2154 | static struct ioword **copyio(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2155 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2156 | struct ioword **iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2157 | |
| 2158 | iop = (struct ioword **) getwords(iolist); |
| 2159 | iolist = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2160 | return iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2163 | static struct ioword *io(int u, int f, char *cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2164 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2165 | struct ioword *iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2166 | |
| 2167 | iop = (struct ioword *) tree(sizeof(*iop)); |
| 2168 | iop->io_unit = u; |
| 2169 | iop->io_flag = f; |
| 2170 | iop->io_name = cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2171 | iolist = addword((char *) iop, iolist); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2172 | return iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2175 | static int yylex(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2176 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2177 | int c, c1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2178 | int atstart; |
| 2179 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2180 | c = peeksym; |
| 2181 | if (c > 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2182 | peeksym = 0; |
| 2183 | if (c == '\n') |
| 2184 | startl = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2185 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2186 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2187 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2188 | nlseen = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2189 | atstart = startl; |
| 2190 | startl = 0; |
| 2191 | yylval.i = 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2192 | global_env.linep = line; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2193 | |
| 2194 | /* MALAMO */ |
| 2195 | line[LINELIM - 1] = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2196 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2197 | loop: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2198 | while ((c = my_getc(0)) == ' ' || c == '\t') /* Skip whitespace */ |
| 2199 | ; |
| 2200 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2201 | switch (c) { |
| 2202 | default: |
| 2203 | if (any(c, "0123456789")) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2204 | c1 = my_getc(0); |
| 2205 | unget(c1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2206 | if (c1 == '<' || c1 == '>') { |
| 2207 | iounit = c - '0'; |
| 2208 | goto loop; |
| 2209 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2210 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2211 | c = c1; |
| 2212 | } |
| 2213 | break; |
| 2214 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2215 | case '#': /* Comment, skip to next newline or End-of-string */ |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2216 | while ((c = my_getc(0)) != '\0' && c != '\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2217 | unget(c); |
| 2218 | goto loop; |
| 2219 | |
| 2220 | case 0: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2221 | DBGPRINTF5(("YYLEX: return 0, c=%d\n", c)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2222 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2223 | |
| 2224 | case '$': |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2225 | DBGPRINTF9(("YYLEX: found $\n")); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2226 | *global_env.linep++ = c; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2227 | c = my_getc(0); |
| 2228 | if (c == '{') { |
| 2229 | c = collect(c, '}'); |
| 2230 | if (c != '\0') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2231 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2232 | goto pack; |
| 2233 | } |
| 2234 | break; |
| 2235 | |
| 2236 | case '`': |
| 2237 | case '\'': |
| 2238 | case '"': |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2239 | c = collect(c, c); |
| 2240 | if (c != '\0') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2241 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2242 | goto pack; |
| 2243 | |
| 2244 | case '|': |
| 2245 | case '&': |
| 2246 | case ';': |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2247 | startl = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2248 | /* If more chars process them, else return NULL char */ |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2249 | c1 = dual(c); |
| 2250 | if (c1 != '\0') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2251 | return c1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2252 | return c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2253 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2254 | case '^': |
| 2255 | startl = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2256 | return '|'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2257 | case '>': |
| 2258 | case '<': |
| 2259 | diag(c); |
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 '\n': |
| 2263 | nlseen++; |
| 2264 | gethere(); |
| 2265 | startl = 1; |
| 2266 | if (multiline || cf & CONTIN) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2267 | if (interactive && global_env.iop <= iostack) { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 2268 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2269 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2270 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2271 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2272 | #endif |
| 2273 | } |
| 2274 | if (cf & CONTIN) |
| 2275 | goto loop; |
| 2276 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2277 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2278 | |
| 2279 | case '(': |
| 2280 | case ')': |
| 2281 | startl = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2282 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | unget(c); |
| 2286 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2287 | pack: |
| 2288 | while ((c = my_getc(0)) != '\0' && !any(c, "`$ '\"\t;&<>()|^\n")) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2289 | if (global_env.linep >= elinep) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2290 | err("word too long"); |
| 2291 | else |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2292 | *global_env.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2293 | }; |
| 2294 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2295 | unget(c); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2296 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2297 | if (any(c, "\"'`$")) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2298 | goto loop; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2299 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2300 | *global_env.linep++ = '\0'; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2301 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2302 | if (atstart) { |
| 2303 | c = rlookup(line); |
| 2304 | if (c != 0) { |
| 2305 | startl = 1; |
| 2306 | return c; |
| 2307 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2308 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2309 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2310 | yylval.cp = strsave(line, areanum); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2311 | return WORD; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2314 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2315 | static int collect(int c, int c1) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2316 | { |
| 2317 | char s[2]; |
| 2318 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2319 | DBGPRINTF8(("COLLECT: enter, c=%d, c1=%d\n", c, c1)); |
| 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 | while ((c = my_getc(c1)) != c1) { |
| 2323 | if (c == 0) { |
| 2324 | unget(c); |
| 2325 | s[0] = c1; |
| 2326 | s[1] = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2327 | prs("no closing "); |
| 2328 | yyerror(s); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2329 | return YYERRCODE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2330 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2331 | if (interactive && c == '\n' && global_env.iop <= iostack) { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 2332 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2333 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2334 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2335 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2336 | #endif |
| 2337 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2338 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2339 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2340 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 2341 | *global_env.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2342 | |
| 2343 | DBGPRINTF8(("COLLECT: return 0, line is %s\n", line)); |
| 2344 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2345 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2348 | /* "multiline commands" helper func */ |
| 2349 | /* see if next 2 chars form a shell multiline */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2350 | static int dual(int c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2351 | { |
| 2352 | char s[3]; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2353 | char *cp = s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2354 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2355 | DBGPRINTF8(("DUAL: enter, c=%d\n", c)); |
| 2356 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2357 | *cp++ = c; /* c is the given "peek" char */ |
| 2358 | *cp++ = my_getc(0); /* get next char of input */ |
| 2359 | *cp = '\0'; /* add EOS marker */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2360 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2361 | c = rlookup(s); /* see if 2 chars form a shell multiline */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2362 | if (c == 0) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2363 | unget(*--cp); /* String is not a shell multiline, put peek char back */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2364 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2365 | return c; /* String is multiline, return numeric multiline (restab) code */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2368 | static void diag(int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2369 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2370 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2371 | |
| 2372 | DBGPRINTF8(("DIAG: enter, ec=%d\n", ec)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2373 | |
| 2374 | c = my_getc(0); |
| 2375 | if (c == '>' || c == '<') { |
| 2376 | if (c != ec) |
| 2377 | zzerr(); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2378 | yylval.i = (ec == '>' ? IOWRITE | IOCAT : IOHERE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2379 | c = my_getc(0); |
| 2380 | } else |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2381 | yylval.i = (ec == '>' ? IOWRITE : IOREAD); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2382 | if (c != '&' || yylval.i == IOHERE) |
| 2383 | unget(c); |
| 2384 | else |
| 2385 | yylval.i |= IODUP; |
| 2386 | } |
| 2387 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2388 | static char *tree(unsigned size) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2389 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2390 | char *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2391 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2392 | t = getcell(size); |
| 2393 | if (t == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2394 | DBGPRINTF2(("TREE: getcell(%d) failed!\n", size)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2395 | prs("command line too complicated\n"); |
| 2396 | fail(); |
| 2397 | /* NOTREACHED */ |
| 2398 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2399 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2402 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2403 | /* VARARGS1 */ |
| 2404 | /* ARGSUSED */ |
| 2405 | |
| 2406 | /* -------- exec.c -------- */ |
| 2407 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2408 | static struct op **find1case(struct op *t, const char *w) |
| 2409 | { |
| 2410 | struct op *t1; |
| 2411 | struct op **tp; |
| 2412 | char **wp; |
| 2413 | char *cp; |
| 2414 | |
| 2415 | if (t == NULL) { |
| 2416 | DBGPRINTF3(("FIND1CASE: enter, t==NULL, returning.\n")); |
| 2417 | return NULL; |
| 2418 | } |
| 2419 | |
| 2420 | DBGPRINTF3(("FIND1CASE: enter, t->type=%d (%s)\n", t->type, |
| 2421 | T_CMD_NAMES[t->type])); |
| 2422 | |
| 2423 | if (t->type == TLIST) { |
| 2424 | tp = find1case(t->left, w); |
| 2425 | if (tp != NULL) { |
| 2426 | DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=%p\n", tp)); |
| 2427 | return tp; |
| 2428 | } |
| 2429 | t1 = t->right; /* TPAT */ |
| 2430 | } else |
| 2431 | t1 = t; |
| 2432 | |
| 2433 | for (wp = t1->words; *wp;) { |
| 2434 | cp = evalstr(*wp++, DOSUB); |
| 2435 | if (cp && gmatch(w, cp)) { |
| 2436 | DBGPRINTF3(("FIND1CASE: returning &t1->left= %p.\n", |
| 2437 | &t1->left)); |
| 2438 | return &t1->left; |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | DBGPRINTF(("FIND1CASE: returning NULL\n")); |
| 2443 | return NULL; |
| 2444 | } |
| 2445 | |
| 2446 | static struct op *findcase(struct op *t, const char *w) |
| 2447 | { |
| 2448 | struct op **tp; |
| 2449 | |
| 2450 | tp = find1case(t, w); |
| 2451 | return tp != NULL ? *tp : NULL; |
| 2452 | } |
| 2453 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2454 | /* |
| 2455 | * execute tree |
| 2456 | */ |
| 2457 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2458 | static int execute(struct op *t, int *pin, int *pout, int act) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2459 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2460 | struct op *t1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2461 | volatile int i, rv, a; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2462 | const char *cp; |
| 2463 | char **wp, **wp2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2464 | struct var *vp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2465 | struct op *outtree_save; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2466 | struct brkcon bc; |
| 2467 | |
| 2468 | #if __GNUC__ |
| 2469 | /* Avoid longjmp clobbering */ |
| 2470 | (void) ℘ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2471 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2472 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2473 | if (t == NULL) { |
| 2474 | DBGPRINTF4(("EXECUTE: enter, t==null, returning.\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2475 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2478 | DBGPRINTF(("EXECUTE: t=%p, t->type=%d (%s), t->words is %s\n", t, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2479 | t->type, T_CMD_NAMES[t->type], |
| 2480 | ((t->words == NULL) ? "NULL" : t->words[0]))); |
| 2481 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2482 | rv = 0; |
| 2483 | a = areanum++; |
| 2484 | wp = (wp2 = t->words) != NULL |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2485 | ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY) |
| 2486 | : NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2487 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2488 | switch (t->type) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2489 | case TDOT: |
| 2490 | DBGPRINTF3(("EXECUTE: TDOT\n")); |
| 2491 | |
| 2492 | outtree_save = outtree; |
| 2493 | |
| 2494 | newfile(evalstr(t->words[0], DOALL)); |
| 2495 | |
| 2496 | t->left = dowholefile(TLIST, 0); |
| 2497 | t->right = NULL; |
| 2498 | |
| 2499 | outtree = outtree_save; |
| 2500 | |
| 2501 | if (t->left) |
| 2502 | rv = execute(t->left, pin, pout, 0); |
| 2503 | if (t->right) |
| 2504 | rv = execute(t->right, pin, pout, 0); |
| 2505 | break; |
| 2506 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2507 | case TPAREN: |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 2508 | rv = execute(t->left, pin, pout, 0); |
| 2509 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2510 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2511 | case TCOM: |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 2512 | rv = forkexec(t, pin, pout, act, wp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2513 | break; |
| 2514 | |
| 2515 | case TPIPE: |
| 2516 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2517 | int pv[2]; |
| 2518 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2519 | rv = openpipe(pv); |
| 2520 | if (rv < 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2521 | break; |
| 2522 | pv[0] = remap(pv[0]); |
| 2523 | pv[1] = remap(pv[1]); |
| 2524 | (void) execute(t->left, pin, pv, 0); |
| 2525 | rv = execute(t->right, pv, pout, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2526 | } |
| 2527 | break; |
| 2528 | |
| 2529 | case TLIST: |
| 2530 | (void) execute(t->left, pin, pout, 0); |
| 2531 | rv = execute(t->right, pin, pout, 0); |
| 2532 | break; |
| 2533 | |
| 2534 | case TASYNC: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2535 | { |
| 2536 | int hinteractive = interactive; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2537 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2538 | DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n")); |
| 2539 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2540 | i = vfork(); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2541 | if (i == 0) { /* child */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2542 | signal(SIGINT, SIG_IGN); |
| 2543 | signal(SIGQUIT, SIG_IGN); |
| 2544 | if (interactive) |
| 2545 | signal(SIGTERM, SIG_DFL); |
| 2546 | interactive = 0; |
| 2547 | if (pin == NULL) { |
| 2548 | close(0); |
Denis Vlasenko | 2b54aaa | 2007-05-09 22:16:08 +0000 | [diff] [blame] | 2549 | xopen(bb_dev_null, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2550 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2551 | _exit(execute(t->left, pin, pout, FEXEC)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2552 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2553 | interactive = hinteractive; |
| 2554 | if (i != -1) { |
| 2555 | setval(lookup("!"), putn(i)); |
| 2556 | if (pin != NULL) |
| 2557 | closepipe(pin); |
| 2558 | if (interactive) { |
| 2559 | prs(putn(i)); |
| 2560 | prs("\n"); |
| 2561 | } |
| 2562 | } else |
| 2563 | rv = -1; |
| 2564 | setstatus(rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2565 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2566 | break; |
| 2567 | |
| 2568 | case TOR: |
| 2569 | case TAND: |
| 2570 | rv = execute(t->left, pin, pout, 0); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2571 | t1 = t->right; |
| 2572 | if (t1 != NULL && (rv == 0) == (t->type == TAND)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2573 | rv = execute(t1, pin, pout, 0); |
| 2574 | break; |
| 2575 | |
| 2576 | case TFOR: |
| 2577 | if (wp == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2578 | wp = dolv + 1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2579 | i = dolc; |
| 2580 | if (i < 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2581 | i = 0; |
| 2582 | } else { |
| 2583 | i = -1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2584 | while (*wp++ != NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2585 | } |
| 2586 | vp = lookup(t->str); |
| 2587 | while (setjmp(bc.brkpt)) |
| 2588 | if (isbreak) |
| 2589 | goto broken; |
| 2590 | brkset(&bc); |
| 2591 | for (t1 = t->left; i-- && *wp != NULL;) { |
| 2592 | setval(vp, *wp++); |
| 2593 | rv = execute(t1, pin, pout, 0); |
| 2594 | } |
| 2595 | brklist = brklist->nextlev; |
| 2596 | break; |
| 2597 | |
| 2598 | case TWHILE: |
| 2599 | case TUNTIL: |
| 2600 | while (setjmp(bc.brkpt)) |
| 2601 | if (isbreak) |
| 2602 | goto broken; |
| 2603 | brkset(&bc); |
| 2604 | t1 = t->left; |
| 2605 | while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE)) |
| 2606 | rv = execute(t->right, pin, pout, 0); |
| 2607 | brklist = brklist->nextlev; |
| 2608 | break; |
| 2609 | |
| 2610 | case TIF: |
| 2611 | case TELIF: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2612 | if (t->right != NULL) { |
| 2613 | rv = !execute(t->left, pin, pout, 0) ? |
| 2614 | execute(t->right->left, pin, pout, 0) : |
| 2615 | execute(t->right->right, pin, pout, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2616 | } |
| 2617 | break; |
| 2618 | |
| 2619 | case TCASE: |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2620 | cp = evalstr(t->str, DOSUB | DOTRIM); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2621 | if (cp == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2622 | cp = ""; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2623 | |
| 2624 | DBGPRINTF7(("EXECUTE: TCASE, t->str is %s, cp is %s\n", |
| 2625 | ((t->str == NULL) ? "NULL" : t->str), |
| 2626 | ((cp == NULL) ? "NULL" : cp))); |
| 2627 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2628 | t1 = findcase(t->left, cp); |
| 2629 | if (t1 != NULL) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2630 | DBGPRINTF7(("EXECUTE: TCASE, calling execute(t=%p, t1=%p)...\n", t, t1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2631 | rv = execute(t1, pin, pout, 0); |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2632 | 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] | 2633 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2634 | break; |
| 2635 | |
| 2636 | case TBRACE: |
| 2637 | /* |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2638 | iopp = t->ioact; |
| 2639 | if (i) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2640 | while (*iopp) |
| 2641 | if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) { |
| 2642 | rv = -1; |
| 2643 | break; |
| 2644 | } |
| 2645 | */ |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2646 | if (rv >= 0) { |
| 2647 | t1 = t->left; |
| 2648 | if (t1) { |
| 2649 | rv = execute(t1, pin, pout, 0); |
| 2650 | } |
| 2651 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2652 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2653 | |
| 2654 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2655 | |
Denis Vlasenko | e471275 | 2007-04-14 15:08:41 +0000 | [diff] [blame] | 2656 | broken: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2657 | t->words = wp2; |
| 2658 | isbreak = 0; |
| 2659 | freehere(areanum); |
| 2660 | freearea(areanum); |
| 2661 | areanum = a; |
| 2662 | if (interactive && intr) { |
| 2663 | closeall(); |
| 2664 | fail(); |
| 2665 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2666 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2667 | i = trapset; |
| 2668 | if (i != 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2669 | trapset = 0; |
| 2670 | runtrap(i); |
| 2671 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2672 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2673 | DBGPRINTF(("EXECUTE: returning from t=%p, rv=%d\n", t, rv)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2674 | return rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2677 | typedef int (*builtin_func_ptr)(struct op *); |
| 2678 | |
Denis Vlasenko | e471275 | 2007-04-14 15:08:41 +0000 | [diff] [blame] | 2679 | static builtin_func_ptr inbuilt(const char *s) |
| 2680 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2681 | const struct builtincmd *bp; |
| 2682 | |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 2683 | for (bp = builtincmds; bp->name; bp++) |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2684 | if (strcmp(bp->name, s) == 0) |
| 2685 | return bp->builtinfunc; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2686 | return NULL; |
| 2687 | } |
| 2688 | |
| 2689 | static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2690 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2691 | pid_t newpid; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2692 | int i, rv; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2693 | builtin_func_ptr shcom = NULL; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2694 | int f; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2695 | const char *cp = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2696 | struct ioword **iopp; |
| 2697 | int resetsig; |
| 2698 | char **owp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2699 | int forked = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2700 | |
| 2701 | int *hpin = pin; |
| 2702 | int *hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2703 | char *hwp; |
| 2704 | int hinteractive; |
| 2705 | int hintr; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2706 | struct brkcon *hbrklist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2707 | int hexecflg; |
| 2708 | |
| 2709 | #if __GNUC__ |
| 2710 | /* Avoid longjmp clobbering */ |
| 2711 | (void) &pin; |
| 2712 | (void) &pout; |
| 2713 | (void) ℘ |
| 2714 | (void) &shcom; |
| 2715 | (void) &cp; |
| 2716 | (void) &resetsig; |
| 2717 | (void) &owp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2718 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2719 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2720 | DBGPRINTF(("FORKEXEC: t=%p, pin %p, pout %p, act %d\n", t, pin, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2721 | pout, act)); |
| 2722 | DBGPRINTF7(("FORKEXEC: t->words is %s\n", |
| 2723 | ((t->words == NULL) ? "NULL" : t->words[0]))); |
| 2724 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2725 | owp = wp; |
| 2726 | resetsig = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2727 | rv = -1; /* system-detected error */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2728 | if (t->type == TCOM) { |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 2729 | while (*wp++ != NULL) |
| 2730 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2731 | cp = *wp; |
| 2732 | |
| 2733 | /* strip all initial assignments */ |
| 2734 | /* not correct wrt PATH=yyy command etc */ |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 2735 | if (FLAG['x']) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2736 | DBGPRINTF9(("FORKEXEC: echo'ing, cp=%p, wp=%p, owp=%p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2737 | cp, wp, owp)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2738 | echo(cp ? wp : owp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2739 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2740 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2741 | if (cp == NULL && t->ioact == NULL) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2742 | while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 2743 | continue; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2744 | DBGPRINTF(("FORKEXEC: returning setstatus()\n")); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2745 | return setstatus(0); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2746 | } |
| 2747 | if (cp != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2748 | shcom = inbuilt(cp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2749 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2750 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2751 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2752 | t->words = wp; |
| 2753 | f = act; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2754 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2755 | DBGPRINTF(("FORKEXEC: shcom %p, f&FEXEC 0x%x, owp %p\n", shcom, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2756 | f & FEXEC, owp)); |
| 2757 | |
| 2758 | if (shcom == NULL && (f & FEXEC) == 0) { |
| 2759 | /* Save values in case the child process alters them */ |
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")); |
| 2769 | |
| 2770 | newpid = vfork(); |
| 2771 | |
| 2772 | if (newpid == -1) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 2773 | DBGPRINTF(("FORKEXEC: ERROR, cannot vfork()!\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2774 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 2777 | if (newpid > 0) { /* Parent */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2778 | /* Restore values */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2779 | pin = hpin; |
| 2780 | pout = hpout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2781 | *wp = hwp; |
| 2782 | interactive = hinteractive; |
| 2783 | intr = hintr; |
| 2784 | brklist = hbrklist; |
| 2785 | execflg = hexecflg; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2786 | /* moved up |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2787 | if (i == -1) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2788 | return rv; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2789 | */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2790 | if (pin != NULL) |
| 2791 | closepipe(pin); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2792 | |
| 2793 | return (pout == NULL ? setstatus(waitfor(newpid, 0)) : 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2796 | /* Must be the child process, pid should be 0 */ |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2797 | DBGPRINTF(("FORKEXEC: child process, shcom=%p\n", shcom)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2798 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2799 | if (interactive) { |
| 2800 | signal(SIGINT, SIG_IGN); |
| 2801 | signal(SIGQUIT, SIG_IGN); |
| 2802 | resetsig = 1; |
| 2803 | } |
| 2804 | interactive = 0; |
| 2805 | intr = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2806 | forked = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2807 | brklist = 0; |
| 2808 | execflg = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2809 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2810 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2811 | if (owp != NULL) |
| 2812 | while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
| 2813 | if (shcom == NULL) |
| 2814 | export(lookup(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2815 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2816 | #ifdef COMPIPE |
| 2817 | if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) { |
| 2818 | err("piping to/from shell builtins not yet done"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2819 | if (forked) |
| 2820 | _exit(-1); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2821 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2822 | } |
| 2823 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2824 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2825 | if (pin != NULL) { |
Denis Vlasenko | 2b54aaa | 2007-05-09 22:16:08 +0000 | [diff] [blame] | 2826 | xmove_fd(pin[0], 0); |
Denis Vlasenko | 847fa77 | 2008-01-28 22:45:43 +0000 | [diff] [blame] | 2827 | if (pin[1] != 0) |
| 2828 | close(pin[1]); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2829 | } |
| 2830 | if (pout != NULL) { |
Denis Vlasenko | 2b54aaa | 2007-05-09 22:16:08 +0000 | [diff] [blame] | 2831 | xmove_fd(pout[1], 1); |
Denis Vlasenko | 847fa77 | 2008-01-28 22:45:43 +0000 | [diff] [blame] | 2832 | if (pout[1] != 1) |
| 2833 | close(pout[0]); |
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 | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2836 | iopp = t->ioact; |
| 2837 | if (iopp != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2838 | if (shcom != NULL && shcom != doexec) { |
| 2839 | prs(cp); |
| 2840 | err(": cannot redirect shell command"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2841 | if (forked) |
| 2842 | _exit(-1); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2843 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2844 | } |
| 2845 | while (*iopp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2846 | if (iosetup(*iopp++, pin != NULL, pout != NULL)) { |
| 2847 | if (forked) |
| 2848 | _exit(rv); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2849 | return rv; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2850 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2851 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2852 | |
| 2853 | if (shcom) { |
| 2854 | i = setstatus((*shcom) (t)); |
| 2855 | if (forked) |
| 2856 | _exit(i); |
| 2857 | DBGPRINTF(("FORKEXEC: returning i=%d\n", i)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2858 | return i; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2861 | /* should use FIOCEXCL */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2862 | for (i = FDBASE; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2863 | close(i); |
| 2864 | if (resetsig) { |
| 2865 | signal(SIGINT, SIG_DFL); |
| 2866 | signal(SIGQUIT, SIG_DFL); |
| 2867 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2868 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2869 | if (t->type == TPAREN) |
| 2870 | _exit(execute(t->left, NOPIPE, NOPIPE, FEXEC)); |
| 2871 | if (wp[0] == NULL) |
| 2872 | _exit(0); |
| 2873 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 2874 | cp = rexecve(wp[0], wp, makenv(0, NULL)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2875 | prs(wp[0]); |
| 2876 | prs(": "); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2877 | err(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2878 | if (!execflg) |
| 2879 | trap[0] = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2880 | |
| 2881 | DBGPRINTF(("FORKEXEC: calling leave(), pid=%d\n", newpid)); |
| 2882 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2883 | leave(); |
| 2884 | /* NOTREACHED */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2885 | _exit(1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | /* |
| 2889 | * 0< 1> are ignored as required |
| 2890 | * within pipelines. |
| 2891 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2892 | static int iosetup(struct ioword *iop, int pipein, int pipeout) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2893 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2894 | int u = -1; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2895 | char *cp = NULL; |
| 2896 | const char *msg; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2897 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2898 | DBGPRINTF(("IOSETUP: iop %p, pipein %i, pipeout %i\n", iop, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2899 | pipein, pipeout)); |
| 2900 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2901 | if (iop->io_unit == IODEFAULT) /* take default */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2902 | iop->io_unit = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2903 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2904 | if (pipein && iop->io_unit == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2905 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2906 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2907 | if (pipeout && iop->io_unit == 1) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2908 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2909 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2910 | msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2911 | if ((iop->io_flag & IOHERE) == 0) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2912 | cp = iop->io_name; /* huh?? */ |
| 2913 | cp = evalstr(cp, DOSUB | DOTRIM); |
| 2914 | if (cp == NULL) |
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 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2917 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2918 | if (iop->io_flag & IODUP) { |
| 2919 | if (cp[1] || (!isdigit(*cp) && *cp != '-')) { |
| 2920 | prs(cp); |
| 2921 | err(": illegal >& argument"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2922 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2923 | } |
| 2924 | if (*cp == '-') |
| 2925 | iop->io_flag = IOCLOSE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2926 | iop->io_flag &= ~(IOREAD | IOWRITE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2927 | } |
| 2928 | switch (iop->io_flag) { |
| 2929 | case IOREAD: |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 2930 | u = open(cp, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2931 | break; |
| 2932 | |
| 2933 | case IOHERE: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2934 | case IOHERE | IOXHERE: |
| 2935 | u = herein(iop->io_name, iop->io_flag & IOXHERE); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2936 | cp = (char*)"here file"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2937 | break; |
| 2938 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2939 | case IOWRITE | IOCAT: |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 2940 | u = open(cp, O_WRONLY); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2941 | if (u >= 0) { |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 2942 | lseek(u, (long) 0, SEEK_END); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2943 | break; |
| 2944 | } |
| 2945 | case IOWRITE: |
| 2946 | u = creat(cp, 0666); |
| 2947 | break; |
| 2948 | |
| 2949 | case IODUP: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2950 | u = dup2(*cp - '0', iop->io_unit); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2951 | break; |
| 2952 | |
| 2953 | case IOCLOSE: |
| 2954 | close(iop->io_unit); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2955 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2956 | } |
| 2957 | if (u < 0) { |
| 2958 | prs(cp); |
| 2959 | prs(": cannot "); |
| 2960 | warn(msg); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2961 | return 1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2962 | } |
| 2963 | if (u != iop->io_unit) { |
| 2964 | dup2(u, iop->io_unit); |
| 2965 | close(u); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2966 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2967 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2970 | /* |
| 2971 | * Enter a new loop level (marked for break/continue). |
| 2972 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2973 | static void brkset(struct brkcon *bc) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2974 | { |
| 2975 | bc->nextlev = brklist; |
| 2976 | brklist = bc; |
| 2977 | } |
| 2978 | |
| 2979 | /* |
| 2980 | * Wait for the last process created. |
| 2981 | * Print a message for each process found |
| 2982 | * that was killed by a signal. |
| 2983 | * Ignore interrupt signals while waiting |
| 2984 | * unless `canintr' is true. |
| 2985 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2986 | static int waitfor(int lastpid, int canintr) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2987 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2988 | int pid, rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2989 | int s; |
| 2990 | int oheedint = heedint; |
| 2991 | |
| 2992 | heedint = 0; |
| 2993 | rv = 0; |
| 2994 | do { |
| 2995 | pid = wait(&s); |
| 2996 | if (pid == -1) { |
| 2997 | if (errno != EINTR || canintr) |
| 2998 | break; |
| 2999 | } else { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3000 | rv = WAITSIG(s); |
| 3001 | if (rv != 0) { |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 3002 | if (rv < ARRAY_SIZE(signame)) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3003 | if (signame[rv] != NULL) { |
| 3004 | if (pid != lastpid) { |
| 3005 | prn(pid); |
| 3006 | prs(": "); |
| 3007 | } |
| 3008 | prs(signame[rv]); |
| 3009 | } |
| 3010 | } else { |
| 3011 | if (pid != lastpid) { |
| 3012 | prn(pid); |
| 3013 | prs(": "); |
| 3014 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3015 | prs("Signal "); |
| 3016 | prn(rv); |
| 3017 | prs(" "); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3018 | } |
| 3019 | if (WAITCORE(s)) |
| 3020 | prs(" - core dumped"); |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 3021 | if (rv >= ARRAY_SIZE(signame) || signame[rv]) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3022 | prs("\n"); |
| 3023 | rv = -1; |
| 3024 | } else |
| 3025 | rv = WAITVAL(s); |
| 3026 | } |
| 3027 | } while (pid != lastpid); |
| 3028 | heedint = oheedint; |
| 3029 | if (intr) { |
| 3030 | if (interactive) { |
| 3031 | if (canintr) |
| 3032 | intr = 0; |
| 3033 | } else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3034 | if (exstat == 0) |
| 3035 | exstat = rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3036 | onintr(0); |
| 3037 | } |
| 3038 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3039 | return rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3042 | static int setstatus(int s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3043 | { |
| 3044 | exstat = s; |
| 3045 | setval(lookup("?"), putn(s)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3046 | return s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | /* |
| 3050 | * PATH-searching interface to execve. |
| 3051 | * If getenv("PATH") were kept up-to-date, |
| 3052 | * execvp might be used. |
| 3053 | */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3054 | static const char *rexecve(char *c, char **v, char **envp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3055 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3056 | int i; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3057 | const char *sp; |
| 3058 | char *tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3059 | int eacces = 0, asis = 0; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3060 | char *name = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3061 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3062 | if (ENABLE_FEATURE_SH_STANDALONE) { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3063 | if (find_applet_by_name(name) >= 0) { |
Rob Landley | a299efb | 2006-08-10 21:46:43 +0000 | [diff] [blame] | 3064 | /* We have to exec here since we vforked. Running |
Denis Vlasenko | e4f2d06 | 2007-04-11 17:03:19 +0000 | [diff] [blame] | 3065 | * run_applet_and_exit() won't work and bad things |
Rob Landley | a299efb | 2006-08-10 21:46:43 +0000 | [diff] [blame] | 3066 | * will happen. */ |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 3067 | execve(bb_busybox_exec_path, v, envp); |
Rob Landley | a299efb | 2006-08-10 21:46:43 +0000 | [diff] [blame] | 3068 | } |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3069 | } |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3070 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3071 | DBGPRINTF(("REXECVE: c=%p, v=%p, envp=%p\n", c, v, envp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3072 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3073 | sp = any('/', c) ? "" : path->value; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3074 | asis = (*sp == '\0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3075 | while (asis || *sp != '\0') { |
| 3076 | asis = 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3077 | tp = global_env.linep; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3078 | for (; *sp != '\0'; tp++) { |
| 3079 | *tp = *sp++; |
| 3080 | if (*tp == ':') { |
| 3081 | asis = (*sp == '\0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3082 | break; |
| 3083 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3084 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3085 | if (tp != global_env.linep) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3086 | *tp++ = '/'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3087 | for (i = 0; (*tp++ = c[i++]) != '\0';); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3088 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3089 | DBGPRINTF3(("REXECVE: global_env.linep is %s\n", global_env.linep)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3090 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3091 | execve(global_env.linep, v, envp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3092 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3093 | switch (errno) { |
| 3094 | case ENOEXEC: |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3095 | *v = global_env.linep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3096 | tp = *--v; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3097 | *v = global_env.linep; |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 3098 | execve(DEFAULT_SHELL, v, envp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3099 | *v = tp; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3100 | return "no Shell"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3101 | |
| 3102 | case ENOMEM: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3103 | return (char *) bb_msg_memory_exhausted; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3104 | |
| 3105 | case E2BIG: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3106 | return "argument list too long"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3107 | |
| 3108 | case EACCES: |
| 3109 | eacces++; |
| 3110 | break; |
| 3111 | } |
| 3112 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3113 | return errno == ENOENT ? "not found" : "cannot execute"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | /* |
| 3117 | * Run the command produced by generator `f' |
| 3118 | * applied to stream `arg'. |
| 3119 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3120 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3121 | { |
| 3122 | struct op *otree; |
| 3123 | struct wdblock *swdlist; |
| 3124 | struct wdblock *siolist; |
| 3125 | jmp_buf ev, rt; |
| 3126 | xint *ofail; |
| 3127 | int rv; |
| 3128 | |
| 3129 | #if __GNUC__ |
| 3130 | /* Avoid longjmp clobbering */ |
| 3131 | (void) &rv; |
| 3132 | #endif |
| 3133 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3134 | DBGPRINTF(("RUN: enter, areanum %d, outtree %p, failpt %p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3135 | areanum, outtree, failpt)); |
| 3136 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3137 | areanum++; |
| 3138 | swdlist = wdlist; |
| 3139 | siolist = iolist; |
| 3140 | otree = outtree; |
| 3141 | ofail = failpt; |
| 3142 | rv = -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3143 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3144 | errpt = ev; |
| 3145 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3146 | wdlist = 0; |
| 3147 | iolist = 0; |
| 3148 | pushio(argp, f); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3149 | global_env.iobase = global_env.iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3150 | yynerrs = 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3151 | failpt = rt; |
| 3152 | if (setjmp(failpt) == 0 && yyparse() == 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3153 | rv = execute(outtree, NOPIPE, NOPIPE, 0); |
| 3154 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3155 | } else { |
| 3156 | DBGPRINTF(("RUN: error from newenv()!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3157 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3158 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3159 | wdlist = swdlist; |
| 3160 | iolist = siolist; |
| 3161 | failpt = ofail; |
| 3162 | outtree = otree; |
| 3163 | freearea(areanum--); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3164 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3165 | return rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | /* -------- do.c -------- */ |
| 3169 | |
| 3170 | /* |
| 3171 | * built-in commands: doX |
| 3172 | */ |
| 3173 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3174 | static int dohelp(struct op *t) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3175 | { |
| 3176 | int col; |
| 3177 | const struct builtincmd *x; |
| 3178 | |
Denis Vlasenko | 0ee3999 | 2006-12-24 15:23:28 +0000 | [diff] [blame] | 3179 | puts("\nBuilt-in commands:\n" |
| 3180 | "-------------------"); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3181 | |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 3182 | col = 0; |
| 3183 | x = builtincmds; |
| 3184 | while (x->name) { |
| 3185 | col += printf("%c%s", ((col == 0) ? '\t' : ' '), x->name); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3186 | if (col > 60) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 3187 | bb_putchar('\n'); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3188 | col = 0; |
| 3189 | } |
Denis Vlasenko | 95cb326 | 2007-04-09 03:06:34 +0000 | [diff] [blame] | 3190 | x++; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3191 | } |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3192 | #if ENABLE_FEATURE_SH_STANDALONE |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3193 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3194 | const char *applet = applet_names; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3195 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3196 | while (*applet) { |
| 3197 | col += printf("%c%s", ((col == 0) ? '\t' : ' '), applet); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3198 | if (col > 60) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 3199 | bb_putchar('\n'); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3200 | col = 0; |
| 3201 | } |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3202 | applet += strlen(applet) + 1; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3203 | } |
| 3204 | } |
| 3205 | #endif |
Denis Vlasenko | 0ee3999 | 2006-12-24 15:23:28 +0000 | [diff] [blame] | 3206 | puts("\n"); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3207 | return EXIT_SUCCESS; |
| 3208 | } |
| 3209 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3210 | static int dolabel(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3211 | { |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3212 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3215 | static int dochdir(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3216 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3217 | const char *cp, *er; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3218 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3219 | cp = t->words[1]; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3220 | if (cp == NULL) { |
| 3221 | cp = homedir->value; |
Denis Vlasenko | d244c5e | 2007-02-09 17:30:14 +0000 | [diff] [blame] | 3222 | if (cp != NULL) |
| 3223 | goto do_cd; |
| 3224 | er = ": no home directory"; |
| 3225 | } else { |
| 3226 | do_cd: |
| 3227 | if (chdir(cp) >= 0) |
| 3228 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3229 | er = ": bad directory"; |
Denis Vlasenko | d244c5e | 2007-02-09 17:30:14 +0000 | [diff] [blame] | 3230 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3231 | prs(cp != NULL ? cp : "cd"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3232 | err(er); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3233 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3234 | } |
| 3235 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3236 | static int doshift(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3237 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3238 | int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3239 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3240 | n = t->words[1] ? getn(t->words[1]) : 1; |
| 3241 | if (dolc < n) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3242 | err("nothing to shift"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3243 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3244 | } |
| 3245 | dolv[n] = dolv[0]; |
| 3246 | dolv += n; |
| 3247 | dolc -= n; |
| 3248 | setval(lookup("#"), putn(dolc)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3249 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | /* |
| 3253 | * execute login and newgrp directly |
| 3254 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3255 | static int dologin(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3256 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3257 | const char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3258 | |
| 3259 | if (interactive) { |
| 3260 | signal(SIGINT, SIG_DFL); |
| 3261 | signal(SIGQUIT, SIG_DFL); |
| 3262 | } |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3263 | cp = rexecve(t->words[0], t->words, makenv(0, NULL)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3264 | prs(t->words[0]); |
| 3265 | prs(": "); |
| 3266 | err(cp); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3267 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3270 | static int doumask(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3271 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3272 | int i, n; |
| 3273 | char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3274 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3275 | cp = t->words[1]; |
| 3276 | if (cp == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3277 | i = umask(0); |
| 3278 | umask(i); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3279 | for (n = 3 * 4; (n -= 3) >= 0;) |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 3280 | fputc('0' + ((i >> n) & 07), stderr); |
| 3281 | fputc('\n', stderr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3282 | } else { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3283 | /* huh??? '8','9' are not allowed! */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3284 | for (n = 0; *cp >= '0' && *cp <= '9'; cp++) |
| 3285 | n = n * 8 + (*cp - '0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3286 | umask(n); |
| 3287 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3288 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3291 | static int doexec(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3292 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3293 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3294 | jmp_buf ex; |
| 3295 | xint *ofail; |
| 3296 | |
| 3297 | t->ioact = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3298 | for (i = 0; (t->words[i] = t->words[i + 1]) != NULL; i++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3299 | if (i == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3300 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3301 | execflg = 1; |
| 3302 | ofail = failpt; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3303 | failpt = ex; |
| 3304 | if (setjmp(failpt) == 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3305 | execute(t, NOPIPE, NOPIPE, FEXEC); |
| 3306 | failpt = ofail; |
| 3307 | execflg = 0; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3308 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3311 | static int dodot(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3312 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3313 | int i; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3314 | const char *sp; |
| 3315 | char *tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3316 | char *cp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3317 | int maltmp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3318 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3319 | DBGPRINTF(("DODOT: enter, t=%p, tleft %p, tright %p, global_env.linep is %s\n", |
| 3320 | 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] | 3321 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3322 | cp = t->words[1]; |
| 3323 | if (cp == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3324 | DBGPRINTF(("DODOT: bad args, ret 0\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3325 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3326 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3327 | DBGPRINTF(("DODOT: cp is %s\n", cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3328 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3329 | sp = any('/', cp) ? ":" : path->value; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3330 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3331 | DBGPRINTF(("DODOT: sp is %s, global_env.linep is %s\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3332 | ((sp == NULL) ? "NULL" : sp), |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3333 | ((global_env.linep == NULL) ? "NULL" : global_env.linep))); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3334 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3335 | while (*sp) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3336 | tp = global_env.linep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3337 | while (*sp && (*tp = *sp++) != ':') |
| 3338 | tp++; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3339 | if (tp != global_env.linep) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3340 | *tp++ = '/'; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3341 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3342 | for (i = 0; (*tp++ = cp[i++]) != '\0';); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3343 | |
| 3344 | /* Original code */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3345 | i = open(global_env.linep, O_RDONLY); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3346 | if (i >= 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3347 | exstat = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3348 | maltmp = remap(i); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3349 | DBGPRINTF(("DODOT: remap=%d, exstat=%d, global_env.iofd %d, i %d, global_env.linep is %s\n", |
| 3350 | maltmp, exstat, global_env.iofd, i, global_env.linep)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3351 | |
| 3352 | next(maltmp); /* Basically a PUSHIO */ |
| 3353 | |
| 3354 | DBGPRINTF(("DODOT: returning exstat=%d\n", exstat)); |
| 3355 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3356 | return exstat; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3357 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3358 | } /* while */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3359 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3360 | prs(cp); |
| 3361 | err(": not found"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3362 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3363 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3366 | static int dowait(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3367 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3368 | int i; |
| 3369 | char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3370 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3371 | cp = t->words[1]; |
| 3372 | if (cp != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3373 | i = getn(cp); |
| 3374 | if (i == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3375 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3376 | } else |
| 3377 | i = -1; |
| 3378 | setstatus(waitfor(i, 1)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3379 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3382 | static int doread(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3383 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3384 | char *cp, **wp; |
| 3385 | int nb = 0; |
| 3386 | int nl = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3387 | |
| 3388 | if (t->words[1] == NULL) { |
| 3389 | err("Usage: read name ..."); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3390 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3391 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3392 | for (wp = t->words + 1; *wp; wp++) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3393 | for (cp = global_env.linep; !nl && cp < elinep - 1; cp++) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3394 | nb = read(0, cp, sizeof(*cp)); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3395 | if (nb != sizeof(*cp)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3396 | break; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3397 | nl = (*cp == '\n'); |
| 3398 | if (nl || (wp[1] && any(*cp, ifs->value))) |
| 3399 | break; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3400 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3401 | *cp = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3402 | if (nb <= 0) |
| 3403 | break; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3404 | setval(lookup(*wp), global_env.linep); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3405 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3406 | return nb <= 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3409 | static int doeval(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3410 | { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3411 | return RUN(awordlist, t->words + 1, wdchar); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3414 | static int dotrap(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3415 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3416 | int n, i; |
| 3417 | int resetsig; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3418 | |
| 3419 | if (t->words[1] == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3420 | for (i = 0; i <= _NSIG; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3421 | if (trap[i]) { |
| 3422 | prn(i); |
| 3423 | prs(": "); |
| 3424 | prs(trap[i]); |
| 3425 | prs("\n"); |
| 3426 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3427 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3428 | } |
| 3429 | resetsig = isdigit(*t->words[1]); |
| 3430 | for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) { |
| 3431 | n = getsig(t->words[i]); |
| 3432 | freecell(trap[n]); |
| 3433 | trap[n] = 0; |
| 3434 | if (!resetsig) { |
| 3435 | if (*t->words[1] != '\0') { |
| 3436 | trap[n] = strsave(t->words[1], 0); |
| 3437 | setsig(n, sig); |
| 3438 | } else |
| 3439 | setsig(n, SIG_IGN); |
| 3440 | } else { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 3441 | if (interactive) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3442 | if (n == SIGINT) |
| 3443 | setsig(n, onintr); |
| 3444 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3445 | setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 3446 | } else |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3447 | setsig(n, SIG_DFL); |
| 3448 | } |
| 3449 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3450 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3453 | static int getsig(char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3454 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3455 | int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3456 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3457 | n = getn(s); |
| 3458 | if (n < 0 || n > _NSIG) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3459 | err("trap: bad signal number"); |
| 3460 | n = 0; |
| 3461 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3462 | return n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3465 | static void setsig(int n, sighandler_t f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3466 | { |
| 3467 | if (n == 0) |
| 3468 | return; |
| 3469 | if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) { |
| 3470 | ourtrap[n] = 1; |
| 3471 | signal(n, f); |
| 3472 | } |
| 3473 | } |
| 3474 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3475 | static int getn(char *as) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3476 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3477 | char *s; |
| 3478 | int n, m; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3479 | |
| 3480 | s = as; |
| 3481 | m = 1; |
| 3482 | if (*s == '-') { |
| 3483 | m = -1; |
| 3484 | s++; |
| 3485 | } |
| 3486 | for (n = 0; isdigit(*s); s++) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3487 | n = (n * 10) + (*s - '0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3488 | if (*s) { |
| 3489 | prs(as); |
| 3490 | err(": bad number"); |
| 3491 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3492 | return n * m; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3495 | static int dobreak(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3496 | { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3497 | return brkcontin(t->words[1], 1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3500 | static int docontinue(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3501 | { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3502 | return brkcontin(t->words[1], 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3505 | static int brkcontin(char *cp, int val) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3506 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3507 | struct brkcon *bc; |
| 3508 | int nl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3509 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3510 | nl = cp == NULL ? 1 : getn(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3511 | if (nl <= 0) |
| 3512 | nl = 999; |
| 3513 | do { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3514 | bc = brklist; |
| 3515 | if (bc == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3516 | break; |
| 3517 | brklist = bc->nextlev; |
| 3518 | } while (--nl); |
| 3519 | if (nl) { |
| 3520 | err("bad break/continue level"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3521 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3522 | } |
| 3523 | isbreak = val; |
| 3524 | longjmp(bc->brkpt, 1); |
| 3525 | /* NOTREACHED */ |
| 3526 | } |
| 3527 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3528 | static int doexit(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3529 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3530 | char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3531 | |
| 3532 | execflg = 0; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3533 | cp = t->words[1]; |
| 3534 | if (cp != NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3535 | setstatus(getn(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3536 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3537 | DBGPRINTF(("DOEXIT: calling leave(), t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3538 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3539 | leave(); |
| 3540 | /* NOTREACHED */ |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3541 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3544 | static int doexport(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3545 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3546 | rdexp(t->words + 1, export, EXPORT); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3547 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3550 | static int doreadonly(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3551 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3552 | rdexp(t->words + 1, ronly, RONLY); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3553 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3556 | static void rdexp(char **wp, void (*f) (struct var *), int key) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3557 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3558 | DBGPRINTF6(("RDEXP: enter, wp=%p, func=%p, key=%d\n", wp, f, key)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3559 | DBGPRINTF6(("RDEXP: *wp=%s\n", *wp)); |
| 3560 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3561 | if (*wp != NULL) { |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3562 | for (; *wp != NULL; wp++) { |
| 3563 | if (isassign(*wp)) { |
| 3564 | char *cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3565 | |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3566 | assign(*wp, COPYV); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3567 | for (cp = *wp; *cp != '='; cp++); |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3568 | *cp = '\0'; |
| 3569 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3570 | if (checkname(*wp)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3571 | (*f) (lookup(*wp)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3572 | else |
| 3573 | badid(*wp); |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3574 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3575 | } else |
| 3576 | putvlist(key, 1); |
| 3577 | } |
| 3578 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3579 | static void badid(char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3580 | { |
| 3581 | prs(s); |
| 3582 | err(": bad identifier"); |
| 3583 | } |
| 3584 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3585 | static int doset(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3586 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3587 | struct var *vp; |
| 3588 | char *cp; |
| 3589 | int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3590 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3591 | cp = t->words[1]; |
| 3592 | if (cp == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3593 | for (vp = vlist; vp; vp = vp->next) |
| 3594 | varput(vp->name, 1); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3595 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3596 | } |
| 3597 | if (*cp == '-') { |
| 3598 | /* bad: t->words++; */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3599 | for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3600 | if (*++cp == 0) |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3601 | FLAG['x'] = FLAG['v'] = 0; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3602 | else { |
| 3603 | for (; *cp; cp++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3604 | switch (*cp) { |
| 3605 | case 'e': |
| 3606 | if (!interactive) |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3607 | FLAG['e']++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3608 | break; |
| 3609 | |
| 3610 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3611 | if (*cp >= 'a' && *cp <= 'z') |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3612 | FLAG[(int) *cp]++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3613 | break; |
| 3614 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3615 | } |
| 3616 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3617 | setdash(); |
| 3618 | } |
| 3619 | if (t->words[1]) { |
| 3620 | t->words[0] = dolv[0]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3621 | for (n = 1; t->words[n]; n++) |
| 3622 | setarea((char *) t->words[n], 0); |
| 3623 | dolc = n - 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3624 | dolv = t->words; |
| 3625 | setval(lookup("#"), putn(dolc)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3626 | setarea((char *) (dolv - 1), 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3627 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3628 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3631 | static void varput(char *s, int out) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3632 | { |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3633 | if (isalnum(*s) || *s == '_') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3634 | write(out, s, strlen(s)); |
| 3635 | write(out, "\n", 1); |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | |
| 3640 | /* |
| 3641 | * Copyright (c) 1999 Herbert Xu <herbert@debian.org> |
| 3642 | * This file contains code for the times builtin. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3643 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3644 | static int dotimes(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3645 | { |
| 3646 | struct tms buf; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3647 | long clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3648 | |
| 3649 | times(&buf); |
| 3650 | printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n", |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3651 | (int) (buf.tms_utime / clk_tck / 60), |
| 3652 | ((double) buf.tms_utime) / clk_tck, |
| 3653 | (int) (buf.tms_stime / clk_tck / 60), |
| 3654 | ((double) buf.tms_stime) / clk_tck, |
| 3655 | (int) (buf.tms_cutime / clk_tck / 60), |
| 3656 | ((double) buf.tms_cutime) / clk_tck, |
| 3657 | (int) (buf.tms_cstime / clk_tck / 60), |
| 3658 | ((double) buf.tms_cstime) / clk_tck); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3659 | return 0; |
| 3660 | } |
| 3661 | |
| 3662 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3663 | /* -------- eval.c -------- */ |
| 3664 | |
| 3665 | /* |
| 3666 | * ${} |
| 3667 | * `command` |
| 3668 | * blank interpretation |
| 3669 | * quoting |
| 3670 | * glob |
| 3671 | */ |
| 3672 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3673 | static char **eval(char **ap, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3674 | { |
| 3675 | struct wdblock *wb; |
| 3676 | char **wp; |
| 3677 | char **wf; |
| 3678 | jmp_buf ev; |
| 3679 | |
| 3680 | #if __GNUC__ |
| 3681 | /* Avoid longjmp clobbering */ |
| 3682 | (void) ℘ |
| 3683 | (void) ≈ |
| 3684 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3685 | |
| 3686 | DBGPRINTF4(("EVAL: enter, f=%d\n", f)); |
| 3687 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3688 | wp = NULL; |
| 3689 | wb = NULL; |
| 3690 | wf = NULL; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3691 | errpt = ev; |
| 3692 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3693 | while (*ap && isassign(*ap)) |
| 3694 | expand(*ap++, &wb, f & ~DOGLOB); |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3695 | if (FLAG['k']) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3696 | for (wf = ap; *wf; wf++) { |
| 3697 | if (isassign(*wf)) |
| 3698 | expand(*wf, &wb, f & ~DOGLOB); |
| 3699 | } |
| 3700 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3701 | for (wb = addword((char *) 0, wb); *ap; ap++) { |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3702 | if (!FLAG['k'] || !isassign(*ap)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3703 | expand(*ap, &wb, f & ~DOKEY); |
| 3704 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3705 | wb = addword((char *) 0, wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3706 | wp = getwords(wb); |
| 3707 | quitenv(); |
| 3708 | } else |
| 3709 | gflg = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3710 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3711 | return gflg ? (char **) NULL : wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3714 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3715 | /* |
| 3716 | * Make the exported environment from the exported |
| 3717 | * names in the dictionary. Keyword assignments |
| 3718 | * will already have been done. |
| 3719 | */ |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3720 | static char **makenv(int all, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3721 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3722 | struct var *vp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3723 | |
| 3724 | DBGPRINTF5(("MAKENV: enter, all=%d\n", all)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3725 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3726 | for (vp = vlist; vp; vp = vp->next) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3727 | if (all || vp->status & EXPORT) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3728 | wb = addword(vp->name, wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3729 | wb = addword((char *) 0, wb); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3730 | return getwords(wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3733 | static int expand(const char *cp, struct wdblock **wbp, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3734 | { |
| 3735 | jmp_buf ev; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3736 | char *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3737 | |
| 3738 | #if __GNUC__ |
| 3739 | /* Avoid longjmp clobbering */ |
| 3740 | (void) &cp; |
| 3741 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3742 | |
| 3743 | DBGPRINTF3(("EXPAND: enter, f=%d\n", f)); |
| 3744 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3745 | gflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3746 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3747 | if (cp == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3748 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3749 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3750 | if (!anys("$`'\"", cp) && !anys(ifs->value, cp) |
| 3751 | && ((f & DOGLOB) == 0 || !anys("[*?", cp)) |
| 3752 | ) { |
| 3753 | xp = strsave(cp, areanum); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3754 | if (f & DOTRIM) |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3755 | unquote(xp); |
| 3756 | *wbp = addword(xp, *wbp); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3757 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3758 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3759 | errpt = ev; |
| 3760 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3761 | PUSHIO(aword, cp, strchar); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3762 | global_env.iobase = global_env.iop; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3763 | while ((xp = blank(f)) && gflg == 0) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3764 | global_env.linep = xp; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3765 | xp = strsave(xp, areanum); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3766 | if ((f & DOGLOB) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3767 | if (f & DOTRIM) |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3768 | unquote(xp); |
| 3769 | *wbp = addword(xp, *wbp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3770 | } else |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3771 | *wbp = glob(xp, *wbp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3772 | } |
| 3773 | quitenv(); |
| 3774 | } else |
| 3775 | gflg = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3776 | return gflg == 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3779 | static char *evalstr(char *cp, int f) |
| 3780 | { |
| 3781 | struct wdblock *wb; |
| 3782 | |
| 3783 | DBGPRINTF6(("EVALSTR: enter, cp=%p, f=%d\n", cp, f)); |
| 3784 | |
| 3785 | wb = NULL; |
| 3786 | if (expand(cp, &wb, f)) { |
| 3787 | if (wb == NULL || wb->w_nword == 0 |
| 3788 | || (cp = wb->w_words[0]) == NULL |
| 3789 | ) { |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 3790 | // TODO: I suspect that |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3791 | // char *evalstr(char *cp, int f) is actually |
| 3792 | // const char *evalstr(const char *cp, int f)! |
| 3793 | cp = (char*)""; |
| 3794 | } |
| 3795 | DELETE(wb); |
| 3796 | } else |
| 3797 | cp = NULL; |
| 3798 | return cp; |
| 3799 | } |
| 3800 | |
| 3801 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3802 | /* |
| 3803 | * Blank interpretation and quoting |
| 3804 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3805 | static char *blank(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3806 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3807 | int c, c1; |
| 3808 | char *sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3809 | int scanequals, foundequals; |
| 3810 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3811 | DBGPRINTF3(("BLANK: enter, f=%d\n", f)); |
| 3812 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3813 | sp = global_env.linep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3814 | scanequals = f & DOKEY; |
| 3815 | foundequals = 0; |
| 3816 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3817 | loop: |
| 3818 | c = subgetc('"', foundequals); |
| 3819 | switch (c) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3820 | case 0: |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3821 | if (sp == global_env.linep) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3822 | return 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3823 | *global_env.linep++ = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3824 | return sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3825 | |
| 3826 | default: |
| 3827 | if (f & DOBLANK && any(c, ifs->value)) |
| 3828 | goto loop; |
| 3829 | break; |
| 3830 | |
| 3831 | case '"': |
| 3832 | case '\'': |
| 3833 | scanequals = 0; |
| 3834 | if (INSUB()) |
| 3835 | break; |
| 3836 | for (c1 = c; (c = subgetc(c1, 1)) != c1;) { |
| 3837 | if (c == 0) |
| 3838 | break; |
| 3839 | if (c == '\'' || !any(c, "$`\"")) |
| 3840 | c |= QUOTE; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3841 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3842 | } |
| 3843 | c = 0; |
| 3844 | } |
| 3845 | unget(c); |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3846 | if (!isalpha(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3847 | scanequals = 0; |
| 3848 | for (;;) { |
| 3849 | c = subgetc('"', foundequals); |
| 3850 | if (c == 0 || |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3851 | f & (DOBLANK && any(c, ifs->value)) || |
| 3852 | (!INSUB() && any(c, "\"'"))) { |
| 3853 | scanequals = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3854 | unget(c); |
| 3855 | if (any(c, "\"'")) |
| 3856 | goto loop; |
| 3857 | break; |
| 3858 | } |
| 3859 | if (scanequals) { |
| 3860 | if (c == '=') { |
| 3861 | foundequals = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3862 | scanequals = 0; |
| 3863 | } else if (!isalnum(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3864 | scanequals = 0; |
| 3865 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3866 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3867 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3868 | *global_env.linep++ = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3869 | return sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | /* |
| 3873 | * Get characters, substituting for ` and $ |
| 3874 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3875 | static int subgetc(char ec, int quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3876 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3877 | char c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3878 | |
| 3879 | DBGPRINTF3(("SUBGETC: enter, quoted=%d\n", quoted)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3880 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3881 | again: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3882 | c = my_getc(ec); |
| 3883 | if (!INSUB() && ec != '\'') { |
| 3884 | if (c == '`') { |
| 3885 | if (grave(quoted) == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3886 | return 0; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3887 | global_env.iop->task = XGRAVE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3888 | goto again; |
| 3889 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3890 | if (c == '$') { |
| 3891 | c = dollar(quoted); |
| 3892 | if (c == 0) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3893 | global_env.iop->task = XDOLL; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3894 | goto again; |
| 3895 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3896 | } |
| 3897 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3898 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | /* |
| 3902 | * Prepare to generate the string returned by ${} substitution. |
| 3903 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3904 | static int dollar(int quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3905 | { |
| 3906 | int otask; |
| 3907 | struct io *oiop; |
| 3908 | char *dolp; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3909 | char *s, c, *cp = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3910 | struct var *vp; |
| 3911 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3912 | DBGPRINTF3(("DOLLAR: enter, quoted=%d\n", quoted)); |
| 3913 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3914 | c = readc(); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3915 | s = global_env.linep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3916 | if (c != '{') { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3917 | *global_env.linep++ = c; |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3918 | if (isalpha(c) || c == '_') { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3919 | while ((c = readc()) != 0 && (isalnum(c) || c == '_')) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3920 | if (global_env.linep < elinep) |
| 3921 | *global_env.linep++ = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3922 | unget(c); |
| 3923 | } |
| 3924 | c = 0; |
| 3925 | } else { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3926 | oiop = global_env.iop; |
| 3927 | otask = global_env.iop->task; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3928 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3929 | global_env.iop->task = XOTHER; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3930 | while ((c = subgetc('"', 0)) != 0 && c != '}' && c != '\n') |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3931 | if (global_env.linep < elinep) |
| 3932 | *global_env.linep++ = c; |
| 3933 | if (oiop == global_env.iop) |
| 3934 | global_env.iop->task = otask; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3935 | if (c != '}') { |
| 3936 | err("unclosed ${"); |
| 3937 | gflg++; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3938 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3939 | } |
| 3940 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3941 | if (global_env.linep >= elinep) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3942 | err("string in ${} too long"); |
| 3943 | gflg++; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3944 | global_env.linep -= 10; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3945 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3946 | *global_env.linep = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3947 | if (*s) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3948 | for (cp = s + 1; *cp; cp++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3949 | if (any(*cp, "=-+?")) { |
| 3950 | c = *cp; |
| 3951 | *cp++ = 0; |
| 3952 | break; |
| 3953 | } |
| 3954 | if (s[1] == 0 && (*s == '*' || *s == '@')) { |
| 3955 | if (dolc > 1) { |
| 3956 | /* currently this does not distinguish $* and $@ */ |
| 3957 | /* should check dollar */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 3958 | global_env.linep = s; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3959 | PUSHIO(awordlist, dolv + 1, dolchar); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3960 | return 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3961 | } else { /* trap the nasty ${=} */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3962 | s[0] = '1'; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3963 | s[1] = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3964 | } |
| 3965 | } |
| 3966 | vp = lookup(s); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3967 | dolp = vp->value; |
| 3968 | if (dolp == null) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3969 | switch (c) { |
| 3970 | case '=': |
| 3971 | if (isdigit(*s)) { |
| 3972 | err("cannot use ${...=...} with $n"); |
| 3973 | gflg++; |
| 3974 | break; |
| 3975 | } |
| 3976 | setval(vp, cp); |
| 3977 | dolp = vp->value; |
| 3978 | break; |
| 3979 | |
| 3980 | case '-': |
| 3981 | dolp = strsave(cp, areanum); |
| 3982 | break; |
| 3983 | |
| 3984 | case '?': |
| 3985 | if (*cp == 0) { |
| 3986 | prs("missing value for "); |
| 3987 | err(s); |
| 3988 | } else |
| 3989 | err(cp); |
| 3990 | gflg++; |
| 3991 | break; |
| 3992 | } |
| 3993 | } else if (c == '+') |
| 3994 | dolp = strsave(cp, areanum); |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3995 | if (FLAG['u'] && dolp == null) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3996 | prs("unset variable: "); |
| 3997 | err(s); |
| 3998 | gflg++; |
| 3999 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4000 | global_env.linep = s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4001 | PUSHIO(aword, dolp, quoted ? qstrchar : strchar); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4002 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | /* |
| 4006 | * Run the command in `...` and read its output. |
| 4007 | */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4008 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4009 | static int grave(int quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4010 | { |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 4011 | /* moved to G: static char child_cmd[LINELIM]; */ |
| 4012 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4013 | const char *cp; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4014 | int i; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4015 | int j; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4016 | int pf[2]; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4017 | const char *src; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4018 | char *dest; |
| 4019 | int count; |
| 4020 | int ignore; |
| 4021 | int ignore_once; |
| 4022 | char *argument_list[4]; |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4023 | struct wdblock *wb = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4024 | |
| 4025 | #if __GNUC__ |
| 4026 | /* Avoid longjmp clobbering */ |
| 4027 | (void) &cp; |
| 4028 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4029 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4030 | for (cp = global_env.iop->argp->aword; *cp != '`'; cp++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4031 | if (*cp == 0) { |
| 4032 | err("no closing `"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4033 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4034 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4035 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4036 | |
| 4037 | /* string copy with dollar expansion */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4038 | src = global_env.iop->argp->aword; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4039 | dest = child_cmd; |
| 4040 | count = 0; |
| 4041 | ignore = 0; |
| 4042 | ignore_once = 0; |
| 4043 | while ((*src != '`') && (count < LINELIM)) { |
| 4044 | if (*src == '\'') |
| 4045 | ignore = !ignore; |
| 4046 | if (*src == '\\') |
| 4047 | ignore_once = 1; |
| 4048 | if (*src == '$' && !ignore && !ignore_once) { |
| 4049 | struct var *vp; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 4050 | /* moved to G to reduce stack usage |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4051 | char var_name[LINELIM]; |
| 4052 | char alt_value[LINELIM]; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 4053 | */ |
| 4054 | #define var_name (G.grave__var_name) |
| 4055 | #define alt_value (G.grave__alt_value) |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4056 | int var_index = 0; |
| 4057 | int alt_index = 0; |
| 4058 | char operator = 0; |
| 4059 | int braces = 0; |
| 4060 | char *value; |
| 4061 | |
| 4062 | src++; |
| 4063 | if (*src == '{') { |
| 4064 | braces = 1; |
| 4065 | src++; |
| 4066 | } |
| 4067 | |
| 4068 | var_name[var_index++] = *src++; |
Paul Fox | 54690dc | 2005-07-20 18:33:12 +0000 | [diff] [blame] | 4069 | while (isalnum(*src) || *src=='_') |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4070 | var_name[var_index++] = *src++; |
| 4071 | var_name[var_index] = 0; |
| 4072 | |
| 4073 | if (braces) { |
| 4074 | switch (*src) { |
| 4075 | case '}': |
| 4076 | break; |
| 4077 | case '-': |
| 4078 | case '=': |
| 4079 | case '+': |
| 4080 | case '?': |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4081 | operator = * src; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4082 | break; |
| 4083 | default: |
| 4084 | err("unclosed ${\n"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4085 | return 0; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4086 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4087 | if (operator) { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4088 | src++; |
| 4089 | while (*src && (*src != '}')) { |
| 4090 | alt_value[alt_index++] = *src++; |
| 4091 | } |
| 4092 | alt_value[alt_index] = 0; |
| 4093 | if (*src != '}') { |
| 4094 | err("unclosed ${\n"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4095 | return 0; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4096 | } |
| 4097 | } |
| 4098 | src++; |
| 4099 | } |
| 4100 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4101 | if (isalpha(*var_name)) { |
| 4102 | /* let subshell handle it instead */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4103 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4104 | char *namep = var_name; |
| 4105 | |
| 4106 | *dest++ = '$'; |
| 4107 | if (braces) |
| 4108 | *dest++ = '{'; |
| 4109 | while (*namep) |
| 4110 | *dest++ = *namep++; |
| 4111 | if (operator) { |
| 4112 | char *altp = alt_value; |
| 4113 | *dest++ = operator; |
| 4114 | while (*altp) |
| 4115 | *dest++ = *altp++; |
| 4116 | } |
| 4117 | if (braces) |
| 4118 | *dest++ = '}'; |
| 4119 | |
| 4120 | wb = addword(lookup(var_name)->name, wb); |
| 4121 | } else { |
| 4122 | /* expand */ |
| 4123 | |
| 4124 | vp = lookup(var_name); |
| 4125 | if (vp->value != null) |
| 4126 | value = (operator == '+') ? |
| 4127 | alt_value : vp->value; |
| 4128 | else if (operator == '?') { |
| 4129 | err(alt_value); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4130 | return 0; |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4131 | } else if (alt_index && (operator != '+')) { |
| 4132 | value = alt_value; |
| 4133 | if (operator == '=') |
| 4134 | setval(vp, value); |
| 4135 | } else |
| 4136 | continue; |
| 4137 | |
| 4138 | while (*value && (count < LINELIM)) { |
| 4139 | *dest++ = *value++; |
| 4140 | count++; |
| 4141 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4142 | } |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 4143 | #undef var_name |
| 4144 | #undef alt_value |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4145 | } else { |
| 4146 | *dest++ = *src++; |
| 4147 | count++; |
| 4148 | ignore_once = 0; |
| 4149 | } |
| 4150 | } |
| 4151 | *dest = '\0'; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4152 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4153 | if (openpipe(pf) < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4154 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4155 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4156 | while ((i = vfork()) == -1 && errno == EAGAIN); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4157 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4158 | DBGPRINTF3(("GRAVE: i is %p\n", io)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4159 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4160 | if (i < 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4161 | closepipe(pf); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4162 | err((char *) bb_msg_memory_exhausted); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4163 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4164 | } |
| 4165 | if (i != 0) { |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 4166 | waitpid(i, NULL, 0); // safe_waitpid? |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4167 | global_env.iop->argp->aword = ++cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4168 | close(pf[1]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4169 | PUSHIO(afile, remap(pf[0]), |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4170 | (int (*)(struct ioarg *)) ((quoted) ? qgravechar : gravechar)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4171 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4172 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4173 | /* allow trapped signals */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4174 | /* XXX - Maybe this signal stuff should go as well? */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4175 | for (j = 0; j <= _NSIG; j++) |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4176 | if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN) |
| 4177 | signal(j, SIG_DFL); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4178 | |
Denis Vlasenko | 2b54aaa | 2007-05-09 22:16:08 +0000 | [diff] [blame] | 4179 | /* Testcase where below checks are needed: |
| 4180 | * close stdout & run this script: |
| 4181 | * files=`ls` |
| 4182 | * echo "$files" >zz |
| 4183 | */ |
| 4184 | xmove_fd(pf[1], 1); |
Denis Vlasenko | 847fa77 | 2008-01-28 22:45:43 +0000 | [diff] [blame] | 4185 | if (pf[0] != 1) |
| 4186 | close(pf[0]); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4187 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4188 | argument_list[0] = (char *) DEFAULT_SHELL; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4189 | argument_list[1] = (char *) "-c"; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4190 | argument_list[2] = child_cmd; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4191 | argument_list[3] = NULL; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4192 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4193 | cp = rexecve(argument_list[0], argument_list, makenv(1, wb)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4194 | prs(argument_list[0]); |
| 4195 | prs(": "); |
| 4196 | err(cp); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4197 | _exit(1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4200 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4201 | static char *unquote(char *as) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4202 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4203 | char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4204 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4205 | s = as; |
| 4206 | if (s != NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4207 | while (*s) |
| 4208 | *s++ &= ~QUOTE; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4209 | return as; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4210 | } |
| 4211 | |
| 4212 | /* -------- glob.c -------- */ |
| 4213 | |
| 4214 | /* |
| 4215 | * glob |
| 4216 | */ |
| 4217 | |
| 4218 | #define scopy(x) strsave((x), areanum) |
| 4219 | #define BLKSIZ 512 |
| 4220 | #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent)) |
| 4221 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4222 | static struct wdblock *cl, *nl; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 4223 | static const char spcl[] ALIGN1= "[?*"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4224 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4225 | static struct wdblock *glob(char *cp, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4226 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4227 | int i; |
| 4228 | char *pp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4229 | |
| 4230 | if (cp == 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4231 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4232 | i = 0; |
| 4233 | for (pp = cp; *pp; pp++) |
| 4234 | if (any(*pp, spcl)) |
| 4235 | i++; |
| 4236 | else if (!any(*pp & ~QUOTE, spcl)) |
| 4237 | *pp &= ~QUOTE; |
| 4238 | if (i != 0) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4239 | for (cl = addword(scopy(cp), NULL); anyspcl(cl); cl = nl) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4240 | nl = newword(cl->w_nword * 2); |
| 4241 | for (i = 0; i < cl->w_nword; i++) { /* for each argument */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4242 | for (pp = cl->w_words[i]; *pp; pp++) |
| 4243 | if (any(*pp, spcl)) { |
| 4244 | globname(cl->w_words[i], pp); |
| 4245 | break; |
| 4246 | } |
| 4247 | if (*pp == '\0') |
| 4248 | nl = addword(scopy(cl->w_words[i]), nl); |
| 4249 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4250 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4251 | DELETE(cl->w_words[i]); |
| 4252 | DELETE(cl); |
| 4253 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4254 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4255 | unquote(cl->w_words[i]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4256 | glob0((char *) cl->w_words, cl->w_nword, sizeof(char *), xstrcmp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4257 | if (cl->w_nword) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4258 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4259 | wb = addword(cl->w_words[i], wb); |
| 4260 | DELETE(cl); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4261 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4262 | } |
| 4263 | } |
| 4264 | wb = addword(unquote(cp), wb); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4265 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4268 | static void globname(char *we, char *pp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4269 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4270 | char *np, *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4271 | char *name, *gp, *dp; |
| 4272 | int k; |
| 4273 | DIR *dirp; |
| 4274 | struct dirent *de; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4275 | char dname[NAME_MAX + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4276 | struct stat dbuf; |
| 4277 | |
| 4278 | for (np = we; np != pp; pp--) |
| 4279 | if (pp[-1] == '/') |
| 4280 | break; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4281 | for (dp = cp = space((int) (pp - np) + 3); np < pp;) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4282 | *cp++ = *np++; |
| 4283 | *cp++ = '.'; |
| 4284 | *cp = '\0'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4285 | for (gp = cp = space(strlen(pp) + 1); *np && *np != '/';) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4286 | *cp++ = *np++; |
| 4287 | *cp = '\0'; |
| 4288 | dirp = opendir(dp); |
| 4289 | if (dirp == 0) { |
| 4290 | DELETE(dp); |
| 4291 | DELETE(gp); |
| 4292 | return; |
| 4293 | } |
| 4294 | dname[NAME_MAX] = '\0'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4295 | while ((de = readdir(dirp)) != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4296 | /* XXX Hmmm... What this could be? (abial) */ |
| 4297 | /* |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4298 | if (ent[j].d_ino == 0) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4299 | continue; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4300 | */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4301 | strncpy(dname, de->d_name, NAME_MAX); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4302 | if (dname[0] == '.') |
| 4303 | if (*gp != '.') |
| 4304 | continue; |
| 4305 | for (k = 0; k < NAME_MAX; k++) |
| 4306 | if (any(dname[k], spcl)) |
| 4307 | dname[k] |= QUOTE; |
| 4308 | if (gmatch(dname, gp)) { |
| 4309 | name = generate(we, pp, dname, np); |
| 4310 | if (*np && !anys(np, spcl)) { |
| 4311 | if (stat(name, &dbuf)) { |
| 4312 | DELETE(name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4313 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4314 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4315 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4316 | nl = addword(name, nl); |
| 4317 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4318 | } |
| 4319 | closedir(dirp); |
| 4320 | DELETE(dp); |
| 4321 | DELETE(gp); |
| 4322 | } |
| 4323 | |
| 4324 | /* |
| 4325 | * generate a pathname as below. |
| 4326 | * start..end1 / middle end |
| 4327 | * the slashes come for free |
| 4328 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4329 | static char *generate(char *start1, char *end1, char *middle, char *end) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4330 | { |
| 4331 | char *p; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4332 | char *op, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4333 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4334 | p = op = space((int)(end1 - start1) + strlen(middle) + strlen(end) + 2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4335 | for (xp = start1; xp != end1;) |
| 4336 | *op++ = *xp++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4337 | for (xp = middle; (*op++ = *xp++) != '\0';); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4338 | op--; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4339 | for (xp = end; (*op++ = *xp++) != '\0';); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4340 | return p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4341 | } |
| 4342 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4343 | static int anyspcl(struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4344 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4345 | int i; |
| 4346 | char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4347 | |
| 4348 | wd = wb->w_words; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4349 | for (i = 0; i < wb->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4350 | if (anys(spcl, *wd++)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4351 | return 1; |
| 4352 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4353 | } |
| 4354 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4355 | static int xstrcmp(char *p1, char *p2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4356 | { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4357 | return strcmp(*(char **) p1, *(char **) p2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4358 | } |
| 4359 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4360 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4361 | /* -------- word.c -------- */ |
| 4362 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4363 | static struct wdblock *newword(int nw) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4364 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4365 | struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4366 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4367 | wb = (struct wdblock *) space(sizeof(*wb) + nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4368 | wb->w_bsize = nw; |
| 4369 | wb->w_nword = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4370 | return wb; |
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 struct wdblock *addword(char *wd, 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 | struct wdblock *wb2; |
| 4376 | int nw; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4377 | |
| 4378 | if (wb == NULL) |
| 4379 | wb = newword(NSTART); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4380 | nw = wb->w_nword; |
| 4381 | if (nw >= wb->w_bsize) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4382 | wb2 = newword(nw * 2); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4383 | memcpy((char *) wb2->w_words, (char *) wb->w_words, |
| 4384 | nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4385 | wb2->w_nword = nw; |
| 4386 | DELETE(wb); |
| 4387 | wb = wb2; |
| 4388 | } |
| 4389 | wb->w_words[wb->w_nword++] = wd; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4390 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4391 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4392 | |
Denis Vlasenko | e471275 | 2007-04-14 15:08:41 +0000 | [diff] [blame] | 4393 | static char **getwords(struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4394 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4395 | char **wd; |
| 4396 | int nb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4397 | |
| 4398 | if (wb == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4399 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4400 | if (wb->w_nword == 0) { |
| 4401 | DELETE(wb); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4402 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4403 | } |
| 4404 | wd = (char **) space(nb = sizeof(*wd) * wb->w_nword); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4405 | memcpy((char *) wd, (char *) wb->w_words, nb); |
| 4406 | DELETE(wb); /* perhaps should done by caller */ |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4407 | return wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4408 | } |
| 4409 | |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 4410 | static int (*func) (char *, char *); |
| 4411 | static int globv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4412 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4413 | static void glob3(char *i, char *j, char *k) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4414 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4415 | char *index1, *index2, *index3; |
| 4416 | int c; |
| 4417 | int m; |
| 4418 | |
| 4419 | m = globv; |
| 4420 | index1 = i; |
| 4421 | index2 = j; |
| 4422 | index3 = k; |
| 4423 | do { |
| 4424 | c = *index1; |
| 4425 | *index1++ = *index3; |
| 4426 | *index3++ = *index2; |
| 4427 | *index2++ = c; |
| 4428 | } while (--m); |
| 4429 | } |
| 4430 | |
| 4431 | static void glob2(char *i, char *j) |
| 4432 | { |
| 4433 | char *index1, *index2, c; |
| 4434 | int m; |
| 4435 | |
| 4436 | m = globv; |
| 4437 | index1 = i; |
| 4438 | index2 = j; |
| 4439 | do { |
| 4440 | c = *index1; |
| 4441 | *index1++ = *index2; |
| 4442 | *index2++ = c; |
| 4443 | } while (--m); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4444 | } |
| 4445 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4446 | static void glob1(char *base, char *lim) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4447 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4448 | char *i, *j; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4449 | int v2; |
| 4450 | char *lptr, *hptr; |
| 4451 | int c; |
| 4452 | unsigned n; |
| 4453 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4454 | v2 = globv; |
| 4455 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4456 | top: |
| 4457 | n = (int) (lim - base); |
| 4458 | if (n <= v2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4459 | return; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4460 | n = v2 * (n / (2 * v2)); |
| 4461 | hptr = lptr = base + n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4462 | i = base; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4463 | j = lim - v2; |
| 4464 | for (;;) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4465 | if (i < lptr) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4466 | c = (*func) (i, lptr); |
| 4467 | if (c == 0) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4468 | lptr -= v2; |
| 4469 | glob2(i, lptr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4470 | continue; |
| 4471 | } |
| 4472 | if (c < 0) { |
| 4473 | i += v2; |
| 4474 | continue; |
| 4475 | } |
| 4476 | } |
| 4477 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4478 | begin: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4479 | if (j > hptr) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4480 | c = (*func) (hptr, j); |
| 4481 | if (c == 0) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4482 | hptr += v2; |
| 4483 | glob2(hptr, j); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4484 | goto begin; |
| 4485 | } |
| 4486 | if (c > 0) { |
| 4487 | if (i == lptr) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4488 | hptr += v2; |
| 4489 | glob3(i, hptr, j); |
| 4490 | i = (lptr += v2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4491 | goto begin; |
| 4492 | } |
| 4493 | glob2(i, j); |
| 4494 | j -= v2; |
| 4495 | i += v2; |
| 4496 | continue; |
| 4497 | } |
| 4498 | j -= v2; |
| 4499 | goto begin; |
| 4500 | } |
| 4501 | |
| 4502 | |
| 4503 | if (i == lptr) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4504 | if (lptr - base >= lim - hptr) { |
| 4505 | glob1(hptr + v2, lim); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4506 | lim = lptr; |
| 4507 | } else { |
| 4508 | glob1(base, lptr); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4509 | base = hptr + v2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4510 | } |
| 4511 | goto top; |
| 4512 | } |
| 4513 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4514 | lptr -= v2; |
| 4515 | glob3(j, lptr, i); |
| 4516 | j = (hptr -= v2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4517 | } |
| 4518 | } |
| 4519 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4520 | static void glob0(char *a0, unsigned a1, int a2, int (*a3) (char *, char *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4521 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4522 | func = a3; |
| 4523 | globv = a2; |
| 4524 | glob1(a0, a0 + a1 * a2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4527 | |
| 4528 | /* -------- io.c -------- */ |
| 4529 | |
| 4530 | /* |
| 4531 | * shell IO |
| 4532 | */ |
| 4533 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4534 | static int my_getc(int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4535 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4536 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4537 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4538 | if (global_env.linep > elinep) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4539 | while ((c = readc()) != '\n' && c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4540 | err("input line too long"); |
| 4541 | gflg++; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4542 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4543 | } |
| 4544 | c = readc(); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4545 | if ((ec != '\'') && (ec != '`') && (global_env.iop->task != XGRAVE)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4546 | if (c == '\\') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4547 | c = readc(); |
| 4548 | if (c == '\n' && ec != '\"') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4549 | return my_getc(ec); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4550 | c |= QUOTE; |
| 4551 | } |
| 4552 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4553 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4554 | } |
| 4555 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4556 | static void unget(int c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4557 | { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4558 | if (global_env.iop >= global_env.iobase) |
| 4559 | global_env.iop->peekc = c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4562 | static int eofc(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4563 | { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4564 | 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] | 4565 | } |
| 4566 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4567 | static int readc(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4568 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4569 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4570 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4571 | 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] | 4572 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4573 | for (; global_env.iop >= global_env.iobase; global_env.iop--) { |
| 4574 | RCPRINTF(("READC: global_env.iop %p, peekc 0x%x\n", global_env.iop, global_env.iop->peekc)); |
| 4575 | c = global_env.iop->peekc; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4576 | if (c != '\0') { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4577 | global_env.iop->peekc = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4578 | return c; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4579 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4580 | if (global_env.iop->prev != 0) { |
| 4581 | c = (*global_env.iop->iofn)(global_env.iop->argp, global_env.iop); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4582 | if (c != '\0') { |
| 4583 | if (c == -1) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4584 | global_env.iop++; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4585 | continue; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4586 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4587 | if (global_env.iop == iostack) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4588 | ioecho(c); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4589 | global_env.iop->prev = c; |
| 4590 | return global_env.iop->prev; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4591 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4592 | if (global_env.iop->task == XIO && global_env.iop->prev != '\n') { |
| 4593 | global_env.iop->prev = 0; |
| 4594 | if (global_env.iop == iostack) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4595 | ioecho('\n'); |
| 4596 | return '\n'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4597 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4598 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4599 | if (global_env.iop->task == XIO) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4600 | if (multiline) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4601 | global_env.iop->prev = 0; |
| 4602 | return global_env.iop->prev; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4603 | } |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4604 | if (interactive && global_env.iop == iostack + 1) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4605 | #if ENABLE_FEATURE_EDITING |
| 4606 | current_prompt = prompt->value; |
| 4607 | #else |
| 4608 | prs(prompt->value); |
| 4609 | #endif |
| 4610 | } |
| 4611 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4612 | } /* FOR */ |
| 4613 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4614 | if (global_env.iop >= iostack) { |
| 4615 | RCPRINTF(("READC: return 0, global_env.iop %p\n", global_env.iop)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4616 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | DBGPRINTF(("READC: leave()...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4620 | leave(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4621 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4622 | /* NOTREACHED */ |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4623 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4626 | static void ioecho(char c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4627 | { |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 4628 | if (FLAG['v']) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4629 | write(2, &c, sizeof c); |
| 4630 | } |
| 4631 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4632 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4633 | static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4634 | { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4635 | DBGPRINTF(("PUSHIO: argp %p, argp->afid 0x%x, global_env.iop %p\n", argp, |
| 4636 | argp->afid, global_env.iop)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4637 | |
| 4638 | /* 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] | 4639 | if (++global_env.iop >= &iostack[NPUSH]) { |
| 4640 | global_env.iop--; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4641 | err("Shell input nested too deeply"); |
| 4642 | gflg++; |
| 4643 | return; |
| 4644 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4645 | |
| 4646 | /* We did not overflow the NPUSH array spots so setup data structs */ |
| 4647 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4648 | 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] | 4649 | |
| 4650 | if (argp->afid != AFID_NOBUF) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4651 | global_env.iop->argp = argp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4652 | else { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4653 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4654 | global_env.iop->argp = ioargstack + (global_env.iop - iostack); /* MAL - index into stack */ |
| 4655 | *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] | 4656 | |
| 4657 | /* MAL - mainbuf is for 1st data source (command line?) and all nested use a single shared buffer? */ |
| 4658 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4659 | if (global_env.iop == &iostack[0]) |
| 4660 | global_env.iop->argp->afbuf = &mainbuf; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4661 | else |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4662 | global_env.iop->argp->afbuf = &sharedbuf; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4663 | |
| 4664 | /* MAL - if not a termimal AND (commandline OR readable file) then give it a buffer id? */ |
| 4665 | /* This line appears to be active when running scripts from command line */ |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4666 | if ((isatty(global_env.iop->argp->afile) == 0) |
| 4667 | && (global_env.iop == &iostack[0] |
| 4668 | || lseek(global_env.iop->argp->afile, 0L, SEEK_CUR) != -1)) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4669 | if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */ |
| 4670 | bufid = AFID_ID; /* AFID_ID = 0 */ |
| 4671 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4672 | global_env.iop->argp->afid = bufid; /* assign buffer id */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4673 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4674 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4675 | DBGPRINTF(("PUSHIO: iostack %p, global_env.iop %p, afbuf %p\n", |
| 4676 | iostack, global_env.iop, global_env.iop->argp->afbuf)); |
| 4677 | DBGPRINTF(("PUSHIO: mbuf %p, sbuf %p, bid %d, global_env.iop %p\n", |
| 4678 | &mainbuf, &sharedbuf, bufid, global_env.iop)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4679 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4682 | global_env.iop->prev = ~'\n'; |
| 4683 | global_env.iop->peekc = 0; |
| 4684 | global_env.iop->xchar = 0; |
| 4685 | global_env.iop->nlcount = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4686 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4687 | if (fn == filechar || fn == linechar) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4688 | global_env.iop->task = XIO; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4689 | else if (fn == (int (*)(struct ioarg *)) gravechar |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4690 | || fn == (int (*)(struct ioarg *)) qgravechar) |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4691 | global_env.iop->task = XGRAVE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4692 | else |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4693 | global_env.iop->task = XOTHER; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4696 | static struct io *setbase(struct io *ip) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4697 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4698 | struct io *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4699 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4700 | xp = global_env.iobase; |
| 4701 | global_env.iobase = ip; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4702 | return xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | /* |
| 4706 | * Input generating functions |
| 4707 | */ |
| 4708 | |
| 4709 | /* |
| 4710 | * Produce the characters of a string, then a newline, then EOF. |
| 4711 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4712 | static int nlchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4713 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4714 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4715 | |
| 4716 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4717 | return 0; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4718 | c = *ap->aword++; |
| 4719 | if (c == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4720 | ap->aword = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4721 | return '\n'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4722 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4723 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | /* |
| 4727 | * Given a list of words, produce the characters |
| 4728 | * in them, with a space after each word. |
| 4729 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4730 | static int wdchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4731 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4732 | char c; |
| 4733 | char **wl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4734 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4735 | wl = ap->awordlist; |
| 4736 | if (wl == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4737 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4738 | if (*wl != NULL) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4739 | c = *(*wl)++; |
| 4740 | if (c != 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4741 | return c & 0177; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4742 | ap->awordlist++; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4743 | return ' '; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4744 | } |
| 4745 | ap->awordlist = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4746 | return '\n'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | /* |
| 4750 | * Return the characters of a list of words, |
| 4751 | * producing a space between them. |
| 4752 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4753 | static int dolchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4754 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4755 | char *wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4756 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4757 | wp = *ap->awordlist++; |
| 4758 | if (wp != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4759 | PUSHIO(aword, wp, *ap->awordlist == NULL ? strchar : xxchar); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4760 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4761 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4762 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4765 | static int xxchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4766 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4767 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4768 | |
| 4769 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4770 | return 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4771 | c = *ap->aword++; |
| 4772 | if (c == '\0') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4773 | ap->aword = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4774 | return ' '; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4775 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4776 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
| 4779 | /* |
| 4780 | * Produce the characters from a single word (string). |
| 4781 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4782 | static int strchar(struct ioarg *ap) |
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 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4785 | return 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4786 | return *ap->aword++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
| 4789 | /* |
| 4790 | * Produce quoted characters from a single word (string). |
| 4791 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4792 | static int qstrchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4793 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4794 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4795 | |
Denis Vlasenko | b2abef3 | 2007-01-01 18:18:04 +0000 | [diff] [blame] | 4796 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4797 | return 0; |
Denis Vlasenko | b2abef3 | 2007-01-01 18:18:04 +0000 | [diff] [blame] | 4798 | c = *ap->aword++; |
| 4799 | if (c) |
| 4800 | c |= QUOTE; |
| 4801 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4802 | } |
| 4803 | |
| 4804 | /* |
| 4805 | * Return the characters from a file. |
| 4806 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4807 | static int filechar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4808 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4809 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4810 | char c; |
| 4811 | struct iobuf *bp = ap->afbuf; |
| 4812 | |
| 4813 | if (ap->afid != AFID_NOBUF) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4814 | i = (ap->afid != bp->id); |
| 4815 | if (i || bp->bufp == bp->ebufp) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4816 | if (i) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 4817 | lseek(ap->afile, ap->afpos, SEEK_SET); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4818 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4819 | i = safe_read(ap->afile, bp->buf, sizeof(bp->buf)); |
| 4820 | if (i <= 0) { |
| 4821 | closef(ap->afile); |
| 4822 | return 0; |
| 4823 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4824 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4825 | bp->id = ap->afid; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4826 | bp->bufp = bp->buf; |
| 4827 | bp->ebufp = bp->bufp + i; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4828 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4829 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4830 | ap->afpos++; |
| 4831 | return *bp->bufp++ & 0177; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4832 | } |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4833 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4834 | if (interactive && isatty(ap->afile)) { |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 4835 | /* moved to G: static char filechar_cmdbuf[BUFSIZ]; */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4836 | static int position = 0, size = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4837 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4838 | while (size == 0 || position >= size) { |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 4839 | read_line_input(current_prompt, filechar_cmdbuf, BUFSIZ, line_input_state); |
| 4840 | size = strlen(filechar_cmdbuf); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4841 | position = 0; |
| 4842 | } |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 4843 | c = filechar_cmdbuf[position]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4844 | position++; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4845 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4846 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4847 | #endif |
| 4848 | i = safe_read(ap->afile, &c, sizeof(c)); |
| 4849 | return i == sizeof(c) ? (c & 0x7f) : (closef(ap->afile), 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4850 | } |
| 4851 | |
| 4852 | /* |
| 4853 | * Return the characters from a here temp file. |
| 4854 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4855 | static int herechar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4856 | { |
| 4857 | char c; |
| 4858 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4859 | if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) { |
| 4860 | close(ap->afile); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4861 | c = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4862 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4863 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | /* |
| 4867 | * Return the characters produced by a process (`...`). |
| 4868 | * Quote them if required, and remove any trailing newline characters. |
| 4869 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4870 | static int gravechar(struct ioarg *ap, struct io *iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4871 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4872 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4873 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4874 | c = qgravechar(ap, iop) & ~QUOTE; |
| 4875 | if (c == '\n') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4876 | c = ' '; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4877 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4878 | } |
| 4879 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4880 | static int qgravechar(struct ioarg *ap, struct io *iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4881 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4882 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4883 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4884 | DBGPRINTF3(("QGRAVECHAR: enter, ap=%p, iop=%p\n", ap, iop)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4885 | |
| 4886 | if (iop->xchar) { |
| 4887 | if (iop->nlcount) { |
| 4888 | iop->nlcount--; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4889 | return '\n' | QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4890 | } |
| 4891 | c = iop->xchar; |
| 4892 | iop->xchar = 0; |
| 4893 | } else if ((c = filechar(ap)) == '\n') { |
| 4894 | iop->nlcount = 1; |
| 4895 | while ((c = filechar(ap)) == '\n') |
| 4896 | iop->nlcount++; |
| 4897 | iop->xchar = c; |
| 4898 | if (c == 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4899 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4900 | iop->nlcount--; |
| 4901 | c = '\n'; |
| 4902 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4903 | return c != 0 ? c | QUOTE : 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | /* |
| 4907 | * Return a single command (usually the first line) from a file. |
| 4908 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4909 | static int linechar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4910 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4911 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4912 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4913 | c = filechar(ap); |
| 4914 | if (c == '\n') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4915 | if (!multiline) { |
| 4916 | closef(ap->afile); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4917 | ap->afile = -1; /* illegal value */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4918 | } |
| 4919 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4920 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4921 | } |
| 4922 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4923 | /* |
| 4924 | * remap fd into Shell's fd space |
| 4925 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4926 | static int remap(int fd) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4927 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4928 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4929 | int map[NOFILE]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4930 | int newfd; |
| 4931 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4932 | 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] | 4933 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4934 | if (fd < global_env.iofd) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4935 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4936 | map[i] = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4937 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4938 | do { |
| 4939 | map[fd] = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4940 | newfd = dup(fd); |
| 4941 | fd = newfd; |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 4942 | } while (fd >= 0 && fd < global_env.iofd); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4943 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4944 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4945 | if (map[i]) |
| 4946 | close(i); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4947 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4948 | if (fd < 0) |
| 4949 | err("too many files open in shell"); |
| 4950 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4951 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4952 | return fd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4955 | static int openpipe(int *pv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4956 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4957 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4958 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4959 | i = pipe(pv); |
| 4960 | if (i < 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4961 | err("can't create pipe - try again"); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4962 | return i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4965 | static void closepipe(int *pv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4966 | { |
| 4967 | if (pv != NULL) { |
| 4968 | close(*pv++); |
| 4969 | close(*pv); |
| 4970 | } |
| 4971 | } |
| 4972 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4973 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4974 | /* -------- here.c -------- */ |
| 4975 | |
| 4976 | /* |
| 4977 | * here documents |
| 4978 | */ |
| 4979 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4980 | static void markhere(char *s, struct ioword *iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4981 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4982 | struct here *h, *lh; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4983 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4984 | DBGPRINTF7(("MARKHERE: enter, s=%p\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4985 | |
| 4986 | h = (struct here *) space(sizeof(struct here)); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4987 | if (h == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4988 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4989 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4990 | h->h_tag = evalstr(s, DOSUB); |
| 4991 | if (h->h_tag == 0) |
| 4992 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4993 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4994 | h->h_iop = iop; |
| 4995 | iop->io_name = 0; |
| 4996 | h->h_next = NULL; |
| 4997 | if (inhere == 0) |
| 4998 | inhere = h; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4999 | else { |
| 5000 | for (lh = inhere; lh != NULL; lh = lh->h_next) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5001 | if (lh->h_next == 0) { |
| 5002 | lh->h_next = h; |
| 5003 | break; |
| 5004 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 5005 | } |
| 5006 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5007 | iop->io_flag |= IOHERE | IOXHERE; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 5008 | for (s = h->h_tag; *s; s++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5009 | if (*s & QUOTE) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5010 | iop->io_flag &= ~IOXHERE; |
| 5011 | *s &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5012 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 5013 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5014 | h->h_dosub = iop->io_flag & IOXHERE; |
| 5015 | } |
| 5016 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 5017 | static void gethere(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5018 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5019 | struct here *h, *hp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5020 | |
| 5021 | DBGPRINTF7(("GETHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5022 | |
| 5023 | /* Scan here files first leaving inhere list in place */ |
| 5024 | for (hp = h = inhere; h != NULL; hp = h, h = h->h_next) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5025 | readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub ? 0 : '\''); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5026 | |
| 5027 | /* Make inhere list active - keep list intact for scraphere */ |
| 5028 | if (hp != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5029 | hp->h_next = acthere; |
| 5030 | acthere = inhere; |
| 5031 | inhere = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5032 | } |
| 5033 | } |
| 5034 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5035 | static void readhere(char **name, char *s, int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5036 | { |
| 5037 | int tf; |
| 5038 | char tname[30] = ".msh_XXXXXX"; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5039 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5040 | jmp_buf ev; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5041 | char myline[LINELIM + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5042 | char *thenext; |
| 5043 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 5044 | DBGPRINTF7(("READHERE: enter, name=%p, s=%p\n", name, s)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5045 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5046 | tf = mkstemp(tname); |
| 5047 | if (tf < 0) |
| 5048 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5049 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5050 | *name = strsave(tname, areanum); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 5051 | errpt = ev; |
| 5052 | if (newenv(setjmp(errpt)) != 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5053 | unlink(tname); |
| 5054 | else { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 5055 | pushio(global_env.iop->argp, (int (*)(struct ioarg *)) global_env.iop->iofn); |
| 5056 | global_env.iobase = global_env.iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5057 | for (;;) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 5058 | if (interactive && global_env.iop <= iostack) { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 5059 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5060 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5061 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5062 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5063 | #endif |
| 5064 | } |
| 5065 | thenext = myline; |
| 5066 | while ((c = my_getc(ec)) != '\n' && c) { |
| 5067 | if (ec == '\'') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5068 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5069 | if (thenext >= &myline[LINELIM]) { |
| 5070 | c = 0; |
| 5071 | break; |
| 5072 | } |
| 5073 | *thenext++ = c; |
| 5074 | } |
| 5075 | *thenext = 0; |
| 5076 | if (strcmp(s, myline) == 0 || c == 0) |
| 5077 | break; |
| 5078 | *thenext++ = '\n'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5079 | write(tf, myline, (int) (thenext - myline)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5080 | } |
| 5081 | if (c == 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5082 | prs("here document `"); |
| 5083 | prs(s); |
| 5084 | err("' unclosed"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5085 | } |
| 5086 | quitenv(); |
| 5087 | } |
| 5088 | close(tf); |
| 5089 | } |
| 5090 | |
| 5091 | /* |
| 5092 | * open here temp file. |
| 5093 | * if unquoted here, expand here temp file into second temp file. |
| 5094 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 5095 | static int herein(char *hname, int xdoll) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5096 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5097 | int hf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5098 | int tf; |
| 5099 | |
| 5100 | #if __GNUC__ |
| 5101 | /* Avoid longjmp clobbering */ |
| 5102 | (void) &tf; |
| 5103 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5104 | if (hname == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 5105 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5106 | |
| 5107 | DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll)); |
| 5108 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5109 | hf = open(hname, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5110 | if (hf < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 5111 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5112 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5113 | if (xdoll) { |
| 5114 | char c; |
| 5115 | char tname[30] = ".msh_XXXXXX"; |
| 5116 | jmp_buf ev; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5117 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5118 | tf = mkstemp(tname); |
| 5119 | if (tf < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 5120 | return -1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 5121 | errpt = ev; |
| 5122 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5123 | PUSHIO(afile, hf, herechar); |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 5124 | setbase(global_env.iop); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5125 | while ((c = subgetc(0, 0)) != 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5126 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5127 | write(tf, &c, sizeof c); |
| 5128 | } |
| 5129 | quitenv(); |
| 5130 | } else |
| 5131 | unlink(tname); |
| 5132 | close(tf); |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5133 | tf = open(tname, O_RDONLY); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5134 | unlink(tname); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 5135 | return tf; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 5136 | } |
| 5137 | return hf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5138 | } |
| 5139 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 5140 | static void scraphere(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5141 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5142 | struct here *h; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5143 | |
| 5144 | DBGPRINTF7(("SCRAPHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5145 | |
| 5146 | for (h = inhere; h != NULL; h = h->h_next) { |
| 5147 | if (h->h_iop && h->h_iop->io_name) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5148 | unlink(h->h_iop->io_name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5149 | } |
| 5150 | inhere = NULL; |
| 5151 | } |
| 5152 | |
| 5153 | /* unlink here temp files before a freearea(area) */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 5154 | static void freehere(int area) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5155 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5156 | struct here *h, *hl; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5157 | |
| 5158 | DBGPRINTF6(("FREEHERE: enter, area=%d\n", area)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5159 | |
| 5160 | hl = NULL; |
| 5161 | for (h = acthere; h != NULL; h = h->h_next) |
| 5162 | if (getarea((char *) h) >= area) { |
| 5163 | if (h->h_iop->io_name != NULL) |
| 5164 | unlink(h->h_iop->io_name); |
| 5165 | if (hl == NULL) |
| 5166 | acthere = h->h_next; |
| 5167 | else |
| 5168 | hl->h_next = h->h_next; |
| 5169 | } else |
| 5170 | hl = h; |
| 5171 | } |
| 5172 | |
| 5173 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5174 | /* -------- sh.c -------- */ |
| 5175 | /* |
| 5176 | * shell |
| 5177 | */ |
| 5178 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 5179 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5180 | int msh_main(int argc, char **argv) |
| 5181 | { |
| 5182 | int f; |
| 5183 | char *s; |
| 5184 | int cflag; |
| 5185 | char *name, **ap; |
| 5186 | int (*iof) (struct ioarg *); |
| 5187 | |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 5188 | INIT_G(); |
| 5189 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 5190 | sharedbuf.id = AFID_NOBUF; |
| 5191 | mainbuf.id = AFID_NOBUF; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 5192 | elinep = line + sizeof(line) - 5; |
| 5193 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5194 | #if ENABLE_FEATURE_EDITING |
| 5195 | line_input_state = new_line_input_t(FOR_SHELL); |
| 5196 | #endif |
| 5197 | |
| 5198 | DBGPRINTF(("MSH_MAIN: argc %d, environ %p\n", argc, environ)); |
| 5199 | |
| 5200 | initarea(); |
| 5201 | ap = environ; |
| 5202 | if (ap != NULL) { |
| 5203 | while (*ap) |
| 5204 | assign(*ap++, !COPYV); |
| 5205 | for (ap = environ; *ap;) |
| 5206 | export(lookup(*ap++)); |
| 5207 | } |
| 5208 | closeall(); |
| 5209 | areanum = 1; |
| 5210 | |
| 5211 | shell = lookup("SHELL"); |
| 5212 | if (shell->value == null) |
| 5213 | setval(shell, (char *)DEFAULT_SHELL); |
| 5214 | export(shell); |
| 5215 | |
| 5216 | homedir = lookup("HOME"); |
| 5217 | if (homedir->value == null) |
| 5218 | setval(homedir, "/"); |
| 5219 | export(homedir); |
| 5220 | |
| 5221 | setval(lookup("$"), putn(getpid())); |
| 5222 | |
| 5223 | path = lookup("PATH"); |
| 5224 | if (path->value == null) { |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5225 | /* Can be merged with same string elsewhere in bbox */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5226 | if (geteuid() == 0) |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 5227 | setval(path, bb_default_root_path); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5228 | else |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 5229 | setval(path, bb_default_path); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5230 | } |
| 5231 | export(path); |
| 5232 | |
| 5233 | ifs = lookup("IFS"); |
| 5234 | if (ifs->value == null) |
| 5235 | setval(ifs, " \t\n"); |
| 5236 | |
| 5237 | #ifdef MSHDEBUG |
| 5238 | mshdbg_var = lookup("MSHDEBUG"); |
| 5239 | if (mshdbg_var->value == null) |
| 5240 | setval(mshdbg_var, "0"); |
| 5241 | #endif |
| 5242 | |
| 5243 | prompt = lookup("PS1"); |
| 5244 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 5245 | if (prompt->value == null) |
| 5246 | #endif |
| 5247 | setval(prompt, DEFAULT_USER_PROMPT); |
| 5248 | if (geteuid() == 0) { |
| 5249 | setval(prompt, DEFAULT_ROOT_PROMPT); |
| 5250 | prompt->status &= ~EXPORT; |
| 5251 | } |
| 5252 | cprompt = lookup("PS2"); |
| 5253 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 5254 | if (cprompt->value == null) |
| 5255 | #endif |
| 5256 | setval(cprompt, "> "); |
| 5257 | |
| 5258 | iof = filechar; |
| 5259 | cflag = 0; |
| 5260 | name = *argv++; |
| 5261 | if (--argc >= 1) { |
| 5262 | if (argv[0][0] == '-' && argv[0][1] != '\0') { |
| 5263 | for (s = argv[0] + 1; *s; s++) |
| 5264 | switch (*s) { |
| 5265 | case 'c': |
| 5266 | prompt->status &= ~EXPORT; |
| 5267 | cprompt->status &= ~EXPORT; |
| 5268 | setval(prompt, ""); |
| 5269 | setval(cprompt, ""); |
| 5270 | cflag = 1; |
| 5271 | if (--argc > 0) |
| 5272 | PUSHIO(aword, *++argv, iof = nlchar); |
| 5273 | break; |
| 5274 | |
| 5275 | case 'q': |
| 5276 | qflag = SIG_DFL; |
| 5277 | break; |
| 5278 | |
| 5279 | case 's': |
| 5280 | /* standard input */ |
| 5281 | break; |
| 5282 | |
| 5283 | case 't': |
| 5284 | prompt->status &= ~EXPORT; |
| 5285 | setval(prompt, ""); |
| 5286 | iof = linechar; |
| 5287 | break; |
| 5288 | |
| 5289 | case 'i': |
| 5290 | interactive++; |
| 5291 | default: |
| 5292 | if (*s >= 'a' && *s <= 'z') |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 5293 | FLAG[(int) *s]++; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5294 | } |
| 5295 | } else { |
| 5296 | argv--; |
| 5297 | argc++; |
| 5298 | } |
| 5299 | |
| 5300 | if (iof == filechar && --argc > 0) { |
| 5301 | setval(prompt, ""); |
| 5302 | setval(cprompt, ""); |
| 5303 | prompt->status &= ~EXPORT; |
| 5304 | cprompt->status &= ~EXPORT; |
| 5305 | |
| 5306 | /* Shell is non-interactive, activate printf-based debug */ |
| 5307 | #ifdef MSHDEBUG |
| 5308 | mshdbg = (int) (((char) (mshdbg_var->value[0])) - '0'); |
| 5309 | if (mshdbg < 0) |
| 5310 | mshdbg = 0; |
| 5311 | #endif |
| 5312 | DBGPRINTF(("MSH_MAIN: calling newfile()\n")); |
| 5313 | |
| 5314 | name = *++argv; |
| 5315 | if (newfile(name)) |
| 5316 | exit(1); /* Exit on error */ |
| 5317 | } |
| 5318 | } |
| 5319 | |
| 5320 | setdash(); |
| 5321 | |
| 5322 | /* 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] | 5323 | if (global_env.iop < iostack) { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5324 | PUSHIO(afile, 0, iof); |
| 5325 | if (isatty(0) && isatty(1) && !cflag) { |
| 5326 | interactive++; |
| 5327 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 5328 | #ifdef MSHDEBUG |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 5329 | 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] | 5330 | #else |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 5331 | printf("\n\n%s built-in shell (msh)\n", bb_banner); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5332 | #endif |
| 5333 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 5334 | #endif |
| 5335 | } |
| 5336 | } |
| 5337 | |
| 5338 | signal(SIGQUIT, qflag); |
| 5339 | if (name && name[0] == '-') { |
| 5340 | interactive++; |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5341 | f = open(".profile", O_RDONLY); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5342 | if (f >= 0) |
| 5343 | next(remap(f)); |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 5344 | f = open("/etc/profile", O_RDONLY); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5345 | if (f >= 0) |
| 5346 | next(remap(f)); |
| 5347 | } |
| 5348 | if (interactive) |
| 5349 | signal(SIGTERM, sig); |
| 5350 | |
| 5351 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 5352 | signal(SIGINT, onintr); |
| 5353 | dolv = argv; |
| 5354 | dolc = argc; |
| 5355 | dolv[0] = name; |
| 5356 | if (dolc > 1) { |
| 5357 | for (ap = ++argv; --argc > 0;) { |
| 5358 | *ap = *argv++; |
| 5359 | if (assign(*ap, !COPYV)) { |
| 5360 | dolc--; /* keyword */ |
| 5361 | } else { |
| 5362 | ap++; |
| 5363 | } |
| 5364 | } |
| 5365 | } |
| 5366 | setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc)); |
| 5367 | |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 5368 | 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] | 5369 | |
| 5370 | for (;;) { |
Denis Vlasenko | c794c51 | 2007-12-16 17:21:29 +0000 | [diff] [blame] | 5371 | if (interactive && global_env.iop <= iostack) { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5372 | #if ENABLE_FEATURE_EDITING |
| 5373 | current_prompt = prompt->value; |
| 5374 | #else |
| 5375 | prs(prompt->value); |
| 5376 | #endif |
| 5377 | } |
| 5378 | onecommand(); |
| 5379 | /* Ensure that getenv("PATH") stays current */ |
| 5380 | setenv("PATH", path->value, 1); |
| 5381 | } |
| 5382 | |
| 5383 | DBGPRINTF(("MSH_MAIN: returning.\n")); |
| 5384 | } |
| 5385 | |
| 5386 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5387 | /* |
| 5388 | * Copyright (c) 1987,1997, Prentice Hall |
| 5389 | * All rights reserved. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5390 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5391 | * Redistribution and use of the MINIX operating system in source and |
| 5392 | * binary forms, with or without modification, are permitted provided |
| 5393 | * that the following conditions are met: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5394 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5395 | * Redistributions of source code must retain the above copyright |
| 5396 | * notice, this list of conditions and the following disclaimer. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5397 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5398 | * Redistributions in binary form must reproduce the above |
| 5399 | * copyright notice, this list of conditions and the following |
| 5400 | * disclaimer in the documentation and/or other materials provided |
| 5401 | * with the distribution. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5402 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5403 | * Neither the name of Prentice Hall nor the names of the software |
| 5404 | * authors or contributors may be used to endorse or promote |
| 5405 | * products derived from this software without specific prior |
| 5406 | * written permission. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5407 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5408 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND |
| 5409 | * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 5410 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 5411 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 5412 | * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE |
| 5413 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 5414 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 5415 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 5416 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 5417 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 5418 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 5419 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 5420 | * |
| 5421 | */ |