5dc0b2b5143c27c7a96795dbb8532a6091a34767
[oweals/busybox.git] / shell / msh.c
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
6  * by Erik Andersen <andersen@codepoet.org>
7  *
8  * - backtick expansion did not work properly
9  *   Jonas Holmberg <jonas.holmberg@axis.com>
10  *   Robert Schwebel <r.schwebel@pengutronix.de>
11  *   Erik Andersen <andersen@codepoet.org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  * Original copyright notice is retained at the end of this file.
28  */
29
30 #include <ctype.h>
31 #include <dirent.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <limits.h>
35 #include <setjmp.h>
36 #include <signal.h>
37 #include <stddef.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include <unistd.h>
43 #include <sys/stat.h>
44 #include <sys/times.h>
45 #include <sys/types.h>
46 #include <sys/wait.h>
47
48 #include "cmdedit.h"
49 #include "busybox.h"
50
51
52 /* -------- sh.h -------- */
53 /*
54  * shell
55  */
56
57 #define LINELIM 2100
58 #define NPUSH   8       /* limit to input nesting */
59
60 #undef NOFILE
61 #define NOFILE  20      /* Number of open files */
62 #define NUFILE  10      /* Number of user-accessible files */
63 #define FDBASE  10      /* First file usable by Shell */
64
65 /*
66  * values returned by wait
67  */
68 #define WAITSIG(s) ((s)&0177)
69 #define WAITVAL(s) (((s)>>8)&0377)
70 #define WAITCORE(s) (((s)&0200)!=0)
71
72 /*
73  * library and system defintions
74  */
75 typedef void xint;      /* base type of jmp_buf, for not broken compilers */
76
77 /*
78  * shell components
79  */
80
81 #define QUOTE   0200
82
83 #define NOBLOCK ((struct op *)NULL)
84 #define NOWORD  ((char *)NULL)
85 #define NOWORDS ((char **)NULL)
86 #define NOPIPE  ((int *)NULL)
87
88 /*
89  * Description of a command or an operation on commands.
90  * Might eventually use a union.
91  */
92 struct op {
93         int     type;   /* operation type, see below */
94         char    **words;        /* arguments to a command */
95         struct  ioword  **ioact;        /* IO actions (eg, < > >>) */
96         struct op *left;
97         struct op *right;
98         char    *str;   /* identifier for case and for */
99 };
100
101 #define TCOM    1       /* command */
102 #define TPAREN  2       /* (c-list) */
103 #define TPIPE   3       /* a | b */
104 #define TLIST   4       /* a [&;] b */
105 #define TOR     5       /* || */
106 #define TAND    6       /* && */
107 #define TFOR    7
108 #define TDO     8
109 #define TCASE   9
110 #define TIF     10
111 #define TWHILE  11
112 #define TUNTIL  12
113 #define TELIF   13
114 #define TPAT    14      /* pattern in case */
115 #define TBRACE  15      /* {c-list} */
116 #define TASYNC  16      /* c & */
117
118 /*
119  * actions determining the environment of a process
120  */
121 #define BIT(i)  (1<<(i))
122 #define FEXEC   BIT(0)  /* execute without forking */
123
124 /*
125  * flags to control evaluation of words
126  */
127 #define DOSUB   1       /* interpret $, `, and quotes */
128 #define DOBLANK 2       /* perform blank interpretation */
129 #define DOGLOB  4       /* interpret [?* */
130 #define DOKEY   8       /* move words with `=' to 2nd arg. list */
131 #define DOTRIM  16      /* trim resulting string */
132
133 #define DOALL   (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
134
135 static  char    **dolv;
136 static  int     dolc;
137 static  int     exstat;
138 static  char    gflg;
139 static  int     interactive;    /* Is this an interactive shell */
140 static  int     execflg;
141 static  int     multiline;      /* \n changed to ; */
142 static  struct  op      *outtree;       /* result from parser */
143
144 static  xint    *failpt;
145 static  xint    *errpt;
146 static  struct brkcon   *brklist;
147 static  int     isbreak;
148 static int newfile(char *s);
149 static char *findeq(char *cp);
150 static char *cclass(char *p, int sub);
151 static void initarea(void);
152 extern int msh_main(int argc, char **argv);
153
154
155 struct  brkcon {
156         jmp_buf brkpt;
157         struct  brkcon  *nextlev;
158 } ;
159
160 /*
161  * redirection
162  */
163 struct ioword {
164         short   io_unit;        /* unit affected */
165         short   io_flag;        /* action (below) */
166         char    *io_name;       /* file name */
167 };
168 #define IOREAD  1       /* < */
169 #define IOHERE  2       /* << (here file) */
170 #define IOWRITE 4       /* > */
171 #define IOCAT   8       /* >> */
172 #define IOXHERE 16      /* ${}, ` in << */
173 #define IODUP   32      /* >&digit */
174 #define IOCLOSE 64      /* >&- */
175
176 #define IODEFAULT (-1)  /* token for default IO unit */
177
178 static  struct  wdblock *wdlist;
179 static  struct  wdblock *iolist;
180
181 /*
182  * parsing & execution environment
183  */
184 static struct   env {
185         char    *linep;
186         struct  io      *iobase;
187         struct  io      *iop;
188         xint    *errpt;
189         int     iofd;
190         struct  env     *oenv;
191 } e;
192
193 /*
194  * flags:
195  * -e: quit on error
196  * -k: look for name=value everywhere on command line
197  * -n: no execution
198  * -t: exit after reading and executing one command
199  * -v: echo as read
200  * -x: trace
201  * -u: unset variables net diagnostic
202  */
203 static  char    *flag;
204
205 static  char    *null;  /* null value for variable */
206 static  int     intr;   /* interrupt pending */
207
208 static  char    *trap[_NSIG+1];
209 static  char    ourtrap[_NSIG+1];
210 static  int     trapset;        /* trap pending */
211
212 static  int     heedint;        /* heed interrupt signals */
213
214 static  int     yynerrs;        /* yacc */
215
216 static  char    line[LINELIM];
217 static  char    *elinep;
218
219 /*
220  * other functions
221  */
222 static int(*inbuilt(char *s))(struct op *);
223
224
225 static char *rexecve (char *c , char **v, char **envp );
226 static char *space (int n );
227 static char *strsave (char *s, int a );
228 static char *evalstr (char *cp, int f );
229 static char *putn (int n );
230 static char *itoa (int n );
231 static char *unquote (char *as );
232 static struct var *lookup (char *n );
233 static int rlookup (char *n );
234 static struct wdblock *glob (char *cp, struct wdblock *wb );
235 static int my_getc( int ec);
236 static int subgetc (int ec, int quoted );
237 static char **makenv (void);
238 static char **eval (char **ap, int f );
239 static int setstatus (int s );
240 static int waitfor (int lastpid, int canintr );
241
242 static void onintr (int s ); /* SIGINT handler */
243
244 static int newenv (int f );
245 static void quitenv (void);
246 static void err (char *s );
247 static int anys (char *s1, char *s2 );
248 static int any (int c, char *s );
249 static void next (int f );
250 static void setdash (void);
251 static void onecommand (void);
252 static void runtrap (int i );
253 static int gmatch (char *s, char *p );
254
255 /*
256  * error handling
257  */
258 static void leave (void); /* abort shell (or fail in subshell) */
259 static void fail (void);         /* fail but return to process next command */
260 static void warn (char *s );
261 static void sig (int i );        /* default signal handler */
262
263
264
265 /* -------- area stuff -------- */
266
267 #define REGSIZE         sizeof(struct region)
268 #define GROWBY          256
269 //#define       SHRINKBY   64
270 #undef  SHRINKBY
271 #define FREE 32767
272 #define BUSY 0
273 #define ALIGN (sizeof(int)-1)
274
275
276 struct region {
277         struct  region *next;
278         int     area;
279 };
280
281
282
283 /* -------- grammar stuff -------- */
284 typedef union {
285         char    *cp;
286         char    **wp;
287         int     i;
288         struct  op *o;
289 } YYSTYPE;
290 #define WORD    256
291 #define LOGAND  257
292 #define LOGOR   258
293 #define BREAK   259
294 #define IF      260
295 #define THEN    261
296 #define ELSE    262
297 #define ELIF    263
298 #define FI      264
299 #define CASE    265
300 #define ESAC    266
301 #define FOR     267
302 #define WHILE   268
303 #define UNTIL   269
304 #define DO      270
305 #define DONE    271
306 #define IN      272
307 #define YYERRCODE 300
308
309 /* flags to yylex */
310 #define CONTIN  01      /* skip new lines to complete command */
311
312 #define SYNTAXERR       zzerr()
313 static struct op *pipeline(int cf );
314 static struct op *andor(void);
315 static struct op *c_list(void);
316 static int synio(int cf );
317 static void musthave (int c, int cf );
318 static struct op *simple(void);
319 static struct op *nested(int type, int mark );
320 static struct op *command(int cf );
321 static struct op *dogroup(int onlydone );
322 static struct op *thenpart(void);
323 static struct op *elsepart(void);
324 static struct op *caselist(void);
325 static struct op *casepart(void);
326 static char **pattern(void);
327 static char **wordlist(void);
328 static struct op *list(struct op *t1, struct op *t2 );
329 static struct op *block(int type, struct op *t1, struct op *t2, char **wp );
330 static struct op *newtp(void);
331 static struct op *namelist(struct op *t );
332 static char **copyw(void);
333 static void word(char *cp );
334 static struct ioword **copyio(void);
335 static struct ioword *io (int u, int f, char *cp );
336 static void zzerr(void);
337 static void yyerror(char *s );
338 static int yylex(int cf );
339 static int collect(int c, int c1 );
340 static int dual(int c );
341 static void diag(int ec );
342 static char *tree(unsigned size );
343
344 /* -------- var.h -------- */
345
346 struct  var {
347         char    *value;
348         char    *name;
349         struct  var     *next;
350         char    status;
351 };
352 #define COPYV   1       /* flag to setval, suggesting copy */
353 #define RONLY   01      /* variable is read-only */
354 #define EXPORT  02      /* variable is to be exported */
355 #define GETCELL 04      /* name & value space was got with getcell */
356
357 static  struct  var     *vlist;         /* dictionary */
358
359 static  struct  var     *homedir;       /* home directory */
360 static  struct  var     *prompt;        /* main prompt */
361 static  struct  var     *cprompt;       /* continuation prompt */
362 static  struct  var     *path;          /* search path for commands */
363 static  struct  var     *shell;         /* shell to interpret command files */
364 static  struct  var     *ifs;           /* field separators */
365
366 static int yyparse (void);
367 static struct var *lookup (char *n );
368 static void setval (struct var *vp, char *val );
369 static void nameval (struct var *vp, char *val, char *name );
370 static void export (struct var *vp );
371 static void ronly (struct var *vp );
372 static int isassign (char *s );
373 static int checkname (char *cp );
374 static int assign (char *s, int cf );
375 static void putvlist (int f, int out );
376 static int eqname (char *n1, char *n2 );
377
378 static int execute (struct op *t, int *pin, int *pout, int act );
379
380 /* -------- io.h -------- */
381 /* io buffer */
382 struct iobuf {
383   unsigned id;                          /* buffer id */
384   char buf[512];                        /* buffer */
385   char *bufp;                           /* pointer into buffer */
386   char *ebufp;                          /* pointer to end of buffer */
387 };
388
389 /* possible arguments to an IO function */
390 struct ioarg {
391         char    *aword;
392         char    **awordlist;
393         int     afile;          /* file descriptor */
394         unsigned afid;          /* buffer id */
395         long    afpos;          /* file position */
396         struct iobuf *afbuf;    /* buffer for this file */
397 };
398 //static struct ioarg ioargstack[NPUSH];
399 #define AFID_NOBUF      (~0)
400 #define AFID_ID         0
401
402 /* an input generator's state */
403 struct  io {
404         int     (*iofn)(struct  ioarg *, struct io *);
405         struct  ioarg   *argp;
406         int     peekc;
407         char    prev;           /* previous character read by readc() */
408         char    nlcount;        /* for `'s */
409         char    xchar;          /* for `'s */
410         char    task;           /* reason for pushed IO */
411 };
412 //static        struct  io      iostack[NPUSH];
413 #define XOTHER  0       /* none of the below */
414 #define XDOLL   1       /* expanding ${} */
415 #define XGRAVE  2       /* expanding `'s */
416 #define XIO     3       /* file IO */
417
418 /* in substitution */
419 #define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL)
420
421 /*
422  * input generators for IO structure
423  */
424 static int nlchar (struct ioarg *ap );
425 static int strchar (struct ioarg *ap );
426 static int qstrchar (struct ioarg *ap );
427 static int filechar (struct ioarg *ap );
428 static int herechar (struct ioarg *ap );
429 static int linechar (struct ioarg *ap );
430 static int gravechar (struct ioarg *ap, struct io *iop );
431 static int qgravechar (struct ioarg *ap, struct io *iop );
432 static int dolchar (struct ioarg *ap );
433 static int wdchar (struct ioarg *ap );
434 static void scraphere (void);
435 static void freehere (int area );
436 static void gethere (void);
437 static void markhere (char *s, struct ioword *iop );
438 static int herein (char *hname, int xdoll );
439 static int run (struct ioarg *argp, int (*f)(struct ioarg *));
440
441 /*
442  * IO functions
443  */
444 static int eofc (void);
445 static int readc (void);
446 static void unget (int c );
447 static void ioecho (int c );
448 static void prs (char *s );
449 static void prn (unsigned u );
450 static void closef (int i );
451 static void closeall (void);
452
453 /*
454  * IO control
455  */
456 static void pushio (struct ioarg *argp, int (*f)(struct ioarg *));
457 static int remap (int fd );
458 static int openpipe (int *pv );
459 static void closepipe (int *pv );
460 static struct io *setbase (struct io *ip );
461
462 static  struct  ioarg   temparg;        /* temporary for PUSHIO */
463 #define PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen)))
464 #define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen)))
465
466 /* -------- word.h -------- */
467
468 #define NSTART  16      /* default number of words to allow for initially */
469
470 struct  wdblock {
471         short   w_bsize;
472         short   w_nword;
473         /* bounds are arbitrary */
474         char    *w_words[1];
475 };
476
477 static struct wdblock *addword (char *wd, struct wdblock *wb );
478 static struct wdblock *newword (int nw );
479 static char **getwords (struct wdblock *wb );
480
481 /* -------- area.h -------- */
482
483 /*
484  * storage allocation
485  */
486 static char *getcell (unsigned nbytes );
487 static void garbage (void);
488 static void setarea (char *cp, int a );
489 static int getarea (char *cp );
490 static void freearea (int a );
491 static void freecell (char *cp );
492 static  int     areanum;        /* current allocation area */
493
494 #define NEW(type) (type *)getcell(sizeof(type))
495 #define DELETE(obj)     freecell((char *)obj)
496
497
498 /* -------- misc stuff -------- */
499
500 static int forkexec (struct op *t, int *pin, int *pout, int act, char **wp, int *pforked );
501 static int iosetup (struct ioword *iop, int pipein, int pipeout );
502 static void echo(char **wp );
503 static struct op **find1case (struct op *t, char *w );
504 static struct op *findcase (struct op *t, char *w );
505 static void brkset(struct brkcon *bc );
506 static int dolabel(struct op *t );
507 static int dohelp(struct op *t );
508 static int dochdir(struct op *t );
509 static int doshift(struct op *t );
510 static int dologin(struct op *t );
511 static int doumask(struct op *t );
512 static int doexec(struct op *t );
513 static int dodot(struct op *t );
514 static int dowait(struct op *t );
515 static int doread(struct op *t );
516 static int doeval(struct op *t );
517 static int dotrap(struct op *t );
518 static int getsig(char *s );
519 static void setsig (int n, sighandler_t f);
520 static int getn(char *as );
521 static int dobreak(struct op *t );
522 static int docontinue(struct op *t );
523 static int brkcontin (char *cp, int val );
524 static int doexit(struct op *t );
525 static int doexport(struct op *t );
526 static int doreadonly(struct op *t );
527 static void rdexp (char **wp, void (*f)(struct var *), int key);
528 static void badid(char *s );
529 static int doset(struct op *t );
530 static void varput (char *s, int out );
531 static int dotimes(struct op *t );
532 static int expand (char *cp, struct wdblock **wbp, int f );
533 static char *blank(int f );
534 static int dollar(int quoted );
535 static int grave(int quoted );
536 static void globname (char *we, char *pp );
537 static char *generate (char *start1, char *end1, char *middle, char *end );
538 static int anyspcl(struct wdblock *wb );
539 static int xstrcmp (char *p1, char *p2 );
540 static void glob0 (char *a0, unsigned int a1, int a2, int (*a3)(char *, char *));
541 static void glob1 (char *base, char *lim );
542 static void glob2 (char *i, char *j );
543 static void glob3 (char *i, char *j, char *k );
544 static void readhere (char **name, char *s, int ec );
545 static void pushio (struct ioarg *argp, int (*f)(struct ioarg *));
546 static int xxchar(struct ioarg *ap );
547
548 struct  here {
549         char    *h_tag;
550         int     h_dosub;
551         struct  ioword *h_iop;
552         struct  here    *h_next;
553 };
554
555 static  char    *signame[] = {
556         "Signal 0",
557         "Hangup",
558         (char *)NULL,   /* interrupt */
559         "Quit",
560         "Illegal instruction",
561         "Trace/BPT trap",
562         "Abort",
563         "Bus error",
564         "Floating Point Exception",
565         "Killed",
566         "SIGUSR1",
567         "SIGSEGV",
568         "SIGUSR2",
569         (char *)NULL,   /* broken pipe */
570         "Alarm clock",
571         "Terminated",
572 };
573 #define NSIGNAL (sizeof(signame)/sizeof(signame[0]))
574
575 struct res {
576         char *r_name;
577         int       r_val;
578 };
579 static struct res restab[] = {
580     {"for",             FOR},
581     {"case",    CASE},
582     {"esac",    ESAC},
583     {"while",   WHILE},
584     {"do",              DO},
585     {"done",    DONE},
586     {"if",              IF},
587     {"in",              IN},
588     {"then",    THEN},
589     {"else",    ELSE},
590     {"elif",    ELIF},
591     {"until",   UNTIL},
592     {"fi",              FI},
593
594     {";;",              BREAK},
595     {"||",              LOGOR},
596     {"&&",              LOGAND},
597     {"{",               '{'},
598     {"}",               '}'},
599     {0,         0},
600 };
601
602
603 struct builtincmd {
604         const char *name;
605         int (*builtinfunc)(struct op *t);
606 };
607 static const struct     builtincmd      builtincmds[] = {
608     {".",               dodot},
609     {":",               dolabel},
610     {"break",   dobreak},
611     {"cd",              dochdir},
612     {"continue",docontinue},
613     {"eval",    doeval},
614     {"exec",    doexec},
615     {"exit",    doexit},
616     {"export",  doexport},
617     {"help",    dohelp},
618     {"login",   dologin},
619     {"newgrp",  dologin},
620     {"read",    doread},
621     {"readonly",doreadonly},
622     {"set",             doset},
623     {"shift",   doshift},
624     {"times",   dotimes},
625     {"trap",    dotrap},
626     {"umask",   doumask},
627     {"wait",    dowait},
628     {0,0}
629 };
630
631 /* Globals */
632 extern  char    **environ;      /* environment pointer */
633 static char     **dolv;
634 static int      dolc;
635 static int      exstat;
636 static char     gflg;
637 static int      interactive;    /* Is this an interactive shell */
638 static int      execflg;
639 static int      multiline;      /* \n changed to ; */
640 static struct   op      *outtree;       /* result from parser */
641 static xint     *failpt;
642 static xint     *errpt;
643 static struct   brkcon  *brklist;
644 static int      isbreak;
645 static struct   wdblock *wdlist;
646 static struct   wdblock *iolist;
647 static char     *trap[_NSIG+1];
648 static char     ourtrap[_NSIG+1];
649 static int      trapset;        /* trap pending */
650 static int      yynerrs;        /* yacc */
651 static char     line[LINELIM];
652 static struct   var     *vlist;         /* dictionary */
653 static struct   var     *homedir;       /* home directory */
654 static struct   var     *prompt;        /* main prompt */
655 static struct   var     *cprompt;       /* continuation prompt */
656 static struct   var     *path;          /* search path for commands */
657 static struct   var     *shell;         /* shell to interpret command files */
658 static struct   var     *ifs;           /* field separators */
659 static struct   ioarg ioargstack[NPUSH];
660 static struct   io      iostack[NPUSH];
661 static int      areanum;        /* current allocation area */
662 static int      intr;
663 static int      inparse;
664 static char     flags['z'-'a'+1];
665 static char     *flag = flags-'a';
666 static char     *elinep = line+sizeof(line)-5;
667 static char     *null   = "";
668 static int      heedint =1;
669 static struct env e ={line, iostack, iostack-1, (xint *)NULL, FDBASE, (struct env *)NULL};
670 static void (*qflag)(int) = SIG_IGN;
671 static  int     startl;
672 static  int     peeksym;
673 static  int     nlseen;
674 static  int     iounit = IODEFAULT;
675 static  YYSTYPE yylval;
676 static struct iobuf sharedbuf = {AFID_NOBUF};
677 static struct iobuf mainbuf = {AFID_NOBUF};
678 static unsigned bufid = AFID_ID;        /* buffer id counter */
679 static struct ioarg temparg = {0, 0, 0, AFID_NOBUF, 0};
680 static  struct here *inhere;            /* list of hear docs while parsing */
681 static  struct here *acthere;           /* list of active here documents */
682 static  struct region *areabot;         /* bottom of area */
683 static  struct region *areatop;         /* top of area */
684 static  struct region *areanxt;         /* starting point of scan */
685 static void * brktop;
686 static void * brkaddr;
687
688
689 #ifdef CONFIG_FEATURE_COMMAND_EDITING
690 static char * current_prompt;
691 #endif
692
693 /* -------- sh.c -------- */
694 /*
695  * shell
696  */
697
698
699 extern int msh_main(int argc, char **argv)
700 {
701         register int f;
702         register char *s;
703         int cflag;
704         char *name, **ap;
705         int (*iof)(struct ioarg *);
706
707         initarea();
708         if ((ap = environ) != NULL) {
709                 while (*ap)
710                         assign(*ap++, !COPYV);
711                 for (ap = environ; *ap;)
712                         export(lookup(*ap++));
713         }
714         closeall();
715         areanum = 1;
716
717         shell = lookup("SHELL");
718         if (shell->value == null)
719                 setval(shell, DEFAULT_SHELL);
720         export(shell);
721
722         homedir = lookup("HOME");
723         if (homedir->value == null)
724                 setval(homedir, "/");
725         export(homedir);
726
727         setval(lookup("$"), putn(getpid()));
728
729         path = lookup("PATH");
730         if (path->value == null) {
731                 if (geteuid() == 0)
732                         setval(path, "/sbin:/bin:/usr/sbin:/usr/bin");
733                 else
734                         setval(path, "/bin:/usr/bin");
735         }
736         export(path);
737
738         ifs = lookup("IFS");
739         if (ifs->value == null)
740                 setval(ifs, " \t\n");
741
742         prompt = lookup("PS1");
743 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
744         if (prompt->value == null)
745 #endif
746                 setval(prompt, "$ ");
747         if (geteuid() == 0) {
748                 setval(prompt, "# ");
749                 prompt->status &= ~EXPORT;
750         }
751         cprompt = lookup("PS2");
752 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
753         if (cprompt->value == null)
754 #endif
755                 setval(cprompt, "> ");
756
757         iof = filechar;
758         cflag = 0;
759         name = *argv++;
760         if (--argc >= 1) {
761                 if(argv[0][0] == '-' && argv[0][1] != '\0') {
762                         for (s = argv[0]+1; *s; s++)
763                                 switch (*s) {
764                                 case 'c':
765                                         prompt->status &= ~EXPORT;
766                                         cprompt->status &= ~EXPORT;
767                                         setval(prompt, "");
768                                         setval(cprompt, "");
769                                         cflag = 1;
770                                         if (--argc > 0)
771                                                 PUSHIO(aword, *++argv, iof = nlchar);
772                                         break;
773
774                                 case 'q':
775                                         qflag = SIG_DFL;
776                                         break;
777
778                                 case 's':
779                                         /* standard input */
780                                         break;
781
782                                 case 't':
783                                         prompt->status &= ~EXPORT;
784                                         setval(prompt, "");
785                                         iof = linechar;
786                                         break;
787
788                                 case 'i':
789                                         interactive++;
790                                 default:
791                                         if (*s>='a' && *s<='z')
792                                                 flag[(int)*s]++;
793                                 }
794                 } else {
795                         argv--;
796                         argc++;
797                 }
798                 if (iof == filechar && --argc > 0) {
799                         setval(prompt, "");
800                         setval(cprompt, "");
801                         prompt->status &= ~EXPORT;
802                         cprompt->status &= ~EXPORT;
803                         if (newfile(name = *++argv))
804                                 exit(1);
805                 }
806         }
807         setdash();
808         if (e.iop < iostack) {
809                 PUSHIO(afile, 0, iof);
810                 if (isatty(0) && isatty(1) && !cflag) {
811                         interactive++;
812 #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
813                         printf( "\n\n" BB_BANNER " Built-in shell (msh)\n");
814                         printf( "Enter 'help' for a list of built-in commands.\n\n");
815 #endif
816                 }
817         }
818         signal(SIGQUIT, qflag);
819         if (name && name[0] == '-') {
820                 interactive++;
821                 if ((f = open(".profile", 0)) >= 0)
822                         next(remap(f));
823                 if ((f = open("/etc/profile", 0)) >= 0)
824                         next(remap(f));
825         }
826         if (interactive)
827                 signal(SIGTERM, sig);
828         if (signal(SIGINT, SIG_IGN) != SIG_IGN)
829                 signal(SIGINT, onintr);
830         dolv = argv;
831         dolc = argc;
832         dolv[0] = name;
833         if (dolc > 1) {
834                 for (ap = ++argv; --argc > 0;) {
835                         if (assign(*ap = *argv++, !COPYV)) {
836                                 dolc--; /* keyword */
837                         } else {
838                                 ap++;
839                         }
840                 }
841         }
842         setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
843
844         for (;;) {
845                 if (interactive && e.iop <= iostack) {
846 #ifdef CONFIG_FEATURE_COMMAND_EDITING
847                         current_prompt=prompt->value;
848 #else
849                         prs(prompt->value);
850 #endif
851                 }
852                 onecommand();
853                 /* Ensure that getenv("PATH") stays current */
854                 setenv("PATH", path->value, 1);
855         }
856 }
857
858 static void
859 setdash()
860 {
861         register char *cp;
862         register int c;
863         char m['z'-'a'+1];
864
865         cp = m;
866         for (c='a'; c<='z'; c++)
867                 if (flag[(int)c])
868                         *cp++ = c;
869         *cp = 0;
870         setval(lookup("-"), m);
871 }
872
873 static int
874 newfile(s)
875 register char *s;
876 {
877         register int f;
878
879         if (strcmp(s, "-") != 0) {
880                 f = open(s, 0);
881                 if (f < 0) {
882                         prs(s);
883                         err(": cannot open");
884                         return(1);
885                 }
886         } else
887                 f = 0;
888         next(remap(f));
889         return(0);
890 }
891
892 static void
893 onecommand()
894 {
895         register int i;
896         jmp_buf m1;
897
898         while (e.oenv)
899                 quitenv();
900         areanum = 1;
901         freehere(areanum);
902         freearea(areanum);
903         garbage();
904         wdlist = 0;
905         iolist = 0;
906         e.errpt = 0;
907         e.linep = line;
908         yynerrs = 0;
909         multiline = 0;
910         inparse = 1;
911         intr = 0;
912         execflg = 0;
913         setjmp(failpt = m1);    /* Bruce Evans' fix */
914         if (setjmp(failpt = m1) || yyparse() || intr) {
915                 while (e.oenv)
916                         quitenv();
917                 scraphere();
918                 if (!interactive && intr)
919                         leave();
920                 inparse = 0;
921                 intr = 0;
922                 return;
923         }
924         inparse = 0;
925         brklist = 0;
926         intr = 0;
927         execflg = 0;
928         if (!flag['n'])
929                 execute(outtree, NOPIPE, NOPIPE, 0);
930         if (!interactive && intr) {
931                 execflg = 0;
932                 leave();
933         }
934         if ((i = trapset) != 0) {
935                 trapset = 0;
936                 runtrap(i);
937         }
938 }
939
940 static void
941 fail()
942 {
943         longjmp(failpt, 1);
944         /* NOTREACHED */
945 }
946
947 static void
948 leave()
949 {
950         if (execflg)
951                 fail();
952         scraphere();
953         freehere(1);
954         runtrap(0);
955         exit(exstat);
956         /* NOTREACHED */
957 }
958
959 static void
960 warn(s)
961 register char *s;
962 {
963         if(*s) {
964                 prs(s);
965                 exstat = -1;
966         }
967         prs("\n");
968         if (flag['e'])
969                 leave();
970 }
971
972 static void
973 err(s)
974 char *s;
975 {
976         warn(s);
977         if (flag['n'])
978                 return;
979         if (!interactive)
980                 leave();
981         if (e.errpt)
982                 longjmp(e.errpt, 1);
983         closeall();
984         e.iop = e.iobase = iostack;
985 }
986
987 static int
988 newenv(f)
989 int f;
990 {
991         register struct env *ep;
992
993         if (f) {
994                 quitenv();
995                 return(1);
996         }
997         ep = (struct env *) space(sizeof(*ep));
998         if (ep == NULL) {
999                 while (e.oenv)
1000                         quitenv();
1001                 fail();
1002         }
1003         *ep = e;
1004         e.oenv = ep;
1005         e.errpt = errpt;
1006         return(0);
1007 }
1008
1009 static void
1010 quitenv()
1011 {
1012         register struct env *ep;
1013         register int fd;
1014
1015         if ((ep = e.oenv) != NULL) {
1016                 fd = e.iofd;
1017                 e = *ep;
1018                 /* should close `'d files */
1019                 DELETE(ep);
1020                 while (--fd >= e.iofd)
1021                         close(fd);
1022         }
1023 }
1024
1025 /*
1026  * Is any character from s1 in s2?
1027  */
1028 static int
1029 anys(s1, s2)
1030 register char *s1, *s2;
1031 {
1032         while (*s1)
1033                 if (any(*s1++, s2))
1034                         return(1);
1035         return(0);
1036 }
1037
1038 /*
1039  * Is character c in s?
1040  */
1041 static int
1042 any(c, s)
1043 register int c;
1044 register char *s;
1045 {
1046         while (*s)
1047                 if (*s++ == c)
1048                         return(1);
1049         return(0);
1050 }
1051
1052 static char *
1053 putn(n)
1054 register int n;
1055 {
1056         return(itoa(n));
1057 }
1058
1059 static char *
1060 itoa(n)
1061 register int n;
1062 {
1063         static char s[20];
1064         snprintf(s, sizeof(s), "%u", n);
1065         return(s);
1066 }
1067
1068 static void
1069 next(int f)
1070 {
1071         PUSHIO(afile, f, filechar);
1072 }
1073
1074 static void
1075 onintr(s)
1076 int s;                          /* ANSI C requires a parameter */
1077 {
1078         signal(SIGINT, onintr);
1079         intr = 1;
1080         if (interactive) {
1081                 if (inparse) {
1082                         prs("\n");
1083                         fail();
1084                 }
1085         }
1086         else if (heedint) {
1087                 execflg = 0;
1088                 leave();
1089         }
1090 }
1091
1092 static char *
1093 space(n)
1094 int n;
1095 {
1096         register char *cp;
1097
1098         if ((cp = getcell(n)) == 0)
1099                 err("out of string space");
1100         return(cp);
1101 }
1102
1103 static char *
1104 strsave(s, a)
1105 register char *s;
1106 int a;
1107 {
1108         register char *cp, *xp;
1109
1110         if ((cp = space(strlen(s)+1)) != NULL) {
1111                 setarea((char *)cp, a);
1112                 for (xp = cp; (*xp++ = *s++) != '\0';)
1113                         ;
1114                 return(cp);
1115         }
1116         return("");
1117 }
1118
1119 /*
1120  * trap handling
1121  */
1122 static void
1123 sig(i)
1124 register int i;
1125 {
1126         trapset = i;
1127         signal(i, sig);
1128 }
1129
1130 static void runtrap(i)
1131 int i;
1132 {
1133         char *trapstr;
1134
1135         if ((trapstr = trap[i]) == NULL)
1136                 return;
1137         if (i == 0)
1138                 trap[i] = 0;
1139         RUN(aword, trapstr, nlchar);
1140 }
1141
1142 /* -------- var.c -------- */
1143
1144 /*
1145  * Find the given name in the dictionary
1146  * and return its value.  If the name was
1147  * not previously there, enter it now and
1148  * return a null value.
1149  */
1150 static struct var *
1151 lookup(n)
1152 register char *n;
1153 {
1154         register struct var *vp;
1155         register char *cp;
1156         register int c;
1157         static struct var dummy;
1158
1159         if (isdigit(*n)) {
1160                 dummy.name = n;
1161                 for (c = 0; isdigit(*n) && c < 1000; n++)
1162                         c = c*10 + *n-'0';
1163                 dummy.status = RONLY;
1164                 dummy.value = c <= dolc? dolv[c]: null;
1165                 return(&dummy);
1166         }
1167         for (vp = vlist; vp; vp = vp->next)
1168                 if (eqname(vp->name, n))
1169                         return(vp);
1170         cp = findeq(n);
1171         vp = (struct var *)space(sizeof(*vp));
1172         if (vp == 0 || (vp->name = space((int)(cp-n)+2)) == 0) {
1173                 dummy.name = dummy.value = "";
1174                 return(&dummy);
1175         }
1176         for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++)
1177                 ;
1178         if (*cp == 0)
1179                 *cp = '=';
1180         *++cp = 0;
1181         setarea((char *)vp, 0);
1182         setarea((char *)vp->name, 0);
1183         vp->value = null;
1184         vp->next = vlist;
1185         vp->status = GETCELL;
1186         vlist = vp;
1187         return(vp);
1188 }
1189
1190 /*
1191  * give variable at `vp' the value `val'.
1192  */
1193 static void
1194 setval(vp, val)
1195 struct var *vp;
1196 char *val;
1197 {
1198         nameval(vp, val, (char *)NULL);
1199 }
1200
1201 /*
1202  * if name is not NULL, it must be
1203  * a prefix of the space `val',
1204  * and end with `='.
1205  * this is all so that exporting
1206  * values is reasonably painless.
1207  */
1208 static void
1209 nameval(vp, val, name)
1210 register struct var *vp;
1211 char *val, *name;
1212 {
1213         register char *cp, *xp;
1214         char *nv;
1215         int fl;
1216
1217         if (vp->status & RONLY) {
1218                 for (xp = vp->name; *xp && *xp != '=';)
1219                         putc(*xp++, stderr);
1220                 err(" is read-only");
1221                 return;
1222         }
1223         fl = 0;
1224         if (name == NULL) {
1225                 xp = space(strlen(vp->name)+strlen(val)+2);
1226                 if (xp == 0)
1227                         return;
1228                 /* make string:  name=value */
1229                 setarea((char *)xp, 0);
1230                 name = xp;
1231                 for (cp = vp->name; (*xp = *cp++) && *xp!='='; xp++)
1232                         ;
1233                 if (*xp++ == 0)
1234                         xp[-1] = '=';
1235                 nv = xp;
1236                 for (cp = val; (*xp++ = *cp++) != '\0';)
1237                         ;
1238                 val = nv;
1239                 fl = GETCELL;
1240         }
1241         if (vp->status & GETCELL)
1242                 freecell(vp->name);    /* form new string `name=value' */
1243         vp->name = name;
1244         vp->value = val;
1245         vp->status |= fl;
1246 }
1247
1248 static void
1249 export(vp)
1250 struct var *vp;
1251 {
1252         vp->status |= EXPORT;
1253 }
1254
1255 static void
1256 ronly(vp)
1257 struct var *vp;
1258 {
1259         if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */
1260                 vp->status |= RONLY;
1261 }
1262
1263 static int
1264 isassign(s)
1265 register char *s;
1266 {
1267         if (!isalpha((int)*s) && *s != '_')
1268                 return(0);
1269         for (; *s != '='; s++)
1270                 if (*s == 0 || (!isalnum(*s) && *s != '_'))
1271                         return(0);
1272         return(1);
1273 }
1274
1275 static int
1276 assign(s, cf)
1277 register char *s;
1278 int cf;
1279 {
1280         register char *cp;
1281         struct var *vp;
1282
1283         if (!isalpha(*s) && *s != '_')
1284                 return(0);
1285         for (cp = s; *cp != '='; cp++)
1286                 if (*cp == 0 || (!isalnum(*cp) && *cp != '_'))
1287                         return(0);
1288         vp = lookup(s);
1289         nameval(vp, ++cp, cf == COPYV? (char *)NULL: s);
1290         if (cf != COPYV)
1291                 vp->status &= ~GETCELL;
1292         return(1);
1293 }
1294
1295 static int
1296 checkname(cp)
1297 register char *cp;
1298 {
1299         if (!isalpha(*cp++) && *(cp-1) != '_')
1300                 return(0);
1301         while (*cp)
1302                 if (!isalnum(*cp++) && *(cp-1) != '_')
1303                         return(0);
1304         return(1);
1305 }
1306
1307 static void
1308 putvlist(f, out)
1309 register int f, out;
1310 {
1311         register struct var *vp;
1312
1313         for (vp = vlist; vp; vp = vp->next)
1314                 if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) {
1315                         if (vp->status & EXPORT)
1316                                 write(out, "export ", 7);
1317                         if (vp->status & RONLY)
1318                                 write(out, "readonly ", 9);
1319                         write(out, vp->name, (int)(findeq(vp->name) - vp->name));
1320                         write(out, "\n", 1);
1321                 }
1322 }
1323
1324 static int
1325 eqname(n1, n2)
1326 register char *n1, *n2;
1327 {
1328         for (; *n1 != '=' && *n1 != 0; n1++)
1329                 if (*n2++ != *n1)
1330                         return(0);
1331         return(*n2 == 0 || *n2 == '=');
1332 }
1333
1334 static char *
1335 findeq(cp)
1336 register char *cp;
1337 {
1338         while (*cp != '\0' && *cp != '=')
1339                 cp++;
1340         return(cp);
1341 }
1342
1343 /* -------- gmatch.c -------- */
1344 /*
1345  * int gmatch(string, pattern)
1346  * char *string, *pattern;
1347  *
1348  * Match a pattern as in sh(1).
1349  */
1350
1351 #define CMASK   0377
1352 #define QUOTE   0200
1353 #define QMASK   (CMASK&~QUOTE)
1354 #define NOT     '!'     /* might use ^ */
1355
1356 static int
1357 gmatch(s, p)
1358 register char *s, *p;
1359 {
1360         register int sc, pc;
1361
1362         if (s == NULL || p == NULL)
1363                 return(0);
1364         while ((pc = *p++ & CMASK) != '\0') {
1365                 sc = *s++ & QMASK;
1366                 switch (pc) {
1367                 case '[':
1368                         if ((p = cclass(p, sc)) == NULL)
1369                                 return(0);
1370                         break;
1371
1372                 case '?':
1373                         if (sc == 0)
1374                                 return(0);
1375                         break;
1376
1377                 case '*':
1378                         s--;
1379                         do {
1380                                 if (*p == '\0' || gmatch(s, p))
1381                                         return(1);
1382                         } while (*s++ != '\0');
1383                         return(0);
1384
1385                 default:
1386                         if (sc != (pc&~QUOTE))
1387                                 return(0);
1388                 }
1389         }
1390         return(*s == 0);
1391 }
1392
1393 static char *
1394 cclass(p, sub)
1395 register char *p;
1396 register int sub;
1397 {
1398         register int c, d, not, found;
1399
1400         if ((not = *p == NOT) != 0)
1401                 p++;
1402         found = not;
1403         do {
1404                 if (*p == '\0')
1405                         return((char *)NULL);
1406                 c = *p & CMASK;
1407                 if (p[1] == '-' && p[2] != ']') {
1408                         d = p[2] & CMASK;
1409                         p++;
1410                 } else
1411                         d = c;
1412                 if (c == sub || (c <= sub && sub <= d))
1413                         found = !not;
1414         } while (*++p != ']');
1415         return(found? p+1: (char *)NULL);
1416 }
1417
1418
1419 /* -------- area.c -------- */
1420
1421 /*
1422  * All memory between (char *)areabot and (char *)(areatop+1) is
1423  * exclusively administered by the area management routines.
1424  * It is assumed that sbrk() and brk() manipulate the high end.
1425  */
1426
1427 #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;})
1428
1429 static void
1430 initarea()
1431 {
1432         brkaddr = malloc(65000);
1433         brktop = brkaddr + 65000;
1434
1435         while ((int)sbrk(0) & ALIGN)
1436                 sbrk(1);
1437         areabot = (struct region *)sbrk(REGSIZE);
1438
1439         areabot->next = areabot;
1440         areabot->area = BUSY;
1441         areatop = areabot;
1442         areanxt = areabot;
1443 }
1444
1445 char *
1446 getcell(nbytes)
1447 unsigned nbytes;
1448 {
1449         register int nregio;
1450         register struct region *p, *q;
1451         register int i;
1452
1453         if (nbytes == 0) {
1454                 puts("getcell(0)");
1455                 abort();
1456         }       /* silly and defeats the algorithm */
1457         /*
1458          * round upwards and add administration area
1459          */
1460         nregio = (nbytes+(REGSIZE-1))/REGSIZE + 1;
1461         for (p = areanxt;;) {
1462                 if (p->area > areanum) {
1463                         /*
1464                          * merge free cells
1465                          */
1466                         while ((q = p->next)->area > areanum && q != areanxt)
1467                                 p->next = q->next;
1468                         /*
1469                          * exit loop if cell big enough
1470                          */
1471                         if (q >= p + nregio)
1472                                 goto found;
1473                 }
1474                 p = p->next;
1475                 if (p == areanxt)
1476                         break;
1477         }
1478         i = nregio >= GROWBY ? nregio : GROWBY;
1479         p = (struct region *)sbrk(i * REGSIZE);
1480         if (p == (struct region *)-1)
1481                 return((char *)NULL);
1482         p--;
1483         if (p != areatop) {
1484                 puts("not contig");
1485                 abort();        /* allocated areas are contiguous */
1486         }
1487         q = p + i;
1488         p->next = q;
1489         p->area = FREE;
1490         q->next = areabot;
1491         q->area = BUSY;
1492         areatop = q;
1493 found:
1494         /*
1495          * we found a FREE area big enough, pointed to by 'p', and up to 'q'
1496          */
1497         areanxt = p + nregio;
1498         if (areanxt < q) {
1499                 /*
1500                  * split into requested area and rest
1501                  */
1502                 if (areanxt+1 > q) {
1503                         puts("OOM");
1504                         abort();        /* insufficient space left for admin */
1505                 }
1506                 areanxt->next = q;
1507                 areanxt->area = FREE;
1508                 p->next = areanxt;
1509         }
1510         p->area = areanum;
1511         return((char *)(p+1));
1512 }
1513
1514 static void
1515 freecell(cp)
1516 char *cp;
1517 {
1518         register struct region *p;
1519
1520         if ((p = (struct region *)cp) != NULL) {
1521                 p--;
1522                 if (p < areanxt)
1523                         areanxt = p;
1524                 p->area = FREE;
1525         }
1526 }
1527
1528 static void
1529 freearea(a)
1530 register int a;
1531 {
1532         register struct region *p, *top;
1533
1534         top = areatop;
1535         for (p = areabot; p != top; p = p->next)
1536                 if (p->area >= a)
1537                         p->area = FREE;
1538 }
1539
1540 static void
1541 setarea(cp,a)
1542 char *cp;
1543 int a;
1544 {
1545         register struct region *p;
1546
1547         if ((p = (struct region *)cp) != NULL)
1548                 (p-1)->area = a;
1549 }
1550
1551 int
1552 getarea(cp)
1553 char *cp;
1554 {
1555         return ((struct region*)cp-1)->area;
1556 }
1557
1558 static void
1559 garbage()
1560 {
1561         register struct region *p, *q, *top;
1562
1563         top = areatop;
1564         for (p = areabot; p != top; p = p->next) {
1565                 if (p->area > areanum) {
1566                         while ((q = p->next)->area > areanum)
1567                                 p->next = q->next;
1568                         areanxt = p;
1569                 }
1570         }
1571 #ifdef SHRINKBY
1572         if (areatop >= q + SHRINKBY && q->area > areanum) {
1573                 brk((char *)(q+1));
1574                 q->next = areabot;
1575                 q->area = BUSY;
1576                 areatop = q;
1577         }
1578 #endif
1579 }
1580
1581 /* -------- csyn.c -------- */
1582 /*
1583  * shell: syntax (C version)
1584  */
1585
1586
1587 int
1588 yyparse()
1589 {
1590         startl  = 1;
1591         peeksym = 0;
1592         yynerrs = 0;
1593         outtree = c_list();
1594         musthave('\n', 0);
1595         return(yynerrs!=0);
1596 }
1597
1598 static struct op *
1599 pipeline(cf)
1600 int cf;
1601 {
1602         register struct op *t, *p;
1603         register int c;
1604
1605         t = command(cf);
1606         if (t != NULL) {
1607                 while ((c = yylex(0)) == '|') {
1608                         if ((p = command(CONTIN)) == NULL)
1609                                 SYNTAXERR;
1610                         if (t->type != TPAREN && t->type != TCOM) {
1611                                 /* shell statement */
1612                                 t = block(TPAREN, t, NOBLOCK, NOWORDS);
1613                         }
1614                         t = block(TPIPE, t, p, NOWORDS);
1615                 }
1616                 peeksym = c;
1617         }
1618         return(t);
1619 }
1620
1621 static struct op *
1622 andor()
1623 {
1624         register struct op *t, *p;
1625         register int c;
1626
1627         t = pipeline(0);
1628         if (t != NULL) {
1629                 while ((c = yylex(0)) == LOGAND || c == LOGOR) {
1630                         if ((p = pipeline(CONTIN)) == NULL)
1631                                 SYNTAXERR;
1632                         t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS);
1633                 }
1634                 peeksym = c;
1635         }
1636         return(t);
1637 }
1638
1639 static struct op *
1640 c_list()
1641 {
1642         register struct op *t, *p;
1643         register int c;
1644
1645         t = andor();
1646         if (t != NULL) {
1647                 if((peeksym = yylex(0)) == '&')
1648                         t = block(TASYNC, t, NOBLOCK, NOWORDS);
1649                 while ((c = yylex(0)) == ';' || c == '&' || (multiline && c == '\n')) {
1650                         if ((p = andor()) == NULL)
1651                                 return(t);
1652                         if((peeksym = yylex(0)) == '&')
1653                                 p = block(TASYNC, p, NOBLOCK, NOWORDS);
1654                         t = list(t, p);
1655                 }
1656                 peeksym = c;
1657         }
1658         return(t);
1659 }
1660
1661
1662 static int
1663 synio(cf)
1664 int cf;
1665 {
1666         register struct ioword *iop;
1667         register int i;
1668         register int c;
1669
1670         if ((c = yylex(cf)) != '<' && c != '>') {
1671                 peeksym = c;
1672                 return(0);
1673         }
1674         i = yylval.i;
1675         musthave(WORD, 0);
1676         iop = io(iounit, i, yylval.cp);
1677         iounit = IODEFAULT;
1678         if (i & IOHERE)
1679                 markhere(yylval.cp, iop);
1680         return(1);
1681 }
1682
1683 static void
1684 musthave(c, cf)
1685 int c, cf;
1686 {
1687         if ((peeksym = yylex(cf)) != c)
1688                 SYNTAXERR;
1689         peeksym = 0;
1690 }
1691
1692 static struct op *
1693 simple()
1694 {
1695         register struct op *t;
1696
1697         t = NULL;
1698         for (;;) {
1699                 switch (peeksym = yylex(0)) {
1700                 case '<':
1701                 case '>':
1702                         (void) synio(0);
1703                         break;
1704
1705                 case WORD:
1706                         if (t == NULL) {
1707                                 t = newtp();
1708                                 t->type = TCOM;
1709                         }
1710                         peeksym = 0;
1711                         word(yylval.cp);
1712                         break;
1713
1714                 default:
1715                         return(t);
1716                 }
1717         }
1718 }
1719
1720 static struct op *
1721 nested(type, mark)
1722 int type, mark;
1723 {
1724         register struct op *t;
1725
1726         multiline++;
1727         t = c_list();
1728         musthave(mark, 0);
1729         multiline--;
1730         return(block(type, t, NOBLOCK, NOWORDS));
1731 }
1732
1733 static struct op *
1734 command(cf)
1735 int cf;
1736 {
1737         register struct op *t;
1738         struct wdblock *iosave;
1739         register int c;
1740
1741         iosave = iolist;
1742         iolist = NULL;
1743         if (multiline)
1744                 cf |= CONTIN;
1745         while (synio(cf))
1746                 cf = 0;
1747         switch (c = yylex(cf)) {
1748         default:
1749                 peeksym = c;
1750                 if ((t = simple()) == NULL) {
1751                         if (iolist == NULL)
1752                                 return((struct op *)NULL);
1753                         t = newtp();
1754                         t->type = TCOM;
1755                 }
1756                 break;
1757
1758         case '(':
1759                 t = nested(TPAREN, ')');
1760                 break;
1761
1762         case '{':
1763                 t = nested(TBRACE, '}');
1764                 break;
1765
1766         case FOR:
1767                 t = newtp();
1768                 t->type = TFOR;
1769                 musthave(WORD, 0);
1770                 startl = 1;
1771                 t->str = yylval.cp;
1772                 multiline++;
1773                 t->words = wordlist();
1774                 if ((c = yylex(0)) != '\n' && c != ';')
1775                         peeksym = c;
1776                 t->left = dogroup(0);
1777                 multiline--;
1778                 break;
1779
1780         case WHILE:
1781         case UNTIL:
1782                 multiline++;
1783                 t = newtp();
1784                 t->type = c == WHILE? TWHILE: TUNTIL;
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++;
1800                 t->left = caselist();
1801                 musthave(ESAC, 0);
1802                 multiline--;
1803                 break;
1804
1805         case IF:
1806                 multiline++;
1807                 t = newtp();
1808                 t->type = TIF;
1809                 t->left = c_list();
1810                 t->right = thenpart();
1811                 musthave(FI, 0);
1812                 multiline--;
1813                 break;
1814         }
1815         while (synio(0))
1816                 ;
1817         t = namelist(t);
1818         iolist = iosave;
1819         return(t);
1820 }
1821
1822 static struct op *
1823 dogroup(onlydone)
1824 int onlydone;
1825 {
1826         register int c;
1827         register struct op *mylist;
1828
1829         c = yylex(CONTIN);
1830         if (c == DONE && onlydone)
1831                 return((struct op *)NULL);
1832         if (c != DO)
1833                 SYNTAXERR;
1834         mylist = c_list();
1835         musthave(DONE, 0);
1836         return(mylist);
1837 }
1838
1839 static struct op *
1840 thenpart()
1841 {
1842         register int c;
1843         register struct op *t;
1844
1845         if ((c = yylex(0)) != THEN) {
1846                 peeksym = c;
1847                 return((struct op *)NULL);
1848         }
1849         t = newtp();
1850         t->type = 0;
1851         t->left = c_list();
1852         if (t->left == NULL)
1853                 SYNTAXERR;
1854         t->right = elsepart();
1855         return(t);
1856 }
1857
1858 static struct op *
1859 elsepart()
1860 {
1861         register int c;
1862         register struct op *t;
1863
1864         switch (c = yylex(0)) {
1865         case ELSE:
1866                 if ((t = c_list()) == NULL)
1867                         SYNTAXERR;
1868                 return(t);
1869
1870         case ELIF:
1871                 t = newtp();
1872                 t->type = TELIF;
1873                 t->left = c_list();
1874                 t->right = thenpart();
1875                 return(t);
1876
1877         default:
1878                 peeksym = c;
1879                 return((struct op *)NULL);
1880         }
1881 }
1882
1883 static struct op *
1884 caselist()
1885 {
1886         register struct op *t;
1887
1888         t = NULL;
1889         while ((peeksym = yylex(CONTIN)) != ESAC)
1890                 t = list(t, casepart());
1891         return(t);
1892 }
1893
1894 static struct op *
1895 casepart()
1896 {
1897         register struct op *t;
1898
1899         t = newtp();
1900         t->type = TPAT;
1901         t->words = pattern();
1902         musthave(')', 0);
1903         t->left = c_list();
1904         if ((peeksym = yylex(CONTIN)) != ESAC)
1905                 musthave(BREAK, CONTIN);
1906         return(t);
1907 }
1908
1909 static char **
1910 pattern()
1911 {
1912         register int c, cf;
1913
1914         cf = CONTIN;
1915         do {
1916                 musthave(WORD, cf);
1917                 word(yylval.cp);
1918                 cf = 0;
1919         } while ((c = yylex(0)) == '|');
1920         peeksym = c;
1921         word(NOWORD);
1922         return(copyw());
1923 }
1924
1925 static char **
1926 wordlist()
1927 {
1928         register int c;
1929
1930         if ((c = yylex(0)) != IN) {
1931                 peeksym = c;
1932                 return((char **)NULL);
1933         }
1934         startl = 0;
1935         while ((c = yylex(0)) == WORD)
1936                 word(yylval.cp);
1937         word(NOWORD);
1938         peeksym = c;
1939         return(copyw());
1940 }
1941
1942 /*
1943  * supporting functions
1944  */
1945 static struct op *
1946 list(t1, t2)
1947 register struct op *t1, *t2;
1948 {
1949         if (t1 == NULL)
1950                 return(t2);
1951         if (t2 == NULL)
1952                 return(t1);
1953         return(block(TLIST, t1, t2, NOWORDS));
1954 }
1955
1956 static struct op *
1957 block(type, t1, t2, wp)
1958 int type;
1959 struct op *t1, *t2;
1960 char **wp;
1961 {
1962         register struct op *t;
1963
1964         t = newtp();
1965         t->type = type;
1966         t->left = t1;
1967         t->right = t2;
1968         t->words = wp;
1969         return(t);
1970 }
1971
1972 static int
1973 rlookup(n)
1974 register char *n;
1975 {
1976         register struct res *rp;
1977
1978         for (rp = restab; rp->r_name; rp++)
1979                 if (strcmp(rp->r_name, n) == 0)
1980                         return(rp->r_val);
1981         return(0);
1982 }
1983
1984 static struct op *
1985 newtp()
1986 {
1987         register struct op *t;
1988
1989         t = (struct op *)tree(sizeof(*t));
1990         t->type = 0;
1991         t->words = NULL;
1992         t->ioact = NULL;
1993         t->left = NULL;
1994         t->right = NULL;
1995         t->str = NULL;
1996         return(t);
1997 }
1998
1999 static struct op *
2000 namelist(t)
2001 register struct op *t;
2002 {
2003         if (iolist) {
2004                 iolist = addword((char *)NULL, iolist);
2005                 t->ioact = copyio();
2006         } else
2007                 t->ioact = NULL;
2008         if (t->type != TCOM) {
2009                 if (t->type != TPAREN && t->ioact != NULL) {
2010                         t = block(TPAREN, t, NOBLOCK, NOWORDS);
2011                         t->ioact = t->left->ioact;
2012                         t->left->ioact = NULL;
2013                 }
2014                 return(t);
2015         }
2016         word(NOWORD);
2017         t->words = copyw();
2018         return(t);
2019 }
2020
2021 static char **
2022 copyw()
2023 {
2024         register char **wd;
2025
2026         wd = getwords(wdlist);
2027         wdlist = 0;
2028         return(wd);
2029 }
2030
2031 static void
2032 word(cp)
2033 char *cp;
2034 {
2035         wdlist = addword(cp, wdlist);
2036 }
2037
2038 static struct ioword **
2039 copyio()
2040 {
2041         register struct ioword **iop;
2042
2043         iop = (struct ioword **) getwords(iolist);
2044         iolist = 0;
2045         return(iop);
2046 }
2047
2048 static struct ioword *
2049 io(u, f, cp)
2050 int u;
2051 int f;
2052 char *cp;
2053 {
2054         register struct ioword *iop;
2055
2056         iop = (struct ioword *) tree(sizeof(*iop));
2057         iop->io_unit = u;
2058         iop->io_flag = f;
2059         iop->io_name = cp;
2060         iolist = addword((char *)iop, iolist);
2061         return(iop);
2062 }
2063
2064 static void
2065 zzerr()
2066 {
2067         yyerror("syntax error");
2068 }
2069
2070 static void
2071 yyerror(s)
2072 char *s;
2073 {
2074         yynerrs++;
2075         if (interactive && e.iop <= iostack) {
2076                 multiline = 0;
2077                 while (eofc() == 0 && yylex(0) != '\n')
2078                         ;
2079         }
2080         err(s);
2081         fail();
2082 }
2083
2084 static int
2085 yylex(cf)
2086 int cf;
2087 {
2088         register int c, c1;
2089         int atstart;
2090
2091         if ((c = peeksym) > 0) {
2092                 peeksym = 0;
2093                 if (c == '\n')
2094                         startl = 1;
2095                 return(c);
2096         }
2097         nlseen = 0;
2098         e.linep = line;
2099         atstart = startl;
2100         startl = 0;
2101         yylval.i = 0;
2102
2103 loop:
2104         while ((c = my_getc(0)) == ' ' || c == '\t')
2105                 ;
2106         switch (c) {
2107         default:
2108                 if (any(c, "0123456789")) {
2109                         unget(c1 = my_getc(0));
2110                         if (c1 == '<' || c1 == '>') {
2111                                 iounit = c - '0';
2112                                 goto loop;
2113                         }
2114                         *e.linep++ = c;
2115                         c = c1;
2116                 }
2117                 break;
2118
2119         case '#':
2120                 while ((c = my_getc(0)) != 0 && c != '\n')
2121                         ;
2122                 unget(c);
2123                 goto loop;
2124
2125         case 0:
2126                 return(c);
2127
2128         case '$':
2129                 *e.linep++ = c;
2130                 if ((c = my_getc(0)) == '{') {
2131                         if ((c = collect(c, '}')) != '\0')
2132                                 return(c);
2133                         goto pack;
2134                 }
2135                 break;
2136
2137         case '`':
2138         case '\'':
2139         case '"':
2140                 if ((c = collect(c, c)) != '\0')
2141                         return(c);
2142                 goto pack;
2143
2144         case '|':
2145         case '&':
2146         case ';':
2147                 if ((c1 = dual(c)) != '\0') {
2148                         startl = 1;
2149                         return(c1);
2150                 }
2151                 startl = 1;
2152                 return(c);
2153         case '^':
2154                 startl = 1;
2155                 return('|');
2156         case '>':
2157         case '<':
2158                 diag(c);
2159                 return(c);
2160
2161         case '\n':
2162                 nlseen++;
2163                 gethere();
2164                 startl = 1;
2165                 if (multiline || cf & CONTIN) {
2166                         if (interactive && e.iop <= iostack) {
2167 #ifdef CONFIG_FEATURE_COMMAND_EDITING
2168                         current_prompt=cprompt->value;
2169 #else
2170                         prs(cprompt->value);
2171 #endif
2172                         }
2173                         if (cf & CONTIN)
2174                                 goto loop;
2175                 }
2176                 return(c);
2177
2178         case '(':
2179         case ')':
2180                 startl = 1;
2181                 return(c);
2182         }
2183
2184         unget(c);
2185
2186 pack:
2187         while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n"))
2188                 if (e.linep >= elinep)
2189                         err("word too long");
2190                 else
2191                         *e.linep++ = c;
2192         unget(c);
2193         if(any(c, "\"'`$"))
2194                 goto loop;
2195         *e.linep++ = '\0';
2196         if (atstart && (c = rlookup(line))!=0) {
2197                 startl = 1;
2198                 return(c);
2199         }
2200         yylval.cp = strsave(line, areanum);
2201         return(WORD);
2202 }
2203
2204 static int
2205 collect(c, c1)
2206 register int c, c1;
2207 {
2208         char s[2];
2209
2210         *e.linep++ = c;
2211         while ((c = my_getc(c1)) != c1) {
2212                 if (c == 0) {
2213                         unget(c);
2214                         s[0] = c1;
2215                         s[1] = 0;
2216                         prs("no closing "); yyerror(s);
2217                         return(YYERRCODE);
2218                 }
2219                 if (interactive && c == '\n' && e.iop <= iostack) {
2220 #ifdef CONFIG_FEATURE_COMMAND_EDITING
2221                     current_prompt=cprompt->value;
2222 #else
2223                     prs(cprompt->value);
2224 #endif
2225                 }
2226                 *e.linep++ = c;
2227         }
2228         *e.linep++ = c;
2229         return(0);
2230 }
2231
2232 static int
2233 dual(c)
2234 register int c;
2235 {
2236         char s[3];
2237         register char *cp = s;
2238
2239         *cp++ = c;
2240         *cp++ = my_getc(0);
2241         *cp = 0;
2242         if ((c = rlookup(s)) == 0)
2243                 unget(*--cp);
2244         return(c);
2245 }
2246
2247 static void
2248 diag(ec)
2249 register int ec;
2250 {
2251         register int c;
2252
2253         c = my_getc(0);
2254         if (c == '>' || c == '<') {
2255                 if (c != ec)
2256                         zzerr();
2257                 yylval.i = ec == '>'? IOWRITE|IOCAT: IOHERE;
2258                 c = my_getc(0);
2259         } else
2260                 yylval.i = ec == '>'? IOWRITE: IOREAD;
2261         if (c != '&' || yylval.i == IOHERE)
2262                 unget(c);
2263         else
2264                 yylval.i |= IODUP;
2265 }
2266
2267 static char *
2268 tree(size)
2269 unsigned size;
2270 {
2271         register char *t;
2272
2273         if ((t = getcell(size)) == NULL) {
2274                 prs("command line too complicated\n");
2275                 fail();
2276                 /* NOTREACHED */
2277         }
2278         return(t);
2279 }
2280
2281 /* VARARGS1 */
2282 /* ARGSUSED */
2283
2284 /* -------- exec.c -------- */
2285
2286 /*
2287  * execute tree
2288  */
2289
2290
2291 static int
2292 execute(t, pin, pout, act)
2293 register struct op *t;
2294 int *pin, *pout;
2295 int act;
2296 {
2297         register struct op *t1;
2298         volatile int i, rv, a;
2299         char *cp, **wp, **wp2;
2300         struct var *vp;
2301         struct brkcon bc;
2302
2303 #if __GNUC__
2304         /* Avoid longjmp clobbering */
2305         (void) &wp;
2306 #endif
2307
2308
2309         if (t == NULL)
2310                 return(0);
2311         rv = 0;
2312         a = areanum++;
2313         wp = (wp2 = t->words) != NULL
2314              ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
2315              : NULL;
2316
2317         switch(t->type) {
2318         case TPAREN:
2319                 rv = execute(t->left, pin, pout, 0);
2320                 break;
2321
2322         case TCOM:
2323                 {
2324                         int child;
2325                         rv = forkexec(t, pin, pout, act, wp, &child);
2326                         if (child) {
2327                                 exstat = rv;
2328                                 leave();
2329                         }
2330                 }
2331                 break;
2332
2333         case TPIPE:
2334                 {
2335                     int pv[2];
2336                     if ((rv = openpipe(pv)) < 0)
2337                         break;
2338                     pv[0] = remap(pv[0]);
2339                     pv[1] = remap(pv[1]);
2340                     (void) execute(t->left, pin, pv, 0);
2341                     rv = execute(t->right, pv, pout, 0);
2342                 }
2343                 break;
2344
2345         case TLIST:
2346                 (void) execute(t->left, pin, pout, 0);
2347                 rv = execute(t->right, pin, pout, 0);
2348                 break;
2349
2350         case TASYNC:
2351         {
2352                 int hinteractive = interactive;
2353
2354                 i = vfork();
2355                 if (i != 0) {
2356                         interactive = hinteractive;
2357                         if (i != -1) {
2358                                 setval(lookup("!"), putn(i));
2359                                 if (pin != NULL)
2360                                         closepipe(pin);
2361                                 if (interactive) {
2362                                         prs(putn(i));
2363                                         prs("\n");
2364                                 }
2365                         } else
2366                                 rv = -1;
2367                         setstatus(rv);
2368                 } else {
2369                         signal(SIGINT, SIG_IGN);
2370                         signal(SIGQUIT, SIG_IGN);
2371                         if (interactive)
2372                                 signal(SIGTERM, SIG_DFL);
2373                         interactive = 0;
2374                         if (pin == NULL) {
2375                                 close(0);
2376                                 open("/dev/null", 0);
2377                         }
2378                         exit(execute(t->left, pin, pout, FEXEC));
2379                 }
2380         }
2381                 break;
2382
2383         case TOR:
2384         case TAND:
2385                 rv = execute(t->left, pin, pout, 0);
2386                 if ((t1 = t->right)!=NULL && (rv == 0) == (t->type == TAND))
2387                         rv = execute(t1, pin, pout, 0);
2388                 break;
2389
2390         case TFOR:
2391                 if (wp == NULL) {
2392                         wp = dolv+1;
2393                         if ((i = dolc) < 0)
2394                                 i = 0;
2395                 } else {
2396                         i = -1;
2397                         while (*wp++ != NULL)
2398                                 ;
2399                 }
2400                 vp = lookup(t->str);
2401                 while (setjmp(bc.brkpt))
2402                         if (isbreak)
2403                                 goto broken;
2404                 brkset(&bc);
2405                 for (t1 = t->left; i-- && *wp != NULL;) {
2406                         setval(vp, *wp++);
2407                         rv = execute(t1, pin, pout, 0);
2408                 }
2409                 brklist = brklist->nextlev;
2410                 break;
2411
2412         case TWHILE:
2413         case TUNTIL:
2414                 while (setjmp(bc.brkpt))
2415                         if (isbreak)
2416                                 goto broken;
2417                 brkset(&bc);
2418                 t1 = t->left;
2419                 while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE))
2420                         rv = execute(t->right, pin, pout, 0);
2421                 brklist = brklist->nextlev;
2422                 break;
2423
2424         case TIF:
2425         case TELIF:
2426                 if (t->right != NULL) {
2427                 rv = !execute(t->left, pin, pout, 0) ?
2428                         execute(t->right->left, pin, pout, 0):
2429                         execute(t->right->right, pin, pout, 0);
2430                 }
2431                 break;
2432
2433         case TCASE:
2434                 if ((cp = evalstr(t->str, DOSUB|DOTRIM)) == 0)
2435                         cp = "";
2436                 if ((t1 = findcase(t->left, cp)) != NULL)
2437                         rv = execute(t1, pin, pout, 0);
2438                 break;
2439
2440         case TBRACE:
2441 /*
2442                 if (iopp = t->ioact)
2443                         while (*iopp)
2444                                 if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
2445                                         rv = -1;
2446                                         break;
2447                                 }
2448 */
2449                 if (rv >= 0 && (t1 = t->left))
2450                         rv = execute(t1, pin, pout, 0);
2451                 break;
2452         }
2453
2454 broken:
2455         t->words = wp2;
2456         isbreak = 0;
2457         freehere(areanum);
2458         freearea(areanum);
2459         areanum = a;
2460         if (interactive && intr) {
2461                 closeall();
2462                 fail();
2463         }
2464         if ((i = trapset) != 0) {
2465                 trapset = 0;
2466                 runtrap(i);
2467         }
2468         return(rv);
2469 }
2470
2471 static int
2472 forkexec( register struct op *t, int *pin, int *pout, int act, char **wp, int *pforked)
2473 {
2474         int i, rv;
2475         int (*shcom)(struct op *) = NULL;
2476         register int f;
2477         char *cp = NULL;
2478         struct ioword **iopp;
2479         int resetsig;
2480         char **owp;
2481
2482         int *hpin = pin;
2483         int *hpout = pout;
2484         int hforked;
2485         char *hwp;
2486         int hinteractive;
2487         int hintr;
2488         struct brkcon * hbrklist;
2489         int hexecflg;
2490
2491 #if __GNUC__
2492         /* Avoid longjmp clobbering */
2493         (void) &pin;
2494         (void) &pout;
2495         (void) &wp;
2496         (void) &shcom;
2497         (void) &cp;
2498         (void) &resetsig;
2499         (void) &owp;
2500 #endif
2501
2502         owp = wp;
2503         resetsig = 0;
2504         *pforked = 0;
2505         rv = -1;        /* system-detected error */
2506         if (t->type == TCOM) {
2507                 while ((cp = *wp++) != NULL)
2508                         ;
2509                 cp = *wp;
2510
2511                 /* strip all initial assignments */
2512                 /* not correct wrt PATH=yyy command  etc */
2513                 if (flag['x'])
2514                         echo (cp ? wp: owp);
2515                 if (cp == NULL && t->ioact == NULL) {
2516                         while ((cp = *owp++) != NULL && assign(cp, COPYV))
2517                                 ;
2518                         return(setstatus(0));
2519                 }
2520                 else if (cp != NULL)
2521                         shcom = inbuilt(cp);
2522         }
2523         t->words = wp;
2524         f = act;
2525         if (shcom == NULL && (f & FEXEC) == 0) {
2526
2527                 hpin = pin;
2528                 hpout = pout;
2529                 hforked = *pforked;
2530                 hwp = *wp;
2531                 hinteractive = interactive;
2532                 hintr = intr;
2533                 hbrklist = brklist;
2534                 hexecflg = execflg;
2535
2536                 i = vfork();
2537                 if (i != 0) {
2538                         /* who wrote this crappy non vfork safe shit? */
2539                         pin = hpin;
2540                         pout = hpout;
2541                         *pforked = hforked;
2542                         *wp = hwp;
2543                         interactive = hinteractive;
2544                         intr = hintr;
2545                         brklist = hbrklist;
2546                         execflg = hexecflg;
2547
2548                         *pforked = 0;
2549                         if (i == -1)
2550                                 return(rv);
2551                         if (pin != NULL)
2552                                 closepipe(pin);
2553                         return(pout==NULL? setstatus(waitfor(i,0)): 0);
2554                 }
2555
2556                 if (interactive) {
2557                         signal(SIGINT, SIG_IGN);
2558                         signal(SIGQUIT, SIG_IGN);
2559                         resetsig = 1;
2560                 }
2561                 interactive = 0;
2562                 intr = 0;
2563                 (*pforked)++;
2564                 brklist = 0;
2565                 execflg = 0;
2566         }
2567         if (owp != NULL)
2568                 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2569                         if (shcom == NULL)
2570                                 export(lookup(cp));
2571 #ifdef COMPIPE
2572         if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) {
2573                 err("piping to/from shell builtins not yet done");
2574                 return(-1);
2575         }
2576 #endif
2577         if (pin != NULL) {
2578                 dup2(pin[0], 0);
2579                 closepipe(pin);
2580         }
2581         if (pout != NULL) {
2582                 dup2(pout[1], 1);
2583                 closepipe(pout);
2584         }
2585         if ((iopp = t->ioact) != NULL) {
2586                 if (shcom != NULL && shcom != doexec) {
2587                         prs(cp);
2588                         err(": cannot redirect shell command");
2589                         return(-1);
2590                 }
2591                 while (*iopp)
2592                         if (iosetup(*iopp++, pin!=NULL, pout!=NULL))
2593                                 return(rv);
2594         }
2595         if (shcom)
2596                 return(setstatus((*shcom)(t)));
2597         /* should use FIOCEXCL */
2598         for (i=FDBASE; i<NOFILE; i++)
2599                 close(i);
2600         if (resetsig) {
2601                 signal(SIGINT, SIG_DFL);
2602                 signal(SIGQUIT, SIG_DFL);
2603         }
2604         if (t->type == TPAREN)
2605                 exit(execute(t->left, NOPIPE, NOPIPE, FEXEC));
2606         if (wp[0] == NULL)
2607                 exit(0);
2608
2609         cp = rexecve(wp[0], wp, makenv());
2610         prs(wp[0]); prs(": "); warn(cp);
2611         if (!execflg)
2612                 trap[0] = NULL;
2613         leave();
2614         /* NOTREACHED */
2615         exit(1);
2616 }
2617
2618 /*
2619  * 0< 1> are ignored as required
2620  * within pipelines.
2621  */
2622 static int
2623 iosetup(iop, pipein, pipeout)
2624 register struct ioword *iop;
2625 int pipein, pipeout;
2626 {
2627         register int u = -1;
2628         char *cp=NULL, *msg;
2629
2630         if (iop->io_unit == IODEFAULT)  /* take default */
2631                 iop->io_unit = iop->io_flag&(IOREAD|IOHERE)? 0: 1;
2632         if (pipein && iop->io_unit == 0)
2633                 return(0);
2634         if (pipeout && iop->io_unit == 1)
2635                 return(0);
2636         msg = iop->io_flag&(IOREAD|IOHERE)? "open": "create";
2637         if ((iop->io_flag & IOHERE) == 0) {
2638                 cp = iop->io_name;
2639                 if ((cp = evalstr(cp, DOSUB|DOTRIM)) == NULL)
2640                         return(1);
2641         }
2642         if (iop->io_flag & IODUP) {
2643                 if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
2644                         prs(cp);
2645                         err(": illegal >& argument");
2646                         return(1);
2647                 }
2648                 if (*cp == '-')
2649                         iop->io_flag = IOCLOSE;
2650                 iop->io_flag &= ~(IOREAD|IOWRITE);
2651         }
2652         switch (iop->io_flag) {
2653         case IOREAD:
2654                 u = open(cp, 0);
2655                 break;
2656
2657         case IOHERE:
2658         case IOHERE|IOXHERE:
2659                 u = herein(iop->io_name, iop->io_flag&IOXHERE);
2660                 cp = "here file";
2661                 break;
2662
2663         case IOWRITE|IOCAT:
2664                 if ((u = open(cp, 1)) >= 0) {
2665                         lseek(u, (long)0, 2);
2666                         break;
2667                 }
2668         case IOWRITE:
2669                 u = creat(cp, 0666);
2670                 break;
2671
2672         case IODUP:
2673                 u = dup2(*cp-'0', iop->io_unit);
2674                 break;
2675
2676         case IOCLOSE:
2677                 close(iop->io_unit);
2678                 return(0);
2679         }
2680         if (u < 0) {
2681                 prs(cp);
2682                 prs(": cannot ");
2683                 warn(msg);
2684                 return(1);
2685         } else {
2686                 if (u != iop->io_unit) {
2687                         dup2(u, iop->io_unit);
2688                         close(u);
2689                 }
2690         }
2691         return(0);
2692 }
2693
2694 static void
2695 echo(wp)
2696 register char **wp;
2697 {
2698         register int i;
2699
2700         prs("+");
2701         for (i=0; wp[i]; i++) {
2702                 if (i)
2703                         prs(" ");
2704                 prs(wp[i]);
2705         }
2706         prs("\n");
2707 }
2708
2709 static struct op **
2710 find1case(t, w)
2711 struct op *t;
2712 char *w;
2713 {
2714         register struct op *t1;
2715         struct op **tp;
2716         register char **wp, *cp;
2717
2718         if (t == NULL)
2719                 return((struct op **)NULL);
2720         if (t->type == TLIST) {
2721                 if ((tp = find1case(t->left, w)) != NULL)
2722                         return(tp);
2723                 t1 = t->right;  /* TPAT */
2724         } else
2725                 t1 = t;
2726         for (wp = t1->words; *wp;)
2727                 if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp))
2728                         return(&t1->left);
2729         return((struct op **)NULL);
2730 }
2731
2732 static struct op *
2733 findcase(t, w)
2734 struct op *t;
2735 char *w;
2736 {
2737         register struct op **tp;
2738
2739         return((tp = find1case(t, w)) != NULL? *tp: (struct op *)NULL);
2740 }
2741
2742 /*
2743  * Enter a new loop level (marked for break/continue).
2744  */
2745 static void
2746 brkset(bc)
2747 struct brkcon *bc;
2748 {
2749         bc->nextlev = brklist;
2750         brklist = bc;
2751 }
2752
2753 /*
2754  * Wait for the last process created.
2755  * Print a message for each process found
2756  * that was killed by a signal.
2757  * Ignore interrupt signals while waiting
2758  * unless `canintr' is true.
2759  */
2760 static int
2761 waitfor(lastpid, canintr)
2762 register int lastpid;
2763 int canintr;
2764 {
2765         register int pid, rv;
2766         int s;
2767         int oheedint = heedint;
2768
2769         heedint = 0;
2770         rv = 0;
2771         do {
2772                 pid = wait(&s);
2773                 if (pid == -1) {
2774                         if (errno != EINTR || canintr)
2775                                 break;
2776                 } else {
2777                         if ((rv = WAITSIG(s)) != 0) {
2778                                 if (rv < NSIGNAL) {
2779                                         if (signame[rv] != NULL) {
2780                                                 if (pid != lastpid) {
2781                                                         prn(pid);
2782                                                         prs(": ");
2783                                                 }
2784                                                 prs(signame[rv]);
2785                                         }
2786                                 } else {
2787                                         if (pid != lastpid) {
2788                                                 prn(pid);
2789                                                 prs(": ");
2790                                         }
2791                                         prs("Signal "); prn(rv); prs(" ");
2792                                 }
2793                                 if (WAITCORE(s))
2794                                         prs(" - core dumped");
2795                                 if (rv >= NSIGNAL || signame[rv])
2796                                         prs("\n");
2797                                 rv = -1;
2798                         } else
2799                                 rv = WAITVAL(s);
2800                 }
2801         } while (pid != lastpid);
2802         heedint = oheedint;
2803         if (intr) {
2804                 if (interactive) {
2805                         if (canintr)
2806                                 intr = 0;
2807                 } else {
2808                         if (exstat == 0) exstat = rv;
2809                         onintr(0);
2810                 }
2811         }
2812         return(rv);
2813 }
2814
2815 static int
2816 setstatus(s)
2817 register int s;
2818 {
2819         exstat = s;
2820         setval(lookup("?"), putn(s));
2821         return(s);
2822 }
2823
2824 /*
2825  * PATH-searching interface to execve.
2826  * If getenv("PATH") were kept up-to-date,
2827  * execvp might be used.
2828  */
2829 static char *
2830 rexecve(c, v, envp)
2831 char *c, **v, **envp;
2832 {
2833         register int i;
2834         register char *sp, *tp;
2835         int eacces = 0, asis = 0;
2836
2837 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
2838         char *name = c;
2839         optind = 1;
2840         if (find_applet_by_name(name)) {
2841                 /* We have to exec here since we vforked.  Running
2842                  * run_applet_by_name() won't work and bad things
2843                  * will happen. */
2844                 execve("/proc/self/exe", v, envp);
2845                 execve("busybox", v, envp);
2846         }
2847 #endif
2848
2849         sp = any('/', c)? "": path->value;
2850         asis = *sp == '\0';
2851         while (asis || *sp != '\0') {
2852                 asis = 0;
2853                 tp = e.linep;
2854                 for (; *sp != '\0'; tp++)
2855                         if ((*tp = *sp++) == ':') {
2856                                 asis = *sp == '\0';
2857                                 break;
2858                         }
2859                 if (tp != e.linep)
2860                         *tp++ = '/';
2861                 for (i = 0; (*tp++ = c[i++]) != '\0';)
2862                         ;
2863
2864                 execve(e.linep, v, envp);
2865                 switch (errno) {
2866                 case ENOEXEC:
2867                         *v = e.linep;
2868                         tp = *--v;
2869                         *v = e.linep;
2870                         execve(DEFAULT_SHELL, v, envp);
2871                         *v = tp;
2872                         return("no Shell");
2873
2874                 case ENOMEM:
2875                         return((char*)bb_msg_memory_exhausted);
2876
2877                 case E2BIG:
2878                         return("argument list too long");
2879
2880                 case EACCES:
2881                         eacces++;
2882                         break;
2883                 }
2884         }
2885         return(errno==ENOENT ? "not found" : "cannot execute");
2886 }
2887
2888 /*
2889  * Run the command produced by generator `f'
2890  * applied to stream `arg'.
2891  */
2892 static int
2893 run(struct ioarg *argp, int (*f)(struct ioarg *))
2894 {
2895         struct op *otree;
2896         struct wdblock *swdlist;
2897         struct wdblock *siolist;
2898         jmp_buf ev, rt;
2899         xint *ofail;
2900         int rv;
2901
2902 #if __GNUC__
2903         /* Avoid longjmp clobbering */
2904         (void) &rv;
2905 #endif
2906
2907         areanum++;
2908         swdlist = wdlist;
2909         siolist = iolist;
2910         otree = outtree;
2911         ofail = failpt;
2912         rv = -1;
2913         if (newenv(setjmp(errpt = ev)) == 0) {
2914                 wdlist = 0;
2915                 iolist = 0;
2916                 pushio(argp, f);
2917                 e.iobase = e.iop;
2918                 yynerrs = 0;
2919                 if (setjmp(failpt = rt) == 0 && yyparse() == 0)
2920                         rv = execute(outtree, NOPIPE, NOPIPE, 0);
2921                 quitenv();
2922         }
2923         wdlist = swdlist;
2924         iolist = siolist;
2925         failpt = ofail;
2926         outtree = otree;
2927         freearea(areanum--);
2928         return(rv);
2929 }
2930
2931 /* -------- do.c -------- */
2932
2933 /*
2934  * built-in commands: doX
2935  */
2936
2937 static int dohelp(struct op *t )
2938 {
2939         int col;
2940         const struct builtincmd *x;
2941
2942         printf("\nBuilt-in commands:\n");
2943         printf("-------------------\n");
2944
2945         for (col=0, x = builtincmds; x->builtinfunc != NULL; x++) {
2946                 if (!x->name)
2947                         continue;
2948                 col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name);
2949                 if (col > 60) {
2950                         printf("\n");
2951                         col = 0;
2952                 }
2953         }
2954 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
2955         {
2956                 int i;
2957                 const struct BB_applet *applet;
2958                 extern const struct BB_applet applets[];
2959                 extern const size_t NUM_APPLETS;
2960
2961                 for (i=0, applet = applets; i < NUM_APPLETS; applet++, i++) {
2962                         if (!applet->name)
2963                                 continue;
2964
2965                         col += printf("%s%s", ((col == 0) ? "\t" : " "),
2966                                         applet->name);
2967                         if (col > 60) {
2968                                 printf("\n");
2969                                 col = 0;
2970                         }
2971                 }
2972         }
2973 #endif
2974         printf("\n\n");
2975         return EXIT_SUCCESS;
2976 }
2977
2978
2979
2980 static int dolabel(struct op *t )
2981 {
2982         return(0);
2983 }
2984
2985 static int
2986 dochdir(t)
2987 register struct op *t;
2988 {
2989         register char *cp, *er;
2990
2991         if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL)
2992                 er = ": no home directory";
2993         else if(chdir(cp) < 0)
2994                 er = ": bad directory";
2995         else
2996                 return(0);
2997         prs(cp != NULL? cp: "cd");
2998         err(er);
2999         return(1);
3000 }
3001
3002 static int
3003 doshift(t)
3004 register struct op *t;
3005 {
3006         register int n;
3007
3008         n = t->words[1]? getn(t->words[1]): 1;
3009         if(dolc < n) {
3010                 err("nothing to shift");
3011                 return(1);
3012         }
3013         dolv[n] = dolv[0];
3014         dolv += n;
3015         dolc -= n;
3016         setval(lookup("#"), putn(dolc));
3017         return(0);
3018 }
3019
3020 /*
3021  * execute login and newgrp directly
3022  */
3023 static int
3024 dologin(t)
3025 struct op *t;
3026 {
3027         register char *cp;
3028
3029         if (interactive) {
3030                 signal(SIGINT, SIG_DFL);
3031                 signal(SIGQUIT, SIG_DFL);
3032         }
3033         cp = rexecve(t->words[0], t->words, makenv());
3034         prs(t->words[0]); prs(": "); err(cp);
3035         return(1);
3036 }
3037
3038 static int
3039 doumask(t)
3040 register struct op *t;
3041 {
3042         register int i, n;
3043         register char *cp;
3044
3045         if ((cp = t->words[1]) == NULL) {
3046                 i = umask(0);
3047                 umask(i);
3048                 for (n=3*4; (n-=3) >= 0;)
3049                         putc('0'+((i>>n)&07), stderr);
3050                 putc('\n', stderr);
3051         } else {
3052                 for (n=0; *cp>='0' && *cp<='9'; cp++)
3053                         n = n*8 + (*cp-'0');
3054                 umask(n);
3055         }
3056         return(0);
3057 }
3058
3059 static int
3060 doexec(t)
3061 register struct op *t;
3062 {
3063         register int i;
3064         jmp_buf ex;
3065         xint *ofail;
3066
3067         t->ioact = NULL;
3068         for(i = 0; (t->words[i]=t->words[i+1]) != NULL; i++)
3069                 ;
3070         if (i == 0)
3071                 return(1);
3072         execflg = 1;
3073         ofail = failpt;
3074         if (setjmp(failpt = ex) == 0)
3075                 execute(t, NOPIPE, NOPIPE, FEXEC);
3076         failpt = ofail;
3077         execflg = 0;
3078         return(1);
3079 }
3080
3081 static int
3082 dodot(t)
3083 struct op *t;
3084 {
3085         register int i;
3086         register char *sp, *tp;
3087         char *cp;
3088
3089         if ((cp = t->words[1]) == NULL)
3090                 return(0);
3091         sp = any('/', cp)? ":": path->value;
3092         while (*sp) {
3093                 tp = e.linep;
3094                 while (*sp && (*tp = *sp++) != ':')
3095                         tp++;
3096                 if (tp != e.linep)
3097                         *tp++ = '/';
3098                 for (i = 0; (*tp++ = cp[i++]) != '\0';)
3099                         ;
3100                 if ((i = open(e.linep, 0)) >= 0) {
3101                         exstat = 0;
3102                         next(remap(i));
3103                         return(exstat);
3104                 }
3105         }
3106         prs(cp);
3107         err(": not found");
3108         return(-1);
3109 }
3110
3111 static int
3112 dowait(t)
3113 struct op *t;
3114 {
3115         register int i;
3116         register char *cp;
3117
3118         if ((cp = t->words[1]) != NULL) {
3119                 i = getn(cp);
3120                 if (i == 0)
3121                         return(0);
3122         } else
3123                 i = -1;
3124         setstatus(waitfor(i, 1));
3125         return(0);
3126 }
3127
3128 static int
3129 doread(t)
3130 struct op *t;
3131 {
3132         register char *cp, **wp;
3133         register int nb = 0;
3134         register int  nl = 0;
3135
3136         if (t->words[1] == NULL) {
3137                 err("Usage: read name ...");
3138                 return(1);
3139         }
3140         for (wp = t->words+1; *wp; wp++) {
3141                 for (cp = e.linep; !nl && cp < elinep-1; cp++)
3142                         if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) ||
3143                             (nl = (*cp == '\n')) ||
3144                             (wp[1] && any(*cp, ifs->value)))
3145                                 break;
3146                 *cp = 0;
3147                 if (nb <= 0)
3148                         break;
3149                 setval(lookup(*wp), e.linep);
3150         }
3151         return(nb <= 0);
3152 }
3153
3154 static int
3155 doeval(t)
3156 register struct op *t;
3157 {
3158         return(RUN(awordlist, t->words+1, wdchar));
3159 }
3160
3161 static int
3162 dotrap(t)
3163 register struct op *t;
3164 {
3165         register int  n, i;
3166         register int  resetsig;
3167
3168         if (t->words[1] == NULL) {
3169                 for (i=0; i<=_NSIG; i++)
3170                         if (trap[i]) {
3171                                 prn(i);
3172                                 prs(": ");
3173                                 prs(trap[i]);
3174                                 prs("\n");
3175                         }
3176                 return(0);
3177         }
3178         resetsig = isdigit(*t->words[1]);
3179         for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) {
3180                 n = getsig(t->words[i]);
3181                 freecell(trap[n]);
3182                 trap[n] = 0;
3183                 if (!resetsig) {
3184                         if (*t->words[1] != '\0') {
3185                                 trap[n] = strsave(t->words[1], 0);
3186                                 setsig(n, sig);
3187                         } else
3188                                 setsig(n, SIG_IGN);
3189                 } else {
3190                         if (interactive)
3191                                 if (n == SIGINT)
3192                                         setsig(n, onintr);
3193                                 else
3194                                         setsig(n, n == SIGQUIT ? SIG_IGN
3195                                                                : SIG_DFL);
3196                         else
3197                                 setsig(n, SIG_DFL);
3198                 }
3199         }
3200         return(0);
3201 }
3202
3203 static int
3204 getsig(s)
3205 char *s;
3206 {
3207         register int n;
3208
3209         if ((n = getn(s)) < 0 || n > _NSIG) {
3210                 err("trap: bad signal number");
3211                 n = 0;
3212         }
3213         return(n);
3214 }
3215
3216 static void
3217 setsig( register int n, sighandler_t f)
3218 {
3219         if (n == 0)
3220                 return;
3221         if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) {
3222                 ourtrap[n] = 1;
3223                 signal(n, f);
3224         }
3225 }
3226
3227 static int
3228 getn(as)
3229 char *as;
3230 {
3231         register char *s;
3232         register int n, m;
3233
3234         s = as;
3235         m = 1;
3236         if (*s == '-') {
3237                 m = -1;
3238                 s++;
3239         }
3240         for (n = 0; isdigit(*s); s++)
3241                 n = (n*10) + (*s-'0');
3242         if (*s) {
3243                 prs(as);
3244                 err(": bad number");
3245         }
3246         return(n*m);
3247 }
3248
3249 static int
3250 dobreak(t)
3251 struct op *t;
3252 {
3253         return(brkcontin(t->words[1], 1));
3254 }
3255
3256 static int
3257 docontinue(t)
3258 struct op *t;
3259 {
3260         return(brkcontin(t->words[1], 0));
3261 }
3262
3263 static int
3264 brkcontin(cp, val)
3265 register char *cp;
3266 int val;
3267 {
3268         register struct brkcon *bc;
3269         register int nl;
3270
3271         nl = cp == NULL? 1: getn(cp);
3272         if (nl <= 0)
3273                 nl = 999;
3274         do {
3275                 if ((bc = brklist) == NULL)
3276                         break;
3277                 brklist = bc->nextlev;
3278         } while (--nl);
3279         if (nl) {
3280                 err("bad break/continue level");
3281                 return(1);
3282         }
3283         isbreak = val;
3284         longjmp(bc->brkpt, 1);
3285         /* NOTREACHED */
3286 }
3287
3288 static int
3289 doexit(t)
3290 struct op *t;
3291 {
3292         register char *cp;
3293
3294         execflg = 0;
3295         if ((cp = t->words[1]) != NULL)
3296                 setstatus(getn(cp));
3297         leave();
3298         /* NOTREACHED */
3299         return(0);
3300 }
3301
3302 static int
3303 doexport(t)
3304 struct op *t;
3305 {
3306         rdexp(t->words+1, export, EXPORT);
3307         return(0);
3308 }
3309
3310 static int
3311 doreadonly(t)
3312 struct op *t;
3313 {
3314         rdexp(t->words+1, ronly, RONLY);
3315         return(0);
3316 }
3317
3318 static void rdexp (char **wp, void (*f)(struct var *), int key)
3319 {
3320         if (*wp != NULL) {
3321                 for (; *wp != NULL; wp++) {
3322                         if (isassign(*wp)) {
3323                                 char *cp;
3324                                 assign(*wp, COPYV);
3325                                 for (cp = *wp; *cp != '='; cp++)
3326                                         ;
3327                                 *cp = '\0';
3328                         }
3329                         if (checkname(*wp))
3330                                 (*f)(lookup(*wp));
3331                         else
3332                                 badid(*wp);
3333                 }
3334         } else
3335                 putvlist(key, 1);
3336 }
3337
3338 static void
3339 badid(s)
3340 register char *s;
3341 {
3342         prs(s);
3343         err(": bad identifier");
3344 }
3345
3346 static int
3347 doset(t)
3348 register struct op *t;
3349 {
3350         register struct var *vp;
3351         register char *cp;
3352         register int n;
3353
3354         if ((cp = t->words[1]) == NULL) {
3355                 for (vp = vlist; vp; vp = vp->next)
3356                         varput(vp->name, 1);
3357                 return(0);
3358         }
3359         if (*cp == '-') {
3360                 /* bad: t->words++; */
3361                 for(n = 0; (t->words[n]=t->words[n+1]) != NULL; n++)
3362                         ;
3363                 if (*++cp == 0)
3364                         flag['x'] = flag['v'] = 0;
3365                 else
3366                         for (; *cp; cp++)
3367                                 switch (*cp) {
3368                                 case 'e':
3369                                         if (!interactive)
3370                                                 flag['e']++;
3371                                         break;
3372
3373                                 default:
3374                                         if (*cp>='a' && *cp<='z')
3375                                                 flag[(int)*cp]++;
3376                                         break;
3377                                 }
3378                 setdash();
3379         }
3380         if (t->words[1]) {
3381                 t->words[0] = dolv[0];
3382                 for (n=1; t->words[n]; n++)
3383                         setarea((char *)t->words[n], 0);
3384                 dolc = n-1;
3385                 dolv = t->words;
3386                 setval(lookup("#"), putn(dolc));
3387                 setarea((char *)(dolv-1), 0);
3388         }
3389         return(0);
3390 }
3391
3392 static void
3393 varput(s, out)
3394 register char *s;
3395 int out;
3396 {
3397         if (isalnum(*s) || *s == '_') {
3398                 write(out, s, strlen(s));
3399                 write(out, "\n", 1);
3400         }
3401 }
3402
3403
3404 /*
3405  * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
3406  * This file contains code for the times builtin.
3407  */
3408 static int dotimes(struct op *t )
3409 {
3410         struct tms buf;
3411         long int clk_tck = sysconf(_SC_CLK_TCK);
3412
3413         times(&buf);
3414         printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
3415                (int) (buf.tms_utime / clk_tck / 60),
3416                ((double) buf.tms_utime) / clk_tck,
3417                (int) (buf.tms_stime / clk_tck / 60),
3418                ((double) buf.tms_stime) / clk_tck,
3419                (int) (buf.tms_cutime / clk_tck / 60),
3420                ((double) buf.tms_cutime) / clk_tck,
3421                (int) (buf.tms_cstime / clk_tck / 60),
3422                ((double) buf.tms_cstime) / clk_tck);
3423         return 0;
3424 }
3425
3426
3427 static int(*inbuilt(char *s))(struct op *)
3428 {
3429         const struct builtincmd *bp;
3430
3431         for (bp = builtincmds; bp->name != NULL; bp++)
3432                 if (strcmp(bp->name, s) == 0)
3433                         return(bp->builtinfunc);
3434
3435         return(NULL);
3436 }
3437
3438 /* -------- eval.c -------- */
3439
3440 /*
3441  * ${}
3442  * `command`
3443  * blank interpretation
3444  * quoting
3445  * glob
3446  */
3447
3448 static char ** eval( char **ap, int f)
3449 {
3450         struct wdblock *wb;
3451         char **wp;
3452         char **wf;
3453         jmp_buf ev;
3454
3455 #if __GNUC__
3456         /* Avoid longjmp clobbering */
3457         (void) &wp;
3458         (void) &ap;
3459 #endif
3460         wp = NULL;
3461         wb = NULL;
3462         wf = NULL;
3463         if (newenv(setjmp(errpt = ev)) == 0) {
3464                 while (*ap && isassign(*ap))
3465                         expand(*ap++, &wb, f & ~DOGLOB);
3466                 if (flag['k']) {
3467                         for (wf = ap; *wf; wf++) {
3468                                 if (isassign(*wf))
3469                                         expand(*wf, &wb, f & ~DOGLOB);
3470                         }
3471                 }
3472                 for (wb = addword((char *)0, wb); *ap; ap++) {
3473                         if (!flag['k'] || !isassign(*ap))
3474                                 expand(*ap, &wb, f & ~DOKEY);
3475                 }
3476                 wb = addword((char *)0, wb);
3477                 wp = getwords(wb);
3478                 quitenv();
3479         } else
3480                 gflg = 1;
3481         return(gflg? (char **)NULL: wp);
3482 }
3483
3484 /*
3485  * Make the exported environment from the exported
3486  * names in the dictionary. Keyword assignments
3487  * will already have been done.
3488  */
3489 static char **
3490 makenv()
3491
3492 {
3493         register struct wdblock *wb;
3494         register struct var *vp;
3495
3496         wb = NULL;
3497         for (vp = vlist; vp; vp = vp->next)
3498                 if (vp->status & EXPORT)
3499                         wb = addword(vp->name, wb);
3500         wb = addword((char *)0, wb);
3501         return(getwords(wb));
3502 }
3503
3504 static char *
3505 evalstr(cp, f)
3506 register char *cp;
3507 int f;
3508 {
3509         struct wdblock *wb;
3510
3511         wb = NULL;
3512         if (expand(cp, &wb, f)) {
3513                 if (wb == NULL || wb->w_nword == 0 || (cp = wb->w_words[0]) == NULL)
3514                         cp = "";
3515                 DELETE(wb);
3516         } else
3517                 cp = NULL;
3518         return(cp);
3519 }
3520
3521 static int
3522 expand( char *cp, register struct wdblock **wbp, int f)
3523 {
3524         jmp_buf ev;
3525
3526 #if __GNUC__
3527         /* Avoid longjmp clobbering */
3528         (void) &cp;
3529 #endif
3530         gflg = 0;
3531         if (cp == NULL)
3532                 return(0);
3533         if (!anys("$`'\"", cp) &&
3534             !anys(ifs->value, cp) &&
3535             ((f&DOGLOB)==0 || !anys("[*?", cp))) {
3536                 cp = strsave(cp, areanum);
3537                 if (f & DOTRIM)
3538                         unquote(cp);
3539                 *wbp = addword(cp, *wbp);
3540                 return(1);
3541         }
3542         if (newenv(setjmp(errpt = ev)) == 0) {
3543                 PUSHIO(aword, cp, strchar);
3544                 e.iobase = e.iop;
3545                 while ((cp = blank(f)) && gflg == 0) {
3546                         e.linep = cp;
3547                         cp = strsave(cp, areanum);
3548                         if ((f&DOGLOB) == 0) {
3549                                 if (f & DOTRIM)
3550                                         unquote(cp);
3551                                 *wbp = addword(cp, *wbp);
3552                         } else
3553                                 *wbp = glob(cp, *wbp);
3554                 }
3555                 quitenv();
3556         } else
3557                 gflg = 1;
3558         return(gflg == 0);
3559 }
3560
3561 /*
3562  * Blank interpretation and quoting
3563  */
3564 static char *
3565 blank(f)
3566 int f;
3567 {
3568         register int c, c1;
3569         register char *sp;
3570         int scanequals, foundequals;
3571
3572         sp = e.linep;
3573         scanequals = f & DOKEY;
3574         foundequals = 0;
3575
3576 loop:
3577         switch (c = subgetc('"', foundequals)) {
3578         case 0:
3579                 if (sp == e.linep)
3580                         return(0);
3581                 *e.linep++ = 0;
3582                 return(sp);
3583
3584         default:
3585                 if (f & DOBLANK && any(c, ifs->value))
3586                         goto loop;
3587                 break;
3588
3589         case '"':
3590         case '\'':
3591                 scanequals = 0;
3592                 if (INSUB())
3593                         break;
3594                 for (c1 = c; (c = subgetc(c1, 1)) != c1;) {
3595                         if (c == 0)
3596                                 break;
3597                         if (c == '\'' || !any(c, "$`\""))
3598                                 c |= QUOTE;
3599                         *e.linep++ = c;
3600                 }
3601                 c = 0;
3602         }
3603         unget(c);
3604         if (!isalpha(c) && c != '_')
3605                 scanequals = 0;
3606         for (;;) {
3607                 c = subgetc('"', foundequals);
3608                 if (c == 0 ||
3609                     f & (DOBLANK && any(c, ifs->value)) ||
3610                     (!INSUB() && any(c, "\"'"))) {
3611                         scanequals = 0;
3612                         unget(c);
3613                         if (any(c, "\"'"))
3614                                 goto loop;
3615                         break;
3616                 }
3617                 if (scanequals) {
3618                         if (c == '=') {
3619                                 foundequals = 1;
3620                                 scanequals  = 0;
3621                         }
3622                         else if (!isalnum(c) && c != '_')
3623                                 scanequals = 0;
3624                 }
3625                 *e.linep++ = c;
3626         }
3627         *e.linep++ = 0;
3628         return(sp);
3629 }
3630
3631 /*
3632  * Get characters, substituting for ` and $
3633  */
3634 static int
3635 subgetc(ec, quoted)
3636 register char ec;
3637 int quoted;
3638 {
3639         register char c;
3640
3641 again:
3642         c = my_getc(ec);
3643         if (!INSUB() && ec != '\'') {
3644                 if (c == '`') {
3645                         if (grave(quoted) == 0)
3646                                 return(0);
3647                         e.iop->task = XGRAVE;
3648                         goto again;
3649                 }
3650                 if (c == '$' && (c = dollar(quoted)) == 0) {
3651                         e.iop->task = XDOLL;
3652                         goto again;
3653                 }
3654         }
3655         return(c);
3656 }
3657
3658 /*
3659  * Prepare to generate the string returned by ${} substitution.
3660  */
3661 static int
3662 dollar(quoted)
3663 int quoted;
3664 {
3665         int otask;
3666         struct io *oiop;
3667         char *dolp;
3668         register char *s, c, *cp=NULL;
3669         struct var *vp;
3670
3671         c = readc();
3672         s = e.linep;
3673         if (c != '{') {
3674                 *e.linep++ = c;
3675                 if (isalpha(c) || c == '_') {
3676                         while ((c = readc())!=0 && (isalnum(c) || c == '_'))
3677                                 if (e.linep < elinep)
3678                                         *e.linep++ = c;
3679                         unget(c);
3680                 }
3681                 c = 0;
3682         } else {
3683                 oiop = e.iop;
3684                 otask = e.iop->task;
3685                 e.iop->task = XOTHER;
3686                 while ((c = subgetc('"', 0))!=0 && c!='}' && c!='\n')
3687                         if (e.linep < elinep)
3688                                 *e.linep++ = c;
3689                 if (oiop == e.iop)
3690                         e.iop->task = otask;
3691                 if (c != '}') {
3692                         err("unclosed ${");
3693                         gflg++;
3694                         return(c);
3695                 }
3696         }
3697         if (e.linep >= elinep) {
3698                 err("string in ${} too long");
3699                 gflg++;
3700                 e.linep -= 10;
3701         }
3702         *e.linep = 0;
3703         if (*s)
3704                 for (cp = s+1; *cp; cp++)
3705                         if (any(*cp, "=-+?")) {
3706                                 c = *cp;
3707                                 *cp++ = 0;
3708                                 break;
3709                         }
3710         if (s[1] == 0 && (*s == '*' || *s == '@')) {
3711                 if (dolc > 1) {
3712                         /* currently this does not distinguish $* and $@ */
3713                         /* should check dollar */
3714                         e.linep = s;
3715                         PUSHIO(awordlist, dolv+1, dolchar);
3716                         return(0);
3717                 } else {        /* trap the nasty ${=} */
3718                         s[0] = '1';
3719                         s[1] = 0;
3720                 }
3721         }
3722         vp = lookup(s);
3723         if ((dolp = vp->value) == null) {
3724                 switch (c) {
3725                 case '=':
3726                         if (isdigit(*s)) {
3727                                 err("cannot use ${...=...} with $n");
3728                                 gflg++;
3729                                 break;
3730                         }
3731                         setval(vp, cp);
3732                         dolp = vp->value;
3733                         break;
3734
3735                 case '-':
3736                         dolp = strsave(cp, areanum);
3737                         break;
3738
3739                 case '?':
3740                         if (*cp == 0) {
3741                                 prs("missing value for ");
3742                                 err(s);
3743                         } else
3744                                 err(cp);
3745                         gflg++;
3746                         break;
3747                 }
3748         } else if (c == '+')
3749                 dolp = strsave(cp, areanum);
3750         if (flag['u'] && dolp == null) {
3751                 prs("unset variable: ");
3752                 err(s);
3753                 gflg++;
3754         }
3755         e.linep = s;
3756         PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
3757         return(0);
3758 }
3759
3760 /*
3761  * Run the command in `...` and read its output.
3762  */
3763
3764 static int
3765 grave(quoted)
3766 int quoted;
3767 {
3768         char *cp;
3769         register int i;
3770         int j;
3771         int pf[2];
3772         static char child_cmd[LINELIM];
3773         char *src;
3774         char *dest;
3775         int count;
3776         int ignore;
3777         int ignore_once;
3778         char *argument_list[4];
3779
3780 #if __GNUC__
3781         /* Avoid longjmp clobbering */
3782         (void) &cp;
3783 #endif
3784
3785         for (cp = e.iop->argp->aword; *cp != '`'; cp++)
3786                 if (*cp == 0) {
3787                         err("no closing `");
3788                         return(0);
3789                 }
3790
3791         /* string copy with dollar expansion */
3792         src = e.iop->argp->aword;
3793         dest = child_cmd;
3794         count = 0;
3795         ignore = 0;
3796         ignore_once = 0;
3797         while ((*src != '`') && (count < LINELIM)) {
3798                 if (*src == '\'')
3799                         ignore = !ignore;
3800                 if (*src == '\\')
3801                         ignore_once = 1;
3802                 if (*src == '$' && !ignore && !ignore_once) {
3803                         struct var *vp;
3804                         char var_name[LINELIM];
3805                         char alt_value[LINELIM];
3806                         int var_index = 0;
3807                         int alt_index = 0;
3808                         char operator = 0;
3809                         int braces = 0;
3810                         char *value;
3811
3812                         src++;
3813                         if (*src == '{') {
3814                                 braces = 1;
3815                                 src++;
3816                         }
3817
3818                         var_name[var_index++] = *src++;
3819                         while (isalnum(*src))
3820                                 var_name[var_index++] = *src++;
3821                         var_name[var_index] = 0;
3822
3823                         if (braces) {
3824                                 switch (*src) {
3825                                 case '}':
3826                                         break;
3827                                 case '-':
3828                                 case '=':
3829                                 case '+':
3830                                 case '?':
3831                                         operator = *src;
3832                                         break;
3833                                 default:
3834                                         err("unclosed ${\n");
3835                                         return(0);
3836                                 }
3837                                 if (operator) {
3838                                         src++;
3839                                         while (*src && (*src != '}')) {
3840                                                 alt_value[alt_index++] = *src++;
3841                                         }
3842                                         alt_value[alt_index] = 0;
3843                                         if (*src != '}') {
3844                                                 err("unclosed ${\n");
3845                                                 return(0);
3846                                         }
3847                                 }
3848                                 src++;
3849                         }
3850
3851                         vp = lookup(var_name);
3852                         if (vp->value != null)
3853                                 value = (operator == '+')? alt_value : vp->value;
3854                         else if (operator == '?') {
3855                                 err(alt_value);
3856                                 return(0);
3857                         } else if (alt_index && (operator != '+')) {
3858                                 value = alt_value;
3859                                 if (operator == '=')
3860                                         setval(vp, value);
3861                         } else
3862                                 continue;
3863
3864                         while (*value && (count < LINELIM)) {
3865                                 *dest++ = *value++;
3866                                 count++;
3867                         }
3868                 } else {
3869                         *dest++ = *src++;
3870                         count++;
3871                         ignore_once = 0;
3872                 }
3873         }
3874         *dest = '\0';
3875
3876         if (openpipe(pf) < 0)
3877                 return(0);
3878         while ((i = vfork()) == -1 && errno == EAGAIN)
3879                 ;
3880         if (i < 0) {
3881                 closepipe(pf);
3882                 err((char*)bb_msg_memory_exhausted);
3883                 return(0);
3884         }
3885         if (i != 0) {
3886                 waitpid(i, NULL, 0);
3887                 e.iop->argp->aword = ++cp;
3888                 close(pf[1]);
3889                 PUSHIO(afile, remap(pf[0]), (int(*)(struct ioarg *))((quoted)? qgravechar: gravechar));
3890                 return(1);
3891         }
3892         /* allow trapped signals */
3893         /* XXX - Maybe this signal stuff should go as well? */
3894         for (j=0; j<=_NSIG; j++)
3895                 if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN)
3896                         signal(j, SIG_DFL);
3897
3898         dup2(pf[1], 1);
3899         closepipe(pf);
3900
3901         argument_list[0] = (char *)DEFAULT_SHELL;
3902         argument_list[1] = "-c";
3903         argument_list[2] = child_cmd;
3904         argument_list[3] = 0;
3905
3906         prs(rexecve(argument_list[0], argument_list, makenv()));
3907         _exit(1);
3908 }
3909
3910
3911 static char *
3912 unquote(as)
3913 register char *as;
3914 {
3915         register char *s;
3916
3917         if ((s = as) != NULL)
3918                 while (*s)
3919                         *s++ &= ~QUOTE;
3920         return(as);
3921 }
3922
3923 /* -------- glob.c -------- */
3924
3925 /*
3926  * glob
3927  */
3928
3929 #define scopy(x) strsave((x), areanum)
3930 #define BLKSIZ  512
3931 #define NDENT   ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
3932
3933 static  struct wdblock  *cl, *nl;
3934 static  char    spcl[] = "[?*";
3935
3936 static struct wdblock *
3937 glob(cp, wb)
3938 char *cp;
3939 struct wdblock *wb;
3940 {
3941         register int i;
3942         register char *pp;
3943
3944         if (cp == 0)
3945                 return(wb);
3946         i = 0;
3947         for (pp = cp; *pp; pp++)
3948                 if (any(*pp, spcl))
3949                         i++;
3950                 else if (!any(*pp & ~QUOTE, spcl))
3951                         *pp &= ~QUOTE;
3952         if (i != 0) {
3953                 for (cl = addword(scopy(cp), (struct wdblock *)0); anyspcl(cl); cl = nl) {
3954                         nl = newword(cl->w_nword*2);
3955                         for(i=0; i<cl->w_nword; i++) { /* for each argument */
3956                                 for (pp = cl->w_words[i]; *pp; pp++)
3957                                         if (any(*pp, spcl)) {
3958                                                 globname(cl->w_words[i], pp);
3959                                                 break;
3960                                         }
3961                                 if (*pp == '\0')
3962                                         nl = addword(scopy(cl->w_words[i]), nl);
3963                         }
3964                         for(i=0; i<cl->w_nword; i++)
3965                                 DELETE(cl->w_words[i]);
3966                         DELETE(cl);
3967                 }
3968                 for(i=0; i<cl->w_nword; i++)
3969                         unquote(cl->w_words[i]);
3970                 glob0((char *)cl->w_words, cl->w_nword, sizeof(char *), xstrcmp);
3971                 if (cl->w_nword) {
3972                         for (i=0; i<cl->w_nword; i++)
3973                                 wb = addword(cl->w_words[i], wb);
3974                         DELETE(cl);
3975                         return(wb);
3976                 }
3977         }
3978         wb = addword(unquote(cp), wb);
3979         return(wb);
3980 }
3981
3982 static void
3983 globname(we, pp)
3984 char *we;
3985 register char *pp;
3986 {
3987         register char *np, *cp;
3988         char *name, *gp, *dp;
3989         int k;
3990         DIR *dirp;
3991         struct dirent *de;
3992         char dname[NAME_MAX+1];
3993         struct stat dbuf;
3994
3995         for (np = we; np != pp; pp--)
3996                 if (pp[-1] == '/')
3997                         break;
3998         for (dp = cp = space((int)(pp-np)+3); np < pp;)
3999                 *cp++ = *np++;
4000         *cp++ = '.';
4001         *cp = '\0';
4002         for (gp = cp = space(strlen(pp)+1); *np && *np != '/';)
4003                 *cp++ = *np++;
4004         *cp = '\0';
4005         dirp = opendir(dp);
4006         if (dirp == 0) {
4007                 DELETE(dp);
4008                 DELETE(gp);
4009                 return;
4010         }
4011         dname[NAME_MAX] = '\0';
4012         while ((de=readdir(dirp))!=NULL) {
4013                 /* XXX Hmmm... What this could be? (abial) */
4014                 /*
4015                 if (ent[j].d_ino == 0)
4016                         continue;
4017                 */
4018                 strncpy(dname, de->d_name, NAME_MAX);
4019                         if (dname[0] == '.')
4020                                 if (*gp != '.')
4021                                         continue;
4022                         for(k=0; k<NAME_MAX; k++)
4023                                 if (any(dname[k], spcl))
4024                                         dname[k] |= QUOTE;
4025                         if (gmatch(dname, gp)) {
4026                                 name = generate(we, pp, dname, np);
4027                                 if (*np && !anys(np, spcl)) {
4028                                         if (stat(name,&dbuf)) {
4029                                                 DELETE(name);
4030                                                 continue;
4031                                         }
4032                                 }
4033                                 nl = addword(name, nl);
4034                         }
4035         }
4036         closedir(dirp);
4037         DELETE(dp);
4038         DELETE(gp);
4039 }
4040
4041 /*
4042  * generate a pathname as below.
4043  * start..end1 / middle end
4044  * the slashes come for free
4045  */
4046 static char *
4047 generate(start1, end1, middle, end)
4048 char *start1;
4049 register char *end1;
4050 char *middle, *end;
4051 {
4052         char *p;
4053         register char *op, *xp;
4054
4055         p = op = space((int)(end1-start1)+strlen(middle)+strlen(end)+2);
4056         for (xp = start1; xp != end1;)
4057                 *op++ = *xp++;
4058         for (xp = middle; (*op++ = *xp++) != '\0';)
4059                 ;
4060         op--;
4061         for (xp = end; (*op++ = *xp++) != '\0';)
4062                 ;
4063         return(p);
4064 }
4065
4066 static int
4067 anyspcl(wb)
4068 register struct wdblock *wb;
4069 {
4070         register int i;
4071         register char **wd;
4072
4073         wd = wb->w_words;
4074         for (i=0; i<wb->w_nword; i++)
4075                 if (anys(spcl, *wd++))
4076                         return(1);
4077         return(0);
4078 }
4079
4080 static int
4081 xstrcmp(p1, p2)
4082 char *p1, *p2;
4083 {
4084         return(strcmp(*(char **)p1, *(char **)p2));
4085 }
4086
4087 /* -------- word.c -------- */
4088
4089 static struct wdblock *
4090 newword(nw)
4091 register int nw;
4092 {
4093         register struct wdblock *wb;
4094
4095         wb = (struct wdblock *) space(sizeof(*wb) + nw*sizeof(char *));
4096         wb->w_bsize = nw;
4097         wb->w_nword = 0;
4098         return(wb);
4099 }
4100
4101 static struct wdblock *
4102 addword(wd, wb)
4103 char *wd;
4104 register struct wdblock *wb;
4105 {
4106         register struct wdblock *wb2;
4107         register int nw;
4108
4109         if (wb == NULL)
4110                 wb = newword(NSTART);
4111         if ((nw = wb->w_nword) >= wb->w_bsize) {
4112                 wb2 = newword(nw * 2);
4113                 memcpy((char *)wb2->w_words, (char *)wb->w_words, nw*sizeof(char *));
4114                 wb2->w_nword = nw;
4115                 DELETE(wb);
4116                 wb = wb2;
4117         }
4118         wb->w_words[wb->w_nword++] = wd;
4119         return(wb);
4120 }
4121 static
4122 char **
4123 getwords(wb)
4124 register struct wdblock *wb;
4125 {
4126         register char **wd;
4127         register int nb;
4128
4129         if (wb == NULL)
4130                 return((char **)NULL);
4131         if (wb->w_nword == 0) {
4132                 DELETE(wb);
4133                 return((char **)NULL);
4134         }
4135         wd = (char **) space(nb = sizeof(*wd) * wb->w_nword);
4136         memcpy((char *)wd, (char *)wb->w_words, nb);
4137         DELETE(wb);     /* perhaps should done by caller */
4138         return(wd);
4139 }
4140
4141 int (*func)(char *, char *);
4142 int     globv;
4143
4144 static void
4145 glob0(a0, a1, a2, a3)
4146 char *a0;
4147 unsigned a1;
4148 int a2;
4149 int (*a3) (char *, char *);
4150 {
4151         func = a3;
4152         globv = a2;
4153         glob1(a0, a0 + a1 * a2);
4154 }
4155
4156 static void
4157 glob1(base, lim)
4158 char *base, *lim;
4159 {
4160         register char *i, *j;
4161         int v2;
4162         char *lptr, *hptr;
4163         int c;
4164         unsigned n;
4165
4166
4167         v2 = globv;
4168
4169 top:
4170         if ((n=(int)(lim-base)) <= v2)
4171                 return;
4172         n = v2 * (n / (2*v2));
4173         hptr = lptr = base+n;
4174         i = base;
4175         j = lim-v2;
4176         for(;;) {
4177                 if (i < lptr) {
4178                         if ((c = (*func)(i, lptr)) == 0) {
4179                                 glob2(i, lptr -= v2);
4180                                 continue;
4181                         }
4182                         if (c < 0) {
4183                                 i += v2;
4184                                 continue;
4185                         }
4186                 }
4187
4188 begin:
4189                 if (j > hptr) {
4190                         if ((c = (*func)(hptr, j)) == 0) {
4191                                 glob2(hptr += v2, j);
4192                                 goto begin;
4193                         }
4194                         if (c > 0) {
4195                                 if (i == lptr) {
4196                                         glob3(i, hptr += v2, j);
4197                                         i = lptr += v2;
4198                                         goto begin;
4199                                 }
4200                                 glob2(i, j);
4201                                 j -= v2;
4202                                 i += v2;
4203                                 continue;
4204                         }
4205                         j -= v2;
4206                         goto begin;
4207                 }
4208
4209
4210                 if (i == lptr) {
4211                         if (lptr-base >= lim-hptr) {
4212                                 glob1(hptr+v2, lim);
4213                                 lim = lptr;
4214                         } else {
4215                                 glob1(base, lptr);
4216                                 base = hptr+v2;
4217                         }
4218                         goto top;
4219                 }
4220
4221
4222                 glob3(j, lptr -= v2, i);
4223                 j = hptr -= v2;
4224         }
4225 }
4226
4227 static void
4228 glob2(i, j)
4229 char *i, *j;
4230 {
4231         register char *index1, *index2, c;
4232         int m;
4233
4234         m = globv;
4235         index1 = i;
4236         index2 = j;
4237         do {
4238                 c = *index1;
4239                 *index1++ = *index2;
4240                 *index2++ = c;
4241         } while(--m);
4242 }
4243
4244 static void
4245 glob3(i, j, k)
4246 char *i, *j, *k;
4247 {
4248         register char *index1, *index2, *index3;
4249         int c;
4250         int m;
4251
4252         m = globv;
4253         index1 = i;
4254         index2 = j;
4255         index3 = k;
4256         do {
4257                 c = *index1;
4258                 *index1++ = *index3;
4259                 *index3++ = *index2;
4260                 *index2++ = c;
4261         } while(--m);
4262 }
4263
4264 /* -------- io.c -------- */
4265
4266 /*
4267  * shell IO
4268  */
4269
4270 static int my_getc( int ec)
4271 {
4272         register int c;
4273
4274         if(e.linep > elinep) {
4275                 while((c=readc()) != '\n' && c)
4276                         ;
4277                 err("input line too long");
4278                 gflg++;
4279                 return(c);
4280         }
4281         c = readc();
4282         if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) {
4283                 if(c == '\\') {
4284                         c = readc();
4285                         if (c == '\n' && ec != '\"')
4286                                 return(my_getc(ec));
4287                         c |= QUOTE;
4288                 }
4289         }
4290         return(c);
4291 }
4292
4293 static void
4294 unget(c)
4295 int c;
4296 {
4297         if (e.iop >= e.iobase)
4298                 e.iop->peekc = c;
4299 }
4300
4301 static int
4302 eofc()
4303
4304 {
4305   return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0);
4306 }
4307
4308 static int
4309 readc()
4310 {
4311         register int c;
4312
4313         for (; e.iop >= e.iobase; e.iop--)
4314                 if ((c = e.iop->peekc) != '\0') {
4315                         e.iop->peekc = 0;
4316                         return(c);
4317                 }
4318                 else {
4319                     if (e.iop->prev != 0) {
4320                         if ((c = (*e.iop->iofn)(e.iop->argp, e.iop)) != '\0') {
4321                                 if (c == -1) {
4322                                         e.iop++;
4323                                         continue;
4324                                 }
4325                                 if (e.iop == iostack)
4326                                         ioecho(c);
4327                                 return(e.iop->prev = c);
4328                         }
4329                         else if (e.iop->task == XIO && e.iop->prev != '\n') {
4330                                 e.iop->prev = 0;
4331                                 if (e.iop == iostack)
4332                                         ioecho('\n');
4333                                 return '\n';
4334                         }
4335                     }
4336                     if (e.iop->task == XIO) {
4337                         if (multiline)
4338                             return e.iop->prev = 0;
4339                         if (interactive && e.iop == iostack+1) {
4340 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4341                             current_prompt=prompt->value;
4342 #else
4343                             prs(prompt->value);
4344 #endif
4345                         }
4346                     }
4347                 }
4348         if (e.iop >= iostack)
4349                 return(0);
4350         leave();
4351         /* NOTREACHED */
4352         return(0);
4353 }
4354
4355 static void
4356 ioecho(c)
4357 char c;
4358 {
4359         if (flag['v'])
4360                 write(2, &c, sizeof c);
4361 }
4362
4363 static void
4364 pushio(struct ioarg *argp, int (*fn)(struct ioarg *))
4365 {
4366         if (++e.iop >= &iostack[NPUSH]) {
4367                 e.iop--;
4368                 err("Shell input nested too deeply");
4369                 gflg++;
4370                 return;
4371         }
4372         e.iop->iofn = (int (*)(struct  ioarg *, struct io *))fn;
4373
4374         if (argp->afid != AFID_NOBUF)
4375           e.iop->argp = argp;
4376         else {
4377           e.iop->argp  = ioargstack + (e.iop - iostack);
4378           *e.iop->argp = *argp;
4379           e.iop->argp->afbuf = e.iop == &iostack[0] ? &mainbuf : &sharedbuf;
4380           if (isatty(e.iop->argp->afile) == 0 &&
4381               (e.iop == &iostack[0] ||
4382                lseek(e.iop->argp->afile, 0L, 1) != -1)) {
4383             if (++bufid == AFID_NOBUF)
4384               bufid = AFID_ID;
4385             e.iop->argp->afid  = bufid;
4386           }
4387         }
4388
4389         e.iop->prev  = ~'\n';
4390         e.iop->peekc = 0;
4391         e.iop->xchar = 0;
4392         e.iop->nlcount = 0;
4393         if (fn == filechar || fn == linechar)
4394                 e.iop->task = XIO;
4395         else if (fn == (int(*)(struct ioarg *))gravechar || fn == (int(*)(struct ioarg *))qgravechar)
4396                 e.iop->task = XGRAVE;
4397         else
4398                 e.iop->task = XOTHER;
4399 }
4400
4401 static struct io *
4402 setbase(ip)
4403 struct io *ip;
4404 {
4405         register struct io *xp;
4406
4407         xp = e.iobase;
4408         e.iobase = ip;
4409         return(xp);
4410 }
4411
4412 /*
4413  * Input generating functions
4414  */
4415
4416 /*
4417  * Produce the characters of a string, then a newline, then EOF.
4418  */
4419 static int
4420 nlchar(ap)
4421 register struct ioarg *ap;
4422 {
4423         register int c;
4424
4425         if (ap->aword == NULL)
4426                 return(0);
4427         if ((c = *ap->aword++) == 0) {
4428                 ap->aword = NULL;
4429                 return('\n');
4430         }
4431         return(c);
4432 }
4433
4434 /*
4435  * Given a list of words, produce the characters
4436  * in them, with a space after each word.
4437  */
4438 static int
4439 wdchar(ap)
4440 register struct ioarg *ap;
4441 {
4442         register char c;
4443         register char **wl;
4444
4445         if ((wl = ap->awordlist) == NULL)
4446                 return(0);
4447         if (*wl != NULL) {
4448                 if ((c = *(*wl)++) != 0)
4449                         return(c & 0177);
4450                 ap->awordlist++;
4451                 return(' ');
4452         }
4453         ap->awordlist = NULL;
4454         return('\n');
4455 }
4456
4457 /*
4458  * Return the characters of a list of words,
4459  * producing a space between them.
4460  */
4461 static int
4462 dolchar(ap)
4463 register struct ioarg *ap;
4464 {
4465         register char *wp;
4466
4467         if ((wp = *ap->awordlist++) != NULL) {
4468                 PUSHIO(aword, wp, *ap->awordlist == NULL? strchar: xxchar);
4469                 return(-1);
4470         }
4471         return(0);
4472 }
4473
4474 static int
4475 xxchar(ap)
4476 register struct ioarg *ap;
4477 {
4478         register int c;
4479
4480         if (ap->aword == NULL)
4481                 return(0);
4482         if ((c = *ap->aword++) == '\0') {
4483                 ap->aword = NULL;
4484                 return(' ');
4485         }
4486         return(c);
4487 }
4488
4489 /*
4490  * Produce the characters from a single word (string).
4491  */
4492 static int
4493 strchar(ap)
4494 register struct ioarg *ap;
4495 {
4496         register int c;
4497
4498         if (ap->aword == NULL || (c = *ap->aword++) == 0)
4499                 return(0);
4500         return(c);
4501 }
4502
4503 /*
4504  * Produce quoted characters from a single word (string).
4505  */
4506 static int
4507 qstrchar(ap)
4508 register struct ioarg *ap;
4509 {
4510         register int c;
4511
4512         if (ap->aword == NULL || (c = *ap->aword++) == 0)
4513                 return(0);
4514         return(c|QUOTE);
4515 }
4516
4517 /*
4518  * Return the characters from a file.
4519  */
4520 static int
4521 filechar(ap)
4522 register struct ioarg *ap;
4523 {
4524         register int i;
4525         char c;
4526         struct iobuf *bp = ap->afbuf;
4527
4528         if (ap->afid != AFID_NOBUF) {
4529           if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
4530             if (i)
4531               lseek(ap->afile, ap->afpos, 0);
4532             i = safe_read(ap->afile, bp->buf, sizeof(bp->buf));
4533             if (i <= 0) {
4534               closef(ap->afile);
4535               return 0;
4536             }
4537             bp->id = ap->afid;
4538             bp->ebufp = (bp->bufp  = bp->buf) + i;
4539           }
4540           ap->afpos++;
4541           return *bp->bufp++ & 0177;
4542         }
4543
4544 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4545         if (interactive && isatty(ap->afile)) {
4546             static char mycommand[BUFSIZ];
4547             static int position = 0, size = 0;
4548
4549             while (size == 0 || position >= size) {
4550                 cmdedit_read_input(current_prompt, mycommand);
4551                 size = strlen(mycommand);
4552                 position = 0;
4553             }
4554             c = mycommand[position];
4555             position++;
4556             return(c);
4557         } else
4558 #endif
4559         {
4560                 i = safe_read(ap->afile, &c, sizeof(c));
4561                 return(i == sizeof(c)? c&0177: (closef(ap->afile), 0));
4562         }
4563 }
4564
4565 /*
4566  * Return the characters from a here temp file.
4567  */
4568 static int
4569 herechar(ap)
4570 register struct ioarg *ap;
4571 {
4572         char c;
4573
4574
4575         if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
4576                 close(ap->afile);
4577                 c = 0;
4578         }
4579         return (c);
4580
4581 }
4582
4583 /*
4584  * Return the characters produced by a process (`...`).
4585  * Quote them if required, and remove any trailing newline characters.
4586  */
4587 static int
4588 gravechar(ap, iop)
4589 struct ioarg *ap;
4590 struct io *iop;
4591 {
4592         register int c;
4593
4594         if ((c = qgravechar(ap, iop)&~QUOTE) == '\n')
4595                 c = ' ';
4596         return(c);
4597 }
4598
4599 static int
4600 qgravechar(ap, iop)
4601 register struct ioarg *ap;
4602 struct io *iop;
4603 {
4604         register int c;
4605
4606         if (iop->xchar) {
4607                 if (iop->nlcount) {
4608                         iop->nlcount--;
4609                         return('\n'|QUOTE);
4610                 }
4611                 c = iop->xchar;
4612                 iop->xchar = 0;
4613         } else if ((c = filechar(ap)) == '\n') {
4614                 iop->nlcount = 1;
4615                 while ((c = filechar(ap)) == '\n')
4616                         iop->nlcount++;
4617                 iop->xchar = c;
4618                 if (c == 0)
4619                         return(c);
4620                 iop->nlcount--;
4621                 c = '\n';
4622         }
4623         return(c!=0? c|QUOTE: 0);
4624 }
4625
4626 /*
4627  * Return a single command (usually the first line) from a file.
4628  */
4629 static int
4630 linechar(ap)
4631 register struct ioarg *ap;
4632 {
4633         register int c;
4634
4635         if ((c = filechar(ap)) == '\n') {
4636                 if (!multiline) {
4637                         closef(ap->afile);
4638                         ap->afile = -1; /* illegal value */
4639                 }
4640         }
4641         return(c);
4642 }
4643
4644 static void
4645 prs(s)
4646 register char *s;
4647 {
4648         if (*s)
4649                 write(2, s, strlen(s));
4650 }
4651
4652 static void
4653 prn(u)
4654 unsigned u;
4655 {
4656         prs(itoa(u));
4657 }
4658
4659 static void
4660 closef(i)
4661 register int i;
4662 {
4663         if (i > 2)
4664                 close(i);
4665 }
4666
4667 static void
4668 closeall()
4669 {
4670         register int u;
4671
4672         for (u=NUFILE; u<NOFILE;)
4673                 close(u++);
4674 }
4675
4676 /*
4677  * remap fd into Shell's fd space
4678  */
4679 static int
4680 remap(fd)
4681 register int fd;
4682 {
4683         register int i;
4684         int map[NOFILE];
4685
4686         if (fd < e.iofd) {
4687                 for (i=0; i<NOFILE; i++)
4688                         map[i] = 0;
4689                 do {
4690                         map[fd] = 1;
4691                         fd = dup(fd);
4692                 } while (fd >= 0 && fd < e.iofd);
4693                 for (i=0; i<NOFILE; i++)
4694                         if (map[i])
4695                                 close(i);
4696                 if (fd < 0)
4697                         err("too many files open in shell");
4698         }
4699         return(fd);
4700 }
4701
4702 static int
4703 openpipe(pv)
4704 register int *pv;
4705 {
4706         register int i;
4707
4708         if ((i = pipe(pv)) < 0)
4709                 err("can't create pipe - try again");
4710         return(i);
4711 }
4712
4713 static void
4714 closepipe(pv)
4715 register int *pv;
4716 {
4717         if (pv != NULL) {
4718                 close(*pv++);
4719                 close(*pv);
4720         }
4721 }
4722
4723 /* -------- here.c -------- */
4724
4725 /*
4726  * here documents
4727  */
4728
4729 static void
4730 markhere(s, iop)
4731 register char *s;
4732 struct ioword *iop;
4733 {
4734         register struct here *h, *lh;
4735
4736         h = (struct here *) space(sizeof(struct here));
4737         if (h == 0)
4738                 return;
4739         h->h_tag = evalstr(s, DOSUB);
4740         if (h->h_tag == 0)
4741                 return;
4742         h->h_iop = iop;
4743         iop->io_name = 0;
4744         h->h_next = NULL;
4745         if (inhere == 0)
4746                 inhere = h;
4747         else
4748                 for (lh = inhere; lh!=NULL; lh = lh->h_next)
4749                         if (lh->h_next == 0) {
4750                                 lh->h_next = h;
4751                                 break;
4752                         }
4753         iop->io_flag |= IOHERE|IOXHERE;
4754         for (s = h->h_tag; *s; s++)
4755                 if (*s & QUOTE) {
4756                         iop->io_flag &= ~ IOXHERE;
4757                         *s &= ~ QUOTE;
4758                 }
4759         h->h_dosub = iop->io_flag & IOXHERE;
4760 }
4761
4762 static void
4763 gethere()
4764 {
4765         register struct here *h, *hp;
4766
4767         /* Scan here files first leaving inhere list in place */
4768         for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
4769           readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub? 0: '\'');
4770
4771         /* Make inhere list active - keep list intact for scraphere */
4772         if (hp != NULL) {
4773           hp->h_next = acthere;
4774           acthere    = inhere;
4775           inhere     = NULL;
4776         }
4777 }
4778
4779 static void
4780 readhere(name, s, ec)
4781 char **name;
4782 register char *s;
4783 int ec;
4784 {
4785         int tf;
4786         char tname[30] = ".msh_XXXXXX";
4787         register int c;
4788         jmp_buf ev;
4789         char myline [LINELIM+1];
4790         char *thenext;
4791
4792         tf = mkstemp(tname);
4793         if (tf < 0)
4794                 return;
4795         *name = strsave(tname, areanum);
4796         if (newenv(setjmp(errpt = ev)) != 0)
4797                 unlink(tname);
4798         else {
4799                 pushio(e.iop->argp, (int(*)(struct ioarg *))e.iop->iofn);
4800                 e.iobase = e.iop;
4801                 for (;;) {
4802                     if (interactive && e.iop <= iostack) {
4803 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4804                             current_prompt=cprompt->value;
4805 #else
4806                             prs(cprompt->value);
4807 #endif
4808                         }
4809                         thenext = myline;
4810                         while ((c = my_getc(ec)) != '\n' && c) {
4811                                 if (ec == '\'')
4812                                         c &= ~ QUOTE;
4813                                 if (thenext >= &myline[LINELIM]) {
4814                                         c = 0;
4815                                         break;
4816                                 }
4817                                 *thenext++ = c;
4818                         }
4819                         *thenext = 0;
4820                         if (strcmp(s, myline) == 0 || c == 0)
4821                                 break;
4822                         *thenext++ = '\n';
4823                         write (tf, myline, (int)(thenext-myline));
4824                 }
4825                 if (c == 0) {
4826                         prs("here document `"); prs(s); err("' unclosed");
4827                 }
4828                 quitenv();
4829         }
4830         close(tf);
4831 }
4832
4833 /*
4834  * open here temp file.
4835  * if unquoted here, expand here temp file into second temp file.
4836  */
4837 static int
4838 herein(hname, xdoll)
4839 char *hname;
4840 int xdoll;
4841 {
4842         register int hf;
4843         int tf;
4844
4845 #if __GNUC__
4846         /* Avoid longjmp clobbering */
4847         (void) &tf;
4848 #endif
4849         if (hname == 0)
4850                 return(-1);
4851         hf = open(hname, 0);
4852         if (hf < 0)
4853                 return (-1);
4854         if (xdoll) {
4855                 char c;
4856                 char tname[30] = ".msh_XXXXXX";
4857                 jmp_buf ev;
4858
4859                 tf = mkstemp(tname);
4860                 if (tf < 0)
4861                         return (-1);
4862                 if (newenv(setjmp(errpt = ev)) == 0) {
4863                         PUSHIO(afile, hf, herechar);
4864                         setbase(e.iop);
4865                         while ((c = subgetc(0, 0)) != 0) {
4866                                 c &= ~ QUOTE;
4867                                 write(tf, &c, sizeof c);
4868                         }
4869                         quitenv();
4870                 } else
4871                         unlink(tname);
4872                 close(tf);
4873                 tf = open(tname, 0);
4874                 unlink(tname);
4875                 return (tf);
4876         } else
4877                 return (hf);
4878 }
4879
4880 static void
4881 scraphere()
4882 {
4883         register struct here *h;
4884
4885         for (h = inhere; h != NULL; h = h->h_next) {
4886                 if (h->h_iop && h->h_iop->io_name)
4887                   unlink(h->h_iop->io_name);
4888         }
4889         inhere = NULL;
4890 }
4891
4892 /* unlink here temp files before a freearea(area) */
4893 static void
4894 freehere(area)
4895 int area;
4896 {
4897         register struct here *h, *hl;
4898
4899         hl = NULL;
4900         for (h = acthere; h != NULL; h = h->h_next)
4901                 if (getarea((char *) h) >= area) {
4902                         if (h->h_iop->io_name != NULL)
4903                                 unlink(h->h_iop->io_name);
4904                         if (hl == NULL)
4905                                 acthere = h->h_next;
4906                         else
4907                                 hl->h_next = h->h_next;
4908                 } else
4909                         hl = h;
4910 }
4911
4912
4913
4914 /*
4915  * Copyright (c) 1987,1997, Prentice Hall
4916  * All rights reserved.
4917  *
4918  * Redistribution and use of the MINIX operating system in source and
4919  * binary forms, with or without modification, are permitted provided
4920  * that the following conditions are met:
4921  *
4922  * Redistributions of source code must retain the above copyright
4923  * notice, this list of conditions and the following disclaimer.
4924  *
4925  * Redistributions in binary form must reproduce the above
4926  * copyright notice, this list of conditions and the following
4927  * disclaimer in the documentation and/or other materials provided
4928  * with the distribution.
4929  *
4930  * Neither the name of Prentice Hall nor the names of the software
4931  * authors or contributors may be used to endorse or promote
4932  * products derived from this software without specific prior
4933  * written permission.
4934  *
4935  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
4936  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
4937  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4938  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4939  * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
4940  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4941  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4942  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
4943  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4944  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
4945  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
4946  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4947  *
4948  */
4949