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 dochdir(struct op *t );
501 static int doshift(struct op *t );
502 static int dologin(struct op *t );
503 static int doumask(struct op *t );
504 static int doexec(struct op *t );
505 static int dodot(struct op *t );
506 static int dowait(struct op *t );
507 static int doread(struct op *t );
508 static int doeval(struct op *t );
509 static int dotrap(struct op *t );
510 static int getsig(char *s );
511 static void setsig (int n, void (*f)());
512 static int getn(char *as );
513 static int dobreak(struct op *t );
514 static int docontinue(struct op *t );
515 static int brkcontin (char *cp, int val );
516 static int doexit(struct op *t );
517 static int doexport(struct op *t );
518 static int doreadonly(struct op *t );
519 static void rdexp (char **wp, void (*f)(), int key);
520 static void badid(char *s );
521 static int doset(struct op *t );
522 static void varput (char *s, int out );
523 static int dotimes(void);
524 static int expand (char *cp, struct wdblock **wbp, int f );
525 static char *blank(int f );
526 static int dollar(int quoted );
527 static int grave(int quoted );
528 static void globname (char *we, char *pp );
529 static char *generate (char *start1, char *end1, char *middle, char *end );
530 static int anyspcl(struct wdblock *wb );
531 static int xstrcmp (char *p1, char *p2 );
532 static void glob0 (char *a0, unsigned int a1, int a2, int (*a3)(char *, char *));
533 static void glob1 (char *base, char *lim );
534 static void glob2 (char *i, char *j );
535 static void glob3 (char *i, char *j, char *k );
536 static void readhere (char **name, char *s, int ec );
537 static void pushio(struct ioarg *argp, int (*fn)());
538 static int xxchar(struct ioarg *ap );
543 struct ioword *h_iop;
547 static char *signame[] = {
550 (char *)NULL, /* interrupt */
552 "Illegal instruction",
556 "Floating Point Exception",
561 (char *)NULL, /* broken pipe */
565 #define NSIGNAL (sizeof(signame)/sizeof(signame[0]))
571 static struct res restab[] = {
599 static struct builtin builtin[] = {
609 {"continue",docontinue},
611 {"export", doexport},
612 {"readonly",doreadonly},
623 extern char **environ; /* environment pointer */
628 static int interactive; /* Is this an interactive shell */
630 static int multiline; /* \n changed to ; */
631 static struct op *outtree; /* result from parser */
634 static struct brkcon *brklist;
636 static struct wdblock *wdlist;
637 static struct wdblock *iolist;
638 static char *trap[_NSIG+1];
639 static char ourtrap[_NSIG+1];
640 static int trapset; /* trap pending */
641 static int yynerrs; /* yacc */
642 static char line[LINELIM];
643 static struct var *vlist; /* dictionary */
644 static struct var *homedir; /* home directory */
645 static struct var *prompt; /* main prompt */
646 static struct var *cprompt; /* continuation prompt */
647 static struct var *path; /* search path for commands */
648 static struct var *shell; /* shell to interpret command files */
649 static struct var *ifs; /* field separators */
650 static struct ioarg ioargstack[NPUSH];
651 static struct io iostack[NPUSH];
652 static int areanum; /* current allocation area */
655 static char flags['z'-'a'+1];
656 static char *flag = flags-'a';
657 static char *elinep = line+sizeof(line)-5;
658 static char *null = "";
659 static int heedint =1;
660 static struct env e ={line, iostack, iostack-1, (xint *)NULL, FDBASE, (struct env *)NULL};
661 static void (*qflag)(int) = SIG_IGN;
662 static char shellname[] = "/bin/sh";
663 static char search[] = ":/bin:/usr/bin";
667 static int iounit = IODEFAULT;
668 static YYSTYPE yylval;
669 static struct iobuf sharedbuf = {AFID_NOBUF};
670 static struct iobuf mainbuf = {AFID_NOBUF};
671 static unsigned bufid = AFID_ID; /* buffer id counter */
672 static struct ioarg temparg = {0, 0, 0, AFID_NOBUF, 0};
673 static struct here *inhere; /* list of hear docs while parsing */
674 static struct here *acthere; /* list of active here documents */
675 static struct region *areabot; /* bottom of area */
676 static struct region *areatop; /* top of area */
677 static struct region *areanxt; /* starting point of scan */
678 static void * brktop;
679 static void * brkaddr;
682 #ifdef BB_FEATURE_COMMAND_EDITING
683 char * current_prompt;
684 unsigned int shell_context;
688 /* -------- sh.c -------- */
694 extern int shell_main(int argc, char **argv)
703 if ((ap = environ) != NULL) {
705 assign(*ap++, !COPYV);
706 for (ap = environ; *ap;)
707 export(lookup(*ap++));
712 shell = lookup("SHELL");
713 if (shell->value == null)
714 setval(shell, shellname);
717 homedir = lookup("HOME");
718 if (homedir->value == null)
719 setval(homedir, "/");
722 setval(lookup("$"), itoa(getpid(), 5));
724 path = lookup("PATH");
725 if (path->value == null)
726 setval(path, search);
730 if (ifs->value == null)
731 setval(ifs, " \t\n");
733 prompt = lookup("PS1");
734 if (prompt->value == null)
735 setval(prompt, "$ ");
736 if (geteuid() == 0) {
737 setval(prompt, "# ");
738 prompt->status &= ~EXPORT;
740 cprompt = lookup("PS2");
741 if (cprompt->value == null)
742 setval(cprompt, "> ");
748 if(argv[0][0] == '-' && argv[0][1] != '\0') {
749 for (s = argv[0]+1; *s; s++)
752 prompt->status &= ~EXPORT;
753 cprompt->status &= ~EXPORT;
758 PUSHIO(aword, *++argv, iof = nlchar);
770 prompt->status &= ~EXPORT;
778 if (*s>='a' && *s<='z')
785 if (iof == filechar && --argc > 0) {
788 prompt->status &= ~EXPORT;
789 cprompt->status &= ~EXPORT;
790 if (newfile(name = *++argv))
795 if (e.iop < iostack) {
796 PUSHIO(afile, 0, iof);
797 if (isatty(0) && isatty(1) && !cflag)
800 signal(SIGQUIT, qflag);
801 if (name && name[0] == '-') {
803 if ((f = open(".profile", 0)) >= 0)
805 if ((f = open("/etc/profile", 0)) >= 0)
809 signal(SIGTERM, sig);
810 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
811 signal(SIGINT, onintr);
816 for (ap = ++argv; --argc > 0;) {
817 if (assign(*ap = *argv++, !COPYV)) {
818 dolc--; /* keyword */
824 setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
827 if (interactive && e.iop <= iostack) {
828 #ifdef BB_FEATURE_COMMAND_EDITING
829 current_prompt=prompt->value;
846 for (c='a'; c<='z'; c++)
850 setval(lookup("-"), m);
859 if (strcmp(s, "-") != 0) {
863 err(": cannot open");
893 setjmp(failpt = m1); /* Bruce Evans' fix */
894 if (setjmp(failpt = m1) || yyparse() || intr) {
898 if (!interactive && intr)
909 execute(outtree, NOPIPE, NOPIPE, 0);
910 if (!interactive && intr) {
914 if ((i = trapset) != 0) {
964 e.iop = e.iobase = iostack;
971 register struct env *ep;
977 ep = (struct env *) space(sizeof(*ep));
992 register struct env *ep;
995 if ((ep = e.oenv) != NULL) {
998 /* should close `'d files */
1000 while (--fd >= e.iofd)
1006 * Is any character from s1 in s2?
1010 register char *s1, *s2;
1019 * Is character c in s?
1036 return(itoa(n, -1));
1041 register unsigned u;
1049 if (n < 0 && (int) u < 0) {
1058 } while (--n > 0 || u);
1068 PUSHIO(afile, f, filechar);
1073 int s; /* ANSI C requires a parameter */
1075 signal(SIGINT, onintr);
1095 if ((cp = getcell(n)) == 0)
1096 err("out of string space");
1105 register char *cp, *xp;
1107 if ((cp = space(strlen(s)+1)) != NULL) {
1108 setarea((char *)cp, a);
1109 for (xp = cp; (*xp++ = *s++) != '\0';)
1127 static void runtrap(i)
1132 if ((trapstr = trap[i]) == NULL)
1136 RUN(aword, trapstr, nlchar);
1139 /* -------- var.c -------- */
1142 * Find the given name in the dictionary
1143 * and return its value. If the name was
1144 * not previously there, enter it now and
1145 * return a null value.
1151 register struct var *vp;
1154 static struct var dummy;
1158 for (c = 0; isdigit(*n) && c < 1000; n++)
1160 dummy.status = RONLY;
1161 dummy.value = c <= dolc? dolv[c]: null;
1164 for (vp = vlist; vp; vp = vp->next)
1165 if (eqname(vp->name, n))
1168 vp = (struct var *)space(sizeof(*vp));
1169 if (vp == 0 || (vp->name = space((int)(cp-n)+2)) == 0) {
1170 dummy.name = dummy.value = "";
1173 for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++)
1178 setarea((char *)vp, 0);
1179 setarea((char *)vp->name, 0);
1182 vp->status = GETCELL;
1188 * give variable at `vp' the value `val'.
1195 nameval(vp, val, (char *)NULL);
1199 * if name is not NULL, it must be
1200 * a prefix of the space `val',
1202 * this is all so that exporting
1203 * values is reasonably painless.
1206 nameval(vp, val, name)
1207 register struct var *vp;
1210 register char *cp, *xp;
1214 if (vp->status & RONLY) {
1215 for (xp = vp->name; *xp && *xp != '=';)
1216 putc(*xp++, stderr);
1217 err(" is read-only");
1222 xp = space(strlen(vp->name)+strlen(val)+2);
1225 /* make string: name=value */
1226 setarea((char *)xp, 0);
1228 for (cp = vp->name; (*xp = *cp++) && *xp!='='; xp++)
1233 for (cp = val; (*xp++ = *cp++) != '\0';)
1238 if (vp->status & GETCELL)
1239 freecell(vp->name); /* form new string `name=value' */
1249 vp->status |= EXPORT;
1256 if (isalpha(vp->name[0])) /* not an internal symbol ($# etc) */
1257 vp->status |= RONLY;
1264 if (!isalpha((int)*s))
1266 for (; *s != '='; s++)
1267 if (*s == 0 || !isalnum(*s))
1282 for (cp = s; *cp != '='; cp++)
1283 if (*cp == 0 || !isalnum(*cp))
1286 nameval(vp, ++cp, cf == COPYV? (char *)NULL: s);
1288 vp->status &= ~GETCELL;
1296 if (!isalpha(*cp++))
1299 if (!isalnum(*cp++))
1306 register int f, out;
1308 register struct var *vp;
1310 for (vp = vlist; vp; vp = vp->next)
1311 if (vp->status & f && isalpha(*vp->name)) {
1312 if (vp->status & EXPORT)
1313 write(out, "export ", 7);
1314 if (vp->status & RONLY)
1315 write(out, "readonly ", 9);
1316 write(out, vp->name, (int)(findeq(vp->name) - vp->name));
1317 write(out, "\n", 1);
1323 register char *n1, *n2;
1325 for (; *n1 != '=' && *n1 != 0; n1++)
1328 return(*n2 == 0 || *n2 == '=');
1335 while (*cp != '\0' && *cp != '=')
1340 /* -------- gmatch.c -------- */
1342 * int gmatch(string, pattern)
1343 * char *string, *pattern;
1345 * Match a pattern as in sh(1).
1350 #define QMASK (CMASK&~QUOTE)
1351 #define NOT '!' /* might use ^ */
1355 register char *s, *p;
1357 register int sc, pc;
1359 if (s == NULL || p == NULL)
1361 while ((pc = *p++ & CMASK) != '\0') {
1365 if ((p = cclass(p, sc)) == NULL)
1377 if (*p == '\0' || gmatch(s, p))
1379 } while (*s++ != '\0');
1383 if (sc != (pc&~QUOTE))
1395 register int c, d, not, found;
1397 if ((not = *p == NOT) != 0)
1402 return((char *)NULL);
1404 if (p[1] == '-' && p[2] != ']') {
1409 if (c == sub || (c <= sub && sub <= d))
1411 } while (*++p != ']');
1412 return(found? p+1: (char *)NULL);
1416 /* -------- area.c -------- */
1419 * All memory between (char *)areabot and (char *)(areatop+1) is
1420 * exclusively administered by the area management routines.
1421 * It is assumed that sbrk() and brk() manipulate the high end.
1424 #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;})
1429 brkaddr = malloc(65000);
1430 brktop = brkaddr + 65000;
1432 while ((int)sbrk(0) & ALIGN)
1434 areabot = (struct region *)sbrk(REGSIZE);
1436 areabot->next = areabot;
1437 areabot->area = BUSY;
1446 register int nregio;
1447 register struct region *p, *q;
1453 } /* silly and defeats the algorithm */
1455 * round upwards and add administration area
1457 nregio = (nbytes+(REGSIZE-1))/REGSIZE + 1;
1458 for (p = areanxt;;) {
1459 if (p->area > areanum) {
1463 while ((q = p->next)->area > areanum && q != areanxt)
1466 * exit loop if cell big enough
1468 if (q >= p + nregio)
1475 i = nregio >= GROWBY ? nregio : GROWBY;
1476 p = (struct region *)sbrk(i * REGSIZE);
1477 if (p == (struct region *)-1)
1478 return((char *)NULL);
1482 abort(); /* allocated areas are contiguous */
1492 * we found a FREE area big enough, pointed to by 'p', and up to 'q'
1494 areanxt = p + nregio;
1497 * split into requested area and rest
1499 if (areanxt+1 > q) {
1501 abort(); /* insufficient space left for admin */
1504 areanxt->area = FREE;
1508 return((char *)(p+1));
1515 register struct region *p;
1517 if ((p = (struct region *)cp) != NULL) {
1529 register struct region *p, *top;
1532 for (p = areabot; p != top; p = p->next)
1542 register struct region *p;
1544 if ((p = (struct region *)cp) != NULL)
1552 return ((struct region*)cp-1)->area;
1558 register struct region *p, *q, *top;
1561 for (p = areabot; p != top; p = p->next) {
1562 if (p->area > areanum) {
1563 while ((q = p->next)->area > areanum)
1569 if (areatop >= q + SHRINKBY && q->area > areanum) {
1578 /* -------- csyn.c -------- */
1580 * shell: syntax (C version)
1599 register struct op *t, *p;
1604 while ((c = yylex(0)) == '|') {
1605 if ((p = command(CONTIN)) == NULL)
1607 if (t->type != TPAREN && t->type != TCOM) {
1608 /* shell statement */
1609 t = block(TPAREN, t, NOBLOCK, NOWORDS);
1611 t = block(TPIPE, t, p, NOWORDS);
1621 register struct op *t, *p;
1626 while ((c = yylex(0)) == LOGAND || c == LOGOR) {
1627 if ((p = pipeline(CONTIN)) == NULL)
1629 t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS);
1639 register struct op *t, *p;
1644 if((peeksym = yylex(0)) == '&')
1645 t = block(TASYNC, t, NOBLOCK, NOWORDS);
1646 while ((c = yylex(0)) == ';' || c == '&' || (multiline && c == '\n')) {
1647 if ((p = andor()) == NULL)
1649 if((peeksym = yylex(0)) == '&')
1650 p = block(TASYNC, p, NOBLOCK, NOWORDS);
1663 register struct ioword *iop;
1667 if ((c = yylex(cf)) != '<' && c != '>') {
1673 iop = io(iounit, i, yylval.cp);
1676 markhere(yylval.cp, iop);
1684 if ((peeksym = yylex(cf)) != c)
1692 register struct op *t;
1696 switch (peeksym = yylex(0)) {
1721 register struct op *t;
1727 return(block(type, t, NOBLOCK, NOWORDS));
1734 register struct op *t;
1735 struct wdblock *iosave;
1744 switch (c = yylex(cf)) {
1747 if ((t = simple()) == NULL) {
1749 return((struct op *)NULL);
1756 t = nested(TPAREN, ')');
1760 t = nested(TBRACE, '}');
1770 t->words = wordlist();
1771 if ((c = yylex(0)) != '\n' && c != ';')
1773 t->left = dogroup(0);
1781 t->type = c == WHILE? TWHILE: TUNTIL;
1783 t->right = dogroup(1);
1795 musthave(IN, CONTIN);
1797 t->left = caselist();
1807 t->right = thenpart();
1824 register struct op *mylist;
1827 if (c == DONE && onlydone)
1828 return((struct op *)NULL);
1840 register struct op *t;
1842 if ((c = yylex(0)) != THEN) {
1844 return((struct op *)NULL);
1849 if (t->left == NULL)
1851 t->right = elsepart();
1859 register struct op *t;
1861 switch (c = yylex(0)) {
1863 if ((t = c_list()) == NULL)
1871 t->right = thenpart();
1876 return((struct op *)NULL);
1883 register struct op *t;
1886 while ((peeksym = yylex(CONTIN)) != ESAC)
1887 t = list(t, casepart());
1894 register struct op *t;
1898 t->words = pattern();
1901 if ((peeksym = yylex(CONTIN)) != ESAC)
1902 musthave(BREAK, CONTIN);
1916 } while ((c = yylex(0)) == '|');
1927 if ((c = yylex(0)) != IN) {
1929 return((char **)NULL);
1932 while ((c = yylex(0)) == WORD)
1940 * supporting functions
1944 register struct op *t1, *t2;
1950 return(block(TLIST, t1, t2, NOWORDS));
1954 block(type, t1, t2, wp)
1959 register struct op *t;
1973 register struct res *rp;
1975 for (rp = restab; rp->r_name; rp++)
1976 if (strcmp(rp->r_name, n) == 0)
1984 register struct op *t;
1986 t = (struct op *)tree(sizeof(*t));
1998 register struct op *t;
2001 iolist = addword((char *)NULL, iolist);
2002 t->ioact = copyio();
2005 if (t->type != TCOM) {
2006 if (t->type != TPAREN && t->ioact != NULL) {
2007 t = block(TPAREN, t, NOBLOCK, NOWORDS);
2008 t->ioact = t->left->ioact;
2009 t->left->ioact = NULL;
2023 wd = getwords(wdlist);
2032 wdlist = addword(cp, wdlist);
2035 static struct ioword **
2038 register struct ioword **iop;
2040 iop = (struct ioword **) getwords(iolist);
2045 static struct ioword *
2051 register struct ioword *iop;
2053 iop = (struct ioword *) tree(sizeof(*iop));
2057 iolist = addword((char *)iop, iolist);
2064 yyerror("syntax error");
2072 if (interactive && e.iop <= iostack) {
2074 while (eofc() == 0 && yylex(0) != '\n')
2088 if ((c = peeksym) > 0) {
2101 while ((c = my_getc(0)) == ' ' || c == '\t')
2105 if (any(c, "0123456789")) {
2106 unget(c1 = my_getc(0));
2107 if (c1 == '<' || c1 == '>') {
2117 while ((c = my_getc(0)) != 0 && c != '\n')
2127 if ((c = my_getc(0)) == '{') {
2128 if ((c = collect(c, '}')) != '\0')
2137 if ((c = collect(c, c)) != '\0')
2144 if ((c1 = dual(c)) != '\0') {
2162 if (multiline || cf & CONTIN) {
2163 if (interactive && e.iop <= iostack) {
2164 #ifdef BB_FEATURE_COMMAND_EDITING
2165 current_prompt=cprompt->value;
2167 prs(cprompt->value);
2184 while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n"))
2185 if (e.linep >= elinep)
2186 err("word too long");
2193 if (atstart && (c = rlookup(line))!=0) {
2197 yylval.cp = strsave(line, areanum);
2208 while ((c = my_getc(c1)) != c1) {
2213 prs("no closing "); yyerror(s);
2216 if (interactive && c == '\n' && e.iop <= iostack) {
2217 #ifdef BB_FEATURE_COMMAND_EDITING
2218 current_prompt=cprompt->value;
2220 prs(cprompt->value);
2234 register char *cp = s;
2239 if ((c = rlookup(s)) == 0)
2251 if (c == '>' || c == '<') {
2254 yylval.i = ec == '>'? IOWRITE|IOCAT: IOHERE;
2257 yylval.i = ec == '>'? IOWRITE: IOREAD;
2258 if (c != '&' || yylval.i == IOHERE)
2270 if ((t = getcell(size)) == NULL) {
2271 prs("command line too complicated\n");
2281 /* -------- exec.c -------- */
2289 execute(t, pin, pout, act)
2290 register struct op *t;
2294 register struct op *t1;
2295 volatile int i, rv, a;
2296 char *cp, **wp, **wp2;
2301 /* Avoid longjmp clobbering */
2310 wp = (wp2 = t->words) != NULL
2311 ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
2319 rv = forkexec(t, pin, pout, act, wp, &child);
2330 if ((rv = openpipe(pv)) < 0)
2332 pv[0] = remap(pv[0]);
2333 pv[1] = remap(pv[1]);
2334 (void) execute(t->left, pin, pv, 0);
2335 rv = execute(t->right, pv, pout, 0);
2340 (void) execute(t->left, pin, pout, 0);
2341 rv = execute(t->right, pin, pout, 0);
2346 int hinteractive = interactive;
2350 interactive = hinteractive;
2352 setval(lookup("!"), putn(i));
2363 signal(SIGINT, SIG_IGN);
2364 signal(SIGQUIT, SIG_IGN);
2366 signal(SIGTERM, SIG_DFL);
2370 open("/dev/null", 0);
2372 exit(execute(t->left, pin, pout, FEXEC));
2379 rv = execute(t->left, pin, pout, 0);
2380 if ((t1 = t->right)!=NULL && (rv == 0) == (t->type == TAND))
2381 rv = execute(t1, pin, pout, 0);
2391 while (*wp++ != NULL)
2394 vp = lookup(t->str);
2395 while (setjmp(bc.brkpt))
2399 for (t1 = t->left; i-- && *wp != NULL;) {
2401 rv = execute(t1, pin, pout, 0);
2403 brklist = brklist->nextlev;
2408 while (setjmp(bc.brkpt))
2413 while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE))
2414 rv = execute(t->right, pin, pout, 0);
2415 brklist = brklist->nextlev;
2420 if (t->right != NULL) {
2421 rv = !execute(t->left, pin, pout, 0) ?
2422 execute(t->right->left, pin, pout, 0):
2423 execute(t->right->right, pin, pout, 0);
2428 if ((cp = evalstr(t->str, DOSUB|DOTRIM)) == 0)
2430 if ((t1 = findcase(t->left, cp)) != NULL)
2431 rv = execute(t1, pin, pout, 0);
2436 if (iopp = t->ioact)
2438 if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
2443 if (rv >= 0 && (t1 = t->left))
2444 rv = execute(t1, pin, pout, 0);
2454 if (interactive && intr) {
2458 if ((i = trapset) != 0) {
2466 forkexec( register struct op *t, int *pin, int *pout, int act, char **wp, int *pforked)
2469 int (*shcom)() = NULL;
2472 struct ioword **iopp;
2482 struct brkcon * hbrklist;
2486 /* Avoid longjmp clobbering */
2499 rv = -1; /* system-detected error */
2500 if (t->type == TCOM) {
2501 while ((cp = *wp++) != NULL)
2505 /* strip all initial assignments */
2506 /* not correct wrt PATH=yyy command etc */
2508 echo (cp ? wp: owp);
2509 if (cp == NULL && t->ioact == NULL) {
2510 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2512 return(setstatus(0));
2514 else if (cp != NULL)
2515 shcom = inbuilt(cp);
2519 if (shcom == NULL && (f & FEXEC) == 0) {
2525 hinteractive = interactive;
2532 /* who wrote this crappy non vfork safe shit? */
2537 interactive = hinteractive;
2547 return(pout==NULL? setstatus(waitfor(i,0)): 0);
2551 signal(SIGINT, SIG_IGN);
2552 signal(SIGQUIT, SIG_IGN);
2562 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2566 if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) {
2567 err("piping to/from shell builtins not yet done");
2579 if ((iopp = t->ioact) != NULL) {
2580 if (shcom != NULL && shcom != doexec) {
2582 err(": cannot redirect shell command");
2586 if (iosetup(*iopp++, pin!=NULL, pout!=NULL))
2590 return(setstatus((*shcom)(t)));
2591 /* should use FIOCEXCL */
2592 for (i=FDBASE; i<NOFILE; i++)
2595 signal(SIGINT, SIG_DFL);
2596 signal(SIGQUIT, SIG_DFL);
2598 if (t->type == TPAREN)
2599 exit(execute(t->left, NOPIPE, NOPIPE, FEXEC));
2603 cp = rexecve(wp[0], wp, makenv());
2604 prs(wp[0]); prs(": "); warn(cp);
2613 * 0< 1> are ignored as required
2617 iosetup(iop, pipein, pipeout)
2618 register struct ioword *iop;
2619 int pipein, pipeout;
2621 register int u = -1;
2622 char *cp=NULL, *msg;
2624 if (iop->io_unit == IODEFAULT) /* take default */
2625 iop->io_unit = iop->io_flag&(IOREAD|IOHERE)? 0: 1;
2626 if (pipein && iop->io_unit == 0)
2628 if (pipeout && iop->io_unit == 1)
2630 msg = iop->io_flag&(IOREAD|IOHERE)? "open": "create";
2631 if ((iop->io_flag & IOHERE) == 0) {
2633 if ((cp = evalstr(cp, DOSUB|DOTRIM)) == NULL)
2636 if (iop->io_flag & IODUP) {
2637 if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
2639 err(": illegal >& argument");
2643 iop->io_flag = IOCLOSE;
2644 iop->io_flag &= ~(IOREAD|IOWRITE);
2646 switch (iop->io_flag) {
2652 case IOHERE|IOXHERE:
2653 u = herein(iop->io_name, iop->io_flag&IOXHERE);
2658 if ((u = open(cp, 1)) >= 0) {
2659 lseek(u, (long)0, 2);
2663 u = creat(cp, 0666);
2667 u = dup2(*cp-'0', iop->io_unit);
2671 close(iop->io_unit);
2680 if (u != iop->io_unit) {
2681 dup2(u, iop->io_unit);
2695 for (i=0; wp[i]; i++) {
2708 register struct op *t1;
2710 register char **wp, *cp;
2713 return((struct op **)NULL);
2714 if (t->type == TLIST) {
2715 if ((tp = find1case(t->left, w)) != NULL)
2717 t1 = t->right; /* TPAT */
2720 for (wp = t1->words; *wp;)
2721 if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp))
2723 return((struct op **)NULL);
2731 register struct op **tp;
2733 return((tp = find1case(t, w)) != NULL? *tp: (struct op *)NULL);
2737 * Enter a new loop level (marked for break/continue).
2743 bc->nextlev = brklist;
2748 * Wait for the last process created.
2749 * Print a message for each process found
2750 * that was killed by a signal.
2751 * Ignore interrupt signals while waiting
2752 * unless `canintr' is true.
2755 waitfor(lastpid, canintr)
2756 register int lastpid;
2759 register int pid, rv;
2761 int oheedint = heedint;
2768 if (errno != EINTR || canintr)
2771 if ((rv = WAITSIG(s)) != 0) {
2773 if (signame[rv] != NULL) {
2774 if (pid != lastpid) {
2781 if (pid != lastpid) {
2785 prs("Signal "); prn(rv); prs(" ");
2788 prs(" - core dumped");
2789 if (rv >= NSIGNAL || signame[rv])
2795 } while (pid != lastpid);
2802 if (exstat == 0) exstat = rv;
2814 setval(lookup("?"), putn(s));
2819 * PATH-searching interface to execve.
2820 * If getenv("PATH") were kept up-to-date,
2821 * execvp might be used.
2825 char *c, **v, **envp;
2828 register char *sp, *tp;
2829 int eacces = 0, asis = 0;
2831 sp = any('/', c)? "": path->value;
2833 while (asis || *sp != '\0') {
2836 for (; *sp != '\0'; tp++)
2837 if ((*tp = *sp++) == ':') {
2843 for (i = 0; (*tp++ = c[i++]) != '\0';)
2845 execve(e.linep, v, envp);
2851 execve("/bin/sh", v, envp);
2856 return("program too big");
2859 return("argument list too long");
2866 return(errno==ENOENT ? "not found" : "cannot execute");
2870 * Run the command produced by generator `f'
2871 * applied to stream `arg'.
2879 struct wdblock *swdlist;
2880 struct wdblock *siolist;
2886 /* Avoid longjmp clobbering */
2896 if (newenv(setjmp(errpt = ev)) == 0) {
2902 if (setjmp(failpt = rt) == 0 && yyparse() == 0)
2903 rv = execute(outtree, NOPIPE, NOPIPE, 0);
2910 freearea(areanum--);
2914 /* -------- do.c -------- */
2917 * built-in commands: doX
2928 register struct op *t;
2930 register char *cp, *er;
2932 if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL)
2933 er = ": no home directory";
2934 else if(chdir(cp) < 0)
2935 er = ": bad directory";
2938 prs(cp != NULL? cp: "cd");
2945 register struct op *t;
2949 n = t->words[1]? getn(t->words[1]): 1;
2951 err("nothing to shift");
2957 setval(lookup("#"), putn(dolc));
2962 * execute login and newgrp directly
2971 signal(SIGINT, SIG_DFL);
2972 signal(SIGQUIT, SIG_DFL);
2974 cp = rexecve(t->words[0], t->words, makenv());
2975 prs(t->words[0]); prs(": "); err(cp);
2981 register struct op *t;
2986 if ((cp = t->words[1]) == NULL) {
2989 for (n=3*4; (n-=3) >= 0;)
2990 putc('0'+((i>>n)&07), stderr);
2993 for (n=0; *cp>='0' && *cp<='9'; cp++)
2994 n = n*8 + (*cp-'0');
3002 register struct op *t;
3009 for(i = 0; (t->words[i]=t->words[i+1]) != NULL; i++)
3015 if (setjmp(failpt = ex) == 0)
3016 execute(t, NOPIPE, NOPIPE, FEXEC);
3027 register char *sp, *tp;
3030 if ((cp = t->words[1]) == NULL)
3032 sp = any('/', cp)? ":": path->value;
3035 while (*sp && (*tp = *sp++) != ':')
3039 for (i = 0; (*tp++ = cp[i++]) != '\0';)
3041 if ((i = open(e.linep, 0)) >= 0) {
3059 if ((cp = t->words[1]) != NULL) {
3065 setstatus(waitfor(i, 1));
3073 register char *cp, **wp;
3074 register int nb = 0;
3075 register int nl = 0;
3077 if (t->words[1] == NULL) {
3078 err("Usage: read name ...");
3081 for (wp = t->words+1; *wp; wp++) {
3082 for (cp = e.linep; !nl && cp < elinep-1; cp++)
3083 if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) ||
3084 (nl = (*cp == '\n')) ||
3085 (wp[1] && any(*cp, ifs->value)))
3090 setval(lookup(*wp), e.linep);
3097 register struct op *t;
3099 return(RUN(awordlist, t->words+1, wdchar));
3104 register struct op *t;
3107 register int resetsig;
3109 if (t->words[1] == NULL) {
3110 for (i=0; i<=_NSIG; i++)
3119 resetsig = isdigit(*t->words[1]);
3120 for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) {
3121 n = getsig(t->words[i]);
3125 if (*t->words[1] != '\0') {
3126 trap[n] = strsave(t->words[1], 0);
3135 setsig(n, n == SIGQUIT ? SIG_IGN
3150 if ((n = getn(s)) < 0 || n > _NSIG) {
3151 err("trap: bad signal number");
3158 setsig( register int n, void (*f)(int))
3162 if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) {
3181 for (n = 0; isdigit(*s); s++)
3182 n = (n*10) + (*s-'0');
3185 err(": bad number");
3194 return(brkcontin(t->words[1], 1));
3201 return(brkcontin(t->words[1], 0));
3209 register struct brkcon *bc;
3212 nl = cp == NULL? 1: getn(cp);
3216 if ((bc = brklist) == NULL)
3218 brklist = bc->nextlev;
3221 err("bad break/continue level");
3225 longjmp(bc->brkpt, 1);
3236 if ((cp = t->words[1]) != NULL)
3237 setstatus(getn(cp));
3247 rdexp(t->words+1, export, EXPORT);
3255 rdexp(t->words+1, ronly, RONLY);
3266 for (; *wp != NULL; wp++)
3280 err(": bad identifier");
3285 register struct op *t;
3287 register struct var *vp;
3291 if ((cp = t->words[1]) == NULL) {
3292 for (vp = vlist; vp; vp = vp->next)
3293 varput(vp->name, 1);
3297 /* bad: t->words++; */
3298 for(n = 0; (t->words[n]=t->words[n+1]) != NULL; n++)
3301 flag['x'] = flag['v'] = 0;
3311 if (*cp>='a' && *cp<='z')
3318 t->words[0] = dolv[0];
3319 for (n=1; t->words[n]; n++)
3320 setarea((char *)t->words[n], 0);
3323 setval(lookup("#"), putn(dolc));
3324 setarea((char *)(dolv-1), 0);
3335 write(out, s, strlen(s));
3336 write(out, "\n", 1);
3342 * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
3343 * This file contains code for the times builtin.
3344 * $Id: msh.c,v 1.1 2001/06/29 04:57:14 andersen Exp $
3346 static int dotimes ()
3349 long int clk_tck = sysconf(_SC_CLK_TCK);
3352 printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
3353 (int) (buf.tms_utime / clk_tck / 60),
3354 ((double) buf.tms_utime) / clk_tck,
3355 (int) (buf.tms_stime / clk_tck / 60),
3356 ((double) buf.tms_stime) / clk_tck,
3357 (int) (buf.tms_cutime / clk_tck / 60),
3358 ((double) buf.tms_cutime) / clk_tck,
3359 (int) (buf.tms_cstime / clk_tck / 60),
3360 ((double) buf.tms_cstime) / clk_tck);
3365 static int (*inbuilt(s))()
3368 register struct builtin *bp;
3370 for (bp = builtin; bp->command != NULL; bp++)
3371 if (strcmp(bp->command, s) == 0)
3373 return((int(*)())NULL);
3376 /* -------- eval.c -------- */
3381 * blank interpretation
3386 static char ** eval( char **ap, int f)
3394 /* Avoid longjmp clobbering */
3401 if (newenv(setjmp(errpt = ev)) == 0) {
3402 while (*ap && isassign(*ap))
3403 expand(*ap++, &wb, f & ~DOGLOB);
3405 for (wf = ap; *wf; wf++) {
3407 expand(*wf, &wb, f & ~DOGLOB);
3410 for (wb = addword((char *)0, wb); *ap; ap++) {
3411 if (!flag['k'] || !isassign(*ap))
3412 expand(*ap, &wb, f & ~DOKEY);
3414 wb = addword((char *)0, wb);
3419 return(gflg? (char **)NULL: wp);
3423 * Make the exported environment from the exported
3424 * names in the dictionary. Keyword assignments
3425 * will already have been done.
3431 register struct wdblock *wb;
3432 register struct var *vp;
3435 for (vp = vlist; vp; vp = vp->next)
3436 if (vp->status & EXPORT)
3437 wb = addword(vp->name, wb);
3438 wb = addword((char *)0, wb);
3439 return(getwords(wb));
3450 if (expand(cp, &wb, f)) {
3451 if (wb == NULL || wb->w_nword == 0 || (cp = wb->w_words[0]) == NULL)
3460 expand( char *cp, register struct wdblock **wbp, int f)
3465 /* Avoid longjmp clobbering */
3471 if (!anys("$`'\"", cp) &&
3472 !anys(ifs->value, cp) &&
3473 ((f&DOGLOB)==0 || !anys("[*?", cp))) {
3474 cp = strsave(cp, areanum);
3477 *wbp = addword(cp, *wbp);
3480 if (newenv(setjmp(errpt = ev)) == 0) {
3481 PUSHIO(aword, cp, strchar);
3483 while ((cp = blank(f)) && gflg == 0) {
3485 cp = strsave(cp, areanum);
3486 if ((f&DOGLOB) == 0) {
3489 *wbp = addword(cp, *wbp);
3491 *wbp = glob(cp, *wbp);
3500 * Blank interpretation and quoting
3508 int scanequals, foundequals;
3511 scanequals = f & DOKEY;
3515 switch (c = subgetc('"', foundequals)) {
3523 if (f & DOBLANK && any(c, ifs->value))
3532 for (c1 = c; (c = subgetc(c1, 1)) != c1;) {
3535 if (c == '\'' || !any(c, "$`\""))
3545 c = subgetc('"', foundequals);
3547 f & (DOBLANK && any(c, ifs->value)) ||
3548 (!INSUB() && any(c, "\"'"))) {
3560 else if (!isalnum(c))
3570 * Get characters, substituting for ` and $
3581 if (!INSUB() && ec != '\'') {
3583 if (grave(quoted) == 0)
3585 e.iop->task = XGRAVE;
3588 if (c == '$' && (c = dollar(quoted)) == 0) {
3589 e.iop->task = XDOLL;
3597 * Prepare to generate the string returned by ${} substitution.
3606 register char *s, c, *cp=NULL;
3614 while ((c = readc())!=0 && isalnum(c))
3615 if (e.linep < elinep)
3622 otask = e.iop->task;
3623 e.iop->task = XOTHER;
3624 while ((c = subgetc('"', 0))!=0 && c!='}' && c!='\n')
3625 if (e.linep < elinep)
3628 e.iop->task = otask;
3635 if (e.linep >= elinep) {
3636 err("string in ${} too long");
3642 for (cp = s+1; *cp; cp++)
3643 if (any(*cp, "=-+?")) {
3648 if (s[1] == 0 && (*s == '*' || *s == '@')) {
3650 /* currently this does not distinguish $* and $@ */
3651 /* should check dollar */
3653 PUSHIO(awordlist, dolv+1, dolchar);
3655 } else { /* trap the nasty ${=} */
3661 if ((dolp = vp->value) == null) {
3665 err("cannot use ${...=...} with $n");
3674 dolp = strsave(cp, areanum);
3679 prs("missing value for ");
3686 } else if (c == '+')
3687 dolp = strsave(cp, areanum);
3688 if (flag['u'] && dolp == null) {
3689 prs("unset variable: ");
3694 PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
3699 * Run the command in `...` and read its output.
3710 /* Avoid longjmp clobbering */
3713 for (cp = e.iop->argp->aword; *cp != '`'; cp++)
3715 err("no closing `");
3718 if (openpipe(pf) < 0)
3720 if ((i = vfork()) == -1) {
3726 e.iop->argp->aword = ++cp;
3728 PUSHIO(afile, remap(pf[0]), quoted? qgravechar: gravechar);
3732 /* allow trapped signals */
3733 for (i=0; i<=_NSIG; i++)
3734 if (ourtrap[i] && signal(i, SIG_IGN) != SIG_IGN)
3741 cp = strsave(e.iop->argp->aword, 0);
3744 freearea(areanum); /* free old space */
3746 e.iop = (e.iobase = iostack) - 1;
3749 PUSHIO(aword, cp, nlchar);
3760 if ((s = as) != NULL)
3766 /* -------- glob.c -------- */
3772 #define scopy(x) strsave((x), areanum)
3774 #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
3776 static struct wdblock *cl, *nl;
3777 static char spcl[] = "[?*";
3779 static struct wdblock *
3790 for (pp = cp; *pp; pp++)
3793 else if (!any(*pp & ~QUOTE, spcl))
3796 for (cl = addword(scopy(cp), (struct wdblock *)0); anyspcl(cl); cl = nl) {
3797 nl = newword(cl->w_nword*2);
3798 for(i=0; i<cl->w_nword; i++) { /* for each argument */
3799 for (pp = cl->w_words[i]; *pp; pp++)
3800 if (any(*pp, spcl)) {
3801 globname(cl->w_words[i], pp);
3805 nl = addword(scopy(cl->w_words[i]), nl);
3807 for(i=0; i<cl->w_nword; i++)
3808 DELETE(cl->w_words[i]);
3811 for(i=0; i<cl->w_nword; i++)
3812 unquote(cl->w_words[i]);
3813 glob0((char *)cl->w_words, cl->w_nword, sizeof(char *), xstrcmp);
3815 for (i=0; i<cl->w_nword; i++)
3816 wb = addword(cl->w_words[i], wb);
3821 wb = addword(unquote(cp), wb);
3830 register char *np, *cp;
3831 char *name, *gp, *dp;
3835 char dname[NAME_MAX+1];
3838 for (np = we; np != pp; pp--)
3841 for (dp = cp = space((int)(pp-np)+3); np < pp;)
3845 for (gp = cp = space(strlen(pp)+1); *np && *np != '/';)
3854 dname[NAME_MAX] = '\0';
3855 while ((de=readdir(dirp))!=NULL) {
3856 /* XXX Hmmm... What this could be? (abial) */
3858 if (ent[j].d_ino == 0)
3861 strncpy(dname, de->d_name, NAME_MAX);
3862 if (dname[0] == '.')
3865 for(k=0; k<NAME_MAX; k++)
3866 if (any(dname[k], spcl))
3868 if (gmatch(dname, gp)) {
3869 name = generate(we, pp, dname, np);
3870 if (*np && !anys(np, spcl)) {
3871 if (stat(name,&dbuf)) {
3876 nl = addword(name, nl);
3885 * generate a pathname as below.
3886 * start..end1 / middle end
3887 * the slashes come for free
3890 generate(start1, end1, middle, end)
3892 register char *end1;
3896 register char *op, *xp;
3898 p = op = space((int)(end1-start1)+strlen(middle)+strlen(end)+2);
3899 for (xp = start1; xp != end1;)
3901 for (xp = middle; (*op++ = *xp++) != '\0';)
3904 for (xp = end; (*op++ = *xp++) != '\0';)
3911 register struct wdblock *wb;
3917 for (i=0; i<wb->w_nword; i++)
3918 if (anys(spcl, *wd++))
3927 return(strcmp(*(char **)p1, *(char **)p2));
3930 /* -------- word.c -------- */
3932 static struct wdblock *
3936 register struct wdblock *wb;
3938 wb = (struct wdblock *) space(sizeof(*wb) + nw*sizeof(char *));
3944 static struct wdblock *
3947 register struct wdblock *wb;
3949 register struct wdblock *wb2;
3953 wb = newword(NSTART);
3954 if ((nw = wb->w_nword) >= wb->w_bsize) {
3955 wb2 = newword(nw * 2);
3956 memcpy((char *)wb2->w_words, (char *)wb->w_words, nw*sizeof(char *));
3961 wb->w_words[wb->w_nword++] = wd;
3967 register struct wdblock *wb;
3973 return((char **)NULL);
3974 if (wb->w_nword == 0) {
3976 return((char **)NULL);
3978 wd = (char **) space(nb = sizeof(*wd) * wb->w_nword);
3979 memcpy((char *)wd, (char *)wb->w_words, nb);
3980 DELETE(wb); /* perhaps should done by caller */
3984 int (*func)(char *, char *);
3988 glob0(a0, a1, a2, a3)
3992 int (*a3) (char *, char *);
3996 glob1(a0, a0 + a1 * a2);
4003 register char *i, *j;
4013 if ((n=(int)(lim-base)) <= v2)
4015 n = v2 * (n / (2*v2));
4016 hptr = lptr = base+n;
4021 if ((c = (*func)(i, lptr)) == 0) {
4022 glob2(i, lptr -= v2);
4033 if ((c = (*func)(hptr, j)) == 0) {
4034 glob2(hptr += v2, j);
4039 glob3(i, hptr += v2, j);
4054 if (lptr-base >= lim-hptr) {
4055 glob1(hptr+v2, lim);
4065 glob3(j, lptr -= v2, i);
4074 register char *index1, *index2, c;
4082 *index1++ = *index2;
4091 register char *index1, *index2, *index3;
4101 *index1++ = *index3;
4102 *index3++ = *index2;
4107 /* -------- io.c -------- */
4113 static int my_getc( int ec)
4117 if(e.linep > elinep) {
4118 while((c=readc()) != '\n' && c)
4120 err("input line too long");
4125 if (ec != '\'' && e.iop->task != XGRAVE) {
4128 if (c == '\n' && ec != '\"')
4129 return(my_getc(ec));
4140 if (e.iop >= e.iobase)
4148 return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0);
4156 for (; e.iop >= e.iobase; e.iop--)
4157 if ((c = e.iop->peekc) != '\0') {
4162 if (e.iop->prev != 0) {
4163 if ((c = (*e.iop->iofn)(e.iop->argp, e.iop)) != '\0') {
4168 if (e.iop == iostack)
4170 return(e.iop->prev = c);
4172 else if (e.iop->task == XIO && e.iop->prev != '\n') {
4174 if (e.iop == iostack)
4179 if (e.iop->task == XIO) {
4181 return e.iop->prev = 0;
4182 if (interactive && e.iop == iostack+1) {
4183 #ifdef BB_FEATURE_COMMAND_EDITING
4184 current_prompt=prompt->value;
4191 if (e.iop >= iostack)
4203 write(2, &c, sizeof c);
4211 if (++e.iop >= &iostack[NPUSH]) {
4213 err("Shell input nested too deeply");
4219 if (argp->afid != AFID_NOBUF)
4222 e.iop->argp = ioargstack + (e.iop - iostack);
4223 *e.iop->argp = *argp;
4224 e.iop->argp->afbuf = e.iop == &iostack[0] ? &mainbuf : &sharedbuf;
4225 if (isatty(e.iop->argp->afile) == 0 &&
4226 (e.iop == &iostack[0] ||
4227 lseek(e.iop->argp->afile, 0L, 1) != -1)) {
4228 if (++bufid == AFID_NOBUF)
4230 e.iop->argp->afid = bufid;
4234 e.iop->prev = ~'\n';
4238 if (fn == filechar || fn == linechar)
4240 else if (fn == gravechar || fn == qgravechar)
4241 e.iop->task = XGRAVE;
4243 e.iop->task = XOTHER;
4250 register struct io *xp;
4258 * Input generating functions
4262 * Produce the characters of a string, then a newline, then EOF.
4266 register struct ioarg *ap;
4270 if (ap->aword == NULL)
4272 if ((c = *ap->aword++) == 0) {
4280 * Given a list of words, produce the characters
4281 * in them, with a space after each word.
4285 register struct ioarg *ap;
4290 if ((wl = ap->awordlist) == NULL)
4293 if ((c = *(*wl)++) != 0)
4298 ap->awordlist = NULL;
4303 * Return the characters of a list of words,
4304 * producing a space between them.
4308 register struct ioarg *ap;
4312 if ((wp = *ap->awordlist++) != NULL) {
4313 PUSHIO(aword, wp, *ap->awordlist == NULL? strchar: xxchar);
4321 register struct ioarg *ap;
4325 if (ap->aword == NULL)
4327 if ((c = *ap->aword++) == '\0') {
4335 * Produce the characters from a single word (string).
4339 register struct ioarg *ap;
4343 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4349 * Produce quoted characters from a single word (string).
4353 register struct ioarg *ap;
4357 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4363 * Return the characters from a file.
4367 register struct ioarg *ap;
4371 struct iobuf *bp = ap->afbuf;
4373 if (ap->afid != AFID_NOBUF) {
4374 if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
4376 lseek(ap->afile, ap->afpos, 0);
4378 i = read(ap->afile, bp->buf, sizeof(bp->buf));
4379 } while (i < 0 && errno == EINTR);
4385 bp->ebufp = (bp->bufp = bp->buf) + i;
4388 return *bp->bufp++ & 0177;
4391 #ifdef BB_FEATURE_COMMAND_EDITING
4393 static char mycommand[BUFSIZ];
4394 static int position = 0, size = 0;
4396 while (size == 0 || position >= size) {
4397 cmdedit_read_input(current_prompt, mycommand);
4398 cmdedit_terminate();
4399 size = strlen(mycommand);
4402 c = mycommand[position];
4409 i = read(ap->afile, &c, sizeof(c));
4410 } while (i < 0 && errno == EINTR);
4411 return(i == sizeof(c)? c&0177: (closef(ap->afile), 0));
4416 * Return the characters from a here temp file.
4420 register struct ioarg *ap;
4425 if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
4434 * Return the characters produced by a process (`...`).
4435 * Quote them if required, and remove any trailing newline characters.
4444 if ((c = qgravechar(ap, iop)&~QUOTE) == '\n')
4451 register struct ioarg *ap;
4463 } else if ((c = filechar(ap)) == '\n') {
4465 while ((c = filechar(ap)) == '\n')
4473 return(c!=0? c|QUOTE: 0);
4477 * Return a single command (usually the first line) from a file.
4481 register struct ioarg *ap;
4485 if ((c = filechar(ap)) == '\n') {
4488 ap->afile = -1; /* illegal value */
4499 write(2, s, strlen(s));
4522 for (u=NUFILE; u<NOFILE;)
4527 * remap fd into Shell's fd space
4537 for (i=0; i<NOFILE; i++)
4542 } while (fd >= 0 && fd < e.iofd);
4543 for (i=0; i<NOFILE; i++)
4547 err("too many files open in shell");
4558 if ((i = pipe(pv)) < 0)
4559 err("can't create pipe - try again");
4573 /* -------- here.c -------- */
4584 register struct here *h, *lh;
4586 h = (struct here *) space(sizeof(struct here));
4589 h->h_tag = evalstr(s, DOSUB);
4598 for (lh = inhere; lh!=NULL; lh = lh->h_next)
4599 if (lh->h_next == 0) {
4603 iop->io_flag |= IOHERE|IOXHERE;
4604 for (s = h->h_tag; *s; s++)
4606 iop->io_flag &= ~ IOXHERE;
4609 h->h_dosub = iop->io_flag & IOXHERE;
4615 register struct here *h, *hp;
4617 /* Scan here files first leaving inhere list in place */
4618 for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
4619 readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub? 0: '\'');
4621 /* Make inhere list active - keep list intact for scraphere */
4623 hp->h_next = acthere;
4630 readhere(name, s, ec)
4636 char tname[30] = ".msh_XXXXXX";
4639 char myline [LINELIM+1];
4642 tf = mkstemp(tname);
4645 *name = strsave(tname, areanum);
4646 if (newenv(setjmp(errpt = ev)) != 0)
4649 pushio(e.iop->argp, e.iop->iofn);
4652 if (interactive && e.iop <= iostack) {
4653 #ifdef BB_FEATURE_COMMAND_EDITING
4654 current_prompt=cprompt->value;
4656 prs(cprompt->value);
4660 while ((c = my_getc(ec)) != '\n' && c) {
4663 if (thenext >= &myline[LINELIM]) {
4670 if (strcmp(s, myline) == 0 || c == 0)
4673 write (tf, myline, (int)(thenext-myline));
4676 prs("here document `"); prs(s); err("' unclosed");
4684 * open here temp file.
4685 * if unquoted here, expand here temp file into second temp file.
4688 herein(hname, xdoll)
4696 /* Avoid longjmp clobbering */
4701 hf = open(hname, 0);
4706 char tname[30] = ".msh_XXXXXX";
4709 tf = mkstemp(tname);
4712 if (newenv(setjmp(errpt = ev)) == 0) {
4713 PUSHIO(afile, hf, herechar);
4715 while ((c = subgetc(0, 0)) != 0) {
4717 write(tf, &c, sizeof c);
4723 tf = open(tname, 0);
4733 register struct here *h;
4735 for (h = inhere; h != NULL; h = h->h_next) {
4736 if (h->h_iop && h->h_iop->io_name)
4737 unlink(h->h_iop->io_name);
4742 /* unlink here temp files before a freearea(area) */
4747 register struct here *h, *hl;
4750 for (h = acthere; h != NULL; h = h->h_next)
4751 if (getarea((char *) h) >= area) {
4752 if (h->h_iop->io_name != NULL)
4753 unlink(h->h_iop->io_name);
4755 acthere = h->h_next;
4757 hl->h_next = h->h_next;
4765 * Copyright (c) 1987,1997, Prentice Hall
4766 * All rights reserved.
4768 * Redistribution and use of the MINIX operating system in source and
4769 * binary forms, with or without modification, are permitted provided
4770 * that the following conditions are met:
4772 * Redistributions of source code must retain the above copyright
4773 * notice, this list of conditions and the following disclaimer.
4775 * Redistributions in binary form must reproduce the above
4776 * copyright notice, this list of conditions and the following
4777 * disclaimer in the documentation and/or other materials provided
4778 * with the distribution.
4780 * Neither the name of Prentice Hall nor the names of the software
4781 * authors or contributors may be used to endorse or promote
4782 * products derived from this software without specific prior
4783 * written permission.
4785 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
4786 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
4787 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4788 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4789 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
4790 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4791 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4792 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
4793 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4794 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
4795 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
4796 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.