| /* vi: set sw=4 ts=4: */ |
| /* |
| * Minix shell port for busybox |
| * |
| * This version of the Minix shell was adapted for use in busybox |
| * by Erik Andersen <andersen@codepoet.org> |
| * |
| * - backtick expansion did not work properly |
| * Jonas Holmberg <jonas.holmberg@axis.com> |
| * Robert Schwebel <r.schwebel@pengutronix.de> |
| * Erik Andersen <andersen@codepoet.org> |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License as published by |
| * the Free Software Foundation; either version 2 of the License, or |
| * (at your option) any later version. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with this program; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| * |
| * Original copyright notice is retained at the end of this file. |
| */ |
| |
| #include <ctype.h> |
| #include <dirent.h> |
| #include <errno.h> |
| #include <fcntl.h> |
| #include <limits.h> |
| #include <setjmp.h> |
| #include <signal.h> |
| #include <stddef.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <time.h> |
| #include <unistd.h> |
| #include <sys/stat.h> |
| #include <sys/times.h> |
| #include <sys/types.h> |
| #include <sys/wait.h> |
| |
| #include "cmdedit.h" |
| #include "busybox.h" |
| |
| |
| /* -------- sh.h -------- */ |
| /* |
| * shell |
| */ |
| |
| #define LINELIM 2100 |
| #define NPUSH 8 /* limit to input nesting */ |
| |
| #undef NOFILE |
| #define NOFILE 20 /* Number of open files */ |
| #define NUFILE 10 /* Number of user-accessible files */ |
| #define FDBASE 10 /* First file usable by Shell */ |
| |
| /* |
| * values returned by wait |
| */ |
| #define WAITSIG(s) ((s)&0177) |
| #define WAITVAL(s) (((s)>>8)&0377) |
| #define WAITCORE(s) (((s)&0200)!=0) |
| |
| /* |
| * library and system defintions |
| */ |
| typedef void xint; /* base type of jmp_buf, for not broken compilers */ |
| |
| /* |
| * shell components |
| */ |
| |
| #define QUOTE 0200 |
| |
| #define NOBLOCK ((struct op *)NULL) |
| #define NOWORD ((char *)NULL) |
| #define NOWORDS ((char **)NULL) |
| #define NOPIPE ((int *)NULL) |
| |
| /* |
| * Description of a command or an operation on commands. |
| * Might eventually use a union. |
| */ |
| struct op { |
| int type; /* operation type, see below */ |
| char **words; /* arguments to a command */ |
| struct ioword **ioact; /* IO actions (eg, < > >>) */ |
| struct op *left; |
| struct op *right; |
| char *str; /* identifier for case and for */ |
| }; |
| |
| #define TCOM 1 /* command */ |
| #define TPAREN 2 /* (c-list) */ |
| #define TPIPE 3 /* a | b */ |
| #define TLIST 4 /* a [&;] b */ |
| #define TOR 5 /* || */ |
| #define TAND 6 /* && */ |
| #define TFOR 7 |
| #define TDO 8 |
| #define TCASE 9 |
| #define TIF 10 |
| #define TWHILE 11 |
| #define TUNTIL 12 |
| #define TELIF 13 |
| #define TPAT 14 /* pattern in case */ |
| #define TBRACE 15 /* {c-list} */ |
| #define TASYNC 16 /* c & */ |
| |
| /* |
| * actions determining the environment of a process |
| */ |
| #define BIT(i) (1<<(i)) |
| #define FEXEC BIT(0) /* execute without forking */ |
| |
| /* |
| * flags to control evaluation of words |
| */ |
| #define DOSUB 1 /* interpret $, `, and quotes */ |
| #define DOBLANK 2 /* perform blank interpretation */ |
| #define DOGLOB 4 /* interpret [?* */ |
| #define DOKEY 8 /* move words with `=' to 2nd arg. list */ |
| #define DOTRIM 16 /* trim resulting string */ |
| |
| #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM) |
| |
| static char **dolv; |
| static int dolc; |
| static int exstat; |
| static char gflg; |
| static int interactive; /* Is this an interactive shell */ |
| static int execflg; |
| static int multiline; /* \n changed to ; */ |
| static struct op *outtree; /* result from parser */ |
| |
| static xint *failpt; |
| static xint *errpt; |
| static struct brkcon *brklist; |
| static int isbreak; |
| static int newfile(char *s); |
| static char *findeq(char *cp); |
| static char *cclass(char *p, int sub); |
| static void initarea(void); |
| extern int msh_main(int argc, char **argv); |
| |
| |
| struct brkcon { |
| jmp_buf brkpt; |
| struct brkcon *nextlev; |
| } ; |
| |
| /* |
| * redirection |
| */ |
| struct ioword { |
| short io_unit; /* unit affected */ |
| short io_flag; /* action (below) */ |
| char *io_name; /* file name */ |
| }; |
| #define IOREAD 1 /* < */ |
| #define IOHERE 2 /* << (here file) */ |
| #define IOWRITE 4 /* > */ |
| #define IOCAT 8 /* >> */ |
| #define IOXHERE 16 /* ${}, ` in << */ |
| #define IODUP 32 /* >&digit */ |
| #define IOCLOSE 64 /* >&- */ |
| |
| #define IODEFAULT (-1) /* token for default IO unit */ |
| |
| static struct wdblock *wdlist; |
| static struct wdblock *iolist; |
| |
| /* |
| * parsing & execution environment |
| */ |
| static struct env { |
| char *linep; |
| struct io *iobase; |
| struct io *iop; |
| xint *errpt; |
| int iofd; |
| struct env *oenv; |
| } e; |
| |
| /* |
| * flags: |
| * -e: quit on error |
| * -k: look for name=value everywhere on command line |
| * -n: no execution |
| * -t: exit after reading and executing one command |
| * -v: echo as read |
| * -x: trace |
| * -u: unset variables net diagnostic |
| */ |
| static char *flag; |
| |
| static char *null; /* null value for variable */ |
| static int intr; /* interrupt pending */ |
| |
| static char *trap[_NSIG+1]; |
| static char ourtrap[_NSIG+1]; |
| static int trapset; /* trap pending */ |
| |
| static int heedint; /* heed interrupt signals */ |
| |
| static int yynerrs; /* yacc */ |
| |
| static char line[LINELIM]; |
| static char *elinep; |
| |
| /* |
| * other functions |
| */ |
| static int(*inbuilt(char *s))(struct op *); |
| |
| |
| static char *rexecve (char *c , char **v, char **envp ); |
| static char *space (int n ); |
| static char *strsave (char *s, int a ); |
| static char *evalstr (char *cp, int f ); |
| static char *putn (int n ); |
| static char *itoa (int n ); |
| static char *unquote (char *as ); |
| static struct var *lookup (char *n ); |
| static int rlookup (char *n ); |
| static struct wdblock *glob (char *cp, struct wdblock *wb ); |
| static int my_getc( int ec); |
| static int subgetc (int ec, int quoted ); |
| static char **makenv (void); |
| static char **eval (char **ap, int f ); |
| static int setstatus (int s ); |
| static int waitfor (int lastpid, int canintr ); |
| |
| static void onintr (int s ); /* SIGINT handler */ |
| |
| static int newenv (int f ); |
| static void quitenv (void); |
| static void err (char *s ); |
| static int anys (char *s1, char *s2 ); |
| static int any (int c, char *s ); |
| static void next (int f ); |
| static void setdash (void); |
| static void onecommand (void); |
| static void runtrap (int i ); |
| static int gmatch (char *s, char *p ); |
| |
| /* |
| * error handling |
| */ |
| static void leave (void); /* abort shell (or fail in subshell) */ |
| static void fail (void); /* fail but return to process next command */ |
| static void warn (char *s ); |
| static void sig (int i ); /* default signal handler */ |
| |
| |
| |
| /* -------- area stuff -------- */ |
| |
| #define REGSIZE sizeof(struct region) |
| #define GROWBY 256 |
| //#define SHRINKBY 64 |
| #undef SHRINKBY |
| #define FREE 32767 |
| #define BUSY 0 |
| #define ALIGN (sizeof(int)-1) |
| |
| |
| struct region { |
| struct region *next; |
| int area; |
| }; |
| |
| |
| |
| /* -------- grammar stuff -------- */ |
| typedef union { |
| char *cp; |
| char **wp; |
| int i; |
| struct op *o; |
| } YYSTYPE; |
| #define WORD 256 |
| #define LOGAND 257 |
| #define LOGOR 258 |
| #define BREAK 259 |
| #define IF 260 |
| #define THEN 261 |
| #define ELSE 262 |
| #define ELIF 263 |
| #define FI 264 |
| #define CASE 265 |
| #define ESAC 266 |
| #define FOR 267 |
| #define WHILE 268 |
| #define UNTIL 269 |
| #define DO 270 |
| #define DONE 271 |
| #define IN 272 |
| #define YYERRCODE 300 |
| |
| /* flags to yylex */ |
| #define CONTIN 01 /* skip new lines to complete command */ |
| |
| #define SYNTAXERR zzerr() |
| static struct op *pipeline(int cf ); |
| static struct op *andor(void); |
| static struct op *c_list(void); |
| static int synio(int cf ); |
| static void musthave (int c, int cf ); |
| static struct op *simple(void); |
| static struct op *nested(int type, int mark ); |
| static struct op *command(int cf ); |
| static struct op *dogroup(int onlydone ); |
| static struct op *thenpart(void); |
| static struct op *elsepart(void); |
| static struct op *caselist(void); |
| static struct op *casepart(void); |
| static char **pattern(void); |
| static char **wordlist(void); |
| static struct op *list(struct op *t1, struct op *t2 ); |
| static struct op *block(int type, struct op *t1, struct op *t2, char **wp ); |
| static struct op *newtp(void); |
| static struct op *namelist(struct op *t ); |
| static char **copyw(void); |
| static void word(char *cp ); |
| static struct ioword **copyio(void); |
| static struct ioword *io (int u, int f, char *cp ); |
| static void zzerr(void); |
| static void yyerror(char *s ); |
| static int yylex(int cf ); |
| static int collect(int c, int c1 ); |
| static int dual(int c ); |
| static void diag(int ec ); |
| static char *tree(unsigned size ); |
| |
| /* -------- var.h -------- */ |
| |
| struct var { |
| char *value; |
| char *name; |
| struct var *next; |
| char status; |
| }; |
| #define COPYV 1 /* flag to setval, suggesting copy */ |
| #define RONLY 01 /* variable is read-only */ |
| #define EXPORT 02 /* variable is to be exported */ |
| #define GETCELL 04 /* name & value space was got with getcell */ |
| |
| static struct var *vlist; /* dictionary */ |
| |
| static struct var *homedir; /* home directory */ |
| static struct var *prompt; /* main prompt */ |
| static struct var *cprompt; /* continuation prompt */ |
| static struct var *path; /* search path for commands */ |
| static struct var *shell; /* shell to interpret command files */ |
| static struct var *ifs; /* field separators */ |
| |
| static int yyparse (void); |
| static struct var *lookup (char *n ); |
| static void setval (struct var *vp, char *val ); |
| static void nameval (struct var *vp, char *val, char *name ); |
| static void export (struct var *vp ); |
| static void ronly (struct var *vp ); |
| static int isassign (char *s ); |
| static int checkname (char *cp ); |
| static int assign (char *s, int cf ); |
| static void putvlist (int f, int out ); |
| static int eqname (char *n1, char *n2 ); |
| |
| static int execute (struct op *t, int *pin, int *pout, int act ); |
| |
| /* -------- io.h -------- */ |
| /* io buffer */ |
| struct iobuf { |
| unsigned id; /* buffer id */ |
| char buf[512]; /* buffer */ |
| char *bufp; /* pointer into buffer */ |
| char *ebufp; /* pointer to end of buffer */ |
| }; |
| |
| /* possible arguments to an IO function */ |
| struct ioarg { |
| char *aword; |
| char **awordlist; |
| int afile; /* file descriptor */ |
| unsigned afid; /* buffer id */ |
| long afpos; /* file position */ |
| struct iobuf *afbuf; /* buffer for this file */ |
| }; |
| //static struct ioarg ioargstack[NPUSH]; |
| #define AFID_NOBUF (~0) |
| #define AFID_ID 0 |
| |
| /* an input generator's state */ |
| struct io { |
| int (*iofn)(struct ioarg *, struct io *); |
| struct ioarg *argp; |
| int peekc; |
| char prev; /* previous character read by readc() */ |
| char nlcount; /* for `'s */ |
| char xchar; /* for `'s */ |
| char task; /* reason for pushed IO */ |
| }; |
| //static struct io iostack[NPUSH]; |
| #define XOTHER 0 /* none of the below */ |
| #define XDOLL 1 /* expanding ${} */ |
| #define XGRAVE 2 /* expanding `'s */ |
| #define XIO 3 /* file IO */ |
| |
| /* in substitution */ |
| #define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL) |
| |
| /* |
| * input generators for IO structure |
| */ |
| static int nlchar (struct ioarg *ap ); |
| static int strchar (struct ioarg *ap ); |
| static int qstrchar (struct ioarg *ap ); |
| static int filechar (struct ioarg *ap ); |
| static int herechar (struct ioarg *ap ); |
| static int linechar (struct ioarg *ap ); |
| static int gravechar (struct ioarg *ap, struct io *iop ); |
| static int qgravechar (struct ioarg *ap, struct io *iop ); |
| static int dolchar (struct ioarg *ap ); |
| static int wdchar (struct ioarg *ap ); |
| static void scraphere (void); |
| static void freehere (int area ); |
| static void gethere (void); |
| static void markhere (char *s, struct ioword *iop ); |
| static int herein (char *hname, int xdoll ); |
| static int run (struct ioarg *argp, int (*f)(struct ioarg *)); |
| |
| /* |
| * IO functions |
| */ |
| static int eofc (void); |
| static int readc (void); |
| static void unget (int c ); |
| static void ioecho (int c ); |
| static void prs (char *s ); |
| static void prn (unsigned u ); |
| static void closef (int i ); |
| static void closeall (void); |
| |
| /* |
| * IO control |
| */ |
| static void pushio (struct ioarg *argp, int (*f)(struct ioarg *)); |
| static int remap (int fd ); |
| static int openpipe (int *pv ); |
| static void closepipe (int *pv ); |
| static struct io *setbase (struct io *ip ); |
| |
| static struct ioarg temparg; /* temporary for PUSHIO */ |
| #define PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen))) |
| #define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen))) |
| |
| /* -------- word.h -------- */ |
| |
| #define NSTART 16 /* default number of words to allow for initially */ |
| |
| struct wdblock { |
| short w_bsize; |
| short w_nword; |
| /* bounds are arbitrary */ |
| char *w_words[1]; |
| }; |
| |
| static struct wdblock *addword (char *wd, struct wdblock *wb ); |
| static struct wdblock *newword (int nw ); |
| static char **getwords (struct wdblock *wb ); |
| |
| /* -------- area.h -------- */ |
| |
| /* |
| * storage allocation |
| */ |
| static char *getcell (unsigned nbytes ); |
| static void garbage (void); |
| static void setarea (char *cp, int a ); |
| static int getarea (char *cp ); |
| static void freearea (int a ); |
| static void freecell (char *cp ); |
| static int areanum; /* current allocation area */ |
| |
| #define NEW(type) (type *)getcell(sizeof(type)) |
| #define DELETE(obj) freecell((char *)obj) |
| |
| |
| /* -------- misc stuff -------- */ |
| |
| static int forkexec (struct op *t, int *pin, int *pout, int act, char **wp, int *pforked ); |
| static int iosetup (struct ioword *iop, int pipein, int pipeout ); |
| static void echo(char **wp ); |
| static struct op **find1case (struct op *t, char *w ); |
| static struct op *findcase (struct op *t, char *w ); |
| static void brkset(struct brkcon *bc ); |
| static int dolabel(struct op *t ); |
| static int dohelp(struct op *t ); |
| static int dochdir(struct op *t ); |
| static int doshift(struct op *t ); |
| static int dologin(struct op *t ); |
| static int doumask(struct op *t ); |
| static int doexec(struct op *t ); |
| static int dodot(struct op *t ); |
| static int dowait(struct op *t ); |
| static int doread(struct op *t ); |
| static int doeval(struct op *t ); |
| static int dotrap(struct op *t ); |
| static int getsig(char *s ); |
| static void setsig (int n, sighandler_t f); |
| static int getn(char *as ); |
| static int dobreak(struct op *t ); |
| static int docontinue(struct op *t ); |
| static int brkcontin (char *cp, int val ); |
| static int doexit(struct op *t ); |
| static int doexport(struct op *t ); |
| static int doreadonly(struct op *t ); |
| static void rdexp (char **wp, void (*f)(struct var *), int key); |
| static void badid(char *s ); |
| static int doset(struct op *t ); |
| static void varput (char *s, int out ); |
| static int dotimes(struct op *t ); |
| static int expand (char *cp, struct wdblock **wbp, int f ); |
| static char *blank(int f ); |
| static int dollar(int quoted ); |
| static int grave(int quoted ); |
| static void globname (char *we, char *pp ); |
| static char *generate (char *start1, char *end1, char *middle, char *end ); |
| static int anyspcl(struct wdblock *wb ); |
| static int xstrcmp (char *p1, char *p2 ); |
| static void glob0 (char *a0, unsigned int a1, int a2, int (*a3)(char *, char *)); |
| static void glob1 (char *base, char *lim ); |
| static void glob2 (char *i, char *j ); |
| static void glob3 (char *i, char *j, char *k ); |
| static void readhere (char **name, char *s, int ec ); |
| static void pushio (struct ioarg *argp, int (*f)(struct ioarg *)); |
| static int xxchar(struct ioarg *ap ); |
| |
| struct here { |
| char *h_tag; |
| int h_dosub; |
| struct ioword *h_iop; |
| struct here *h_next; |
| }; |
| |
| static char *signame[] = { |
| "Signal 0", |
| "Hangup", |
| (char *)NULL, /* interrupt */ |
| "Quit", |
| "Illegal instruction", |
| "Trace/BPT trap", |
| "Abort", |
| "Bus error", |
| "Floating Point Exception", |
| "Killed", |
| "SIGUSR1", |
| "SIGSEGV", |
| "SIGUSR2", |
| (char *)NULL, /* broken pipe */ |
| "Alarm clock", |
| "Terminated", |
| }; |
| #define NSIGNAL (sizeof(signame)/sizeof(signame[0])) |
| |
| struct res { |
| char *r_name; |
| int r_val; |
| }; |
| static struct res restab[] = { |
| {"for", FOR}, |
| {"case", CASE}, |
| {"esac", ESAC}, |
| {"while", WHILE}, |
| {"do", DO}, |
| {"done", DONE}, |
| {"if", IF}, |
| {"in", IN}, |
| {"then", THEN}, |
| {"else", ELSE}, |
| {"elif", ELIF}, |
| {"until", UNTIL}, |
| {"fi", FI}, |
| |
| {";;", BREAK}, |
| {"||", LOGOR}, |
| {"&&", LOGAND}, |
| {"{", '{'}, |
| {"}", '}'}, |
| {0, 0}, |
| }; |
| |
| |
| struct builtincmd { |
| const char *name; |
| int (*builtinfunc)(struct op *t); |
| }; |
| static const struct builtincmd builtincmds[] = { |
| {".", dodot}, |
| {":", dolabel}, |
| {"break", dobreak}, |
| {"cd", dochdir}, |
| {"continue",docontinue}, |
| {"eval", doeval}, |
| {"exec", doexec}, |
| {"exit", doexit}, |
| {"export", doexport}, |
| {"help", dohelp}, |
| {"login", dologin}, |
| {"newgrp", dologin}, |
| {"read", doread}, |
| {"readonly",doreadonly}, |
| {"set", doset}, |
| {"shift", doshift}, |
| {"times", dotimes}, |
| {"trap", dotrap}, |
| {"umask", doumask}, |
| {"wait", dowait}, |
| {0,0} |
| }; |
| |
| /* Globals */ |
| extern char **environ; /* environment pointer */ |
| static char **dolv; |
| static int dolc; |
| static int exstat; |
| static char gflg; |
| static int interactive; /* Is this an interactive shell */ |
| static int execflg; |
| static int multiline; /* \n changed to ; */ |
| static struct op *outtree; /* result from parser */ |
| static xint *failpt; |
| static xint *errpt; |
| static struct brkcon *brklist; |
| static int isbreak; |
| static struct wdblock *wdlist; |
| static struct wdblock *iolist; |
| static char *trap[_NSIG+1]; |
| static char ourtrap[_NSIG+1]; |
| static int trapset; /* trap pending */ |
| static int yynerrs; /* yacc */ |
| static char line[LINELIM]; |
| static struct var *vlist; /* dictionary */ |
| static struct var *homedir; /* home directory */ |
| static struct var *prompt; /* main prompt */ |
| static struct var *cprompt; /* continuation prompt */ |
| static struct var *path; /* search path for commands */ |
| static struct var *shell; /* shell to interpret command files */ |
| static struct var *ifs; /* field separators */ |
| static struct ioarg ioargstack[NPUSH]; |
| static struct io iostack[NPUSH]; |
| static int areanum; /* current allocation area */ |
| static int intr; |
| static int inparse; |
| static char flags['z'-'a'+1]; |
| static char *flag = flags-'a'; |
| static char *elinep = line+sizeof(line)-5; |
| static char *null = ""; |
| static int heedint =1; |
| static struct env e ={line, iostack, iostack-1, (xint *)NULL, FDBASE, (struct env *)NULL}; |
| static void (*qflag)(int) = SIG_IGN; |
| static int startl; |
| static int peeksym; |
| static int nlseen; |
| static int iounit = IODEFAULT; |
| static YYSTYPE yylval; |
| static struct iobuf sharedbuf = {AFID_NOBUF}; |
| static struct iobuf mainbuf = {AFID_NOBUF}; |
| static unsigned bufid = AFID_ID; /* buffer id counter */ |
| static struct ioarg temparg = {0, 0, 0, AFID_NOBUF, 0}; |
| static struct here *inhere; /* list of hear docs while parsing */ |
| static struct here *acthere; /* list of active here documents */ |
| static struct region *areabot; /* bottom of area */ |
| static struct region *areatop; /* top of area */ |
| static struct region *areanxt; /* starting point of scan */ |
| static void * brktop; |
| static void * brkaddr; |
| |
| |
| #ifdef CONFIG_FEATURE_COMMAND_EDITING |
| static char * current_prompt; |
| #endif |
| |
| /* -------- sh.c -------- */ |
| /* |
| * shell |
| */ |
| |
| |
| extern int msh_main(int argc, char **argv) |
| { |
| register int f; |
| register char *s; |
| int cflag; |
| char *name, **ap; |
| int (*iof)(struct ioarg *); |
| |
| initarea(); |
| if ((ap = environ) != NULL) { |
| while (*ap) |
| assign(*ap++, !COPYV); |
| for (ap = environ; *ap;) |
| export(lookup(*ap++)); |
| } |
| closeall(); |
| areanum = 1; |
| |
| shell = lookup("SHELL"); |
| if (shell->value == null) |
| setval(shell, DEFAULT_SHELL); |
| export(shell); |
| |
| homedir = lookup("HOME"); |
| if (homedir->value == null) |
| setval(homedir, "/"); |
| export(homedir); |
| |
| setval(lookup("$"), putn(getpid())); |
| |
| path = lookup("PATH"); |
| if (path->value == null) { |
| if (geteuid() == 0) |
| setval(path, "/sbin:/bin:/usr/sbin:/usr/bin"); |
| else |
| setval(path, "/bin:/usr/bin"); |
| } |
| export(path); |
| |
| ifs = lookup("IFS"); |
| if (ifs->value == null) |
| setval(ifs, " \t\n"); |
| |
| prompt = lookup("PS1"); |
| #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT |
| if (prompt->value == null) |
| #endif |
| setval(prompt, "$ "); |
| if (geteuid() == 0) { |
| setval(prompt, "# "); |
| prompt->status &= ~EXPORT; |
| } |
| cprompt = lookup("PS2"); |
| #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT |
| if (cprompt->value == null) |
| #endif |
| setval(cprompt, "> "); |
| |
| iof = filechar; |
| cflag = 0; |
| name = *argv++; |
| if (--argc >= 1) { |
| if(argv[0][0] == '-' && argv[0][1] != '\0') { |
| for (s = argv[0]+1; *s; s++) |
| switch (*s) { |
| case 'c': |
| prompt->status &= ~EXPORT; |
| cprompt->status &= ~EXPORT; |
| setval(prompt, ""); |
| setval(cprompt, ""); |
| cflag = 1; |
| if (--argc > 0) |
| PUSHIO(aword, *++argv, iof = nlchar); |
| break; |
| |
| case 'q': |
| qflag = SIG_DFL; |
| break; |
| |
| case 's': |
| /* standard input */ |
| break; |
| |
| case 't': |
| prompt->status &= ~EXPORT; |
| setval(prompt, ""); |
| iof = linechar; |
| break; |
| |
| case 'i': |
| interactive++; |
| default: |
| if (*s>='a' && *s<='z') |
| flag[(int)*s]++; |
| } |
| } else { |
| argv--; |
| argc++; |
| } |
| if (iof == filechar && --argc > 0) { |
| setval(prompt, ""); |
| setval(cprompt, ""); |
| prompt->status &= ~EXPORT; |
| cprompt->status &= ~EXPORT; |
| if (newfile(name = *++argv)) |
| exit(1); |
| } |
| } |
| setdash(); |
| if (e.iop < iostack) { |
| PUSHIO(afile, 0, iof); |
| if (isatty(0) && isatty(1) && !cflag) { |
| interactive++; |
| #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET |
| printf( "\n\n" BB_BANNER " Built-in shell (msh)\n"); |
| printf( "Enter 'help' for a list of built-in commands.\n\n"); |
| #endif |
| } |
| } |
| signal(SIGQUIT, qflag); |
| if (name && name[0] == '-') { |
| interactive++; |
| if ((f = open(".profile", 0)) >= 0) |
| next(remap(f)); |
| if ((f = open("/etc/profile", 0)) >= 0) |
| next(remap(f)); |
| } |
| if (interactive) |
| signal(SIGTERM, sig); |
| if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| signal(SIGINT, onintr); |
| dolv = argv; |
| dolc = argc; |
| dolv[0] = name; |
| if (dolc > 1) { |
| for (ap = ++argv; --argc > 0;) { |
| if (assign(*ap = *argv++, !COPYV)) { |
| dolc--; /* keyword */ |
| } else { |
| ap++; |
| } |
| } |
| } |
| setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc)); |
| |
| for (;;) { |
| if (interactive && e.iop <= iostack) { |
| #ifdef CONFIG_FEATURE_COMMAND_EDITING |
| current_prompt=prompt->value; |
| #else |
| prs(prompt->value); |
| #endif |
| } |
| onecommand(); |
| /* Ensure that getenv("PATH") stays current */ |
| setenv("PATH", path->value, 1); |
| } |
| } |
| |
| static void |
| setdash() |
| { |
| register char *cp; |
| register int c; |
| char m['z'-'a'+1]; |
| |
| cp = m; |
| for (c='a'; c<='z'; c++) |
| if (flag[(int)c]) |
| *cp++ = c; |
| *cp = 0; |
| setval(lookup("-"), m); |
| } |
| |
| static int |
| newfile(s) |
| register char *s; |
| { |
| register int f; |
| |
| if (strcmp(s, "-") != 0) { |
| f = open(s, 0); |
| if (f < 0) { |
| prs(s); |
| err(": cannot open"); |
| return(1); |
| } |
| } else |
| f = 0; |
| next(remap(f)); |
| return(0); |
| } |
| |
| static void |
| onecommand() |
| { |
| register int i; |
| jmp_buf m1; |
| |
| while (e.oenv) |
| quitenv(); |
| areanum = 1; |
| freehere(areanum); |
| freearea(areanum); |
| garbage(); |
| wdlist = 0; |
| iolist = 0; |
| e.errpt = 0; |
| e.linep = line; |
| yynerrs = 0; |
| multiline = 0; |
| inparse = 1; |
| intr = 0; |
| execflg = 0; |
| setjmp(failpt = m1); /* Bruce Evans' fix */ |
| if (setjmp(failpt = m1) || yyparse() || intr) { |
| while (e.oenv) |
| quitenv(); |
| scraphere(); |
| if (!interactive && intr) |
| leave(); |
| inparse = 0; |
| intr = 0; |
| return; |
| } |
| inparse = 0; |
| brklist = 0; |
| intr = 0; |
| execflg = 0; |
| if (!flag['n']) |
| execute(outtree, NOPIPE, NOPIPE, 0); |
| if (!interactive && intr) { |
| execflg = 0; |
| leave(); |
| } |
| if ((i = trapset) != 0) { |
| trapset = 0; |
| runtrap(i); |
| } |
| } |
| |
| static void |
| fail() |
| { |
| longjmp(failpt, 1); |
| /* NOTREACHED */ |
| } |
| |
| static void |
| leave() |
| { |
| if (execflg) |
| fail(); |
| scraphere(); |
| freehere(1); |
| runtrap(0); |
| exit(exstat); |
| /* NOTREACHED */ |
| } |
| |
| static void |
| warn(s) |
| register char *s; |
| { |
| if(*s) { |
| prs(s); |
| exstat = -1; |
| } |
| prs("\n"); |
| if (flag['e']) |
| leave(); |
| } |
| |
| static void |
| err(s) |
| char *s; |
| { |
| warn(s); |
| if (flag['n']) |
| return; |
| if (!interactive) |
| leave(); |
| if (e.errpt) |
| longjmp(e.errpt, 1); |
| closeall(); |
| e.iop = e.iobase = iostack; |
| } |
| |
| static int |
| newenv(f) |
| int f; |
| { |
| register struct env *ep; |
| |
| if (f) { |
| quitenv(); |
| return(1); |
| } |
| ep = (struct env *) space(sizeof(*ep)); |
| if (ep == NULL) { |
| while (e.oenv) |
| quitenv(); |
| fail(); |
| } |
| *ep = e; |
| e.oenv = ep; |
| e.errpt = errpt; |
| return(0); |
| } |
| |
| static void |
| quitenv() |
| { |
| register struct env *ep; |
| register int fd; |
| |
| if ((ep = e.oenv) != NULL) { |
| fd = e.iofd; |
| e = *ep; |
| /* should close `'d files */ |
| DELETE(ep); |
| while (--fd >= e.iofd) |
| close(fd); |
| } |
| } |
| |
| /* |
| * Is any character from s1 in s2? |
| */ |
| static int |
| anys(s1, s2) |
| register char *s1, *s2; |
| { |
| while (*s1) |
| if (any(*s1++, s2)) |
| return(1); |
| return(0); |
| } |
| |
| /* |
| * Is character c in s? |
| */ |
| static int |
| any(c, s) |
| register int c; |
| register char *s; |
| { |
| while (*s) |
| if (*s++ == c) |
| return(1); |
| return(0); |
| } |
| |
| static char * |
| putn(n) |
| register int n; |
| { |
| return(itoa(n)); |
| } |
| |
| static char * |
| itoa(n) |
| register int n; |
| { |
| static char s[20]; |
| snprintf(s, sizeof(s), "%u", n); |
| return(s); |
| } |
| |
| static void |
| next(int f) |
| { |
| PUSHIO(afile, f, filechar); |
| } |
| |
| static void |
| onintr(s) |
| int s; /* ANSI C requires a parameter */ |
| { |
| signal(SIGINT, onintr); |
| intr = 1; |
| if (interactive) { |
| if (inparse) { |
| prs("\n"); |
| fail(); |
| } |
| } |
| else if (heedint) { |
| execflg = 0; |
| leave(); |
| } |
| } |
| |
| static char * |
| space(n) |
| int n; |
| { |
| register char *cp; |
| |
| if ((cp = getcell(n)) == 0) |
| err("out of string space"); |
| return(cp); |
| } |
| |
| static char * |
| strsave(s, a) |
| register char *s; |
| int a; |
| { |
| register char *cp, *xp; |
| |
| if ((cp = space(strlen(s)+1)) != NULL) { |
| setarea((char *)cp, a); |
| for (xp = cp; (*xp++ = *s++) != '\0';) |
| ; |
| return(cp); |
| } |
| return(""); |
| } |
| |
| /* |
| * trap handling |
| */ |
| static void |
| sig(i) |
| register int i; |
| { |
| trapset = i; |
| signal(i, sig); |
| } |
| |
| static void runtrap(i) |
| int i; |
| { |
| char *trapstr; |
| |
| if ((trapstr = trap[i]) == NULL) |
| return; |
| if (i == 0) |
| trap[i] = 0; |
| RUN(aword, trapstr, nlchar); |
| } |
| |
| /* -------- var.c -------- */ |
| |
| /* |
| * Find the given name in the dictionary |
| * and return its value. If the name was |
| * not previously there, enter it now and |
| * return a null value. |
| */ |
| static struct var * |
| lookup(n) |
| register char *n; |
| { |
| register struct var *vp; |
| register char *cp; |
| register int c; |
| static struct var dummy; |
| |
| if (isdigit(*n)) { |
| dummy.name = n; |
| for (c = 0; isdigit(*n) && c < 1000; n++) |
| c = c*10 + *n-'0'; |
| dummy.status = RONLY; |
| dummy.value = c <= dolc? dolv[c]: null; |
| return(&dummy); |
| } |
| for (vp = vlist; vp; vp = vp->next) |
| if (eqname(vp->name, n)) |
| return(vp); |
| cp = findeq(n); |
| vp = (struct var *)space(sizeof(*vp)); |
| if (vp == 0 || (vp->name = space((int)(cp-n)+2)) == 0) { |
| dummy.name = dummy.value = ""; |
| return(&dummy); |
| } |
| for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++) |
| ; |
| if (*cp == 0) |
| *cp = '='; |
| *++cp = 0; |
| setarea((char *)vp, 0); |
| setarea((char *)vp->name, 0); |
| vp->value = null; |
| vp->next = vlist; |
| vp->status = GETCELL; |
| vlist = vp; |
| return(vp); |
| } |
| |
| /* |
| * give variable at `vp' the value `val'. |
| */ |
| static void |
| setval(vp, val) |
| struct var *vp; |
| char *val; |
| { |
| nameval(vp, val, (char *)NULL); |
| } |
| |
| /* |
| * if name is not NULL, it must be |
| * a prefix of the space `val', |
| * and end with `='. |
| * this is all so that exporting |
| * values is reasonably painless. |
| */ |
| static void |
| nameval(vp, val, name) |
| register struct var *vp; |
| char *val, *name; |
| { |
| register char *cp, *xp; |
| char *nv; |
| int fl; |
| |
| if (vp->status & RONLY) { |
| for (xp = vp->name; *xp && *xp != '=';) |
| putc(*xp++, stderr); |
| err(" is read-only"); |
| return; |
| } |
| fl = 0; |
| if (name == NULL) { |
| xp = space(strlen(vp->name)+strlen(val)+2); |
| if (xp == 0) |
| return; |
| /* make string: name=value */ |
| setarea((char *)xp, 0); |
| name = xp; |
| for (cp = vp->name; (*xp = *cp++) && *xp!='='; xp++) |
| ; |
| if (*xp++ == 0) |
| xp[-1] = '='; |
| nv = xp; |
| for (cp = val; (*xp++ = *cp++) != '\0';) |
| ; |
| val = nv; |
| fl = GETCELL; |
| } |
| if (vp->status & GETCELL) |
| freecell(vp->name); /* form new string `name=value' */ |
| vp->name = name; |
| vp->value = val; |
| vp->status |= fl; |
| } |
| |
| static void |
| export(vp) |
| struct var *vp; |
| { |
| vp->status |= EXPORT; |
| } |
| |
| static void |
| ronly(vp) |
| struct var *vp; |
| { |
| if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */ |
| vp->status |= RONLY; |
| } |
| |
| static int |
| isassign(s) |
| register char *s; |
| { |
| if (!isalpha((int)*s) && *s != '_') |
| return(0); |
| for (; *s != '='; s++) |
| if (*s == 0 || (!isalnum(*s) && *s != '_')) |
| return(0); |
| return(1); |
| } |
| |
| static int |
| assign(s, cf) |
| register char *s; |
| int cf; |
| { |
| register char *cp; |
| struct var *vp; |
| |
| if (!isalpha(*s) && *s != '_') |
| return(0); |
| for (cp = s; *cp != '='; cp++) |
| if (*cp == 0 || (!isalnum(*cp) && *cp != '_')) |
| return(0); |
| vp = lookup(s); |
| nameval(vp, ++cp, cf == COPYV? (char *)NULL: s); |
| if (cf != COPYV) |
| vp->status &= ~GETCELL; |
| return(1); |
| } |
| |
| static int |
| checkname(cp) |
| register char *cp; |
| { |
| if (!isalpha(*cp++) && *(cp-1) != '_') |
| return(0); |
| while (*cp) |
| if (!isalnum(*cp++) && *(cp-1) != '_') |
| return(0); |
| return(1); |
| } |
| |
| static void |
| putvlist(f, out) |
| register int f, out; |
| { |
| register struct var *vp; |
| |
| for (vp = vlist; vp; vp = vp->next) |
| if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) { |
| if (vp->status & EXPORT) |
| write(out, "export ", 7); |
| if (vp->status & RONLY) |
| write(out, "readonly ", 9); |
| write(out, vp->name, (int)(findeq(vp->name) - vp->name)); |
| write(out, "\n", 1); |
| } |
| } |
| |
| static int |
| eqname(n1, n2) |
| register char *n1, *n2; |
| { |
| for (; *n1 != '=' && *n1 != 0; n1++) |
| if (*n2++ != *n1) |
| return(0); |
| return(*n2 == 0 || *n2 == '='); |
| } |
| |
| static char * |
| findeq(cp) |
| register char *cp; |
| { |
| while (*cp != '\0' && *cp != '=') |
| cp++; |
| return(cp); |
| } |
| |
| /* -------- gmatch.c -------- */ |
| /* |
| * int gmatch(string, pattern) |
| * char *string, *pattern; |
| * |
| * Match a pattern as in sh(1). |
| */ |
| |
| #define CMASK 0377 |
| #define QUOTE 0200 |
| #define QMASK (CMASK&~QUOTE) |
| #define NOT '!' /* might use ^ */ |
| |
| static int |
| gmatch(s, p) |
| register char *s, *p; |
| { |
| register int sc, pc; |
| |
| if (s == NULL || p == NULL) |
| return(0); |
| while ((pc = *p++ & CMASK) != '\0') { |
| sc = *s++ & QMASK; |
| switch (pc) { |
| case '[': |
| if ((p = cclass(p, sc)) == NULL) |
| return(0); |
| break; |
| |
| case '?': |
| if (sc == 0) |
| return(0); |
| break; |
| |
| case '*': |
| s--; |
| do { |
| if (*p == '\0' || gmatch(s, p)) |
| return(1); |
| } while (*s++ != '\0'); |
| return(0); |
| |
| default: |
| if (sc != (pc&~QUOTE)) |
| return(0); |
| } |
| } |
| return(*s == 0); |
| } |
| |
| static char * |
| cclass(p, sub) |
| register char *p; |
| register int sub; |
| { |
| register int c, d, not, found; |
| |
| if ((not = *p == NOT) != 0) |
| p++; |
| found = not; |
| do { |
| if (*p == '\0') |
| return((char *)NULL); |
| c = *p & CMASK; |
| if (p[1] == '-' && p[2] != ']') { |
| d = p[2] & CMASK; |
| p++; |
| } else |
| d = c; |
| if (c == sub || (c <= sub && sub <= d)) |
| found = !not; |
| } while (*++p != ']'); |
| return(found? p+1: (char *)NULL); |
| } |
| |
| |
| /* -------- area.c -------- */ |
| |
| /* |
| * All memory between (char *)areabot and (char *)(areatop+1) is |
| * exclusively administered by the area management routines. |
| * It is assumed that sbrk() and brk() manipulate the high end. |
| */ |
| |
| #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;}) |
| |
| static void |
| initarea() |
| { |
| brkaddr = malloc(65000); |
| brktop = brkaddr + 65000; |
| |
| while ((int)sbrk(0) & ALIGN) |
| sbrk(1); |
| areabot = (struct region *)sbrk(REGSIZE); |
| |
| areabot->next = areabot; |
| areabot->area = BUSY; |
| areatop = areabot; |
| areanxt = areabot; |
| } |
| |
| char * |
| getcell(nbytes) |
| unsigned nbytes; |
| { |
| register int nregio; |
| register struct region *p, *q; |
| register int i; |
| |
| if (nbytes == 0) { |
| puts("getcell(0)"); |
| abort(); |
| } /* silly and defeats the algorithm */ |
| /* |
| * round upwards and add administration area |
| */ |
| nregio = (nbytes+(REGSIZE-1))/REGSIZE + 1; |
| for (p = areanxt;;) { |
| if (p->area > areanum) { |
| /* |
| * merge free cells |
| */ |
| while ((q = p->next)->area > areanum && q != areanxt) |
| p->next = q->next; |
| /* |
| * exit loop if cell big enough |
| */ |
| if (q >= p + nregio) |
| goto found; |
| } |
| p = p->next; |
| if (p == areanxt) |
| break; |
| } |
| i = nregio >= GROWBY ? nregio : GROWBY; |
| p = (struct region *)sbrk(i * REGSIZE); |
| if (p == (struct region *)-1) |
| return((char *)NULL); |
| p--; |
| if (p != areatop) { |
| puts("not contig"); |
| abort(); /* allocated areas are contiguous */ |
| } |
| q = p + i; |
| p->next = q; |
| p->area = FREE; |
| q->next = areabot; |
| q->area = BUSY; |
| areatop = q; |
| found: |
| /* |
| * we found a FREE area big enough, pointed to by 'p', and up to 'q' |
| */ |
| areanxt = p + nregio; |
| if (areanxt < q) { |
| /* |
| * split into requested area and rest |
| */ |
| if (areanxt+1 > q) { |
| puts("OOM"); |
| abort(); /* insufficient space left for admin */ |
| } |
| areanxt->next = q; |
| areanxt->area = FREE; |
| p->next = areanxt; |
| } |
| p->area = areanum; |
| return((char *)(p+1)); |
| } |
| |
| static void |
| freecell(cp) |
| char *cp; |
| { |
| register struct region *p; |
| |
| if ((p = (struct region *)cp) != NULL) { |
| p--; |
| if (p < areanxt) |
| areanxt = p; |
| p->area = FREE; |
| } |
| } |
| |
| static void |
| freearea(a) |
| register int a; |
| { |
| register struct region *p, *top; |
| |
| top = areatop; |
| for (p = areabot; p != top; p = p->next) |
| if (p->area >= a) |
| p->area = FREE; |
| } |
| |
| static void |
| setarea(cp,a) |
| char *cp; |
| int a; |
| { |
| register struct region *p; |
| |
| if ((p = (struct region *)cp) != NULL) |
| (p-1)->area = a; |
| } |
| |
| int |
| getarea(cp) |
| char *cp; |
| { |
| return ((struct region*)cp-1)->area; |
| } |
| |
| static void |
| garbage() |
| { |
| register struct region *p, *q, *top; |
| |
| top = areatop; |
| for (p = areabot; p != top; p = p->next) { |
| if (p->area > areanum) { |
| while ((q = p->next)->area > areanum) |
| p->next = q->next; |
| areanxt = p; |
| } |
| } |
| #ifdef SHRINKBY |
| if (areatop >= q + SHRINKBY && q->area > areanum) { |
| brk((char *)(q+1)); |
| q->next = areabot; |
| q->area = BUSY; |
| areatop = q; |
| } |
| #endif |
| } |
| |
| /* -------- csyn.c -------- */ |
| /* |
| * shell: syntax (C version) |
| */ |
| |
| |
| int |
| yyparse() |
| { |
| startl = 1; |
| peeksym = 0; |
| yynerrs = 0; |
| outtree = c_list(); |
| musthave('\n', 0); |
| return(yynerrs!=0); |
| } |
| |
| static struct op * |
| pipeline(cf) |
| int cf; |
| { |
| register struct op *t, *p; |
| register int c; |
| |
| t = command(cf); |
| if (t != NULL) { |
| while ((c = yylex(0)) == '|') { |
| if ((p = command(CONTIN)) == NULL) |
| SYNTAXERR; |
| if (t->type != TPAREN && t->type != TCOM) { |
| /* shell statement */ |
| t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| } |
| t = block(TPIPE, t, p, NOWORDS); |
| } |
| peeksym = c; |
| } |
| return(t); |
| } |
| |
| static struct op * |
| andor() |
| { |
| register struct op *t, *p; |
| register int c; |
| |
| t = pipeline(0); |
| if (t != NULL) { |
| while ((c = yylex(0)) == LOGAND || c == LOGOR) { |
| if ((p = pipeline(CONTIN)) == NULL) |
| SYNTAXERR; |
| t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS); |
| } |
| peeksym = c; |
| } |
| return(t); |
| } |
| |
| static struct op * |
| c_list() |
| { |
| register struct op *t, *p; |
| register int c; |
| |
| t = andor(); |
| if (t != NULL) { |
| if((peeksym = yylex(0)) == '&') |
| t = block(TASYNC, t, NOBLOCK, NOWORDS); |
| while ((c = yylex(0)) == ';' || c == '&' || (multiline && c == '\n')) { |
| if ((p = andor()) == NULL) |
| return(t); |
| if((peeksym = yylex(0)) == '&') |
| p = block(TASYNC, p, NOBLOCK, NOWORDS); |
| t = list(t, p); |
| } |
| peeksym = c; |
| } |
| return(t); |
| } |
| |
| |
| static int |
| synio(cf) |
| int cf; |
| { |
| register struct ioword *iop; |
| register int i; |
| register int c; |
| |
| if ((c = yylex(cf)) != '<' && c != '>') { |
| peeksym = c; |
| return(0); |
| } |
| i = yylval.i; |
| musthave(WORD, 0); |
| iop = io(iounit, i, yylval.cp); |
| iounit = IODEFAULT; |
| if (i & IOHERE) |
| markhere(yylval.cp, iop); |
| return(1); |
| } |
| |
| static void |
| musthave(c, cf) |
| int c, cf; |
| { |
| if ((peeksym = yylex(cf)) != c) |
| SYNTAXERR; |
| peeksym = 0; |
| } |
| |
| static struct op * |
| simple() |
| { |
| register struct op *t; |
| |
| t = NULL; |
| for (;;) { |
| switch (peeksym = yylex(0)) { |
| case '<': |
| case '>': |
| (void) synio(0); |
| break; |
| |
| case WORD: |
| if (t == NULL) { |
| t = newtp(); |
| t->type = TCOM; |
| } |
| peeksym = 0; |
| word(yylval.cp); |
| break; |
| |
| default: |
| return(t); |
| } |
| } |
| } |
| |
| static struct op * |
| nested(type, mark) |
| int type, mark; |
| { |
| register struct op *t; |
| |
| multiline++; |
| t = c_list(); |
| musthave(mark, 0); |
| multiline--; |
| return(block(type, t, NOBLOCK, NOWORDS)); |
| } |
| |
| static struct op * |
| command(cf) |
| int cf; |
| { |
| register struct op *t; |
| struct wdblock *iosave; |
| register int c; |
| |
| iosave = iolist; |
| iolist = NULL; |
| if (multiline) |
| cf |= CONTIN; |
| while (synio(cf)) |
| cf = 0; |
| switch (c = yylex(cf)) { |
| default: |
| peeksym = c; |
| if ((t = simple()) == NULL) { |
| if (iolist == NULL) |
| return((struct op *)NULL); |
| t = newtp(); |
| t->type = TCOM; |
| } |
| break; |
| |
| case '(': |
| t = nested(TPAREN, ')'); |
| break; |
| |
| case '{': |
| t = nested(TBRACE, '}'); |
| break; |
| |
| case FOR: |
| t = newtp(); |
| t->type = TFOR; |
| musthave(WORD, 0); |
| startl = 1; |
| t->str = yylval.cp; |
| multiline++; |
| t->words = wordlist(); |
| if ((c = yylex(0)) != '\n' && c != ';') |
| peeksym = c; |
| t->left = dogroup(0); |
| multiline--; |
| break; |
| |
| case WHILE: |
| case UNTIL: |
| multiline++; |
| t = newtp(); |
| t->type = c == WHILE? TWHILE: TUNTIL; |
| t->left = c_list(); |
| t->right = dogroup(1); |
| t->words = NULL; |
| multiline--; |
| break; |
| |
| case CASE: |
| t = newtp(); |
| t->type = TCASE; |
| musthave(WORD, 0); |
| t->str = yylval.cp; |
| startl++; |
| multiline++; |
| musthave(IN, CONTIN); |
| startl++; |
| t->left = caselist(); |
| musthave(ESAC, 0); |
| multiline--; |
| break; |
| |
| case IF: |
| multiline++; |
| t = newtp(); |
| t->type = TIF; |
| t->left = c_list(); |
| t->right = thenpart(); |
| musthave(FI, 0); |
| multiline--; |
| break; |
| } |
| while (synio(0)) |
| ; |
| t = namelist(t); |
| iolist = iosave; |
| return(t); |
| } |
| |
| static struct op * |
| dogroup(onlydone) |
| int onlydone; |
| { |
| register int c; |
| register struct op *mylist; |
| |
| c = yylex(CONTIN); |
| if (c == DONE && onlydone) |
| return((struct op *)NULL); |
| if (c != DO) |
| SYNTAXERR; |
| mylist = c_list(); |
| musthave(DONE, 0); |
| return(mylist); |
| } |
| |
| static struct op * |
| thenpart() |
| { |
| register int c; |
| register struct op *t; |
| |
| if ((c = yylex(0)) != THEN) { |
| peeksym = c; |
| return((struct op *)NULL); |
| } |
| t = newtp(); |
| t->type = 0; |
| t->left = c_list(); |
| if (t->left == NULL) |
| SYNTAXERR; |
| t->right = elsepart(); |
| return(t); |
| } |
| |
| static struct op * |
| elsepart() |
| { |
| register int c; |
| register struct op *t; |
| |
| switch (c = yylex(0)) { |
| case ELSE: |
| if ((t = c_list()) == NULL) |
| SYNTAXERR; |
| return(t); |
| |
| case ELIF: |
| t = newtp(); |
| t->type = TELIF; |
| t->left = c_list(); |
| t->right = thenpart(); |
| return(t); |
| |
| default: |
| peeksym = c; |
| return((struct op *)NULL); |
| } |
| } |
| |
| static struct op * |
| caselist() |
| { |
| register struct op *t; |
| |
| t = NULL; |
| while ((peeksym = yylex(CONTIN)) != ESAC) |
| t = list(t, casepart()); |
| return(t); |
| } |
| |
| static struct op * |
| casepart() |
| { |
| register struct op *t; |
| |
| t = newtp(); |
| t->type = TPAT; |
| t->words = pattern(); |
| musthave(')', 0); |
| t->left = c_list(); |
| if ((peeksym = yylex(CONTIN)) != ESAC) |
| musthave(BREAK, CONTIN); |
| return(t); |
| } |
| |
| static char ** |
| pattern() |
| { |
| register int c, cf; |
| |
| cf = CONTIN; |
| do { |
| musthave(WORD, cf); |
| word(yylval.cp); |
| cf = 0; |
| } while ((c = yylex(0)) == '|'); |
| peeksym = c; |
| word(NOWORD); |
| return(copyw()); |
| } |
| |
| static char ** |
| wordlist() |
| { |
| register int c; |
| |
| if ((c = yylex(0)) != IN) { |
| peeksym = c; |
| return((char **)NULL); |
| } |
| startl = 0; |
| while ((c = yylex(0)) == WORD) |
| word(yylval.cp); |
| word(NOWORD); |
| peeksym = c; |
| return(copyw()); |
| } |
| |
| /* |
| * supporting functions |
| */ |
| static struct op * |
| list(t1, t2) |
| register struct op *t1, *t2; |
| { |
| if (t1 == NULL) |
| return(t2); |
| if (t2 == NULL) |
| return(t1); |
| return(block(TLIST, t1, t2, NOWORDS)); |
| } |
| |
| static struct op * |
| block(type, t1, t2, wp) |
| int type; |
| struct op *t1, *t2; |
| char **wp; |
| { |
| register struct op *t; |
| |
| t = newtp(); |
| t->type = type; |
| t->left = t1; |
| t->right = t2; |
| t->words = wp; |
| return(t); |
| } |
| |
| static int |
| rlookup(n) |
| register char *n; |
| { |
| register struct res *rp; |
| |
| for (rp = restab; rp->r_name; rp++) |
| if (strcmp(rp->r_name, n) == 0) |
| return(rp->r_val); |
| return(0); |
| } |
| |
| static struct op * |
| newtp() |
| { |
| register struct op *t; |
| |
| t = (struct op *)tree(sizeof(*t)); |
| t->type = 0; |
| t->words = NULL; |
| t->ioact = NULL; |
| t->left = NULL; |
| t->right = NULL; |
| t->str = NULL; |
| return(t); |
| } |
| |
| static struct op * |
| namelist(t) |
| register struct op *t; |
| { |
| if (iolist) { |
| iolist = addword((char *)NULL, iolist); |
| t->ioact = copyio(); |
| } else |
| t->ioact = NULL; |
| if (t->type != TCOM) { |
| if (t->type != TPAREN && t->ioact != NULL) { |
| t = block(TPAREN, t, NOBLOCK, NOWORDS); |
| t->ioact = t->left->ioact; |
| t->left->ioact = NULL; |
| } |
| return(t); |
| } |
| word(NOWORD); |
| t->words = copyw(); |
| return(t); |
| } |
| |
| static char ** |
| copyw() |
| { |
| register char **wd; |
| |
| wd = getwords(wdlist); |
| wdlist = 0; |
| return(wd); |
| } |
| |
| static void |
| word(cp) |
| char *cp; |
| { |
| wdlist = addword(cp, wdlist); |
| } |
| |
| static struct ioword ** |
| copyio() |
| { |
| register struct ioword **iop; |
| |
| iop = (struct ioword **) getwords(iolist); |
| iolist = 0; |
| return(iop); |
| } |
| |
| static struct ioword * |
| io(u, f, cp) |
| int u; |
| int f; |
| char *cp; |
| { |
| register struct ioword *iop; |
| |
| iop = (struct ioword *) tree(sizeof(*iop)); |
| iop->io_unit = u; |
| iop->io_flag = f; |
| iop->io_name = cp; |
| iolist = addword((char *)iop, iolist); |
| return(iop); |
| } |
| |
| static void |
| zzerr() |
| { |
| yyerror("syntax error"); |
| } |
| |
| static void |
| yyerror(s) |
| char *s; |
| { |
| yynerrs++; |
| if (interactive && e.iop <= iostack) { |
| multiline = 0; |
| while (eofc() == 0 && yylex(0) != '\n') |
| ; |
| } |
| err(s); |
| fail(); |
| } |
| |
| static int |
| yylex(cf) |
| int cf; |
| { |
| register int c, c1; |
| int atstart; |
| |
| if ((c = peeksym) > 0) { |
| peeksym = 0; |
| if (c == '\n') |
| startl = 1; |
| return(c); |
| } |
| nlseen = 0; |
| e.linep = line; |
| atstart = startl; |
| startl = 0; |
| yylval.i = 0; |
| |
| loop: |
| while ((c = my_getc(0)) == ' ' || c == '\t') |
| ; |
| switch (c) { |
| default: |
| if (any(c, "0123456789")) { |
| unget(c1 = my_getc(0)); |
| if (c1 == '<' || c1 == '>') { |
| iounit = c - '0'; |
| goto loop; |
| } |
| *e.linep++ = c; |
| c = c1; |
| } |
| break; |
| |
| case '#': |
| while ((c = my_getc(0)) != 0 && c != '\n') |
| ; |
| unget(c); |
| goto loop; |
| |
| case 0: |
| return(c); |
| |
| case '$': |
| *e.linep++ = c; |
| if ((c = my_getc(0)) == '{') { |
| if ((c = collect(c, '}')) != '\0') |
| return(c); |
| goto pack; |
| } |
| break; |
| |
| case '`': |
| case '\'': |
| case '"': |
| if ((c = collect(c, c)) != '\0') |
| return(c); |
| goto pack; |
| |
| case '|': |
| case '&': |
| case ';': |
| if ((c1 = dual(c)) != '\0') { |
| startl = 1; |
| return(c1); |
| } |
| startl = 1; |
| return(c); |
| case '^': |
| startl = 1; |
| return('|'); |
| case '>': |
| case '<': |
| diag(c); |
| return(c); |
| |
| case '\n': |
| nlseen++; |
| gethere(); |
| startl = 1; |
| if (multiline || cf & CONTIN) { |
| if (interactive && e.iop <= iostack) { |
| #ifdef CONFIG_FEATURE_COMMAND_EDITING |
| current_prompt=cprompt->value; |
| #else |
| prs(cprompt->value); |
| #endif |
| } |
| if (cf & CONTIN) |
| goto loop; |
| } |
| return(c); |
| |
| case '(': |
| case ')': |
| startl = 1; |
| return(c); |
| } |
| |
| unget(c); |
| |
| pack: |
| while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n")) |
| if (e.linep >= elinep) |
| err("word too long"); |
| else |
| *e.linep++ = c; |
| unget(c); |
| if(any(c, "\"'`$")) |
| goto loop; |
| *e.linep++ = '\0'; |
| if (atstart && (c = rlookup(line))!=0) { |
| startl = 1; |
| return(c); |
| } |
| yylval.cp = strsave(line, areanum); |
| return(WORD); |
| } |
| |
| static int |
| collect(c, c1) |
| register int c, c1; |
| { |
| char s[2]; |
| |
| *e.linep++ = c; |
| while ((c = my_getc(c1)) != c1) { |
| if (c == 0) { |
| unget(c); |
| s[0] = c1; |
| s[1] = 0; |
| prs("no closing "); yyerror(s); |
| return(YYERRCODE); |
| } |
| if (interactive && c == '\n' && e.iop <= iostack) { |
| #ifdef CONFIG_FEATURE_COMMAND_EDITING |
| current_prompt=cprompt->value; |
| #else |
| prs(cprompt->value); |
| #endif |
| } |
| *e.linep++ = c; |
| } |
| *e.linep++ = c; |
| return(0); |
| } |
| |
| static int |
| dual(c) |
| register int c; |
| { |
| char s[3]; |
| register char *cp = s; |
| |
| *cp++ = c; |
| *cp++ = my_getc(0); |
| *cp = 0; |
| if ((c = rlookup(s)) == 0) |
| unget(*--cp); |
| return(c); |
| } |
| |
| static void |
| diag(ec) |
| register int ec; |
| { |
| register int c; |
| |
| c = my_getc(0); |
| if (c == '>' || c == '<') { |
| if (c != ec) |
| zzerr(); |
| yylval.i = ec == '>'? IOWRITE|IOCAT: IOHERE; |
| c = my_getc(0); |
| } else |
| yylval.i = ec == '>'? IOWRITE: IOREAD; |
| if (c != '&' || yylval.i == IOHERE) |
| unget(c); |
| else |
| yylval.i |= IODUP; |
| } |
| |
| static char * |
| tree(size) |
| unsigned size; |
| { |
| register char *t; |
| |
| if ((t = getcell(size)) == NULL) { |
| prs("command line too complicated\n"); |
| fail(); |
| /* NOTREACHED */ |
| } |
| return(t); |
| } |
| |
| /* VARARGS1 */ |
| /* ARGSUSED */ |
| |
| /* -------- exec.c -------- */ |
| |
| /* |
| * execute tree |
| */ |
| |
| |
| static int |
| execute(t, pin, pout, act) |
| register struct op *t; |
| int *pin, *pout; |
| int act; |
| { |
| register struct op *t1; |
| volatile int i, rv, a; |
| char *cp, **wp, **wp2; |
| struct var *vp; |
| struct brkcon bc; |
| |
| #if __GNUC__ |
| /* Avoid longjmp clobbering */ |
| (void) ℘ |
| #endif |
| |
| |
| if (t == NULL) |
| return(0); |
| rv = 0; |
| a = areanum++; |
| wp = (wp2 = t->words) != NULL |
| ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY) |
| : NULL; |
| |
| switch(t->type) { |
| case TPAREN: |
| rv = execute(t->left, pin, pout, 0); |
| break; |
| |
| case TCOM: |
| { |
| int child; |
| rv = forkexec(t, pin, pout, act, wp, &child); |
| if (child) { |
| exstat = rv; |
| leave(); |
| } |
| } |
| break; |
| |
| case TPIPE: |
| { |
| int pv[2]; |
| if ((rv = openpipe(pv)) < 0) |
| break; |
| pv[0] = remap(pv[0]); |
| pv[1] = remap(pv[1]); |
| (void) execute(t->left, pin, pv, 0); |
| rv = execute(t->right, pv, pout, 0); |
| } |
| break; |
| |
| case TLIST: |
| (void) execute(t->left, pin, pout, 0); |
| rv = execute(t->right, pin, pout, 0); |
| break; |
| |
| case TASYNC: |
| { |
| int hinteractive = interactive; |
| |
| i = vfork(); |
| if (i != 0) { |
| interactive = hinteractive; |
| if (i != -1) { |
| setval(lookup("!"), putn(i)); |
| if (pin != NULL) |
| closepipe(pin); |
| if (interactive) { |
| prs(putn(i)); |
| prs("\n"); |
| } |
| } else |
| rv = -1; |
| setstatus(rv); |
| } else { |
| signal(SIGINT, SIG_IGN); |
| signal(SIGQUIT, SIG_IGN); |
| if (interactive) |
| signal(SIGTERM, SIG_DFL); |
| interactive = 0; |
| if (pin == NULL) { |
| close(0); |
| open("/dev/null", 0); |
| } |
| exit(execute(t->left, pin, pout, FEXEC)); |
| } |
| } |
| break; |
| |
| case TOR: |
| case TAND: |
| rv = execute(t->left, pin, pout, 0); |
| if ((t1 = t->right)!=NULL && (rv == 0) == (t->type == TAND)) |
| rv = execute(t1, pin, pout, 0); |
| break; |
| |
| case TFOR: |
| if (wp == NULL) { |
| wp = dolv+1; |
| if ((i = dolc) < 0) |
| i = 0; |
| } else { |
| i = -1; |
| while (*wp++ != NULL) |
| ; |
| } |
| vp = lookup(t->str); |
| while (setjmp(bc.brkpt)) |
| if (isbreak) |
| goto broken; |
| brkset(&bc); |
| for (t1 = t->left; i-- && *wp != NULL;) { |
| setval(vp, *wp++); |
| rv = execute(t1, pin, pout, 0); |
| } |
| brklist = brklist->nextlev; |
| break; |
| |
| case TWHILE: |
| case TUNTIL: |
| while (setjmp(bc.brkpt)) |
| if (isbreak) |
| goto broken; |
| brkset(&bc); |
| t1 = t->left; |
| while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE)) |
| rv = execute(t->right, pin, pout, 0); |
| brklist = brklist->nextlev; |
| break; |
| |
| case TIF: |
| case TELIF: |
| if (t->right != NULL) { |
| rv = !execute(t->left, pin, pout, 0) ? |
| execute(t->right->left, pin, pout, 0): |
| execute(t->right->right, pin, pout, 0); |
| } |
| break; |
| |
| case TCASE: |
| if ((cp = evalstr(t->str, DOSUB|DOTRIM)) == 0) |
| cp = ""; |
| if ((t1 = findcase(t->left, cp)) != NULL) |
| rv = execute(t1, pin, pout, 0); |
| break; |
| |
| case TBRACE: |
| /* |
| if (iopp = t->ioact) |
| while (*iopp) |
| if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) { |
| rv = -1; |
| break; |
| } |
| */ |
| if (rv >= 0 && (t1 = t->left)) |
| rv = execute(t1, pin, pout, 0); |
| break; |
| } |
| |
| broken: |
| t->words = wp2; |
| isbreak = 0; |
| freehere(areanum); |
| freearea(areanum); |
| areanum = a; |
| if (interactive && intr) { |
| closeall(); |
| fail(); |
| } |
| if ((i = trapset) != 0) { |
| trapset = 0; |
| runtrap(i); |
| } |
| return(rv); |
| } |
| |
| static int |
| forkexec( register struct op *t, int *pin, int *pout, int act, char **wp, int *pforked) |
| { |
| int i, rv; |
| int (*shcom)(struct op *) = NULL; |
| register int f; |
| char *cp = NULL; |
| struct ioword **iopp; |
| int resetsig; |
| char **owp; |
| |
| int *hpin = pin; |
| int *hpout = pout; |
| int hforked; |
| char *hwp; |
| int hinteractive; |
| int hintr; |
| struct brkcon * hbrklist; |
| int hexecflg; |
| |
| #if __GNUC__ |
| /* Avoid longjmp clobbering */ |
| (void) &pin; |
| (void) &pout; |
| (void) ℘ |
| (void) &shcom; |
| (void) &cp; |
| (void) &resetsig; |
| (void) &owp; |
| #endif |
| |
| owp = wp; |
| resetsig = 0; |
| *pforked = 0; |
| rv = -1; /* system-detected error */ |
| if (t->type == TCOM) { |
| while ((cp = *wp++) != NULL) |
| ; |
| cp = *wp; |
| |
| /* strip all initial assignments */ |
| /* not correct wrt PATH=yyy command etc */ |
| if (flag['x']) |
| echo (cp ? wp: owp); |
| if (cp == NULL && t->ioact == NULL) { |
| while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
| ; |
| return(setstatus(0)); |
| } |
| else if (cp != NULL) |
| shcom = inbuilt(cp); |
| } |
| t->words = wp; |
| f = act; |
| if (shcom == NULL && (f & FEXEC) == 0) { |
| |
| hpin = pin; |
| hpout = pout; |
| hforked = *pforked; |
| hwp = *wp; |
| hinteractive = interactive; |
| hintr = intr; |
| hbrklist = brklist; |
| hexecflg = execflg; |
| |
| i = vfork(); |
| if (i != 0) { |
| /* who wrote this crappy non vfork safe shit? */ |
| pin = hpin; |
| pout = hpout; |
| *pforked = hforked; |
| *wp = hwp; |
| interactive = hinteractive; |
| intr = hintr; |
| brklist = hbrklist; |
| execflg = hexecflg; |
| |
| *pforked = 0; |
| if (i == -1) |
| return(rv); |
| if (pin != NULL) |
| closepipe(pin); |
| return(pout==NULL? setstatus(waitfor(i,0)): 0); |
| } |
| |
| if (interactive) { |
| signal(SIGINT, SIG_IGN); |
| signal(SIGQUIT, SIG_IGN); |
| resetsig = 1; |
| } |
| interactive = 0; |
| intr = 0; |
| (*pforked)++; |
| brklist = 0; |
| execflg = 0; |
| } |
| if (owp != NULL) |
| while ((cp = *owp++) != NULL && assign(cp, COPYV)) |
| if (shcom == NULL) |
| export(lookup(cp)); |
| #ifdef COMPIPE |
| if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) { |
| err("piping to/from shell builtins not yet done"); |
| return(-1); |
| } |
| #endif |
| if (pin != NULL) { |
| dup2(pin[0], 0); |
| closepipe(pin); |
| } |
| if (pout != NULL) { |
| dup2(pout[1], 1); |
| closepipe(pout); |
| } |
| if ((iopp = t->ioact) != NULL) { |
| if (shcom != NULL && shcom != doexec) { |
| prs(cp); |
| err(": cannot redirect shell command"); |
| return(-1); |
| } |
| while (*iopp) |
| if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) |
| return(rv); |
| } |
| if (shcom) |
| return(setstatus((*shcom)(t))); |
| /* should use FIOCEXCL */ |
| for (i=FDBASE; i<NOFILE; i++) |
| close(i); |
| if (resetsig) { |
| signal(SIGINT, SIG_DFL); |
| signal(SIGQUIT, SIG_DFL); |
| } |
| if (t->type == TPAREN) |
| exit(execute(t->left, NOPIPE, NOPIPE, FEXEC)); |
| if (wp[0] == NULL) |
| exit(0); |
| |
| cp = rexecve(wp[0], wp, makenv()); |
| prs(wp[0]); prs(": "); warn(cp); |
| if (!execflg) |
| trap[0] = NULL; |
| leave(); |
| /* NOTREACHED */ |
| exit(1); |
| } |
| |
| /* |
| * 0< 1> are ignored as required |
| * within pipelines. |
| */ |
| static int |
| iosetup(iop, pipein, pipeout) |
| register struct ioword *iop; |
| int pipein, pipeout; |
| { |
| register int u = -1; |
| char *cp=NULL, *msg; |
| |
| if (iop->io_unit == IODEFAULT) /* take default */ |
| iop->io_unit = iop->io_flag&(IOREAD|IOHERE)? 0: 1; |
| if (pipein && iop->io_unit == 0) |
| return(0); |
| if (pipeout && iop->io_unit == 1) |
| return(0); |
| msg = iop->io_flag&(IOREAD|IOHERE)? "open": "create"; |
| if ((iop->io_flag & IOHERE) == 0) { |
| cp = iop->io_name; |
| if ((cp = evalstr(cp, DOSUB|DOTRIM)) == NULL) |
| return(1); |
| } |
| if (iop->io_flag & IODUP) { |
| if (cp[1] || (!isdigit(*cp) && *cp != '-')) { |
| prs(cp); |
| err(": illegal >& argument"); |
| return(1); |
| } |
| if (*cp == '-') |
| iop->io_flag = IOCLOSE; |
| iop->io_flag &= ~(IOREAD|IOWRITE); |
| } |
| switch (iop->io_flag) { |
| case IOREAD: |
| u = open(cp, 0); |
| break; |
| |
| case IOHERE: |
| case IOHERE|IOXHERE: |
| u = herein(iop->io_name, iop->io_flag&IOXHERE); |
| cp = "here file"; |
| break; |
| |
| case IOWRITE|IOCAT: |
| if ((u = open(cp, 1)) >= 0) { |
| lseek(u, (long)0, 2); |
| break; |
| } |
| case IOWRITE: |
| u = creat(cp, 0666); |
| break; |
| |
| case IODUP: |
| u = dup2(*cp-'0', iop->io_unit); |
| break; |
| |
| case IOCLOSE: |
| close(iop->io_unit); |
| return(0); |
| } |
| if (u < 0) { |
| prs(cp); |
| prs(": cannot "); |
| warn(msg); |
| return(1); |
| } else { |
| if (u != iop->io_unit) { |
| dup2(u, iop->io_unit); |
| close(u); |
| } |
| } |
| return(0); |
| } |
| |
| static void |
| echo(wp) |
| register char **wp; |
| { |
| register int i; |
| |
| prs("+"); |
| for (i=0; wp[i]; i++) { |
| if (i) |
| prs(" "); |
| prs(wp[i]); |
| } |
| prs("\n"); |
| } |
| |
| static struct op ** |
| find1case(t, w) |
| struct op *t; |
| char *w; |
| { |
| register struct op *t1; |
| struct op **tp; |
| register char **wp, *cp; |
| |
| if (t == NULL) |
| return((struct op **)NULL); |
| if (t->type == TLIST) { |
| if ((tp = find1case(t->left, w)) != NULL) |
| return(tp); |
| t1 = t->right; /* TPAT */ |
| } else |
| t1 = t; |
| for (wp = t1->words; *wp;) |
| if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp)) |
| return(&t1->left); |
| return((struct op **)NULL); |
| } |
| |
| static struct op * |
| findcase(t, w) |
| struct op *t; |
| char *w; |
| { |
| register struct op **tp; |
| |
| return((tp = find1case(t, w)) != NULL? *tp: (struct op *)NULL); |
| } |
| |
| /* |
| * Enter a new loop level (marked for break/continue). |
| */ |
| static void |
| brkset(bc) |
| struct brkcon *bc; |
| { |
| bc->nextlev = brklist; |
| brklist = bc; |
| } |
| |
| /* |
| * Wait for the last process created. |
| * Print a message for each process found |
| * that was killed by a signal. |
| * Ignore interrupt signals while waiting |
| * unless `canintr' is true. |
| */ |
| static int |
| waitfor(lastpid, canintr) |
| register int lastpid; |
| int canintr; |
| { |
| register int pid, rv; |
| int s; |
| int oheedint = heedint; |
| |
| heedint = 0; |
| rv = 0; |
| do { |
| pid = wait(&s); |
| if (pid == -1) { |
| if (errno != EINTR || canintr) |
| break; |
| } else { |
| if ((rv = WAITSIG(s)) != 0) { |
| if (rv < NSIGNAL) { |
| if (signame[rv] != NULL) { |
| if (pid != lastpid) { |
| prn(pid); |
| prs(": "); |
| } |
| prs(signame[rv]); |
| } |
| } else { |
| if (pid != lastpid) { |
| prn(pid); |
| prs(": "); |
| } |
| prs("Signal "); prn(rv); prs(" "); |
| } |
| if (WAITCORE(s)) |
| prs(" - core dumped"); |
| if (rv >= NSIGNAL || signame[rv]) |
| prs("\n"); |
| rv = -1; |
| } else |
| rv = WAITVAL(s); |
| } |
| } while (pid != lastpid); |
| heedint = oheedint; |
| if (intr) { |
| if (interactive) { |
| if (canintr) |
| intr = 0; |
| } else { |
| if (exstat == 0) exstat = rv; |
| onintr(0); |
| } |
| } |
| return(rv); |
| } |
| |
| static int |
| setstatus(s) |
| register int s; |
| { |
| exstat = s; |
| setval(lookup("?"), putn(s)); |
| return(s); |
| } |
| |
| /* |
| * PATH-searching interface to execve. |
| * If getenv("PATH") were kept up-to-date, |
| * execvp might be used. |
| */ |
| static char * |
| rexecve(c, v, envp) |
| char *c, **v, **envp; |
| { |
| register int i; |
| register char *sp, *tp; |
| int eacces = 0, asis = 0; |
| |
| #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
| char *name = c; |
| optind = 1; |
| if (find_applet_by_name(name)) { |
| /* We have to exec here since we vforked. Running |
| * run_applet_by_name() won't work and bad things |
| * will happen. */ |
| execve("/proc/self/exe", v, envp); |
| execve("busybox", v, envp); |
| } |
| #endif |
| |
| sp = any('/', c)? "": path->value; |
| asis = *sp == '\0'; |
| while (asis || *sp != '\0') { |
| asis = 0; |
| tp = e.linep; |
| for (; *sp != '\0'; tp++) |
| if ((*tp = *sp++) == ':') { |
| asis = *sp == '\0'; |
| break; |
| } |
| if (tp != e.linep) |
| *tp++ = '/'; |
| for (i = 0; (*tp++ = c[i++]) != '\0';) |
| ; |
| |
| execve(e.linep, v, envp); |
| switch (errno) { |
| case ENOEXEC: |
| *v = e.linep; |
| tp = *--v; |
| *v = e.linep; |
| execve(DEFAULT_SHELL, v, envp); |
| *v = tp; |
| return("no Shell"); |
| |
| case ENOMEM: |
| return((char*)bb_msg_memory_exhausted); |
| |
| case E2BIG: |
| return("argument list too long"); |
| |
| case EACCES: |
| eacces++; |
| break; |
| } |
| } |
| return(errno==ENOENT ? "not found" : "cannot execute"); |
| } |
| |
| /* |
| * Run the command produced by generator `f' |
| * applied to stream `arg'. |
| */ |
| static int |
| run(struct ioarg *argp, int (*f)(struct ioarg *)) |
| { |
| struct op *otree; |
| struct wdblock *swdlist; |
| struct wdblock *siolist; |
| jmp_buf ev, rt; |
| xint *ofail; |
| int rv; |
| |
| #if __GNUC__ |
| /* Avoid longjmp clobbering */ |
| (void) &rv; |
| #endif |
| |
| areanum++; |
| swdlist = wdlist; |
| siolist = iolist; |
| otree = outtree; |
| ofail = failpt; |
| rv = -1; |
| if (newenv(setjmp(errpt = ev)) == 0) { |
| wdlist = 0; |
| iolist = 0; |
| pushio(argp, f); |
| e.iobase = e.iop; |
| yynerrs = 0; |
| if (setjmp(failpt = rt) == 0 && yyparse() == 0) |
| rv = execute(outtree, NOPIPE, NOPIPE, 0); |
| quitenv(); |
| } |
| wdlist = swdlist; |
| iolist = siolist; |
| failpt = ofail; |
| outtree = otree; |
| freearea(areanum--); |
| return(rv); |
| } |
| |
| /* -------- do.c -------- */ |
| |
| /* |
| * built-in commands: doX |
| */ |
| |
| static int dohelp(struct op *t ) |
| { |
| int col; |
| const struct builtincmd *x; |
| |
| printf("\nBuilt-in commands:\n"); |
| printf("-------------------\n"); |
| |
| for (col=0, x = builtincmds; x->builtinfunc != NULL; x++) { |
| if (!x->name) |
| continue; |
| col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name); |
| if (col > 60) { |
| printf("\n"); |
| col = 0; |
| } |
| } |
| #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
| { |
| int i; |
| const struct BB_applet *applet; |
| extern const struct BB_applet applets[]; |
| extern const size_t NUM_APPLETS; |
| |
| for (i=0, applet = applets; i < NUM_APPLETS; applet++, i++) { |
| if (!applet->name) |
| continue; |
| |
| col += printf("%s%s", ((col == 0) ? "\t" : " "), |
| applet->name); |
| if (col > 60) { |
| printf("\n"); |
| col = 0; |
| } |
| } |
| } |
| #endif |
| printf("\n\n"); |
| return EXIT_SUCCESS; |
| } |
| |
| |
| |
| static int dolabel(struct op *t ) |
| { |
| return(0); |
| } |
| |
| static int |
| dochdir(t) |
| register struct op *t; |
| { |
| register char *cp, *er; |
| |
| if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL) |
| er = ": no home directory"; |
| else if(chdir(cp) < 0) |
| er = ": bad directory"; |
| else |
| return(0); |
| prs(cp != NULL? cp: "cd"); |
| err(er); |
| return(1); |
| } |
| |
| static int |
| doshift(t) |
| register struct op *t; |
| { |
| register int n; |
| |
| n = t->words[1]? getn(t->words[1]): 1; |
| if(dolc < n) { |
| err("nothing to shift"); |
| return(1); |
| } |
| dolv[n] = dolv[0]; |
| dolv += n; |
| dolc -= n; |
| setval(lookup("#"), putn(dolc)); |
| return(0); |
| } |
| |
| /* |
| * execute login and newgrp directly |
| */ |
| static int |
| dologin(t) |
| struct op *t; |
| { |
| register char *cp; |
| |
| if (interactive) { |
| signal(SIGINT, SIG_DFL); |
| signal(SIGQUIT, SIG_DFL); |
| } |
| cp = rexecve(t->words[0], t->words, makenv()); |
| prs(t->words[0]); prs(": "); err(cp); |
| return(1); |
| } |
| |
| static int |
| |