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 shell_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 char * current_prompt;
686 unsigned int shell_context;
690 /* -------- sh.c -------- */
696 extern int shell_main(int argc, char **argv)
705 if ((ap = environ) != NULL) {
707 assign(*ap++, !COPYV);
708 for (ap = environ; *ap;)
709 export(lookup(*ap++));
714 shell = lookup("SHELL");
715 if (shell->value == null)
716 setval(shell, shellname);
719 homedir = lookup("HOME");
720 if (homedir->value == null)
721 setval(homedir, "/");
724 setval(lookup("$"), itoa(getpid(), 5));
726 path = lookup("PATH");
727 if (path->value == null)
728 setval(path, search);
732 if (ifs->value == null)
733 setval(ifs, " \t\n");
735 prompt = lookup("PS1");
736 #ifdef BB_FEATURE_SH_FANCY_PROMPT
737 if (prompt->value == null)
739 setval(prompt, "$ ");
740 if (geteuid() == 0) {
741 setval(prompt, "# ");
742 prompt->status &= ~EXPORT;
744 cprompt = lookup("PS2");
745 #ifdef BB_FEATURE_SH_FANCY_PROMPT
746 if (cprompt->value == null)
748 setval(cprompt, "> ");
754 if(argv[0][0] == '-' && argv[0][1] != '\0') {
755 for (s = argv[0]+1; *s; s++)
758 prompt->status &= ~EXPORT;
759 cprompt->status &= ~EXPORT;
764 PUSHIO(aword, *++argv, iof = nlchar);
776 prompt->status &= ~EXPORT;
784 if (*s>='a' && *s<='z')
791 if (iof == filechar && --argc > 0) {
794 prompt->status &= ~EXPORT;
795 cprompt->status &= ~EXPORT;
796 if (newfile(name = *++argv))
801 if (e.iop < iostack) {
802 PUSHIO(afile, 0, iof);
803 if (isatty(0) && isatty(1) && !cflag) {
805 printf( "\n\n" BB_BANNER " Built-in shell (msh)\n");
806 printf( "Enter 'help' for a list of built-in commands.\n\n");
809 signal(SIGQUIT, qflag);
810 if (name && name[0] == '-') {
812 if ((f = open(".profile", 0)) >= 0)
814 if ((f = open("/etc/profile", 0)) >= 0)
818 signal(SIGTERM, sig);
819 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
820 signal(SIGINT, onintr);
825 for (ap = ++argv; --argc > 0;) {
826 if (assign(*ap = *argv++, !COPYV)) {
827 dolc--; /* keyword */
833 setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
836 if (interactive && e.iop <= iostack) {
837 #ifdef BB_FEATURE_COMMAND_EDITING
838 current_prompt=prompt->value;
855 for (c='a'; c<='z'; c++)
859 setval(lookup("-"), m);
868 if (strcmp(s, "-") != 0) {
872 err(": cannot open");
902 setjmp(failpt = m1); /* Bruce Evans' fix */
903 if (setjmp(failpt = m1) || yyparse() || intr) {
907 if (!interactive && intr)
918 execute(outtree, NOPIPE, NOPIPE, 0);
919 if (!interactive && intr) {
923 if ((i = trapset) != 0) {
973 e.iop = e.iobase = iostack;
980 register struct env *ep;
986 ep = (struct env *) space(sizeof(*ep));
1001 register struct env *ep;
1004 if ((ep = e.oenv) != NULL) {
1007 /* should close `'d files */
1009 while (--fd >= e.iofd)
1015 * Is any character from s1 in s2?
1019 register char *s1, *s2;
1028 * Is character c in s?
1045 return(itoa(n, -1));
1050 register unsigned u;
1058 if (n < 0 && (int) u < 0) {
1067 } while (--n > 0 || u);
1077 PUSHIO(afile, f, filechar);
1082 int s; /* ANSI C requires a parameter */
1084 signal(SIGINT, onintr);
1104 if ((cp = getcell(n)) == 0)
1105 err("out of string space");
1114 register char *cp, *xp;
1116 if ((cp = space(strlen(s)+1)) != NULL) {
1117 setarea((char *)cp, a);
1118 for (xp = cp; (*xp++ = *s++) != '\0';)
1136 static void runtrap(i)
1141 if ((trapstr = trap[i]) == NULL)
1145 RUN(aword, trapstr, nlchar);
1148 /* -------- var.c -------- */
1151 * Find the given name in the dictionary
1152 * and return its value. If the name was
1153 * not previously there, enter it now and
1154 * return a null value.
1160 register struct var *vp;
1163 static struct var dummy;
1167 for (c = 0; isdigit(*n) && c < 1000; n++)
1169 dummy.status = RONLY;
1170 dummy.value = c <= dolc? dolv[c]: null;
1173 for (vp = vlist; vp; vp = vp->next)
1174 if (eqname(vp->name, n))
1177 vp = (struct var *)space(sizeof(*vp));
1178 if (vp == 0 || (vp->name = space((int)(cp-n)+2)) == 0) {
1179 dummy.name = dummy.value = "";
1182 for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++)
1187 setarea((char *)vp, 0);
1188 setarea((char *)vp->name, 0);
1191 vp->status = GETCELL;
1197 * give variable at `vp' the value `val'.
1204 nameval(vp, val, (char *)NULL);
1208 * if name is not NULL, it must be
1209 * a prefix of the space `val',
1211 * this is all so that exporting
1212 * values is reasonably painless.
1215 nameval(vp, val, name)
1216 register struct var *vp;
1219 register char *cp, *xp;
1223 if (vp->status & RONLY) {
1224 for (xp = vp->name; *xp && *xp != '=';)
1225 putc(*xp++, stderr);
1226 err(" is read-only");
1231 xp = space(strlen(vp->name)+strlen(val)+2);
1234 /* make string: name=value */
1235 setarea((char *)xp, 0);
1237 for (cp = vp->name; (*xp = *cp++) && *xp!='='; xp++)
1242 for (cp = val; (*xp++ = *cp++) != '\0';)
1247 if (vp->status & GETCELL)
1248 freecell(vp->name); /* form new string `name=value' */
1258 vp->status |= EXPORT;
1265 if (isalpha(vp->name[0])) /* not an internal symbol ($# etc) */
1266 vp->status |= RONLY;
1273 if (!isalpha((int)*s))
1275 for (; *s != '='; s++)
1276 if (*s == 0 || !isalnum(*s))
1291 for (cp = s; *cp != '='; cp++)
1292 if (*cp == 0 || !isalnum(*cp))
1295 nameval(vp, ++cp, cf == COPYV? (char *)NULL: s);
1297 vp->status &= ~GETCELL;
1305 if (!isalpha(*cp++))
1308 if (!isalnum(*cp++))
1315 register int f, out;
1317 register struct var *vp;
1319 for (vp = vlist; vp; vp = vp->next)
1320 if (vp->status & f && isalpha(*vp->name)) {
1321 if (vp->status & EXPORT)
1322 write(out, "export ", 7);
1323 if (vp->status & RONLY)
1324 write(out, "readonly ", 9);
1325 write(out, vp->name, (int)(findeq(vp->name) - vp->name));
1326 write(out, "\n", 1);
1332 register char *n1, *n2;
1334 for (; *n1 != '=' && *n1 != 0; n1++)
1337 return(*n2 == 0 || *n2 == '=');
1344 while (*cp != '\0' && *cp != '=')
1349 /* -------- gmatch.c -------- */
1351 * int gmatch(string, pattern)
1352 * char *string, *pattern;
1354 * Match a pattern as in sh(1).
1359 #define QMASK (CMASK&~QUOTE)
1360 #define NOT '!' /* might use ^ */
1364 register char *s, *p;
1366 register int sc, pc;
1368 if (s == NULL || p == NULL)
1370 while ((pc = *p++ & CMASK) != '\0') {
1374 if ((p = cclass(p, sc)) == NULL)
1386 if (*p == '\0' || gmatch(s, p))
1388 } while (*s++ != '\0');
1392 if (sc != (pc&~QUOTE))
1404 register int c, d, not, found;
1406 if ((not = *p == NOT) != 0)
1411 return((char *)NULL);
1413 if (p[1] == '-' && p[2] != ']') {
1418 if (c == sub || (c <= sub && sub <= d))
1420 } while (*++p != ']');
1421 return(found? p+1: (char *)NULL);
1425 /* -------- area.c -------- */
1428 * All memory between (char *)areabot and (char *)(areatop+1) is
1429 * exclusively administered by the area management routines.
1430 * It is assumed that sbrk() and brk() manipulate the high end.
1433 #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;})
1438 brkaddr = malloc(65000);
1439 brktop = brkaddr + 65000;
1441 while ((int)sbrk(0) & ALIGN)
1443 areabot = (struct region *)sbrk(REGSIZE);
1445 areabot->next = areabot;
1446 areabot->area = BUSY;
1455 register int nregio;
1456 register struct region *p, *q;
1462 } /* silly and defeats the algorithm */
1464 * round upwards and add administration area
1466 nregio = (nbytes+(REGSIZE-1))/REGSIZE + 1;
1467 for (p = areanxt;;) {
1468 if (p->area > areanum) {
1472 while ((q = p->next)->area > areanum && q != areanxt)
1475 * exit loop if cell big enough
1477 if (q >= p + nregio)
1484 i = nregio >= GROWBY ? nregio : GROWBY;
1485 p = (struct region *)sbrk(i * REGSIZE);
1486 if (p == (struct region *)-1)
1487 return((char *)NULL);
1491 abort(); /* allocated areas are contiguous */
1501 * we found a FREE area big enough, pointed to by 'p', and up to 'q'
1503 areanxt = p + nregio;
1506 * split into requested area and rest
1508 if (areanxt+1 > q) {
1510 abort(); /* insufficient space left for admin */
1513 areanxt->area = FREE;
1517 return((char *)(p+1));
1524 register struct region *p;
1526 if ((p = (struct region *)cp) != NULL) {
1538 register struct region *p, *top;
1541 for (p = areabot; p != top; p = p->next)
1551 register struct region *p;
1553 if ((p = (struct region *)cp) != NULL)
1561 return ((struct region*)cp-1)->area;
1567 register struct region *p, *q, *top;
1570 for (p = areabot; p != top; p = p->next) {
1571 if (p->area > areanum) {
1572 while ((q = p->next)->area > areanum)
1578 if (areatop >= q + SHRINKBY && q->area > areanum) {
1587 /* -------- csyn.c -------- */
1589 * shell: syntax (C version)
1608 register struct op *t, *p;
1613 while ((c = yylex(0)) == '|') {
1614 if ((p = command(CONTIN)) == NULL)
1616 if (t->type != TPAREN && t->type != TCOM) {
1617 /* shell statement */
1618 t = block(TPAREN, t, NOBLOCK, NOWORDS);
1620 t = block(TPIPE, t, p, NOWORDS);
1630 register struct op *t, *p;
1635 while ((c = yylex(0)) == LOGAND || c == LOGOR) {
1636 if ((p = pipeline(CONTIN)) == NULL)
1638 t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS);
1648 register struct op *t, *p;
1653 if((peeksym = yylex(0)) == '&')
1654 t = block(TASYNC, t, NOBLOCK, NOWORDS);
1655 while ((c = yylex(0)) == ';' || c == '&' || (multiline && c == '\n')) {
1656 if ((p = andor()) == NULL)
1658 if((peeksym = yylex(0)) == '&')
1659 p = block(TASYNC, p, NOBLOCK, NOWORDS);
1672 register struct ioword *iop;
1676 if ((c = yylex(cf)) != '<' && c != '>') {
1682 iop = io(iounit, i, yylval.cp);
1685 markhere(yylval.cp, iop);
1693 if ((peeksym = yylex(cf)) != c)
1701 register struct op *t;
1705 switch (peeksym = yylex(0)) {
1730 register struct op *t;
1736 return(block(type, t, NOBLOCK, NOWORDS));
1743 register struct op *t;
1744 struct wdblock *iosave;
1753 switch (c = yylex(cf)) {
1756 if ((t = simple()) == NULL) {
1758 return((struct op *)NULL);
1765 t = nested(TPAREN, ')');
1769 t = nested(TBRACE, '}');
1779 t->words = wordlist();
1780 if ((c = yylex(0)) != '\n' && c != ';')
1782 t->left = dogroup(0);
1790 t->type = c == WHILE? TWHILE: TUNTIL;
1792 t->right = dogroup(1);
1804 musthave(IN, CONTIN);
1806 t->left = caselist();
1816 t->right = thenpart();
1833 register struct op *mylist;
1836 if (c == DONE && onlydone)
1837 return((struct op *)NULL);
1849 register struct op *t;
1851 if ((c = yylex(0)) != THEN) {
1853 return((struct op *)NULL);
1858 if (t->left == NULL)
1860 t->right = elsepart();
1868 register struct op *t;
1870 switch (c = yylex(0)) {
1872 if ((t = c_list()) == NULL)
1880 t->right = thenpart();
1885 return((struct op *)NULL);
1892 register struct op *t;
1895 while ((peeksym = yylex(CONTIN)) != ESAC)
1896 t = list(t, casepart());
1903 register struct op *t;
1907 t->words = pattern();
1910 if ((peeksym = yylex(CONTIN)) != ESAC)
1911 musthave(BREAK, CONTIN);
1925 } while ((c = yylex(0)) == '|');
1936 if ((c = yylex(0)) != IN) {
1938 return((char **)NULL);
1941 while ((c = yylex(0)) == WORD)
1949 * supporting functions
1953 register struct op *t1, *t2;
1959 return(block(TLIST, t1, t2, NOWORDS));
1963 block(type, t1, t2, wp)
1968 register struct op *t;
1982 register struct res *rp;
1984 for (rp = restab; rp->r_name; rp++)
1985 if (strcmp(rp->r_name, n) == 0)
1993 register struct op *t;
1995 t = (struct op *)tree(sizeof(*t));
2007 register struct op *t;
2010 iolist = addword((char *)NULL, iolist);
2011 t->ioact = copyio();
2014 if (t->type != TCOM) {
2015 if (t->type != TPAREN && t->ioact != NULL) {
2016 t = block(TPAREN, t, NOBLOCK, NOWORDS);
2017 t->ioact = t->left->ioact;
2018 t->left->ioact = NULL;
2032 wd = getwords(wdlist);
2041 wdlist = addword(cp, wdlist);
2044 static struct ioword **
2047 register struct ioword **iop;
2049 iop = (struct ioword **) getwords(iolist);
2054 static struct ioword *
2060 register struct ioword *iop;
2062 iop = (struct ioword *) tree(sizeof(*iop));
2066 iolist = addword((char *)iop, iolist);
2073 yyerror("syntax error");
2081 if (interactive && e.iop <= iostack) {
2083 while (eofc() == 0 && yylex(0) != '\n')
2097 if ((c = peeksym) > 0) {
2110 while ((c = my_getc(0)) == ' ' || c == '\t')
2114 if (any(c, "0123456789")) {
2115 unget(c1 = my_getc(0));
2116 if (c1 == '<' || c1 == '>') {
2126 while ((c = my_getc(0)) != 0 && c != '\n')
2136 if ((c = my_getc(0)) == '{') {
2137 if ((c = collect(c, '}')) != '\0')
2146 if ((c = collect(c, c)) != '\0')
2153 if ((c1 = dual(c)) != '\0') {
2171 if (multiline || cf & CONTIN) {
2172 if (interactive && e.iop <= iostack) {
2173 #ifdef BB_FEATURE_COMMAND_EDITING
2174 current_prompt=cprompt->value;
2176 prs(cprompt->value);
2193 while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n"))
2194 if (e.linep >= elinep)
2195 err("word too long");
2202 if (atstart && (c = rlookup(line))!=0) {
2206 yylval.cp = strsave(line, areanum);
2217 while ((c = my_getc(c1)) != c1) {
2222 prs("no closing "); yyerror(s);
2225 if (interactive && c == '\n' && e.iop <= iostack) {
2226 #ifdef BB_FEATURE_COMMAND_EDITING
2227 current_prompt=cprompt->value;
2229 prs(cprompt->value);
2243 register char *cp = s;
2248 if ((c = rlookup(s)) == 0)
2260 if (c == '>' || c == '<') {
2263 yylval.i = ec == '>'? IOWRITE|IOCAT: IOHERE;
2266 yylval.i = ec == '>'? IOWRITE: IOREAD;
2267 if (c != '&' || yylval.i == IOHERE)
2279 if ((t = getcell(size)) == NULL) {
2280 prs("command line too complicated\n");
2290 /* -------- exec.c -------- */
2298 execute(t, pin, pout, act)
2299 register struct op *t;
2303 register struct op *t1;
2304 volatile int i, rv, a;
2305 char *cp, **wp, **wp2;
2310 /* Avoid longjmp clobbering */
2319 wp = (wp2 = t->words) != NULL
2320 ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
2328 rv = forkexec(t, pin, pout, act, wp, &child);
2339 if ((rv = openpipe(pv)) < 0)
2341 pv[0] = remap(pv[0]);
2342 pv[1] = remap(pv[1]);
2343 (void) execute(t->left, pin, pv, 0);
2344 rv = execute(t->right, pv, pout, 0);
2349 (void) execute(t->left, pin, pout, 0);
2350 rv = execute(t->right, pin, pout, 0);
2355 int hinteractive = interactive;
2359 interactive = hinteractive;
2361 setval(lookup("!"), putn(i));
2372 signal(SIGINT, SIG_IGN);
2373 signal(SIGQUIT, SIG_IGN);
2375 signal(SIGTERM, SIG_DFL);
2379 open("/dev/null", 0);
2381 exit(execute(t->left, pin, pout, FEXEC));
2388 rv = execute(t->left, pin, pout, 0);
2389 if ((t1 = t->right)!=NULL && (rv == 0) == (t->type == TAND))
2390 rv = execute(t1, pin, pout, 0);
2400 while (*wp++ != NULL)
2403 vp = lookup(t->str);
2404 while (setjmp(bc.brkpt))
2408 for (t1 = t->left; i-- && *wp != NULL;) {
2410 rv = execute(t1, pin, pout, 0);
2412 brklist = brklist->nextlev;
2417 while (setjmp(bc.brkpt))
2422 while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE))
2423 rv = execute(t->right, pin, pout, 0);
2424 brklist = brklist->nextlev;
2429 if (t->right != NULL) {
2430 rv = !execute(t->left, pin, pout, 0) ?
2431 execute(t->right->left, pin, pout, 0):
2432 execute(t->right->right, pin, pout, 0);
2437 if ((cp = evalstr(t->str, DOSUB|DOTRIM)) == 0)
2439 if ((t1 = findcase(t->left, cp)) != NULL)
2440 rv = execute(t1, pin, pout, 0);
2445 if (iopp = t->ioact)
2447 if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
2452 if (rv >= 0 && (t1 = t->left))
2453 rv = execute(t1, pin, pout, 0);
2463 if (interactive && intr) {
2467 if ((i = trapset) != 0) {
2475 forkexec( register struct op *t, int *pin, int *pout, int act, char **wp, int *pforked)
2478 int (*shcom)() = NULL;
2481 struct ioword **iopp;
2491 struct brkcon * hbrklist;
2495 /* Avoid longjmp clobbering */
2508 rv = -1; /* system-detected error */
2509 if (t->type == TCOM) {
2510 while ((cp = *wp++) != NULL)
2514 /* strip all initial assignments */
2515 /* not correct wrt PATH=yyy command etc */
2517 echo (cp ? wp: owp);
2518 if (cp == NULL && t->ioact == NULL) {
2519 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2521 return(setstatus(0));
2523 else if (cp != NULL)
2524 shcom = inbuilt(cp);
2528 if (shcom == NULL && (f & FEXEC) == 0) {
2534 hinteractive = interactive;
2541 /* who wrote this crappy non vfork safe shit? */
2546 interactive = hinteractive;
2556 return(pout==NULL? setstatus(waitfor(i,0)): 0);
2560 signal(SIGINT, SIG_IGN);
2561 signal(SIGQUIT, SIG_IGN);
2571 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2575 if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) {
2576 err("piping to/from shell builtins not yet done");
2588 if ((iopp = t->ioact) != NULL) {
2589 if (shcom != NULL && shcom != doexec) {
2591 err(": cannot redirect shell command");
2595 if (iosetup(*iopp++, pin!=NULL, pout!=NULL))
2599 return(setstatus((*shcom)(t)));
2600 /* should use FIOCEXCL */
2601 for (i=FDBASE; i<NOFILE; i++)
2604 signal(SIGINT, SIG_DFL);
2605 signal(SIGQUIT, SIG_DFL);
2607 if (t->type == TPAREN)
2608 exit(execute(t->left, NOPIPE, NOPIPE, FEXEC));
2612 cp = rexecve(wp[0], wp, makenv());
2613 prs(wp[0]); prs(": "); warn(cp);
2622 * 0< 1> are ignored as required
2626 iosetup(iop, pipein, pipeout)
2627 register struct ioword *iop;
2628 int pipein, pipeout;
2630 register int u = -1;
2631 char *cp=NULL, *msg;
2633 if (iop->io_unit == IODEFAULT) /* take default */
2634 iop->io_unit = iop->io_flag&(IOREAD|IOHERE)? 0: 1;
2635 if (pipein && iop->io_unit == 0)
2637 if (pipeout && iop->io_unit == 1)
2639 msg = iop->io_flag&(IOREAD|IOHERE)? "open": "create";
2640 if ((iop->io_flag & IOHERE) == 0) {
2642 if ((cp = evalstr(cp, DOSUB|DOTRIM)) == NULL)
2645 if (iop->io_flag & IODUP) {
2646 if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
2648 err(": illegal >& argument");
2652 iop->io_flag = IOCLOSE;
2653 iop->io_flag &= ~(IOREAD|IOWRITE);
2655 switch (iop->io_flag) {
2661 case IOHERE|IOXHERE:
2662 u = herein(iop->io_name, iop->io_flag&IOXHERE);
2667 if ((u = open(cp, 1)) >= 0) {
2668 lseek(u, (long)0, 2);
2672 u = creat(cp, 0666);
2676 u = dup2(*cp-'0', iop->io_unit);
2680 close(iop->io_unit);
2689 if (u != iop->io_unit) {
2690 dup2(u, iop->io_unit);
2704 for (i=0; wp[i]; i++) {
2717 register struct op *t1;
2719 register char **wp, *cp;
2722 return((struct op **)NULL);
2723 if (t->type == TLIST) {
2724 if ((tp = find1case(t->left, w)) != NULL)
2726 t1 = t->right; /* TPAT */
2729 for (wp = t1->words; *wp;)
2730 if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp))
2732 return((struct op **)NULL);
2740 register struct op **tp;
2742 return((tp = find1case(t, w)) != NULL? *tp: (struct op *)NULL);
2746 * Enter a new loop level (marked for break/continue).
2752 bc->nextlev = brklist;
2757 * Wait for the last process created.
2758 * Print a message for each process found
2759 * that was killed by a signal.
2760 * Ignore interrupt signals while waiting
2761 * unless `canintr' is true.
2764 waitfor(lastpid, canintr)
2765 register int lastpid;
2768 register int pid, rv;
2770 int oheedint = heedint;
2777 if (errno != EINTR || canintr)
2780 if ((rv = WAITSIG(s)) != 0) {
2782 if (signame[rv] != NULL) {
2783 if (pid != lastpid) {
2790 if (pid != lastpid) {
2794 prs("Signal "); prn(rv); prs(" ");
2797 prs(" - core dumped");
2798 if (rv >= NSIGNAL || signame[rv])
2804 } while (pid != lastpid);
2811 if (exstat == 0) exstat = rv;
2823 setval(lookup("?"), putn(s));
2828 * PATH-searching interface to execve.
2829 * If getenv("PATH") were kept up-to-date,
2830 * execvp might be used.
2834 char *c, **v, **envp;
2837 register char *sp, *tp;
2838 int eacces = 0, asis = 0;
2840 #ifdef BB_FEATURE_SH_STANDALONE_SHELL
2842 #ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
2843 name = get_last_path_component(name);
2846 if (find_applet_by_name(name)) {
2847 /* We have to exec here since we vforked. Running
2848 * run_applet_by_name() won't work and bad things
2850 execve("/proc/self/exe", v, envp);
2851 execve("busybox", v, envp);
2855 sp = any('/', c)? "": path->value;
2857 while (asis || *sp != '\0') {
2860 for (; *sp != '\0'; tp++)
2861 if ((*tp = *sp++) == ':') {
2867 for (i = 0; (*tp++ = c[i++]) != '\0';)
2870 fprintf(stderr, "calling exec\n");
2871 execve(e.linep, v, envp);
2877 execve("/bin/sh", v, envp);
2882 return("program too big");
2885 return("argument list too long");
2892 return(errno==ENOENT ? "not found" : "cannot execute");
2896 * Run the command produced by generator `f'
2897 * applied to stream `arg'.
2905 struct wdblock *swdlist;
2906 struct wdblock *siolist;
2912 /* Avoid longjmp clobbering */
2922 if (newenv(setjmp(errpt = ev)) == 0) {
2928 if (setjmp(failpt = rt) == 0 && yyparse() == 0)
2929 rv = execute(outtree, NOPIPE, NOPIPE, 0);
2936 freearea(areanum--);
2940 /* -------- do.c -------- */
2943 * built-in commands: doX
2949 const struct builtincmd *x;
2951 printf("\nBuilt-in commands:\n");
2952 printf("-------------------\n");
2954 for (col=0, x = builtincmds; x->builtinfunc != NULL; x++) {
2957 col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name);
2963 #ifdef BB_FEATURE_SH_STANDALONE_SHELL
2966 const struct BB_applet *applet;
2967 extern const struct BB_applet applets[];
2968 extern const size_t NUM_APPLETS;
2970 for (i=0, applet = applets; i < NUM_APPLETS; applet++, i++) {
2974 col += printf("%s%s", ((col == 0) ? "\t" : " "),
2984 return EXIT_SUCCESS;
2997 register struct op *t;
2999 register char *cp, *er;
3001 if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL)
3002 er = ": no home directory";
3003 else if(chdir(cp) < 0)
3004 er = ": bad directory";
3007 prs(cp != NULL? cp: "cd");
3014 register struct op *t;
3018 n = t->words[1]? getn(t->words[1]): 1;
3020 err("nothing to shift");
3026 setval(lookup("#"), putn(dolc));
3031 * execute login and newgrp directly
3040 signal(SIGINT, SIG_DFL);
3041 signal(SIGQUIT, SIG_DFL);
3043 cp = rexecve(t->words[0], t->words, makenv());
3044 prs(t->words[0]); prs(": "); err(cp);
3050 register struct op *t;
3055 if ((cp = t->words[1]) == NULL) {
3058 for (n=3*4; (n-=3) >= 0;)
3059 putc('0'+((i>>n)&07), stderr);
3062 for (n=0; *cp>='0' && *cp<='9'; cp++)
3063 n = n*8 + (*cp-'0');
3071 register struct op *t;
3078 for(i = 0; (t->words[i]=t->words[i+1]) != NULL; i++)
3084 if (setjmp(failpt = ex) == 0)
3085 execute(t, NOPIPE, NOPIPE, FEXEC);
3096 register char *sp, *tp;
3099 if ((cp = t->words[1]) == NULL)
3101 sp = any('/', cp)? ":": path->value;
3104 while (*sp && (*tp = *sp++) != ':')
3108 for (i = 0; (*tp++ = cp[i++]) != '\0';)
3110 if ((i = open(e.linep, 0)) >= 0) {
3128 if ((cp = t->words[1]) != NULL) {
3134 setstatus(waitfor(i, 1));
3142 register char *cp, **wp;
3143 register int nb = 0;
3144 register int nl = 0;
3146 if (t->words[1] == NULL) {
3147 err("Usage: read name ...");
3150 for (wp = t->words+1; *wp; wp++) {
3151 for (cp = e.linep; !nl && cp < elinep-1; cp++)
3152 if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) ||
3153 (nl = (*cp == '\n')) ||
3154 (wp[1] && any(*cp, ifs->value)))
3159 setval(lookup(*wp), e.linep);
3166 register struct op *t;
3168 return(RUN(awordlist, t->words+1, wdchar));
3173 register struct op *t;
3176 register int resetsig;
3178 if (t->words[1] == NULL) {
3179 for (i=0; i<=_NSIG; i++)
3188 resetsig = isdigit(*t->words[1]);
3189 for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) {
3190 n = getsig(t->words[i]);
3194 if (*t->words[1] != '\0') {
3195 trap[n] = strsave(t->words[1], 0);
3204 setsig(n, n == SIGQUIT ? SIG_IGN
3219 if ((n = getn(s)) < 0 || n > _NSIG) {
3220 err("trap: bad signal number");
3227 setsig( register int n, void (*f)(int))
3231 if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) {
3250 for (n = 0; isdigit(*s); s++)
3251 n = (n*10) + (*s-'0');
3254 err(": bad number");
3263 return(brkcontin(t->words[1], 1));
3270 return(brkcontin(t->words[1], 0));
3278 register struct brkcon *bc;
3281 nl = cp == NULL? 1: getn(cp);
3285 if ((bc = brklist) == NULL)
3287 brklist = bc->nextlev;
3290 err("bad break/continue level");
3294 longjmp(bc->brkpt, 1);
3305 if ((cp = t->words[1]) != NULL)
3306 setstatus(getn(cp));
3316 rdexp(t->words+1, export, EXPORT);
3324 rdexp(t->words+1, ronly, RONLY);
3335 for (; *wp != NULL; wp++)
3349 err(": bad identifier");
3354 register struct op *t;
3356 register struct var *vp;
3360 if ((cp = t->words[1]) == NULL) {
3361 for (vp = vlist; vp; vp = vp->next)
3362 varput(vp->name, 1);
3366 /* bad: t->words++; */
3367 for(n = 0; (t->words[n]=t->words[n+1]) != NULL; n++)
3370 flag['x'] = flag['v'] = 0;
3380 if (*cp>='a' && *cp<='z')
3387 t->words[0] = dolv[0];
3388 for (n=1; t->words[n]; n++)
3389 setarea((char *)t->words[n], 0);
3392 setval(lookup("#"), putn(dolc));
3393 setarea((char *)(dolv-1), 0);
3404 write(out, s, strlen(s));
3405 write(out, "\n", 1);
3411 * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
3412 * This file contains code for the times builtin.
3414 static int dotimes ()
3417 long int clk_tck = sysconf(_SC_CLK_TCK);
3420 printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
3421 (int) (buf.tms_utime / clk_tck / 60),
3422 ((double) buf.tms_utime) / clk_tck,
3423 (int) (buf.tms_stime / clk_tck / 60),
3424 ((double) buf.tms_stime) / clk_tck,
3425 (int) (buf.tms_cutime / clk_tck / 60),
3426 ((double) buf.tms_cutime) / clk_tck,
3427 (int) (buf.tms_cstime / clk_tck / 60),
3428 ((double) buf.tms_cstime) / clk_tck);
3433 static int (*inbuilt(char *s))()
3435 const struct builtincmd *bp;
3437 for (bp = builtincmds; bp->name != NULL; bp++)
3438 if (strcmp(bp->name, s) == 0)
3439 return(bp->builtinfunc);
3441 return((int(*)())NULL);
3444 /* -------- eval.c -------- */
3449 * blank interpretation
3454 static char ** eval( char **ap, int f)
3462 /* Avoid longjmp clobbering */
3469 if (newenv(setjmp(errpt = ev)) == 0) {
3470 while (*ap && isassign(*ap))
3471 expand(*ap++, &wb, f & ~DOGLOB);
3473 for (wf = ap; *wf; wf++) {
3475 expand(*wf, &wb, f & ~DOGLOB);
3478 for (wb = addword((char *)0, wb); *ap; ap++) {
3479 if (!flag['k'] || !isassign(*ap))
3480 expand(*ap, &wb, f & ~DOKEY);
3482 wb = addword((char *)0, wb);
3487 return(gflg? (char **)NULL: wp);
3491 * Make the exported environment from the exported
3492 * names in the dictionary. Keyword assignments
3493 * will already have been done.
3499 register struct wdblock *wb;
3500 register struct var *vp;
3503 for (vp = vlist; vp; vp = vp->next)
3504 if (vp->status & EXPORT)
3505 wb = addword(vp->name, wb);
3506 wb = addword((char *)0, wb);
3507 return(getwords(wb));
3518 if (expand(cp, &wb, f)) {
3519 if (wb == NULL || wb->w_nword == 0 || (cp = wb->w_words[0]) == NULL)
3528 expand( char *cp, register struct wdblock **wbp, int f)
3533 /* Avoid longjmp clobbering */
3539 if (!anys("$`'\"", cp) &&
3540 !anys(ifs->value, cp) &&
3541 ((f&DOGLOB)==0 || !anys("[*?", cp))) {
3542 cp = strsave(cp, areanum);
3545 *wbp = addword(cp, *wbp);
3548 if (newenv(setjmp(errpt = ev)) == 0) {
3549 PUSHIO(aword, cp, strchar);
3551 while ((cp = blank(f)) && gflg == 0) {
3553 cp = strsave(cp, areanum);
3554 if ((f&DOGLOB) == 0) {
3557 *wbp = addword(cp, *wbp);
3559 *wbp = glob(cp, *wbp);
3568 * Blank interpretation and quoting
3576 int scanequals, foundequals;
3579 scanequals = f & DOKEY;
3583 switch (c = subgetc('"', foundequals)) {
3591 if (f & DOBLANK && any(c, ifs->value))
3600 for (c1 = c; (c = subgetc(c1, 1)) != c1;) {
3603 if (c == '\'' || !any(c, "$`\""))
3613 c = subgetc('"', foundequals);
3615 f & (DOBLANK && any(c, ifs->value)) ||
3616 (!INSUB() && any(c, "\"'"))) {
3628 else if (!isalnum(c))
3638 * Get characters, substituting for ` and $
3649 if (!INSUB() && ec != '\'') {
3651 if (grave(quoted) == 0)
3653 e.iop->task = XGRAVE;
3656 if (c == '$' && (c = dollar(quoted)) == 0) {
3657 e.iop->task = XDOLL;
3665 * Prepare to generate the string returned by ${} substitution.
3674 register char *s, c, *cp=NULL;
3682 while ((c = readc())!=0 && isalnum(c))
3683 if (e.linep < elinep)
3690 otask = e.iop->task;
3691 e.iop->task = XOTHER;
3692 while ((c = subgetc('"', 0))!=0 && c!='}' && c!='\n')
3693 if (e.linep < elinep)
3696 e.iop->task = otask;
3703 if (e.linep >= elinep) {
3704 err("string in ${} too long");
3710 for (cp = s+1; *cp; cp++)
3711 if (any(*cp, "=-+?")) {
3716 if (s[1] == 0 && (*s == '*' || *s == '@')) {
3718 /* currently this does not distinguish $* and $@ */
3719 /* should check dollar */
3721 PUSHIO(awordlist, dolv+1, dolchar);
3723 } else { /* trap the nasty ${=} */
3729 if ((dolp = vp->value) == null) {
3733 err("cannot use ${...=...} with $n");
3742 dolp = strsave(cp, areanum);
3747 prs("missing value for ");
3754 } else if (c == '+')
3755 dolp = strsave(cp, areanum);
3756 if (flag['u'] && dolp == null) {
3757 prs("unset variable: ");
3762 PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
3767 * Run the command in `...` and read its output.
3778 /* Avoid longjmp clobbering */
3781 for (cp = e.iop->argp->aword; *cp != '`'; cp++)
3783 err("no closing `");
3786 if (openpipe(pf) < 0)
3788 if ((i = vfork()) == -1) {
3794 e.iop->argp->aword = ++cp;
3796 PUSHIO(afile, remap(pf[0]), quoted? qgravechar: gravechar);
3800 /* allow trapped signals */
3801 for (i=0; i<=_NSIG; i++)
3802 if (ourtrap[i] && signal(i, SIG_IGN) != SIG_IGN)
3809 cp = strsave(e.iop->argp->aword, 0);
3812 freearea(areanum); /* free old space */
3814 e.iop = (e.iobase = iostack) - 1;
3817 PUSHIO(aword, cp, nlchar);
3828 if ((s = as) != NULL)
3834 /* -------- glob.c -------- */
3840 #define scopy(x) strsave((x), areanum)
3842 #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
3844 static struct wdblock *cl, *nl;
3845 static char spcl[] = "[?*";
3847 static struct wdblock *
3858 for (pp = cp; *pp; pp++)
3861 else if (!any(*pp & ~QUOTE, spcl))
3864 for (cl = addword(scopy(cp), (struct wdblock *)0); anyspcl(cl); cl = nl) {
3865 nl = newword(cl->w_nword*2);
3866 for(i=0; i<cl->w_nword; i++) { /* for each argument */
3867 for (pp = cl->w_words[i]; *pp; pp++)
3868 if (any(*pp, spcl)) {
3869 globname(cl->w_words[i], pp);
3873 nl = addword(scopy(cl->w_words[i]), nl);
3875 for(i=0; i<cl->w_nword; i++)
3876 DELETE(cl->w_words[i]);
3879 for(i=0; i<cl->w_nword; i++)
3880 unquote(cl->w_words[i]);
3881 glob0((char *)cl->w_words, cl->w_nword, sizeof(char *), xstrcmp);
3883 for (i=0; i<cl->w_nword; i++)
3884 wb = addword(cl->w_words[i], wb);
3889 wb = addword(unquote(cp), wb);
3898 register char *np, *cp;
3899 char *name, *gp, *dp;
3903 char dname[NAME_MAX+1];
3906 for (np = we; np != pp; pp--)
3909 for (dp = cp = space((int)(pp-np)+3); np < pp;)
3913 for (gp = cp = space(strlen(pp)+1); *np && *np != '/';)
3922 dname[NAME_MAX] = '\0';
3923 while ((de=readdir(dirp))!=NULL) {
3924 /* XXX Hmmm... What this could be? (abial) */
3926 if (ent[j].d_ino == 0)
3929 strncpy(dname, de->d_name, NAME_MAX);
3930 if (dname[0] == '.')
3933 for(k=0; k<NAME_MAX; k++)
3934 if (any(dname[k], spcl))
3936 if (gmatch(dname, gp)) {
3937 name = generate(we, pp, dname, np);
3938 if (*np && !anys(np, spcl)) {
3939 if (stat(name,&dbuf)) {
3944 nl = addword(name, nl);
3953 * generate a pathname as below.
3954 * start..end1 / middle end
3955 * the slashes come for free
3958 generate(start1, end1, middle, end)
3960 register char *end1;
3964 register char *op, *xp;
3966 p = op = space((int)(end1-start1)+strlen(middle)+strlen(end)+2);
3967 for (xp = start1; xp != end1;)
3969 for (xp = middle; (*op++ = *xp++) != '\0';)
3972 for (xp = end; (*op++ = *xp++) != '\0';)
3979 register struct wdblock *wb;
3985 for (i=0; i<wb->w_nword; i++)
3986 if (anys(spcl, *wd++))
3995 return(strcmp(*(char **)p1, *(char **)p2));
3998 /* -------- word.c -------- */
4000 static struct wdblock *
4004 register struct wdblock *wb;
4006 wb = (struct wdblock *) space(sizeof(*wb) + nw*sizeof(char *));
4012 static struct wdblock *
4015 register struct wdblock *wb;
4017 register struct wdblock *wb2;
4021 wb = newword(NSTART);
4022 if ((nw = wb->w_nword) >= wb->w_bsize) {
4023 wb2 = newword(nw * 2);
4024 memcpy((char *)wb2->w_words, (char *)wb->w_words, nw*sizeof(char *));
4029 wb->w_words[wb->w_nword++] = wd;
4035 register struct wdblock *wb;
4041 return((char **)NULL);
4042 if (wb->w_nword == 0) {
4044 return((char **)NULL);
4046 wd = (char **) space(nb = sizeof(*wd) * wb->w_nword);
4047 memcpy((char *)wd, (char *)wb->w_words, nb);
4048 DELETE(wb); /* perhaps should done by caller */
4052 int (*func)(char *, char *);
4056 glob0(a0, a1, a2, a3)
4060 int (*a3) (char *, char *);
4064 glob1(a0, a0 + a1 * a2);
4071 register char *i, *j;
4081 if ((n=(int)(lim-base)) <= v2)
4083 n = v2 * (n / (2*v2));
4084 hptr = lptr = base+n;
4089 if ((c = (*func)(i, lptr)) == 0) {
4090 glob2(i, lptr -= v2);
4101 if ((c = (*func)(hptr, j)) == 0) {
4102 glob2(hptr += v2, j);
4107 glob3(i, hptr += v2, j);
4122 if (lptr-base >= lim-hptr) {
4123 glob1(hptr+v2, lim);
4133 glob3(j, lptr -= v2, i);
4142 register char *index1, *index2, c;
4150 *index1++ = *index2;
4159 register char *index1, *index2, *index3;
4169 *index1++ = *index3;
4170 *index3++ = *index2;
4175 /* -------- io.c -------- */
4181 static int my_getc( int ec)
4185 if(e.linep > elinep) {
4186 while((c=readc()) != '\n' && c)
4188 err("input line too long");
4193 if (ec != '\'' && e.iop->task != XGRAVE) {
4196 if (c == '\n' && ec != '\"')
4197 return(my_getc(ec));
4208 if (e.iop >= e.iobase)
4216 return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0);
4224 for (; e.iop >= e.iobase; e.iop--)
4225 if ((c = e.iop->peekc) != '\0') {
4230 if (e.iop->prev != 0) {
4231 if ((c = (*e.iop->iofn)(e.iop->argp, e.iop)) != '\0') {
4236 if (e.iop == iostack)
4238 return(e.iop->prev = c);
4240 else if (e.iop->task == XIO && e.iop->prev != '\n') {
4242 if (e.iop == iostack)
4247 if (e.iop->task == XIO) {
4249 return e.iop->prev = 0;
4250 if (interactive && e.iop == iostack+1) {
4251 #ifdef BB_FEATURE_COMMAND_EDITING
4252 current_prompt=prompt->value;
4259 if (e.iop >= iostack)
4271 write(2, &c, sizeof c);
4279 if (++e.iop >= &iostack[NPUSH]) {
4281 err("Shell input nested too deeply");
4287 if (argp->afid != AFID_NOBUF)
4290 e.iop->argp = ioargstack + (e.iop - iostack);
4291 *e.iop->argp = *argp;
4292 e.iop->argp->afbuf = e.iop == &iostack[0] ? &mainbuf : &sharedbuf;
4293 if (isatty(e.iop->argp->afile) == 0 &&
4294 (e.iop == &iostack[0] ||
4295 lseek(e.iop->argp->afile, 0L, 1) != -1)) {
4296 if (++bufid == AFID_NOBUF)
4298 e.iop->argp->afid = bufid;
4302 e.iop->prev = ~'\n';
4306 if (fn == filechar || fn == linechar)
4308 else if (fn == gravechar || fn == qgravechar)
4309 e.iop->task = XGRAVE;
4311 e.iop->task = XOTHER;
4318 register struct io *xp;
4326 * Input generating functions
4330 * Produce the characters of a string, then a newline, then EOF.
4334 register struct ioarg *ap;
4338 if (ap->aword == NULL)
4340 if ((c = *ap->aword++) == 0) {
4348 * Given a list of words, produce the characters
4349 * in them, with a space after each word.
4353 register struct ioarg *ap;
4358 if ((wl = ap->awordlist) == NULL)
4361 if ((c = *(*wl)++) != 0)
4366 ap->awordlist = NULL;
4371 * Return the characters of a list of words,
4372 * producing a space between them.
4376 register struct ioarg *ap;
4380 if ((wp = *ap->awordlist++) != NULL) {
4381 PUSHIO(aword, wp, *ap->awordlist == NULL? strchar: xxchar);
4389 register struct ioarg *ap;
4393 if (ap->aword == NULL)
4395 if ((c = *ap->aword++) == '\0') {
4403 * Produce the characters from a single word (string).
4407 register struct ioarg *ap;
4411 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4417 * Produce quoted characters from a single word (string).
4421 register struct ioarg *ap;
4425 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4431 * Return the characters from a file.
4435 register struct ioarg *ap;
4439 struct iobuf *bp = ap->afbuf;
4441 if (ap->afid != AFID_NOBUF) {
4442 if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
4444 lseek(ap->afile, ap->afpos, 0);
4446 i = read(ap->afile, bp->buf, sizeof(bp->buf));
4447 } while (i < 0 && errno == EINTR);
4453 bp->ebufp = (bp->bufp = bp->buf) + i;
4456 return *bp->bufp++ & 0177;
4459 #ifdef BB_FEATURE_COMMAND_EDITING
4461 static char mycommand[BUFSIZ];
4462 static int position = 0, size = 0;
4464 while (size == 0 || position >= size) {
4465 cmdedit_read_input(current_prompt, mycommand);
4466 cmdedit_terminate();
4467 size = strlen(mycommand);
4470 c = mycommand[position];
4477 i = read(ap->afile, &c, sizeof(c));
4478 } while (i < 0 && errno == EINTR);
4479 return(i == sizeof(c)? c&0177: (closef(ap->afile), 0));
4484 * Return the characters from a here temp file.
4488 register struct ioarg *ap;
4493 if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
4502 * Return the characters produced by a process (`...`).
4503 * Quote them if required, and remove any trailing newline characters.
4512 if ((c = qgravechar(ap, iop)&~QUOTE) == '\n')
4519 register struct ioarg *ap;
4531 } else if ((c = filechar(ap)) == '\n') {
4533 while ((c = filechar(ap)) == '\n')
4541 return(c!=0? c|QUOTE: 0);
4545 * Return a single command (usually the first line) from a file.
4549 register struct ioarg *ap;
4553 if ((c = filechar(ap)) == '\n') {
4556 ap->afile = -1; /* illegal value */
4567 write(2, s, strlen(s));
4590 for (u=NUFILE; u<NOFILE;)
4595 * remap fd into Shell's fd space
4605 for (i=0; i<NOFILE; i++)
4610 } while (fd >= 0 && fd < e.iofd);
4611 for (i=0; i<NOFILE; i++)
4615 err("too many files open in shell");
4626 if ((i = pipe(pv)) < 0)
4627 err("can't create pipe - try again");
4641 /* -------- here.c -------- */
4652 register struct here *h, *lh;
4654 h = (struct here *) space(sizeof(struct here));
4657 h->h_tag = evalstr(s, DOSUB);
4666 for (lh = inhere; lh!=NULL; lh = lh->h_next)
4667 if (lh->h_next == 0) {
4671 iop->io_flag |= IOHERE|IOXHERE;
4672 for (s = h->h_tag; *s; s++)
4674 iop->io_flag &= ~ IOXHERE;
4677 h->h_dosub = iop->io_flag & IOXHERE;
4683 register struct here *h, *hp;
4685 /* Scan here files first leaving inhere list in place */
4686 for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
4687 readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub? 0: '\'');
4689 /* Make inhere list active - keep list intact for scraphere */
4691 hp->h_next = acthere;
4698 readhere(name, s, ec)
4704 char tname[30] = ".msh_XXXXXX";
4707 char myline [LINELIM+1];
4710 tf = mkstemp(tname);
4713 *name = strsave(tname, areanum);
4714 if (newenv(setjmp(errpt = ev)) != 0)
4717 pushio(e.iop->argp, e.iop->iofn);
4720 if (interactive && e.iop <= iostack) {
4721 #ifdef BB_FEATURE_COMMAND_EDITING
4722 current_prompt=cprompt->value;
4724 prs(cprompt->value);
4728 while ((c = my_getc(ec)) != '\n' && c) {
4731 if (thenext >= &myline[LINELIM]) {
4738 if (strcmp(s, myline) == 0 || c == 0)
4741 write (tf, myline, (int)(thenext-myline));
4744 prs("here document `"); prs(s); err("' unclosed");
4752 * open here temp file.
4753 * if unquoted here, expand here temp file into second temp file.
4756 herein(hname, xdoll)
4764 /* Avoid longjmp clobbering */
4769 hf = open(hname, 0);
4774 char tname[30] = ".msh_XXXXXX";
4777 tf = mkstemp(tname);
4780 if (newenv(setjmp(errpt = ev)) == 0) {
4781 PUSHIO(afile, hf, herechar);
4783 while ((c = subgetc(0, 0)) != 0) {
4785 write(tf, &c, sizeof c);
4791 tf = open(tname, 0);
4801 register struct here *h;
4803 for (h = inhere; h != NULL; h = h->h_next) {
4804 if (h->h_iop && h->h_iop->io_name)
4805 unlink(h->h_iop->io_name);
4810 /* unlink here temp files before a freearea(area) */
4815 register struct here *h, *hl;
4818 for (h = acthere; h != NULL; h = h->h_next)
4819 if (getarea((char *) h) >= area) {
4820 if (h->h_iop->io_name != NULL)
4821 unlink(h->h_iop->io_name);
4823 acthere = h->h_next;
4825 hl->h_next = h->h_next;
4833 * Copyright (c) 1987,1997, Prentice Hall
4834 * All rights reserved.
4836 * Redistribution and use of the MINIX operating system in source and
4837 * binary forms, with or without modification, are permitted provided
4838 * that the following conditions are met:
4840 * Redistributions of source code must retain the above copyright
4841 * notice, this list of conditions and the following disclaimer.
4843 * Redistributions in binary form must reproduce the above
4844 * copyright notice, this list of conditions and the following
4845 * disclaimer in the documentation and/or other materials provided
4846 * with the distribution.
4848 * Neither the name of Prentice Hall nor the names of the software
4849 * authors or contributors may be used to endorse or promote
4850 * products derived from this software without specific prior
4851 * written permission.
4853 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
4854 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
4855 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4856 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4857 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
4858 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4859 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4860 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
4861 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4862 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
4863 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
4864 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.