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 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | * General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 26 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 27 | * Original copyright notice is retained at the end of this file. |
| 28 | */ |
| 29 | |
| 30 | #include <ctype.h> |
| 31 | #include <dirent.h> |
| 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <limits.h> |
| 35 | #include <setjmp.h> |
| 36 | #include <signal.h> |
| 37 | #include <stddef.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <time.h> |
| 42 | #include <unistd.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <sys/times.h> |
| 45 | #include <sys/types.h> |
| 46 | #include <sys/wait.h> |
| 47 | |
| 48 | #include "cmdedit.h" |
| 49 | #include "busybox.h" |
| 50 | |
| 51 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 52 | /* Conditional use of "register" keyword */ |
| 53 | #define REGISTER register |
| 54 | |
| 55 | |
| 56 | /*#define MSHDEBUG 1*/ |
| 57 | |
| 58 | #ifdef MSHDEBUG |
| 59 | int mshdbg = 0; |
| 60 | |
| 61 | #define DBGPRINTF(x) if(mshdbg>0)printf x |
| 62 | #define DBGPRINTF0(x) if(mshdbg>0)printf x |
| 63 | #define DBGPRINTF1(x) if(mshdbg>1)printf x |
| 64 | #define DBGPRINTF2(x) if(mshdbg>2)printf x |
| 65 | #define DBGPRINTF3(x) if(mshdbg>3)printf x |
| 66 | #define DBGPRINTF4(x) if(mshdbg>4)printf x |
| 67 | #define DBGPRINTF5(x) if(mshdbg>5)printf x |
| 68 | #define DBGPRINTF6(x) if(mshdbg>6)printf x |
| 69 | #define DBGPRINTF7(x) if(mshdbg>7)printf x |
| 70 | #define DBGPRINTF8(x) if(mshdbg>8)printf x |
| 71 | #define DBGPRINTF9(x) if(mshdbg>9)printf x |
| 72 | |
| 73 | int mshdbg_rc = 0; |
| 74 | |
| 75 | #define RCPRINTF(x) if(mshdbg_rc)printf x |
| 76 | |
| 77 | #else |
| 78 | |
| 79 | #define DBGPRINTF(x) |
| 80 | #define DBGPRINTF0(x) |
| 81 | #define DBGPRINTF1(x) |
| 82 | #define DBGPRINTF2(x) |
| 83 | #define DBGPRINTF3(x) |
| 84 | #define DBGPRINTF4(x) |
| 85 | #define DBGPRINTF5(x) |
| 86 | #define DBGPRINTF6(x) |
| 87 | #define DBGPRINTF7(x) |
| 88 | #define DBGPRINTF8(x) |
| 89 | #define DBGPRINTF9(x) |
| 90 | |
| 91 | #define RCPRINTF(x) |
| 92 | |
| 93 | #endif /* MSHDEBUG */ |
| 94 | |
| 95 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 96 | /* -------- sh.h -------- */ |
| 97 | /* |
| 98 | * shell |
| 99 | */ |
| 100 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 101 | #define LINELIM 2100 |
| 102 | #define NPUSH 8 /* limit to input nesting */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 103 | |
Eric Andersen | 392947c | 2002-12-11 07:42:46 +0000 | [diff] [blame] | 104 | #undef NOFILE |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 105 | #define NOFILE 20 /* Number of open files */ |
| 106 | #define NUFILE 10 /* Number of user-accessible files */ |
| 107 | #define FDBASE 10 /* First file usable by Shell */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * values returned by wait |
| 111 | */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 112 | #define WAITSIG(s) ((s)&0177) |
| 113 | #define WAITVAL(s) (((s)>>8)&0377) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 114 | #define WAITCORE(s) (((s)&0200)!=0) |
| 115 | |
| 116 | /* |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 117 | * library and system definitions |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 118 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 119 | typedef void xint; /* base type of jmp_buf, for not broken compilers */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * shell components |
| 123 | */ |
| 124 | |
| 125 | #define QUOTE 0200 |
| 126 | |
| 127 | #define NOBLOCK ((struct op *)NULL) |
| 128 | #define NOWORD ((char *)NULL) |
| 129 | #define NOWORDS ((char **)NULL) |
| 130 | #define NOPIPE ((int *)NULL) |
| 131 | |
| 132 | /* |
| 133 | * Description of a command or an operation on commands. |
| 134 | * Might eventually use a union. |
| 135 | */ |
| 136 | struct op { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 137 | int type; /* operation type, see below */ |
| 138 | char **words; /* arguments to a command */ |
| 139 | struct ioword **ioact; /* IO actions (eg, < > >>) */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 140 | struct op *left; |
| 141 | struct op *right; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 142 | char *str; /* identifier for case and for */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 145 | #define TCOM 1 /* command */ |
| 146 | #define TPAREN 2 /* (c-list) */ |
| 147 | #define TPIPE 3 /* a | b */ |
| 148 | #define TLIST 4 /* a [&;] b */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 149 | #define TOR 5 /* || */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 150 | #define TAND 6 /* && */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 151 | #define TFOR 7 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 152 | #define TDO 8 |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 153 | #define TCASE 9 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 154 | #define TIF 10 |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 155 | #define TWHILE 11 |
| 156 | #define TUNTIL 12 |
| 157 | #define TELIF 13 |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 158 | #define TPAT 14 /* pattern in case */ |
| 159 | #define TBRACE 15 /* {c-list} */ |
| 160 | #define TASYNC 16 /* c & */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 161 | /* Added to support "." file expansion */ |
| 162 | #define TDOT 17 |
| 163 | |
| 164 | /* Strings for names to make debug easier */ |
| 165 | char *T_CMD_NAMES[] = { |
| 166 | "PLACEHOLDER", |
| 167 | "TCOM", |
| 168 | "TPAREN", |
| 169 | "TPIPE", |
| 170 | "TLIST", |
| 171 | "TOR", |
| 172 | "TAND", |
| 173 | "TFOR", |
| 174 | "TDO", |
| 175 | "TCASE", |
| 176 | "TIF", |
| 177 | "TWHILE", |
| 178 | "TUNTIL", |
| 179 | "TELIF", |
| 180 | "TPAT", |
| 181 | "TBRACE", |
| 182 | "TASYNC", |
| 183 | "TDOT", |
| 184 | }; |
| 185 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * actions determining the environment of a process |
| 189 | */ |
| 190 | #define BIT(i) (1<<(i)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 191 | #define FEXEC BIT(0) /* execute without forking */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 192 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 193 | #if 0 /* Original value */ |
| 194 | #define AREASIZE (65000) |
| 195 | #else |
| 196 | #define AREASIZE (90000) |
| 197 | #endif |
| 198 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 199 | /* |
| 200 | * flags to control evaluation of words |
| 201 | */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 202 | #define DOSUB 1 /* interpret $, `, and quotes */ |
| 203 | #define DOBLANK 2 /* perform blank interpretation */ |
| 204 | #define DOGLOB 4 /* interpret [?* */ |
| 205 | #define DOKEY 8 /* move words with `=' to 2nd arg. list */ |
| 206 | #define DOTRIM 16 /* trim resulting string */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 207 | |
| 208 | #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM) |
| 209 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 210 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 211 | /* PROTOTYPES */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 212 | static int newfile(char *s); |
| 213 | static char *findeq(char *cp); |
| 214 | static char *cclass(char *p, int sub); |
| 215 | static void initarea(void); |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 216 | extern int msh_main(int argc, char **argv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 217 | |
| 218 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 219 | struct brkcon { |
| 220 | jmp_buf brkpt; |
| 221 | struct brkcon *nextlev; |
| 222 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 223 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 224 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 225 | /* |
| 226 | * redirection |
| 227 | */ |
| 228 | struct ioword { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 229 | short io_unit; /* unit affected */ |
| 230 | short io_flag; /* action (below) */ |
| 231 | char *io_name; /* file name */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 232 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 233 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 234 | #define IOREAD 1 /* < */ |
| 235 | #define IOHERE 2 /* << (here file) */ |
| 236 | #define IOWRITE 4 /* > */ |
| 237 | #define IOCAT 8 /* >> */ |
| 238 | #define IOXHERE 16 /* ${}, ` in << */ |
| 239 | #define IODUP 32 /* >&digit */ |
| 240 | #define IOCLOSE 64 /* >&- */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 241 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 242 | #define IODEFAULT (-1) /* token for default IO unit */ |
| 243 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 244 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 245 | |
| 246 | /* |
| 247 | * parsing & execution environment |
| 248 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 249 | static struct env { |
| 250 | char *linep; |
| 251 | struct io *iobase; |
| 252 | struct io *iop; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 253 | xint *errpt; /* void * */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 254 | int iofd; |
| 255 | struct env *oenv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 256 | } e; |
| 257 | |
| 258 | /* |
| 259 | * flags: |
| 260 | * -e: quit on error |
| 261 | * -k: look for name=value everywhere on command line |
| 262 | * -n: no execution |
| 263 | * -t: exit after reading and executing one command |
| 264 | * -v: echo as read |
| 265 | * -x: trace |
| 266 | * -u: unset variables net diagnostic |
| 267 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 268 | static char *flag; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 269 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 270 | static char *null; /* null value for variable */ |
| 271 | static int intr; /* interrupt pending */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 272 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 273 | static char *trap[_NSIG + 1]; |
| 274 | static char ourtrap[_NSIG + 1]; |
| 275 | static int trapset; /* trap pending */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 276 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 277 | static int heedint; /* heed interrupt signals */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 278 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 279 | static int yynerrs; /* yacc */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 280 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 281 | static char line[LINELIM]; |
| 282 | static char *elinep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 283 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 284 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 285 | /* |
| 286 | * other functions |
| 287 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 288 | static int (*inbuilt(char *s)) (struct op *); |
Eric Andersen | 392947c | 2002-12-11 07:42:46 +0000 | [diff] [blame] | 289 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 290 | static char *rexecve(char *c, char **v, char **envp); |
| 291 | static char *space(int n); |
| 292 | static char *strsave(char *s, int a); |
| 293 | static char *evalstr(char *cp, int f); |
| 294 | static char *putn(int n); |
| 295 | static char *itoa(int n); |
| 296 | static char *unquote(char *as); |
| 297 | static struct var *lookup(char *n); |
| 298 | static int rlookup(char *n); |
| 299 | static struct wdblock *glob(char *cp, struct wdblock *wb); |
| 300 | static int my_getc(int ec); |
| 301 | static int subgetc(int ec, int quoted); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 302 | static char **makenv(int all); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 303 | static char **eval(char **ap, int f); |
| 304 | static int setstatus(int s); |
| 305 | static int waitfor(int lastpid, int canintr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 306 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 307 | static void onintr(int s); /* SIGINT handler */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 308 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 309 | static int newenv(int f); |
| 310 | static void quitenv(void); |
| 311 | static void err(char *s); |
| 312 | static int anys(char *s1, char *s2); |
| 313 | static int any(int c, char *s); |
| 314 | static void next(int f); |
| 315 | static void setdash(void); |
| 316 | static void onecommand(void); |
| 317 | static void runtrap(int i); |
| 318 | static int gmatch(char *s, char *p); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 319 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 320 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 321 | /* |
| 322 | * error handling |
| 323 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 324 | static void leave(void); /* abort shell (or fail in subshell) */ |
| 325 | static void fail(void); /* fail but return to process next command */ |
| 326 | static void warn(char *s); |
| 327 | static void sig(int i); /* default signal handler */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 328 | |
| 329 | |
| 330 | |
| 331 | /* -------- area stuff -------- */ |
| 332 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 333 | #define REGSIZE sizeof(struct region) |
| 334 | #define GROWBY (256) |
| 335 | /* #define SHRINKBY (64) */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 336 | #undef SHRINKBY |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 337 | #define FREE (32767) |
| 338 | #define BUSY (0) |
| 339 | #define ALIGN (sizeof(int)-1) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 340 | |
| 341 | |
| 342 | struct region { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 343 | struct region *next; |
| 344 | int area; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | |
| 348 | |
| 349 | /* -------- grammar stuff -------- */ |
| 350 | typedef union { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 351 | char *cp; |
| 352 | char **wp; |
| 353 | int i; |
| 354 | struct op *o; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 355 | } YYSTYPE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 356 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 357 | #define WORD 256 |
| 358 | #define LOGAND 257 |
| 359 | #define LOGOR 258 |
| 360 | #define BREAK 259 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 361 | #define IF 260 |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 362 | #define THEN 261 |
| 363 | #define ELSE 262 |
| 364 | #define ELIF 263 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 365 | #define FI 264 |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 366 | #define CASE 265 |
| 367 | #define ESAC 266 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 368 | #define FOR 267 |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 369 | #define WHILE 268 |
| 370 | #define UNTIL 269 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 371 | #define DO 270 |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 372 | #define DONE 271 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 373 | #define IN 272 |
| 374 | /* Added for "." file expansion */ |
| 375 | #define DOT 273 |
| 376 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 377 | #define YYERRCODE 300 |
| 378 | |
| 379 | /* flags to yylex */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 380 | #define CONTIN 01 /* skip new lines to complete command */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 381 | |
| 382 | #define SYNTAXERR zzerr() |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 383 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 384 | static struct op *pipeline(int cf); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 385 | static struct op *andor(void); |
| 386 | static struct op *c_list(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 387 | static int synio(int cf); |
| 388 | static void musthave(int c, int cf); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 389 | static struct op *simple(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 390 | static struct op *nested(int type, int mark); |
| 391 | static struct op *command(int cf); |
| 392 | static struct op *dogroup(int onlydone); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 393 | static struct op *thenpart(void); |
| 394 | static struct op *elsepart(void); |
| 395 | static struct op *caselist(void); |
| 396 | static struct op *casepart(void); |
| 397 | static char **pattern(void); |
| 398 | static char **wordlist(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 399 | static struct op *list(struct op *t1, struct op *t2); |
| 400 | 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] | 401 | static struct op *newtp(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 402 | static struct op *namelist(struct op *t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 403 | static char **copyw(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 404 | static void word(char *cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 405 | static struct ioword **copyio(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 406 | static struct ioword *io(int u, int f, char *cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 407 | static void zzerr(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 408 | static void yyerror(char *s); |
| 409 | static int yylex(int cf); |
| 410 | static int collect(int c, int c1); |
| 411 | static int dual(int c); |
| 412 | static void diag(int ec); |
| 413 | static char *tree(unsigned size); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 414 | |
| 415 | /* -------- var.h -------- */ |
| 416 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 417 | struct var { |
| 418 | char *value; |
| 419 | char *name; |
| 420 | struct var *next; |
| 421 | char status; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 422 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 423 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 424 | #define COPYV 1 /* flag to setval, suggesting copy */ |
| 425 | #define RONLY 01 /* variable is read-only */ |
| 426 | #define EXPORT 02 /* variable is to be exported */ |
| 427 | #define GETCELL 04 /* name & value space was got with getcell */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 428 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 429 | static int yyparse(void); |
| 430 | static struct var *lookup(char *n); |
| 431 | static void setval(struct var *vp, char *val); |
| 432 | static void nameval(struct var *vp, char *val, char *name); |
| 433 | static void export(struct var *vp); |
| 434 | static void ronly(struct var *vp); |
| 435 | static int isassign(char *s); |
| 436 | static int checkname(char *cp); |
| 437 | static int assign(char *s, int cf); |
| 438 | static void putvlist(int f, int out); |
| 439 | static int eqname(char *n1, char *n2); |
| 440 | |
| 441 | static int execute(struct op *t, int *pin, int *pout, int act); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 442 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 443 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 444 | /* -------- io.h -------- */ |
| 445 | /* io buffer */ |
| 446 | struct iobuf { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 447 | unsigned id; /* buffer id */ |
| 448 | char buf[512]; /* buffer */ |
| 449 | char *bufp; /* pointer into buffer */ |
| 450 | char *ebufp; /* pointer to end of buffer */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 451 | }; |
| 452 | |
| 453 | /* possible arguments to an IO function */ |
| 454 | struct ioarg { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 455 | char *aword; |
| 456 | char **awordlist; |
| 457 | int afile; /* file descriptor */ |
| 458 | unsigned afid; /* buffer id */ |
| 459 | long afpos; /* file position */ |
| 460 | struct iobuf *afbuf; /* buffer for this file */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 461 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 462 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 463 | //static struct ioarg ioargstack[NPUSH]; |
| 464 | #define AFID_NOBUF (~0) |
| 465 | #define AFID_ID 0 |
| 466 | |
| 467 | /* an input generator's state */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 468 | struct io { |
| 469 | int (*iofn) (struct ioarg *, struct io *); |
| 470 | struct ioarg *argp; |
| 471 | int peekc; |
| 472 | char prev; /* previous character read by readc() */ |
| 473 | char nlcount; /* for `'s */ |
| 474 | char xchar; /* for `'s */ |
| 475 | char task; /* reason for pushed IO */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 476 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 477 | |
| 478 | //static struct io iostack[NPUSH]; |
| 479 | #define XOTHER 0 /* none of the below */ |
| 480 | #define XDOLL 1 /* expanding ${} */ |
| 481 | #define XGRAVE 2 /* expanding `'s */ |
| 482 | #define XIO 3 /* file IO */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 483 | |
| 484 | /* in substitution */ |
| 485 | #define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL) |
| 486 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 487 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 488 | /* |
| 489 | * input generators for IO structure |
| 490 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 491 | static int nlchar(struct ioarg *ap); |
| 492 | static int strchar(struct ioarg *ap); |
| 493 | static int qstrchar(struct ioarg *ap); |
| 494 | static int filechar(struct ioarg *ap); |
| 495 | static int herechar(struct ioarg *ap); |
| 496 | static int linechar(struct ioarg *ap); |
| 497 | static int gravechar(struct ioarg *ap, struct io *iop); |
| 498 | static int qgravechar(struct ioarg *ap, struct io *iop); |
| 499 | static int dolchar(struct ioarg *ap); |
| 500 | static int wdchar(struct ioarg *ap); |
| 501 | static void scraphere(void); |
| 502 | static void freehere(int area); |
| 503 | static void gethere(void); |
| 504 | static void markhere(char *s, struct ioword *iop); |
| 505 | static int herein(char *hname, int xdoll); |
| 506 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 507 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 508 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 509 | /* |
| 510 | * IO functions |
| 511 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 512 | static int eofc(void); |
| 513 | static int readc(void); |
| 514 | static void unget(int c); |
| 515 | static void ioecho(int c); |
| 516 | static void prs(char *s); |
| 517 | static void prn(unsigned u); |
| 518 | static void closef(int i); |
| 519 | static void closeall(void); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 520 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 521 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 522 | /* |
| 523 | * IO control |
| 524 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 525 | static void pushio(struct ioarg *argp, int (*f) (struct ioarg *)); |
| 526 | static int remap(int fd); |
| 527 | static int openpipe(int *pv); |
| 528 | static void closepipe(int *pv); |
| 529 | static struct io *setbase(struct io *ip); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 530 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 531 | #define PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen))) |
| 532 | #define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen))) |
| 533 | |
| 534 | /* -------- word.h -------- */ |
| 535 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 536 | #define NSTART 16 /* default number of words to allow for initially */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 537 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 538 | struct wdblock { |
| 539 | short w_bsize; |
| 540 | short w_nword; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 541 | /* bounds are arbitrary */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 542 | char *w_words[1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 543 | }; |
| 544 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 545 | static struct wdblock *addword(char *wd, struct wdblock *wb); |
| 546 | static struct wdblock *newword(int nw); |
| 547 | static char **getwords(struct wdblock *wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 548 | |
| 549 | /* -------- area.h -------- */ |
| 550 | |
| 551 | /* |
| 552 | * storage allocation |
| 553 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 554 | static char *getcell(unsigned nbytes); |
| 555 | static void garbage(void); |
| 556 | static void setarea(char *cp, int a); |
| 557 | static int getarea(char *cp); |
| 558 | static void freearea(int a); |
| 559 | static void freecell(char *cp); |
| 560 | static int areanum; /* current allocation area */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 561 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 562 | #define NEW(type) (type *)getcell(sizeof(type)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 563 | #define DELETE(obj) freecell((char *)obj) |
| 564 | |
| 565 | |
| 566 | /* -------- misc stuff -------- */ |
| 567 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 568 | 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] | 569 | static int iosetup(struct ioword *iop, int pipein, int pipeout); |
| 570 | static void echo(char **wp); |
| 571 | static struct op **find1case(struct op *t, char *w); |
| 572 | static struct op *findcase(struct op *t, char *w); |
| 573 | static void brkset(struct brkcon *bc); |
| 574 | static int dolabel(struct op *t); |
| 575 | static int dohelp(struct op *t); |
| 576 | static int dochdir(struct op *t); |
| 577 | static int doshift(struct op *t); |
| 578 | static int dologin(struct op *t); |
| 579 | static int doumask(struct op *t); |
| 580 | static int doexec(struct op *t); |
| 581 | static int dodot(struct op *t); |
| 582 | static int dowait(struct op *t); |
| 583 | static int doread(struct op *t); |
| 584 | static int doeval(struct op *t); |
| 585 | static int dotrap(struct op *t); |
| 586 | static int getsig(char *s); |
| 587 | static void setsig(int n, sighandler_t f); |
| 588 | static int getn(char *as); |
| 589 | static int dobreak(struct op *t); |
| 590 | static int docontinue(struct op *t); |
| 591 | static int brkcontin(char *cp, int val); |
| 592 | static int doexit(struct op *t); |
| 593 | static int doexport(struct op *t); |
| 594 | static int doreadonly(struct op *t); |
| 595 | static void rdexp(char **wp, void (*f) (struct var *), int key); |
| 596 | static void badid(char *s); |
| 597 | static int doset(struct op *t); |
| 598 | static void varput(char *s, int out); |
| 599 | static int dotimes(struct op *t); |
| 600 | static int expand(char *cp, struct wdblock **wbp, int f); |
| 601 | static char *blank(int f); |
| 602 | static int dollar(int quoted); |
| 603 | static int grave(int quoted); |
| 604 | static void globname(char *we, char *pp); |
| 605 | static char *generate(char *start1, char *end1, char *middle, char *end); |
| 606 | static int anyspcl(struct wdblock *wb); |
| 607 | static int xstrcmp(char *p1, char *p2); |
| 608 | static void glob0(char *a0, unsigned int a1, int a2, |
| 609 | int (*a3) (char *, char *)); |
| 610 | static void glob1(char *base, char *lim); |
| 611 | static void glob2(char *i, char *j); |
| 612 | static void glob3(char *i, char *j, char *k); |
| 613 | static void readhere(char **name, char *s, int ec); |
| 614 | static void pushio(struct ioarg *argp, int (*f) (struct ioarg *)); |
| 615 | static int xxchar(struct ioarg *ap); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 616 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 617 | struct here { |
| 618 | char *h_tag; |
| 619 | int h_dosub; |
| 620 | struct ioword *h_iop; |
| 621 | struct here *h_next; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 622 | }; |
| 623 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 624 | static char *signame[] = { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 625 | "Signal 0", |
| 626 | "Hangup", |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 627 | (char *) NULL, /* interrupt */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 628 | "Quit", |
| 629 | "Illegal instruction", |
| 630 | "Trace/BPT trap", |
| 631 | "Abort", |
| 632 | "Bus error", |
| 633 | "Floating Point Exception", |
| 634 | "Killed", |
| 635 | "SIGUSR1", |
| 636 | "SIGSEGV", |
| 637 | "SIGUSR2", |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 638 | (char *) NULL, /* broken pipe */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 639 | "Alarm clock", |
| 640 | "Terminated", |
| 641 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 642 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 643 | #define NSIGNAL (sizeof(signame)/sizeof(signame[0])) |
| 644 | |
| 645 | struct res { |
| 646 | char *r_name; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 647 | int r_val; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 648 | }; |
| 649 | static struct res restab[] = { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 650 | {"for", FOR}, |
| 651 | {"case", CASE}, |
| 652 | {"esac", ESAC}, |
| 653 | {"while", WHILE}, |
| 654 | {"do", DO}, |
| 655 | {"done", DONE}, |
| 656 | {"if", IF}, |
| 657 | {"in", IN}, |
| 658 | {"then", THEN}, |
| 659 | {"else", ELSE}, |
| 660 | {"elif", ELIF}, |
| 661 | {"until", UNTIL}, |
| 662 | {"fi", FI}, |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 663 | {";;", BREAK}, |
| 664 | {"||", LOGOR}, |
| 665 | {"&&", LOGAND}, |
| 666 | {"{", '{'}, |
| 667 | {"}", '}'}, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 668 | {".", DOT}, |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 669 | {0, 0}, |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 670 | }; |
| 671 | |
| 672 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 673 | struct builtincmd { |
| 674 | const char *name; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 675 | int (*builtinfunc) (struct op * t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 676 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 677 | static const struct builtincmd builtincmds[] = { |
| 678 | {".", dodot}, |
| 679 | {":", dolabel}, |
| 680 | {"break", dobreak}, |
| 681 | {"cd", dochdir}, |
| 682 | {"continue", docontinue}, |
| 683 | {"eval", doeval}, |
| 684 | {"exec", doexec}, |
| 685 | {"exit", doexit}, |
| 686 | {"export", doexport}, |
| 687 | {"help", dohelp}, |
| 688 | {"login", dologin}, |
| 689 | {"newgrp", dologin}, |
| 690 | {"read", doread}, |
| 691 | {"readonly", doreadonly}, |
| 692 | {"set", doset}, |
| 693 | {"shift", doshift}, |
| 694 | {"times", dotimes}, |
| 695 | {"trap", dotrap}, |
| 696 | {"umask", doumask}, |
| 697 | {"wait", dowait}, |
| 698 | {0, 0} |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 699 | }; |
| 700 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 701 | static int expand_dotnode(struct op *); |
| 702 | struct op *scantree(struct op *); |
| 703 | static struct op *dowholefile(int, int); |
| 704 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 705 | /* Globals */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 706 | extern char **environ; /* environment pointer */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 707 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 708 | static char **dolv; |
| 709 | static int dolc; |
| 710 | static int exstat; |
| 711 | static char gflg; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 712 | static int interactive = 0; /* Is this an interactive shell */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 713 | static int execflg; |
| 714 | static int multiline; /* \n changed to ; */ |
| 715 | static struct op *outtree; /* result from parser */ |
| 716 | static xint *failpt; |
| 717 | static xint *errpt; |
| 718 | static struct brkcon *brklist; |
| 719 | static int isbreak; |
| 720 | static struct wdblock *wdlist; |
| 721 | static struct wdblock *iolist; |
| 722 | static char *trap[_NSIG + 1]; |
| 723 | static char ourtrap[_NSIG + 1]; |
| 724 | static int trapset; /* trap pending */ |
| 725 | static int yynerrs; /* yacc */ |
| 726 | static char line[LINELIM]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 727 | |
| 728 | #ifdef MSHDEBUG |
| 729 | static struct var *mshdbg_var; |
| 730 | #endif |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 731 | static struct var *vlist; /* dictionary */ |
| 732 | static struct var *homedir; /* home directory */ |
| 733 | static struct var *prompt; /* main prompt */ |
| 734 | static struct var *cprompt; /* continuation prompt */ |
| 735 | static struct var *path; /* search path for commands */ |
| 736 | static struct var *shell; /* shell to interpret command files */ |
| 737 | static struct var *ifs; /* field separators */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 738 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 739 | static int areanum; /* current allocation area */ |
| 740 | static int intr; |
| 741 | static int inparse; |
| 742 | static char flags['z' - 'a' + 1]; |
| 743 | static char *flag = flags - 'a'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 744 | static char *null = ""; |
| 745 | static int heedint = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 746 | static void (*qflag) (int) = SIG_IGN; |
| 747 | static int startl; |
| 748 | static int peeksym; |
| 749 | static int nlseen; |
| 750 | static int iounit = IODEFAULT; |
| 751 | static YYSTYPE yylval; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 752 | static char *elinep = line + sizeof(line) - 5; |
| 753 | |
| 754 | static struct ioarg temparg = { 0, 0, 0, AFID_NOBUF, 0 }; /* temporary for PUSHIO */ |
| 755 | static struct ioarg ioargstack[NPUSH]; |
| 756 | static struct io iostack[NPUSH]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 757 | static struct iobuf sharedbuf = { AFID_NOBUF }; |
| 758 | static struct iobuf mainbuf = { AFID_NOBUF }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 759 | static unsigned bufid = AFID_ID; /* buffer id counter */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 760 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 761 | static struct here *inhere; /* list of hear docs while parsing */ |
| 762 | static struct here *acthere; /* list of active here documents */ |
| 763 | static struct region *areabot; /* bottom of area */ |
| 764 | static struct region *areatop; /* top of area */ |
| 765 | static struct region *areanxt; /* starting point of scan */ |
| 766 | static void *brktop; |
| 767 | static void *brkaddr; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 768 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 769 | static struct env e = { |
| 770 | line, /* linep: char ptr */ |
| 771 | iostack, /* iobase: struct io ptr */ |
| 772 | iostack - 1, /* iop: struct io ptr */ |
| 773 | (xint *) NULL, /* errpt: void ptr for errors? */ |
| 774 | FDBASE, /* iofd: file desc */ |
| 775 | (struct env *) NULL /* oenv: struct env ptr */ |
| 776 | }; |
| 777 | |
| 778 | #ifdef MSHDEBUG |
| 779 | void print_t(struct op *t) |
| 780 | { |
| 781 | DBGPRINTF(("T: t=0x%x, type %s, words=0x%x, IOword=0x%x\n", t, |
| 782 | T_CMD_NAMES[t->type], t->words, t->ioact)); |
| 783 | |
| 784 | if (t->words) { |
| 785 | DBGPRINTF(("T: W1: %s", t->words[0])); |
| 786 | } |
| 787 | |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | void print_tree(struct op *head) |
| 792 | { |
| 793 | if (head == NULL) { |
| 794 | DBGPRINTF(("PRINT_TREE: no tree\n")); |
| 795 | return; |
| 796 | } |
| 797 | |
| 798 | DBGPRINTF(("NODE: 0x%x, left 0x%x, right 0x%x\n", head, head->left, |
| 799 | head->right)); |
| 800 | |
| 801 | if (head->left) |
| 802 | print_tree(head->left); |
| 803 | |
| 804 | if (head->right) |
| 805 | print_tree(head->right); |
| 806 | |
| 807 | return; |
| 808 | } |
| 809 | #endif /* MSHDEBUG */ |
| 810 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 811 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 812 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 813 | static char *current_prompt; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 814 | #endif |
| 815 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 816 | /* -------- sh.c -------- */ |
| 817 | /* |
| 818 | * shell |
| 819 | */ |
| 820 | |
| 821 | |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 822 | extern int msh_main(int argc, char **argv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 823 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 824 | REGISTER int f; |
| 825 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 826 | int cflag; |
| 827 | char *name, **ap; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 828 | int (*iof) (struct ioarg *); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 829 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 830 | DBGPRINTF(("MSH_MAIN: argc %d, environ 0x%x\n", argc, environ)); |
| 831 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 832 | initarea(); |
| 833 | if ((ap = environ) != NULL) { |
| 834 | while (*ap) |
| 835 | assign(*ap++, !COPYV); |
| 836 | for (ap = environ; *ap;) |
| 837 | export(lookup(*ap++)); |
| 838 | } |
| 839 | closeall(); |
| 840 | areanum = 1; |
| 841 | |
| 842 | shell = lookup("SHELL"); |
| 843 | if (shell->value == null) |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 844 | setval(shell, DEFAULT_SHELL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 845 | export(shell); |
| 846 | |
| 847 | homedir = lookup("HOME"); |
| 848 | if (homedir->value == null) |
| 849 | setval(homedir, "/"); |
| 850 | export(homedir); |
| 851 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 852 | setval(lookup("$"), putn(getpid())); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 853 | |
| 854 | path = lookup("PATH"); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 855 | if (path->value == null) { |
| 856 | if (geteuid() == 0) |
| 857 | setval(path, "/sbin:/bin:/usr/sbin:/usr/bin"); |
| 858 | else |
| 859 | setval(path, "/bin:/usr/bin"); |
| 860 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 861 | export(path); |
| 862 | |
| 863 | ifs = lookup("IFS"); |
| 864 | if (ifs->value == null) |
| 865 | setval(ifs, " \t\n"); |
| 866 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 867 | #ifdef MSHDEBUG |
| 868 | mshdbg_var = lookup("MSHDEBUG"); |
| 869 | if (mshdbg_var->value == null) |
| 870 | setval(mshdbg_var, "0"); |
| 871 | #endif |
| 872 | |
| 873 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 874 | prompt = lookup("PS1"); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 875 | #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 876 | if (prompt->value == null) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 877 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 878 | setval(prompt, "$ "); |
| 879 | if (geteuid() == 0) { |
| 880 | setval(prompt, "# "); |
| 881 | prompt->status &= ~EXPORT; |
| 882 | } |
| 883 | cprompt = lookup("PS2"); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 884 | #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 885 | if (cprompt->value == null) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 886 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 887 | setval(cprompt, "> "); |
| 888 | |
| 889 | iof = filechar; |
| 890 | cflag = 0; |
| 891 | name = *argv++; |
| 892 | if (--argc >= 1) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 893 | if (argv[0][0] == '-' && argv[0][1] != '\0') { |
| 894 | for (s = argv[0] + 1; *s; s++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 895 | switch (*s) { |
| 896 | case 'c': |
| 897 | prompt->status &= ~EXPORT; |
| 898 | cprompt->status &= ~EXPORT; |
| 899 | setval(prompt, ""); |
| 900 | setval(cprompt, ""); |
| 901 | cflag = 1; |
| 902 | if (--argc > 0) |
| 903 | PUSHIO(aword, *++argv, iof = nlchar); |
| 904 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 905 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 906 | case 'q': |
| 907 | qflag = SIG_DFL; |
| 908 | break; |
| 909 | |
| 910 | case 's': |
| 911 | /* standard input */ |
| 912 | break; |
| 913 | |
| 914 | case 't': |
| 915 | prompt->status &= ~EXPORT; |
| 916 | setval(prompt, ""); |
| 917 | iof = linechar; |
| 918 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 919 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 920 | case 'i': |
| 921 | interactive++; |
| 922 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 923 | if (*s >= 'a' && *s <= 'z') |
| 924 | flag[(int) *s]++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 925 | } |
| 926 | } else { |
| 927 | argv--; |
| 928 | argc++; |
| 929 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 930 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 931 | if (iof == filechar && --argc > 0) { |
| 932 | setval(prompt, ""); |
| 933 | setval(cprompt, ""); |
| 934 | prompt->status &= ~EXPORT; |
| 935 | cprompt->status &= ~EXPORT; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 936 | |
| 937 | /* Shell is non-interactive, activate printf-based debug */ |
| 938 | #ifdef MSHDEBUG |
| 939 | mshdbg = (int) (((char) (mshdbg_var->value[0])) - '0'); |
| 940 | if (mshdbg < 0) |
| 941 | mshdbg = 0; |
| 942 | #endif |
| 943 | DBGPRINTF(("MSH_MAIN: calling newfile()\n")); |
| 944 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 945 | if (newfile(name = *++argv)) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 946 | exit(1); /* Exit on error */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 947 | } |
| 948 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 949 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 950 | setdash(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 951 | |
| 952 | /* This won't be true if PUSHIO has been called, say from newfile() above */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 953 | if (e.iop < iostack) { |
| 954 | PUSHIO(afile, 0, iof); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 955 | if (isatty(0) && isatty(1) && !cflag) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 956 | interactive++; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 957 | #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 958 | #ifdef MSHDEBUG |
| 959 | printf("\n\n" BB_BANNER " Built-in shell (msh with debug)\n"); |
| 960 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 961 | printf("\n\n" BB_BANNER " Built-in shell (msh)\n"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 962 | #endif |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 963 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
Eric Andersen | d63dee4 | 2001-10-19 00:22:23 +0000 | [diff] [blame] | 964 | #endif |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 965 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 966 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 967 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 968 | signal(SIGQUIT, qflag); |
| 969 | if (name && name[0] == '-') { |
| 970 | interactive++; |
| 971 | if ((f = open(".profile", 0)) >= 0) |
| 972 | next(remap(f)); |
| 973 | if ((f = open("/etc/profile", 0)) >= 0) |
| 974 | next(remap(f)); |
| 975 | } |
| 976 | if (interactive) |
| 977 | signal(SIGTERM, sig); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 978 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 979 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 980 | signal(SIGINT, onintr); |
| 981 | dolv = argv; |
| 982 | dolc = argc; |
| 983 | dolv[0] = name; |
| 984 | if (dolc > 1) { |
| 985 | for (ap = ++argv; --argc > 0;) { |
| 986 | if (assign(*ap = *argv++, !COPYV)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 987 | dolc--; /* keyword */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 988 | } else { |
| 989 | ap++; |
| 990 | } |
| 991 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 992 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 993 | setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc)); |
| 994 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 995 | DBGPRINTF(("MSH_MAIN: begin FOR loop, interactive %d, e.iop 0x%x, iostack 0x%x\n", interactive, e.iop, iostack)); |
| 996 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 997 | for (;;) { |
| 998 | if (interactive && e.iop <= iostack) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 999 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1000 | current_prompt = prompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1001 | #else |
| 1002 | prs(prompt->value); |
| 1003 | #endif |
| 1004 | } |
| 1005 | onecommand(); |
Eric Andersen | 1c31501 | 2002-04-26 23:40:09 +0000 | [diff] [blame] | 1006 | /* Ensure that getenv("PATH") stays current */ |
| 1007 | setenv("PATH", path->value, 1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1008 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1009 | |
| 1010 | DBGPRINTF(("MSH_MAIN: returning.\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1013 | static void setdash() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1014 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1015 | REGISTER char *cp; |
| 1016 | REGISTER int c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1017 | char m['z' - 'a' + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1018 | |
| 1019 | cp = m; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1020 | for (c = 'a'; c <= 'z'; c++) |
| 1021 | if (flag[(int) c]) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1022 | *cp++ = c; |
| 1023 | *cp = 0; |
| 1024 | setval(lookup("-"), m); |
| 1025 | } |
| 1026 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1027 | static int newfile(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1028 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1029 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1030 | REGISTER int f; |
| 1031 | |
| 1032 | DBGPRINTF7(("NEWFILE: opening %s\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1033 | |
| 1034 | if (strcmp(s, "-") != 0) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1035 | DBGPRINTF(("NEWFILE: s is %s\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1036 | f = open(s, 0); |
| 1037 | if (f < 0) { |
| 1038 | prs(s); |
| 1039 | err(": cannot open"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1040 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1041 | } |
| 1042 | } else |
| 1043 | f = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1044 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1045 | next(remap(f)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1046 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1049 | |
| 1050 | static int expand_dotnode(node) |
| 1051 | struct op *node; |
| 1052 | { |
| 1053 | struct op *outtree_save = outtree; |
| 1054 | |
| 1055 | node->type = TDOT; |
| 1056 | newfile(node->words[1]); |
| 1057 | |
| 1058 | node->left = dowholefile(TDOT, 0); |
| 1059 | |
| 1060 | node->right = NULL; |
| 1061 | |
| 1062 | outtree = outtree_save; |
| 1063 | |
| 1064 | return (1); |
| 1065 | } |
| 1066 | |
| 1067 | struct op *scantree(head) |
| 1068 | struct op *head; |
| 1069 | { |
| 1070 | struct op *dotnode; |
| 1071 | |
| 1072 | if (head == NULL) |
| 1073 | return (NULL); |
| 1074 | |
| 1075 | if (head->left != NULL) { |
| 1076 | dotnode = scantree(head->left); |
| 1077 | if (dotnode) |
| 1078 | return (dotnode); |
| 1079 | } |
| 1080 | |
| 1081 | if (head->right != NULL) { |
| 1082 | dotnode = scantree(head->right); |
| 1083 | if (dotnode) |
| 1084 | return (dotnode); |
| 1085 | } |
| 1086 | |
| 1087 | if (head->words == NULL) |
| 1088 | return (NULL); |
| 1089 | |
| 1090 | DBGPRINTF5(("SCANTREE: checking node 0x%x\n", head)); |
| 1091 | |
| 1092 | if ((head->type != TDOT) && (strcmp(".", head->words[0]) == 0)) { |
| 1093 | DBGPRINTF5(("SCANTREE: dot found in node 0x%x\n", head)); |
| 1094 | return (head); |
| 1095 | } |
| 1096 | |
| 1097 | return (NULL); |
| 1098 | } |
| 1099 | |
| 1100 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1101 | static void onecommand() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1102 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1103 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1104 | jmp_buf m1; |
| 1105 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1106 | DBGPRINTF(("ONECOMMAND: enter, outtree=0x%x\n", outtree)); |
| 1107 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1108 | while (e.oenv) |
| 1109 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1110 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1111 | areanum = 1; |
| 1112 | freehere(areanum); |
| 1113 | freearea(areanum); |
| 1114 | garbage(); |
| 1115 | wdlist = 0; |
| 1116 | iolist = 0; |
| 1117 | e.errpt = 0; |
| 1118 | e.linep = line; |
| 1119 | yynerrs = 0; |
| 1120 | multiline = 0; |
| 1121 | inparse = 1; |
| 1122 | intr = 0; |
| 1123 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1124 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1125 | setjmp(failpt = m1); /* Bruce Evans' fix */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1126 | if (setjmp(failpt = m1) || yyparse() || intr) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1127 | |
| 1128 | DBGPRINTF(("ONECOMMAND: this is not good.\n")); |
| 1129 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1130 | while (e.oenv) |
| 1131 | quitenv(); |
| 1132 | scraphere(); |
| 1133 | if (!interactive && intr) |
| 1134 | leave(); |
| 1135 | inparse = 0; |
| 1136 | intr = 0; |
| 1137 | return; |
| 1138 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1139 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1140 | inparse = 0; |
| 1141 | brklist = 0; |
| 1142 | intr = 0; |
| 1143 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1144 | |
| 1145 | if (!flag['n']) { |
| 1146 | DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=0x%x\n", |
| 1147 | outtree)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1148 | execute(outtree, NOPIPE, NOPIPE, 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1149 | } |
| 1150 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1151 | if (!interactive && intr) { |
| 1152 | execflg = 0; |
| 1153 | leave(); |
| 1154 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1155 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1156 | if ((i = trapset) != 0) { |
| 1157 | trapset = 0; |
| 1158 | runtrap(i); |
| 1159 | } |
| 1160 | } |
| 1161 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1162 | static void fail() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1163 | { |
| 1164 | longjmp(failpt, 1); |
| 1165 | /* NOTREACHED */ |
| 1166 | } |
| 1167 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1168 | static void leave() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1169 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1170 | DBGPRINTF(("LEAVE: leave called!\n")); |
| 1171 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1172 | if (execflg) |
| 1173 | fail(); |
| 1174 | scraphere(); |
| 1175 | freehere(1); |
| 1176 | runtrap(0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1177 | _exit(exstat); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1178 | /* NOTREACHED */ |
| 1179 | } |
| 1180 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1181 | static void warn(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1182 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1183 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1184 | if (*s) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1185 | prs(s); |
| 1186 | exstat = -1; |
| 1187 | } |
| 1188 | prs("\n"); |
| 1189 | if (flag['e']) |
| 1190 | leave(); |
| 1191 | } |
| 1192 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1193 | static void err(s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1194 | char *s; |
| 1195 | { |
| 1196 | warn(s); |
| 1197 | if (flag['n']) |
| 1198 | return; |
| 1199 | if (!interactive) |
| 1200 | leave(); |
| 1201 | if (e.errpt) |
| 1202 | longjmp(e.errpt, 1); |
| 1203 | closeall(); |
| 1204 | e.iop = e.iobase = iostack; |
| 1205 | } |
| 1206 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1207 | static int newenv(f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1208 | int f; |
| 1209 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1210 | REGISTER struct env *ep; |
| 1211 | |
| 1212 | DBGPRINTF(("NEWENV: f=%d (indicates quitenv and return)\n", f)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1213 | |
| 1214 | if (f) { |
| 1215 | quitenv(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1216 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1217 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1218 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1219 | ep = (struct env *) space(sizeof(*ep)); |
| 1220 | if (ep == NULL) { |
| 1221 | while (e.oenv) |
| 1222 | quitenv(); |
| 1223 | fail(); |
| 1224 | } |
| 1225 | *ep = e; |
| 1226 | e.oenv = ep; |
| 1227 | e.errpt = errpt; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1228 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1229 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1232 | static void quitenv() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1233 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1234 | REGISTER struct env *ep; |
| 1235 | REGISTER int fd; |
| 1236 | |
| 1237 | DBGPRINTF(("QUITENV: e.oenv=0x%x\n", e.oenv)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1238 | |
| 1239 | if ((ep = e.oenv) != NULL) { |
| 1240 | fd = e.iofd; |
| 1241 | e = *ep; |
| 1242 | /* should close `'d files */ |
| 1243 | DELETE(ep); |
| 1244 | while (--fd >= e.iofd) |
| 1245 | close(fd); |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | /* |
| 1250 | * Is any character from s1 in s2? |
| 1251 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1252 | static int anys(s1, s2) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1253 | REGISTER char *s1, *s2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1254 | { |
| 1255 | while (*s1) |
| 1256 | if (any(*s1++, s2)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1257 | return (1); |
| 1258 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * Is character c in s? |
| 1263 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1264 | static int any(c, s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1265 | REGISTER int c; |
| 1266 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1267 | { |
| 1268 | while (*s) |
| 1269 | if (*s++ == c) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1270 | return (1); |
| 1271 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1274 | static char *putn(n) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1275 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1276 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1277 | return (itoa(n)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1280 | static char *itoa(n) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1281 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1282 | { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1283 | static char s[20]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1284 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 1285 | snprintf(s, sizeof(s), "%u", n); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1286 | return (s); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1289 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1290 | static void next(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1291 | { |
| 1292 | PUSHIO(afile, f, filechar); |
| 1293 | } |
| 1294 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1295 | static void onintr(s) |
| 1296 | int s; /* ANSI C requires a parameter */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1297 | { |
| 1298 | signal(SIGINT, onintr); |
| 1299 | intr = 1; |
| 1300 | if (interactive) { |
| 1301 | if (inparse) { |
| 1302 | prs("\n"); |
| 1303 | fail(); |
| 1304 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1305 | } else if (heedint) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1306 | execflg = 0; |
| 1307 | leave(); |
| 1308 | } |
| 1309 | } |
| 1310 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1311 | static char *space(n) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1312 | int n; |
| 1313 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1314 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1315 | |
| 1316 | if ((cp = getcell(n)) == 0) |
| 1317 | err("out of string space"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1318 | return (cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1321 | static char *strsave(s, a) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1322 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1323 | int a; |
| 1324 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1325 | REGISTER char *cp, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1326 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1327 | if ((cp = space(strlen(s) + 1)) != NULL) { |
| 1328 | setarea((char *) cp, a); |
| 1329 | for (xp = cp; (*xp++ = *s++) != '\0';); |
| 1330 | return (cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1331 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1332 | return (""); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /* |
| 1336 | * trap handling |
| 1337 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1338 | static void sig(i) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1339 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1340 | { |
| 1341 | trapset = i; |
| 1342 | signal(i, sig); |
| 1343 | } |
| 1344 | |
| 1345 | static void runtrap(i) |
| 1346 | int i; |
| 1347 | { |
| 1348 | char *trapstr; |
| 1349 | |
| 1350 | if ((trapstr = trap[i]) == NULL) |
| 1351 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1352 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1353 | if (i == 0) |
| 1354 | trap[i] = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1355 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1356 | RUN(aword, trapstr, nlchar); |
| 1357 | } |
| 1358 | |
| 1359 | /* -------- var.c -------- */ |
| 1360 | |
| 1361 | /* |
| 1362 | * Find the given name in the dictionary |
| 1363 | * and return its value. If the name was |
| 1364 | * not previously there, enter it now and |
| 1365 | * return a null value. |
| 1366 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1367 | static struct var *lookup(n) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1368 | REGISTER char *n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1369 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1370 | REGISTER struct var *vp; |
| 1371 | REGISTER char *cp; |
| 1372 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1373 | static struct var dummy; |
| 1374 | |
| 1375 | if (isdigit(*n)) { |
| 1376 | dummy.name = n; |
| 1377 | for (c = 0; isdigit(*n) && c < 1000; n++) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1378 | c = c * 10 + *n - '0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1379 | dummy.status = RONLY; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1380 | dummy.value = c <= dolc ? dolv[c] : null; |
| 1381 | return (&dummy); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1382 | } |
| 1383 | for (vp = vlist; vp; vp = vp->next) |
| 1384 | if (eqname(vp->name, n)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1385 | return (vp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1386 | cp = findeq(n); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1387 | vp = (struct var *) space(sizeof(*vp)); |
| 1388 | if (vp == 0 || (vp->name = space((int) (cp - n) + 2)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1389 | dummy.name = dummy.value = ""; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1390 | return (&dummy); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1391 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1392 | for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1393 | if (*cp == 0) |
| 1394 | *cp = '='; |
| 1395 | *++cp = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1396 | setarea((char *) vp, 0); |
| 1397 | setarea((char *) vp->name, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1398 | vp->value = null; |
| 1399 | vp->next = vlist; |
| 1400 | vp->status = GETCELL; |
| 1401 | vlist = vp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1402 | return (vp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | /* |
| 1406 | * give variable at `vp' the value `val'. |
| 1407 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1408 | static void setval(vp, val) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1409 | struct var *vp; |
| 1410 | char *val; |
| 1411 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1412 | nameval(vp, val, (char *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | /* |
| 1416 | * if name is not NULL, it must be |
| 1417 | * a prefix of the space `val', |
| 1418 | * and end with `='. |
| 1419 | * this is all so that exporting |
| 1420 | * values is reasonably painless. |
| 1421 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1422 | static void nameval(vp, val, name) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1423 | REGISTER struct var *vp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1424 | char *val, *name; |
| 1425 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1426 | REGISTER char *cp, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1427 | char *nv; |
| 1428 | int fl; |
| 1429 | |
| 1430 | if (vp->status & RONLY) { |
| 1431 | for (xp = vp->name; *xp && *xp != '=';) |
| 1432 | putc(*xp++, stderr); |
| 1433 | err(" is read-only"); |
| 1434 | return; |
| 1435 | } |
| 1436 | fl = 0; |
| 1437 | if (name == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1438 | xp = space(strlen(vp->name) + strlen(val) + 2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1439 | if (xp == 0) |
| 1440 | return; |
| 1441 | /* make string: name=value */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1442 | setarea((char *) xp, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1443 | name = xp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1444 | for (cp = vp->name; (*xp = *cp++) && *xp != '='; xp++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1445 | if (*xp++ == 0) |
| 1446 | xp[-1] = '='; |
| 1447 | nv = xp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1448 | for (cp = val; (*xp++ = *cp++) != '\0';); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1449 | val = nv; |
| 1450 | fl = GETCELL; |
| 1451 | } |
| 1452 | if (vp->status & GETCELL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1453 | freecell(vp->name); /* form new string `name=value' */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1454 | vp->name = name; |
| 1455 | vp->value = val; |
| 1456 | vp->status |= fl; |
| 1457 | } |
| 1458 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1459 | static void export(vp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1460 | struct var *vp; |
| 1461 | { |
| 1462 | vp->status |= EXPORT; |
| 1463 | } |
| 1464 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1465 | static void ronly(vp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1466 | struct var *vp; |
| 1467 | { |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1468 | if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1469 | vp->status |= RONLY; |
| 1470 | } |
| 1471 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1472 | static int isassign(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1473 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1474 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1475 | DBGPRINTF7(("ISASSIGN: enter, s=%s\n", s)); |
| 1476 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1477 | if (!isalpha((int) *s) && *s != '_') |
| 1478 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1479 | for (; *s != '='; s++) |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1480 | if (*s == 0 || (!isalnum(*s) && *s != '_')) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1481 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1482 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1483 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1486 | static int assign(s, cf) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1487 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1488 | int cf; |
| 1489 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1490 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1491 | struct var *vp; |
| 1492 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1493 | DBGPRINTF7(("ASSIGN: enter, s=%s, cf=%d\n", s, cf)); |
| 1494 | |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1495 | if (!isalpha(*s) && *s != '_') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1496 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1497 | for (cp = s; *cp != '='; cp++) |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1498 | if (*cp == 0 || (!isalnum(*cp) && *cp != '_')) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1499 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1500 | vp = lookup(s); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1501 | nameval(vp, ++cp, cf == COPYV ? (char *) NULL : s); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1502 | if (cf != COPYV) |
| 1503 | vp->status &= ~GETCELL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1504 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1507 | static int checkname(cp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1508 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1509 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1510 | DBGPRINTF7(("CHECKNAME: enter, cp=%s\n", cp)); |
| 1511 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1512 | if (!isalpha(*cp++) && *(cp - 1) != '_') |
| 1513 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1514 | while (*cp) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1515 | if (!isalnum(*cp++) && *(cp - 1) != '_') |
| 1516 | return (0); |
| 1517 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1520 | static void putvlist(f, out) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1521 | REGISTER int f, out; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1522 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1523 | REGISTER struct var *vp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1524 | |
| 1525 | for (vp = vlist; vp; vp = vp->next) |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1526 | if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1527 | if (vp->status & EXPORT) |
| 1528 | write(out, "export ", 7); |
| 1529 | if (vp->status & RONLY) |
| 1530 | write(out, "readonly ", 9); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1531 | write(out, vp->name, (int) (findeq(vp->name) - vp->name)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1532 | write(out, "\n", 1); |
| 1533 | } |
| 1534 | } |
| 1535 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1536 | static int eqname(n1, n2) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1537 | REGISTER char *n1, *n2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1538 | { |
| 1539 | for (; *n1 != '=' && *n1 != 0; n1++) |
| 1540 | if (*n2++ != *n1) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1541 | return (0); |
| 1542 | return (*n2 == 0 || *n2 == '='); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1545 | static char *findeq(cp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1546 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1547 | { |
| 1548 | while (*cp != '\0' && *cp != '=') |
| 1549 | cp++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1550 | return (cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | /* -------- gmatch.c -------- */ |
| 1554 | /* |
| 1555 | * int gmatch(string, pattern) |
| 1556 | * char *string, *pattern; |
| 1557 | * |
| 1558 | * Match a pattern as in sh(1). |
| 1559 | */ |
| 1560 | |
| 1561 | #define CMASK 0377 |
| 1562 | #define QUOTE 0200 |
| 1563 | #define QMASK (CMASK&~QUOTE) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1564 | #define NOT '!' /* might use ^ */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1565 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1566 | static int gmatch(s, p) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1567 | REGISTER char *s, *p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1568 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1569 | REGISTER int sc, pc; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1570 | |
| 1571 | if (s == NULL || p == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1572 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1573 | while ((pc = *p++ & CMASK) != '\0') { |
| 1574 | sc = *s++ & QMASK; |
| 1575 | switch (pc) { |
| 1576 | case '[': |
| 1577 | if ((p = cclass(p, sc)) == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1578 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1579 | break; |
| 1580 | |
| 1581 | case '?': |
| 1582 | if (sc == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1583 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1584 | break; |
| 1585 | |
| 1586 | case '*': |
| 1587 | s--; |
| 1588 | do { |
| 1589 | if (*p == '\0' || gmatch(s, p)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1590 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1591 | } while (*s++ != '\0'); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1592 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1593 | |
| 1594 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1595 | if (sc != (pc & ~QUOTE)) |
| 1596 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1597 | } |
| 1598 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1599 | return (*s == 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1602 | static char *cclass(p, sub) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1603 | REGISTER char *p; |
| 1604 | REGISTER int sub; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1605 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1606 | REGISTER int c, d, not, found; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1607 | |
| 1608 | if ((not = *p == NOT) != 0) |
| 1609 | p++; |
| 1610 | found = not; |
| 1611 | do { |
| 1612 | if (*p == '\0') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1613 | return ((char *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1614 | c = *p & CMASK; |
| 1615 | if (p[1] == '-' && p[2] != ']') { |
| 1616 | d = p[2] & CMASK; |
| 1617 | p++; |
| 1618 | } else |
| 1619 | d = c; |
| 1620 | if (c == sub || (c <= sub && sub <= d)) |
| 1621 | found = !not; |
| 1622 | } while (*++p != ']'); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1623 | return (found ? p + 1 : (char *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | |
| 1627 | /* -------- area.c -------- */ |
| 1628 | |
| 1629 | /* |
| 1630 | * All memory between (char *)areabot and (char *)(areatop+1) is |
| 1631 | * exclusively administered by the area management routines. |
| 1632 | * It is assumed that sbrk() and brk() manipulate the high end. |
| 1633 | */ |
| 1634 | |
| 1635 | #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;}) |
| 1636 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1637 | static void initarea() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1638 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1639 | brkaddr = malloc(AREASIZE); |
| 1640 | brktop = brkaddr + AREASIZE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1641 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1642 | while ((int) sbrk(0) & ALIGN) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1643 | sbrk(1); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1644 | areabot = (struct region *) sbrk(REGSIZE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1645 | |
| 1646 | areabot->next = areabot; |
| 1647 | areabot->area = BUSY; |
| 1648 | areatop = areabot; |
| 1649 | areanxt = areabot; |
| 1650 | } |
| 1651 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1652 | char *getcell(nbytes) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1653 | unsigned nbytes; |
| 1654 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1655 | REGISTER int nregio; |
| 1656 | REGISTER struct region *p, *q; |
| 1657 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1658 | |
| 1659 | if (nbytes == 0) { |
| 1660 | puts("getcell(0)"); |
| 1661 | abort(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1662 | } |
| 1663 | /* silly and defeats the algorithm */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1664 | /* |
| 1665 | * round upwards and add administration area |
| 1666 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1667 | nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1668 | for (p = areanxt;;) { |
| 1669 | if (p->area > areanum) { |
| 1670 | /* |
| 1671 | * merge free cells |
| 1672 | */ |
| 1673 | while ((q = p->next)->area > areanum && q != areanxt) |
| 1674 | p->next = q->next; |
| 1675 | /* |
| 1676 | * exit loop if cell big enough |
| 1677 | */ |
| 1678 | if (q >= p + nregio) |
| 1679 | goto found; |
| 1680 | } |
| 1681 | p = p->next; |
| 1682 | if (p == areanxt) |
| 1683 | break; |
| 1684 | } |
| 1685 | i = nregio >= GROWBY ? nregio : GROWBY; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1686 | p = (struct region *) sbrk(i * REGSIZE); |
| 1687 | if (p == (struct region *) -1) |
| 1688 | return ((char *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1689 | p--; |
| 1690 | if (p != areatop) { |
| 1691 | puts("not contig"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1692 | abort(); /* allocated areas are contiguous */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1693 | } |
| 1694 | q = p + i; |
| 1695 | p->next = q; |
| 1696 | p->area = FREE; |
| 1697 | q->next = areabot; |
| 1698 | q->area = BUSY; |
| 1699 | areatop = q; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1700 | found: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1701 | /* |
| 1702 | * we found a FREE area big enough, pointed to by 'p', and up to 'q' |
| 1703 | */ |
| 1704 | areanxt = p + nregio; |
| 1705 | if (areanxt < q) { |
| 1706 | /* |
| 1707 | * split into requested area and rest |
| 1708 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1709 | if (areanxt + 1 > q) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1710 | puts("OOM"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1711 | abort(); /* insufficient space left for admin */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1712 | } |
| 1713 | areanxt->next = q; |
| 1714 | areanxt->area = FREE; |
| 1715 | p->next = areanxt; |
| 1716 | } |
| 1717 | p->area = areanum; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1718 | return ((char *) (p + 1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1721 | static void freecell(cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1722 | char *cp; |
| 1723 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1724 | REGISTER struct region *p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1725 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1726 | if ((p = (struct region *) cp) != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1727 | p--; |
| 1728 | if (p < areanxt) |
| 1729 | areanxt = p; |
| 1730 | p->area = FREE; |
| 1731 | } |
| 1732 | } |
| 1733 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1734 | static void freearea(a) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1735 | REGISTER int a; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1736 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1737 | REGISTER struct region *p, *top; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1738 | |
| 1739 | top = areatop; |
| 1740 | for (p = areabot; p != top; p = p->next) |
| 1741 | if (p->area >= a) |
| 1742 | p->area = FREE; |
| 1743 | } |
| 1744 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1745 | static void setarea(cp, a) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1746 | char *cp; |
| 1747 | int a; |
| 1748 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1749 | REGISTER struct region *p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1750 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1751 | if ((p = (struct region *) cp) != NULL) |
| 1752 | (p - 1)->area = a; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1755 | int getarea(cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1756 | char *cp; |
| 1757 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1758 | return ((struct region *) cp - 1)->area; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1761 | static void garbage() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1762 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1763 | REGISTER struct region *p, *q, *top; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1764 | |
| 1765 | top = areatop; |
| 1766 | for (p = areabot; p != top; p = p->next) { |
| 1767 | if (p->area > areanum) { |
| 1768 | while ((q = p->next)->area > areanum) |
| 1769 | p->next = q->next; |
| 1770 | areanxt = p; |
| 1771 | } |
| 1772 | } |
| 1773 | #ifdef SHRINKBY |
| 1774 | if (areatop >= q + SHRINKBY && q->area > areanum) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1775 | brk((char *) (q + 1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1776 | q->next = areabot; |
| 1777 | q->area = BUSY; |
| 1778 | areatop = q; |
| 1779 | } |
| 1780 | #endif |
| 1781 | } |
| 1782 | |
| 1783 | /* -------- csyn.c -------- */ |
| 1784 | /* |
| 1785 | * shell: syntax (C version) |
| 1786 | */ |
| 1787 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1788 | int yyparse() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1789 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1790 | DBGPRINTF7(("YYPARSE: enter...\n")); |
| 1791 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1792 | startl = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1793 | peeksym = 0; |
| 1794 | yynerrs = 0; |
| 1795 | outtree = c_list(); |
| 1796 | musthave('\n', 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1797 | return (yynerrs != 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1800 | static struct op *pipeline(cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1801 | int cf; |
| 1802 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1803 | REGISTER struct op *t, *p; |
| 1804 | REGISTER int c; |
| 1805 | |
| 1806 | DBGPRINTF7(("PIPELINE: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1807 | |
| 1808 | t = command(cf); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1809 | |
| 1810 | DBGPRINTF9(("PIPELINE: t=0x%x\n", t)); |
| 1811 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1812 | if (t != NULL) { |
| 1813 | while ((c = yylex(0)) == '|') { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1814 | if ((p = command(CONTIN)) == NULL) { |
| 1815 | DBGPRINTF8(("PIPELINE: error!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1816 | SYNTAXERR; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1817 | } |
| 1818 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1819 | if (t->type != TPAREN && t->type != TCOM) { |
| 1820 | /* shell statement */ |
| 1821 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 1822 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1823 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1824 | t = block(TPIPE, t, p, NOWORDS); |
| 1825 | } |
| 1826 | peeksym = c; |
| 1827 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1828 | |
| 1829 | DBGPRINTF7(("PIPELINE: returning t=0x%x\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1830 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1833 | static struct op *andor() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1834 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1835 | REGISTER struct op *t, *p; |
| 1836 | REGISTER int c; |
| 1837 | |
| 1838 | DBGPRINTF7(("ANDOR: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1839 | |
| 1840 | t = pipeline(0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1841 | |
| 1842 | DBGPRINTF9(("ANDOR: t=0x%x\n", t)); |
| 1843 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1844 | if (t != NULL) { |
| 1845 | while ((c = yylex(0)) == LOGAND || c == LOGOR) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1846 | if ((p = pipeline(CONTIN)) == NULL) { |
| 1847 | DBGPRINTF8(("ANDOR: error!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1848 | SYNTAXERR; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1849 | } |
| 1850 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1851 | t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1852 | } /* WHILE */ |
| 1853 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1854 | peeksym = c; |
| 1855 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1856 | |
| 1857 | DBGPRINTF7(("ANDOR: returning t=0x%x\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1858 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1861 | static struct op *c_list() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1862 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1863 | REGISTER struct op *t, *p; |
| 1864 | REGISTER int c; |
| 1865 | |
| 1866 | DBGPRINTF7(("C_LIST: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1867 | |
| 1868 | t = andor(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1869 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1870 | if (t != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1871 | if ((peeksym = yylex(0)) == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1872 | t = block(TASYNC, t, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1873 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1874 | while ((c = yylex(0)) == ';' || c == '&' |
| 1875 | || (multiline && c == '\n')) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1876 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1877 | if ((p = andor()) == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1878 | return (t); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1879 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1880 | if ((peeksym = yylex(0)) == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1881 | p = block(TASYNC, p, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1882 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1883 | t = list(t, p); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1884 | } /* WHILE */ |
| 1885 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1886 | peeksym = c; |
| 1887 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1888 | /* IF */ |
| 1889 | DBGPRINTF7(("C_LIST: returning t=0x%x\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1890 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1893 | static int synio(cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1894 | int cf; |
| 1895 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1896 | REGISTER struct ioword *iop; |
| 1897 | REGISTER int i; |
| 1898 | REGISTER int c; |
| 1899 | |
| 1900 | DBGPRINTF7(("SYNIO: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1901 | |
| 1902 | if ((c = yylex(cf)) != '<' && c != '>') { |
| 1903 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1904 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1905 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1906 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1907 | i = yylval.i; |
| 1908 | musthave(WORD, 0); |
| 1909 | iop = io(iounit, i, yylval.cp); |
| 1910 | iounit = IODEFAULT; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1911 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1912 | if (i & IOHERE) |
| 1913 | markhere(yylval.cp, iop); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1914 | |
| 1915 | DBGPRINTF7(("SYNIO: returning 1\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1916 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1919 | static void musthave(c, cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1920 | int c, cf; |
| 1921 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1922 | if ((peeksym = yylex(cf)) != c) { |
| 1923 | DBGPRINTF7(("MUSTHAVE: error!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1924 | SYNTAXERR; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1925 | } |
| 1926 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1927 | peeksym = 0; |
| 1928 | } |
| 1929 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1930 | static struct op *simple() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1931 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1932 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1933 | |
| 1934 | t = NULL; |
| 1935 | for (;;) { |
| 1936 | switch (peeksym = yylex(0)) { |
| 1937 | case '<': |
| 1938 | case '>': |
| 1939 | (void) synio(0); |
| 1940 | break; |
| 1941 | |
| 1942 | case WORD: |
| 1943 | if (t == NULL) { |
| 1944 | t = newtp(); |
| 1945 | t->type = TCOM; |
| 1946 | } |
| 1947 | peeksym = 0; |
| 1948 | word(yylval.cp); |
| 1949 | break; |
| 1950 | |
| 1951 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1952 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1953 | } |
| 1954 | } |
| 1955 | } |
| 1956 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1957 | static struct op *nested(type, mark) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1958 | int type, mark; |
| 1959 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1960 | REGISTER struct op *t; |
| 1961 | |
| 1962 | DBGPRINTF3(("NESTED: enter, type=%d, mark=%d\n", type, mark)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1963 | |
| 1964 | multiline++; |
| 1965 | t = c_list(); |
| 1966 | musthave(mark, 0); |
| 1967 | multiline--; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1968 | return (block(type, t, NOBLOCK, NOWORDS)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1971 | static struct op *command(cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1972 | int cf; |
| 1973 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1974 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1975 | struct wdblock *iosave; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1976 | REGISTER int c; |
| 1977 | |
| 1978 | DBGPRINTF(("COMMAND: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1979 | |
| 1980 | iosave = iolist; |
| 1981 | iolist = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1982 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1983 | if (multiline) |
| 1984 | cf |= CONTIN; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1985 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1986 | while (synio(cf)) |
| 1987 | cf = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 1988 | |
| 1989 | c = yylex(cf); |
| 1990 | |
| 1991 | switch (c) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1992 | default: |
| 1993 | peeksym = c; |
| 1994 | if ((t = simple()) == NULL) { |
| 1995 | if (iolist == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1996 | return ((struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1997 | t = newtp(); |
| 1998 | t->type = TCOM; |
| 1999 | } |
| 2000 | break; |
| 2001 | |
| 2002 | case '(': |
| 2003 | t = nested(TPAREN, ')'); |
| 2004 | break; |
| 2005 | |
| 2006 | case '{': |
| 2007 | t = nested(TBRACE, '}'); |
| 2008 | break; |
| 2009 | |
| 2010 | case FOR: |
| 2011 | t = newtp(); |
| 2012 | t->type = TFOR; |
| 2013 | musthave(WORD, 0); |
| 2014 | startl = 1; |
| 2015 | t->str = yylval.cp; |
| 2016 | multiline++; |
| 2017 | t->words = wordlist(); |
| 2018 | if ((c = yylex(0)) != '\n' && c != ';') |
| 2019 | peeksym = c; |
| 2020 | t->left = dogroup(0); |
| 2021 | multiline--; |
| 2022 | break; |
| 2023 | |
| 2024 | case WHILE: |
| 2025 | case UNTIL: |
| 2026 | multiline++; |
| 2027 | t = newtp(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2028 | t->type = c == WHILE ? TWHILE : TUNTIL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2029 | t->left = c_list(); |
| 2030 | t->right = dogroup(1); |
| 2031 | t->words = NULL; |
| 2032 | multiline--; |
| 2033 | break; |
| 2034 | |
| 2035 | case CASE: |
| 2036 | t = newtp(); |
| 2037 | t->type = TCASE; |
| 2038 | musthave(WORD, 0); |
| 2039 | t->str = yylval.cp; |
| 2040 | startl++; |
| 2041 | multiline++; |
| 2042 | musthave(IN, CONTIN); |
| 2043 | startl++; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2044 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2045 | t->left = caselist(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2046 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2047 | musthave(ESAC, 0); |
| 2048 | multiline--; |
| 2049 | break; |
| 2050 | |
| 2051 | case IF: |
| 2052 | multiline++; |
| 2053 | t = newtp(); |
| 2054 | t->type = TIF; |
| 2055 | t->left = c_list(); |
| 2056 | t->right = thenpart(); |
| 2057 | musthave(FI, 0); |
| 2058 | multiline--; |
| 2059 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2060 | |
| 2061 | case DOT: |
| 2062 | t = newtp(); |
| 2063 | t->type = TDOT; |
| 2064 | |
| 2065 | musthave(WORD, 0); /* gets name of file */ |
| 2066 | DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp)); |
| 2067 | |
| 2068 | word(yylval.cp); /* add word to wdlist */ |
| 2069 | word(NOWORD); /* terminate wdlist */ |
| 2070 | t->words = copyw(); /* dup wdlist */ |
| 2071 | break; |
| 2072 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2073 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2074 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2075 | while (synio(0)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2076 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2077 | t = namelist(t); |
| 2078 | iolist = iosave; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2079 | |
| 2080 | DBGPRINTF(("COMMAND: returning 0x%x\n", t)); |
| 2081 | |
| 2082 | return (t); |
| 2083 | } |
| 2084 | |
| 2085 | static struct op *dowholefile(type, mark) |
| 2086 | int type; |
| 2087 | int mark; |
| 2088 | { |
| 2089 | REGISTER struct op *t; |
| 2090 | |
| 2091 | DBGPRINTF(("DOWHOLEFILE: enter, type=%d, mark=%d\n", type, mark)); |
| 2092 | |
| 2093 | multiline++; |
| 2094 | t = c_list(); |
| 2095 | multiline--; |
| 2096 | t = block(type, t, NOBLOCK, NOWORDS); |
| 2097 | DBGPRINTF(("DOWHOLEFILE: return t=0x%x\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2098 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2101 | static struct op *dogroup(onlydone) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2102 | int onlydone; |
| 2103 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2104 | REGISTER int c; |
| 2105 | REGISTER struct op *mylist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2106 | |
| 2107 | c = yylex(CONTIN); |
| 2108 | if (c == DONE && onlydone) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2109 | return ((struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2110 | if (c != DO) |
| 2111 | SYNTAXERR; |
| 2112 | mylist = c_list(); |
| 2113 | musthave(DONE, 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2114 | return (mylist); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2117 | static struct op *thenpart() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2118 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2119 | REGISTER int c; |
| 2120 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2121 | |
| 2122 | if ((c = yylex(0)) != THEN) { |
| 2123 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2124 | return ((struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2125 | } |
| 2126 | t = newtp(); |
| 2127 | t->type = 0; |
| 2128 | t->left = c_list(); |
| 2129 | if (t->left == NULL) |
| 2130 | SYNTAXERR; |
| 2131 | t->right = elsepart(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2132 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2135 | static struct op *elsepart() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2136 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2137 | REGISTER int c; |
| 2138 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2139 | |
| 2140 | switch (c = yylex(0)) { |
| 2141 | case ELSE: |
| 2142 | if ((t = c_list()) == NULL) |
| 2143 | SYNTAXERR; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2144 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2145 | |
| 2146 | case ELIF: |
| 2147 | t = newtp(); |
| 2148 | t->type = TELIF; |
| 2149 | t->left = c_list(); |
| 2150 | t->right = thenpart(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2151 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2152 | |
| 2153 | default: |
| 2154 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2155 | return ((struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2156 | } |
| 2157 | } |
| 2158 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2159 | static struct op *caselist() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2160 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2161 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2162 | |
| 2163 | t = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2164 | while ((peeksym = yylex(CONTIN)) != ESAC) { |
| 2165 | DBGPRINTF(("CASELIST, doing yylex, peeksym=%d\n", peeksym)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2166 | t = list(t, casepart()); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2167 | } |
| 2168 | |
| 2169 | DBGPRINTF(("CASELIST, returning t=0x%x\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2170 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2173 | static struct op *casepart() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2174 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2175 | REGISTER struct op *t; |
| 2176 | |
| 2177 | DBGPRINTF7(("CASEPART: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2178 | |
| 2179 | t = newtp(); |
| 2180 | t->type = TPAT; |
| 2181 | t->words = pattern(); |
| 2182 | musthave(')', 0); |
| 2183 | t->left = c_list(); |
| 2184 | if ((peeksym = yylex(CONTIN)) != ESAC) |
| 2185 | musthave(BREAK, CONTIN); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2186 | |
| 2187 | DBGPRINTF7(("CASEPART: made newtp(TPAT, t=0x%x)\n", t)); |
| 2188 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2189 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2192 | static char **pattern() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2193 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2194 | REGISTER int c, cf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2195 | |
| 2196 | cf = CONTIN; |
| 2197 | do { |
| 2198 | musthave(WORD, cf); |
| 2199 | word(yylval.cp); |
| 2200 | cf = 0; |
| 2201 | } while ((c = yylex(0)) == '|'); |
| 2202 | peeksym = c; |
| 2203 | word(NOWORD); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2204 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2205 | return (copyw()); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2208 | static char **wordlist() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2209 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2210 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2211 | |
| 2212 | if ((c = yylex(0)) != IN) { |
| 2213 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2214 | return ((char **) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2215 | } |
| 2216 | startl = 0; |
| 2217 | while ((c = yylex(0)) == WORD) |
| 2218 | word(yylval.cp); |
| 2219 | word(NOWORD); |
| 2220 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2221 | return (copyw()); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | /* |
| 2225 | * supporting functions |
| 2226 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2227 | static struct op *list(t1, t2) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2228 | REGISTER struct op *t1, *t2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2229 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2230 | DBGPRINTF7(("LIST: enter, t1=0x%x, t2=0x%x\n", t1, t2)); |
| 2231 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2232 | if (t1 == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2233 | return (t2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2234 | if (t2 == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2235 | return (t1); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2236 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2237 | return (block(TLIST, t1, t2, NOWORDS)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2240 | static struct op *block(type, t1, t2, wp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2241 | int type; |
| 2242 | struct op *t1, *t2; |
| 2243 | char **wp; |
| 2244 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2245 | REGISTER struct op *t; |
| 2246 | |
| 2247 | DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type])); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2248 | |
| 2249 | t = newtp(); |
| 2250 | t->type = type; |
| 2251 | t->left = t1; |
| 2252 | t->right = t2; |
| 2253 | t->words = wp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2254 | |
| 2255 | DBGPRINTF7(("BLOCK: inserted 0x%x between 0x%x and 0x%x\n", t, t1, |
| 2256 | t2)); |
| 2257 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2258 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2261 | /* See if given string is a shell multiline (FOR, IF, etc) */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2262 | static int rlookup(n) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2263 | REGISTER char *n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2264 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2265 | REGISTER struct res *rp; |
| 2266 | |
| 2267 | DBGPRINTF7(("RLOOKUP: enter, n is %s\n", n)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2268 | |
| 2269 | for (rp = restab; rp->r_name; rp++) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2270 | if (strcmp(rp->r_name, n) == 0) { |
| 2271 | DBGPRINTF7(("RLOOKUP: match, returning %d\n", rp->r_val)); |
| 2272 | return (rp->r_val); /* Return numeric code for shell multiline */ |
| 2273 | } |
| 2274 | |
| 2275 | DBGPRINTF7(("RLOOKUP: NO match, returning 0\n")); |
| 2276 | return (0); /* Not a shell multiline */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2279 | static struct op *newtp() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2280 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2281 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2282 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2283 | t = (struct op *) tree(sizeof(*t)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2284 | t->type = 0; |
| 2285 | t->words = NULL; |
| 2286 | t->ioact = NULL; |
| 2287 | t->left = NULL; |
| 2288 | t->right = NULL; |
| 2289 | t->str = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2290 | |
| 2291 | DBGPRINTF3(("NEWTP: allocated 0x%x\n", t)); |
| 2292 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2293 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2296 | static struct op *namelist(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2297 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2298 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2299 | |
| 2300 | DBGPRINTF7(("NAMELIST: enter, t=0x%x, type %s, iolist=0x%x\n", t, |
| 2301 | T_CMD_NAMES[t->type], iolist)); |
| 2302 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2303 | if (iolist) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2304 | iolist = addword((char *) NULL, iolist); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2305 | t->ioact = copyio(); |
| 2306 | } else |
| 2307 | t->ioact = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2308 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2309 | if (t->type != TCOM) { |
| 2310 | if (t->type != TPAREN && t->ioact != NULL) { |
| 2311 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 2312 | t->ioact = t->left->ioact; |
| 2313 | t->left->ioact = NULL; |
| 2314 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2315 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2316 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2317 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2318 | word(NOWORD); |
| 2319 | t->words = copyw(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2320 | |
| 2321 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2322 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2325 | static char **copyw() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2326 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2327 | REGISTER char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2328 | |
| 2329 | wd = getwords(wdlist); |
| 2330 | wdlist = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2331 | return (wd); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2334 | static void word(cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2335 | char *cp; |
| 2336 | { |
| 2337 | wdlist = addword(cp, wdlist); |
| 2338 | } |
| 2339 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2340 | static struct ioword **copyio() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2341 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2342 | REGISTER struct ioword **iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2343 | |
| 2344 | iop = (struct ioword **) getwords(iolist); |
| 2345 | iolist = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2346 | return (iop); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2349 | static struct ioword *io(u, f, cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2350 | int u; |
| 2351 | int f; |
| 2352 | char *cp; |
| 2353 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2354 | REGISTER struct ioword *iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2355 | |
| 2356 | iop = (struct ioword *) tree(sizeof(*iop)); |
| 2357 | iop->io_unit = u; |
| 2358 | iop->io_flag = f; |
| 2359 | iop->io_name = cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2360 | iolist = addword((char *) iop, iolist); |
| 2361 | return (iop); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2364 | static void zzerr() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2365 | { |
| 2366 | yyerror("syntax error"); |
| 2367 | } |
| 2368 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2369 | static void yyerror(s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2370 | char *s; |
| 2371 | { |
| 2372 | yynerrs++; |
| 2373 | if (interactive && e.iop <= iostack) { |
| 2374 | multiline = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2375 | while (eofc() == 0 && yylex(0) != '\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2376 | } |
| 2377 | err(s); |
| 2378 | fail(); |
| 2379 | } |
| 2380 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2381 | static int yylex(cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2382 | int cf; |
| 2383 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2384 | REGISTER int c, c1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2385 | int atstart; |
| 2386 | |
| 2387 | if ((c = peeksym) > 0) { |
| 2388 | peeksym = 0; |
| 2389 | if (c == '\n') |
| 2390 | startl = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2391 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2392 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2393 | |
| 2394 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2395 | nlseen = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2396 | atstart = startl; |
| 2397 | startl = 0; |
| 2398 | yylval.i = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2399 | e.linep = line; |
| 2400 | |
| 2401 | /* MALAMO */ |
| 2402 | line[LINELIM - 1] = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2403 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2404 | loop: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2405 | while ((c = my_getc(0)) == ' ' || c == '\t') /* Skip whitespace */ |
| 2406 | ; |
| 2407 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2408 | switch (c) { |
| 2409 | default: |
| 2410 | if (any(c, "0123456789")) { |
| 2411 | unget(c1 = my_getc(0)); |
| 2412 | if (c1 == '<' || c1 == '>') { |
| 2413 | iounit = c - '0'; |
| 2414 | goto loop; |
| 2415 | } |
| 2416 | *e.linep++ = c; |
| 2417 | c = c1; |
| 2418 | } |
| 2419 | break; |
| 2420 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2421 | case '#': /* Comment, skip to next newline or End-of-string */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2422 | while ((c = my_getc(0)) != 0 && c != '\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2423 | unget(c); |
| 2424 | goto loop; |
| 2425 | |
| 2426 | case 0: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2427 | DBGPRINTF5(("YYLEX: return 0, c=%d\n", c)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2428 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2429 | |
| 2430 | case '$': |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2431 | DBGPRINTF9(("YYLEX: found $\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2432 | *e.linep++ = c; |
| 2433 | if ((c = my_getc(0)) == '{') { |
| 2434 | if ((c = collect(c, '}')) != '\0') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2435 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2436 | goto pack; |
| 2437 | } |
| 2438 | break; |
| 2439 | |
| 2440 | case '`': |
| 2441 | case '\'': |
| 2442 | case '"': |
| 2443 | if ((c = collect(c, c)) != '\0') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2444 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2445 | goto pack; |
| 2446 | |
| 2447 | case '|': |
| 2448 | case '&': |
| 2449 | case ';': |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2450 | startl = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2451 | /* If more chars process them, else return NULL char */ |
| 2452 | if ((c1 = dual(c)) != '\0') |
| 2453 | return (c1); |
| 2454 | else |
| 2455 | return (c); |
| 2456 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2457 | case '^': |
| 2458 | startl = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2459 | return ('|'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2460 | case '>': |
| 2461 | case '<': |
| 2462 | diag(c); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2463 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2464 | |
| 2465 | case '\n': |
| 2466 | nlseen++; |
| 2467 | gethere(); |
| 2468 | startl = 1; |
| 2469 | if (multiline || cf & CONTIN) { |
| 2470 | if (interactive && e.iop <= iostack) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 2471 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2472 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2473 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2474 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2475 | #endif |
| 2476 | } |
| 2477 | if (cf & CONTIN) |
| 2478 | goto loop; |
| 2479 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2480 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2481 | |
| 2482 | case '(': |
| 2483 | case ')': |
| 2484 | startl = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2485 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | unget(c); |
| 2489 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2490 | pack: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2491 | while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n")) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2492 | if (e.linep >= elinep) |
| 2493 | err("word too long"); |
| 2494 | else |
| 2495 | *e.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2496 | }; |
| 2497 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2498 | unget(c); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2499 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2500 | if (any(c, "\"'`$")) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2501 | goto loop; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2502 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2503 | *e.linep++ = '\0'; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2504 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2505 | if (atstart && (c = rlookup(line)) != 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2506 | startl = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2507 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2508 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2509 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2510 | yylval.cp = strsave(line, areanum); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2511 | return (WORD); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2514 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2515 | static int collect(c, c1) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2516 | REGISTER int c, c1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2517 | { |
| 2518 | char s[2]; |
| 2519 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2520 | DBGPRINTF8(("COLLECT: enter, c=%d, c1=%d\n", c, c1)); |
| 2521 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2522 | *e.linep++ = c; |
| 2523 | while ((c = my_getc(c1)) != c1) { |
| 2524 | if (c == 0) { |
| 2525 | unget(c); |
| 2526 | s[0] = c1; |
| 2527 | s[1] = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2528 | prs("no closing "); |
| 2529 | yyerror(s); |
| 2530 | return (YYERRCODE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2531 | } |
| 2532 | if (interactive && c == '\n' && e.iop <= iostack) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 2533 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2534 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2535 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2536 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2537 | #endif |
| 2538 | } |
| 2539 | *e.linep++ = c; |
| 2540 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2541 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2542 | *e.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2543 | |
| 2544 | DBGPRINTF8(("COLLECT: return 0, line is %s\n", line)); |
| 2545 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2546 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2549 | /* "multiline commands" helper func */ |
| 2550 | /* see if next 2 chars form a shell multiline */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2551 | static int dual(c) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2552 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2553 | { |
| 2554 | char s[3]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2555 | REGISTER char *cp = s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2556 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2557 | DBGPRINTF8(("DUAL: enter, c=%d\n", c)); |
| 2558 | |
| 2559 | *cp++ = c; /* c is the given "peek" char */ |
| 2560 | *cp++ = my_getc(0); /* get next char of input */ |
| 2561 | *cp = 0; /* add EOS marker */ |
| 2562 | |
| 2563 | c = rlookup(s); /* see if 2 chars form a shell multiline */ |
| 2564 | if (c == 0) |
| 2565 | unget(*--cp); /* String is not a shell multiline, put peek char back */ |
| 2566 | |
| 2567 | return (c); /* String is multiline, return numeric multiline (restab) code */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2570 | static void diag(ec) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2571 | REGISTER int ec; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2572 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2573 | REGISTER int c; |
| 2574 | |
| 2575 | DBGPRINTF8(("DIAG: enter, ec=%d\n", ec)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2576 | |
| 2577 | c = my_getc(0); |
| 2578 | if (c == '>' || c == '<') { |
| 2579 | if (c != ec) |
| 2580 | zzerr(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2581 | yylval.i = ec == '>' ? IOWRITE | IOCAT : IOHERE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2582 | c = my_getc(0); |
| 2583 | } else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2584 | yylval.i = ec == '>' ? IOWRITE : IOREAD; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2585 | if (c != '&' || yylval.i == IOHERE) |
| 2586 | unget(c); |
| 2587 | else |
| 2588 | yylval.i |= IODUP; |
| 2589 | } |
| 2590 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2591 | static char *tree(size) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2592 | unsigned size; |
| 2593 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2594 | REGISTER char *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2595 | |
| 2596 | if ((t = getcell(size)) == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2597 | DBGPRINTF2(("TREE: getcell(%d) failed!\n", size)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2598 | prs("command line too complicated\n"); |
| 2599 | fail(); |
| 2600 | /* NOTREACHED */ |
| 2601 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2602 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
| 2605 | /* VARARGS1 */ |
| 2606 | /* ARGSUSED */ |
| 2607 | |
| 2608 | /* -------- exec.c -------- */ |
| 2609 | |
| 2610 | /* |
| 2611 | * execute tree |
| 2612 | */ |
| 2613 | |
| 2614 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2615 | static int execute(t, pin, pout, act) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2616 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2617 | int *pin, *pout; |
| 2618 | int act; |
| 2619 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2620 | REGISTER struct op *t1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2621 | volatile int i, rv, a; |
| 2622 | char *cp, **wp, **wp2; |
| 2623 | struct var *vp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2624 | struct op *outtree_save; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2625 | struct brkcon bc; |
| 2626 | |
| 2627 | #if __GNUC__ |
| 2628 | /* Avoid longjmp clobbering */ |
| 2629 | (void) ℘ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2630 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2631 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2632 | if (t == NULL) { |
| 2633 | DBGPRINTF4(("EXECUTE: enter, t==null, returning.\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2634 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2635 | } |
| 2636 | |
| 2637 | DBGPRINTF(("EXECUTE: t=0x%x, t->type=%d (%s), t->words is %s\n", t, |
| 2638 | t->type, T_CMD_NAMES[t->type], |
| 2639 | ((t->words == NULL) ? "NULL" : t->words[0]))); |
| 2640 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2641 | rv = 0; |
| 2642 | a = areanum++; |
| 2643 | wp = (wp2 = t->words) != NULL |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2644 | ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY) |
| 2645 | : NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2646 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2647 | /* Hard to know how many words there are, be careful of garbage pointer values */ |
| 2648 | /* They are likely to cause "PCI bus fault" errors */ |
| 2649 | #if 0 |
| 2650 | DBGPRINTF(("EXECUTE: t->left=0x%x, t->right=0x%x, t->words[1] is %s\n", |
| 2651 | t->left, t->right, |
| 2652 | ((t->words[1] == NULL) ? "NULL" : t->words[1]))); |
| 2653 | DBGPRINTF7(("EXECUTE: t->words[2] is %s, t->words[3] is %s\n", |
| 2654 | ((t->words[2] == NULL) ? "NULL" : t->words[2]), |
| 2655 | ((t->words[3] == NULL) ? "NULL" : t->words[3]))); |
| 2656 | #endif |
| 2657 | |
| 2658 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2659 | switch (t->type) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2660 | case TDOT: |
| 2661 | DBGPRINTF3(("EXECUTE: TDOT\n")); |
| 2662 | |
| 2663 | outtree_save = outtree; |
| 2664 | |
| 2665 | newfile(evalstr(t->words[0], DOALL)); |
| 2666 | |
| 2667 | t->left = dowholefile(TLIST, 0); |
| 2668 | t->right = NULL; |
| 2669 | |
| 2670 | outtree = outtree_save; |
| 2671 | |
| 2672 | if (t->left) |
| 2673 | rv = execute(t->left, pin, pout, 0); |
| 2674 | if (t->right) |
| 2675 | rv = execute(t->right, pin, pout, 0); |
| 2676 | break; |
| 2677 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2678 | case TPAREN: |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 2679 | rv = execute(t->left, pin, pout, 0); |
| 2680 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2681 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2682 | case TCOM: |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 2683 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2684 | rv = forkexec(t, pin, pout, act, wp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2685 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2686 | break; |
| 2687 | |
| 2688 | case TPIPE: |
| 2689 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2690 | int pv[2]; |
| 2691 | |
| 2692 | if ((rv = openpipe(pv)) < 0) |
| 2693 | break; |
| 2694 | pv[0] = remap(pv[0]); |
| 2695 | pv[1] = remap(pv[1]); |
| 2696 | (void) execute(t->left, pin, pv, 0); |
| 2697 | rv = execute(t->right, pv, pout, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2698 | } |
| 2699 | break; |
| 2700 | |
| 2701 | case TLIST: |
| 2702 | (void) execute(t->left, pin, pout, 0); |
| 2703 | rv = execute(t->right, pin, pout, 0); |
| 2704 | break; |
| 2705 | |
| 2706 | case TASYNC: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2707 | { |
| 2708 | int hinteractive = interactive; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2709 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2710 | DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n")); |
| 2711 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2712 | i = vfork(); |
| 2713 | if (i != 0) { |
| 2714 | interactive = hinteractive; |
| 2715 | if (i != -1) { |
| 2716 | setval(lookup("!"), putn(i)); |
| 2717 | if (pin != NULL) |
| 2718 | closepipe(pin); |
| 2719 | if (interactive) { |
| 2720 | prs(putn(i)); |
| 2721 | prs("\n"); |
| 2722 | } |
| 2723 | } else |
| 2724 | rv = -1; |
| 2725 | setstatus(rv); |
| 2726 | } else { |
| 2727 | signal(SIGINT, SIG_IGN); |
| 2728 | signal(SIGQUIT, SIG_IGN); |
| 2729 | if (interactive) |
| 2730 | signal(SIGTERM, SIG_DFL); |
| 2731 | interactive = 0; |
| 2732 | if (pin == NULL) { |
| 2733 | close(0); |
| 2734 | open("/dev/null", 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2735 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2736 | _exit(execute(t->left, pin, pout, FEXEC)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2737 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2738 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2739 | break; |
| 2740 | |
| 2741 | case TOR: |
| 2742 | case TAND: |
| 2743 | rv = execute(t->left, pin, pout, 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2744 | if ((t1 = t->right) != NULL && (rv == 0) == (t->type == TAND)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2745 | rv = execute(t1, pin, pout, 0); |
| 2746 | break; |
| 2747 | |
| 2748 | case TFOR: |
| 2749 | if (wp == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2750 | wp = dolv + 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2751 | if ((i = dolc) < 0) |
| 2752 | i = 0; |
| 2753 | } else { |
| 2754 | i = -1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2755 | while (*wp++ != NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2756 | } |
| 2757 | vp = lookup(t->str); |
| 2758 | while (setjmp(bc.brkpt)) |
| 2759 | if (isbreak) |
| 2760 | goto broken; |
| 2761 | brkset(&bc); |
| 2762 | for (t1 = t->left; i-- && *wp != NULL;) { |
| 2763 | setval(vp, *wp++); |
| 2764 | rv = execute(t1, pin, pout, 0); |
| 2765 | } |
| 2766 | brklist = brklist->nextlev; |
| 2767 | break; |
| 2768 | |
| 2769 | case TWHILE: |
| 2770 | case TUNTIL: |
| 2771 | while (setjmp(bc.brkpt)) |
| 2772 | if (isbreak) |
| 2773 | goto broken; |
| 2774 | brkset(&bc); |
| 2775 | t1 = t->left; |
| 2776 | while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE)) |
| 2777 | rv = execute(t->right, pin, pout, 0); |
| 2778 | brklist = brklist->nextlev; |
| 2779 | break; |
| 2780 | |
| 2781 | case TIF: |
| 2782 | case TELIF: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2783 | if (t->right != NULL) { |
| 2784 | rv = !execute(t->left, pin, pout, 0) ? |
| 2785 | execute(t->right->left, pin, pout, 0) : |
| 2786 | execute(t->right->right, pin, pout, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2787 | } |
| 2788 | break; |
| 2789 | |
| 2790 | case TCASE: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2791 | if ((cp = evalstr(t->str, DOSUB | DOTRIM)) == 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2792 | cp = ""; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2793 | |
| 2794 | DBGPRINTF7(("EXECUTE: TCASE, t->str is %s, cp is %s\n", |
| 2795 | ((t->str == NULL) ? "NULL" : t->str), |
| 2796 | ((cp == NULL) ? "NULL" : cp))); |
| 2797 | |
| 2798 | if ((t1 = findcase(t->left, cp)) != NULL) { |
| 2799 | DBGPRINTF7(("EXECUTE: TCASE, calling execute(t=0x%x, t1=0x%x)...\n", t, t1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2800 | rv = execute(t1, pin, pout, 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2801 | DBGPRINTF7(("EXECUTE: TCASE, back from execute(t=0x%x, t1=0x%x)...\n", t, t1)); |
| 2802 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2803 | break; |
| 2804 | |
| 2805 | case TBRACE: |
| 2806 | /* |
| 2807 | if (iopp = t->ioact) |
| 2808 | while (*iopp) |
| 2809 | if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) { |
| 2810 | rv = -1; |
| 2811 | break; |
| 2812 | } |
| 2813 | */ |
| 2814 | if (rv >= 0 && (t1 = t->left)) |
| 2815 | rv = execute(t1, pin, pout, 0); |
| 2816 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2817 | |
| 2818 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2819 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2820 | broken: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2821 | t->words = wp2; |
| 2822 | isbreak = 0; |
| 2823 | freehere(areanum); |
| 2824 | freearea(areanum); |
| 2825 | areanum = a; |
| 2826 | if (interactive && intr) { |
| 2827 | closeall(); |
| 2828 | fail(); |
| 2829 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2830 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2831 | if ((i = trapset) != 0) { |
| 2832 | trapset = 0; |
| 2833 | runtrap(i); |
| 2834 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2835 | |
| 2836 | DBGPRINTF(("EXECUTE: returning from t=0x%x, rv=%d\n", t, rv)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2837 | return (rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | static int |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2841 | forkexec(REGISTER struct op *t, int *pin, int *pout, int act, char **wp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2842 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2843 | pid_t newpid; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2844 | int i, rv; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2845 | int (*shcom) (struct op *) = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2846 | REGISTER int f; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2847 | char *cp = NULL; |
| 2848 | struct ioword **iopp; |
| 2849 | int resetsig; |
| 2850 | char **owp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2851 | int forked = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2852 | |
| 2853 | int *hpin = pin; |
| 2854 | int *hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2855 | char *hwp; |
| 2856 | int hinteractive; |
| 2857 | int hintr; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2858 | struct brkcon *hbrklist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2859 | int hexecflg; |
| 2860 | |
| 2861 | #if __GNUC__ |
| 2862 | /* Avoid longjmp clobbering */ |
| 2863 | (void) &pin; |
| 2864 | (void) &pout; |
| 2865 | (void) ℘ |
| 2866 | (void) &shcom; |
| 2867 | (void) &cp; |
| 2868 | (void) &resetsig; |
| 2869 | (void) &owp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2870 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2871 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2872 | DBGPRINTF(("FORKEXEC: t=0x%x, pin 0x%x, pout 0x%x, act %d\n", t, pin, |
| 2873 | pout, act)); |
| 2874 | DBGPRINTF7(("FORKEXEC: t->words is %s\n", |
| 2875 | ((t->words == NULL) ? "NULL" : t->words[0]))); |
| 2876 | |
| 2877 | /* Hard to know how many words there are, be careful of garbage pointer values */ |
| 2878 | /* They are likely to cause "PCI bus fault" errors */ |
| 2879 | #if 0 |
| 2880 | DBGPRINTF7(("FORKEXEC: t->words is %s, t->words[1] is %s\n", |
| 2881 | ((t->words == NULL) ? "NULL" : t->words[0]), |
| 2882 | ((t->words == NULL) ? "NULL" : t->words[1]))); |
| 2883 | DBGPRINTF7(("FORKEXEC: wp is %s, wp[1] is %s\n", |
| 2884 | ((wp == NULL) ? "NULL" : wp[0]), |
| 2885 | ((wp[1] == NULL) ? "NULL" : wp[1]))); |
| 2886 | DBGPRINTF7(("FORKEXEC: wp2 is %s, wp[3] is %s\n", |
| 2887 | ((wp[2] == NULL) ? "NULL" : wp[2]), |
| 2888 | ((wp[3] == NULL) ? "NULL" : wp[3]))); |
| 2889 | #endif |
| 2890 | |
| 2891 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2892 | owp = wp; |
| 2893 | resetsig = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2894 | rv = -1; /* system-detected error */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2895 | if (t->type == TCOM) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2896 | while ((cp = *wp++) != NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2897 | cp = *wp; |
| 2898 | |
| 2899 | /* strip all initial assignments */ |
| 2900 | /* not correct wrt PATH=yyy command etc */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2901 | if (flag['x']) { |
| 2902 | DBGPRINTF9(("FORKEXEC: echo'ing, cp=0x%x, wp=0x%x, owp=0x%x\n", |
| 2903 | cp, wp, owp)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2904 | echo(cp ? wp : owp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2905 | } |
| 2906 | #if 0 |
| 2907 | DBGPRINTF9(("FORKEXEC: t->words is %s, t->words[1] is %s\n", |
| 2908 | ((t->words == NULL) ? "NULL" : t->words[0]), |
| 2909 | ((t->words == NULL) ? "NULL" : t->words[1]))); |
| 2910 | DBGPRINTF9(("FORKEXEC: wp is %s, wp[1] is %s\n", |
| 2911 | ((wp == NULL) ? "NULL" : wp[0]), |
| 2912 | ((wp == NULL) ? "NULL" : wp[1]))); |
| 2913 | #endif |
| 2914 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2915 | if (cp == NULL && t->ioact == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2916 | while ((cp = *owp++) != NULL && assign(cp, COPYV)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2917 | DBGPRINTF(("FORKEXEC: returning setstatus()\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2918 | return (setstatus(0)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2919 | } else if (cp != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2920 | shcom = inbuilt(cp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2921 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2922 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2923 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2924 | t->words = wp; |
| 2925 | f = act; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2926 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2927 | #if 0 |
| 2928 | DBGPRINTF3(("FORKEXEC: t->words is %s, t->words[1] is %s\n", |
| 2929 | ((t->words == NULL) ? "NULL" : t->words[0]), |
| 2930 | ((t->words == NULL) ? "NULL" : t->words[1]))); |
| 2931 | #endif |
| 2932 | DBGPRINTF(("FORKEXEC: shcom 0x%x, f&FEXEC 0x%x, owp 0x%x\n", shcom, |
| 2933 | f & FEXEC, owp)); |
| 2934 | |
| 2935 | if (shcom == NULL && (f & FEXEC) == 0) { |
| 2936 | /* Save values in case the child process alters them */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2937 | hpin = pin; |
| 2938 | hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2939 | hwp = *wp; |
| 2940 | hinteractive = interactive; |
| 2941 | hintr = intr; |
| 2942 | hbrklist = brklist; |
| 2943 | hexecflg = execflg; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2944 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2945 | DBGPRINTF3(("FORKEXEC: calling vfork()...\n")); |
| 2946 | |
| 2947 | newpid = vfork(); |
| 2948 | |
| 2949 | if (newpid == -1) { |
| 2950 | DBGPRINTF(("FORKEXEC: ERROR, unable to vfork()!\n")); |
| 2951 | return (-1); |
| 2952 | } |
| 2953 | |
| 2954 | |
| 2955 | if (newpid > 0) { /* Parent */ |
| 2956 | |
| 2957 | /* Restore values */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2958 | pin = hpin; |
| 2959 | pout = hpout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2960 | *wp = hwp; |
| 2961 | interactive = hinteractive; |
| 2962 | intr = hintr; |
| 2963 | brklist = hbrklist; |
| 2964 | execflg = hexecflg; |
| 2965 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2966 | /* moved up |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2967 | if (i == -1) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2968 | return(rv); |
| 2969 | */ |
| 2970 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2971 | if (pin != NULL) |
| 2972 | closepipe(pin); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2973 | |
| 2974 | return (pout == NULL ? setstatus(waitfor(newpid, 0)) : 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2977 | /* Must be the child process, pid should be 0 */ |
| 2978 | DBGPRINTF(("FORKEXEC: child process, shcom=0x%x\n", shcom)); |
| 2979 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2980 | if (interactive) { |
| 2981 | signal(SIGINT, SIG_IGN); |
| 2982 | signal(SIGQUIT, SIG_IGN); |
| 2983 | resetsig = 1; |
| 2984 | } |
| 2985 | interactive = 0; |
| 2986 | intr = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2987 | forked = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2988 | brklist = 0; |
| 2989 | execflg = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2990 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2991 | |
| 2992 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2993 | if (owp != NULL) |
| 2994 | while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
| 2995 | if (shcom == NULL) |
| 2996 | export(lookup(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 2997 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2998 | #ifdef COMPIPE |
| 2999 | if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) { |
| 3000 | err("piping to/from shell builtins not yet done"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3001 | if (forked) |
| 3002 | _exit(-1); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3003 | return (-1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3004 | } |
| 3005 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3006 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3007 | if (pin != NULL) { |
| 3008 | dup2(pin[0], 0); |
| 3009 | closepipe(pin); |
| 3010 | } |
| 3011 | if (pout != NULL) { |
| 3012 | dup2(pout[1], 1); |
| 3013 | closepipe(pout); |
| 3014 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3015 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3016 | if ((iopp = t->ioact) != NULL) { |
| 3017 | if (shcom != NULL && shcom != doexec) { |
| 3018 | prs(cp); |
| 3019 | err(": cannot redirect shell command"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3020 | if (forked) |
| 3021 | _exit(-1); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3022 | return (-1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3023 | } |
| 3024 | while (*iopp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3025 | if (iosetup(*iopp++, pin != NULL, pout != NULL)) { |
| 3026 | if (forked) |
| 3027 | _exit(rv); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3028 | return (rv); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3029 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3030 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3031 | |
| 3032 | if (shcom) { |
| 3033 | i = setstatus((*shcom) (t)); |
| 3034 | if (forked) |
| 3035 | _exit(i); |
| 3036 | DBGPRINTF(("FORKEXEC: returning i=%d\n", i)); |
| 3037 | return (i); |
| 3038 | } |
| 3039 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3040 | /* should use FIOCEXCL */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3041 | for (i = FDBASE; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3042 | close(i); |
| 3043 | if (resetsig) { |
| 3044 | signal(SIGINT, SIG_DFL); |
| 3045 | signal(SIGQUIT, SIG_DFL); |
| 3046 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3047 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3048 | if (t->type == TPAREN) |
| 3049 | _exit(execute(t->left, NOPIPE, NOPIPE, FEXEC)); |
| 3050 | if (wp[0] == NULL) |
| 3051 | _exit(0); |
| 3052 | |
| 3053 | cp = rexecve(wp[0], wp, makenv(0)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3054 | prs(wp[0]); |
| 3055 | prs(": "); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3056 | err(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3057 | if (!execflg) |
| 3058 | trap[0] = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3059 | |
| 3060 | DBGPRINTF(("FORKEXEC: calling leave(), pid=%d\n", newpid)); |
| 3061 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3062 | leave(); |
| 3063 | /* NOTREACHED */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3064 | _exit(1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | /* |
| 3068 | * 0< 1> are ignored as required |
| 3069 | * within pipelines. |
| 3070 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3071 | static int iosetup(iop, pipein, pipeout) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3072 | REGISTER struct ioword *iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3073 | int pipein, pipeout; |
| 3074 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3075 | REGISTER int u = -1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3076 | char *cp = NULL, *msg; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3077 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3078 | DBGPRINTF(("IOSETUP: iop 0x%x, pipein 0x%x, pipeout 0x%x\n", iop, |
| 3079 | pipein, pipeout)); |
| 3080 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3081 | if (iop->io_unit == IODEFAULT) /* take default */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3082 | iop->io_unit = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3083 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3084 | if (pipein && iop->io_unit == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3085 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3086 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3087 | if (pipeout && iop->io_unit == 1) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3088 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3089 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3090 | msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3091 | if ((iop->io_flag & IOHERE) == 0) { |
| 3092 | cp = iop->io_name; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3093 | if ((cp = evalstr(cp, DOSUB | DOTRIM)) == NULL) |
| 3094 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3095 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3096 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3097 | if (iop->io_flag & IODUP) { |
| 3098 | if (cp[1] || (!isdigit(*cp) && *cp != '-')) { |
| 3099 | prs(cp); |
| 3100 | err(": illegal >& argument"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3101 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3102 | } |
| 3103 | if (*cp == '-') |
| 3104 | iop->io_flag = IOCLOSE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3105 | iop->io_flag &= ~(IOREAD | IOWRITE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3106 | } |
| 3107 | switch (iop->io_flag) { |
| 3108 | case IOREAD: |
| 3109 | u = open(cp, 0); |
| 3110 | break; |
| 3111 | |
| 3112 | case IOHERE: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3113 | case IOHERE | IOXHERE: |
| 3114 | u = herein(iop->io_name, iop->io_flag & IOXHERE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3115 | cp = "here file"; |
| 3116 | break; |
| 3117 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3118 | case IOWRITE | IOCAT: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3119 | if ((u = open(cp, 1)) >= 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3120 | lseek(u, (long) 0, 2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3121 | break; |
| 3122 | } |
| 3123 | case IOWRITE: |
| 3124 | u = creat(cp, 0666); |
| 3125 | break; |
| 3126 | |
| 3127 | case IODUP: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3128 | u = dup2(*cp - '0', iop->io_unit); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3129 | break; |
| 3130 | |
| 3131 | case IOCLOSE: |
| 3132 | close(iop->io_unit); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3133 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3134 | } |
| 3135 | if (u < 0) { |
| 3136 | prs(cp); |
| 3137 | prs(": cannot "); |
| 3138 | warn(msg); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3139 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3140 | } else { |
| 3141 | if (u != iop->io_unit) { |
| 3142 | dup2(u, iop->io_unit); |
| 3143 | close(u); |
| 3144 | } |
| 3145 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3146 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3149 | static void echo(wp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3150 | REGISTER char **wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3151 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3152 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3153 | |
| 3154 | prs("+"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3155 | for (i = 0; wp[i]; i++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3156 | if (i) |
| 3157 | prs(" "); |
| 3158 | prs(wp[i]); |
| 3159 | } |
| 3160 | prs("\n"); |
| 3161 | } |
| 3162 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3163 | static struct op **find1case(t, w) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3164 | struct op *t; |
| 3165 | char *w; |
| 3166 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3167 | REGISTER struct op *t1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3168 | struct op **tp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3169 | REGISTER char **wp, *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3170 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3171 | |
| 3172 | if (t == NULL) { |
| 3173 | DBGPRINTF3(("FIND1CASE: enter, t==NULL, returning.\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3174 | return ((struct op **) NULL); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3175 | } |
| 3176 | |
| 3177 | DBGPRINTF3(("FIND1CASE: enter, t->type=%d (%s)\n", t->type, |
| 3178 | T_CMD_NAMES[t->type])); |
| 3179 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3180 | if (t->type == TLIST) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3181 | if ((tp = find1case(t->left, w)) != NULL) { |
| 3182 | DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=0x%x\n", tp)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3183 | return (tp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3184 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3185 | t1 = t->right; /* TPAT */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3186 | } else |
| 3187 | t1 = t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3188 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3189 | for (wp = t1->words; *wp;) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3190 | if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp)) { |
| 3191 | DBGPRINTF3(("FIND1CASE: returning &t1->left= 0x%x.\n", |
| 3192 | &t1->left)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3193 | return (&t1->left); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3194 | } |
| 3195 | |
| 3196 | DBGPRINTF(("FIND1CASE: returning NULL\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3197 | return ((struct op **) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3200 | static struct op *findcase(t, w) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3201 | struct op *t; |
| 3202 | char *w; |
| 3203 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3204 | REGISTER struct op **tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3205 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3206 | return ((tp = find1case(t, w)) != NULL ? *tp : (struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | /* |
| 3210 | * Enter a new loop level (marked for break/continue). |
| 3211 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3212 | static void brkset(bc) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3213 | struct brkcon *bc; |
| 3214 | { |
| 3215 | bc->nextlev = brklist; |
| 3216 | brklist = bc; |
| 3217 | } |
| 3218 | |
| 3219 | /* |
| 3220 | * Wait for the last process created. |
| 3221 | * Print a message for each process found |
| 3222 | * that was killed by a signal. |
| 3223 | * Ignore interrupt signals while waiting |
| 3224 | * unless `canintr' is true. |
| 3225 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3226 | static int waitfor(lastpid, canintr) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3227 | REGISTER int lastpid; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3228 | int canintr; |
| 3229 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3230 | REGISTER int pid, rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3231 | int s; |
| 3232 | int oheedint = heedint; |
| 3233 | |
| 3234 | heedint = 0; |
| 3235 | rv = 0; |
| 3236 | do { |
| 3237 | pid = wait(&s); |
| 3238 | if (pid == -1) { |
| 3239 | if (errno != EINTR || canintr) |
| 3240 | break; |
| 3241 | } else { |
| 3242 | if ((rv = WAITSIG(s)) != 0) { |
| 3243 | if (rv < NSIGNAL) { |
| 3244 | if (signame[rv] != NULL) { |
| 3245 | if (pid != lastpid) { |
| 3246 | prn(pid); |
| 3247 | prs(": "); |
| 3248 | } |
| 3249 | prs(signame[rv]); |
| 3250 | } |
| 3251 | } else { |
| 3252 | if (pid != lastpid) { |
| 3253 | prn(pid); |
| 3254 | prs(": "); |
| 3255 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3256 | prs("Signal "); |
| 3257 | prn(rv); |
| 3258 | prs(" "); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3259 | } |
| 3260 | if (WAITCORE(s)) |
| 3261 | prs(" - core dumped"); |
| 3262 | if (rv >= NSIGNAL || signame[rv]) |
| 3263 | prs("\n"); |
| 3264 | rv = -1; |
| 3265 | } else |
| 3266 | rv = WAITVAL(s); |
| 3267 | } |
| 3268 | } while (pid != lastpid); |
| 3269 | heedint = oheedint; |
| 3270 | if (intr) { |
| 3271 | if (interactive) { |
| 3272 | if (canintr) |
| 3273 | intr = 0; |
| 3274 | } else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3275 | if (exstat == 0) |
| 3276 | exstat = rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3277 | onintr(0); |
| 3278 | } |
| 3279 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3280 | return (rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3283 | static int setstatus(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3284 | REGISTER int s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3285 | { |
| 3286 | exstat = s; |
| 3287 | setval(lookup("?"), putn(s)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3288 | return (s); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | /* |
| 3292 | * PATH-searching interface to execve. |
| 3293 | * If getenv("PATH") were kept up-to-date, |
| 3294 | * execvp might be used. |
| 3295 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3296 | static char *rexecve(c, v, envp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3297 | char *c, **v, **envp; |
| 3298 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3299 | REGISTER int i; |
| 3300 | REGISTER char *sp, *tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3301 | int eacces = 0, asis = 0; |
| 3302 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3303 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3304 | char *name = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3305 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3306 | optind = 1; |
| 3307 | if (find_applet_by_name(name)) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3308 | /* We have to exec here since we vforked. Running |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3309 | * run_applet_by_name() won't work and bad things |
| 3310 | * will happen. */ |
| 3311 | execve("/proc/self/exe", v, envp); |
| 3312 | execve("busybox", v, envp); |
| 3313 | } |
| 3314 | #endif |
| 3315 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3316 | DBGPRINTF(("REXECVE: c=0x%x, v=0x%x, envp=0x%x\n", c, v, envp)); |
| 3317 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3318 | sp = any('/', c) ? "" : path->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3319 | asis = *sp == '\0'; |
| 3320 | while (asis || *sp != '\0') { |
| 3321 | asis = 0; |
| 3322 | tp = e.linep; |
| 3323 | for (; *sp != '\0'; tp++) |
| 3324 | if ((*tp = *sp++) == ':') { |
| 3325 | asis = *sp == '\0'; |
| 3326 | break; |
| 3327 | } |
| 3328 | if (tp != e.linep) |
| 3329 | *tp++ = '/'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3330 | for (i = 0; (*tp++ = c[i++]) != '\0';); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3331 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3332 | DBGPRINTF3(("REXECVE: e.linep is %s\n", e.linep)); |
| 3333 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3334 | execve(e.linep, v, envp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3335 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3336 | switch (errno) { |
| 3337 | case ENOEXEC: |
| 3338 | *v = e.linep; |
| 3339 | tp = *--v; |
| 3340 | *v = e.linep; |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 3341 | execve(DEFAULT_SHELL, v, envp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3342 | *v = tp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3343 | return ("no Shell"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3344 | |
| 3345 | case ENOMEM: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3346 | return ((char *) bb_msg_memory_exhausted); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3347 | |
| 3348 | case E2BIG: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3349 | return ("argument list too long"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3350 | |
| 3351 | case EACCES: |
| 3352 | eacces++; |
| 3353 | break; |
| 3354 | } |
| 3355 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3356 | return (errno == ENOENT ? "not found" : "cannot execute"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | /* |
| 3360 | * Run the command produced by generator `f' |
| 3361 | * applied to stream `arg'. |
| 3362 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3363 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3364 | { |
| 3365 | struct op *otree; |
| 3366 | struct wdblock *swdlist; |
| 3367 | struct wdblock *siolist; |
| 3368 | jmp_buf ev, rt; |
| 3369 | xint *ofail; |
| 3370 | int rv; |
| 3371 | |
| 3372 | #if __GNUC__ |
| 3373 | /* Avoid longjmp clobbering */ |
| 3374 | (void) &rv; |
| 3375 | #endif |
| 3376 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3377 | DBGPRINTF(("RUN: enter, areanum %d, outtree 0x%x, failpt 0x%x\n", |
| 3378 | areanum, outtree, failpt)); |
| 3379 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3380 | areanum++; |
| 3381 | swdlist = wdlist; |
| 3382 | siolist = iolist; |
| 3383 | otree = outtree; |
| 3384 | ofail = failpt; |
| 3385 | rv = -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3386 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3387 | if (newenv(setjmp(errpt = ev)) == 0) { |
| 3388 | wdlist = 0; |
| 3389 | iolist = 0; |
| 3390 | pushio(argp, f); |
| 3391 | e.iobase = e.iop; |
| 3392 | yynerrs = 0; |
| 3393 | if (setjmp(failpt = rt) == 0 && yyparse() == 0) |
| 3394 | rv = execute(outtree, NOPIPE, NOPIPE, 0); |
| 3395 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3396 | } else { |
| 3397 | DBGPRINTF(("RUN: error from newenv()!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3398 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3399 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3400 | wdlist = swdlist; |
| 3401 | iolist = siolist; |
| 3402 | failpt = ofail; |
| 3403 | outtree = otree; |
| 3404 | freearea(areanum--); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3405 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3406 | return (rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | /* -------- do.c -------- */ |
| 3410 | |
| 3411 | /* |
| 3412 | * built-in commands: doX |
| 3413 | */ |
| 3414 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3415 | static int dohelp(struct op *t) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3416 | { |
| 3417 | int col; |
| 3418 | const struct builtincmd *x; |
| 3419 | |
| 3420 | printf("\nBuilt-in commands:\n"); |
| 3421 | printf("-------------------\n"); |
| 3422 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3423 | for (col = 0, x = builtincmds; x->builtinfunc != NULL; x++) { |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3424 | if (!x->name) |
| 3425 | continue; |
| 3426 | col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name); |
| 3427 | if (col > 60) { |
| 3428 | printf("\n"); |
| 3429 | col = 0; |
| 3430 | } |
| 3431 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3432 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3433 | { |
| 3434 | int i; |
| 3435 | const struct BB_applet *applet; |
| 3436 | extern const struct BB_applet applets[]; |
| 3437 | extern const size_t NUM_APPLETS; |
| 3438 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3439 | for (i = 0, applet = applets; i < NUM_APPLETS; applet++, i++) { |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3440 | if (!applet->name) |
| 3441 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3442 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3443 | col += printf("%s%s", ((col == 0) ? "\t" : " "), applet->name); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3444 | if (col > 60) { |
| 3445 | printf("\n"); |
| 3446 | col = 0; |
| 3447 | } |
| 3448 | } |
| 3449 | } |
| 3450 | #endif |
| 3451 | printf("\n\n"); |
| 3452 | return EXIT_SUCCESS; |
| 3453 | } |
| 3454 | |
| 3455 | |
| 3456 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3457 | static int dolabel(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3458 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3459 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3462 | static int dochdir(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3463 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3464 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3465 | REGISTER char *cp, *er; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3466 | |
| 3467 | if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL) |
| 3468 | er = ": no home directory"; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3469 | else if (chdir(cp) < 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3470 | er = ": bad directory"; |
| 3471 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3472 | return (0); |
| 3473 | prs(cp != NULL ? cp : "cd"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3474 | err(er); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3475 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3478 | static int doshift(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3479 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3480 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3481 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3482 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3483 | n = t->words[1] ? getn(t->words[1]) : 1; |
| 3484 | if (dolc < n) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3485 | err("nothing to shift"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3486 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3487 | } |
| 3488 | dolv[n] = dolv[0]; |
| 3489 | dolv += n; |
| 3490 | dolc -= n; |
| 3491 | setval(lookup("#"), putn(dolc)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3492 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | /* |
| 3496 | * execute login and newgrp directly |
| 3497 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3498 | static int dologin(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3499 | struct op *t; |
| 3500 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3501 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3502 | |
| 3503 | if (interactive) { |
| 3504 | signal(SIGINT, SIG_DFL); |
| 3505 | signal(SIGQUIT, SIG_DFL); |
| 3506 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3507 | cp = rexecve(t->words[0], t->words, makenv(0)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3508 | prs(t->words[0]); |
| 3509 | prs(": "); |
| 3510 | err(cp); |
| 3511 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3514 | static int doumask(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3515 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3516 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3517 | REGISTER int i, n; |
| 3518 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3519 | |
| 3520 | if ((cp = t->words[1]) == NULL) { |
| 3521 | i = umask(0); |
| 3522 | umask(i); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3523 | for (n = 3 * 4; (n -= 3) >= 0;) |
| 3524 | putc('0' + ((i >> n) & 07), stderr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3525 | putc('\n', stderr); |
| 3526 | } else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3527 | for (n = 0; *cp >= '0' && *cp <= '9'; cp++) |
| 3528 | n = n * 8 + (*cp - '0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3529 | umask(n); |
| 3530 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3531 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3534 | static int doexec(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3535 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3536 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3537 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3538 | jmp_buf ex; |
| 3539 | xint *ofail; |
| 3540 | |
| 3541 | t->ioact = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3542 | for (i = 0; (t->words[i] = t->words[i + 1]) != NULL; i++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3543 | if (i == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3544 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3545 | execflg = 1; |
| 3546 | ofail = failpt; |
| 3547 | if (setjmp(failpt = ex) == 0) |
| 3548 | execute(t, NOPIPE, NOPIPE, FEXEC); |
| 3549 | failpt = ofail; |
| 3550 | execflg = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3551 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3554 | static int dodot(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3555 | struct op *t; |
| 3556 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3557 | REGISTER int i; |
| 3558 | REGISTER char *sp, *tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3559 | char *cp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3560 | int maltmp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3561 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3562 | DBGPRINTF(("DODOT: enter, t=0x%x, tleft 0x%x, tright 0x%x, e.linep is %s\n", t, t->left, t->right, ((e.linep == NULL) ? "NULL" : e.linep))); |
| 3563 | |
| 3564 | if ((cp = t->words[1]) == NULL) { |
| 3565 | DBGPRINTF(("DODOT: bad args, ret 0\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3566 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3567 | } else { |
| 3568 | DBGPRINTF(("DODOT: cp is %s\n", cp)); |
| 3569 | } |
| 3570 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3571 | sp = any('/', cp) ? ":" : path->value; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3572 | |
| 3573 | DBGPRINTF(("DODOT: sp is %s, e.linep is %s\n", |
| 3574 | ((sp == NULL) ? "NULL" : sp), |
| 3575 | ((e.linep == NULL) ? "NULL" : e.linep))); |
| 3576 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3577 | while (*sp) { |
| 3578 | tp = e.linep; |
| 3579 | while (*sp && (*tp = *sp++) != ':') |
| 3580 | tp++; |
| 3581 | if (tp != e.linep) |
| 3582 | *tp++ = '/'; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3583 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3584 | for (i = 0; (*tp++ = cp[i++]) != '\0';); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3585 | |
| 3586 | /* Original code */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3587 | if ((i = open(e.linep, 0)) >= 0) { |
| 3588 | exstat = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3589 | maltmp = remap(i); |
| 3590 | DBGPRINTF(("DODOT: remap=%d, exstat=%d, e.iofd %d, i %d, e.linep is %s\n", maltmp, exstat, e.iofd, i, e.linep)); |
| 3591 | |
| 3592 | next(maltmp); /* Basically a PUSHIO */ |
| 3593 | |
| 3594 | DBGPRINTF(("DODOT: returning exstat=%d\n", exstat)); |
| 3595 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3596 | return (exstat); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3597 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3598 | |
| 3599 | } /* While */ |
| 3600 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3601 | prs(cp); |
| 3602 | err(": not found"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3603 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3604 | return (-1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3607 | static int dowait(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3608 | struct op *t; |
| 3609 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3610 | REGISTER int i; |
| 3611 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3612 | |
| 3613 | if ((cp = t->words[1]) != NULL) { |
| 3614 | i = getn(cp); |
| 3615 | if (i == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3616 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3617 | } else |
| 3618 | i = -1; |
| 3619 | setstatus(waitfor(i, 1)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3620 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3623 | static int doread(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3624 | struct op *t; |
| 3625 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3626 | REGISTER char *cp, **wp; |
| 3627 | REGISTER int nb = 0; |
| 3628 | REGISTER int nl = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3629 | |
| 3630 | if (t->words[1] == NULL) { |
| 3631 | err("Usage: read name ..."); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3632 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3633 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3634 | for (wp = t->words + 1; *wp; wp++) { |
| 3635 | for (cp = e.linep; !nl && cp < elinep - 1; cp++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3636 | if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) || |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3637 | (nl = (*cp == '\n')) || (wp[1] && any(*cp, ifs->value))) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3638 | break; |
| 3639 | *cp = 0; |
| 3640 | if (nb <= 0) |
| 3641 | break; |
| 3642 | setval(lookup(*wp), e.linep); |
| 3643 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3644 | return (nb <= 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3647 | static int doeval(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3648 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3649 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3650 | return (RUN(awordlist, t->words + 1, wdchar)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3653 | static int dotrap(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3654 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3655 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3656 | REGISTER int n, i; |
| 3657 | REGISTER int resetsig; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3658 | |
| 3659 | if (t->words[1] == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3660 | for (i = 0; i <= _NSIG; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3661 | if (trap[i]) { |
| 3662 | prn(i); |
| 3663 | prs(": "); |
| 3664 | prs(trap[i]); |
| 3665 | prs("\n"); |
| 3666 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3667 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3668 | } |
| 3669 | resetsig = isdigit(*t->words[1]); |
| 3670 | for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) { |
| 3671 | n = getsig(t->words[i]); |
| 3672 | freecell(trap[n]); |
| 3673 | trap[n] = 0; |
| 3674 | if (!resetsig) { |
| 3675 | if (*t->words[1] != '\0') { |
| 3676 | trap[n] = strsave(t->words[1], 0); |
| 3677 | setsig(n, sig); |
| 3678 | } else |
| 3679 | setsig(n, SIG_IGN); |
| 3680 | } else { |
| 3681 | if (interactive) |
| 3682 | if (n == SIGINT) |
| 3683 | setsig(n, onintr); |
| 3684 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3685 | setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3686 | else |
| 3687 | setsig(n, SIG_DFL); |
| 3688 | } |
| 3689 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3690 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3693 | static int getsig(s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3694 | char *s; |
| 3695 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3696 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3697 | |
| 3698 | if ((n = getn(s)) < 0 || n > _NSIG) { |
| 3699 | err("trap: bad signal number"); |
| 3700 | n = 0; |
| 3701 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3702 | return (n); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3705 | static void setsig(REGISTER int n, sighandler_t f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3706 | { |
| 3707 | if (n == 0) |
| 3708 | return; |
| 3709 | if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) { |
| 3710 | ourtrap[n] = 1; |
| 3711 | signal(n, f); |
| 3712 | } |
| 3713 | } |
| 3714 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3715 | static int getn(as) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3716 | char *as; |
| 3717 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3718 | REGISTER char *s; |
| 3719 | REGISTER int n, m; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3720 | |
| 3721 | s = as; |
| 3722 | m = 1; |
| 3723 | if (*s == '-') { |
| 3724 | m = -1; |
| 3725 | s++; |
| 3726 | } |
| 3727 | for (n = 0; isdigit(*s); s++) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3728 | n = (n * 10) + (*s - '0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3729 | if (*s) { |
| 3730 | prs(as); |
| 3731 | err(": bad number"); |
| 3732 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3733 | return (n * m); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3736 | static int dobreak(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3737 | struct op *t; |
| 3738 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3739 | return (brkcontin(t->words[1], 1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3742 | static int docontinue(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3743 | struct op *t; |
| 3744 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3745 | return (brkcontin(t->words[1], 0)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3748 | static int brkcontin(cp, val) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3749 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3750 | int val; |
| 3751 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3752 | REGISTER struct brkcon *bc; |
| 3753 | REGISTER int nl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3754 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3755 | nl = cp == NULL ? 1 : getn(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3756 | if (nl <= 0) |
| 3757 | nl = 999; |
| 3758 | do { |
| 3759 | if ((bc = brklist) == NULL) |
| 3760 | break; |
| 3761 | brklist = bc->nextlev; |
| 3762 | } while (--nl); |
| 3763 | if (nl) { |
| 3764 | err("bad break/continue level"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3765 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3766 | } |
| 3767 | isbreak = val; |
| 3768 | longjmp(bc->brkpt, 1); |
| 3769 | /* NOTREACHED */ |
| 3770 | } |
| 3771 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3772 | static int doexit(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3773 | struct op *t; |
| 3774 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3775 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3776 | |
| 3777 | execflg = 0; |
| 3778 | if ((cp = t->words[1]) != NULL) |
| 3779 | setstatus(getn(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3780 | |
| 3781 | DBGPRINTF(("DOEXIT: calling leave(), t=0x%x\n", t)); |
| 3782 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3783 | leave(); |
| 3784 | /* NOTREACHED */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3785 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3788 | static int doexport(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3789 | struct op *t; |
| 3790 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3791 | rdexp(t->words + 1, export, EXPORT); |
| 3792 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3795 | static int doreadonly(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3796 | struct op *t; |
| 3797 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3798 | rdexp(t->words + 1, ronly, RONLY); |
| 3799 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3802 | static void rdexp(char **wp, void (*f) (struct var *), int key) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3803 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3804 | DBGPRINTF6(("RDEXP: enter, wp=0x%x, func=0x%x, key=%d\n", wp, f, key)); |
| 3805 | DBGPRINTF6(("RDEXP: *wp=%s\n", *wp)); |
| 3806 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3807 | if (*wp != NULL) { |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3808 | for (; *wp != NULL; wp++) { |
| 3809 | if (isassign(*wp)) { |
| 3810 | char *cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3811 | |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3812 | assign(*wp, COPYV); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3813 | for (cp = *wp; *cp != '='; cp++); |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3814 | *cp = '\0'; |
| 3815 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3816 | if (checkname(*wp)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3817 | (*f) (lookup(*wp)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3818 | else |
| 3819 | badid(*wp); |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3820 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3821 | } else |
| 3822 | putvlist(key, 1); |
| 3823 | } |
| 3824 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3825 | static void badid(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3826 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3827 | { |
| 3828 | prs(s); |
| 3829 | err(": bad identifier"); |
| 3830 | } |
| 3831 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3832 | static int doset(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3833 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3834 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3835 | REGISTER struct var *vp; |
| 3836 | REGISTER char *cp; |
| 3837 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3838 | |
| 3839 | if ((cp = t->words[1]) == NULL) { |
| 3840 | for (vp = vlist; vp; vp = vp->next) |
| 3841 | varput(vp->name, 1); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3842 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3843 | } |
| 3844 | if (*cp == '-') { |
| 3845 | /* bad: t->words++; */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3846 | for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3847 | if (*++cp == 0) |
| 3848 | flag['x'] = flag['v'] = 0; |
| 3849 | else |
| 3850 | for (; *cp; cp++) |
| 3851 | switch (*cp) { |
| 3852 | case 'e': |
| 3853 | if (!interactive) |
| 3854 | flag['e']++; |
| 3855 | break; |
| 3856 | |
| 3857 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3858 | if (*cp >= 'a' && *cp <= 'z') |
| 3859 | flag[(int) *cp]++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3860 | break; |
| 3861 | } |
| 3862 | setdash(); |
| 3863 | } |
| 3864 | if (t->words[1]) { |
| 3865 | t->words[0] = dolv[0]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3866 | for (n = 1; t->words[n]; n++) |
| 3867 | setarea((char *) t->words[n], 0); |
| 3868 | dolc = n - 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3869 | dolv = t->words; |
| 3870 | setval(lookup("#"), putn(dolc)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3871 | setarea((char *) (dolv - 1), 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3872 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3873 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3876 | static void varput(s, out) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3877 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3878 | int out; |
| 3879 | { |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3880 | if (isalnum(*s) || *s == '_') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3881 | write(out, s, strlen(s)); |
| 3882 | write(out, "\n", 1); |
| 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | |
| 3887 | /* |
| 3888 | * Copyright (c) 1999 Herbert Xu <herbert@debian.org> |
| 3889 | * This file contains code for the times builtin. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3890 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3891 | static int dotimes(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3892 | { |
| 3893 | struct tms buf; |
| 3894 | long int clk_tck = sysconf(_SC_CLK_TCK); |
| 3895 | |
| 3896 | times(&buf); |
| 3897 | printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n", |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3898 | (int) (buf.tms_utime / clk_tck / 60), |
| 3899 | ((double) buf.tms_utime) / clk_tck, |
| 3900 | (int) (buf.tms_stime / clk_tck / 60), |
| 3901 | ((double) buf.tms_stime) / clk_tck, |
| 3902 | (int) (buf.tms_cutime / clk_tck / 60), |
| 3903 | ((double) buf.tms_cutime) / clk_tck, |
| 3904 | (int) (buf.tms_cstime / clk_tck / 60), |
| 3905 | ((double) buf.tms_cstime) / clk_tck); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3906 | return 0; |
| 3907 | } |
| 3908 | |
| 3909 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3910 | static int (*inbuilt(char *s)) (struct op *) { |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3911 | const struct builtincmd *bp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3912 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3913 | for (bp = builtincmds; bp->name != NULL; bp++) |
| 3914 | if (strcmp(bp->name, s) == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3915 | return (bp->builtinfunc); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3916 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3917 | return (NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
| 3920 | /* -------- eval.c -------- */ |
| 3921 | |
| 3922 | /* |
| 3923 | * ${} |
| 3924 | * `command` |
| 3925 | * blank interpretation |
| 3926 | * quoting |
| 3927 | * glob |
| 3928 | */ |
| 3929 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3930 | static char **eval(char **ap, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3931 | { |
| 3932 | struct wdblock *wb; |
| 3933 | char **wp; |
| 3934 | char **wf; |
| 3935 | jmp_buf ev; |
| 3936 | |
| 3937 | #if __GNUC__ |
| 3938 | /* Avoid longjmp clobbering */ |
| 3939 | (void) ℘ |
| 3940 | (void) ≈ |
| 3941 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3942 | |
| 3943 | DBGPRINTF4(("EVAL: enter, f=%d\n", f)); |
| 3944 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3945 | wp = NULL; |
| 3946 | wb = NULL; |
| 3947 | wf = NULL; |
| 3948 | if (newenv(setjmp(errpt = ev)) == 0) { |
| 3949 | while (*ap && isassign(*ap)) |
| 3950 | expand(*ap++, &wb, f & ~DOGLOB); |
| 3951 | if (flag['k']) { |
| 3952 | for (wf = ap; *wf; wf++) { |
| 3953 | if (isassign(*wf)) |
| 3954 | expand(*wf, &wb, f & ~DOGLOB); |
| 3955 | } |
| 3956 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3957 | for (wb = addword((char *) 0, wb); *ap; ap++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3958 | if (!flag['k'] || !isassign(*ap)) |
| 3959 | expand(*ap, &wb, f & ~DOKEY); |
| 3960 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3961 | wb = addword((char *) 0, wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3962 | wp = getwords(wb); |
| 3963 | quitenv(); |
| 3964 | } else |
| 3965 | gflg = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3966 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3967 | return (gflg ? (char **) NULL : wp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3968 | } |
| 3969 | |
| 3970 | /* |
| 3971 | * Make the exported environment from the exported |
| 3972 | * names in the dictionary. Keyword assignments |
| 3973 | * will already have been done. |
| 3974 | */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3975 | static char **makenv(int all) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3976 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3977 | REGISTER struct wdblock *wb; |
| 3978 | REGISTER struct var *vp; |
| 3979 | |
| 3980 | DBGPRINTF5(("MAKENV: enter, all=%d\n", all)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3981 | |
| 3982 | wb = NULL; |
| 3983 | for (vp = vlist; vp; vp = vp->next) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3984 | if (all || vp->status & EXPORT) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3985 | wb = addword(vp->name, wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3986 | wb = addword((char *) 0, wb); |
| 3987 | return (getwords(wb)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3990 | static char *evalstr(cp, f) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3991 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3992 | int f; |
| 3993 | { |
| 3994 | struct wdblock *wb; |
| 3995 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 3996 | DBGPRINTF6(("EVALSTR: enter, cp=0x%x, f=%d\n", cp, f)); |
| 3997 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3998 | wb = NULL; |
| 3999 | if (expand(cp, &wb, f)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4000 | if (wb == NULL || wb->w_nword == 0 |
| 4001 | || (cp = wb->w_words[0]) == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4002 | cp = ""; |
| 4003 | DELETE(wb); |
| 4004 | } else |
| 4005 | cp = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4006 | return (cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4009 | static int expand(char *cp, REGISTER struct wdblock **wbp, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4010 | { |
| 4011 | jmp_buf ev; |
| 4012 | |
| 4013 | #if __GNUC__ |
| 4014 | /* Avoid longjmp clobbering */ |
| 4015 | (void) &cp; |
| 4016 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4017 | |
| 4018 | DBGPRINTF3(("EXPAND: enter, f=%d\n", f)); |
| 4019 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4020 | gflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4021 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4022 | if (cp == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4023 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4024 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4025 | if (!anys("$`'\"", cp) && |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4026 | !anys(ifs->value, cp) && ((f & DOGLOB) == 0 || !anys("[*?", cp))) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4027 | cp = strsave(cp, areanum); |
| 4028 | if (f & DOTRIM) |
| 4029 | unquote(cp); |
| 4030 | *wbp = addword(cp, *wbp); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4031 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4032 | } |
| 4033 | if (newenv(setjmp(errpt = ev)) == 0) { |
| 4034 | PUSHIO(aword, cp, strchar); |
| 4035 | e.iobase = e.iop; |
| 4036 | while ((cp = blank(f)) && gflg == 0) { |
| 4037 | e.linep = cp; |
| 4038 | cp = strsave(cp, areanum); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4039 | if ((f & DOGLOB) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4040 | if (f & DOTRIM) |
| 4041 | unquote(cp); |
| 4042 | *wbp = addword(cp, *wbp); |
| 4043 | } else |
| 4044 | *wbp = glob(cp, *wbp); |
| 4045 | } |
| 4046 | quitenv(); |
| 4047 | } else |
| 4048 | gflg = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4049 | return (gflg == 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
| 4052 | /* |
| 4053 | * Blank interpretation and quoting |
| 4054 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4055 | static char *blank(f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4056 | int f; |
| 4057 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4058 | REGISTER int c, c1; |
| 4059 | REGISTER char *sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4060 | int scanequals, foundequals; |
| 4061 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4062 | DBGPRINTF3(("BLANK: enter, f=%d\n", f)); |
| 4063 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4064 | sp = e.linep; |
| 4065 | scanequals = f & DOKEY; |
| 4066 | foundequals = 0; |
| 4067 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4068 | loop: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4069 | switch (c = subgetc('"', foundequals)) { |
| 4070 | case 0: |
| 4071 | if (sp == e.linep) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4072 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4073 | *e.linep++ = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4074 | return (sp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4075 | |
| 4076 | default: |
| 4077 | if (f & DOBLANK && any(c, ifs->value)) |
| 4078 | goto loop; |
| 4079 | break; |
| 4080 | |
| 4081 | case '"': |
| 4082 | case '\'': |
| 4083 | scanequals = 0; |
| 4084 | if (INSUB()) |
| 4085 | break; |
| 4086 | for (c1 = c; (c = subgetc(c1, 1)) != c1;) { |
| 4087 | if (c == 0) |
| 4088 | break; |
| 4089 | if (c == '\'' || !any(c, "$`\"")) |
| 4090 | c |= QUOTE; |
| 4091 | *e.linep++ = c; |
| 4092 | } |
| 4093 | c = 0; |
| 4094 | } |
| 4095 | unget(c); |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 4096 | if (!isalpha(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4097 | scanequals = 0; |
| 4098 | for (;;) { |
| 4099 | c = subgetc('"', foundequals); |
| 4100 | if (c == 0 || |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4101 | f & (DOBLANK && any(c, ifs->value)) || |
| 4102 | (!INSUB() && any(c, "\"'"))) { |
| 4103 | scanequals = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4104 | unget(c); |
| 4105 | if (any(c, "\"'")) |
| 4106 | goto loop; |
| 4107 | break; |
| 4108 | } |
| 4109 | if (scanequals) { |
| 4110 | if (c == '=') { |
| 4111 | foundequals = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4112 | scanequals = 0; |
| 4113 | } else if (!isalnum(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4114 | scanequals = 0; |
| 4115 | } |
| 4116 | *e.linep++ = c; |
| 4117 | } |
| 4118 | *e.linep++ = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4119 | return (sp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
| 4122 | /* |
| 4123 | * Get characters, substituting for ` and $ |
| 4124 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4125 | static int subgetc(ec, quoted) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4126 | REGISTER char ec; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4127 | int quoted; |
| 4128 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4129 | REGISTER char c; |
| 4130 | |
| 4131 | DBGPRINTF3(("SUBGETC: enter, quoted=%d\n", quoted)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4132 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4133 | again: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4134 | c = my_getc(ec); |
| 4135 | if (!INSUB() && ec != '\'') { |
| 4136 | if (c == '`') { |
| 4137 | if (grave(quoted) == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4138 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4139 | e.iop->task = XGRAVE; |
| 4140 | goto again; |
| 4141 | } |
| 4142 | if (c == '$' && (c = dollar(quoted)) == 0) { |
| 4143 | e.iop->task = XDOLL; |
| 4144 | goto again; |
| 4145 | } |
| 4146 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4147 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4148 | } |
| 4149 | |
| 4150 | /* |
| 4151 | * Prepare to generate the string returned by ${} substitution. |
| 4152 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4153 | static int dollar(quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4154 | int quoted; |
| 4155 | { |
| 4156 | int otask; |
| 4157 | struct io *oiop; |
| 4158 | char *dolp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4159 | REGISTER char *s, c, *cp = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4160 | struct var *vp; |
| 4161 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4162 | DBGPRINTF3(("DOLLAR: enter, quoted=%d\n", quoted)); |
| 4163 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4164 | c = readc(); |
| 4165 | s = e.linep; |
| 4166 | if (c != '{') { |
| 4167 | *e.linep++ = c; |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 4168 | if (isalpha(c) || c == '_') { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4169 | while ((c = readc()) != 0 && (isalnum(c) || c == '_')) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4170 | if (e.linep < elinep) |
| 4171 | *e.linep++ = c; |
| 4172 | unget(c); |
| 4173 | } |
| 4174 | c = 0; |
| 4175 | } else { |
| 4176 | oiop = e.iop; |
| 4177 | otask = e.iop->task; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4178 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4179 | e.iop->task = XOTHER; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4180 | while ((c = subgetc('"', 0)) != 0 && c != '}' && c != '\n') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4181 | if (e.linep < elinep) |
| 4182 | *e.linep++ = c; |
| 4183 | if (oiop == e.iop) |
| 4184 | e.iop->task = otask; |
| 4185 | if (c != '}') { |
| 4186 | err("unclosed ${"); |
| 4187 | gflg++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4188 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4189 | } |
| 4190 | } |
| 4191 | if (e.linep >= elinep) { |
| 4192 | err("string in ${} too long"); |
| 4193 | gflg++; |
| 4194 | e.linep -= 10; |
| 4195 | } |
| 4196 | *e.linep = 0; |
| 4197 | if (*s) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4198 | for (cp = s + 1; *cp; cp++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4199 | if (any(*cp, "=-+?")) { |
| 4200 | c = *cp; |
| 4201 | *cp++ = 0; |
| 4202 | break; |
| 4203 | } |
| 4204 | if (s[1] == 0 && (*s == '*' || *s == '@')) { |
| 4205 | if (dolc > 1) { |
| 4206 | /* currently this does not distinguish $* and $@ */ |
| 4207 | /* should check dollar */ |
| 4208 | e.linep = s; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4209 | PUSHIO(awordlist, dolv + 1, dolchar); |
| 4210 | return (0); |
| 4211 | } else { /* trap the nasty ${=} */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4212 | s[0] = '1'; |
| 4213 | s[1] = 0; |
| 4214 | } |
| 4215 | } |
| 4216 | vp = lookup(s); |
| 4217 | if ((dolp = vp->value) == null) { |
| 4218 | switch (c) { |
| 4219 | case '=': |
| 4220 | if (isdigit(*s)) { |
| 4221 | err("cannot use ${...=...} with $n"); |
| 4222 | gflg++; |
| 4223 | break; |
| 4224 | } |
| 4225 | setval(vp, cp); |
| 4226 | dolp = vp->value; |
| 4227 | break; |
| 4228 | |
| 4229 | case '-': |
| 4230 | dolp = strsave(cp, areanum); |
| 4231 | break; |
| 4232 | |
| 4233 | case '?': |
| 4234 | if (*cp == 0) { |
| 4235 | prs("missing value for "); |
| 4236 | err(s); |
| 4237 | } else |
| 4238 | err(cp); |
| 4239 | gflg++; |
| 4240 | break; |
| 4241 | } |
| 4242 | } else if (c == '+') |
| 4243 | dolp = strsave(cp, areanum); |
| 4244 | if (flag['u'] && dolp == null) { |
| 4245 | prs("unset variable: "); |
| 4246 | err(s); |
| 4247 | gflg++; |
| 4248 | } |
| 4249 | e.linep = s; |
| 4250 | PUSHIO(aword, dolp, quoted ? qstrchar : strchar); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4251 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | /* |
| 4255 | * Run the command in `...` and read its output. |
| 4256 | */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4257 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4258 | static int grave(quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4259 | int quoted; |
| 4260 | { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4261 | char *cp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4262 | REGISTER int i; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4263 | int j; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4264 | int pf[2]; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4265 | static char child_cmd[LINELIM]; |
| 4266 | char *src; |
| 4267 | char *dest; |
| 4268 | int count; |
| 4269 | int ignore; |
| 4270 | int ignore_once; |
| 4271 | char *argument_list[4]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4272 | |
| 4273 | #if __GNUC__ |
| 4274 | /* Avoid longjmp clobbering */ |
| 4275 | (void) &cp; |
| 4276 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4277 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4278 | for (cp = e.iop->argp->aword; *cp != '`'; cp++) |
| 4279 | if (*cp == 0) { |
| 4280 | err("no closing `"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4281 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4282 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4283 | |
| 4284 | /* string copy with dollar expansion */ |
| 4285 | src = e.iop->argp->aword; |
| 4286 | dest = child_cmd; |
| 4287 | count = 0; |
| 4288 | ignore = 0; |
| 4289 | ignore_once = 0; |
| 4290 | while ((*src != '`') && (count < LINELIM)) { |
| 4291 | if (*src == '\'') |
| 4292 | ignore = !ignore; |
| 4293 | if (*src == '\\') |
| 4294 | ignore_once = 1; |
| 4295 | if (*src == '$' && !ignore && !ignore_once) { |
| 4296 | struct var *vp; |
| 4297 | char var_name[LINELIM]; |
| 4298 | char alt_value[LINELIM]; |
| 4299 | int var_index = 0; |
| 4300 | int alt_index = 0; |
| 4301 | char operator = 0; |
| 4302 | int braces = 0; |
| 4303 | char *value; |
| 4304 | |
| 4305 | src++; |
| 4306 | if (*src == '{') { |
| 4307 | braces = 1; |
| 4308 | src++; |
| 4309 | } |
| 4310 | |
| 4311 | var_name[var_index++] = *src++; |
| 4312 | while (isalnum(*src)) |
| 4313 | var_name[var_index++] = *src++; |
| 4314 | var_name[var_index] = 0; |
| 4315 | |
| 4316 | if (braces) { |
| 4317 | switch (*src) { |
| 4318 | case '}': |
| 4319 | break; |
| 4320 | case '-': |
| 4321 | case '=': |
| 4322 | case '+': |
| 4323 | case '?': |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4324 | operator = * src; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4325 | break; |
| 4326 | default: |
| 4327 | err("unclosed ${\n"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4328 | return (0); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4329 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4330 | if (operator) { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4331 | src++; |
| 4332 | while (*src && (*src != '}')) { |
| 4333 | alt_value[alt_index++] = *src++; |
| 4334 | } |
| 4335 | alt_value[alt_index] = 0; |
| 4336 | if (*src != '}') { |
| 4337 | err("unclosed ${\n"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4338 | return (0); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4339 | } |
| 4340 | } |
| 4341 | src++; |
| 4342 | } |
| 4343 | |
| 4344 | vp = lookup(var_name); |
| 4345 | if (vp->value != null) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4346 | value = (operator == '+') ? alt_value : vp->value; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4347 | else if (operator == '?') { |
| 4348 | err(alt_value); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4349 | return (0); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4350 | } else if (alt_index && (operator != '+')) { |
| 4351 | value = alt_value; |
| 4352 | if (operator == '=') |
| 4353 | setval(vp, value); |
| 4354 | } else |
| 4355 | continue; |
| 4356 | |
| 4357 | while (*value && (count < LINELIM)) { |
| 4358 | *dest++ = *value++; |
| 4359 | count++; |
| 4360 | } |
| 4361 | } else { |
| 4362 | *dest++ = *src++; |
| 4363 | count++; |
| 4364 | ignore_once = 0; |
| 4365 | } |
| 4366 | } |
| 4367 | *dest = '\0'; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4368 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4369 | if (openpipe(pf) < 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4370 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4371 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4372 | while ((i = vfork()) == -1 && errno == EAGAIN); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4373 | |
| 4374 | DBGPRINTF3(("GRAVE: i is %d\n", io)); |
| 4375 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4376 | if (i < 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4377 | closepipe(pf); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4378 | err((char *) bb_msg_memory_exhausted); |
| 4379 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4380 | } |
| 4381 | if (i != 0) { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4382 | waitpid(i, NULL, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4383 | e.iop->argp->aword = ++cp; |
| 4384 | close(pf[1]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4385 | PUSHIO(afile, remap(pf[0]), |
| 4386 | (int (*)(struct ioarg *)) ((quoted) ? qgravechar : |
| 4387 | gravechar)); |
| 4388 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4389 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4390 | /* allow trapped signals */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4391 | /* XXX - Maybe this signal stuff should go as well? */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4392 | for (j = 0; j <= _NSIG; j++) |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4393 | if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN) |
| 4394 | signal(j, SIG_DFL); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4395 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4396 | dup2(pf[1], 1); |
| 4397 | closepipe(pf); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4398 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4399 | argument_list[0] = (char *) DEFAULT_SHELL; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4400 | argument_list[1] = "-c"; |
| 4401 | argument_list[2] = child_cmd; |
| 4402 | argument_list[3] = 0; |
| 4403 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4404 | cp = rexecve(argument_list[0], argument_list, makenv(1)); |
| 4405 | prs(argument_list[0]); |
| 4406 | prs(": "); |
| 4407 | err(cp); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4408 | _exit(1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4411 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4412 | static char *unquote(as) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4413 | REGISTER char *as; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4414 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4415 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4416 | |
| 4417 | if ((s = as) != NULL) |
| 4418 | while (*s) |
| 4419 | *s++ &= ~QUOTE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4420 | return (as); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4421 | } |
| 4422 | |
| 4423 | /* -------- glob.c -------- */ |
| 4424 | |
| 4425 | /* |
| 4426 | * glob |
| 4427 | */ |
| 4428 | |
| 4429 | #define scopy(x) strsave((x), areanum) |
| 4430 | #define BLKSIZ 512 |
| 4431 | #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent)) |
| 4432 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4433 | static struct wdblock *cl, *nl; |
| 4434 | static char spcl[] = "[?*"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4435 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4436 | static struct wdblock *glob(cp, wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4437 | char *cp; |
| 4438 | struct wdblock *wb; |
| 4439 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4440 | REGISTER int i; |
| 4441 | REGISTER char *pp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4442 | |
| 4443 | if (cp == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4444 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4445 | i = 0; |
| 4446 | for (pp = cp; *pp; pp++) |
| 4447 | if (any(*pp, spcl)) |
| 4448 | i++; |
| 4449 | else if (!any(*pp & ~QUOTE, spcl)) |
| 4450 | *pp &= ~QUOTE; |
| 4451 | if (i != 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4452 | for (cl = addword(scopy(cp), (struct wdblock *) 0); anyspcl(cl); |
| 4453 | cl = nl) { |
| 4454 | nl = newword(cl->w_nword * 2); |
| 4455 | for (i = 0; i < cl->w_nword; i++) { /* for each argument */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4456 | for (pp = cl->w_words[i]; *pp; pp++) |
| 4457 | if (any(*pp, spcl)) { |
| 4458 | globname(cl->w_words[i], pp); |
| 4459 | break; |
| 4460 | } |
| 4461 | if (*pp == '\0') |
| 4462 | nl = addword(scopy(cl->w_words[i]), nl); |
| 4463 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4464 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4465 | DELETE(cl->w_words[i]); |
| 4466 | DELETE(cl); |
| 4467 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4468 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4469 | unquote(cl->w_words[i]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4470 | glob0((char *) cl->w_words, cl->w_nword, sizeof(char *), xstrcmp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4471 | if (cl->w_nword) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4472 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4473 | wb = addword(cl->w_words[i], wb); |
| 4474 | DELETE(cl); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4475 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4476 | } |
| 4477 | } |
| 4478 | wb = addword(unquote(cp), wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4479 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4482 | static void globname(we, pp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4483 | char *we; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4484 | REGISTER char *pp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4485 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4486 | REGISTER char *np, *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4487 | char *name, *gp, *dp; |
| 4488 | int k; |
| 4489 | DIR *dirp; |
| 4490 | struct dirent *de; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4491 | char dname[NAME_MAX + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4492 | struct stat dbuf; |
| 4493 | |
| 4494 | for (np = we; np != pp; pp--) |
| 4495 | if (pp[-1] == '/') |
| 4496 | break; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4497 | for (dp = cp = space((int) (pp - np) + 3); np < pp;) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4498 | *cp++ = *np++; |
| 4499 | *cp++ = '.'; |
| 4500 | *cp = '\0'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4501 | for (gp = cp = space(strlen(pp) + 1); *np && *np != '/';) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4502 | *cp++ = *np++; |
| 4503 | *cp = '\0'; |
| 4504 | dirp = opendir(dp); |
| 4505 | if (dirp == 0) { |
| 4506 | DELETE(dp); |
| 4507 | DELETE(gp); |
| 4508 | return; |
| 4509 | } |
| 4510 | dname[NAME_MAX] = '\0'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4511 | while ((de = readdir(dirp)) != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4512 | /* XXX Hmmm... What this could be? (abial) */ |
| 4513 | /* |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4514 | if (ent[j].d_ino == 0) |
| 4515 | continue; |
| 4516 | */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4517 | strncpy(dname, de->d_name, NAME_MAX); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4518 | if (dname[0] == '.') |
| 4519 | if (*gp != '.') |
| 4520 | continue; |
| 4521 | for (k = 0; k < NAME_MAX; k++) |
| 4522 | if (any(dname[k], spcl)) |
| 4523 | dname[k] |= QUOTE; |
| 4524 | if (gmatch(dname, gp)) { |
| 4525 | name = generate(we, pp, dname, np); |
| 4526 | if (*np && !anys(np, spcl)) { |
| 4527 | if (stat(name, &dbuf)) { |
| 4528 | DELETE(name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4529 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4530 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4531 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4532 | nl = addword(name, nl); |
| 4533 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4534 | } |
| 4535 | closedir(dirp); |
| 4536 | DELETE(dp); |
| 4537 | DELETE(gp); |
| 4538 | } |
| 4539 | |
| 4540 | /* |
| 4541 | * generate a pathname as below. |
| 4542 | * start..end1 / middle end |
| 4543 | * the slashes come for free |
| 4544 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4545 | static char *generate(start1, end1, middle, end) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4546 | char *start1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4547 | REGISTER char *end1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4548 | char *middle, *end; |
| 4549 | { |
| 4550 | char *p; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4551 | REGISTER char *op, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4552 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4553 | p = op = |
| 4554 | space((int) (end1 - start1) + strlen(middle) + strlen(end) + 2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4555 | for (xp = start1; xp != end1;) |
| 4556 | *op++ = *xp++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4557 | for (xp = middle; (*op++ = *xp++) != '\0';); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4558 | op--; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4559 | for (xp = end; (*op++ = *xp++) != '\0';); |
| 4560 | return (p); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4563 | static int anyspcl(wb) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4564 | REGISTER struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4565 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4566 | REGISTER int i; |
| 4567 | REGISTER char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4568 | |
| 4569 | wd = wb->w_words; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4570 | for (i = 0; i < wb->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4571 | if (anys(spcl, *wd++)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4572 | return (1); |
| 4573 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4576 | static int xstrcmp(p1, p2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4577 | char *p1, *p2; |
| 4578 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4579 | return (strcmp(*(char **) p1, *(char **) p2)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | /* -------- word.c -------- */ |
| 4583 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4584 | static struct wdblock *newword(nw) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4585 | REGISTER int nw; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4586 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4587 | REGISTER struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4588 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4589 | wb = (struct wdblock *) space(sizeof(*wb) + nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4590 | wb->w_bsize = nw; |
| 4591 | wb->w_nword = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4592 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4595 | static struct wdblock *addword(wd, wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4596 | char *wd; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4597 | REGISTER struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4598 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4599 | REGISTER struct wdblock *wb2; |
| 4600 | REGISTER int nw; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4601 | |
| 4602 | if (wb == NULL) |
| 4603 | wb = newword(NSTART); |
| 4604 | if ((nw = wb->w_nword) >= wb->w_bsize) { |
| 4605 | wb2 = newword(nw * 2); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4606 | memcpy((char *) wb2->w_words, (char *) wb->w_words, |
| 4607 | nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4608 | wb2->w_nword = nw; |
| 4609 | DELETE(wb); |
| 4610 | wb = wb2; |
| 4611 | } |
| 4612 | wb->w_words[wb->w_nword++] = wd; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4613 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4614 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4615 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4616 | static |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4617 | char **getwords(wb) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4618 | REGISTER struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4619 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4620 | REGISTER char **wd; |
| 4621 | REGISTER int nb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4622 | |
| 4623 | if (wb == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4624 | return ((char **) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4625 | if (wb->w_nword == 0) { |
| 4626 | DELETE(wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4627 | return ((char **) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4628 | } |
| 4629 | wd = (char **) space(nb = sizeof(*wd) * wb->w_nword); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4630 | memcpy((char *) wd, (char *) wb->w_words, nb); |
| 4631 | DELETE(wb); /* perhaps should done by caller */ |
| 4632 | return (wd); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4633 | } |
| 4634 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4635 | int (*func) (char *, char *); |
| 4636 | int globv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4637 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4638 | static void glob0(a0, a1, a2, a3) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4639 | char *a0; |
| 4640 | unsigned a1; |
| 4641 | int a2; |
| 4642 | int (*a3) (char *, char *); |
| 4643 | { |
| 4644 | func = a3; |
| 4645 | globv = a2; |
| 4646 | glob1(a0, a0 + a1 * a2); |
| 4647 | } |
| 4648 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4649 | static void glob1(base, lim) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4650 | char *base, *lim; |
| 4651 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4652 | REGISTER char *i, *j; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4653 | int v2; |
| 4654 | char *lptr, *hptr; |
| 4655 | int c; |
| 4656 | unsigned n; |
| 4657 | |
| 4658 | |
| 4659 | v2 = globv; |
| 4660 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4661 | top: |
| 4662 | if ((n = (int) (lim - base)) <= v2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4663 | return; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4664 | n = v2 * (n / (2 * v2)); |
| 4665 | hptr = lptr = base + n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4666 | i = base; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4667 | j = lim - v2; |
| 4668 | for (;;) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4669 | if (i < lptr) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4670 | if ((c = (*func) (i, lptr)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4671 | glob2(i, lptr -= v2); |
| 4672 | continue; |
| 4673 | } |
| 4674 | if (c < 0) { |
| 4675 | i += v2; |
| 4676 | continue; |
| 4677 | } |
| 4678 | } |
| 4679 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4680 | begin: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4681 | if (j > hptr) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4682 | if ((c = (*func) (hptr, j)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4683 | glob2(hptr += v2, j); |
| 4684 | goto begin; |
| 4685 | } |
| 4686 | if (c > 0) { |
| 4687 | if (i == lptr) { |
| 4688 | glob3(i, hptr += v2, j); |
| 4689 | i = lptr += v2; |
| 4690 | goto begin; |
| 4691 | } |
| 4692 | glob2(i, j); |
| 4693 | j -= v2; |
| 4694 | i += v2; |
| 4695 | continue; |
| 4696 | } |
| 4697 | j -= v2; |
| 4698 | goto begin; |
| 4699 | } |
| 4700 | |
| 4701 | |
| 4702 | if (i == lptr) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4703 | if (lptr - base >= lim - hptr) { |
| 4704 | glob1(hptr + v2, lim); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4705 | lim = lptr; |
| 4706 | } else { |
| 4707 | glob1(base, lptr); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4708 | base = hptr + v2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4709 | } |
| 4710 | goto top; |
| 4711 | } |
| 4712 | |
| 4713 | |
| 4714 | glob3(j, lptr -= v2, i); |
| 4715 | j = hptr -= v2; |
| 4716 | } |
| 4717 | } |
| 4718 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4719 | static void glob2(i, j) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4720 | char *i, *j; |
| 4721 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4722 | REGISTER char *index1, *index2, c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4723 | int m; |
| 4724 | |
| 4725 | m = globv; |
| 4726 | index1 = i; |
| 4727 | index2 = j; |
| 4728 | do { |
| 4729 | c = *index1; |
| 4730 | *index1++ = *index2; |
| 4731 | *index2++ = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4732 | } while (--m); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4733 | } |
| 4734 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4735 | static void glob3(i, j, k) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4736 | char *i, *j, *k; |
| 4737 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4738 | REGISTER char *index1, *index2, *index3; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4739 | int c; |
| 4740 | int m; |
| 4741 | |
| 4742 | m = globv; |
| 4743 | index1 = i; |
| 4744 | index2 = j; |
| 4745 | index3 = k; |
| 4746 | do { |
| 4747 | c = *index1; |
| 4748 | *index1++ = *index3; |
| 4749 | *index3++ = *index2; |
| 4750 | *index2++ = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4751 | } while (--m); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | /* -------- io.c -------- */ |
| 4755 | |
| 4756 | /* |
| 4757 | * shell IO |
| 4758 | */ |
| 4759 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4760 | static int my_getc(int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4761 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4762 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4763 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4764 | if (e.linep > elinep) { |
| 4765 | while ((c = readc()) != '\n' && c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4766 | err("input line too long"); |
| 4767 | gflg++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4768 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4769 | } |
| 4770 | c = readc(); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4771 | if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4772 | if (c == '\\') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4773 | c = readc(); |
| 4774 | if (c == '\n' && ec != '\"') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4775 | return (my_getc(ec)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4776 | c |= QUOTE; |
| 4777 | } |
| 4778 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4779 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4782 | static void unget(c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4783 | int c; |
| 4784 | { |
| 4785 | if (e.iop >= e.iobase) |
| 4786 | e.iop->peekc = c; |
| 4787 | } |
| 4788 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4789 | static int eofc() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4790 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4791 | return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4794 | static int readc() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4795 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4796 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4797 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4798 | RCPRINTF(("READC: e.iop 0x%x, e.iobase 0x%x\n", e.iop, e.iobase)); |
| 4799 | |
| 4800 | for (; e.iop >= e.iobase; e.iop--) { |
| 4801 | RCPRINTF(("READC: e.iop 0x%x, peekc 0x%x\n", e.iop, e.iop->peekc)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4802 | if ((c = e.iop->peekc) != '\0') { |
| 4803 | e.iop->peekc = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4804 | return (c); |
| 4805 | } else { |
| 4806 | if (e.iop->prev != 0) { |
| 4807 | if ((c = (*e.iop->iofn) (e.iop->argp, e.iop)) != '\0') { |
| 4808 | if (c == -1) { |
| 4809 | e.iop++; |
| 4810 | continue; |
| 4811 | } |
| 4812 | if (e.iop == iostack) |
| 4813 | ioecho(c); |
| 4814 | return (e.iop->prev = c); |
| 4815 | } else if (e.iop->task == XIO && e.iop->prev != '\n') { |
| 4816 | e.iop->prev = 0; |
| 4817 | if (e.iop == iostack) |
| 4818 | ioecho('\n'); |
| 4819 | return '\n'; |
| 4820 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4821 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4822 | if (e.iop->task == XIO) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4823 | if (multiline) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4824 | return e.iop->prev = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4825 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4826 | if (interactive && e.iop == iostack + 1) { |
| 4827 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
| 4828 | current_prompt = prompt->value; |
| 4829 | #else |
| 4830 | prs(prompt->value); |
| 4831 | #endif |
| 4832 | } |
| 4833 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4834 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4835 | |
| 4836 | } /* FOR */ |
| 4837 | |
| 4838 | if (e.iop >= iostack) { |
| 4839 | RCPRINTF(("READC: return 0, e.iop 0x%x\n", e.iop)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4840 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4841 | } |
| 4842 | |
| 4843 | DBGPRINTF(("READC: leave()...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4844 | leave(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4845 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4846 | /* NOTREACHED */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4847 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4850 | static void ioecho(c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4851 | char c; |
| 4852 | { |
| 4853 | if (flag['v']) |
| 4854 | write(2, &c, sizeof c); |
| 4855 | } |
| 4856 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4857 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4858 | static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4859 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4860 | DBGPRINTF(("PUSHIO: argp 0x%x, argp->afid 0x%x, e.iop 0x%x\n", argp, |
| 4861 | argp->afid, e.iop)); |
| 4862 | |
| 4863 | /* Set env ptr for io source to next array spot and check for array overflow */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4864 | if (++e.iop >= &iostack[NPUSH]) { |
| 4865 | e.iop--; |
| 4866 | err("Shell input nested too deeply"); |
| 4867 | gflg++; |
| 4868 | return; |
| 4869 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4870 | |
| 4871 | /* We did not overflow the NPUSH array spots so setup data structs */ |
| 4872 | |
| 4873 | e.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] | 4874 | |
| 4875 | if (argp->afid != AFID_NOBUF) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4876 | e.iop->argp = argp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4877 | else { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4878 | |
| 4879 | e.iop->argp = ioargstack + (e.iop - iostack); /* MAL - index into stack */ |
| 4880 | *e.iop->argp = *argp; /* copy data from temp area into stack spot */ |
| 4881 | |
| 4882 | /* MAL - mainbuf is for 1st data source (command line?) and all nested use a single shared buffer? */ |
| 4883 | |
| 4884 | if (e.iop == &iostack[0]) |
| 4885 | e.iop->argp->afbuf = &mainbuf; |
| 4886 | else |
| 4887 | e.iop->argp->afbuf = &sharedbuf; |
| 4888 | |
| 4889 | /* MAL - if not a termimal AND (commandline OR readable file) then give it a buffer id? */ |
| 4890 | /* This line appears to be active when running scripts from command line */ |
| 4891 | if ((isatty(e.iop->argp->afile) == 0) |
| 4892 | && (e.iop == &iostack[0] |
| 4893 | || lseek(e.iop->argp->afile, 0L, 1) != -1)) { |
| 4894 | if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */ |
| 4895 | bufid = AFID_ID; /* AFID_ID = 0 */ |
| 4896 | |
| 4897 | e.iop->argp->afid = bufid; /* assign buffer id */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4898 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4899 | |
| 4900 | DBGPRINTF(("PUSHIO: iostack 0x%x, e.iop 0x%x, afbuf 0x%x\n", |
| 4901 | iostack, e.iop, e.iop->argp->afbuf)); |
| 4902 | DBGPRINTF(("PUSHIO: mbuf 0x%x, sbuf 0x%x, bid %d, e.iop 0x%x\n", |
| 4903 | &mainbuf, &sharedbuf, bufid, e.iop)); |
| 4904 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4905 | } |
| 4906 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4907 | e.iop->prev = ~'\n'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4908 | e.iop->peekc = 0; |
| 4909 | e.iop->xchar = 0; |
| 4910 | e.iop->nlcount = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4911 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4912 | if (fn == filechar || fn == linechar) |
| 4913 | e.iop->task = XIO; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4914 | else if (fn == (int (*)(struct ioarg *)) gravechar |
| 4915 | || fn == (int (*)(struct ioarg *)) qgravechar) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4916 | e.iop->task = XGRAVE; |
| 4917 | else |
| 4918 | e.iop->task = XOTHER; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4919 | |
| 4920 | return; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4921 | } |
| 4922 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4923 | static struct io *setbase(ip) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4924 | struct io *ip; |
| 4925 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4926 | REGISTER struct io *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4927 | |
| 4928 | xp = e.iobase; |
| 4929 | e.iobase = ip; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4930 | return (xp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4931 | } |
| 4932 | |
| 4933 | /* |
| 4934 | * Input generating functions |
| 4935 | */ |
| 4936 | |
| 4937 | /* |
| 4938 | * Produce the characters of a string, then a newline, then EOF. |
| 4939 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4940 | static int nlchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4941 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4942 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4943 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4944 | |
| 4945 | if (ap->aword == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4946 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4947 | if ((c = *ap->aword++) == 0) { |
| 4948 | ap->aword = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4949 | return ('\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4950 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4951 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | /* |
| 4955 | * Given a list of words, produce the characters |
| 4956 | * in them, with a space after each word. |
| 4957 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4958 | static int wdchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4959 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4960 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4961 | REGISTER char c; |
| 4962 | REGISTER char **wl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4963 | |
| 4964 | if ((wl = ap->awordlist) == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4965 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4966 | if (*wl != NULL) { |
| 4967 | if ((c = *(*wl)++) != 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4968 | return (c & 0177); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4969 | ap->awordlist++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4970 | return (' '); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4971 | } |
| 4972 | ap->awordlist = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4973 | return ('\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4974 | } |
| 4975 | |
| 4976 | /* |
| 4977 | * Return the characters of a list of words, |
| 4978 | * producing a space between them. |
| 4979 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4980 | static int dolchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4981 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4982 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4983 | REGISTER char *wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4984 | |
| 4985 | if ((wp = *ap->awordlist++) != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4986 | PUSHIO(aword, wp, *ap->awordlist == NULL ? strchar : xxchar); |
| 4987 | return (-1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4988 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4989 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4992 | static int xxchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4993 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4994 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 4995 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4996 | |
| 4997 | if (ap->aword == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4998 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4999 | if ((c = *ap->aword++) == '\0') { |
| 5000 | ap->aword = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5001 | return (' '); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5002 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5003 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
| 5006 | /* |
| 5007 | * Produce the characters from a single word (string). |
| 5008 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5009 | static int strchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5010 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5011 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5012 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5013 | |
| 5014 | if (ap->aword == NULL || (c = *ap->aword++) == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5015 | return (0); |
| 5016 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | /* |
| 5020 | * Produce quoted characters from a single word (string). |
| 5021 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5022 | static int qstrchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5023 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5024 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5025 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5026 | |
| 5027 | if (ap->aword == NULL || (c = *ap->aword++) == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5028 | return (0); |
| 5029 | return (c | QUOTE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
| 5032 | /* |
| 5033 | * Return the characters from a file. |
| 5034 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5035 | static int filechar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5036 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5037 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5038 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5039 | char c; |
| 5040 | struct iobuf *bp = ap->afbuf; |
| 5041 | |
| 5042 | if (ap->afid != AFID_NOBUF) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5043 | if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5044 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5045 | if (i) |
| 5046 | lseek(ap->afile, ap->afpos, 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5047 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5048 | i = safe_read(ap->afile, bp->buf, sizeof(bp->buf)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5049 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5050 | if (i <= 0) { |
| 5051 | closef(ap->afile); |
| 5052 | return 0; |
| 5053 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5054 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5055 | bp->id = ap->afid; |
| 5056 | bp->ebufp = (bp->bufp = bp->buf) + i; |
| 5057 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5058 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5059 | ap->afpos++; |
| 5060 | return *bp->bufp++ & 0177; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5061 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5062 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 5063 | if (interactive && isatty(ap->afile)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5064 | static char mycommand[BUFSIZ]; |
| 5065 | static int position = 0, size = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5066 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5067 | while (size == 0 || position >= size) { |
| 5068 | cmdedit_read_input(current_prompt, mycommand); |
| 5069 | size = strlen(mycommand); |
| 5070 | position = 0; |
| 5071 | } |
| 5072 | c = mycommand[position]; |
| 5073 | position++; |
| 5074 | return (c); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5075 | } else |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5076 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5077 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5078 | { |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 5079 | i = safe_read(ap->afile, &c, sizeof(c)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5080 | return (i == sizeof(c) ? (c & 0x7f) : (closef(ap->afile), 0)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5081 | } |
| 5082 | } |
| 5083 | |
| 5084 | /* |
| 5085 | * Return the characters from a here temp file. |
| 5086 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5087 | static int herechar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5088 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5089 | { |
| 5090 | char c; |
| 5091 | |
| 5092 | |
| 5093 | if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) { |
| 5094 | close(ap->afile); |
| 5095 | c = 0; |
| 5096 | } |
| 5097 | return (c); |
| 5098 | |
| 5099 | } |
| 5100 | |
| 5101 | /* |
| 5102 | * Return the characters produced by a process (`...`). |
| 5103 | * Quote them if required, and remove any trailing newline characters. |
| 5104 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5105 | static int gravechar(ap, iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5106 | struct ioarg *ap; |
| 5107 | struct io *iop; |
| 5108 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5109 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5110 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5111 | if ((c = qgravechar(ap, iop) & ~QUOTE) == '\n') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5112 | c = ' '; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5113 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5116 | static int qgravechar(ap, iop) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5117 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5118 | struct io *iop; |
| 5119 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5120 | REGISTER int c; |
| 5121 | |
| 5122 | DBGPRINTF3(("QGRAVECHAR: enter, ap=0x%x, iop=0x%x\n", ap, iop)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5123 | |
| 5124 | if (iop->xchar) { |
| 5125 | if (iop->nlcount) { |
| 5126 | iop->nlcount--; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5127 | return ('\n' | QUOTE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5128 | } |
| 5129 | c = iop->xchar; |
| 5130 | iop->xchar = 0; |
| 5131 | } else if ((c = filechar(ap)) == '\n') { |
| 5132 | iop->nlcount = 1; |
| 5133 | while ((c = filechar(ap)) == '\n') |
| 5134 | iop->nlcount++; |
| 5135 | iop->xchar = c; |
| 5136 | if (c == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5137 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5138 | iop->nlcount--; |
| 5139 | c = '\n'; |
| 5140 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5141 | return (c != 0 ? c | QUOTE : 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
| 5144 | /* |
| 5145 | * Return a single command (usually the first line) from a file. |
| 5146 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5147 | static int linechar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5148 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5149 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5150 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5151 | |
| 5152 | if ((c = filechar(ap)) == '\n') { |
| 5153 | if (!multiline) { |
| 5154 | closef(ap->afile); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5155 | ap->afile = -1; /* illegal value */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5156 | } |
| 5157 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5158 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5161 | static void prs(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5162 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5163 | { |
| 5164 | if (*s) |
| 5165 | write(2, s, strlen(s)); |
| 5166 | } |
| 5167 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5168 | static void prn(u) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5169 | unsigned u; |
| 5170 | { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 5171 | prs(itoa(u)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5172 | } |
| 5173 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5174 | static void closef(i) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5175 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5176 | { |
| 5177 | if (i > 2) |
| 5178 | close(i); |
| 5179 | } |
| 5180 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5181 | static void closeall() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5182 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5183 | REGISTER int u; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5184 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5185 | for (u = NUFILE; u < NOFILE;) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5186 | close(u++); |
| 5187 | } |
| 5188 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5189 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5190 | /* |
| 5191 | * remap fd into Shell's fd space |
| 5192 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5193 | static int remap(fd) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5194 | REGISTER int fd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5195 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5196 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5197 | int map[NOFILE]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5198 | int newfd; |
| 5199 | |
| 5200 | |
| 5201 | DBGPRINTF(("REMAP: fd=%d, e.iofd=%d\n", fd, e.iofd)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5202 | |
| 5203 | if (fd < e.iofd) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5204 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5205 | map[i] = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5206 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5207 | do { |
| 5208 | map[fd] = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5209 | newfd = dup(fd); |
| 5210 | fd = newfd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5211 | } while (fd >= 0 && fd < e.iofd); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5212 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5213 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5214 | if (map[i]) |
| 5215 | close(i); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5216 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5217 | if (fd < 0) |
| 5218 | err("too many files open in shell"); |
| 5219 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5220 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5221 | return (fd); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5224 | static int openpipe(pv) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5225 | REGISTER int *pv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5226 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5227 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5228 | |
| 5229 | if ((i = pipe(pv)) < 0) |
| 5230 | err("can't create pipe - try again"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5231 | return (i); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5232 | } |
| 5233 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5234 | static void closepipe(pv) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5235 | REGISTER int *pv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5236 | { |
| 5237 | if (pv != NULL) { |
| 5238 | close(*pv++); |
| 5239 | close(*pv); |
| 5240 | } |
| 5241 | } |
| 5242 | |
| 5243 | /* -------- here.c -------- */ |
| 5244 | |
| 5245 | /* |
| 5246 | * here documents |
| 5247 | */ |
| 5248 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5249 | static void markhere(s, iop) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5250 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5251 | struct ioword *iop; |
| 5252 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5253 | REGISTER struct here *h, *lh; |
| 5254 | |
| 5255 | DBGPRINTF7(("MARKHERE: enter, s=0x%x\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5256 | |
| 5257 | h = (struct here *) space(sizeof(struct here)); |
| 5258 | if (h == 0) |
| 5259 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5260 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5261 | h->h_tag = evalstr(s, DOSUB); |
| 5262 | if (h->h_tag == 0) |
| 5263 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5264 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5265 | h->h_iop = iop; |
| 5266 | iop->io_name = 0; |
| 5267 | h->h_next = NULL; |
| 5268 | if (inhere == 0) |
| 5269 | inhere = h; |
| 5270 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5271 | for (lh = inhere; lh != NULL; lh = lh->h_next) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5272 | if (lh->h_next == 0) { |
| 5273 | lh->h_next = h; |
| 5274 | break; |
| 5275 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5276 | iop->io_flag |= IOHERE | IOXHERE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5277 | for (s = h->h_tag; *s; s++) |
| 5278 | if (*s & QUOTE) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5279 | iop->io_flag &= ~IOXHERE; |
| 5280 | *s &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5281 | } |
| 5282 | h->h_dosub = iop->io_flag & IOXHERE; |
| 5283 | } |
| 5284 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5285 | static void gethere() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5286 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5287 | REGISTER struct here *h, *hp; |
| 5288 | |
| 5289 | DBGPRINTF7(("GETHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5290 | |
| 5291 | /* Scan here files first leaving inhere list in place */ |
| 5292 | for (hp = h = inhere; h != NULL; hp = h, h = h->h_next) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5293 | 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] | 5294 | |
| 5295 | /* Make inhere list active - keep list intact for scraphere */ |
| 5296 | if (hp != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5297 | hp->h_next = acthere; |
| 5298 | acthere = inhere; |
| 5299 | inhere = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5300 | } |
| 5301 | } |
| 5302 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5303 | static void readhere(name, s, ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5304 | char **name; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5305 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5306 | int ec; |
| 5307 | { |
| 5308 | int tf; |
| 5309 | char tname[30] = ".msh_XXXXXX"; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5310 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5311 | jmp_buf ev; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5312 | char myline[LINELIM + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5313 | char *thenext; |
| 5314 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5315 | DBGPRINTF7(("READHERE: enter, name=0x%x, s=0x%x\n", name, s)); |
| 5316 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5317 | tf = mkstemp(tname); |
| 5318 | if (tf < 0) |
| 5319 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5320 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5321 | *name = strsave(tname, areanum); |
| 5322 | if (newenv(setjmp(errpt = ev)) != 0) |
| 5323 | unlink(tname); |
| 5324 | else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5325 | pushio(e.iop->argp, (int (*)(struct ioarg *)) e.iop->iofn); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5326 | e.iobase = e.iop; |
| 5327 | for (;;) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5328 | if (interactive && e.iop <= iostack) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5329 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5330 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5331 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5332 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5333 | #endif |
| 5334 | } |
| 5335 | thenext = myline; |
| 5336 | while ((c = my_getc(ec)) != '\n' && c) { |
| 5337 | if (ec == '\'') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5338 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5339 | if (thenext >= &myline[LINELIM]) { |
| 5340 | c = 0; |
| 5341 | break; |
| 5342 | } |
| 5343 | *thenext++ = c; |
| 5344 | } |
| 5345 | *thenext = 0; |
| 5346 | if (strcmp(s, myline) == 0 || c == 0) |
| 5347 | break; |
| 5348 | *thenext++ = '\n'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5349 | write(tf, myline, (int) (thenext - myline)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5350 | } |
| 5351 | if (c == 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5352 | prs("here document `"); |
| 5353 | prs(s); |
| 5354 | err("' unclosed"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5355 | } |
| 5356 | quitenv(); |
| 5357 | } |
| 5358 | close(tf); |
| 5359 | } |
| 5360 | |
| 5361 | /* |
| 5362 | * open here temp file. |
| 5363 | * if unquoted here, expand here temp file into second temp file. |
| 5364 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5365 | static int herein(hname, xdoll) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5366 | char *hname; |
| 5367 | int xdoll; |
| 5368 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5369 | REGISTER int hf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5370 | int tf; |
| 5371 | |
| 5372 | #if __GNUC__ |
| 5373 | /* Avoid longjmp clobbering */ |
| 5374 | (void) &tf; |
| 5375 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5376 | if (hname == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5377 | return (-1); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5378 | |
| 5379 | DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll)); |
| 5380 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5381 | hf = open(hname, 0); |
| 5382 | if (hf < 0) |
| 5383 | return (-1); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5384 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5385 | if (xdoll) { |
| 5386 | char c; |
| 5387 | char tname[30] = ".msh_XXXXXX"; |
| 5388 | jmp_buf ev; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5389 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5390 | tf = mkstemp(tname); |
| 5391 | if (tf < 0) |
| 5392 | return (-1); |
| 5393 | if (newenv(setjmp(errpt = ev)) == 0) { |
| 5394 | PUSHIO(afile, hf, herechar); |
| 5395 | setbase(e.iop); |
| 5396 | while ((c = subgetc(0, 0)) != 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5397 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5398 | write(tf, &c, sizeof c); |
| 5399 | } |
| 5400 | quitenv(); |
| 5401 | } else |
| 5402 | unlink(tname); |
| 5403 | close(tf); |
| 5404 | tf = open(tname, 0); |
| 5405 | unlink(tname); |
| 5406 | return (tf); |
| 5407 | } else |
| 5408 | return (hf); |
| 5409 | } |
| 5410 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5411 | static void scraphere() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5412 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5413 | REGISTER struct here *h; |
| 5414 | |
| 5415 | DBGPRINTF7(("SCRAPHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5416 | |
| 5417 | for (h = inhere; h != NULL; h = h->h_next) { |
| 5418 | if (h->h_iop && h->h_iop->io_name) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5419 | unlink(h->h_iop->io_name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5420 | } |
| 5421 | inhere = NULL; |
| 5422 | } |
| 5423 | |
| 5424 | /* unlink here temp files before a freearea(area) */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5425 | static void freehere(area) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5426 | int area; |
| 5427 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame^] | 5428 | REGISTER struct here *h, *hl; |
| 5429 | |
| 5430 | DBGPRINTF6(("FREEHERE: enter, area=%d\n", area)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5431 | |
| 5432 | hl = NULL; |
| 5433 | for (h = acthere; h != NULL; h = h->h_next) |
| 5434 | if (getarea((char *) h) >= area) { |
| 5435 | if (h->h_iop->io_name != NULL) |
| 5436 | unlink(h->h_iop->io_name); |
| 5437 | if (hl == NULL) |
| 5438 | acthere = h->h_next; |
| 5439 | else |
| 5440 | hl->h_next = h->h_next; |
| 5441 | } else |
| 5442 | hl = h; |
| 5443 | } |
| 5444 | |
| 5445 | |
| 5446 | |
| 5447 | /* |
| 5448 | * Copyright (c) 1987,1997, Prentice Hall |
| 5449 | * All rights reserved. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5450 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5451 | * Redistribution and use of the MINIX operating system in source and |
| 5452 | * binary forms, with or without modification, are permitted provided |
| 5453 | * that the following conditions are met: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5454 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5455 | * Redistributions of source code must retain the above copyright |
| 5456 | * notice, this list of conditions and the following disclaimer. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5457 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5458 | * Redistributions in binary form must reproduce the above |
| 5459 | * copyright notice, this list of conditions and the following |
| 5460 | * disclaimer in the documentation and/or other materials provided |
| 5461 | * with the distribution. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5462 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5463 | * Neither the name of Prentice Hall nor the names of the software |
| 5464 | * authors or contributors may be used to endorse or promote |
| 5465 | * products derived from this software without specific prior |
| 5466 | * written permission. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5467 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5468 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND |
| 5469 | * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 5470 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 5471 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 5472 | * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE |
| 5473 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 5474 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 5475 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 5476 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 5477 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 5478 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 5479 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 5480 | * |
| 5481 | */ |