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 | |
Mike Frysinger | 1781188 | 2006-05-05 20:33:07 +0000 | [diff] [blame] | 56 | /*#define MSHDEBUG 1*/ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 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 */ |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 165 | #ifdef MSHDEBUG |
| 166 | static char *T_CMD_NAMES[] = { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 167 | "PLACEHOLDER", |
| 168 | "TCOM", |
| 169 | "TPAREN", |
| 170 | "TPIPE", |
| 171 | "TLIST", |
| 172 | "TOR", |
| 173 | "TAND", |
| 174 | "TFOR", |
| 175 | "TDO", |
| 176 | "TCASE", |
| 177 | "TIF", |
| 178 | "TWHILE", |
| 179 | "TUNTIL", |
| 180 | "TELIF", |
| 181 | "TPAT", |
| 182 | "TBRACE", |
| 183 | "TASYNC", |
| 184 | "TDOT", |
| 185 | }; |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 186 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * actions determining the environment of a process |
| 190 | */ |
| 191 | #define BIT(i) (1<<(i)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 192 | #define FEXEC BIT(0) /* execute without forking */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 193 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 194 | #if 0 /* Original value */ |
| 195 | #define AREASIZE (65000) |
| 196 | #else |
| 197 | #define AREASIZE (90000) |
| 198 | #endif |
| 199 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 200 | /* |
| 201 | * flags to control evaluation of words |
| 202 | */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 203 | #define DOSUB 1 /* interpret $, `, and quotes */ |
| 204 | #define DOBLANK 2 /* perform blank interpretation */ |
| 205 | #define DOGLOB 4 /* interpret [?* */ |
| 206 | #define DOKEY 8 /* move words with `=' to 2nd arg. list */ |
| 207 | #define DOTRIM 16 /* trim resulting string */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 208 | |
| 209 | #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM) |
| 210 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 211 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 212 | /* PROTOTYPES */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 213 | static int newfile(char *s); |
| 214 | static char *findeq(char *cp); |
| 215 | static char *cclass(char *p, int sub); |
| 216 | static void initarea(void); |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 217 | extern int msh_main(int argc, char **argv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 218 | |
| 219 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 220 | struct brkcon { |
| 221 | jmp_buf brkpt; |
| 222 | struct brkcon *nextlev; |
| 223 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 224 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 225 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 226 | /* |
| 227 | * redirection |
| 228 | */ |
| 229 | struct ioword { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 230 | short io_unit; /* unit affected */ |
| 231 | short io_flag; /* action (below) */ |
| 232 | char *io_name; /* file name */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 233 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 234 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 235 | #define IOREAD 1 /* < */ |
| 236 | #define IOHERE 2 /* << (here file) */ |
| 237 | #define IOWRITE 4 /* > */ |
| 238 | #define IOCAT 8 /* >> */ |
| 239 | #define IOXHERE 16 /* ${}, ` in << */ |
| 240 | #define IODUP 32 /* >&digit */ |
| 241 | #define IOCLOSE 64 /* >&- */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 242 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 243 | #define IODEFAULT (-1) /* token for default IO unit */ |
| 244 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 245 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 246 | |
| 247 | /* |
| 248 | * parsing & execution environment |
| 249 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 250 | static struct env { |
| 251 | char *linep; |
| 252 | struct io *iobase; |
| 253 | struct io *iop; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 254 | xint *errpt; /* void * */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 255 | int iofd; |
| 256 | struct env *oenv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 257 | } e; |
| 258 | |
| 259 | /* |
| 260 | * flags: |
| 261 | * -e: quit on error |
| 262 | * -k: look for name=value everywhere on command line |
| 263 | * -n: no execution |
| 264 | * -t: exit after reading and executing one command |
| 265 | * -v: echo as read |
| 266 | * -x: trace |
| 267 | * -u: unset variables net diagnostic |
| 268 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 269 | static char *flag; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 270 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 271 | static char *null; /* null value for variable */ |
| 272 | static int intr; /* interrupt pending */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 273 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 274 | static char *trap[_NSIG + 1]; |
| 275 | static char ourtrap[_NSIG + 1]; |
| 276 | static int trapset; /* trap pending */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 277 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 278 | static int heedint; /* heed interrupt signals */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 279 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 280 | static int yynerrs; /* yacc */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 281 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 282 | static char line[LINELIM]; |
| 283 | static char *elinep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 284 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 285 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 286 | /* |
| 287 | * other functions |
| 288 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 289 | static int (*inbuilt(char *s)) (struct op *); |
Eric Andersen | 392947c | 2002-12-11 07:42:46 +0000 | [diff] [blame] | 290 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 291 | static char *rexecve(char *c, char **v, char **envp); |
| 292 | static char *space(int n); |
| 293 | static char *strsave(char *s, int a); |
| 294 | static char *evalstr(char *cp, int f); |
| 295 | static char *putn(int n); |
| 296 | static char *itoa(int n); |
| 297 | static char *unquote(char *as); |
| 298 | static struct var *lookup(char *n); |
| 299 | static int rlookup(char *n); |
| 300 | static struct wdblock *glob(char *cp, struct wdblock *wb); |
| 301 | static int my_getc(int ec); |
| 302 | static int subgetc(int ec, int quoted); |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 303 | static char **makenv(int all, struct wdblock *wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 304 | static char **eval(char **ap, int f); |
| 305 | static int setstatus(int s); |
| 306 | static int waitfor(int lastpid, int canintr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 307 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 308 | static void onintr(int s); /* SIGINT handler */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 309 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 310 | static int newenv(int f); |
| 311 | static void quitenv(void); |
| 312 | static void err(char *s); |
| 313 | static int anys(char *s1, char *s2); |
| 314 | static int any(int c, char *s); |
| 315 | static void next(int f); |
| 316 | static void setdash(void); |
| 317 | static void onecommand(void); |
| 318 | static void runtrap(int i); |
| 319 | static int gmatch(char *s, char *p); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 320 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 321 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 322 | /* |
| 323 | * error handling |
| 324 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 325 | static void leave(void); /* abort shell (or fail in subshell) */ |
| 326 | static void fail(void); /* fail but return to process next command */ |
| 327 | static void warn(char *s); |
| 328 | static void sig(int i); /* default signal handler */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 329 | |
| 330 | |
| 331 | |
| 332 | /* -------- area stuff -------- */ |
| 333 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 334 | #define REGSIZE sizeof(struct region) |
| 335 | #define GROWBY (256) |
| 336 | /* #define SHRINKBY (64) */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 337 | #undef SHRINKBY |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 338 | #define FREE (32767) |
| 339 | #define BUSY (0) |
| 340 | #define ALIGN (sizeof(int)-1) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 341 | |
| 342 | |
| 343 | struct region { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 344 | struct region *next; |
| 345 | int area; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | |
| 349 | |
| 350 | /* -------- grammar stuff -------- */ |
| 351 | typedef union { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 352 | char *cp; |
| 353 | char **wp; |
| 354 | int i; |
| 355 | struct op *o; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 356 | } YYSTYPE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 357 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 358 | #define WORD 256 |
| 359 | #define LOGAND 257 |
| 360 | #define LOGOR 258 |
| 361 | #define BREAK 259 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 362 | #define IF 260 |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 363 | #define THEN 261 |
| 364 | #define ELSE 262 |
| 365 | #define ELIF 263 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 366 | #define FI 264 |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 367 | #define CASE 265 |
| 368 | #define ESAC 266 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 369 | #define FOR 267 |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 370 | #define WHILE 268 |
| 371 | #define UNTIL 269 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 372 | #define DO 270 |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 373 | #define DONE 271 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 374 | #define IN 272 |
| 375 | /* Added for "." file expansion */ |
| 376 | #define DOT 273 |
| 377 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 378 | #define YYERRCODE 300 |
| 379 | |
| 380 | /* flags to yylex */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 381 | #define CONTIN 01 /* skip new lines to complete command */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 382 | |
| 383 | #define SYNTAXERR zzerr() |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 384 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 385 | static struct op *pipeline(int cf); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 386 | static struct op *andor(void); |
| 387 | static struct op *c_list(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 388 | static int synio(int cf); |
| 389 | static void musthave(int c, int cf); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 390 | static struct op *simple(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 391 | static struct op *nested(int type, int mark); |
| 392 | static struct op *command(int cf); |
| 393 | static struct op *dogroup(int onlydone); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 394 | static struct op *thenpart(void); |
| 395 | static struct op *elsepart(void); |
| 396 | static struct op *caselist(void); |
| 397 | static struct op *casepart(void); |
| 398 | static char **pattern(void); |
| 399 | static char **wordlist(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 400 | static struct op *list(struct op *t1, struct op *t2); |
| 401 | 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] | 402 | static struct op *newtp(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 403 | static struct op *namelist(struct op *t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 404 | static char **copyw(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 405 | static void word(char *cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 406 | static struct ioword **copyio(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 407 | static struct ioword *io(int u, int f, char *cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 408 | static void zzerr(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 409 | static void yyerror(char *s); |
| 410 | static int yylex(int cf); |
| 411 | static int collect(int c, int c1); |
| 412 | static int dual(int c); |
| 413 | static void diag(int ec); |
| 414 | static char *tree(unsigned size); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 415 | |
| 416 | /* -------- var.h -------- */ |
| 417 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 418 | struct var { |
| 419 | char *value; |
| 420 | char *name; |
| 421 | struct var *next; |
| 422 | char status; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 423 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 424 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 425 | #define COPYV 1 /* flag to setval, suggesting copy */ |
| 426 | #define RONLY 01 /* variable is read-only */ |
| 427 | #define EXPORT 02 /* variable is to be exported */ |
| 428 | #define GETCELL 04 /* name & value space was got with getcell */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 429 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 430 | static int yyparse(void); |
| 431 | static struct var *lookup(char *n); |
| 432 | static void setval(struct var *vp, char *val); |
| 433 | static void nameval(struct var *vp, char *val, char *name); |
| 434 | static void export(struct var *vp); |
| 435 | static void ronly(struct var *vp); |
| 436 | static int isassign(char *s); |
| 437 | static int checkname(char *cp); |
| 438 | static int assign(char *s, int cf); |
| 439 | static void putvlist(int f, int out); |
| 440 | static int eqname(char *n1, char *n2); |
| 441 | |
| 442 | static int execute(struct op *t, int *pin, int *pout, int act); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 443 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 444 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 445 | /* -------- io.h -------- */ |
| 446 | /* io buffer */ |
| 447 | struct iobuf { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 448 | unsigned id; /* buffer id */ |
| 449 | char buf[512]; /* buffer */ |
| 450 | char *bufp; /* pointer into buffer */ |
| 451 | char *ebufp; /* pointer to end of buffer */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 452 | }; |
| 453 | |
| 454 | /* possible arguments to an IO function */ |
| 455 | struct ioarg { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 456 | char *aword; |
| 457 | char **awordlist; |
| 458 | int afile; /* file descriptor */ |
| 459 | unsigned afid; /* buffer id */ |
| 460 | long afpos; /* file position */ |
| 461 | struct iobuf *afbuf; /* buffer for this file */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 462 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 463 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 464 | //static struct ioarg ioargstack[NPUSH]; |
| 465 | #define AFID_NOBUF (~0) |
| 466 | #define AFID_ID 0 |
| 467 | |
| 468 | /* an input generator's state */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 469 | struct io { |
| 470 | int (*iofn) (struct ioarg *, struct io *); |
| 471 | struct ioarg *argp; |
| 472 | int peekc; |
| 473 | char prev; /* previous character read by readc() */ |
| 474 | char nlcount; /* for `'s */ |
| 475 | char xchar; /* for `'s */ |
| 476 | char task; /* reason for pushed IO */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 477 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 478 | |
| 479 | //static struct io iostack[NPUSH]; |
| 480 | #define XOTHER 0 /* none of the below */ |
| 481 | #define XDOLL 1 /* expanding ${} */ |
| 482 | #define XGRAVE 2 /* expanding `'s */ |
| 483 | #define XIO 3 /* file IO */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 484 | |
| 485 | /* in substitution */ |
| 486 | #define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL) |
| 487 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 488 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 489 | /* |
| 490 | * input generators for IO structure |
| 491 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 492 | static int nlchar(struct ioarg *ap); |
| 493 | static int strchar(struct ioarg *ap); |
| 494 | static int qstrchar(struct ioarg *ap); |
| 495 | static int filechar(struct ioarg *ap); |
| 496 | static int herechar(struct ioarg *ap); |
| 497 | static int linechar(struct ioarg *ap); |
| 498 | static int gravechar(struct ioarg *ap, struct io *iop); |
| 499 | static int qgravechar(struct ioarg *ap, struct io *iop); |
| 500 | static int dolchar(struct ioarg *ap); |
| 501 | static int wdchar(struct ioarg *ap); |
| 502 | static void scraphere(void); |
| 503 | static void freehere(int area); |
| 504 | static void gethere(void); |
| 505 | static void markhere(char *s, struct ioword *iop); |
| 506 | static int herein(char *hname, int xdoll); |
| 507 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 508 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 509 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 510 | /* |
| 511 | * IO functions |
| 512 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 513 | static int eofc(void); |
| 514 | static int readc(void); |
| 515 | static void unget(int c); |
| 516 | static void ioecho(int c); |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 517 | static void prs(const char *s); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 518 | static void prn(unsigned u); |
| 519 | static void closef(int i); |
| 520 | static void closeall(void); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 521 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 522 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 523 | /* |
| 524 | * IO control |
| 525 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 526 | static void pushio(struct ioarg *argp, int (*f) (struct ioarg *)); |
| 527 | static int remap(int fd); |
| 528 | static int openpipe(int *pv); |
| 529 | static void closepipe(int *pv); |
| 530 | static struct io *setbase(struct io *ip); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 531 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 532 | #define PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen))) |
| 533 | #define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen))) |
| 534 | |
| 535 | /* -------- word.h -------- */ |
| 536 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 537 | #define NSTART 16 /* default number of words to allow for initially */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 538 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 539 | struct wdblock { |
| 540 | short w_bsize; |
| 541 | short w_nword; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 542 | /* bounds are arbitrary */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 543 | char *w_words[1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 544 | }; |
| 545 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 546 | static struct wdblock *addword(char *wd, struct wdblock *wb); |
| 547 | static struct wdblock *newword(int nw); |
| 548 | static char **getwords(struct wdblock *wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 549 | |
| 550 | /* -------- area.h -------- */ |
| 551 | |
| 552 | /* |
| 553 | * storage allocation |
| 554 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 555 | static char *getcell(unsigned nbytes); |
| 556 | static void garbage(void); |
| 557 | static void setarea(char *cp, int a); |
| 558 | static int getarea(char *cp); |
| 559 | static void freearea(int a); |
| 560 | static void freecell(char *cp); |
| 561 | static int areanum; /* current allocation area */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 562 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 563 | #define NEW(type) (type *)getcell(sizeof(type)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 564 | #define DELETE(obj) freecell((char *)obj) |
| 565 | |
| 566 | |
| 567 | /* -------- misc stuff -------- */ |
| 568 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 569 | 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] | 570 | static int iosetup(struct ioword *iop, int pipein, int pipeout); |
| 571 | static void echo(char **wp); |
| 572 | static struct op **find1case(struct op *t, char *w); |
| 573 | static struct op *findcase(struct op *t, char *w); |
| 574 | static void brkset(struct brkcon *bc); |
| 575 | static int dolabel(struct op *t); |
| 576 | static int dohelp(struct op *t); |
| 577 | static int dochdir(struct op *t); |
| 578 | static int doshift(struct op *t); |
| 579 | static int dologin(struct op *t); |
| 580 | static int doumask(struct op *t); |
| 581 | static int doexec(struct op *t); |
| 582 | static int dodot(struct op *t); |
| 583 | static int dowait(struct op *t); |
| 584 | static int doread(struct op *t); |
| 585 | static int doeval(struct op *t); |
| 586 | static int dotrap(struct op *t); |
| 587 | static int getsig(char *s); |
| 588 | static void setsig(int n, sighandler_t f); |
| 589 | static int getn(char *as); |
| 590 | static int dobreak(struct op *t); |
| 591 | static int docontinue(struct op *t); |
| 592 | static int brkcontin(char *cp, int val); |
| 593 | static int doexit(struct op *t); |
| 594 | static int doexport(struct op *t); |
| 595 | static int doreadonly(struct op *t); |
| 596 | static void rdexp(char **wp, void (*f) (struct var *), int key); |
| 597 | static void badid(char *s); |
| 598 | static int doset(struct op *t); |
| 599 | static void varput(char *s, int out); |
| 600 | static int dotimes(struct op *t); |
| 601 | static int expand(char *cp, struct wdblock **wbp, int f); |
| 602 | static char *blank(int f); |
| 603 | static int dollar(int quoted); |
| 604 | static int grave(int quoted); |
| 605 | static void globname(char *we, char *pp); |
| 606 | static char *generate(char *start1, char *end1, char *middle, char *end); |
| 607 | static int anyspcl(struct wdblock *wb); |
| 608 | static int xstrcmp(char *p1, char *p2); |
| 609 | static void glob0(char *a0, unsigned int a1, int a2, |
| 610 | int (*a3) (char *, char *)); |
| 611 | static void glob1(char *base, char *lim); |
| 612 | static void glob2(char *i, char *j); |
| 613 | static void glob3(char *i, char *j, char *k); |
| 614 | static void readhere(char **name, char *s, int ec); |
| 615 | static void pushio(struct ioarg *argp, int (*f) (struct ioarg *)); |
| 616 | static int xxchar(struct ioarg *ap); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 617 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 618 | struct here { |
| 619 | char *h_tag; |
| 620 | int h_dosub; |
| 621 | struct ioword *h_iop; |
| 622 | struct here *h_next; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 623 | }; |
| 624 | |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 625 | static const char * const signame[] = { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 626 | "Signal 0", |
| 627 | "Hangup", |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 628 | (char *) NULL, /* interrupt */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 629 | "Quit", |
| 630 | "Illegal instruction", |
| 631 | "Trace/BPT trap", |
| 632 | "Abort", |
| 633 | "Bus error", |
| 634 | "Floating Point Exception", |
| 635 | "Killed", |
| 636 | "SIGUSR1", |
| 637 | "SIGSEGV", |
| 638 | "SIGUSR2", |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 639 | (char *) NULL, /* broken pipe */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 640 | "Alarm clock", |
| 641 | "Terminated", |
| 642 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 643 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 644 | #define NSIGNAL (sizeof(signame)/sizeof(signame[0])) |
| 645 | |
| 646 | struct res { |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 647 | const char *r_name; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 648 | int r_val; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 649 | }; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 650 | static const struct res restab[] = { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 651 | {"for", FOR}, |
| 652 | {"case", CASE}, |
| 653 | {"esac", ESAC}, |
| 654 | {"while", WHILE}, |
| 655 | {"do", DO}, |
| 656 | {"done", DONE}, |
| 657 | {"if", IF}, |
| 658 | {"in", IN}, |
| 659 | {"then", THEN}, |
| 660 | {"else", ELSE}, |
| 661 | {"elif", ELIF}, |
| 662 | {"until", UNTIL}, |
| 663 | {"fi", FI}, |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 664 | {";;", BREAK}, |
| 665 | {"||", LOGOR}, |
| 666 | {"&&", LOGAND}, |
| 667 | {"{", '{'}, |
| 668 | {"}", '}'}, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 669 | {".", DOT}, |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 670 | {0, 0}, |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 671 | }; |
| 672 | |
| 673 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 674 | struct builtincmd { |
| 675 | const char *name; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 676 | int (*builtinfunc) (struct op * t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 677 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 678 | static const struct builtincmd builtincmds[] = { |
| 679 | {".", dodot}, |
| 680 | {":", dolabel}, |
| 681 | {"break", dobreak}, |
| 682 | {"cd", dochdir}, |
| 683 | {"continue", docontinue}, |
| 684 | {"eval", doeval}, |
| 685 | {"exec", doexec}, |
| 686 | {"exit", doexit}, |
| 687 | {"export", doexport}, |
| 688 | {"help", dohelp}, |
| 689 | {"login", dologin}, |
| 690 | {"newgrp", dologin}, |
| 691 | {"read", doread}, |
| 692 | {"readonly", doreadonly}, |
| 693 | {"set", doset}, |
| 694 | {"shift", doshift}, |
| 695 | {"times", dotimes}, |
| 696 | {"trap", dotrap}, |
| 697 | {"umask", doumask}, |
| 698 | {"wait", dowait}, |
| 699 | {0, 0} |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 700 | }; |
| 701 | |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 702 | static struct op *scantree(struct op *); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 703 | static struct op *dowholefile(int, int); |
| 704 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 705 | /* Globals */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 706 | extern char **environ; /* environment pointer */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 707 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 708 | static char **dolv; |
| 709 | static int dolc; |
| 710 | static int exstat; |
| 711 | static char gflg; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 712 | static int interactive; /* Is this an interactive shell */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 713 | static int execflg; |
| 714 | static int multiline; /* \n changed to ; */ |
| 715 | static struct op *outtree; /* result from parser */ |
| 716 | static xint *failpt; |
| 717 | static xint *errpt; |
| 718 | static struct brkcon *brklist; |
| 719 | static int isbreak; |
| 720 | static struct wdblock *wdlist; |
| 721 | static struct wdblock *iolist; |
| 722 | static char *trap[_NSIG + 1]; |
| 723 | static char ourtrap[_NSIG + 1]; |
| 724 | static int trapset; /* trap pending */ |
| 725 | static int yynerrs; /* yacc */ |
| 726 | static char line[LINELIM]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 727 | |
| 728 | #ifdef MSHDEBUG |
| 729 | static struct var *mshdbg_var; |
| 730 | #endif |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 731 | static struct var *vlist; /* dictionary */ |
| 732 | static struct var *homedir; /* home directory */ |
| 733 | static struct var *prompt; /* main prompt */ |
| 734 | static struct var *cprompt; /* continuation prompt */ |
| 735 | static struct var *path; /* search path for commands */ |
| 736 | static struct var *shell; /* shell to interpret command files */ |
| 737 | static struct var *ifs; /* field separators */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 738 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 739 | static int areanum; /* current allocation area */ |
| 740 | static int intr; |
| 741 | static int inparse; |
| 742 | static char flags['z' - 'a' + 1]; |
| 743 | static char *flag = flags - 'a'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 744 | static char *null = ""; |
| 745 | static int heedint = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 746 | static void (*qflag) (int) = SIG_IGN; |
| 747 | static int startl; |
| 748 | static int peeksym; |
| 749 | static int nlseen; |
| 750 | static int iounit = IODEFAULT; |
| 751 | static YYSTYPE yylval; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 752 | static char *elinep = line + sizeof(line) - 5; |
| 753 | |
| 754 | static struct ioarg temparg = { 0, 0, 0, AFID_NOBUF, 0 }; /* temporary for PUSHIO */ |
| 755 | static struct ioarg ioargstack[NPUSH]; |
| 756 | static struct io iostack[NPUSH]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 757 | static struct iobuf sharedbuf = { AFID_NOBUF }; |
| 758 | static struct iobuf mainbuf = { AFID_NOBUF }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 759 | static unsigned bufid = AFID_ID; /* buffer id counter */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 760 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 761 | static struct here *inhere; /* list of hear docs while parsing */ |
| 762 | static struct here *acthere; /* list of active here documents */ |
| 763 | static struct region *areabot; /* bottom of area */ |
| 764 | static struct region *areatop; /* top of area */ |
| 765 | static struct region *areanxt; /* starting point of scan */ |
| 766 | static void *brktop; |
| 767 | static void *brkaddr; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 768 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 769 | static struct env e = { |
| 770 | line, /* linep: char ptr */ |
| 771 | iostack, /* iobase: struct io ptr */ |
| 772 | iostack - 1, /* iop: struct io ptr */ |
| 773 | (xint *) NULL, /* errpt: void ptr for errors? */ |
| 774 | FDBASE, /* iofd: file desc */ |
| 775 | (struct env *) NULL /* oenv: struct env ptr */ |
| 776 | }; |
| 777 | |
| 778 | #ifdef MSHDEBUG |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 779 | void print_t(struct op *t); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 780 | void print_t(struct op *t) |
| 781 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 782 | DBGPRINTF(("T: t=%p, type %s, words=%p, IOword=%p\n", t, |
| 783 | T_CMD_NAMES[t->type], t->words, t->ioact)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 784 | |
| 785 | if (t->words) { |
| 786 | DBGPRINTF(("T: W1: %s", t->words[0])); |
| 787 | } |
| 788 | |
| 789 | return; |
| 790 | } |
| 791 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 792 | void print_tree(struct op *head); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 793 | void print_tree(struct op *head) |
| 794 | { |
| 795 | if (head == NULL) { |
| 796 | DBGPRINTF(("PRINT_TREE: no tree\n")); |
| 797 | return; |
| 798 | } |
| 799 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 800 | DBGPRINTF(("NODE: %p, left %p, right %p\n", head, head->left, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 801 | head->right)); |
| 802 | |
| 803 | if (head->left) |
| 804 | print_tree(head->left); |
| 805 | |
| 806 | if (head->right) |
| 807 | print_tree(head->right); |
| 808 | |
| 809 | return; |
| 810 | } |
| 811 | #endif /* MSHDEBUG */ |
| 812 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 813 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 814 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 815 | static char *current_prompt; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 816 | #endif |
| 817 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 818 | /* -------- sh.c -------- */ |
| 819 | /* |
| 820 | * shell |
| 821 | */ |
| 822 | |
| 823 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 824 | int msh_main(int argc, char **argv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 825 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 826 | REGISTER int f; |
| 827 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 828 | int cflag; |
| 829 | char *name, **ap; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 830 | int (*iof) (struct ioarg *); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 831 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 832 | DBGPRINTF(("MSH_MAIN: argc %d, environ %p\n", argc, environ)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 833 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 834 | initarea(); |
| 835 | if ((ap = environ) != NULL) { |
| 836 | while (*ap) |
| 837 | assign(*ap++, !COPYV); |
| 838 | for (ap = environ; *ap;) |
| 839 | export(lookup(*ap++)); |
| 840 | } |
| 841 | closeall(); |
| 842 | areanum = 1; |
| 843 | |
| 844 | shell = lookup("SHELL"); |
| 845 | if (shell->value == null) |
Eric Andersen | 7850014 | 2004-08-27 19:55:28 +0000 | [diff] [blame] | 846 | setval(shell, (char *)DEFAULT_SHELL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 847 | export(shell); |
| 848 | |
| 849 | homedir = lookup("HOME"); |
| 850 | if (homedir->value == null) |
| 851 | setval(homedir, "/"); |
| 852 | export(homedir); |
| 853 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 854 | setval(lookup("$"), putn(getpid())); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 855 | |
| 856 | path = lookup("PATH"); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 857 | if (path->value == null) { |
| 858 | if (geteuid() == 0) |
| 859 | setval(path, "/sbin:/bin:/usr/sbin:/usr/bin"); |
| 860 | else |
| 861 | setval(path, "/bin:/usr/bin"); |
| 862 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 863 | export(path); |
| 864 | |
| 865 | ifs = lookup("IFS"); |
| 866 | if (ifs->value == null) |
| 867 | setval(ifs, " \t\n"); |
| 868 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 869 | #ifdef MSHDEBUG |
| 870 | mshdbg_var = lookup("MSHDEBUG"); |
| 871 | if (mshdbg_var->value == null) |
| 872 | setval(mshdbg_var, "0"); |
| 873 | #endif |
| 874 | |
| 875 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 876 | prompt = lookup("PS1"); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 877 | #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 878 | if (prompt->value == null) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 879 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 880 | setval(prompt, "$ "); |
| 881 | if (geteuid() == 0) { |
| 882 | setval(prompt, "# "); |
| 883 | prompt->status &= ~EXPORT; |
| 884 | } |
| 885 | cprompt = lookup("PS2"); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 886 | #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 887 | if (cprompt->value == null) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 888 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 889 | setval(cprompt, "> "); |
| 890 | |
| 891 | iof = filechar; |
| 892 | cflag = 0; |
| 893 | name = *argv++; |
| 894 | if (--argc >= 1) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 895 | if (argv[0][0] == '-' && argv[0][1] != '\0') { |
| 896 | for (s = argv[0] + 1; *s; s++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 897 | switch (*s) { |
| 898 | case 'c': |
| 899 | prompt->status &= ~EXPORT; |
| 900 | cprompt->status &= ~EXPORT; |
| 901 | setval(prompt, ""); |
| 902 | setval(cprompt, ""); |
| 903 | cflag = 1; |
| 904 | if (--argc > 0) |
| 905 | PUSHIO(aword, *++argv, iof = nlchar); |
| 906 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 907 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 908 | case 'q': |
| 909 | qflag = SIG_DFL; |
| 910 | break; |
| 911 | |
| 912 | case 's': |
| 913 | /* standard input */ |
| 914 | break; |
| 915 | |
| 916 | case 't': |
| 917 | prompt->status &= ~EXPORT; |
| 918 | setval(prompt, ""); |
| 919 | iof = linechar; |
| 920 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 921 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 922 | case 'i': |
| 923 | interactive++; |
| 924 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 925 | if (*s >= 'a' && *s <= 'z') |
| 926 | flag[(int) *s]++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 927 | } |
| 928 | } else { |
| 929 | argv--; |
| 930 | argc++; |
| 931 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 932 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 933 | if (iof == filechar && --argc > 0) { |
| 934 | setval(prompt, ""); |
| 935 | setval(cprompt, ""); |
| 936 | prompt->status &= ~EXPORT; |
| 937 | cprompt->status &= ~EXPORT; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 938 | |
| 939 | /* Shell is non-interactive, activate printf-based debug */ |
| 940 | #ifdef MSHDEBUG |
| 941 | mshdbg = (int) (((char) (mshdbg_var->value[0])) - '0'); |
| 942 | if (mshdbg < 0) |
| 943 | mshdbg = 0; |
| 944 | #endif |
| 945 | DBGPRINTF(("MSH_MAIN: calling newfile()\n")); |
| 946 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 947 | if (newfile(name = *++argv)) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 948 | exit(1); /* Exit on error */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 949 | } |
| 950 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 951 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 952 | setdash(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 953 | |
| 954 | /* 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] | 955 | if (e.iop < iostack) { |
| 956 | PUSHIO(afile, 0, iof); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 957 | if (isatty(0) && isatty(1) && !cflag) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 958 | interactive++; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 959 | #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 960 | #ifdef MSHDEBUG |
"Vladimir N. Oleynik" | dd1ccdd | 2006-02-16 15:40:24 +0000 | [diff] [blame] | 961 | printf("\n\n%s Built-in shell (msh with debug)\n", BB_BANNER); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 962 | #else |
"Vladimir N. Oleynik" | dd1ccdd | 2006-02-16 15:40:24 +0000 | [diff] [blame] | 963 | printf("\n\n%s Built-in shell (msh)\n", BB_BANNER); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 964 | #endif |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 965 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
Eric Andersen | d63dee4 | 2001-10-19 00:22:23 +0000 | [diff] [blame] | 966 | #endif |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 967 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 968 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 969 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 970 | signal(SIGQUIT, qflag); |
| 971 | if (name && name[0] == '-') { |
| 972 | interactive++; |
| 973 | if ((f = open(".profile", 0)) >= 0) |
| 974 | next(remap(f)); |
| 975 | if ((f = open("/etc/profile", 0)) >= 0) |
| 976 | next(remap(f)); |
| 977 | } |
| 978 | if (interactive) |
| 979 | signal(SIGTERM, sig); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 980 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 981 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 982 | signal(SIGINT, onintr); |
| 983 | dolv = argv; |
| 984 | dolc = argc; |
| 985 | dolv[0] = name; |
| 986 | if (dolc > 1) { |
| 987 | for (ap = ++argv; --argc > 0;) { |
| 988 | if (assign(*ap = *argv++, !COPYV)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 989 | dolc--; /* keyword */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 990 | } else { |
| 991 | ap++; |
| 992 | } |
| 993 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 994 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 995 | setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc)); |
| 996 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 997 | DBGPRINTF(("MSH_MAIN: begin FOR loop, interactive %d, e.iop %p, iostack %p\n", interactive, e.iop, iostack)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 998 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 999 | for (;;) { |
| 1000 | if (interactive && e.iop <= iostack) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1001 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1002 | current_prompt = prompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1003 | #else |
| 1004 | prs(prompt->value); |
| 1005 | #endif |
| 1006 | } |
| 1007 | onecommand(); |
Eric Andersen | 1c31501 | 2002-04-26 23:40:09 +0000 | [diff] [blame] | 1008 | /* Ensure that getenv("PATH") stays current */ |
| 1009 | setenv("PATH", path->value, 1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1010 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1011 | |
| 1012 | DBGPRINTF(("MSH_MAIN: returning.\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1015 | static void setdash() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1016 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1017 | REGISTER char *cp; |
| 1018 | REGISTER int c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1019 | char m['z' - 'a' + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1020 | |
| 1021 | cp = m; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1022 | for (c = 'a'; c <= 'z'; c++) |
| 1023 | if (flag[(int) c]) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1024 | *cp++ = c; |
| 1025 | *cp = 0; |
| 1026 | setval(lookup("-"), m); |
| 1027 | } |
| 1028 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1029 | static int newfile(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1030 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1031 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1032 | REGISTER int f; |
| 1033 | |
| 1034 | DBGPRINTF7(("NEWFILE: opening %s\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1035 | |
| 1036 | if (strcmp(s, "-") != 0) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1037 | DBGPRINTF(("NEWFILE: s is %s\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1038 | f = open(s, 0); |
| 1039 | if (f < 0) { |
| 1040 | prs(s); |
| 1041 | err(": cannot open"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1042 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1043 | } |
| 1044 | } else |
| 1045 | f = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1046 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1047 | next(remap(f)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1048 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1051 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1052 | struct op *scantree(head) |
| 1053 | struct op *head; |
| 1054 | { |
| 1055 | struct op *dotnode; |
| 1056 | |
| 1057 | if (head == NULL) |
| 1058 | return (NULL); |
| 1059 | |
| 1060 | if (head->left != NULL) { |
| 1061 | dotnode = scantree(head->left); |
| 1062 | if (dotnode) |
| 1063 | return (dotnode); |
| 1064 | } |
| 1065 | |
| 1066 | if (head->right != NULL) { |
| 1067 | dotnode = scantree(head->right); |
| 1068 | if (dotnode) |
| 1069 | return (dotnode); |
| 1070 | } |
| 1071 | |
| 1072 | if (head->words == NULL) |
| 1073 | return (NULL); |
| 1074 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1075 | DBGPRINTF5(("SCANTREE: checking node %p\n", head)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1076 | |
| 1077 | if ((head->type != TDOT) && (strcmp(".", head->words[0]) == 0)) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1078 | DBGPRINTF5(("SCANTREE: dot found in node %p\n", head)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1079 | return (head); |
| 1080 | } |
| 1081 | |
| 1082 | return (NULL); |
| 1083 | } |
| 1084 | |
| 1085 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1086 | static void onecommand() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1087 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1088 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1089 | jmp_buf m1; |
| 1090 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1091 | DBGPRINTF(("ONECOMMAND: enter, outtree=%p\n", outtree)); |
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 | while (e.oenv) |
| 1094 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1095 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1096 | areanum = 1; |
| 1097 | freehere(areanum); |
| 1098 | freearea(areanum); |
| 1099 | garbage(); |
| 1100 | wdlist = 0; |
| 1101 | iolist = 0; |
| 1102 | e.errpt = 0; |
| 1103 | e.linep = line; |
| 1104 | yynerrs = 0; |
| 1105 | multiline = 0; |
| 1106 | inparse = 1; |
| 1107 | intr = 0; |
| 1108 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1109 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1110 | setjmp(failpt = m1); /* Bruce Evans' fix */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1111 | if (setjmp(failpt = m1) || yyparse() || intr) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1112 | |
| 1113 | DBGPRINTF(("ONECOMMAND: this is not good.\n")); |
| 1114 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1115 | while (e.oenv) |
| 1116 | quitenv(); |
| 1117 | scraphere(); |
| 1118 | if (!interactive && intr) |
| 1119 | leave(); |
| 1120 | inparse = 0; |
| 1121 | intr = 0; |
| 1122 | return; |
| 1123 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1124 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1125 | inparse = 0; |
| 1126 | brklist = 0; |
| 1127 | intr = 0; |
| 1128 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1129 | |
| 1130 | if (!flag['n']) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1131 | DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=%p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1132 | outtree)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1133 | execute(outtree, NOPIPE, NOPIPE, 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1136 | if (!interactive && intr) { |
| 1137 | execflg = 0; |
| 1138 | leave(); |
| 1139 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1140 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1141 | if ((i = trapset) != 0) { |
| 1142 | trapset = 0; |
| 1143 | runtrap(i); |
| 1144 | } |
| 1145 | } |
| 1146 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1147 | static void fail() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1148 | { |
| 1149 | longjmp(failpt, 1); |
| 1150 | /* NOTREACHED */ |
| 1151 | } |
| 1152 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1153 | static void leave() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1154 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1155 | DBGPRINTF(("LEAVE: leave called!\n")); |
| 1156 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1157 | if (execflg) |
| 1158 | fail(); |
| 1159 | scraphere(); |
| 1160 | freehere(1); |
| 1161 | runtrap(0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1162 | _exit(exstat); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1163 | /* NOTREACHED */ |
| 1164 | } |
| 1165 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1166 | static void warn(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1167 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1168 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1169 | if (*s) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1170 | prs(s); |
| 1171 | exstat = -1; |
| 1172 | } |
| 1173 | prs("\n"); |
| 1174 | if (flag['e']) |
| 1175 | leave(); |
| 1176 | } |
| 1177 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1178 | static void err(s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1179 | char *s; |
| 1180 | { |
| 1181 | warn(s); |
| 1182 | if (flag['n']) |
| 1183 | return; |
| 1184 | if (!interactive) |
| 1185 | leave(); |
| 1186 | if (e.errpt) |
| 1187 | longjmp(e.errpt, 1); |
| 1188 | closeall(); |
| 1189 | e.iop = e.iobase = iostack; |
| 1190 | } |
| 1191 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1192 | static int newenv(f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1193 | int f; |
| 1194 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1195 | REGISTER struct env *ep; |
| 1196 | |
| 1197 | DBGPRINTF(("NEWENV: f=%d (indicates quitenv and return)\n", f)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1198 | |
| 1199 | if (f) { |
| 1200 | quitenv(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1201 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1202 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1203 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1204 | ep = (struct env *) space(sizeof(*ep)); |
| 1205 | if (ep == NULL) { |
| 1206 | while (e.oenv) |
| 1207 | quitenv(); |
| 1208 | fail(); |
| 1209 | } |
| 1210 | *ep = e; |
| 1211 | e.oenv = ep; |
| 1212 | e.errpt = errpt; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1213 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1214 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1217 | static void quitenv() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1218 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1219 | REGISTER struct env *ep; |
| 1220 | REGISTER int fd; |
| 1221 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1222 | DBGPRINTF(("QUITENV: e.oenv=%p\n", e.oenv)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1223 | |
| 1224 | if ((ep = e.oenv) != NULL) { |
| 1225 | fd = e.iofd; |
| 1226 | e = *ep; |
| 1227 | /* should close `'d files */ |
| 1228 | DELETE(ep); |
| 1229 | while (--fd >= e.iofd) |
| 1230 | close(fd); |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | /* |
| 1235 | * Is any character from s1 in s2? |
| 1236 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1237 | static int anys(s1, s2) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1238 | REGISTER char *s1, *s2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1239 | { |
| 1240 | while (*s1) |
| 1241 | if (any(*s1++, s2)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1242 | return (1); |
| 1243 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * Is character c in s? |
| 1248 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1249 | static int any(c, s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1250 | REGISTER int c; |
| 1251 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1252 | { |
| 1253 | while (*s) |
| 1254 | if (*s++ == c) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1255 | return (1); |
| 1256 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1259 | static char *putn(n) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1260 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1261 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1262 | return (itoa(n)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1265 | static char *itoa(n) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1266 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1267 | { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1268 | static char s[20]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1269 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 1270 | snprintf(s, sizeof(s), "%u", n); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1271 | return (s); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1274 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1275 | static void next(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1276 | { |
| 1277 | PUSHIO(afile, f, filechar); |
| 1278 | } |
| 1279 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1280 | static void onintr(s) |
| 1281 | int s; /* ANSI C requires a parameter */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1282 | { |
| 1283 | signal(SIGINT, onintr); |
| 1284 | intr = 1; |
| 1285 | if (interactive) { |
| 1286 | if (inparse) { |
| 1287 | prs("\n"); |
| 1288 | fail(); |
| 1289 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1290 | } else if (heedint) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1291 | execflg = 0; |
| 1292 | leave(); |
| 1293 | } |
| 1294 | } |
| 1295 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1296 | static char *space(n) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1297 | int n; |
| 1298 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1299 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1300 | |
| 1301 | if ((cp = getcell(n)) == 0) |
| 1302 | err("out of string space"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1303 | return (cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1306 | static char *strsave(s, a) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1307 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1308 | int a; |
| 1309 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1310 | REGISTER char *cp, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1311 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1312 | if ((cp = space(strlen(s) + 1)) != NULL) { |
| 1313 | setarea((char *) cp, a); |
| 1314 | for (xp = cp; (*xp++ = *s++) != '\0';); |
| 1315 | return (cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1316 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1317 | return (""); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /* |
| 1321 | * trap handling |
| 1322 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1323 | static void sig(i) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1324 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1325 | { |
| 1326 | trapset = i; |
| 1327 | signal(i, sig); |
| 1328 | } |
| 1329 | |
| 1330 | static void runtrap(i) |
| 1331 | int i; |
| 1332 | { |
| 1333 | char *trapstr; |
| 1334 | |
| 1335 | if ((trapstr = trap[i]) == NULL) |
| 1336 | return; |
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 | if (i == 0) |
| 1339 | trap[i] = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1340 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1341 | RUN(aword, trapstr, nlchar); |
| 1342 | } |
| 1343 | |
| 1344 | /* -------- var.c -------- */ |
| 1345 | |
| 1346 | /* |
| 1347 | * Find the given name in the dictionary |
| 1348 | * and return its value. If the name was |
| 1349 | * not previously there, enter it now and |
| 1350 | * return a null value. |
| 1351 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1352 | static struct var *lookup(n) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1353 | REGISTER char *n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1354 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1355 | REGISTER struct var *vp; |
| 1356 | REGISTER char *cp; |
| 1357 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1358 | static struct var dummy; |
| 1359 | |
| 1360 | if (isdigit(*n)) { |
| 1361 | dummy.name = n; |
| 1362 | for (c = 0; isdigit(*n) && c < 1000; n++) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1363 | c = c * 10 + *n - '0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1364 | dummy.status = RONLY; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1365 | dummy.value = c <= dolc ? dolv[c] : null; |
| 1366 | return (&dummy); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1367 | } |
| 1368 | for (vp = vlist; vp; vp = vp->next) |
| 1369 | if (eqname(vp->name, n)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1370 | return (vp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1371 | cp = findeq(n); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1372 | vp = (struct var *) space(sizeof(*vp)); |
| 1373 | if (vp == 0 || (vp->name = space((int) (cp - n) + 2)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1374 | dummy.name = dummy.value = ""; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1375 | return (&dummy); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1376 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1377 | for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1378 | if (*cp == 0) |
| 1379 | *cp = '='; |
| 1380 | *++cp = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1381 | setarea((char *) vp, 0); |
| 1382 | setarea((char *) vp->name, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1383 | vp->value = null; |
| 1384 | vp->next = vlist; |
| 1385 | vp->status = GETCELL; |
| 1386 | vlist = vp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1387 | return (vp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * give variable at `vp' the value `val'. |
| 1392 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1393 | static void setval(vp, val) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1394 | struct var *vp; |
| 1395 | char *val; |
| 1396 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1397 | nameval(vp, val, (char *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | /* |
| 1401 | * if name is not NULL, it must be |
| 1402 | * a prefix of the space `val', |
| 1403 | * and end with `='. |
| 1404 | * this is all so that exporting |
| 1405 | * values is reasonably painless. |
| 1406 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1407 | static void nameval(vp, val, name) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1408 | REGISTER struct var *vp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1409 | char *val, *name; |
| 1410 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1411 | REGISTER char *cp, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1412 | char *nv; |
| 1413 | int fl; |
| 1414 | |
| 1415 | if (vp->status & RONLY) { |
| 1416 | for (xp = vp->name; *xp && *xp != '=';) |
| 1417 | putc(*xp++, stderr); |
| 1418 | err(" is read-only"); |
| 1419 | return; |
| 1420 | } |
| 1421 | fl = 0; |
| 1422 | if (name == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1423 | xp = space(strlen(vp->name) + strlen(val) + 2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1424 | if (xp == 0) |
| 1425 | return; |
| 1426 | /* make string: name=value */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1427 | setarea((char *) xp, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1428 | name = xp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1429 | for (cp = vp->name; (*xp = *cp++) && *xp != '='; xp++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1430 | if (*xp++ == 0) |
| 1431 | xp[-1] = '='; |
| 1432 | nv = xp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1433 | for (cp = val; (*xp++ = *cp++) != '\0';); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1434 | val = nv; |
| 1435 | fl = GETCELL; |
| 1436 | } |
| 1437 | if (vp->status & GETCELL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1438 | freecell(vp->name); /* form new string `name=value' */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1439 | vp->name = name; |
| 1440 | vp->value = val; |
| 1441 | vp->status |= fl; |
| 1442 | } |
| 1443 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1444 | static void export(vp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1445 | struct var *vp; |
| 1446 | { |
| 1447 | vp->status |= EXPORT; |
| 1448 | } |
| 1449 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1450 | static void ronly(vp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1451 | struct var *vp; |
| 1452 | { |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1453 | if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1454 | vp->status |= RONLY; |
| 1455 | } |
| 1456 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1457 | static int isassign(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1458 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1459 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1460 | DBGPRINTF7(("ISASSIGN: enter, s=%s\n", s)); |
| 1461 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1462 | if (!isalpha((int) *s) && *s != '_') |
| 1463 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1464 | for (; *s != '='; s++) |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1465 | if (*s == 0 || (!isalnum(*s) && *s != '_')) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1466 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1467 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1468 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1471 | static int assign(s, cf) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1472 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1473 | int cf; |
| 1474 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1475 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1476 | struct var *vp; |
| 1477 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1478 | DBGPRINTF7(("ASSIGN: enter, s=%s, cf=%d\n", s, cf)); |
| 1479 | |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1480 | if (!isalpha(*s) && *s != '_') |
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 | for (cp = s; *cp != '='; cp++) |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1483 | if (*cp == 0 || (!isalnum(*cp) && *cp != '_')) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1484 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1485 | vp = lookup(s); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1486 | nameval(vp, ++cp, cf == COPYV ? (char *) NULL : s); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1487 | if (cf != COPYV) |
| 1488 | vp->status &= ~GETCELL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1489 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1492 | static int checkname(cp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1493 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1494 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1495 | DBGPRINTF7(("CHECKNAME: enter, cp=%s\n", cp)); |
| 1496 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1497 | if (!isalpha(*cp++) && *(cp - 1) != '_') |
| 1498 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1499 | while (*cp) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1500 | if (!isalnum(*cp++) && *(cp - 1) != '_') |
| 1501 | return (0); |
| 1502 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1505 | static void putvlist(f, out) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1506 | REGISTER int f, out; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1507 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1508 | REGISTER struct var *vp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1509 | |
| 1510 | for (vp = vlist; vp; vp = vp->next) |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 1511 | if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1512 | if (vp->status & EXPORT) |
| 1513 | write(out, "export ", 7); |
| 1514 | if (vp->status & RONLY) |
| 1515 | write(out, "readonly ", 9); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1516 | write(out, vp->name, (int) (findeq(vp->name) - vp->name)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1517 | write(out, "\n", 1); |
| 1518 | } |
| 1519 | } |
| 1520 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1521 | static int eqname(n1, n2) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1522 | REGISTER char *n1, *n2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1523 | { |
| 1524 | for (; *n1 != '=' && *n1 != 0; n1++) |
| 1525 | if (*n2++ != *n1) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1526 | return (0); |
| 1527 | return (*n2 == 0 || *n2 == '='); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1530 | static char *findeq(cp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1531 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1532 | { |
| 1533 | while (*cp != '\0' && *cp != '=') |
| 1534 | cp++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1535 | return (cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | /* -------- gmatch.c -------- */ |
| 1539 | /* |
| 1540 | * int gmatch(string, pattern) |
| 1541 | * char *string, *pattern; |
| 1542 | * |
| 1543 | * Match a pattern as in sh(1). |
| 1544 | */ |
| 1545 | |
| 1546 | #define CMASK 0377 |
| 1547 | #define QUOTE 0200 |
| 1548 | #define QMASK (CMASK&~QUOTE) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1549 | #define NOT '!' /* might use ^ */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1550 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1551 | static int gmatch(s, p) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1552 | REGISTER char *s, *p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1553 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1554 | REGISTER int sc, pc; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1555 | |
| 1556 | if (s == NULL || p == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1557 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1558 | while ((pc = *p++ & CMASK) != '\0') { |
| 1559 | sc = *s++ & QMASK; |
| 1560 | switch (pc) { |
| 1561 | case '[': |
| 1562 | if ((p = cclass(p, sc)) == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1563 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1564 | break; |
| 1565 | |
| 1566 | case '?': |
| 1567 | if (sc == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1568 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1569 | break; |
| 1570 | |
| 1571 | case '*': |
| 1572 | s--; |
| 1573 | do { |
| 1574 | if (*p == '\0' || gmatch(s, p)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1575 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1576 | } while (*s++ != '\0'); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1577 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1578 | |
| 1579 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1580 | if (sc != (pc & ~QUOTE)) |
| 1581 | return (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 | return (*s == 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1587 | static char *cclass(p, sub) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1588 | REGISTER char *p; |
| 1589 | REGISTER int sub; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1590 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1591 | REGISTER int c, d, not, found; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1592 | |
| 1593 | if ((not = *p == NOT) != 0) |
| 1594 | p++; |
| 1595 | found = not; |
| 1596 | do { |
| 1597 | if (*p == '\0') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1598 | return ((char *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1599 | c = *p & CMASK; |
| 1600 | if (p[1] == '-' && p[2] != ']') { |
| 1601 | d = p[2] & CMASK; |
| 1602 | p++; |
| 1603 | } else |
| 1604 | d = c; |
| 1605 | if (c == sub || (c <= sub && sub <= d)) |
| 1606 | found = !not; |
| 1607 | } while (*++p != ']'); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1608 | return (found ? p + 1 : (char *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | |
| 1612 | /* -------- area.c -------- */ |
| 1613 | |
| 1614 | /* |
| 1615 | * All memory between (char *)areabot and (char *)(areatop+1) is |
| 1616 | * exclusively administered by the area management routines. |
| 1617 | * It is assumed that sbrk() and brk() manipulate the high end. |
| 1618 | */ |
| 1619 | |
| 1620 | #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;}) |
| 1621 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1622 | static void initarea() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1623 | { |
Mike Frysinger | 3132e9d | 2006-06-02 06:30:30 +0000 | [diff] [blame^] | 1624 | brkaddr = xmalloc(AREASIZE); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1625 | brktop = brkaddr + AREASIZE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1626 | |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 1627 | while ((long) sbrk(0) & ALIGN) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1628 | sbrk(1); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1629 | areabot = (struct region *) sbrk(REGSIZE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1630 | |
| 1631 | areabot->next = areabot; |
| 1632 | areabot->area = BUSY; |
| 1633 | areatop = areabot; |
| 1634 | areanxt = areabot; |
| 1635 | } |
| 1636 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1637 | char *getcell(nbytes) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1638 | unsigned nbytes; |
| 1639 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1640 | REGISTER int nregio; |
| 1641 | REGISTER struct region *p, *q; |
| 1642 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1643 | |
| 1644 | if (nbytes == 0) { |
| 1645 | puts("getcell(0)"); |
| 1646 | abort(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1647 | } |
| 1648 | /* silly and defeats the algorithm */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1649 | /* |
| 1650 | * round upwards and add administration area |
| 1651 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1652 | nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1653 | for (p = areanxt;;) { |
| 1654 | if (p->area > areanum) { |
| 1655 | /* |
| 1656 | * merge free cells |
| 1657 | */ |
| 1658 | while ((q = p->next)->area > areanum && q != areanxt) |
| 1659 | p->next = q->next; |
| 1660 | /* |
| 1661 | * exit loop if cell big enough |
| 1662 | */ |
| 1663 | if (q >= p + nregio) |
| 1664 | goto found; |
| 1665 | } |
| 1666 | p = p->next; |
| 1667 | if (p == areanxt) |
| 1668 | break; |
| 1669 | } |
| 1670 | i = nregio >= GROWBY ? nregio : GROWBY; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1671 | p = (struct region *) sbrk(i * REGSIZE); |
| 1672 | if (p == (struct region *) -1) |
| 1673 | return ((char *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1674 | p--; |
| 1675 | if (p != areatop) { |
| 1676 | puts("not contig"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1677 | abort(); /* allocated areas are contiguous */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1678 | } |
| 1679 | q = p + i; |
| 1680 | p->next = q; |
| 1681 | p->area = FREE; |
| 1682 | q->next = areabot; |
| 1683 | q->area = BUSY; |
| 1684 | areatop = q; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1685 | found: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1686 | /* |
| 1687 | * we found a FREE area big enough, pointed to by 'p', and up to 'q' |
| 1688 | */ |
| 1689 | areanxt = p + nregio; |
| 1690 | if (areanxt < q) { |
| 1691 | /* |
| 1692 | * split into requested area and rest |
| 1693 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1694 | if (areanxt + 1 > q) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1695 | puts("OOM"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1696 | abort(); /* insufficient space left for admin */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1697 | } |
| 1698 | areanxt->next = q; |
| 1699 | areanxt->area = FREE; |
| 1700 | p->next = areanxt; |
| 1701 | } |
| 1702 | p->area = areanum; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1703 | return ((char *) (p + 1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1706 | static void freecell(cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1707 | char *cp; |
| 1708 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1709 | REGISTER struct region *p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1710 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1711 | if ((p = (struct region *) cp) != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1712 | p--; |
| 1713 | if (p < areanxt) |
| 1714 | areanxt = p; |
| 1715 | p->area = FREE; |
| 1716 | } |
| 1717 | } |
| 1718 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1719 | static void freearea(a) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1720 | REGISTER int a; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1721 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1722 | REGISTER struct region *p, *top; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1723 | |
| 1724 | top = areatop; |
| 1725 | for (p = areabot; p != top; p = p->next) |
| 1726 | if (p->area >= a) |
| 1727 | p->area = FREE; |
| 1728 | } |
| 1729 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1730 | static void setarea(cp, a) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1731 | char *cp; |
| 1732 | int a; |
| 1733 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1734 | REGISTER struct region *p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1735 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1736 | if ((p = (struct region *) cp) != NULL) |
| 1737 | (p - 1)->area = a; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1740 | int getarea(cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1741 | char *cp; |
| 1742 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1743 | return ((struct region *) cp - 1)->area; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1746 | static void garbage() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1747 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1748 | REGISTER struct region *p, *q, *top; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1749 | |
| 1750 | top = areatop; |
| 1751 | for (p = areabot; p != top; p = p->next) { |
| 1752 | if (p->area > areanum) { |
| 1753 | while ((q = p->next)->area > areanum) |
| 1754 | p->next = q->next; |
| 1755 | areanxt = p; |
| 1756 | } |
| 1757 | } |
| 1758 | #ifdef SHRINKBY |
| 1759 | if (areatop >= q + SHRINKBY && q->area > areanum) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1760 | brk((char *) (q + 1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1761 | q->next = areabot; |
| 1762 | q->area = BUSY; |
| 1763 | areatop = q; |
| 1764 | } |
| 1765 | #endif |
| 1766 | } |
| 1767 | |
| 1768 | /* -------- csyn.c -------- */ |
| 1769 | /* |
| 1770 | * shell: syntax (C version) |
| 1771 | */ |
| 1772 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1773 | int yyparse() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1774 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1775 | DBGPRINTF7(("YYPARSE: enter...\n")); |
| 1776 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1777 | startl = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1778 | peeksym = 0; |
| 1779 | yynerrs = 0; |
| 1780 | outtree = c_list(); |
| 1781 | musthave('\n', 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1782 | return (yynerrs != 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1785 | static struct op *pipeline(cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1786 | int cf; |
| 1787 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1788 | REGISTER struct op *t, *p; |
| 1789 | REGISTER int c; |
| 1790 | |
| 1791 | DBGPRINTF7(("PIPELINE: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1792 | |
| 1793 | t = command(cf); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1794 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1795 | DBGPRINTF9(("PIPELINE: t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1796 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1797 | if (t != NULL) { |
| 1798 | while ((c = yylex(0)) == '|') { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1799 | if ((p = command(CONTIN)) == NULL) { |
| 1800 | DBGPRINTF8(("PIPELINE: error!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1801 | SYNTAXERR; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1804 | if (t->type != TPAREN && t->type != TCOM) { |
| 1805 | /* shell statement */ |
| 1806 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 1807 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1808 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1809 | t = block(TPIPE, t, p, NOWORDS); |
| 1810 | } |
| 1811 | peeksym = c; |
| 1812 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1813 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1814 | DBGPRINTF7(("PIPELINE: returning t=%p\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1815 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1818 | static struct op *andor() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1819 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1820 | REGISTER struct op *t, *p; |
| 1821 | REGISTER int c; |
| 1822 | |
| 1823 | DBGPRINTF7(("ANDOR: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1824 | |
| 1825 | t = pipeline(0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1826 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1827 | DBGPRINTF9(("ANDOR: t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1828 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1829 | if (t != NULL) { |
| 1830 | while ((c = yylex(0)) == LOGAND || c == LOGOR) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1831 | if ((p = pipeline(CONTIN)) == NULL) { |
| 1832 | DBGPRINTF8(("ANDOR: error!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1833 | SYNTAXERR; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1836 | t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1837 | } /* WHILE */ |
| 1838 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1839 | peeksym = c; |
| 1840 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1841 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1842 | DBGPRINTF7(("ANDOR: returning t=%p\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1843 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1846 | static struct op *c_list() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1847 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1848 | REGISTER struct op *t, *p; |
| 1849 | REGISTER int c; |
| 1850 | |
| 1851 | DBGPRINTF7(("C_LIST: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1852 | |
| 1853 | t = andor(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1854 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1855 | if (t != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1856 | if ((peeksym = yylex(0)) == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1857 | t = block(TASYNC, t, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1858 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1859 | while ((c = yylex(0)) == ';' || c == '&' |
| 1860 | || (multiline && c == '\n')) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1861 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1862 | if ((p = andor()) == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1863 | return (t); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1864 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1865 | if ((peeksym = yylex(0)) == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1866 | p = block(TASYNC, p, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1867 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1868 | t = list(t, p); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1869 | } /* WHILE */ |
| 1870 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1871 | peeksym = c; |
| 1872 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1873 | /* IF */ |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1874 | DBGPRINTF7(("C_LIST: returning t=%p\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1875 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1878 | static int synio(cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1879 | int cf; |
| 1880 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1881 | REGISTER struct ioword *iop; |
| 1882 | REGISTER int i; |
| 1883 | REGISTER int c; |
| 1884 | |
| 1885 | DBGPRINTF7(("SYNIO: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1886 | |
| 1887 | if ((c = yylex(cf)) != '<' && c != '>') { |
| 1888 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1889 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1890 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1891 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1892 | i = yylval.i; |
| 1893 | musthave(WORD, 0); |
| 1894 | iop = io(iounit, i, yylval.cp); |
| 1895 | iounit = IODEFAULT; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1896 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1897 | if (i & IOHERE) |
| 1898 | markhere(yylval.cp, iop); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1899 | |
| 1900 | DBGPRINTF7(("SYNIO: returning 1\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1901 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1904 | static void musthave(c, cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1905 | int c, cf; |
| 1906 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1907 | if ((peeksym = yylex(cf)) != c) { |
| 1908 | DBGPRINTF7(("MUSTHAVE: error!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1909 | SYNTAXERR; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1912 | peeksym = 0; |
| 1913 | } |
| 1914 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1915 | static struct op *simple() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1916 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1917 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1918 | |
| 1919 | t = NULL; |
| 1920 | for (;;) { |
| 1921 | switch (peeksym = yylex(0)) { |
| 1922 | case '<': |
| 1923 | case '>': |
| 1924 | (void) synio(0); |
| 1925 | break; |
| 1926 | |
| 1927 | case WORD: |
| 1928 | if (t == NULL) { |
| 1929 | t = newtp(); |
| 1930 | t->type = TCOM; |
| 1931 | } |
| 1932 | peeksym = 0; |
| 1933 | word(yylval.cp); |
| 1934 | break; |
| 1935 | |
| 1936 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1937 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | } |
| 1941 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1942 | static struct op *nested(type, mark) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1943 | int type, mark; |
| 1944 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1945 | REGISTER struct op *t; |
| 1946 | |
| 1947 | DBGPRINTF3(("NESTED: enter, type=%d, mark=%d\n", type, mark)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1948 | |
| 1949 | multiline++; |
| 1950 | t = c_list(); |
| 1951 | musthave(mark, 0); |
| 1952 | multiline--; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1953 | return (block(type, t, NOBLOCK, NOWORDS)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1956 | static struct op *command(cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1957 | int cf; |
| 1958 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1959 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1960 | struct wdblock *iosave; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1961 | REGISTER int c; |
| 1962 | |
| 1963 | DBGPRINTF(("COMMAND: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1964 | |
| 1965 | iosave = iolist; |
| 1966 | iolist = NULL; |
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 | if (multiline) |
| 1969 | cf |= CONTIN; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1970 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1971 | while (synio(cf)) |
| 1972 | cf = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1973 | |
| 1974 | c = yylex(cf); |
| 1975 | |
| 1976 | switch (c) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1977 | default: |
| 1978 | peeksym = c; |
| 1979 | if ((t = simple()) == NULL) { |
| 1980 | if (iolist == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1981 | return ((struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1982 | t = newtp(); |
| 1983 | t->type = TCOM; |
| 1984 | } |
| 1985 | break; |
| 1986 | |
| 1987 | case '(': |
| 1988 | t = nested(TPAREN, ')'); |
| 1989 | break; |
| 1990 | |
| 1991 | case '{': |
| 1992 | t = nested(TBRACE, '}'); |
| 1993 | break; |
| 1994 | |
| 1995 | case FOR: |
| 1996 | t = newtp(); |
| 1997 | t->type = TFOR; |
| 1998 | musthave(WORD, 0); |
| 1999 | startl = 1; |
| 2000 | t->str = yylval.cp; |
| 2001 | multiline++; |
| 2002 | t->words = wordlist(); |
| 2003 | if ((c = yylex(0)) != '\n' && c != ';') |
| 2004 | peeksym = c; |
| 2005 | t->left = dogroup(0); |
| 2006 | multiline--; |
| 2007 | break; |
| 2008 | |
| 2009 | case WHILE: |
| 2010 | case UNTIL: |
| 2011 | multiline++; |
| 2012 | t = newtp(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2013 | t->type = c == WHILE ? TWHILE : TUNTIL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2014 | t->left = c_list(); |
| 2015 | t->right = dogroup(1); |
| 2016 | t->words = NULL; |
| 2017 | multiline--; |
| 2018 | break; |
| 2019 | |
| 2020 | case CASE: |
| 2021 | t = newtp(); |
| 2022 | t->type = TCASE; |
| 2023 | musthave(WORD, 0); |
| 2024 | t->str = yylval.cp; |
| 2025 | startl++; |
| 2026 | multiline++; |
| 2027 | musthave(IN, CONTIN); |
| 2028 | startl++; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2029 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2030 | t->left = caselist(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2031 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2032 | musthave(ESAC, 0); |
| 2033 | multiline--; |
| 2034 | break; |
| 2035 | |
| 2036 | case IF: |
| 2037 | multiline++; |
| 2038 | t = newtp(); |
| 2039 | t->type = TIF; |
| 2040 | t->left = c_list(); |
| 2041 | t->right = thenpart(); |
| 2042 | musthave(FI, 0); |
| 2043 | multiline--; |
| 2044 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2045 | |
| 2046 | case DOT: |
| 2047 | t = newtp(); |
| 2048 | t->type = TDOT; |
| 2049 | |
| 2050 | musthave(WORD, 0); /* gets name of file */ |
| 2051 | DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp)); |
| 2052 | |
| 2053 | word(yylval.cp); /* add word to wdlist */ |
| 2054 | word(NOWORD); /* terminate wdlist */ |
| 2055 | t->words = copyw(); /* dup wdlist */ |
| 2056 | break; |
| 2057 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2058 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2059 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2060 | while (synio(0)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2061 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2062 | t = namelist(t); |
| 2063 | iolist = iosave; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2064 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2065 | DBGPRINTF(("COMMAND: returning %p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2066 | |
| 2067 | return (t); |
| 2068 | } |
| 2069 | |
| 2070 | static struct op *dowholefile(type, mark) |
| 2071 | int type; |
| 2072 | int mark; |
| 2073 | { |
| 2074 | REGISTER struct op *t; |
| 2075 | |
| 2076 | DBGPRINTF(("DOWHOLEFILE: enter, type=%d, mark=%d\n", type, mark)); |
| 2077 | |
| 2078 | multiline++; |
| 2079 | t = c_list(); |
| 2080 | multiline--; |
| 2081 | t = block(type, t, NOBLOCK, NOWORDS); |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2082 | DBGPRINTF(("DOWHOLEFILE: return t=%p\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2083 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2086 | static struct op *dogroup(onlydone) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2087 | int onlydone; |
| 2088 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2089 | REGISTER int c; |
| 2090 | REGISTER struct op *mylist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2091 | |
| 2092 | c = yylex(CONTIN); |
| 2093 | if (c == DONE && onlydone) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2094 | return ((struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2095 | if (c != DO) |
| 2096 | SYNTAXERR; |
| 2097 | mylist = c_list(); |
| 2098 | musthave(DONE, 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2099 | return (mylist); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2102 | static struct op *thenpart() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2103 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2104 | REGISTER int c; |
| 2105 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2106 | |
| 2107 | if ((c = yylex(0)) != THEN) { |
| 2108 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2109 | return ((struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2110 | } |
| 2111 | t = newtp(); |
| 2112 | t->type = 0; |
| 2113 | t->left = c_list(); |
| 2114 | if (t->left == NULL) |
| 2115 | SYNTAXERR; |
| 2116 | t->right = elsepart(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2117 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2120 | static struct op *elsepart() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2121 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2122 | REGISTER int c; |
| 2123 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2124 | |
| 2125 | switch (c = yylex(0)) { |
| 2126 | case ELSE: |
| 2127 | if ((t = c_list()) == NULL) |
| 2128 | SYNTAXERR; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2129 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2130 | |
| 2131 | case ELIF: |
| 2132 | t = newtp(); |
| 2133 | t->type = TELIF; |
| 2134 | t->left = c_list(); |
| 2135 | t->right = thenpart(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2136 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2137 | |
| 2138 | default: |
| 2139 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2140 | return ((struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2141 | } |
| 2142 | } |
| 2143 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2144 | static struct op *caselist() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2145 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2146 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2147 | |
| 2148 | t = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2149 | while ((peeksym = yylex(CONTIN)) != ESAC) { |
| 2150 | DBGPRINTF(("CASELIST, doing yylex, peeksym=%d\n", peeksym)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2151 | t = list(t, casepart()); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2154 | DBGPRINTF(("CASELIST, returning t=%p\n", t)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2155 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2158 | static struct op *casepart() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2159 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2160 | REGISTER struct op *t; |
| 2161 | |
| 2162 | DBGPRINTF7(("CASEPART: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2163 | |
| 2164 | t = newtp(); |
| 2165 | t->type = TPAT; |
| 2166 | t->words = pattern(); |
| 2167 | musthave(')', 0); |
| 2168 | t->left = c_list(); |
| 2169 | if ((peeksym = yylex(CONTIN)) != ESAC) |
| 2170 | musthave(BREAK, CONTIN); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2171 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2172 | DBGPRINTF7(("CASEPART: made newtp(TPAT, t=%p)\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2173 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2174 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2177 | static char **pattern() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2178 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2179 | REGISTER int c, cf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2180 | |
| 2181 | cf = CONTIN; |
| 2182 | do { |
| 2183 | musthave(WORD, cf); |
| 2184 | word(yylval.cp); |
| 2185 | cf = 0; |
| 2186 | } while ((c = yylex(0)) == '|'); |
| 2187 | peeksym = c; |
| 2188 | word(NOWORD); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2189 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2190 | return (copyw()); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2193 | static char **wordlist() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2194 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2195 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2196 | |
| 2197 | if ((c = yylex(0)) != IN) { |
| 2198 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2199 | return ((char **) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2200 | } |
| 2201 | startl = 0; |
| 2202 | while ((c = yylex(0)) == WORD) |
| 2203 | word(yylval.cp); |
| 2204 | word(NOWORD); |
| 2205 | peeksym = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2206 | return (copyw()); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * supporting functions |
| 2211 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2212 | static struct op *list(t1, t2) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2213 | REGISTER struct op *t1, *t2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2214 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2215 | DBGPRINTF7(("LIST: enter, t1=%p, t2=%p\n", t1, t2)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2216 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2217 | if (t1 == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2218 | return (t2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2219 | if (t2 == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2220 | return (t1); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2221 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2222 | return (block(TLIST, t1, t2, NOWORDS)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2225 | static struct op *block(type, t1, t2, wp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2226 | int type; |
| 2227 | struct op *t1, *t2; |
| 2228 | char **wp; |
| 2229 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2230 | REGISTER struct op *t; |
| 2231 | |
| 2232 | DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type])); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2233 | |
| 2234 | t = newtp(); |
| 2235 | t->type = type; |
| 2236 | t->left = t1; |
| 2237 | t->right = t2; |
| 2238 | t->words = wp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2239 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2240 | DBGPRINTF7(("BLOCK: inserted %p between %p and %p\n", t, t1, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2241 | t2)); |
| 2242 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2243 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2246 | /* See if given string is a shell multiline (FOR, IF, etc) */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2247 | static int rlookup(n) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2248 | REGISTER char *n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2249 | { |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 2250 | REGISTER const struct res *rp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2251 | |
| 2252 | DBGPRINTF7(("RLOOKUP: enter, n is %s\n", n)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2253 | |
| 2254 | for (rp = restab; rp->r_name; rp++) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2255 | if (strcmp(rp->r_name, n) == 0) { |
| 2256 | DBGPRINTF7(("RLOOKUP: match, returning %d\n", rp->r_val)); |
| 2257 | return (rp->r_val); /* Return numeric code for shell multiline */ |
| 2258 | } |
| 2259 | |
| 2260 | DBGPRINTF7(("RLOOKUP: NO match, returning 0\n")); |
| 2261 | return (0); /* Not a shell multiline */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2264 | static struct op *newtp() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2265 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2266 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2267 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2268 | t = (struct op *) tree(sizeof(*t)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2269 | t->type = 0; |
| 2270 | t->words = NULL; |
| 2271 | t->ioact = NULL; |
| 2272 | t->left = NULL; |
| 2273 | t->right = NULL; |
| 2274 | t->str = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2275 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2276 | DBGPRINTF3(("NEWTP: allocated %p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2277 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2278 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2281 | static struct op *namelist(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2282 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2283 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2284 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2285 | DBGPRINTF7(("NAMELIST: enter, t=%p, type %s, iolist=%p\n", t, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2286 | T_CMD_NAMES[t->type], iolist)); |
| 2287 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2288 | if (iolist) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2289 | iolist = addword((char *) NULL, iolist); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2290 | t->ioact = copyio(); |
| 2291 | } else |
| 2292 | t->ioact = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2293 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2294 | if (t->type != TCOM) { |
| 2295 | if (t->type != TPAREN && t->ioact != NULL) { |
| 2296 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 2297 | t->ioact = t->left->ioact; |
| 2298 | t->left->ioact = NULL; |
| 2299 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2300 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2301 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2302 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2303 | word(NOWORD); |
| 2304 | t->words = copyw(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2305 | |
| 2306 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2307 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2310 | static char **copyw() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2311 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2312 | REGISTER char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2313 | |
| 2314 | wd = getwords(wdlist); |
| 2315 | wdlist = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2316 | return (wd); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2319 | static void word(cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2320 | char *cp; |
| 2321 | { |
| 2322 | wdlist = addword(cp, wdlist); |
| 2323 | } |
| 2324 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2325 | static struct ioword **copyio() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2326 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2327 | REGISTER struct ioword **iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2328 | |
| 2329 | iop = (struct ioword **) getwords(iolist); |
| 2330 | iolist = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2331 | return (iop); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2334 | static struct ioword *io(u, f, cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2335 | int u; |
| 2336 | int f; |
| 2337 | char *cp; |
| 2338 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2339 | REGISTER struct ioword *iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2340 | |
| 2341 | iop = (struct ioword *) tree(sizeof(*iop)); |
| 2342 | iop->io_unit = u; |
| 2343 | iop->io_flag = f; |
| 2344 | iop->io_name = cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2345 | iolist = addword((char *) iop, iolist); |
| 2346 | return (iop); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2349 | static void zzerr() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2350 | { |
| 2351 | yyerror("syntax error"); |
| 2352 | } |
| 2353 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2354 | static void yyerror(s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2355 | char *s; |
| 2356 | { |
| 2357 | yynerrs++; |
| 2358 | if (interactive && e.iop <= iostack) { |
| 2359 | multiline = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2360 | while (eofc() == 0 && yylex(0) != '\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2361 | } |
| 2362 | err(s); |
| 2363 | fail(); |
| 2364 | } |
| 2365 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2366 | static int yylex(cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2367 | int cf; |
| 2368 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2369 | REGISTER int c, c1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2370 | int atstart; |
| 2371 | |
| 2372 | if ((c = peeksym) > 0) { |
| 2373 | peeksym = 0; |
| 2374 | if (c == '\n') |
| 2375 | startl = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2376 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2377 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2378 | |
| 2379 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2380 | nlseen = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2381 | atstart = startl; |
| 2382 | startl = 0; |
| 2383 | yylval.i = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2384 | e.linep = line; |
| 2385 | |
| 2386 | /* MALAMO */ |
| 2387 | line[LINELIM - 1] = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2388 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2389 | loop: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2390 | while ((c = my_getc(0)) == ' ' || c == '\t') /* Skip whitespace */ |
| 2391 | ; |
| 2392 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2393 | switch (c) { |
| 2394 | default: |
| 2395 | if (any(c, "0123456789")) { |
| 2396 | unget(c1 = my_getc(0)); |
| 2397 | if (c1 == '<' || c1 == '>') { |
| 2398 | iounit = c - '0'; |
| 2399 | goto loop; |
| 2400 | } |
| 2401 | *e.linep++ = c; |
| 2402 | c = c1; |
| 2403 | } |
| 2404 | break; |
| 2405 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2406 | case '#': /* Comment, skip to next newline or End-of-string */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2407 | while ((c = my_getc(0)) != 0 && c != '\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2408 | unget(c); |
| 2409 | goto loop; |
| 2410 | |
| 2411 | case 0: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2412 | DBGPRINTF5(("YYLEX: return 0, c=%d\n", c)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2413 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2414 | |
| 2415 | case '$': |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2416 | DBGPRINTF9(("YYLEX: found $\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2417 | *e.linep++ = c; |
| 2418 | if ((c = my_getc(0)) == '{') { |
| 2419 | if ((c = collect(c, '}')) != '\0') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2420 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2421 | goto pack; |
| 2422 | } |
| 2423 | break; |
| 2424 | |
| 2425 | case '`': |
| 2426 | case '\'': |
| 2427 | case '"': |
| 2428 | if ((c = collect(c, c)) != '\0') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2429 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2430 | goto pack; |
| 2431 | |
| 2432 | case '|': |
| 2433 | case '&': |
| 2434 | case ';': |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2435 | startl = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2436 | /* If more chars process them, else return NULL char */ |
| 2437 | if ((c1 = dual(c)) != '\0') |
| 2438 | return (c1); |
| 2439 | else |
| 2440 | return (c); |
| 2441 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2442 | case '^': |
| 2443 | startl = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2444 | return ('|'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2445 | case '>': |
| 2446 | case '<': |
| 2447 | diag(c); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2448 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2449 | |
| 2450 | case '\n': |
| 2451 | nlseen++; |
| 2452 | gethere(); |
| 2453 | startl = 1; |
| 2454 | if (multiline || cf & CONTIN) { |
| 2455 | if (interactive && e.iop <= iostack) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 2456 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2457 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2458 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2459 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2460 | #endif |
| 2461 | } |
| 2462 | if (cf & CONTIN) |
| 2463 | goto loop; |
| 2464 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2465 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2466 | |
| 2467 | case '(': |
| 2468 | case ')': |
| 2469 | startl = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2470 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | unget(c); |
| 2474 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2475 | pack: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2476 | while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n")) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2477 | if (e.linep >= elinep) |
| 2478 | err("word too long"); |
| 2479 | else |
| 2480 | *e.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2481 | }; |
| 2482 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2483 | unget(c); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2484 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2485 | if (any(c, "\"'`$")) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2486 | goto loop; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2487 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2488 | *e.linep++ = '\0'; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2489 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2490 | if (atstart && (c = rlookup(line)) != 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2491 | startl = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2492 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2493 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2494 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2495 | yylval.cp = strsave(line, areanum); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2496 | return (WORD); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2499 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2500 | static int collect(c, c1) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2501 | REGISTER int c, c1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2502 | { |
| 2503 | char s[2]; |
| 2504 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2505 | DBGPRINTF8(("COLLECT: enter, c=%d, c1=%d\n", c, c1)); |
| 2506 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2507 | *e.linep++ = c; |
| 2508 | while ((c = my_getc(c1)) != c1) { |
| 2509 | if (c == 0) { |
| 2510 | unget(c); |
| 2511 | s[0] = c1; |
| 2512 | s[1] = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2513 | prs("no closing "); |
| 2514 | yyerror(s); |
| 2515 | return (YYERRCODE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2516 | } |
| 2517 | if (interactive && c == '\n' && e.iop <= iostack) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 2518 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2519 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2520 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2521 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2522 | #endif |
| 2523 | } |
| 2524 | *e.linep++ = c; |
| 2525 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2526 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2527 | *e.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2528 | |
| 2529 | DBGPRINTF8(("COLLECT: return 0, line is %s\n", line)); |
| 2530 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2531 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2534 | /* "multiline commands" helper func */ |
| 2535 | /* see if next 2 chars form a shell multiline */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2536 | static int dual(c) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2537 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2538 | { |
| 2539 | char s[3]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2540 | REGISTER char *cp = s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2541 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2542 | DBGPRINTF8(("DUAL: enter, c=%d\n", c)); |
| 2543 | |
| 2544 | *cp++ = c; /* c is the given "peek" char */ |
| 2545 | *cp++ = my_getc(0); /* get next char of input */ |
| 2546 | *cp = 0; /* add EOS marker */ |
| 2547 | |
| 2548 | c = rlookup(s); /* see if 2 chars form a shell multiline */ |
| 2549 | if (c == 0) |
| 2550 | unget(*--cp); /* String is not a shell multiline, put peek char back */ |
| 2551 | |
| 2552 | return (c); /* String is multiline, return numeric multiline (restab) code */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2555 | static void diag(ec) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2556 | REGISTER int ec; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2557 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2558 | REGISTER int c; |
| 2559 | |
| 2560 | DBGPRINTF8(("DIAG: enter, ec=%d\n", ec)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2561 | |
| 2562 | c = my_getc(0); |
| 2563 | if (c == '>' || c == '<') { |
| 2564 | if (c != ec) |
| 2565 | zzerr(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2566 | yylval.i = ec == '>' ? IOWRITE | IOCAT : IOHERE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2567 | c = my_getc(0); |
| 2568 | } else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2569 | yylval.i = ec == '>' ? IOWRITE : IOREAD; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2570 | if (c != '&' || yylval.i == IOHERE) |
| 2571 | unget(c); |
| 2572 | else |
| 2573 | yylval.i |= IODUP; |
| 2574 | } |
| 2575 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2576 | static char *tree(size) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2577 | unsigned size; |
| 2578 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2579 | REGISTER char *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2580 | |
| 2581 | if ((t = getcell(size)) == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2582 | DBGPRINTF2(("TREE: getcell(%d) failed!\n", size)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2583 | prs("command line too complicated\n"); |
| 2584 | fail(); |
| 2585 | /* NOTREACHED */ |
| 2586 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2587 | return (t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | /* VARARGS1 */ |
| 2591 | /* ARGSUSED */ |
| 2592 | |
| 2593 | /* -------- exec.c -------- */ |
| 2594 | |
| 2595 | /* |
| 2596 | * execute tree |
| 2597 | */ |
| 2598 | |
| 2599 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2600 | static int execute(t, pin, pout, act) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2601 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2602 | int *pin, *pout; |
| 2603 | int act; |
| 2604 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2605 | REGISTER struct op *t1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2606 | volatile int i, rv, a; |
| 2607 | char *cp, **wp, **wp2; |
| 2608 | struct var *vp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2609 | struct op *outtree_save; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2610 | struct brkcon bc; |
| 2611 | |
| 2612 | #if __GNUC__ |
| 2613 | /* Avoid longjmp clobbering */ |
| 2614 | (void) ℘ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2615 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2616 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2617 | if (t == NULL) { |
| 2618 | DBGPRINTF4(("EXECUTE: enter, t==null, returning.\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2619 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2622 | DBGPRINTF(("EXECUTE: t=%p, t->type=%d (%s), t->words is %s\n", t, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2623 | t->type, T_CMD_NAMES[t->type], |
| 2624 | ((t->words == NULL) ? "NULL" : t->words[0]))); |
| 2625 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2626 | rv = 0; |
| 2627 | a = areanum++; |
| 2628 | wp = (wp2 = t->words) != NULL |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2629 | ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY) |
| 2630 | : NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2631 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2632 | /* Hard to know how many words there are, be careful of garbage pointer values */ |
| 2633 | /* They are likely to cause "PCI bus fault" errors */ |
| 2634 | #if 0 |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2635 | DBGPRINTF(("EXECUTE: t->left=%p, t->right=%p, t->words[1] is %s\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2636 | t->left, t->right, |
| 2637 | ((t->words[1] == NULL) ? "NULL" : t->words[1]))); |
| 2638 | DBGPRINTF7(("EXECUTE: t->words[2] is %s, t->words[3] is %s\n", |
| 2639 | ((t->words[2] == NULL) ? "NULL" : t->words[2]), |
| 2640 | ((t->words[3] == NULL) ? "NULL" : t->words[3]))); |
| 2641 | #endif |
| 2642 | |
| 2643 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2644 | switch (t->type) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2645 | case TDOT: |
| 2646 | DBGPRINTF3(("EXECUTE: TDOT\n")); |
| 2647 | |
| 2648 | outtree_save = outtree; |
| 2649 | |
| 2650 | newfile(evalstr(t->words[0], DOALL)); |
| 2651 | |
| 2652 | t->left = dowholefile(TLIST, 0); |
| 2653 | t->right = NULL; |
| 2654 | |
| 2655 | outtree = outtree_save; |
| 2656 | |
| 2657 | if (t->left) |
| 2658 | rv = execute(t->left, pin, pout, 0); |
| 2659 | if (t->right) |
| 2660 | rv = execute(t->right, pin, pout, 0); |
| 2661 | break; |
| 2662 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2663 | case TPAREN: |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 2664 | rv = execute(t->left, pin, pout, 0); |
| 2665 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2666 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2667 | case TCOM: |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 2668 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2669 | rv = forkexec(t, pin, pout, act, wp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2670 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2671 | break; |
| 2672 | |
| 2673 | case TPIPE: |
| 2674 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2675 | int pv[2]; |
| 2676 | |
| 2677 | if ((rv = openpipe(pv)) < 0) |
| 2678 | break; |
| 2679 | pv[0] = remap(pv[0]); |
| 2680 | pv[1] = remap(pv[1]); |
| 2681 | (void) execute(t->left, pin, pv, 0); |
| 2682 | rv = execute(t->right, pv, pout, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2683 | } |
| 2684 | break; |
| 2685 | |
| 2686 | case TLIST: |
| 2687 | (void) execute(t->left, pin, pout, 0); |
| 2688 | rv = execute(t->right, pin, pout, 0); |
| 2689 | break; |
| 2690 | |
| 2691 | case TASYNC: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2692 | { |
| 2693 | int hinteractive = interactive; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2694 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2695 | DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n")); |
| 2696 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2697 | i = vfork(); |
| 2698 | if (i != 0) { |
| 2699 | interactive = hinteractive; |
| 2700 | if (i != -1) { |
| 2701 | setval(lookup("!"), putn(i)); |
| 2702 | if (pin != NULL) |
| 2703 | closepipe(pin); |
| 2704 | if (interactive) { |
| 2705 | prs(putn(i)); |
| 2706 | prs("\n"); |
| 2707 | } |
| 2708 | } else |
| 2709 | rv = -1; |
| 2710 | setstatus(rv); |
| 2711 | } else { |
| 2712 | signal(SIGINT, SIG_IGN); |
| 2713 | signal(SIGQUIT, SIG_IGN); |
| 2714 | if (interactive) |
| 2715 | signal(SIGTERM, SIG_DFL); |
| 2716 | interactive = 0; |
| 2717 | if (pin == NULL) { |
| 2718 | close(0); |
"Vladimir N. Oleynik" | 6c35c7c | 2005-10-12 15:34:25 +0000 | [diff] [blame] | 2719 | open(bb_dev_null, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2720 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2721 | _exit(execute(t->left, pin, pout, FEXEC)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2722 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2723 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2724 | break; |
| 2725 | |
| 2726 | case TOR: |
| 2727 | case TAND: |
| 2728 | rv = execute(t->left, pin, pout, 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2729 | if ((t1 = t->right) != NULL && (rv == 0) == (t->type == TAND)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2730 | rv = execute(t1, pin, pout, 0); |
| 2731 | break; |
| 2732 | |
| 2733 | case TFOR: |
| 2734 | if (wp == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2735 | wp = dolv + 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2736 | if ((i = dolc) < 0) |
| 2737 | i = 0; |
| 2738 | } else { |
| 2739 | i = -1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2740 | while (*wp++ != NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2741 | } |
| 2742 | vp = lookup(t->str); |
| 2743 | while (setjmp(bc.brkpt)) |
| 2744 | if (isbreak) |
| 2745 | goto broken; |
| 2746 | brkset(&bc); |
| 2747 | for (t1 = t->left; i-- && *wp != NULL;) { |
| 2748 | setval(vp, *wp++); |
| 2749 | rv = execute(t1, pin, pout, 0); |
| 2750 | } |
| 2751 | brklist = brklist->nextlev; |
| 2752 | break; |
| 2753 | |
| 2754 | case TWHILE: |
| 2755 | case TUNTIL: |
| 2756 | while (setjmp(bc.brkpt)) |
| 2757 | if (isbreak) |
| 2758 | goto broken; |
| 2759 | brkset(&bc); |
| 2760 | t1 = t->left; |
| 2761 | while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE)) |
| 2762 | rv = execute(t->right, pin, pout, 0); |
| 2763 | brklist = brklist->nextlev; |
| 2764 | break; |
| 2765 | |
| 2766 | case TIF: |
| 2767 | case TELIF: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2768 | if (t->right != NULL) { |
| 2769 | rv = !execute(t->left, pin, pout, 0) ? |
| 2770 | execute(t->right->left, pin, pout, 0) : |
| 2771 | execute(t->right->right, pin, pout, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2772 | } |
| 2773 | break; |
| 2774 | |
| 2775 | case TCASE: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2776 | if ((cp = evalstr(t->str, DOSUB | DOTRIM)) == 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2777 | cp = ""; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2778 | |
| 2779 | DBGPRINTF7(("EXECUTE: TCASE, t->str is %s, cp is %s\n", |
| 2780 | ((t->str == NULL) ? "NULL" : t->str), |
| 2781 | ((cp == NULL) ? "NULL" : cp))); |
| 2782 | |
| 2783 | if ((t1 = findcase(t->left, cp)) != NULL) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2784 | DBGPRINTF7(("EXECUTE: TCASE, calling execute(t=%p, t1=%p)...\n", t, t1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2785 | rv = execute(t1, pin, pout, 0); |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2786 | DBGPRINTF7(("EXECUTE: TCASE, back from execute(t=%p, t1=%p)...\n", t, t1)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2787 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2788 | break; |
| 2789 | |
| 2790 | case TBRACE: |
| 2791 | /* |
| 2792 | if (iopp = t->ioact) |
| 2793 | while (*iopp) |
| 2794 | if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) { |
| 2795 | rv = -1; |
| 2796 | break; |
| 2797 | } |
| 2798 | */ |
| 2799 | if (rv >= 0 && (t1 = t->left)) |
| 2800 | rv = execute(t1, pin, pout, 0); |
| 2801 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2802 | |
| 2803 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2804 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2805 | broken: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2806 | t->words = wp2; |
| 2807 | isbreak = 0; |
| 2808 | freehere(areanum); |
| 2809 | freearea(areanum); |
| 2810 | areanum = a; |
| 2811 | if (interactive && intr) { |
| 2812 | closeall(); |
| 2813 | fail(); |
| 2814 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2815 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2816 | if ((i = trapset) != 0) { |
| 2817 | trapset = 0; |
| 2818 | runtrap(i); |
| 2819 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2820 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2821 | DBGPRINTF(("EXECUTE: returning from t=%p, rv=%d\n", t, rv)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2822 | return (rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | static int |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2826 | 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] | 2827 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2828 | pid_t newpid; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2829 | int i, rv; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2830 | int (*shcom) (struct op *) = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2831 | REGISTER int f; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2832 | char *cp = NULL; |
| 2833 | struct ioword **iopp; |
| 2834 | int resetsig; |
| 2835 | char **owp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2836 | int forked = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2837 | |
| 2838 | int *hpin = pin; |
| 2839 | int *hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2840 | char *hwp; |
| 2841 | int hinteractive; |
| 2842 | int hintr; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2843 | struct brkcon *hbrklist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2844 | int hexecflg; |
| 2845 | |
| 2846 | #if __GNUC__ |
| 2847 | /* Avoid longjmp clobbering */ |
| 2848 | (void) &pin; |
| 2849 | (void) &pout; |
| 2850 | (void) ℘ |
| 2851 | (void) &shcom; |
| 2852 | (void) &cp; |
| 2853 | (void) &resetsig; |
| 2854 | (void) &owp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2855 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2856 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2857 | DBGPRINTF(("FORKEXEC: t=%p, pin %p, pout %p, act %d\n", t, pin, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2858 | pout, act)); |
| 2859 | DBGPRINTF7(("FORKEXEC: t->words is %s\n", |
| 2860 | ((t->words == NULL) ? "NULL" : t->words[0]))); |
| 2861 | |
| 2862 | /* Hard to know how many words there are, be careful of garbage pointer values */ |
| 2863 | /* They are likely to cause "PCI bus fault" errors */ |
| 2864 | #if 0 |
| 2865 | DBGPRINTF7(("FORKEXEC: t->words is %s, t->words[1] is %s\n", |
| 2866 | ((t->words == NULL) ? "NULL" : t->words[0]), |
| 2867 | ((t->words == NULL) ? "NULL" : t->words[1]))); |
| 2868 | DBGPRINTF7(("FORKEXEC: wp is %s, wp[1] is %s\n", |
| 2869 | ((wp == NULL) ? "NULL" : wp[0]), |
| 2870 | ((wp[1] == NULL) ? "NULL" : wp[1]))); |
| 2871 | DBGPRINTF7(("FORKEXEC: wp2 is %s, wp[3] is %s\n", |
| 2872 | ((wp[2] == NULL) ? "NULL" : wp[2]), |
| 2873 | ((wp[3] == NULL) ? "NULL" : wp[3]))); |
| 2874 | #endif |
| 2875 | |
| 2876 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2877 | owp = wp; |
| 2878 | resetsig = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2879 | rv = -1; /* system-detected error */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2880 | if (t->type == TCOM) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2881 | while ((cp = *wp++) != NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2882 | cp = *wp; |
| 2883 | |
| 2884 | /* strip all initial assignments */ |
| 2885 | /* not correct wrt PATH=yyy command etc */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2886 | if (flag['x']) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2887 | DBGPRINTF9(("FORKEXEC: echo'ing, cp=%p, wp=%p, owp=%p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2888 | cp, wp, owp)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2889 | echo(cp ? wp : owp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2890 | } |
| 2891 | #if 0 |
| 2892 | DBGPRINTF9(("FORKEXEC: t->words is %s, t->words[1] is %s\n", |
| 2893 | ((t->words == NULL) ? "NULL" : t->words[0]), |
| 2894 | ((t->words == NULL) ? "NULL" : t->words[1]))); |
| 2895 | DBGPRINTF9(("FORKEXEC: wp is %s, wp[1] is %s\n", |
| 2896 | ((wp == NULL) ? "NULL" : wp[0]), |
| 2897 | ((wp == NULL) ? "NULL" : wp[1]))); |
| 2898 | #endif |
| 2899 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2900 | if (cp == NULL && t->ioact == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2901 | while ((cp = *owp++) != NULL && assign(cp, COPYV)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2902 | DBGPRINTF(("FORKEXEC: returning setstatus()\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2903 | return (setstatus(0)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2904 | } else if (cp != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2905 | shcom = inbuilt(cp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2906 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2907 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2908 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2909 | t->words = wp; |
| 2910 | f = act; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2911 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2912 | #if 0 |
| 2913 | DBGPRINTF3(("FORKEXEC: t->words is %s, t->words[1] is %s\n", |
| 2914 | ((t->words == NULL) ? "NULL" : t->words[0]), |
| 2915 | ((t->words == NULL) ? "NULL" : t->words[1]))); |
| 2916 | #endif |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2917 | DBGPRINTF(("FORKEXEC: shcom %p, f&FEXEC 0x%x, owp %p\n", shcom, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2918 | f & FEXEC, owp)); |
| 2919 | |
| 2920 | if (shcom == NULL && (f & FEXEC) == 0) { |
| 2921 | /* Save values in case the child process alters them */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2922 | hpin = pin; |
| 2923 | hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2924 | hwp = *wp; |
| 2925 | hinteractive = interactive; |
| 2926 | hintr = intr; |
| 2927 | hbrklist = brklist; |
| 2928 | hexecflg = execflg; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2929 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2930 | DBGPRINTF3(("FORKEXEC: calling vfork()...\n")); |
| 2931 | |
| 2932 | newpid = vfork(); |
| 2933 | |
| 2934 | if (newpid == -1) { |
| 2935 | DBGPRINTF(("FORKEXEC: ERROR, unable to vfork()!\n")); |
| 2936 | return (-1); |
| 2937 | } |
| 2938 | |
| 2939 | |
| 2940 | if (newpid > 0) { /* Parent */ |
| 2941 | |
| 2942 | /* Restore values */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2943 | pin = hpin; |
| 2944 | pout = hpout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2945 | *wp = hwp; |
| 2946 | interactive = hinteractive; |
| 2947 | intr = hintr; |
| 2948 | brklist = hbrklist; |
| 2949 | execflg = hexecflg; |
| 2950 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2951 | /* moved up |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2952 | if (i == -1) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2953 | return(rv); |
| 2954 | */ |
| 2955 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2956 | if (pin != NULL) |
| 2957 | closepipe(pin); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2958 | |
| 2959 | return (pout == NULL ? setstatus(waitfor(newpid, 0)) : 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2962 | /* Must be the child process, pid should be 0 */ |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2963 | DBGPRINTF(("FORKEXEC: child process, shcom=%p\n", shcom)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2964 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2965 | if (interactive) { |
| 2966 | signal(SIGINT, SIG_IGN); |
| 2967 | signal(SIGQUIT, SIG_IGN); |
| 2968 | resetsig = 1; |
| 2969 | } |
| 2970 | interactive = 0; |
| 2971 | intr = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2972 | forked = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2973 | brklist = 0; |
| 2974 | execflg = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2975 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2976 | |
| 2977 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2978 | if (owp != NULL) |
| 2979 | while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
| 2980 | if (shcom == NULL) |
| 2981 | export(lookup(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2982 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2983 | #ifdef COMPIPE |
| 2984 | if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) { |
| 2985 | err("piping to/from shell builtins not yet done"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2986 | if (forked) |
| 2987 | _exit(-1); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2988 | return (-1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2989 | } |
| 2990 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2991 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2992 | if (pin != NULL) { |
| 2993 | dup2(pin[0], 0); |
| 2994 | closepipe(pin); |
| 2995 | } |
| 2996 | if (pout != NULL) { |
| 2997 | dup2(pout[1], 1); |
| 2998 | closepipe(pout); |
| 2999 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3000 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3001 | if ((iopp = t->ioact) != NULL) { |
| 3002 | if (shcom != NULL && shcom != doexec) { |
| 3003 | prs(cp); |
| 3004 | err(": cannot redirect shell command"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3005 | if (forked) |
| 3006 | _exit(-1); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3007 | return (-1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3008 | } |
| 3009 | while (*iopp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3010 | if (iosetup(*iopp++, pin != NULL, pout != NULL)) { |
| 3011 | if (forked) |
| 3012 | _exit(rv); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3013 | return (rv); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3014 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3015 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3016 | |
| 3017 | if (shcom) { |
| 3018 | i = setstatus((*shcom) (t)); |
| 3019 | if (forked) |
| 3020 | _exit(i); |
| 3021 | DBGPRINTF(("FORKEXEC: returning i=%d\n", i)); |
| 3022 | return (i); |
| 3023 | } |
| 3024 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3025 | /* should use FIOCEXCL */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3026 | for (i = FDBASE; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3027 | close(i); |
| 3028 | if (resetsig) { |
| 3029 | signal(SIGINT, SIG_DFL); |
| 3030 | signal(SIGQUIT, SIG_DFL); |
Mike Frysinger | 4e512f1 | 2006-05-05 18:38:07 +0000 | [diff] [blame] | 3031 | } else { |
| 3032 | /* put non-interactive processes into a different process group. |
| 3033 | * we don't support jobs, but this is at least sane: see Bug 659 */ |
Bernhard Reutner-Fischer | b180e5a | 2006-05-26 14:24:56 +0000 | [diff] [blame] | 3034 | bb_setpgrp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3035 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3036 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3037 | if (t->type == TPAREN) |
| 3038 | _exit(execute(t->left, NOPIPE, NOPIPE, FEXEC)); |
| 3039 | if (wp[0] == NULL) |
| 3040 | _exit(0); |
| 3041 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3042 | cp = rexecve(wp[0], wp, makenv(0, NULL)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3043 | prs(wp[0]); |
| 3044 | prs(": "); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3045 | err(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3046 | if (!execflg) |
| 3047 | trap[0] = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3048 | |
| 3049 | DBGPRINTF(("FORKEXEC: calling leave(), pid=%d\n", newpid)); |
| 3050 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3051 | leave(); |
| 3052 | /* NOTREACHED */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3053 | _exit(1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | /* |
| 3057 | * 0< 1> are ignored as required |
| 3058 | * within pipelines. |
| 3059 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3060 | static int iosetup(iop, pipein, pipeout) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3061 | REGISTER struct ioword *iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3062 | int pipein, pipeout; |
| 3063 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3064 | REGISTER int u = -1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3065 | char *cp = NULL, *msg; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3066 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3067 | DBGPRINTF(("IOSETUP: iop %p, pipein %i, pipeout %i\n", iop, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3068 | pipein, pipeout)); |
| 3069 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3070 | if (iop->io_unit == IODEFAULT) /* take default */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3071 | iop->io_unit = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3072 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3073 | if (pipein && iop->io_unit == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3074 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3075 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3076 | if (pipeout && iop->io_unit == 1) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3077 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3078 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3079 | msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3080 | if ((iop->io_flag & IOHERE) == 0) { |
| 3081 | cp = iop->io_name; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3082 | if ((cp = evalstr(cp, DOSUB | DOTRIM)) == NULL) |
| 3083 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3084 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3085 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3086 | if (iop->io_flag & IODUP) { |
| 3087 | if (cp[1] || (!isdigit(*cp) && *cp != '-')) { |
| 3088 | prs(cp); |
| 3089 | err(": illegal >& argument"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3090 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3091 | } |
| 3092 | if (*cp == '-') |
| 3093 | iop->io_flag = IOCLOSE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3094 | iop->io_flag &= ~(IOREAD | IOWRITE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3095 | } |
| 3096 | switch (iop->io_flag) { |
| 3097 | case IOREAD: |
| 3098 | u = open(cp, 0); |
| 3099 | break; |
| 3100 | |
| 3101 | case IOHERE: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3102 | case IOHERE | IOXHERE: |
| 3103 | u = herein(iop->io_name, iop->io_flag & IOXHERE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3104 | cp = "here file"; |
| 3105 | break; |
| 3106 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3107 | case IOWRITE | IOCAT: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3108 | if ((u = open(cp, 1)) >= 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3109 | lseek(u, (long) 0, 2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3110 | break; |
| 3111 | } |
| 3112 | case IOWRITE: |
| 3113 | u = creat(cp, 0666); |
| 3114 | break; |
| 3115 | |
| 3116 | case IODUP: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3117 | u = dup2(*cp - '0', iop->io_unit); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3118 | break; |
| 3119 | |
| 3120 | case IOCLOSE: |
| 3121 | close(iop->io_unit); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3122 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3123 | } |
| 3124 | if (u < 0) { |
| 3125 | prs(cp); |
| 3126 | prs(": cannot "); |
| 3127 | warn(msg); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3128 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3129 | } else { |
| 3130 | if (u != iop->io_unit) { |
| 3131 | dup2(u, iop->io_unit); |
| 3132 | close(u); |
| 3133 | } |
| 3134 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3135 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3138 | static void echo(wp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3139 | REGISTER char **wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3140 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3141 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3142 | |
| 3143 | prs("+"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3144 | for (i = 0; wp[i]; i++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3145 | if (i) |
| 3146 | prs(" "); |
| 3147 | prs(wp[i]); |
| 3148 | } |
| 3149 | prs("\n"); |
| 3150 | } |
| 3151 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3152 | static struct op **find1case(t, w) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3153 | struct op *t; |
| 3154 | char *w; |
| 3155 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3156 | REGISTER struct op *t1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3157 | struct op **tp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3158 | REGISTER char **wp, *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3159 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3160 | |
| 3161 | if (t == NULL) { |
| 3162 | DBGPRINTF3(("FIND1CASE: enter, t==NULL, returning.\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3163 | return ((struct op **) NULL); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | DBGPRINTF3(("FIND1CASE: enter, t->type=%d (%s)\n", t->type, |
| 3167 | T_CMD_NAMES[t->type])); |
| 3168 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3169 | if (t->type == TLIST) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3170 | if ((tp = find1case(t->left, w)) != NULL) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3171 | DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=%p\n", tp)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3172 | return (tp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3173 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3174 | t1 = t->right; /* TPAT */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3175 | } else |
| 3176 | t1 = t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3177 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3178 | for (wp = t1->words; *wp;) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3179 | if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp)) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3180 | DBGPRINTF3(("FIND1CASE: returning &t1->left= %p.\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3181 | &t1->left)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3182 | return (&t1->left); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | DBGPRINTF(("FIND1CASE: returning NULL\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3186 | return ((struct op **) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3189 | static struct op *findcase(t, w) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3190 | struct op *t; |
| 3191 | char *w; |
| 3192 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3193 | REGISTER struct op **tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3194 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3195 | return ((tp = find1case(t, w)) != NULL ? *tp : (struct op *) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | /* |
| 3199 | * Enter a new loop level (marked for break/continue). |
| 3200 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3201 | static void brkset(bc) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3202 | struct brkcon *bc; |
| 3203 | { |
| 3204 | bc->nextlev = brklist; |
| 3205 | brklist = bc; |
| 3206 | } |
| 3207 | |
| 3208 | /* |
| 3209 | * Wait for the last process created. |
| 3210 | * Print a message for each process found |
| 3211 | * that was killed by a signal. |
| 3212 | * Ignore interrupt signals while waiting |
| 3213 | * unless `canintr' is true. |
| 3214 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3215 | static int waitfor(lastpid, canintr) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3216 | REGISTER int lastpid; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3217 | int canintr; |
| 3218 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3219 | REGISTER int pid, rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3220 | int s; |
| 3221 | int oheedint = heedint; |
| 3222 | |
| 3223 | heedint = 0; |
| 3224 | rv = 0; |
| 3225 | do { |
| 3226 | pid = wait(&s); |
| 3227 | if (pid == -1) { |
| 3228 | if (errno != EINTR || canintr) |
| 3229 | break; |
| 3230 | } else { |
| 3231 | if ((rv = WAITSIG(s)) != 0) { |
| 3232 | if (rv < NSIGNAL) { |
| 3233 | if (signame[rv] != NULL) { |
| 3234 | if (pid != lastpid) { |
| 3235 | prn(pid); |
| 3236 | prs(": "); |
| 3237 | } |
| 3238 | prs(signame[rv]); |
| 3239 | } |
| 3240 | } else { |
| 3241 | if (pid != lastpid) { |
| 3242 | prn(pid); |
| 3243 | prs(": "); |
| 3244 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3245 | prs("Signal "); |
| 3246 | prn(rv); |
| 3247 | prs(" "); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3248 | } |
| 3249 | if (WAITCORE(s)) |
| 3250 | prs(" - core dumped"); |
| 3251 | if (rv >= NSIGNAL || signame[rv]) |
| 3252 | prs("\n"); |
| 3253 | rv = -1; |
| 3254 | } else |
| 3255 | rv = WAITVAL(s); |
| 3256 | } |
| 3257 | } while (pid != lastpid); |
| 3258 | heedint = oheedint; |
| 3259 | if (intr) { |
| 3260 | if (interactive) { |
| 3261 | if (canintr) |
| 3262 | intr = 0; |
| 3263 | } else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3264 | if (exstat == 0) |
| 3265 | exstat = rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3266 | onintr(0); |
| 3267 | } |
| 3268 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3269 | return (rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3272 | static int setstatus(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3273 | REGISTER int s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3274 | { |
| 3275 | exstat = s; |
| 3276 | setval(lookup("?"), putn(s)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3277 | return (s); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | /* |
| 3281 | * PATH-searching interface to execve. |
| 3282 | * If getenv("PATH") were kept up-to-date, |
| 3283 | * execvp might be used. |
| 3284 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3285 | static char *rexecve(c, v, envp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3286 | char *c, **v, **envp; |
| 3287 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3288 | REGISTER int i; |
| 3289 | REGISTER char *sp, *tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3290 | int eacces = 0, asis = 0; |
| 3291 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3292 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3293 | char *name = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3294 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3295 | optind = 1; |
| 3296 | if (find_applet_by_name(name)) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3297 | /* We have to exec here since we vforked. Running |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3298 | * run_applet_by_name() won't work and bad things |
| 3299 | * will happen. */ |
| 3300 | execve("/proc/self/exe", v, envp); |
| 3301 | execve("busybox", v, envp); |
| 3302 | } |
| 3303 | #endif |
| 3304 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3305 | DBGPRINTF(("REXECVE: c=%p, v=%p, envp=%p\n", c, v, envp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3306 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3307 | sp = any('/', c) ? "" : path->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3308 | asis = *sp == '\0'; |
| 3309 | while (asis || *sp != '\0') { |
| 3310 | asis = 0; |
| 3311 | tp = e.linep; |
| 3312 | for (; *sp != '\0'; tp++) |
| 3313 | if ((*tp = *sp++) == ':') { |
| 3314 | asis = *sp == '\0'; |
| 3315 | break; |
| 3316 | } |
| 3317 | if (tp != e.linep) |
| 3318 | *tp++ = '/'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3319 | for (i = 0; (*tp++ = c[i++]) != '\0';); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3320 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3321 | DBGPRINTF3(("REXECVE: e.linep is %s\n", e.linep)); |
| 3322 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3323 | execve(e.linep, v, envp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3324 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3325 | switch (errno) { |
| 3326 | case ENOEXEC: |
| 3327 | *v = e.linep; |
| 3328 | tp = *--v; |
| 3329 | *v = e.linep; |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 3330 | execve(DEFAULT_SHELL, v, envp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3331 | *v = tp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3332 | return ("no Shell"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3333 | |
| 3334 | case ENOMEM: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3335 | return ((char *) bb_msg_memory_exhausted); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3336 | |
| 3337 | case E2BIG: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3338 | return ("argument list too long"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3339 | |
| 3340 | case EACCES: |
| 3341 | eacces++; |
| 3342 | break; |
| 3343 | } |
| 3344 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3345 | return (errno == ENOENT ? "not found" : "cannot execute"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3346 | } |
| 3347 | |
| 3348 | /* |
| 3349 | * Run the command produced by generator `f' |
| 3350 | * applied to stream `arg'. |
| 3351 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3352 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3353 | { |
| 3354 | struct op *otree; |
| 3355 | struct wdblock *swdlist; |
| 3356 | struct wdblock *siolist; |
| 3357 | jmp_buf ev, rt; |
| 3358 | xint *ofail; |
| 3359 | int rv; |
| 3360 | |
| 3361 | #if __GNUC__ |
| 3362 | /* Avoid longjmp clobbering */ |
| 3363 | (void) &rv; |
| 3364 | #endif |
| 3365 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3366 | DBGPRINTF(("RUN: enter, areanum %d, outtree %p, failpt %p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3367 | areanum, outtree, failpt)); |
| 3368 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3369 | areanum++; |
| 3370 | swdlist = wdlist; |
| 3371 | siolist = iolist; |
| 3372 | otree = outtree; |
| 3373 | ofail = failpt; |
| 3374 | rv = -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3375 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3376 | if (newenv(setjmp(errpt = ev)) == 0) { |
| 3377 | wdlist = 0; |
| 3378 | iolist = 0; |
| 3379 | pushio(argp, f); |
| 3380 | e.iobase = e.iop; |
| 3381 | yynerrs = 0; |
| 3382 | if (setjmp(failpt = rt) == 0 && yyparse() == 0) |
| 3383 | rv = execute(outtree, NOPIPE, NOPIPE, 0); |
| 3384 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3385 | } else { |
| 3386 | DBGPRINTF(("RUN: error from newenv()!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3387 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3388 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3389 | wdlist = swdlist; |
| 3390 | iolist = siolist; |
| 3391 | failpt = ofail; |
| 3392 | outtree = otree; |
| 3393 | freearea(areanum--); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3394 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3395 | return (rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | /* -------- do.c -------- */ |
| 3399 | |
| 3400 | /* |
| 3401 | * built-in commands: doX |
| 3402 | */ |
| 3403 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3404 | static int dohelp(struct op *t) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3405 | { |
| 3406 | int col; |
| 3407 | const struct builtincmd *x; |
| 3408 | |
| 3409 | printf("\nBuilt-in commands:\n"); |
| 3410 | printf("-------------------\n"); |
| 3411 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3412 | for (col = 0, x = builtincmds; x->builtinfunc != NULL; x++) { |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3413 | if (!x->name) |
| 3414 | continue; |
| 3415 | col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name); |
| 3416 | if (col > 60) { |
| 3417 | printf("\n"); |
| 3418 | col = 0; |
| 3419 | } |
| 3420 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3421 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3422 | { |
| 3423 | int i; |
| 3424 | const struct BB_applet *applet; |
| 3425 | extern const struct BB_applet applets[]; |
| 3426 | extern const size_t NUM_APPLETS; |
| 3427 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3428 | for (i = 0, applet = applets; i < NUM_APPLETS; applet++, i++) { |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3429 | if (!applet->name) |
| 3430 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3431 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3432 | col += printf("%s%s", ((col == 0) ? "\t" : " "), applet->name); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3433 | if (col > 60) { |
| 3434 | printf("\n"); |
| 3435 | col = 0; |
| 3436 | } |
| 3437 | } |
| 3438 | } |
| 3439 | #endif |
| 3440 | printf("\n\n"); |
| 3441 | return EXIT_SUCCESS; |
| 3442 | } |
| 3443 | |
| 3444 | |
| 3445 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3446 | static int dolabel(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3447 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3448 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3451 | static int dochdir(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3452 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3453 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3454 | REGISTER char *cp, *er; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3455 | |
| 3456 | if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL) |
| 3457 | er = ": no home directory"; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3458 | else if (chdir(cp) < 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3459 | er = ": bad directory"; |
| 3460 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3461 | return (0); |
| 3462 | prs(cp != NULL ? cp : "cd"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3463 | err(er); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3464 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3467 | static int doshift(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3468 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3469 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3470 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3471 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3472 | n = t->words[1] ? getn(t->words[1]) : 1; |
| 3473 | if (dolc < n) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3474 | err("nothing to shift"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3475 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3476 | } |
| 3477 | dolv[n] = dolv[0]; |
| 3478 | dolv += n; |
| 3479 | dolc -= n; |
| 3480 | setval(lookup("#"), putn(dolc)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3481 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | /* |
| 3485 | * execute login and newgrp directly |
| 3486 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3487 | static int dologin(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3488 | struct op *t; |
| 3489 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3490 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3491 | |
| 3492 | if (interactive) { |
| 3493 | signal(SIGINT, SIG_DFL); |
| 3494 | signal(SIGQUIT, SIG_DFL); |
| 3495 | } |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3496 | cp = rexecve(t->words[0], t->words, makenv(0, NULL)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3497 | prs(t->words[0]); |
| 3498 | prs(": "); |
| 3499 | err(cp); |
| 3500 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3503 | static int doumask(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3504 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3505 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3506 | REGISTER int i, n; |
| 3507 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3508 | |
| 3509 | if ((cp = t->words[1]) == NULL) { |
| 3510 | i = umask(0); |
| 3511 | umask(i); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3512 | for (n = 3 * 4; (n -= 3) >= 0;) |
| 3513 | putc('0' + ((i >> n) & 07), stderr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3514 | putc('\n', stderr); |
| 3515 | } else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3516 | for (n = 0; *cp >= '0' && *cp <= '9'; cp++) |
| 3517 | n = n * 8 + (*cp - '0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3518 | umask(n); |
| 3519 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3520 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3523 | static int doexec(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3524 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3525 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3526 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3527 | jmp_buf ex; |
| 3528 | xint *ofail; |
| 3529 | |
| 3530 | t->ioact = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3531 | for (i = 0; (t->words[i] = t->words[i + 1]) != NULL; i++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3532 | if (i == 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 | execflg = 1; |
| 3535 | ofail = failpt; |
| 3536 | if (setjmp(failpt = ex) == 0) |
| 3537 | execute(t, NOPIPE, NOPIPE, FEXEC); |
| 3538 | failpt = ofail; |
| 3539 | execflg = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3540 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3543 | static int dodot(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3544 | struct op *t; |
| 3545 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3546 | REGISTER int i; |
| 3547 | REGISTER char *sp, *tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3548 | char *cp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3549 | int maltmp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3550 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3551 | DBGPRINTF(("DODOT: enter, t=%p, tleft %p, tright %p, e.linep is %s\n", t, t->left, t->right, ((e.linep == NULL) ? "NULL" : e.linep))); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3552 | |
| 3553 | if ((cp = t->words[1]) == NULL) { |
| 3554 | DBGPRINTF(("DODOT: bad args, ret 0\n")); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3555 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3556 | } else { |
| 3557 | DBGPRINTF(("DODOT: cp is %s\n", cp)); |
| 3558 | } |
| 3559 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3560 | sp = any('/', cp) ? ":" : path->value; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3561 | |
| 3562 | DBGPRINTF(("DODOT: sp is %s, e.linep is %s\n", |
| 3563 | ((sp == NULL) ? "NULL" : sp), |
| 3564 | ((e.linep == NULL) ? "NULL" : e.linep))); |
| 3565 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3566 | while (*sp) { |
| 3567 | tp = e.linep; |
| 3568 | while (*sp && (*tp = *sp++) != ':') |
| 3569 | tp++; |
| 3570 | if (tp != e.linep) |
| 3571 | *tp++ = '/'; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3572 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3573 | for (i = 0; (*tp++ = cp[i++]) != '\0';); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3574 | |
| 3575 | /* Original code */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3576 | if ((i = open(e.linep, 0)) >= 0) { |
| 3577 | exstat = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3578 | maltmp = remap(i); |
| 3579 | DBGPRINTF(("DODOT: remap=%d, exstat=%d, e.iofd %d, i %d, e.linep is %s\n", maltmp, exstat, e.iofd, i, e.linep)); |
| 3580 | |
| 3581 | next(maltmp); /* Basically a PUSHIO */ |
| 3582 | |
| 3583 | DBGPRINTF(("DODOT: returning exstat=%d\n", exstat)); |
| 3584 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3585 | return (exstat); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3586 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3587 | |
| 3588 | } /* While */ |
| 3589 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3590 | prs(cp); |
| 3591 | err(": not found"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3592 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3593 | return (-1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3596 | static int dowait(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3597 | struct op *t; |
| 3598 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3599 | REGISTER int i; |
| 3600 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3601 | |
| 3602 | if ((cp = t->words[1]) != NULL) { |
| 3603 | i = getn(cp); |
| 3604 | if (i == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3605 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3606 | } else |
| 3607 | i = -1; |
| 3608 | setstatus(waitfor(i, 1)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3609 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3612 | static int doread(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3613 | struct op *t; |
| 3614 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3615 | REGISTER char *cp, **wp; |
| 3616 | REGISTER int nb = 0; |
| 3617 | REGISTER int nl = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3618 | |
| 3619 | if (t->words[1] == NULL) { |
| 3620 | err("Usage: read name ..."); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3621 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3622 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3623 | for (wp = t->words + 1; *wp; wp++) { |
| 3624 | for (cp = e.linep; !nl && cp < elinep - 1; cp++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3625 | if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) || |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3626 | (nl = (*cp == '\n')) || (wp[1] && any(*cp, ifs->value))) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3627 | break; |
| 3628 | *cp = 0; |
| 3629 | if (nb <= 0) |
| 3630 | break; |
| 3631 | setval(lookup(*wp), e.linep); |
| 3632 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3633 | return (nb <= 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3636 | static int doeval(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3637 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3638 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3639 | return (RUN(awordlist, t->words + 1, wdchar)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3642 | static int dotrap(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3643 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3644 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3645 | REGISTER int n, i; |
| 3646 | REGISTER int resetsig; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3647 | |
| 3648 | if (t->words[1] == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3649 | for (i = 0; i <= _NSIG; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3650 | if (trap[i]) { |
| 3651 | prn(i); |
| 3652 | prs(": "); |
| 3653 | prs(trap[i]); |
| 3654 | prs("\n"); |
| 3655 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3656 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3657 | } |
| 3658 | resetsig = isdigit(*t->words[1]); |
| 3659 | for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) { |
| 3660 | n = getsig(t->words[i]); |
| 3661 | freecell(trap[n]); |
| 3662 | trap[n] = 0; |
| 3663 | if (!resetsig) { |
| 3664 | if (*t->words[1] != '\0') { |
| 3665 | trap[n] = strsave(t->words[1], 0); |
| 3666 | setsig(n, sig); |
| 3667 | } else |
| 3668 | setsig(n, SIG_IGN); |
| 3669 | } else { |
| 3670 | if (interactive) |
| 3671 | if (n == SIGINT) |
| 3672 | setsig(n, onintr); |
| 3673 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3674 | setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3675 | else |
| 3676 | setsig(n, SIG_DFL); |
| 3677 | } |
| 3678 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3679 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3682 | static int getsig(s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3683 | char *s; |
| 3684 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3685 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3686 | |
| 3687 | if ((n = getn(s)) < 0 || n > _NSIG) { |
| 3688 | err("trap: bad signal number"); |
| 3689 | n = 0; |
| 3690 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3691 | return (n); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3694 | static void setsig(REGISTER int n, sighandler_t f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3695 | { |
| 3696 | if (n == 0) |
| 3697 | return; |
| 3698 | if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) { |
| 3699 | ourtrap[n] = 1; |
| 3700 | signal(n, f); |
| 3701 | } |
| 3702 | } |
| 3703 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3704 | static int getn(as) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3705 | char *as; |
| 3706 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3707 | REGISTER char *s; |
| 3708 | REGISTER int n, m; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3709 | |
| 3710 | s = as; |
| 3711 | m = 1; |
| 3712 | if (*s == '-') { |
| 3713 | m = -1; |
| 3714 | s++; |
| 3715 | } |
| 3716 | for (n = 0; isdigit(*s); s++) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3717 | n = (n * 10) + (*s - '0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3718 | if (*s) { |
| 3719 | prs(as); |
| 3720 | err(": bad number"); |
| 3721 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3722 | return (n * m); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3725 | static int dobreak(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3726 | struct op *t; |
| 3727 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3728 | return (brkcontin(t->words[1], 1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3731 | static int docontinue(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3732 | struct op *t; |
| 3733 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3734 | return (brkcontin(t->words[1], 0)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3737 | static int brkcontin(cp, val) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3738 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3739 | int val; |
| 3740 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3741 | REGISTER struct brkcon *bc; |
| 3742 | REGISTER int nl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3743 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3744 | nl = cp == NULL ? 1 : getn(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3745 | if (nl <= 0) |
| 3746 | nl = 999; |
| 3747 | do { |
| 3748 | if ((bc = brklist) == NULL) |
| 3749 | break; |
| 3750 | brklist = bc->nextlev; |
| 3751 | } while (--nl); |
| 3752 | if (nl) { |
| 3753 | err("bad break/continue level"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3754 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3755 | } |
| 3756 | isbreak = val; |
| 3757 | longjmp(bc->brkpt, 1); |
| 3758 | /* NOTREACHED */ |
| 3759 | } |
| 3760 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3761 | static int doexit(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3762 | struct op *t; |
| 3763 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3764 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3765 | |
| 3766 | execflg = 0; |
| 3767 | if ((cp = t->words[1]) != NULL) |
| 3768 | setstatus(getn(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3769 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3770 | DBGPRINTF(("DOEXIT: calling leave(), t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3771 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3772 | leave(); |
| 3773 | /* NOTREACHED */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 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 doexport(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, export, EXPORT); |
| 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 int doreadonly(t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3785 | struct op *t; |
| 3786 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3787 | rdexp(t->words + 1, ronly, RONLY); |
| 3788 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3791 | static void rdexp(char **wp, void (*f) (struct var *), int key) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3792 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3793 | DBGPRINTF6(("RDEXP: enter, wp=%p, func=%p, key=%d\n", wp, f, key)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3794 | DBGPRINTF6(("RDEXP: *wp=%s\n", *wp)); |
| 3795 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3796 | if (*wp != NULL) { |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3797 | for (; *wp != NULL; wp++) { |
| 3798 | if (isassign(*wp)) { |
| 3799 | char *cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3800 | |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3801 | assign(*wp, COPYV); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3802 | for (cp = *wp; *cp != '='; cp++); |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3803 | *cp = '\0'; |
| 3804 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3805 | if (checkname(*wp)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3806 | (*f) (lookup(*wp)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3807 | else |
| 3808 | badid(*wp); |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3809 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3810 | } else |
| 3811 | putvlist(key, 1); |
| 3812 | } |
| 3813 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3814 | static void badid(s) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3815 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3816 | { |
| 3817 | prs(s); |
| 3818 | err(": bad identifier"); |
| 3819 | } |
| 3820 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3821 | static int doset(t) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3822 | REGISTER struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3823 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3824 | REGISTER struct var *vp; |
| 3825 | REGISTER char *cp; |
| 3826 | REGISTER int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3827 | |
| 3828 | if ((cp = t->words[1]) == NULL) { |
| 3829 | for (vp = vlist; vp; vp = vp->next) |
| 3830 | varput(vp->name, 1); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3831 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3832 | } |
| 3833 | if (*cp == '-') { |
| 3834 | /* bad: t->words++; */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3835 | for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3836 | if (*++cp == 0) |
| 3837 | flag['x'] = flag['v'] = 0; |
| 3838 | else |
| 3839 | for (; *cp; cp++) |
| 3840 | switch (*cp) { |
| 3841 | case 'e': |
| 3842 | if (!interactive) |
| 3843 | flag['e']++; |
| 3844 | break; |
| 3845 | |
| 3846 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3847 | if (*cp >= 'a' && *cp <= 'z') |
| 3848 | flag[(int) *cp]++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3849 | break; |
| 3850 | } |
| 3851 | setdash(); |
| 3852 | } |
| 3853 | if (t->words[1]) { |
| 3854 | t->words[0] = dolv[0]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3855 | for (n = 1; t->words[n]; n++) |
| 3856 | setarea((char *) t->words[n], 0); |
| 3857 | dolc = n - 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3858 | dolv = t->words; |
| 3859 | setval(lookup("#"), putn(dolc)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3860 | setarea((char *) (dolv - 1), 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3861 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3862 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3863 | } |
| 3864 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3865 | static void varput(s, out) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3866 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3867 | int out; |
| 3868 | { |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3869 | if (isalnum(*s) || *s == '_') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3870 | write(out, s, strlen(s)); |
| 3871 | write(out, "\n", 1); |
| 3872 | } |
| 3873 | } |
| 3874 | |
| 3875 | |
| 3876 | /* |
| 3877 | * Copyright (c) 1999 Herbert Xu <herbert@debian.org> |
| 3878 | * This file contains code for the times builtin. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3879 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3880 | static int dotimes(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3881 | { |
| 3882 | struct tms buf; |
| 3883 | long int clk_tck = sysconf(_SC_CLK_TCK); |
| 3884 | |
| 3885 | times(&buf); |
| 3886 | printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n", |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3887 | (int) (buf.tms_utime / clk_tck / 60), |
| 3888 | ((double) buf.tms_utime) / clk_tck, |
| 3889 | (int) (buf.tms_stime / clk_tck / 60), |
| 3890 | ((double) buf.tms_stime) / clk_tck, |
| 3891 | (int) (buf.tms_cutime / clk_tck / 60), |
| 3892 | ((double) buf.tms_cutime) / clk_tck, |
| 3893 | (int) (buf.tms_cstime / clk_tck / 60), |
| 3894 | ((double) buf.tms_cstime) / clk_tck); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3895 | return 0; |
| 3896 | } |
| 3897 | |
| 3898 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3899 | static int (*inbuilt(char *s)) (struct op *) { |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3900 | const struct builtincmd *bp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3901 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3902 | for (bp = builtincmds; bp->name != NULL; bp++) |
| 3903 | if (strcmp(bp->name, s) == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3904 | return (bp->builtinfunc); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3905 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3906 | return (NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | /* -------- eval.c -------- */ |
| 3910 | |
| 3911 | /* |
| 3912 | * ${} |
| 3913 | * `command` |
| 3914 | * blank interpretation |
| 3915 | * quoting |
| 3916 | * glob |
| 3917 | */ |
| 3918 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3919 | static char **eval(char **ap, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3920 | { |
| 3921 | struct wdblock *wb; |
| 3922 | char **wp; |
| 3923 | char **wf; |
| 3924 | jmp_buf ev; |
| 3925 | |
| 3926 | #if __GNUC__ |
| 3927 | /* Avoid longjmp clobbering */ |
| 3928 | (void) ℘ |
| 3929 | (void) ≈ |
| 3930 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3931 | |
| 3932 | DBGPRINTF4(("EVAL: enter, f=%d\n", f)); |
| 3933 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3934 | wp = NULL; |
| 3935 | wb = NULL; |
| 3936 | wf = NULL; |
| 3937 | if (newenv(setjmp(errpt = ev)) == 0) { |
| 3938 | while (*ap && isassign(*ap)) |
| 3939 | expand(*ap++, &wb, f & ~DOGLOB); |
| 3940 | if (flag['k']) { |
| 3941 | for (wf = ap; *wf; wf++) { |
| 3942 | if (isassign(*wf)) |
| 3943 | expand(*wf, &wb, f & ~DOGLOB); |
| 3944 | } |
| 3945 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3946 | for (wb = addword((char *) 0, wb); *ap; ap++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3947 | if (!flag['k'] || !isassign(*ap)) |
| 3948 | expand(*ap, &wb, f & ~DOKEY); |
| 3949 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3950 | wb = addword((char *) 0, wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3951 | wp = getwords(wb); |
| 3952 | quitenv(); |
| 3953 | } else |
| 3954 | gflg = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3955 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3956 | return (gflg ? (char **) NULL : wp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
| 3959 | /* |
| 3960 | * Make the exported environment from the exported |
| 3961 | * names in the dictionary. Keyword assignments |
| 3962 | * will already have been done. |
| 3963 | */ |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3964 | static char **makenv(int all, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3965 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3966 | REGISTER struct var *vp; |
| 3967 | |
| 3968 | DBGPRINTF5(("MAKENV: enter, all=%d\n", all)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3969 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3970 | for (vp = vlist; vp; vp = vp->next) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3971 | if (all || vp->status & EXPORT) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3972 | wb = addword(vp->name, wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3973 | wb = addword((char *) 0, wb); |
| 3974 | return (getwords(wb)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3977 | static char *evalstr(cp, f) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3978 | REGISTER char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3979 | int f; |
| 3980 | { |
| 3981 | struct wdblock *wb; |
| 3982 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3983 | DBGPRINTF6(("EVALSTR: enter, cp=%p, f=%d\n", cp, f)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3984 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3985 | wb = NULL; |
| 3986 | if (expand(cp, &wb, f)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3987 | if (wb == NULL || wb->w_nword == 0 |
| 3988 | || (cp = wb->w_words[0]) == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3989 | cp = ""; |
| 3990 | DELETE(wb); |
| 3991 | } else |
| 3992 | cp = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3993 | return (cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3996 | static int expand(char *cp, REGISTER struct wdblock **wbp, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3997 | { |
| 3998 | jmp_buf ev; |
| 3999 | |
| 4000 | #if __GNUC__ |
| 4001 | /* Avoid longjmp clobbering */ |
| 4002 | (void) &cp; |
| 4003 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4004 | |
| 4005 | DBGPRINTF3(("EXPAND: enter, f=%d\n", f)); |
| 4006 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4007 | gflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4008 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4009 | if (cp == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4010 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4011 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4012 | if (!anys("$`'\"", cp) && |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4013 | !anys(ifs->value, cp) && ((f & DOGLOB) == 0 || !anys("[*?", cp))) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4014 | cp = strsave(cp, areanum); |
| 4015 | if (f & DOTRIM) |
| 4016 | unquote(cp); |
| 4017 | *wbp = addword(cp, *wbp); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4018 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4019 | } |
| 4020 | if (newenv(setjmp(errpt = ev)) == 0) { |
| 4021 | PUSHIO(aword, cp, strchar); |
| 4022 | e.iobase = e.iop; |
| 4023 | while ((cp = blank(f)) && gflg == 0) { |
| 4024 | e.linep = cp; |
| 4025 | cp = strsave(cp, areanum); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4026 | if ((f & DOGLOB) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4027 | if (f & DOTRIM) |
| 4028 | unquote(cp); |
| 4029 | *wbp = addword(cp, *wbp); |
| 4030 | } else |
| 4031 | *wbp = glob(cp, *wbp); |
| 4032 | } |
| 4033 | quitenv(); |
| 4034 | } else |
| 4035 | gflg = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4036 | return (gflg == 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | /* |
| 4040 | * Blank interpretation and quoting |
| 4041 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4042 | static char *blank(f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4043 | int f; |
| 4044 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4045 | REGISTER int c, c1; |
| 4046 | REGISTER char *sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4047 | int scanequals, foundequals; |
| 4048 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4049 | DBGPRINTF3(("BLANK: enter, f=%d\n", f)); |
| 4050 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4051 | sp = e.linep; |
| 4052 | scanequals = f & DOKEY; |
| 4053 | foundequals = 0; |
| 4054 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4055 | loop: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4056 | switch (c = subgetc('"', foundequals)) { |
| 4057 | case 0: |
| 4058 | if (sp == e.linep) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4059 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4060 | *e.linep++ = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4061 | return (sp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4062 | |
| 4063 | default: |
| 4064 | if (f & DOBLANK && any(c, ifs->value)) |
| 4065 | goto loop; |
| 4066 | break; |
| 4067 | |
| 4068 | case '"': |
| 4069 | case '\'': |
| 4070 | scanequals = 0; |
| 4071 | if (INSUB()) |
| 4072 | break; |
| 4073 | for (c1 = c; (c = subgetc(c1, 1)) != c1;) { |
| 4074 | if (c == 0) |
| 4075 | break; |
| 4076 | if (c == '\'' || !any(c, "$`\"")) |
| 4077 | c |= QUOTE; |
| 4078 | *e.linep++ = c; |
| 4079 | } |
| 4080 | c = 0; |
| 4081 | } |
| 4082 | unget(c); |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 4083 | if (!isalpha(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4084 | scanequals = 0; |
| 4085 | for (;;) { |
| 4086 | c = subgetc('"', foundequals); |
| 4087 | if (c == 0 || |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4088 | f & (DOBLANK && any(c, ifs->value)) || |
| 4089 | (!INSUB() && any(c, "\"'"))) { |
| 4090 | scanequals = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4091 | unget(c); |
| 4092 | if (any(c, "\"'")) |
| 4093 | goto loop; |
| 4094 | break; |
| 4095 | } |
| 4096 | if (scanequals) { |
| 4097 | if (c == '=') { |
| 4098 | foundequals = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4099 | scanequals = 0; |
| 4100 | } else if (!isalnum(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4101 | scanequals = 0; |
| 4102 | } |
| 4103 | *e.linep++ = c; |
| 4104 | } |
| 4105 | *e.linep++ = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4106 | return (sp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | /* |
| 4110 | * Get characters, substituting for ` and $ |
| 4111 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4112 | static int subgetc(ec, quoted) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4113 | REGISTER char ec; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4114 | int quoted; |
| 4115 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4116 | REGISTER char c; |
| 4117 | |
| 4118 | DBGPRINTF3(("SUBGETC: enter, quoted=%d\n", quoted)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4119 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4120 | again: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4121 | c = my_getc(ec); |
| 4122 | if (!INSUB() && ec != '\'') { |
| 4123 | if (c == '`') { |
| 4124 | if (grave(quoted) == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4125 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4126 | e.iop->task = XGRAVE; |
| 4127 | goto again; |
| 4128 | } |
| 4129 | if (c == '$' && (c = dollar(quoted)) == 0) { |
| 4130 | e.iop->task = XDOLL; |
| 4131 | goto again; |
| 4132 | } |
| 4133 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4134 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | /* |
| 4138 | * Prepare to generate the string returned by ${} substitution. |
| 4139 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4140 | static int dollar(quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4141 | int quoted; |
| 4142 | { |
| 4143 | int otask; |
| 4144 | struct io *oiop; |
| 4145 | char *dolp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4146 | REGISTER char *s, c, *cp = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4147 | struct var *vp; |
| 4148 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4149 | DBGPRINTF3(("DOLLAR: enter, quoted=%d\n", quoted)); |
| 4150 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4151 | c = readc(); |
| 4152 | s = e.linep; |
| 4153 | if (c != '{') { |
| 4154 | *e.linep++ = c; |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 4155 | if (isalpha(c) || c == '_') { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4156 | while ((c = readc()) != 0 && (isalnum(c) || c == '_')) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4157 | if (e.linep < elinep) |
| 4158 | *e.linep++ = c; |
| 4159 | unget(c); |
| 4160 | } |
| 4161 | c = 0; |
| 4162 | } else { |
| 4163 | oiop = e.iop; |
| 4164 | otask = e.iop->task; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4165 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4166 | e.iop->task = XOTHER; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4167 | while ((c = subgetc('"', 0)) != 0 && c != '}' && c != '\n') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4168 | if (e.linep < elinep) |
| 4169 | *e.linep++ = c; |
| 4170 | if (oiop == e.iop) |
| 4171 | e.iop->task = otask; |
| 4172 | if (c != '}') { |
| 4173 | err("unclosed ${"); |
| 4174 | gflg++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4175 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4176 | } |
| 4177 | } |
| 4178 | if (e.linep >= elinep) { |
| 4179 | err("string in ${} too long"); |
| 4180 | gflg++; |
| 4181 | e.linep -= 10; |
| 4182 | } |
| 4183 | *e.linep = 0; |
| 4184 | if (*s) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4185 | for (cp = s + 1; *cp; cp++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4186 | if (any(*cp, "=-+?")) { |
| 4187 | c = *cp; |
| 4188 | *cp++ = 0; |
| 4189 | break; |
| 4190 | } |
| 4191 | if (s[1] == 0 && (*s == '*' || *s == '@')) { |
| 4192 | if (dolc > 1) { |
| 4193 | /* currently this does not distinguish $* and $@ */ |
| 4194 | /* should check dollar */ |
| 4195 | e.linep = s; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4196 | PUSHIO(awordlist, dolv + 1, dolchar); |
| 4197 | return (0); |
| 4198 | } else { /* trap the nasty ${=} */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4199 | s[0] = '1'; |
| 4200 | s[1] = 0; |
| 4201 | } |
| 4202 | } |
| 4203 | vp = lookup(s); |
| 4204 | if ((dolp = vp->value) == null) { |
| 4205 | switch (c) { |
| 4206 | case '=': |
| 4207 | if (isdigit(*s)) { |
| 4208 | err("cannot use ${...=...} with $n"); |
| 4209 | gflg++; |
| 4210 | break; |
| 4211 | } |
| 4212 | setval(vp, cp); |
| 4213 | dolp = vp->value; |
| 4214 | break; |
| 4215 | |
| 4216 | case '-': |
| 4217 | dolp = strsave(cp, areanum); |
| 4218 | break; |
| 4219 | |
| 4220 | case '?': |
| 4221 | if (*cp == 0) { |
| 4222 | prs("missing value for "); |
| 4223 | err(s); |
| 4224 | } else |
| 4225 | err(cp); |
| 4226 | gflg++; |
| 4227 | break; |
| 4228 | } |
| 4229 | } else if (c == '+') |
| 4230 | dolp = strsave(cp, areanum); |
| 4231 | if (flag['u'] && dolp == null) { |
| 4232 | prs("unset variable: "); |
| 4233 | err(s); |
| 4234 | gflg++; |
| 4235 | } |
| 4236 | e.linep = s; |
| 4237 | PUSHIO(aword, dolp, quoted ? qstrchar : strchar); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4238 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | /* |
| 4242 | * Run the command in `...` and read its output. |
| 4243 | */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4244 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4245 | static int grave(quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4246 | int quoted; |
| 4247 | { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4248 | char *cp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4249 | REGISTER int i; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4250 | int j; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4251 | int pf[2]; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4252 | static char child_cmd[LINELIM]; |
| 4253 | char *src; |
| 4254 | char *dest; |
| 4255 | int count; |
| 4256 | int ignore; |
| 4257 | int ignore_once; |
| 4258 | char *argument_list[4]; |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4259 | struct wdblock *wb = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4260 | |
| 4261 | #if __GNUC__ |
| 4262 | /* Avoid longjmp clobbering */ |
| 4263 | (void) &cp; |
| 4264 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4265 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4266 | for (cp = e.iop->argp->aword; *cp != '`'; cp++) |
| 4267 | if (*cp == 0) { |
| 4268 | err("no closing `"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4269 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4270 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4271 | |
| 4272 | /* string copy with dollar expansion */ |
| 4273 | src = e.iop->argp->aword; |
| 4274 | dest = child_cmd; |
| 4275 | count = 0; |
| 4276 | ignore = 0; |
| 4277 | ignore_once = 0; |
| 4278 | while ((*src != '`') && (count < LINELIM)) { |
| 4279 | if (*src == '\'') |
| 4280 | ignore = !ignore; |
| 4281 | if (*src == '\\') |
| 4282 | ignore_once = 1; |
| 4283 | if (*src == '$' && !ignore && !ignore_once) { |
| 4284 | struct var *vp; |
| 4285 | char var_name[LINELIM]; |
| 4286 | char alt_value[LINELIM]; |
| 4287 | int var_index = 0; |
| 4288 | int alt_index = 0; |
| 4289 | char operator = 0; |
| 4290 | int braces = 0; |
| 4291 | char *value; |
| 4292 | |
| 4293 | src++; |
| 4294 | if (*src == '{') { |
| 4295 | braces = 1; |
| 4296 | src++; |
| 4297 | } |
| 4298 | |
| 4299 | var_name[var_index++] = *src++; |
Paul Fox | 54690dc | 2005-07-20 18:33:12 +0000 | [diff] [blame] | 4300 | while (isalnum(*src) || *src=='_') |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4301 | var_name[var_index++] = *src++; |
| 4302 | var_name[var_index] = 0; |
| 4303 | |
| 4304 | if (braces) { |
| 4305 | switch (*src) { |
| 4306 | case '}': |
| 4307 | break; |
| 4308 | case '-': |
| 4309 | case '=': |
| 4310 | case '+': |
| 4311 | case '?': |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4312 | operator = * src; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4313 | break; |
| 4314 | default: |
| 4315 | err("unclosed ${\n"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4316 | return (0); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4317 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4318 | if (operator) { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4319 | src++; |
| 4320 | while (*src && (*src != '}')) { |
| 4321 | alt_value[alt_index++] = *src++; |
| 4322 | } |
| 4323 | alt_value[alt_index] = 0; |
| 4324 | if (*src != '}') { |
| 4325 | err("unclosed ${\n"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4326 | return (0); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4327 | } |
| 4328 | } |
| 4329 | src++; |
| 4330 | } |
| 4331 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4332 | if (isalpha(*var_name)) { |
| 4333 | /* let subshell handle it instead */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4334 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4335 | char *namep = var_name; |
| 4336 | |
| 4337 | *dest++ = '$'; |
| 4338 | if (braces) |
| 4339 | *dest++ = '{'; |
| 4340 | while (*namep) |
| 4341 | *dest++ = *namep++; |
| 4342 | if (operator) { |
| 4343 | char *altp = alt_value; |
| 4344 | *dest++ = operator; |
| 4345 | while (*altp) |
| 4346 | *dest++ = *altp++; |
| 4347 | } |
| 4348 | if (braces) |
| 4349 | *dest++ = '}'; |
| 4350 | |
| 4351 | wb = addword(lookup(var_name)->name, wb); |
| 4352 | } else { |
| 4353 | /* expand */ |
| 4354 | |
| 4355 | vp = lookup(var_name); |
| 4356 | if (vp->value != null) |
| 4357 | value = (operator == '+') ? |
| 4358 | alt_value : vp->value; |
| 4359 | else if (operator == '?') { |
| 4360 | err(alt_value); |
| 4361 | return (0); |
| 4362 | } else if (alt_index && (operator != '+')) { |
| 4363 | value = alt_value; |
| 4364 | if (operator == '=') |
| 4365 | setval(vp, value); |
| 4366 | } else |
| 4367 | continue; |
| 4368 | |
| 4369 | while (*value && (count < LINELIM)) { |
| 4370 | *dest++ = *value++; |
| 4371 | count++; |
| 4372 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4373 | } |
| 4374 | } else { |
| 4375 | *dest++ = *src++; |
| 4376 | count++; |
| 4377 | ignore_once = 0; |
| 4378 | } |
| 4379 | } |
| 4380 | *dest = '\0'; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4381 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4382 | if (openpipe(pf) < 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4383 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4384 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4385 | while ((i = vfork()) == -1 && errno == EAGAIN); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4386 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4387 | DBGPRINTF3(("GRAVE: i is %p\n", io)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4388 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4389 | if (i < 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4390 | closepipe(pf); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4391 | err((char *) bb_msg_memory_exhausted); |
| 4392 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4393 | } |
| 4394 | if (i != 0) { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4395 | waitpid(i, NULL, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4396 | e.iop->argp->aword = ++cp; |
| 4397 | close(pf[1]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4398 | PUSHIO(afile, remap(pf[0]), |
| 4399 | (int (*)(struct ioarg *)) ((quoted) ? qgravechar : |
| 4400 | gravechar)); |
| 4401 | return (1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4402 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4403 | /* allow trapped signals */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4404 | /* XXX - Maybe this signal stuff should go as well? */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4405 | for (j = 0; j <= _NSIG; j++) |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4406 | if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN) |
| 4407 | signal(j, SIG_DFL); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4408 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4409 | dup2(pf[1], 1); |
| 4410 | closepipe(pf); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4411 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4412 | argument_list[0] = (char *) DEFAULT_SHELL; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4413 | argument_list[1] = "-c"; |
| 4414 | argument_list[2] = child_cmd; |
| 4415 | argument_list[3] = 0; |
| 4416 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4417 | cp = rexecve(argument_list[0], argument_list, makenv(1, wb)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4418 | prs(argument_list[0]); |
| 4419 | prs(": "); |
| 4420 | err(cp); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4421 | _exit(1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4424 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4425 | static char *unquote(as) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4426 | REGISTER char *as; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4427 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4428 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4429 | |
| 4430 | if ((s = as) != NULL) |
| 4431 | while (*s) |
| 4432 | *s++ &= ~QUOTE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4433 | return (as); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | /* -------- glob.c -------- */ |
| 4437 | |
| 4438 | /* |
| 4439 | * glob |
| 4440 | */ |
| 4441 | |
| 4442 | #define scopy(x) strsave((x), areanum) |
| 4443 | #define BLKSIZ 512 |
| 4444 | #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent)) |
| 4445 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4446 | static struct wdblock *cl, *nl; |
| 4447 | static char spcl[] = "[?*"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4448 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4449 | static struct wdblock *glob(cp, wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4450 | char *cp; |
| 4451 | struct wdblock *wb; |
| 4452 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4453 | REGISTER int i; |
| 4454 | REGISTER char *pp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4455 | |
| 4456 | if (cp == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4457 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4458 | i = 0; |
| 4459 | for (pp = cp; *pp; pp++) |
| 4460 | if (any(*pp, spcl)) |
| 4461 | i++; |
| 4462 | else if (!any(*pp & ~QUOTE, spcl)) |
| 4463 | *pp &= ~QUOTE; |
| 4464 | if (i != 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4465 | for (cl = addword(scopy(cp), (struct wdblock *) 0); anyspcl(cl); |
| 4466 | cl = nl) { |
| 4467 | nl = newword(cl->w_nword * 2); |
| 4468 | for (i = 0; i < cl->w_nword; i++) { /* for each argument */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4469 | for (pp = cl->w_words[i]; *pp; pp++) |
| 4470 | if (any(*pp, spcl)) { |
| 4471 | globname(cl->w_words[i], pp); |
| 4472 | break; |
| 4473 | } |
| 4474 | if (*pp == '\0') |
| 4475 | nl = addword(scopy(cl->w_words[i]), nl); |
| 4476 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4477 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4478 | DELETE(cl->w_words[i]); |
| 4479 | DELETE(cl); |
| 4480 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4481 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4482 | unquote(cl->w_words[i]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4483 | glob0((char *) cl->w_words, cl->w_nword, sizeof(char *), xstrcmp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4484 | if (cl->w_nword) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4485 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4486 | wb = addword(cl->w_words[i], wb); |
| 4487 | DELETE(cl); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4488 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4489 | } |
| 4490 | } |
| 4491 | wb = addword(unquote(cp), wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4492 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4493 | } |
| 4494 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4495 | static void globname(we, pp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4496 | char *we; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4497 | REGISTER char *pp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4498 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4499 | REGISTER char *np, *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4500 | char *name, *gp, *dp; |
| 4501 | int k; |
| 4502 | DIR *dirp; |
| 4503 | struct dirent *de; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4504 | char dname[NAME_MAX + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4505 | struct stat dbuf; |
| 4506 | |
| 4507 | for (np = we; np != pp; pp--) |
| 4508 | if (pp[-1] == '/') |
| 4509 | break; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4510 | for (dp = cp = space((int) (pp - np) + 3); np < pp;) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4511 | *cp++ = *np++; |
| 4512 | *cp++ = '.'; |
| 4513 | *cp = '\0'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4514 | for (gp = cp = space(strlen(pp) + 1); *np && *np != '/';) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4515 | *cp++ = *np++; |
| 4516 | *cp = '\0'; |
| 4517 | dirp = opendir(dp); |
| 4518 | if (dirp == 0) { |
| 4519 | DELETE(dp); |
| 4520 | DELETE(gp); |
| 4521 | return; |
| 4522 | } |
| 4523 | dname[NAME_MAX] = '\0'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4524 | while ((de = readdir(dirp)) != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4525 | /* XXX Hmmm... What this could be? (abial) */ |
| 4526 | /* |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4527 | if (ent[j].d_ino == 0) |
| 4528 | continue; |
| 4529 | */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4530 | strncpy(dname, de->d_name, NAME_MAX); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4531 | if (dname[0] == '.') |
| 4532 | if (*gp != '.') |
| 4533 | continue; |
| 4534 | for (k = 0; k < NAME_MAX; k++) |
| 4535 | if (any(dname[k], spcl)) |
| 4536 | dname[k] |= QUOTE; |
| 4537 | if (gmatch(dname, gp)) { |
| 4538 | name = generate(we, pp, dname, np); |
| 4539 | if (*np && !anys(np, spcl)) { |
| 4540 | if (stat(name, &dbuf)) { |
| 4541 | DELETE(name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4542 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4543 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4544 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4545 | nl = addword(name, nl); |
| 4546 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4547 | } |
| 4548 | closedir(dirp); |
| 4549 | DELETE(dp); |
| 4550 | DELETE(gp); |
| 4551 | } |
| 4552 | |
| 4553 | /* |
| 4554 | * generate a pathname as below. |
| 4555 | * start..end1 / middle end |
| 4556 | * the slashes come for free |
| 4557 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4558 | static char *generate(start1, end1, middle, end) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4559 | char *start1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4560 | REGISTER char *end1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4561 | char *middle, *end; |
| 4562 | { |
| 4563 | char *p; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4564 | REGISTER char *op, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4565 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4566 | p = op = |
| 4567 | space((int) (end1 - start1) + strlen(middle) + strlen(end) + 2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4568 | for (xp = start1; xp != end1;) |
| 4569 | *op++ = *xp++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4570 | for (xp = middle; (*op++ = *xp++) != '\0';); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4571 | op--; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4572 | for (xp = end; (*op++ = *xp++) != '\0';); |
| 4573 | return (p); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4576 | static int anyspcl(wb) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4577 | REGISTER struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4578 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4579 | REGISTER int i; |
| 4580 | REGISTER char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4581 | |
| 4582 | wd = wb->w_words; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4583 | for (i = 0; i < wb->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4584 | if (anys(spcl, *wd++)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4585 | return (1); |
| 4586 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4589 | static int xstrcmp(p1, p2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4590 | char *p1, *p2; |
| 4591 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4592 | return (strcmp(*(char **) p1, *(char **) p2)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | /* -------- word.c -------- */ |
| 4596 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4597 | static struct wdblock *newword(nw) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4598 | REGISTER int nw; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4599 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4600 | REGISTER struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4601 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4602 | wb = (struct wdblock *) space(sizeof(*wb) + nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4603 | wb->w_bsize = nw; |
| 4604 | wb->w_nword = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4605 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4608 | static struct wdblock *addword(wd, wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4609 | char *wd; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4610 | REGISTER struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4611 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4612 | REGISTER struct wdblock *wb2; |
| 4613 | REGISTER int nw; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4614 | |
| 4615 | if (wb == NULL) |
| 4616 | wb = newword(NSTART); |
| 4617 | if ((nw = wb->w_nword) >= wb->w_bsize) { |
| 4618 | wb2 = newword(nw * 2); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4619 | memcpy((char *) wb2->w_words, (char *) wb->w_words, |
| 4620 | nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4621 | wb2->w_nword = nw; |
| 4622 | DELETE(wb); |
| 4623 | wb = wb2; |
| 4624 | } |
| 4625 | wb->w_words[wb->w_nword++] = wd; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4626 | return (wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4627 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4628 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4629 | static |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4630 | char **getwords(wb) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4631 | REGISTER struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4632 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4633 | REGISTER char **wd; |
| 4634 | REGISTER int nb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4635 | |
| 4636 | if (wb == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4637 | return ((char **) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4638 | if (wb->w_nword == 0) { |
| 4639 | DELETE(wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4640 | return ((char **) NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4641 | } |
| 4642 | wd = (char **) space(nb = sizeof(*wd) * wb->w_nword); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4643 | memcpy((char *) wd, (char *) wb->w_words, nb); |
| 4644 | DELETE(wb); /* perhaps should done by caller */ |
| 4645 | return (wd); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 4648 | static int (*func) (char *, char *); |
| 4649 | static int globv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4650 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4651 | static void glob0(a0, a1, a2, a3) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4652 | char *a0; |
| 4653 | unsigned a1; |
| 4654 | int a2; |
| 4655 | int (*a3) (char *, char *); |
| 4656 | { |
| 4657 | func = a3; |
| 4658 | globv = a2; |
| 4659 | glob1(a0, a0 + a1 * a2); |
| 4660 | } |
| 4661 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4662 | static void glob1(base, lim) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4663 | char *base, *lim; |
| 4664 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4665 | REGISTER char *i, *j; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4666 | int v2; |
| 4667 | char *lptr, *hptr; |
| 4668 | int c; |
| 4669 | unsigned n; |
| 4670 | |
| 4671 | |
| 4672 | v2 = globv; |
| 4673 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4674 | top: |
| 4675 | if ((n = (int) (lim - base)) <= v2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4676 | return; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4677 | n = v2 * (n / (2 * v2)); |
| 4678 | hptr = lptr = base + n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4679 | i = base; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4680 | j = lim - v2; |
| 4681 | for (;;) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4682 | if (i < lptr) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4683 | if ((c = (*func) (i, lptr)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4684 | glob2(i, lptr -= v2); |
| 4685 | continue; |
| 4686 | } |
| 4687 | if (c < 0) { |
| 4688 | i += v2; |
| 4689 | continue; |
| 4690 | } |
| 4691 | } |
| 4692 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4693 | begin: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4694 | if (j > hptr) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4695 | if ((c = (*func) (hptr, j)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4696 | glob2(hptr += v2, j); |
| 4697 | goto begin; |
| 4698 | } |
| 4699 | if (c > 0) { |
| 4700 | if (i == lptr) { |
| 4701 | glob3(i, hptr += v2, j); |
| 4702 | i = lptr += v2; |
| 4703 | goto begin; |
| 4704 | } |
| 4705 | glob2(i, j); |
| 4706 | j -= v2; |
| 4707 | i += v2; |
| 4708 | continue; |
| 4709 | } |
| 4710 | j -= v2; |
| 4711 | goto begin; |
| 4712 | } |
| 4713 | |
| 4714 | |
| 4715 | if (i == lptr) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4716 | if (lptr - base >= lim - hptr) { |
| 4717 | glob1(hptr + v2, lim); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4718 | lim = lptr; |
| 4719 | } else { |
| 4720 | glob1(base, lptr); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4721 | base = hptr + v2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4722 | } |
| 4723 | goto top; |
| 4724 | } |
| 4725 | |
| 4726 | |
| 4727 | glob3(j, lptr -= v2, i); |
| 4728 | j = hptr -= v2; |
| 4729 | } |
| 4730 | } |
| 4731 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4732 | static void glob2(i, j) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4733 | char *i, *j; |
| 4734 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4735 | REGISTER char *index1, *index2, c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4736 | int m; |
| 4737 | |
| 4738 | m = globv; |
| 4739 | index1 = i; |
| 4740 | index2 = j; |
| 4741 | do { |
| 4742 | c = *index1; |
| 4743 | *index1++ = *index2; |
| 4744 | *index2++ = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4745 | } while (--m); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4746 | } |
| 4747 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4748 | static void glob3(i, j, k) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4749 | char *i, *j, *k; |
| 4750 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4751 | REGISTER char *index1, *index2, *index3; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4752 | int c; |
| 4753 | int m; |
| 4754 | |
| 4755 | m = globv; |
| 4756 | index1 = i; |
| 4757 | index2 = j; |
| 4758 | index3 = k; |
| 4759 | do { |
| 4760 | c = *index1; |
| 4761 | *index1++ = *index3; |
| 4762 | *index3++ = *index2; |
| 4763 | *index2++ = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4764 | } while (--m); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4765 | } |
| 4766 | |
| 4767 | /* -------- io.c -------- */ |
| 4768 | |
| 4769 | /* |
| 4770 | * shell IO |
| 4771 | */ |
| 4772 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4773 | static int my_getc(int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4774 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4775 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4776 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4777 | if (e.linep > elinep) { |
| 4778 | while ((c = readc()) != '\n' && c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4779 | err("input line too long"); |
| 4780 | gflg++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4781 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4782 | } |
| 4783 | c = readc(); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4784 | if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4785 | if (c == '\\') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4786 | c = readc(); |
| 4787 | if (c == '\n' && ec != '\"') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4788 | return (my_getc(ec)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4789 | c |= QUOTE; |
| 4790 | } |
| 4791 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4792 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4795 | static void unget(c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4796 | int c; |
| 4797 | { |
| 4798 | if (e.iop >= e.iobase) |
| 4799 | e.iop->peekc = c; |
| 4800 | } |
| 4801 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4802 | static int eofc() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4803 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4804 | 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] | 4805 | } |
| 4806 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4807 | static int readc() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4808 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4809 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4810 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4811 | RCPRINTF(("READC: e.iop %p, e.iobase %p\n", e.iop, e.iobase)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4812 | |
| 4813 | for (; e.iop >= e.iobase; e.iop--) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4814 | RCPRINTF(("READC: e.iop %p, peekc 0x%x\n", e.iop, e.iop->peekc)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4815 | if ((c = e.iop->peekc) != '\0') { |
| 4816 | e.iop->peekc = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4817 | return (c); |
| 4818 | } else { |
| 4819 | if (e.iop->prev != 0) { |
| 4820 | if ((c = (*e.iop->iofn) (e.iop->argp, e.iop)) != '\0') { |
| 4821 | if (c == -1) { |
| 4822 | e.iop++; |
| 4823 | continue; |
| 4824 | } |
| 4825 | if (e.iop == iostack) |
| 4826 | ioecho(c); |
| 4827 | return (e.iop->prev = c); |
| 4828 | } else if (e.iop->task == XIO && e.iop->prev != '\n') { |
| 4829 | e.iop->prev = 0; |
| 4830 | if (e.iop == iostack) |
| 4831 | ioecho('\n'); |
| 4832 | return '\n'; |
| 4833 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4834 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4835 | if (e.iop->task == XIO) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4836 | if (multiline) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4837 | return e.iop->prev = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4838 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4839 | if (interactive && e.iop == iostack + 1) { |
| 4840 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
| 4841 | current_prompt = prompt->value; |
| 4842 | #else |
| 4843 | prs(prompt->value); |
| 4844 | #endif |
| 4845 | } |
| 4846 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4847 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4848 | |
| 4849 | } /* FOR */ |
| 4850 | |
| 4851 | if (e.iop >= iostack) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4852 | RCPRINTF(("READC: return 0, e.iop %p\n", e.iop)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4853 | return (0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | DBGPRINTF(("READC: leave()...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4857 | leave(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4858 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4859 | /* NOTREACHED */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4860 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4863 | static void ioecho(c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4864 | char c; |
| 4865 | { |
| 4866 | if (flag['v']) |
| 4867 | write(2, &c, sizeof c); |
| 4868 | } |
| 4869 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4870 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4871 | static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4872 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4873 | DBGPRINTF(("PUSHIO: argp %p, argp->afid 0x%x, e.iop %p\n", argp, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4874 | argp->afid, e.iop)); |
| 4875 | |
| 4876 | /* 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] | 4877 | if (++e.iop >= &iostack[NPUSH]) { |
| 4878 | e.iop--; |
| 4879 | err("Shell input nested too deeply"); |
| 4880 | gflg++; |
| 4881 | return; |
| 4882 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4883 | |
| 4884 | /* We did not overflow the NPUSH array spots so setup data structs */ |
| 4885 | |
| 4886 | 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] | 4887 | |
| 4888 | if (argp->afid != AFID_NOBUF) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4889 | e.iop->argp = argp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4890 | else { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4891 | |
| 4892 | e.iop->argp = ioargstack + (e.iop - iostack); /* MAL - index into stack */ |
| 4893 | *e.iop->argp = *argp; /* copy data from temp area into stack spot */ |
| 4894 | |
| 4895 | /* MAL - mainbuf is for 1st data source (command line?) and all nested use a single shared buffer? */ |
| 4896 | |
| 4897 | if (e.iop == &iostack[0]) |
| 4898 | e.iop->argp->afbuf = &mainbuf; |
| 4899 | else |
| 4900 | e.iop->argp->afbuf = &sharedbuf; |
| 4901 | |
| 4902 | /* MAL - if not a termimal AND (commandline OR readable file) then give it a buffer id? */ |
| 4903 | /* This line appears to be active when running scripts from command line */ |
| 4904 | if ((isatty(e.iop->argp->afile) == 0) |
| 4905 | && (e.iop == &iostack[0] |
| 4906 | || lseek(e.iop->argp->afile, 0L, 1) != -1)) { |
| 4907 | if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */ |
| 4908 | bufid = AFID_ID; /* AFID_ID = 0 */ |
| 4909 | |
| 4910 | e.iop->argp->afid = bufid; /* assign buffer id */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4911 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4912 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4913 | DBGPRINTF(("PUSHIO: iostack %p, e.iop %p, afbuf %p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4914 | iostack, e.iop, e.iop->argp->afbuf)); |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4915 | DBGPRINTF(("PUSHIO: mbuf %p, sbuf %p, bid %d, e.iop %p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4916 | &mainbuf, &sharedbuf, bufid, e.iop)); |
| 4917 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4920 | e.iop->prev = ~'\n'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4921 | e.iop->peekc = 0; |
| 4922 | e.iop->xchar = 0; |
| 4923 | e.iop->nlcount = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4924 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4925 | if (fn == filechar || fn == linechar) |
| 4926 | e.iop->task = XIO; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4927 | else if (fn == (int (*)(struct ioarg *)) gravechar |
| 4928 | || fn == (int (*)(struct ioarg *)) qgravechar) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4929 | e.iop->task = XGRAVE; |
| 4930 | else |
| 4931 | e.iop->task = XOTHER; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4932 | |
| 4933 | return; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4934 | } |
| 4935 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4936 | static struct io *setbase(ip) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4937 | struct io *ip; |
| 4938 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4939 | REGISTER struct io *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4940 | |
| 4941 | xp = e.iobase; |
| 4942 | e.iobase = ip; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4943 | return (xp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | /* |
| 4947 | * Input generating functions |
| 4948 | */ |
| 4949 | |
| 4950 | /* |
| 4951 | * Produce the characters of a string, then a newline, then EOF. |
| 4952 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4953 | static int nlchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4954 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4955 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4956 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4957 | |
| 4958 | if (ap->aword == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4959 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4960 | if ((c = *ap->aword++) == 0) { |
| 4961 | ap->aword = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4962 | return ('\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4963 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4964 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | /* |
| 4968 | * Given a list of words, produce the characters |
| 4969 | * in them, with a space after each word. |
| 4970 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4971 | static int wdchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4972 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4973 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4974 | REGISTER char c; |
| 4975 | REGISTER char **wl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4976 | |
| 4977 | if ((wl = ap->awordlist) == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4978 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4979 | if (*wl != NULL) { |
| 4980 | if ((c = *(*wl)++) != 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4981 | return (c & 0177); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4982 | ap->awordlist++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4983 | return (' '); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4984 | } |
| 4985 | ap->awordlist = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4986 | return ('\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4987 | } |
| 4988 | |
| 4989 | /* |
| 4990 | * Return the characters of a list of words, |
| 4991 | * producing a space between them. |
| 4992 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4993 | static int dolchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4994 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4995 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4996 | REGISTER char *wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4997 | |
| 4998 | if ((wp = *ap->awordlist++) != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4999 | PUSHIO(aword, wp, *ap->awordlist == NULL ? strchar : xxchar); |
| 5000 | return (-1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5001 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5002 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5003 | } |
| 5004 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5005 | static int xxchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5006 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5007 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5008 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5009 | |
| 5010 | if (ap->aword == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5011 | return (0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5012 | if ((c = *ap->aword++) == '\0') { |
| 5013 | ap->aword = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5014 | return (' '); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5015 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5016 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | /* |
| 5020 | * Produce the characters from a single word (string). |
| 5021 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5022 | static int strchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5023 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5024 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5025 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5026 | |
| 5027 | if (ap->aword == NULL || (c = *ap->aword++) == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5028 | return (0); |
| 5029 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
| 5032 | /* |
| 5033 | * Produce quoted characters from a single word (string). |
| 5034 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5035 | static int qstrchar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5036 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5037 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5038 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5039 | |
| 5040 | if (ap->aword == NULL || (c = *ap->aword++) == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5041 | return (0); |
| 5042 | return (c | QUOTE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | /* |
| 5046 | * Return the characters from a file. |
| 5047 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5048 | static int filechar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5049 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5050 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5051 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5052 | char c; |
| 5053 | struct iobuf *bp = ap->afbuf; |
| 5054 | |
| 5055 | if (ap->afid != AFID_NOBUF) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5056 | if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5057 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5058 | if (i) |
| 5059 | lseek(ap->afile, ap->afpos, 0); |
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 | i = safe_read(ap->afile, bp->buf, sizeof(bp->buf)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5062 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5063 | if (i <= 0) { |
| 5064 | closef(ap->afile); |
| 5065 | return 0; |
| 5066 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5067 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5068 | bp->id = ap->afid; |
| 5069 | bp->ebufp = (bp->bufp = bp->buf) + i; |
| 5070 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5071 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5072 | ap->afpos++; |
| 5073 | return *bp->bufp++ & 0177; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5074 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5075 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 5076 | if (interactive && isatty(ap->afile)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5077 | static char mycommand[BUFSIZ]; |
| 5078 | static int position = 0, size = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5079 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5080 | while (size == 0 || position >= size) { |
| 5081 | cmdedit_read_input(current_prompt, mycommand); |
| 5082 | size = strlen(mycommand); |
| 5083 | position = 0; |
| 5084 | } |
| 5085 | c = mycommand[position]; |
| 5086 | position++; |
| 5087 | return (c); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5088 | } else |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5089 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5090 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5091 | { |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 5092 | i = safe_read(ap->afile, &c, sizeof(c)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5093 | return (i == sizeof(c) ? (c & 0x7f) : (closef(ap->afile), 0)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5094 | } |
| 5095 | } |
| 5096 | |
| 5097 | /* |
| 5098 | * Return the characters from a here temp file. |
| 5099 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5100 | static int herechar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5101 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5102 | { |
| 5103 | char c; |
| 5104 | |
| 5105 | |
| 5106 | if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) { |
| 5107 | close(ap->afile); |
| 5108 | c = 0; |
| 5109 | } |
| 5110 | return (c); |
| 5111 | |
| 5112 | } |
| 5113 | |
| 5114 | /* |
| 5115 | * Return the characters produced by a process (`...`). |
| 5116 | * Quote them if required, and remove any trailing newline characters. |
| 5117 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5118 | static int gravechar(ap, iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5119 | struct ioarg *ap; |
| 5120 | struct io *iop; |
| 5121 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5122 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5123 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5124 | if ((c = qgravechar(ap, iop) & ~QUOTE) == '\n') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5125 | c = ' '; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5126 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5129 | static int qgravechar(ap, iop) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5130 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5131 | struct io *iop; |
| 5132 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5133 | REGISTER int c; |
| 5134 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 5135 | DBGPRINTF3(("QGRAVECHAR: enter, ap=%p, iop=%p\n", ap, iop)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5136 | |
| 5137 | if (iop->xchar) { |
| 5138 | if (iop->nlcount) { |
| 5139 | iop->nlcount--; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5140 | return ('\n' | QUOTE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5141 | } |
| 5142 | c = iop->xchar; |
| 5143 | iop->xchar = 0; |
| 5144 | } else if ((c = filechar(ap)) == '\n') { |
| 5145 | iop->nlcount = 1; |
| 5146 | while ((c = filechar(ap)) == '\n') |
| 5147 | iop->nlcount++; |
| 5148 | iop->xchar = c; |
| 5149 | if (c == 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5150 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5151 | iop->nlcount--; |
| 5152 | c = '\n'; |
| 5153 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5154 | return (c != 0 ? c | QUOTE : 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5155 | } |
| 5156 | |
| 5157 | /* |
| 5158 | * Return a single command (usually the first line) from a file. |
| 5159 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5160 | static int linechar(ap) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5161 | REGISTER struct ioarg *ap; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5162 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5163 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5164 | |
| 5165 | if ((c = filechar(ap)) == '\n') { |
| 5166 | if (!multiline) { |
| 5167 | closef(ap->afile); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5168 | ap->afile = -1; /* illegal value */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5169 | } |
| 5170 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5171 | return (c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5172 | } |
| 5173 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5174 | static void prs(s) |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 5175 | REGISTER const char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5176 | { |
| 5177 | if (*s) |
| 5178 | write(2, s, strlen(s)); |
| 5179 | } |
| 5180 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5181 | static void prn(u) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5182 | unsigned u; |
| 5183 | { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 5184 | prs(itoa(u)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5185 | } |
| 5186 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5187 | static void closef(i) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5188 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5189 | { |
| 5190 | if (i > 2) |
| 5191 | close(i); |
| 5192 | } |
| 5193 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5194 | static void closeall() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5195 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5196 | REGISTER int u; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5197 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5198 | for (u = NUFILE; u < NOFILE;) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5199 | close(u++); |
| 5200 | } |
| 5201 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5202 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5203 | /* |
| 5204 | * remap fd into Shell's fd space |
| 5205 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5206 | static int remap(fd) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5207 | REGISTER int fd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5208 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5209 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5210 | int map[NOFILE]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5211 | int newfd; |
| 5212 | |
| 5213 | |
| 5214 | DBGPRINTF(("REMAP: fd=%d, e.iofd=%d\n", fd, e.iofd)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5215 | |
| 5216 | if (fd < e.iofd) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5217 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5218 | map[i] = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5219 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5220 | do { |
| 5221 | map[fd] = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5222 | newfd = dup(fd); |
| 5223 | fd = newfd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5224 | } while (fd >= 0 && fd < e.iofd); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5225 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5226 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5227 | if (map[i]) |
| 5228 | close(i); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5229 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5230 | if (fd < 0) |
| 5231 | err("too many files open in shell"); |
| 5232 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5233 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5234 | return (fd); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5235 | } |
| 5236 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5237 | static int openpipe(pv) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5238 | REGISTER int *pv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5239 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5240 | REGISTER int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5241 | |
| 5242 | if ((i = pipe(pv)) < 0) |
| 5243 | err("can't create pipe - try again"); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5244 | return (i); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5245 | } |
| 5246 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5247 | static void closepipe(pv) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5248 | REGISTER int *pv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5249 | { |
| 5250 | if (pv != NULL) { |
| 5251 | close(*pv++); |
| 5252 | close(*pv); |
| 5253 | } |
| 5254 | } |
| 5255 | |
| 5256 | /* -------- here.c -------- */ |
| 5257 | |
| 5258 | /* |
| 5259 | * here documents |
| 5260 | */ |
| 5261 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5262 | static void markhere(s, iop) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5263 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5264 | struct ioword *iop; |
| 5265 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5266 | REGISTER struct here *h, *lh; |
| 5267 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 5268 | DBGPRINTF7(("MARKHERE: enter, s=%p\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5269 | |
| 5270 | h = (struct here *) space(sizeof(struct here)); |
| 5271 | if (h == 0) |
| 5272 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5273 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5274 | h->h_tag = evalstr(s, DOSUB); |
| 5275 | if (h->h_tag == 0) |
| 5276 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5277 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5278 | h->h_iop = iop; |
| 5279 | iop->io_name = 0; |
| 5280 | h->h_next = NULL; |
| 5281 | if (inhere == 0) |
| 5282 | inhere = h; |
| 5283 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5284 | for (lh = inhere; lh != NULL; lh = lh->h_next) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5285 | if (lh->h_next == 0) { |
| 5286 | lh->h_next = h; |
| 5287 | break; |
| 5288 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5289 | iop->io_flag |= IOHERE | IOXHERE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5290 | for (s = h->h_tag; *s; s++) |
| 5291 | if (*s & QUOTE) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5292 | iop->io_flag &= ~IOXHERE; |
| 5293 | *s &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5294 | } |
| 5295 | h->h_dosub = iop->io_flag & IOXHERE; |
| 5296 | } |
| 5297 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5298 | static void gethere() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5299 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5300 | REGISTER struct here *h, *hp; |
| 5301 | |
| 5302 | DBGPRINTF7(("GETHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5303 | |
| 5304 | /* Scan here files first leaving inhere list in place */ |
| 5305 | for (hp = h = inhere; h != NULL; hp = h, h = h->h_next) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5306 | 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] | 5307 | |
| 5308 | /* Make inhere list active - keep list intact for scraphere */ |
| 5309 | if (hp != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5310 | hp->h_next = acthere; |
| 5311 | acthere = inhere; |
| 5312 | inhere = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5313 | } |
| 5314 | } |
| 5315 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5316 | static void readhere(name, s, ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5317 | char **name; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5318 | REGISTER char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5319 | int ec; |
| 5320 | { |
| 5321 | int tf; |
| 5322 | char tname[30] = ".msh_XXXXXX"; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5323 | REGISTER int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5324 | jmp_buf ev; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5325 | char myline[LINELIM + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5326 | char *thenext; |
| 5327 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 5328 | DBGPRINTF7(("READHERE: enter, name=%p, s=%p\n", name, s)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5329 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5330 | tf = mkstemp(tname); |
| 5331 | if (tf < 0) |
| 5332 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5333 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5334 | *name = strsave(tname, areanum); |
| 5335 | if (newenv(setjmp(errpt = ev)) != 0) |
| 5336 | unlink(tname); |
| 5337 | else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5338 | pushio(e.iop->argp, (int (*)(struct ioarg *)) e.iop->iofn); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5339 | e.iobase = e.iop; |
| 5340 | for (;;) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5341 | if (interactive && e.iop <= iostack) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5342 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5343 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5344 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5345 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5346 | #endif |
| 5347 | } |
| 5348 | thenext = myline; |
| 5349 | while ((c = my_getc(ec)) != '\n' && c) { |
| 5350 | if (ec == '\'') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5351 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5352 | if (thenext >= &myline[LINELIM]) { |
| 5353 | c = 0; |
| 5354 | break; |
| 5355 | } |
| 5356 | *thenext++ = c; |
| 5357 | } |
| 5358 | *thenext = 0; |
| 5359 | if (strcmp(s, myline) == 0 || c == 0) |
| 5360 | break; |
| 5361 | *thenext++ = '\n'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5362 | write(tf, myline, (int) (thenext - myline)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5363 | } |
| 5364 | if (c == 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5365 | prs("here document `"); |
| 5366 | prs(s); |
| 5367 | err("' unclosed"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5368 | } |
| 5369 | quitenv(); |
| 5370 | } |
| 5371 | close(tf); |
| 5372 | } |
| 5373 | |
| 5374 | /* |
| 5375 | * open here temp file. |
| 5376 | * if unquoted here, expand here temp file into second temp file. |
| 5377 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5378 | static int herein(hname, xdoll) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5379 | char *hname; |
| 5380 | int xdoll; |
| 5381 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5382 | REGISTER int hf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5383 | int tf; |
| 5384 | |
| 5385 | #if __GNUC__ |
| 5386 | /* Avoid longjmp clobbering */ |
| 5387 | (void) &tf; |
| 5388 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5389 | if (hname == NULL) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5390 | return (-1); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5391 | |
| 5392 | DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll)); |
| 5393 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5394 | hf = open(hname, 0); |
| 5395 | if (hf < 0) |
| 5396 | return (-1); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5397 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5398 | if (xdoll) { |
| 5399 | char c; |
| 5400 | char tname[30] = ".msh_XXXXXX"; |
| 5401 | jmp_buf ev; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5402 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5403 | tf = mkstemp(tname); |
| 5404 | if (tf < 0) |
| 5405 | return (-1); |
| 5406 | if (newenv(setjmp(errpt = ev)) == 0) { |
| 5407 | PUSHIO(afile, hf, herechar); |
| 5408 | setbase(e.iop); |
| 5409 | while ((c = subgetc(0, 0)) != 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5410 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5411 | write(tf, &c, sizeof c); |
| 5412 | } |
| 5413 | quitenv(); |
| 5414 | } else |
| 5415 | unlink(tname); |
| 5416 | close(tf); |
| 5417 | tf = open(tname, 0); |
| 5418 | unlink(tname); |
| 5419 | return (tf); |
| 5420 | } else |
| 5421 | return (hf); |
| 5422 | } |
| 5423 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5424 | static void scraphere() |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5425 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5426 | REGISTER struct here *h; |
| 5427 | |
| 5428 | DBGPRINTF7(("SCRAPHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5429 | |
| 5430 | for (h = inhere; h != NULL; h = h->h_next) { |
| 5431 | if (h->h_iop && h->h_iop->io_name) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5432 | unlink(h->h_iop->io_name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5433 | } |
| 5434 | inhere = NULL; |
| 5435 | } |
| 5436 | |
| 5437 | /* unlink here temp files before a freearea(area) */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5438 | static void freehere(area) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5439 | int area; |
| 5440 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5441 | REGISTER struct here *h, *hl; |
| 5442 | |
| 5443 | DBGPRINTF6(("FREEHERE: enter, area=%d\n", area)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5444 | |
| 5445 | hl = NULL; |
| 5446 | for (h = acthere; h != NULL; h = h->h_next) |
| 5447 | if (getarea((char *) h) >= area) { |
| 5448 | if (h->h_iop->io_name != NULL) |
| 5449 | unlink(h->h_iop->io_name); |
| 5450 | if (hl == NULL) |
| 5451 | acthere = h->h_next; |
| 5452 | else |
| 5453 | hl->h_next = h->h_next; |
| 5454 | } else |
| 5455 | hl = h; |
| 5456 | } |
| 5457 | |
| 5458 | |
| 5459 | |
| 5460 | /* |
| 5461 | * Copyright (c) 1987,1997, Prentice Hall |
| 5462 | * All rights reserved. |
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 | * Redistribution and use of the MINIX operating system in source and |
| 5465 | * binary forms, with or without modification, are permitted provided |
| 5466 | * that the following conditions are met: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5467 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5468 | * Redistributions of source code must retain the above copyright |
| 5469 | * notice, this list of conditions and the following disclaimer. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5470 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5471 | * Redistributions in binary form must reproduce the above |
| 5472 | * copyright notice, this list of conditions and the following |
| 5473 | * disclaimer in the documentation and/or other materials provided |
| 5474 | * with the distribution. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5475 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5476 | * Neither the name of Prentice Hall nor the names of the software |
| 5477 | * authors or contributors may be used to endorse or promote |
| 5478 | * products derived from this software without specific prior |
| 5479 | * written permission. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5480 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5481 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND |
| 5482 | * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 5483 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 5484 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 5485 | * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE |
| 5486 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 5487 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 5488 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 5489 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 5490 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 5491 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 5492 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 5493 | * |
| 5494 | */ |