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 <andersee@debian.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Original copyright notice is retained at the end of this file.
39 #include <sys/times.h>
40 #include <sys/types.h>
47 /* -------- sh.h -------- */
53 #define NPUSH 8 /* limit to input nesting */
55 #define NOFILE 20 /* Number of open files */
56 #define NUFILE 10 /* Number of user-accessible files */
57 #define FDBASE 10 /* First file usable by Shell */
60 * values returned by wait
62 #define WAITSIG(s) ((s)&0177)
63 #define WAITVAL(s) (((s)>>8)&0377)
64 #define WAITCORE(s) (((s)&0200)!=0)
67 * library and system defintions
69 typedef void xint; /* base type of jmp_buf, for not broken compilers */
77 #define NOBLOCK ((struct op *)NULL)
78 #define NOWORD ((char *)NULL)
79 #define NOWORDS ((char **)NULL)
80 #define NOPIPE ((int *)NULL)
83 * Description of a command or an operation on commands.
84 * Might eventually use a union.
87 int type; /* operation type, see below */
88 char **words; /* arguments to a command */
89 struct ioword **ioact; /* IO actions (eg, < > >>) */
92 char *str; /* identifier for case and for */
95 #define TCOM 1 /* command */
96 #define TPAREN 2 /* (c-list) */
97 #define TPIPE 3 /* a | b */
98 #define TLIST 4 /* a [&;] b */
99 #define TOR 5 /* || */
100 #define TAND 6 /* && */
108 #define TPAT 14 /* pattern in case */
109 #define TBRACE 15 /* {c-list} */
110 #define TASYNC 16 /* c & */
113 * actions determining the environment of a process
115 #define BIT(i) (1<<(i))
116 #define FEXEC BIT(0) /* execute without forking */
119 * flags to control evaluation of words
121 #define DOSUB 1 /* interpret $, `, and quotes */
122 #define DOBLANK 2 /* perform blank interpretation */
123 #define DOGLOB 4 /* interpret [?* */
124 #define DOKEY 8 /* move words with `=' to 2nd arg. list */
125 #define DOTRIM 16 /* trim resulting string */
127 #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
133 static int interactive; /* Is this an interactive shell */
135 static int multiline; /* \n changed to ; */
136 static struct op *outtree; /* result from parser */
140 static struct brkcon *brklist;
142 static int newfile(char *s);
143 static char *findeq(char *cp);
144 static char *cclass(char *p, int sub);
145 static void initarea(void);
146 extern int msh_main(int argc, char **argv);
151 struct brkcon *nextlev;
158 short io_unit; /* unit affected */
159 short io_flag; /* action (below) */
160 char *io_name; /* file name */
162 #define IOREAD 1 /* < */
163 #define IOHERE 2 /* << (here file) */
164 #define IOWRITE 4 /* > */
165 #define IOCAT 8 /* >> */
166 #define IOXHERE 16 /* ${}, ` in << */
167 #define IODUP 32 /* >&digit */
168 #define IOCLOSE 64 /* >&- */
170 #define IODEFAULT (-1) /* token for default IO unit */
172 static struct wdblock *wdlist;
173 static struct wdblock *iolist;
176 * parsing & execution environment
190 * -k: look for name=value everywhere on command line
192 * -t: exit after reading and executing one command
195 * -u: unset variables net diagnostic
199 static char *null; /* null value for variable */
200 static int intr; /* interrupt pending */
202 static char *trap[_NSIG+1];
203 static char ourtrap[_NSIG+1];
204 static int trapset; /* trap pending */
206 static int heedint; /* heed interrupt signals */
208 static int yynerrs; /* yacc */
210 static char line[LINELIM];
216 static int (*inbuilt(char *s ))(void);
218 static char *rexecve (char *c , char **v, char **envp );
219 static char *space (int n );
220 static char *strsave (char *s, int a );
221 static char *evalstr (char *cp, int f );
222 static char *putn (int n );
223 static char *itoa (unsigned u, int n );
224 static char *unquote (char *as );
225 static struct var *lookup (char *n );
226 static int rlookup (char *n );
227 static struct wdblock *glob (char *cp, struct wdblock *wb );
228 static int my_getc( int ec);
229 static int subgetc (int ec, int quoted );
230 static char **makenv (void);
231 static char **eval (char **ap, int f );
232 static int setstatus (int s );
233 static int waitfor (int lastpid, int canintr );
235 static void onintr (int s ); /* SIGINT handler */
237 static int newenv (int f );
238 static void quitenv (void);
239 static void err (char *s );
240 static int anys (char *s1, char *s2 );
241 static int any (int c, char *s );
242 static void next (int f );
243 static void setdash (void);
244 static void onecommand (void);
245 static void runtrap (int i );
246 static int gmatch (char *s, char *p );
251 static void leave (void); /* abort shell (or fail in subshell) */
252 static void fail (void); /* fail but return to process next command */
253 static void warn (char *s );
254 static void sig (int i ); /* default signal handler */
258 /* -------- area stuff -------- */
260 #define REGSIZE sizeof(struct region)
262 //#define SHRINKBY 64
266 #define ALIGN (sizeof(int)-1)
276 /* -------- grammar stuff -------- */
300 #define YYERRCODE 300
303 #define CONTIN 01 /* skip new lines to complete command */
305 #define SYNTAXERR zzerr()
306 static struct op *pipeline(int cf );
307 static struct op *andor(void);
308 static struct op *c_list(void);
309 static int synio(int cf );
310 static void musthave (int c, int cf );
311 static struct op *simple(void);
312 static struct op *nested(int type, int mark );
313 static struct op *command(int cf );
314 static struct op *dogroup(int onlydone );
315 static struct op *thenpart(void);
316 static struct op *elsepart(void);
317 static struct op *caselist(void);
318 static struct op *casepart(void);
319 static char **pattern(void);
320 static char **wordlist(void);
321 static struct op *list(struct op *t1, struct op *t2 );
322 static struct op *block(int type, struct op *t1, struct op *t2, char **wp );
323 static struct op *newtp(void);
324 static struct op *namelist(struct op *t );
325 static char **copyw(void);
326 static void word(char *cp );
327 static struct ioword **copyio(void);
328 static struct ioword *io (int u, int f, char *cp );
329 static void zzerr(void);
330 static void yyerror(char *s );
331 static int yylex(int cf );
332 static int collect(int c, int c1 );
333 static int dual(int c );
334 static void diag(int ec );
335 static char *tree(unsigned size );
337 /* -------- var.h -------- */
345 #define COPYV 1 /* flag to setval, suggesting copy */
346 #define RONLY 01 /* variable is read-only */
347 #define EXPORT 02 /* variable is to be exported */
348 #define GETCELL 04 /* name & value space was got with getcell */
350 static struct var *vlist; /* dictionary */
352 static struct var *homedir; /* home directory */
353 static struct var *prompt; /* main prompt */
354 static struct var *cprompt; /* continuation prompt */
355 static struct var *path; /* search path for commands */
356 static struct var *shell; /* shell to interpret command files */
357 static struct var *ifs; /* field separators */
359 static int yyparse (void);
360 static struct var *lookup (char *n );
361 static void setval (struct var *vp, char *val );
362 static void nameval (struct var *vp, char *val, char *name );
363 static void export (struct var *vp );
364 static void ronly (struct var *vp );
365 static int isassign (char *s );
366 static int checkname (char *cp );
367 static int assign (char *s, int cf );
368 static void putvlist (int f, int out );
369 static int eqname (char *n1, char *n2 );
371 static int execute (struct op *t, int *pin, int *pout, int act );
373 /* -------- io.h -------- */
376 unsigned id; /* buffer id */
377 char buf[512]; /* buffer */
378 char *bufp; /* pointer into buffer */
379 char *ebufp; /* pointer to end of buffer */
382 /* possible arguments to an IO function */
386 int afile; /* file descriptor */
387 unsigned afid; /* buffer id */
388 long afpos; /* file position */
389 struct iobuf *afbuf; /* buffer for this file */
391 //static struct ioarg ioargstack[NPUSH];
392 #define AFID_NOBUF (~0)
395 /* an input generator's state */
400 char prev; /* previous character read by readc() */
401 char nlcount; /* for `'s */
402 char xchar; /* for `'s */
403 char task; /* reason for pushed IO */
405 //static struct io iostack[NPUSH];
406 #define XOTHER 0 /* none of the below */
407 #define XDOLL 1 /* expanding ${} */
408 #define XGRAVE 2 /* expanding `'s */
409 #define XIO 3 /* file IO */
411 /* in substitution */
412 #define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL)
415 * input generators for IO structure
417 static int nlchar (struct ioarg *ap );
418 static int strchar (struct ioarg *ap );
419 static int qstrchar (struct ioarg *ap );
420 static int filechar (struct ioarg *ap );
421 static int herechar (struct ioarg *ap );
422 static int linechar (struct ioarg *ap );
423 static int gravechar (struct ioarg *ap, struct io *iop );
424 static int qgravechar (struct ioarg *ap, struct io *iop );
425 static int dolchar (struct ioarg *ap );
426 static int wdchar (struct ioarg *ap );
427 static void scraphere (void);
428 static void freehere (int area );
429 static void gethere (void);
430 static void markhere (char *s, struct ioword *iop );
431 static int herein (char *hname, int xdoll );
432 static int run (struct ioarg *argp, int (*f)());
437 static int eofc (void);
438 static int readc (void);
439 static void unget (int c );
440 static void ioecho (int c );
441 static void prs (char *s );
442 static void prn (unsigned u );
443 static void closef (int i );
444 static void closeall (void);
449 static void pushio (struct ioarg *argp, int (*fn)());
450 static int remap (int fd );
451 static int openpipe (int *pv );
452 static void closepipe (int *pv );
453 static struct io *setbase (struct io *ip );
455 static struct ioarg temparg; /* temporary for PUSHIO */
456 #define PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen)))
457 #define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen)))
459 /* -------- word.h -------- */
461 #define NSTART 16 /* default number of words to allow for initially */
466 /* bounds are arbitrary */
470 static struct wdblock *addword (char *wd, struct wdblock *wb );
471 static struct wdblock *newword (int nw );
472 static char **getwords (struct wdblock *wb );
474 /* -------- area.h -------- */
479 static char *getcell (unsigned nbytes );
480 static void garbage (void);
481 static void setarea (char *cp, int a );
482 static int getarea (char *cp );
483 static void freearea (int a );
484 static void freecell (char *cp );
485 static int areanum; /* current allocation area */
487 #define NEW(type) (type *)getcell(sizeof(type))
488 #define DELETE(obj) freecell((char *)obj)
491 /* -------- misc stuff -------- */
493 static int forkexec (struct op *t, int *pin, int *pout, int act, char **wp, int *pforked );
494 static int iosetup (struct ioword *iop, int pipein, int pipeout );
495 static void echo(char **wp );
496 static struct op **find1case (struct op *t, char *w );
497 static struct op *findcase (struct op *t, char *w );
498 static void brkset(struct brkcon *bc );
499 static int dolabel(void);
500 static int dohelp(void);
501 static int dochdir(struct op *t );
502 static int doshift(struct op *t );
503 static int dologin(struct op *t );
504 static int doumask(struct op *t );
505 static int doexec(struct op *t );
506 static int dodot(struct op *t );
507 static int dowait(struct op *t );
508 static int doread(struct op *t );
509 static int doeval(struct op *t );
510 static int dotrap(struct op *t );
511 static int getsig(char *s );
512 static void setsig (int n, void (*f)());
513 static int getn(char *as );
514 static int dobreak(struct op *t );
515 static int docontinue(struct op *t );
516 static int brkcontin (char *cp, int val );
517 static int doexit(struct op *t );
518 static int doexport(struct op *t );
519 static int doreadonly(struct op *t );
520 static void rdexp (char **wp, void (*f)(), int key);
521 static void badid(char *s );
522 static int doset(struct op *t );
523 static void varput (char *s, int out );
524 static int dotimes(void);
525 static int expand (char *cp, struct wdblock **wbp, int f );
526 static char *blank(int f );
527 static int dollar(int quoted );
528 static int grave(int quoted );
529 static void globname (char *we, char *pp );
530 static char *generate (char *start1, char *end1, char *middle, char *end );
531 static int anyspcl(struct wdblock *wb );
532 static int xstrcmp (char *p1, char *p2 );
533 static void glob0 (char *a0, unsigned int a1, int a2, int (*a3)(char *, char *));
534 static void glob1 (char *base, char *lim );
535 static void glob2 (char *i, char *j );
536 static void glob3 (char *i, char *j, char *k );
537 static void readhere (char **name, char *s, int ec );
538 static void pushio(struct ioarg *argp, int (*fn)());
539 static int xxchar(struct ioarg *ap );
544 struct ioword *h_iop;
548 static char *signame[] = {
551 (char *)NULL, /* interrupt */
553 "Illegal instruction",
557 "Floating Point Exception",
562 (char *)NULL, /* broken pipe */
566 #define NSIGNAL (sizeof(signame)/sizeof(signame[0]))
572 static struct res restab[] = {
598 int (*builtinfunc)();
600 static const struct builtincmd builtincmds[] = {
605 {"continue",docontinue},
609 {"export", doexport},
614 {"readonly",doreadonly},
625 extern char **environ; /* environment pointer */
630 static int interactive; /* Is this an interactive shell */
632 static int multiline; /* \n changed to ; */
633 static struct op *outtree; /* result from parser */
636 static struct brkcon *brklist;
638 static struct wdblock *wdlist;
639 static struct wdblock *iolist;
640 static char *trap[_NSIG+1];
641 static char ourtrap[_NSIG+1];
642 static int trapset; /* trap pending */
643 static int yynerrs; /* yacc */
644 static char line[LINELIM];
645 static struct var *vlist; /* dictionary */
646 static struct var *homedir; /* home directory */
647 static struct var *prompt; /* main prompt */
648 static struct var *cprompt; /* continuation prompt */
649 static struct var *path; /* search path for commands */
650 static struct var *shell; /* shell to interpret command files */
651 static struct var *ifs; /* field separators */
652 static struct ioarg ioargstack[NPUSH];
653 static struct io iostack[NPUSH];
654 static int areanum; /* current allocation area */
657 static char flags['z'-'a'+1];
658 static char *flag = flags-'a';
659 static char *elinep = line+sizeof(line)-5;
660 static char *null = "";
661 static int heedint =1;
662 static struct env e ={line, iostack, iostack-1, (xint *)NULL, FDBASE, (struct env *)NULL};
663 static void (*qflag)(int) = SIG_IGN;
664 static char shellname[] = "/bin/sh";
665 static char search[] = ":/bin:/usr/bin";
669 static int iounit = IODEFAULT;
670 static YYSTYPE yylval;
671 static struct iobuf sharedbuf = {AFID_NOBUF};
672 static struct iobuf mainbuf = {AFID_NOBUF};
673 static unsigned bufid = AFID_ID; /* buffer id counter */
674 static struct ioarg temparg = {0, 0, 0, AFID_NOBUF, 0};
675 static struct here *inhere; /* list of hear docs while parsing */
676 static struct here *acthere; /* list of active here documents */
677 static struct region *areabot; /* bottom of area */
678 static struct region *areatop; /* top of area */
679 static struct region *areanxt; /* starting point of scan */
680 static void * brktop;
681 static void * brkaddr;
684 #ifdef BB_FEATURE_COMMAND_EDITING
685 static char * current_prompt;
689 /* -------- sh.c -------- */
695 extern int msh_main(int argc, char **argv)
704 if ((ap = environ) != NULL) {
706 assign(*ap++, !COPYV);
707 for (ap = environ; *ap;)
708 export(lookup(*ap++));
713 shell = lookup("SHELL");
714 if (shell->value == null)
715 setval(shell, shellname);
718 homedir = lookup("HOME");
719 if (homedir->value == null)
720 setval(homedir, "/");
723 setval(lookup("$"), itoa(getpid(), 5));
725 path = lookup("PATH");
726 if (path->value == null)
727 setval(path, search);
731 if (ifs->value == null)
732 setval(ifs, " \t\n");
734 prompt = lookup("PS1");
735 #ifdef BB_FEATURE_SH_FANCY_PROMPT
736 if (prompt->value == null)
738 setval(prompt, "$ ");
739 if (geteuid() == 0) {
740 setval(prompt, "# ");
741 prompt->status &= ~EXPORT;
743 cprompt = lookup("PS2");
744 #ifdef BB_FEATURE_SH_FANCY_PROMPT
745 if (cprompt->value == null)
747 setval(cprompt, "> ");
753 if(argv[0][0] == '-' && argv[0][1] != '\0') {
754 for (s = argv[0]+1; *s; s++)
757 prompt->status &= ~EXPORT;
758 cprompt->status &= ~EXPORT;
763 PUSHIO(aword, *++argv, iof = nlchar);
775 prompt->status &= ~EXPORT;
783 if (*s>='a' && *s<='z')
790 if (iof == filechar && --argc > 0) {
793 prompt->status &= ~EXPORT;
794 cprompt->status &= ~EXPORT;
795 if (newfile(name = *++argv))
800 if (e.iop < iostack) {
801 PUSHIO(afile, 0, iof);
802 if (isatty(0) && isatty(1) && !cflag) {
804 printf( "\n\n" BB_BANNER " Built-in shell (msh)\n");
805 printf( "Enter 'help' for a list of built-in commands.\n\n");
808 signal(SIGQUIT, qflag);
809 if (name && name[0] == '-') {
811 if ((f = open(".profile", 0)) >= 0)
813 if ((f = open("/etc/profile", 0)) >= 0)
817 signal(SIGTERM, sig);
818 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
819 signal(SIGINT, onintr);
824 for (ap = ++argv; --argc > 0;) {
825 if (assign(*ap = *argv++, !COPYV)) {
826 dolc--; /* keyword */
832 setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
835 if (interactive && e.iop <= iostack) {
836 #ifdef BB_FEATURE_COMMAND_EDITING
837 current_prompt=prompt->value;
854 for (c='a'; c<='z'; c++)
858 setval(lookup("-"), m);
867 if (strcmp(s, "-") != 0) {
871 err(": cannot open");
901 setjmp(failpt = m1); /* Bruce Evans' fix */
902 if (setjmp(failpt = m1) || yyparse() || intr) {
906 if (!interactive && intr)
917 execute(outtree, NOPIPE, NOPIPE, 0);
918 if (!interactive && intr) {
922 if ((i = trapset) != 0) {
972 e.iop = e.iobase = iostack;
979 register struct env *ep;
985 ep = (struct env *) space(sizeof(*ep));
1000 register struct env *ep;
1003 if ((ep = e.oenv) != NULL) {
1006 /* should close `'d files */
1008 while (--fd >= e.iofd)
1014 * Is any character from s1 in s2?
1018 register char *s1, *s2;
1027 * Is character c in s?
1044 return(itoa(n, -1));
1049 register unsigned u;
1057 if (n < 0 && (int) u < 0) {
1066 } while (--n > 0 || u);
1076 PUSHIO(afile, f, filechar);
1081 int s; /* ANSI C requires a parameter */
1083 signal(SIGINT, onintr);
1103 if ((cp = getcell(n)) == 0)
1104 err("out of string space");
1113 register char *cp, *xp;
1115 if ((cp = space(strlen(s)+1)) != NULL) {
1116 setarea((char *)cp, a);
1117 for (xp = cp; (*xp++ = *s++) != '\0';)
1135 static void runtrap(i)
1140 if ((trapstr = trap[i]) == NULL)
1144 RUN(aword, trapstr, nlchar);
1147 /* -------- var.c -------- */
1150 * Find the given name in the dictionary
1151 * and return its value. If the name was
1152 * not previously there, enter it now and
1153 * return a null value.
1159 register struct var *vp;
1162 static struct var dummy;
1166 for (c = 0; isdigit(*n) && c < 1000; n++)
1168 dummy.status = RONLY;
1169 dummy.value = c <= dolc? dolv[c]: null;
1172 for (vp = vlist; vp; vp = vp->next)
1173 if (eqname(vp->name, n))
1176 vp = (struct var *)space(sizeof(*vp));
1177 if (vp == 0 || (vp->name = space((int)(cp-n)+2)) == 0) {
1178 dummy.name = dummy.value = "";
1181 for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++)
1186 setarea((char *)vp, 0);
1187 setarea((char *)vp->name, 0);
1190 vp->status = GETCELL;
1196 * give variable at `vp' the value `val'.
1203 nameval(vp, val, (char *)NULL);
1207 * if name is not NULL, it must be
1208 * a prefix of the space `val',
1210 * this is all so that exporting
1211 * values is reasonably painless.
1214 nameval(vp, val, name)
1215 register struct var *vp;
1218 register char *cp, *xp;
1222 if (vp->status & RONLY) {
1223 for (xp = vp->name; *xp && *xp != '=';)
1224 putc(*xp++, stderr);
1225 err(" is read-only");
1230 xp = space(strlen(vp->name)+strlen(val)+2);
1233 /* make string: name=value */
1234 setarea((char *)xp, 0);
1236 for (cp = vp->name; (*xp = *cp++) && *xp!='='; xp++)
1241 for (cp = val; (*xp++ = *cp++) != '\0';)
1246 if (vp->status & GETCELL)
1247 freecell(vp->name); /* form new string `name=value' */
1257 vp->status |= EXPORT;
1264 if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */
1265 vp->status |= RONLY;
1272 if (!isalpha((int)*s) && *s != '_')
1274 for (; *s != '='; s++)
1275 if (*s == 0 || (!isalnum(*s) && *s != '_'))
1288 if (!isalpha(*s) && *s != '_')
1290 for (cp = s; *cp != '='; cp++)
1291 if (*cp == 0 || (!isalnum(*cp) && *cp != '_'))
1294 nameval(vp, ++cp, cf == COPYV? (char *)NULL: s);
1296 vp->status &= ~GETCELL;
1304 if (!isalpha(*cp++) && *(cp-1) != '_')
1307 if (!isalnum(*cp++) && *(cp-1) != '_')
1314 register int f, out;
1316 register struct var *vp;
1318 for (vp = vlist; vp; vp = vp->next)
1319 if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) {
1320 if (vp->status & EXPORT)
1321 write(out, "export ", 7);
1322 if (vp->status & RONLY)
1323 write(out, "readonly ", 9);
1324 write(out, vp->name, (int)(findeq(vp->name) - vp->name));
1325 write(out, "\n", 1);
1331 register char *n1, *n2;
1333 for (; *n1 != '=' && *n1 != 0; n1++)
1336 return(*n2 == 0 || *n2 == '=');
1343 while (*cp != '\0' && *cp != '=')
1348 /* -------- gmatch.c -------- */
1350 * int gmatch(string, pattern)
1351 * char *string, *pattern;
1353 * Match a pattern as in sh(1).
1358 #define QMASK (CMASK&~QUOTE)
1359 #define NOT '!' /* might use ^ */
1363 register char *s, *p;
1365 register int sc, pc;
1367 if (s == NULL || p == NULL)
1369 while ((pc = *p++ & CMASK) != '\0') {
1373 if ((p = cclass(p, sc)) == NULL)
1385 if (*p == '\0' || gmatch(s, p))
1387 } while (*s++ != '\0');
1391 if (sc != (pc&~QUOTE))
1403 register int c, d, not, found;
1405 if ((not = *p == NOT) != 0)
1410 return((char *)NULL);
1412 if (p[1] == '-' && p[2] != ']') {
1417 if (c == sub || (c <= sub && sub <= d))
1419 } while (*++p != ']');
1420 return(found? p+1: (char *)NULL);
1424 /* -------- area.c -------- */
1427 * All memory between (char *)areabot and (char *)(areatop+1) is
1428 * exclusively administered by the area management routines.
1429 * It is assumed that sbrk() and brk() manipulate the high end.
1432 #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;})
1437 brkaddr = malloc(65000);
1438 brktop = brkaddr + 65000;
1440 while ((int)sbrk(0) & ALIGN)
1442 areabot = (struct region *)sbrk(REGSIZE);
1444 areabot->next = areabot;
1445 areabot->area = BUSY;
1454 register int nregio;
1455 register struct region *p, *q;
1461 } /* silly and defeats the algorithm */
1463 * round upwards and add administration area
1465 nregio = (nbytes+(REGSIZE-1))/REGSIZE + 1;
1466 for (p = areanxt;;) {
1467 if (p->area > areanum) {
1471 while ((q = p->next)->area > areanum && q != areanxt)
1474 * exit loop if cell big enough
1476 if (q >= p + nregio)
1483 i = nregio >= GROWBY ? nregio : GROWBY;
1484 p = (struct region *)sbrk(i * REGSIZE);
1485 if (p == (struct region *)-1)
1486 return((char *)NULL);
1490 abort(); /* allocated areas are contiguous */
1500 * we found a FREE area big enough, pointed to by 'p', and up to 'q'
1502 areanxt = p + nregio;
1505 * split into requested area and rest
1507 if (areanxt+1 > q) {
1509 abort(); /* insufficient space left for admin */
1512 areanxt->area = FREE;
1516 return((char *)(p+1));
1523 register struct region *p;
1525 if ((p = (struct region *)cp) != NULL) {
1537 register struct region *p, *top;
1540 for (p = areabot; p != top; p = p->next)
1550 register struct region *p;
1552 if ((p = (struct region *)cp) != NULL)
1560 return ((struct region*)cp-1)->area;
1566 register struct region *p, *q, *top;
1569 for (p = areabot; p != top; p = p->next) {
1570 if (p->area > areanum) {
1571 while ((q = p->next)->area > areanum)
1577 if (areatop >= q + SHRINKBY && q->area > areanum) {
1586 /* -------- csyn.c -------- */
1588 * shell: syntax (C version)
1607 register struct op *t, *p;
1612 while ((c = yylex(0)) == '|') {
1613 if ((p = command(CONTIN)) == NULL)
1615 if (t->type != TPAREN && t->type != TCOM) {
1616 /* shell statement */
1617 t = block(TPAREN, t, NOBLOCK, NOWORDS);
1619 t = block(TPIPE, t, p, NOWORDS);
1629 register struct op *t, *p;
1634 while ((c = yylex(0)) == LOGAND || c == LOGOR) {
1635 if ((p = pipeline(CONTIN)) == NULL)
1637 t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS);
1647 register struct op *t, *p;
1652 if((peeksym = yylex(0)) == '&')
1653 t = block(TASYNC, t, NOBLOCK, NOWORDS);
1654 while ((c = yylex(0)) == ';' || c == '&' || (multiline && c == '\n')) {
1655 if ((p = andor()) == NULL)
1657 if((peeksym = yylex(0)) == '&')
1658 p = block(TASYNC, p, NOBLOCK, NOWORDS);
1671 register struct ioword *iop;
1675 if ((c = yylex(cf)) != '<' && c != '>') {
1681 iop = io(iounit, i, yylval.cp);
1684 markhere(yylval.cp, iop);
1692 if ((peeksym = yylex(cf)) != c)
1700 register struct op *t;
1704 switch (peeksym = yylex(0)) {
1729 register struct op *t;
1735 return(block(type, t, NOBLOCK, NOWORDS));
1742 register struct op *t;
1743 struct wdblock *iosave;
1752 switch (c = yylex(cf)) {
1755 if ((t = simple()) == NULL) {
1757 return((struct op *)NULL);
1764 t = nested(TPAREN, ')');
1768 t = nested(TBRACE, '}');
1778 t->words = wordlist();
1779 if ((c = yylex(0)) != '\n' && c != ';')
1781 t->left = dogroup(0);
1789 t->type = c == WHILE? TWHILE: TUNTIL;
1791 t->right = dogroup(1);
1803 musthave(IN, CONTIN);
1805 t->left = caselist();
1815 t->right = thenpart();
1832 register struct op *mylist;
1835 if (c == DONE && onlydone)
1836 return((struct op *)NULL);
1848 register struct op *t;
1850 if ((c = yylex(0)) != THEN) {
1852 return((struct op *)NULL);
1857 if (t->left == NULL)
1859 t->right = elsepart();
1867 register struct op *t;
1869 switch (c = yylex(0)) {
1871 if ((t = c_list()) == NULL)
1879 t->right = thenpart();
1884 return((struct op *)NULL);
1891 register struct op *t;
1894 while ((peeksym = yylex(CONTIN)) != ESAC)
1895 t = list(t, casepart());
1902 register struct op *t;
1906 t->words = pattern();
1909 if ((peeksym = yylex(CONTIN)) != ESAC)
1910 musthave(BREAK, CONTIN);
1924 } while ((c = yylex(0)) == '|');
1935 if ((c = yylex(0)) != IN) {
1937 return((char **)NULL);
1940 while ((c = yylex(0)) == WORD)
1948 * supporting functions
1952 register struct op *t1, *t2;
1958 return(block(TLIST, t1, t2, NOWORDS));
1962 block(type, t1, t2, wp)
1967 register struct op *t;
1981 register struct res *rp;
1983 for (rp = restab; rp->r_name; rp++)
1984 if (strcmp(rp->r_name, n) == 0)
1992 register struct op *t;
1994 t = (struct op *)tree(sizeof(*t));
2006 register struct op *t;
2009 iolist = addword((char *)NULL, iolist);
2010 t->ioact = copyio();
2013 if (t->type != TCOM) {
2014 if (t->type != TPAREN && t->ioact != NULL) {
2015 t = block(TPAREN, t, NOBLOCK, NOWORDS);
2016 t->ioact = t->left->ioact;
2017 t->left->ioact = NULL;
2031 wd = getwords(wdlist);
2040 wdlist = addword(cp, wdlist);
2043 static struct ioword **
2046 register struct ioword **iop;
2048 iop = (struct ioword **) getwords(iolist);
2053 static struct ioword *
2059 register struct ioword *iop;
2061 iop = (struct ioword *) tree(sizeof(*iop));
2065 iolist = addword((char *)iop, iolist);
2072 yyerror("syntax error");
2080 if (interactive && e.iop <= iostack) {
2082 while (eofc() == 0 && yylex(0) != '\n')
2096 if ((c = peeksym) > 0) {
2109 while ((c = my_getc(0)) == ' ' || c == '\t')
2113 if (any(c, "0123456789")) {
2114 unget(c1 = my_getc(0));
2115 if (c1 == '<' || c1 == '>') {
2125 while ((c = my_getc(0)) != 0 && c != '\n')
2135 if ((c = my_getc(0)) == '{') {
2136 if ((c = collect(c, '}')) != '\0')
2145 if ((c = collect(c, c)) != '\0')
2152 if ((c1 = dual(c)) != '\0') {
2170 if (multiline || cf & CONTIN) {
2171 if (interactive && e.iop <= iostack) {
2172 #ifdef BB_FEATURE_COMMAND_EDITING
2173 current_prompt=cprompt->value;
2175 prs(cprompt->value);
2192 while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n"))
2193 if (e.linep >= elinep)
2194 err("word too long");
2201 if (atstart && (c = rlookup(line))!=0) {
2205 yylval.cp = strsave(line, areanum);
2216 while ((c = my_getc(c1)) != c1) {
2221 prs("no closing "); yyerror(s);
2224 if (interactive && c == '\n' && e.iop <= iostack) {
2225 #ifdef BB_FEATURE_COMMAND_EDITING
2226 current_prompt=cprompt->value;
2228 prs(cprompt->value);
2242 register char *cp = s;
2247 if ((c = rlookup(s)) == 0)
2259 if (c == '>' || c == '<') {
2262 yylval.i = ec == '>'? IOWRITE|IOCAT: IOHERE;
2265 yylval.i = ec == '>'? IOWRITE: IOREAD;
2266 if (c != '&' || yylval.i == IOHERE)
2278 if ((t = getcell(size)) == NULL) {
2279 prs("command line too complicated\n");
2289 /* -------- exec.c -------- */
2297 execute(t, pin, pout, act)
2298 register struct op *t;
2302 register struct op *t1;
2303 volatile int i, rv, a;
2304 char *cp, **wp, **wp2;
2309 /* Avoid longjmp clobbering */
2318 wp = (wp2 = t->words) != NULL
2319 ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
2327 rv = forkexec(t, pin, pout, act, wp, &child);
2338 if ((rv = openpipe(pv)) < 0)
2340 pv[0] = remap(pv[0]);
2341 pv[1] = remap(pv[1]);
2342 (void) execute(t->left, pin, pv, 0);
2343 rv = execute(t->right, pv, pout, 0);
2348 (void) execute(t->left, pin, pout, 0);
2349 rv = execute(t->right, pin, pout, 0);
2354 int hinteractive = interactive;
2358 interactive = hinteractive;
2360 setval(lookup("!"), putn(i));
2371 signal(SIGINT, SIG_IGN);
2372 signal(SIGQUIT, SIG_IGN);
2374 signal(SIGTERM, SIG_DFL);
2378 open("/dev/null", 0);
2380 exit(execute(t->left, pin, pout, FEXEC));
2387 rv = execute(t->left, pin, pout, 0);
2388 if ((t1 = t->right)!=NULL && (rv == 0) == (t->type == TAND))
2389 rv = execute(t1, pin, pout, 0);
2399 while (*wp++ != NULL)
2402 vp = lookup(t->str);
2403 while (setjmp(bc.brkpt))
2407 for (t1 = t->left; i-- && *wp != NULL;) {
2409 rv = execute(t1, pin, pout, 0);
2411 brklist = brklist->nextlev;
2416 while (setjmp(bc.brkpt))
2421 while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE))
2422 rv = execute(t->right, pin, pout, 0);
2423 brklist = brklist->nextlev;
2428 if (t->right != NULL) {
2429 rv = !execute(t->left, pin, pout, 0) ?
2430 execute(t->right->left, pin, pout, 0):
2431 execute(t->right->right, pin, pout, 0);
2436 if ((cp = evalstr(t->str, DOSUB|DOTRIM)) == 0)
2438 if ((t1 = findcase(t->left, cp)) != NULL)
2439 rv = execute(t1, pin, pout, 0);
2444 if (iopp = t->ioact)
2446 if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
2451 if (rv >= 0 && (t1 = t->left))
2452 rv = execute(t1, pin, pout, 0);
2462 if (interactive && intr) {
2466 if ((i = trapset) != 0) {
2474 forkexec( register struct op *t, int *pin, int *pout, int act, char **wp, int *pforked)
2477 int (*shcom)() = NULL;
2480 struct ioword **iopp;
2490 struct brkcon * hbrklist;
2494 /* Avoid longjmp clobbering */
2507 rv = -1; /* system-detected error */
2508 if (t->type == TCOM) {
2509 while ((cp = *wp++) != NULL)
2513 /* strip all initial assignments */
2514 /* not correct wrt PATH=yyy command etc */
2516 echo (cp ? wp: owp);
2517 if (cp == NULL && t->ioact == NULL) {
2518 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2520 return(setstatus(0));
2522 else if (cp != NULL)
2523 shcom = inbuilt(cp);
2527 if (shcom == NULL && (f & FEXEC) == 0) {
2533 hinteractive = interactive;
2540 /* who wrote this crappy non vfork safe shit? */
2545 interactive = hinteractive;
2555 return(pout==NULL? setstatus(waitfor(i,0)): 0);
2559 signal(SIGINT, SIG_IGN);
2560 signal(SIGQUIT, SIG_IGN);
2570 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2574 if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) {
2575 err("piping to/from shell builtins not yet done");
2587 if ((iopp = t->ioact) != NULL) {
2588 if (shcom != NULL && shcom != doexec) {
2590 err(": cannot redirect shell command");
2594 if (iosetup(*iopp++, pin!=NULL, pout!=NULL))
2598 return(setstatus((*shcom)(t)));
2599 /* should use FIOCEXCL */
2600 for (i=FDBASE; i<NOFILE; i++)
2603 signal(SIGINT, SIG_DFL);
2604 signal(SIGQUIT, SIG_DFL);
2606 if (t->type == TPAREN)
2607 exit(execute(t->left, NOPIPE, NOPIPE, FEXEC));
2611 cp = rexecve(wp[0], wp, makenv());
2612 prs(wp[0]); prs(": "); warn(cp);
2621 * 0< 1> are ignored as required
2625 iosetup(iop, pipein, pipeout)
2626 register struct ioword *iop;
2627 int pipein, pipeout;
2629 register int u = -1;
2630 char *cp=NULL, *msg;
2632 if (iop->io_unit == IODEFAULT) /* take default */
2633 iop->io_unit = iop->io_flag&(IOREAD|IOHERE)? 0: 1;
2634 if (pipein && iop->io_unit == 0)
2636 if (pipeout && iop->io_unit == 1)
2638 msg = iop->io_flag&(IOREAD|IOHERE)? "open": "create";
2639 if ((iop->io_flag & IOHERE) == 0) {
2641 if ((cp = evalstr(cp, DOSUB|DOTRIM)) == NULL)
2644 if (iop->io_flag & IODUP) {
2645 if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
2647 err(": illegal >& argument");
2651 iop->io_flag = IOCLOSE;
2652 iop->io_flag &= ~(IOREAD|IOWRITE);
2654 switch (iop->io_flag) {
2660 case IOHERE|IOXHERE:
2661 u = herein(iop->io_name, iop->io_flag&IOXHERE);
2666 if ((u = open(cp, 1)) >= 0) {
2667 lseek(u, (long)0, 2);
2671 u = creat(cp, 0666);
2675 u = dup2(*cp-'0', iop->io_unit);
2679 close(iop->io_unit);
2688 if (u != iop->io_unit) {
2689 dup2(u, iop->io_unit);
2703 for (i=0; wp[i]; i++) {
2716 register struct op *t1;
2718 register char **wp, *cp;
2721 return((struct op **)NULL);
2722 if (t->type == TLIST) {
2723 if ((tp = find1case(t->left, w)) != NULL)
2725 t1 = t->right; /* TPAT */
2728 for (wp = t1->words; *wp;)
2729 if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp))
2731 return((struct op **)NULL);
2739 register struct op **tp;
2741 return((tp = find1case(t, w)) != NULL? *tp: (struct op *)NULL);
2745 * Enter a new loop level (marked for break/continue).
2751 bc->nextlev = brklist;
2756 * Wait for the last process created.
2757 * Print a message for each process found
2758 * that was killed by a signal.
2759 * Ignore interrupt signals while waiting
2760 * unless `canintr' is true.
2763 waitfor(lastpid, canintr)
2764 register int lastpid;
2767 register int pid, rv;
2769 int oheedint = heedint;
2776 if (errno != EINTR || canintr)
2779 if ((rv = WAITSIG(s)) != 0) {
2781 if (signame[rv] != NULL) {
2782 if (pid != lastpid) {
2789 if (pid != lastpid) {
2793 prs("Signal "); prn(rv); prs(" ");
2796 prs(" - core dumped");
2797 if (rv >= NSIGNAL || signame[rv])
2803 } while (pid != lastpid);
2810 if (exstat == 0) exstat = rv;
2822 setval(lookup("?"), putn(s));
2827 * PATH-searching interface to execve.
2828 * If getenv("PATH") were kept up-to-date,
2829 * execvp might be used.
2833 char *c, **v, **envp;
2836 register char *sp, *tp;
2837 int eacces = 0, asis = 0;
2839 #ifdef BB_FEATURE_SH_STANDALONE_SHELL
2841 #ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
2842 name = get_last_path_component(name);
2845 if (find_applet_by_name(name)) {
2846 /* We have to exec here since we vforked. Running
2847 * run_applet_by_name() won't work and bad things
2849 execve("/proc/self/exe", v, envp);
2850 execve("busybox", v, envp);
2854 sp = any('/', c)? "": path->value;
2856 while (asis || *sp != '\0') {
2859 for (; *sp != '\0'; tp++)
2860 if ((*tp = *sp++) == ':') {
2866 for (i = 0; (*tp++ = c[i++]) != '\0';)
2869 execve(e.linep, v, envp);
2875 execve("/bin/sh", v, envp);
2880 return("program too big");
2883 return("argument list too long");
2890 return(errno==ENOENT ? "not found" : "cannot execute");
2894 * Run the command produced by generator `f'
2895 * applied to stream `arg'.
2903 struct wdblock *swdlist;
2904 struct wdblock *siolist;
2910 /* Avoid longjmp clobbering */
2920 if (newenv(setjmp(errpt = ev)) == 0) {
2926 if (setjmp(failpt = rt) == 0 && yyparse() == 0)
2927 rv = execute(outtree, NOPIPE, NOPIPE, 0);
2934 freearea(areanum--);
2938 /* -------- do.c -------- */
2941 * built-in commands: doX
2947 const struct builtincmd *x;
2949 printf("\nBuilt-in commands:\n");
2950 printf("-------------------\n");
2952 for (col=0, x = builtincmds; x->builtinfunc != NULL; x++) {
2955 col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name);
2961 #ifdef BB_FEATURE_SH_STANDALONE_SHELL
2964 const struct BB_applet *applet;
2965 extern const struct BB_applet applets[];
2966 extern const size_t NUM_APPLETS;
2968 for (i=0, applet = applets; i < NUM_APPLETS; applet++, i++) {
2972 col += printf("%s%s", ((col == 0) ? "\t" : " "),
2982 return EXIT_SUCCESS;
2995 register struct op *t;
2997 register char *cp, *er;
2999 if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL)
3000 er = ": no home directory";
3001 else if(chdir(cp) < 0)
3002 er = ": bad directory";
3005 prs(cp != NULL? cp: "cd");
3012 register struct op *t;
3016 n = t->words[1]? getn(t->words[1]): 1;
3018 err("nothing to shift");
3024 setval(lookup("#"), putn(dolc));
3029 * execute login and newgrp directly
3038 signal(SIGINT, SIG_DFL);
3039 signal(SIGQUIT, SIG_DFL);
3041 cp = rexecve(t->words[0], t->words, makenv());
3042 prs(t->words[0]); prs(": "); err(cp);
3048 register struct op *t;
3053 if ((cp = t->words[1]) == NULL) {
3056 for (n=3*4; (n-=3) >= 0;)
3057 putc('0'+((i>>n)&07), stderr);
3060 for (n=0; *cp>='0' && *cp<='9'; cp++)
3061 n = n*8 + (*cp-'0');
3069 register struct op *t;
3076 for(i = 0; (t->words[i]=t->words[i+1]) != NULL; i++)
3082 if (setjmp(failpt = ex) == 0)
3083 execute(t, NOPIPE, NOPIPE, FEXEC);
3094 register char *sp, *tp;
3097 if ((cp = t->words[1]) == NULL)
3099 sp = any('/', cp)? ":": path->value;
3102 while (*sp && (*tp = *sp++) != ':')
3106 for (i = 0; (*tp++ = cp[i++]) != '\0';)
3108 if ((i = open(e.linep, 0)) >= 0) {
3126 if ((cp = t->words[1]) != NULL) {
3132 setstatus(waitfor(i, 1));
3140 register char *cp, **wp;
3141 register int nb = 0;
3142 register int nl = 0;
3144 if (t->words[1] == NULL) {
3145 err("Usage: read name ...");
3148 for (wp = t->words+1; *wp; wp++) {
3149 for (cp = e.linep; !nl && cp < elinep-1; cp++)
3150 if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) ||
3151 (nl = (*cp == '\n')) ||
3152 (wp[1] && any(*cp, ifs->value)))
3157 setval(lookup(*wp), e.linep);
3164 register struct op *t;
3166 return(RUN(awordlist, t->words+1, wdchar));
3171 register struct op *t;
3174 register int resetsig;
3176 if (t->words[1] == NULL) {
3177 for (i=0; i<=_NSIG; i++)
3186 resetsig = isdigit(*t->words[1]);
3187 for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) {
3188 n = getsig(t->words[i]);
3192 if (*t->words[1] != '\0') {
3193 trap[n] = strsave(t->words[1], 0);
3202 setsig(n, n == SIGQUIT ? SIG_IGN
3217 if ((n = getn(s)) < 0 || n > _NSIG) {
3218 err("trap: bad signal number");
3225 setsig( register int n, void (*f)(int))
3229 if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) {
3248 for (n = 0; isdigit(*s); s++)
3249 n = (n*10) + (*s-'0');
3252 err(": bad number");
3261 return(brkcontin(t->words[1], 1));
3268 return(brkcontin(t->words[1], 0));
3276 register struct brkcon *bc;
3279 nl = cp == NULL? 1: getn(cp);
3283 if ((bc = brklist) == NULL)
3285 brklist = bc->nextlev;
3288 err("bad break/continue level");
3292 longjmp(bc->brkpt, 1);
3303 if ((cp = t->words[1]) != NULL)
3304 setstatus(getn(cp));
3314 rdexp(t->words+1, export, EXPORT);
3322 rdexp(t->words+1, ronly, RONLY);
3333 for (; *wp != NULL; wp++) {
3334 if (isassign(*wp)) {
3337 for (cp = *wp; *cp != '='; cp++)
3355 err(": bad identifier");
3360 register struct op *t;
3362 register struct var *vp;
3366 if ((cp = t->words[1]) == NULL) {
3367 for (vp = vlist; vp; vp = vp->next)
3368 varput(vp->name, 1);
3372 /* bad: t->words++; */
3373 for(n = 0; (t->words[n]=t->words[n+1]) != NULL; n++)
3376 flag['x'] = flag['v'] = 0;
3386 if (*cp>='a' && *cp<='z')
3393 t->words[0] = dolv[0];
3394 for (n=1; t->words[n]; n++)
3395 setarea((char *)t->words[n], 0);
3398 setval(lookup("#"), putn(dolc));
3399 setarea((char *)(dolv-1), 0);
3409 if (isalnum(*s) || *s == '_') {
3410 write(out, s, strlen(s));
3411 write(out, "\n", 1);
3417 * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
3418 * This file contains code for the times builtin.
3420 static int dotimes ()
3423 long int clk_tck = sysconf(_SC_CLK_TCK);
3426 printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
3427 (int) (buf.tms_utime / clk_tck / 60),
3428 ((double) buf.tms_utime) / clk_tck,
3429 (int) (buf.tms_stime / clk_tck / 60),
3430 ((double) buf.tms_stime) / clk_tck,
3431 (int) (buf.tms_cutime / clk_tck / 60),
3432 ((double) buf.tms_cutime) / clk_tck,
3433 (int) (buf.tms_cstime / clk_tck / 60),
3434 ((double) buf.tms_cstime) / clk_tck);
3439 static int (*inbuilt(char *s))()
3441 const struct builtincmd *bp;
3443 for (bp = builtincmds; bp->name != NULL; bp++)
3444 if (strcmp(bp->name, s) == 0)
3445 return(bp->builtinfunc);
3447 return((int(*)())NULL);
3450 /* -------- eval.c -------- */
3455 * blank interpretation
3460 static char ** eval( char **ap, int f)
3468 /* Avoid longjmp clobbering */
3475 if (newenv(setjmp(errpt = ev)) == 0) {
3476 while (*ap && isassign(*ap))
3477 expand(*ap++, &wb, f & ~DOGLOB);
3479 for (wf = ap; *wf; wf++) {
3481 expand(*wf, &wb, f & ~DOGLOB);
3484 for (wb = addword((char *)0, wb); *ap; ap++) {
3485 if (!flag['k'] || !isassign(*ap))
3486 expand(*ap, &wb, f & ~DOKEY);
3488 wb = addword((char *)0, wb);
3493 return(gflg? (char **)NULL: wp);
3497 * Make the exported environment from the exported
3498 * names in the dictionary. Keyword assignments
3499 * will already have been done.
3505 register struct wdblock *wb;
3506 register struct var *vp;
3509 for (vp = vlist; vp; vp = vp->next)
3510 if (vp->status & EXPORT)
3511 wb = addword(vp->name, wb);
3512 wb = addword((char *)0, wb);
3513 return(getwords(wb));
3524 if (expand(cp, &wb, f)) {
3525 if (wb == NULL || wb->w_nword == 0 || (cp = wb->w_words[0]) == NULL)
3534 expand( char *cp, register struct wdblock **wbp, int f)
3539 /* Avoid longjmp clobbering */
3545 if (!anys("$`'\"", cp) &&
3546 !anys(ifs->value, cp) &&
3547 ((f&DOGLOB)==0 || !anys("[*?", cp))) {
3548 cp = strsave(cp, areanum);
3551 *wbp = addword(cp, *wbp);
3554 if (newenv(setjmp(errpt = ev)) == 0) {
3555 PUSHIO(aword, cp, strchar);
3557 while ((cp = blank(f)) && gflg == 0) {
3559 cp = strsave(cp, areanum);
3560 if ((f&DOGLOB) == 0) {
3563 *wbp = addword(cp, *wbp);
3565 *wbp = glob(cp, *wbp);
3574 * Blank interpretation and quoting
3582 int scanequals, foundequals;
3585 scanequals = f & DOKEY;
3589 switch (c = subgetc('"', foundequals)) {
3597 if (f & DOBLANK && any(c, ifs->value))
3606 for (c1 = c; (c = subgetc(c1, 1)) != c1;) {
3609 if (c == '\'' || !any(c, "$`\""))
3616 if (!isalpha(c) && c != '_')
3619 c = subgetc('"', foundequals);
3621 f & (DOBLANK && any(c, ifs->value)) ||
3622 (!INSUB() && any(c, "\"'"))) {
3634 else if (!isalnum(c) && c != '_')
3644 * Get characters, substituting for ` and $
3655 if (!INSUB() && ec != '\'') {
3657 if (grave(quoted) == 0)
3659 e.iop->task = XGRAVE;
3662 if (c == '$' && (c = dollar(quoted)) == 0) {
3663 e.iop->task = XDOLL;
3671 * Prepare to generate the string returned by ${} substitution.
3680 register char *s, c, *cp=NULL;
3687 if (isalpha(c) || c == '_') {
3688 while ((c = readc())!=0 && (isalnum(c) || c == '_'))
3689 if (e.linep < elinep)
3696 otask = e.iop->task;
3697 e.iop->task = XOTHER;
3698 while ((c = subgetc('"', 0))!=0 && c!='}' && c!='\n')
3699 if (e.linep < elinep)
3702 e.iop->task = otask;
3709 if (e.linep >= elinep) {
3710 err("string in ${} too long");
3716 for (cp = s+1; *cp; cp++)
3717 if (any(*cp, "=-+?")) {
3722 if (s[1] == 0 && (*s == '*' || *s == '@')) {
3724 /* currently this does not distinguish $* and $@ */
3725 /* should check dollar */
3727 PUSHIO(awordlist, dolv+1, dolchar);
3729 } else { /* trap the nasty ${=} */
3735 if ((dolp = vp->value) == null) {
3739 err("cannot use ${...=...} with $n");
3748 dolp = strsave(cp, areanum);
3753 prs("missing value for ");
3760 } else if (c == '+')
3761 dolp = strsave(cp, areanum);
3762 if (flag['u'] && dolp == null) {
3763 prs("unset variable: ");
3768 PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
3773 * Run the command in `...` and read its output.
3784 /* Avoid longjmp clobbering */
3787 for (cp = e.iop->argp->aword; *cp != '`'; cp++)
3789 err("no closing `");
3792 if (openpipe(pf) < 0)
3794 if ((i = vfork()) == -1) {
3800 e.iop->argp->aword = ++cp;
3802 PUSHIO(afile, remap(pf[0]), quoted? qgravechar: gravechar);
3806 /* allow trapped signals */
3807 for (i=0; i<=_NSIG; i++)
3808 if (ourtrap[i] && signal(i, SIG_IGN) != SIG_IGN)
3815 cp = strsave(e.iop->argp->aword, 0);
3818 freearea(areanum); /* free old space */
3820 e.iop = (e.iobase = iostack) - 1;
3823 PUSHIO(aword, cp, nlchar);
3834 if ((s = as) != NULL)
3840 /* -------- glob.c -------- */
3846 #define scopy(x) strsave((x), areanum)
3848 #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
3850 static struct wdblock *cl, *nl;
3851 static char spcl[] = "[?*";
3853 static struct wdblock *
3864 for (pp = cp; *pp; pp++)
3867 else if (!any(*pp & ~QUOTE, spcl))
3870 for (cl = addword(scopy(cp), (struct wdblock *)0); anyspcl(cl); cl = nl) {
3871 nl = newword(cl->w_nword*2);
3872 for(i=0; i<cl->w_nword; i++) { /* for each argument */
3873 for (pp = cl->w_words[i]; *pp; pp++)
3874 if (any(*pp, spcl)) {
3875 globname(cl->w_words[i], pp);
3879 nl = addword(scopy(cl->w_words[i]), nl);
3881 for(i=0; i<cl->w_nword; i++)
3882 DELETE(cl->w_words[i]);
3885 for(i=0; i<cl->w_nword; i++)
3886 unquote(cl->w_words[i]);
3887 glob0((char *)cl->w_words, cl->w_nword, sizeof(char *), xstrcmp);
3889 for (i=0; i<cl->w_nword; i++)
3890 wb = addword(cl->w_words[i], wb);
3895 wb = addword(unquote(cp), wb);
3904 register char *np, *cp;
3905 char *name, *gp, *dp;
3909 char dname[NAME_MAX+1];
3912 for (np = we; np != pp; pp--)
3915 for (dp = cp = space((int)(pp-np)+3); np < pp;)
3919 for (gp = cp = space(strlen(pp)+1); *np && *np != '/';)
3928 dname[NAME_MAX] = '\0';
3929 while ((de=readdir(dirp))!=NULL) {
3930 /* XXX Hmmm... What this could be? (abial) */
3932 if (ent[j].d_ino == 0)
3935 strncpy(dname, de->d_name, NAME_MAX);
3936 if (dname[0] == '.')
3939 for(k=0; k<NAME_MAX; k++)
3940 if (any(dname[k], spcl))
3942 if (gmatch(dname, gp)) {
3943 name = generate(we, pp, dname, np);
3944 if (*np && !anys(np, spcl)) {
3945 if (stat(name,&dbuf)) {
3950 nl = addword(name, nl);
3959 * generate a pathname as below.
3960 * start..end1 / middle end
3961 * the slashes come for free
3964 generate(start1, end1, middle, end)
3966 register char *end1;
3970 register char *op, *xp;
3972 p = op = space((int)(end1-start1)+strlen(middle)+strlen(end)+2);
3973 for (xp = start1; xp != end1;)
3975 for (xp = middle; (*op++ = *xp++) != '\0';)
3978 for (xp = end; (*op++ = *xp++) != '\0';)
3985 register struct wdblock *wb;
3991 for (i=0; i<wb->w_nword; i++)
3992 if (anys(spcl, *wd++))
4001 return(strcmp(*(char **)p1, *(char **)p2));
4004 /* -------- word.c -------- */
4006 static struct wdblock *
4010 register struct wdblock *wb;
4012 wb = (struct wdblock *) space(sizeof(*wb) + nw*sizeof(char *));
4018 static struct wdblock *
4021 register struct wdblock *wb;
4023 register struct wdblock *wb2;
4027 wb = newword(NSTART);
4028 if ((nw = wb->w_nword) >= wb->w_bsize) {
4029 wb2 = newword(nw * 2);
4030 memcpy((char *)wb2->w_words, (char *)wb->w_words, nw*sizeof(char *));
4035 wb->w_words[wb->w_nword++] = wd;
4041 register struct wdblock *wb;
4047 return((char **)NULL);
4048 if (wb->w_nword == 0) {
4050 return((char **)NULL);
4052 wd = (char **) space(nb = sizeof(*wd) * wb->w_nword);
4053 memcpy((char *)wd, (char *)wb->w_words, nb);
4054 DELETE(wb); /* perhaps should done by caller */
4058 int (*func)(char *, char *);
4062 glob0(a0, a1, a2, a3)
4066 int (*a3) (char *, char *);
4070 glob1(a0, a0 + a1 * a2);
4077 register char *i, *j;
4087 if ((n=(int)(lim-base)) <= v2)
4089 n = v2 * (n / (2*v2));
4090 hptr = lptr = base+n;
4095 if ((c = (*func)(i, lptr)) == 0) {
4096 glob2(i, lptr -= v2);
4107 if ((c = (*func)(hptr, j)) == 0) {
4108 glob2(hptr += v2, j);
4113 glob3(i, hptr += v2, j);
4128 if (lptr-base >= lim-hptr) {
4129 glob1(hptr+v2, lim);
4139 glob3(j, lptr -= v2, i);
4148 register char *index1, *index2, c;
4156 *index1++ = *index2;
4165 register char *index1, *index2, *index3;
4175 *index1++ = *index3;
4176 *index3++ = *index2;
4181 /* -------- io.c -------- */
4187 static int my_getc( int ec)
4191 if(e.linep > elinep) {
4192 while((c=readc()) != '\n' && c)
4194 err("input line too long");
4199 if (ec != '\'' && e.iop->task != XGRAVE) {
4202 if (c == '\n' && ec != '\"')
4203 return(my_getc(ec));
4214 if (e.iop >= e.iobase)
4222 return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0);
4230 for (; e.iop >= e.iobase; e.iop--)
4231 if ((c = e.iop->peekc) != '\0') {
4236 if (e.iop->prev != 0) {
4237 if ((c = (*e.iop->iofn)(e.iop->argp, e.iop)) != '\0') {
4242 if (e.iop == iostack)
4244 return(e.iop->prev = c);
4246 else if (e.iop->task == XIO && e.iop->prev != '\n') {
4248 if (e.iop == iostack)
4253 if (e.iop->task == XIO) {
4255 return e.iop->prev = 0;
4256 if (interactive && e.iop == iostack+1) {
4257 #ifdef BB_FEATURE_COMMAND_EDITING
4258 current_prompt=prompt->value;
4265 if (e.iop >= iostack)
4277 write(2, &c, sizeof c);
4285 if (++e.iop >= &iostack[NPUSH]) {
4287 err("Shell input nested too deeply");
4293 if (argp->afid != AFID_NOBUF)
4296 e.iop->argp = ioargstack + (e.iop - iostack);
4297 *e.iop->argp = *argp;
4298 e.iop->argp->afbuf = e.iop == &iostack[0] ? &mainbuf : &sharedbuf;
4299 if (isatty(e.iop->argp->afile) == 0 &&
4300 (e.iop == &iostack[0] ||
4301 lseek(e.iop->argp->afile, 0L, 1) != -1)) {
4302 if (++bufid == AFID_NOBUF)
4304 e.iop->argp->afid = bufid;
4308 e.iop->prev = ~'\n';
4312 if (fn == filechar || fn == linechar)
4314 else if (fn == gravechar || fn == qgravechar)
4315 e.iop->task = XGRAVE;
4317 e.iop->task = XOTHER;
4324 register struct io *xp;
4332 * Input generating functions
4336 * Produce the characters of a string, then a newline, then EOF.
4340 register struct ioarg *ap;
4344 if (ap->aword == NULL)
4346 if ((c = *ap->aword++) == 0) {
4354 * Given a list of words, produce the characters
4355 * in them, with a space after each word.
4359 register struct ioarg *ap;
4364 if ((wl = ap->awordlist) == NULL)
4367 if ((c = *(*wl)++) != 0)
4372 ap->awordlist = NULL;
4377 * Return the characters of a list of words,
4378 * producing a space between them.
4382 register struct ioarg *ap;
4386 if ((wp = *ap->awordlist++) != NULL) {
4387 PUSHIO(aword, wp, *ap->awordlist == NULL? strchar: xxchar);
4395 register struct ioarg *ap;
4399 if (ap->aword == NULL)
4401 if ((c = *ap->aword++) == '\0') {
4409 * Produce the characters from a single word (string).
4413 register struct ioarg *ap;
4417 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4423 * Produce quoted characters from a single word (string).
4427 register struct ioarg *ap;
4431 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4437 * Return the characters from a file.
4441 register struct ioarg *ap;
4445 struct iobuf *bp = ap->afbuf;
4447 if (ap->afid != AFID_NOBUF) {
4448 if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
4450 lseek(ap->afile, ap->afpos, 0);
4451 i = safe_read(ap->afile, bp->buf, sizeof(bp->buf));
4457 bp->ebufp = (bp->bufp = bp->buf) + i;
4460 return *bp->bufp++ & 0177;
4463 #ifdef BB_FEATURE_COMMAND_EDITING
4465 static char mycommand[BUFSIZ];
4466 static int position = 0, size = 0;
4468 while (size == 0 || position >= size) {
4469 cmdedit_read_input(current_prompt, mycommand);
4470 size = strlen(mycommand);
4473 c = mycommand[position];
4479 i = safe_read(ap->afile, &c, sizeof(c));
4480 return(i == sizeof(c)? c&0177: (closef(ap->afile), 0));
4485 * Return the characters from a here temp file.
4489 register struct ioarg *ap;
4494 if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
4503 * Return the characters produced by a process (`...`).
4504 * Quote them if required, and remove any trailing newline characters.
4513 if ((c = qgravechar(ap, iop)&~QUOTE) == '\n')
4520 register struct ioarg *ap;
4532 } else if ((c = filechar(ap)) == '\n') {
4534 while ((c = filechar(ap)) == '\n')
4542 return(c!=0? c|QUOTE: 0);
4546 * Return a single command (usually the first line) from a file.
4550 register struct ioarg *ap;
4554 if ((c = filechar(ap)) == '\n') {
4557 ap->afile = -1; /* illegal value */
4568 write(2, s, strlen(s));
4591 for (u=NUFILE; u<NOFILE;)
4596 * remap fd into Shell's fd space
4606 for (i=0; i<NOFILE; i++)
4611 } while (fd >= 0 && fd < e.iofd);
4612 for (i=0; i<NOFILE; i++)
4616 err("too many files open in shell");
4627 if ((i = pipe(pv)) < 0)
4628 err("can't create pipe - try again");
4642 /* -------- here.c -------- */
4653 register struct here *h, *lh;
4655 h = (struct here *) space(sizeof(struct here));
4658 h->h_tag = evalstr(s, DOSUB);
4667 for (lh = inhere; lh!=NULL; lh = lh->h_next)
4668 if (lh->h_next == 0) {
4672 iop->io_flag |= IOHERE|IOXHERE;
4673 for (s = h->h_tag; *s; s++)
4675 iop->io_flag &= ~ IOXHERE;
4678 h->h_dosub = iop->io_flag & IOXHERE;
4684 register struct here *h, *hp;
4686 /* Scan here files first leaving inhere list in place */
4687 for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
4688 readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub? 0: '\'');
4690 /* Make inhere list active - keep list intact for scraphere */
4692 hp->h_next = acthere;
4699 readhere(name, s, ec)
4705 char tname[30] = ".msh_XXXXXX";
4708 char myline [LINELIM+1];
4711 tf = mkstemp(tname);
4714 *name = strsave(tname, areanum);
4715 if (newenv(setjmp(errpt = ev)) != 0)
4718 pushio(e.iop->argp, e.iop->iofn);
4721 if (interactive && e.iop <= iostack) {
4722 #ifdef BB_FEATURE_COMMAND_EDITING
4723 current_prompt=cprompt->value;
4725 prs(cprompt->value);
4729 while ((c = my_getc(ec)) != '\n' && c) {
4732 if (thenext >= &myline[LINELIM]) {
4739 if (strcmp(s, myline) == 0 || c == 0)
4742 write (tf, myline, (int)(thenext-myline));
4745 prs("here document `"); prs(s); err("' unclosed");
4753 * open here temp file.
4754 * if unquoted here, expand here temp file into second temp file.
4757 herein(hname, xdoll)
4765 /* Avoid longjmp clobbering */
4770 hf = open(hname, 0);
4775 char tname[30] = ".msh_XXXXXX";
4778 tf = mkstemp(tname);
4781 if (newenv(setjmp(errpt = ev)) == 0) {
4782 PUSHIO(afile, hf, herechar);
4784 while ((c = subgetc(0, 0)) != 0) {
4786 write(tf, &c, sizeof c);
4792 tf = open(tname, 0);
4802 register struct here *h;
4804 for (h = inhere; h != NULL; h = h->h_next) {
4805 if (h->h_iop && h->h_iop->io_name)
4806 unlink(h->h_iop->io_name);
4811 /* unlink here temp files before a freearea(area) */
4816 register struct here *h, *hl;
4819 for (h = acthere; h != NULL; h = h->h_next)
4820 if (getarea((char *) h) >= area) {
4821 if (h->h_iop->io_name != NULL)
4822 unlink(h->h_iop->io_name);
4824 acthere = h->h_next;
4826 hl->h_next = h->h_next;
4834 * Copyright (c) 1987,1997, Prentice Hall
4835 * All rights reserved.
4837 * Redistribution and use of the MINIX operating system in source and
4838 * binary forms, with or without modification, are permitted provided
4839 * that the following conditions are met:
4841 * Redistributions of source code must retain the above copyright
4842 * notice, this list of conditions and the following disclaimer.
4844 * Redistributions in binary form must reproduce the above
4845 * copyright notice, this list of conditions and the following
4846 * disclaimer in the documentation and/or other materials provided
4847 * with the distribution.
4849 * Neither the name of Prentice Hall nor the names of the software
4850 * authors or contributors may be used to endorse or promote
4851 * products derived from this software without specific prior
4852 * written permission.
4854 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
4855 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
4856 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4857 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4858 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
4859 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4860 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4861 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
4862 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4863 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
4864 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
4865 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.