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 */
56 #define NOFILE 20 /* Number of open files */
57 #define NUFILE 10 /* Number of user-accessible files */
58 #define FDBASE 10 /* First file usable by Shell */
61 * values returned by wait
63 #define WAITSIG(s) ((s)&0177)
64 #define WAITVAL(s) (((s)>>8)&0377)
65 #define WAITCORE(s) (((s)&0200)!=0)
68 * library and system defintions
70 typedef void xint; /* base type of jmp_buf, for not broken compilers */
78 #define NOBLOCK ((struct op *)NULL)
79 #define NOWORD ((char *)NULL)
80 #define NOWORDS ((char **)NULL)
81 #define NOPIPE ((int *)NULL)
84 * Description of a command or an operation on commands.
85 * Might eventually use a union.
88 int type; /* operation type, see below */
89 char **words; /* arguments to a command */
90 struct ioword **ioact; /* IO actions (eg, < > >>) */
93 char *str; /* identifier for case and for */
96 #define TCOM 1 /* command */
97 #define TPAREN 2 /* (c-list) */
98 #define TPIPE 3 /* a | b */
99 #define TLIST 4 /* a [&;] b */
100 #define TOR 5 /* || */
101 #define TAND 6 /* && */
109 #define TPAT 14 /* pattern in case */
110 #define TBRACE 15 /* {c-list} */
111 #define TASYNC 16 /* c & */
114 * actions determining the environment of a process
116 #define BIT(i) (1<<(i))
117 #define FEXEC BIT(0) /* execute without forking */
120 * flags to control evaluation of words
122 #define DOSUB 1 /* interpret $, `, and quotes */
123 #define DOBLANK 2 /* perform blank interpretation */
124 #define DOGLOB 4 /* interpret [?* */
125 #define DOKEY 8 /* move words with `=' to 2nd arg. list */
126 #define DOTRIM 16 /* trim resulting string */
128 #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
134 static int interactive; /* Is this an interactive shell */
136 static int multiline; /* \n changed to ; */
137 static struct op *outtree; /* result from parser */
141 static struct brkcon *brklist;
143 static int newfile(char *s);
144 static char *findeq(char *cp);
145 static char *cclass(char *p, int sub);
146 static void initarea(void);
147 extern int msh_main(int argc, char **argv);
152 struct brkcon *nextlev;
159 short io_unit; /* unit affected */
160 short io_flag; /* action (below) */
161 char *io_name; /* file name */
163 #define IOREAD 1 /* < */
164 #define IOHERE 2 /* << (here file) */
165 #define IOWRITE 4 /* > */
166 #define IOCAT 8 /* >> */
167 #define IOXHERE 16 /* ${}, ` in << */
168 #define IODUP 32 /* >&digit */
169 #define IOCLOSE 64 /* >&- */
171 #define IODEFAULT (-1) /* token for default IO unit */
173 static struct wdblock *wdlist;
174 static struct wdblock *iolist;
177 * parsing & execution environment
191 * -k: look for name=value everywhere on command line
193 * -t: exit after reading and executing one command
196 * -u: unset variables net diagnostic
200 static char *null; /* null value for variable */
201 static int intr; /* interrupt pending */
203 static char *trap[_NSIG+1];
204 static char ourtrap[_NSIG+1];
205 static int trapset; /* trap pending */
207 static int heedint; /* heed interrupt signals */
209 static int yynerrs; /* yacc */
211 static char line[LINELIM];
217 static int(*inbuilt(char *s))(struct op *);
220 static char *rexecve (char *c , char **v, char **envp );
221 static char *space (int n );
222 static char *strsave (char *s, int a );
223 static char *evalstr (char *cp, int f );
224 static char *putn (int n );
225 static char *itoa (unsigned u, int n );
226 static char *unquote (char *as );
227 static struct var *lookup (char *n );
228 static int rlookup (char *n );
229 static struct wdblock *glob (char *cp, struct wdblock *wb );
230 static int my_getc( int ec);
231 static int subgetc (int ec, int quoted );
232 static char **makenv (void);
233 static char **eval (char **ap, int f );
234 static int setstatus (int s );
235 static int waitfor (int lastpid, int canintr );
237 static void onintr (int s ); /* SIGINT handler */
239 static int newenv (int f );
240 static void quitenv (void);
241 static void err (char *s );
242 static int anys (char *s1, char *s2 );
243 static int any (int c, char *s );
244 static void next (int f );
245 static void setdash (void);
246 static void onecommand (void);
247 static void runtrap (int i );
248 static int gmatch (char *s, char *p );
253 static void leave (void); /* abort shell (or fail in subshell) */
254 static void fail (void); /* fail but return to process next command */
255 static void warn (char *s );
256 static void sig (int i ); /* default signal handler */
260 /* -------- area stuff -------- */
262 #define REGSIZE sizeof(struct region)
264 //#define SHRINKBY 64
268 #define ALIGN (sizeof(int)-1)
278 /* -------- grammar stuff -------- */
302 #define YYERRCODE 300
305 #define CONTIN 01 /* skip new lines to complete command */
307 #define SYNTAXERR zzerr()
308 static struct op *pipeline(int cf );
309 static struct op *andor(void);
310 static struct op *c_list(void);
311 static int synio(int cf );
312 static void musthave (int c, int cf );
313 static struct op *simple(void);
314 static struct op *nested(int type, int mark );
315 static struct op *command(int cf );
316 static struct op *dogroup(int onlydone );
317 static struct op *thenpart(void);
318 static struct op *elsepart(void);
319 static struct op *caselist(void);
320 static struct op *casepart(void);
321 static char **pattern(void);
322 static char **wordlist(void);
323 static struct op *list(struct op *t1, struct op *t2 );
324 static struct op *block(int type, struct op *t1, struct op *t2, char **wp );
325 static struct op *newtp(void);
326 static struct op *namelist(struct op *t );
327 static char **copyw(void);
328 static void word(char *cp );
329 static struct ioword **copyio(void);
330 static struct ioword *io (int u, int f, char *cp );
331 static void zzerr(void);
332 static void yyerror(char *s );
333 static int yylex(int cf );
334 static int collect(int c, int c1 );
335 static int dual(int c );
336 static void diag(int ec );
337 static char *tree(unsigned size );
339 /* -------- var.h -------- */
347 #define COPYV 1 /* flag to setval, suggesting copy */
348 #define RONLY 01 /* variable is read-only */
349 #define EXPORT 02 /* variable is to be exported */
350 #define GETCELL 04 /* name & value space was got with getcell */
352 static struct var *vlist; /* dictionary */
354 static struct var *homedir; /* home directory */
355 static struct var *prompt; /* main prompt */
356 static struct var *cprompt; /* continuation prompt */
357 static struct var *path; /* search path for commands */
358 static struct var *shell; /* shell to interpret command files */
359 static struct var *ifs; /* field separators */
361 static int yyparse (void);
362 static struct var *lookup (char *n );
363 static void setval (struct var *vp, char *val );
364 static void nameval (struct var *vp, char *val, char *name );
365 static void export (struct var *vp );
366 static void ronly (struct var *vp );
367 static int isassign (char *s );
368 static int checkname (char *cp );
369 static int assign (char *s, int cf );
370 static void putvlist (int f, int out );
371 static int eqname (char *n1, char *n2 );
373 static int execute (struct op *t, int *pin, int *pout, int act );
375 /* -------- io.h -------- */
378 unsigned id; /* buffer id */
379 char buf[512]; /* buffer */
380 char *bufp; /* pointer into buffer */
381 char *ebufp; /* pointer to end of buffer */
384 /* possible arguments to an IO function */
388 int afile; /* file descriptor */
389 unsigned afid; /* buffer id */
390 long afpos; /* file position */
391 struct iobuf *afbuf; /* buffer for this file */
393 //static struct ioarg ioargstack[NPUSH];
394 #define AFID_NOBUF (~0)
397 /* an input generator's state */
399 int (*iofn)(struct ioarg *, struct io *);
402 char prev; /* previous character read by readc() */
403 char nlcount; /* for `'s */
404 char xchar; /* for `'s */
405 char task; /* reason for pushed IO */
407 //static struct io iostack[NPUSH];
408 #define XOTHER 0 /* none of the below */
409 #define XDOLL 1 /* expanding ${} */
410 #define XGRAVE 2 /* expanding `'s */
411 #define XIO 3 /* file IO */
413 /* in substitution */
414 #define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL)
417 * input generators for IO structure
419 static int nlchar (struct ioarg *ap );
420 static int strchar (struct ioarg *ap );
421 static int qstrchar (struct ioarg *ap );
422 static int filechar (struct ioarg *ap );
423 static int herechar (struct ioarg *ap );
424 static int linechar (struct ioarg *ap );
425 static int gravechar (struct ioarg *ap, struct io *iop );
426 static int qgravechar (struct ioarg *ap, struct io *iop );
427 static int dolchar (struct ioarg *ap );
428 static int wdchar (struct ioarg *ap );
429 static void scraphere (void);
430 static void freehere (int area );
431 static void gethere (void);
432 static void markhere (char *s, struct ioword *iop );
433 static int herein (char *hname, int xdoll );
434 static int run (struct ioarg *argp, int (*f)(struct ioarg *));
439 static int eofc (void);
440 static int readc (void);
441 static void unget (int c );
442 static void ioecho (int c );
443 static void prs (char *s );
444 static void prn (unsigned u );
445 static void closef (int i );
446 static void closeall (void);
451 static void pushio (struct ioarg *argp, int (*f)(struct ioarg *));
452 static int remap (int fd );
453 static int openpipe (int *pv );
454 static void closepipe (int *pv );
455 static struct io *setbase (struct io *ip );
457 static struct ioarg temparg; /* temporary for PUSHIO */
458 #define PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen)))
459 #define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen)))
461 /* -------- word.h -------- */
463 #define NSTART 16 /* default number of words to allow for initially */
468 /* bounds are arbitrary */
472 static struct wdblock *addword (char *wd, struct wdblock *wb );
473 static struct wdblock *newword (int nw );
474 static char **getwords (struct wdblock *wb );
476 /* -------- area.h -------- */
481 static char *getcell (unsigned nbytes );
482 static void garbage (void);
483 static void setarea (char *cp, int a );
484 static int getarea (char *cp );
485 static void freearea (int a );
486 static void freecell (char *cp );
487 static int areanum; /* current allocation area */
489 #define NEW(type) (type *)getcell(sizeof(type))
490 #define DELETE(obj) freecell((char *)obj)
493 /* -------- misc stuff -------- */
495 static int forkexec (struct op *t, int *pin, int *pout, int act, char **wp, int *pforked );
496 static int iosetup (struct ioword *iop, int pipein, int pipeout );
497 static void echo(char **wp );
498 static struct op **find1case (struct op *t, char *w );
499 static struct op *findcase (struct op *t, char *w );
500 static void brkset(struct brkcon *bc );
501 static int dolabel(struct op *t );
502 static int dohelp(struct op *t );
503 static int dochdir(struct op *t );
504 static int doshift(struct op *t );
505 static int dologin(struct op *t );
506 static int doumask(struct op *t );
507 static int doexec(struct op *t );
508 static int dodot(struct op *t );
509 static int dowait(struct op *t );
510 static int doread(struct op *t );
511 static int doeval(struct op *t );
512 static int dotrap(struct op *t );
513 static int getsig(char *s );
514 static void setsig (int n, sighandler_t f);
515 static int getn(char *as );
516 static int dobreak(struct op *t );
517 static int docontinue(struct op *t );
518 static int brkcontin (char *cp, int val );
519 static int doexit(struct op *t );
520 static int doexport(struct op *t );
521 static int doreadonly(struct op *t );
522 static void rdexp (char **wp, void (*f)(struct var *), int key);
523 static void badid(char *s );
524 static int doset(struct op *t );
525 static void varput (char *s, int out );
526 static int dotimes(struct op *t );
527 static int expand (char *cp, struct wdblock **wbp, int f );
528 static char *blank(int f );
529 static int dollar(int quoted );
530 static int grave(int quoted );
531 static void globname (char *we, char *pp );
532 static char *generate (char *start1, char *end1, char *middle, char *end );
533 static int anyspcl(struct wdblock *wb );
534 static int xstrcmp (char *p1, char *p2 );
535 static void glob0 (char *a0, unsigned int a1, int a2, int (*a3)(char *, char *));
536 static void glob1 (char *base, char *lim );
537 static void glob2 (char *i, char *j );
538 static void glob3 (char *i, char *j, char *k );
539 static void readhere (char **name, char *s, int ec );
540 static void pushio (struct ioarg *argp, int (*f)(struct ioarg *));
541 static int xxchar(struct ioarg *ap );
546 struct ioword *h_iop;
550 static char *signame[] = {
553 (char *)NULL, /* interrupt */
555 "Illegal instruction",
559 "Floating Point Exception",
564 (char *)NULL, /* broken pipe */
568 #define NSIGNAL (sizeof(signame)/sizeof(signame[0]))
574 static struct res restab[] = {
600 int (*builtinfunc)(struct op *t);
602 static const struct builtincmd builtincmds[] = {
607 {"continue",docontinue},
611 {"export", doexport},
616 {"readonly",doreadonly},
627 extern char **environ; /* environment pointer */
632 static int interactive; /* Is this an interactive shell */
634 static int multiline; /* \n changed to ; */
635 static struct op *outtree; /* result from parser */
638 static struct brkcon *brklist;
640 static struct wdblock *wdlist;
641 static struct wdblock *iolist;
642 static char *trap[_NSIG+1];
643 static char ourtrap[_NSIG+1];
644 static int trapset; /* trap pending */
645 static int yynerrs; /* yacc */
646 static char line[LINELIM];
647 static struct var *vlist; /* dictionary */
648 static struct var *homedir; /* home directory */
649 static struct var *prompt; /* main prompt */
650 static struct var *cprompt; /* continuation prompt */
651 static struct var *path; /* search path for commands */
652 static struct var *shell; /* shell to interpret command files */
653 static struct var *ifs; /* field separators */
654 static struct ioarg ioargstack[NPUSH];
655 static struct io iostack[NPUSH];
656 static int areanum; /* current allocation area */
659 static char flags['z'-'a'+1];
660 static char *flag = flags-'a';
661 static char *elinep = line+sizeof(line)-5;
662 static char *null = "";
663 static int heedint =1;
664 static struct env e ={line, iostack, iostack-1, (xint *)NULL, FDBASE, (struct env *)NULL};
665 static void (*qflag)(int) = SIG_IGN;
666 static char shellname[] = "/bin/sh";
667 static char search[] = ":/bin:/usr/bin";
671 static int iounit = IODEFAULT;
672 static YYSTYPE yylval;
673 static struct iobuf sharedbuf = {AFID_NOBUF};
674 static struct iobuf mainbuf = {AFID_NOBUF};
675 static unsigned bufid = AFID_ID; /* buffer id counter */
676 static struct ioarg temparg = {0, 0, 0, AFID_NOBUF, 0};
677 static struct here *inhere; /* list of hear docs while parsing */
678 static struct here *acthere; /* list of active here documents */
679 static struct region *areabot; /* bottom of area */
680 static struct region *areatop; /* top of area */
681 static struct region *areanxt; /* starting point of scan */
682 static void * brktop;
683 static void * brkaddr;
686 #ifdef CONFIG_FEATURE_COMMAND_EDITING
687 static char * current_prompt;
691 /* -------- sh.c -------- */
697 extern int msh_main(int argc, char **argv)
703 int (*iof)(struct ioarg *);
706 if ((ap = environ) != NULL) {
708 assign(*ap++, !COPYV);
709 for (ap = environ; *ap;)
710 export(lookup(*ap++));
715 shell = lookup("SHELL");
716 if (shell->value == null)
717 setval(shell, shellname);
720 homedir = lookup("HOME");
721 if (homedir->value == null)
722 setval(homedir, "/");
725 setval(lookup("$"), itoa(getpid(), 5));
727 path = lookup("PATH");
728 if (path->value == null)
729 setval(path, search);
733 if (ifs->value == null)
734 setval(ifs, " \t\n");
736 prompt = lookup("PS1");
737 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
738 if (prompt->value == null)
740 setval(prompt, "$ ");
741 if (geteuid() == 0) {
742 setval(prompt, "# ");
743 prompt->status &= ~EXPORT;
745 cprompt = lookup("PS2");
746 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
747 if (cprompt->value == null)
749 setval(cprompt, "> ");
755 if(argv[0][0] == '-' && argv[0][1] != '\0') {
756 for (s = argv[0]+1; *s; s++)
759 prompt->status &= ~EXPORT;
760 cprompt->status &= ~EXPORT;
765 PUSHIO(aword, *++argv, iof = nlchar);
777 prompt->status &= ~EXPORT;
785 if (*s>='a' && *s<='z')
792 if (iof == filechar && --argc > 0) {
795 prompt->status &= ~EXPORT;
796 cprompt->status &= ~EXPORT;
797 if (newfile(name = *++argv))
802 if (e.iop < iostack) {
803 PUSHIO(afile, 0, iof);
804 if (isatty(0) && isatty(1) && !cflag) {
806 #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
807 printf( "\n\n" BB_BANNER " Built-in shell (msh)\n");
808 printf( "Enter 'help' for a list of built-in commands.\n\n");
812 signal(SIGQUIT, qflag);
813 if (name && name[0] == '-') {
815 if ((f = open(".profile", 0)) >= 0)
817 if ((f = open("/etc/profile", 0)) >= 0)
821 signal(SIGTERM, sig);
822 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
823 signal(SIGINT, onintr);
828 for (ap = ++argv; --argc > 0;) {
829 if (assign(*ap = *argv++, !COPYV)) {
830 dolc--; /* keyword */
836 setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
839 if (interactive && e.iop <= iostack) {
840 #ifdef CONFIG_FEATURE_COMMAND_EDITING
841 current_prompt=prompt->value;
847 /* Ensure that getenv("PATH") stays current */
848 setenv("PATH", path->value, 1);
860 for (c='a'; c<='z'; c++)
864 setval(lookup("-"), m);
873 if (strcmp(s, "-") != 0) {
877 err(": cannot open");
907 setjmp(failpt = m1); /* Bruce Evans' fix */
908 if (setjmp(failpt = m1) || yyparse() || intr) {
912 if (!interactive && intr)
923 execute(outtree, NOPIPE, NOPIPE, 0);
924 if (!interactive && intr) {
928 if ((i = trapset) != 0) {
978 e.iop = e.iobase = iostack;
985 register struct env *ep;
991 ep = (struct env *) space(sizeof(*ep));
1006 register struct env *ep;
1009 if ((ep = e.oenv) != NULL) {
1012 /* should close `'d files */
1014 while (--fd >= e.iofd)
1020 * Is any character from s1 in s2?
1024 register char *s1, *s2;
1033 * Is character c in s?
1050 return(itoa(n, -1));
1055 register unsigned u;
1063 if (n < 0 && (int) u < 0) {
1072 } while (--n > 0 || u);
1081 PUSHIO(afile, f, filechar);
1086 int s; /* ANSI C requires a parameter */
1088 signal(SIGINT, onintr);
1108 if ((cp = getcell(n)) == 0)
1109 err("out of string space");
1118 register char *cp, *xp;
1120 if ((cp = space(strlen(s)+1)) != NULL) {
1121 setarea((char *)cp, a);
1122 for (xp = cp; (*xp++ = *s++) != '\0';)
1140 static void runtrap(i)
1145 if ((trapstr = trap[i]) == NULL)
1149 RUN(aword, trapstr, nlchar);
1152 /* -------- var.c -------- */
1155 * Find the given name in the dictionary
1156 * and return its value. If the name was
1157 * not previously there, enter it now and
1158 * return a null value.
1164 register struct var *vp;
1167 static struct var dummy;
1171 for (c = 0; isdigit(*n) && c < 1000; n++)
1173 dummy.status = RONLY;
1174 dummy.value = c <= dolc? dolv[c]: null;
1177 for (vp = vlist; vp; vp = vp->next)
1178 if (eqname(vp->name, n))
1181 vp = (struct var *)space(sizeof(*vp));
1182 if (vp == 0 || (vp->name = space((int)(cp-n)+2)) == 0) {
1183 dummy.name = dummy.value = "";
1186 for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++)
1191 setarea((char *)vp, 0);
1192 setarea((char *)vp->name, 0);
1195 vp->status = GETCELL;
1201 * give variable at `vp' the value `val'.
1208 nameval(vp, val, (char *)NULL);
1212 * if name is not NULL, it must be
1213 * a prefix of the space `val',
1215 * this is all so that exporting
1216 * values is reasonably painless.
1219 nameval(vp, val, name)
1220 register struct var *vp;
1223 register char *cp, *xp;
1227 if (vp->status & RONLY) {
1228 for (xp = vp->name; *xp && *xp != '=';)
1229 putc(*xp++, stderr);
1230 err(" is read-only");
1235 xp = space(strlen(vp->name)+strlen(val)+2);
1238 /* make string: name=value */
1239 setarea((char *)xp, 0);
1241 for (cp = vp->name; (*xp = *cp++) && *xp!='='; xp++)
1246 for (cp = val; (*xp++ = *cp++) != '\0';)
1251 if (vp->status & GETCELL)
1252 freecell(vp->name); /* form new string `name=value' */
1262 vp->status |= EXPORT;
1269 if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */
1270 vp->status |= RONLY;
1277 if (!isalpha((int)*s) && *s != '_')
1279 for (; *s != '='; s++)
1280 if (*s == 0 || (!isalnum(*s) && *s != '_'))
1293 if (!isalpha(*s) && *s != '_')
1295 for (cp = s; *cp != '='; cp++)
1296 if (*cp == 0 || (!isalnum(*cp) && *cp != '_'))
1299 nameval(vp, ++cp, cf == COPYV? (char *)NULL: s);
1301 vp->status &= ~GETCELL;
1309 if (!isalpha(*cp++) && *(cp-1) != '_')
1312 if (!isalnum(*cp++) && *(cp-1) != '_')
1319 register int f, out;
1321 register struct var *vp;
1323 for (vp = vlist; vp; vp = vp->next)
1324 if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) {
1325 if (vp->status & EXPORT)
1326 write(out, "export ", 7);
1327 if (vp->status & RONLY)
1328 write(out, "readonly ", 9);
1329 write(out, vp->name, (int)(findeq(vp->name) - vp->name));
1330 write(out, "\n", 1);
1336 register char *n1, *n2;
1338 for (; *n1 != '=' && *n1 != 0; n1++)
1341 return(*n2 == 0 || *n2 == '=');
1348 while (*cp != '\0' && *cp != '=')
1353 /* -------- gmatch.c -------- */
1355 * int gmatch(string, pattern)
1356 * char *string, *pattern;
1358 * Match a pattern as in sh(1).
1363 #define QMASK (CMASK&~QUOTE)
1364 #define NOT '!' /* might use ^ */
1368 register char *s, *p;
1370 register int sc, pc;
1372 if (s == NULL || p == NULL)
1374 while ((pc = *p++ & CMASK) != '\0') {
1378 if ((p = cclass(p, sc)) == NULL)
1390 if (*p == '\0' || gmatch(s, p))
1392 } while (*s++ != '\0');
1396 if (sc != (pc&~QUOTE))
1408 register int c, d, not, found;
1410 if ((not = *p == NOT) != 0)
1415 return((char *)NULL);
1417 if (p[1] == '-' && p[2] != ']') {
1422 if (c == sub || (c <= sub && sub <= d))
1424 } while (*++p != ']');
1425 return(found? p+1: (char *)NULL);
1429 /* -------- area.c -------- */
1432 * All memory between (char *)areabot and (char *)(areatop+1) is
1433 * exclusively administered by the area management routines.
1434 * It is assumed that sbrk() and brk() manipulate the high end.
1437 #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;})
1442 brkaddr = malloc(65000);
1443 brktop = brkaddr + 65000;
1445 while ((int)sbrk(0) & ALIGN)
1447 areabot = (struct region *)sbrk(REGSIZE);
1449 areabot->next = areabot;
1450 areabot->area = BUSY;
1459 register int nregio;
1460 register struct region *p, *q;
1466 } /* silly and defeats the algorithm */
1468 * round upwards and add administration area
1470 nregio = (nbytes+(REGSIZE-1))/REGSIZE + 1;
1471 for (p = areanxt;;) {
1472 if (p->area > areanum) {
1476 while ((q = p->next)->area > areanum && q != areanxt)
1479 * exit loop if cell big enough
1481 if (q >= p + nregio)
1488 i = nregio >= GROWBY ? nregio : GROWBY;
1489 p = (struct region *)sbrk(i * REGSIZE);
1490 if (p == (struct region *)-1)
1491 return((char *)NULL);
1495 abort(); /* allocated areas are contiguous */
1505 * we found a FREE area big enough, pointed to by 'p', and up to 'q'
1507 areanxt = p + nregio;
1510 * split into requested area and rest
1512 if (areanxt+1 > q) {
1514 abort(); /* insufficient space left for admin */
1517 areanxt->area = FREE;
1521 return((char *)(p+1));
1528 register struct region *p;
1530 if ((p = (struct region *)cp) != NULL) {
1542 register struct region *p, *top;
1545 for (p = areabot; p != top; p = p->next)
1555 register struct region *p;
1557 if ((p = (struct region *)cp) != NULL)
1565 return ((struct region*)cp-1)->area;
1571 register struct region *p, *q, *top;
1574 for (p = areabot; p != top; p = p->next) {
1575 if (p->area > areanum) {
1576 while ((q = p->next)->area > areanum)
1582 if (areatop >= q + SHRINKBY && q->area > areanum) {
1591 /* -------- csyn.c -------- */
1593 * shell: syntax (C version)
1612 register struct op *t, *p;
1617 while ((c = yylex(0)) == '|') {
1618 if ((p = command(CONTIN)) == NULL)
1620 if (t->type != TPAREN && t->type != TCOM) {
1621 /* shell statement */
1622 t = block(TPAREN, t, NOBLOCK, NOWORDS);
1624 t = block(TPIPE, t, p, NOWORDS);
1634 register struct op *t, *p;
1639 while ((c = yylex(0)) == LOGAND || c == LOGOR) {
1640 if ((p = pipeline(CONTIN)) == NULL)
1642 t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS);
1652 register struct op *t, *p;
1657 if((peeksym = yylex(0)) == '&')
1658 t = block(TASYNC, t, NOBLOCK, NOWORDS);
1659 while ((c = yylex(0)) == ';' || c == '&' || (multiline && c == '\n')) {
1660 if ((p = andor()) == NULL)
1662 if((peeksym = yylex(0)) == '&')
1663 p = block(TASYNC, p, NOBLOCK, NOWORDS);
1676 register struct ioword *iop;
1680 if ((c = yylex(cf)) != '<' && c != '>') {
1686 iop = io(iounit, i, yylval.cp);
1689 markhere(yylval.cp, iop);
1697 if ((peeksym = yylex(cf)) != c)
1705 register struct op *t;
1709 switch (peeksym = yylex(0)) {
1734 register struct op *t;
1740 return(block(type, t, NOBLOCK, NOWORDS));
1747 register struct op *t;
1748 struct wdblock *iosave;
1757 switch (c = yylex(cf)) {
1760 if ((t = simple()) == NULL) {
1762 return((struct op *)NULL);
1769 t = nested(TPAREN, ')');
1773 t = nested(TBRACE, '}');
1783 t->words = wordlist();
1784 if ((c = yylex(0)) != '\n' && c != ';')
1786 t->left = dogroup(0);
1794 t->type = c == WHILE? TWHILE: TUNTIL;
1796 t->right = dogroup(1);
1808 musthave(IN, CONTIN);
1810 t->left = caselist();
1820 t->right = thenpart();
1837 register struct op *mylist;
1840 if (c == DONE && onlydone)
1841 return((struct op *)NULL);
1853 register struct op *t;
1855 if ((c = yylex(0)) != THEN) {
1857 return((struct op *)NULL);
1862 if (t->left == NULL)
1864 t->right = elsepart();
1872 register struct op *t;
1874 switch (c = yylex(0)) {
1876 if ((t = c_list()) == NULL)
1884 t->right = thenpart();
1889 return((struct op *)NULL);
1896 register struct op *t;
1899 while ((peeksym = yylex(CONTIN)) != ESAC)
1900 t = list(t, casepart());
1907 register struct op *t;
1911 t->words = pattern();
1914 if ((peeksym = yylex(CONTIN)) != ESAC)
1915 musthave(BREAK, CONTIN);
1929 } while ((c = yylex(0)) == '|');
1940 if ((c = yylex(0)) != IN) {
1942 return((char **)NULL);
1945 while ((c = yylex(0)) == WORD)
1953 * supporting functions
1957 register struct op *t1, *t2;
1963 return(block(TLIST, t1, t2, NOWORDS));
1967 block(type, t1, t2, wp)
1972 register struct op *t;
1986 register struct res *rp;
1988 for (rp = restab; rp->r_name; rp++)
1989 if (strcmp(rp->r_name, n) == 0)
1997 register struct op *t;
1999 t = (struct op *)tree(sizeof(*t));
2011 register struct op *t;
2014 iolist = addword((char *)NULL, iolist);
2015 t->ioact = copyio();
2018 if (t->type != TCOM) {
2019 if (t->type != TPAREN && t->ioact != NULL) {
2020 t = block(TPAREN, t, NOBLOCK, NOWORDS);
2021 t->ioact = t->left->ioact;
2022 t->left->ioact = NULL;
2036 wd = getwords(wdlist);
2045 wdlist = addword(cp, wdlist);
2048 static struct ioword **
2051 register struct ioword **iop;
2053 iop = (struct ioword **) getwords(iolist);
2058 static struct ioword *
2064 register struct ioword *iop;
2066 iop = (struct ioword *) tree(sizeof(*iop));
2070 iolist = addword((char *)iop, iolist);
2077 yyerror("syntax error");
2085 if (interactive && e.iop <= iostack) {
2087 while (eofc() == 0 && yylex(0) != '\n')
2101 if ((c = peeksym) > 0) {
2114 while ((c = my_getc(0)) == ' ' || c == '\t')
2118 if (any(c, "0123456789")) {
2119 unget(c1 = my_getc(0));
2120 if (c1 == '<' || c1 == '>') {
2130 while ((c = my_getc(0)) != 0 && c != '\n')
2140 if ((c = my_getc(0)) == '{') {
2141 if ((c = collect(c, '}')) != '\0')
2150 if ((c = collect(c, c)) != '\0')
2157 if ((c1 = dual(c)) != '\0') {
2175 if (multiline || cf & CONTIN) {
2176 if (interactive && e.iop <= iostack) {
2177 #ifdef CONFIG_FEATURE_COMMAND_EDITING
2178 current_prompt=cprompt->value;
2180 prs(cprompt->value);
2197 while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n"))
2198 if (e.linep >= elinep)
2199 err("word too long");
2206 if (atstart && (c = rlookup(line))!=0) {
2210 yylval.cp = strsave(line, areanum);
2221 while ((c = my_getc(c1)) != c1) {
2226 prs("no closing "); yyerror(s);
2229 if (interactive && c == '\n' && e.iop <= iostack) {
2230 #ifdef CONFIG_FEATURE_COMMAND_EDITING
2231 current_prompt=cprompt->value;
2233 prs(cprompt->value);
2247 register char *cp = s;
2252 if ((c = rlookup(s)) == 0)
2264 if (c == '>' || c == '<') {
2267 yylval.i = ec == '>'? IOWRITE|IOCAT: IOHERE;
2270 yylval.i = ec == '>'? IOWRITE: IOREAD;
2271 if (c != '&' || yylval.i == IOHERE)
2283 if ((t = getcell(size)) == NULL) {
2284 prs("command line too complicated\n");
2294 /* -------- exec.c -------- */
2302 execute(t, pin, pout, act)
2303 register struct op *t;
2307 register struct op *t1;
2308 volatile int i, rv, a;
2309 char *cp, **wp, **wp2;
2314 /* Avoid longjmp clobbering */
2323 wp = (wp2 = t->words) != NULL
2324 ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
2332 rv = forkexec(t, pin, pout, act, wp, &child);
2343 if ((rv = openpipe(pv)) < 0)
2345 pv[0] = remap(pv[0]);
2346 pv[1] = remap(pv[1]);
2347 (void) execute(t->left, pin, pv, 0);
2348 rv = execute(t->right, pv, pout, 0);
2353 (void) execute(t->left, pin, pout, 0);
2354 rv = execute(t->right, pin, pout, 0);
2359 int hinteractive = interactive;
2363 interactive = hinteractive;
2365 setval(lookup("!"), putn(i));
2376 signal(SIGINT, SIG_IGN);
2377 signal(SIGQUIT, SIG_IGN);
2379 signal(SIGTERM, SIG_DFL);
2383 open("/dev/null", 0);
2385 exit(execute(t->left, pin, pout, FEXEC));
2392 rv = execute(t->left, pin, pout, 0);
2393 if ((t1 = t->right)!=NULL && (rv == 0) == (t->type == TAND))
2394 rv = execute(t1, pin, pout, 0);
2404 while (*wp++ != NULL)
2407 vp = lookup(t->str);
2408 while (setjmp(bc.brkpt))
2412 for (t1 = t->left; i-- && *wp != NULL;) {
2414 rv = execute(t1, pin, pout, 0);
2416 brklist = brklist->nextlev;
2421 while (setjmp(bc.brkpt))
2426 while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE))
2427 rv = execute(t->right, pin, pout, 0);
2428 brklist = brklist->nextlev;
2433 if (t->right != NULL) {
2434 rv = !execute(t->left, pin, pout, 0) ?
2435 execute(t->right->left, pin, pout, 0):
2436 execute(t->right->right, pin, pout, 0);
2441 if ((cp = evalstr(t->str, DOSUB|DOTRIM)) == 0)
2443 if ((t1 = findcase(t->left, cp)) != NULL)
2444 rv = execute(t1, pin, pout, 0);
2449 if (iopp = t->ioact)
2451 if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
2456 if (rv >= 0 && (t1 = t->left))
2457 rv = execute(t1, pin, pout, 0);
2467 if (interactive && intr) {
2471 if ((i = trapset) != 0) {
2479 forkexec( register struct op *t, int *pin, int *pout, int act, char **wp, int *pforked)
2482 int (*shcom)(struct op *) = NULL;
2485 struct ioword **iopp;
2495 struct brkcon * hbrklist;
2499 /* Avoid longjmp clobbering */
2512 rv = -1; /* system-detected error */
2513 if (t->type == TCOM) {
2514 while ((cp = *wp++) != NULL)
2518 /* strip all initial assignments */
2519 /* not correct wrt PATH=yyy command etc */
2521 echo (cp ? wp: owp);
2522 if (cp == NULL && t->ioact == NULL) {
2523 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2525 return(setstatus(0));
2527 else if (cp != NULL)
2528 shcom = inbuilt(cp);
2532 if (shcom == NULL && (f & FEXEC) == 0) {
2538 hinteractive = interactive;
2545 /* who wrote this crappy non vfork safe shit? */
2550 interactive = hinteractive;
2560 return(pout==NULL? setstatus(waitfor(i,0)): 0);
2564 signal(SIGINT, SIG_IGN);
2565 signal(SIGQUIT, SIG_IGN);
2575 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2579 if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) {
2580 err("piping to/from shell builtins not yet done");
2592 if ((iopp = t->ioact) != NULL) {
2593 if (shcom != NULL && shcom != doexec) {
2595 err(": cannot redirect shell command");
2599 if (iosetup(*iopp++, pin!=NULL, pout!=NULL))
2603 return(setstatus((*shcom)(t)));
2604 /* should use FIOCEXCL */
2605 for (i=FDBASE; i<NOFILE; i++)
2608 signal(SIGINT, SIG_DFL);
2609 signal(SIGQUIT, SIG_DFL);
2611 if (t->type == TPAREN)
2612 exit(execute(t->left, NOPIPE, NOPIPE, FEXEC));
2616 cp = rexecve(wp[0], wp, makenv());
2617 prs(wp[0]); prs(": "); warn(cp);
2626 * 0< 1> are ignored as required
2630 iosetup(iop, pipein, pipeout)
2631 register struct ioword *iop;
2632 int pipein, pipeout;
2634 register int u = -1;
2635 char *cp=NULL, *msg;
2637 if (iop->io_unit == IODEFAULT) /* take default */
2638 iop->io_unit = iop->io_flag&(IOREAD|IOHERE)? 0: 1;
2639 if (pipein && iop->io_unit == 0)
2641 if (pipeout && iop->io_unit == 1)
2643 msg = iop->io_flag&(IOREAD|IOHERE)? "open": "create";
2644 if ((iop->io_flag & IOHERE) == 0) {
2646 if ((cp = evalstr(cp, DOSUB|DOTRIM)) == NULL)
2649 if (iop->io_flag & IODUP) {
2650 if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
2652 err(": illegal >& argument");
2656 iop->io_flag = IOCLOSE;
2657 iop->io_flag &= ~(IOREAD|IOWRITE);
2659 switch (iop->io_flag) {
2665 case IOHERE|IOXHERE:
2666 u = herein(iop->io_name, iop->io_flag&IOXHERE);
2671 if ((u = open(cp, 1)) >= 0) {
2672 lseek(u, (long)0, 2);
2676 u = creat(cp, 0666);
2680 u = dup2(*cp-'0', iop->io_unit);
2684 close(iop->io_unit);
2693 if (u != iop->io_unit) {
2694 dup2(u, iop->io_unit);
2708 for (i=0; wp[i]; i++) {
2721 register struct op *t1;
2723 register char **wp, *cp;
2726 return((struct op **)NULL);
2727 if (t->type == TLIST) {
2728 if ((tp = find1case(t->left, w)) != NULL)
2730 t1 = t->right; /* TPAT */
2733 for (wp = t1->words; *wp;)
2734 if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp))
2736 return((struct op **)NULL);
2744 register struct op **tp;
2746 return((tp = find1case(t, w)) != NULL? *tp: (struct op *)NULL);
2750 * Enter a new loop level (marked for break/continue).
2756 bc->nextlev = brklist;
2761 * Wait for the last process created.
2762 * Print a message for each process found
2763 * that was killed by a signal.
2764 * Ignore interrupt signals while waiting
2765 * unless `canintr' is true.
2768 waitfor(lastpid, canintr)
2769 register int lastpid;
2772 register int pid, rv;
2774 int oheedint = heedint;
2781 if (errno != EINTR || canintr)
2784 if ((rv = WAITSIG(s)) != 0) {
2786 if (signame[rv] != NULL) {
2787 if (pid != lastpid) {
2794 if (pid != lastpid) {
2798 prs("Signal "); prn(rv); prs(" ");
2801 prs(" - core dumped");
2802 if (rv >= NSIGNAL || signame[rv])
2808 } while (pid != lastpid);
2815 if (exstat == 0) exstat = rv;
2827 setval(lookup("?"), putn(s));
2832 * PATH-searching interface to execve.
2833 * If getenv("PATH") were kept up-to-date,
2834 * execvp might be used.
2838 char *c, **v, **envp;
2841 register char *sp, *tp;
2842 int eacces = 0, asis = 0;
2844 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
2846 #ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN
2847 name = get_last_path_component(name);
2850 if (find_applet_by_name(name)) {
2851 /* We have to exec here since we vforked. Running
2852 * run_applet_by_name() won't work and bad things
2854 execve("/proc/self/exe", v, envp);
2855 execve("busybox", v, envp);
2859 sp = any('/', c)? "": path->value;
2861 while (asis || *sp != '\0') {
2864 for (; *sp != '\0'; tp++)
2865 if ((*tp = *sp++) == ':') {
2871 for (i = 0; (*tp++ = c[i++]) != '\0';)
2874 execve(e.linep, v, envp);
2880 execve("/bin/sh", v, envp);
2885 return("program too big");
2888 return("argument list too long");
2895 return(errno==ENOENT ? "not found" : "cannot execute");
2899 * Run the command produced by generator `f'
2900 * applied to stream `arg'.
2903 run(struct ioarg *argp, int (*f)(struct ioarg *))
2906 struct wdblock *swdlist;
2907 struct wdblock *siolist;
2913 /* Avoid longjmp clobbering */
2923 if (newenv(setjmp(errpt = ev)) == 0) {
2929 if (setjmp(failpt = rt) == 0 && yyparse() == 0)
2930 rv = execute(outtree, NOPIPE, NOPIPE, 0);
2937 freearea(areanum--);
2941 /* -------- do.c -------- */
2944 * built-in commands: doX
2947 static int dohelp(struct op *t )
2950 const struct builtincmd *x;
2952 printf("\nBuilt-in commands:\n");
2953 printf("-------------------\n");
2955 for (col=0, x = builtincmds; x->builtinfunc != NULL; x++) {
2958 col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name);
2964 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
2967 const struct BB_applet *applet;
2968 extern const struct BB_applet applets[];
2969 extern const size_t NUM_APPLETS;
2971 for (i=0, applet = applets; i < NUM_APPLETS; applet++, i++) {
2975 col += printf("%s%s", ((col == 0) ? "\t" : " "),
2985 return EXIT_SUCCESS;
2990 static int dolabel(struct op *t )
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, sighandler_t f)
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);
3328 static void rdexp (char **wp, void (*f)(struct var *), int key)
3331 for (; *wp != NULL; wp++) {
3332 if (isassign(*wp)) {
3335 for (cp = *wp; *cp != '='; cp++)
3353 err(": bad identifier");
3358 register struct op *t;
3360 register struct var *vp;
3364 if ((cp = t->words[1]) == NULL) {
3365 for (vp = vlist; vp; vp = vp->next)
3366 varput(vp->name, 1);
3370 /* bad: t->words++; */
3371 for(n = 0; (t->words[n]=t->words[n+1]) != NULL; n++)
3374 flag['x'] = flag['v'] = 0;
3384 if (*cp>='a' && *cp<='z')
3391 t->words[0] = dolv[0];
3392 for (n=1; t->words[n]; n++)
3393 setarea((char *)t->words[n], 0);
3396 setval(lookup("#"), putn(dolc));
3397 setarea((char *)(dolv-1), 0);
3407 if (isalnum(*s) || *s == '_') {
3408 write(out, s, strlen(s));
3409 write(out, "\n", 1);
3415 * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
3416 * This file contains code for the times builtin.
3418 static int dotimes(struct op *t )
3421 long int clk_tck = sysconf(_SC_CLK_TCK);
3424 printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
3425 (int) (buf.tms_utime / clk_tck / 60),
3426 ((double) buf.tms_utime) / clk_tck,
3427 (int) (buf.tms_stime / clk_tck / 60),
3428 ((double) buf.tms_stime) / clk_tck,
3429 (int) (buf.tms_cutime / clk_tck / 60),
3430 ((double) buf.tms_cutime) / clk_tck,
3431 (int) (buf.tms_cstime / clk_tck / 60),
3432 ((double) buf.tms_cstime) / clk_tck);
3437 static int(*inbuilt(char *s))(struct op *)
3439 const struct builtincmd *bp;
3441 for (bp = builtincmds; bp->name != NULL; bp++)
3442 if (strcmp(bp->name, s) == 0)
3443 return(bp->builtinfunc);
3448 /* -------- eval.c -------- */
3453 * blank interpretation
3458 static char ** eval( char **ap, int f)
3466 /* Avoid longjmp clobbering */
3473 if (newenv(setjmp(errpt = ev)) == 0) {
3474 while (*ap && isassign(*ap))
3475 expand(*ap++, &wb, f & ~DOGLOB);
3477 for (wf = ap; *wf; wf++) {
3479 expand(*wf, &wb, f & ~DOGLOB);
3482 for (wb = addword((char *)0, wb); *ap; ap++) {
3483 if (!flag['k'] || !isassign(*ap))
3484 expand(*ap, &wb, f & ~DOKEY);
3486 wb = addword((char *)0, wb);
3491 return(gflg? (char **)NULL: wp);
3495 * Make the exported environment from the exported
3496 * names in the dictionary. Keyword assignments
3497 * will already have been done.
3503 register struct wdblock *wb;
3504 register struct var *vp;
3507 for (vp = vlist; vp; vp = vp->next)
3508 if (vp->status & EXPORT)
3509 wb = addword(vp->name, wb);
3510 wb = addword((char *)0, wb);
3511 return(getwords(wb));
3522 if (expand(cp, &wb, f)) {
3523 if (wb == NULL || wb->w_nword == 0 || (cp = wb->w_words[0]) == NULL)
3532 expand( char *cp, register struct wdblock **wbp, int f)
3537 /* Avoid longjmp clobbering */
3543 if (!anys("$`'\"", cp) &&
3544 !anys(ifs->value, cp) &&
3545 ((f&DOGLOB)==0 || !anys("[*?", cp))) {
3546 cp = strsave(cp, areanum);
3549 *wbp = addword(cp, *wbp);
3552 if (newenv(setjmp(errpt = ev)) == 0) {
3553 PUSHIO(aword, cp, strchar);
3555 while ((cp = blank(f)) && gflg == 0) {
3557 cp = strsave(cp, areanum);
3558 if ((f&DOGLOB) == 0) {
3561 *wbp = addword(cp, *wbp);
3563 *wbp = glob(cp, *wbp);
3572 * Blank interpretation and quoting
3580 int scanequals, foundequals;
3583 scanequals = f & DOKEY;
3587 switch (c = subgetc('"', foundequals)) {
3595 if (f & DOBLANK && any(c, ifs->value))
3604 for (c1 = c; (c = subgetc(c1, 1)) != c1;) {
3607 if (c == '\'' || !any(c, "$`\""))
3614 if (!isalpha(c) && c != '_')
3617 c = subgetc('"', foundequals);
3619 f & (DOBLANK && any(c, ifs->value)) ||
3620 (!INSUB() && any(c, "\"'"))) {
3632 else if (!isalnum(c) && c != '_')
3642 * Get characters, substituting for ` and $
3653 if (!INSUB() && ec != '\'') {
3655 if (grave(quoted) == 0)
3657 e.iop->task = XGRAVE;
3660 if (c == '$' && (c = dollar(quoted)) == 0) {
3661 e.iop->task = XDOLL;
3669 * Prepare to generate the string returned by ${} substitution.
3678 register char *s, c, *cp=NULL;
3685 if (isalpha(c) || c == '_') {
3686 while ((c = readc())!=0 && (isalnum(c) || c == '_'))
3687 if (e.linep < elinep)
3694 otask = e.iop->task;
3695 e.iop->task = XOTHER;
3696 while ((c = subgetc('"', 0))!=0 && c!='}' && c!='\n')
3697 if (e.linep < elinep)
3700 e.iop->task = otask;
3707 if (e.linep >= elinep) {
3708 err("string in ${} too long");
3714 for (cp = s+1; *cp; cp++)
3715 if (any(*cp, "=-+?")) {
3720 if (s[1] == 0 && (*s == '*' || *s == '@')) {
3722 /* currently this does not distinguish $* and $@ */
3723 /* should check dollar */
3725 PUSHIO(awordlist, dolv+1, dolchar);
3727 } else { /* trap the nasty ${=} */
3733 if ((dolp = vp->value) == null) {
3737 err("cannot use ${...=...} with $n");
3746 dolp = strsave(cp, areanum);
3751 prs("missing value for ");
3758 } else if (c == '+')
3759 dolp = strsave(cp, areanum);
3760 if (flag['u'] && dolp == null) {
3761 prs("unset variable: ");
3766 PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
3771 * Run the command in `...` and read its output.
3782 /* Avoid longjmp clobbering */
3785 for (cp = e.iop->argp->aword; *cp != '`'; cp++)
3787 err("no closing `");
3790 if (openpipe(pf) < 0)
3792 if ((i = vfork()) == -1) {
3798 e.iop->argp->aword = ++cp;
3800 PUSHIO(afile, remap(pf[0]), (int(*)(struct ioarg *))((quoted)? qgravechar: gravechar));
3804 /* allow trapped signals */
3805 for (i=0; i<=_NSIG; i++)
3806 if (ourtrap[i] && signal(i, SIG_IGN) != SIG_IGN)
3813 cp = strsave(e.iop->argp->aword, 0);
3816 freearea(areanum); /* free old space */
3818 e.iop = (e.iobase = iostack) - 1;
3821 PUSHIO(aword, cp, nlchar);
3832 if ((s = as) != NULL)
3838 /* -------- glob.c -------- */
3844 #define scopy(x) strsave((x), areanum)
3846 #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
3848 static struct wdblock *cl, *nl;
3849 static char spcl[] = "[?*";
3851 static struct wdblock *
3862 for (pp = cp; *pp; pp++)
3865 else if (!any(*pp & ~QUOTE, spcl))
3868 for (cl = addword(scopy(cp), (struct wdblock *)0); anyspcl(cl); cl = nl) {
3869 nl = newword(cl->w_nword*2);
3870 for(i=0; i<cl->w_nword; i++) { /* for each argument */
3871 for (pp = cl->w_words[i]; *pp; pp++)
3872 if (any(*pp, spcl)) {
3873 globname(cl->w_words[i], pp);
3877 nl = addword(scopy(cl->w_words[i]), nl);
3879 for(i=0; i<cl->w_nword; i++)
3880 DELETE(cl->w_words[i]);
3883 for(i=0; i<cl->w_nword; i++)
3884 unquote(cl->w_words[i]);
3885 glob0((char *)cl->w_words, cl->w_nword, sizeof(char *), xstrcmp);
3887 for (i=0; i<cl->w_nword; i++)
3888 wb = addword(cl->w_words[i], wb);
3893 wb = addword(unquote(cp), wb);
3902 register char *np, *cp;
3903 char *name, *gp, *dp;
3907 char dname[NAME_MAX+1];
3910 for (np = we; np != pp; pp--)
3913 for (dp = cp = space((int)(pp-np)+3); np < pp;)
3917 for (gp = cp = space(strlen(pp)+1); *np && *np != '/';)
3926 dname[NAME_MAX] = '\0';
3927 while ((de=readdir(dirp))!=NULL) {
3928 /* XXX Hmmm... What this could be? (abial) */
3930 if (ent[j].d_ino == 0)
3933 strncpy(dname, de->d_name, NAME_MAX);
3934 if (dname[0] == '.')
3937 for(k=0; k<NAME_MAX; k++)
3938 if (any(dname[k], spcl))
3940 if (gmatch(dname, gp)) {
3941 name = generate(we, pp, dname, np);
3942 if (*np && !anys(np, spcl)) {
3943 if (stat(name,&dbuf)) {
3948 nl = addword(name, nl);
3957 * generate a pathname as below.
3958 * start..end1 / middle end
3959 * the slashes come for free
3962 generate(start1, end1, middle, end)
3964 register char *end1;
3968 register char *op, *xp;
3970 p = op = space((int)(end1-start1)+strlen(middle)+strlen(end)+2);
3971 for (xp = start1; xp != end1;)
3973 for (xp = middle; (*op++ = *xp++) != '\0';)
3976 for (xp = end; (*op++ = *xp++) != '\0';)
3983 register struct wdblock *wb;
3989 for (i=0; i<wb->w_nword; i++)
3990 if (anys(spcl, *wd++))
3999 return(strcmp(*(char **)p1, *(char **)p2));
4002 /* -------- word.c -------- */
4004 static struct wdblock *
4008 register struct wdblock *wb;
4010 wb = (struct wdblock *) space(sizeof(*wb) + nw*sizeof(char *));
4016 static struct wdblock *
4019 register struct wdblock *wb;
4021 register struct wdblock *wb2;
4025 wb = newword(NSTART);
4026 if ((nw = wb->w_nword) >= wb->w_bsize) {
4027 wb2 = newword(nw * 2);
4028 memcpy((char *)wb2->w_words, (char *)wb->w_words, nw*sizeof(char *));
4033 wb->w_words[wb->w_nword++] = wd;
4039 register struct wdblock *wb;
4045 return((char **)NULL);
4046 if (wb->w_nword == 0) {
4048 return((char **)NULL);
4050 wd = (char **) space(nb = sizeof(*wd) * wb->w_nword);
4051 memcpy((char *)wd, (char *)wb->w_words, nb);
4052 DELETE(wb); /* perhaps should done by caller */
4056 int (*func)(char *, char *);
4060 glob0(a0, a1, a2, a3)
4064 int (*a3) (char *, char *);
4068 glob1(a0, a0 + a1 * a2);
4075 register char *i, *j;
4085 if ((n=(int)(lim-base)) <= v2)
4087 n = v2 * (n / (2*v2));
4088 hptr = lptr = base+n;
4093 if ((c = (*func)(i, lptr)) == 0) {
4094 glob2(i, lptr -= v2);
4105 if ((c = (*func)(hptr, j)) == 0) {
4106 glob2(hptr += v2, j);
4111 glob3(i, hptr += v2, j);
4126 if (lptr-base >= lim-hptr) {
4127 glob1(hptr+v2, lim);
4137 glob3(j, lptr -= v2, i);
4146 register char *index1, *index2, c;
4154 *index1++ = *index2;
4163 register char *index1, *index2, *index3;
4173 *index1++ = *index3;
4174 *index3++ = *index2;
4179 /* -------- io.c -------- */
4185 static int my_getc( int ec)
4189 if(e.linep > elinep) {
4190 while((c=readc()) != '\n' && c)
4192 err("input line too long");
4197 if (ec != '\'' && e.iop->task != XGRAVE) {
4200 if (c == '\n' && ec != '\"')
4201 return(my_getc(ec));
4212 if (e.iop >= e.iobase)
4220 return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0);
4228 for (; e.iop >= e.iobase; e.iop--)
4229 if ((c = e.iop->peekc) != '\0') {
4234 if (e.iop->prev != 0) {
4235 if ((c = (*e.iop->iofn)(e.iop->argp, e.iop)) != '\0') {
4240 if (e.iop == iostack)
4242 return(e.iop->prev = c);
4244 else if (e.iop->task == XIO && e.iop->prev != '\n') {
4246 if (e.iop == iostack)
4251 if (e.iop->task == XIO) {
4253 return e.iop->prev = 0;
4254 if (interactive && e.iop == iostack+1) {
4255 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4256 current_prompt=prompt->value;
4263 if (e.iop >= iostack)
4275 write(2, &c, sizeof c);
4279 pushio(struct ioarg *argp, int (*fn)(struct ioarg *))
4281 if (++e.iop >= &iostack[NPUSH]) {
4283 err("Shell input nested too deeply");
4287 e.iop->iofn = (int (*)(struct ioarg *, struct io *))fn;
4289 if (argp->afid != AFID_NOBUF)
4292 e.iop->argp = ioargstack + (e.iop - iostack);
4293 *e.iop->argp = *argp;
4294 e.iop->argp->afbuf = e.iop == &iostack[0] ? &mainbuf : &sharedbuf;
4295 if (isatty(e.iop->argp->afile) == 0 &&
4296 (e.iop == &iostack[0] ||
4297 lseek(e.iop->argp->afile, 0L, 1) != -1)) {
4298 if (++bufid == AFID_NOBUF)
4300 e.iop->argp->afid = bufid;
4304 e.iop->prev = ~'\n';
4308 if (fn == filechar || fn == linechar)
4310 else if (fn == (int(*)(struct ioarg *))gravechar || fn == (int(*)(struct ioarg *))qgravechar)
4311 e.iop->task = XGRAVE;
4313 e.iop->task = XOTHER;
4320 register struct io *xp;
4328 * Input generating functions
4332 * Produce the characters of a string, then a newline, then EOF.
4336 register struct ioarg *ap;
4340 if (ap->aword == NULL)
4342 if ((c = *ap->aword++) == 0) {
4350 * Given a list of words, produce the characters
4351 * in them, with a space after each word.
4355 register struct ioarg *ap;
4360 if ((wl = ap->awordlist) == NULL)
4363 if ((c = *(*wl)++) != 0)
4368 ap->awordlist = NULL;
4373 * Return the characters of a list of words,
4374 * producing a space between them.
4378 register struct ioarg *ap;
4382 if ((wp = *ap->awordlist++) != NULL) {
4383 PUSHIO(aword, wp, *ap->awordlist == NULL? strchar: xxchar);
4391 register struct ioarg *ap;
4395 if (ap->aword == NULL)
4397 if ((c = *ap->aword++) == '\0') {
4405 * Produce the characters from a single word (string).
4409 register struct ioarg *ap;
4413 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4419 * Produce quoted characters from a single word (string).
4423 register struct ioarg *ap;
4427 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4433 * Return the characters from a file.
4437 register struct ioarg *ap;
4441 struct iobuf *bp = ap->afbuf;
4443 if (ap->afid != AFID_NOBUF) {
4444 if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
4446 lseek(ap->afile, ap->afpos, 0);
4447 i = safe_read(ap->afile, bp->buf, sizeof(bp->buf));
4453 bp->ebufp = (bp->bufp = bp->buf) + i;
4456 return *bp->bufp++ & 0177;
4459 #ifdef CONFIG_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 size = strlen(mycommand);
4469 c = mycommand[position];
4475 i = safe_read(ap->afile, &c, sizeof(c));
4476 return(i == sizeof(c)? c&0177: (closef(ap->afile), 0));
4481 * Return the characters from a here temp file.
4485 register struct ioarg *ap;
4490 if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
4499 * Return the characters produced by a process (`...`).
4500 * Quote them if required, and remove any trailing newline characters.
4509 if ((c = qgravechar(ap, iop)&~QUOTE) == '\n')
4516 register struct ioarg *ap;
4528 } else if ((c = filechar(ap)) == '\n') {
4530 while ((c = filechar(ap)) == '\n')
4538 return(c!=0? c|QUOTE: 0);
4542 * Return a single command (usually the first line) from a file.
4546 register struct ioarg *ap;
4550 if ((c = filechar(ap)) == '\n') {
4553 ap->afile = -1; /* illegal value */
4564 write(2, s, strlen(s));
4587 for (u=NUFILE; u<NOFILE;)
4592 * remap fd into Shell's fd space
4602 for (i=0; i<NOFILE; i++)
4607 } while (fd >= 0 && fd < e.iofd);
4608 for (i=0; i<NOFILE; i++)
4612 err("too many files open in shell");
4623 if ((i = pipe(pv)) < 0)
4624 err("can't create pipe - try again");
4638 /* -------- here.c -------- */
4649 register struct here *h, *lh;
4651 h = (struct here *) space(sizeof(struct here));
4654 h->h_tag = evalstr(s, DOSUB);
4663 for (lh = inhere; lh!=NULL; lh = lh->h_next)
4664 if (lh->h_next == 0) {
4668 iop->io_flag |= IOHERE|IOXHERE;
4669 for (s = h->h_tag; *s; s++)
4671 iop->io_flag &= ~ IOXHERE;
4674 h->h_dosub = iop->io_flag & IOXHERE;
4680 register struct here *h, *hp;
4682 /* Scan here files first leaving inhere list in place */
4683 for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
4684 readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub? 0: '\'');
4686 /* Make inhere list active - keep list intact for scraphere */
4688 hp->h_next = acthere;
4695 readhere(name, s, ec)
4701 char tname[30] = ".msh_XXXXXX";
4704 char myline [LINELIM+1];
4707 tf = mkstemp(tname);
4710 *name = strsave(tname, areanum);
4711 if (newenv(setjmp(errpt = ev)) != 0)
4714 pushio(e.iop->argp, (int(*)(struct ioarg *))e.iop->iofn);
4717 if (interactive && e.iop <= iostack) {
4718 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4719 current_prompt=cprompt->value;
4721 prs(cprompt->value);
4725 while ((c = my_getc(ec)) != '\n' && c) {
4728 if (thenext >= &myline[LINELIM]) {
4735 if (strcmp(s, myline) == 0 || c == 0)
4738 write (tf, myline, (int)(thenext-myline));
4741 prs("here document `"); prs(s); err("' unclosed");
4749 * open here temp file.
4750 * if unquoted here, expand here temp file into second temp file.
4753 herein(hname, xdoll)
4761 /* Avoid longjmp clobbering */
4766 hf = open(hname, 0);
4771 char tname[30] = ".msh_XXXXXX";
4774 tf = mkstemp(tname);
4777 if (newenv(setjmp(errpt = ev)) == 0) {
4778 PUSHIO(afile, hf, herechar);
4780 while ((c = subgetc(0, 0)) != 0) {
4782 write(tf, &c, sizeof c);
4788 tf = open(tname, 0);
4798 register struct here *h;
4800 for (h = inhere; h != NULL; h = h->h_next) {
4801 if (h->h_iop && h->h_iop->io_name)
4802 unlink(h->h_iop->io_name);
4807 /* unlink here temp files before a freearea(area) */
4812 register struct here *h, *hl;
4815 for (h = acthere; h != NULL; h = h->h_next)
4816 if (getarea((char *) h) >= area) {
4817 if (h->h_iop->io_name != NULL)
4818 unlink(h->h_iop->io_name);
4820 acthere = h->h_next;
4822 hl->h_next = h->h_next;
4830 * Copyright (c) 1987,1997, Prentice Hall
4831 * All rights reserved.
4833 * Redistribution and use of the MINIX operating system in source and
4834 * binary forms, with or without modification, are permitted provided
4835 * that the following conditions are met:
4837 * Redistributions of source code must retain the above copyright
4838 * notice, this list of conditions and the following disclaimer.
4840 * Redistributions in binary form must reproduce the above
4841 * copyright notice, this list of conditions and the following
4842 * disclaimer in the documentation and/or other materials provided
4843 * with the distribution.
4845 * Neither the name of Prentice Hall nor the names of the software
4846 * authors or contributors may be used to endorse or promote
4847 * products derived from this software without specific prior
4848 * written permission.
4850 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
4851 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
4852 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4853 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4854 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
4855 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4856 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4857 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
4858 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4859 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
4860 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
4861 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.