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 | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 302 | static char **makenv(int all, struct wdblock *wb); |
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 | struct op *scantree(struct op *); |
| 702 | static struct op *dowholefile(int, int); |
| 703 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 704 | /* Globals */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 705 | extern char **environ; /* environment pointer */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 706 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 707 | static char **dolv; |
| 708 | static int dolc; |
| 709 | static int exstat; |
| 710 | static char gflg; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 711 | static int interactive = 0; /* Is this an interactive shell */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 712 | static int execflg; |
| 713 | static int multiline; /* \n changed to ; */ |
| 714 | static struct op *outtree; /* result from parser */ |
| 715 | static xint *failpt; |
| 716 | static xint *errpt; |
| 717 | static struct brkcon *brklist; |
| 718 | static int isbreak; |
| 719 | static struct wdblock *wdlist; |
| 720 | static struct wdblock *iolist; |
| 721 | static char *trap[_NSIG + 1]; |
| 722 | static char ourtrap[_NSIG + 1]; |
| 723 | static int trapset; /* trap pending */ |
| 724 | static int yynerrs; /* yacc */ |
| 725 | static char line[LINELIM]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 726 | |
| 727 | #ifdef MSHDEBUG |
| 728 | static struct var *mshdbg_var; |
| 729 | #endif |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 730 | static struct var *vlist; /* dictionary */ |
| 731 | static struct var *homedir; /* home directory */ |
| 732 | static struct var *prompt; /* main prompt */ |
| 733 | static struct var *cprompt; /* continuation prompt */ |
| 734 | static struct var *path; /* search path for commands */ |
| 735 | static struct var *shell; /* shell to interpret command files */ |
| 736 | static struct var *ifs; /* field separators */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 737 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 738 | static int areanum; /* current allocation area */ |
| 739 | static int intr; |
| 740 | static int inparse; |
| 741 | static char flags['z' - 'a' + 1]; |
| 742 | static char *flag = flags - 'a'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 743 | static char *null = ""; |
| 744 | static int heedint = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 745 | static void (*qflag) (int) = SIG_IGN; |
| 746 | static int startl; |
| 747 | static int peeksym; |
| 748 | static int nlseen; |
| 749 | static int iounit = IODEFAULT; |
| 750 | static YYSTYPE yylval; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 751 | static char *elinep = line + sizeof(line) - 5; |
| 752 | |
| 753 | static struct ioarg temparg = { 0, 0, 0, AFID_NOBUF, 0 }; /* temporary for PUSHIO */ |
| 754 | static struct ioarg ioargstack[NPUSH]; |
| 755 | static struct io iostack[NPUSH]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 756 | static struct iobuf sharedbuf = { AFID_NOBUF }; |
| 757 | static struct iobuf mainbuf = { AFID_NOBUF }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 758 | static unsigned bufid = AFID_ID; /* buffer id counter */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 759 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 760 | static struct here *inhere; /* list of hear docs while parsing */ |
| 761 | static struct here *acthere; /* list of active here documents */ |
| 762 | static struct region *areabot; /* bottom of area */ |
| 763 | static struct region *areatop; /* top of area */ |
| 764 | static struct region *areanxt; /* starting point of scan */ |
| 765 | static void *brktop; |
| 766 | static void *brkaddr; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 767 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 768 | static struct env e = { |
| 769 | line, /* linep: char ptr */ |
| 770 | iostack, /* iobase: struct io ptr */ |
| 771 | iostack - 1, /* iop: struct io ptr */ |
| 772 | (xint *) NULL, /* errpt: void ptr for errors? */ |
| 773 | FDBASE, /* iofd: file desc */ |
| 774 | (struct env *) NULL /* oenv: struct env ptr */ |
| 775 | }; |
| 776 | |
| 777 | #ifdef MSHDEBUG |
| 778 | void print_t(struct op *t) |
| 779 | { |
| 780 | DBGPRINTF(("T: t=0x%x, type %s, words=0x%x, IOword=0x%x\n", t, |
| 781 | T_CMD_NAMES[t->type], t->words, t->ioact)); |
| 782 | |
| 783 | if (t->words) { |
| 784 | DBGPRINTF(("T: W1: %s", t->words[0])); |
| 785 | } |
| 786 | |
| 787 | return; |
| 788 | } |
| 789 | |
| 790 | void print_tree(struct op *head) |
| 791 | { |
| 792 | if (head == NULL) { |
| 793 | DBGPRINTF(("PRINT_TREE: no tree\n")); |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | DBGPRINTF(("NODE: 0x%x, left 0x%x, right 0x%x\n", head, head->left, |
| 798 | head->right)); |
| 799 | |
| 800 | if (head->left) |
| 801 | print_tree(head->left); |
| 802 | |
| 803 | if (head->right) |
| 804 | print_tree(head->right); |
| 805 | |
| 806 | return; |
| 807 | } |
| 808 | #endif /* MSHDEBUG */ |
| 809 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 810 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 811 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 812 | static char *current_prompt; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 813 | #endif |
| 814 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 815 | /* -------- sh.c -------- */ |
| 816 | /* |
| 817 | * shell |
| 818 | */ |
| 819 | |
| 820 | |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 821 | extern int msh_main(int argc, char **argv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 822 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 823 | REGISTER int f; |
| 824 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 825 | int cflag; |
| 826 | char *name, **ap; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 827 | int (*iof) (struct ioarg *); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 828 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 829 | DBGPRINTF(("MSH_MAIN: argc %d, environ 0x%x\n", argc, environ)); |
| 830 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 831 | initarea(); |
| 832 | if ((ap = environ) != NULL) { |
| 833 | while (*ap) |
| 834 | assign(*ap++, !COPYV); |
| 835 | for (ap = environ; *ap;) |
| 836 | export(lookup(*ap++)); |
| 837 | } |
| 838 | closeall(); |
| 839 | areanum = 1; |
| 840 | |
| 841 | shell = lookup("SHELL"); |
| 842 | if (shell->value == null) |
Eric Andersen | 7850014 | 2004-08-27 19:55:28 +0000 | [diff] [blame] | 843 | setval(shell, (char *)DEFAULT_SHELL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 844 | export(shell); |
| 845 | |
| 846 | homedir = lookup("HOME"); |
| 847 | if (homedir->value == null) |
| 848 | setval(homedir, "/"); |
| 849 | export(homedir); |
| 850 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 851 | setval(lookup("$"), putn(getpid())); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 852 | |
| 853 | path = lookup("PATH"); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 854 | if (path->value == null) { |
| 855 | if (geteuid() == 0) |
| 856 | setval(path, "/sbin:/bin:/usr/sbin:/usr/bin"); |
| 857 | else |
| 858 | setval(path, "/bin:/usr/bin"); |
| 859 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 860 | export(path); |
| 861 | |
| 862 | ifs = lookup("IFS"); |
| 863 | if (ifs->value == null) |
| 864 | setval(ifs, " \t\n"); |
| 865 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 866 | #ifdef MSHDEBUG |
| 867 | mshdbg_var = lookup("MSHDEBUG"); |
| 868 | if (mshdbg_var->value == null) |
| 869 | setval(mshdbg_var, "0"); |
| 870 | #endif |
| 871 | |
| 872 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 873 | prompt = lookup("PS1"); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 874 | #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 875 | if (prompt->value == null) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 876 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 877 | setval(prompt, "$ "); |
| 878 | if (geteuid() == 0) { |
| 879 | setval(prompt, "# "); |
| 880 | prompt->status &= ~EXPORT; |
| 881 | } |
| 882 | cprompt = lookup("PS2"); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 883 | #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 884 | if (cprompt->value == null) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 885 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 886 | setval(cprompt, "> "); |
| 887 | |
| 888 | iof = filechar; |
| 889 | cflag = 0; |
| 890 | name = *argv++; |
| 891 | if (--argc >= 1) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 892 | if (argv[0][0] == '-' && argv[0][1] != '\0') { |
| 893 | for (s = argv[0] + 1; *s; s++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 894 | switch (*s) { |
| 895 | case 'c': |
| 896 | prompt->status &= ~EXPORT; |
| 897 | cprompt->status &= ~EXPORT; |
| 898 | setval(prompt, ""); |
| 899 | setval(cprompt, ""); |
| 900 | cflag = 1; |
| 901 | if (--argc > 0) |
| 902 | PUSHIO(aword, *++argv, iof = nlchar); |
| 903 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 904 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 905 | case 'q': |
| 906 | qflag = SIG_DFL; |
| 907 | break; |
| 908 | |
| 909 | case 's': |
| 910 | /* standard input */ |
| 911 | break; |
| 912 | |
| 913 | case 't': |
| 914 | prompt->status &= ~EXPORT; |
| 915 | setval(prompt, ""); |
| 916 | iof = linechar; |
| 917 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 918 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 919 | case 'i': |
| 920 | interactive++; |
| 921 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 922 | if (*s >= 'a' && *s <= 'z') |
| 923 | flag[(int) *s]++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 924 | } |
| 925 | } else { |
| 926 | argv--; |
| 927 | argc++; |
| 928 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 929 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 930 | if (iof == filechar && --argc > 0) { |
| 931 | setval(prompt, ""); |
| 932 | setval(cprompt, ""); |
| 933 | prompt->status &= ~EXPORT; |
| 934 | cprompt->status &= ~EXPORT; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 935 | |
| 936 | /* Shell is non-interactive, activate printf-based debug */ |
| 937 | #ifdef MSHDEBUG |
| 938 | mshdbg = (int) (((char) (mshdbg_var->value[0])) - '0'); |
| 939 | if (mshdbg < 0) |
| 940 | mshdbg = 0; |
| 941 | #endif |
| 942 | DBGPRINTF(("MSH_MAIN: calling newfile()\n")); |
| 943 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 944 | if (newfile(name = *++argv)) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 945 | exit(1); /* Exit on error */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 946 | } |
| 947 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 948 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 949 | setdash(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 950 | |
| 951 | /* 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] | 952 | if (e.iop < iostack) { |
| 953 | PUSHIO(afile, 0, iof); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 954 | if (isatty(0) && isatty(1) && !cflag) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 955 | interactive++; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 956 | #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 957 | #ifdef MSHDEBUG |
| 958 | printf("\n\n" BB_BANNER " Built-in shell (msh with debug)\n"); |
| 959 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 960 | printf("\n\n" BB_BANNER " Built-in shell (msh)\n"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 961 | #endif |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 962 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
Eric Andersen | d63dee4 | 2001-10-19 00:22:23 +0000 | [diff] [blame] | 963 | #endif |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 964 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 965 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 966 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 967 | signal(SIGQUIT, qflag); |
| 968 | if (name && name[0] == '-') { |
| 969 | interactive++; |
| 970 | if ((f = open(".profile", 0)) >= 0) |
| 971 | next(remap(f)); |
| 972 | if ((f = open("/etc/profile", 0)) >= 0) |
| 973 | next(remap(f)); |
| 974 | } |
| 975 | if (interactive) |
| 976 | signal(SIGTERM, sig); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 977 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 978 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 979 | signal(SIGINT, onintr); |
| 980 | dolv = argv; |
| 981 | dolc = argc; |
| 982 | dolv[0] = name; |
| 983 | if (dolc > 1) { |
| 984 | for (ap = ++argv; --argc > 0;) { |
| 985 | if (assign(*ap = *argv++, !COPYV)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 986 | dolc--; /* keyword */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 987 | } else { |
| 988 | ap++; |
| 989 | } |
| 990 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 991 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 992 | setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc)); |
| 993 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 994 | DBGPRINTF(("MSH_MAIN: begin FOR loop, interactive %d, e.iop 0x%x, iostack 0x%x\n", interactive, e.iop, iostack)); |
| 995 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 996 | for (;;) { |
| 997 | if (interactive && e.iop <= iostack) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 998 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 999 | current_prompt = prompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1000 | #else |
| 1001 | prs(prompt->value); |
| 1002 | #endif |
| 1003 | } |
| 1004 | onecommand(); |
Eric Andersen | 1c31501 | 2002-04-26 23:40:09 +0000 | [diff] [blame] | 1005 | /* Ensure that getenv("PATH") stays current */ |
| 1006 | setenv("PATH", path->value, 1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1007 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1008 | |
| 1009 | DBGPRINTF(("MSH_MAIN: returning.\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1012 | static void setdash() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1013 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1014 | REGISTER char *cp; |
| 1015 | REGISTER int c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1016 | char m['z' - 'a' + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1017 | |
| 1018 | cp = m; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1019 | for (c = 'a'; c <= 'z'; c++) |
| 1020 | if (flag[(int) c]) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1021 | *cp++ = c; |
| 1022 | *cp = 0; |
| 1023 | setval(lookup("-"), m); |
| 1024 | } |
| 1025 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1026 | static int newfile(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1027 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1028 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1029 | REGISTER int f; |
| 1030 | |
| 1031 | DBGPRINTF7(("NEWFILE: opening %s\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1032 | |
| 1033 | if (strcmp(s, "-") != 0) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1034 | DBGPRINTF(("NEWFILE: s is %s\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1035 | f = open(s, 0); |
| 1036 | if (f < 0) { |
| 1037 | prs(s); |
| 1038 | err(": cannot open"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1039 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } else |
| 1042 | f = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1043 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1044 | next(remap(f)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1045 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1048 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1049 | struct op *scantree(head) |
| 1050 | struct op *head; |
| 1051 | { |
| 1052 | struct op *dotnode; |
| 1053 | |
| 1054 | if (head == NULL) |
| 1055 | return (NULL); |
| 1056 | |
| 1057 | if (head->left != NULL) { |
| 1058 | dotnode = scantree(head->left); |
| 1059 | if (dotnode) |
| 1060 | return (dotnode); |
| 1061 | } |
| 1062 | |
| 1063 | if (head->right != NULL) { |
| 1064 | dotnode = scantree(head->right); |
| 1065 | if (dotnode) |
| 1066 | return (dotnode); |
| 1067 | } |
| 1068 | |
| 1069 | if (head->words == NULL) |
| 1070 | return (NULL); |
| 1071 | |
| 1072 | DBGPRINTF5(("SCANTREE: checking node 0x%x\n", head)); |
| 1073 | |
| 1074 | if ((head->type != TDOT) && (strcmp(".", head->words[0]) == 0)) { |
| 1075 | DBGPRINTF5(("SCANTREE: dot found in node 0x%x\n", head)); |
| 1076 | return (head); |
| 1077 | } |
| 1078 | |
| 1079 | return (NULL); |
| 1080 | } |
| 1081 | |
| 1082 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1083 | static void onecommand() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1084 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1085 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1086 | jmp_buf m1; |
| 1087 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1088 | DBGPRINTF(("ONECOMMAND: enter, outtree=0x%x\n", outtree)); |
| 1089 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1090 | while (e.oenv) |
| 1091 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1092 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1093 | areanum = 1; |
| 1094 | freehere(areanum); |
| 1095 | freearea(areanum); |
| 1096 | garbage(); |
| 1097 | wdlist = 0; |
| 1098 | iolist = 0; |
| 1099 | e.errpt = 0; |
| 1100 | e.linep = line; |
| 1101 | yynerrs = 0; |
| 1102 | multiline = 0; |
| 1103 | inparse = 1; |
| 1104 | intr = 0; |
| 1105 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1106 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1107 | setjmp(failpt = m1); /* Bruce Evans' fix */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1108 | if (setjmp(failpt = m1) || yyparse() || intr) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1109 | |
| 1110 | DBGPRINTF(("ONECOMMAND: this is not good.\n")); |
| 1111 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1112 | while (e.oenv) |
| 1113 | quitenv(); |
| 1114 | scraphere(); |
| 1115 | if (!interactive && intr) |
| 1116 | leave(); |
| 1117 | inparse = 0; |
| 1118 | intr = 0; |
| 1119 | return; |
| 1120 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1121 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1122 | inparse = 0; |
| 1123 | brklist = 0; |
| 1124 | intr = 0; |
| 1125 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1126 | |
| 1127 | if (!flag['n']) { |
| 1128 | DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=0x%x\n", |
| 1129 | outtree)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1130 | execute(outtree, NOPIPE, NOPIPE, 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1133 | if (!interactive && intr) { |
| 1134 | execflg = 0; |
| 1135 | leave(); |
| 1136 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1137 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1138 | if ((i = trapset) != 0) { |
| 1139 | trapset = 0; |
| 1140 | runtrap(i); |
| 1141 | } |
| 1142 | } |
| 1143 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1144 | static void fail() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1145 | { |
| 1146 | longjmp(failpt, 1); |
| 1147 | /* NOTREACHED */ |
| 1148 | } |
| 1149 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1150 | static void leave() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1151 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1152 | DBGPRINTF(("LEAVE: leave called!\n")); |
| 1153 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1154 | if (execflg) |
| 1155 | fail(); |
| 1156 | scraphere(); |
| 1157 | freehere(1); |
| 1158 | runtrap(0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1159 | _exit(exstat); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1160 | /* NOTREACHED */ |
| 1161 | } |
| 1162 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1163 | static void warn(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1164 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1165 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1166 | if (*s) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1167 | prs(s); |
| 1168 | exstat = -1; |
| 1169 | } |
| 1170 | prs("\n"); |
| 1171 | if (flag['e']) |
| 1172 | leave(); |
| 1173 | } |
| 1174 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1175 | static void err(s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1176 | char *s; |
| 1177 | { |
| 1178 | warn(s); |
| 1179 | if (flag['n']) |
| 1180 | return; |
| 1181 | if (!interactive) |
| 1182 | leave(); |
| 1183 | if (e.errpt) |
| 1184 | longjmp(e.errpt, 1); |
| 1185 | closeall(); |
| 1186 | e.iop = e.iobase = iostack; |
| 1187 | } |
| 1188 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1189 | static int newenv(f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1190 | int f; |
| 1191 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1192 | REGISTER struct env *ep; |
| 1193 | |
| 1194 | DBGPRINTF(("NEWENV: f=%d (indicates quitenv and return)\n", f)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1195 | |
| 1196 | if (f) { |
| 1197 | quitenv(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1198 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1199 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1200 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1201 | ep = (struct env *) space(sizeof(*ep)); |
| 1202 | if (ep == NULL) { |
| 1203 | while (e.oenv) |
| 1204 | quitenv(); |
| 1205 | fail(); |
| 1206 | } |
| 1207 | *ep = e; |
| 1208 | e.oenv = ep; |
| 1209 | e.errpt = errpt; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1210 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1211 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1214 | static void quitenv() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1215 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1216 | REGISTER struct env *ep; |
| 1217 | REGISTER int fd; |
| 1218 | |
| 1219 | DBGPRINTF(("QUITENV: e.oenv=0x%x\n", e.oenv)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1220 | |
| 1221 | if ((ep = e.oenv) != NULL) { |
| 1222 | fd = e.iofd; |
| 1223 | e = *ep; |
| 1224 | /* should close `'d files */ |
| 1225 | DELETE(ep); |
| 1226 | while (--fd >= e.iofd) |
| 1227 | close(fd); |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | /* |
| 1232 | * Is any character from s1 in s2? |
| 1233 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1234 | static int anys(s1, s2) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1235 | REGISTER char *s1, *s2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1236 | { |
| 1237 | while (*s1) |
| 1238 | if (any(*s1++, s2)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1239 | return (1); |
| 1240 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | /* |
| 1244 | * Is character c in s? |
| 1245 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1246 | static int any(c, s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1247 | REGISTER int c; |
| 1248 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1249 | { |
| 1250 | while (*s) |
| 1251 | if (*s++ == c) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1252 | return (1); |
| 1253 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1256 | static char *putn(n) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1257 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1258 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1259 | return (itoa(n)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1262 | static char *itoa(n) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1263 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1264 | { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1265 | static char s[20]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1266 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 1267 | snprintf(s, sizeof(s), "%u", n); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1268 | return (s); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1271 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1272 | static void next(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1273 | { |
| 1274 | PUSHIO(afile, f, filechar); |
| 1275 | } |
| 1276 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1277 | static void onintr(s) |
| 1278 | int s; /* ANSI C requires a parameter */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1279 | { |
| 1280 | signal(SIGINT, onintr); |
| 1281 | intr = 1; |
| 1282 | if (interactive) { |
| 1283 | if (inparse) { |
| 1284 | prs("\n"); |
| 1285 | fail(); |
| 1286 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1287 | } else if (heedint) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1288 | execflg = 0; |
| 1289 | leave(); |
| 1290 | } |
| 1291 | } |
| 1292 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1293 | static char *space(n) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1294 | int n; |
| 1295 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1296 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1297 | |
| 1298 | if ((cp = getcell(n)) == 0) |
| 1299 | err("out of string space"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1300 | return (cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1303 | static char *strsave(s, a) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1304 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1305 | int a; |
| 1306 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1307 | REGISTER char *cp, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1308 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1309 | if ((cp = space(strlen(s) + 1)) != NULL) { |
| 1310 | setarea((char *) cp, a); |
| 1311 | for (xp = cp; (*xp++ = *s++) != '\0';); |
| 1312 | return (cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1313 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1314 | return (""); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | /* |
| 1318 | * trap handling |
| 1319 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1320 | static void sig(i) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1321 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1322 | { |
| 1323 | trapset = i; |
| 1324 | signal(i, sig); |
| 1325 | } |
| 1326 | |
| 1327 | static void runtrap(i) |
| 1328 | int i; |
| 1329 | { |
| 1330 | char *trapstr; |
| 1331 | |
| 1332 | if ((trapstr = trap[i]) == NULL) |
| 1333 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1334 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1335 | if (i == 0) |
| 1336 | trap[i] = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1337 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1338 | RUN(aword, trapstr, nlchar); |
| 1339 | } |
| 1340 | |
| 1341 | /* -------- var.c -------- */ |
| 1342 | |
| 1343 | /* |
| 1344 | * Find the given name in the dictionary |
| 1345 | * and return its value. If the name was |
| 1346 | * not previously there, enter it now and |
| 1347 | * return a null value. |
| 1348 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1349 | static struct var *lookup(n) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1350 | REGISTER char *n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1351 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1352 | REGISTER struct var *vp; |
| 1353 | REGISTER char *cp; |
| 1354 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1355 | static struct var dummy; |
| 1356 | |
| 1357 | if (isdigit(*n)) { |
| 1358 | dummy.name = n; |
| 1359 | for (c = 0; isdigit(*n) && c < 1000; n++) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1360 | c = c * 10 + *n - '0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1361 | dummy.status = RONLY; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1362 | dummy.value = c <= dolc ? dolv[c] : null; |
| 1363 | return (&dummy); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1364 | } |
| 1365 | for (vp = vlist; vp; vp = vp->next) |
| 1366 | if (eqname(vp->name, n)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1367 | return (vp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1368 | cp = findeq(n); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1369 | vp = (struct var *) space(sizeof(*vp)); |
| 1370 | if (vp == 0 || (vp->name = space((int) (cp - n) + 2)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1371 | dummy.name = dummy.value = ""; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1372 | return (&dummy); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1373 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1374 | for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1375 | if (*cp == 0) |
| 1376 | *cp = '='; |
| 1377 | *++cp = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1378 | setarea((char *) vp, 0); |
| 1379 | setarea((char *) vp->name, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1380 | vp->value = null; |
| 1381 | vp->next = vlist; |
| 1382 | vp->status = GETCELL; |
| 1383 | vlist = vp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1384 | return (vp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | /* |
| 1388 | * give variable at `vp' the value `val'. |
| 1389 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1390 | static void setval(vp, val) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1391 | struct var *vp; |
| 1392 | char *val; |
| 1393 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1394 | nameval(vp, val, (char *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * if name is not NULL, it must be |
| 1399 | * a prefix of the space `val', |
| 1400 | * and end with `='. |
| 1401 | * this is all so that exporting |
| 1402 | * values is reasonably painless. |
| 1403 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1404 | static void nameval(vp, val, name) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1405 | REGISTER struct var *vp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1406 | char *val, *name; |
| 1407 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1408 | REGISTER char *cp, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1409 | char *nv; |
| 1410 | int fl; |
| 1411 | |
| 1412 | if (vp->status & RONLY) { |
| 1413 | for (xp = vp->name; *xp && *xp != '=';) |
| 1414 | putc(*xp++, stderr); |
| 1415 | err(" is read-only"); |
| 1416 | return; |
| 1417 | } |
| 1418 | fl = 0; |
| 1419 | if (name == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1420 | xp = space(strlen(vp->name) + strlen(val) + 2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1421 | if (xp == 0) |
| 1422 | return; |
| 1423 | /* make string: name=value */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1424 | setarea((char *) xp, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1425 | name = xp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1426 | for (cp = vp->name; (*xp = *cp++) && *xp != '='; xp++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1427 | if (*xp++ == 0) |
| 1428 | xp[-1] = '='; |
| 1429 | nv = xp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1430 | for (cp = val; (*xp++ = *cp++) != '\0';); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1431 | val = nv; |
| 1432 | fl = GETCELL; |
| 1433 | } |
| 1434 | if (vp->status & GETCELL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1435 | freecell(vp->name); /* form new string `name=value' */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1436 | vp->name = name; |
| 1437 | vp->value = val; |
| 1438 | vp->status |= fl; |
| 1439 | } |
| 1440 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1441 | static void export(vp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1442 | struct var *vp; |
| 1443 | { |
| 1444 | vp->status |= EXPORT; |
| 1445 | } |
| 1446 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1447 | static void ronly(vp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1448 | struct var *vp; |
| 1449 | { |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1450 | if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1451 | vp->status |= RONLY; |
| 1452 | } |
| 1453 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1454 | static int isassign(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1455 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1456 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1457 | DBGPRINTF7(("ISASSIGN: enter, s=%s\n", s)); |
| 1458 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1459 | if (!isalpha((int) *s) && *s != '_') |
| 1460 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1461 | for (; *s != '='; s++) |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1462 | if (*s == 0 || (!isalnum(*s) && *s != '_')) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1463 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1464 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1465 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1468 | static int assign(s, cf) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1469 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1470 | int cf; |
| 1471 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1472 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1473 | struct var *vp; |
| 1474 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1475 | DBGPRINTF7(("ASSIGN: enter, s=%s, cf=%d\n", s, cf)); |
| 1476 | |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1477 | if (!isalpha(*s) && *s != '_') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1478 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1479 | for (cp = s; *cp != '='; cp++) |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1480 | if (*cp == 0 || (!isalnum(*cp) && *cp != '_')) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1481 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1482 | vp = lookup(s); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1483 | nameval(vp, ++cp, cf == COPYV ? (char *) NULL : s); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1484 | if (cf != COPYV) |
| 1485 | vp->status &= ~GETCELL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1486 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1489 | static int checkname(cp) |
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 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1492 | DBGPRINTF7(("CHECKNAME: enter, cp=%s\n", cp)); |
| 1493 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1494 | if (!isalpha(*cp++) && *(cp - 1) != '_') |
| 1495 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1496 | while (*cp) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1497 | if (!isalnum(*cp++) && *(cp - 1) != '_') |
| 1498 | return (0); |
| 1499 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1502 | static void putvlist(f, out) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1503 | REGISTER int f, out; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1504 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1505 | REGISTER struct var *vp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1506 | |
| 1507 | for (vp = vlist; vp; vp = vp->next) |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1508 | if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1509 | if (vp->status & EXPORT) |
| 1510 | write(out, "export ", 7); |
| 1511 | if (vp->status & RONLY) |
| 1512 | write(out, "readonly ", 9); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1513 | write(out, vp->name, (int) (findeq(vp->name) - vp->name)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1514 | write(out, "\n", 1); |
| 1515 | } |
| 1516 | } |
| 1517 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1518 | static int eqname(n1, n2) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1519 | REGISTER char *n1, *n2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1520 | { |
| 1521 | for (; *n1 != '=' && *n1 != 0; n1++) |
| 1522 | if (*n2++ != *n1) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1523 | return (0); |
| 1524 | return (*n2 == 0 || *n2 == '='); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1527 | static char *findeq(cp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1528 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1529 | { |
| 1530 | while (*cp != '\0' && *cp != '=') |
| 1531 | cp++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1532 | return (cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | /* -------- gmatch.c -------- */ |
| 1536 | /* |
| 1537 | * int gmatch(string, pattern) |
| 1538 | * char *string, *pattern; |
| 1539 | * |
| 1540 | * Match a pattern as in sh(1). |
| 1541 | */ |
| 1542 | |
| 1543 | #define CMASK 0377 |
| 1544 | #define QUOTE 0200 |
| 1545 | #define QMASK (CMASK&~QUOTE) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1546 | #define NOT '!' /* might use ^ */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1547 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1548 | static int gmatch(s, p) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1549 | REGISTER char *s, *p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1550 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1551 | REGISTER int sc, pc; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1552 | |
| 1553 | if (s == NULL || p == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1554 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1555 | while ((pc = *p++ & CMASK) != '\0') { |
| 1556 | sc = *s++ & QMASK; |
| 1557 | switch (pc) { |
| 1558 | case '[': |
| 1559 | if ((p = cclass(p, sc)) == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1560 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1561 | break; |
| 1562 | |
| 1563 | case '?': |
| 1564 | if (sc == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1565 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1566 | break; |
| 1567 | |
| 1568 | case '*': |
| 1569 | s--; |
| 1570 | do { |
| 1571 | if (*p == '\0' || gmatch(s, p)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1572 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1573 | } while (*s++ != '\0'); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1574 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1575 | |
| 1576 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1577 | if (sc != (pc & ~QUOTE)) |
| 1578 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1579 | } |
| 1580 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1581 | return (*s == 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1584 | static char *cclass(p, sub) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1585 | REGISTER char *p; |
| 1586 | REGISTER int sub; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1587 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1588 | REGISTER int c, d, not, found; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1589 | |
| 1590 | if ((not = *p == NOT) != 0) |
| 1591 | p++; |
| 1592 | found = not; |
| 1593 | do { |
| 1594 | if (*p == '\0') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1595 | return ((char *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1596 | c = *p & CMASK; |
| 1597 | if (p[1] == '-' && p[2] != ']') { |
| 1598 | d = p[2] & CMASK; |
| 1599 | p++; |
| 1600 | } else |
| 1601 | d = c; |
| 1602 | if (c == sub || (c <= sub && sub <= d)) |
| 1603 | found = !not; |
| 1604 | } while (*++p != ']'); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1605 | return (found ? p + 1 : (char *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | |
| 1609 | /* -------- area.c -------- */ |
| 1610 | |
| 1611 | /* |
| 1612 | * All memory between (char *)areabot and (char *)(areatop+1) is |
| 1613 | * exclusively administered by the area management routines. |
| 1614 | * It is assumed that sbrk() and brk() manipulate the high end. |
| 1615 | */ |
| 1616 | |
| 1617 | #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;}) |
| 1618 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1619 | static void initarea() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1620 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1621 | brkaddr = malloc(AREASIZE); |
| 1622 | brktop = brkaddr + AREASIZE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1623 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1624 | while ((int) sbrk(0) & ALIGN) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1625 | sbrk(1); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1626 | areabot = (struct region *) sbrk(REGSIZE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1627 | |
| 1628 | areabot->next = areabot; |
| 1629 | areabot->area = BUSY; |
| 1630 | areatop = areabot; |
| 1631 | areanxt = areabot; |
| 1632 | } |
| 1633 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1634 | char *getcell(nbytes) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1635 | unsigned nbytes; |
| 1636 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1637 | REGISTER int nregio; |
| 1638 | REGISTER struct region *p, *q; |
| 1639 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1640 | |
| 1641 | if (nbytes == 0) { |
| 1642 | puts("getcell(0)"); |
| 1643 | abort(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1644 | } |
| 1645 | /* silly and defeats the algorithm */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1646 | /* |
| 1647 | * round upwards and add administration area |
| 1648 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1649 | nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1650 | for (p = areanxt;;) { |
| 1651 | if (p->area > areanum) { |
| 1652 | /* |
| 1653 | * merge free cells |
| 1654 | */ |
| 1655 | while ((q = p->next)->area > areanum && q != areanxt) |
| 1656 | p->next = q->next; |
| 1657 | /* |
| 1658 | * exit loop if cell big enough |
| 1659 | */ |
| 1660 | if (q >= p + nregio) |
| 1661 | goto found; |
| 1662 | } |
| 1663 | p = p->next; |
| 1664 | if (p == areanxt) |
| 1665 | break; |
| 1666 | } |
| 1667 | i = nregio >= GROWBY ? nregio : GROWBY; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1668 | p = (struct region *) sbrk(i * REGSIZE); |
| 1669 | if (p == (struct region *) -1) |
| 1670 | return ((char *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1671 | p--; |
| 1672 | if (p != areatop) { |
| 1673 | puts("not contig"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1674 | abort(); /* allocated areas are contiguous */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1675 | } |
| 1676 | q = p + i; |
| 1677 | p->next = q; |
| 1678 | p->area = FREE; |
| 1679 | q->next = areabot; |
| 1680 | q->area = BUSY; |
| 1681 | areatop = q; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1682 | found: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1683 | /* |
| 1684 | * we found a FREE area big enough, pointed to by 'p', and up to 'q' |
| 1685 | */ |
| 1686 | areanxt = p + nregio; |
| 1687 | if (areanxt < q) { |
| 1688 | /* |
| 1689 | * split into requested area and rest |
| 1690 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1691 | if (areanxt + 1 > q) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1692 | puts("OOM"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1693 | abort(); /* insufficient space left for admin */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1694 | } |
| 1695 | areanxt->next = q; |
| 1696 | areanxt->area = FREE; |
| 1697 | p->next = areanxt; |
| 1698 | } |
| 1699 | p->area = areanum; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1700 | return ((char *) (p + 1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1703 | static void freecell(cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1704 | char *cp; |
| 1705 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1706 | REGISTER struct region *p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1707 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1708 | if ((p = (struct region *) cp) != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1709 | p--; |
| 1710 | if (p < areanxt) |
| 1711 | areanxt = p; |
| 1712 | p->area = FREE; |
| 1713 | } |
| 1714 | } |
| 1715 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1716 | static void freearea(a) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1717 | REGISTER int a; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1718 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1719 | REGISTER struct region *p, *top; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1720 | |
| 1721 | top = areatop; |
| 1722 | for (p = areabot; p != top; p = p->next) |
| 1723 | if (p->area >= a) |
| 1724 | p->area = FREE; |
| 1725 | } |
| 1726 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1727 | static void setarea(cp, a) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1728 | char *cp; |
| 1729 | int a; |
| 1730 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1731 | REGISTER struct region *p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1732 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1733 | if ((p = (struct region *) cp) != NULL) |
| 1734 | (p - 1)->area = a; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1737 | int getarea(cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1738 | char *cp; |
| 1739 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1740 | return ((struct region *) cp - 1)->area; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1743 | static void garbage() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1744 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1745 | REGISTER struct region *p, *q, *top; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1746 | |
| 1747 | top = areatop; |
| 1748 | for (p = areabot; p != top; p = p->next) { |
| 1749 | if (p->area > areanum) { |
| 1750 | while ((q = p->next)->area > areanum) |
| 1751 | p->next = q->next; |
| 1752 | areanxt = p; |
| 1753 | } |
| 1754 | } |
| 1755 | #ifdef SHRINKBY |
| 1756 | if (areatop >= q + SHRINKBY && q->area > areanum) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1757 | brk((char *) (q + 1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1758 | q->next = areabot; |
| 1759 | q->area = BUSY; |
| 1760 | areatop = q; |
| 1761 | } |
| 1762 | #endif |
| 1763 | } |
| 1764 | |
| 1765 | /* -------- csyn.c -------- */ |
| 1766 | /* |
| 1767 | * shell: syntax (C version) |
| 1768 | */ |
| 1769 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1770 | int yyparse() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1771 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1772 | DBGPRINTF7(("YYPARSE: enter...\n")); |
| 1773 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1774 | startl = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1775 | peeksym = 0; |
| 1776 | yynerrs = 0; |
| 1777 | outtree = c_list(); |
| 1778 | musthave('\n', 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1779 | return (yynerrs != 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1782 | static struct op *pipeline(cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1783 | int cf; |
| 1784 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1785 | REGISTER struct op *t, *p; |
| 1786 | REGISTER int c; |
| 1787 | |
| 1788 | DBGPRINTF7(("PIPELINE: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1789 | |
| 1790 | t = command(cf); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1791 | |
| 1792 | DBGPRINTF9(("PIPELINE: t=0x%x\n", t)); |
| 1793 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1794 | if (t != NULL) { |
| 1795 | while ((c = yylex(0)) == '|') { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1796 | if ((p = command(CONTIN)) == NULL) { |
| 1797 | DBGPRINTF8(("PIPELINE: error!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1798 | SYNTAXERR; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1801 | if (t->type != TPAREN && t->type != TCOM) { |
| 1802 | /* shell statement */ |
| 1803 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 1804 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1805 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1806 | t = block(TPIPE, t, p, NOWORDS); |
| 1807 | } |
| 1808 | peeksym = c; |
| 1809 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1810 | |
| 1811 | DBGPRINTF7(("PIPELINE: returning t=0x%x\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1812 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1815 | static struct op *andor() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1816 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1817 | REGISTER struct op *t, *p; |
| 1818 | REGISTER int c; |
| 1819 | |
| 1820 | DBGPRINTF7(("ANDOR: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1821 | |
| 1822 | t = pipeline(0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1823 | |
| 1824 | DBGPRINTF9(("ANDOR: t=0x%x\n", t)); |
| 1825 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1826 | if (t != NULL) { |
| 1827 | while ((c = yylex(0)) == LOGAND || c == LOGOR) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1828 | if ((p = pipeline(CONTIN)) == NULL) { |
| 1829 | DBGPRINTF8(("ANDOR: error!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1830 | SYNTAXERR; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1833 | t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1834 | } /* WHILE */ |
| 1835 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1836 | peeksym = c; |
| 1837 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1838 | |
| 1839 | DBGPRINTF7(("ANDOR: returning t=0x%x\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1840 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1843 | static struct op *c_list() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1844 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1845 | REGISTER struct op *t, *p; |
| 1846 | REGISTER int c; |
| 1847 | |
| 1848 | DBGPRINTF7(("C_LIST: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1849 | |
| 1850 | t = andor(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1851 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1852 | if (t != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1853 | if ((peeksym = yylex(0)) == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1854 | t = block(TASYNC, t, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1855 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1856 | while ((c = yylex(0)) == ';' || c == '&' |
| 1857 | || (multiline && c == '\n')) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1858 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1859 | if ((p = andor()) == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1860 | return (t); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1861 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1862 | if ((peeksym = yylex(0)) == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1863 | p = block(TASYNC, p, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1864 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1865 | t = list(t, p); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1866 | } /* WHILE */ |
| 1867 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1868 | peeksym = c; |
| 1869 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1870 | /* IF */ |
| 1871 | DBGPRINTF7(("C_LIST: returning t=0x%x\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1872 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1875 | static int synio(cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1876 | int cf; |
| 1877 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1878 | REGISTER struct ioword *iop; |
| 1879 | REGISTER int i; |
| 1880 | REGISTER int c; |
| 1881 | |
| 1882 | DBGPRINTF7(("SYNIO: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1883 | |
| 1884 | if ((c = yylex(cf)) != '<' && c != '>') { |
| 1885 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1886 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1887 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1888 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1889 | i = yylval.i; |
| 1890 | musthave(WORD, 0); |
| 1891 | iop = io(iounit, i, yylval.cp); |
| 1892 | iounit = IODEFAULT; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1893 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1894 | if (i & IOHERE) |
| 1895 | markhere(yylval.cp, iop); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1896 | |
| 1897 | DBGPRINTF7(("SYNIO: returning 1\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1898 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1901 | static void musthave(c, cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1902 | int c, cf; |
| 1903 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1904 | if ((peeksym = yylex(cf)) != c) { |
| 1905 | DBGPRINTF7(("MUSTHAVE: error!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1906 | SYNTAXERR; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1909 | peeksym = 0; |
| 1910 | } |
| 1911 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1912 | static struct op *simple() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1913 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1914 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1915 | |
| 1916 | t = NULL; |
| 1917 | for (;;) { |
| 1918 | switch (peeksym = yylex(0)) { |
| 1919 | case '<': |
| 1920 | case '>': |
| 1921 | (void) synio(0); |
| 1922 | break; |
| 1923 | |
| 1924 | case WORD: |
| 1925 | if (t == NULL) { |
| 1926 | t = newtp(); |
| 1927 | t->type = TCOM; |
| 1928 | } |
| 1929 | peeksym = 0; |
| 1930 | word(yylval.cp); |
| 1931 | break; |
| 1932 | |
| 1933 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1934 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | } |
| 1938 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1939 | static struct op *nested(type, mark) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1940 | int type, mark; |
| 1941 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1942 | REGISTER struct op *t; |
| 1943 | |
| 1944 | DBGPRINTF3(("NESTED: enter, type=%d, mark=%d\n", type, mark)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1945 | |
| 1946 | multiline++; |
| 1947 | t = c_list(); |
| 1948 | musthave(mark, 0); |
| 1949 | multiline--; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1950 | return (block(type, t, NOBLOCK, NOWORDS)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1953 | static struct op *command(cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1954 | int cf; |
| 1955 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1956 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1957 | struct wdblock *iosave; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1958 | REGISTER int c; |
| 1959 | |
| 1960 | DBGPRINTF(("COMMAND: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1961 | |
| 1962 | iosave = iolist; |
| 1963 | iolist = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1964 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1965 | if (multiline) |
| 1966 | cf |= CONTIN; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1967 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1968 | while (synio(cf)) |
| 1969 | cf = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1970 | |
| 1971 | c = yylex(cf); |
| 1972 | |
| 1973 | switch (c) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1974 | default: |
| 1975 | peeksym = c; |
| 1976 | if ((t = simple()) == NULL) { |
| 1977 | if (iolist == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1978 | return ((struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1979 | t = newtp(); |
| 1980 | t->type = TCOM; |
| 1981 | } |
| 1982 | break; |
| 1983 | |
| 1984 | case '(': |
| 1985 | t = nested(TPAREN, ')'); |
| 1986 | break; |
| 1987 | |
| 1988 | case '{': |
| 1989 | t = nested(TBRACE, '}'); |
| 1990 | break; |
| 1991 | |
| 1992 | case FOR: |
| 1993 | t = newtp(); |
| 1994 | t->type = TFOR; |
| 1995 | musthave(WORD, 0); |
| 1996 | startl = 1; |
| 1997 | t->str = yylval.cp; |
| 1998 | multiline++; |
| 1999 | t->words = wordlist(); |
| 2000 | if ((c = yylex(0)) != '\n' && c != ';') |
| 2001 | peeksym = c; |
| 2002 | t->left = dogroup(0); |
| 2003 | multiline--; |
| 2004 | break; |
| 2005 | |
| 2006 | case WHILE: |
| 2007 | case UNTIL: |
| 2008 | multiline++; |
| 2009 | t = newtp(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2010 | t->type = c == WHILE ? TWHILE : TUNTIL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2011 | t->left = c_list(); |
| 2012 | t->right = dogroup(1); |
| 2013 | t->words = NULL; |
| 2014 | multiline--; |
| 2015 | break; |
| 2016 | |
| 2017 | case CASE: |
| 2018 | t = newtp(); |
| 2019 | t->type = TCASE; |
| 2020 | musthave(WORD, 0); |
| 2021 | t->str = yylval.cp; |
| 2022 | startl++; |
| 2023 | multiline++; |
| 2024 | musthave(IN, CONTIN); |
| 2025 | startl++; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2026 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2027 | t->left = caselist(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2028 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2029 | musthave(ESAC, 0); |
| 2030 | multiline--; |
| 2031 | break; |
| 2032 | |
| 2033 | case IF: |
| 2034 | multiline++; |
| 2035 | t = newtp(); |
| 2036 | t->type = TIF; |
| 2037 | t->left = c_list(); |
| 2038 | t->right = thenpart(); |
| 2039 | musthave(FI, 0); |
| 2040 | multiline--; |
| 2041 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2042 | |
| 2043 | case DOT: |
| 2044 | t = newtp(); |
| 2045 | t->type = TDOT; |
| 2046 | |
| 2047 | musthave(WORD, 0); /* gets name of file */ |
| 2048 | DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp)); |
| 2049 | |
| 2050 | word(yylval.cp); /* add word to wdlist */ |
| 2051 | word(NOWORD); /* terminate wdlist */ |
| 2052 | t->words = copyw(); /* dup wdlist */ |
| 2053 | break; |
| 2054 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2055 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2056 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2057 | while (synio(0)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2058 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2059 | t = namelist(t); |
| 2060 | iolist = iosave; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2061 | |
| 2062 | DBGPRINTF(("COMMAND: returning 0x%x\n", t)); |
| 2063 | |
| 2064 | return (t); |
| 2065 | } |
| 2066 | |
| 2067 | static struct op *dowholefile(type, mark) |
| 2068 | int type; |
| 2069 | int mark; |
| 2070 | { |
| 2071 | REGISTER struct op *t; |
| 2072 | |
| 2073 | DBGPRINTF(("DOWHOLEFILE: enter, type=%d, mark=%d\n", type, mark)); |
| 2074 | |
| 2075 | multiline++; |
| 2076 | t = c_list(); |
| 2077 | multiline--; |
| 2078 | t = block(type, t, NOBLOCK, NOWORDS); |
| 2079 | DBGPRINTF(("DOWHOLEFILE: return t=0x%x\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2080 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2083 | static struct op *dogroup(onlydone) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2084 | int onlydone; |
| 2085 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2086 | REGISTER int c; |
| 2087 | REGISTER struct op *mylist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2088 | |
| 2089 | c = yylex(CONTIN); |
| 2090 | if (c == DONE && onlydone) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2091 | return ((struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2092 | if (c != DO) |
| 2093 | SYNTAXERR; |
| 2094 | mylist = c_list(); |
| 2095 | musthave(DONE, 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2096 | return (mylist); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2099 | static struct op *thenpart() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2100 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2101 | REGISTER int c; |
| 2102 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2103 | |
| 2104 | if ((c = yylex(0)) != THEN) { |
| 2105 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2106 | return ((struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2107 | } |
| 2108 | t = newtp(); |
| 2109 | t->type = 0; |
| 2110 | t->left = c_list(); |
| 2111 | if (t->left == NULL) |
| 2112 | SYNTAXERR; |
| 2113 | t->right = elsepart(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2114 | return (t); |
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 *elsepart() |
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 | switch (c = yylex(0)) { |
| 2123 | case ELSE: |
| 2124 | if ((t = c_list()) == NULL) |
| 2125 | SYNTAXERR; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2126 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2127 | |
| 2128 | case ELIF: |
| 2129 | t = newtp(); |
| 2130 | t->type = TELIF; |
| 2131 | t->left = c_list(); |
| 2132 | t->right = thenpart(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2133 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2134 | |
| 2135 | default: |
| 2136 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2137 | return ((struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2138 | } |
| 2139 | } |
| 2140 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2141 | static struct op *caselist() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2142 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2143 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2144 | |
| 2145 | t = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2146 | while ((peeksym = yylex(CONTIN)) != ESAC) { |
| 2147 | DBGPRINTF(("CASELIST, doing yylex, peeksym=%d\n", peeksym)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2148 | t = list(t, casepart()); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | DBGPRINTF(("CASELIST, returning t=0x%x\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2152 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2155 | static struct op *casepart() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2156 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2157 | REGISTER struct op *t; |
| 2158 | |
| 2159 | DBGPRINTF7(("CASEPART: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2160 | |
| 2161 | t = newtp(); |
| 2162 | t->type = TPAT; |
| 2163 | t->words = pattern(); |
| 2164 | musthave(')', 0); |
| 2165 | t->left = c_list(); |
| 2166 | if ((peeksym = yylex(CONTIN)) != ESAC) |
| 2167 | musthave(BREAK, CONTIN); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2168 | |
| 2169 | DBGPRINTF7(("CASEPART: made newtp(TPAT, t=0x%x)\n", t)); |
| 2170 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2171 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2174 | static char **pattern() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2175 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2176 | REGISTER int c, cf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2177 | |
| 2178 | cf = CONTIN; |
| 2179 | do { |
| 2180 | musthave(WORD, cf); |
| 2181 | word(yylval.cp); |
| 2182 | cf = 0; |
| 2183 | } while ((c = yylex(0)) == '|'); |
| 2184 | peeksym = c; |
| 2185 | word(NOWORD); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2186 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2187 | return (copyw()); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2190 | static char **wordlist() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2191 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2192 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2193 | |
| 2194 | if ((c = yylex(0)) != IN) { |
| 2195 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2196 | return ((char **) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2197 | } |
| 2198 | startl = 0; |
| 2199 | while ((c = yylex(0)) == WORD) |
| 2200 | word(yylval.cp); |
| 2201 | word(NOWORD); |
| 2202 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2203 | return (copyw()); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | /* |
| 2207 | * supporting functions |
| 2208 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2209 | static struct op *list(t1, t2) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2210 | REGISTER struct op *t1, *t2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2211 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2212 | DBGPRINTF7(("LIST: enter, t1=0x%x, t2=0x%x\n", t1, t2)); |
| 2213 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2214 | if (t1 == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2215 | return (t2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2216 | if (t2 == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2217 | return (t1); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2218 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2219 | return (block(TLIST, t1, t2, NOWORDS)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2222 | static struct op *block(type, t1, t2, wp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2223 | int type; |
| 2224 | struct op *t1, *t2; |
| 2225 | char **wp; |
| 2226 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2227 | REGISTER struct op *t; |
| 2228 | |
| 2229 | DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type])); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2230 | |
| 2231 | t = newtp(); |
| 2232 | t->type = type; |
| 2233 | t->left = t1; |
| 2234 | t->right = t2; |
| 2235 | t->words = wp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2236 | |
| 2237 | DBGPRINTF7(("BLOCK: inserted 0x%x between 0x%x and 0x%x\n", t, t1, |
| 2238 | t2)); |
| 2239 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2240 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2243 | /* See if given string is a shell multiline (FOR, IF, etc) */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2244 | static int rlookup(n) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2245 | REGISTER char *n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2246 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2247 | REGISTER struct res *rp; |
| 2248 | |
| 2249 | DBGPRINTF7(("RLOOKUP: enter, n is %s\n", n)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2250 | |
| 2251 | for (rp = restab; rp->r_name; rp++) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2252 | if (strcmp(rp->r_name, n) == 0) { |
| 2253 | DBGPRINTF7(("RLOOKUP: match, returning %d\n", rp->r_val)); |
| 2254 | return (rp->r_val); /* Return numeric code for shell multiline */ |
| 2255 | } |
| 2256 | |
| 2257 | DBGPRINTF7(("RLOOKUP: NO match, returning 0\n")); |
| 2258 | return (0); /* Not a shell multiline */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2261 | static struct op *newtp() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2262 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2263 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2264 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2265 | t = (struct op *) tree(sizeof(*t)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2266 | t->type = 0; |
| 2267 | t->words = NULL; |
| 2268 | t->ioact = NULL; |
| 2269 | t->left = NULL; |
| 2270 | t->right = NULL; |
| 2271 | t->str = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2272 | |
| 2273 | DBGPRINTF3(("NEWTP: allocated 0x%x\n", t)); |
| 2274 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2275 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2278 | static struct op *namelist(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2279 | REGISTER struct op *t; |
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 | |
| 2282 | DBGPRINTF7(("NAMELIST: enter, t=0x%x, type %s, iolist=0x%x\n", t, |
| 2283 | T_CMD_NAMES[t->type], iolist)); |
| 2284 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2285 | if (iolist) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2286 | iolist = addword((char *) NULL, iolist); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2287 | t->ioact = copyio(); |
| 2288 | } else |
| 2289 | t->ioact = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2290 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2291 | if (t->type != TCOM) { |
| 2292 | if (t->type != TPAREN && t->ioact != NULL) { |
| 2293 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 2294 | t->ioact = t->left->ioact; |
| 2295 | t->left->ioact = NULL; |
| 2296 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2297 | return (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 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2300 | word(NOWORD); |
| 2301 | t->words = copyw(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2302 | |
| 2303 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2304 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2307 | static char **copyw() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2308 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2309 | REGISTER char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2310 | |
| 2311 | wd = getwords(wdlist); |
| 2312 | wdlist = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2313 | return (wd); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2316 | static void word(cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2317 | char *cp; |
| 2318 | { |
| 2319 | wdlist = addword(cp, wdlist); |
| 2320 | } |
| 2321 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2322 | static struct ioword **copyio() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2323 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2324 | REGISTER struct ioword **iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2325 | |
| 2326 | iop = (struct ioword **) getwords(iolist); |
| 2327 | iolist = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2328 | return (iop); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2331 | static struct ioword *io(u, f, cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2332 | int u; |
| 2333 | int f; |
| 2334 | char *cp; |
| 2335 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2336 | REGISTER struct ioword *iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2337 | |
| 2338 | iop = (struct ioword *) tree(sizeof(*iop)); |
| 2339 | iop->io_unit = u; |
| 2340 | iop->io_flag = f; |
| 2341 | iop->io_name = cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2342 | iolist = addword((char *) iop, iolist); |
| 2343 | return (iop); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2346 | static void zzerr() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2347 | { |
| 2348 | yyerror("syntax error"); |
| 2349 | } |
| 2350 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2351 | static void yyerror(s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2352 | char *s; |
| 2353 | { |
| 2354 | yynerrs++; |
| 2355 | if (interactive && e.iop <= iostack) { |
| 2356 | multiline = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2357 | while (eofc() == 0 && yylex(0) != '\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2358 | } |
| 2359 | err(s); |
| 2360 | fail(); |
| 2361 | } |
| 2362 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2363 | static int yylex(cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2364 | int cf; |
| 2365 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2366 | REGISTER int c, c1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2367 | int atstart; |
| 2368 | |
| 2369 | if ((c = peeksym) > 0) { |
| 2370 | peeksym = 0; |
| 2371 | if (c == '\n') |
| 2372 | startl = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2373 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2374 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2375 | |
| 2376 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2377 | nlseen = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2378 | atstart = startl; |
| 2379 | startl = 0; |
| 2380 | yylval.i = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2381 | e.linep = line; |
| 2382 | |
| 2383 | /* MALAMO */ |
| 2384 | line[LINELIM - 1] = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2385 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2386 | loop: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2387 | while ((c = my_getc(0)) == ' ' || c == '\t') /* Skip whitespace */ |
| 2388 | ; |
| 2389 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2390 | switch (c) { |
| 2391 | default: |
| 2392 | if (any(c, "0123456789")) { |
| 2393 | unget(c1 = my_getc(0)); |
| 2394 | if (c1 == '<' || c1 == '>') { |
| 2395 | iounit = c - '0'; |
| 2396 | goto loop; |
| 2397 | } |
| 2398 | *e.linep++ = c; |
| 2399 | c = c1; |
| 2400 | } |
| 2401 | break; |
| 2402 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2403 | case '#': /* Comment, skip to next newline or End-of-string */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2404 | while ((c = my_getc(0)) != 0 && c != '\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2405 | unget(c); |
| 2406 | goto loop; |
| 2407 | |
| 2408 | case 0: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2409 | DBGPRINTF5(("YYLEX: return 0, c=%d\n", c)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2410 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2411 | |
| 2412 | case '$': |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2413 | DBGPRINTF9(("YYLEX: found $\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2414 | *e.linep++ = c; |
| 2415 | if ((c = my_getc(0)) == '{') { |
| 2416 | if ((c = collect(c, '}')) != '\0') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2417 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2418 | goto pack; |
| 2419 | } |
| 2420 | break; |
| 2421 | |
| 2422 | case '`': |
| 2423 | case '\'': |
| 2424 | case '"': |
| 2425 | if ((c = collect(c, c)) != '\0') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2426 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2427 | goto pack; |
| 2428 | |
| 2429 | case '|': |
| 2430 | case '&': |
| 2431 | case ';': |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2432 | startl = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2433 | /* If more chars process them, else return NULL char */ |
| 2434 | if ((c1 = dual(c)) != '\0') |
| 2435 | return (c1); |
| 2436 | else |
| 2437 | return (c); |
| 2438 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2439 | case '^': |
| 2440 | startl = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2441 | return ('|'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2442 | case '>': |
| 2443 | case '<': |
| 2444 | diag(c); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2445 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2446 | |
| 2447 | case '\n': |
| 2448 | nlseen++; |
| 2449 | gethere(); |
| 2450 | startl = 1; |
| 2451 | if (multiline || cf & CONTIN) { |
| 2452 | if (interactive && e.iop <= iostack) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 2453 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2454 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2455 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2456 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2457 | #endif |
| 2458 | } |
| 2459 | if (cf & CONTIN) |
| 2460 | goto loop; |
| 2461 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2462 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2463 | |
| 2464 | case '(': |
| 2465 | case ')': |
| 2466 | startl = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2467 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | unget(c); |
| 2471 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2472 | pack: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2473 | while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n")) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2474 | if (e.linep >= elinep) |
| 2475 | err("word too long"); |
| 2476 | else |
| 2477 | *e.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2478 | }; |
| 2479 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2480 | unget(c); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2481 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2482 | if (any(c, "\"'`$")) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2483 | goto loop; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2484 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2485 | *e.linep++ = '\0'; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2486 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2487 | if (atstart && (c = rlookup(line)) != 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2488 | startl = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2489 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2490 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2491 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2492 | yylval.cp = strsave(line, areanum); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2493 | return (WORD); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2496 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2497 | static int collect(c, c1) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2498 | REGISTER int c, c1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2499 | { |
| 2500 | char s[2]; |
| 2501 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2502 | DBGPRINTF8(("COLLECT: enter, c=%d, c1=%d\n", c, c1)); |
| 2503 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2504 | *e.linep++ = c; |
| 2505 | while ((c = my_getc(c1)) != c1) { |
| 2506 | if (c == 0) { |
| 2507 | unget(c); |
| 2508 | s[0] = c1; |
| 2509 | s[1] = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2510 | prs("no closing "); |
| 2511 | yyerror(s); |
| 2512 | return (YYERRCODE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2513 | } |
| 2514 | if (interactive && c == '\n' && e.iop <= iostack) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 2515 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2516 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2517 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2518 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2519 | #endif |
| 2520 | } |
| 2521 | *e.linep++ = c; |
| 2522 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2523 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2524 | *e.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2525 | |
| 2526 | DBGPRINTF8(("COLLECT: return 0, line is %s\n", line)); |
| 2527 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2528 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2531 | /* "multiline commands" helper func */ |
| 2532 | /* see if next 2 chars form a shell multiline */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2533 | static int dual(c) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2534 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2535 | { |
| 2536 | char s[3]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2537 | REGISTER char *cp = s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2538 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2539 | DBGPRINTF8(("DUAL: enter, c=%d\n", c)); |
| 2540 | |
| 2541 | *cp++ = c; /* c is the given "peek" char */ |
| 2542 | *cp++ = my_getc(0); /* get next char of input */ |
| 2543 | *cp = 0; /* add EOS marker */ |
| 2544 | |
| 2545 | c = rlookup(s); /* see if 2 chars form a shell multiline */ |
| 2546 | if (c == 0) |
| 2547 | unget(*--cp); /* String is not a shell multiline, put peek char back */ |
| 2548 | |
| 2549 | return (c); /* String is multiline, return numeric multiline (restab) code */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2552 | static void diag(ec) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2553 | REGISTER int ec; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2554 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2555 | REGISTER int c; |
| 2556 | |
| 2557 | DBGPRINTF8(("DIAG: enter, ec=%d\n", ec)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2558 | |
| 2559 | c = my_getc(0); |
| 2560 | if (c == '>' || c == '<') { |
| 2561 | if (c != ec) |
| 2562 | zzerr(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2563 | yylval.i = ec == '>' ? IOWRITE | IOCAT : IOHERE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2564 | c = my_getc(0); |
| 2565 | } else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2566 | yylval.i = ec == '>' ? IOWRITE : IOREAD; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2567 | if (c != '&' || yylval.i == IOHERE) |
| 2568 | unget(c); |
| 2569 | else |
| 2570 | yylval.i |= IODUP; |
| 2571 | } |
| 2572 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2573 | static char *tree(size) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2574 | unsigned size; |
| 2575 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2576 | REGISTER char *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2577 | |
| 2578 | if ((t = getcell(size)) == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2579 | DBGPRINTF2(("TREE: getcell(%d) failed!\n", size)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2580 | prs("command line too complicated\n"); |
| 2581 | fail(); |
| 2582 | /* NOTREACHED */ |
| 2583 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2584 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | /* VARARGS1 */ |
| 2588 | /* ARGSUSED */ |
| 2589 | |
| 2590 | /* -------- exec.c -------- */ |
| 2591 | |
| 2592 | /* |
| 2593 | * execute tree |
| 2594 | */ |
| 2595 | |
| 2596 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2597 | static int execute(t, pin, pout, act) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2598 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2599 | int *pin, *pout; |
| 2600 | int act; |
| 2601 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2602 | REGISTER struct op *t1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2603 | volatile int i, rv, a; |
| 2604 | char *cp, **wp, **wp2; |
| 2605 | struct var *vp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2606 | struct op *outtree_save; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2607 | struct brkcon bc; |
| 2608 | |
| 2609 | #if __GNUC__ |
| 2610 | /* Avoid longjmp clobbering */ |
| 2611 | (void) ℘ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2612 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2613 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2614 | if (t == NULL) { |
| 2615 | DBGPRINTF4(("EXECUTE: enter, t==null, returning.\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2616 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
| 2619 | DBGPRINTF(("EXECUTE: t=0x%x, t->type=%d (%s), t->words is %s\n", t, |
| 2620 | t->type, T_CMD_NAMES[t->type], |
| 2621 | ((t->words == NULL) ? "NULL" : t->words[0]))); |
| 2622 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2623 | rv = 0; |
| 2624 | a = areanum++; |
| 2625 | wp = (wp2 = t->words) != NULL |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2626 | ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY) |
| 2627 | : NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2628 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2629 | /* Hard to know how many words there are, be careful of garbage pointer values */ |
| 2630 | /* They are likely to cause "PCI bus fault" errors */ |
| 2631 | #if 0 |
| 2632 | DBGPRINTF(("EXECUTE: t->left=0x%x, t->right=0x%x, t->words[1] is %s\n", |
| 2633 | t->left, t->right, |
| 2634 | ((t->words[1] == NULL) ? "NULL" : t->words[1]))); |
| 2635 | DBGPRINTF7(("EXECUTE: t->words[2] is %s, t->words[3] is %s\n", |
| 2636 | ((t->words[2] == NULL) ? "NULL" : t->words[2]), |
| 2637 | ((t->words[3] == NULL) ? "NULL" : t->words[3]))); |
| 2638 | #endif |
| 2639 | |
| 2640 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2641 | switch (t->type) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2642 | case TDOT: |
| 2643 | DBGPRINTF3(("EXECUTE: TDOT\n")); |
| 2644 | |
| 2645 | outtree_save = outtree; |
| 2646 | |
| 2647 | newfile(evalstr(t->words[0], DOALL)); |
| 2648 | |
| 2649 | t->left = dowholefile(TLIST, 0); |
| 2650 | t->right = NULL; |
| 2651 | |
| 2652 | outtree = outtree_save; |
| 2653 | |
| 2654 | if (t->left) |
| 2655 | rv = execute(t->left, pin, pout, 0); |
| 2656 | if (t->right) |
| 2657 | rv = execute(t->right, pin, pout, 0); |
| 2658 | break; |
| 2659 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2660 | case TPAREN: |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 2661 | rv = execute(t->left, pin, pout, 0); |
| 2662 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2663 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2664 | case TCOM: |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 2665 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2666 | rv = forkexec(t, pin, pout, act, wp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2667 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2668 | break; |
| 2669 | |
| 2670 | case TPIPE: |
| 2671 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2672 | int pv[2]; |
| 2673 | |
| 2674 | if ((rv = openpipe(pv)) < 0) |
| 2675 | break; |
| 2676 | pv[0] = remap(pv[0]); |
| 2677 | pv[1] = remap(pv[1]); |
| 2678 | (void) execute(t->left, pin, pv, 0); |
| 2679 | rv = execute(t->right, pv, pout, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2680 | } |
| 2681 | break; |
| 2682 | |
| 2683 | case TLIST: |
| 2684 | (void) execute(t->left, pin, pout, 0); |
| 2685 | rv = execute(t->right, pin, pout, 0); |
| 2686 | break; |
| 2687 | |
| 2688 | case TASYNC: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2689 | { |
| 2690 | int hinteractive = interactive; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2691 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2692 | DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n")); |
| 2693 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2694 | i = vfork(); |
| 2695 | if (i != 0) { |
| 2696 | interactive = hinteractive; |
| 2697 | if (i != -1) { |
| 2698 | setval(lookup("!"), putn(i)); |
| 2699 | if (pin != NULL) |
| 2700 | closepipe(pin); |
| 2701 | if (interactive) { |
| 2702 | prs(putn(i)); |
| 2703 | prs("\n"); |
| 2704 | } |
| 2705 | } else |
| 2706 | rv = -1; |
| 2707 | setstatus(rv); |
| 2708 | } else { |
| 2709 | signal(SIGINT, SIG_IGN); |
| 2710 | signal(SIGQUIT, SIG_IGN); |
| 2711 | if (interactive) |
| 2712 | signal(SIGTERM, SIG_DFL); |
| 2713 | interactive = 0; |
| 2714 | if (pin == NULL) { |
| 2715 | close(0); |
| 2716 | open("/dev/null", 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2717 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2718 | _exit(execute(t->left, pin, pout, FEXEC)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2719 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2720 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2721 | break; |
| 2722 | |
| 2723 | case TOR: |
| 2724 | case TAND: |
| 2725 | rv = execute(t->left, pin, pout, 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2726 | if ((t1 = t->right) != NULL && (rv == 0) == (t->type == TAND)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2727 | rv = execute(t1, pin, pout, 0); |
| 2728 | break; |
| 2729 | |
| 2730 | case TFOR: |
| 2731 | if (wp == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2732 | wp = dolv + 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2733 | if ((i = dolc) < 0) |
| 2734 | i = 0; |
| 2735 | } else { |
| 2736 | i = -1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2737 | while (*wp++ != NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2738 | } |
| 2739 | vp = lookup(t->str); |
| 2740 | while (setjmp(bc.brkpt)) |
| 2741 | if (isbreak) |
| 2742 | goto broken; |
| 2743 | brkset(&bc); |
| 2744 | for (t1 = t->left; i-- && *wp != NULL;) { |
| 2745 | setval(vp, *wp++); |
| 2746 | rv = execute(t1, pin, pout, 0); |
| 2747 | } |
| 2748 | brklist = brklist->nextlev; |
| 2749 | break; |
| 2750 | |
| 2751 | case TWHILE: |
| 2752 | case TUNTIL: |
| 2753 | while (setjmp(bc.brkpt)) |
| 2754 | if (isbreak) |
| 2755 | goto broken; |
| 2756 | brkset(&bc); |
| 2757 | t1 = t->left; |
| 2758 | while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE)) |
| 2759 | rv = execute(t->right, pin, pout, 0); |
| 2760 | brklist = brklist->nextlev; |
| 2761 | break; |
| 2762 | |
| 2763 | case TIF: |
| 2764 | case TELIF: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2765 | if (t->right != NULL) { |
| 2766 | rv = !execute(t->left, pin, pout, 0) ? |
| 2767 | execute(t->right->left, pin, pout, 0) : |
| 2768 | execute(t->right->right, pin, pout, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2769 | } |
| 2770 | break; |
| 2771 | |
| 2772 | case TCASE: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2773 | if ((cp = evalstr(t->str, DOSUB | DOTRIM)) == 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2774 | cp = ""; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2775 | |
| 2776 | DBGPRINTF7(("EXECUTE: TCASE, t->str is %s, cp is %s\n", |
| 2777 | ((t->str == NULL) ? "NULL" : t->str), |
| 2778 | ((cp == NULL) ? "NULL" : cp))); |
| 2779 | |
| 2780 | if ((t1 = findcase(t->left, cp)) != NULL) { |
| 2781 | 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] | 2782 | rv = execute(t1, pin, pout, 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2783 | DBGPRINTF7(("EXECUTE: TCASE, back from execute(t=0x%x, t1=0x%x)...\n", t, t1)); |
| 2784 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2785 | break; |
| 2786 | |
| 2787 | case TBRACE: |
| 2788 | /* |
| 2789 | if (iopp = t->ioact) |
| 2790 | while (*iopp) |
| 2791 | if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) { |
| 2792 | rv = -1; |
| 2793 | break; |
| 2794 | } |
| 2795 | */ |
| 2796 | if (rv >= 0 && (t1 = t->left)) |
| 2797 | rv = execute(t1, pin, pout, 0); |
| 2798 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2799 | |
| 2800 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2801 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2802 | broken: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2803 | t->words = wp2; |
| 2804 | isbreak = 0; |
| 2805 | freehere(areanum); |
| 2806 | freearea(areanum); |
| 2807 | areanum = a; |
| 2808 | if (interactive && intr) { |
| 2809 | closeall(); |
| 2810 | fail(); |
| 2811 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2812 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2813 | if ((i = trapset) != 0) { |
| 2814 | trapset = 0; |
| 2815 | runtrap(i); |
| 2816 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2817 | |
| 2818 | DBGPRINTF(("EXECUTE: returning from t=0x%x, rv=%d\n", t, rv)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2819 | return (rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | static int |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2823 | 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] | 2824 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2825 | pid_t newpid; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2826 | int i, rv; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2827 | int (*shcom) (struct op *) = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2828 | REGISTER int f; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2829 | char *cp = NULL; |
| 2830 | struct ioword **iopp; |
| 2831 | int resetsig; |
| 2832 | char **owp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2833 | int forked = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2834 | |
| 2835 | int *hpin = pin; |
| 2836 | int *hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2837 | char *hwp; |
| 2838 | int hinteractive; |
| 2839 | int hintr; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2840 | struct brkcon *hbrklist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2841 | int hexecflg; |
| 2842 | |
| 2843 | #if __GNUC__ |
| 2844 | /* Avoid longjmp clobbering */ |
| 2845 | (void) &pin; |
| 2846 | (void) &pout; |
| 2847 | (void) ℘ |
| 2848 | (void) &shcom; |
| 2849 | (void) &cp; |
| 2850 | (void) &resetsig; |
| 2851 | (void) &owp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2852 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2853 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2854 | DBGPRINTF(("FORKEXEC: t=0x%x, pin 0x%x, pout 0x%x, act %d\n", t, pin, |
| 2855 | pout, act)); |
| 2856 | DBGPRINTF7(("FORKEXEC: t->words is %s\n", |
| 2857 | ((t->words == NULL) ? "NULL" : t->words[0]))); |
| 2858 | |
| 2859 | /* Hard to know how many words there are, be careful of garbage pointer values */ |
| 2860 | /* They are likely to cause "PCI bus fault" errors */ |
| 2861 | #if 0 |
| 2862 | DBGPRINTF7(("FORKEXEC: t->words is %s, t->words[1] is %s\n", |
| 2863 | ((t->words == NULL) ? "NULL" : t->words[0]), |
| 2864 | ((t->words == NULL) ? "NULL" : t->words[1]))); |
| 2865 | DBGPRINTF7(("FORKEXEC: wp is %s, wp[1] is %s\n", |
| 2866 | ((wp == NULL) ? "NULL" : wp[0]), |
| 2867 | ((wp[1] == NULL) ? "NULL" : wp[1]))); |
| 2868 | DBGPRINTF7(("FORKEXEC: wp2 is %s, wp[3] is %s\n", |
| 2869 | ((wp[2] == NULL) ? "NULL" : wp[2]), |
| 2870 | ((wp[3] == NULL) ? "NULL" : wp[3]))); |
| 2871 | #endif |
| 2872 | |
| 2873 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2874 | owp = wp; |
| 2875 | resetsig = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2876 | rv = -1; /* system-detected error */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2877 | if (t->type == TCOM) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2878 | while ((cp = *wp++) != NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2879 | cp = *wp; |
| 2880 | |
| 2881 | /* strip all initial assignments */ |
| 2882 | /* not correct wrt PATH=yyy command etc */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2883 | if (flag['x']) { |
| 2884 | DBGPRINTF9(("FORKEXEC: echo'ing, cp=0x%x, wp=0x%x, owp=0x%x\n", |
| 2885 | cp, wp, owp)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2886 | echo(cp ? wp : owp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2887 | } |
| 2888 | #if 0 |
| 2889 | DBGPRINTF9(("FORKEXEC: t->words is %s, t->words[1] is %s\n", |
| 2890 | ((t->words == NULL) ? "NULL" : t->words[0]), |
| 2891 | ((t->words == NULL) ? "NULL" : t->words[1]))); |
| 2892 | DBGPRINTF9(("FORKEXEC: wp is %s, wp[1] is %s\n", |
| 2893 | ((wp == NULL) ? "NULL" : wp[0]), |
| 2894 | ((wp == NULL) ? "NULL" : wp[1]))); |
| 2895 | #endif |
| 2896 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2897 | if (cp == NULL && t->ioact == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2898 | while ((cp = *owp++) != NULL && assign(cp, COPYV)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2899 | DBGPRINTF(("FORKEXEC: returning setstatus()\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2900 | return (setstatus(0)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2901 | } else if (cp != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2902 | shcom = inbuilt(cp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2903 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2904 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2905 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2906 | t->words = wp; |
| 2907 | f = act; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2908 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2909 | #if 0 |
| 2910 | DBGPRINTF3(("FORKEXEC: t->words is %s, t->words[1] is %s\n", |
| 2911 | ((t->words == NULL) ? "NULL" : t->words[0]), |
| 2912 | ((t->words == NULL) ? "NULL" : t->words[1]))); |
| 2913 | #endif |
| 2914 | DBGPRINTF(("FORKEXEC: shcom 0x%x, f&FEXEC 0x%x, owp 0x%x\n", shcom, |
| 2915 | f & FEXEC, owp)); |
| 2916 | |
| 2917 | if (shcom == NULL && (f & FEXEC) == 0) { |
| 2918 | /* Save values in case the child process alters them */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2919 | hpin = pin; |
| 2920 | hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2921 | hwp = *wp; |
| 2922 | hinteractive = interactive; |
| 2923 | hintr = intr; |
| 2924 | hbrklist = brklist; |
| 2925 | hexecflg = execflg; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2926 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2927 | DBGPRINTF3(("FORKEXEC: calling vfork()...\n")); |
| 2928 | |
| 2929 | newpid = vfork(); |
| 2930 | |
| 2931 | if (newpid == -1) { |
| 2932 | DBGPRINTF(("FORKEXEC: ERROR, unable to vfork()!\n")); |
| 2933 | return (-1); |
| 2934 | } |
| 2935 | |
| 2936 | |
| 2937 | if (newpid > 0) { /* Parent */ |
| 2938 | |
| 2939 | /* Restore values */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2940 | pin = hpin; |
| 2941 | pout = hpout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2942 | *wp = hwp; |
| 2943 | interactive = hinteractive; |
| 2944 | intr = hintr; |
| 2945 | brklist = hbrklist; |
| 2946 | execflg = hexecflg; |
| 2947 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2948 | /* moved up |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2949 | if (i == -1) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2950 | return(rv); |
| 2951 | */ |
| 2952 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2953 | if (pin != NULL) |
| 2954 | closepipe(pin); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2955 | |
| 2956 | return (pout == NULL ? setstatus(waitfor(newpid, 0)) : 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2959 | /* Must be the child process, pid should be 0 */ |
| 2960 | DBGPRINTF(("FORKEXEC: child process, shcom=0x%x\n", shcom)); |
| 2961 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2962 | if (interactive) { |
| 2963 | signal(SIGINT, SIG_IGN); |
| 2964 | signal(SIGQUIT, SIG_IGN); |
| 2965 | resetsig = 1; |
| 2966 | } |
| 2967 | interactive = 0; |
| 2968 | intr = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2969 | forked = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2970 | brklist = 0; |
| 2971 | execflg = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2972 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2973 | |
| 2974 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2975 | if (owp != NULL) |
| 2976 | while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
| 2977 | if (shcom == NULL) |
| 2978 | export(lookup(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2979 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2980 | #ifdef COMPIPE |
| 2981 | if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) { |
| 2982 | err("piping to/from shell builtins not yet done"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2983 | if (forked) |
| 2984 | _exit(-1); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2985 | return (-1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2986 | } |
| 2987 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2988 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2989 | if (pin != NULL) { |
| 2990 | dup2(pin[0], 0); |
| 2991 | closepipe(pin); |
| 2992 | } |
| 2993 | if (pout != NULL) { |
| 2994 | dup2(pout[1], 1); |
| 2995 | closepipe(pout); |
| 2996 | } |
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 | if ((iopp = t->ioact) != NULL) { |
| 2999 | if (shcom != NULL && shcom != doexec) { |
| 3000 | prs(cp); |
| 3001 | err(": cannot redirect shell command"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3002 | if (forked) |
| 3003 | _exit(-1); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3004 | return (-1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3005 | } |
| 3006 | while (*iopp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3007 | if (iosetup(*iopp++, pin != NULL, pout != NULL)) { |
| 3008 | if (forked) |
| 3009 | _exit(rv); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3010 | return (rv); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3011 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3012 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3013 | |
| 3014 | if (shcom) { |
| 3015 | i = setstatus((*shcom) (t)); |
| 3016 | if (forked) |
| 3017 | _exit(i); |
| 3018 | DBGPRINTF(("FORKEXEC: returning i=%d\n", i)); |
| 3019 | return (i); |
| 3020 | } |
| 3021 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3022 | /* should use FIOCEXCL */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3023 | for (i = FDBASE; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3024 | close(i); |
| 3025 | if (resetsig) { |
| 3026 | signal(SIGINT, SIG_DFL); |
| 3027 | signal(SIGQUIT, SIG_DFL); |
| 3028 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3029 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3030 | if (t->type == TPAREN) |
| 3031 | _exit(execute(t->left, NOPIPE, NOPIPE, FEXEC)); |
| 3032 | if (wp[0] == NULL) |
| 3033 | _exit(0); |
| 3034 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3035 | cp = rexecve(wp[0], wp, makenv(0, NULL)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3036 | prs(wp[0]); |
| 3037 | prs(": "); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3038 | err(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3039 | if (!execflg) |
| 3040 | trap[0] = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3041 | |
| 3042 | DBGPRINTF(("FORKEXEC: calling leave(), pid=%d\n", newpid)); |
| 3043 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3044 | leave(); |
| 3045 | /* NOTREACHED */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3046 | _exit(1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | /* |
| 3050 | * 0< 1> are ignored as required |
| 3051 | * within pipelines. |
| 3052 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3053 | static int iosetup(iop, pipein, pipeout) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3054 | REGISTER struct ioword *iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3055 | int pipein, pipeout; |
| 3056 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3057 | REGISTER int u = -1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3058 | char *cp = NULL, *msg; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3059 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3060 | DBGPRINTF(("IOSETUP: iop 0x%x, pipein 0x%x, pipeout 0x%x\n", iop, |
| 3061 | pipein, pipeout)); |
| 3062 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3063 | if (iop->io_unit == IODEFAULT) /* take default */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3064 | iop->io_unit = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3065 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3066 | if (pipein && iop->io_unit == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3067 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3068 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3069 | if (pipeout && iop->io_unit == 1) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3070 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3071 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3072 | msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3073 | if ((iop->io_flag & IOHERE) == 0) { |
| 3074 | cp = iop->io_name; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3075 | if ((cp = evalstr(cp, DOSUB | DOTRIM)) == NULL) |
| 3076 | return (1); |
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 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3079 | if (iop->io_flag & IODUP) { |
| 3080 | if (cp[1] || (!isdigit(*cp) && *cp != '-')) { |
| 3081 | prs(cp); |
| 3082 | err(": illegal >& argument"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3083 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3084 | } |
| 3085 | if (*cp == '-') |
| 3086 | iop->io_flag = IOCLOSE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3087 | iop->io_flag &= ~(IOREAD | IOWRITE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3088 | } |
| 3089 | switch (iop->io_flag) { |
| 3090 | case IOREAD: |
| 3091 | u = open(cp, 0); |
| 3092 | break; |
| 3093 | |
| 3094 | case IOHERE: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3095 | case IOHERE | IOXHERE: |
| 3096 | u = herein(iop->io_name, iop->io_flag & IOXHERE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3097 | cp = "here file"; |
| 3098 | break; |
| 3099 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3100 | case IOWRITE | IOCAT: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3101 | if ((u = open(cp, 1)) >= 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3102 | lseek(u, (long) 0, 2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3103 | break; |
| 3104 | } |
| 3105 | case IOWRITE: |
| 3106 | u = creat(cp, 0666); |
| 3107 | break; |
| 3108 | |
| 3109 | case IODUP: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3110 | u = dup2(*cp - '0', iop->io_unit); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3111 | break; |
| 3112 | |
| 3113 | case IOCLOSE: |
| 3114 | close(iop->io_unit); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3115 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3116 | } |
| 3117 | if (u < 0) { |
| 3118 | prs(cp); |
| 3119 | prs(": cannot "); |
| 3120 | warn(msg); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3121 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3122 | } else { |
| 3123 | if (u != iop->io_unit) { |
| 3124 | dup2(u, iop->io_unit); |
| 3125 | close(u); |
| 3126 | } |
| 3127 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3128 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3131 | static void echo(wp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3132 | REGISTER char **wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3133 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3134 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3135 | |
| 3136 | prs("+"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3137 | for (i = 0; wp[i]; i++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3138 | if (i) |
| 3139 | prs(" "); |
| 3140 | prs(wp[i]); |
| 3141 | } |
| 3142 | prs("\n"); |
| 3143 | } |
| 3144 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3145 | static struct op **find1case(t, w) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3146 | struct op *t; |
| 3147 | char *w; |
| 3148 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3149 | REGISTER struct op *t1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3150 | struct op **tp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3151 | REGISTER char **wp, *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3152 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3153 | |
| 3154 | if (t == NULL) { |
| 3155 | DBGPRINTF3(("FIND1CASE: enter, t==NULL, returning.\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3156 | return ((struct op **) NULL); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | DBGPRINTF3(("FIND1CASE: enter, t->type=%d (%s)\n", t->type, |
| 3160 | T_CMD_NAMES[t->type])); |
| 3161 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3162 | if (t->type == TLIST) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3163 | if ((tp = find1case(t->left, w)) != NULL) { |
| 3164 | 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] | 3165 | return (tp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3166 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3167 | t1 = t->right; /* TPAT */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3168 | } else |
| 3169 | t1 = t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3170 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3171 | for (wp = t1->words; *wp;) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3172 | if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp)) { |
| 3173 | DBGPRINTF3(("FIND1CASE: returning &t1->left= 0x%x.\n", |
| 3174 | &t1->left)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3175 | return (&t1->left); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | DBGPRINTF(("FIND1CASE: returning NULL\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3179 | return ((struct op **) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3182 | static struct op *findcase(t, w) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3183 | struct op *t; |
| 3184 | char *w; |
| 3185 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3186 | REGISTER struct op **tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3187 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3188 | return ((tp = find1case(t, w)) != NULL ? *tp : (struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
| 3191 | /* |
| 3192 | * Enter a new loop level (marked for break/continue). |
| 3193 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3194 | static void brkset(bc) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3195 | struct brkcon *bc; |
| 3196 | { |
| 3197 | bc->nextlev = brklist; |
| 3198 | brklist = bc; |
| 3199 | } |
| 3200 | |
| 3201 | /* |
| 3202 | * Wait for the last process created. |
| 3203 | * Print a message for each process found |
| 3204 | * that was killed by a signal. |
| 3205 | * Ignore interrupt signals while waiting |
| 3206 | * unless `canintr' is true. |
| 3207 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3208 | static int waitfor(lastpid, canintr) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3209 | REGISTER int lastpid; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3210 | int canintr; |
| 3211 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3212 | REGISTER int pid, rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3213 | int s; |
| 3214 | int oheedint = heedint; |
| 3215 | |
| 3216 | heedint = 0; |
| 3217 | rv = 0; |
| 3218 | do { |
| 3219 | pid = wait(&s); |
| 3220 | if (pid == -1) { |
| 3221 | if (errno != EINTR || canintr) |
| 3222 | break; |
| 3223 | } else { |
| 3224 | if ((rv = WAITSIG(s)) != 0) { |
| 3225 | if (rv < NSIGNAL) { |
| 3226 | if (signame[rv] != NULL) { |
| 3227 | if (pid != lastpid) { |
| 3228 | prn(pid); |
| 3229 | prs(": "); |
| 3230 | } |
| 3231 | prs(signame[rv]); |
| 3232 | } |
| 3233 | } else { |
| 3234 | if (pid != lastpid) { |
| 3235 | prn(pid); |
| 3236 | prs(": "); |
| 3237 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3238 | prs("Signal "); |
| 3239 | prn(rv); |
| 3240 | prs(" "); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3241 | } |
| 3242 | if (WAITCORE(s)) |
| 3243 | prs(" - core dumped"); |
| 3244 | if (rv >= NSIGNAL || signame[rv]) |
| 3245 | prs("\n"); |
| 3246 | rv = -1; |
| 3247 | } else |
| 3248 | rv = WAITVAL(s); |
| 3249 | } |
| 3250 | } while (pid != lastpid); |
| 3251 | heedint = oheedint; |
| 3252 | if (intr) { |
| 3253 | if (interactive) { |
| 3254 | if (canintr) |
| 3255 | intr = 0; |
| 3256 | } else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3257 | if (exstat == 0) |
| 3258 | exstat = rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3259 | onintr(0); |
| 3260 | } |
| 3261 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3262 | return (rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3265 | static int setstatus(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3266 | REGISTER int s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3267 | { |
| 3268 | exstat = s; |
| 3269 | setval(lookup("?"), putn(s)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3270 | return (s); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | /* |
| 3274 | * PATH-searching interface to execve. |
| 3275 | * If getenv("PATH") were kept up-to-date, |
| 3276 | * execvp might be used. |
| 3277 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3278 | static char *rexecve(c, v, envp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3279 | char *c, **v, **envp; |
| 3280 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3281 | REGISTER int i; |
| 3282 | REGISTER char *sp, *tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3283 | int eacces = 0, asis = 0; |
| 3284 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3285 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3286 | char *name = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3287 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3288 | optind = 1; |
| 3289 | if (find_applet_by_name(name)) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3290 | /* We have to exec here since we vforked. Running |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3291 | * run_applet_by_name() won't work and bad things |
| 3292 | * will happen. */ |
| 3293 | execve("/proc/self/exe", v, envp); |
| 3294 | execve("busybox", v, envp); |
| 3295 | } |
| 3296 | #endif |
| 3297 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3298 | DBGPRINTF(("REXECVE: c=0x%x, v=0x%x, envp=0x%x\n", c, v, envp)); |
| 3299 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3300 | sp = any('/', c) ? "" : path->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3301 | asis = *sp == '\0'; |
| 3302 | while (asis || *sp != '\0') { |
| 3303 | asis = 0; |
| 3304 | tp = e.linep; |
| 3305 | for (; *sp != '\0'; tp++) |
| 3306 | if ((*tp = *sp++) == ':') { |
| 3307 | asis = *sp == '\0'; |
| 3308 | break; |
| 3309 | } |
| 3310 | if (tp != e.linep) |
| 3311 | *tp++ = '/'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3312 | for (i = 0; (*tp++ = c[i++]) != '\0';); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3313 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3314 | DBGPRINTF3(("REXECVE: e.linep is %s\n", e.linep)); |
| 3315 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3316 | execve(e.linep, v, envp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3317 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3318 | switch (errno) { |
| 3319 | case ENOEXEC: |
| 3320 | *v = e.linep; |
| 3321 | tp = *--v; |
| 3322 | *v = e.linep; |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 3323 | execve(DEFAULT_SHELL, v, envp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3324 | *v = tp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3325 | return ("no Shell"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3326 | |
| 3327 | case ENOMEM: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3328 | return ((char *) bb_msg_memory_exhausted); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3329 | |
| 3330 | case E2BIG: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3331 | return ("argument list too long"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3332 | |
| 3333 | case EACCES: |
| 3334 | eacces++; |
| 3335 | break; |
| 3336 | } |
| 3337 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3338 | return (errno == ENOENT ? "not found" : "cannot execute"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | /* |
| 3342 | * Run the command produced by generator `f' |
| 3343 | * applied to stream `arg'. |
| 3344 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3345 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3346 | { |
| 3347 | struct op *otree; |
| 3348 | struct wdblock *swdlist; |
| 3349 | struct wdblock *siolist; |
| 3350 | jmp_buf ev, rt; |
| 3351 | xint *ofail; |
| 3352 | int rv; |
| 3353 | |
| 3354 | #if __GNUC__ |
| 3355 | /* Avoid longjmp clobbering */ |
| 3356 | (void) &rv; |
| 3357 | #endif |
| 3358 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3359 | DBGPRINTF(("RUN: enter, areanum %d, outtree 0x%x, failpt 0x%x\n", |
| 3360 | areanum, outtree, failpt)); |
| 3361 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3362 | areanum++; |
| 3363 | swdlist = wdlist; |
| 3364 | siolist = iolist; |
| 3365 | otree = outtree; |
| 3366 | ofail = failpt; |
| 3367 | rv = -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3368 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3369 | if (newenv(setjmp(errpt = ev)) == 0) { |
| 3370 | wdlist = 0; |
| 3371 | iolist = 0; |
| 3372 | pushio(argp, f); |
| 3373 | e.iobase = e.iop; |
| 3374 | yynerrs = 0; |
| 3375 | if (setjmp(failpt = rt) == 0 && yyparse() == 0) |
| 3376 | rv = execute(outtree, NOPIPE, NOPIPE, 0); |
| 3377 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3378 | } else { |
| 3379 | DBGPRINTF(("RUN: error from newenv()!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3380 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3381 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3382 | wdlist = swdlist; |
| 3383 | iolist = siolist; |
| 3384 | failpt = ofail; |
| 3385 | outtree = otree; |
| 3386 | freearea(areanum--); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3387 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3388 | return (rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | /* -------- do.c -------- */ |
| 3392 | |
| 3393 | /* |
| 3394 | * built-in commands: doX |
| 3395 | */ |
| 3396 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3397 | static int dohelp(struct op *t) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3398 | { |
| 3399 | int col; |
| 3400 | const struct builtincmd *x; |
| 3401 | |
| 3402 | printf("\nBuilt-in commands:\n"); |
| 3403 | printf("-------------------\n"); |
| 3404 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3405 | for (col = 0, x = builtincmds; x->builtinfunc != NULL; x++) { |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3406 | if (!x->name) |
| 3407 | continue; |
| 3408 | col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name); |
| 3409 | if (col > 60) { |
| 3410 | printf("\n"); |
| 3411 | col = 0; |
| 3412 | } |
| 3413 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3414 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3415 | { |
| 3416 | int i; |
| 3417 | const struct BB_applet *applet; |
| 3418 | extern const struct BB_applet applets[]; |
| 3419 | extern const size_t NUM_APPLETS; |
| 3420 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3421 | for (i = 0, applet = applets; i < NUM_APPLETS; applet++, i++) { |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3422 | if (!applet->name) |
| 3423 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3424 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3425 | col += printf("%s%s", ((col == 0) ? "\t" : " "), applet->name); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3426 | if (col > 60) { |
| 3427 | printf("\n"); |
| 3428 | col = 0; |
| 3429 | } |
| 3430 | } |
| 3431 | } |
| 3432 | #endif |
| 3433 | printf("\n\n"); |
| 3434 | return EXIT_SUCCESS; |
| 3435 | } |
| 3436 | |
| 3437 | |
| 3438 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3439 | static int dolabel(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3440 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3441 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3444 | static int dochdir(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3445 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3446 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3447 | REGISTER char *cp, *er; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3448 | |
| 3449 | if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL) |
| 3450 | er = ": no home directory"; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3451 | else if (chdir(cp) < 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3452 | er = ": bad directory"; |
| 3453 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3454 | return (0); |
| 3455 | prs(cp != NULL ? cp : "cd"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3456 | err(er); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3457 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3460 | static int doshift(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3461 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3462 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3463 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3464 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3465 | n = t->words[1] ? getn(t->words[1]) : 1; |
| 3466 | if (dolc < n) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3467 | err("nothing to shift"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3468 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3469 | } |
| 3470 | dolv[n] = dolv[0]; |
| 3471 | dolv += n; |
| 3472 | dolc -= n; |
| 3473 | setval(lookup("#"), putn(dolc)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3474 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | /* |
| 3478 | * execute login and newgrp directly |
| 3479 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3480 | static int dologin(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3481 | struct op *t; |
| 3482 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3483 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3484 | |
| 3485 | if (interactive) { |
| 3486 | signal(SIGINT, SIG_DFL); |
| 3487 | signal(SIGQUIT, SIG_DFL); |
| 3488 | } |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3489 | cp = rexecve(t->words[0], t->words, makenv(0, NULL)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3490 | prs(t->words[0]); |
| 3491 | prs(": "); |
| 3492 | err(cp); |
| 3493 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3496 | static int doumask(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3497 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3498 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3499 | REGISTER int i, n; |
| 3500 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3501 | |
| 3502 | if ((cp = t->words[1]) == NULL) { |
| 3503 | i = umask(0); |
| 3504 | umask(i); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3505 | for (n = 3 * 4; (n -= 3) >= 0;) |
| 3506 | putc('0' + ((i >> n) & 07), stderr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3507 | putc('\n', stderr); |
| 3508 | } else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3509 | for (n = 0; *cp >= '0' && *cp <= '9'; cp++) |
| 3510 | n = n * 8 + (*cp - '0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3511 | umask(n); |
| 3512 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3513 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3514 | } |
| 3515 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3516 | static int doexec(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3517 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3518 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3519 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3520 | jmp_buf ex; |
| 3521 | xint *ofail; |
| 3522 | |
| 3523 | t->ioact = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3524 | for (i = 0; (t->words[i] = t->words[i + 1]) != NULL; i++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3525 | if (i == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3526 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3527 | execflg = 1; |
| 3528 | ofail = failpt; |
| 3529 | if (setjmp(failpt = ex) == 0) |
| 3530 | execute(t, NOPIPE, NOPIPE, FEXEC); |
| 3531 | failpt = ofail; |
| 3532 | execflg = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3533 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3536 | static int dodot(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3537 | struct op *t; |
| 3538 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3539 | REGISTER int i; |
| 3540 | REGISTER char *sp, *tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3541 | char *cp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3542 | int maltmp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3543 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3544 | 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))); |
| 3545 | |
| 3546 | if ((cp = t->words[1]) == NULL) { |
| 3547 | DBGPRINTF(("DODOT: bad args, ret 0\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3548 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3549 | } else { |
| 3550 | DBGPRINTF(("DODOT: cp is %s\n", cp)); |
| 3551 | } |
| 3552 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3553 | sp = any('/', cp) ? ":" : path->value; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3554 | |
| 3555 | DBGPRINTF(("DODOT: sp is %s, e.linep is %s\n", |
| 3556 | ((sp == NULL) ? "NULL" : sp), |
| 3557 | ((e.linep == NULL) ? "NULL" : e.linep))); |
| 3558 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3559 | while (*sp) { |
| 3560 | tp = e.linep; |
| 3561 | while (*sp && (*tp = *sp++) != ':') |
| 3562 | tp++; |
| 3563 | if (tp != e.linep) |
| 3564 | *tp++ = '/'; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3565 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3566 | for (i = 0; (*tp++ = cp[i++]) != '\0';); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3567 | |
| 3568 | /* Original code */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3569 | if ((i = open(e.linep, 0)) >= 0) { |
| 3570 | exstat = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3571 | maltmp = remap(i); |
| 3572 | DBGPRINTF(("DODOT: remap=%d, exstat=%d, e.iofd %d, i %d, e.linep is %s\n", maltmp, exstat, e.iofd, i, e.linep)); |
| 3573 | |
| 3574 | next(maltmp); /* Basically a PUSHIO */ |
| 3575 | |
| 3576 | DBGPRINTF(("DODOT: returning exstat=%d\n", exstat)); |
| 3577 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3578 | return (exstat); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3579 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3580 | |
| 3581 | } /* While */ |
| 3582 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3583 | prs(cp); |
| 3584 | err(": not found"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3585 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3586 | return (-1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3587 | } |
| 3588 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3589 | static int dowait(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3590 | struct op *t; |
| 3591 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3592 | REGISTER int i; |
| 3593 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3594 | |
| 3595 | if ((cp = t->words[1]) != NULL) { |
| 3596 | i = getn(cp); |
| 3597 | if (i == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3598 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3599 | } else |
| 3600 | i = -1; |
| 3601 | setstatus(waitfor(i, 1)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3602 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3605 | static int doread(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3606 | struct op *t; |
| 3607 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3608 | REGISTER char *cp, **wp; |
| 3609 | REGISTER int nb = 0; |
| 3610 | REGISTER int nl = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3611 | |
| 3612 | if (t->words[1] == NULL) { |
| 3613 | err("Usage: read name ..."); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3614 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3615 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3616 | for (wp = t->words + 1; *wp; wp++) { |
| 3617 | for (cp = e.linep; !nl && cp < elinep - 1; cp++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3618 | if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) || |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3619 | (nl = (*cp == '\n')) || (wp[1] && any(*cp, ifs->value))) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3620 | break; |
| 3621 | *cp = 0; |
| 3622 | if (nb <= 0) |
| 3623 | break; |
| 3624 | setval(lookup(*wp), e.linep); |
| 3625 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3626 | return (nb <= 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3629 | static int doeval(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3630 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3631 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3632 | return (RUN(awordlist, t->words + 1, wdchar)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3635 | static int dotrap(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3636 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3637 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3638 | REGISTER int n, i; |
| 3639 | REGISTER int resetsig; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3640 | |
| 3641 | if (t->words[1] == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3642 | for (i = 0; i <= _NSIG; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3643 | if (trap[i]) { |
| 3644 | prn(i); |
| 3645 | prs(": "); |
| 3646 | prs(trap[i]); |
| 3647 | prs("\n"); |
| 3648 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3649 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3650 | } |
| 3651 | resetsig = isdigit(*t->words[1]); |
| 3652 | for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) { |
| 3653 | n = getsig(t->words[i]); |
| 3654 | freecell(trap[n]); |
| 3655 | trap[n] = 0; |
| 3656 | if (!resetsig) { |
| 3657 | if (*t->words[1] != '\0') { |
| 3658 | trap[n] = strsave(t->words[1], 0); |
| 3659 | setsig(n, sig); |
| 3660 | } else |
| 3661 | setsig(n, SIG_IGN); |
| 3662 | } else { |
| 3663 | if (interactive) |
| 3664 | if (n == SIGINT) |
| 3665 | setsig(n, onintr); |
| 3666 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3667 | setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3668 | else |
| 3669 | setsig(n, SIG_DFL); |
| 3670 | } |
| 3671 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3672 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3675 | static int getsig(s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3676 | char *s; |
| 3677 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3678 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3679 | |
| 3680 | if ((n = getn(s)) < 0 || n > _NSIG) { |
| 3681 | err("trap: bad signal number"); |
| 3682 | n = 0; |
| 3683 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3684 | return (n); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3687 | static void setsig(REGISTER int n, sighandler_t f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3688 | { |
| 3689 | if (n == 0) |
| 3690 | return; |
| 3691 | if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) { |
| 3692 | ourtrap[n] = 1; |
| 3693 | signal(n, f); |
| 3694 | } |
| 3695 | } |
| 3696 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3697 | static int getn(as) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3698 | char *as; |
| 3699 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3700 | REGISTER char *s; |
| 3701 | REGISTER int n, m; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3702 | |
| 3703 | s = as; |
| 3704 | m = 1; |
| 3705 | if (*s == '-') { |
| 3706 | m = -1; |
| 3707 | s++; |
| 3708 | } |
| 3709 | for (n = 0; isdigit(*s); s++) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3710 | n = (n * 10) + (*s - '0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3711 | if (*s) { |
| 3712 | prs(as); |
| 3713 | err(": bad number"); |
| 3714 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3715 | return (n * m); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3718 | static int dobreak(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3719 | struct op *t; |
| 3720 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3721 | return (brkcontin(t->words[1], 1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3724 | static int docontinue(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3725 | struct op *t; |
| 3726 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3727 | return (brkcontin(t->words[1], 0)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3730 | static int brkcontin(cp, val) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3731 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3732 | int val; |
| 3733 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3734 | REGISTER struct brkcon *bc; |
| 3735 | REGISTER int nl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3736 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3737 | nl = cp == NULL ? 1 : getn(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3738 | if (nl <= 0) |
| 3739 | nl = 999; |
| 3740 | do { |
| 3741 | if ((bc = brklist) == NULL) |
| 3742 | break; |
| 3743 | brklist = bc->nextlev; |
| 3744 | } while (--nl); |
| 3745 | if (nl) { |
| 3746 | err("bad break/continue level"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3747 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3748 | } |
| 3749 | isbreak = val; |
| 3750 | longjmp(bc->brkpt, 1); |
| 3751 | /* NOTREACHED */ |
| 3752 | } |
| 3753 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3754 | static int doexit(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3755 | struct op *t; |
| 3756 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3757 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3758 | |
| 3759 | execflg = 0; |
| 3760 | if ((cp = t->words[1]) != NULL) |
| 3761 | setstatus(getn(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3762 | |
| 3763 | DBGPRINTF(("DOEXIT: calling leave(), t=0x%x\n", t)); |
| 3764 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3765 | leave(); |
| 3766 | /* NOTREACHED */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3767 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3770 | static int doexport(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3771 | struct op *t; |
| 3772 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3773 | rdexp(t->words + 1, export, EXPORT); |
| 3774 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3777 | static int doreadonly(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3778 | struct op *t; |
| 3779 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3780 | rdexp(t->words + 1, ronly, RONLY); |
| 3781 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3784 | static void rdexp(char **wp, void (*f) (struct var *), int key) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3785 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3786 | DBGPRINTF6(("RDEXP: enter, wp=0x%x, func=0x%x, key=%d\n", wp, f, key)); |
| 3787 | DBGPRINTF6(("RDEXP: *wp=%s\n", *wp)); |
| 3788 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3789 | if (*wp != NULL) { |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3790 | for (; *wp != NULL; wp++) { |
| 3791 | if (isassign(*wp)) { |
| 3792 | char *cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3793 | |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3794 | assign(*wp, COPYV); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3795 | for (cp = *wp; *cp != '='; cp++); |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3796 | *cp = '\0'; |
| 3797 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3798 | if (checkname(*wp)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3799 | (*f) (lookup(*wp)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3800 | else |
| 3801 | badid(*wp); |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3802 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3803 | } else |
| 3804 | putvlist(key, 1); |
| 3805 | } |
| 3806 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3807 | static void badid(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3808 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3809 | { |
| 3810 | prs(s); |
| 3811 | err(": bad identifier"); |
| 3812 | } |
| 3813 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3814 | static int doset(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3815 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3816 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3817 | REGISTER struct var *vp; |
| 3818 | REGISTER char *cp; |
| 3819 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3820 | |
| 3821 | if ((cp = t->words[1]) == NULL) { |
| 3822 | for (vp = vlist; vp; vp = vp->next) |
| 3823 | varput(vp->name, 1); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3824 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3825 | } |
| 3826 | if (*cp == '-') { |
| 3827 | /* bad: t->words++; */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3828 | for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3829 | if (*++cp == 0) |
| 3830 | flag['x'] = flag['v'] = 0; |
| 3831 | else |
| 3832 | for (; *cp; cp++) |
| 3833 | switch (*cp) { |
| 3834 | case 'e': |
| 3835 | if (!interactive) |
| 3836 | flag['e']++; |
| 3837 | break; |
| 3838 | |
| 3839 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3840 | if (*cp >= 'a' && *cp <= 'z') |
| 3841 | flag[(int) *cp]++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3842 | break; |
| 3843 | } |
| 3844 | setdash(); |
| 3845 | } |
| 3846 | if (t->words[1]) { |
| 3847 | t->words[0] = dolv[0]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3848 | for (n = 1; t->words[n]; n++) |
| 3849 | setarea((char *) t->words[n], 0); |
| 3850 | dolc = n - 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3851 | dolv = t->words; |
| 3852 | setval(lookup("#"), putn(dolc)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3853 | setarea((char *) (dolv - 1), 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3854 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3855 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3858 | static void varput(s, out) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3859 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3860 | int out; |
| 3861 | { |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3862 | if (isalnum(*s) || *s == '_') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3863 | write(out, s, strlen(s)); |
| 3864 | write(out, "\n", 1); |
| 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | |
| 3869 | /* |
| 3870 | * Copyright (c) 1999 Herbert Xu <herbert@debian.org> |
| 3871 | * This file contains code for the times builtin. |
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 | static int dotimes(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3874 | { |
| 3875 | struct tms buf; |
| 3876 | long int clk_tck = sysconf(_SC_CLK_TCK); |
| 3877 | |
| 3878 | times(&buf); |
| 3879 | printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n", |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3880 | (int) (buf.tms_utime / clk_tck / 60), |
| 3881 | ((double) buf.tms_utime) / clk_tck, |
| 3882 | (int) (buf.tms_stime / clk_tck / 60), |
| 3883 | ((double) buf.tms_stime) / clk_tck, |
| 3884 | (int) (buf.tms_cutime / clk_tck / 60), |
| 3885 | ((double) buf.tms_cutime) / clk_tck, |
| 3886 | (int) (buf.tms_cstime / clk_tck / 60), |
| 3887 | ((double) buf.tms_cstime) / clk_tck); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3888 | return 0; |
| 3889 | } |
| 3890 | |
| 3891 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3892 | static int (*inbuilt(char *s)) (struct op *) { |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3893 | const struct builtincmd *bp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3894 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3895 | for (bp = builtincmds; bp->name != NULL; bp++) |
| 3896 | if (strcmp(bp->name, s) == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3897 | return (bp->builtinfunc); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3898 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3899 | return (NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | /* -------- eval.c -------- */ |
| 3903 | |
| 3904 | /* |
| 3905 | * ${} |
| 3906 | * `command` |
| 3907 | * blank interpretation |
| 3908 | * quoting |
| 3909 | * glob |
| 3910 | */ |
| 3911 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3912 | static char **eval(char **ap, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3913 | { |
| 3914 | struct wdblock *wb; |
| 3915 | char **wp; |
| 3916 | char **wf; |
| 3917 | jmp_buf ev; |
| 3918 | |
| 3919 | #if __GNUC__ |
| 3920 | /* Avoid longjmp clobbering */ |
| 3921 | (void) ℘ |
| 3922 | (void) ≈ |
| 3923 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3924 | |
| 3925 | DBGPRINTF4(("EVAL: enter, f=%d\n", f)); |
| 3926 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3927 | wp = NULL; |
| 3928 | wb = NULL; |
| 3929 | wf = NULL; |
| 3930 | if (newenv(setjmp(errpt = ev)) == 0) { |
| 3931 | while (*ap && isassign(*ap)) |
| 3932 | expand(*ap++, &wb, f & ~DOGLOB); |
| 3933 | if (flag['k']) { |
| 3934 | for (wf = ap; *wf; wf++) { |
| 3935 | if (isassign(*wf)) |
| 3936 | expand(*wf, &wb, f & ~DOGLOB); |
| 3937 | } |
| 3938 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3939 | for (wb = addword((char *) 0, wb); *ap; ap++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3940 | if (!flag['k'] || !isassign(*ap)) |
| 3941 | expand(*ap, &wb, f & ~DOKEY); |
| 3942 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3943 | wb = addword((char *) 0, wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3944 | wp = getwords(wb); |
| 3945 | quitenv(); |
| 3946 | } else |
| 3947 | gflg = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3948 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3949 | return (gflg ? (char **) NULL : wp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | /* |
| 3953 | * Make the exported environment from the exported |
| 3954 | * names in the dictionary. Keyword assignments |
| 3955 | * will already have been done. |
| 3956 | */ |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3957 | static char **makenv(int all, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3958 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3959 | REGISTER struct var *vp; |
| 3960 | |
| 3961 | DBGPRINTF5(("MAKENV: enter, all=%d\n", all)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3962 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3963 | for (vp = vlist; vp; vp = vp->next) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3964 | if (all || vp->status & EXPORT) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3965 | wb = addword(vp->name, wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3966 | wb = addword((char *) 0, wb); |
| 3967 | return (getwords(wb)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3968 | } |
| 3969 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3970 | static char *evalstr(cp, f) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3971 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3972 | int f; |
| 3973 | { |
| 3974 | struct wdblock *wb; |
| 3975 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3976 | DBGPRINTF6(("EVALSTR: enter, cp=0x%x, f=%d\n", cp, f)); |
| 3977 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3978 | wb = NULL; |
| 3979 | if (expand(cp, &wb, f)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3980 | if (wb == NULL || wb->w_nword == 0 |
| 3981 | || (cp = wb->w_words[0]) == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3982 | cp = ""; |
| 3983 | DELETE(wb); |
| 3984 | } else |
| 3985 | cp = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3986 | return (cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3989 | static int expand(char *cp, REGISTER struct wdblock **wbp, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3990 | { |
| 3991 | jmp_buf ev; |
| 3992 | |
| 3993 | #if __GNUC__ |
| 3994 | /* Avoid longjmp clobbering */ |
| 3995 | (void) &cp; |
| 3996 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3997 | |
| 3998 | DBGPRINTF3(("EXPAND: enter, f=%d\n", f)); |
| 3999 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4000 | gflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4001 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4002 | if (cp == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4003 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4004 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4005 | if (!anys("$`'\"", cp) && |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4006 | !anys(ifs->value, cp) && ((f & DOGLOB) == 0 || !anys("[*?", cp))) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4007 | cp = strsave(cp, areanum); |
| 4008 | if (f & DOTRIM) |
| 4009 | unquote(cp); |
| 4010 | *wbp = addword(cp, *wbp); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4011 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4012 | } |
| 4013 | if (newenv(setjmp(errpt = ev)) == 0) { |
| 4014 | PUSHIO(aword, cp, strchar); |
| 4015 | e.iobase = e.iop; |
| 4016 | while ((cp = blank(f)) && gflg == 0) { |
| 4017 | e.linep = cp; |
| 4018 | cp = strsave(cp, areanum); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4019 | if ((f & DOGLOB) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4020 | if (f & DOTRIM) |
| 4021 | unquote(cp); |
| 4022 | *wbp = addword(cp, *wbp); |
| 4023 | } else |
| 4024 | *wbp = glob(cp, *wbp); |
| 4025 | } |
| 4026 | quitenv(); |
| 4027 | } else |
| 4028 | gflg = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4029 | return (gflg == 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
| 4032 | /* |
| 4033 | * Blank interpretation and quoting |
| 4034 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4035 | static char *blank(f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4036 | int f; |
| 4037 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4038 | REGISTER int c, c1; |
| 4039 | REGISTER char *sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4040 | int scanequals, foundequals; |
| 4041 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4042 | DBGPRINTF3(("BLANK: enter, f=%d\n", f)); |
| 4043 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4044 | sp = e.linep; |
| 4045 | scanequals = f & DOKEY; |
| 4046 | foundequals = 0; |
| 4047 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4048 | loop: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4049 | switch (c = subgetc('"', foundequals)) { |
| 4050 | case 0: |
| 4051 | if (sp == e.linep) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4052 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4053 | *e.linep++ = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4054 | return (sp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4055 | |
| 4056 | default: |
| 4057 | if (f & DOBLANK && any(c, ifs->value)) |
| 4058 | goto loop; |
| 4059 | break; |
| 4060 | |
| 4061 | case '"': |
| 4062 | case '\'': |
| 4063 | scanequals = 0; |
| 4064 | if (INSUB()) |
| 4065 | break; |
| 4066 | for (c1 = c; (c = subgetc(c1, 1)) != c1;) { |
| 4067 | if (c == 0) |
| 4068 | break; |
| 4069 | if (c == '\'' || !any(c, "$`\"")) |
| 4070 | c |= QUOTE; |
| 4071 | *e.linep++ = c; |
| 4072 | } |
| 4073 | c = 0; |
| 4074 | } |
| 4075 | unget(c); |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 4076 | if (!isalpha(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4077 | scanequals = 0; |
| 4078 | for (;;) { |
| 4079 | c = subgetc('"', foundequals); |
| 4080 | if (c == 0 || |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4081 | f & (DOBLANK && any(c, ifs->value)) || |
| 4082 | (!INSUB() && any(c, "\"'"))) { |
| 4083 | scanequals = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4084 | unget(c); |
| 4085 | if (any(c, "\"'")) |
| 4086 | goto loop; |
| 4087 | break; |
| 4088 | } |
| 4089 | if (scanequals) { |
| 4090 | if (c == '=') { |
| 4091 | foundequals = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4092 | scanequals = 0; |
| 4093 | } else if (!isalnum(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4094 | scanequals = 0; |
| 4095 | } |
| 4096 | *e.linep++ = c; |
| 4097 | } |
| 4098 | *e.linep++ = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4099 | return (sp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | /* |
| 4103 | * Get characters, substituting for ` and $ |
| 4104 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4105 | static int subgetc(ec, quoted) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4106 | REGISTER char ec; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4107 | int quoted; |
| 4108 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4109 | REGISTER char c; |
| 4110 | |
| 4111 | DBGPRINTF3(("SUBGETC: enter, quoted=%d\n", quoted)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4112 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4113 | again: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4114 | c = my_getc(ec); |
| 4115 | if (!INSUB() && ec != '\'') { |
| 4116 | if (c == '`') { |
| 4117 | if (grave(quoted) == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4118 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4119 | e.iop->task = XGRAVE; |
| 4120 | goto again; |
| 4121 | } |
| 4122 | if (c == '$' && (c = dollar(quoted)) == 0) { |
| 4123 | e.iop->task = XDOLL; |
| 4124 | goto again; |
| 4125 | } |
| 4126 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4127 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
| 4130 | /* |
| 4131 | * Prepare to generate the string returned by ${} substitution. |
| 4132 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4133 | static int dollar(quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4134 | int quoted; |
| 4135 | { |
| 4136 | int otask; |
| 4137 | struct io *oiop; |
| 4138 | char *dolp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4139 | REGISTER char *s, c, *cp = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4140 | struct var *vp; |
| 4141 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4142 | DBGPRINTF3(("DOLLAR: enter, quoted=%d\n", quoted)); |
| 4143 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4144 | c = readc(); |
| 4145 | s = e.linep; |
| 4146 | if (c != '{') { |
| 4147 | *e.linep++ = c; |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 4148 | if (isalpha(c) || c == '_') { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4149 | while ((c = readc()) != 0 && (isalnum(c) || c == '_')) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4150 | if (e.linep < elinep) |
| 4151 | *e.linep++ = c; |
| 4152 | unget(c); |
| 4153 | } |
| 4154 | c = 0; |
| 4155 | } else { |
| 4156 | oiop = e.iop; |
| 4157 | otask = e.iop->task; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4158 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4159 | e.iop->task = XOTHER; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4160 | while ((c = subgetc('"', 0)) != 0 && c != '}' && c != '\n') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4161 | if (e.linep < elinep) |
| 4162 | *e.linep++ = c; |
| 4163 | if (oiop == e.iop) |
| 4164 | e.iop->task = otask; |
| 4165 | if (c != '}') { |
| 4166 | err("unclosed ${"); |
| 4167 | gflg++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4168 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4169 | } |
| 4170 | } |
| 4171 | if (e.linep >= elinep) { |
| 4172 | err("string in ${} too long"); |
| 4173 | gflg++; |
| 4174 | e.linep -= 10; |
| 4175 | } |
| 4176 | *e.linep = 0; |
| 4177 | if (*s) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4178 | for (cp = s + 1; *cp; cp++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4179 | if (any(*cp, "=-+?")) { |
| 4180 | c = *cp; |
| 4181 | *cp++ = 0; |
| 4182 | break; |
| 4183 | } |
| 4184 | if (s[1] == 0 && (*s == '*' || *s == '@')) { |
| 4185 | if (dolc > 1) { |
| 4186 | /* currently this does not distinguish $* and $@ */ |
| 4187 | /* should check dollar */ |
| 4188 | e.linep = s; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4189 | PUSHIO(awordlist, dolv + 1, dolchar); |
| 4190 | return (0); |
| 4191 | } else { /* trap the nasty ${=} */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4192 | s[0] = '1'; |
| 4193 | s[1] = 0; |
| 4194 | } |
| 4195 | } |
| 4196 | vp = lookup(s); |
| 4197 | if ((dolp = vp->value) == null) { |
| 4198 | switch (c) { |
| 4199 | case '=': |
| 4200 | if (isdigit(*s)) { |
| 4201 | err("cannot use ${...=...} with $n"); |
| 4202 | gflg++; |
| 4203 | break; |
| 4204 | } |
| 4205 | setval(vp, cp); |
| 4206 | dolp = vp->value; |
| 4207 | break; |
| 4208 | |
| 4209 | case '-': |
| 4210 | dolp = strsave(cp, areanum); |
| 4211 | break; |
| 4212 | |
| 4213 | case '?': |
| 4214 | if (*cp == 0) { |
| 4215 | prs("missing value for "); |
| 4216 | err(s); |
| 4217 | } else |
| 4218 | err(cp); |
| 4219 | gflg++; |
| 4220 | break; |
| 4221 | } |
| 4222 | } else if (c == '+') |
| 4223 | dolp = strsave(cp, areanum); |
| 4224 | if (flag['u'] && dolp == null) { |
| 4225 | prs("unset variable: "); |
| 4226 | err(s); |
| 4227 | gflg++; |
| 4228 | } |
| 4229 | e.linep = s; |
| 4230 | PUSHIO(aword, dolp, quoted ? qstrchar : strchar); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4231 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
| 4234 | /* |
| 4235 | * Run the command in `...` and read its output. |
| 4236 | */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4237 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4238 | static int grave(quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4239 | int quoted; |
| 4240 | { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4241 | char *cp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4242 | REGISTER int i; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4243 | int j; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4244 | int pf[2]; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4245 | static char child_cmd[LINELIM]; |
| 4246 | char *src; |
| 4247 | char *dest; |
| 4248 | int count; |
| 4249 | int ignore; |
| 4250 | int ignore_once; |
| 4251 | char *argument_list[4]; |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4252 | struct wdblock *wb = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4253 | |
| 4254 | #if __GNUC__ |
| 4255 | /* Avoid longjmp clobbering */ |
| 4256 | (void) &cp; |
| 4257 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4258 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4259 | for (cp = e.iop->argp->aword; *cp != '`'; cp++) |
| 4260 | if (*cp == 0) { |
| 4261 | err("no closing `"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4262 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4263 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4264 | |
| 4265 | /* string copy with dollar expansion */ |
| 4266 | src = e.iop->argp->aword; |
| 4267 | dest = child_cmd; |
| 4268 | count = 0; |
| 4269 | ignore = 0; |
| 4270 | ignore_once = 0; |
| 4271 | while ((*src != '`') && (count < LINELIM)) { |
| 4272 | if (*src == '\'') |
| 4273 | ignore = !ignore; |
| 4274 | if (*src == '\\') |
| 4275 | ignore_once = 1; |
| 4276 | if (*src == '$' && !ignore && !ignore_once) { |
| 4277 | struct var *vp; |
| 4278 | char var_name[LINELIM]; |
| 4279 | char alt_value[LINELIM]; |
| 4280 | int var_index = 0; |
| 4281 | int alt_index = 0; |
| 4282 | char operator = 0; |
| 4283 | int braces = 0; |
| 4284 | char *value; |
| 4285 | |
| 4286 | src++; |
| 4287 | if (*src == '{') { |
| 4288 | braces = 1; |
| 4289 | src++; |
| 4290 | } |
| 4291 | |
| 4292 | var_name[var_index++] = *src++; |
| 4293 | while (isalnum(*src)) |
| 4294 | var_name[var_index++] = *src++; |
| 4295 | var_name[var_index] = 0; |
| 4296 | |
| 4297 | if (braces) { |
| 4298 | switch (*src) { |
| 4299 | case '}': |
| 4300 | break; |
| 4301 | case '-': |
| 4302 | case '=': |
| 4303 | case '+': |
| 4304 | case '?': |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4305 | operator = * src; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4306 | break; |
| 4307 | default: |
| 4308 | err("unclosed ${\n"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4309 | return (0); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4310 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4311 | if (operator) { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4312 | src++; |
| 4313 | while (*src && (*src != '}')) { |
| 4314 | alt_value[alt_index++] = *src++; |
| 4315 | } |
| 4316 | alt_value[alt_index] = 0; |
| 4317 | if (*src != '}') { |
| 4318 | err("unclosed ${\n"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4319 | return (0); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4320 | } |
| 4321 | } |
| 4322 | src++; |
| 4323 | } |
| 4324 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4325 | if (isalpha(*var_name)) { |
| 4326 | /* let subshell handle it instead */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4327 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4328 | char *namep = var_name; |
| 4329 | |
| 4330 | *dest++ = '$'; |
| 4331 | if (braces) |
| 4332 | *dest++ = '{'; |
| 4333 | while (*namep) |
| 4334 | *dest++ = *namep++; |
| 4335 | if (operator) { |
| 4336 | char *altp = alt_value; |
| 4337 | *dest++ = operator; |
| 4338 | while (*altp) |
| 4339 | *dest++ = *altp++; |
| 4340 | } |
| 4341 | if (braces) |
| 4342 | *dest++ = '}'; |
| 4343 | |
| 4344 | wb = addword(lookup(var_name)->name, wb); |
| 4345 | } else { |
| 4346 | /* expand */ |
| 4347 | |
| 4348 | vp = lookup(var_name); |
| 4349 | if (vp->value != null) |
| 4350 | value = (operator == '+') ? |
| 4351 | alt_value : vp->value; |
| 4352 | else if (operator == '?') { |
| 4353 | err(alt_value); |
| 4354 | return (0); |
| 4355 | } else if (alt_index && (operator != '+')) { |
| 4356 | value = alt_value; |
| 4357 | if (operator == '=') |
| 4358 | setval(vp, value); |
| 4359 | } else |
| 4360 | continue; |
| 4361 | |
| 4362 | while (*value && (count < LINELIM)) { |
| 4363 | *dest++ = *value++; |
| 4364 | count++; |
| 4365 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4366 | } |
| 4367 | } else { |
| 4368 | *dest++ = *src++; |
| 4369 | count++; |
| 4370 | ignore_once = 0; |
| 4371 | } |
| 4372 | } |
| 4373 | *dest = '\0'; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4374 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4375 | if (openpipe(pf) < 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4376 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4377 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4378 | while ((i = vfork()) == -1 && errno == EAGAIN); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4379 | |
| 4380 | DBGPRINTF3(("GRAVE: i is %d\n", io)); |
| 4381 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4382 | if (i < 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4383 | closepipe(pf); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4384 | err((char *) bb_msg_memory_exhausted); |
| 4385 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4386 | } |
| 4387 | if (i != 0) { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4388 | waitpid(i, NULL, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4389 | e.iop->argp->aword = ++cp; |
| 4390 | close(pf[1]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4391 | PUSHIO(afile, remap(pf[0]), |
| 4392 | (int (*)(struct ioarg *)) ((quoted) ? qgravechar : |
| 4393 | gravechar)); |
| 4394 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4395 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4396 | /* allow trapped signals */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4397 | /* XXX - Maybe this signal stuff should go as well? */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4398 | for (j = 0; j <= _NSIG; j++) |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4399 | if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN) |
| 4400 | signal(j, SIG_DFL); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4401 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4402 | dup2(pf[1], 1); |
| 4403 | closepipe(pf); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4404 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4405 | argument_list[0] = (char *) DEFAULT_SHELL; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4406 | argument_list[1] = "-c"; |
| 4407 | argument_list[2] = child_cmd; |
| 4408 | argument_list[3] = 0; |
| 4409 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4410 | cp = rexecve(argument_list[0], argument_list, makenv(1, wb)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4411 | prs(argument_list[0]); |
| 4412 | prs(": "); |
| 4413 | err(cp); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4414 | _exit(1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4415 | } |
| 4416 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4417 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4418 | static char *unquote(as) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4419 | REGISTER char *as; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4420 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4421 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4422 | |
| 4423 | if ((s = as) != NULL) |
| 4424 | while (*s) |
| 4425 | *s++ &= ~QUOTE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4426 | return (as); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | /* -------- glob.c -------- */ |
| 4430 | |
| 4431 | /* |
| 4432 | * glob |
| 4433 | */ |
| 4434 | |
| 4435 | #define scopy(x) strsave((x), areanum) |
| 4436 | #define BLKSIZ 512 |
| 4437 | #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent)) |
| 4438 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4439 | static struct wdblock *cl, *nl; |
| 4440 | static char spcl[] = "[?*"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4441 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4442 | static struct wdblock *glob(cp, wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4443 | char *cp; |
| 4444 | struct wdblock *wb; |
| 4445 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4446 | REGISTER int i; |
| 4447 | REGISTER char *pp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4448 | |
| 4449 | if (cp == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4450 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4451 | i = 0; |
| 4452 | for (pp = cp; *pp; pp++) |
| 4453 | if (any(*pp, spcl)) |
| 4454 | i++; |
| 4455 | else if (!any(*pp & ~QUOTE, spcl)) |
| 4456 | *pp &= ~QUOTE; |
| 4457 | if (i != 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4458 | for (cl = addword(scopy(cp), (struct wdblock *) 0); anyspcl(cl); |
| 4459 | cl = nl) { |
| 4460 | nl = newword(cl->w_nword * 2); |
| 4461 | for (i = 0; i < cl->w_nword; i++) { /* for each argument */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4462 | for (pp = cl->w_words[i]; *pp; pp++) |
| 4463 | if (any(*pp, spcl)) { |
| 4464 | globname(cl->w_words[i], pp); |
| 4465 | break; |
| 4466 | } |
| 4467 | if (*pp == '\0') |
| 4468 | nl = addword(scopy(cl->w_words[i]), nl); |
| 4469 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4470 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4471 | DELETE(cl->w_words[i]); |
| 4472 | DELETE(cl); |
| 4473 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4474 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4475 | unquote(cl->w_words[i]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4476 | glob0((char *) cl->w_words, cl->w_nword, sizeof(char *), xstrcmp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4477 | if (cl->w_nword) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4478 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4479 | wb = addword(cl->w_words[i], wb); |
| 4480 | DELETE(cl); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4481 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4482 | } |
| 4483 | } |
| 4484 | wb = addword(unquote(cp), wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4485 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4488 | static void globname(we, pp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4489 | char *we; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4490 | REGISTER char *pp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4491 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4492 | REGISTER char *np, *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4493 | char *name, *gp, *dp; |
| 4494 | int k; |
| 4495 | DIR *dirp; |
| 4496 | struct dirent *de; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4497 | char dname[NAME_MAX + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4498 | struct stat dbuf; |
| 4499 | |
| 4500 | for (np = we; np != pp; pp--) |
| 4501 | if (pp[-1] == '/') |
| 4502 | break; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4503 | for (dp = cp = space((int) (pp - np) + 3); np < pp;) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4504 | *cp++ = *np++; |
| 4505 | *cp++ = '.'; |
| 4506 | *cp = '\0'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4507 | for (gp = cp = space(strlen(pp) + 1); *np && *np != '/';) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4508 | *cp++ = *np++; |
| 4509 | *cp = '\0'; |
| 4510 | dirp = opendir(dp); |
| 4511 | if (dirp == 0) { |
| 4512 | DELETE(dp); |
| 4513 | DELETE(gp); |
| 4514 | return; |
| 4515 | } |
| 4516 | dname[NAME_MAX] = '\0'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4517 | while ((de = readdir(dirp)) != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4518 | /* XXX Hmmm... What this could be? (abial) */ |
| 4519 | /* |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4520 | if (ent[j].d_ino == 0) |
| 4521 | continue; |
| 4522 | */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4523 | strncpy(dname, de->d_name, NAME_MAX); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4524 | if (dname[0] == '.') |
| 4525 | if (*gp != '.') |
| 4526 | continue; |
| 4527 | for (k = 0; k < NAME_MAX; k++) |
| 4528 | if (any(dname[k], spcl)) |
| 4529 | dname[k] |= QUOTE; |
| 4530 | if (gmatch(dname, gp)) { |
| 4531 | name = generate(we, pp, dname, np); |
| 4532 | if (*np && !anys(np, spcl)) { |
| 4533 | if (stat(name, &dbuf)) { |
| 4534 | DELETE(name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4535 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4536 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4537 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4538 | nl = addword(name, nl); |
| 4539 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4540 | } |
| 4541 | closedir(dirp); |
| 4542 | DELETE(dp); |
| 4543 | DELETE(gp); |
| 4544 | } |
| 4545 | |
| 4546 | /* |
| 4547 | * generate a pathname as below. |
| 4548 | * start..end1 / middle end |
| 4549 | * the slashes come for free |
| 4550 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4551 | static char *generate(start1, end1, middle, end) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4552 | char *start1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4553 | REGISTER char *end1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4554 | char *middle, *end; |
| 4555 | { |
| 4556 | char *p; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4557 | REGISTER char *op, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4558 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4559 | p = op = |
| 4560 | space((int) (end1 - start1) + strlen(middle) + strlen(end) + 2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4561 | for (xp = start1; xp != end1;) |
| 4562 | *op++ = *xp++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4563 | for (xp = middle; (*op++ = *xp++) != '\0';); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4564 | op--; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4565 | for (xp = end; (*op++ = *xp++) != '\0';); |
| 4566 | return (p); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4569 | static int anyspcl(wb) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4570 | REGISTER struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4571 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4572 | REGISTER int i; |
| 4573 | REGISTER char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4574 | |
| 4575 | wd = wb->w_words; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4576 | for (i = 0; i < wb->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4577 | if (anys(spcl, *wd++)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4578 | return (1); |
| 4579 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4582 | static int xstrcmp(p1, p2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4583 | char *p1, *p2; |
| 4584 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4585 | return (strcmp(*(char **) p1, *(char **) p2)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | /* -------- word.c -------- */ |
| 4589 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4590 | static struct wdblock *newword(nw) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4591 | REGISTER int nw; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4592 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4593 | REGISTER struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4594 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4595 | wb = (struct wdblock *) space(sizeof(*wb) + nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4596 | wb->w_bsize = nw; |
| 4597 | wb->w_nword = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4598 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4601 | static struct wdblock *addword(wd, wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4602 | char *wd; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4603 | REGISTER struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4604 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4605 | REGISTER struct wdblock *wb2; |
| 4606 | REGISTER int nw; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4607 | |
| 4608 | if (wb == NULL) |
| 4609 | wb = newword(NSTART); |
| 4610 | if ((nw = wb->w_nword) >= wb->w_bsize) { |
| 4611 | wb2 = newword(nw * 2); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4612 | memcpy((char *) wb2->w_words, (char *) wb->w_words, |
| 4613 | nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4614 | wb2->w_nword = nw; |
| 4615 | DELETE(wb); |
| 4616 | wb = wb2; |
| 4617 | } |
| 4618 | wb->w_words[wb->w_nword++] = wd; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4619 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4620 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4621 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4622 | static |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4623 | char **getwords(wb) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4624 | REGISTER struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4625 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4626 | REGISTER char **wd; |
| 4627 | REGISTER int nb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4628 | |
| 4629 | if (wb == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4630 | return ((char **) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4631 | if (wb->w_nword == 0) { |
| 4632 | DELETE(wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4633 | return ((char **) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4634 | } |
| 4635 | wd = (char **) space(nb = sizeof(*wd) * wb->w_nword); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4636 | memcpy((char *) wd, (char *) wb->w_words, nb); |
| 4637 | DELETE(wb); /* perhaps should done by caller */ |
| 4638 | return (wd); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4641 | int (*func) (char *, char *); |
| 4642 | int globv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4643 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4644 | static void glob0(a0, a1, a2, a3) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4645 | char *a0; |
| 4646 | unsigned a1; |
| 4647 | int a2; |
| 4648 | int (*a3) (char *, char *); |
| 4649 | { |
| 4650 | func = a3; |
| 4651 | globv = a2; |
| 4652 | glob1(a0, a0 + a1 * a2); |
| 4653 | } |
| 4654 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4655 | static void glob1(base, lim) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4656 | char *base, *lim; |
| 4657 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4658 | REGISTER char *i, *j; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4659 | int v2; |
| 4660 | char *lptr, *hptr; |
| 4661 | int c; |
| 4662 | unsigned n; |
| 4663 | |
| 4664 | |
| 4665 | v2 = globv; |
| 4666 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4667 | top: |
| 4668 | if ((n = (int) (lim - base)) <= v2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4669 | return; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4670 | n = v2 * (n / (2 * v2)); |
| 4671 | hptr = lptr = base + n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4672 | i = base; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4673 | j = lim - v2; |
| 4674 | for (;;) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4675 | if (i < lptr) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4676 | if ((c = (*func) (i, lptr)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4677 | glob2(i, lptr -= v2); |
| 4678 | continue; |
| 4679 | } |
| 4680 | if (c < 0) { |
| 4681 | i += v2; |
| 4682 | continue; |
| 4683 | } |
| 4684 | } |
| 4685 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4686 | begin: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4687 | if (j > hptr) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4688 | if ((c = (*func) (hptr, j)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4689 | glob2(hptr += v2, j); |
| 4690 | goto begin; |
| 4691 | } |
| 4692 | if (c > 0) { |
| 4693 | if (i == lptr) { |
| 4694 | glob3(i, hptr += v2, j); |
| 4695 | i = lptr += v2; |
| 4696 | goto begin; |
| 4697 | } |
| 4698 | glob2(i, j); |
| 4699 | j -= v2; |
| 4700 | i += v2; |
| 4701 | continue; |
| 4702 | } |
| 4703 | j -= v2; |
| 4704 | goto begin; |
| 4705 | } |
| 4706 | |
| 4707 | |
| 4708 | if (i == lptr) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4709 | if (lptr - base >= lim - hptr) { |
| 4710 | glob1(hptr + v2, lim); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4711 | lim = lptr; |
| 4712 | } else { |
| 4713 | glob1(base, lptr); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4714 | base = hptr + v2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4715 | } |
| 4716 | goto top; |
| 4717 | } |
| 4718 | |
| 4719 | |
| 4720 | glob3(j, lptr -= v2, i); |
| 4721 | j = hptr -= v2; |
| 4722 | } |
| 4723 | } |
| 4724 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4725 | static void glob2(i, j) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4726 | char *i, *j; |
| 4727 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4728 | REGISTER char *index1, *index2, c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4729 | int m; |
| 4730 | |
| 4731 | m = globv; |
| 4732 | index1 = i; |
| 4733 | index2 = j; |
| 4734 | do { |
| 4735 | c = *index1; |
| 4736 | *index1++ = *index2; |
| 4737 | *index2++ = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4738 | } while (--m); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4741 | static void glob3(i, j, k) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4742 | char *i, *j, *k; |
| 4743 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4744 | REGISTER char *index1, *index2, *index3; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4745 | int c; |
| 4746 | int m; |
| 4747 | |
| 4748 | m = globv; |
| 4749 | index1 = i; |
| 4750 | index2 = j; |
| 4751 | index3 = k; |
| 4752 | do { |
| 4753 | c = *index1; |
| 4754 | *index1++ = *index3; |
| 4755 | *index3++ = *index2; |
| 4756 | *index2++ = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4757 | } while (--m); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | /* -------- io.c -------- */ |
| 4761 | |
| 4762 | /* |
| 4763 | * shell IO |
| 4764 | */ |
| 4765 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4766 | static int my_getc(int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4767 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4768 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4769 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4770 | if (e.linep > elinep) { |
| 4771 | while ((c = readc()) != '\n' && c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4772 | err("input line too long"); |
| 4773 | gflg++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4774 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4775 | } |
| 4776 | c = readc(); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4777 | if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4778 | if (c == '\\') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4779 | c = readc(); |
| 4780 | if (c == '\n' && ec != '\"') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4781 | return (my_getc(ec)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4782 | c |= QUOTE; |
| 4783 | } |
| 4784 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4785 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4788 | static void unget(c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4789 | int c; |
| 4790 | { |
| 4791 | if (e.iop >= e.iobase) |
| 4792 | e.iop->peekc = c; |
| 4793 | } |
| 4794 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4795 | static int eofc() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4796 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4797 | 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] | 4798 | } |
| 4799 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4800 | static int readc() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4801 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4802 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4803 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4804 | RCPRINTF(("READC: e.iop 0x%x, e.iobase 0x%x\n", e.iop, e.iobase)); |
| 4805 | |
| 4806 | for (; e.iop >= e.iobase; e.iop--) { |
| 4807 | 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] | 4808 | if ((c = e.iop->peekc) != '\0') { |
| 4809 | e.iop->peekc = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4810 | return (c); |
| 4811 | } else { |
| 4812 | if (e.iop->prev != 0) { |
| 4813 | if ((c = (*e.iop->iofn) (e.iop->argp, e.iop)) != '\0') { |
| 4814 | if (c == -1) { |
| 4815 | e.iop++; |
| 4816 | continue; |
| 4817 | } |
| 4818 | if (e.iop == iostack) |
| 4819 | ioecho(c); |
| 4820 | return (e.iop->prev = c); |
| 4821 | } else if (e.iop->task == XIO && e.iop->prev != '\n') { |
| 4822 | e.iop->prev = 0; |
| 4823 | if (e.iop == iostack) |
| 4824 | ioecho('\n'); |
| 4825 | return '\n'; |
| 4826 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4827 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4828 | if (e.iop->task == XIO) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4829 | if (multiline) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4830 | return e.iop->prev = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4831 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4832 | if (interactive && e.iop == iostack + 1) { |
| 4833 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
| 4834 | current_prompt = prompt->value; |
| 4835 | #else |
| 4836 | prs(prompt->value); |
| 4837 | #endif |
| 4838 | } |
| 4839 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4840 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4841 | |
| 4842 | } /* FOR */ |
| 4843 | |
| 4844 | if (e.iop >= iostack) { |
| 4845 | RCPRINTF(("READC: return 0, e.iop 0x%x\n", e.iop)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4846 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | DBGPRINTF(("READC: leave()...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4850 | leave(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4851 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4852 | /* NOTREACHED */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4853 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4856 | static void ioecho(c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4857 | char c; |
| 4858 | { |
| 4859 | if (flag['v']) |
| 4860 | write(2, &c, sizeof c); |
| 4861 | } |
| 4862 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4863 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4864 | static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4865 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4866 | DBGPRINTF(("PUSHIO: argp 0x%x, argp->afid 0x%x, e.iop 0x%x\n", argp, |
| 4867 | argp->afid, e.iop)); |
| 4868 | |
| 4869 | /* 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] | 4870 | if (++e.iop >= &iostack[NPUSH]) { |
| 4871 | e.iop--; |
| 4872 | err("Shell input nested too deeply"); |
| 4873 | gflg++; |
| 4874 | return; |
| 4875 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4876 | |
| 4877 | /* We did not overflow the NPUSH array spots so setup data structs */ |
| 4878 | |
| 4879 | 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] | 4880 | |
| 4881 | if (argp->afid != AFID_NOBUF) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4882 | e.iop->argp = argp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4883 | else { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4884 | |
| 4885 | e.iop->argp = ioargstack + (e.iop - iostack); /* MAL - index into stack */ |
| 4886 | *e.iop->argp = *argp; /* copy data from temp area into stack spot */ |
| 4887 | |
| 4888 | /* MAL - mainbuf is for 1st data source (command line?) and all nested use a single shared buffer? */ |
| 4889 | |
| 4890 | if (e.iop == &iostack[0]) |
| 4891 | e.iop->argp->afbuf = &mainbuf; |
| 4892 | else |
| 4893 | e.iop->argp->afbuf = &sharedbuf; |
| 4894 | |
| 4895 | /* MAL - if not a termimal AND (commandline OR readable file) then give it a buffer id? */ |
| 4896 | /* This line appears to be active when running scripts from command line */ |
| 4897 | if ((isatty(e.iop->argp->afile) == 0) |
| 4898 | && (e.iop == &iostack[0] |
| 4899 | || lseek(e.iop->argp->afile, 0L, 1) != -1)) { |
| 4900 | if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */ |
| 4901 | bufid = AFID_ID; /* AFID_ID = 0 */ |
| 4902 | |
| 4903 | e.iop->argp->afid = bufid; /* assign buffer id */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4904 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4905 | |
| 4906 | DBGPRINTF(("PUSHIO: iostack 0x%x, e.iop 0x%x, afbuf 0x%x\n", |
| 4907 | iostack, e.iop, e.iop->argp->afbuf)); |
| 4908 | DBGPRINTF(("PUSHIO: mbuf 0x%x, sbuf 0x%x, bid %d, e.iop 0x%x\n", |
| 4909 | &mainbuf, &sharedbuf, bufid, e.iop)); |
| 4910 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4913 | e.iop->prev = ~'\n'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4914 | e.iop->peekc = 0; |
| 4915 | e.iop->xchar = 0; |
| 4916 | e.iop->nlcount = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4917 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4918 | if (fn == filechar || fn == linechar) |
| 4919 | e.iop->task = XIO; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4920 | else if (fn == (int (*)(struct ioarg *)) gravechar |
| 4921 | || fn == (int (*)(struct ioarg *)) qgravechar) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4922 | e.iop->task = XGRAVE; |
| 4923 | else |
| 4924 | e.iop->task = XOTHER; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4925 | |
| 4926 | return; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4929 | static struct io *setbase(ip) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4930 | struct io *ip; |
| 4931 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4932 | REGISTER struct io *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4933 | |
| 4934 | xp = e.iobase; |
| 4935 | e.iobase = ip; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4936 | return (xp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
| 4939 | /* |
| 4940 | * Input generating functions |
| 4941 | */ |
| 4942 | |
| 4943 | /* |
| 4944 | * Produce the characters of a string, then a newline, then EOF. |
| 4945 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4946 | static int nlchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4947 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4948 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4949 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4950 | |
| 4951 | if (ap->aword == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4952 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4953 | if ((c = *ap->aword++) == 0) { |
| 4954 | ap->aword = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4955 | return ('\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4956 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4957 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
| 4960 | /* |
| 4961 | * Given a list of words, produce the characters |
| 4962 | * in them, with a space after each word. |
| 4963 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4964 | static int wdchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4965 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4966 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4967 | REGISTER char c; |
| 4968 | REGISTER char **wl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4969 | |
| 4970 | if ((wl = ap->awordlist) == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4971 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4972 | if (*wl != NULL) { |
| 4973 | if ((c = *(*wl)++) != 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4974 | return (c & 0177); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4975 | ap->awordlist++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4976 | return (' '); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4977 | } |
| 4978 | ap->awordlist = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4979 | return ('\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | /* |
| 4983 | * Return the characters of a list of words, |
| 4984 | * producing a space between them. |
| 4985 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4986 | static int dolchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4987 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4988 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4989 | REGISTER char *wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4990 | |
| 4991 | if ((wp = *ap->awordlist++) != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4992 | PUSHIO(aword, wp, *ap->awordlist == NULL ? strchar : xxchar); |
| 4993 | return (-1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4994 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4995 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4996 | } |
| 4997 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4998 | static int xxchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4999 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5000 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5001 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5002 | |
| 5003 | if (ap->aword == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5004 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5005 | if ((c = *ap->aword++) == '\0') { |
| 5006 | ap->aword = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5007 | return (' '); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5008 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5009 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
| 5012 | /* |
| 5013 | * Produce the characters from a single word (string). |
| 5014 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5015 | static int strchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5016 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5017 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5018 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5019 | |
| 5020 | if (ap->aword == NULL || (c = *ap->aword++) == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5021 | return (0); |
| 5022 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | /* |
| 5026 | * Produce quoted characters from a single word (string). |
| 5027 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5028 | static int qstrchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5029 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5030 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5031 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5032 | |
| 5033 | if (ap->aword == NULL || (c = *ap->aword++) == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5034 | return (0); |
| 5035 | return (c | QUOTE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
| 5038 | /* |
| 5039 | * Return the characters from a file. |
| 5040 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5041 | static int filechar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5042 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5043 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5044 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5045 | char c; |
| 5046 | struct iobuf *bp = ap->afbuf; |
| 5047 | |
| 5048 | if (ap->afid != AFID_NOBUF) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5049 | if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5050 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5051 | if (i) |
| 5052 | lseek(ap->afile, ap->afpos, 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5053 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5054 | i = safe_read(ap->afile, bp->buf, sizeof(bp->buf)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5055 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5056 | if (i <= 0) { |
| 5057 | closef(ap->afile); |
| 5058 | return 0; |
| 5059 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5060 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5061 | bp->id = ap->afid; |
| 5062 | bp->ebufp = (bp->bufp = bp->buf) + i; |
| 5063 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5064 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5065 | ap->afpos++; |
| 5066 | return *bp->bufp++ & 0177; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5067 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5068 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 5069 | if (interactive && isatty(ap->afile)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5070 | static char mycommand[BUFSIZ]; |
| 5071 | static int position = 0, size = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5072 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5073 | while (size == 0 || position >= size) { |
| 5074 | cmdedit_read_input(current_prompt, mycommand); |
| 5075 | size = strlen(mycommand); |
| 5076 | position = 0; |
| 5077 | } |
| 5078 | c = mycommand[position]; |
| 5079 | position++; |
| 5080 | return (c); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5081 | } else |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5082 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5083 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5084 | { |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 5085 | i = safe_read(ap->afile, &c, sizeof(c)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5086 | return (i == sizeof(c) ? (c & 0x7f) : (closef(ap->afile), 0)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5087 | } |
| 5088 | } |
| 5089 | |
| 5090 | /* |
| 5091 | * Return the characters from a here temp file. |
| 5092 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5093 | static int herechar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5094 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5095 | { |
| 5096 | char c; |
| 5097 | |
| 5098 | |
| 5099 | if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) { |
| 5100 | close(ap->afile); |
| 5101 | c = 0; |
| 5102 | } |
| 5103 | return (c); |
| 5104 | |
| 5105 | } |
| 5106 | |
| 5107 | /* |
| 5108 | * Return the characters produced by a process (`...`). |
| 5109 | * Quote them if required, and remove any trailing newline characters. |
| 5110 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5111 | static int gravechar(ap, iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5112 | struct ioarg *ap; |
| 5113 | struct io *iop; |
| 5114 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5115 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5116 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5117 | if ((c = qgravechar(ap, iop) & ~QUOTE) == '\n') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5118 | c = ' '; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5119 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5122 | static int qgravechar(ap, iop) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5123 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5124 | struct io *iop; |
| 5125 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5126 | REGISTER int c; |
| 5127 | |
| 5128 | DBGPRINTF3(("QGRAVECHAR: enter, ap=0x%x, iop=0x%x\n", ap, iop)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5129 | |
| 5130 | if (iop->xchar) { |
| 5131 | if (iop->nlcount) { |
| 5132 | iop->nlcount--; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5133 | return ('\n' | QUOTE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5134 | } |
| 5135 | c = iop->xchar; |
| 5136 | iop->xchar = 0; |
| 5137 | } else if ((c = filechar(ap)) == '\n') { |
| 5138 | iop->nlcount = 1; |
| 5139 | while ((c = filechar(ap)) == '\n') |
| 5140 | iop->nlcount++; |
| 5141 | iop->xchar = c; |
| 5142 | if (c == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5143 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5144 | iop->nlcount--; |
| 5145 | c = '\n'; |
| 5146 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5147 | return (c != 0 ? c | QUOTE : 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
| 5150 | /* |
| 5151 | * Return a single command (usually the first line) from a file. |
| 5152 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5153 | static int linechar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5154 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5155 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5156 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5157 | |
| 5158 | if ((c = filechar(ap)) == '\n') { |
| 5159 | if (!multiline) { |
| 5160 | closef(ap->afile); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5161 | ap->afile = -1; /* illegal value */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5162 | } |
| 5163 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5164 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5167 | static void prs(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5168 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5169 | { |
| 5170 | if (*s) |
| 5171 | write(2, s, strlen(s)); |
| 5172 | } |
| 5173 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5174 | static void prn(u) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5175 | unsigned u; |
| 5176 | { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 5177 | prs(itoa(u)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5178 | } |
| 5179 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5180 | static void closef(i) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5181 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5182 | { |
| 5183 | if (i > 2) |
| 5184 | close(i); |
| 5185 | } |
| 5186 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5187 | static void closeall() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5188 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5189 | REGISTER int u; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5190 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5191 | for (u = NUFILE; u < NOFILE;) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5192 | close(u++); |
| 5193 | } |
| 5194 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5195 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5196 | /* |
| 5197 | * remap fd into Shell's fd space |
| 5198 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5199 | static int remap(fd) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5200 | REGISTER int fd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5201 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5202 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5203 | int map[NOFILE]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5204 | int newfd; |
| 5205 | |
| 5206 | |
| 5207 | DBGPRINTF(("REMAP: fd=%d, e.iofd=%d\n", fd, e.iofd)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5208 | |
| 5209 | if (fd < e.iofd) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5210 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5211 | map[i] = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5212 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5213 | do { |
| 5214 | map[fd] = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5215 | newfd = dup(fd); |
| 5216 | fd = newfd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5217 | } while (fd >= 0 && fd < e.iofd); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5218 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5219 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5220 | if (map[i]) |
| 5221 | close(i); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5222 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5223 | if (fd < 0) |
| 5224 | err("too many files open in shell"); |
| 5225 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5226 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5227 | return (fd); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5228 | } |
| 5229 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5230 | static int openpipe(pv) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5231 | REGISTER int *pv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5232 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5233 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5234 | |
| 5235 | if ((i = pipe(pv)) < 0) |
| 5236 | err("can't create pipe - try again"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5237 | return (i); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5238 | } |
| 5239 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5240 | static void closepipe(pv) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5241 | REGISTER int *pv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5242 | { |
| 5243 | if (pv != NULL) { |
| 5244 | close(*pv++); |
| 5245 | close(*pv); |
| 5246 | } |
| 5247 | } |
| 5248 | |
| 5249 | /* -------- here.c -------- */ |
| 5250 | |
| 5251 | /* |
| 5252 | * here documents |
| 5253 | */ |
| 5254 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5255 | static void markhere(s, iop) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5256 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5257 | struct ioword *iop; |
| 5258 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5259 | REGISTER struct here *h, *lh; |
| 5260 | |
| 5261 | DBGPRINTF7(("MARKHERE: enter, s=0x%x\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5262 | |
| 5263 | h = (struct here *) space(sizeof(struct here)); |
| 5264 | if (h == 0) |
| 5265 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5266 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5267 | h->h_tag = evalstr(s, DOSUB); |
| 5268 | if (h->h_tag == 0) |
| 5269 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5270 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5271 | h->h_iop = iop; |
| 5272 | iop->io_name = 0; |
| 5273 | h->h_next = NULL; |
| 5274 | if (inhere == 0) |
| 5275 | inhere = h; |
| 5276 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5277 | for (lh = inhere; lh != NULL; lh = lh->h_next) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5278 | if (lh->h_next == 0) { |
| 5279 | lh->h_next = h; |
| 5280 | break; |
| 5281 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5282 | iop->io_flag |= IOHERE | IOXHERE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5283 | for (s = h->h_tag; *s; s++) |
| 5284 | if (*s & QUOTE) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5285 | iop->io_flag &= ~IOXHERE; |
| 5286 | *s &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5287 | } |
| 5288 | h->h_dosub = iop->io_flag & IOXHERE; |
| 5289 | } |
| 5290 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5291 | static void gethere() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5292 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5293 | REGISTER struct here *h, *hp; |
| 5294 | |
| 5295 | DBGPRINTF7(("GETHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5296 | |
| 5297 | /* Scan here files first leaving inhere list in place */ |
| 5298 | for (hp = h = inhere; h != NULL; hp = h, h = h->h_next) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5299 | 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] | 5300 | |
| 5301 | /* Make inhere list active - keep list intact for scraphere */ |
| 5302 | if (hp != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5303 | hp->h_next = acthere; |
| 5304 | acthere = inhere; |
| 5305 | inhere = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5306 | } |
| 5307 | } |
| 5308 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5309 | static void readhere(name, s, ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5310 | char **name; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5311 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5312 | int ec; |
| 5313 | { |
| 5314 | int tf; |
| 5315 | char tname[30] = ".msh_XXXXXX"; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5316 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5317 | jmp_buf ev; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5318 | char myline[LINELIM + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5319 | char *thenext; |
| 5320 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5321 | DBGPRINTF7(("READHERE: enter, name=0x%x, s=0x%x\n", name, s)); |
| 5322 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5323 | tf = mkstemp(tname); |
| 5324 | if (tf < 0) |
| 5325 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5326 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5327 | *name = strsave(tname, areanum); |
| 5328 | if (newenv(setjmp(errpt = ev)) != 0) |
| 5329 | unlink(tname); |
| 5330 | else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5331 | pushio(e.iop->argp, (int (*)(struct ioarg *)) e.iop->iofn); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5332 | e.iobase = e.iop; |
| 5333 | for (;;) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5334 | if (interactive && e.iop <= iostack) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5335 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5336 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5337 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5338 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5339 | #endif |
| 5340 | } |
| 5341 | thenext = myline; |
| 5342 | while ((c = my_getc(ec)) != '\n' && c) { |
| 5343 | if (ec == '\'') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5344 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5345 | if (thenext >= &myline[LINELIM]) { |
| 5346 | c = 0; |
| 5347 | break; |
| 5348 | } |
| 5349 | *thenext++ = c; |
| 5350 | } |
| 5351 | *thenext = 0; |
| 5352 | if (strcmp(s, myline) == 0 || c == 0) |
| 5353 | break; |
| 5354 | *thenext++ = '\n'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5355 | write(tf, myline, (int) (thenext - myline)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5356 | } |
| 5357 | if (c == 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5358 | prs("here document `"); |
| 5359 | prs(s); |
| 5360 | err("' unclosed"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5361 | } |
| 5362 | quitenv(); |
| 5363 | } |
| 5364 | close(tf); |
| 5365 | } |
| 5366 | |
| 5367 | /* |
| 5368 | * open here temp file. |
| 5369 | * if unquoted here, expand here temp file into second temp file. |
| 5370 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5371 | static int herein(hname, xdoll) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5372 | char *hname; |
| 5373 | int xdoll; |
| 5374 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5375 | REGISTER int hf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5376 | int tf; |
| 5377 | |
| 5378 | #if __GNUC__ |
| 5379 | /* Avoid longjmp clobbering */ |
| 5380 | (void) &tf; |
| 5381 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5382 | if (hname == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5383 | return (-1); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5384 | |
| 5385 | DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll)); |
| 5386 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5387 | hf = open(hname, 0); |
| 5388 | if (hf < 0) |
| 5389 | return (-1); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5390 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5391 | if (xdoll) { |
| 5392 | char c; |
| 5393 | char tname[30] = ".msh_XXXXXX"; |
| 5394 | jmp_buf ev; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5395 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5396 | tf = mkstemp(tname); |
| 5397 | if (tf < 0) |
| 5398 | return (-1); |
| 5399 | if (newenv(setjmp(errpt = ev)) == 0) { |
| 5400 | PUSHIO(afile, hf, herechar); |
| 5401 | setbase(e.iop); |
| 5402 | while ((c = subgetc(0, 0)) != 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5403 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5404 | write(tf, &c, sizeof c); |
| 5405 | } |
| 5406 | quitenv(); |
| 5407 | } else |
| 5408 | unlink(tname); |
| 5409 | close(tf); |
| 5410 | tf = open(tname, 0); |
| 5411 | unlink(tname); |
| 5412 | return (tf); |
| 5413 | } else |
| 5414 | return (hf); |
| 5415 | } |
| 5416 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5417 | static void scraphere() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5418 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5419 | REGISTER struct here *h; |
| 5420 | |
| 5421 | DBGPRINTF7(("SCRAPHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5422 | |
| 5423 | for (h = inhere; h != NULL; h = h->h_next) { |
| 5424 | if (h->h_iop && h->h_iop->io_name) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5425 | unlink(h->h_iop->io_name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5426 | } |
| 5427 | inhere = NULL; |
| 5428 | } |
| 5429 | |
| 5430 | /* unlink here temp files before a freearea(area) */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5431 | static void freehere(area) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5432 | int area; |
| 5433 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5434 | REGISTER struct here *h, *hl; |
| 5435 | |
| 5436 | DBGPRINTF6(("FREEHERE: enter, area=%d\n", area)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5437 | |
| 5438 | hl = NULL; |
| 5439 | for (h = acthere; h != NULL; h = h->h_next) |
| 5440 | if (getarea((char *) h) >= area) { |
| 5441 | if (h->h_iop->io_name != NULL) |
| 5442 | unlink(h->h_iop->io_name); |
| 5443 | if (hl == NULL) |
| 5444 | acthere = h->h_next; |
| 5445 | else |
| 5446 | hl->h_next = h->h_next; |
| 5447 | } else |
| 5448 | hl = h; |
| 5449 | } |
| 5450 | |
| 5451 | |
| 5452 | |
| 5453 | /* |
| 5454 | * Copyright (c) 1987,1997, Prentice Hall |
| 5455 | * All rights reserved. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5456 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5457 | * Redistribution and use of the MINIX operating system in source and |
| 5458 | * binary forms, with or without modification, are permitted provided |
| 5459 | * that the following conditions are met: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5460 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5461 | * Redistributions of source code must retain the above copyright |
| 5462 | * notice, this list of conditions and the following disclaimer. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5463 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5464 | * Redistributions in binary form must reproduce the above |
| 5465 | * copyright notice, this list of conditions and the following |
| 5466 | * disclaimer in the documentation and/or other materials provided |
| 5467 | * with the distribution. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5468 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5469 | * Neither the name of Prentice Hall nor the names of the software |
| 5470 | * authors or contributors may be used to endorse or promote |
| 5471 | * products derived from this software without specific prior |
| 5472 | * written permission. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5473 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5474 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND |
| 5475 | * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 5476 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 5477 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 5478 | * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE |
| 5479 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 5480 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 5481 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 5482 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 5483 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 5484 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 5485 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 5486 | * |
| 5487 | */ |