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 /* -------- sh.h -------- */
58 #define NPUSH 8 /* limit to input nesting */
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 */
66 * values returned by wait
68 #define WAITSIG(s) ((s)&0177)
69 #define WAITVAL(s) (((s)>>8)&0377)
70 #define WAITCORE(s) (((s)&0200)!=0)
73 * library and system defintions
75 typedef void xint; /* base type of jmp_buf, for not broken compilers */
83 #define NOBLOCK ((struct op *)NULL)
84 #define NOWORD ((char *)NULL)
85 #define NOWORDS ((char **)NULL)
86 #define NOPIPE ((int *)NULL)
89 * Description of a command or an operation on commands.
90 * Might eventually use a union.
93 int type; /* operation type, see below */
94 char **words; /* arguments to a command */
95 struct ioword **ioact; /* IO actions (eg, < > >>) */
98 char *str; /* identifier for case and for */
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 /* && */
114 #define TPAT 14 /* pattern in case */
115 #define TBRACE 15 /* {c-list} */
116 #define TASYNC 16 /* c & */
119 * actions determining the environment of a process
121 #define BIT(i) (1<<(i))
122 #define FEXEC BIT(0) /* execute without forking */
125 * flags to control evaluation of words
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 */
133 #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
139 static int interactive; /* Is this an interactive shell */
141 static int multiline; /* \n changed to ; */
142 static struct op *outtree; /* result from parser */
146 static struct brkcon *brklist;
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);
157 struct brkcon *nextlev;
164 short io_unit; /* unit affected */
165 short io_flag; /* action (below) */
166 char *io_name; /* file name */
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 /* >&- */
176 #define IODEFAULT (-1) /* token for default IO unit */
178 static struct wdblock *wdlist;
179 static struct wdblock *iolist;
182 * parsing & execution environment
196 * -k: look for name=value everywhere on command line
198 * -t: exit after reading and executing one command
201 * -u: unset variables net diagnostic
205 static char *null; /* null value for variable */
206 static int intr; /* interrupt pending */
208 static char *trap[_NSIG+1];
209 static char ourtrap[_NSIG+1];
210 static int trapset; /* trap pending */
212 static int heedint; /* heed interrupt signals */
214 static int yynerrs; /* yacc */
216 static char line[LINELIM];
222 static int(*inbuilt(char *s))(struct op *);
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 );
242 static void onintr (int s ); /* SIGINT handler */
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 );
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 */
265 /* -------- area stuff -------- */
267 #define REGSIZE sizeof(struct region)
269 //#define SHRINKBY 64
273 #define ALIGN (sizeof(int)-1)
283 /* -------- grammar stuff -------- */
307 #define YYERRCODE 300
310 #define CONTIN 01 /* skip new lines to complete command */
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 );
344 /* -------- var.h -------- */
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 */
357 static struct var *vlist; /* dictionary */
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 */
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 );
378 static int execute (struct op *t, int *pin, int *pout, int act );
380 /* -------- io.h -------- */
383 unsigned id; /* buffer id */
384 char buf[512]; /* buffer */
385 char *bufp; /* pointer into buffer */
386 char *ebufp; /* pointer to end of buffer */
389 /* possible arguments to an IO function */
393 int afile; /* file descriptor */
394 unsigned afid; /* buffer id */
395 long afpos; /* file position */
396 struct iobuf *afbuf; /* buffer for this file */
398 //static struct ioarg ioargstack[NPUSH];
399 #define AFID_NOBUF (~0)
402 /* an input generator's state */
404 int (*iofn)(struct ioarg *, struct io *);
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 */
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 */
418 /* in substitution */
419 #define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL)
422 * input generators for IO structure
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 *));
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);
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 );
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)))
466 /* -------- word.h -------- */
468 #define NSTART 16 /* default number of words to allow for initially */
473 /* bounds are arbitrary */
477 static struct wdblock *addword (char *wd, struct wdblock *wb );
478 static struct wdblock *newword (int nw );
479 static char **getwords (struct wdblock *wb );
481 /* -------- area.h -------- */
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 */
494 #define NEW(type) (type *)getcell(sizeof(type))
495 #define DELETE(obj) freecell((char *)obj)
498 /* -------- misc stuff -------- */
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 );
551 struct ioword *h_iop;
555 static char *signame[] = {
558 (char *)NULL, /* interrupt */
560 "Illegal instruction",
564 "Floating Point Exception",
569 (char *)NULL, /* broken pipe */
573 #define NSIGNAL (sizeof(signame)/sizeof(signame[0]))
579 static struct res restab[] = {
605 int (*builtinfunc)(struct op *t);
607 static const struct builtincmd builtincmds[] = {
612 {"continue",docontinue},
616 {"export", doexport},
621 {"readonly",doreadonly},
632 extern char **environ; /* environment pointer */
637 static int interactive; /* Is this an interactive shell */
639 static int multiline; /* \n changed to ; */
640 static struct op *outtree; /* result from parser */
643 static struct brkcon *brklist;
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 */
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 char shellname[] = "/bin/sh";
675 static int iounit = IODEFAULT;
676 static YYSTYPE yylval;
677 static struct iobuf sharedbuf = {AFID_NOBUF};
678 static struct iobuf mainbuf = {AFID_NOBUF};
679 static unsigned bufid = AFID_ID; /* buffer id counter */
680 static struct ioarg temparg = {0, 0, 0, AFID_NOBUF, 0};
681 static struct here *inhere; /* list of hear docs while parsing */
682 static struct here *acthere; /* list of active here documents */
683 static struct region *areabot; /* bottom of area */
684 static struct region *areatop; /* top of area */
685 static struct region *areanxt; /* starting point of scan */
686 static void * brktop;
687 static void * brkaddr;
690 #ifdef CONFIG_FEATURE_COMMAND_EDITING
691 static char * current_prompt;
694 /* -------- sh.c -------- */
700 extern int msh_main(int argc, char **argv)
706 int (*iof)(struct ioarg *);
709 if ((ap = environ) != NULL) {
711 assign(*ap++, !COPYV);
712 for (ap = environ; *ap;)
713 export(lookup(*ap++));
718 shell = lookup("SHELL");
719 if (shell->value == null)
720 setval(shell, shellname);
723 homedir = lookup("HOME");
724 if (homedir->value == null)
725 setval(homedir, "/");
728 setval(lookup("$"), putn(getpid()));
730 path = lookup("PATH");
731 if (path->value == null) {
733 setval(path, "/sbin:/bin:/usr/sbin:/usr/bin");
735 setval(path, "/bin:/usr/bin");
740 if (ifs->value == null)
741 setval(ifs, " \t\n");
743 prompt = lookup("PS1");
744 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
745 if (prompt->value == null)
747 setval(prompt, "$ ");
748 if (geteuid() == 0) {
749 setval(prompt, "# ");
750 prompt->status &= ~EXPORT;
752 cprompt = lookup("PS2");
753 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
754 if (cprompt->value == null)
756 setval(cprompt, "> ");
762 if(argv[0][0] == '-' && argv[0][1] != '\0') {
763 for (s = argv[0]+1; *s; s++)
766 prompt->status &= ~EXPORT;
767 cprompt->status &= ~EXPORT;
772 PUSHIO(aword, *++argv, iof = nlchar);
784 prompt->status &= ~EXPORT;
792 if (*s>='a' && *s<='z')
799 if (iof == filechar && --argc > 0) {
802 prompt->status &= ~EXPORT;
803 cprompt->status &= ~EXPORT;
804 if (newfile(name = *++argv))
809 if (e.iop < iostack) {
810 PUSHIO(afile, 0, iof);
811 if (isatty(0) && isatty(1) && !cflag) {
813 #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
814 printf( "\n\n" BB_BANNER " Built-in shell (msh)\n");
815 printf( "Enter 'help' for a list of built-in commands.\n\n");
819 signal(SIGQUIT, qflag);
820 if (name && name[0] == '-') {
822 if ((f = open(".profile", 0)) >= 0)
824 if ((f = open("/etc/profile", 0)) >= 0)
828 signal(SIGTERM, sig);
829 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
830 signal(SIGINT, onintr);
835 for (ap = ++argv; --argc > 0;) {
836 if (assign(*ap = *argv++, !COPYV)) {
837 dolc--; /* keyword */
843 setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
846 if (interactive && e.iop <= iostack) {
847 #ifdef CONFIG_FEATURE_COMMAND_EDITING
848 current_prompt=prompt->value;
854 /* Ensure that getenv("PATH") stays current */
855 setenv("PATH", path->value, 1);
867 for (c='a'; c<='z'; c++)
871 setval(lookup("-"), m);
880 if (strcmp(s, "-") != 0) {
884 err(": cannot open");
914 setjmp(failpt = m1); /* Bruce Evans' fix */
915 if (setjmp(failpt = m1) || yyparse() || intr) {
919 if (!interactive && intr)
930 execute(outtree, NOPIPE, NOPIPE, 0);
931 if (!interactive && intr) {
935 if ((i = trapset) != 0) {
985 e.iop = e.iobase = iostack;
992 register struct env *ep;
998 ep = (struct env *) space(sizeof(*ep));
1013 register struct env *ep;
1016 if ((ep = e.oenv) != NULL) {
1019 /* should close `'d files */
1021 while (--fd >= e.iofd)
1027 * Is any character from s1 in s2?
1031 register char *s1, *s2;
1040 * Is character c in s?
1065 snprintf(s, sizeof(s), "%u", n);
1072 PUSHIO(afile, f, filechar);
1077 int s; /* ANSI C requires a parameter */
1079 signal(SIGINT, onintr);
1099 if ((cp = getcell(n)) == 0)
1100 err("out of string space");
1109 register char *cp, *xp;
1111 if ((cp = space(strlen(s)+1)) != NULL) {
1112 setarea((char *)cp, a);
1113 for (xp = cp; (*xp++ = *s++) != '\0';)
1131 static void runtrap(i)
1136 if ((trapstr = trap[i]) == NULL)
1140 RUN(aword, trapstr, nlchar);
1143 /* -------- var.c -------- */
1146 * Find the given name in the dictionary
1147 * and return its value. If the name was
1148 * not previously there, enter it now and
1149 * return a null value.
1155 register struct var *vp;
1158 static struct var dummy;
1162 for (c = 0; isdigit(*n) && c < 1000; n++)
1164 dummy.status = RONLY;
1165 dummy.value = c <= dolc? dolv[c]: null;
1168 for (vp = vlist; vp; vp = vp->next)
1169 if (eqname(vp->name, n))
1172 vp = (struct var *)space(sizeof(*vp));
1173 if (vp == 0 || (vp->name = space((int)(cp-n)+2)) == 0) {
1174 dummy.name = dummy.value = "";
1177 for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++)
1182 setarea((char *)vp, 0);
1183 setarea((char *)vp->name, 0);
1186 vp->status = GETCELL;
1192 * give variable at `vp' the value `val'.
1199 nameval(vp, val, (char *)NULL);
1203 * if name is not NULL, it must be
1204 * a prefix of the space `val',
1206 * this is all so that exporting
1207 * values is reasonably painless.
1210 nameval(vp, val, name)
1211 register struct var *vp;
1214 register char *cp, *xp;
1218 if (vp->status & RONLY) {
1219 for (xp = vp->name; *xp && *xp != '=';)
1220 putc(*xp++, stderr);
1221 err(" is read-only");
1226 xp = space(strlen(vp->name)+strlen(val)+2);
1229 /* make string: name=value */
1230 setarea((char *)xp, 0);
1232 for (cp = vp->name; (*xp = *cp++) && *xp!='='; xp++)
1237 for (cp = val; (*xp++ = *cp++) != '\0';)
1242 if (vp->status & GETCELL)
1243 freecell(vp->name); /* form new string `name=value' */
1253 vp->status |= EXPORT;
1260 if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */
1261 vp->status |= RONLY;
1268 if (!isalpha((int)*s) && *s != '_')
1270 for (; *s != '='; s++)
1271 if (*s == 0 || (!isalnum(*s) && *s != '_'))
1284 if (!isalpha(*s) && *s != '_')
1286 for (cp = s; *cp != '='; cp++)
1287 if (*cp == 0 || (!isalnum(*cp) && *cp != '_'))
1290 nameval(vp, ++cp, cf == COPYV? (char *)NULL: s);
1292 vp->status &= ~GETCELL;
1300 if (!isalpha(*cp++) && *(cp-1) != '_')
1303 if (!isalnum(*cp++) && *(cp-1) != '_')
1310 register int f, out;
1312 register struct var *vp;
1314 for (vp = vlist; vp; vp = vp->next)
1315 if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) {
1316 if (vp->status & EXPORT)
1317 write(out, "export ", 7);
1318 if (vp->status & RONLY)
1319 write(out, "readonly ", 9);
1320 write(out, vp->name, (int)(findeq(vp->name) - vp->name));
1321 write(out, "\n", 1);
1327 register char *n1, *n2;
1329 for (; *n1 != '=' && *n1 != 0; n1++)
1332 return(*n2 == 0 || *n2 == '=');
1339 while (*cp != '\0' && *cp != '=')
1344 /* -------- gmatch.c -------- */
1346 * int gmatch(string, pattern)
1347 * char *string, *pattern;
1349 * Match a pattern as in sh(1).
1354 #define QMASK (CMASK&~QUOTE)
1355 #define NOT '!' /* might use ^ */
1359 register char *s, *p;
1361 register int sc, pc;
1363 if (s == NULL || p == NULL)
1365 while ((pc = *p++ & CMASK) != '\0') {
1369 if ((p = cclass(p, sc)) == NULL)
1381 if (*p == '\0' || gmatch(s, p))
1383 } while (*s++ != '\0');
1387 if (sc != (pc&~QUOTE))
1399 register int c, d, not, found;
1401 if ((not = *p == NOT) != 0)
1406 return((char *)NULL);
1408 if (p[1] == '-' && p[2] != ']') {
1413 if (c == sub || (c <= sub && sub <= d))
1415 } while (*++p != ']');
1416 return(found? p+1: (char *)NULL);
1420 /* -------- area.c -------- */
1423 * All memory between (char *)areabot and (char *)(areatop+1) is
1424 * exclusively administered by the area management routines.
1425 * It is assumed that sbrk() and brk() manipulate the high end.
1428 #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;})
1433 brkaddr = malloc(65000);
1434 brktop = brkaddr + 65000;
1436 while ((int)sbrk(0) & ALIGN)
1438 areabot = (struct region *)sbrk(REGSIZE);
1440 areabot->next = areabot;
1441 areabot->area = BUSY;
1450 register int nregio;
1451 register struct region *p, *q;
1457 } /* silly and defeats the algorithm */
1459 * round upwards and add administration area
1461 nregio = (nbytes+(REGSIZE-1))/REGSIZE + 1;
1462 for (p = areanxt;;) {
1463 if (p->area > areanum) {
1467 while ((q = p->next)->area > areanum && q != areanxt)
1470 * exit loop if cell big enough
1472 if (q >= p + nregio)
1479 i = nregio >= GROWBY ? nregio : GROWBY;
1480 p = (struct region *)sbrk(i * REGSIZE);
1481 if (p == (struct region *)-1)
1482 return((char *)NULL);
1486 abort(); /* allocated areas are contiguous */
1496 * we found a FREE area big enough, pointed to by 'p', and up to 'q'
1498 areanxt = p + nregio;
1501 * split into requested area and rest
1503 if (areanxt+1 > q) {
1505 abort(); /* insufficient space left for admin */
1508 areanxt->area = FREE;
1512 return((char *)(p+1));
1519 register struct region *p;
1521 if ((p = (struct region *)cp) != NULL) {
1533 register struct region *p, *top;
1536 for (p = areabot; p != top; p = p->next)
1546 register struct region *p;
1548 if ((p = (struct region *)cp) != NULL)
1556 return ((struct region*)cp-1)->area;
1562 register struct region *p, *q, *top;
1565 for (p = areabot; p != top; p = p->next) {
1566 if (p->area > areanum) {
1567 while ((q = p->next)->area > areanum)
1573 if (areatop >= q + SHRINKBY && q->area > areanum) {
1582 /* -------- csyn.c -------- */
1584 * shell: syntax (C version)
1603 register struct op *t, *p;
1608 while ((c = yylex(0)) == '|') {
1609 if ((p = command(CONTIN)) == NULL)
1611 if (t->type != TPAREN && t->type != TCOM) {
1612 /* shell statement */
1613 t = block(TPAREN, t, NOBLOCK, NOWORDS);
1615 t = block(TPIPE, t, p, NOWORDS);
1625 register struct op *t, *p;
1630 while ((c = yylex(0)) == LOGAND || c == LOGOR) {
1631 if ((p = pipeline(CONTIN)) == NULL)
1633 t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS);
1643 register struct op *t, *p;
1648 if((peeksym = yylex(0)) == '&')
1649 t = block(TASYNC, t, NOBLOCK, NOWORDS);
1650 while ((c = yylex(0)) == ';' || c == '&' || (multiline && c == '\n')) {
1651 if ((p = andor()) == NULL)
1653 if((peeksym = yylex(0)) == '&')
1654 p = block(TASYNC, p, NOBLOCK, NOWORDS);
1667 register struct ioword *iop;
1671 if ((c = yylex(cf)) != '<' && c != '>') {
1677 iop = io(iounit, i, yylval.cp);
1680 markhere(yylval.cp, iop);
1688 if ((peeksym = yylex(cf)) != c)
1696 register struct op *t;
1700 switch (peeksym = yylex(0)) {
1725 register struct op *t;
1731 return(block(type, t, NOBLOCK, NOWORDS));
1738 register struct op *t;
1739 struct wdblock *iosave;
1748 switch (c = yylex(cf)) {
1751 if ((t = simple()) == NULL) {
1753 return((struct op *)NULL);
1760 t = nested(TPAREN, ')');
1764 t = nested(TBRACE, '}');
1774 t->words = wordlist();
1775 if ((c = yylex(0)) != '\n' && c != ';')
1777 t->left = dogroup(0);
1785 t->type = c == WHILE? TWHILE: TUNTIL;
1787 t->right = dogroup(1);
1799 musthave(IN, CONTIN);
1801 t->left = caselist();
1811 t->right = thenpart();
1828 register struct op *mylist;
1831 if (c == DONE && onlydone)
1832 return((struct op *)NULL);
1844 register struct op *t;
1846 if ((c = yylex(0)) != THEN) {
1848 return((struct op *)NULL);
1853 if (t->left == NULL)
1855 t->right = elsepart();
1863 register struct op *t;
1865 switch (c = yylex(0)) {
1867 if ((t = c_list()) == NULL)
1875 t->right = thenpart();
1880 return((struct op *)NULL);
1887 register struct op *t;
1890 while ((peeksym = yylex(CONTIN)) != ESAC)
1891 t = list(t, casepart());
1898 register struct op *t;
1902 t->words = pattern();
1905 if ((peeksym = yylex(CONTIN)) != ESAC)
1906 musthave(BREAK, CONTIN);
1920 } while ((c = yylex(0)) == '|');
1931 if ((c = yylex(0)) != IN) {
1933 return((char **)NULL);
1936 while ((c = yylex(0)) == WORD)
1944 * supporting functions
1948 register struct op *t1, *t2;
1954 return(block(TLIST, t1, t2, NOWORDS));
1958 block(type, t1, t2, wp)
1963 register struct op *t;
1977 register struct res *rp;
1979 for (rp = restab; rp->r_name; rp++)
1980 if (strcmp(rp->r_name, n) == 0)
1988 register struct op *t;
1990 t = (struct op *)tree(sizeof(*t));
2002 register struct op *t;
2005 iolist = addword((char *)NULL, iolist);
2006 t->ioact = copyio();
2009 if (t->type != TCOM) {
2010 if (t->type != TPAREN && t->ioact != NULL) {
2011 t = block(TPAREN, t, NOBLOCK, NOWORDS);
2012 t->ioact = t->left->ioact;
2013 t->left->ioact = NULL;
2027 wd = getwords(wdlist);
2036 wdlist = addword(cp, wdlist);
2039 static struct ioword **
2042 register struct ioword **iop;
2044 iop = (struct ioword **) getwords(iolist);
2049 static struct ioword *
2055 register struct ioword *iop;
2057 iop = (struct ioword *) tree(sizeof(*iop));
2061 iolist = addword((char *)iop, iolist);
2068 yyerror("syntax error");
2076 if (interactive && e.iop <= iostack) {
2078 while (eofc() == 0 && yylex(0) != '\n')
2092 if ((c = peeksym) > 0) {
2105 while ((c = my_getc(0)) == ' ' || c == '\t')
2109 if (any(c, "0123456789")) {
2110 unget(c1 = my_getc(0));
2111 if (c1 == '<' || c1 == '>') {
2121 while ((c = my_getc(0)) != 0 && c != '\n')
2131 if ((c = my_getc(0)) == '{') {
2132 if ((c = collect(c, '}')) != '\0')
2141 if ((c = collect(c, c)) != '\0')
2148 if ((c1 = dual(c)) != '\0') {
2166 if (multiline || cf & CONTIN) {
2167 if (interactive && e.iop <= iostack) {
2168 #ifdef CONFIG_FEATURE_COMMAND_EDITING
2169 current_prompt=cprompt->value;
2171 prs(cprompt->value);
2188 while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n"))
2189 if (e.linep >= elinep)
2190 err("word too long");
2197 if (atstart && (c = rlookup(line))!=0) {
2201 yylval.cp = strsave(line, areanum);
2212 while ((c = my_getc(c1)) != c1) {
2217 prs("no closing "); yyerror(s);
2220 if (interactive && c == '\n' && e.iop <= iostack) {
2221 #ifdef CONFIG_FEATURE_COMMAND_EDITING
2222 current_prompt=cprompt->value;
2224 prs(cprompt->value);
2238 register char *cp = s;
2243 if ((c = rlookup(s)) == 0)
2255 if (c == '>' || c == '<') {
2258 yylval.i = ec == '>'? IOWRITE|IOCAT: IOHERE;
2261 yylval.i = ec == '>'? IOWRITE: IOREAD;
2262 if (c != '&' || yylval.i == IOHERE)
2274 if ((t = getcell(size)) == NULL) {
2275 prs("command line too complicated\n");
2285 /* -------- exec.c -------- */
2293 execute(t, pin, pout, act)
2294 register struct op *t;
2298 register struct op *t1;
2299 volatile int i, rv, a;
2300 char *cp, **wp, **wp2;
2305 /* Avoid longjmp clobbering */
2314 wp = (wp2 = t->words) != NULL
2315 ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
2320 rv = execute(t->left, pin, pout, 0);
2326 rv = forkexec(t, pin, pout, act, wp, &child);
2337 if ((rv = openpipe(pv)) < 0)
2339 pv[0] = remap(pv[0]);
2340 pv[1] = remap(pv[1]);
2341 (void) execute(t->left, pin, pv, 0);
2342 rv = execute(t->right, pv, pout, 0);
2347 (void) execute(t->left, pin, pout, 0);
2348 rv = execute(t->right, pin, pout, 0);
2353 int hinteractive = interactive;
2357 interactive = hinteractive;
2359 setval(lookup("!"), putn(i));
2370 signal(SIGINT, SIG_IGN);
2371 signal(SIGQUIT, SIG_IGN);
2373 signal(SIGTERM, SIG_DFL);
2377 open("/dev/null", 0);
2379 exit(execute(t->left, pin, pout, FEXEC));
2386 rv = execute(t->left, pin, pout, 0);
2387 if ((t1 = t->right)!=NULL && (rv == 0) == (t->type == TAND))
2388 rv = execute(t1, pin, pout, 0);
2398 while (*wp++ != NULL)
2401 vp = lookup(t->str);
2402 while (setjmp(bc.brkpt))
2406 for (t1 = t->left; i-- && *wp != NULL;) {
2408 rv = execute(t1, pin, pout, 0);
2410 brklist = brklist->nextlev;
2415 while (setjmp(bc.brkpt))
2420 while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE))
2421 rv = execute(t->right, pin, pout, 0);
2422 brklist = brklist->nextlev;
2427 if (t->right != NULL) {
2428 rv = !execute(t->left, pin, pout, 0) ?
2429 execute(t->right->left, pin, pout, 0):
2430 execute(t->right->right, pin, pout, 0);
2435 if ((cp = evalstr(t->str, DOSUB|DOTRIM)) == 0)
2437 if ((t1 = findcase(t->left, cp)) != NULL)
2438 rv = execute(t1, pin, pout, 0);
2443 if (iopp = t->ioact)
2445 if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
2450 if (rv >= 0 && (t1 = t->left))
2451 rv = execute(t1, pin, pout, 0);
2461 if (interactive && intr) {
2465 if ((i = trapset) != 0) {
2473 forkexec( register struct op *t, int *pin, int *pout, int act, char **wp, int *pforked)
2476 int (*shcom)(struct op *) = NULL;
2479 struct ioword **iopp;
2489 struct brkcon * hbrklist;
2493 /* Avoid longjmp clobbering */
2506 rv = -1; /* system-detected error */
2507 if (t->type == TCOM) {
2508 while ((cp = *wp++) != NULL)
2512 /* strip all initial assignments */
2513 /* not correct wrt PATH=yyy command etc */
2515 echo (cp ? wp: owp);
2516 if (cp == NULL && t->ioact == NULL) {
2517 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2519 return(setstatus(0));
2521 else if (cp != NULL)
2522 shcom = inbuilt(cp);
2526 if (shcom == NULL && (f & FEXEC) == 0) {
2532 hinteractive = interactive;
2539 /* who wrote this crappy non vfork safe shit? */
2544 interactive = hinteractive;
2554 return(pout==NULL? setstatus(waitfor(i,0)): 0);
2558 signal(SIGINT, SIG_IGN);
2559 signal(SIGQUIT, SIG_IGN);
2569 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2573 if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) {
2574 err("piping to/from shell builtins not yet done");
2586 if ((iopp = t->ioact) != NULL) {
2587 if (shcom != NULL && shcom != doexec) {
2589 err(": cannot redirect shell command");
2593 if (iosetup(*iopp++, pin!=NULL, pout!=NULL))
2597 return(setstatus((*shcom)(t)));
2598 /* should use FIOCEXCL */
2599 for (i=FDBASE; i<NOFILE; i++)
2602 signal(SIGINT, SIG_DFL);
2603 signal(SIGQUIT, SIG_DFL);
2605 if (t->type == TPAREN)
2606 exit(execute(t->left, NOPIPE, NOPIPE, FEXEC));
2610 cp = rexecve(wp[0], wp, makenv());
2611 prs(wp[0]); prs(": "); warn(cp);
2620 * 0< 1> are ignored as required
2624 iosetup(iop, pipein, pipeout)
2625 register struct ioword *iop;
2626 int pipein, pipeout;
2628 register int u = -1;
2629 char *cp=NULL, *msg;
2631 if (iop->io_unit == IODEFAULT) /* take default */
2632 iop->io_unit = iop->io_flag&(IOREAD|IOHERE)? 0: 1;
2633 if (pipein && iop->io_unit == 0)
2635 if (pipeout && iop->io_unit == 1)
2637 msg = iop->io_flag&(IOREAD|IOHERE)? "open": "create";
2638 if ((iop->io_flag & IOHERE) == 0) {
2640 if ((cp = evalstr(cp, DOSUB|DOTRIM)) == NULL)
2643 if (iop->io_flag & IODUP) {
2644 if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
2646 err(": illegal >& argument");
2650 iop->io_flag = IOCLOSE;
2651 iop->io_flag &= ~(IOREAD|IOWRITE);
2653 switch (iop->io_flag) {
2659 case IOHERE|IOXHERE:
2660 u = herein(iop->io_name, iop->io_flag&IOXHERE);
2665 if ((u = open(cp, 1)) >= 0) {
2666 lseek(u, (long)0, 2);
2670 u = creat(cp, 0666);
2674 u = dup2(*cp-'0', iop->io_unit);
2678 close(iop->io_unit);
2687 if (u != iop->io_unit) {
2688 dup2(u, iop->io_unit);
2702 for (i=0; wp[i]; i++) {
2715 register struct op *t1;
2717 register char **wp, *cp;
2720 return((struct op **)NULL);
2721 if (t->type == TLIST) {
2722 if ((tp = find1case(t->left, w)) != NULL)
2724 t1 = t->right; /* TPAT */
2727 for (wp = t1->words; *wp;)
2728 if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp))
2730 return((struct op **)NULL);
2738 register struct op **tp;
2740 return((tp = find1case(t, w)) != NULL? *tp: (struct op *)NULL);
2744 * Enter a new loop level (marked for break/continue).
2750 bc->nextlev = brklist;
2755 * Wait for the last process created.
2756 * Print a message for each process found
2757 * that was killed by a signal.
2758 * Ignore interrupt signals while waiting
2759 * unless `canintr' is true.
2762 waitfor(lastpid, canintr)
2763 register int lastpid;
2766 register int pid, rv;
2768 int oheedint = heedint;
2775 if (errno != EINTR || canintr)
2778 if ((rv = WAITSIG(s)) != 0) {
2780 if (signame[rv] != NULL) {
2781 if (pid != lastpid) {
2788 if (pid != lastpid) {
2792 prs("Signal "); prn(rv); prs(" ");
2795 prs(" - core dumped");
2796 if (rv >= NSIGNAL || signame[rv])
2802 } while (pid != lastpid);
2809 if (exstat == 0) exstat = rv;
2821 setval(lookup("?"), putn(s));
2826 * PATH-searching interface to execve.
2827 * If getenv("PATH") were kept up-to-date,
2828 * execvp might be used.
2832 char *c, **v, **envp;
2835 register char *sp, *tp;
2836 int eacces = 0, asis = 0;
2838 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
2840 #ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN
2841 name = bb_get_last_path_component(name);
2844 if (find_applet_by_name(name)) {
2845 /* We have to exec here since we vforked. Running
2846 * run_applet_by_name() won't work and bad things
2848 execve("/proc/self/exe", v, envp);
2849 execve("busybox", v, envp);
2853 sp = any('/', c)? "": path->value;
2855 while (asis || *sp != '\0') {
2858 for (; *sp != '\0'; tp++)
2859 if ((*tp = *sp++) == ':') {
2865 for (i = 0; (*tp++ = c[i++]) != '\0';)
2868 execve(e.linep, v, envp);
2874 execve(shellname, v, envp);
2879 return((char*)bb_msg_memory_exhausted);
2882 return("argument list too long");
2889 return(errno==ENOENT ? "not found" : "cannot execute");
2893 * Run the command produced by generator `f'
2894 * applied to stream `arg'.
2897 run(struct ioarg *argp, int (*f)(struct ioarg *))
2900 struct wdblock *swdlist;
2901 struct wdblock *siolist;
2907 /* Avoid longjmp clobbering */
2917 if (newenv(setjmp(errpt = ev)) == 0) {
2923 if (setjmp(failpt = rt) == 0 && yyparse() == 0)
2924 rv = execute(outtree, NOPIPE, NOPIPE, 0);
2931 freearea(areanum--);
2935 /* -------- do.c -------- */
2938 * built-in commands: doX
2941 static int dohelp(struct op *t )
2944 const struct builtincmd *x;
2946 printf("\nBuilt-in commands:\n");
2947 printf("-------------------\n");
2949 for (col=0, x = builtincmds; x->builtinfunc != NULL; x++) {
2952 col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name);
2958 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
2961 const struct BB_applet *applet;
2962 extern const struct BB_applet applets[];
2963 extern const size_t NUM_APPLETS;
2965 for (i=0, applet = applets; i < NUM_APPLETS; applet++, i++) {
2969 col += printf("%s%s", ((col == 0) ? "\t" : " "),
2979 return EXIT_SUCCESS;
2984 static int dolabel(struct op *t )
2991 register struct op *t;
2993 register char *cp, *er;
2995 if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL)
2996 er = ": no home directory";
2997 else if(chdir(cp) < 0)
2998 er = ": bad directory";
3001 prs(cp != NULL? cp: "cd");
3008 register struct op *t;
3012 n = t->words[1]? getn(t->words[1]): 1;
3014 err("nothing to shift");
3020 setval(lookup("#"), putn(dolc));
3025 * execute login and newgrp directly
3034 signal(SIGINT, SIG_DFL);
3035 signal(SIGQUIT, SIG_DFL);
3037 cp = rexecve(t->words[0], t->words, makenv());
3038 prs(t->words[0]); prs(": "); err(cp);
3044 register struct op *t;
3049 if ((cp = t->words[1]) == NULL) {
3052 for (n=3*4; (n-=3) >= 0;)
3053 putc('0'+((i>>n)&07), stderr);
3056 for (n=0; *cp>='0' && *cp<='9'; cp++)
3057 n = n*8 + (*cp-'0');
3065 register struct op *t;
3072 for(i = 0; (t->words[i]=t->words[i+1]) != NULL; i++)
3078 if (setjmp(failpt = ex) == 0)
3079 execute(t, NOPIPE, NOPIPE, FEXEC);
3090 register char *sp, *tp;
3093 if ((cp = t->words[1]) == NULL)
3095 sp = any('/', cp)? ":": path->value;
3098 while (*sp && (*tp = *sp++) != ':')
3102 for (i = 0; (*tp++ = cp[i++]) != '\0';)
3104 if ((i = open(e.linep, 0)) >= 0) {
3122 if ((cp = t->words[1]) != NULL) {
3128 setstatus(waitfor(i, 1));
3136 register char *cp, **wp;
3137 register int nb = 0;
3138 register int nl = 0;
3140 if (t->words[1] == NULL) {
3141 err("Usage: read name ...");
3144 for (wp = t->words+1; *wp; wp++) {
3145 for (cp = e.linep; !nl && cp < elinep-1; cp++)
3146 if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) ||
3147 (nl = (*cp == '\n')) ||
3148 (wp[1] && any(*cp, ifs->value)))
3153 setval(lookup(*wp), e.linep);
3160 register struct op *t;
3162 return(RUN(awordlist, t->words+1, wdchar));
3167 register struct op *t;
3170 register int resetsig;
3172 if (t->words[1] == NULL) {
3173 for (i=0; i<=_NSIG; i++)
3182 resetsig = isdigit(*t->words[1]);
3183 for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) {
3184 n = getsig(t->words[i]);
3188 if (*t->words[1] != '\0') {
3189 trap[n] = strsave(t->words[1], 0);
3198 setsig(n, n == SIGQUIT ? SIG_IGN
3213 if ((n = getn(s)) < 0 || n > _NSIG) {
3214 err("trap: bad signal number");
3221 setsig( register int n, sighandler_t f)
3225 if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) {
3244 for (n = 0; isdigit(*s); s++)
3245 n = (n*10) + (*s-'0');
3248 err(": bad number");
3257 return(brkcontin(t->words[1], 1));
3264 return(brkcontin(t->words[1], 0));
3272 register struct brkcon *bc;
3275 nl = cp == NULL? 1: getn(cp);
3279 if ((bc = brklist) == NULL)
3281 brklist = bc->nextlev;
3284 err("bad break/continue level");
3288 longjmp(bc->brkpt, 1);
3299 if ((cp = t->words[1]) != NULL)
3300 setstatus(getn(cp));
3310 rdexp(t->words+1, export, EXPORT);
3318 rdexp(t->words+1, ronly, RONLY);
3322 static void rdexp (char **wp, void (*f)(struct var *), int key)
3325 for (; *wp != NULL; wp++) {
3326 if (isassign(*wp)) {
3329 for (cp = *wp; *cp != '='; cp++)
3347 err(": bad identifier");
3352 register struct op *t;
3354 register struct var *vp;
3358 if ((cp = t->words[1]) == NULL) {
3359 for (vp = vlist; vp; vp = vp->next)
3360 varput(vp->name, 1);
3364 /* bad: t->words++; */
3365 for(n = 0; (t->words[n]=t->words[n+1]) != NULL; n++)
3368 flag['x'] = flag['v'] = 0;
3378 if (*cp>='a' && *cp<='z')
3385 t->words[0] = dolv[0];
3386 for (n=1; t->words[n]; n++)
3387 setarea((char *)t->words[n], 0);
3390 setval(lookup("#"), putn(dolc));
3391 setarea((char *)(dolv-1), 0);
3401 if (isalnum(*s) || *s == '_') {
3402 write(out, s, strlen(s));
3403 write(out, "\n", 1);
3409 * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
3410 * This file contains code for the times builtin.
3412 static int dotimes(struct op *t )
3415 long int clk_tck = sysconf(_SC_CLK_TCK);
3418 printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
3419 (int) (buf.tms_utime / clk_tck / 60),
3420 ((double) buf.tms_utime) / clk_tck,
3421 (int) (buf.tms_stime / clk_tck / 60),
3422 ((double) buf.tms_stime) / clk_tck,
3423 (int) (buf.tms_cutime / clk_tck / 60),
3424 ((double) buf.tms_cutime) / clk_tck,
3425 (int) (buf.tms_cstime / clk_tck / 60),
3426 ((double) buf.tms_cstime) / clk_tck);
3431 static int(*inbuilt(char *s))(struct op *)
3433 const struct builtincmd *bp;
3435 for (bp = builtincmds; bp->name != NULL; bp++)
3436 if (strcmp(bp->name, s) == 0)
3437 return(bp->builtinfunc);
3442 /* -------- eval.c -------- */
3447 * blank interpretation
3452 static char ** eval( char **ap, int f)
3460 /* Avoid longjmp clobbering */
3467 if (newenv(setjmp(errpt = ev)) == 0) {
3468 while (*ap && isassign(*ap))
3469 expand(*ap++, &wb, f & ~DOGLOB);
3471 for (wf = ap; *wf; wf++) {
3473 expand(*wf, &wb, f & ~DOGLOB);
3476 for (wb = addword((char *)0, wb); *ap; ap++) {
3477 if (!flag['k'] || !isassign(*ap))
3478 expand(*ap, &wb, f & ~DOKEY);
3480 wb = addword((char *)0, wb);
3485 return(gflg? (char **)NULL: wp);
3489 * Make the exported environment from the exported
3490 * names in the dictionary. Keyword assignments
3491 * will already have been done.
3497 register struct wdblock *wb;
3498 register struct var *vp;
3501 for (vp = vlist; vp; vp = vp->next)
3502 if (vp->status & EXPORT)
3503 wb = addword(vp->name, wb);
3504 wb = addword((char *)0, wb);
3505 return(getwords(wb));
3516 if (expand(cp, &wb, f)) {
3517 if (wb == NULL || wb->w_nword == 0 || (cp = wb->w_words[0]) == NULL)
3526 expand( char *cp, register struct wdblock **wbp, int f)
3531 /* Avoid longjmp clobbering */
3537 if (!anys("$`'\"", cp) &&
3538 !anys(ifs->value, cp) &&
3539 ((f&DOGLOB)==0 || !anys("[*?", cp))) {
3540 cp = strsave(cp, areanum);
3543 *wbp = addword(cp, *wbp);
3546 if (newenv(setjmp(errpt = ev)) == 0) {
3547 PUSHIO(aword, cp, strchar);
3549 while ((cp = blank(f)) && gflg == 0) {
3551 cp = strsave(cp, areanum);
3552 if ((f&DOGLOB) == 0) {
3555 *wbp = addword(cp, *wbp);
3557 *wbp = glob(cp, *wbp);
3566 * Blank interpretation and quoting
3574 int scanequals, foundequals;
3577 scanequals = f & DOKEY;
3581 switch (c = subgetc('"', foundequals)) {
3589 if (f & DOBLANK && any(c, ifs->value))
3598 for (c1 = c; (c = subgetc(c1, 1)) != c1;) {
3601 if (c == '\'' || !any(c, "$`\""))
3608 if (!isalpha(c) && c != '_')
3611 c = subgetc('"', foundequals);
3613 f & (DOBLANK && any(c, ifs->value)) ||
3614 (!INSUB() && any(c, "\"'"))) {
3626 else if (!isalnum(c) && c != '_')
3636 * Get characters, substituting for ` and $
3647 if (!INSUB() && ec != '\'') {
3649 if (grave(quoted) == 0)
3651 e.iop->task = XGRAVE;
3654 if (c == '$' && (c = dollar(quoted)) == 0) {
3655 e.iop->task = XDOLL;
3663 * Prepare to generate the string returned by ${} substitution.
3672 register char *s, c, *cp=NULL;
3679 if (isalpha(c) || c == '_') {
3680 while ((c = readc())!=0 && (isalnum(c) || c == '_'))
3681 if (e.linep < elinep)
3688 otask = e.iop->task;
3689 e.iop->task = XOTHER;
3690 while ((c = subgetc('"', 0))!=0 && c!='}' && c!='\n')
3691 if (e.linep < elinep)
3694 e.iop->task = otask;
3701 if (e.linep >= elinep) {
3702 err("string in ${} too long");
3708 for (cp = s+1; *cp; cp++)
3709 if (any(*cp, "=-+?")) {
3714 if (s[1] == 0 && (*s == '*' || *s == '@')) {
3716 /* currently this does not distinguish $* and $@ */
3717 /* should check dollar */
3719 PUSHIO(awordlist, dolv+1, dolchar);
3721 } else { /* trap the nasty ${=} */
3727 if ((dolp = vp->value) == null) {
3731 err("cannot use ${...=...} with $n");
3740 dolp = strsave(cp, areanum);
3745 prs("missing value for ");
3752 } else if (c == '+')
3753 dolp = strsave(cp, areanum);
3754 if (flag['u'] && dolp == null) {
3755 prs("unset variable: ");
3760 PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
3765 * Run the command in `...` and read its output.
3776 static char child_cmd[LINELIM];
3782 char *argument_list[4];
3785 /* Avoid longjmp clobbering */
3789 for (cp = e.iop->argp->aword; *cp != '`'; cp++)
3791 err("no closing `");
3795 /* string copy with dollar expansion */
3796 src = e.iop->argp->aword;
3801 while ((*src != '`') && (count < LINELIM)) {
3806 if (*src == '$' && !ignore && !ignore_once) {
3808 char var_name[LINELIM];
3809 char alt_value[LINELIM];
3822 var_name[var_index++] = *src++;
3823 while (isalnum(*src))
3824 var_name[var_index++] = *src++;
3825 var_name[var_index] = 0;
3838 err("unclosed ${\n");
3843 while (*src && (*src != '}')) {
3844 alt_value[alt_index++] = *src++;
3846 alt_value[alt_index] = 0;
3848 err("unclosed ${\n");
3855 vp = lookup(var_name);
3856 if (vp->value != null)
3857 value = (operator == '+')? alt_value : vp->value;
3858 else if (operator == '?') {
3861 } else if (alt_index && (operator != '+')) {
3863 if (operator == '=')
3868 while (*value && (count < LINELIM)) {
3880 if (openpipe(pf) < 0)
3882 while ((i = vfork()) == -1 && errno == EAGAIN)
3886 err((char*)bb_msg_memory_exhausted);
3890 waitpid(i, NULL, 0);
3891 e.iop->argp->aword = ++cp;
3893 PUSHIO(afile, remap(pf[0]), (int(*)(struct ioarg *))((quoted)? qgravechar: gravechar));
3896 /* allow trapped signals */
3897 /* XXX - Maybe this signal stuff should go as well? */
3898 for (j=0; j<=_NSIG; j++)
3899 if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN)
3905 argument_list[0] = shellname;
3906 argument_list[1] = "-c";
3907 argument_list[2] = child_cmd;
3908 argument_list[3] = 0;
3910 prs(rexecve(argument_list[0], argument_list, makenv()));
3921 if ((s = as) != NULL)
3927 /* -------- glob.c -------- */
3933 #define scopy(x) strsave((x), areanum)
3935 #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
3937 static struct wdblock *cl, *nl;
3938 static char spcl[] = "[?*";
3940 static struct wdblock *
3951 for (pp = cp; *pp; pp++)
3954 else if (!any(*pp & ~QUOTE, spcl))
3957 for (cl = addword(scopy(cp), (struct wdblock *)0); anyspcl(cl); cl = nl) {
3958 nl = newword(cl->w_nword*2);
3959 for(i=0; i<cl->w_nword; i++) { /* for each argument */
3960 for (pp = cl->w_words[i]; *pp; pp++)
3961 if (any(*pp, spcl)) {
3962 globname(cl->w_words[i], pp);
3966 nl = addword(scopy(cl->w_words[i]), nl);
3968 for(i=0; i<cl->w_nword; i++)
3969 DELETE(cl->w_words[i]);
3972 for(i=0; i<cl->w_nword; i++)
3973 unquote(cl->w_words[i]);
3974 glob0((char *)cl->w_words, cl->w_nword, sizeof(char *), xstrcmp);
3976 for (i=0; i<cl->w_nword; i++)
3977 wb = addword(cl->w_words[i], wb);
3982 wb = addword(unquote(cp), wb);
3991 register char *np, *cp;
3992 char *name, *gp, *dp;
3996 char dname[NAME_MAX+1];
3999 for (np = we; np != pp; pp--)
4002 for (dp = cp = space((int)(pp-np)+3); np < pp;)
4006 for (gp = cp = space(strlen(pp)+1); *np && *np != '/';)
4015 dname[NAME_MAX] = '\0';
4016 while ((de=readdir(dirp))!=NULL) {
4017 /* XXX Hmmm... What this could be? (abial) */
4019 if (ent[j].d_ino == 0)
4022 strncpy(dname, de->d_name, NAME_MAX);
4023 if (dname[0] == '.')
4026 for(k=0; k<NAME_MAX; k++)
4027 if (any(dname[k], spcl))
4029 if (gmatch(dname, gp)) {
4030 name = generate(we, pp, dname, np);
4031 if (*np && !anys(np, spcl)) {
4032 if (stat(name,&dbuf)) {
4037 nl = addword(name, nl);
4046 * generate a pathname as below.
4047 * start..end1 / middle end
4048 * the slashes come for free
4051 generate(start1, end1, middle, end)
4053 register char *end1;
4057 register char *op, *xp;
4059 p = op = space((int)(end1-start1)+strlen(middle)+strlen(end)+2);
4060 for (xp = start1; xp != end1;)
4062 for (xp = middle; (*op++ = *xp++) != '\0';)
4065 for (xp = end; (*op++ = *xp++) != '\0';)
4072 register struct wdblock *wb;
4078 for (i=0; i<wb->w_nword; i++)
4079 if (anys(spcl, *wd++))
4088 return(strcmp(*(char **)p1, *(char **)p2));
4091 /* -------- word.c -------- */
4093 static struct wdblock *
4097 register struct wdblock *wb;
4099 wb = (struct wdblock *) space(sizeof(*wb) + nw*sizeof(char *));
4105 static struct wdblock *
4108 register struct wdblock *wb;
4110 register struct wdblock *wb2;
4114 wb = newword(NSTART);
4115 if ((nw = wb->w_nword) >= wb->w_bsize) {
4116 wb2 = newword(nw * 2);
4117 memcpy((char *)wb2->w_words, (char *)wb->w_words, nw*sizeof(char *));
4122 wb->w_words[wb->w_nword++] = wd;
4128 register struct wdblock *wb;
4134 return((char **)NULL);
4135 if (wb->w_nword == 0) {
4137 return((char **)NULL);
4139 wd = (char **) space(nb = sizeof(*wd) * wb->w_nword);
4140 memcpy((char *)wd, (char *)wb->w_words, nb);
4141 DELETE(wb); /* perhaps should done by caller */
4145 int (*func)(char *, char *);
4149 glob0(a0, a1, a2, a3)
4153 int (*a3) (char *, char *);
4157 glob1(a0, a0 + a1 * a2);
4164 register char *i, *j;
4174 if ((n=(int)(lim-base)) <= v2)
4176 n = v2 * (n / (2*v2));
4177 hptr = lptr = base+n;
4182 if ((c = (*func)(i, lptr)) == 0) {
4183 glob2(i, lptr -= v2);
4194 if ((c = (*func)(hptr, j)) == 0) {
4195 glob2(hptr += v2, j);
4200 glob3(i, hptr += v2, j);
4215 if (lptr-base >= lim-hptr) {
4216 glob1(hptr+v2, lim);
4226 glob3(j, lptr -= v2, i);
4235 register char *index1, *index2, c;
4243 *index1++ = *index2;
4252 register char *index1, *index2, *index3;
4262 *index1++ = *index3;
4263 *index3++ = *index2;
4268 /* -------- io.c -------- */
4274 static int my_getc( int ec)
4278 if(e.linep > elinep) {
4279 while((c=readc()) != '\n' && c)
4281 err("input line too long");
4286 if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) {
4289 if (c == '\n' && ec != '\"')
4290 return(my_getc(ec));
4301 if (e.iop >= e.iobase)
4309 return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0);
4317 for (; e.iop >= e.iobase; e.iop--)
4318 if ((c = e.iop->peekc) != '\0') {
4323 if (e.iop->prev != 0) {
4324 if ((c = (*e.iop->iofn)(e.iop->argp, e.iop)) != '\0') {
4329 if (e.iop == iostack)
4331 return(e.iop->prev = c);
4333 else if (e.iop->task == XIO && e.iop->prev != '\n') {
4335 if (e.iop == iostack)
4340 if (e.iop->task == XIO) {
4342 return e.iop->prev = 0;
4343 if (interactive && e.iop == iostack+1) {
4344 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4345 current_prompt=prompt->value;
4352 if (e.iop >= iostack)
4364 write(2, &c, sizeof c);
4368 pushio(struct ioarg *argp, int (*fn)(struct ioarg *))
4370 if (++e.iop >= &iostack[NPUSH]) {
4372 err("Shell input nested too deeply");
4376 e.iop->iofn = (int (*)(struct ioarg *, struct io *))fn;
4378 if (argp->afid != AFID_NOBUF)
4381 e.iop->argp = ioargstack + (e.iop - iostack);
4382 *e.iop->argp = *argp;
4383 e.iop->argp->afbuf = e.iop == &iostack[0] ? &mainbuf : &sharedbuf;
4384 if (isatty(e.iop->argp->afile) == 0 &&
4385 (e.iop == &iostack[0] ||
4386 lseek(e.iop->argp->afile, 0L, 1) != -1)) {
4387 if (++bufid == AFID_NOBUF)
4389 e.iop->argp->afid = bufid;
4393 e.iop->prev = ~'\n';
4397 if (fn == filechar || fn == linechar)
4399 else if (fn == (int(*)(struct ioarg *))gravechar || fn == (int(*)(struct ioarg *))qgravechar)
4400 e.iop->task = XGRAVE;
4402 e.iop->task = XOTHER;
4409 register struct io *xp;
4417 * Input generating functions
4421 * Produce the characters of a string, then a newline, then EOF.
4425 register struct ioarg *ap;
4429 if (ap->aword == NULL)
4431 if ((c = *ap->aword++) == 0) {
4439 * Given a list of words, produce the characters
4440 * in them, with a space after each word.
4444 register struct ioarg *ap;
4449 if ((wl = ap->awordlist) == NULL)
4452 if ((c = *(*wl)++) != 0)
4457 ap->awordlist = NULL;
4462 * Return the characters of a list of words,
4463 * producing a space between them.
4467 register struct ioarg *ap;
4471 if ((wp = *ap->awordlist++) != NULL) {
4472 PUSHIO(aword, wp, *ap->awordlist == NULL? strchar: xxchar);
4480 register struct ioarg *ap;
4484 if (ap->aword == NULL)
4486 if ((c = *ap->aword++) == '\0') {
4494 * Produce the characters from a single word (string).
4498 register struct ioarg *ap;
4502 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4508 * Produce quoted characters from a single word (string).
4512 register struct ioarg *ap;
4516 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4522 * Return the characters from a file.
4526 register struct ioarg *ap;
4530 struct iobuf *bp = ap->afbuf;
4532 if (ap->afid != AFID_NOBUF) {
4533 if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
4535 lseek(ap->afile, ap->afpos, 0);
4536 i = safe_read(ap->afile, bp->buf, sizeof(bp->buf));
4542 bp->ebufp = (bp->bufp = bp->buf) + i;
4545 return *bp->bufp++ & 0177;
4548 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4549 if (interactive && isatty(ap->afile)) {
4550 static char mycommand[BUFSIZ];
4551 static int position = 0, size = 0;
4553 while (size == 0 || position >= size) {
4554 cmdedit_read_input(current_prompt, mycommand);
4555 size = strlen(mycommand);
4558 c = mycommand[position];
4564 i = safe_read(ap->afile, &c, sizeof(c));
4565 return(i == sizeof(c)? c&0177: (closef(ap->afile), 0));
4570 * Return the characters from a here temp file.
4574 register struct ioarg *ap;
4579 if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
4588 * Return the characters produced by a process (`...`).
4589 * Quote them if required, and remove any trailing newline characters.
4598 if ((c = qgravechar(ap, iop)&~QUOTE) == '\n')
4605 register struct ioarg *ap;
4617 } else if ((c = filechar(ap)) == '\n') {
4619 while ((c = filechar(ap)) == '\n')
4627 return(c!=0? c|QUOTE: 0);
4631 * Return a single command (usually the first line) from a file.
4635 register struct ioarg *ap;
4639 if ((c = filechar(ap)) == '\n') {
4642 ap->afile = -1; /* illegal value */
4653 write(2, s, strlen(s));
4676 for (u=NUFILE; u<NOFILE;)
4681 * remap fd into Shell's fd space
4691 for (i=0; i<NOFILE; i++)
4696 } while (fd >= 0 && fd < e.iofd);
4697 for (i=0; i<NOFILE; i++)
4701 err("too many files open in shell");
4712 if ((i = pipe(pv)) < 0)
4713 err("can't create pipe - try again");
4727 /* -------- here.c -------- */
4738 register struct here *h, *lh;
4740 h = (struct here *) space(sizeof(struct here));
4743 h->h_tag = evalstr(s, DOSUB);
4752 for (lh = inhere; lh!=NULL; lh = lh->h_next)
4753 if (lh->h_next == 0) {
4757 iop->io_flag |= IOHERE|IOXHERE;
4758 for (s = h->h_tag; *s; s++)
4760 iop->io_flag &= ~ IOXHERE;
4763 h->h_dosub = iop->io_flag & IOXHERE;
4769 register struct here *h, *hp;
4771 /* Scan here files first leaving inhere list in place */
4772 for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
4773 readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub? 0: '\'');
4775 /* Make inhere list active - keep list intact for scraphere */
4777 hp->h_next = acthere;
4784 readhere(name, s, ec)
4790 char tname[30] = ".msh_XXXXXX";
4793 char myline [LINELIM+1];
4796 tf = mkstemp(tname);
4799 *name = strsave(tname, areanum);
4800 if (newenv(setjmp(errpt = ev)) != 0)
4803 pushio(e.iop->argp, (int(*)(struct ioarg *))e.iop->iofn);
4806 if (interactive && e.iop <= iostack) {
4807 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4808 current_prompt=cprompt->value;
4810 prs(cprompt->value);
4814 while ((c = my_getc(ec)) != '\n' && c) {
4817 if (thenext >= &myline[LINELIM]) {
4824 if (strcmp(s, myline) == 0 || c == 0)
4827 write (tf, myline, (int)(thenext-myline));
4830 prs("here document `"); prs(s); err("' unclosed");
4838 * open here temp file.
4839 * if unquoted here, expand here temp file into second temp file.
4842 herein(hname, xdoll)
4850 /* Avoid longjmp clobbering */
4855 hf = open(hname, 0);
4860 char tname[30] = ".msh_XXXXXX";
4863 tf = mkstemp(tname);
4866 if (newenv(setjmp(errpt = ev)) == 0) {
4867 PUSHIO(afile, hf, herechar);
4869 while ((c = subgetc(0, 0)) != 0) {
4871 write(tf, &c, sizeof c);
4877 tf = open(tname, 0);
4887 register struct here *h;
4889 for (h = inhere; h != NULL; h = h->h_next) {
4890 if (h->h_iop && h->h_iop->io_name)
4891 unlink(h->h_iop->io_name);
4896 /* unlink here temp files before a freearea(area) */
4901 register struct here *h, *hl;
4904 for (h = acthere; h != NULL; h = h->h_next)
4905 if (getarea((char *) h) >= area) {
4906 if (h->h_iop->io_name != NULL)
4907 unlink(h->h_iop->io_name);
4909 acthere = h->h_next;
4911 hl->h_next = h->h_next;
4919 * Copyright (c) 1987,1997, Prentice Hall
4920 * All rights reserved.
4922 * Redistribution and use of the MINIX operating system in source and
4923 * binary forms, with or without modification, are permitted provided
4924 * that the following conditions are met:
4926 * Redistributions of source code must retain the above copyright
4927 * notice, this list of conditions and the following disclaimer.
4929 * Redistributions in binary form must reproduce the above
4930 * copyright notice, this list of conditions and the following
4931 * disclaimer in the documentation and/or other materials provided
4932 * with the distribution.
4934 * Neither the name of Prentice Hall nor the names of the software
4935 * authors or contributors may be used to endorse or promote
4936 * products derived from this software without specific prior
4937 * written permission.
4939 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
4940 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
4941 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4942 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4943 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
4944 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4945 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4946 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
4947 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4948 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
4949 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
4950 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.