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;
674 static int iounit = IODEFAULT;
675 static YYSTYPE yylval;
676 static struct iobuf sharedbuf = {AFID_NOBUF};
677 static struct iobuf mainbuf = {AFID_NOBUF};
678 static unsigned bufid = AFID_ID; /* buffer id counter */
679 static struct ioarg temparg = {0, 0, 0, AFID_NOBUF, 0};
680 static struct here *inhere; /* list of hear docs while parsing */
681 static struct here *acthere; /* list of active here documents */
682 static struct region *areabot; /* bottom of area */
683 static struct region *areatop; /* top of area */
684 static struct region *areanxt; /* starting point of scan */
685 static void * brktop;
686 static void * brkaddr;
689 #ifdef CONFIG_FEATURE_COMMAND_EDITING
690 static char * current_prompt;
693 /* -------- sh.c -------- */
699 extern int msh_main(int argc, char **argv)
705 int (*iof)(struct ioarg *);
708 if ((ap = environ) != NULL) {
710 assign(*ap++, !COPYV);
711 for (ap = environ; *ap;)
712 export(lookup(*ap++));
717 shell = lookup("SHELL");
718 if (shell->value == null)
719 setval(shell, DEFAULT_SHELL);
722 homedir = lookup("HOME");
723 if (homedir->value == null)
724 setval(homedir, "/");
727 setval(lookup("$"), putn(getpid()));
729 path = lookup("PATH");
730 if (path->value == null) {
732 setval(path, "/sbin:/bin:/usr/sbin:/usr/bin");
734 setval(path, "/bin:/usr/bin");
739 if (ifs->value == null)
740 setval(ifs, " \t\n");
742 prompt = lookup("PS1");
743 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
744 if (prompt->value == null)
746 setval(prompt, "$ ");
747 if (geteuid() == 0) {
748 setval(prompt, "# ");
749 prompt->status &= ~EXPORT;
751 cprompt = lookup("PS2");
752 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
753 if (cprompt->value == null)
755 setval(cprompt, "> ");
761 if(argv[0][0] == '-' && argv[0][1] != '\0') {
762 for (s = argv[0]+1; *s; s++)
765 prompt->status &= ~EXPORT;
766 cprompt->status &= ~EXPORT;
771 PUSHIO(aword, *++argv, iof = nlchar);
783 prompt->status &= ~EXPORT;
791 if (*s>='a' && *s<='z')
798 if (iof == filechar && --argc > 0) {
801 prompt->status &= ~EXPORT;
802 cprompt->status &= ~EXPORT;
803 if (newfile(name = *++argv))
808 if (e.iop < iostack) {
809 PUSHIO(afile, 0, iof);
810 if (isatty(0) && isatty(1) && !cflag) {
812 #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
813 printf( "\n\n" BB_BANNER " Built-in shell (msh)\n");
814 printf( "Enter 'help' for a list of built-in commands.\n\n");
818 signal(SIGQUIT, qflag);
819 if (name && name[0] == '-') {
821 if ((f = open(".profile", 0)) >= 0)
823 if ((f = open("/etc/profile", 0)) >= 0)
827 signal(SIGTERM, sig);
828 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
829 signal(SIGINT, onintr);
834 for (ap = ++argv; --argc > 0;) {
835 if (assign(*ap = *argv++, !COPYV)) {
836 dolc--; /* keyword */
842 setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
845 if (interactive && e.iop <= iostack) {
846 #ifdef CONFIG_FEATURE_COMMAND_EDITING
847 current_prompt=prompt->value;
853 /* Ensure that getenv("PATH") stays current */
854 setenv("PATH", path->value, 1);
866 for (c='a'; c<='z'; c++)
870 setval(lookup("-"), m);
879 if (strcmp(s, "-") != 0) {
883 err(": cannot open");
913 setjmp(failpt = m1); /* Bruce Evans' fix */
914 if (setjmp(failpt = m1) || yyparse() || intr) {
918 if (!interactive && intr)
929 execute(outtree, NOPIPE, NOPIPE, 0);
930 if (!interactive && intr) {
934 if ((i = trapset) != 0) {
984 e.iop = e.iobase = iostack;
991 register struct env *ep;
997 ep = (struct env *) space(sizeof(*ep));
1012 register struct env *ep;
1015 if ((ep = e.oenv) != NULL) {
1018 /* should close `'d files */
1020 while (--fd >= e.iofd)
1026 * Is any character from s1 in s2?
1030 register char *s1, *s2;
1039 * Is character c in s?
1064 snprintf(s, sizeof(s), "%u", n);
1071 PUSHIO(afile, f, filechar);
1076 int s; /* ANSI C requires a parameter */
1078 signal(SIGINT, onintr);
1098 if ((cp = getcell(n)) == 0)
1099 err("out of string space");
1108 register char *cp, *xp;
1110 if ((cp = space(strlen(s)+1)) != NULL) {
1111 setarea((char *)cp, a);
1112 for (xp = cp; (*xp++ = *s++) != '\0';)
1130 static void runtrap(i)
1135 if ((trapstr = trap[i]) == NULL)
1139 RUN(aword, trapstr, nlchar);
1142 /* -------- var.c -------- */
1145 * Find the given name in the dictionary
1146 * and return its value. If the name was
1147 * not previously there, enter it now and
1148 * return a null value.
1154 register struct var *vp;
1157 static struct var dummy;
1161 for (c = 0; isdigit(*n) && c < 1000; n++)
1163 dummy.status = RONLY;
1164 dummy.value = c <= dolc? dolv[c]: null;
1167 for (vp = vlist; vp; vp = vp->next)
1168 if (eqname(vp->name, n))
1171 vp = (struct var *)space(sizeof(*vp));
1172 if (vp == 0 || (vp->name = space((int)(cp-n)+2)) == 0) {
1173 dummy.name = dummy.value = "";
1176 for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++)
1181 setarea((char *)vp, 0);
1182 setarea((char *)vp->name, 0);
1185 vp->status = GETCELL;
1191 * give variable at `vp' the value `val'.
1198 nameval(vp, val, (char *)NULL);
1202 * if name is not NULL, it must be
1203 * a prefix of the space `val',
1205 * this is all so that exporting
1206 * values is reasonably painless.
1209 nameval(vp, val, name)
1210 register struct var *vp;
1213 register char *cp, *xp;
1217 if (vp->status & RONLY) {
1218 for (xp = vp->name; *xp && *xp != '=';)
1219 putc(*xp++, stderr);
1220 err(" is read-only");
1225 xp = space(strlen(vp->name)+strlen(val)+2);
1228 /* make string: name=value */
1229 setarea((char *)xp, 0);
1231 for (cp = vp->name; (*xp = *cp++) && *xp!='='; xp++)
1236 for (cp = val; (*xp++ = *cp++) != '\0';)
1241 if (vp->status & GETCELL)
1242 freecell(vp->name); /* form new string `name=value' */
1252 vp->status |= EXPORT;
1259 if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */
1260 vp->status |= RONLY;
1267 if (!isalpha((int)*s) && *s != '_')
1269 for (; *s != '='; s++)
1270 if (*s == 0 || (!isalnum(*s) && *s != '_'))
1283 if (!isalpha(*s) && *s != '_')
1285 for (cp = s; *cp != '='; cp++)
1286 if (*cp == 0 || (!isalnum(*cp) && *cp != '_'))
1289 nameval(vp, ++cp, cf == COPYV? (char *)NULL: s);
1291 vp->status &= ~GETCELL;
1299 if (!isalpha(*cp++) && *(cp-1) != '_')
1302 if (!isalnum(*cp++) && *(cp-1) != '_')
1309 register int f, out;
1311 register struct var *vp;
1313 for (vp = vlist; vp; vp = vp->next)
1314 if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) {
1315 if (vp->status & EXPORT)
1316 write(out, "export ", 7);
1317 if (vp->status & RONLY)
1318 write(out, "readonly ", 9);
1319 write(out, vp->name, (int)(findeq(vp->name) - vp->name));
1320 write(out, "\n", 1);
1326 register char *n1, *n2;
1328 for (; *n1 != '=' && *n1 != 0; n1++)
1331 return(*n2 == 0 || *n2 == '=');
1338 while (*cp != '\0' && *cp != '=')
1343 /* -------- gmatch.c -------- */
1345 * int gmatch(string, pattern)
1346 * char *string, *pattern;
1348 * Match a pattern as in sh(1).
1353 #define QMASK (CMASK&~QUOTE)
1354 #define NOT '!' /* might use ^ */
1358 register char *s, *p;
1360 register int sc, pc;
1362 if (s == NULL || p == NULL)
1364 while ((pc = *p++ & CMASK) != '\0') {
1368 if ((p = cclass(p, sc)) == NULL)
1380 if (*p == '\0' || gmatch(s, p))
1382 } while (*s++ != '\0');
1386 if (sc != (pc&~QUOTE))
1398 register int c, d, not, found;
1400 if ((not = *p == NOT) != 0)
1405 return((char *)NULL);
1407 if (p[1] == '-' && p[2] != ']') {
1412 if (c == sub || (c <= sub && sub <= d))
1414 } while (*++p != ']');
1415 return(found? p+1: (char *)NULL);
1419 /* -------- area.c -------- */
1422 * All memory between (char *)areabot and (char *)(areatop+1) is
1423 * exclusively administered by the area management routines.
1424 * It is assumed that sbrk() and brk() manipulate the high end.
1427 #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;})
1432 brkaddr = malloc(65000);
1433 brktop = brkaddr + 65000;
1435 while ((int)sbrk(0) & ALIGN)
1437 areabot = (struct region *)sbrk(REGSIZE);
1439 areabot->next = areabot;
1440 areabot->area = BUSY;
1449 register int nregio;
1450 register struct region *p, *q;
1456 } /* silly and defeats the algorithm */
1458 * round upwards and add administration area
1460 nregio = (nbytes+(REGSIZE-1))/REGSIZE + 1;
1461 for (p = areanxt;;) {
1462 if (p->area > areanum) {
1466 while ((q = p->next)->area > areanum && q != areanxt)
1469 * exit loop if cell big enough
1471 if (q >= p + nregio)
1478 i = nregio >= GROWBY ? nregio : GROWBY;
1479 p = (struct region *)sbrk(i * REGSIZE);
1480 if (p == (struct region *)-1)
1481 return((char *)NULL);
1485 abort(); /* allocated areas are contiguous */
1495 * we found a FREE area big enough, pointed to by 'p', and up to 'q'
1497 areanxt = p + nregio;
1500 * split into requested area and rest
1502 if (areanxt+1 > q) {
1504 abort(); /* insufficient space left for admin */
1507 areanxt->area = FREE;
1511 return((char *)(p+1));
1518 register struct region *p;
1520 if ((p = (struct region *)cp) != NULL) {
1532 register struct region *p, *top;
1535 for (p = areabot; p != top; p = p->next)
1545 register struct region *p;
1547 if ((p = (struct region *)cp) != NULL)
1555 return ((struct region*)cp-1)->area;
1561 register struct region *p, *q, *top;
1564 for (p = areabot; p != top; p = p->next) {
1565 if (p->area > areanum) {
1566 while ((q = p->next)->area > areanum)
1572 if (areatop >= q + SHRINKBY && q->area > areanum) {
1581 /* -------- csyn.c -------- */
1583 * shell: syntax (C version)
1602 register struct op *t, *p;
1607 while ((c = yylex(0)) == '|') {
1608 if ((p = command(CONTIN)) == NULL)
1610 if (t->type != TPAREN && t->type != TCOM) {
1611 /* shell statement */
1612 t = block(TPAREN, t, NOBLOCK, NOWORDS);
1614 t = block(TPIPE, t, p, NOWORDS);
1624 register struct op *t, *p;
1629 while ((c = yylex(0)) == LOGAND || c == LOGOR) {
1630 if ((p = pipeline(CONTIN)) == NULL)
1632 t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS);
1642 register struct op *t, *p;
1647 if((peeksym = yylex(0)) == '&')
1648 t = block(TASYNC, t, NOBLOCK, NOWORDS);
1649 while ((c = yylex(0)) == ';' || c == '&' || (multiline && c == '\n')) {
1650 if ((p = andor()) == NULL)
1652 if((peeksym = yylex(0)) == '&')
1653 p = block(TASYNC, p, NOBLOCK, NOWORDS);
1666 register struct ioword *iop;
1670 if ((c = yylex(cf)) != '<' && c != '>') {
1676 iop = io(iounit, i, yylval.cp);
1679 markhere(yylval.cp, iop);
1687 if ((peeksym = yylex(cf)) != c)
1695 register struct op *t;
1699 switch (peeksym = yylex(0)) {
1724 register struct op *t;
1730 return(block(type, t, NOBLOCK, NOWORDS));
1737 register struct op *t;
1738 struct wdblock *iosave;
1747 switch (c = yylex(cf)) {
1750 if ((t = simple()) == NULL) {
1752 return((struct op *)NULL);
1759 t = nested(TPAREN, ')');
1763 t = nested(TBRACE, '}');
1773 t->words = wordlist();
1774 if ((c = yylex(0)) != '\n' && c != ';')
1776 t->left = dogroup(0);
1784 t->type = c == WHILE? TWHILE: TUNTIL;
1786 t->right = dogroup(1);
1798 musthave(IN, CONTIN);
1800 t->left = caselist();
1810 t->right = thenpart();
1827 register struct op *mylist;
1830 if (c == DONE && onlydone)
1831 return((struct op *)NULL);
1843 register struct op *t;
1845 if ((c = yylex(0)) != THEN) {
1847 return((struct op *)NULL);
1852 if (t->left == NULL)
1854 t->right = elsepart();
1862 register struct op *t;
1864 switch (c = yylex(0)) {
1866 if ((t = c_list()) == NULL)
1874 t->right = thenpart();
1879 return((struct op *)NULL);
1886 register struct op *t;
1889 while ((peeksym = yylex(CONTIN)) != ESAC)
1890 t = list(t, casepart());
1897 register struct op *t;
1901 t->words = pattern();
1904 if ((peeksym = yylex(CONTIN)) != ESAC)
1905 musthave(BREAK, CONTIN);
1919 } while ((c = yylex(0)) == '|');
1930 if ((c = yylex(0)) != IN) {
1932 return((char **)NULL);
1935 while ((c = yylex(0)) == WORD)
1943 * supporting functions
1947 register struct op *t1, *t2;
1953 return(block(TLIST, t1, t2, NOWORDS));
1957 block(type, t1, t2, wp)
1962 register struct op *t;
1976 register struct res *rp;
1978 for (rp = restab; rp->r_name; rp++)
1979 if (strcmp(rp->r_name, n) == 0)
1987 register struct op *t;
1989 t = (struct op *)tree(sizeof(*t));
2001 register struct op *t;
2004 iolist = addword((char *)NULL, iolist);
2005 t->ioact = copyio();
2008 if (t->type != TCOM) {
2009 if (t->type != TPAREN && t->ioact != NULL) {
2010 t = block(TPAREN, t, NOBLOCK, NOWORDS);
2011 t->ioact = t->left->ioact;
2012 t->left->ioact = NULL;
2026 wd = getwords(wdlist);
2035 wdlist = addword(cp, wdlist);
2038 static struct ioword **
2041 register struct ioword **iop;
2043 iop = (struct ioword **) getwords(iolist);
2048 static struct ioword *
2054 register struct ioword *iop;
2056 iop = (struct ioword *) tree(sizeof(*iop));
2060 iolist = addword((char *)iop, iolist);
2067 yyerror("syntax error");
2075 if (interactive && e.iop <= iostack) {
2077 while (eofc() == 0 && yylex(0) != '\n')
2091 if ((c = peeksym) > 0) {
2104 while ((c = my_getc(0)) == ' ' || c == '\t')
2108 if (any(c, "0123456789")) {
2109 unget(c1 = my_getc(0));
2110 if (c1 == '<' || c1 == '>') {
2120 while ((c = my_getc(0)) != 0 && c != '\n')
2130 if ((c = my_getc(0)) == '{') {
2131 if ((c = collect(c, '}')) != '\0')
2140 if ((c = collect(c, c)) != '\0')
2147 if ((c1 = dual(c)) != '\0') {
2165 if (multiline || cf & CONTIN) {
2166 if (interactive && e.iop <= iostack) {
2167 #ifdef CONFIG_FEATURE_COMMAND_EDITING
2168 current_prompt=cprompt->value;
2170 prs(cprompt->value);
2187 while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n"))
2188 if (e.linep >= elinep)
2189 err("word too long");
2196 if (atstart && (c = rlookup(line))!=0) {
2200 yylval.cp = strsave(line, areanum);
2211 while ((c = my_getc(c1)) != c1) {
2216 prs("no closing "); yyerror(s);
2219 if (interactive && c == '\n' && e.iop <= iostack) {
2220 #ifdef CONFIG_FEATURE_COMMAND_EDITING
2221 current_prompt=cprompt->value;
2223 prs(cprompt->value);
2237 register char *cp = s;
2242 if ((c = rlookup(s)) == 0)
2254 if (c == '>' || c == '<') {
2257 yylval.i = ec == '>'? IOWRITE|IOCAT: IOHERE;
2260 yylval.i = ec == '>'? IOWRITE: IOREAD;
2261 if (c != '&' || yylval.i == IOHERE)
2273 if ((t = getcell(size)) == NULL) {
2274 prs("command line too complicated\n");
2284 /* -------- exec.c -------- */
2292 execute(t, pin, pout, act)
2293 register struct op *t;
2297 register struct op *t1;
2298 volatile int i, rv, a;
2299 char *cp, **wp, **wp2;
2304 /* Avoid longjmp clobbering */
2313 wp = (wp2 = t->words) != NULL
2314 ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
2319 rv = execute(t->left, pin, pout, 0);
2325 rv = forkexec(t, pin, pout, act, wp, &child);
2336 if ((rv = openpipe(pv)) < 0)
2338 pv[0] = remap(pv[0]);
2339 pv[1] = remap(pv[1]);
2340 (void) execute(t->left, pin, pv, 0);
2341 rv = execute(t->right, pv, pout, 0);
2346 (void) execute(t->left, pin, pout, 0);
2347 rv = execute(t->right, pin, pout, 0);
2352 int hinteractive = interactive;
2356 interactive = hinteractive;
2358 setval(lookup("!"), putn(i));
2369 signal(SIGINT, SIG_IGN);
2370 signal(SIGQUIT, SIG_IGN);
2372 signal(SIGTERM, SIG_DFL);
2376 open("/dev/null", 0);
2378 exit(execute(t->left, pin, pout, FEXEC));
2385 rv = execute(t->left, pin, pout, 0);
2386 if ((t1 = t->right)!=NULL && (rv == 0) == (t->type == TAND))
2387 rv = execute(t1, pin, pout, 0);
2397 while (*wp++ != NULL)
2400 vp = lookup(t->str);
2401 while (setjmp(bc.brkpt))
2405 for (t1 = t->left; i-- && *wp != NULL;) {
2407 rv = execute(t1, pin, pout, 0);
2409 brklist = brklist->nextlev;
2414 while (setjmp(bc.brkpt))
2419 while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE))
2420 rv = execute(t->right, pin, pout, 0);
2421 brklist = brklist->nextlev;
2426 if (t->right != NULL) {
2427 rv = !execute(t->left, pin, pout, 0) ?
2428 execute(t->right->left, pin, pout, 0):
2429 execute(t->right->right, pin, pout, 0);
2434 if ((cp = evalstr(t->str, DOSUB|DOTRIM)) == 0)
2436 if ((t1 = findcase(t->left, cp)) != NULL)
2437 rv = execute(t1, pin, pout, 0);
2442 if (iopp = t->ioact)
2444 if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
2449 if (rv >= 0 && (t1 = t->left))
2450 rv = execute(t1, pin, pout, 0);
2460 if (interactive && intr) {
2464 if ((i = trapset) != 0) {
2472 forkexec( register struct op *t, int *pin, int *pout, int act, char **wp, int *pforked)
2475 int (*shcom)(struct op *) = NULL;
2478 struct ioword **iopp;
2488 struct brkcon * hbrklist;
2492 /* Avoid longjmp clobbering */
2505 rv = -1; /* system-detected error */
2506 if (t->type == TCOM) {
2507 while ((cp = *wp++) != NULL)
2511 /* strip all initial assignments */
2512 /* not correct wrt PATH=yyy command etc */
2514 echo (cp ? wp: owp);
2515 if (cp == NULL && t->ioact == NULL) {
2516 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2518 return(setstatus(0));
2520 else if (cp != NULL)
2521 shcom = inbuilt(cp);
2525 if (shcom == NULL && (f & FEXEC) == 0) {
2531 hinteractive = interactive;
2538 /* who wrote this crappy non vfork safe shit? */
2543 interactive = hinteractive;
2553 return(pout==NULL? setstatus(waitfor(i,0)): 0);
2557 signal(SIGINT, SIG_IGN);
2558 signal(SIGQUIT, SIG_IGN);
2568 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2572 if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) {
2573 err("piping to/from shell builtins not yet done");
2585 if ((iopp = t->ioact) != NULL) {
2586 if (shcom != NULL && shcom != doexec) {
2588 err(": cannot redirect shell command");
2592 if (iosetup(*iopp++, pin!=NULL, pout!=NULL))
2596 return(setstatus((*shcom)(t)));
2597 /* should use FIOCEXCL */
2598 for (i=FDBASE; i<NOFILE; i++)
2601 signal(SIGINT, SIG_DFL);
2602 signal(SIGQUIT, SIG_DFL);
2604 if (t->type == TPAREN)
2605 exit(execute(t->left, NOPIPE, NOPIPE, FEXEC));
2609 cp = rexecve(wp[0], wp, makenv());
2610 prs(wp[0]); prs(": "); warn(cp);
2619 * 0< 1> are ignored as required
2623 iosetup(iop, pipein, pipeout)
2624 register struct ioword *iop;
2625 int pipein, pipeout;
2627 register int u = -1;
2628 char *cp=NULL, *msg;
2630 if (iop->io_unit == IODEFAULT) /* take default */
2631 iop->io_unit = iop->io_flag&(IOREAD|IOHERE)? 0: 1;
2632 if (pipein && iop->io_unit == 0)
2634 if (pipeout && iop->io_unit == 1)
2636 msg = iop->io_flag&(IOREAD|IOHERE)? "open": "create";
2637 if ((iop->io_flag & IOHERE) == 0) {
2639 if ((cp = evalstr(cp, DOSUB|DOTRIM)) == NULL)
2642 if (iop->io_flag & IODUP) {
2643 if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
2645 err(": illegal >& argument");
2649 iop->io_flag = IOCLOSE;
2650 iop->io_flag &= ~(IOREAD|IOWRITE);
2652 switch (iop->io_flag) {
2658 case IOHERE|IOXHERE:
2659 u = herein(iop->io_name, iop->io_flag&IOXHERE);
2664 if ((u = open(cp, 1)) >= 0) {
2665 lseek(u, (long)0, 2);
2669 u = creat(cp, 0666);
2673 u = dup2(*cp-'0', iop->io_unit);
2677 close(iop->io_unit);
2686 if (u != iop->io_unit) {
2687 dup2(u, iop->io_unit);
2701 for (i=0; wp[i]; i++) {
2714 register struct op *t1;
2716 register char **wp, *cp;
2719 return((struct op **)NULL);
2720 if (t->type == TLIST) {
2721 if ((tp = find1case(t->left, w)) != NULL)
2723 t1 = t->right; /* TPAT */
2726 for (wp = t1->words; *wp;)
2727 if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp))
2729 return((struct op **)NULL);
2737 register struct op **tp;
2739 return((tp = find1case(t, w)) != NULL? *tp: (struct op *)NULL);
2743 * Enter a new loop level (marked for break/continue).
2749 bc->nextlev = brklist;
2754 * Wait for the last process created.
2755 * Print a message for each process found
2756 * that was killed by a signal.
2757 * Ignore interrupt signals while waiting
2758 * unless `canintr' is true.
2761 waitfor(lastpid, canintr)
2762 register int lastpid;
2765 register int pid, rv;
2767 int oheedint = heedint;
2774 if (errno != EINTR || canintr)
2777 if ((rv = WAITSIG(s)) != 0) {
2779 if (signame[rv] != NULL) {
2780 if (pid != lastpid) {
2787 if (pid != lastpid) {
2791 prs("Signal "); prn(rv); prs(" ");
2794 prs(" - core dumped");
2795 if (rv >= NSIGNAL || signame[rv])
2801 } while (pid != lastpid);
2808 if (exstat == 0) exstat = rv;
2820 setval(lookup("?"), putn(s));
2825 * PATH-searching interface to execve.
2826 * If getenv("PATH") were kept up-to-date,
2827 * execvp might be used.
2831 char *c, **v, **envp;
2834 register char *sp, *tp;
2835 int eacces = 0, asis = 0;
2837 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
2839 #ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN
2840 name = bb_get_last_path_component(name);
2843 if (find_applet_by_name(name)) {
2844 /* We have to exec here since we vforked. Running
2845 * run_applet_by_name() won't work and bad things
2847 execve("/proc/self/exe", v, envp);
2848 execve("busybox", v, envp);
2852 sp = any('/', c)? "": path->value;
2854 while (asis || *sp != '\0') {
2857 for (; *sp != '\0'; tp++)
2858 if ((*tp = *sp++) == ':') {
2864 for (i = 0; (*tp++ = c[i++]) != '\0';)
2867 execve(e.linep, v, envp);
2873 execve(DEFAULT_SHELL, v, envp);
2878 return((char*)bb_msg_memory_exhausted);
2881 return("argument list too long");
2888 return(errno==ENOENT ? "not found" : "cannot execute");
2892 * Run the command produced by generator `f'
2893 * applied to stream `arg'.
2896 run(struct ioarg *argp, int (*f)(struct ioarg *))
2899 struct wdblock *swdlist;
2900 struct wdblock *siolist;
2906 /* Avoid longjmp clobbering */
2916 if (newenv(setjmp(errpt = ev)) == 0) {
2922 if (setjmp(failpt = rt) == 0 && yyparse() == 0)
2923 rv = execute(outtree, NOPIPE, NOPIPE, 0);
2930 freearea(areanum--);
2934 /* -------- do.c -------- */
2937 * built-in commands: doX
2940 static int dohelp(struct op *t )
2943 const struct builtincmd *x;
2945 printf("\nBuilt-in commands:\n");
2946 printf("-------------------\n");
2948 for (col=0, x = builtincmds; x->builtinfunc != NULL; x++) {
2951 col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name);
2957 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
2960 const struct BB_applet *applet;
2961 extern const struct BB_applet applets[];
2962 extern const size_t NUM_APPLETS;
2964 for (i=0, applet = applets; i < NUM_APPLETS; applet++, i++) {
2968 col += printf("%s%s", ((col == 0) ? "\t" : " "),
2978 return EXIT_SUCCESS;
2983 static int dolabel(struct op *t )
2990 register struct op *t;
2992 register char *cp, *er;
2994 if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL)
2995 er = ": no home directory";
2996 else if(chdir(cp) < 0)
2997 er = ": bad directory";
3000 prs(cp != NULL? cp: "cd");
3007 register struct op *t;
3011 n = t->words[1]? getn(t->words[1]): 1;
3013 err("nothing to shift");
3019 setval(lookup("#"), putn(dolc));
3024 * execute login and newgrp directly
3033 signal(SIGINT, SIG_DFL);
3034 signal(SIGQUIT, SIG_DFL);
3036 cp = rexecve(t->words[0], t->words, makenv());
3037 prs(t->words[0]); prs(": "); err(cp);
3043 register struct op *t;
3048 if ((cp = t->words[1]) == NULL) {
3051 for (n=3*4; (n-=3) >= 0;)
3052 putc('0'+((i>>n)&07), stderr);
3055 for (n=0; *cp>='0' && *cp<='9'; cp++)
3056 n = n*8 + (*cp-'0');
3064 register struct op *t;
3071 for(i = 0; (t->words[i]=t->words[i+1]) != NULL; i++)
3077 if (setjmp(failpt = ex) == 0)
3078 execute(t, NOPIPE, NOPIPE, FEXEC);
3089 register char *sp, *tp;
3092 if ((cp = t->words[1]) == NULL)
3094 sp = any('/', cp)? ":": path->value;
3097 while (*sp && (*tp = *sp++) != ':')
3101 for (i = 0; (*tp++ = cp[i++]) != '\0';)
3103 if ((i = open(e.linep, 0)) >= 0) {
3121 if ((cp = t->words[1]) != NULL) {
3127 setstatus(waitfor(i, 1));
3135 register char *cp, **wp;
3136 register int nb = 0;
3137 register int nl = 0;
3139 if (t->words[1] == NULL) {
3140 err("Usage: read name ...");
3143 for (wp = t->words+1; *wp; wp++) {
3144 for (cp = e.linep; !nl && cp < elinep-1; cp++)
3145 if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) ||
3146 (nl = (*cp == '\n')) ||
3147 (wp[1] && any(*cp, ifs->value)))
3152 setval(lookup(*wp), e.linep);
3159 register struct op *t;
3161 return(RUN(awordlist, t->words+1, wdchar));
3166 register struct op *t;
3169 register int resetsig;
3171 if (t->words[1] == NULL) {
3172 for (i=0; i<=_NSIG; i++)
3181 resetsig = isdigit(*t->words[1]);
3182 for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) {
3183 n = getsig(t->words[i]);
3187 if (*t->words[1] != '\0') {
3188 trap[n] = strsave(t->words[1], 0);
3197 setsig(n, n == SIGQUIT ? SIG_IGN
3212 if ((n = getn(s)) < 0 || n > _NSIG) {
3213 err("trap: bad signal number");
3220 setsig( register int n, sighandler_t f)
3224 if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) {
3243 for (n = 0; isdigit(*s); s++)
3244 n = (n*10) + (*s-'0');
3247 err(": bad number");
3256 return(brkcontin(t->words[1], 1));
3263 return(brkcontin(t->words[1], 0));
3271 register struct brkcon *bc;
3274 nl = cp == NULL? 1: getn(cp);
3278 if ((bc = brklist) == NULL)
3280 brklist = bc->nextlev;
3283 err("bad break/continue level");
3287 longjmp(bc->brkpt, 1);
3298 if ((cp = t->words[1]) != NULL)
3299 setstatus(getn(cp));
3309 rdexp(t->words+1, export, EXPORT);
3317 rdexp(t->words+1, ronly, RONLY);
3321 static void rdexp (char **wp, void (*f)(struct var *), int key)
3324 for (; *wp != NULL; wp++) {
3325 if (isassign(*wp)) {
3328 for (cp = *wp; *cp != '='; cp++)
3346 err(": bad identifier");
3351 register struct op *t;
3353 register struct var *vp;
3357 if ((cp = t->words[1]) == NULL) {
3358 for (vp = vlist; vp; vp = vp->next)
3359 varput(vp->name, 1);
3363 /* bad: t->words++; */
3364 for(n = 0; (t->words[n]=t->words[n+1]) != NULL; n++)
3367 flag['x'] = flag['v'] = 0;
3377 if (*cp>='a' && *cp<='z')
3384 t->words[0] = dolv[0];
3385 for (n=1; t->words[n]; n++)
3386 setarea((char *)t->words[n], 0);
3389 setval(lookup("#"), putn(dolc));
3390 setarea((char *)(dolv-1), 0);
3400 if (isalnum(*s) || *s == '_') {
3401 write(out, s, strlen(s));
3402 write(out, "\n", 1);
3408 * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
3409 * This file contains code for the times builtin.
3411 static int dotimes(struct op *t )
3414 long int clk_tck = sysconf(_SC_CLK_TCK);
3417 printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
3418 (int) (buf.tms_utime / clk_tck / 60),
3419 ((double) buf.tms_utime) / clk_tck,
3420 (int) (buf.tms_stime / clk_tck / 60),
3421 ((double) buf.tms_stime) / clk_tck,
3422 (int) (buf.tms_cutime / clk_tck / 60),
3423 ((double) buf.tms_cutime) / clk_tck,
3424 (int) (buf.tms_cstime / clk_tck / 60),
3425 ((double) buf.tms_cstime) / clk_tck);
3430 static int(*inbuilt(char *s))(struct op *)
3432 const struct builtincmd *bp;
3434 for (bp = builtincmds; bp->name != NULL; bp++)
3435 if (strcmp(bp->name, s) == 0)
3436 return(bp->builtinfunc);
3441 /* -------- eval.c -------- */
3446 * blank interpretation
3451 static char ** eval( char **ap, int f)
3459 /* Avoid longjmp clobbering */
3466 if (newenv(setjmp(errpt = ev)) == 0) {
3467 while (*ap && isassign(*ap))
3468 expand(*ap++, &wb, f & ~DOGLOB);
3470 for (wf = ap; *wf; wf++) {
3472 expand(*wf, &wb, f & ~DOGLOB);
3475 for (wb = addword((char *)0, wb); *ap; ap++) {
3476 if (!flag['k'] || !isassign(*ap))
3477 expand(*ap, &wb, f & ~DOKEY);
3479 wb = addword((char *)0, wb);
3484 return(gflg? (char **)NULL: wp);
3488 * Make the exported environment from the exported
3489 * names in the dictionary. Keyword assignments
3490 * will already have been done.
3496 register struct wdblock *wb;
3497 register struct var *vp;
3500 for (vp = vlist; vp; vp = vp->next)
3501 if (vp->status & EXPORT)
3502 wb = addword(vp->name, wb);
3503 wb = addword((char *)0, wb);
3504 return(getwords(wb));
3515 if (expand(cp, &wb, f)) {
3516 if (wb == NULL || wb->w_nword == 0 || (cp = wb->w_words[0]) == NULL)
3525 expand( char *cp, register struct wdblock **wbp, int f)
3530 /* Avoid longjmp clobbering */
3536 if (!anys("$`'\"", cp) &&
3537 !anys(ifs->value, cp) &&
3538 ((f&DOGLOB)==0 || !anys("[*?", cp))) {
3539 cp = strsave(cp, areanum);
3542 *wbp = addword(cp, *wbp);
3545 if (newenv(setjmp(errpt = ev)) == 0) {
3546 PUSHIO(aword, cp, strchar);
3548 while ((cp = blank(f)) && gflg == 0) {
3550 cp = strsave(cp, areanum);
3551 if ((f&DOGLOB) == 0) {
3554 *wbp = addword(cp, *wbp);
3556 *wbp = glob(cp, *wbp);
3565 * Blank interpretation and quoting
3573 int scanequals, foundequals;
3576 scanequals = f & DOKEY;
3580 switch (c = subgetc('"', foundequals)) {
3588 if (f & DOBLANK && any(c, ifs->value))
3597 for (c1 = c; (c = subgetc(c1, 1)) != c1;) {
3600 if (c == '\'' || !any(c, "$`\""))
3607 if (!isalpha(c) && c != '_')
3610 c = subgetc('"', foundequals);
3612 f & (DOBLANK && any(c, ifs->value)) ||
3613 (!INSUB() && any(c, "\"'"))) {
3625 else if (!isalnum(c) && c != '_')
3635 * Get characters, substituting for ` and $
3646 if (!INSUB() && ec != '\'') {
3648 if (grave(quoted) == 0)
3650 e.iop->task = XGRAVE;
3653 if (c == '$' && (c = dollar(quoted)) == 0) {
3654 e.iop->task = XDOLL;
3662 * Prepare to generate the string returned by ${} substitution.
3671 register char *s, c, *cp=NULL;
3678 if (isalpha(c) || c == '_') {
3679 while ((c = readc())!=0 && (isalnum(c) || c == '_'))
3680 if (e.linep < elinep)
3687 otask = e.iop->task;
3688 e.iop->task = XOTHER;
3689 while ((c = subgetc('"', 0))!=0 && c!='}' && c!='\n')
3690 if (e.linep < elinep)
3693 e.iop->task = otask;
3700 if (e.linep >= elinep) {
3701 err("string in ${} too long");
3707 for (cp = s+1; *cp; cp++)
3708 if (any(*cp, "=-+?")) {
3713 if (s[1] == 0 && (*s == '*' || *s == '@')) {
3715 /* currently this does not distinguish $* and $@ */
3716 /* should check dollar */
3718 PUSHIO(awordlist, dolv+1, dolchar);
3720 } else { /* trap the nasty ${=} */
3726 if ((dolp = vp->value) == null) {
3730 err("cannot use ${...=...} with $n");
3739 dolp = strsave(cp, areanum);
3744 prs("missing value for ");
3751 } else if (c == '+')
3752 dolp = strsave(cp, areanum);
3753 if (flag['u'] && dolp == null) {
3754 prs("unset variable: ");
3759 PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
3764 * Run the command in `...` and read its output.
3775 static char child_cmd[LINELIM];
3781 char *argument_list[4];
3784 /* Avoid longjmp clobbering */
3788 for (cp = e.iop->argp->aword; *cp != '`'; cp++)
3790 err("no closing `");
3794 /* string copy with dollar expansion */
3795 src = e.iop->argp->aword;
3800 while ((*src != '`') && (count < LINELIM)) {
3805 if (*src == '$' && !ignore && !ignore_once) {
3807 char var_name[LINELIM];
3808 char alt_value[LINELIM];
3821 var_name[var_index++] = *src++;
3822 while (isalnum(*src))
3823 var_name[var_index++] = *src++;
3824 var_name[var_index] = 0;
3837 err("unclosed ${\n");
3842 while (*src && (*src != '}')) {
3843 alt_value[alt_index++] = *src++;
3845 alt_value[alt_index] = 0;
3847 err("unclosed ${\n");
3854 vp = lookup(var_name);
3855 if (vp->value != null)
3856 value = (operator == '+')? alt_value : vp->value;
3857 else if (operator == '?') {
3860 } else if (alt_index && (operator != '+')) {
3862 if (operator == '=')
3867 while (*value && (count < LINELIM)) {
3879 if (openpipe(pf) < 0)
3881 while ((i = vfork()) == -1 && errno == EAGAIN)
3885 err((char*)bb_msg_memory_exhausted);
3889 waitpid(i, NULL, 0);
3890 e.iop->argp->aword = ++cp;
3892 PUSHIO(afile, remap(pf[0]), (int(*)(struct ioarg *))((quoted)? qgravechar: gravechar));
3895 /* allow trapped signals */
3896 /* XXX - Maybe this signal stuff should go as well? */
3897 for (j=0; j<=_NSIG; j++)
3898 if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN)
3904 argument_list[0] = (char *)DEFAULT_SHELL;
3905 argument_list[1] = "-c";
3906 argument_list[2] = child_cmd;
3907 argument_list[3] = 0;
3909 prs(rexecve(argument_list[0], argument_list, makenv()));
3920 if ((s = as) != NULL)
3926 /* -------- glob.c -------- */
3932 #define scopy(x) strsave((x), areanum)
3934 #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
3936 static struct wdblock *cl, *nl;
3937 static char spcl[] = "[?*";
3939 static struct wdblock *
3950 for (pp = cp; *pp; pp++)
3953 else if (!any(*pp & ~QUOTE, spcl))
3956 for (cl = addword(scopy(cp), (struct wdblock *)0); anyspcl(cl); cl = nl) {
3957 nl = newword(cl->w_nword*2);
3958 for(i=0; i<cl->w_nword; i++) { /* for each argument */
3959 for (pp = cl->w_words[i]; *pp; pp++)
3960 if (any(*pp, spcl)) {
3961 globname(cl->w_words[i], pp);
3965 nl = addword(scopy(cl->w_words[i]), nl);
3967 for(i=0; i<cl->w_nword; i++)
3968 DELETE(cl->w_words[i]);
3971 for(i=0; i<cl->w_nword; i++)
3972 unquote(cl->w_words[i]);
3973 glob0((char *)cl->w_words, cl->w_nword, sizeof(char *), xstrcmp);
3975 for (i=0; i<cl->w_nword; i++)
3976 wb = addword(cl->w_words[i], wb);
3981 wb = addword(unquote(cp), wb);
3990 register char *np, *cp;
3991 char *name, *gp, *dp;
3995 char dname[NAME_MAX+1];
3998 for (np = we; np != pp; pp--)
4001 for (dp = cp = space((int)(pp-np)+3); np < pp;)
4005 for (gp = cp = space(strlen(pp)+1); *np && *np != '/';)
4014 dname[NAME_MAX] = '\0';
4015 while ((de=readdir(dirp))!=NULL) {
4016 /* XXX Hmmm... What this could be? (abial) */
4018 if (ent[j].d_ino == 0)
4021 strncpy(dname, de->d_name, NAME_MAX);
4022 if (dname[0] == '.')
4025 for(k=0; k<NAME_MAX; k++)
4026 if (any(dname[k], spcl))
4028 if (gmatch(dname, gp)) {
4029 name = generate(we, pp, dname, np);
4030 if (*np && !anys(np, spcl)) {
4031 if (stat(name,&dbuf)) {
4036 nl = addword(name, nl);
4045 * generate a pathname as below.
4046 * start..end1 / middle end
4047 * the slashes come for free
4050 generate(start1, end1, middle, end)
4052 register char *end1;
4056 register char *op, *xp;
4058 p = op = space((int)(end1-start1)+strlen(middle)+strlen(end)+2);
4059 for (xp = start1; xp != end1;)
4061 for (xp = middle; (*op++ = *xp++) != '\0';)
4064 for (xp = end; (*op++ = *xp++) != '\0';)
4071 register struct wdblock *wb;
4077 for (i=0; i<wb->w_nword; i++)
4078 if (anys(spcl, *wd++))
4087 return(strcmp(*(char **)p1, *(char **)p2));
4090 /* -------- word.c -------- */
4092 static struct wdblock *
4096 register struct wdblock *wb;
4098 wb = (struct wdblock *) space(sizeof(*wb) + nw*sizeof(char *));
4104 static struct wdblock *
4107 register struct wdblock *wb;
4109 register struct wdblock *wb2;
4113 wb = newword(NSTART);
4114 if ((nw = wb->w_nword) >= wb->w_bsize) {
4115 wb2 = newword(nw * 2);
4116 memcpy((char *)wb2->w_words, (char *)wb->w_words, nw*sizeof(char *));
4121 wb->w_words[wb->w_nword++] = wd;
4127 register struct wdblock *wb;
4133 return((char **)NULL);
4134 if (wb->w_nword == 0) {
4136 return((char **)NULL);
4138 wd = (char **) space(nb = sizeof(*wd) * wb->w_nword);
4139 memcpy((char *)wd, (char *)wb->w_words, nb);
4140 DELETE(wb); /* perhaps should done by caller */
4144 int (*func)(char *, char *);
4148 glob0(a0, a1, a2, a3)
4152 int (*a3) (char *, char *);
4156 glob1(a0, a0 + a1 * a2);
4163 register char *i, *j;
4173 if ((n=(int)(lim-base)) <= v2)
4175 n = v2 * (n / (2*v2));
4176 hptr = lptr = base+n;
4181 if ((c = (*func)(i, lptr)) == 0) {
4182 glob2(i, lptr -= v2);
4193 if ((c = (*func)(hptr, j)) == 0) {
4194 glob2(hptr += v2, j);
4199 glob3(i, hptr += v2, j);
4214 if (lptr-base >= lim-hptr) {
4215 glob1(hptr+v2, lim);
4225 glob3(j, lptr -= v2, i);
4234 register char *index1, *index2, c;
4242 *index1++ = *index2;
4251 register char *index1, *index2, *index3;
4261 *index1++ = *index3;
4262 *index3++ = *index2;
4267 /* -------- io.c -------- */
4273 static int my_getc( int ec)
4277 if(e.linep > elinep) {
4278 while((c=readc()) != '\n' && c)
4280 err("input line too long");
4285 if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) {
4288 if (c == '\n' && ec != '\"')
4289 return(my_getc(ec));
4300 if (e.iop >= e.iobase)
4308 return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0);
4316 for (; e.iop >= e.iobase; e.iop--)
4317 if ((c = e.iop->peekc) != '\0') {
4322 if (e.iop->prev != 0) {
4323 if ((c = (*e.iop->iofn)(e.iop->argp, e.iop)) != '\0') {
4328 if (e.iop == iostack)
4330 return(e.iop->prev = c);
4332 else if (e.iop->task == XIO && e.iop->prev != '\n') {
4334 if (e.iop == iostack)
4339 if (e.iop->task == XIO) {
4341 return e.iop->prev = 0;
4342 if (interactive && e.iop == iostack+1) {
4343 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4344 current_prompt=prompt->value;
4351 if (e.iop >= iostack)
4363 write(2, &c, sizeof c);
4367 pushio(struct ioarg *argp, int (*fn)(struct ioarg *))
4369 if (++e.iop >= &iostack[NPUSH]) {
4371 err("Shell input nested too deeply");
4375 e.iop->iofn = (int (*)(struct ioarg *, struct io *))fn;
4377 if (argp->afid != AFID_NOBUF)
4380 e.iop->argp = ioargstack + (e.iop - iostack);
4381 *e.iop->argp = *argp;
4382 e.iop->argp->afbuf = e.iop == &iostack[0] ? &mainbuf : &sharedbuf;
4383 if (isatty(e.iop->argp->afile) == 0 &&
4384 (e.iop == &iostack[0] ||
4385 lseek(e.iop->argp->afile, 0L, 1) != -1)) {
4386 if (++bufid == AFID_NOBUF)
4388 e.iop->argp->afid = bufid;
4392 e.iop->prev = ~'\n';
4396 if (fn == filechar || fn == linechar)
4398 else if (fn == (int(*)(struct ioarg *))gravechar || fn == (int(*)(struct ioarg *))qgravechar)
4399 e.iop->task = XGRAVE;
4401 e.iop->task = XOTHER;
4408 register struct io *xp;
4416 * Input generating functions
4420 * Produce the characters of a string, then a newline, then EOF.
4424 register struct ioarg *ap;
4428 if (ap->aword == NULL)
4430 if ((c = *ap->aword++) == 0) {
4438 * Given a list of words, produce the characters
4439 * in them, with a space after each word.
4443 register struct ioarg *ap;
4448 if ((wl = ap->awordlist) == NULL)
4451 if ((c = *(*wl)++) != 0)
4456 ap->awordlist = NULL;
4461 * Return the characters of a list of words,
4462 * producing a space between them.
4466 register struct ioarg *ap;
4470 if ((wp = *ap->awordlist++) != NULL) {
4471 PUSHIO(aword, wp, *ap->awordlist == NULL? strchar: xxchar);
4479 register struct ioarg *ap;
4483 if (ap->aword == NULL)
4485 if ((c = *ap->aword++) == '\0') {
4493 * Produce the characters from a single word (string).
4497 register struct ioarg *ap;
4501 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4507 * Produce quoted characters from a single word (string).
4511 register struct ioarg *ap;
4515 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4521 * Return the characters from a file.
4525 register struct ioarg *ap;
4529 struct iobuf *bp = ap->afbuf;
4531 if (ap->afid != AFID_NOBUF) {
4532 if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
4534 lseek(ap->afile, ap->afpos, 0);
4535 i = safe_read(ap->afile, bp->buf, sizeof(bp->buf));
4541 bp->ebufp = (bp->bufp = bp->buf) + i;
4544 return *bp->bufp++ & 0177;
4547 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4548 if (interactive && isatty(ap->afile)) {
4549 static char mycommand[BUFSIZ];
4550 static int position = 0, size = 0;
4552 while (size == 0 || position >= size) {
4553 cmdedit_read_input(current_prompt, mycommand);
4554 size = strlen(mycommand);
4557 c = mycommand[position];
4563 i = safe_read(ap->afile, &c, sizeof(c));
4564 return(i == sizeof(c)? c&0177: (closef(ap->afile), 0));
4569 * Return the characters from a here temp file.
4573 register struct ioarg *ap;
4578 if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
4587 * Return the characters produced by a process (`...`).
4588 * Quote them if required, and remove any trailing newline characters.
4597 if ((c = qgravechar(ap, iop)&~QUOTE) == '\n')
4604 register struct ioarg *ap;
4616 } else if ((c = filechar(ap)) == '\n') {
4618 while ((c = filechar(ap)) == '\n')
4626 return(c!=0? c|QUOTE: 0);
4630 * Return a single command (usually the first line) from a file.
4634 register struct ioarg *ap;
4638 if ((c = filechar(ap)) == '\n') {
4641 ap->afile = -1; /* illegal value */
4652 write(2, s, strlen(s));
4675 for (u=NUFILE; u<NOFILE;)
4680 * remap fd into Shell's fd space
4690 for (i=0; i<NOFILE; i++)
4695 } while (fd >= 0 && fd < e.iofd);
4696 for (i=0; i<NOFILE; i++)
4700 err("too many files open in shell");
4711 if ((i = pipe(pv)) < 0)
4712 err("can't create pipe - try again");
4726 /* -------- here.c -------- */
4737 register struct here *h, *lh;
4739 h = (struct here *) space(sizeof(struct here));
4742 h->h_tag = evalstr(s, DOSUB);
4751 for (lh = inhere; lh!=NULL; lh = lh->h_next)
4752 if (lh->h_next == 0) {
4756 iop->io_flag |= IOHERE|IOXHERE;
4757 for (s = h->h_tag; *s; s++)
4759 iop->io_flag &= ~ IOXHERE;
4762 h->h_dosub = iop->io_flag & IOXHERE;
4768 register struct here *h, *hp;
4770 /* Scan here files first leaving inhere list in place */
4771 for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
4772 readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub? 0: '\'');
4774 /* Make inhere list active - keep list intact for scraphere */
4776 hp->h_next = acthere;
4783 readhere(name, s, ec)
4789 char tname[30] = ".msh_XXXXXX";
4792 char myline [LINELIM+1];
4795 tf = mkstemp(tname);
4798 *name = strsave(tname, areanum);
4799 if (newenv(setjmp(errpt = ev)) != 0)
4802 pushio(e.iop->argp, (int(*)(struct ioarg *))e.iop->iofn);
4805 if (interactive && e.iop <= iostack) {
4806 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4807 current_prompt=cprompt->value;
4809 prs(cprompt->value);
4813 while ((c = my_getc(ec)) != '\n' && c) {
4816 if (thenext >= &myline[LINELIM]) {
4823 if (strcmp(s, myline) == 0 || c == 0)
4826 write (tf, myline, (int)(thenext-myline));
4829 prs("here document `"); prs(s); err("' unclosed");
4837 * open here temp file.
4838 * if unquoted here, expand here temp file into second temp file.
4841 herein(hname, xdoll)
4849 /* Avoid longjmp clobbering */
4854 hf = open(hname, 0);
4859 char tname[30] = ".msh_XXXXXX";
4862 tf = mkstemp(tname);
4865 if (newenv(setjmp(errpt = ev)) == 0) {
4866 PUSHIO(afile, hf, herechar);
4868 while ((c = subgetc(0, 0)) != 0) {
4870 write(tf, &c, sizeof c);
4876 tf = open(tname, 0);
4886 register struct here *h;
4888 for (h = inhere; h != NULL; h = h->h_next) {
4889 if (h->h_iop && h->h_iop->io_name)
4890 unlink(h->h_iop->io_name);
4895 /* unlink here temp files before a freearea(area) */
4900 register struct here *h, *hl;
4903 for (h = acthere; h != NULL; h = h->h_next)
4904 if (getarea((char *) h) >= area) {
4905 if (h->h_iop->io_name != NULL)
4906 unlink(h->h_iop->io_name);
4908 acthere = h->h_next;
4910 hl->h_next = h->h_next;
4918 * Copyright (c) 1987,1997, Prentice Hall
4919 * All rights reserved.
4921 * Redistribution and use of the MINIX operating system in source and
4922 * binary forms, with or without modification, are permitted provided
4923 * that the following conditions are met:
4925 * Redistributions of source code must retain the above copyright
4926 * notice, this list of conditions and the following disclaimer.
4928 * Redistributions in binary form must reproduce the above
4929 * copyright notice, this list of conditions and the following
4930 * disclaimer in the documentation and/or other materials provided
4931 * with the distribution.
4933 * Neither the name of Prentice Hall nor the names of the software
4934 * authors or contributors may be used to endorse or promote
4935 * products derived from this software without specific prior
4936 * written permission.
4938 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
4939 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
4940 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4941 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4942 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
4943 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4944 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4945 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
4946 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4947 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
4948 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
4949 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.