Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Minix shell port for busybox |
| 4 | * |
| 5 | * This version of the Minix shell was adapted for use in busybox |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 6 | * by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 8 | * - backtick expansion did not work properly |
| 9 | * Jonas Holmberg <jonas.holmberg@axis.com> |
| 10 | * Robert Schwebel <r.schwebel@pengutronix.de> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 11 | * Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 12 | * |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 13 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 16 | #include <setjmp.h> |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 17 | #include <sys/times.h> |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 18 | #include "busybox.h" |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 20 | extern char **environ; |
| 21 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 22 | |
Mike Frysinger | 1781188 | 2006-05-05 20:33:07 +0000 | [diff] [blame] | 23 | /*#define MSHDEBUG 1*/ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 24 | |
| 25 | #ifdef MSHDEBUG |
Mike Frysinger | 14ff19b | 2006-06-20 20:37:01 +0000 | [diff] [blame] | 26 | int mshdbg = MSHDEBUG; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 27 | |
| 28 | #define DBGPRINTF(x) if(mshdbg>0)printf x |
| 29 | #define DBGPRINTF0(x) if(mshdbg>0)printf x |
| 30 | #define DBGPRINTF1(x) if(mshdbg>1)printf x |
| 31 | #define DBGPRINTF2(x) if(mshdbg>2)printf x |
| 32 | #define DBGPRINTF3(x) if(mshdbg>3)printf x |
| 33 | #define DBGPRINTF4(x) if(mshdbg>4)printf x |
| 34 | #define DBGPRINTF5(x) if(mshdbg>5)printf x |
| 35 | #define DBGPRINTF6(x) if(mshdbg>6)printf x |
| 36 | #define DBGPRINTF7(x) if(mshdbg>7)printf x |
| 37 | #define DBGPRINTF8(x) if(mshdbg>8)printf x |
| 38 | #define DBGPRINTF9(x) if(mshdbg>9)printf x |
| 39 | |
| 40 | int mshdbg_rc = 0; |
| 41 | |
| 42 | #define RCPRINTF(x) if(mshdbg_rc)printf x |
| 43 | |
| 44 | #else |
| 45 | |
| 46 | #define DBGPRINTF(x) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 47 | #define DBGPRINTF0(x) ((void)0) |
| 48 | #define DBGPRINTF1(x) ((void)0) |
| 49 | #define DBGPRINTF2(x) ((void)0) |
| 50 | #define DBGPRINTF3(x) ((void)0) |
| 51 | #define DBGPRINTF4(x) ((void)0) |
| 52 | #define DBGPRINTF5(x) ((void)0) |
| 53 | #define DBGPRINTF6(x) ((void)0) |
| 54 | #define DBGPRINTF7(x) ((void)0) |
| 55 | #define DBGPRINTF8(x) ((void)0) |
| 56 | #define DBGPRINTF9(x) ((void)0) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 57 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 58 | #define RCPRINTF(x) ((void)0) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 59 | |
| 60 | #endif /* MSHDEBUG */ |
| 61 | |
| 62 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 63 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 2a13175 | 2006-06-06 06:26:12 +0000 | [diff] [blame] | 64 | # define DEFAULT_ROOT_PROMPT "\\u:\\w> " |
| 65 | # define DEFAULT_USER_PROMPT "\\u:\\w$ " |
| 66 | #else |
| 67 | # define DEFAULT_ROOT_PROMPT "# " |
| 68 | # define DEFAULT_USER_PROMPT "$ " |
| 69 | #endif |
| 70 | |
| 71 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 72 | /* -------- sh.h -------- */ |
| 73 | /* |
| 74 | * shell |
| 75 | */ |
| 76 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 77 | #define LINELIM 2100 |
| 78 | #define NPUSH 8 /* limit to input nesting */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 79 | |
Eric Andersen | 392947c | 2002-12-11 07:42:46 +0000 | [diff] [blame] | 80 | #undef NOFILE |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 81 | #define NOFILE 20 /* Number of open files */ |
| 82 | #define NUFILE 10 /* Number of user-accessible files */ |
| 83 | #define FDBASE 10 /* First file usable by Shell */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * values returned by wait |
| 87 | */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 88 | #define WAITSIG(s) ((s)&0177) |
| 89 | #define WAITVAL(s) (((s)>>8)&0377) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 90 | #define WAITCORE(s) (((s)&0200)!=0) |
| 91 | |
| 92 | /* |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 93 | * library and system definitions |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 94 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 95 | typedef void xint; /* base type of jmp_buf, for not broken compilers */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * shell components |
| 99 | */ |
| 100 | |
| 101 | #define QUOTE 0200 |
| 102 | |
| 103 | #define NOBLOCK ((struct op *)NULL) |
| 104 | #define NOWORD ((char *)NULL) |
| 105 | #define NOWORDS ((char **)NULL) |
| 106 | #define NOPIPE ((int *)NULL) |
| 107 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * redirection |
| 111 | */ |
| 112 | struct ioword { |
| 113 | short io_unit; /* unit affected */ |
| 114 | short io_flag; /* action (below) */ |
| 115 | char *io_name; /* file name */ |
| 116 | }; |
| 117 | |
| 118 | #define IOREAD 1 /* < */ |
| 119 | #define IOHERE 2 /* << (here file) */ |
| 120 | #define IOWRITE 4 /* > */ |
| 121 | #define IOCAT 8 /* >> */ |
| 122 | #define IOXHERE 16 /* ${}, ` in << */ |
| 123 | #define IODUP 32 /* >&digit */ |
| 124 | #define IOCLOSE 64 /* >&- */ |
| 125 | |
| 126 | #define IODEFAULT (-1) /* token for default IO unit */ |
| 127 | |
| 128 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 129 | /* |
| 130 | * Description of a command or an operation on commands. |
| 131 | * Might eventually use a union. |
| 132 | */ |
| 133 | struct op { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 134 | int type; /* operation type, see below */ |
| 135 | char **words; /* arguments to a command */ |
| 136 | struct ioword **ioact; /* IO actions (eg, < > >>) */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 137 | struct op *left; |
| 138 | struct op *right; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 139 | char *str; /* identifier for case and for */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 142 | #define TCOM 1 /* command */ |
| 143 | #define TPAREN 2 /* (c-list) */ |
| 144 | #define TPIPE 3 /* a | b */ |
| 145 | #define TLIST 4 /* a [&;] b */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 146 | #define TOR 5 /* || */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 147 | #define TAND 6 /* && */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 148 | #define TFOR 7 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 149 | #define TDO 8 |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 150 | #define TCASE 9 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 151 | #define TIF 10 |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 152 | #define TWHILE 11 |
| 153 | #define TUNTIL 12 |
| 154 | #define TELIF 13 |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 155 | #define TPAT 14 /* pattern in case */ |
| 156 | #define TBRACE 15 /* {c-list} */ |
| 157 | #define TASYNC 16 /* c & */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 158 | /* Added to support "." file expansion */ |
| 159 | #define TDOT 17 |
| 160 | |
| 161 | /* Strings for names to make debug easier */ |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 162 | #ifdef MSHDEBUG |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 163 | static const char *const T_CMD_NAMES[] = { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 164 | "PLACEHOLDER", |
| 165 | "TCOM", |
| 166 | "TPAREN", |
| 167 | "TPIPE", |
| 168 | "TLIST", |
| 169 | "TOR", |
| 170 | "TAND", |
| 171 | "TFOR", |
| 172 | "TDO", |
| 173 | "TCASE", |
| 174 | "TIF", |
| 175 | "TWHILE", |
| 176 | "TUNTIL", |
| 177 | "TELIF", |
| 178 | "TPAT", |
| 179 | "TBRACE", |
| 180 | "TASYNC", |
| 181 | "TDOT", |
| 182 | }; |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 183 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * actions determining the environment of a process |
| 187 | */ |
| 188 | #define BIT(i) (1<<(i)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 189 | #define FEXEC BIT(0) /* execute without forking */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 190 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 191 | #define AREASIZE (90000) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 192 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 193 | /* |
| 194 | * flags to control evaluation of words |
| 195 | */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 196 | #define DOSUB 1 /* interpret $, `, and quotes */ |
| 197 | #define DOBLANK 2 /* perform blank interpretation */ |
| 198 | #define DOGLOB 4 /* interpret [?* */ |
| 199 | #define DOKEY 8 /* move words with `=' to 2nd arg. list */ |
| 200 | #define DOTRIM 16 /* trim resulting string */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 201 | |
| 202 | #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM) |
| 203 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 204 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 205 | /* PROTOTYPES */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 206 | static int newfile(char *s); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 207 | |
| 208 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 209 | struct brkcon { |
| 210 | jmp_buf brkpt; |
| 211 | struct brkcon *nextlev; |
| 212 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 213 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 214 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 215 | /* |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 216 | * flags: |
| 217 | * -e: quit on error |
| 218 | * -k: look for name=value everywhere on command line |
| 219 | * -n: no execution |
| 220 | * -t: exit after reading and executing one command |
| 221 | * -v: echo as read |
| 222 | * -x: trace |
| 223 | * -u: unset variables net diagnostic |
| 224 | */ |
Mike Frysinger | 3672fe9 | 2006-11-15 21:52:10 +0000 | [diff] [blame] | 225 | static char flags['z' - 'a' + 1]; |
| 226 | /* this looks weird, but is OK ... we index flag with 'a'...'z' */ |
| 227 | static char *flag = flags - 'a'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 228 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 229 | static char *null; /* null value for variable */ |
| 230 | static int intr; /* interrupt pending */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 231 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 232 | static char *trap[_NSIG + 1]; |
| 233 | static char ourtrap[_NSIG + 1]; |
| 234 | static int trapset; /* trap pending */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 235 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 236 | static int heedint; /* heed interrupt signals */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 237 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 238 | static int yynerrs; /* yacc */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 239 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 240 | static char line[LINELIM]; |
| 241 | static char *elinep; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 242 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 243 | #if ENABLE_FEATURE_EDITING |
| 244 | static char *current_prompt; |
| 245 | static line_input_t *line_input_state; |
| 246 | #endif |
| 247 | |
| 248 | static int areanum; /* current allocation area */ |
| 249 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 250 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 251 | /* |
| 252 | * other functions |
| 253 | */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 254 | static const char *rexecve(char *c, char **v, char **envp); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 255 | static char *evalstr(char *cp, int f); |
| 256 | static char *putn(int n); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 257 | static char *unquote(char *as); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 258 | static int rlookup(char *n); |
| 259 | static struct wdblock *glob(char *cp, struct wdblock *wb); |
| 260 | static int my_getc(int ec); |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 261 | static int subgetc(char ec, int quoted); |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 262 | static char **makenv(int all, struct wdblock *wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 263 | static char **eval(char **ap, int f); |
| 264 | static int setstatus(int s); |
| 265 | static int waitfor(int lastpid, int canintr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 266 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 267 | static void onintr(int s); /* SIGINT handler */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 268 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 269 | static int newenv(int f); |
| 270 | static void quitenv(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 271 | static void next(int f); |
| 272 | static void setdash(void); |
| 273 | static void onecommand(void); |
| 274 | static void runtrap(int i); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 275 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 276 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 277 | /* -------- area stuff -------- */ |
| 278 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 279 | #define REGSIZE sizeof(struct region) |
| 280 | #define GROWBY (256) |
| 281 | /* #define SHRINKBY (64) */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 282 | #undef SHRINKBY |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 283 | #define FREE (32767) |
| 284 | #define BUSY (0) |
| 285 | #define ALIGN (sizeof(int)-1) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 286 | |
| 287 | |
| 288 | struct region { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 289 | struct region *next; |
| 290 | int area; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 294 | /* -------- grammar stuff -------- */ |
| 295 | typedef union { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 296 | char *cp; |
| 297 | char **wp; |
| 298 | int i; |
| 299 | struct op *o; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 300 | } YYSTYPE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 301 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 302 | #define WORD 256 |
| 303 | #define LOGAND 257 |
| 304 | #define LOGOR 258 |
| 305 | #define BREAK 259 |
| 306 | #define IF 260 |
| 307 | #define THEN 261 |
| 308 | #define ELSE 262 |
| 309 | #define ELIF 263 |
| 310 | #define FI 264 |
| 311 | #define CASE 265 |
| 312 | #define ESAC 266 |
| 313 | #define FOR 267 |
| 314 | #define WHILE 268 |
| 315 | #define UNTIL 269 |
| 316 | #define DO 270 |
| 317 | #define DONE 271 |
| 318 | #define IN 272 |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 319 | /* Added for "." file expansion */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 320 | #define DOT 273 |
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 | #define YYERRCODE 300 |
| 323 | |
| 324 | /* flags to yylex */ |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 325 | #define CONTIN 01 /* skip new lines to complete command */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 326 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 327 | static struct op *pipeline(int cf); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 328 | static struct op *andor(void); |
| 329 | static struct op *c_list(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 330 | static int synio(int cf); |
| 331 | static void musthave(int c, int cf); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 332 | static struct op *simple(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 333 | static struct op *nested(int type, int mark); |
| 334 | static struct op *command(int cf); |
| 335 | static struct op *dogroup(int onlydone); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 336 | static struct op *thenpart(void); |
| 337 | static struct op *elsepart(void); |
| 338 | static struct op *caselist(void); |
| 339 | static struct op *casepart(void); |
| 340 | static char **pattern(void); |
| 341 | static char **wordlist(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 342 | static struct op *list(struct op *t1, struct op *t2); |
| 343 | 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] | 344 | static struct op *newtp(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 345 | static struct op *namelist(struct op *t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 346 | static char **copyw(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 347 | static void word(char *cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 348 | static struct ioword **copyio(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 349 | static struct ioword *io(int u, int f, char *cp); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 350 | static int yylex(int cf); |
| 351 | static int collect(int c, int c1); |
| 352 | static int dual(int c); |
| 353 | static void diag(int ec); |
| 354 | static char *tree(unsigned size); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 355 | |
| 356 | /* -------- var.h -------- */ |
| 357 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 358 | struct var { |
| 359 | char *value; |
| 360 | char *name; |
| 361 | struct var *next; |
| 362 | char status; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 363 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 364 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 365 | #define COPYV 1 /* flag to setval, suggesting copy */ |
| 366 | #define RONLY 01 /* variable is read-only */ |
| 367 | #define EXPORT 02 /* variable is to be exported */ |
| 368 | #define GETCELL 04 /* name & value space was got with getcell */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 369 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 370 | static int yyparse(void); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 371 | |
| 372 | static int execute(struct op *t, int *pin, int *pout, int act); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 373 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 374 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 375 | #define AFID_NOBUF (~0) |
| 376 | #define AFID_ID 0 |
| 377 | |
| 378 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 379 | /* -------- io.h -------- */ |
| 380 | /* io buffer */ |
| 381 | struct iobuf { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 382 | unsigned id; /* buffer id */ |
| 383 | char buf[512]; /* buffer */ |
| 384 | char *bufp; /* pointer into buffer */ |
| 385 | char *ebufp; /* pointer to end of buffer */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 386 | }; |
| 387 | |
| 388 | /* possible arguments to an IO function */ |
| 389 | struct ioarg { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 390 | const char *aword; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 391 | char **awordlist; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 392 | int afile; /* file descriptor */ |
| 393 | unsigned afid; /* buffer id */ |
| 394 | long afpos; /* file position */ |
| 395 | struct iobuf *afbuf; /* buffer for this file */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 396 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 397 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 398 | /* an input generator's state */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 399 | struct io { |
| 400 | int (*iofn) (struct ioarg *, struct io *); |
| 401 | struct ioarg *argp; |
| 402 | int peekc; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 403 | char prev; /* previous character read by readc() */ |
| 404 | char nlcount; /* for `'s */ |
| 405 | char xchar; /* for `'s */ |
| 406 | char task; /* reason for pushed IO */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 407 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 408 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 409 | #define XOTHER 0 /* none of the below */ |
| 410 | #define XDOLL 1 /* expanding ${} */ |
| 411 | #define XGRAVE 2 /* expanding `'s */ |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 412 | #define XIO 3 /* file IO */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 413 | |
| 414 | /* in substitution */ |
| 415 | #define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL) |
| 416 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 417 | static struct ioarg temparg = { 0, 0, 0, AFID_NOBUF, 0 }; /* temporary for PUSHIO */ |
| 418 | static struct ioarg ioargstack[NPUSH]; |
| 419 | static struct io iostack[NPUSH]; |
| 420 | static struct iobuf sharedbuf = { AFID_NOBUF }; |
| 421 | static struct iobuf mainbuf = { AFID_NOBUF }; |
| 422 | static unsigned bufid = AFID_ID; /* buffer id counter */ |
| 423 | |
| 424 | #define PUSHIO(what,arg,gen) ((temparg.what = (arg)), pushio(&temparg,(gen))) |
| 425 | #define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen))) |
| 426 | |
| 427 | |
| 428 | /* |
| 429 | * parsing & execution environment |
| 430 | */ |
| 431 | static struct env { |
| 432 | char *linep; |
| 433 | struct io *iobase; |
| 434 | struct io *iop; |
| 435 | xint *errpt; /* void * */ |
| 436 | int iofd; |
| 437 | struct env *oenv; |
| 438 | } e; |
| 439 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 440 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 441 | /* |
| 442 | * input generators for IO structure |
| 443 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 444 | static int nlchar(struct ioarg *ap); |
| 445 | static int strchar(struct ioarg *ap); |
| 446 | static int qstrchar(struct ioarg *ap); |
| 447 | static int filechar(struct ioarg *ap); |
| 448 | static int herechar(struct ioarg *ap); |
| 449 | static int linechar(struct ioarg *ap); |
| 450 | static int gravechar(struct ioarg *ap, struct io *iop); |
| 451 | static int qgravechar(struct ioarg *ap, struct io *iop); |
| 452 | static int dolchar(struct ioarg *ap); |
| 453 | static int wdchar(struct ioarg *ap); |
| 454 | static void scraphere(void); |
| 455 | static void freehere(int area); |
| 456 | static void gethere(void); |
| 457 | static void markhere(char *s, struct ioword *iop); |
| 458 | static int herein(char *hname, int xdoll); |
| 459 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 460 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 461 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 462 | static int eofc(void); |
| 463 | static int readc(void); |
| 464 | static void unget(int c); |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 465 | static void ioecho(char c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 466 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 467 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 468 | /* |
| 469 | * IO control |
| 470 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 471 | static void pushio(struct ioarg *argp, int (*f) (struct ioarg *)); |
| 472 | static int remap(int fd); |
| 473 | static int openpipe(int *pv); |
| 474 | static void closepipe(int *pv); |
| 475 | static struct io *setbase(struct io *ip); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 476 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 477 | /* -------- word.h -------- */ |
| 478 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 479 | #define NSTART 16 /* default number of words to allow for initially */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 480 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 481 | struct wdblock { |
| 482 | short w_bsize; |
| 483 | short w_nword; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 484 | /* bounds are arbitrary */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 485 | char *w_words[1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 486 | }; |
| 487 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 488 | static struct wdblock *addword(char *wd, struct wdblock *wb); |
| 489 | static struct wdblock *newword(int nw); |
| 490 | static char **getwords(struct wdblock *wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 491 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 492 | /* -------- misc stuff -------- */ |
| 493 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 494 | 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] | 495 | static int iosetup(struct ioword *iop, int pipein, int pipeout); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 496 | static void brkset(struct brkcon *bc); |
| 497 | static int dolabel(struct op *t); |
| 498 | static int dohelp(struct op *t); |
| 499 | static int dochdir(struct op *t); |
| 500 | static int doshift(struct op *t); |
| 501 | static int dologin(struct op *t); |
| 502 | static int doumask(struct op *t); |
| 503 | static int doexec(struct op *t); |
| 504 | static int dodot(struct op *t); |
| 505 | static int dowait(struct op *t); |
| 506 | static int doread(struct op *t); |
| 507 | static int doeval(struct op *t); |
| 508 | static int dotrap(struct op *t); |
| 509 | static int getsig(char *s); |
| 510 | static void setsig(int n, sighandler_t f); |
| 511 | static int getn(char *as); |
| 512 | static int dobreak(struct op *t); |
| 513 | static int docontinue(struct op *t); |
| 514 | static int brkcontin(char *cp, int val); |
| 515 | static int doexit(struct op *t); |
| 516 | static int doexport(struct op *t); |
| 517 | static int doreadonly(struct op *t); |
| 518 | static void rdexp(char **wp, void (*f) (struct var *), int key); |
| 519 | static void badid(char *s); |
| 520 | static int doset(struct op *t); |
| 521 | static void varput(char *s, int out); |
| 522 | static int dotimes(struct op *t); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 523 | static int expand(const char *cp, struct wdblock **wbp, int f); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 524 | static char *blank(int f); |
| 525 | static int dollar(int quoted); |
| 526 | static int grave(int quoted); |
| 527 | static void globname(char *we, char *pp); |
| 528 | static char *generate(char *start1, char *end1, char *middle, char *end); |
| 529 | static int anyspcl(struct wdblock *wb); |
| 530 | static int xstrcmp(char *p1, char *p2); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 531 | static void glob0(char *a0, unsigned a1, int a2, |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 532 | int (*a3) (char *, char *)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 533 | static void readhere(char **name, char *s, int ec); |
| 534 | static void pushio(struct ioarg *argp, int (*f) (struct ioarg *)); |
| 535 | static int xxchar(struct ioarg *ap); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 536 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 537 | struct here { |
| 538 | char *h_tag; |
| 539 | int h_dosub; |
| 540 | struct ioword *h_iop; |
| 541 | struct here *h_next; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 542 | }; |
| 543 | |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 544 | static const char * const signame[] = { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 545 | "Signal 0", |
| 546 | "Hangup", |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 547 | NULL, /* interrupt */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 548 | "Quit", |
| 549 | "Illegal instruction", |
| 550 | "Trace/BPT trap", |
| 551 | "Abort", |
| 552 | "Bus error", |
| 553 | "Floating Point Exception", |
| 554 | "Killed", |
| 555 | "SIGUSR1", |
| 556 | "SIGSEGV", |
| 557 | "SIGUSR2", |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 558 | NULL, /* broken pipe */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 559 | "Alarm clock", |
| 560 | "Terminated", |
| 561 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 562 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 563 | #define NSIGNAL (sizeof(signame)/sizeof(signame[0])) |
| 564 | |
| 565 | struct res { |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 566 | const char *r_name; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 567 | int r_val; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 568 | }; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 569 | static const struct res restab[] = { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 570 | {"for", FOR}, |
| 571 | {"case", CASE}, |
| 572 | {"esac", ESAC}, |
| 573 | {"while", WHILE}, |
| 574 | {"do", DO}, |
| 575 | {"done", DONE}, |
| 576 | {"if", IF}, |
| 577 | {"in", IN}, |
| 578 | {"then", THEN}, |
| 579 | {"else", ELSE}, |
| 580 | {"elif", ELIF}, |
| 581 | {"until", UNTIL}, |
| 582 | {"fi", FI}, |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 583 | {";;", BREAK}, |
| 584 | {"||", LOGOR}, |
| 585 | {"&&", LOGAND}, |
| 586 | {"{", '{'}, |
| 587 | {"}", '}'}, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 588 | {".", DOT}, |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 589 | {0, 0}, |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 593 | struct builtincmd { |
| 594 | const char *name; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 595 | int (*builtinfunc) (struct op * t); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 596 | }; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 597 | static const struct builtincmd builtincmds[] = { |
| 598 | {".", dodot}, |
| 599 | {":", dolabel}, |
| 600 | {"break", dobreak}, |
| 601 | {"cd", dochdir}, |
| 602 | {"continue", docontinue}, |
| 603 | {"eval", doeval}, |
| 604 | {"exec", doexec}, |
| 605 | {"exit", doexit}, |
| 606 | {"export", doexport}, |
| 607 | {"help", dohelp}, |
| 608 | {"login", dologin}, |
| 609 | {"newgrp", dologin}, |
| 610 | {"read", doread}, |
| 611 | {"readonly", doreadonly}, |
| 612 | {"set", doset}, |
| 613 | {"shift", doshift}, |
| 614 | {"times", dotimes}, |
| 615 | {"trap", dotrap}, |
| 616 | {"umask", doumask}, |
| 617 | {"wait", dowait}, |
| 618 | {0, 0} |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 619 | }; |
| 620 | |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 621 | static struct op *scantree(struct op *); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 622 | static struct op *dowholefile(int, int); |
| 623 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 624 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 625 | /* Globals */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 626 | static char **dolv; |
| 627 | static int dolc; |
| 628 | static int exstat; |
| 629 | static char gflg; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 630 | static int interactive; /* Is this an interactive shell */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 631 | static int execflg; |
| 632 | static int multiline; /* \n changed to ; */ |
| 633 | static struct op *outtree; /* result from parser */ |
| 634 | static xint *failpt; |
| 635 | static xint *errpt; |
| 636 | static struct brkcon *brklist; |
| 637 | static int isbreak; |
| 638 | static struct wdblock *wdlist; |
| 639 | static struct wdblock *iolist; |
| 640 | static char *trap[_NSIG + 1]; |
| 641 | static char ourtrap[_NSIG + 1]; |
| 642 | static int trapset; /* trap pending */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 643 | |
| 644 | #ifdef MSHDEBUG |
| 645 | static struct var *mshdbg_var; |
| 646 | #endif |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 647 | static struct var *vlist; /* dictionary */ |
| 648 | static struct var *homedir; /* home directory */ |
| 649 | static struct var *prompt; /* main prompt */ |
| 650 | static struct var *cprompt; /* continuation prompt */ |
| 651 | static struct var *path; /* search path for commands */ |
| 652 | static struct var *shell; /* shell to interpret command files */ |
| 653 | static struct var *ifs; /* field separators */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 654 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 655 | static int areanum; /* current allocation area */ |
| 656 | static int intr; |
| 657 | static int inparse; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 658 | static char *null = (char*)""; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 659 | static int heedint = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 660 | static void (*qflag) (int) = SIG_IGN; |
| 661 | static int startl; |
| 662 | static int peeksym; |
| 663 | static int nlseen; |
| 664 | static int iounit = IODEFAULT; |
| 665 | static YYSTYPE yylval; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 666 | static char *elinep = line + sizeof(line) - 5; |
| 667 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 668 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 669 | static struct here *inhere; /* list of hear docs while parsing */ |
| 670 | static struct here *acthere; /* list of active here documents */ |
| 671 | static struct region *areabot; /* bottom of area */ |
| 672 | static struct region *areatop; /* top of area */ |
| 673 | static struct region *areanxt; /* starting point of scan */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 674 | static void *brktop; |
| 675 | static void *brkaddr; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 676 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 677 | static struct env e = { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 678 | line, /* linep: char ptr */ |
| 679 | iostack, /* iobase: struct io ptr */ |
| 680 | iostack - 1, /* iop: struct io ptr */ |
| 681 | (xint *) NULL, /* errpt: void ptr for errors? */ |
| 682 | FDBASE, /* iofd: file desc */ |
| 683 | (struct env *) NULL /* oenv: struct env ptr */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 684 | }; |
| 685 | |
| 686 | #ifdef MSHDEBUG |
| 687 | void print_t(struct op *t) |
| 688 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 689 | DBGPRINTF(("T: t=%p, type %s, words=%p, IOword=%p\n", t, |
| 690 | T_CMD_NAMES[t->type], t->words, t->ioact)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 691 | |
| 692 | if (t->words) { |
| 693 | DBGPRINTF(("T: W1: %s", t->words[0])); |
| 694 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | void print_tree(struct op *head) |
| 698 | { |
| 699 | if (head == NULL) { |
| 700 | DBGPRINTF(("PRINT_TREE: no tree\n")); |
| 701 | return; |
| 702 | } |
| 703 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 704 | DBGPRINTF(("NODE: %p, left %p, right %p\n", head, head->left, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 705 | head->right)); |
| 706 | |
| 707 | if (head->left) |
| 708 | print_tree(head->left); |
| 709 | |
| 710 | if (head->right) |
| 711 | print_tree(head->right); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 712 | } |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 713 | #endif /* MSHDEBUG */ |
| 714 | |
| 715 | |
| 716 | /* |
| 717 | * IO functions |
| 718 | */ |
| 719 | static void prs(const char *s) |
| 720 | { |
| 721 | if (*s) |
| 722 | write(2, s, strlen(s)); |
| 723 | } |
| 724 | |
| 725 | static void prn(unsigned u) |
| 726 | { |
| 727 | prs(itoa(u)); |
| 728 | } |
| 729 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 730 | static void echo(char **wp) |
| 731 | { |
| 732 | int i; |
| 733 | |
| 734 | prs("+"); |
| 735 | for (i = 0; wp[i]; i++) { |
| 736 | if (i) |
| 737 | prs(" "); |
| 738 | prs(wp[i]); |
| 739 | } |
| 740 | prs("\n"); |
| 741 | } |
| 742 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 743 | static void closef(int i) |
| 744 | { |
| 745 | if (i > 2) |
| 746 | close(i); |
| 747 | } |
| 748 | |
| 749 | static void closeall(void) |
| 750 | { |
| 751 | int u; |
| 752 | |
| 753 | for (u = NUFILE; u < NOFILE;) |
| 754 | close(u++); |
| 755 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 756 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 757 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 758 | /* fail but return to process next command */ |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 759 | static void fail(void) ATTRIBUTE_NORETURN; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 760 | static void fail(void) |
| 761 | { |
| 762 | longjmp(failpt, 1); |
| 763 | /* NOTREACHED */ |
| 764 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 765 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 766 | /* abort shell (or fail in subshell) */ |
| 767 | static void leave(void) ATTRIBUTE_NORETURN; |
| 768 | static void leave(void) |
| 769 | { |
| 770 | DBGPRINTF(("LEAVE: leave called!\n")); |
| 771 | |
| 772 | if (execflg) |
| 773 | fail(); |
| 774 | scraphere(); |
| 775 | freehere(1); |
| 776 | runtrap(0); |
| 777 | _exit(exstat); |
| 778 | /* NOTREACHED */ |
| 779 | } |
| 780 | |
| 781 | static void warn(const char *s) |
| 782 | { |
| 783 | if (*s) { |
| 784 | prs(s); |
| 785 | exstat = -1; |
| 786 | } |
| 787 | prs("\n"); |
| 788 | if (flag['e']) |
| 789 | leave(); |
| 790 | } |
| 791 | |
| 792 | static void err(const char *s) |
| 793 | { |
| 794 | warn(s); |
| 795 | if (flag['n']) |
| 796 | return; |
| 797 | if (!interactive) |
| 798 | leave(); |
| 799 | if (e.errpt) |
| 800 | longjmp(e.errpt, 1); |
| 801 | closeall(); |
| 802 | e.iop = e.iobase = iostack; |
| 803 | } |
| 804 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 805 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 806 | /* -------- area.c -------- */ |
| 807 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 808 | /* |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 809 | * All memory between (char *)areabot and (char *)(areatop+1) is |
| 810 | * exclusively administered by the area management routines. |
| 811 | * It is assumed that sbrk() and brk() manipulate the high end. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 812 | */ |
| 813 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 814 | #define sbrk(X) ({ \ |
| 815 | void * __q = (void *)-1; \ |
| 816 | if (brkaddr + (int)(X) < brktop) { \ |
| 817 | __q = brkaddr; \ |
| 818 | brkaddr += (int)(X); \ |
| 819 | } \ |
| 820 | __q; \ |
| 821 | }) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 822 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 823 | static void initarea(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 824 | { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 825 | brkaddr = xmalloc(AREASIZE); |
| 826 | brktop = brkaddr + AREASIZE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 827 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 828 | while ((long) sbrk(0) & ALIGN) |
| 829 | sbrk(1); |
| 830 | areabot = (struct region *) sbrk(REGSIZE); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 831 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 832 | areabot->next = areabot; |
| 833 | areabot->area = BUSY; |
| 834 | areatop = areabot; |
| 835 | areanxt = areabot; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 838 | static char *getcell(unsigned nbytes) |
| 839 | { |
| 840 | int nregio; |
| 841 | struct region *p, *q; |
| 842 | int i; |
| 843 | |
| 844 | if (nbytes == 0) { |
| 845 | puts("getcell(0)"); |
| 846 | abort(); |
| 847 | } |
| 848 | /* silly and defeats the algorithm */ |
| 849 | /* |
| 850 | * round upwards and add administration area |
| 851 | */ |
| 852 | nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1; |
| 853 | p = areanxt; |
| 854 | for (;;) { |
| 855 | if (p->area > areanum) { |
| 856 | /* |
| 857 | * merge free cells |
| 858 | */ |
| 859 | while ((q = p->next)->area > areanum && q != areanxt) |
| 860 | p->next = q->next; |
| 861 | /* |
| 862 | * exit loop if cell big enough |
| 863 | */ |
| 864 | if (q >= p + nregio) |
| 865 | goto found; |
| 866 | } |
| 867 | p = p->next; |
| 868 | if (p == areanxt) |
| 869 | break; |
| 870 | } |
| 871 | i = nregio >= GROWBY ? nregio : GROWBY; |
| 872 | p = (struct region *) sbrk(i * REGSIZE); |
| 873 | if (p == (struct region *) -1) |
| 874 | return NULL; |
| 875 | p--; |
| 876 | if (p != areatop) { |
| 877 | puts("not contig"); |
| 878 | abort(); /* allocated areas are contiguous */ |
| 879 | } |
| 880 | q = p + i; |
| 881 | p->next = q; |
| 882 | p->area = FREE; |
| 883 | q->next = areabot; |
| 884 | q->area = BUSY; |
| 885 | areatop = q; |
| 886 | found: |
| 887 | /* |
| 888 | * we found a FREE area big enough, pointed to by 'p', and up to 'q' |
| 889 | */ |
| 890 | areanxt = p + nregio; |
| 891 | if (areanxt < q) { |
| 892 | /* |
| 893 | * split into requested area and rest |
| 894 | */ |
| 895 | if (areanxt + 1 > q) { |
| 896 | puts("OOM"); |
| 897 | abort(); /* insufficient space left for admin */ |
| 898 | } |
| 899 | areanxt->next = q; |
| 900 | areanxt->area = FREE; |
| 901 | p->next = areanxt; |
| 902 | } |
| 903 | p->area = areanum; |
| 904 | return (char *) (p + 1); |
| 905 | } |
| 906 | |
| 907 | static void freecell(char *cp) |
| 908 | { |
| 909 | struct region *p; |
| 910 | |
| 911 | p = (struct region *) cp; |
| 912 | if (p != NULL) { |
| 913 | p--; |
| 914 | if (p < areanxt) |
| 915 | areanxt = p; |
| 916 | p->area = FREE; |
| 917 | } |
| 918 | } |
| 919 | #define DELETE(obj) freecell((char *)obj) |
| 920 | |
| 921 | static void freearea(int a) |
| 922 | { |
| 923 | struct region *p, *top; |
| 924 | |
| 925 | top = areatop; |
| 926 | for (p = areabot; p != top; p = p->next) |
| 927 | if (p->area >= a) |
| 928 | p->area = FREE; |
| 929 | } |
| 930 | |
| 931 | static void setarea(char *cp, int a) |
| 932 | { |
| 933 | struct region *p; |
| 934 | |
| 935 | p = (struct region *) cp; |
| 936 | if (p != NULL) |
| 937 | (p - 1)->area = a; |
| 938 | } |
| 939 | |
| 940 | static int getarea(char *cp) |
| 941 | { |
| 942 | return ((struct region *) cp - 1)->area; |
| 943 | } |
| 944 | |
| 945 | static void garbage(void) |
| 946 | { |
| 947 | struct region *p, *q, *top; |
| 948 | |
| 949 | top = areatop; |
| 950 | for (p = areabot; p != top; p = p->next) { |
| 951 | if (p->area > areanum) { |
| 952 | while ((q = p->next)->area > areanum) |
| 953 | p->next = q->next; |
| 954 | areanxt = p; |
| 955 | } |
| 956 | } |
| 957 | #ifdef SHRINKBY |
| 958 | if (areatop >= q + SHRINKBY && q->area > areanum) { |
| 959 | brk((char *) (q + 1)); |
| 960 | q->next = areabot; |
| 961 | q->area = BUSY; |
| 962 | areatop = q; |
| 963 | } |
| 964 | #endif |
| 965 | } |
| 966 | |
| 967 | static char *space(int n) |
| 968 | { |
| 969 | char *cp; |
| 970 | |
| 971 | cp = getcell(n); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 972 | if (cp == NULL) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 973 | err("out of string space"); |
| 974 | return cp; |
| 975 | } |
| 976 | |
| 977 | static char *strsave(const char *s, int a) |
| 978 | { |
| 979 | char *cp; |
| 980 | |
| 981 | cp = space(strlen(s) + 1); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 982 | if (cp == NULL) { |
| 983 | // FIXME: I highly doubt this is good. |
| 984 | return (char*)""; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 985 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 986 | setarea(cp, a); |
| 987 | strcpy(cp, s); |
| 988 | return cp; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 991 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 992 | /* -------- var.c -------- */ |
| 993 | |
| 994 | static int eqname(const char *n1, const char *n2) |
| 995 | { |
| 996 | for (; *n1 != '=' && *n1 != '\0'; n1++) |
| 997 | if (*n2++ != *n1) |
| 998 | return 0; |
| 999 | return *n2 == '\0' || *n2 == '='; |
| 1000 | } |
| 1001 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1002 | static const char *findeq(const char *cp) |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1003 | { |
| 1004 | while (*cp != '\0' && *cp != '=') |
| 1005 | cp++; |
| 1006 | return cp; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * Find the given name in the dictionary |
| 1011 | * and return its value. If the name was |
| 1012 | * not previously there, enter it now and |
| 1013 | * return a null value. |
| 1014 | */ |
| 1015 | static struct var *lookup(const char *n) |
| 1016 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1017 | // FIXME: dirty hack |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1018 | static struct var dummy; |
| 1019 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1020 | struct var *vp; |
| 1021 | const char *cp; |
| 1022 | char *xp; |
| 1023 | int c; |
| 1024 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1025 | if (isdigit(*n)) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1026 | dummy.name = (char*)n; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1027 | for (c = 0; isdigit(*n) && c < 1000; n++) |
| 1028 | c = c * 10 + *n - '0'; |
| 1029 | dummy.status = RONLY; |
| 1030 | dummy.value = (c <= dolc ? dolv[c] : null); |
| 1031 | return &dummy; |
| 1032 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1033 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1034 | for (vp = vlist; vp; vp = vp->next) |
| 1035 | if (eqname(vp->name, n)) |
| 1036 | return vp; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1037 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1038 | cp = findeq(n); |
| 1039 | vp = (struct var *) space(sizeof(*vp)); |
| 1040 | if (vp == 0 || (vp->name = space((int) (cp - n) + 2)) == 0) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1041 | dummy.name = dummy.value = (char*)""; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1042 | return &dummy; |
| 1043 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1044 | |
| 1045 | xp = vp->name; |
| 1046 | while ((*xp = *n++) != '\0' && *xp != '=') |
| 1047 | xp++; |
| 1048 | *xp++ = '='; |
| 1049 | *xp = '\0'; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1050 | setarea((char *) vp, 0); |
| 1051 | setarea((char *) vp->name, 0); |
| 1052 | vp->value = null; |
| 1053 | vp->next = vlist; |
| 1054 | vp->status = GETCELL; |
| 1055 | vlist = vp; |
| 1056 | return vp; |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * if name is not NULL, it must be |
| 1061 | * a prefix of the space `val', |
| 1062 | * and end with `='. |
| 1063 | * this is all so that exporting |
| 1064 | * values is reasonably painless. |
| 1065 | */ |
| 1066 | static void nameval(struct var *vp, const char *val, const char *name) |
| 1067 | { |
| 1068 | const char *cp; |
| 1069 | char *xp; |
| 1070 | int fl; |
| 1071 | |
| 1072 | if (vp->status & RONLY) { |
| 1073 | xp = vp->name; |
| 1074 | while (*xp && *xp != '=') |
| 1075 | putc(*xp++, stderr); |
| 1076 | err(" is read-only"); |
| 1077 | return; |
| 1078 | } |
| 1079 | fl = 0; |
| 1080 | if (name == NULL) { |
| 1081 | xp = space(strlen(vp->name) + strlen(val) + 2); |
| 1082 | if (xp == NULL) |
| 1083 | return; |
| 1084 | /* make string: name=value */ |
| 1085 | setarea(xp, 0); |
| 1086 | name = xp; |
| 1087 | cp = vp->name; |
| 1088 | while ((*xp = *cp++) != '\0' && *xp != '=') |
| 1089 | xp++; |
| 1090 | *xp++ = '='; |
| 1091 | strcpy(xp, val); |
| 1092 | val = xp; |
| 1093 | fl = GETCELL; |
| 1094 | } |
| 1095 | if (vp->status & GETCELL) |
| 1096 | freecell(vp->name); /* form new string `name=value' */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1097 | vp->name = (char*)name; |
| 1098 | vp->value = (char*)val; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1099 | vp->status |= fl; |
| 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * give variable at `vp' the value `val'. |
| 1104 | */ |
| 1105 | static void setval(struct var *vp, const char *val) |
| 1106 | { |
| 1107 | nameval(vp, val, NULL); |
| 1108 | } |
| 1109 | |
| 1110 | static void export(struct var *vp) |
| 1111 | { |
| 1112 | vp->status |= EXPORT; |
| 1113 | } |
| 1114 | |
| 1115 | static void ronly(struct var *vp) |
| 1116 | { |
| 1117 | if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */ |
| 1118 | vp->status |= RONLY; |
| 1119 | } |
| 1120 | |
| 1121 | static int isassign(const char *s) |
| 1122 | { |
| 1123 | unsigned char c; |
| 1124 | DBGPRINTF7(("ISASSIGN: enter, s=%s\n", s)); |
| 1125 | |
| 1126 | /* no isalpha() - we shouldn't use locale */ |
| 1127 | c = *s; |
| 1128 | if (c != '_' |
| 1129 | && (unsigned)((c|0x20) - 'a') > 25 /* not letter */ |
| 1130 | ) { |
| 1131 | return 0; |
| 1132 | } |
| 1133 | while (1) { |
| 1134 | c = *++s; |
| 1135 | if (c == '\0') |
| 1136 | return 0; |
| 1137 | if (c == '=') |
| 1138 | return 1; |
| 1139 | c |= 0x20; /* lowercase letters, doesn't affect numbers */ |
| 1140 | if (c != '_' |
| 1141 | && (unsigned)(c - '0') > 9 /* not number */ |
| 1142 | && (unsigned)(c - 'a') > 25 /* not letter */ |
| 1143 | ) { |
| 1144 | return 0; |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | static int assign(const char *s, int cf) |
| 1150 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1151 | const char *cp; |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1152 | struct var *vp; |
| 1153 | |
| 1154 | DBGPRINTF7(("ASSIGN: enter, s=%s, cf=%d\n", s, cf)); |
| 1155 | |
| 1156 | if (!isalpha(*s) && *s != '_') |
| 1157 | return 0; |
| 1158 | for (cp = s; *cp != '='; cp++) |
| 1159 | if (*cp == '\0' || (!isalnum(*cp) && *cp != '_')) |
| 1160 | return 0; |
| 1161 | vp = lookup(s); |
| 1162 | nameval(vp, ++cp, cf == COPYV ? NULL : s); |
| 1163 | if (cf != COPYV) |
| 1164 | vp->status &= ~GETCELL; |
| 1165 | return 1; |
| 1166 | } |
| 1167 | |
| 1168 | static int checkname(char *cp) |
| 1169 | { |
| 1170 | DBGPRINTF7(("CHECKNAME: enter, cp=%s\n", cp)); |
| 1171 | |
| 1172 | if (!isalpha(*cp++) && *(cp - 1) != '_') |
| 1173 | return 0; |
| 1174 | while (*cp) |
| 1175 | if (!isalnum(*cp++) && *(cp - 1) != '_') |
| 1176 | return 0; |
| 1177 | return 1; |
| 1178 | } |
| 1179 | |
| 1180 | static void putvlist(int f, int out) |
| 1181 | { |
| 1182 | struct var *vp; |
| 1183 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1184 | for (vp = vlist; vp; vp = vp->next) { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1185 | if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) { |
| 1186 | if (vp->status & EXPORT) |
| 1187 | write(out, "export ", 7); |
| 1188 | if (vp->status & RONLY) |
| 1189 | write(out, "readonly ", 9); |
| 1190 | write(out, vp->name, (int) (findeq(vp->name) - vp->name)); |
| 1191 | write(out, "\n", 1); |
| 1192 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1193 | } |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | |
| 1197 | /* |
| 1198 | * trap handling |
| 1199 | */ |
| 1200 | static void sig(int i) |
| 1201 | { |
| 1202 | trapset = i; |
| 1203 | signal(i, sig); |
| 1204 | } |
| 1205 | |
| 1206 | static void runtrap(int i) |
| 1207 | { |
| 1208 | char *trapstr; |
| 1209 | |
| 1210 | trapstr = trap[i]; |
| 1211 | if (trapstr == NULL) |
| 1212 | return; |
| 1213 | |
| 1214 | if (i == 0) |
| 1215 | trap[i] = NULL; |
| 1216 | |
| 1217 | RUN(aword, trapstr, nlchar); |
| 1218 | } |
| 1219 | |
| 1220 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1221 | static void setdash(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1222 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1223 | char *cp; |
| 1224 | int c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1225 | char m['z' - 'a' + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1226 | |
| 1227 | cp = m; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1228 | for (c = 'a'; c <= 'z'; c++) |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1229 | if (flag[c]) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1230 | *cp++ = c; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1231 | *cp = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1232 | setval(lookup("-"), m); |
| 1233 | } |
| 1234 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1235 | static int newfile(char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1236 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1237 | int f; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1238 | |
| 1239 | DBGPRINTF7(("NEWFILE: opening %s\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1240 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1241 | f = 0; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 1242 | if (NOT_LONE_DASH(s)) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1243 | DBGPRINTF(("NEWFILE: s is %s\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1244 | f = open(s, 0); |
| 1245 | if (f < 0) { |
| 1246 | prs(s); |
| 1247 | err(": cannot open"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1248 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1249 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1250 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1251 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1252 | next(remap(f)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1253 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1256 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1257 | struct op *scantree(struct op *head) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1258 | { |
| 1259 | struct op *dotnode; |
| 1260 | |
| 1261 | if (head == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1262 | return NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1263 | |
| 1264 | if (head->left != NULL) { |
| 1265 | dotnode = scantree(head->left); |
| 1266 | if (dotnode) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1267 | return dotnode; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | if (head->right != NULL) { |
| 1271 | dotnode = scantree(head->right); |
| 1272 | if (dotnode) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1273 | return dotnode; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | if (head->words == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1277 | return NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1278 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1279 | DBGPRINTF5(("SCANTREE: checking node %p\n", head)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1280 | |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame^] | 1281 | if ((head->type != TDOT) && LONE_CHAR(head->words[0], '.')) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1282 | DBGPRINTF5(("SCANTREE: dot found in node %p\n", head)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1283 | return head; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1286 | return NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1290 | static void onecommand(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1291 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1292 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1293 | jmp_buf m1; |
| 1294 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1295 | DBGPRINTF(("ONECOMMAND: enter, outtree=%p\n", outtree)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1296 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1297 | while (e.oenv) |
| 1298 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1299 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1300 | areanum = 1; |
| 1301 | freehere(areanum); |
| 1302 | freearea(areanum); |
| 1303 | garbage(); |
| 1304 | wdlist = 0; |
| 1305 | iolist = 0; |
| 1306 | e.errpt = 0; |
| 1307 | e.linep = line; |
| 1308 | yynerrs = 0; |
| 1309 | multiline = 0; |
| 1310 | inparse = 1; |
| 1311 | intr = 0; |
| 1312 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1313 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1314 | failpt = m1; |
| 1315 | setjmp(failpt); /* Bruce Evans' fix */ |
| 1316 | failpt = m1; |
| 1317 | if (setjmp(failpt) || yyparse() || intr) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1318 | DBGPRINTF(("ONECOMMAND: this is not good.\n")); |
| 1319 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1320 | while (e.oenv) |
| 1321 | quitenv(); |
| 1322 | scraphere(); |
| 1323 | if (!interactive && intr) |
| 1324 | leave(); |
| 1325 | inparse = 0; |
| 1326 | intr = 0; |
| 1327 | return; |
| 1328 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1329 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1330 | inparse = 0; |
| 1331 | brklist = 0; |
| 1332 | intr = 0; |
| 1333 | execflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1334 | |
| 1335 | if (!flag['n']) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1336 | DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=%p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1337 | outtree)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1338 | execute(outtree, NOPIPE, NOPIPE, 0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1341 | if (!interactive && intr) { |
| 1342 | execflg = 0; |
| 1343 | leave(); |
| 1344 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1345 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1346 | i = trapset; |
| 1347 | if (i != 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1348 | trapset = 0; |
| 1349 | runtrap(i); |
| 1350 | } |
| 1351 | } |
| 1352 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1353 | static int newenv(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1354 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1355 | struct env *ep; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1356 | |
| 1357 | DBGPRINTF(("NEWENV: f=%d (indicates quitenv and return)\n", f)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1358 | |
| 1359 | if (f) { |
| 1360 | quitenv(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1361 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1362 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1363 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1364 | ep = (struct env *) space(sizeof(*ep)); |
| 1365 | if (ep == NULL) { |
| 1366 | while (e.oenv) |
| 1367 | quitenv(); |
| 1368 | fail(); |
| 1369 | } |
| 1370 | *ep = e; |
| 1371 | e.oenv = ep; |
| 1372 | e.errpt = errpt; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1373 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1374 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1377 | static void quitenv(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1378 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1379 | struct env *ep; |
| 1380 | int fd; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1381 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1382 | DBGPRINTF(("QUITENV: e.oenv=%p\n", e.oenv)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1383 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1384 | ep = e.oenv; |
| 1385 | if (ep != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1386 | fd = e.iofd; |
| 1387 | e = *ep; |
| 1388 | /* should close `'d files */ |
| 1389 | DELETE(ep); |
| 1390 | while (--fd >= e.iofd) |
| 1391 | close(fd); |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | /* |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1396 | * Is character c in s? |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1397 | */ |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1398 | static int any(int c, const char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1399 | { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1400 | while (*s) |
| 1401 | if (*s++ == c) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1402 | return 1; |
| 1403 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /* |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1407 | * Is any character from s1 in s2? |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1408 | */ |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1409 | static int anys(const char *s1, const char *s2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1410 | { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 1411 | while (*s1) |
| 1412 | if (any(*s1++, s2)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1413 | return 1; |
| 1414 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1417 | static char *putn(int n) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1418 | { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1419 | return itoa(n); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1422 | static void next(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1423 | { |
| 1424 | PUSHIO(afile, f, filechar); |
| 1425 | } |
| 1426 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1427 | static void onintr(int s) /* ANSI C requires a parameter */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1428 | { |
| 1429 | signal(SIGINT, onintr); |
| 1430 | intr = 1; |
| 1431 | if (interactive) { |
| 1432 | if (inparse) { |
| 1433 | prs("\n"); |
| 1434 | fail(); |
| 1435 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1436 | } else if (heedint) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1437 | execflg = 0; |
| 1438 | leave(); |
| 1439 | } |
| 1440 | } |
| 1441 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1442 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1443 | /* -------- gmatch.c -------- */ |
| 1444 | /* |
| 1445 | * int gmatch(string, pattern) |
| 1446 | * char *string, *pattern; |
| 1447 | * |
| 1448 | * Match a pattern as in sh(1). |
| 1449 | */ |
| 1450 | |
| 1451 | #define CMASK 0377 |
| 1452 | #define QUOTE 0200 |
| 1453 | #define QMASK (CMASK&~QUOTE) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1454 | #define NOT '!' /* might use ^ */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1455 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1456 | static const char *cclass(const char *p, int sub) |
| 1457 | { |
| 1458 | int c, d, not, found; |
| 1459 | |
| 1460 | not = (*p == NOT); |
| 1461 | if (not != 0) |
| 1462 | p++; |
| 1463 | found = not; |
| 1464 | do { |
| 1465 | if (*p == '\0') |
| 1466 | return NULL; |
| 1467 | c = *p & CMASK; |
| 1468 | if (p[1] == '-' && p[2] != ']') { |
| 1469 | d = p[2] & CMASK; |
| 1470 | p++; |
| 1471 | } else |
| 1472 | d = c; |
| 1473 | if (c == sub || (c <= sub && sub <= d)) |
| 1474 | found = !not; |
| 1475 | } while (*++p != ']'); |
| 1476 | return found ? p + 1 : NULL; |
| 1477 | } |
| 1478 | |
| 1479 | static int gmatch(const char *s, const char *p) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1480 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1481 | int sc, pc; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1482 | |
| 1483 | if (s == NULL || p == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1484 | return 0; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 1485 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1486 | while ((pc = *p++ & CMASK) != '\0') { |
| 1487 | sc = *s++ & QMASK; |
| 1488 | switch (pc) { |
| 1489 | case '[': |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1490 | p = cclass(p, sc); |
| 1491 | if (p == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1492 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1493 | break; |
| 1494 | |
| 1495 | case '?': |
| 1496 | if (sc == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1497 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1498 | break; |
| 1499 | |
| 1500 | case '*': |
| 1501 | s--; |
| 1502 | do { |
| 1503 | if (*p == '\0' || gmatch(s, p)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1504 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1505 | } while (*s++ != '\0'); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1506 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1507 | |
| 1508 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1509 | if (sc != (pc & ~QUOTE)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1510 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1511 | } |
| 1512 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1513 | return *s == '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1516 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1517 | /* -------- csyn.c -------- */ |
| 1518 | /* |
| 1519 | * shell: syntax (C version) |
| 1520 | */ |
| 1521 | |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1522 | static void yyerror(const char *s) ATTRIBUTE_NORETURN; |
| 1523 | static void yyerror(const char *s) |
| 1524 | { |
| 1525 | yynerrs++; |
| 1526 | if (interactive && e.iop <= iostack) { |
| 1527 | multiline = 0; |
| 1528 | while (eofc() == 0 && yylex(0) != '\n'); |
| 1529 | } |
| 1530 | err(s); |
| 1531 | fail(); |
| 1532 | } |
| 1533 | |
| 1534 | static void zzerr(void) ATTRIBUTE_NORETURN; |
| 1535 | static void zzerr(void) |
| 1536 | { |
| 1537 | yyerror("syntax error"); |
| 1538 | } |
| 1539 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1540 | int yyparse(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1541 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1542 | DBGPRINTF7(("YYPARSE: enter...\n")); |
| 1543 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1544 | startl = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1545 | peeksym = 0; |
| 1546 | yynerrs = 0; |
| 1547 | outtree = c_list(); |
| 1548 | musthave('\n', 0); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1549 | return (yynerrs != 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1552 | static struct op *pipeline(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1553 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1554 | struct op *t, *p; |
| 1555 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1556 | |
| 1557 | DBGPRINTF7(("PIPELINE: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1558 | |
| 1559 | t = command(cf); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1560 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1561 | DBGPRINTF9(("PIPELINE: t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1562 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1563 | if (t != NULL) { |
| 1564 | while ((c = yylex(0)) == '|') { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1565 | p = command(CONTIN); |
| 1566 | if (p == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1567 | DBGPRINTF8(("PIPELINE: error!\n")); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1568 | zzerr(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1571 | if (t->type != TPAREN && t->type != TCOM) { |
| 1572 | /* shell statement */ |
| 1573 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 1574 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1575 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1576 | t = block(TPIPE, t, p, NOWORDS); |
| 1577 | } |
| 1578 | peeksym = c; |
| 1579 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1580 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1581 | DBGPRINTF7(("PIPELINE: returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1582 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1585 | static struct op *andor(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1586 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1587 | struct op *t, *p; |
| 1588 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1589 | |
| 1590 | DBGPRINTF7(("ANDOR: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1591 | |
| 1592 | t = pipeline(0); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1593 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1594 | DBGPRINTF9(("ANDOR: t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1595 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1596 | if (t != NULL) { |
| 1597 | while ((c = yylex(0)) == LOGAND || c == LOGOR) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1598 | p = pipeline(CONTIN); |
| 1599 | if (p == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1600 | DBGPRINTF8(("ANDOR: error!\n")); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1601 | zzerr(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1604 | t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1605 | } /* WHILE */ |
| 1606 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1607 | peeksym = c; |
| 1608 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1609 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1610 | DBGPRINTF7(("ANDOR: returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1611 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1614 | static struct op *c_list(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1615 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1616 | struct op *t, *p; |
| 1617 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1618 | |
| 1619 | DBGPRINTF7(("C_LIST: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1620 | |
| 1621 | t = andor(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1622 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1623 | if (t != NULL) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1624 | peeksym = yylex(0); |
| 1625 | if (peeksym == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1626 | t = block(TASYNC, t, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1627 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1628 | while ((c = yylex(0)) == ';' || c == '&' |
| 1629 | || (multiline && c == '\n')) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1630 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1631 | p = andor(); |
| 1632 | if (p== NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1633 | return t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1634 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1635 | peeksym = yylex(0); |
| 1636 | if (peeksym == '&') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1637 | p = block(TASYNC, p, NOBLOCK, NOWORDS); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1638 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1639 | t = list(t, p); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1640 | } /* WHILE */ |
| 1641 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1642 | peeksym = c; |
| 1643 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1644 | /* IF */ |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1645 | DBGPRINTF7(("C_LIST: returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1646 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1649 | static int synio(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1650 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1651 | struct ioword *iop; |
| 1652 | int i; |
| 1653 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1654 | |
| 1655 | DBGPRINTF7(("SYNIO: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1656 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1657 | c = yylex(cf); |
| 1658 | if (c != '<' && c != '>') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1659 | peeksym = c; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1660 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1661 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1662 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1663 | i = yylval.i; |
| 1664 | musthave(WORD, 0); |
| 1665 | iop = io(iounit, i, yylval.cp); |
| 1666 | iounit = IODEFAULT; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1667 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1668 | if (i & IOHERE) |
| 1669 | markhere(yylval.cp, iop); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1670 | |
| 1671 | DBGPRINTF7(("SYNIO: returning 1\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1672 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1675 | static void musthave(int c, int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1676 | { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 1677 | peeksym = yylex(cf); |
| 1678 | if (peeksym != c) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1679 | DBGPRINTF7(("MUSTHAVE: error!\n")); |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1680 | zzerr(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1683 | peeksym = 0; |
| 1684 | } |
| 1685 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1686 | static struct op *simple(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1687 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1688 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1689 | |
| 1690 | t = NULL; |
| 1691 | for (;;) { |
| 1692 | switch (peeksym = yylex(0)) { |
| 1693 | case '<': |
| 1694 | case '>': |
| 1695 | (void) synio(0); |
| 1696 | break; |
| 1697 | |
| 1698 | case WORD: |
| 1699 | if (t == NULL) { |
| 1700 | t = newtp(); |
| 1701 | t->type = TCOM; |
| 1702 | } |
| 1703 | peeksym = 0; |
| 1704 | word(yylval.cp); |
| 1705 | break; |
| 1706 | |
| 1707 | default: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1708 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | } |
| 1712 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1713 | static struct op *nested(int type, int mark) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1714 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1715 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1716 | |
| 1717 | DBGPRINTF3(("NESTED: enter, type=%d, mark=%d\n", type, mark)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1718 | |
| 1719 | multiline++; |
| 1720 | t = c_list(); |
| 1721 | musthave(mark, 0); |
| 1722 | multiline--; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1723 | return block(type, t, NOBLOCK, NOWORDS); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1726 | static struct op *command(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1727 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1728 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1729 | struct wdblock *iosave; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1730 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1731 | |
| 1732 | DBGPRINTF(("COMMAND: enter, cf=%d\n", cf)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1733 | |
| 1734 | iosave = iolist; |
| 1735 | iolist = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1736 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1737 | if (multiline) |
| 1738 | cf |= CONTIN; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1739 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1740 | while (synio(cf)) |
| 1741 | cf = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1742 | |
| 1743 | c = yylex(cf); |
| 1744 | |
| 1745 | switch (c) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1746 | default: |
| 1747 | peeksym = c; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1748 | t = simple(); |
| 1749 | if (t == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1750 | if (iolist == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1751 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1752 | t = newtp(); |
| 1753 | t->type = TCOM; |
| 1754 | } |
| 1755 | break; |
| 1756 | |
| 1757 | case '(': |
| 1758 | t = nested(TPAREN, ')'); |
| 1759 | break; |
| 1760 | |
| 1761 | case '{': |
| 1762 | t = nested(TBRACE, '}'); |
| 1763 | break; |
| 1764 | |
| 1765 | case FOR: |
| 1766 | t = newtp(); |
| 1767 | t->type = TFOR; |
| 1768 | musthave(WORD, 0); |
| 1769 | startl = 1; |
| 1770 | t->str = yylval.cp; |
| 1771 | multiline++; |
| 1772 | t->words = wordlist(); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1773 | c = yylex(0); |
| 1774 | if (c != '\n' && c != ';') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1775 | peeksym = c; |
| 1776 | t->left = dogroup(0); |
| 1777 | multiline--; |
| 1778 | break; |
| 1779 | |
| 1780 | case WHILE: |
| 1781 | case UNTIL: |
| 1782 | multiline++; |
| 1783 | t = newtp(); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1784 | t->type = c == WHILE ? TWHILE : TUNTIL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1785 | t->left = c_list(); |
| 1786 | t->right = dogroup(1); |
| 1787 | t->words = NULL; |
| 1788 | multiline--; |
| 1789 | break; |
| 1790 | |
| 1791 | case CASE: |
| 1792 | t = newtp(); |
| 1793 | t->type = TCASE; |
| 1794 | musthave(WORD, 0); |
| 1795 | t->str = yylval.cp; |
| 1796 | startl++; |
| 1797 | multiline++; |
| 1798 | musthave(IN, CONTIN); |
| 1799 | startl++; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1800 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1801 | t->left = caselist(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1802 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1803 | musthave(ESAC, 0); |
| 1804 | multiline--; |
| 1805 | break; |
| 1806 | |
| 1807 | case IF: |
| 1808 | multiline++; |
| 1809 | t = newtp(); |
| 1810 | t->type = TIF; |
| 1811 | t->left = c_list(); |
| 1812 | t->right = thenpart(); |
| 1813 | musthave(FI, 0); |
| 1814 | multiline--; |
| 1815 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1816 | |
| 1817 | case DOT: |
| 1818 | t = newtp(); |
| 1819 | t->type = TDOT; |
| 1820 | |
| 1821 | musthave(WORD, 0); /* gets name of file */ |
| 1822 | DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp)); |
| 1823 | |
| 1824 | word(yylval.cp); /* add word to wdlist */ |
| 1825 | word(NOWORD); /* terminate wdlist */ |
| 1826 | t->words = copyw(); /* dup wdlist */ |
| 1827 | break; |
| 1828 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1829 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1830 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 1831 | while (synio(0)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1832 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1833 | t = namelist(t); |
| 1834 | iolist = iosave; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1835 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1836 | DBGPRINTF(("COMMAND: returning %p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1837 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1838 | return t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1841 | static struct op *dowholefile(int type, int mark) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1842 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1843 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1844 | |
| 1845 | DBGPRINTF(("DOWHOLEFILE: enter, type=%d, mark=%d\n", type, mark)); |
| 1846 | |
| 1847 | multiline++; |
| 1848 | t = c_list(); |
| 1849 | multiline--; |
| 1850 | t = block(type, t, NOBLOCK, NOWORDS); |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1851 | DBGPRINTF(("DOWHOLEFILE: return t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1852 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1855 | static struct op *dogroup(int onlydone) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1856 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1857 | int c; |
| 1858 | struct op *mylist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1859 | |
| 1860 | c = yylex(CONTIN); |
| 1861 | if (c == DONE && onlydone) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1862 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1863 | if (c != DO) |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1864 | zzerr(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1865 | mylist = c_list(); |
| 1866 | musthave(DONE, 0); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1867 | return mylist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1870 | static struct op *thenpart(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1871 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1872 | int c; |
| 1873 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1874 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1875 | c = yylex(0); |
| 1876 | if (c != THEN) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1877 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1878 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1879 | } |
| 1880 | t = newtp(); |
| 1881 | t->type = 0; |
| 1882 | t->left = c_list(); |
| 1883 | if (t->left == NULL) |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1884 | zzerr(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1885 | t->right = elsepart(); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1886 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1889 | static struct op *elsepart(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1890 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1891 | int c; |
| 1892 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1893 | |
| 1894 | switch (c = yylex(0)) { |
| 1895 | case ELSE: |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1896 | t = c_list(); |
| 1897 | if (t == NULL) |
Denis Vlasenko | 6b50f73 | 2007-02-01 01:43:39 +0000 | [diff] [blame] | 1898 | zzerr(); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1899 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1900 | |
| 1901 | case ELIF: |
| 1902 | t = newtp(); |
| 1903 | t->type = TELIF; |
| 1904 | t->left = c_list(); |
| 1905 | t->right = thenpart(); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1906 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1907 | |
| 1908 | default: |
| 1909 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1910 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1911 | } |
| 1912 | } |
| 1913 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1914 | static struct op *caselist(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1915 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1916 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1917 | |
| 1918 | t = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1919 | while ((peeksym = yylex(CONTIN)) != ESAC) { |
| 1920 | DBGPRINTF(("CASELIST, doing yylex, peeksym=%d\n", peeksym)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1921 | t = list(t, casepart()); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1924 | DBGPRINTF(("CASELIST, returning t=%p\n", t)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1925 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1928 | static struct op *casepart(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1929 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1930 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1931 | |
| 1932 | DBGPRINTF7(("CASEPART: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1933 | |
| 1934 | t = newtp(); |
| 1935 | t->type = TPAT; |
| 1936 | t->words = pattern(); |
| 1937 | musthave(')', 0); |
| 1938 | t->left = c_list(); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1939 | peeksym = yylex(CONTIN); |
| 1940 | if (peeksym != ESAC) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1941 | musthave(BREAK, CONTIN); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1942 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1943 | DBGPRINTF7(("CASEPART: made newtp(TPAT, t=%p)\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1944 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1945 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1948 | static char **pattern(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1949 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1950 | int c, cf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1951 | |
| 1952 | cf = CONTIN; |
| 1953 | do { |
| 1954 | musthave(WORD, cf); |
| 1955 | word(yylval.cp); |
| 1956 | cf = 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1957 | c = yylex(0); |
| 1958 | } while (c == '|'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1959 | peeksym = c; |
| 1960 | word(NOWORD); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1961 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1962 | return copyw(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1965 | static char **wordlist(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1966 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1967 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1968 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 1969 | c = yylex(0); |
| 1970 | if (c != IN) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1971 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1972 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1973 | } |
| 1974 | startl = 0; |
| 1975 | while ((c = yylex(0)) == WORD) |
| 1976 | word(yylval.cp); |
| 1977 | word(NOWORD); |
| 1978 | peeksym = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1979 | return copyw(); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | /* |
| 1983 | * supporting functions |
| 1984 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1985 | static struct op *list(struct op *t1, struct op *t2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1986 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 1987 | DBGPRINTF7(("LIST: enter, t1=%p, t2=%p\n", t1, t2)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1988 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1989 | if (t1 == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1990 | return t2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1991 | if (t2 == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1992 | return t1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 1993 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1994 | return block(TLIST, t1, t2, NOWORDS); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 1997 | 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] | 1998 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1999 | struct op *t; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2000 | |
| 2001 | DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type])); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2002 | |
| 2003 | t = newtp(); |
| 2004 | t->type = type; |
| 2005 | t->left = t1; |
| 2006 | t->right = t2; |
| 2007 | t->words = wp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2008 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2009 | DBGPRINTF7(("BLOCK: inserted %p between %p and %p\n", t, t1, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2010 | t2)); |
| 2011 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2012 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2015 | /* See if given string is a shell multiline (FOR, IF, etc) */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2016 | static int rlookup(char *n) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2017 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2018 | const struct res *rp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2019 | |
| 2020 | DBGPRINTF7(("RLOOKUP: enter, n is %s\n", n)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2021 | |
| 2022 | for (rp = restab; rp->r_name; rp++) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2023 | if (strcmp(rp->r_name, n) == 0) { |
| 2024 | DBGPRINTF7(("RLOOKUP: match, returning %d\n", rp->r_val)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2025 | return rp->r_val; /* Return numeric code for shell multiline */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | DBGPRINTF7(("RLOOKUP: NO match, returning 0\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2029 | return 0; /* Not a shell multiline */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2032 | static struct op *newtp(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2033 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2034 | struct op *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2035 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2036 | t = (struct op *) tree(sizeof(*t)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2037 | t->type = 0; |
| 2038 | t->words = NULL; |
| 2039 | t->ioact = NULL; |
| 2040 | t->left = NULL; |
| 2041 | t->right = NULL; |
| 2042 | t->str = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2043 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2044 | DBGPRINTF3(("NEWTP: allocated %p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2045 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2046 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2049 | static struct op *namelist(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2050 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2051 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2052 | DBGPRINTF7(("NAMELIST: enter, t=%p, type %s, iolist=%p\n", t, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2053 | T_CMD_NAMES[t->type], iolist)); |
| 2054 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2055 | if (iolist) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2056 | iolist = addword((char *) NULL, iolist); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2057 | t->ioact = copyio(); |
| 2058 | } else |
| 2059 | t->ioact = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2060 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2061 | if (t->type != TCOM) { |
| 2062 | if (t->type != TPAREN && t->ioact != NULL) { |
| 2063 | t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| 2064 | t->ioact = t->left->ioact; |
| 2065 | t->left->ioact = NULL; |
| 2066 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2067 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2068 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2069 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2070 | word(NOWORD); |
| 2071 | t->words = copyw(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2072 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2073 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2076 | static char **copyw(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2077 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2078 | char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2079 | |
| 2080 | wd = getwords(wdlist); |
| 2081 | wdlist = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2082 | return wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2085 | static void word(char *cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2086 | { |
| 2087 | wdlist = addword(cp, wdlist); |
| 2088 | } |
| 2089 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2090 | static struct ioword **copyio(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2091 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2092 | struct ioword **iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2093 | |
| 2094 | iop = (struct ioword **) getwords(iolist); |
| 2095 | iolist = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2096 | return iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2099 | static struct ioword *io(int u, int f, char *cp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2100 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2101 | struct ioword *iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2102 | |
| 2103 | iop = (struct ioword *) tree(sizeof(*iop)); |
| 2104 | iop->io_unit = u; |
| 2105 | iop->io_flag = f; |
| 2106 | iop->io_name = cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2107 | iolist = addword((char *) iop, iolist); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2108 | return iop; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2111 | static int yylex(int cf) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2112 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2113 | int c, c1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2114 | int atstart; |
| 2115 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2116 | c = peeksym; |
| 2117 | if (c > 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2118 | peeksym = 0; |
| 2119 | if (c == '\n') |
| 2120 | startl = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2121 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2122 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2123 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2124 | nlseen = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2125 | atstart = startl; |
| 2126 | startl = 0; |
| 2127 | yylval.i = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2128 | e.linep = line; |
| 2129 | |
| 2130 | /* MALAMO */ |
| 2131 | line[LINELIM - 1] = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2132 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2133 | loop: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2134 | while ((c = my_getc(0)) == ' ' || c == '\t') /* Skip whitespace */ |
| 2135 | ; |
| 2136 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2137 | switch (c) { |
| 2138 | default: |
| 2139 | if (any(c, "0123456789")) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2140 | c1 = my_getc(0); |
| 2141 | unget(c1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2142 | if (c1 == '<' || c1 == '>') { |
| 2143 | iounit = c - '0'; |
| 2144 | goto loop; |
| 2145 | } |
| 2146 | *e.linep++ = c; |
| 2147 | c = c1; |
| 2148 | } |
| 2149 | break; |
| 2150 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2151 | case '#': /* Comment, skip to next newline or End-of-string */ |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2152 | while ((c = my_getc(0)) != '\0' && c != '\n'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2153 | unget(c); |
| 2154 | goto loop; |
| 2155 | |
| 2156 | case 0: |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2157 | DBGPRINTF5(("YYLEX: return 0, c=%d\n", c)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2158 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2159 | |
| 2160 | case '$': |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2161 | DBGPRINTF9(("YYLEX: found $\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2162 | *e.linep++ = c; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2163 | c = my_getc(0); |
| 2164 | if (c == '{') { |
| 2165 | c = collect(c, '}'); |
| 2166 | if (c != '\0') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2167 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2168 | goto pack; |
| 2169 | } |
| 2170 | break; |
| 2171 | |
| 2172 | case '`': |
| 2173 | case '\'': |
| 2174 | case '"': |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2175 | c = collect(c, c); |
| 2176 | if (c != '\0') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2177 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2178 | goto pack; |
| 2179 | |
| 2180 | case '|': |
| 2181 | case '&': |
| 2182 | case ';': |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2183 | startl = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2184 | /* If more chars process them, else return NULL char */ |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2185 | c1 = dual(c); |
| 2186 | if (c1 != '\0') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2187 | return c1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2188 | return c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2189 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2190 | case '^': |
| 2191 | startl = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2192 | return '|'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2193 | case '>': |
| 2194 | case '<': |
| 2195 | diag(c); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2196 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2197 | |
| 2198 | case '\n': |
| 2199 | nlseen++; |
| 2200 | gethere(); |
| 2201 | startl = 1; |
| 2202 | if (multiline || cf & CONTIN) { |
| 2203 | if (interactive && e.iop <= iostack) { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 2204 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2205 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2206 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2207 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2208 | #endif |
| 2209 | } |
| 2210 | if (cf & CONTIN) |
| 2211 | goto loop; |
| 2212 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2213 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2214 | |
| 2215 | case '(': |
| 2216 | case ')': |
| 2217 | startl = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2218 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | unget(c); |
| 2222 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2223 | pack: |
| 2224 | while ((c = my_getc(0)) != '\0' && !any(c, "`$ '\"\t;&<>()|^\n")) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2225 | if (e.linep >= elinep) |
| 2226 | err("word too long"); |
| 2227 | else |
| 2228 | *e.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2229 | }; |
| 2230 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2231 | unget(c); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2232 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2233 | if (any(c, "\"'`$")) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2234 | goto loop; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2235 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2236 | *e.linep++ = '\0'; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2237 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2238 | if (atstart) { |
| 2239 | c = rlookup(line); |
| 2240 | if (c != 0) { |
| 2241 | startl = 1; |
| 2242 | return c; |
| 2243 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2244 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2245 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2246 | yylval.cp = strsave(line, areanum); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2247 | return WORD; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2250 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2251 | static int collect(int c, int c1) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2252 | { |
| 2253 | char s[2]; |
| 2254 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2255 | DBGPRINTF8(("COLLECT: enter, c=%d, c1=%d\n", c, c1)); |
| 2256 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2257 | *e.linep++ = c; |
| 2258 | while ((c = my_getc(c1)) != c1) { |
| 2259 | if (c == 0) { |
| 2260 | unget(c); |
| 2261 | s[0] = c1; |
| 2262 | s[1] = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2263 | prs("no closing "); |
| 2264 | yyerror(s); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2265 | return YYERRCODE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2266 | } |
| 2267 | if (interactive && c == '\n' && e.iop <= iostack) { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 2268 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2269 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2270 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2271 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2272 | #endif |
| 2273 | } |
| 2274 | *e.linep++ = c; |
| 2275 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2276 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2277 | *e.linep++ = c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2278 | |
| 2279 | DBGPRINTF8(("COLLECT: return 0, line is %s\n", line)); |
| 2280 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2281 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2284 | /* "multiline commands" helper func */ |
| 2285 | /* see if next 2 chars form a shell multiline */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2286 | static int dual(int c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2287 | { |
| 2288 | char s[3]; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2289 | char *cp = s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2290 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2291 | DBGPRINTF8(("DUAL: enter, c=%d\n", c)); |
| 2292 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2293 | *cp++ = c; /* c is the given "peek" char */ |
| 2294 | *cp++ = my_getc(0); /* get next char of input */ |
| 2295 | *cp = '\0'; /* add EOS marker */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2296 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2297 | c = rlookup(s); /* see if 2 chars form a shell multiline */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2298 | if (c == 0) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2299 | unget(*--cp); /* String is not a shell multiline, put peek char back */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2300 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2301 | return c; /* String is multiline, return numeric multiline (restab) code */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2304 | static void diag(int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2305 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2306 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2307 | |
| 2308 | DBGPRINTF8(("DIAG: enter, ec=%d\n", ec)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2309 | |
| 2310 | c = my_getc(0); |
| 2311 | if (c == '>' || c == '<') { |
| 2312 | if (c != ec) |
| 2313 | zzerr(); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2314 | yylval.i = (ec == '>' ? IOWRITE | IOCAT : IOHERE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2315 | c = my_getc(0); |
| 2316 | } else |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2317 | yylval.i = (ec == '>' ? IOWRITE : IOREAD); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2318 | if (c != '&' || yylval.i == IOHERE) |
| 2319 | unget(c); |
| 2320 | else |
| 2321 | yylval.i |= IODUP; |
| 2322 | } |
| 2323 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2324 | static char *tree(unsigned size) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2325 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2326 | char *t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2327 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2328 | t = getcell(size); |
| 2329 | if (t == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2330 | DBGPRINTF2(("TREE: getcell(%d) failed!\n", size)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2331 | prs("command line too complicated\n"); |
| 2332 | fail(); |
| 2333 | /* NOTREACHED */ |
| 2334 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2335 | return t; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2338 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2339 | /* VARARGS1 */ |
| 2340 | /* ARGSUSED */ |
| 2341 | |
| 2342 | /* -------- exec.c -------- */ |
| 2343 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2344 | static struct op **find1case(struct op *t, const char *w) |
| 2345 | { |
| 2346 | struct op *t1; |
| 2347 | struct op **tp; |
| 2348 | char **wp; |
| 2349 | char *cp; |
| 2350 | |
| 2351 | if (t == NULL) { |
| 2352 | DBGPRINTF3(("FIND1CASE: enter, t==NULL, returning.\n")); |
| 2353 | return NULL; |
| 2354 | } |
| 2355 | |
| 2356 | DBGPRINTF3(("FIND1CASE: enter, t->type=%d (%s)\n", t->type, |
| 2357 | T_CMD_NAMES[t->type])); |
| 2358 | |
| 2359 | if (t->type == TLIST) { |
| 2360 | tp = find1case(t->left, w); |
| 2361 | if (tp != NULL) { |
| 2362 | DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=%p\n", tp)); |
| 2363 | return tp; |
| 2364 | } |
| 2365 | t1 = t->right; /* TPAT */ |
| 2366 | } else |
| 2367 | t1 = t; |
| 2368 | |
| 2369 | for (wp = t1->words; *wp;) { |
| 2370 | cp = evalstr(*wp++, DOSUB); |
| 2371 | if (cp && gmatch(w, cp)) { |
| 2372 | DBGPRINTF3(("FIND1CASE: returning &t1->left= %p.\n", |
| 2373 | &t1->left)); |
| 2374 | return &t1->left; |
| 2375 | } |
| 2376 | } |
| 2377 | |
| 2378 | DBGPRINTF(("FIND1CASE: returning NULL\n")); |
| 2379 | return NULL; |
| 2380 | } |
| 2381 | |
| 2382 | static struct op *findcase(struct op *t, const char *w) |
| 2383 | { |
| 2384 | struct op **tp; |
| 2385 | |
| 2386 | tp = find1case(t, w); |
| 2387 | return tp != NULL ? *tp : NULL; |
| 2388 | } |
| 2389 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2390 | /* |
| 2391 | * execute tree |
| 2392 | */ |
| 2393 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2394 | static int execute(struct op *t, int *pin, int *pout, int act) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2395 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2396 | struct op *t1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2397 | volatile int i, rv, a; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2398 | const char *cp; |
| 2399 | char **wp, **wp2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2400 | struct var *vp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2401 | struct op *outtree_save; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2402 | struct brkcon bc; |
| 2403 | |
| 2404 | #if __GNUC__ |
| 2405 | /* Avoid longjmp clobbering */ |
| 2406 | (void) ℘ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2407 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2408 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2409 | if (t == NULL) { |
| 2410 | DBGPRINTF4(("EXECUTE: enter, t==null, returning.\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2411 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2414 | 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] | 2415 | t->type, T_CMD_NAMES[t->type], |
| 2416 | ((t->words == NULL) ? "NULL" : t->words[0]))); |
| 2417 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2418 | rv = 0; |
| 2419 | a = areanum++; |
| 2420 | wp = (wp2 = t->words) != NULL |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2421 | ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY) |
| 2422 | : NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2423 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2424 | switch (t->type) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2425 | case TDOT: |
| 2426 | DBGPRINTF3(("EXECUTE: TDOT\n")); |
| 2427 | |
| 2428 | outtree_save = outtree; |
| 2429 | |
| 2430 | newfile(evalstr(t->words[0], DOALL)); |
| 2431 | |
| 2432 | t->left = dowholefile(TLIST, 0); |
| 2433 | t->right = NULL; |
| 2434 | |
| 2435 | outtree = outtree_save; |
| 2436 | |
| 2437 | if (t->left) |
| 2438 | rv = execute(t->left, pin, pout, 0); |
| 2439 | if (t->right) |
| 2440 | rv = execute(t->right, pin, pout, 0); |
| 2441 | break; |
| 2442 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2443 | case TPAREN: |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 2444 | rv = execute(t->left, pin, pout, 0); |
| 2445 | break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2446 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2447 | case TCOM: |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 2448 | rv = forkexec(t, pin, pout, act, wp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2449 | break; |
| 2450 | |
| 2451 | case TPIPE: |
| 2452 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2453 | int pv[2]; |
| 2454 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2455 | rv = openpipe(pv); |
| 2456 | if (rv < 0) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2457 | break; |
| 2458 | pv[0] = remap(pv[0]); |
| 2459 | pv[1] = remap(pv[1]); |
| 2460 | (void) execute(t->left, pin, pv, 0); |
| 2461 | rv = execute(t->right, pv, pout, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2462 | } |
| 2463 | break; |
| 2464 | |
| 2465 | case TLIST: |
| 2466 | (void) execute(t->left, pin, pout, 0); |
| 2467 | rv = execute(t->right, pin, pout, 0); |
| 2468 | break; |
| 2469 | |
| 2470 | case TASYNC: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2471 | { |
| 2472 | int hinteractive = interactive; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2473 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2474 | DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n")); |
| 2475 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2476 | i = vfork(); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2477 | if (i == 0) { /* child */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2478 | signal(SIGINT, SIG_IGN); |
| 2479 | signal(SIGQUIT, SIG_IGN); |
| 2480 | if (interactive) |
| 2481 | signal(SIGTERM, SIG_DFL); |
| 2482 | interactive = 0; |
| 2483 | if (pin == NULL) { |
| 2484 | close(0); |
"Vladimir N. Oleynik" | 6c35c7c | 2005-10-12 15:34:25 +0000 | [diff] [blame] | 2485 | open(bb_dev_null, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2486 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2487 | _exit(execute(t->left, pin, pout, FEXEC)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2488 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2489 | interactive = hinteractive; |
| 2490 | if (i != -1) { |
| 2491 | setval(lookup("!"), putn(i)); |
| 2492 | if (pin != NULL) |
| 2493 | closepipe(pin); |
| 2494 | if (interactive) { |
| 2495 | prs(putn(i)); |
| 2496 | prs("\n"); |
| 2497 | } |
| 2498 | } else |
| 2499 | rv = -1; |
| 2500 | setstatus(rv); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2501 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2502 | break; |
| 2503 | |
| 2504 | case TOR: |
| 2505 | case TAND: |
| 2506 | rv = execute(t->left, pin, pout, 0); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2507 | t1 = t->right; |
| 2508 | if (t1 != NULL && (rv == 0) == (t->type == TAND)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2509 | rv = execute(t1, pin, pout, 0); |
| 2510 | break; |
| 2511 | |
| 2512 | case TFOR: |
| 2513 | if (wp == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2514 | wp = dolv + 1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2515 | i = dolc; |
| 2516 | if (i < 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2517 | i = 0; |
| 2518 | } else { |
| 2519 | i = -1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2520 | while (*wp++ != NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2521 | } |
| 2522 | vp = lookup(t->str); |
| 2523 | while (setjmp(bc.brkpt)) |
| 2524 | if (isbreak) |
| 2525 | goto broken; |
| 2526 | brkset(&bc); |
| 2527 | for (t1 = t->left; i-- && *wp != NULL;) { |
| 2528 | setval(vp, *wp++); |
| 2529 | rv = execute(t1, pin, pout, 0); |
| 2530 | } |
| 2531 | brklist = brklist->nextlev; |
| 2532 | break; |
| 2533 | |
| 2534 | case TWHILE: |
| 2535 | case TUNTIL: |
| 2536 | while (setjmp(bc.brkpt)) |
| 2537 | if (isbreak) |
| 2538 | goto broken; |
| 2539 | brkset(&bc); |
| 2540 | t1 = t->left; |
| 2541 | while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE)) |
| 2542 | rv = execute(t->right, pin, pout, 0); |
| 2543 | brklist = brklist->nextlev; |
| 2544 | break; |
| 2545 | |
| 2546 | case TIF: |
| 2547 | case TELIF: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2548 | if (t->right != NULL) { |
| 2549 | rv = !execute(t->left, pin, pout, 0) ? |
| 2550 | execute(t->right->left, pin, pout, 0) : |
| 2551 | execute(t->right->right, pin, pout, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2552 | } |
| 2553 | break; |
| 2554 | |
| 2555 | case TCASE: |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2556 | cp = evalstr(t->str, DOSUB | DOTRIM); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2557 | if (cp == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2558 | cp = ""; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2559 | |
| 2560 | DBGPRINTF7(("EXECUTE: TCASE, t->str is %s, cp is %s\n", |
| 2561 | ((t->str == NULL) ? "NULL" : t->str), |
| 2562 | ((cp == NULL) ? "NULL" : cp))); |
| 2563 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2564 | t1 = findcase(t->left, cp); |
| 2565 | if (t1 != NULL) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2566 | DBGPRINTF7(("EXECUTE: TCASE, calling execute(t=%p, t1=%p)...\n", t, t1)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2567 | rv = execute(t1, pin, pout, 0); |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2568 | 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] | 2569 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2570 | break; |
| 2571 | |
| 2572 | case TBRACE: |
| 2573 | /* |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2574 | iopp = t->ioact; |
| 2575 | if (i) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2576 | while (*iopp) |
| 2577 | if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) { |
| 2578 | rv = -1; |
| 2579 | break; |
| 2580 | } |
| 2581 | */ |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2582 | if (rv >= 0) { |
| 2583 | t1 = t->left; |
| 2584 | if (t1) { |
| 2585 | rv = execute(t1, pin, pout, 0); |
| 2586 | } |
| 2587 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2588 | break; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2589 | |
| 2590 | }; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2591 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2592 | broken: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2593 | t->words = wp2; |
| 2594 | isbreak = 0; |
| 2595 | freehere(areanum); |
| 2596 | freearea(areanum); |
| 2597 | areanum = a; |
| 2598 | if (interactive && intr) { |
| 2599 | closeall(); |
| 2600 | fail(); |
| 2601 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2602 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2603 | i = trapset; |
| 2604 | if (i != 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2605 | trapset = 0; |
| 2606 | runtrap(i); |
| 2607 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2608 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2609 | DBGPRINTF(("EXECUTE: returning from t=%p, rv=%d\n", t, rv)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2610 | return rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2613 | typedef int (*builtin_func_ptr)(struct op *); |
| 2614 | |
| 2615 | static builtin_func_ptr inbuilt(const char *s) { |
| 2616 | const struct builtincmd *bp; |
| 2617 | |
| 2618 | for (bp = builtincmds; bp->name != NULL; bp++) |
| 2619 | if (strcmp(bp->name, s) == 0) |
| 2620 | return bp->builtinfunc; |
| 2621 | |
| 2622 | return NULL; |
| 2623 | } |
| 2624 | |
| 2625 | static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2626 | { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2627 | pid_t newpid; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2628 | int i, rv; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2629 | builtin_func_ptr shcom = NULL; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2630 | int f; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2631 | const char *cp = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2632 | struct ioword **iopp; |
| 2633 | int resetsig; |
| 2634 | char **owp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2635 | int forked = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2636 | |
| 2637 | int *hpin = pin; |
| 2638 | int *hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2639 | char *hwp; |
| 2640 | int hinteractive; |
| 2641 | int hintr; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2642 | struct brkcon *hbrklist; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2643 | int hexecflg; |
| 2644 | |
| 2645 | #if __GNUC__ |
| 2646 | /* Avoid longjmp clobbering */ |
| 2647 | (void) &pin; |
| 2648 | (void) &pout; |
| 2649 | (void) ℘ |
| 2650 | (void) &shcom; |
| 2651 | (void) &cp; |
| 2652 | (void) &resetsig; |
| 2653 | (void) &owp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2654 | #endif |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2655 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2656 | 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] | 2657 | pout, act)); |
| 2658 | DBGPRINTF7(("FORKEXEC: t->words is %s\n", |
| 2659 | ((t->words == NULL) ? "NULL" : t->words[0]))); |
| 2660 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2661 | owp = wp; |
| 2662 | resetsig = 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2663 | rv = -1; /* system-detected error */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2664 | if (t->type == TCOM) { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2665 | while (*wp++ != NULL); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2666 | cp = *wp; |
| 2667 | |
| 2668 | /* strip all initial assignments */ |
| 2669 | /* not correct wrt PATH=yyy command etc */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2670 | if (flag['x']) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2671 | DBGPRINTF9(("FORKEXEC: echo'ing, cp=%p, wp=%p, owp=%p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2672 | cp, wp, owp)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2673 | echo(cp ? wp : owp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2674 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2675 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2676 | if (cp == NULL && t->ioact == NULL) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2677 | while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
| 2678 | /**/; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2679 | DBGPRINTF(("FORKEXEC: returning setstatus()\n")); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2680 | return setstatus(0); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2681 | } |
| 2682 | if (cp != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2683 | shcom = inbuilt(cp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2684 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2685 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2686 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2687 | t->words = wp; |
| 2688 | f = act; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2689 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2690 | DBGPRINTF(("FORKEXEC: shcom %p, f&FEXEC 0x%x, owp %p\n", shcom, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2691 | f & FEXEC, owp)); |
| 2692 | |
| 2693 | if (shcom == NULL && (f & FEXEC) == 0) { |
| 2694 | /* Save values in case the child process alters them */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2695 | hpin = pin; |
| 2696 | hpout = pout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2697 | hwp = *wp; |
| 2698 | hinteractive = interactive; |
| 2699 | hintr = intr; |
| 2700 | hbrklist = brklist; |
| 2701 | hexecflg = execflg; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2702 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2703 | DBGPRINTF3(("FORKEXEC: calling vfork()...\n")); |
| 2704 | |
| 2705 | newpid = vfork(); |
| 2706 | |
| 2707 | if (newpid == -1) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 2708 | DBGPRINTF(("FORKEXEC: ERROR, cannot vfork()!\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2709 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 2712 | if (newpid > 0) { /* Parent */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2713 | /* Restore values */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2714 | pin = hpin; |
| 2715 | pout = hpout; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2716 | *wp = hwp; |
| 2717 | interactive = hinteractive; |
| 2718 | intr = hintr; |
| 2719 | brklist = hbrklist; |
| 2720 | execflg = hexecflg; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2721 | /* moved up |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2722 | if (i == -1) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2723 | return rv; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2724 | */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2725 | if (pin != NULL) |
| 2726 | closepipe(pin); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2727 | |
| 2728 | return (pout == NULL ? setstatus(waitfor(newpid, 0)) : 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2731 | /* Must be the child process, pid should be 0 */ |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2732 | DBGPRINTF(("FORKEXEC: child process, shcom=%p\n", shcom)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2733 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2734 | if (interactive) { |
| 2735 | signal(SIGINT, SIG_IGN); |
| 2736 | signal(SIGQUIT, SIG_IGN); |
| 2737 | resetsig = 1; |
| 2738 | } |
| 2739 | interactive = 0; |
| 2740 | intr = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2741 | forked = 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2742 | brklist = 0; |
| 2743 | execflg = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2744 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2745 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2746 | if (owp != NULL) |
| 2747 | while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
| 2748 | if (shcom == NULL) |
| 2749 | export(lookup(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2750 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2751 | #ifdef COMPIPE |
| 2752 | if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) { |
| 2753 | err("piping to/from shell builtins not yet done"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2754 | if (forked) |
| 2755 | _exit(-1); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2756 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2757 | } |
| 2758 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2759 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2760 | if (pin != NULL) { |
| 2761 | dup2(pin[0], 0); |
| 2762 | closepipe(pin); |
| 2763 | } |
| 2764 | if (pout != NULL) { |
| 2765 | dup2(pout[1], 1); |
| 2766 | closepipe(pout); |
| 2767 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2768 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2769 | iopp = t->ioact; |
| 2770 | if (iopp != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2771 | if (shcom != NULL && shcom != doexec) { |
| 2772 | prs(cp); |
| 2773 | err(": cannot redirect shell command"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2774 | if (forked) |
| 2775 | _exit(-1); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2776 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2777 | } |
| 2778 | while (*iopp) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2779 | if (iosetup(*iopp++, pin != NULL, pout != NULL)) { |
| 2780 | if (forked) |
| 2781 | _exit(rv); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2782 | return rv; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2783 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2784 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2785 | |
| 2786 | if (shcom) { |
| 2787 | i = setstatus((*shcom) (t)); |
| 2788 | if (forked) |
| 2789 | _exit(i); |
| 2790 | DBGPRINTF(("FORKEXEC: returning i=%d\n", i)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2791 | return i; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2794 | /* should use FIOCEXCL */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2795 | for (i = FDBASE; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2796 | close(i); |
| 2797 | if (resetsig) { |
| 2798 | signal(SIGINT, SIG_DFL); |
| 2799 | signal(SIGQUIT, SIG_DFL); |
| 2800 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2801 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2802 | if (t->type == TPAREN) |
| 2803 | _exit(execute(t->left, NOPIPE, NOPIPE, FEXEC)); |
| 2804 | if (wp[0] == NULL) |
| 2805 | _exit(0); |
| 2806 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 2807 | cp = rexecve(wp[0], wp, makenv(0, NULL)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2808 | prs(wp[0]); |
| 2809 | prs(": "); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2810 | err(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2811 | if (!execflg) |
| 2812 | trap[0] = NULL; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2813 | |
| 2814 | DBGPRINTF(("FORKEXEC: calling leave(), pid=%d\n", newpid)); |
| 2815 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2816 | leave(); |
| 2817 | /* NOTREACHED */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2818 | _exit(1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
| 2821 | /* |
| 2822 | * 0< 1> are ignored as required |
| 2823 | * within pipelines. |
| 2824 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2825 | static int iosetup(struct ioword *iop, int pipein, int pipeout) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2826 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2827 | int u = -1; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2828 | char *cp = NULL; |
| 2829 | const char *msg; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2830 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 2831 | DBGPRINTF(("IOSETUP: iop %p, pipein %i, pipeout %i\n", iop, |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2832 | pipein, pipeout)); |
| 2833 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2834 | if (iop->io_unit == IODEFAULT) /* take default */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2835 | iop->io_unit = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2836 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2837 | if (pipein && iop->io_unit == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2838 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2839 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2840 | if (pipeout && iop->io_unit == 1) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2841 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2842 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2843 | msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2844 | if ((iop->io_flag & IOHERE) == 0) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2845 | cp = iop->io_name; /* huh?? */ |
| 2846 | cp = evalstr(cp, DOSUB | DOTRIM); |
| 2847 | if (cp == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2848 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2849 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 2850 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2851 | if (iop->io_flag & IODUP) { |
| 2852 | if (cp[1] || (!isdigit(*cp) && *cp != '-')) { |
| 2853 | prs(cp); |
| 2854 | err(": illegal >& argument"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2855 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2856 | } |
| 2857 | if (*cp == '-') |
| 2858 | iop->io_flag = IOCLOSE; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2859 | iop->io_flag &= ~(IOREAD | IOWRITE); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2860 | } |
| 2861 | switch (iop->io_flag) { |
| 2862 | case IOREAD: |
| 2863 | u = open(cp, 0); |
| 2864 | break; |
| 2865 | |
| 2866 | case IOHERE: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2867 | case IOHERE | IOXHERE: |
| 2868 | u = herein(iop->io_name, iop->io_flag & IOXHERE); |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2869 | cp = (char*)"here file"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2870 | break; |
| 2871 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2872 | case IOWRITE | IOCAT: |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2873 | u = open(cp, 1); |
| 2874 | if (u >= 0) { |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 2875 | lseek(u, (long) 0, SEEK_END); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2876 | break; |
| 2877 | } |
| 2878 | case IOWRITE: |
| 2879 | u = creat(cp, 0666); |
| 2880 | break; |
| 2881 | |
| 2882 | case IODUP: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2883 | u = dup2(*cp - '0', iop->io_unit); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2884 | break; |
| 2885 | |
| 2886 | case IOCLOSE: |
| 2887 | close(iop->io_unit); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2888 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2889 | } |
| 2890 | if (u < 0) { |
| 2891 | prs(cp); |
| 2892 | prs(": cannot "); |
| 2893 | warn(msg); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2894 | return 1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 2895 | } |
| 2896 | if (u != iop->io_unit) { |
| 2897 | dup2(u, iop->io_unit); |
| 2898 | close(u); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2899 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2900 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2903 | /* |
| 2904 | * Enter a new loop level (marked for break/continue). |
| 2905 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 2906 | static void brkset(struct brkcon *bc) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2907 | { |
| 2908 | bc->nextlev = brklist; |
| 2909 | brklist = bc; |
| 2910 | } |
| 2911 | |
| 2912 | /* |
| 2913 | * Wait for the last process created. |
| 2914 | * Print a message for each process found |
| 2915 | * that was killed by a signal. |
| 2916 | * Ignore interrupt signals while waiting |
| 2917 | * unless `canintr' is true. |
| 2918 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2919 | static int waitfor(int lastpid, int canintr) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2920 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2921 | int pid, rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2922 | int s; |
| 2923 | int oheedint = heedint; |
| 2924 | |
| 2925 | heedint = 0; |
| 2926 | rv = 0; |
| 2927 | do { |
| 2928 | pid = wait(&s); |
| 2929 | if (pid == -1) { |
| 2930 | if (errno != EINTR || canintr) |
| 2931 | break; |
| 2932 | } else { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 2933 | rv = WAITSIG(s); |
| 2934 | if (rv != 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2935 | if (rv < NSIGNAL) { |
| 2936 | if (signame[rv] != NULL) { |
| 2937 | if (pid != lastpid) { |
| 2938 | prn(pid); |
| 2939 | prs(": "); |
| 2940 | } |
| 2941 | prs(signame[rv]); |
| 2942 | } |
| 2943 | } else { |
| 2944 | if (pid != lastpid) { |
| 2945 | prn(pid); |
| 2946 | prs(": "); |
| 2947 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2948 | prs("Signal "); |
| 2949 | prn(rv); |
| 2950 | prs(" "); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2951 | } |
| 2952 | if (WAITCORE(s)) |
| 2953 | prs(" - core dumped"); |
| 2954 | if (rv >= NSIGNAL || signame[rv]) |
| 2955 | prs("\n"); |
| 2956 | rv = -1; |
| 2957 | } else |
| 2958 | rv = WAITVAL(s); |
| 2959 | } |
| 2960 | } while (pid != lastpid); |
| 2961 | heedint = oheedint; |
| 2962 | if (intr) { |
| 2963 | if (interactive) { |
| 2964 | if (canintr) |
| 2965 | intr = 0; |
| 2966 | } else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2967 | if (exstat == 0) |
| 2968 | exstat = rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2969 | onintr(0); |
| 2970 | } |
| 2971 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2972 | return rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2975 | static int setstatus(int s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2976 | { |
| 2977 | exstat = s; |
| 2978 | setval(lookup("?"), putn(s)); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2979 | return s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | /* |
| 2983 | * PATH-searching interface to execve. |
| 2984 | * If getenv("PATH") were kept up-to-date, |
| 2985 | * execvp might be used. |
| 2986 | */ |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2987 | static const char *rexecve(char *c, char **v, char **envp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2988 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 2989 | int i; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 2990 | const char *sp; |
| 2991 | char *tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 2992 | int eacces = 0, asis = 0; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 2993 | char *name = c; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 2994 | |
Rob Landley | a299efb | 2006-08-10 21:46:43 +0000 | [diff] [blame] | 2995 | if (ENABLE_FEATURE_SH_STANDALONE_SHELL) { |
| 2996 | optind = 1; |
| 2997 | if (find_applet_by_name(name)) { |
| 2998 | /* We have to exec here since we vforked. Running |
| 2999 | * run_applet_by_name() won't work and bad things |
| 3000 | * will happen. */ |
| 3001 | execve(CONFIG_BUSYBOX_EXEC_PATH, v, envp); |
| 3002 | } |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3003 | } |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3004 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3005 | DBGPRINTF(("REXECVE: c=%p, v=%p, envp=%p\n", c, v, envp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3006 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3007 | sp = any('/', c) ? "" : path->value; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3008 | asis = (*sp == '\0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3009 | while (asis || *sp != '\0') { |
| 3010 | asis = 0; |
| 3011 | tp = e.linep; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3012 | for (; *sp != '\0'; tp++) { |
| 3013 | *tp = *sp++; |
| 3014 | if (*tp == ':') { |
| 3015 | asis = (*sp == '\0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3016 | break; |
| 3017 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3018 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3019 | if (tp != e.linep) |
| 3020 | *tp++ = '/'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3021 | for (i = 0; (*tp++ = c[i++]) != '\0';); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3022 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3023 | DBGPRINTF3(("REXECVE: e.linep is %s\n", e.linep)); |
| 3024 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3025 | execve(e.linep, v, envp); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3026 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3027 | switch (errno) { |
| 3028 | case ENOEXEC: |
| 3029 | *v = e.linep; |
| 3030 | tp = *--v; |
| 3031 | *v = e.linep; |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 3032 | execve(DEFAULT_SHELL, v, envp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3033 | *v = tp; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3034 | return "no Shell"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3035 | |
| 3036 | case ENOMEM: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3037 | return (char *) bb_msg_memory_exhausted; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3038 | |
| 3039 | case E2BIG: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3040 | return "argument list too long"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3041 | |
| 3042 | case EACCES: |
| 3043 | eacces++; |
| 3044 | break; |
| 3045 | } |
| 3046 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3047 | return errno == ENOENT ? "not found" : "cannot execute"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | /* |
| 3051 | * Run the command produced by generator `f' |
| 3052 | * applied to stream `arg'. |
| 3053 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3054 | static int run(struct ioarg *argp, int (*f) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3055 | { |
| 3056 | struct op *otree; |
| 3057 | struct wdblock *swdlist; |
| 3058 | struct wdblock *siolist; |
| 3059 | jmp_buf ev, rt; |
| 3060 | xint *ofail; |
| 3061 | int rv; |
| 3062 | |
| 3063 | #if __GNUC__ |
| 3064 | /* Avoid longjmp clobbering */ |
| 3065 | (void) &rv; |
| 3066 | #endif |
| 3067 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3068 | DBGPRINTF(("RUN: enter, areanum %d, outtree %p, failpt %p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3069 | areanum, outtree, failpt)); |
| 3070 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3071 | areanum++; |
| 3072 | swdlist = wdlist; |
| 3073 | siolist = iolist; |
| 3074 | otree = outtree; |
| 3075 | ofail = failpt; |
| 3076 | rv = -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3077 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3078 | errpt = ev; |
| 3079 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3080 | wdlist = 0; |
| 3081 | iolist = 0; |
| 3082 | pushio(argp, f); |
| 3083 | e.iobase = e.iop; |
| 3084 | yynerrs = 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3085 | failpt = rt; |
| 3086 | if (setjmp(failpt) == 0 && yyparse() == 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3087 | rv = execute(outtree, NOPIPE, NOPIPE, 0); |
| 3088 | quitenv(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3089 | } else { |
| 3090 | DBGPRINTF(("RUN: error from newenv()!\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3091 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3092 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3093 | wdlist = swdlist; |
| 3094 | iolist = siolist; |
| 3095 | failpt = ofail; |
| 3096 | outtree = otree; |
| 3097 | freearea(areanum--); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3098 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3099 | return rv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | /* -------- do.c -------- */ |
| 3103 | |
| 3104 | /* |
| 3105 | * built-in commands: doX |
| 3106 | */ |
| 3107 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3108 | static int dohelp(struct op *t) |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3109 | { |
| 3110 | int col; |
| 3111 | const struct builtincmd *x; |
| 3112 | |
Denis Vlasenko | 0ee3999 | 2006-12-24 15:23:28 +0000 | [diff] [blame] | 3113 | puts("\nBuilt-in commands:\n" |
| 3114 | "-------------------"); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3115 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3116 | for (col = 0, x = builtincmds; x->builtinfunc != NULL; x++) { |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3117 | if (!x->name) |
| 3118 | continue; |
| 3119 | col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name); |
| 3120 | if (col > 60) { |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 3121 | puts(""); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3122 | col = 0; |
| 3123 | } |
| 3124 | } |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 3125 | #if ENABLE_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3126 | { |
| 3127 | int i; |
| 3128 | const struct BB_applet *applet; |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3129 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3130 | for (i = 0, applet = applets; i < NUM_APPLETS; applet++, i++) { |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3131 | if (!applet->name) |
| 3132 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3133 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3134 | col += printf("%s%s", ((col == 0) ? "\t" : " "), applet->name); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3135 | if (col > 60) { |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 3136 | puts(""); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3137 | col = 0; |
| 3138 | } |
| 3139 | } |
| 3140 | } |
| 3141 | #endif |
Denis Vlasenko | 0ee3999 | 2006-12-24 15:23:28 +0000 | [diff] [blame] | 3142 | puts("\n"); |
Eric Andersen | 1c03923 | 2001-07-07 00:05:55 +0000 | [diff] [blame] | 3143 | return EXIT_SUCCESS; |
| 3144 | } |
| 3145 | |
| 3146 | |
| 3147 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3148 | static int dolabel(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3149 | { |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3150 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3153 | static int dochdir(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3154 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3155 | const char *cp, *er; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3156 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3157 | cp = t->words[1]; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3158 | if (cp == NULL) { |
| 3159 | cp = homedir->value; |
| 3160 | if (cp == NULL) |
| 3161 | er = ": no home directory"; |
| 3162 | } else if (chdir(cp) < 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3163 | er = ": bad directory"; |
| 3164 | else |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3165 | return 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3166 | prs(cp != NULL ? cp : "cd"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3167 | err(er); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3168 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3171 | static int doshift(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3172 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3173 | int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3174 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3175 | n = t->words[1] ? getn(t->words[1]) : 1; |
| 3176 | if (dolc < n) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3177 | err("nothing to shift"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3178 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3179 | } |
| 3180 | dolv[n] = dolv[0]; |
| 3181 | dolv += n; |
| 3182 | dolc -= n; |
| 3183 | setval(lookup("#"), putn(dolc)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3184 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | /* |
| 3188 | * execute login and newgrp directly |
| 3189 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3190 | static int dologin(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3191 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3192 | const char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3193 | |
| 3194 | if (interactive) { |
| 3195 | signal(SIGINT, SIG_DFL); |
| 3196 | signal(SIGQUIT, SIG_DFL); |
| 3197 | } |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3198 | cp = rexecve(t->words[0], t->words, makenv(0, NULL)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3199 | prs(t->words[0]); |
| 3200 | prs(": "); |
| 3201 | err(cp); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3202 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3205 | static int doumask(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3206 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3207 | int i, n; |
| 3208 | char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3209 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3210 | cp = t->words[1]; |
| 3211 | if (cp == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3212 | i = umask(0); |
| 3213 | umask(i); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3214 | for (n = 3 * 4; (n -= 3) >= 0;) |
| 3215 | putc('0' + ((i >> n) & 07), stderr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3216 | putc('\n', stderr); |
| 3217 | } else { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3218 | /* huh??? '8','9' are not allowed! */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3219 | for (n = 0; *cp >= '0' && *cp <= '9'; cp++) |
| 3220 | n = n * 8 + (*cp - '0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3221 | umask(n); |
| 3222 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3223 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3226 | static int doexec(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3227 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3228 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3229 | jmp_buf ex; |
| 3230 | xint *ofail; |
| 3231 | |
| 3232 | t->ioact = NULL; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3233 | for (i = 0; (t->words[i] = t->words[i + 1]) != NULL; i++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3234 | if (i == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3235 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3236 | execflg = 1; |
| 3237 | ofail = failpt; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3238 | failpt = ex; |
| 3239 | if (setjmp(failpt) == 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3240 | execute(t, NOPIPE, NOPIPE, FEXEC); |
| 3241 | failpt = ofail; |
| 3242 | execflg = 0; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3243 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3246 | static int dodot(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3247 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3248 | int i; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3249 | const char *sp; |
| 3250 | char *tp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3251 | char *cp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3252 | int maltmp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3253 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3254 | 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] | 3255 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3256 | cp = t->words[1]; |
| 3257 | if (cp == NULL) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3258 | DBGPRINTF(("DODOT: bad args, ret 0\n")); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3259 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3260 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3261 | DBGPRINTF(("DODOT: cp is %s\n", cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3262 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3263 | sp = any('/', cp) ? ":" : path->value; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3264 | |
| 3265 | DBGPRINTF(("DODOT: sp is %s, e.linep is %s\n", |
| 3266 | ((sp == NULL) ? "NULL" : sp), |
| 3267 | ((e.linep == NULL) ? "NULL" : e.linep))); |
| 3268 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3269 | while (*sp) { |
| 3270 | tp = e.linep; |
| 3271 | while (*sp && (*tp = *sp++) != ':') |
| 3272 | tp++; |
| 3273 | if (tp != e.linep) |
| 3274 | *tp++ = '/'; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3275 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3276 | for (i = 0; (*tp++ = cp[i++]) != '\0';); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3277 | |
| 3278 | /* Original code */ |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3279 | i = open(e.linep, 0); |
| 3280 | if (i >= 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3281 | exstat = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3282 | maltmp = remap(i); |
| 3283 | DBGPRINTF(("DODOT: remap=%d, exstat=%d, e.iofd %d, i %d, e.linep is %s\n", maltmp, exstat, e.iofd, i, e.linep)); |
| 3284 | |
| 3285 | next(maltmp); /* Basically a PUSHIO */ |
| 3286 | |
| 3287 | DBGPRINTF(("DODOT: returning exstat=%d\n", exstat)); |
| 3288 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3289 | return exstat; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3290 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3291 | } /* while */ |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3292 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3293 | prs(cp); |
| 3294 | err(": not found"); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3295 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3296 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3299 | static int dowait(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3300 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3301 | int i; |
| 3302 | char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3303 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3304 | cp = t->words[1]; |
| 3305 | if (cp != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3306 | i = getn(cp); |
| 3307 | if (i == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3308 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3309 | } else |
| 3310 | i = -1; |
| 3311 | setstatus(waitfor(i, 1)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3312 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3315 | static int doread(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3316 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3317 | char *cp, **wp; |
| 3318 | int nb = 0; |
| 3319 | int nl = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3320 | |
| 3321 | if (t->words[1] == NULL) { |
| 3322 | err("Usage: read name ..."); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3323 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3324 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3325 | for (wp = t->words + 1; *wp; wp++) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3326 | for (cp = e.linep; !nl && cp < elinep - 1; cp++) { |
| 3327 | nb = read(0, cp, sizeof(*cp)); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3328 | if (nb != sizeof(*cp)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3329 | break; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3330 | nl = (*cp == '\n'); |
| 3331 | if (nl || (wp[1] && any(*cp, ifs->value))) |
| 3332 | break; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3333 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3334 | *cp = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3335 | if (nb <= 0) |
| 3336 | break; |
| 3337 | setval(lookup(*wp), e.linep); |
| 3338 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3339 | return nb <= 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3342 | static int doeval(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3343 | { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3344 | return RUN(awordlist, t->words + 1, wdchar); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3347 | static int dotrap(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3348 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3349 | int n, i; |
| 3350 | int resetsig; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3351 | |
| 3352 | if (t->words[1] == NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3353 | for (i = 0; i <= _NSIG; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3354 | if (trap[i]) { |
| 3355 | prn(i); |
| 3356 | prs(": "); |
| 3357 | prs(trap[i]); |
| 3358 | prs("\n"); |
| 3359 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3360 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3361 | } |
| 3362 | resetsig = isdigit(*t->words[1]); |
| 3363 | for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) { |
| 3364 | n = getsig(t->words[i]); |
| 3365 | freecell(trap[n]); |
| 3366 | trap[n] = 0; |
| 3367 | if (!resetsig) { |
| 3368 | if (*t->words[1] != '\0') { |
| 3369 | trap[n] = strsave(t->words[1], 0); |
| 3370 | setsig(n, sig); |
| 3371 | } else |
| 3372 | setsig(n, SIG_IGN); |
| 3373 | } else { |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 3374 | if (interactive) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3375 | if (n == SIGINT) |
| 3376 | setsig(n, onintr); |
| 3377 | else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3378 | setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 3379 | } else |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3380 | setsig(n, SIG_DFL); |
| 3381 | } |
| 3382 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3383 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3386 | static int getsig(char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3387 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3388 | int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3389 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3390 | n = getn(s); |
| 3391 | if (n < 0 || n > _NSIG) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3392 | err("trap: bad signal number"); |
| 3393 | n = 0; |
| 3394 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3395 | return n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3398 | static void setsig(int n, sighandler_t f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3399 | { |
| 3400 | if (n == 0) |
| 3401 | return; |
| 3402 | if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) { |
| 3403 | ourtrap[n] = 1; |
| 3404 | signal(n, f); |
| 3405 | } |
| 3406 | } |
| 3407 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3408 | static int getn(char *as) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3409 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3410 | char *s; |
| 3411 | int n, m; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3412 | |
| 3413 | s = as; |
| 3414 | m = 1; |
| 3415 | if (*s == '-') { |
| 3416 | m = -1; |
| 3417 | s++; |
| 3418 | } |
| 3419 | for (n = 0; isdigit(*s); s++) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3420 | n = (n * 10) + (*s - '0'); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3421 | if (*s) { |
| 3422 | prs(as); |
| 3423 | err(": bad number"); |
| 3424 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3425 | return n * m; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3428 | static int dobreak(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3429 | { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3430 | return brkcontin(t->words[1], 1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3433 | static int docontinue(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3434 | { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3435 | return brkcontin(t->words[1], 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3438 | static int brkcontin(char *cp, int val) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3439 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3440 | struct brkcon *bc; |
| 3441 | int nl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3442 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3443 | nl = cp == NULL ? 1 : getn(cp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3444 | if (nl <= 0) |
| 3445 | nl = 999; |
| 3446 | do { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3447 | bc = brklist; |
| 3448 | if (bc == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3449 | break; |
| 3450 | brklist = bc->nextlev; |
| 3451 | } while (--nl); |
| 3452 | if (nl) { |
| 3453 | err("bad break/continue level"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3454 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3455 | } |
| 3456 | isbreak = val; |
| 3457 | longjmp(bc->brkpt, 1); |
| 3458 | /* NOTREACHED */ |
| 3459 | } |
| 3460 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3461 | static int doexit(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3462 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3463 | char *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3464 | |
| 3465 | execflg = 0; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3466 | cp = t->words[1]; |
| 3467 | if (cp != NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3468 | setstatus(getn(cp)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3469 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3470 | DBGPRINTF(("DOEXIT: calling leave(), t=%p\n", t)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3471 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3472 | leave(); |
| 3473 | /* NOTREACHED */ |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3474 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3477 | static int doexport(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3478 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3479 | rdexp(t->words + 1, export, EXPORT); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3480 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3483 | static int doreadonly(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3484 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3485 | rdexp(t->words + 1, ronly, RONLY); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3486 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3487 | } |
| 3488 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3489 | static void rdexp(char **wp, void (*f) (struct var *), int key) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3490 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 3491 | 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] | 3492 | DBGPRINTF6(("RDEXP: *wp=%s\n", *wp)); |
| 3493 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3494 | if (*wp != NULL) { |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3495 | for (; *wp != NULL; wp++) { |
| 3496 | if (isassign(*wp)) { |
| 3497 | char *cp; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3498 | |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3499 | assign(*wp, COPYV); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3500 | for (cp = *wp; *cp != '='; cp++); |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3501 | *cp = '\0'; |
| 3502 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3503 | if (checkname(*wp)) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3504 | (*f) (lookup(*wp)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3505 | else |
| 3506 | badid(*wp); |
Matt Kraai | f69bfc7 | 2001-07-12 19:39:59 +0000 | [diff] [blame] | 3507 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3508 | } else |
| 3509 | putvlist(key, 1); |
| 3510 | } |
| 3511 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3512 | static void badid(char *s) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3513 | { |
| 3514 | prs(s); |
| 3515 | err(": bad identifier"); |
| 3516 | } |
| 3517 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3518 | static int doset(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3519 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3520 | struct var *vp; |
| 3521 | char *cp; |
| 3522 | int n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3523 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3524 | cp = t->words[1]; |
| 3525 | if (cp == NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3526 | for (vp = vlist; vp; vp = vp->next) |
| 3527 | varput(vp->name, 1); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3528 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3529 | } |
| 3530 | if (*cp == '-') { |
| 3531 | /* bad: t->words++; */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3532 | for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3533 | if (*++cp == 0) |
| 3534 | flag['x'] = flag['v'] = 0; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3535 | else { |
| 3536 | for (; *cp; cp++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3537 | switch (*cp) { |
| 3538 | case 'e': |
| 3539 | if (!interactive) |
| 3540 | flag['e']++; |
| 3541 | break; |
| 3542 | |
| 3543 | default: |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3544 | if (*cp >= 'a' && *cp <= 'z') |
| 3545 | flag[(int) *cp]++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3546 | break; |
| 3547 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3548 | } |
| 3549 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3550 | setdash(); |
| 3551 | } |
| 3552 | if (t->words[1]) { |
| 3553 | t->words[0] = dolv[0]; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3554 | for (n = 1; t->words[n]; n++) |
| 3555 | setarea((char *) t->words[n], 0); |
| 3556 | dolc = n - 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3557 | dolv = t->words; |
| 3558 | setval(lookup("#"), putn(dolc)); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3559 | setarea((char *) (dolv - 1), 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3560 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3561 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3564 | static void varput(char *s, int out) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3565 | { |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3566 | if (isalnum(*s) || *s == '_') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3567 | write(out, s, strlen(s)); |
| 3568 | write(out, "\n", 1); |
| 3569 | } |
| 3570 | } |
| 3571 | |
| 3572 | |
| 3573 | /* |
| 3574 | * Copyright (c) 1999 Herbert Xu <herbert@debian.org> |
| 3575 | * This file contains code for the times builtin. |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3576 | */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3577 | static int dotimes(struct op *t) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3578 | { |
| 3579 | struct tms buf; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3580 | long clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3581 | |
| 3582 | times(&buf); |
| 3583 | printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n", |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3584 | (int) (buf.tms_utime / clk_tck / 60), |
| 3585 | ((double) buf.tms_utime) / clk_tck, |
| 3586 | (int) (buf.tms_stime / clk_tck / 60), |
| 3587 | ((double) buf.tms_stime) / clk_tck, |
| 3588 | (int) (buf.tms_cutime / clk_tck / 60), |
| 3589 | ((double) buf.tms_cutime) / clk_tck, |
| 3590 | (int) (buf.tms_cstime / clk_tck / 60), |
| 3591 | ((double) buf.tms_cstime) / clk_tck); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3592 | return 0; |
| 3593 | } |
| 3594 | |
| 3595 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3596 | /* -------- eval.c -------- */ |
| 3597 | |
| 3598 | /* |
| 3599 | * ${} |
| 3600 | * `command` |
| 3601 | * blank interpretation |
| 3602 | * quoting |
| 3603 | * glob |
| 3604 | */ |
| 3605 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3606 | static char **eval(char **ap, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3607 | { |
| 3608 | struct wdblock *wb; |
| 3609 | char **wp; |
| 3610 | char **wf; |
| 3611 | jmp_buf ev; |
| 3612 | |
| 3613 | #if __GNUC__ |
| 3614 | /* Avoid longjmp clobbering */ |
| 3615 | (void) ℘ |
| 3616 | (void) ≈ |
| 3617 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3618 | |
| 3619 | DBGPRINTF4(("EVAL: enter, f=%d\n", f)); |
| 3620 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3621 | wp = NULL; |
| 3622 | wb = NULL; |
| 3623 | wf = NULL; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3624 | errpt = ev; |
| 3625 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3626 | while (*ap && isassign(*ap)) |
| 3627 | expand(*ap++, &wb, f & ~DOGLOB); |
| 3628 | if (flag['k']) { |
| 3629 | for (wf = ap; *wf; wf++) { |
| 3630 | if (isassign(*wf)) |
| 3631 | expand(*wf, &wb, f & ~DOGLOB); |
| 3632 | } |
| 3633 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3634 | for (wb = addword((char *) 0, wb); *ap; ap++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3635 | if (!flag['k'] || !isassign(*ap)) |
| 3636 | expand(*ap, &wb, f & ~DOKEY); |
| 3637 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3638 | wb = addword((char *) 0, wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3639 | wp = getwords(wb); |
| 3640 | quitenv(); |
| 3641 | } else |
| 3642 | gflg = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3643 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3644 | return gflg ? (char **) NULL : wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3647 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3648 | /* |
| 3649 | * Make the exported environment from the exported |
| 3650 | * names in the dictionary. Keyword assignments |
| 3651 | * will already have been done. |
| 3652 | */ |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3653 | static char **makenv(int all, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3654 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3655 | struct var *vp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3656 | |
| 3657 | DBGPRINTF5(("MAKENV: enter, all=%d\n", all)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3658 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3659 | for (vp = vlist; vp; vp = vp->next) |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3660 | if (all || vp->status & EXPORT) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3661 | wb = addword(vp->name, wb); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3662 | wb = addword((char *) 0, wb); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3663 | return getwords(wb); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3664 | } |
| 3665 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3666 | static int expand(const char *cp, struct wdblock **wbp, int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3667 | { |
| 3668 | jmp_buf ev; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3669 | char *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3670 | |
| 3671 | #if __GNUC__ |
| 3672 | /* Avoid longjmp clobbering */ |
| 3673 | (void) &cp; |
| 3674 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3675 | |
| 3676 | DBGPRINTF3(("EXPAND: enter, f=%d\n", f)); |
| 3677 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3678 | gflg = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3679 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3680 | if (cp == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3681 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3682 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3683 | if (!anys("$`'\"", cp) && !anys(ifs->value, cp) |
| 3684 | && ((f & DOGLOB) == 0 || !anys("[*?", cp)) |
| 3685 | ) { |
| 3686 | xp = strsave(cp, areanum); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3687 | if (f & DOTRIM) |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3688 | unquote(xp); |
| 3689 | *wbp = addword(xp, *wbp); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3690 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3691 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3692 | errpt = ev; |
| 3693 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3694 | PUSHIO(aword, cp, strchar); |
| 3695 | e.iobase = e.iop; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3696 | while ((xp = blank(f)) && gflg == 0) { |
| 3697 | e.linep = xp; |
| 3698 | xp = strsave(xp, areanum); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3699 | if ((f & DOGLOB) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3700 | if (f & DOTRIM) |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3701 | unquote(xp); |
| 3702 | *wbp = addword(xp, *wbp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3703 | } else |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3704 | *wbp = glob(xp, *wbp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3705 | } |
| 3706 | quitenv(); |
| 3707 | } else |
| 3708 | gflg = 1; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3709 | return gflg == 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3712 | static char *evalstr(char *cp, int f) |
| 3713 | { |
| 3714 | struct wdblock *wb; |
| 3715 | |
| 3716 | DBGPRINTF6(("EVALSTR: enter, cp=%p, f=%d\n", cp, f)); |
| 3717 | |
| 3718 | wb = NULL; |
| 3719 | if (expand(cp, &wb, f)) { |
| 3720 | if (wb == NULL || wb->w_nword == 0 |
| 3721 | || (cp = wb->w_words[0]) == NULL |
| 3722 | ) { |
| 3723 | // TODO: I suspect that |
| 3724 | // char *evalstr(char *cp, int f) is actually |
| 3725 | // const char *evalstr(const char *cp, int f)! |
| 3726 | cp = (char*)""; |
| 3727 | } |
| 3728 | DELETE(wb); |
| 3729 | } else |
| 3730 | cp = NULL; |
| 3731 | return cp; |
| 3732 | } |
| 3733 | |
| 3734 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3735 | /* |
| 3736 | * Blank interpretation and quoting |
| 3737 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3738 | static char *blank(int f) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3739 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3740 | int c, c1; |
| 3741 | char *sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3742 | int scanequals, foundequals; |
| 3743 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3744 | DBGPRINTF3(("BLANK: enter, f=%d\n", f)); |
| 3745 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3746 | sp = e.linep; |
| 3747 | scanequals = f & DOKEY; |
| 3748 | foundequals = 0; |
| 3749 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3750 | loop: |
| 3751 | c = subgetc('"', foundequals); |
| 3752 | switch (c) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3753 | case 0: |
| 3754 | if (sp == e.linep) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3755 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3756 | *e.linep++ = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3757 | return sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3758 | |
| 3759 | default: |
| 3760 | if (f & DOBLANK && any(c, ifs->value)) |
| 3761 | goto loop; |
| 3762 | break; |
| 3763 | |
| 3764 | case '"': |
| 3765 | case '\'': |
| 3766 | scanequals = 0; |
| 3767 | if (INSUB()) |
| 3768 | break; |
| 3769 | for (c1 = c; (c = subgetc(c1, 1)) != c1;) { |
| 3770 | if (c == 0) |
| 3771 | break; |
| 3772 | if (c == '\'' || !any(c, "$`\"")) |
| 3773 | c |= QUOTE; |
| 3774 | *e.linep++ = c; |
| 3775 | } |
| 3776 | c = 0; |
| 3777 | } |
| 3778 | unget(c); |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3779 | if (!isalpha(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3780 | scanequals = 0; |
| 3781 | for (;;) { |
| 3782 | c = subgetc('"', foundequals); |
| 3783 | if (c == 0 || |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3784 | f & (DOBLANK && any(c, ifs->value)) || |
| 3785 | (!INSUB() && any(c, "\"'"))) { |
| 3786 | scanequals = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3787 | unget(c); |
| 3788 | if (any(c, "\"'")) |
| 3789 | goto loop; |
| 3790 | break; |
| 3791 | } |
| 3792 | if (scanequals) { |
| 3793 | if (c == '=') { |
| 3794 | foundequals = 1; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3795 | scanequals = 0; |
| 3796 | } else if (!isalnum(c) && c != '_') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3797 | scanequals = 0; |
| 3798 | } |
| 3799 | *e.linep++ = c; |
| 3800 | } |
| 3801 | *e.linep++ = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3802 | return sp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | /* |
| 3806 | * Get characters, substituting for ` and $ |
| 3807 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3808 | static int subgetc(char ec, int quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3809 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3810 | char c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3811 | |
| 3812 | DBGPRINTF3(("SUBGETC: enter, quoted=%d\n", quoted)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3813 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 3814 | again: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3815 | c = my_getc(ec); |
| 3816 | if (!INSUB() && ec != '\'') { |
| 3817 | if (c == '`') { |
| 3818 | if (grave(quoted) == 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3819 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3820 | e.iop->task = XGRAVE; |
| 3821 | goto again; |
| 3822 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3823 | if (c == '$') { |
| 3824 | c = dollar(quoted); |
| 3825 | if (c == 0) { |
| 3826 | e.iop->task = XDOLL; |
| 3827 | goto again; |
| 3828 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3829 | } |
| 3830 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3831 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | /* |
| 3835 | * Prepare to generate the string returned by ${} substitution. |
| 3836 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3837 | static int dollar(int quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3838 | { |
| 3839 | int otask; |
| 3840 | struct io *oiop; |
| 3841 | char *dolp; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3842 | char *s, c, *cp = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3843 | struct var *vp; |
| 3844 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3845 | DBGPRINTF3(("DOLLAR: enter, quoted=%d\n", quoted)); |
| 3846 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3847 | c = readc(); |
| 3848 | s = e.linep; |
| 3849 | if (c != '{') { |
| 3850 | *e.linep++ = c; |
Matt Kraai | 69edfec | 2001-08-06 14:14:18 +0000 | [diff] [blame] | 3851 | if (isalpha(c) || c == '_') { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3852 | while ((c = readc()) != 0 && (isalnum(c) || c == '_')) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3853 | if (e.linep < elinep) |
| 3854 | *e.linep++ = c; |
| 3855 | unget(c); |
| 3856 | } |
| 3857 | c = 0; |
| 3858 | } else { |
| 3859 | oiop = e.iop; |
| 3860 | otask = e.iop->task; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 3861 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3862 | e.iop->task = XOTHER; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3863 | while ((c = subgetc('"', 0)) != 0 && c != '}' && c != '\n') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3864 | if (e.linep < elinep) |
| 3865 | *e.linep++ = c; |
| 3866 | if (oiop == e.iop) |
| 3867 | e.iop->task = otask; |
| 3868 | if (c != '}') { |
| 3869 | err("unclosed ${"); |
| 3870 | gflg++; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3871 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3872 | } |
| 3873 | } |
| 3874 | if (e.linep >= elinep) { |
| 3875 | err("string in ${} too long"); |
| 3876 | gflg++; |
| 3877 | e.linep -= 10; |
| 3878 | } |
| 3879 | *e.linep = 0; |
| 3880 | if (*s) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3881 | for (cp = s + 1; *cp; cp++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3882 | if (any(*cp, "=-+?")) { |
| 3883 | c = *cp; |
| 3884 | *cp++ = 0; |
| 3885 | break; |
| 3886 | } |
| 3887 | if (s[1] == 0 && (*s == '*' || *s == '@')) { |
| 3888 | if (dolc > 1) { |
| 3889 | /* currently this does not distinguish $* and $@ */ |
| 3890 | /* should check dollar */ |
| 3891 | e.linep = s; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3892 | PUSHIO(awordlist, dolv + 1, dolchar); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3893 | return 0; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 3894 | } else { /* trap the nasty ${=} */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3895 | s[0] = '1'; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3896 | s[1] = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3897 | } |
| 3898 | } |
| 3899 | vp = lookup(s); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 3900 | dolp = vp->value; |
| 3901 | if (dolp == null) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3902 | switch (c) { |
| 3903 | case '=': |
| 3904 | if (isdigit(*s)) { |
| 3905 | err("cannot use ${...=...} with $n"); |
| 3906 | gflg++; |
| 3907 | break; |
| 3908 | } |
| 3909 | setval(vp, cp); |
| 3910 | dolp = vp->value; |
| 3911 | break; |
| 3912 | |
| 3913 | case '-': |
| 3914 | dolp = strsave(cp, areanum); |
| 3915 | break; |
| 3916 | |
| 3917 | case '?': |
| 3918 | if (*cp == 0) { |
| 3919 | prs("missing value for "); |
| 3920 | err(s); |
| 3921 | } else |
| 3922 | err(cp); |
| 3923 | gflg++; |
| 3924 | break; |
| 3925 | } |
| 3926 | } else if (c == '+') |
| 3927 | dolp = strsave(cp, areanum); |
| 3928 | if (flag['u'] && dolp == null) { |
| 3929 | prs("unset variable: "); |
| 3930 | err(s); |
| 3931 | gflg++; |
| 3932 | } |
| 3933 | e.linep = s; |
| 3934 | PUSHIO(aword, dolp, quoted ? qstrchar : strchar); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3935 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | /* |
| 3939 | * Run the command in `...` and read its output. |
| 3940 | */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 3941 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 3942 | static int grave(int quoted) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3943 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3944 | const char *cp; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 3945 | int i; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 3946 | int j; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3947 | int pf[2]; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 3948 | static char child_cmd[LINELIM]; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3949 | const char *src; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 3950 | char *dest; |
| 3951 | int count; |
| 3952 | int ignore; |
| 3953 | int ignore_once; |
| 3954 | char *argument_list[4]; |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 3955 | struct wdblock *wb = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3956 | |
| 3957 | #if __GNUC__ |
| 3958 | /* Avoid longjmp clobbering */ |
| 3959 | (void) &cp; |
| 3960 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3961 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3962 | for (cp = e.iop->argp->aword; *cp != '`'; cp++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3963 | if (*cp == 0) { |
| 3964 | err("no closing `"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3965 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 3966 | } |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 3967 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 3968 | |
| 3969 | /* string copy with dollar expansion */ |
| 3970 | src = e.iop->argp->aword; |
| 3971 | dest = child_cmd; |
| 3972 | count = 0; |
| 3973 | ignore = 0; |
| 3974 | ignore_once = 0; |
| 3975 | while ((*src != '`') && (count < LINELIM)) { |
| 3976 | if (*src == '\'') |
| 3977 | ignore = !ignore; |
| 3978 | if (*src == '\\') |
| 3979 | ignore_once = 1; |
| 3980 | if (*src == '$' && !ignore && !ignore_once) { |
| 3981 | struct var *vp; |
| 3982 | char var_name[LINELIM]; |
| 3983 | char alt_value[LINELIM]; |
| 3984 | int var_index = 0; |
| 3985 | int alt_index = 0; |
| 3986 | char operator = 0; |
| 3987 | int braces = 0; |
| 3988 | char *value; |
| 3989 | |
| 3990 | src++; |
| 3991 | if (*src == '{') { |
| 3992 | braces = 1; |
| 3993 | src++; |
| 3994 | } |
| 3995 | |
| 3996 | var_name[var_index++] = *src++; |
Paul Fox | 54690dc | 2005-07-20 18:33:12 +0000 | [diff] [blame] | 3997 | while (isalnum(*src) || *src=='_') |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 3998 | var_name[var_index++] = *src++; |
| 3999 | var_name[var_index] = 0; |
| 4000 | |
| 4001 | if (braces) { |
| 4002 | switch (*src) { |
| 4003 | case '}': |
| 4004 | break; |
| 4005 | case '-': |
| 4006 | case '=': |
| 4007 | case '+': |
| 4008 | case '?': |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4009 | operator = * src; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4010 | break; |
| 4011 | default: |
| 4012 | err("unclosed ${\n"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4013 | return 0; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4014 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4015 | if (operator) { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4016 | src++; |
| 4017 | while (*src && (*src != '}')) { |
| 4018 | alt_value[alt_index++] = *src++; |
| 4019 | } |
| 4020 | alt_value[alt_index] = 0; |
| 4021 | if (*src != '}') { |
| 4022 | err("unclosed ${\n"); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4023 | return 0; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4024 | } |
| 4025 | } |
| 4026 | src++; |
| 4027 | } |
| 4028 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4029 | if (isalpha(*var_name)) { |
| 4030 | /* let subshell handle it instead */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4031 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4032 | char *namep = var_name; |
| 4033 | |
| 4034 | *dest++ = '$'; |
| 4035 | if (braces) |
| 4036 | *dest++ = '{'; |
| 4037 | while (*namep) |
| 4038 | *dest++ = *namep++; |
| 4039 | if (operator) { |
| 4040 | char *altp = alt_value; |
| 4041 | *dest++ = operator; |
| 4042 | while (*altp) |
| 4043 | *dest++ = *altp++; |
| 4044 | } |
| 4045 | if (braces) |
| 4046 | *dest++ = '}'; |
| 4047 | |
| 4048 | wb = addword(lookup(var_name)->name, wb); |
| 4049 | } else { |
| 4050 | /* expand */ |
| 4051 | |
| 4052 | vp = lookup(var_name); |
| 4053 | if (vp->value != null) |
| 4054 | value = (operator == '+') ? |
| 4055 | alt_value : vp->value; |
| 4056 | else if (operator == '?') { |
| 4057 | err(alt_value); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4058 | return 0; |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4059 | } else if (alt_index && (operator != '+')) { |
| 4060 | value = alt_value; |
| 4061 | if (operator == '=') |
| 4062 | setval(vp, value); |
| 4063 | } else |
| 4064 | continue; |
| 4065 | |
| 4066 | while (*value && (count < LINELIM)) { |
| 4067 | *dest++ = *value++; |
| 4068 | count++; |
| 4069 | } |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4070 | } |
| 4071 | } else { |
| 4072 | *dest++ = *src++; |
| 4073 | count++; |
| 4074 | ignore_once = 0; |
| 4075 | } |
| 4076 | } |
| 4077 | *dest = '\0'; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4078 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4079 | if (openpipe(pf) < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4080 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4081 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4082 | while ((i = vfork()) == -1 && errno == EAGAIN); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4083 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4084 | DBGPRINTF3(("GRAVE: i is %p\n", io)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4085 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4086 | if (i < 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4087 | closepipe(pf); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4088 | err((char *) bb_msg_memory_exhausted); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4089 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4090 | } |
| 4091 | if (i != 0) { |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4092 | waitpid(i, NULL, 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4093 | e.iop->argp->aword = ++cp; |
| 4094 | close(pf[1]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4095 | PUSHIO(afile, remap(pf[0]), |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4096 | (int (*)(struct ioarg *)) ((quoted) ? qgravechar : gravechar)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4097 | return 1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4098 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4099 | /* allow trapped signals */ |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4100 | /* XXX - Maybe this signal stuff should go as well? */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4101 | for (j = 0; j <= _NSIG; j++) |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4102 | if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN) |
| 4103 | signal(j, SIG_DFL); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4104 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4105 | dup2(pf[1], 1); |
| 4106 | closepipe(pf); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4107 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4108 | argument_list[0] = (char *) DEFAULT_SHELL; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4109 | argument_list[1] = (char *) "-c"; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4110 | argument_list[2] = child_cmd; |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4111 | argument_list[3] = NULL; |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4112 | |
Eric Andersen | fd7a4c8 | 2004-09-02 23:13:10 +0000 | [diff] [blame] | 4113 | cp = rexecve(argument_list[0], argument_list, makenv(1, wb)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4114 | prs(argument_list[0]); |
| 4115 | prs(": "); |
| 4116 | err(cp); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4117 | _exit(1); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4120 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4121 | static char *unquote(char *as) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4122 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4123 | char *s; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4124 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4125 | s = as; |
| 4126 | if (s != NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4127 | while (*s) |
| 4128 | *s++ &= ~QUOTE; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4129 | return as; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | /* -------- glob.c -------- */ |
| 4133 | |
| 4134 | /* |
| 4135 | * glob |
| 4136 | */ |
| 4137 | |
| 4138 | #define scopy(x) strsave((x), areanum) |
| 4139 | #define BLKSIZ 512 |
| 4140 | #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent)) |
| 4141 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4142 | static struct wdblock *cl, *nl; |
| 4143 | static char spcl[] = "[?*"; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4144 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4145 | static struct wdblock *glob(char *cp, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4146 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4147 | int i; |
| 4148 | char *pp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4149 | |
| 4150 | if (cp == 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4151 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4152 | i = 0; |
| 4153 | for (pp = cp; *pp; pp++) |
| 4154 | if (any(*pp, spcl)) |
| 4155 | i++; |
| 4156 | else if (!any(*pp & ~QUOTE, spcl)) |
| 4157 | *pp &= ~QUOTE; |
| 4158 | if (i != 0) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4159 | for (cl = addword(scopy(cp), NULL); anyspcl(cl); cl = nl) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4160 | nl = newword(cl->w_nword * 2); |
| 4161 | for (i = 0; i < cl->w_nword; i++) { /* for each argument */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4162 | for (pp = cl->w_words[i]; *pp; pp++) |
| 4163 | if (any(*pp, spcl)) { |
| 4164 | globname(cl->w_words[i], pp); |
| 4165 | break; |
| 4166 | } |
| 4167 | if (*pp == '\0') |
| 4168 | nl = addword(scopy(cl->w_words[i]), nl); |
| 4169 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4170 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4171 | DELETE(cl->w_words[i]); |
| 4172 | DELETE(cl); |
| 4173 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4174 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4175 | unquote(cl->w_words[i]); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4176 | glob0((char *) cl->w_words, cl->w_nword, sizeof(char *), xstrcmp); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4177 | if (cl->w_nword) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4178 | for (i = 0; i < cl->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4179 | wb = addword(cl->w_words[i], wb); |
| 4180 | DELETE(cl); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4181 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4182 | } |
| 4183 | } |
| 4184 | wb = addword(unquote(cp), wb); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4185 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4188 | static void globname(char *we, char *pp) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4189 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4190 | char *np, *cp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4191 | char *name, *gp, *dp; |
| 4192 | int k; |
| 4193 | DIR *dirp; |
| 4194 | struct dirent *de; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4195 | char dname[NAME_MAX + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4196 | struct stat dbuf; |
| 4197 | |
| 4198 | for (np = we; np != pp; pp--) |
| 4199 | if (pp[-1] == '/') |
| 4200 | break; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4201 | for (dp = cp = space((int) (pp - np) + 3); np < pp;) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4202 | *cp++ = *np++; |
| 4203 | *cp++ = '.'; |
| 4204 | *cp = '\0'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4205 | for (gp = cp = space(strlen(pp) + 1); *np && *np != '/';) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4206 | *cp++ = *np++; |
| 4207 | *cp = '\0'; |
| 4208 | dirp = opendir(dp); |
| 4209 | if (dirp == 0) { |
| 4210 | DELETE(dp); |
| 4211 | DELETE(gp); |
| 4212 | return; |
| 4213 | } |
| 4214 | dname[NAME_MAX] = '\0'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4215 | while ((de = readdir(dirp)) != NULL) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4216 | /* XXX Hmmm... What this could be? (abial) */ |
| 4217 | /* |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4218 | if (ent[j].d_ino == 0) |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4219 | continue; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4220 | */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4221 | strncpy(dname, de->d_name, NAME_MAX); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4222 | if (dname[0] == '.') |
| 4223 | if (*gp != '.') |
| 4224 | continue; |
| 4225 | for (k = 0; k < NAME_MAX; k++) |
| 4226 | if (any(dname[k], spcl)) |
| 4227 | dname[k] |= QUOTE; |
| 4228 | if (gmatch(dname, gp)) { |
| 4229 | name = generate(we, pp, dname, np); |
| 4230 | if (*np && !anys(np, spcl)) { |
| 4231 | if (stat(name, &dbuf)) { |
| 4232 | DELETE(name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4233 | continue; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4234 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4235 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4236 | nl = addword(name, nl); |
| 4237 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4238 | } |
| 4239 | closedir(dirp); |
| 4240 | DELETE(dp); |
| 4241 | DELETE(gp); |
| 4242 | } |
| 4243 | |
| 4244 | /* |
| 4245 | * generate a pathname as below. |
| 4246 | * start..end1 / middle end |
| 4247 | * the slashes come for free |
| 4248 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4249 | static char *generate(char *start1, char *end1, char *middle, char *end) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4250 | { |
| 4251 | char *p; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4252 | char *op, *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4253 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4254 | p = op = space((int)(end1 - start1) + strlen(middle) + strlen(end) + 2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4255 | for (xp = start1; xp != end1;) |
| 4256 | *op++ = *xp++; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4257 | for (xp = middle; (*op++ = *xp++) != '\0';); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4258 | op--; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4259 | for (xp = end; (*op++ = *xp++) != '\0';); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4260 | return p; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4263 | static int anyspcl(struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4264 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4265 | int i; |
| 4266 | char **wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4267 | |
| 4268 | wd = wb->w_words; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4269 | for (i = 0; i < wb->w_nword; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4270 | if (anys(spcl, *wd++)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4271 | return 1; |
| 4272 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4275 | static int xstrcmp(char *p1, char *p2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4276 | { |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4277 | return strcmp(*(char **) p1, *(char **) p2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4280 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4281 | /* -------- word.c -------- */ |
| 4282 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4283 | static struct wdblock *newword(int nw) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4284 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4285 | struct wdblock *wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4286 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4287 | wb = (struct wdblock *) space(sizeof(*wb) + nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4288 | wb->w_bsize = nw; |
| 4289 | wb->w_nword = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4290 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4293 | static struct wdblock *addword(char *wd, struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4294 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4295 | struct wdblock *wb2; |
| 4296 | int nw; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4297 | |
| 4298 | if (wb == NULL) |
| 4299 | wb = newword(NSTART); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4300 | nw = wb->w_nword; |
| 4301 | if (nw >= wb->w_bsize) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4302 | wb2 = newword(nw * 2); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4303 | memcpy((char *) wb2->w_words, (char *) wb->w_words, |
| 4304 | nw * sizeof(char *)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4305 | wb2->w_nword = nw; |
| 4306 | DELETE(wb); |
| 4307 | wb = wb2; |
| 4308 | } |
| 4309 | wb->w_words[wb->w_nword++] = wd; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4310 | return wb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4311 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4312 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4313 | static |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4314 | char **getwords(struct wdblock *wb) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4315 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4316 | char **wd; |
| 4317 | int nb; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4318 | |
| 4319 | if (wb == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4320 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4321 | if (wb->w_nword == 0) { |
| 4322 | DELETE(wb); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4323 | return NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4324 | } |
| 4325 | wd = (char **) space(nb = sizeof(*wd) * wb->w_nword); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4326 | memcpy((char *) wd, (char *) wb->w_words, nb); |
| 4327 | DELETE(wb); /* perhaps should done by caller */ |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4328 | return wd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
"Vladimir N. Oleynik" | ac97317 | 2005-09-22 14:38:17 +0000 | [diff] [blame] | 4331 | static int (*func) (char *, char *); |
| 4332 | static int globv; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4333 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4334 | static void glob3(char *i, char *j, char *k) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4335 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4336 | char *index1, *index2, *index3; |
| 4337 | int c; |
| 4338 | int m; |
| 4339 | |
| 4340 | m = globv; |
| 4341 | index1 = i; |
| 4342 | index2 = j; |
| 4343 | index3 = k; |
| 4344 | do { |
| 4345 | c = *index1; |
| 4346 | *index1++ = *index3; |
| 4347 | *index3++ = *index2; |
| 4348 | *index2++ = c; |
| 4349 | } while (--m); |
| 4350 | } |
| 4351 | |
| 4352 | static void glob2(char *i, char *j) |
| 4353 | { |
| 4354 | char *index1, *index2, c; |
| 4355 | int m; |
| 4356 | |
| 4357 | m = globv; |
| 4358 | index1 = i; |
| 4359 | index2 = j; |
| 4360 | do { |
| 4361 | c = *index1; |
| 4362 | *index1++ = *index2; |
| 4363 | *index2++ = c; |
| 4364 | } while (--m); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4367 | static void glob1(char *base, char *lim) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4368 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4369 | char *i, *j; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4370 | int v2; |
| 4371 | char *lptr, *hptr; |
| 4372 | int c; |
| 4373 | unsigned n; |
| 4374 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4375 | v2 = globv; |
| 4376 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4377 | top: |
| 4378 | n = (int) (lim - base); |
| 4379 | if (n <= v2) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4380 | return; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4381 | n = v2 * (n / (2 * v2)); |
| 4382 | hptr = lptr = base + n; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4383 | i = base; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4384 | j = lim - v2; |
| 4385 | for (;;) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4386 | if (i < lptr) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4387 | c = (*func) (i, lptr); |
| 4388 | if (c == 0) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4389 | lptr -= v2; |
| 4390 | glob2(i, lptr); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4391 | continue; |
| 4392 | } |
| 4393 | if (c < 0) { |
| 4394 | i += v2; |
| 4395 | continue; |
| 4396 | } |
| 4397 | } |
| 4398 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4399 | begin: |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4400 | if (j > hptr) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4401 | c = (*func) (hptr, j); |
| 4402 | if (c == 0) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4403 | hptr += v2; |
| 4404 | glob2(hptr, j); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4405 | goto begin; |
| 4406 | } |
| 4407 | if (c > 0) { |
| 4408 | if (i == lptr) { |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4409 | hptr += v2; |
| 4410 | glob3(i, hptr, j); |
| 4411 | i = (lptr += v2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4412 | goto begin; |
| 4413 | } |
| 4414 | glob2(i, j); |
| 4415 | j -= v2; |
| 4416 | i += v2; |
| 4417 | continue; |
| 4418 | } |
| 4419 | j -= v2; |
| 4420 | goto begin; |
| 4421 | } |
| 4422 | |
| 4423 | |
| 4424 | if (i == lptr) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4425 | if (lptr - base >= lim - hptr) { |
| 4426 | glob1(hptr + v2, lim); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4427 | lim = lptr; |
| 4428 | } else { |
| 4429 | glob1(base, lptr); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4430 | base = hptr + v2; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4431 | } |
| 4432 | goto top; |
| 4433 | } |
| 4434 | |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4435 | lptr -= v2; |
| 4436 | glob3(j, lptr, i); |
| 4437 | j = (hptr -= v2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4438 | } |
| 4439 | } |
| 4440 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4441 | static void glob0(char *a0, unsigned a1, int a2, int (*a3) (char *, char *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4442 | { |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4443 | func = a3; |
| 4444 | globv = a2; |
| 4445 | glob1(a0, a0 + a1 * a2); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4446 | } |
| 4447 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4448 | |
| 4449 | /* -------- io.c -------- */ |
| 4450 | |
| 4451 | /* |
| 4452 | * shell IO |
| 4453 | */ |
| 4454 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4455 | static int my_getc(int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4456 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4457 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4458 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4459 | if (e.linep > elinep) { |
| 4460 | while ((c = readc()) != '\n' && c); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4461 | err("input line too long"); |
| 4462 | gflg++; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4463 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4464 | } |
| 4465 | c = readc(); |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4466 | if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4467 | if (c == '\\') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4468 | c = readc(); |
| 4469 | if (c == '\n' && ec != '\"') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4470 | return my_getc(ec); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4471 | c |= QUOTE; |
| 4472 | } |
| 4473 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4474 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4475 | } |
| 4476 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4477 | static void unget(int c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4478 | { |
| 4479 | if (e.iop >= e.iobase) |
| 4480 | e.iop->peekc = c; |
| 4481 | } |
| 4482 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4483 | static int eofc(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4484 | { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4485 | 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] | 4486 | } |
| 4487 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4488 | static int readc(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4489 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4490 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4491 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4492 | 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] | 4493 | |
| 4494 | for (; e.iop >= e.iobase; e.iop--) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4495 | RCPRINTF(("READC: e.iop %p, peekc 0x%x\n", e.iop, e.iop->peekc)); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4496 | c = e.iop->peekc; |
| 4497 | if (c != '\0') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4498 | e.iop->peekc = 0; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4499 | return c; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4500 | } |
| 4501 | if (e.iop->prev != 0) { |
| 4502 | c = (*e.iop->iofn)(e.iop->argp, e.iop); |
| 4503 | if (c != '\0') { |
| 4504 | if (c == -1) { |
| 4505 | e.iop++; |
| 4506 | continue; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4507 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4508 | if (e.iop == iostack) |
| 4509 | ioecho(c); |
| 4510 | e.iop->prev = c; |
| 4511 | return e.iop->prev; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4512 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4513 | if (e.iop->task == XIO && e.iop->prev != '\n') { |
| 4514 | e.iop->prev = 0; |
| 4515 | if (e.iop == iostack) |
| 4516 | ioecho('\n'); |
| 4517 | return '\n'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4518 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4519 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4520 | if (e.iop->task == XIO) { |
| 4521 | if (multiline) { |
| 4522 | e.iop->prev = 0; |
| 4523 | return e.iop->prev; |
| 4524 | } |
| 4525 | if (interactive && e.iop == iostack + 1) { |
| 4526 | #if ENABLE_FEATURE_EDITING |
| 4527 | current_prompt = prompt->value; |
| 4528 | #else |
| 4529 | prs(prompt->value); |
| 4530 | #endif |
| 4531 | } |
| 4532 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4533 | } /* FOR */ |
| 4534 | |
| 4535 | if (e.iop >= iostack) { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4536 | RCPRINTF(("READC: return 0, e.iop %p\n", e.iop)); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4537 | return 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | DBGPRINTF(("READC: leave()...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4541 | leave(); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4542 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4543 | /* NOTREACHED */ |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4544 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4547 | static void ioecho(char c) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4548 | { |
| 4549 | if (flag['v']) |
| 4550 | write(2, &c, sizeof c); |
| 4551 | } |
| 4552 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4553 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4554 | static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *)) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4555 | { |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4556 | 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] | 4557 | argp->afid, e.iop)); |
| 4558 | |
| 4559 | /* 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] | 4560 | if (++e.iop >= &iostack[NPUSH]) { |
| 4561 | e.iop--; |
| 4562 | err("Shell input nested too deeply"); |
| 4563 | gflg++; |
| 4564 | return; |
| 4565 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4566 | |
| 4567 | /* We did not overflow the NPUSH array spots so setup data structs */ |
| 4568 | |
| 4569 | 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] | 4570 | |
| 4571 | if (argp->afid != AFID_NOBUF) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4572 | e.iop->argp = argp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4573 | else { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4574 | |
| 4575 | e.iop->argp = ioargstack + (e.iop - iostack); /* MAL - index into stack */ |
| 4576 | *e.iop->argp = *argp; /* copy data from temp area into stack spot */ |
| 4577 | |
| 4578 | /* MAL - mainbuf is for 1st data source (command line?) and all nested use a single shared buffer? */ |
| 4579 | |
| 4580 | if (e.iop == &iostack[0]) |
| 4581 | e.iop->argp->afbuf = &mainbuf; |
| 4582 | else |
| 4583 | e.iop->argp->afbuf = &sharedbuf; |
| 4584 | |
| 4585 | /* MAL - if not a termimal AND (commandline OR readable file) then give it a buffer id? */ |
| 4586 | /* This line appears to be active when running scripts from command line */ |
| 4587 | if ((isatty(e.iop->argp->afile) == 0) |
| 4588 | && (e.iop == &iostack[0] |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 4589 | || lseek(e.iop->argp->afile, 0L, SEEK_CUR) != -1)) { |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4590 | if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */ |
| 4591 | bufid = AFID_ID; /* AFID_ID = 0 */ |
| 4592 | |
| 4593 | e.iop->argp->afid = bufid; /* assign buffer id */ |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4594 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4595 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4596 | DBGPRINTF(("PUSHIO: iostack %p, e.iop %p, afbuf %p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4597 | iostack, e.iop, e.iop->argp->afbuf)); |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4598 | DBGPRINTF(("PUSHIO: mbuf %p, sbuf %p, bid %d, e.iop %p\n", |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4599 | &mainbuf, &sharedbuf, bufid, e.iop)); |
| 4600 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4603 | e.iop->prev = ~'\n'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4604 | e.iop->peekc = 0; |
| 4605 | e.iop->xchar = 0; |
| 4606 | e.iop->nlcount = 0; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4607 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4608 | if (fn == filechar || fn == linechar) |
| 4609 | e.iop->task = XIO; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4610 | else if (fn == (int (*)(struct ioarg *)) gravechar |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4611 | || fn == (int (*)(struct ioarg *)) qgravechar) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4612 | e.iop->task = XGRAVE; |
| 4613 | else |
| 4614 | e.iop->task = XOTHER; |
| 4615 | } |
| 4616 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4617 | static struct io *setbase(struct io *ip) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4618 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4619 | struct io *xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4620 | |
| 4621 | xp = e.iobase; |
| 4622 | e.iobase = ip; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4623 | return xp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | /* |
| 4627 | * Input generating functions |
| 4628 | */ |
| 4629 | |
| 4630 | /* |
| 4631 | * Produce the characters of a string, then a newline, then EOF. |
| 4632 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4633 | static int nlchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4634 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4635 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4636 | |
| 4637 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4638 | return 0; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4639 | c = *ap->aword++; |
| 4640 | if (c == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4641 | ap->aword = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4642 | return '\n'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4643 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4644 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
| 4647 | /* |
| 4648 | * Given a list of words, produce the characters |
| 4649 | * in them, with a space after each word. |
| 4650 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4651 | static int wdchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4652 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4653 | char c; |
| 4654 | char **wl; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4655 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4656 | wl = ap->awordlist; |
| 4657 | if (wl == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4658 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4659 | if (*wl != NULL) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4660 | c = *(*wl)++; |
| 4661 | if (c != 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4662 | return c & 0177; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4663 | ap->awordlist++; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4664 | return ' '; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4665 | } |
| 4666 | ap->awordlist = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4667 | return '\n'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | /* |
| 4671 | * Return the characters of a list of words, |
| 4672 | * producing a space between them. |
| 4673 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4674 | static int dolchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4675 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4676 | char *wp; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4677 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4678 | wp = *ap->awordlist++; |
| 4679 | if (wp != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4680 | PUSHIO(aword, wp, *ap->awordlist == NULL ? strchar : xxchar); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4681 | return -1; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4682 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4683 | return 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4686 | static int xxchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4687 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4688 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4689 | |
| 4690 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4691 | return 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4692 | c = *ap->aword++; |
| 4693 | if (c == '\0') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4694 | ap->aword = NULL; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4695 | return ' '; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4696 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4697 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | /* |
| 4701 | * Produce the characters from a single word (string). |
| 4702 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4703 | static int strchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4704 | { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4705 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4706 | return 0; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4707 | return *ap->aword++; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | /* |
| 4711 | * Produce quoted characters from a single word (string). |
| 4712 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4713 | static int qstrchar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4714 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4715 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4716 | |
Denis Vlasenko | b2abef3 | 2007-01-01 18:18:04 +0000 | [diff] [blame] | 4717 | if (ap->aword == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 4718 | return 0; |
Denis Vlasenko | b2abef3 | 2007-01-01 18:18:04 +0000 | [diff] [blame] | 4719 | c = *ap->aword++; |
| 4720 | if (c) |
| 4721 | c |= QUOTE; |
| 4722 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | /* |
| 4726 | * Return the characters from a file. |
| 4727 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4728 | static int filechar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4729 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4730 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4731 | char c; |
| 4732 | struct iobuf *bp = ap->afbuf; |
| 4733 | |
| 4734 | if (ap->afid != AFID_NOBUF) { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4735 | i = (ap->afid != bp->id); |
| 4736 | if (i || bp->bufp == bp->ebufp) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4737 | if (i) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 4738 | lseek(ap->afile, ap->afpos, SEEK_SET); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4739 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4740 | i = safe_read(ap->afile, bp->buf, sizeof(bp->buf)); |
| 4741 | if (i <= 0) { |
| 4742 | closef(ap->afile); |
| 4743 | return 0; |
| 4744 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4745 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4746 | bp->id = ap->afid; |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4747 | bp->bufp = bp->buf; |
| 4748 | bp->ebufp = bp->bufp + i; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4749 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4750 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4751 | ap->afpos++; |
| 4752 | return *bp->bufp++ & 0177; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4753 | } |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4754 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 737f5fb | 2003-03-14 16:05:59 +0000 | [diff] [blame] | 4755 | if (interactive && isatty(ap->afile)) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4756 | static char mycommand[BUFSIZ]; |
| 4757 | static int position = 0, size = 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4758 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4759 | while (size == 0 || position >= size) { |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 4760 | read_line_input(current_prompt, mycommand, BUFSIZ, line_input_state); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4761 | size = strlen(mycommand); |
| 4762 | position = 0; |
| 4763 | } |
| 4764 | c = mycommand[position]; |
| 4765 | position++; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4766 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4767 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4768 | #endif |
| 4769 | i = safe_read(ap->afile, &c, sizeof(c)); |
| 4770 | return i == sizeof(c) ? (c & 0x7f) : (closef(ap->afile), 0); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | /* |
| 4774 | * Return the characters from a here temp file. |
| 4775 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4776 | static int herechar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4777 | { |
| 4778 | char c; |
| 4779 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4780 | if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) { |
| 4781 | close(ap->afile); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4782 | c = '\0'; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4783 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4784 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4785 | } |
| 4786 | |
| 4787 | /* |
| 4788 | * Return the characters produced by a process (`...`). |
| 4789 | * Quote them if required, and remove any trailing newline characters. |
| 4790 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4791 | static int gravechar(struct ioarg *ap, struct io *iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4792 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4793 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4794 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4795 | c = qgravechar(ap, iop) & ~QUOTE; |
| 4796 | if (c == '\n') |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4797 | c = ' '; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4798 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4801 | static int qgravechar(struct ioarg *ap, struct io *iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4802 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4803 | int c; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4804 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4805 | DBGPRINTF3(("QGRAVECHAR: enter, ap=%p, iop=%p\n", ap, iop)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4806 | |
| 4807 | if (iop->xchar) { |
| 4808 | if (iop->nlcount) { |
| 4809 | iop->nlcount--; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4810 | return '\n' | QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4811 | } |
| 4812 | c = iop->xchar; |
| 4813 | iop->xchar = 0; |
| 4814 | } else if ((c = filechar(ap)) == '\n') { |
| 4815 | iop->nlcount = 1; |
| 4816 | while ((c = filechar(ap)) == '\n') |
| 4817 | iop->nlcount++; |
| 4818 | iop->xchar = c; |
| 4819 | if (c == 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4820 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4821 | iop->nlcount--; |
| 4822 | c = '\n'; |
| 4823 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4824 | return c != 0 ? c | QUOTE : 0; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
| 4827 | /* |
| 4828 | * Return a single command (usually the first line) from a file. |
| 4829 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4830 | static int linechar(struct ioarg *ap) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4831 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4832 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4833 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4834 | c = filechar(ap); |
| 4835 | if (c == '\n') { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4836 | if (!multiline) { |
| 4837 | closef(ap->afile); |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4838 | ap->afile = -1; /* illegal value */ |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4839 | } |
| 4840 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4841 | return c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4842 | } |
| 4843 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4844 | /* |
| 4845 | * remap fd into Shell's fd space |
| 4846 | */ |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4847 | static int remap(int fd) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4848 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4849 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4850 | int map[NOFILE]; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4851 | int newfd; |
| 4852 | |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4853 | DBGPRINTF(("REMAP: fd=%d, e.iofd=%d\n", fd, e.iofd)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4854 | |
| 4855 | if (fd < e.iofd) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4856 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4857 | map[i] = 0; |
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 | do { |
| 4860 | map[fd] = 1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4861 | newfd = dup(fd); |
| 4862 | fd = newfd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4863 | } while (fd >= 0 && fd < e.iofd); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4864 | |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4865 | for (i = 0; i < NOFILE; i++) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4866 | if (map[i]) |
| 4867 | close(i); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4868 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4869 | if (fd < 0) |
| 4870 | err("too many files open in shell"); |
| 4871 | } |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4872 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4873 | return fd; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4874 | } |
| 4875 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4876 | static int openpipe(int *pv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4877 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4878 | int i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4879 | |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4880 | i = pipe(pv); |
| 4881 | if (i < 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4882 | err("can't create pipe - try again"); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 4883 | return i; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4886 | static void closepipe(int *pv) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4887 | { |
| 4888 | if (pv != NULL) { |
| 4889 | close(*pv++); |
| 4890 | close(*pv); |
| 4891 | } |
| 4892 | } |
| 4893 | |
Denis Vlasenko | 1e3b068 | 2007-02-01 01:43:54 +0000 | [diff] [blame] | 4894 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4895 | /* -------- here.c -------- */ |
| 4896 | |
| 4897 | /* |
| 4898 | * here documents |
| 4899 | */ |
| 4900 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4901 | static void markhere(char *s, struct ioword *iop) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4902 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4903 | struct here *h, *lh; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4904 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4905 | DBGPRINTF7(("MARKHERE: enter, s=%p\n", s)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4906 | |
| 4907 | h = (struct here *) space(sizeof(struct here)); |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4908 | if (h == NULL) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4909 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4910 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4911 | h->h_tag = evalstr(s, DOSUB); |
| 4912 | if (h->h_tag == 0) |
| 4913 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4914 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4915 | h->h_iop = iop; |
| 4916 | iop->io_name = 0; |
| 4917 | h->h_next = NULL; |
| 4918 | if (inhere == 0) |
| 4919 | inhere = h; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4920 | else { |
| 4921 | for (lh = inhere; lh != NULL; lh = lh->h_next) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4922 | if (lh->h_next == 0) { |
| 4923 | lh->h_next = h; |
| 4924 | break; |
| 4925 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4926 | } |
| 4927 | } |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4928 | iop->io_flag |= IOHERE | IOXHERE; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4929 | for (s = h->h_tag; *s; s++) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4930 | if (*s & QUOTE) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4931 | iop->io_flag &= ~IOXHERE; |
| 4932 | *s &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4933 | } |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 4934 | } |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4935 | h->h_dosub = iop->io_flag & IOXHERE; |
| 4936 | } |
| 4937 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 4938 | static void gethere(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4939 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4940 | struct here *h, *hp; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4941 | |
| 4942 | DBGPRINTF7(("GETHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4943 | |
| 4944 | /* Scan here files first leaving inhere list in place */ |
| 4945 | for (hp = h = inhere; h != NULL; hp = h, h = h->h_next) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4946 | 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] | 4947 | |
| 4948 | /* Make inhere list active - keep list intact for scraphere */ |
| 4949 | if (hp != NULL) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4950 | hp->h_next = acthere; |
| 4951 | acthere = inhere; |
| 4952 | inhere = NULL; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4953 | } |
| 4954 | } |
| 4955 | |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4956 | static void readhere(char **name, char *s, int ec) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4957 | { |
| 4958 | int tf; |
| 4959 | char tname[30] = ".msh_XXXXXX"; |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 4960 | int c; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4961 | jmp_buf ev; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4962 | char myline[LINELIM + 1]; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4963 | char *thenext; |
| 4964 | |
Mike Frysinger | 02d8fa4 | 2006-05-05 20:32:31 +0000 | [diff] [blame] | 4965 | DBGPRINTF7(("READHERE: enter, name=%p, s=%p\n", name, s)); |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4966 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4967 | tf = mkstemp(tname); |
| 4968 | if (tf < 0) |
| 4969 | return; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 4970 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4971 | *name = strsave(tname, areanum); |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 4972 | errpt = ev; |
| 4973 | if (newenv(setjmp(errpt)) != 0) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4974 | unlink(tname); |
| 4975 | else { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4976 | pushio(e.iop->argp, (int (*)(struct ioarg *)) e.iop->iofn); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4977 | e.iobase = e.iop; |
| 4978 | for (;;) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4979 | if (interactive && e.iop <= iostack) { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4980 | #if ENABLE_FEATURE_EDITING |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4981 | current_prompt = cprompt->value; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4982 | #else |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4983 | prs(cprompt->value); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4984 | #endif |
| 4985 | } |
| 4986 | thenext = myline; |
| 4987 | while ((c = my_getc(ec)) != '\n' && c) { |
| 4988 | if (ec == '\'') |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 4989 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 4990 | if (thenext >= &myline[LINELIM]) { |
| 4991 | c = 0; |
| 4992 | break; |
| 4993 | } |
| 4994 | *thenext++ = c; |
| 4995 | } |
| 4996 | *thenext = 0; |
| 4997 | if (strcmp(s, myline) == 0 || c == 0) |
| 4998 | break; |
| 4999 | *thenext++ = '\n'; |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5000 | write(tf, myline, (int) (thenext - myline)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5001 | } |
| 5002 | if (c == 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5003 | prs("here document `"); |
| 5004 | prs(s); |
| 5005 | err("' unclosed"); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5006 | } |
| 5007 | quitenv(); |
| 5008 | } |
| 5009 | close(tf); |
| 5010 | } |
| 5011 | |
| 5012 | /* |
| 5013 | * open here temp file. |
| 5014 | * if unquoted here, expand here temp file into second temp file. |
| 5015 | */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 5016 | static int herein(char *hname, int xdoll) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5017 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5018 | int hf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5019 | int tf; |
| 5020 | |
| 5021 | #if __GNUC__ |
| 5022 | /* Avoid longjmp clobbering */ |
| 5023 | (void) &tf; |
| 5024 | #endif |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5025 | if (hname == NULL) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 5026 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5027 | |
| 5028 | DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll)); |
| 5029 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5030 | hf = open(hname, 0); |
| 5031 | if (hf < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 5032 | return -1; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5033 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5034 | if (xdoll) { |
| 5035 | char c; |
| 5036 | char tname[30] = ".msh_XXXXXX"; |
| 5037 | jmp_buf ev; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5038 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5039 | tf = mkstemp(tname); |
| 5040 | if (tf < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 5041 | return -1; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 5042 | errpt = ev; |
| 5043 | if (newenv(setjmp(errpt)) == 0) { |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5044 | PUSHIO(afile, hf, herechar); |
| 5045 | setbase(e.iop); |
| 5046 | while ((c = subgetc(0, 0)) != 0) { |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5047 | c &= ~QUOTE; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5048 | write(tf, &c, sizeof c); |
| 5049 | } |
| 5050 | quitenv(); |
| 5051 | } else |
| 5052 | unlink(tname); |
| 5053 | close(tf); |
| 5054 | tf = open(tname, 0); |
| 5055 | unlink(tname); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 5056 | return tf; |
Denis Vlasenko | 00ccf95 | 2007-02-01 01:39:24 +0000 | [diff] [blame] | 5057 | } |
| 5058 | return hf; |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 5061 | static void scraphere(void) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5062 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5063 | struct here *h; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5064 | |
| 5065 | DBGPRINTF7(("SCRAPHERE: enter...\n")); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5066 | |
| 5067 | for (h = inhere; h != NULL; h = h->h_next) { |
| 5068 | if (h->h_iop && h->h_iop->io_name) |
Eric Andersen | 8401eea | 2004-08-04 19:16:54 +0000 | [diff] [blame] | 5069 | unlink(h->h_iop->io_name); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5070 | } |
| 5071 | inhere = NULL; |
| 5072 | } |
| 5073 | |
| 5074 | /* unlink here temp files before a freearea(area) */ |
Bernhard Reutner-Fischer | f087798 | 2006-06-25 22:08:53 +0000 | [diff] [blame] | 5075 | static void freehere(int area) |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5076 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 5077 | struct here *h, *hl; |
Eric Andersen | 12de6cf | 2004-08-04 19:19:10 +0000 | [diff] [blame] | 5078 | |
| 5079 | DBGPRINTF6(("FREEHERE: enter, area=%d\n", area)); |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5080 | |
| 5081 | hl = NULL; |
| 5082 | for (h = acthere; h != NULL; h = h->h_next) |
| 5083 | if (getarea((char *) h) >= area) { |
| 5084 | if (h->h_iop->io_name != NULL) |
| 5085 | unlink(h->h_iop->io_name); |
| 5086 | if (hl == NULL) |
| 5087 | acthere = h->h_next; |
| 5088 | else |
| 5089 | hl->h_next = h->h_next; |
| 5090 | } else |
| 5091 | hl = h; |
| 5092 | } |
| 5093 | |
| 5094 | |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5095 | /* -------- sh.c -------- */ |
| 5096 | /* |
| 5097 | * shell |
| 5098 | */ |
| 5099 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 5100 | int msh_main(int argc, char **argv); |
Denis Vlasenko | 489f93e | 2007-02-01 01:43:16 +0000 | [diff] [blame] | 5101 | int msh_main(int argc, char **argv) |
| 5102 | { |
| 5103 | int f; |
| 5104 | char *s; |
| 5105 | int cflag; |
| 5106 | char *name, **ap; |
| 5107 | int (*iof) (struct ioarg *); |
| 5108 | |
| 5109 | #if ENABLE_FEATURE_EDITING |
| 5110 | line_input_state = new_line_input_t(FOR_SHELL); |
| 5111 | #endif |
| 5112 | |
| 5113 | DBGPRINTF(("MSH_MAIN: argc %d, environ %p\n", argc, environ)); |
| 5114 | |
| 5115 | initarea(); |
| 5116 | ap = environ; |
| 5117 | if (ap != NULL) { |
| 5118 | while (*ap) |
| 5119 | assign(*ap++, !COPYV); |
| 5120 | for (ap = environ; *ap;) |
| 5121 | export(lookup(*ap++)); |
| 5122 | } |
| 5123 | closeall(); |
| 5124 | areanum = 1; |
| 5125 | |
| 5126 | shell = lookup("SHELL"); |
| 5127 | if (shell->value == null) |
| 5128 | setval(shell, (char *)DEFAULT_SHELL); |
| 5129 | export(shell); |
| 5130 | |
| 5131 | homedir = lookup("HOME"); |
| 5132 | if (homedir->value == null) |
| 5133 | setval(homedir, "/"); |
| 5134 | export(homedir); |
| 5135 | |
| 5136 | setval(lookup("$"), putn(getpid())); |
| 5137 | |
| 5138 | path = lookup("PATH"); |
| 5139 | if (path->value == null) { |
| 5140 | if (geteuid() == 0) |
| 5141 | setval(path, "/sbin:/bin:/usr/sbin:/usr/bin"); |
| 5142 | else |
| 5143 | setval(path, "/bin:/usr/bin"); |
| 5144 | } |
| 5145 | export(path); |
| 5146 | |
| 5147 | ifs = lookup("IFS"); |
| 5148 | if (ifs->value == null) |
| 5149 | setval(ifs, " \t\n"); |
| 5150 | |
| 5151 | #ifdef MSHDEBUG |
| 5152 | mshdbg_var = lookup("MSHDEBUG"); |
| 5153 | if (mshdbg_var->value == null) |
| 5154 | setval(mshdbg_var, "0"); |
| 5155 | #endif |
| 5156 | |
| 5157 | prompt = lookup("PS1"); |
| 5158 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 5159 | if (prompt->value == null) |
| 5160 | #endif |
| 5161 | setval(prompt, DEFAULT_USER_PROMPT); |
| 5162 | if (geteuid() == 0) { |
| 5163 | setval(prompt, DEFAULT_ROOT_PROMPT); |
| 5164 | prompt->status &= ~EXPORT; |
| 5165 | } |
| 5166 | cprompt = lookup("PS2"); |
| 5167 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 5168 | if (cprompt->value == null) |
| 5169 | #endif |
| 5170 | setval(cprompt, "> "); |
| 5171 | |
| 5172 | iof = filechar; |
| 5173 | cflag = 0; |
| 5174 | name = *argv++; |
| 5175 | if (--argc >= 1) { |
| 5176 | if (argv[0][0] == '-' && argv[0][1] != '\0') { |
| 5177 | for (s = argv[0] + 1; *s; s++) |
| 5178 | switch (*s) { |
| 5179 | case 'c': |
| 5180 | prompt->status &= ~EXPORT; |
| 5181 | cprompt->status &= ~EXPORT; |
| 5182 | setval(prompt, ""); |
| 5183 | setval(cprompt, ""); |
| 5184 | cflag = 1; |
| 5185 | if (--argc > 0) |
| 5186 | PUSHIO(aword, *++argv, iof = nlchar); |
| 5187 | break; |
| 5188 | |
| 5189 | case 'q': |
| 5190 | qflag = SIG_DFL; |
| 5191 | break; |
| 5192 | |
| 5193 | case 's': |
| 5194 | /* standard input */ |
| 5195 | break; |
| 5196 | |
| 5197 | case 't': |
| 5198 | prompt->status &= ~EXPORT; |
| 5199 | setval(prompt, ""); |
| 5200 | iof = linechar; |
| 5201 | break; |
| 5202 | |
| 5203 | case 'i': |
| 5204 | interactive++; |
| 5205 | default: |
| 5206 | if (*s >= 'a' && *s <= 'z') |
| 5207 | flag[(int) *s]++; |
| 5208 | } |
| 5209 | } else { |
| 5210 | argv--; |
| 5211 | argc++; |
| 5212 | } |
| 5213 | |
| 5214 | if (iof == filechar && --argc > 0) { |
| 5215 | setval(prompt, ""); |
| 5216 | setval(cprompt, ""); |
| 5217 | prompt->status &= ~EXPORT; |
| 5218 | cprompt->status &= ~EXPORT; |
| 5219 | |
| 5220 | /* Shell is non-interactive, activate printf-based debug */ |
| 5221 | #ifdef MSHDEBUG |
| 5222 | mshdbg = (int) (((char) (mshdbg_var->value[0])) - '0'); |
| 5223 | if (mshdbg < 0) |
| 5224 | mshdbg = 0; |
| 5225 | #endif |
| 5226 | DBGPRINTF(("MSH_MAIN: calling newfile()\n")); |
| 5227 | |
| 5228 | name = *++argv; |
| 5229 | if (newfile(name)) |
| 5230 | exit(1); /* Exit on error */ |
| 5231 | } |
| 5232 | } |
| 5233 | |
| 5234 | setdash(); |
| 5235 | |
| 5236 | /* This won't be true if PUSHIO has been called, say from newfile() above */ |
| 5237 | if (e.iop < iostack) { |
| 5238 | PUSHIO(afile, 0, iof); |
| 5239 | if (isatty(0) && isatty(1) && !cflag) { |
| 5240 | interactive++; |
| 5241 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 5242 | #ifdef MSHDEBUG |
| 5243 | printf("\n\n%s Built-in shell (msh with debug)\n", BB_BANNER); |
| 5244 | #else |
| 5245 | printf("\n\n%s Built-in shell (msh)\n", BB_BANNER); |
| 5246 | #endif |
| 5247 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 5248 | #endif |
| 5249 | } |
| 5250 | } |
| 5251 | |
| 5252 | signal(SIGQUIT, qflag); |
| 5253 | if (name && name[0] == '-') { |
| 5254 | interactive++; |
| 5255 | f = open(".profile", 0); |
| 5256 | if (f >= 0) |
| 5257 | next(remap(f)); |
| 5258 | f = open("/etc/profile", 0); |
| 5259 | if (f >= 0) |
| 5260 | next(remap(f)); |
| 5261 | } |
| 5262 | if (interactive) |
| 5263 | signal(SIGTERM, sig); |
| 5264 | |
| 5265 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 5266 | signal(SIGINT, onintr); |
| 5267 | dolv = argv; |
| 5268 | dolc = argc; |
| 5269 | dolv[0] = name; |
| 5270 | if (dolc > 1) { |
| 5271 | for (ap = ++argv; --argc > 0;) { |
| 5272 | *ap = *argv++; |
| 5273 | if (assign(*ap, !COPYV)) { |
| 5274 | dolc--; /* keyword */ |
| 5275 | } else { |
| 5276 | ap++; |
| 5277 | } |
| 5278 | } |
| 5279 | } |
| 5280 | setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc)); |
| 5281 | |
| 5282 | DBGPRINTF(("MSH_MAIN: begin FOR loop, interactive %d, e.iop %p, iostack %p\n", interactive, e.iop, iostack)); |
| 5283 | |
| 5284 | for (;;) { |
| 5285 | if (interactive && e.iop <= iostack) { |
| 5286 | #if ENABLE_FEATURE_EDITING |
| 5287 | current_prompt = prompt->value; |
| 5288 | #else |
| 5289 | prs(prompt->value); |
| 5290 | #endif |
| 5291 | } |
| 5292 | onecommand(); |
| 5293 | /* Ensure that getenv("PATH") stays current */ |
| 5294 | setenv("PATH", path->value, 1); |
| 5295 | } |
| 5296 | |
| 5297 | DBGPRINTF(("MSH_MAIN: returning.\n")); |
| 5298 | } |
| 5299 | |
| 5300 | |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5301 | /* |
| 5302 | * Copyright (c) 1987,1997, Prentice Hall |
| 5303 | * All rights reserved. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5304 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5305 | * Redistribution and use of the MINIX operating system in source and |
| 5306 | * binary forms, with or without modification, are permitted provided |
| 5307 | * that the following conditions are met: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5308 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5309 | * Redistributions of source code must retain the above copyright |
| 5310 | * notice, this list of conditions and the following disclaimer. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5311 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5312 | * Redistributions in binary form must reproduce the above |
| 5313 | * copyright notice, this list of conditions and the following |
| 5314 | * disclaimer in the documentation and/or other materials provided |
| 5315 | * with the distribution. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5316 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5317 | * Neither the name of Prentice Hall nor the names of the software |
| 5318 | * authors or contributors may be used to endorse or promote |
| 5319 | * products derived from this software without specific prior |
| 5320 | * written permission. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5321 | * |
Eric Andersen | ff9eee4 | 2001-06-29 04:57:14 +0000 | [diff] [blame] | 5322 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND |
| 5323 | * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 5324 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 5325 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 5326 | * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE |
| 5327 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 5328 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 5329 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 5330 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 5331 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 5332 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 5333 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 5334 | * |
| 5335 | */ |