1 /* vi: set sw=4 ts=4: */
3 * Minix shell port for busybox
5 * This version of the Minix shell was adapted for use in busybox
6 * by Erik Andersen <andersen@codepoet.org>
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>
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.
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.
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
27 * Original copyright notice is retained at the end of this file.
44 #include <sys/times.h>
45 #include <sys/types.h>
52 /* Conditional use of "register" keyword */
53 #define REGISTER register
56 /*#define MSHDEBUG 1*/
61 #define DBGPRINTF(x) if(mshdbg>0)printf x
62 #define DBGPRINTF0(x) if(mshdbg>0)printf x
63 #define DBGPRINTF1(x) if(mshdbg>1)printf x
64 #define DBGPRINTF2(x) if(mshdbg>2)printf x
65 #define DBGPRINTF3(x) if(mshdbg>3)printf x
66 #define DBGPRINTF4(x) if(mshdbg>4)printf x
67 #define DBGPRINTF5(x) if(mshdbg>5)printf x
68 #define DBGPRINTF6(x) if(mshdbg>6)printf x
69 #define DBGPRINTF7(x) if(mshdbg>7)printf x
70 #define DBGPRINTF8(x) if(mshdbg>8)printf x
71 #define DBGPRINTF9(x) if(mshdbg>9)printf x
75 #define RCPRINTF(x) if(mshdbg_rc)printf x
96 /* -------- sh.h -------- */
102 #define NPUSH 8 /* limit to input nesting */
105 #define NOFILE 20 /* Number of open files */
106 #define NUFILE 10 /* Number of user-accessible files */
107 #define FDBASE 10 /* First file usable by Shell */
110 * values returned by wait
112 #define WAITSIG(s) ((s)&0177)
113 #define WAITVAL(s) (((s)>>8)&0377)
114 #define WAITCORE(s) (((s)&0200)!=0)
117 * library and system definitions
119 typedef void xint; /* base type of jmp_buf, for not broken compilers */
127 #define NOBLOCK ((struct op *)NULL)
128 #define NOWORD ((char *)NULL)
129 #define NOWORDS ((char **)NULL)
130 #define NOPIPE ((int *)NULL)
133 * Description of a command or an operation on commands.
134 * Might eventually use a union.
137 int type; /* operation type, see below */
138 char **words; /* arguments to a command */
139 struct ioword **ioact; /* IO actions (eg, < > >>) */
142 char *str; /* identifier for case and for */
145 #define TCOM 1 /* command */
146 #define TPAREN 2 /* (c-list) */
147 #define TPIPE 3 /* a | b */
148 #define TLIST 4 /* a [&;] b */
149 #define TOR 5 /* || */
150 #define TAND 6 /* && */
158 #define TPAT 14 /* pattern in case */
159 #define TBRACE 15 /* {c-list} */
160 #define TASYNC 16 /* c & */
161 /* Added to support "." file expansion */
164 /* Strings for names to make debug easier */
165 char *T_CMD_NAMES[] = {
188 * actions determining the environment of a process
190 #define BIT(i) (1<<(i))
191 #define FEXEC BIT(0) /* execute without forking */
193 #if 0 /* Original value */
194 #define AREASIZE (65000)
196 #define AREASIZE (90000)
200 * flags to control evaluation of words
202 #define DOSUB 1 /* interpret $, `, and quotes */
203 #define DOBLANK 2 /* perform blank interpretation */
204 #define DOGLOB 4 /* interpret [?* */
205 #define DOKEY 8 /* move words with `=' to 2nd arg. list */
206 #define DOTRIM 16 /* trim resulting string */
208 #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
212 static int newfile(char *s);
213 static char *findeq(char *cp);
214 static char *cclass(char *p, int sub);
215 static void initarea(void);
216 extern int msh_main(int argc, char **argv);
221 struct brkcon *nextlev;
229 short io_unit; /* unit affected */
230 short io_flag; /* action (below) */
231 char *io_name; /* file name */
234 #define IOREAD 1 /* < */
235 #define IOHERE 2 /* << (here file) */
236 #define IOWRITE 4 /* > */
237 #define IOCAT 8 /* >> */
238 #define IOXHERE 16 /* ${}, ` in << */
239 #define IODUP 32 /* >&digit */
240 #define IOCLOSE 64 /* >&- */
242 #define IODEFAULT (-1) /* token for default IO unit */
247 * parsing & execution environment
253 xint *errpt; /* void * */
261 * -k: look for name=value everywhere on command line
263 * -t: exit after reading and executing one command
266 * -u: unset variables net diagnostic
270 static char *null; /* null value for variable */
271 static int intr; /* interrupt pending */
273 static char *trap[_NSIG + 1];
274 static char ourtrap[_NSIG + 1];
275 static int trapset; /* trap pending */
277 static int heedint; /* heed interrupt signals */
279 static int yynerrs; /* yacc */
281 static char line[LINELIM];
288 static int (*inbuilt(char *s)) (struct op *);
290 static char *rexecve(char *c, char **v, char **envp);
291 static char *space(int n);
292 static char *strsave(char *s, int a);
293 static char *evalstr(char *cp, int f);
294 static char *putn(int n);
295 static char *itoa(int n);
296 static char *unquote(char *as);
297 static struct var *lookup(char *n);
298 static int rlookup(char *n);
299 static struct wdblock *glob(char *cp, struct wdblock *wb);
300 static int my_getc(int ec);
301 static int subgetc(int ec, int quoted);
302 static char **makenv(int all, struct wdblock *wb);
303 static char **eval(char **ap, int f);
304 static int setstatus(int s);
305 static int waitfor(int lastpid, int canintr);
307 static void onintr(int s); /* SIGINT handler */
309 static int newenv(int f);
310 static void quitenv(void);
311 static void err(char *s);
312 static int anys(char *s1, char *s2);
313 static int any(int c, char *s);
314 static void next(int f);
315 static void setdash(void);
316 static void onecommand(void);
317 static void runtrap(int i);
318 static int gmatch(char *s, char *p);
324 static void leave(void); /* abort shell (or fail in subshell) */
325 static void fail(void); /* fail but return to process next command */
326 static void warn(char *s);
327 static void sig(int i); /* default signal handler */
331 /* -------- area stuff -------- */
333 #define REGSIZE sizeof(struct region)
335 /* #define SHRINKBY (64) */
339 #define ALIGN (sizeof(int)-1)
349 /* -------- grammar stuff -------- */
374 /* Added for "." file expansion */
377 #define YYERRCODE 300
380 #define CONTIN 01 /* skip new lines to complete command */
382 #define SYNTAXERR zzerr()
384 static struct op *pipeline(int cf);
385 static struct op *andor(void);
386 static struct op *c_list(void);
387 static int synio(int cf);
388 static void musthave(int c, int cf);
389 static struct op *simple(void);
390 static struct op *nested(int type, int mark);
391 static struct op *command(int cf);
392 static struct op *dogroup(int onlydone);
393 static struct op *thenpart(void);
394 static struct op *elsepart(void);
395 static struct op *caselist(void);
396 static struct op *casepart(void);
397 static char **pattern(void);
398 static char **wordlist(void);
399 static struct op *list(struct op *t1, struct op *t2);
400 static struct op *block(int type, struct op *t1, struct op *t2, char **wp);
401 static struct op *newtp(void);
402 static struct op *namelist(struct op *t);
403 static char **copyw(void);
404 static void word(char *cp);
405 static struct ioword **copyio(void);
406 static struct ioword *io(int u, int f, char *cp);
407 static void zzerr(void);
408 static void yyerror(char *s);
409 static int yylex(int cf);
410 static int collect(int c, int c1);
411 static int dual(int c);
412 static void diag(int ec);
413 static char *tree(unsigned size);
415 /* -------- var.h -------- */
424 #define COPYV 1 /* flag to setval, suggesting copy */
425 #define RONLY 01 /* variable is read-only */
426 #define EXPORT 02 /* variable is to be exported */
427 #define GETCELL 04 /* name & value space was got with getcell */
429 static int yyparse(void);
430 static struct var *lookup(char *n);
431 static void setval(struct var *vp, char *val);
432 static void nameval(struct var *vp, char *val, char *name);
433 static void export(struct var *vp);
434 static void ronly(struct var *vp);
435 static int isassign(char *s);
436 static int checkname(char *cp);
437 static int assign(char *s, int cf);
438 static void putvlist(int f, int out);
439 static int eqname(char *n1, char *n2);
441 static int execute(struct op *t, int *pin, int *pout, int act);
444 /* -------- io.h -------- */
447 unsigned id; /* buffer id */
448 char buf[512]; /* buffer */
449 char *bufp; /* pointer into buffer */
450 char *ebufp; /* pointer to end of buffer */
453 /* possible arguments to an IO function */
457 int afile; /* file descriptor */
458 unsigned afid; /* buffer id */
459 long afpos; /* file position */
460 struct iobuf *afbuf; /* buffer for this file */
463 //static struct ioarg ioargstack[NPUSH];
464 #define AFID_NOBUF (~0)
467 /* an input generator's state */
469 int (*iofn) (struct ioarg *, struct io *);
472 char prev; /* previous character read by readc() */
473 char nlcount; /* for `'s */
474 char xchar; /* for `'s */
475 char task; /* reason for pushed IO */
478 //static struct io iostack[NPUSH];
479 #define XOTHER 0 /* none of the below */
480 #define XDOLL 1 /* expanding ${} */
481 #define XGRAVE 2 /* expanding `'s */
482 #define XIO 3 /* file IO */
484 /* in substitution */
485 #define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL)
489 * input generators for IO structure
491 static int nlchar(struct ioarg *ap);
492 static int strchar(struct ioarg *ap);
493 static int qstrchar(struct ioarg *ap);
494 static int filechar(struct ioarg *ap);
495 static int herechar(struct ioarg *ap);
496 static int linechar(struct ioarg *ap);
497 static int gravechar(struct ioarg *ap, struct io *iop);
498 static int qgravechar(struct ioarg *ap, struct io *iop);
499 static int dolchar(struct ioarg *ap);
500 static int wdchar(struct ioarg *ap);
501 static void scraphere(void);
502 static void freehere(int area);
503 static void gethere(void);
504 static void markhere(char *s, struct ioword *iop);
505 static int herein(char *hname, int xdoll);
506 static int run(struct ioarg *argp, int (*f) (struct ioarg *));
512 static int eofc(void);
513 static int readc(void);
514 static void unget(int c);
515 static void ioecho(int c);
516 static void prs(char *s);
517 static void prn(unsigned u);
518 static void closef(int i);
519 static void closeall(void);
525 static void pushio(struct ioarg *argp, int (*f) (struct ioarg *));
526 static int remap(int fd);
527 static int openpipe(int *pv);
528 static void closepipe(int *pv);
529 static struct io *setbase(struct io *ip);
531 #define PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen)))
532 #define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen)))
534 /* -------- word.h -------- */
536 #define NSTART 16 /* default number of words to allow for initially */
541 /* bounds are arbitrary */
545 static struct wdblock *addword(char *wd, struct wdblock *wb);
546 static struct wdblock *newword(int nw);
547 static char **getwords(struct wdblock *wb);
549 /* -------- area.h -------- */
554 static char *getcell(unsigned nbytes);
555 static void garbage(void);
556 static void setarea(char *cp, int a);
557 static int getarea(char *cp);
558 static void freearea(int a);
559 static void freecell(char *cp);
560 static int areanum; /* current allocation area */
562 #define NEW(type) (type *)getcell(sizeof(type))
563 #define DELETE(obj) freecell((char *)obj)
566 /* -------- misc stuff -------- */
568 static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp);
569 static int iosetup(struct ioword *iop, int pipein, int pipeout);
570 static void echo(char **wp);
571 static struct op **find1case(struct op *t, char *w);
572 static struct op *findcase(struct op *t, char *w);
573 static void brkset(struct brkcon *bc);
574 static int dolabel(struct op *t);
575 static int dohelp(struct op *t);
576 static int dochdir(struct op *t);
577 static int doshift(struct op *t);
578 static int dologin(struct op *t);
579 static int doumask(struct op *t);
580 static int doexec(struct op *t);
581 static int dodot(struct op *t);
582 static int dowait(struct op *t);
583 static int doread(struct op *t);
584 static int doeval(struct op *t);
585 static int dotrap(struct op *t);
586 static int getsig(char *s);
587 static void setsig(int n, sighandler_t f);
588 static int getn(char *as);
589 static int dobreak(struct op *t);
590 static int docontinue(struct op *t);
591 static int brkcontin(char *cp, int val);
592 static int doexit(struct op *t);
593 static int doexport(struct op *t);
594 static int doreadonly(struct op *t);
595 static void rdexp(char **wp, void (*f) (struct var *), int key);
596 static void badid(char *s);
597 static int doset(struct op *t);
598 static void varput(char *s, int out);
599 static int dotimes(struct op *t);
600 static int expand(char *cp, struct wdblock **wbp, int f);
601 static char *blank(int f);
602 static int dollar(int quoted);
603 static int grave(int quoted);
604 static void globname(char *we, char *pp);
605 static char *generate(char *start1, char *end1, char *middle, char *end);
606 static int anyspcl(struct wdblock *wb);
607 static int xstrcmp(char *p1, char *p2);
608 static void glob0(char *a0, unsigned int a1, int a2,
609 int (*a3) (char *, char *));
610 static void glob1(char *base, char *lim);
611 static void glob2(char *i, char *j);
612 static void glob3(char *i, char *j, char *k);
613 static void readhere(char **name, char *s, int ec);
614 static void pushio(struct ioarg *argp, int (*f) (struct ioarg *));
615 static int xxchar(struct ioarg *ap);
620 struct ioword *h_iop;
624 static char *signame[] = {
627 (char *) NULL, /* interrupt */
629 "Illegal instruction",
633 "Floating Point Exception",
638 (char *) NULL, /* broken pipe */
643 #define NSIGNAL (sizeof(signame)/sizeof(signame[0]))
649 static struct res restab[] = {
675 int (*builtinfunc) (struct op * t);
677 static const struct builtincmd builtincmds[] = {
682 {"continue", docontinue},
686 {"export", doexport},
691 {"readonly", doreadonly},
701 struct op *scantree(struct op *);
702 static struct op *dowholefile(int, int);
705 extern char **environ; /* environment pointer */
711 static int interactive = 0; /* Is this an interactive shell */
713 static int multiline; /* \n changed to ; */
714 static struct op *outtree; /* result from parser */
717 static struct brkcon *brklist;
719 static struct wdblock *wdlist;
720 static struct wdblock *iolist;
721 static char *trap[_NSIG + 1];
722 static char ourtrap[_NSIG + 1];
723 static int trapset; /* trap pending */
724 static int yynerrs; /* yacc */
725 static char line[LINELIM];
728 static struct var *mshdbg_var;
730 static struct var *vlist; /* dictionary */
731 static struct var *homedir; /* home directory */
732 static struct var *prompt; /* main prompt */
733 static struct var *cprompt; /* continuation prompt */
734 static struct var *path; /* search path for commands */
735 static struct var *shell; /* shell to interpret command files */
736 static struct var *ifs; /* field separators */
738 static int areanum; /* current allocation area */
741 static char flags['z' - 'a' + 1];
742 static char *flag = flags - 'a';
743 static char *null = "";
744 static int heedint = 1;
745 static void (*qflag) (int) = SIG_IGN;
749 static int iounit = IODEFAULT;
750 static YYSTYPE yylval;
751 static char *elinep = line + sizeof(line) - 5;
753 static struct ioarg temparg = { 0, 0, 0, AFID_NOBUF, 0 }; /* temporary for PUSHIO */
754 static struct ioarg ioargstack[NPUSH];
755 static struct io iostack[NPUSH];
756 static struct iobuf sharedbuf = { AFID_NOBUF };
757 static struct iobuf mainbuf = { AFID_NOBUF };
758 static unsigned bufid = AFID_ID; /* buffer id counter */
760 static struct here *inhere; /* list of hear docs while parsing */
761 static struct here *acthere; /* list of active here documents */
762 static struct region *areabot; /* bottom of area */
763 static struct region *areatop; /* top of area */
764 static struct region *areanxt; /* starting point of scan */
766 static void *brkaddr;
768 static struct env e = {
769 line, /* linep: char ptr */
770 iostack, /* iobase: struct io ptr */
771 iostack - 1, /* iop: struct io ptr */
772 (xint *) NULL, /* errpt: void ptr for errors? */
773 FDBASE, /* iofd: file desc */
774 (struct env *) NULL /* oenv: struct env ptr */
778 void print_t(struct op *t)
780 DBGPRINTF(("T: t=0x%x, type %s, words=0x%x, IOword=0x%x\n", t,
781 T_CMD_NAMES[t->type], t->words, t->ioact));
784 DBGPRINTF(("T: W1: %s", t->words[0]));
790 void print_tree(struct op *head)
793 DBGPRINTF(("PRINT_TREE: no tree\n"));
797 DBGPRINTF(("NODE: 0x%x, left 0x%x, right 0x%x\n", head, head->left,
801 print_tree(head->left);
804 print_tree(head->right);
808 #endif /* MSHDEBUG */
811 #ifdef CONFIG_FEATURE_COMMAND_EDITING
812 static char *current_prompt;
815 /* -------- sh.c -------- */
821 extern int msh_main(int argc, char **argv)
827 int (*iof) (struct ioarg *);
829 DBGPRINTF(("MSH_MAIN: argc %d, environ 0x%x\n", argc, environ));
832 if ((ap = environ) != NULL) {
834 assign(*ap++, !COPYV);
835 for (ap = environ; *ap;)
836 export(lookup(*ap++));
841 shell = lookup("SHELL");
842 if (shell->value == null)
843 setval(shell, (char *)DEFAULT_SHELL);
846 homedir = lookup("HOME");
847 if (homedir->value == null)
848 setval(homedir, "/");
851 setval(lookup("$"), putn(getpid()));
853 path = lookup("PATH");
854 if (path->value == null) {
856 setval(path, "/sbin:/bin:/usr/sbin:/usr/bin");
858 setval(path, "/bin:/usr/bin");
863 if (ifs->value == null)
864 setval(ifs, " \t\n");
867 mshdbg_var = lookup("MSHDEBUG");
868 if (mshdbg_var->value == null)
869 setval(mshdbg_var, "0");
873 prompt = lookup("PS1");
874 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
875 if (prompt->value == null)
877 setval(prompt, "$ ");
878 if (geteuid() == 0) {
879 setval(prompt, "# ");
880 prompt->status &= ~EXPORT;
882 cprompt = lookup("PS2");
883 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
884 if (cprompt->value == null)
886 setval(cprompt, "> ");
892 if (argv[0][0] == '-' && argv[0][1] != '\0') {
893 for (s = argv[0] + 1; *s; s++)
896 prompt->status &= ~EXPORT;
897 cprompt->status &= ~EXPORT;
902 PUSHIO(aword, *++argv, iof = nlchar);
914 prompt->status &= ~EXPORT;
922 if (*s >= 'a' && *s <= 'z')
930 if (iof == filechar && --argc > 0) {
933 prompt->status &= ~EXPORT;
934 cprompt->status &= ~EXPORT;
936 /* Shell is non-interactive, activate printf-based debug */
938 mshdbg = (int) (((char) (mshdbg_var->value[0])) - '0');
942 DBGPRINTF(("MSH_MAIN: calling newfile()\n"));
944 if (newfile(name = *++argv))
945 exit(1); /* Exit on error */
951 /* This won't be true if PUSHIO has been called, say from newfile() above */
952 if (e.iop < iostack) {
953 PUSHIO(afile, 0, iof);
954 if (isatty(0) && isatty(1) && !cflag) {
956 #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
958 printf("\n\n" BB_BANNER " Built-in shell (msh with debug)\n");
960 printf("\n\n" BB_BANNER " Built-in shell (msh)\n");
962 printf("Enter 'help' for a list of built-in commands.\n\n");
967 signal(SIGQUIT, qflag);
968 if (name && name[0] == '-') {
970 if ((f = open(".profile", 0)) >= 0)
972 if ((f = open("/etc/profile", 0)) >= 0)
976 signal(SIGTERM, sig);
978 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
979 signal(SIGINT, onintr);
984 for (ap = ++argv; --argc > 0;) {
985 if (assign(*ap = *argv++, !COPYV)) {
986 dolc--; /* keyword */
992 setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
994 DBGPRINTF(("MSH_MAIN: begin FOR loop, interactive %d, e.iop 0x%x, iostack 0x%x\n", interactive, e.iop, iostack));
997 if (interactive && e.iop <= iostack) {
998 #ifdef CONFIG_FEATURE_COMMAND_EDITING
999 current_prompt = prompt->value;
1005 /* Ensure that getenv("PATH") stays current */
1006 setenv("PATH", path->value, 1);
1009 DBGPRINTF(("MSH_MAIN: returning.\n"));
1012 static void setdash()
1016 char m['z' - 'a' + 1];
1019 for (c = 'a'; c <= 'z'; c++)
1023 setval(lookup("-"), m);
1026 static int newfile(s)
1031 DBGPRINTF7(("NEWFILE: opening %s\n", s));
1033 if (strcmp(s, "-") != 0) {
1034 DBGPRINTF(("NEWFILE: s is %s\n", s));
1038 err(": cannot open");
1049 struct op *scantree(head)
1057 if (head->left != NULL) {
1058 dotnode = scantree(head->left);
1063 if (head->right != NULL) {
1064 dotnode = scantree(head->right);
1069 if (head->words == NULL)
1072 DBGPRINTF5(("SCANTREE: checking node 0x%x\n", head));
1074 if ((head->type != TDOT) && (strcmp(".", head->words[0]) == 0)) {
1075 DBGPRINTF5(("SCANTREE: dot found in node 0x%x\n", head));
1083 static void onecommand()
1088 DBGPRINTF(("ONECOMMAND: enter, outtree=0x%x\n", outtree));
1107 setjmp(failpt = m1); /* Bruce Evans' fix */
1108 if (setjmp(failpt = m1) || yyparse() || intr) {
1110 DBGPRINTF(("ONECOMMAND: this is not good.\n"));
1115 if (!interactive && intr)
1128 DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=0x%x\n",
1130 execute(outtree, NOPIPE, NOPIPE, 0);
1133 if (!interactive && intr) {
1138 if ((i = trapset) != 0) {
1152 DBGPRINTF(("LEAVE: leave called!\n"));
1184 longjmp(e.errpt, 1);
1186 e.iop = e.iobase = iostack;
1189 static int newenv(f)
1192 REGISTER struct env *ep;
1194 DBGPRINTF(("NEWENV: f=%d (indicates quitenv and return)\n", f));
1201 ep = (struct env *) space(sizeof(*ep));
1214 static void quitenv()
1216 REGISTER struct env *ep;
1219 DBGPRINTF(("QUITENV: e.oenv=0x%x\n", e.oenv));
1221 if ((ep = e.oenv) != NULL) {
1224 /* should close `'d files */
1226 while (--fd >= e.iofd)
1232 * Is any character from s1 in s2?
1234 static int anys(s1, s2)
1235 REGISTER char *s1, *s2;
1244 * Is character c in s?
1246 static int any(c, s)
1256 static char *putn(n)
1262 static char *itoa(n)
1267 snprintf(s, sizeof(s), "%u", n);
1272 static void next(int f)
1274 PUSHIO(afile, f, filechar);
1277 static void onintr(s)
1278 int s; /* ANSI C requires a parameter */
1280 signal(SIGINT, onintr);
1287 } else if (heedint) {
1293 static char *space(n)
1298 if ((cp = getcell(n)) == 0)
1299 err("out of string space");
1303 static char *strsave(s, a)
1307 REGISTER char *cp, *xp;
1309 if ((cp = space(strlen(s) + 1)) != NULL) {
1310 setarea((char *) cp, a);
1311 for (xp = cp; (*xp++ = *s++) != '\0';);
1327 static void runtrap(i)
1332 if ((trapstr = trap[i]) == NULL)
1338 RUN(aword, trapstr, nlchar);
1341 /* -------- var.c -------- */
1344 * Find the given name in the dictionary
1345 * and return its value. If the name was
1346 * not previously there, enter it now and
1347 * return a null value.
1349 static struct var *lookup(n)
1352 REGISTER struct var *vp;
1355 static struct var dummy;
1359 for (c = 0; isdigit(*n) && c < 1000; n++)
1360 c = c * 10 + *n - '0';
1361 dummy.status = RONLY;
1362 dummy.value = c <= dolc ? dolv[c] : null;
1365 for (vp = vlist; vp; vp = vp->next)
1366 if (eqname(vp->name, n))
1369 vp = (struct var *) space(sizeof(*vp));
1370 if (vp == 0 || (vp->name = space((int) (cp - n) + 2)) == 0) {
1371 dummy.name = dummy.value = "";
1374 for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++);
1378 setarea((char *) vp, 0);
1379 setarea((char *) vp->name, 0);
1382 vp->status = GETCELL;
1388 * give variable at `vp' the value `val'.
1390 static void setval(vp, val)
1394 nameval(vp, val, (char *) NULL);
1398 * if name is not NULL, it must be
1399 * a prefix of the space `val',
1401 * this is all so that exporting
1402 * values is reasonably painless.
1404 static void nameval(vp, val, name)
1405 REGISTER struct var *vp;
1408 REGISTER char *cp, *xp;
1412 if (vp->status & RONLY) {
1413 for (xp = vp->name; *xp && *xp != '=';)
1414 putc(*xp++, stderr);
1415 err(" is read-only");
1420 xp = space(strlen(vp->name) + strlen(val) + 2);
1423 /* make string: name=value */
1424 setarea((char *) xp, 0);
1426 for (cp = vp->name; (*xp = *cp++) && *xp != '='; xp++);
1430 for (cp = val; (*xp++ = *cp++) != '\0';);
1434 if (vp->status & GETCELL)
1435 freecell(vp->name); /* form new string `name=value' */
1441 static void export(vp)
1444 vp->status |= EXPORT;
1447 static void ronly(vp)
1450 if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */
1451 vp->status |= RONLY;
1454 static int isassign(s)
1457 DBGPRINTF7(("ISASSIGN: enter, s=%s\n", s));
1459 if (!isalpha((int) *s) && *s != '_')
1461 for (; *s != '='; s++)
1462 if (*s == 0 || (!isalnum(*s) && *s != '_'))
1468 static int assign(s, cf)
1475 DBGPRINTF7(("ASSIGN: enter, s=%s, cf=%d\n", s, cf));
1477 if (!isalpha(*s) && *s != '_')
1479 for (cp = s; *cp != '='; cp++)
1480 if (*cp == 0 || (!isalnum(*cp) && *cp != '_'))
1483 nameval(vp, ++cp, cf == COPYV ? (char *) NULL : s);
1485 vp->status &= ~GETCELL;
1489 static int checkname(cp)
1492 DBGPRINTF7(("CHECKNAME: enter, cp=%s\n", cp));
1494 if (!isalpha(*cp++) && *(cp - 1) != '_')
1497 if (!isalnum(*cp++) && *(cp - 1) != '_')
1502 static void putvlist(f, out)
1503 REGISTER int f, out;
1505 REGISTER struct var *vp;
1507 for (vp = vlist; vp; vp = vp->next)
1508 if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) {
1509 if (vp->status & EXPORT)
1510 write(out, "export ", 7);
1511 if (vp->status & RONLY)
1512 write(out, "readonly ", 9);
1513 write(out, vp->name, (int) (findeq(vp->name) - vp->name));
1514 write(out, "\n", 1);
1518 static int eqname(n1, n2)
1519 REGISTER char *n1, *n2;
1521 for (; *n1 != '=' && *n1 != 0; n1++)
1524 return (*n2 == 0 || *n2 == '=');
1527 static char *findeq(cp)
1530 while (*cp != '\0' && *cp != '=')
1535 /* -------- gmatch.c -------- */
1537 * int gmatch(string, pattern)
1538 * char *string, *pattern;
1540 * Match a pattern as in sh(1).
1545 #define QMASK (CMASK&~QUOTE)
1546 #define NOT '!' /* might use ^ */
1548 static int gmatch(s, p)
1549 REGISTER char *s, *p;
1551 REGISTER int sc, pc;
1553 if (s == NULL || p == NULL)
1555 while ((pc = *p++ & CMASK) != '\0') {
1559 if ((p = cclass(p, sc)) == NULL)
1571 if (*p == '\0' || gmatch(s, p))
1573 } while (*s++ != '\0');
1577 if (sc != (pc & ~QUOTE))
1584 static char *cclass(p, sub)
1588 REGISTER int c, d, not, found;
1590 if ((not = *p == NOT) != 0)
1595 return ((char *) NULL);
1597 if (p[1] == '-' && p[2] != ']') {
1602 if (c == sub || (c <= sub && sub <= d))
1604 } while (*++p != ']');
1605 return (found ? p + 1 : (char *) NULL);
1609 /* -------- area.c -------- */
1612 * All memory between (char *)areabot and (char *)(areatop+1) is
1613 * exclusively administered by the area management routines.
1614 * It is assumed that sbrk() and brk() manipulate the high end.
1617 #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;})
1619 static void initarea()
1621 brkaddr = malloc(AREASIZE);
1622 brktop = brkaddr + AREASIZE;
1624 while ((int) sbrk(0) & ALIGN)
1626 areabot = (struct region *) sbrk(REGSIZE);
1628 areabot->next = areabot;
1629 areabot->area = BUSY;
1634 char *getcell(nbytes)
1637 REGISTER int nregio;
1638 REGISTER struct region *p, *q;
1645 /* silly and defeats the algorithm */
1647 * round upwards and add administration area
1649 nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1;
1650 for (p = areanxt;;) {
1651 if (p->area > areanum) {
1655 while ((q = p->next)->area > areanum && q != areanxt)
1658 * exit loop if cell big enough
1660 if (q >= p + nregio)
1667 i = nregio >= GROWBY ? nregio : GROWBY;
1668 p = (struct region *) sbrk(i * REGSIZE);
1669 if (p == (struct region *) -1)
1670 return ((char *) NULL);
1674 abort(); /* allocated areas are contiguous */
1684 * we found a FREE area big enough, pointed to by 'p', and up to 'q'
1686 areanxt = p + nregio;
1689 * split into requested area and rest
1691 if (areanxt + 1 > q) {
1693 abort(); /* insufficient space left for admin */
1696 areanxt->area = FREE;
1700 return ((char *) (p + 1));
1703 static void freecell(cp)
1706 REGISTER struct region *p;
1708 if ((p = (struct region *) cp) != NULL) {
1716 static void freearea(a)
1719 REGISTER struct region *p, *top;
1722 for (p = areabot; p != top; p = p->next)
1727 static void setarea(cp, a)
1731 REGISTER struct region *p;
1733 if ((p = (struct region *) cp) != NULL)
1740 return ((struct region *) cp - 1)->area;
1743 static void garbage()
1745 REGISTER struct region *p, *q, *top;
1748 for (p = areabot; p != top; p = p->next) {
1749 if (p->area > areanum) {
1750 while ((q = p->next)->area > areanum)
1756 if (areatop >= q + SHRINKBY && q->area > areanum) {
1757 brk((char *) (q + 1));
1765 /* -------- csyn.c -------- */
1767 * shell: syntax (C version)
1772 DBGPRINTF7(("YYPARSE: enter...\n"));
1779 return (yynerrs != 0);
1782 static struct op *pipeline(cf)
1785 REGISTER struct op *t, *p;
1788 DBGPRINTF7(("PIPELINE: enter, cf=%d\n", cf));
1792 DBGPRINTF9(("PIPELINE: t=0x%x\n", t));
1795 while ((c = yylex(0)) == '|') {
1796 if ((p = command(CONTIN)) == NULL) {
1797 DBGPRINTF8(("PIPELINE: error!\n"));
1801 if (t->type != TPAREN && t->type != TCOM) {
1802 /* shell statement */
1803 t = block(TPAREN, t, NOBLOCK, NOWORDS);
1806 t = block(TPIPE, t, p, NOWORDS);
1811 DBGPRINTF7(("PIPELINE: returning t=0x%x\n", t));
1815 static struct op *andor()
1817 REGISTER struct op *t, *p;
1820 DBGPRINTF7(("ANDOR: enter...\n"));
1824 DBGPRINTF9(("ANDOR: t=0x%x\n", t));
1827 while ((c = yylex(0)) == LOGAND || c == LOGOR) {
1828 if ((p = pipeline(CONTIN)) == NULL) {
1829 DBGPRINTF8(("ANDOR: error!\n"));
1833 t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS);
1839 DBGPRINTF7(("ANDOR: returning t=0x%x\n", t));
1843 static struct op *c_list()
1845 REGISTER struct op *t, *p;
1848 DBGPRINTF7(("C_LIST: enter...\n"));
1853 if ((peeksym = yylex(0)) == '&')
1854 t = block(TASYNC, t, NOBLOCK, NOWORDS);
1856 while ((c = yylex(0)) == ';' || c == '&'
1857 || (multiline && c == '\n')) {
1859 if ((p = andor()) == NULL)
1862 if ((peeksym = yylex(0)) == '&')
1863 p = block(TASYNC, p, NOBLOCK, NOWORDS);
1871 DBGPRINTF7(("C_LIST: returning t=0x%x\n", t));
1875 static int synio(cf)
1878 REGISTER struct ioword *iop;
1882 DBGPRINTF7(("SYNIO: enter, cf=%d\n", cf));
1884 if ((c = yylex(cf)) != '<' && c != '>') {
1891 iop = io(iounit, i, yylval.cp);
1895 markhere(yylval.cp, iop);
1897 DBGPRINTF7(("SYNIO: returning 1\n"));
1901 static void musthave(c, cf)
1904 if ((peeksym = yylex(cf)) != c) {
1905 DBGPRINTF7(("MUSTHAVE: error!\n"));
1912 static struct op *simple()
1914 REGISTER struct op *t;
1918 switch (peeksym = yylex(0)) {
1939 static struct op *nested(type, mark)
1942 REGISTER struct op *t;
1944 DBGPRINTF3(("NESTED: enter, type=%d, mark=%d\n", type, mark));
1950 return (block(type, t, NOBLOCK, NOWORDS));
1953 static struct op *command(cf)
1956 REGISTER struct op *t;
1957 struct wdblock *iosave;
1960 DBGPRINTF(("COMMAND: enter, cf=%d\n", cf));
1976 if ((t = simple()) == NULL) {
1978 return ((struct op *) NULL);
1985 t = nested(TPAREN, ')');
1989 t = nested(TBRACE, '}');
1999 t->words = wordlist();
2000 if ((c = yylex(0)) != '\n' && c != ';')
2002 t->left = dogroup(0);
2010 t->type = c == WHILE ? TWHILE : TUNTIL;
2012 t->right = dogroup(1);
2024 musthave(IN, CONTIN);
2027 t->left = caselist();
2038 t->right = thenpart();
2047 musthave(WORD, 0); /* gets name of file */
2048 DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp));
2050 word(yylval.cp); /* add word to wdlist */
2051 word(NOWORD); /* terminate wdlist */
2052 t->words = copyw(); /* dup wdlist */
2062 DBGPRINTF(("COMMAND: returning 0x%x\n", t));
2067 static struct op *dowholefile(type, mark)
2071 REGISTER struct op *t;
2073 DBGPRINTF(("DOWHOLEFILE: enter, type=%d, mark=%d\n", type, mark));
2078 t = block(type, t, NOBLOCK, NOWORDS);
2079 DBGPRINTF(("DOWHOLEFILE: return t=0x%x\n", t));
2083 static struct op *dogroup(onlydone)
2087 REGISTER struct op *mylist;
2090 if (c == DONE && onlydone)
2091 return ((struct op *) NULL);
2099 static struct op *thenpart()
2102 REGISTER struct op *t;
2104 if ((c = yylex(0)) != THEN) {
2106 return ((struct op *) NULL);
2111 if (t->left == NULL)
2113 t->right = elsepart();
2117 static struct op *elsepart()
2120 REGISTER struct op *t;
2122 switch (c = yylex(0)) {
2124 if ((t = c_list()) == NULL)
2132 t->right = thenpart();
2137 return ((struct op *) NULL);
2141 static struct op *caselist()
2143 REGISTER struct op *t;
2146 while ((peeksym = yylex(CONTIN)) != ESAC) {
2147 DBGPRINTF(("CASELIST, doing yylex, peeksym=%d\n", peeksym));
2148 t = list(t, casepart());
2151 DBGPRINTF(("CASELIST, returning t=0x%x\n", t));
2155 static struct op *casepart()
2157 REGISTER struct op *t;
2159 DBGPRINTF7(("CASEPART: enter...\n"));
2163 t->words = pattern();
2166 if ((peeksym = yylex(CONTIN)) != ESAC)
2167 musthave(BREAK, CONTIN);
2169 DBGPRINTF7(("CASEPART: made newtp(TPAT, t=0x%x)\n", t));
2174 static char **pattern()
2183 } while ((c = yylex(0)) == '|');
2190 static char **wordlist()
2194 if ((c = yylex(0)) != IN) {
2196 return ((char **) NULL);
2199 while ((c = yylex(0)) == WORD)
2207 * supporting functions
2209 static struct op *list(t1, t2)
2210 REGISTER struct op *t1, *t2;
2212 DBGPRINTF7(("LIST: enter, t1=0x%x, t2=0x%x\n", t1, t2));
2219 return (block(TLIST, t1, t2, NOWORDS));
2222 static struct op *block(type, t1, t2, wp)
2227 REGISTER struct op *t;
2229 DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type]));
2237 DBGPRINTF7(("BLOCK: inserted 0x%x between 0x%x and 0x%x\n", t, t1,
2243 /* See if given string is a shell multiline (FOR, IF, etc) */
2244 static int rlookup(n)
2247 REGISTER struct res *rp;
2249 DBGPRINTF7(("RLOOKUP: enter, n is %s\n", n));
2251 for (rp = restab; rp->r_name; rp++)
2252 if (strcmp(rp->r_name, n) == 0) {
2253 DBGPRINTF7(("RLOOKUP: match, returning %d\n", rp->r_val));
2254 return (rp->r_val); /* Return numeric code for shell multiline */
2257 DBGPRINTF7(("RLOOKUP: NO match, returning 0\n"));
2258 return (0); /* Not a shell multiline */
2261 static struct op *newtp()
2263 REGISTER struct op *t;
2265 t = (struct op *) tree(sizeof(*t));
2273 DBGPRINTF3(("NEWTP: allocated 0x%x\n", t));
2278 static struct op *namelist(t)
2279 REGISTER struct op *t;
2282 DBGPRINTF7(("NAMELIST: enter, t=0x%x, type %s, iolist=0x%x\n", t,
2283 T_CMD_NAMES[t->type], iolist));
2286 iolist = addword((char *) NULL, iolist);
2287 t->ioact = copyio();
2291 if (t->type != TCOM) {
2292 if (t->type != TPAREN && t->ioact != NULL) {
2293 t = block(TPAREN, t, NOBLOCK, NOWORDS);
2294 t->ioact = t->left->ioact;
2295 t->left->ioact = NULL;
2307 static char **copyw()
2311 wd = getwords(wdlist);
2316 static void word(cp)
2319 wdlist = addword(cp, wdlist);
2322 static struct ioword **copyio()
2324 REGISTER struct ioword **iop;
2326 iop = (struct ioword **) getwords(iolist);
2331 static struct ioword *io(u, f, cp)
2336 REGISTER struct ioword *iop;
2338 iop = (struct ioword *) tree(sizeof(*iop));
2342 iolist = addword((char *) iop, iolist);
2348 yyerror("syntax error");
2351 static void yyerror(s)
2355 if (interactive && e.iop <= iostack) {
2357 while (eofc() == 0 && yylex(0) != '\n');
2363 static int yylex(cf)
2369 if ((c = peeksym) > 0) {
2384 line[LINELIM - 1] = '\0';
2387 while ((c = my_getc(0)) == ' ' || c == '\t') /* Skip whitespace */
2392 if (any(c, "0123456789")) {
2393 unget(c1 = my_getc(0));
2394 if (c1 == '<' || c1 == '>') {
2403 case '#': /* Comment, skip to next newline or End-of-string */
2404 while ((c = my_getc(0)) != 0 && c != '\n');
2409 DBGPRINTF5(("YYLEX: return 0, c=%d\n", c));
2413 DBGPRINTF9(("YYLEX: found $\n"));
2415 if ((c = my_getc(0)) == '{') {
2416 if ((c = collect(c, '}')) != '\0')
2425 if ((c = collect(c, c)) != '\0')
2433 /* If more chars process them, else return NULL char */
2434 if ((c1 = dual(c)) != '\0')
2451 if (multiline || cf & CONTIN) {
2452 if (interactive && e.iop <= iostack) {
2453 #ifdef CONFIG_FEATURE_COMMAND_EDITING
2454 current_prompt = cprompt->value;
2456 prs(cprompt->value);
2473 while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n")) {
2474 if (e.linep >= elinep)
2475 err("word too long");
2482 if (any(c, "\"'`$"))
2487 if (atstart && (c = rlookup(line)) != 0) {
2492 yylval.cp = strsave(line, areanum);
2497 static int collect(c, c1)
2502 DBGPRINTF8(("COLLECT: enter, c=%d, c1=%d\n", c, c1));
2505 while ((c = my_getc(c1)) != c1) {
2514 if (interactive && c == '\n' && e.iop <= iostack) {
2515 #ifdef CONFIG_FEATURE_COMMAND_EDITING
2516 current_prompt = cprompt->value;
2518 prs(cprompt->value);
2526 DBGPRINTF8(("COLLECT: return 0, line is %s\n", line));
2531 /* "multiline commands" helper func */
2532 /* see if next 2 chars form a shell multiline */
2537 REGISTER char *cp = s;
2539 DBGPRINTF8(("DUAL: enter, c=%d\n", c));
2541 *cp++ = c; /* c is the given "peek" char */
2542 *cp++ = my_getc(0); /* get next char of input */
2543 *cp = 0; /* add EOS marker */
2545 c = rlookup(s); /* see if 2 chars form a shell multiline */
2547 unget(*--cp); /* String is not a shell multiline, put peek char back */
2549 return (c); /* String is multiline, return numeric multiline (restab) code */
2552 static void diag(ec)
2557 DBGPRINTF8(("DIAG: enter, ec=%d\n", ec));
2560 if (c == '>' || c == '<') {
2563 yylval.i = ec == '>' ? IOWRITE | IOCAT : IOHERE;
2566 yylval.i = ec == '>' ? IOWRITE : IOREAD;
2567 if (c != '&' || yylval.i == IOHERE)
2573 static char *tree(size)
2578 if ((t = getcell(size)) == NULL) {
2579 DBGPRINTF2(("TREE: getcell(%d) failed!\n", size));
2580 prs("command line too complicated\n");
2590 /* -------- exec.c -------- */
2597 static int execute(t, pin, pout, act)
2598 REGISTER struct op *t;
2602 REGISTER struct op *t1;
2603 volatile int i, rv, a;
2604 char *cp, **wp, **wp2;
2606 struct op *outtree_save;
2610 /* Avoid longjmp clobbering */
2615 DBGPRINTF4(("EXECUTE: enter, t==null, returning.\n"));
2619 DBGPRINTF(("EXECUTE: t=0x%x, t->type=%d (%s), t->words is %s\n", t,
2620 t->type, T_CMD_NAMES[t->type],
2621 ((t->words == NULL) ? "NULL" : t->words[0])));
2625 wp = (wp2 = t->words) != NULL
2626 ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
2629 /* Hard to know how many words there are, be careful of garbage pointer values */
2630 /* They are likely to cause "PCI bus fault" errors */
2632 DBGPRINTF(("EXECUTE: t->left=0x%x, t->right=0x%x, t->words[1] is %s\n",
2634 ((t->words[1] == NULL) ? "NULL" : t->words[1])));
2635 DBGPRINTF7(("EXECUTE: t->words[2] is %s, t->words[3] is %s\n",
2636 ((t->words[2] == NULL) ? "NULL" : t->words[2]),
2637 ((t->words[3] == NULL) ? "NULL" : t->words[3])));
2643 DBGPRINTF3(("EXECUTE: TDOT\n"));
2645 outtree_save = outtree;
2647 newfile(evalstr(t->words[0], DOALL));
2649 t->left = dowholefile(TLIST, 0);
2652 outtree = outtree_save;
2655 rv = execute(t->left, pin, pout, 0);
2657 rv = execute(t->right, pin, pout, 0);
2661 rv = execute(t->left, pin, pout, 0);
2666 rv = forkexec(t, pin, pout, act, wp);
2674 if ((rv = openpipe(pv)) < 0)
2676 pv[0] = remap(pv[0]);
2677 pv[1] = remap(pv[1]);
2678 (void) execute(t->left, pin, pv, 0);
2679 rv = execute(t->right, pv, pout, 0);
2684 (void) execute(t->left, pin, pout, 0);
2685 rv = execute(t->right, pin, pout, 0);
2690 int hinteractive = interactive;
2692 DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n"));
2696 interactive = hinteractive;
2698 setval(lookup("!"), putn(i));
2709 signal(SIGINT, SIG_IGN);
2710 signal(SIGQUIT, SIG_IGN);
2712 signal(SIGTERM, SIG_DFL);
2716 open("/dev/null", 0);
2718 _exit(execute(t->left, pin, pout, FEXEC));
2725 rv = execute(t->left, pin, pout, 0);
2726 if ((t1 = t->right) != NULL && (rv == 0) == (t->type == TAND))
2727 rv = execute(t1, pin, pout, 0);
2737 while (*wp++ != NULL);
2739 vp = lookup(t->str);
2740 while (setjmp(bc.brkpt))
2744 for (t1 = t->left; i-- && *wp != NULL;) {
2746 rv = execute(t1, pin, pout, 0);
2748 brklist = brklist->nextlev;
2753 while (setjmp(bc.brkpt))
2758 while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE))
2759 rv = execute(t->right, pin, pout, 0);
2760 brklist = brklist->nextlev;
2765 if (t->right != NULL) {
2766 rv = !execute(t->left, pin, pout, 0) ?
2767 execute(t->right->left, pin, pout, 0) :
2768 execute(t->right->right, pin, pout, 0);
2773 if ((cp = evalstr(t->str, DOSUB | DOTRIM)) == 0)
2776 DBGPRINTF7(("EXECUTE: TCASE, t->str is %s, cp is %s\n",
2777 ((t->str == NULL) ? "NULL" : t->str),
2778 ((cp == NULL) ? "NULL" : cp)));
2780 if ((t1 = findcase(t->left, cp)) != NULL) {
2781 DBGPRINTF7(("EXECUTE: TCASE, calling execute(t=0x%x, t1=0x%x)...\n", t, t1));
2782 rv = execute(t1, pin, pout, 0);
2783 DBGPRINTF7(("EXECUTE: TCASE, back from execute(t=0x%x, t1=0x%x)...\n", t, t1));
2789 if (iopp = t->ioact)
2791 if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
2796 if (rv >= 0 && (t1 = t->left))
2797 rv = execute(t1, pin, pout, 0);
2808 if (interactive && intr) {
2813 if ((i = trapset) != 0) {
2818 DBGPRINTF(("EXECUTE: returning from t=0x%x, rv=%d\n", t, rv));
2823 forkexec(REGISTER struct op *t, int *pin, int *pout, int act, char **wp)
2827 int (*shcom) (struct op *) = NULL;
2830 struct ioword **iopp;
2840 struct brkcon *hbrklist;
2844 /* Avoid longjmp clobbering */
2854 DBGPRINTF(("FORKEXEC: t=0x%x, pin 0x%x, pout 0x%x, act %d\n", t, pin,
2856 DBGPRINTF7(("FORKEXEC: t->words is %s\n",
2857 ((t->words == NULL) ? "NULL" : t->words[0])));
2859 /* Hard to know how many words there are, be careful of garbage pointer values */
2860 /* They are likely to cause "PCI bus fault" errors */
2862 DBGPRINTF7(("FORKEXEC: t->words is %s, t->words[1] is %s\n",
2863 ((t->words == NULL) ? "NULL" : t->words[0]),
2864 ((t->words == NULL) ? "NULL" : t->words[1])));
2865 DBGPRINTF7(("FORKEXEC: wp is %s, wp[1] is %s\n",
2866 ((wp == NULL) ? "NULL" : wp[0]),
2867 ((wp[1] == NULL) ? "NULL" : wp[1])));
2868 DBGPRINTF7(("FORKEXEC: wp2 is %s, wp[3] is %s\n",
2869 ((wp[2] == NULL) ? "NULL" : wp[2]),
2870 ((wp[3] == NULL) ? "NULL" : wp[3])));
2876 rv = -1; /* system-detected error */
2877 if (t->type == TCOM) {
2878 while ((cp = *wp++) != NULL);
2881 /* strip all initial assignments */
2882 /* not correct wrt PATH=yyy command etc */
2884 DBGPRINTF9(("FORKEXEC: echo'ing, cp=0x%x, wp=0x%x, owp=0x%x\n",
2886 echo(cp ? wp : owp);
2889 DBGPRINTF9(("FORKEXEC: t->words is %s, t->words[1] is %s\n",
2890 ((t->words == NULL) ? "NULL" : t->words[0]),
2891 ((t->words == NULL) ? "NULL" : t->words[1])));
2892 DBGPRINTF9(("FORKEXEC: wp is %s, wp[1] is %s\n",
2893 ((wp == NULL) ? "NULL" : wp[0]),
2894 ((wp == NULL) ? "NULL" : wp[1])));
2897 if (cp == NULL && t->ioact == NULL) {
2898 while ((cp = *owp++) != NULL && assign(cp, COPYV));
2899 DBGPRINTF(("FORKEXEC: returning setstatus()\n"));
2900 return (setstatus(0));
2901 } else if (cp != NULL) {
2902 shcom = inbuilt(cp);
2910 DBGPRINTF3(("FORKEXEC: t->words is %s, t->words[1] is %s\n",
2911 ((t->words == NULL) ? "NULL" : t->words[0]),
2912 ((t->words == NULL) ? "NULL" : t->words[1])));
2914 DBGPRINTF(("FORKEXEC: shcom 0x%x, f&FEXEC 0x%x, owp 0x%x\n", shcom,
2917 if (shcom == NULL && (f & FEXEC) == 0) {
2918 /* Save values in case the child process alters them */
2922 hinteractive = interactive;
2927 DBGPRINTF3(("FORKEXEC: calling vfork()...\n"));
2932 DBGPRINTF(("FORKEXEC: ERROR, unable to vfork()!\n"));
2937 if (newpid > 0) { /* Parent */
2939 /* Restore values */
2943 interactive = hinteractive;
2956 return (pout == NULL ? setstatus(waitfor(newpid, 0)) : 0);
2959 /* Must be the child process, pid should be 0 */
2960 DBGPRINTF(("FORKEXEC: child process, shcom=0x%x\n", shcom));
2963 signal(SIGINT, SIG_IGN);
2964 signal(SIGQUIT, SIG_IGN);
2976 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2981 if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) {
2982 err("piping to/from shell builtins not yet done");
2998 if ((iopp = t->ioact) != NULL) {
2999 if (shcom != NULL && shcom != doexec) {
3001 err(": cannot redirect shell command");
3007 if (iosetup(*iopp++, pin != NULL, pout != NULL)) {
3015 i = setstatus((*shcom) (t));
3018 DBGPRINTF(("FORKEXEC: returning i=%d\n", i));
3022 /* should use FIOCEXCL */
3023 for (i = FDBASE; i < NOFILE; i++)
3026 signal(SIGINT, SIG_DFL);
3027 signal(SIGQUIT, SIG_DFL);
3030 if (t->type == TPAREN)
3031 _exit(execute(t->left, NOPIPE, NOPIPE, FEXEC));
3035 cp = rexecve(wp[0], wp, makenv(0, NULL));
3042 DBGPRINTF(("FORKEXEC: calling leave(), pid=%d\n", newpid));
3050 * 0< 1> are ignored as required
3053 static int iosetup(iop, pipein, pipeout)
3054 REGISTER struct ioword *iop;
3055 int pipein, pipeout;
3057 REGISTER int u = -1;
3058 char *cp = NULL, *msg;
3060 DBGPRINTF(("IOSETUP: iop 0x%x, pipein 0x%x, pipeout 0x%x\n", iop,
3063 if (iop->io_unit == IODEFAULT) /* take default */
3064 iop->io_unit = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1;
3066 if (pipein && iop->io_unit == 0)
3069 if (pipeout && iop->io_unit == 1)
3072 msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create";
3073 if ((iop->io_flag & IOHERE) == 0) {
3075 if ((cp = evalstr(cp, DOSUB | DOTRIM)) == NULL)
3079 if (iop->io_flag & IODUP) {
3080 if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
3082 err(": illegal >& argument");
3086 iop->io_flag = IOCLOSE;
3087 iop->io_flag &= ~(IOREAD | IOWRITE);
3089 switch (iop->io_flag) {
3095 case IOHERE | IOXHERE:
3096 u = herein(iop->io_name, iop->io_flag & IOXHERE);
3100 case IOWRITE | IOCAT:
3101 if ((u = open(cp, 1)) >= 0) {
3102 lseek(u, (long) 0, 2);
3106 u = creat(cp, 0666);
3110 u = dup2(*cp - '0', iop->io_unit);
3114 close(iop->io_unit);
3123 if (u != iop->io_unit) {
3124 dup2(u, iop->io_unit);
3131 static void echo(wp)
3137 for (i = 0; wp[i]; i++) {
3145 static struct op **find1case(t, w)
3149 REGISTER struct op *t1;
3151 REGISTER char **wp, *cp;
3155 DBGPRINTF3(("FIND1CASE: enter, t==NULL, returning.\n"));
3156 return ((struct op **) NULL);
3159 DBGPRINTF3(("FIND1CASE: enter, t->type=%d (%s)\n", t->type,
3160 T_CMD_NAMES[t->type]));
3162 if (t->type == TLIST) {
3163 if ((tp = find1case(t->left, w)) != NULL) {
3164 DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=0x%x\n", tp));
3167 t1 = t->right; /* TPAT */
3171 for (wp = t1->words; *wp;)
3172 if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp)) {
3173 DBGPRINTF3(("FIND1CASE: returning &t1->left= 0x%x.\n",
3178 DBGPRINTF(("FIND1CASE: returning NULL\n"));
3179 return ((struct op **) NULL);
3182 static struct op *findcase(t, w)
3186 REGISTER struct op **tp;
3188 return ((tp = find1case(t, w)) != NULL ? *tp : (struct op *) NULL);
3192 * Enter a new loop level (marked for break/continue).
3194 static void brkset(bc)
3197 bc->nextlev = brklist;
3202 * Wait for the last process created.
3203 * Print a message for each process found
3204 * that was killed by a signal.
3205 * Ignore interrupt signals while waiting
3206 * unless `canintr' is true.
3208 static int waitfor(lastpid, canintr)
3209 REGISTER int lastpid;
3212 REGISTER int pid, rv;
3214 int oheedint = heedint;
3221 if (errno != EINTR || canintr)
3224 if ((rv = WAITSIG(s)) != 0) {
3226 if (signame[rv] != NULL) {
3227 if (pid != lastpid) {
3234 if (pid != lastpid) {
3243 prs(" - core dumped");
3244 if (rv >= NSIGNAL || signame[rv])
3250 } while (pid != lastpid);
3265 static int setstatus(s)
3269 setval(lookup("?"), putn(s));
3274 * PATH-searching interface to execve.
3275 * If getenv("PATH") were kept up-to-date,
3276 * execvp might be used.
3278 static char *rexecve(c, v, envp)
3279 char *c, **v, **envp;
3282 REGISTER char *sp, *tp;
3283 int eacces = 0, asis = 0;
3285 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
3289 if (find_applet_by_name(name)) {
3290 /* We have to exec here since we vforked. Running
3291 * run_applet_by_name() won't work and bad things
3293 execve("/proc/self/exe", v, envp);
3294 execve("busybox", v, envp);
3298 DBGPRINTF(("REXECVE: c=0x%x, v=0x%x, envp=0x%x\n", c, v, envp));
3300 sp = any('/', c) ? "" : path->value;
3302 while (asis || *sp != '\0') {
3305 for (; *sp != '\0'; tp++)
3306 if ((*tp = *sp++) == ':') {
3312 for (i = 0; (*tp++ = c[i++]) != '\0';);
3314 DBGPRINTF3(("REXECVE: e.linep is %s\n", e.linep));
3316 execve(e.linep, v, envp);
3323 execve(DEFAULT_SHELL, v, envp);
3325 return ("no Shell");
3328 return ((char *) bb_msg_memory_exhausted);
3331 return ("argument list too long");
3338 return (errno == ENOENT ? "not found" : "cannot execute");
3342 * Run the command produced by generator `f'
3343 * applied to stream `arg'.
3345 static int run(struct ioarg *argp, int (*f) (struct ioarg *))
3348 struct wdblock *swdlist;
3349 struct wdblock *siolist;
3355 /* Avoid longjmp clobbering */
3359 DBGPRINTF(("RUN: enter, areanum %d, outtree 0x%x, failpt 0x%x\n",
3360 areanum, outtree, failpt));
3369 if (newenv(setjmp(errpt = ev)) == 0) {
3375 if (setjmp(failpt = rt) == 0 && yyparse() == 0)
3376 rv = execute(outtree, NOPIPE, NOPIPE, 0);
3379 DBGPRINTF(("RUN: error from newenv()!\n"));
3386 freearea(areanum--);
3391 /* -------- do.c -------- */
3394 * built-in commands: doX
3397 static int dohelp(struct op *t)
3400 const struct builtincmd *x;
3402 printf("\nBuilt-in commands:\n");
3403 printf("-------------------\n");
3405 for (col = 0, x = builtincmds; x->builtinfunc != NULL; x++) {
3408 col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name);
3414 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
3417 const struct BB_applet *applet;
3418 extern const struct BB_applet applets[];
3419 extern const size_t NUM_APPLETS;
3421 for (i = 0, applet = applets; i < NUM_APPLETS; applet++, i++) {
3425 col += printf("%s%s", ((col == 0) ? "\t" : " "), applet->name);
3434 return EXIT_SUCCESS;
3439 static int dolabel(struct op *t)
3444 static int dochdir(t)
3445 REGISTER struct op *t;
3447 REGISTER char *cp, *er;
3449 if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL)
3450 er = ": no home directory";
3451 else if (chdir(cp) < 0)
3452 er = ": bad directory";
3455 prs(cp != NULL ? cp : "cd");
3460 static int doshift(t)
3461 REGISTER struct op *t;
3465 n = t->words[1] ? getn(t->words[1]) : 1;
3467 err("nothing to shift");
3473 setval(lookup("#"), putn(dolc));
3478 * execute login and newgrp directly
3480 static int dologin(t)
3486 signal(SIGINT, SIG_DFL);
3487 signal(SIGQUIT, SIG_DFL);
3489 cp = rexecve(t->words[0], t->words, makenv(0, NULL));
3496 static int doumask(t)
3497 REGISTER struct op *t;
3502 if ((cp = t->words[1]) == NULL) {
3505 for (n = 3 * 4; (n -= 3) >= 0;)
3506 putc('0' + ((i >> n) & 07), stderr);
3509 for (n = 0; *cp >= '0' && *cp <= '9'; cp++)
3510 n = n * 8 + (*cp - '0');
3516 static int doexec(t)
3517 REGISTER struct op *t;
3524 for (i = 0; (t->words[i] = t->words[i + 1]) != NULL; i++);
3529 if (setjmp(failpt = ex) == 0)
3530 execute(t, NOPIPE, NOPIPE, FEXEC);
3540 REGISTER char *sp, *tp;
3544 DBGPRINTF(("DODOT: enter, t=0x%x, tleft 0x%x, tright 0x%x, e.linep is %s\n", t, t->left, t->right, ((e.linep == NULL) ? "NULL" : e.linep)));
3546 if ((cp = t->words[1]) == NULL) {
3547 DBGPRINTF(("DODOT: bad args, ret 0\n"));
3550 DBGPRINTF(("DODOT: cp is %s\n", cp));
3553 sp = any('/', cp) ? ":" : path->value;
3555 DBGPRINTF(("DODOT: sp is %s, e.linep is %s\n",
3556 ((sp == NULL) ? "NULL" : sp),
3557 ((e.linep == NULL) ? "NULL" : e.linep)));
3561 while (*sp && (*tp = *sp++) != ':')
3566 for (i = 0; (*tp++ = cp[i++]) != '\0';);
3569 if ((i = open(e.linep, 0)) >= 0) {
3572 DBGPRINTF(("DODOT: remap=%d, exstat=%d, e.iofd %d, i %d, e.linep is %s\n", maltmp, exstat, e.iofd, i, e.linep));
3574 next(maltmp); /* Basically a PUSHIO */
3576 DBGPRINTF(("DODOT: returning exstat=%d\n", exstat));
3589 static int dowait(t)
3595 if ((cp = t->words[1]) != NULL) {
3601 setstatus(waitfor(i, 1));
3605 static int doread(t)
3608 REGISTER char *cp, **wp;
3609 REGISTER int nb = 0;
3610 REGISTER int nl = 0;
3612 if (t->words[1] == NULL) {
3613 err("Usage: read name ...");
3616 for (wp = t->words + 1; *wp; wp++) {
3617 for (cp = e.linep; !nl && cp < elinep - 1; cp++)
3618 if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) ||
3619 (nl = (*cp == '\n')) || (wp[1] && any(*cp, ifs->value)))
3624 setval(lookup(*wp), e.linep);
3629 static int doeval(t)
3630 REGISTER struct op *t;
3632 return (RUN(awordlist, t->words + 1, wdchar));
3635 static int dotrap(t)
3636 REGISTER struct op *t;
3639 REGISTER int resetsig;
3641 if (t->words[1] == NULL) {
3642 for (i = 0; i <= _NSIG; i++)
3651 resetsig = isdigit(*t->words[1]);
3652 for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) {
3653 n = getsig(t->words[i]);
3657 if (*t->words[1] != '\0') {
3658 trap[n] = strsave(t->words[1], 0);
3667 setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL);
3675 static int getsig(s)
3680 if ((n = getn(s)) < 0 || n > _NSIG) {
3681 err("trap: bad signal number");
3687 static void setsig(REGISTER int n, sighandler_t f)
3691 if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) {
3709 for (n = 0; isdigit(*s); s++)
3710 n = (n * 10) + (*s - '0');
3713 err(": bad number");
3718 static int dobreak(t)
3721 return (brkcontin(t->words[1], 1));
3724 static int docontinue(t)
3727 return (brkcontin(t->words[1], 0));
3730 static int brkcontin(cp, val)
3734 REGISTER struct brkcon *bc;
3737 nl = cp == NULL ? 1 : getn(cp);
3741 if ((bc = brklist) == NULL)
3743 brklist = bc->nextlev;
3746 err("bad break/continue level");
3750 longjmp(bc->brkpt, 1);
3754 static int doexit(t)
3760 if ((cp = t->words[1]) != NULL)
3761 setstatus(getn(cp));
3763 DBGPRINTF(("DOEXIT: calling leave(), t=0x%x\n", t));
3770 static int doexport(t)
3773 rdexp(t->words + 1, export, EXPORT);
3777 static int doreadonly(t)
3780 rdexp(t->words + 1, ronly, RONLY);
3784 static void rdexp(char **wp, void (*f) (struct var *), int key)
3786 DBGPRINTF6(("RDEXP: enter, wp=0x%x, func=0x%x, key=%d\n", wp, f, key));
3787 DBGPRINTF6(("RDEXP: *wp=%s\n", *wp));
3790 for (; *wp != NULL; wp++) {
3791 if (isassign(*wp)) {
3795 for (cp = *wp; *cp != '='; cp++);
3807 static void badid(s)
3811 err(": bad identifier");
3815 REGISTER struct op *t;
3817 REGISTER struct var *vp;
3821 if ((cp = t->words[1]) == NULL) {
3822 for (vp = vlist; vp; vp = vp->next)
3823 varput(vp->name, 1);
3827 /* bad: t->words++; */
3828 for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++);
3830 flag['x'] = flag['v'] = 0;
3840 if (*cp >= 'a' && *cp <= 'z')
3847 t->words[0] = dolv[0];
3848 for (n = 1; t->words[n]; n++)
3849 setarea((char *) t->words[n], 0);
3852 setval(lookup("#"), putn(dolc));
3853 setarea((char *) (dolv - 1), 0);
3858 static void varput(s, out)
3862 if (isalnum(*s) || *s == '_') {
3863 write(out, s, strlen(s));
3864 write(out, "\n", 1);
3870 * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
3871 * This file contains code for the times builtin.
3873 static int dotimes(struct op *t)
3876 long int clk_tck = sysconf(_SC_CLK_TCK);
3879 printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
3880 (int) (buf.tms_utime / clk_tck / 60),
3881 ((double) buf.tms_utime) / clk_tck,
3882 (int) (buf.tms_stime / clk_tck / 60),
3883 ((double) buf.tms_stime) / clk_tck,
3884 (int) (buf.tms_cutime / clk_tck / 60),
3885 ((double) buf.tms_cutime) / clk_tck,
3886 (int) (buf.tms_cstime / clk_tck / 60),
3887 ((double) buf.tms_cstime) / clk_tck);
3892 static int (*inbuilt(char *s)) (struct op *) {
3893 const struct builtincmd *bp;
3895 for (bp = builtincmds; bp->name != NULL; bp++)
3896 if (strcmp(bp->name, s) == 0)
3897 return (bp->builtinfunc);
3902 /* -------- eval.c -------- */
3907 * blank interpretation
3912 static char **eval(char **ap, int f)
3920 /* Avoid longjmp clobbering */
3925 DBGPRINTF4(("EVAL: enter, f=%d\n", f));
3930 if (newenv(setjmp(errpt = ev)) == 0) {
3931 while (*ap && isassign(*ap))
3932 expand(*ap++, &wb, f & ~DOGLOB);
3934 for (wf = ap; *wf; wf++) {
3936 expand(*wf, &wb, f & ~DOGLOB);
3939 for (wb = addword((char *) 0, wb); *ap; ap++) {
3940 if (!flag['k'] || !isassign(*ap))
3941 expand(*ap, &wb, f & ~DOKEY);
3943 wb = addword((char *) 0, wb);
3949 return (gflg ? (char **) NULL : wp);
3953 * Make the exported environment from the exported
3954 * names in the dictionary. Keyword assignments
3955 * will already have been done.
3957 static char **makenv(int all, struct wdblock *wb)
3959 REGISTER struct var *vp;
3961 DBGPRINTF5(("MAKENV: enter, all=%d\n", all));
3963 for (vp = vlist; vp; vp = vp->next)
3964 if (all || vp->status & EXPORT)
3965 wb = addword(vp->name, wb);
3966 wb = addword((char *) 0, wb);
3967 return (getwords(wb));
3970 static char *evalstr(cp, f)
3976 DBGPRINTF6(("EVALSTR: enter, cp=0x%x, f=%d\n", cp, f));
3979 if (expand(cp, &wb, f)) {
3980 if (wb == NULL || wb->w_nword == 0
3981 || (cp = wb->w_words[0]) == NULL)
3989 static int expand(char *cp, REGISTER struct wdblock **wbp, int f)
3994 /* Avoid longjmp clobbering */
3998 DBGPRINTF3(("EXPAND: enter, f=%d\n", f));
4005 if (!anys("$`'\"", cp) &&
4006 !anys(ifs->value, cp) && ((f & DOGLOB) == 0 || !anys("[*?", cp))) {
4007 cp = strsave(cp, areanum);
4010 *wbp = addword(cp, *wbp);
4013 if (newenv(setjmp(errpt = ev)) == 0) {
4014 PUSHIO(aword, cp, strchar);
4016 while ((cp = blank(f)) && gflg == 0) {
4018 cp = strsave(cp, areanum);
4019 if ((f & DOGLOB) == 0) {
4022 *wbp = addword(cp, *wbp);
4024 *wbp = glob(cp, *wbp);
4033 * Blank interpretation and quoting
4035 static char *blank(f)
4040 int scanequals, foundequals;
4042 DBGPRINTF3(("BLANK: enter, f=%d\n", f));
4045 scanequals = f & DOKEY;
4049 switch (c = subgetc('"', foundequals)) {
4057 if (f & DOBLANK && any(c, ifs->value))
4066 for (c1 = c; (c = subgetc(c1, 1)) != c1;) {
4069 if (c == '\'' || !any(c, "$`\""))
4076 if (!isalpha(c) && c != '_')
4079 c = subgetc('"', foundequals);
4081 f & (DOBLANK && any(c, ifs->value)) ||
4082 (!INSUB() && any(c, "\"'"))) {
4093 } else if (!isalnum(c) && c != '_')
4103 * Get characters, substituting for ` and $
4105 static int subgetc(ec, quoted)
4111 DBGPRINTF3(("SUBGETC: enter, quoted=%d\n", quoted));
4115 if (!INSUB() && ec != '\'') {
4117 if (grave(quoted) == 0)
4119 e.iop->task = XGRAVE;
4122 if (c == '$' && (c = dollar(quoted)) == 0) {
4123 e.iop->task = XDOLL;
4131 * Prepare to generate the string returned by ${} substitution.
4133 static int dollar(quoted)
4139 REGISTER char *s, c, *cp = NULL;
4142 DBGPRINTF3(("DOLLAR: enter, quoted=%d\n", quoted));
4148 if (isalpha(c) || c == '_') {
4149 while ((c = readc()) != 0 && (isalnum(c) || c == '_'))
4150 if (e.linep < elinep)
4157 otask = e.iop->task;
4159 e.iop->task = XOTHER;
4160 while ((c = subgetc('"', 0)) != 0 && c != '}' && c != '\n')
4161 if (e.linep < elinep)
4164 e.iop->task = otask;
4171 if (e.linep >= elinep) {
4172 err("string in ${} too long");
4178 for (cp = s + 1; *cp; cp++)
4179 if (any(*cp, "=-+?")) {
4184 if (s[1] == 0 && (*s == '*' || *s == '@')) {
4186 /* currently this does not distinguish $* and $@ */
4187 /* should check dollar */
4189 PUSHIO(awordlist, dolv + 1, dolchar);
4191 } else { /* trap the nasty ${=} */
4197 if ((dolp = vp->value) == null) {
4201 err("cannot use ${...=...} with $n");
4210 dolp = strsave(cp, areanum);
4215 prs("missing value for ");
4222 } else if (c == '+')
4223 dolp = strsave(cp, areanum);
4224 if (flag['u'] && dolp == null) {
4225 prs("unset variable: ");
4230 PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
4235 * Run the command in `...` and read its output.
4238 static int grave(quoted)
4245 static char child_cmd[LINELIM];
4251 char *argument_list[4];
4252 struct wdblock *wb = NULL;
4255 /* Avoid longjmp clobbering */
4259 for (cp = e.iop->argp->aword; *cp != '`'; cp++)
4261 err("no closing `");
4265 /* string copy with dollar expansion */
4266 src = e.iop->argp->aword;
4271 while ((*src != '`') && (count < LINELIM)) {
4276 if (*src == '$' && !ignore && !ignore_once) {
4278 char var_name[LINELIM];
4279 char alt_value[LINELIM];
4292 var_name[var_index++] = *src++;
4293 while (isalnum(*src))
4294 var_name[var_index++] = *src++;
4295 var_name[var_index] = 0;
4308 err("unclosed ${\n");
4313 while (*src && (*src != '}')) {
4314 alt_value[alt_index++] = *src++;
4316 alt_value[alt_index] = 0;
4318 err("unclosed ${\n");
4325 if (isalpha(*var_name)) {
4326 /* let subshell handle it instead */
4328 char *namep = var_name;
4336 char *altp = alt_value;
4344 wb = addword(lookup(var_name)->name, wb);
4348 vp = lookup(var_name);
4349 if (vp->value != null)
4350 value = (operator == '+') ?
4351 alt_value : vp->value;
4352 else if (operator == '?') {
4355 } else if (alt_index && (operator != '+')) {
4357 if (operator == '=')
4362 while (*value && (count < LINELIM)) {
4375 if (openpipe(pf) < 0)
4378 while ((i = vfork()) == -1 && errno == EAGAIN);
4380 DBGPRINTF3(("GRAVE: i is %d\n", io));
4384 err((char *) bb_msg_memory_exhausted);
4388 waitpid(i, NULL, 0);
4389 e.iop->argp->aword = ++cp;
4391 PUSHIO(afile, remap(pf[0]),
4392 (int (*)(struct ioarg *)) ((quoted) ? qgravechar :
4396 /* allow trapped signals */
4397 /* XXX - Maybe this signal stuff should go as well? */
4398 for (j = 0; j <= _NSIG; j++)
4399 if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN)
4405 argument_list[0] = (char *) DEFAULT_SHELL;
4406 argument_list[1] = "-c";
4407 argument_list[2] = child_cmd;
4408 argument_list[3] = 0;
4410 cp = rexecve(argument_list[0], argument_list, makenv(1, wb));
4411 prs(argument_list[0]);
4418 static char *unquote(as)
4423 if ((s = as) != NULL)
4429 /* -------- glob.c -------- */
4435 #define scopy(x) strsave((x), areanum)
4437 #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
4439 static struct wdblock *cl, *nl;
4440 static char spcl[] = "[?*";
4442 static struct wdblock *glob(cp, wb)
4452 for (pp = cp; *pp; pp++)
4455 else if (!any(*pp & ~QUOTE, spcl))
4458 for (cl = addword(scopy(cp), (struct wdblock *) 0); anyspcl(cl);
4460 nl = newword(cl->w_nword * 2);
4461 for (i = 0; i < cl->w_nword; i++) { /* for each argument */
4462 for (pp = cl->w_words[i]; *pp; pp++)
4463 if (any(*pp, spcl)) {
4464 globname(cl->w_words[i], pp);
4468 nl = addword(scopy(cl->w_words[i]), nl);
4470 for (i = 0; i < cl->w_nword; i++)
4471 DELETE(cl->w_words[i]);
4474 for (i = 0; i < cl->w_nword; i++)
4475 unquote(cl->w_words[i]);
4476 glob0((char *) cl->w_words, cl->w_nword, sizeof(char *), xstrcmp);
4478 for (i = 0; i < cl->w_nword; i++)
4479 wb = addword(cl->w_words[i], wb);
4484 wb = addword(unquote(cp), wb);
4488 static void globname(we, pp)
4492 REGISTER char *np, *cp;
4493 char *name, *gp, *dp;
4497 char dname[NAME_MAX + 1];
4500 for (np = we; np != pp; pp--)
4503 for (dp = cp = space((int) (pp - np) + 3); np < pp;)
4507 for (gp = cp = space(strlen(pp) + 1); *np && *np != '/';)
4516 dname[NAME_MAX] = '\0';
4517 while ((de = readdir(dirp)) != NULL) {
4518 /* XXX Hmmm... What this could be? (abial) */
4520 if (ent[j].d_ino == 0)
4523 strncpy(dname, de->d_name, NAME_MAX);
4524 if (dname[0] == '.')
4527 for (k = 0; k < NAME_MAX; k++)
4528 if (any(dname[k], spcl))
4530 if (gmatch(dname, gp)) {
4531 name = generate(we, pp, dname, np);
4532 if (*np && !anys(np, spcl)) {
4533 if (stat(name, &dbuf)) {
4538 nl = addword(name, nl);
4547 * generate a pathname as below.
4548 * start..end1 / middle end
4549 * the slashes come for free
4551 static char *generate(start1, end1, middle, end)
4553 REGISTER char *end1;
4557 REGISTER char *op, *xp;
4560 space((int) (end1 - start1) + strlen(middle) + strlen(end) + 2);
4561 for (xp = start1; xp != end1;)
4563 for (xp = middle; (*op++ = *xp++) != '\0';);
4565 for (xp = end; (*op++ = *xp++) != '\0';);
4569 static int anyspcl(wb)
4570 REGISTER struct wdblock *wb;
4576 for (i = 0; i < wb->w_nword; i++)
4577 if (anys(spcl, *wd++))
4582 static int xstrcmp(p1, p2)
4585 return (strcmp(*(char **) p1, *(char **) p2));
4588 /* -------- word.c -------- */
4590 static struct wdblock *newword(nw)
4593 REGISTER struct wdblock *wb;
4595 wb = (struct wdblock *) space(sizeof(*wb) + nw * sizeof(char *));
4601 static struct wdblock *addword(wd, wb)
4603 REGISTER struct wdblock *wb;
4605 REGISTER struct wdblock *wb2;
4609 wb = newword(NSTART);
4610 if ((nw = wb->w_nword) >= wb->w_bsize) {
4611 wb2 = newword(nw * 2);
4612 memcpy((char *) wb2->w_words, (char *) wb->w_words,
4613 nw * sizeof(char *));
4618 wb->w_words[wb->w_nword++] = wd;
4624 REGISTER struct wdblock *wb;
4630 return ((char **) NULL);
4631 if (wb->w_nword == 0) {
4633 return ((char **) NULL);
4635 wd = (char **) space(nb = sizeof(*wd) * wb->w_nword);
4636 memcpy((char *) wd, (char *) wb->w_words, nb);
4637 DELETE(wb); /* perhaps should done by caller */
4641 int (*func) (char *, char *);
4644 static void glob0(a0, a1, a2, a3)
4648 int (*a3) (char *, char *);
4652 glob1(a0, a0 + a1 * a2);
4655 static void glob1(base, lim)
4658 REGISTER char *i, *j;
4668 if ((n = (int) (lim - base)) <= v2)
4670 n = v2 * (n / (2 * v2));
4671 hptr = lptr = base + n;
4676 if ((c = (*func) (i, lptr)) == 0) {
4677 glob2(i, lptr -= v2);
4688 if ((c = (*func) (hptr, j)) == 0) {
4689 glob2(hptr += v2, j);
4694 glob3(i, hptr += v2, j);
4709 if (lptr - base >= lim - hptr) {
4710 glob1(hptr + v2, lim);
4720 glob3(j, lptr -= v2, i);
4725 static void glob2(i, j)
4728 REGISTER char *index1, *index2, c;
4736 *index1++ = *index2;
4741 static void glob3(i, j, k)
4744 REGISTER char *index1, *index2, *index3;
4754 *index1++ = *index3;
4755 *index3++ = *index2;
4760 /* -------- io.c -------- */
4766 static int my_getc(int ec)
4770 if (e.linep > elinep) {
4771 while ((c = readc()) != '\n' && c);
4772 err("input line too long");
4777 if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) {
4780 if (c == '\n' && ec != '\"')
4781 return (my_getc(ec));
4788 static void unget(c)
4791 if (e.iop >= e.iobase)
4797 return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0);
4804 RCPRINTF(("READC: e.iop 0x%x, e.iobase 0x%x\n", e.iop, e.iobase));
4806 for (; e.iop >= e.iobase; e.iop--) {
4807 RCPRINTF(("READC: e.iop 0x%x, peekc 0x%x\n", e.iop, e.iop->peekc));
4808 if ((c = e.iop->peekc) != '\0') {
4812 if (e.iop->prev != 0) {
4813 if ((c = (*e.iop->iofn) (e.iop->argp, e.iop)) != '\0') {
4818 if (e.iop == iostack)
4820 return (e.iop->prev = c);
4821 } else if (e.iop->task == XIO && e.iop->prev != '\n') {
4823 if (e.iop == iostack)
4828 if (e.iop->task == XIO) {
4830 return e.iop->prev = 0;
4832 if (interactive && e.iop == iostack + 1) {
4833 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4834 current_prompt = prompt->value;
4844 if (e.iop >= iostack) {
4845 RCPRINTF(("READC: return 0, e.iop 0x%x\n", e.iop));
4849 DBGPRINTF(("READC: leave()...\n"));
4856 static void ioecho(c)
4860 write(2, &c, sizeof c);
4864 static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *))
4866 DBGPRINTF(("PUSHIO: argp 0x%x, argp->afid 0x%x, e.iop 0x%x\n", argp,
4867 argp->afid, e.iop));
4869 /* Set env ptr for io source to next array spot and check for array overflow */
4870 if (++e.iop >= &iostack[NPUSH]) {
4872 err("Shell input nested too deeply");
4877 /* We did not overflow the NPUSH array spots so setup data structs */
4879 e.iop->iofn = (int (*)(struct ioarg *, struct io *)) fn; /* Store data source func ptr */
4881 if (argp->afid != AFID_NOBUF)
4885 e.iop->argp = ioargstack + (e.iop - iostack); /* MAL - index into stack */
4886 *e.iop->argp = *argp; /* copy data from temp area into stack spot */
4888 /* MAL - mainbuf is for 1st data source (command line?) and all nested use a single shared buffer? */
4890 if (e.iop == &iostack[0])
4891 e.iop->argp->afbuf = &mainbuf;
4893 e.iop->argp->afbuf = &sharedbuf;
4895 /* MAL - if not a termimal AND (commandline OR readable file) then give it a buffer id? */
4896 /* This line appears to be active when running scripts from command line */
4897 if ((isatty(e.iop->argp->afile) == 0)
4898 && (e.iop == &iostack[0]
4899 || lseek(e.iop->argp->afile, 0L, 1) != -1)) {
4900 if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */
4901 bufid = AFID_ID; /* AFID_ID = 0 */
4903 e.iop->argp->afid = bufid; /* assign buffer id */
4906 DBGPRINTF(("PUSHIO: iostack 0x%x, e.iop 0x%x, afbuf 0x%x\n",
4907 iostack, e.iop, e.iop->argp->afbuf));
4908 DBGPRINTF(("PUSHIO: mbuf 0x%x, sbuf 0x%x, bid %d, e.iop 0x%x\n",
4909 &mainbuf, &sharedbuf, bufid, e.iop));
4913 e.iop->prev = ~'\n';
4918 if (fn == filechar || fn == linechar)
4920 else if (fn == (int (*)(struct ioarg *)) gravechar
4921 || fn == (int (*)(struct ioarg *)) qgravechar)
4922 e.iop->task = XGRAVE;
4924 e.iop->task = XOTHER;
4929 static struct io *setbase(ip)
4932 REGISTER struct io *xp;
4940 * Input generating functions
4944 * Produce the characters of a string, then a newline, then EOF.
4946 static int nlchar(ap)
4947 REGISTER struct ioarg *ap;
4951 if (ap->aword == NULL)
4953 if ((c = *ap->aword++) == 0) {
4961 * Given a list of words, produce the characters
4962 * in them, with a space after each word.
4964 static int wdchar(ap)
4965 REGISTER struct ioarg *ap;
4970 if ((wl = ap->awordlist) == NULL)
4973 if ((c = *(*wl)++) != 0)
4978 ap->awordlist = NULL;
4983 * Return the characters of a list of words,
4984 * producing a space between them.
4986 static int dolchar(ap)
4987 REGISTER struct ioarg *ap;
4991 if ((wp = *ap->awordlist++) != NULL) {
4992 PUSHIO(aword, wp, *ap->awordlist == NULL ? strchar : xxchar);
4998 static int xxchar(ap)
4999 REGISTER struct ioarg *ap;
5003 if (ap->aword == NULL)
5005 if ((c = *ap->aword++) == '\0') {
5013 * Produce the characters from a single word (string).
5015 static int strchar(ap)
5016 REGISTER struct ioarg *ap;
5020 if (ap->aword == NULL || (c = *ap->aword++) == 0)
5026 * Produce quoted characters from a single word (string).
5028 static int qstrchar(ap)
5029 REGISTER struct ioarg *ap;
5033 if (ap->aword == NULL || (c = *ap->aword++) == 0)
5039 * Return the characters from a file.
5041 static int filechar(ap)
5042 REGISTER struct ioarg *ap;
5046 struct iobuf *bp = ap->afbuf;
5048 if (ap->afid != AFID_NOBUF) {
5049 if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
5052 lseek(ap->afile, ap->afpos, 0);
5054 i = safe_read(ap->afile, bp->buf, sizeof(bp->buf));
5062 bp->ebufp = (bp->bufp = bp->buf) + i;
5066 return *bp->bufp++ & 0177;
5068 #ifdef CONFIG_FEATURE_COMMAND_EDITING
5069 if (interactive && isatty(ap->afile)) {
5070 static char mycommand[BUFSIZ];
5071 static int position = 0, size = 0;
5073 while (size == 0 || position >= size) {
5074 cmdedit_read_input(current_prompt, mycommand);
5075 size = strlen(mycommand);
5078 c = mycommand[position];
5085 i = safe_read(ap->afile, &c, sizeof(c));
5086 return (i == sizeof(c) ? (c & 0x7f) : (closef(ap->afile), 0));
5091 * Return the characters from a here temp file.
5093 static int herechar(ap)
5094 REGISTER struct ioarg *ap;
5099 if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
5108 * Return the characters produced by a process (`...`).
5109 * Quote them if required, and remove any trailing newline characters.
5111 static int gravechar(ap, iop)
5117 if ((c = qgravechar(ap, iop) & ~QUOTE) == '\n')
5122 static int qgravechar(ap, iop)
5123 REGISTER struct ioarg *ap;
5128 DBGPRINTF3(("QGRAVECHAR: enter, ap=0x%x, iop=0x%x\n", ap, iop));
5133 return ('\n' | QUOTE);
5137 } else if ((c = filechar(ap)) == '\n') {
5139 while ((c = filechar(ap)) == '\n')
5147 return (c != 0 ? c | QUOTE : 0);
5151 * Return a single command (usually the first line) from a file.
5153 static int linechar(ap)
5154 REGISTER struct ioarg *ap;
5158 if ((c = filechar(ap)) == '\n') {
5161 ap->afile = -1; /* illegal value */
5171 write(2, s, strlen(s));
5180 static void closef(i)
5187 static void closeall()
5191 for (u = NUFILE; u < NOFILE;)
5197 * remap fd into Shell's fd space
5199 static int remap(fd)
5207 DBGPRINTF(("REMAP: fd=%d, e.iofd=%d\n", fd, e.iofd));
5210 for (i = 0; i < NOFILE; i++)
5217 } while (fd >= 0 && fd < e.iofd);
5219 for (i = 0; i < NOFILE; i++)
5224 err("too many files open in shell");
5230 static int openpipe(pv)
5235 if ((i = pipe(pv)) < 0)
5236 err("can't create pipe - try again");
5240 static void closepipe(pv)
5249 /* -------- here.c -------- */
5255 static void markhere(s, iop)
5259 REGISTER struct here *h, *lh;
5261 DBGPRINTF7(("MARKHERE: enter, s=0x%x\n", s));
5263 h = (struct here *) space(sizeof(struct here));
5267 h->h_tag = evalstr(s, DOSUB);
5277 for (lh = inhere; lh != NULL; lh = lh->h_next)
5278 if (lh->h_next == 0) {
5282 iop->io_flag |= IOHERE | IOXHERE;
5283 for (s = h->h_tag; *s; s++)
5285 iop->io_flag &= ~IOXHERE;
5288 h->h_dosub = iop->io_flag & IOXHERE;
5291 static void gethere()
5293 REGISTER struct here *h, *hp;
5295 DBGPRINTF7(("GETHERE: enter...\n"));
5297 /* Scan here files first leaving inhere list in place */
5298 for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
5299 readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub ? 0 : '\'');
5301 /* Make inhere list active - keep list intact for scraphere */
5303 hp->h_next = acthere;
5309 static void readhere(name, s, ec)
5315 char tname[30] = ".msh_XXXXXX";
5318 char myline[LINELIM + 1];
5321 DBGPRINTF7(("READHERE: enter, name=0x%x, s=0x%x\n", name, s));
5323 tf = mkstemp(tname);
5327 *name = strsave(tname, areanum);
5328 if (newenv(setjmp(errpt = ev)) != 0)
5331 pushio(e.iop->argp, (int (*)(struct ioarg *)) e.iop->iofn);
5334 if (interactive && e.iop <= iostack) {
5335 #ifdef CONFIG_FEATURE_COMMAND_EDITING
5336 current_prompt = cprompt->value;
5338 prs(cprompt->value);
5342 while ((c = my_getc(ec)) != '\n' && c) {
5345 if (thenext >= &myline[LINELIM]) {
5352 if (strcmp(s, myline) == 0 || c == 0)
5355 write(tf, myline, (int) (thenext - myline));
5358 prs("here document `");
5368 * open here temp file.
5369 * if unquoted here, expand here temp file into second temp file.
5371 static int herein(hname, xdoll)
5379 /* Avoid longjmp clobbering */
5385 DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll));
5387 hf = open(hname, 0);
5393 char tname[30] = ".msh_XXXXXX";
5396 tf = mkstemp(tname);
5399 if (newenv(setjmp(errpt = ev)) == 0) {
5400 PUSHIO(afile, hf, herechar);
5402 while ((c = subgetc(0, 0)) != 0) {
5404 write(tf, &c, sizeof c);
5410 tf = open(tname, 0);
5417 static void scraphere()
5419 REGISTER struct here *h;
5421 DBGPRINTF7(("SCRAPHERE: enter...\n"));
5423 for (h = inhere; h != NULL; h = h->h_next) {
5424 if (h->h_iop && h->h_iop->io_name)
5425 unlink(h->h_iop->io_name);
5430 /* unlink here temp files before a freearea(area) */
5431 static void freehere(area)
5434 REGISTER struct here *h, *hl;
5436 DBGPRINTF6(("FREEHERE: enter, area=%d\n", area));
5439 for (h = acthere; h != NULL; h = h->h_next)
5440 if (getarea((char *) h) >= area) {
5441 if (h->h_iop->io_name != NULL)
5442 unlink(h->h_iop->io_name);
5444 acthere = h->h_next;
5446 hl->h_next = h->h_next;
5454 * Copyright (c) 1987,1997, Prentice Hall
5455 * All rights reserved.
5457 * Redistribution and use of the MINIX operating system in source and
5458 * binary forms, with or without modification, are permitted provided
5459 * that the following conditions are met:
5461 * Redistributions of source code must retain the above copyright
5462 * notice, this list of conditions and the following disclaimer.
5464 * Redistributions in binary form must reproduce the above
5465 * copyright notice, this list of conditions and the following
5466 * disclaimer in the documentation and/or other materials provided
5467 * with the distribution.
5469 * Neither the name of Prentice Hall nor the names of the software
5470 * authors or contributors may be used to endorse or promote
5471 * products derived from this software without specific prior
5472 * written permission.
5474 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
5475 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
5476 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
5477 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5478 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
5479 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5480 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5481 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
5482 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
5483 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
5484 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
5485 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.