blob: 62e7844a857cd1a5ed60d98d4167cd419b7f3320 [file] [log] [blame]
Eric Andersenff9eee42001-06-29 04:57:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Minix shell port for busybox
4 *
5 * This version of the Minix shell was adapted for use in busybox
Eric Andersencb81e642003-07-14 21:21:08 +00006 * by Erik Andersen <andersen@codepoet.org>
Eric Andersenff9eee42001-06-29 04:57:14 +00007 *
Eric Andersen737f5fb2003-03-14 16:05:59 +00008 * - backtick expansion did not work properly
9 * Jonas Holmberg <jonas.holmberg@axis.com>
10 * Robert Schwebel <r.schwebel@pengutronix.de>
Eric Andersencb81e642003-07-14 21:21:08 +000011 * Erik Andersen <andersen@codepoet.org>
Eric Andersen737f5fb2003-03-14 16:05:59 +000012 *
Eric Andersenff9eee42001-06-29 04:57:14 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Eric Andersenc7bda1c2004-03-15 08:29:22 +000026 *
Eric Andersenff9eee42001-06-29 04:57:14 +000027 * Original copyright notice is retained at the end of this file.
28 */
29
30#include <ctype.h>
31#include <dirent.h>
32#include <errno.h>
33#include <fcntl.h>
34#include <limits.h>
35#include <setjmp.h>
36#include <signal.h>
37#include <stddef.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <time.h>
42#include <unistd.h>
43#include <sys/stat.h>
44#include <sys/times.h>
45#include <sys/types.h>
46#include <sys/wait.h>
47
48#include "cmdedit.h"
49#include "busybox.h"
50
51
52/* -------- sh.h -------- */
53/*
54 * shell
55 */
56
57#define LINELIM 2100
58#define NPUSH 8 /* limit to input nesting */
59
Eric Andersen392947c2002-12-11 07:42:46 +000060#undef NOFILE
Eric Andersenff9eee42001-06-29 04:57:14 +000061#define NOFILE 20 /* Number of open files */
62#define NUFILE 10 /* Number of user-accessible files */
63#define FDBASE 10 /* First file usable by Shell */
64
65/*
66 * values returned by wait
67 */
68#define WAITSIG(s) ((s)&0177)
69#define WAITVAL(s) (((s)>>8)&0377)
70#define WAITCORE(s) (((s)&0200)!=0)
71
72/*
Eric Andersenaff114c2004-04-14 17:51:38 +000073 * library and system definitions
Eric Andersenff9eee42001-06-29 04:57:14 +000074 */
75typedef void xint; /* base type of jmp_buf, for not broken compilers */
76
77/*
78 * shell components
79 */
80
81#define QUOTE 0200
82
83#define NOBLOCK ((struct op *)NULL)
84#define NOWORD ((char *)NULL)
85#define NOWORDS ((char **)NULL)
86#define NOPIPE ((int *)NULL)
87
88/*
89 * Description of a command or an operation on commands.
90 * Might eventually use a union.
91 */
92struct op {
93 int type; /* operation type, see below */
94 char **words; /* arguments to a command */
95 struct ioword **ioact; /* IO actions (eg, < > >>) */
96 struct op *left;
97 struct op *right;
98 char *str; /* identifier for case and for */
99};
100
101#define TCOM 1 /* command */
102#define TPAREN 2 /* (c-list) */
103#define TPIPE 3 /* a | b */
104#define TLIST 4 /* a [&;] b */
105#define TOR 5 /* || */
106#define TAND 6 /* && */
107#define TFOR 7
108#define TDO 8
109#define TCASE 9
110#define TIF 10
111#define TWHILE 11
112#define TUNTIL 12
113#define TELIF 13
114#define TPAT 14 /* pattern in case */
115#define TBRACE 15 /* {c-list} */
116#define TASYNC 16 /* c & */
117
118/*
119 * actions determining the environment of a process
120 */
121#define BIT(i) (1<<(i))
122#define FEXEC BIT(0) /* execute without forking */
123
124/*
125 * flags to control evaluation of words
126 */
127#define DOSUB 1 /* interpret $, `, and quotes */
128#define DOBLANK 2 /* perform blank interpretation */
129#define DOGLOB 4 /* interpret [?* */
130#define DOKEY 8 /* move words with `=' to 2nd arg. list */
131#define DOTRIM 16 /* trim resulting string */
132
133#define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
134
135static char **dolv;
136static int dolc;
137static int exstat;
138static char gflg;
139static int interactive; /* Is this an interactive shell */
140static int execflg;
141static int multiline; /* \n changed to ; */
142static struct op *outtree; /* result from parser */
143
144static xint *failpt;
145static xint *errpt;
146static struct brkcon *brklist;
147static int isbreak;
148static int newfile(char *s);
149static char *findeq(char *cp);
150static char *cclass(char *p, int sub);
151static void initarea(void);
Matt Kraai2d91deb2001-08-01 17:21:35 +0000152extern int msh_main(int argc, char **argv);
Eric Andersenff9eee42001-06-29 04:57:14 +0000153
154
155struct brkcon {
156 jmp_buf brkpt;
157 struct brkcon *nextlev;
158} ;
159
160/*
161 * redirection
162 */
163struct ioword {
164 short io_unit; /* unit affected */
165 short io_flag; /* action (below) */
166 char *io_name; /* file name */
167};
168#define IOREAD 1 /* < */
169#define IOHERE 2 /* << (here file) */
170#define IOWRITE 4 /* > */
171#define IOCAT 8 /* >> */
172#define IOXHERE 16 /* ${}, ` in << */
173#define IODUP 32 /* >&digit */
174#define IOCLOSE 64 /* >&- */
175
176#define IODEFAULT (-1) /* token for default IO unit */
177
178static struct wdblock *wdlist;
179static struct wdblock *iolist;
180
181/*
182 * parsing & execution environment
183 */
184static struct env {
185 char *linep;
186 struct io *iobase;
187 struct io *iop;
188 xint *errpt;
189 int iofd;
190 struct env *oenv;
191} e;
192
193/*
194 * flags:
195 * -e: quit on error
196 * -k: look for name=value everywhere on command line
197 * -n: no execution
198 * -t: exit after reading and executing one command
199 * -v: echo as read
200 * -x: trace
201 * -u: unset variables net diagnostic
202 */
203static char *flag;
204
205static char *null; /* null value for variable */
206static int intr; /* interrupt pending */
207
208static char *trap[_NSIG+1];
209static char ourtrap[_NSIG+1];
210static int trapset; /* trap pending */
211
212static int heedint; /* heed interrupt signals */
213
214static int yynerrs; /* yacc */
215
216static char line[LINELIM];
217static char *elinep;
218
219/*
220 * other functions
221 */
Eric Andersen392947c2002-12-11 07:42:46 +0000222static int(*inbuilt(char *s))(struct op *);
223
Eric Andersenff9eee42001-06-29 04:57:14 +0000224
225static char *rexecve (char *c , char **v, char **envp );
226static char *space (int n );
227static char *strsave (char *s, int a );
228static char *evalstr (char *cp, int f );
229static char *putn (int n );
Eric Andersen737f5fb2003-03-14 16:05:59 +0000230static char *itoa (int n );
Eric Andersenff9eee42001-06-29 04:57:14 +0000231static char *unquote (char *as );
232static struct var *lookup (char *n );
233static int rlookup (char *n );
234static struct wdblock *glob (char *cp, struct wdblock *wb );
235static int my_getc( int ec);
236static int subgetc (int ec, int quoted );
237static char **makenv (void);
238static char **eval (char **ap, int f );
239static int setstatus (int s );
240static int waitfor (int lastpid, int canintr );
241
242static void onintr (int s ); /* SIGINT handler */
243
244static int newenv (int f );
245static void quitenv (void);
246static void err (char *s );
247static int anys (char *s1, char *s2 );
248static int any (int c, char *s );
249static void next (int f );
250static void setdash (void);
251static void onecommand (void);
252static void runtrap (int i );
253static int gmatch (char *s, char *p );
254
255/*
256 * error handling
257 */
258static void leave (void); /* abort shell (or fail in subshell) */
259static void fail (void); /* fail but return to process next command */
260static void warn (char *s );
261static void sig (int i ); /* default signal handler */
262
263
264
265/* -------- area stuff -------- */
266
267#define REGSIZE sizeof(struct region)
268#define GROWBY 256
269//#define SHRINKBY 64
270#undef SHRINKBY
271#define FREE 32767
272#define BUSY 0
273#define ALIGN (sizeof(int)-1)
274
275
276struct region {
277 struct region *next;
278 int area;
279};
280
281
282
283/* -------- grammar stuff -------- */
284typedef union {
285 char *cp;
286 char **wp;
287 int i;
288 struct op *o;
289} YYSTYPE;
290#define WORD 256
291#define LOGAND 257
292#define LOGOR 258
293#define BREAK 259
294#define IF 260
295#define THEN 261
296#define ELSE 262
297#define ELIF 263
298#define FI 264
299#define CASE 265
300#define ESAC 266
301#define FOR 267
302#define WHILE 268
303#define UNTIL 269
304#define DO 270
305#define DONE 271
306#define IN 272
307#define YYERRCODE 300
308
309/* flags to yylex */
310#define CONTIN 01 /* skip new lines to complete command */
311
312#define SYNTAXERR zzerr()
313static struct op *pipeline(int cf );
314static struct op *andor(void);
315static struct op *c_list(void);
316static int synio(int cf );
317static void musthave (int c, int cf );
318static struct op *simple(void);
319static struct op *nested(int type, int mark );
320static struct op *command(int cf );
321static struct op *dogroup(int onlydone );
322static struct op *thenpart(void);
323static struct op *elsepart(void);
324static struct op *caselist(void);
325static struct op *casepart(void);
326static char **pattern(void);
327static char **wordlist(void);
328static struct op *list(struct op *t1, struct op *t2 );
329static struct op *block(int type, struct op *t1, struct op *t2, char **wp );
330static struct op *newtp(void);
331static struct op *namelist(struct op *t );
332static char **copyw(void);
333static void word(char *cp );
334static struct ioword **copyio(void);
335static struct ioword *io (int u, int f, char *cp );
336static void zzerr(void);
337static void yyerror(char *s );
338static int yylex(int cf );
339static int collect(int c, int c1 );
340static int dual(int c );
341static void diag(int ec );
342static char *tree(unsigned size );
343
344/* -------- var.h -------- */
345
346struct var {
347 char *value;
348 char *name;
349 struct var *next;
350 char status;
351};
352#define COPYV 1 /* flag to setval, suggesting copy */
353#define RONLY 01 /* variable is read-only */
354#define EXPORT 02 /* variable is to be exported */
355#define GETCELL 04 /* name & value space was got with getcell */
356
357static struct var *vlist; /* dictionary */
358
359static struct var *homedir; /* home directory */
360static struct var *prompt; /* main prompt */
361static struct var *cprompt; /* continuation prompt */
362static struct var *path; /* search path for commands */
363static struct var *shell; /* shell to interpret command files */
364static struct var *ifs; /* field separators */
365
366static int yyparse (void);
367static struct var *lookup (char *n );
368static void setval (struct var *vp, char *val );
369static void nameval (struct var *vp, char *val, char *name );
370static void export (struct var *vp );
371static void ronly (struct var *vp );
372static int isassign (char *s );
373static int checkname (char *cp );
374static int assign (char *s, int cf );
375static void putvlist (int f, int out );
376static int eqname (char *n1, char *n2 );
377
378static int execute (struct op *t, int *pin, int *pout, int act );
379
380/* -------- io.h -------- */
381/* io buffer */
382struct iobuf {
383 unsigned id; /* buffer id */
384 char buf[512]; /* buffer */
385 char *bufp; /* pointer into buffer */
386 char *ebufp; /* pointer to end of buffer */
387};
388
389/* possible arguments to an IO function */
390struct ioarg {
391 char *aword;
392 char **awordlist;
393 int afile; /* file descriptor */
394 unsigned afid; /* buffer id */
395 long afpos; /* file position */
396 struct iobuf *afbuf; /* buffer for this file */
397};
398//static struct ioarg ioargstack[NPUSH];
399#define AFID_NOBUF (~0)
400#define AFID_ID 0
401
402/* an input generator's state */
403struct io {
Eric Andersen392947c2002-12-11 07:42:46 +0000404 int (*iofn)(struct ioarg *, struct io *);
Eric Andersenff9eee42001-06-29 04:57:14 +0000405 struct ioarg *argp;
406 int peekc;
407 char prev; /* previous character read by readc() */
408 char nlcount; /* for `'s */
409 char xchar; /* for `'s */
410 char task; /* reason for pushed IO */
411};
412//static struct io iostack[NPUSH];
413#define XOTHER 0 /* none of the below */
414#define XDOLL 1 /* expanding ${} */
415#define XGRAVE 2 /* expanding `'s */
416#define XIO 3 /* file IO */
417
418/* in substitution */
419#define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL)
420
421/*
422 * input generators for IO structure
423 */
424static int nlchar (struct ioarg *ap );
425static int strchar (struct ioarg *ap );
426static int qstrchar (struct ioarg *ap );
427static int filechar (struct ioarg *ap );
428static int herechar (struct ioarg *ap );
429static int linechar (struct ioarg *ap );
430static int gravechar (struct ioarg *ap, struct io *iop );
431static int qgravechar (struct ioarg *ap, struct io *iop );
432static int dolchar (struct ioarg *ap );
433static int wdchar (struct ioarg *ap );
434static void scraphere (void);
435static void freehere (int area );
436static void gethere (void);
437static void markhere (char *s, struct ioword *iop );
438static int herein (char *hname, int xdoll );
Eric Andersen392947c2002-12-11 07:42:46 +0000439static int run (struct ioarg *argp, int (*f)(struct ioarg *));
Eric Andersenff9eee42001-06-29 04:57:14 +0000440
441/*
442 * IO functions
443 */
444static int eofc (void);
445static int readc (void);
446static void unget (int c );
447static void ioecho (int c );
448static void prs (char *s );
449static void prn (unsigned u );
450static void closef (int i );
451static void closeall (void);
452
453/*
454 * IO control
455 */
Eric Andersen392947c2002-12-11 07:42:46 +0000456static void pushio (struct ioarg *argp, int (*f)(struct ioarg *));
Eric Andersenff9eee42001-06-29 04:57:14 +0000457static int remap (int fd );
458static int openpipe (int *pv );
459static void closepipe (int *pv );
460static struct io *setbase (struct io *ip );
461
462static struct ioarg temparg; /* temporary for PUSHIO */
463#define PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen)))
464#define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen)))
465
466/* -------- word.h -------- */
467
468#define NSTART 16 /* default number of words to allow for initially */
469
470struct wdblock {
471 short w_bsize;
472 short w_nword;
473 /* bounds are arbitrary */
474 char *w_words[1];
475};
476
477static struct wdblock *addword (char *wd, struct wdblock *wb );
478static struct wdblock *newword (int nw );
479static char **getwords (struct wdblock *wb );
480
481/* -------- area.h -------- */
482
483/*
484 * storage allocation
485 */
486static char *getcell (unsigned nbytes );
487static void garbage (void);
488static void setarea (char *cp, int a );
489static int getarea (char *cp );
490static void freearea (int a );
491static void freecell (char *cp );
492static int areanum; /* current allocation area */
493
494#define NEW(type) (type *)getcell(sizeof(type))
495#define DELETE(obj) freecell((char *)obj)
496
497
498/* -------- misc stuff -------- */
499
500static int forkexec (struct op *t, int *pin, int *pout, int act, char **wp, int *pforked );
501static int iosetup (struct ioword *iop, int pipein, int pipeout );
502static void echo(char **wp );
503static struct op **find1case (struct op *t, char *w );
504static struct op *findcase (struct op *t, char *w );
505static void brkset(struct brkcon *bc );
Eric Andersen392947c2002-12-11 07:42:46 +0000506static int dolabel(struct op *t );
507static int dohelp(struct op *t );
Eric Andersenff9eee42001-06-29 04:57:14 +0000508static int dochdir(struct op *t );
509static int doshift(struct op *t );
510static int dologin(struct op *t );
511static int doumask(struct op *t );
512static int doexec(struct op *t );
513static int dodot(struct op *t );
514static int dowait(struct op *t );
515static int doread(struct op *t );
516static int doeval(struct op *t );
517static int dotrap(struct op *t );
518static int getsig(char *s );
Eric Andersen392947c2002-12-11 07:42:46 +0000519static void setsig (int n, sighandler_t f);
Eric Andersenff9eee42001-06-29 04:57:14 +0000520static int getn(char *as );
521static int dobreak(struct op *t );
522static int docontinue(struct op *t );
523static int brkcontin (char *cp, int val );
524static int doexit(struct op *t );
525static int doexport(struct op *t );
526static int doreadonly(struct op *t );
Eric Andersen392947c2002-12-11 07:42:46 +0000527static void rdexp (char **wp, void (*f)(struct var *), int key);
Eric Andersenff9eee42001-06-29 04:57:14 +0000528static void badid(char *s );
529static int doset(struct op *t );
530static void varput (char *s, int out );
Eric Andersen392947c2002-12-11 07:42:46 +0000531static int dotimes(struct op *t );
Eric Andersenff9eee42001-06-29 04:57:14 +0000532static int expand (char *cp, struct wdblock **wbp, int f );
533static char *blank(int f );
534static int dollar(int quoted );
535static int grave(int quoted );
536static void globname (char *we, char *pp );
537static char *generate (char *start1, char *end1, char *middle, char *end );
538static int anyspcl(struct wdblock *wb );
539static int xstrcmp (char *p1, char *p2 );
540static void glob0 (char *a0, unsigned int a1, int a2, int (*a3)(char *, char *));
541static void glob1 (char *base, char *lim );
542static void glob2 (char *i, char *j );
543static void glob3 (char *i, char *j, char *k );
544static void readhere (char **name, char *s, int ec );
Eric Andersen392947c2002-12-11 07:42:46 +0000545static void pushio (struct ioarg *argp, int (*f)(struct ioarg *));
Eric Andersenff9eee42001-06-29 04:57:14 +0000546static int xxchar(struct ioarg *ap );
547
548struct here {
549 char *h_tag;
550 int h_dosub;
551 struct ioword *h_iop;
552 struct here *h_next;
553};
554
555static char *signame[] = {
556 "Signal 0",
557 "Hangup",
558 (char *)NULL, /* interrupt */
559 "Quit",
560 "Illegal instruction",
561 "Trace/BPT trap",
562 "Abort",
563 "Bus error",
564 "Floating Point Exception",
565 "Killed",
566 "SIGUSR1",
567 "SIGSEGV",
568 "SIGUSR2",
569 (char *)NULL, /* broken pipe */
570 "Alarm clock",
571 "Terminated",
572};
573#define NSIGNAL (sizeof(signame)/sizeof(signame[0]))
574
575struct res {
576 char *r_name;
577 int r_val;
578};
579static struct res restab[] = {
580 {"for", FOR},
581 {"case", CASE},
582 {"esac", ESAC},
583 {"while", WHILE},
584 {"do", DO},
585 {"done", DONE},
586 {"if", IF},
587 {"in", IN},
588 {"then", THEN},
589 {"else", ELSE},
590 {"elif", ELIF},
591 {"until", UNTIL},
592 {"fi", FI},
593
594 {";;", BREAK},
595 {"||", LOGOR},
596 {"&&", LOGAND},
597 {"{", '{'},
598 {"}", '}'},
599 {0, 0},
600};
601
602
Eric Andersen1c039232001-07-07 00:05:55 +0000603struct builtincmd {
604 const char *name;
Eric Andersen392947c2002-12-11 07:42:46 +0000605 int (*builtinfunc)(struct op *t);
Eric Andersenff9eee42001-06-29 04:57:14 +0000606};
Eric Andersen1c039232001-07-07 00:05:55 +0000607static const struct builtincmd builtincmds[] = {
608 {".", dodot},
Eric Andersenff9eee42001-06-29 04:57:14 +0000609 {":", dolabel},
Eric Andersenff9eee42001-06-29 04:57:14 +0000610 {"break", dobreak},
Eric Andersen1c039232001-07-07 00:05:55 +0000611 {"cd", dochdir},
Eric Andersenff9eee42001-06-29 04:57:14 +0000612 {"continue",docontinue},
Eric Andersen1c039232001-07-07 00:05:55 +0000613 {"eval", doeval},
614 {"exec", doexec},
Eric Andersenff9eee42001-06-29 04:57:14 +0000615 {"exit", doexit},
616 {"export", doexport},
Eric Andersen1c039232001-07-07 00:05:55 +0000617 {"help", dohelp},
Eric Andersenff9eee42001-06-29 04:57:14 +0000618 {"login", dologin},
619 {"newgrp", dologin},
Eric Andersen1c039232001-07-07 00:05:55 +0000620 {"read", doread},
621 {"readonly",doreadonly},
622 {"set", doset},
623 {"shift", doshift},
Eric Andersenff9eee42001-06-29 04:57:14 +0000624 {"times", dotimes},
Eric Andersen1c039232001-07-07 00:05:55 +0000625 {"trap", dotrap},
626 {"umask", doumask},
627 {"wait", dowait},
Eric Andersenff9eee42001-06-29 04:57:14 +0000628 {0,0}
629};
630
631/* Globals */
632extern char **environ; /* environment pointer */
633static char **dolv;
634static int dolc;
635static int exstat;
636static char gflg;
637static int interactive; /* Is this an interactive shell */
638static int execflg;
639static int multiline; /* \n changed to ; */
640static struct op *outtree; /* result from parser */
641static xint *failpt;
642static xint *errpt;
643static struct brkcon *brklist;
644static int isbreak;
645static struct wdblock *wdlist;
646static struct wdblock *iolist;
647static char *trap[_NSIG+1];
648static char ourtrap[_NSIG+1];
649static int trapset; /* trap pending */
650static int yynerrs; /* yacc */
651static char line[LINELIM];
652static struct var *vlist; /* dictionary */
653static struct var *homedir; /* home directory */
654static struct var *prompt; /* main prompt */
655static struct var *cprompt; /* continuation prompt */
656static struct var *path; /* search path for commands */
657static struct var *shell; /* shell to interpret command files */
658static struct var *ifs; /* field separators */
659static struct ioarg ioargstack[NPUSH];
660static struct io iostack[NPUSH];
661static int areanum; /* current allocation area */
662static int intr;
663static int inparse;
664static char flags['z'-'a'+1];
665static char *flag = flags-'a';
666static char *elinep = line+sizeof(line)-5;
667static char *null = "";
668static int heedint =1;
669static struct env e ={line, iostack, iostack-1, (xint *)NULL, FDBASE, (struct env *)NULL};
670static void (*qflag)(int) = SIG_IGN;
Eric Andersenff9eee42001-06-29 04:57:14 +0000671static int startl;
672static int peeksym;
673static int nlseen;
674static int iounit = IODEFAULT;
675static YYSTYPE yylval;
676static struct iobuf sharedbuf = {AFID_NOBUF};
677static struct iobuf mainbuf = {AFID_NOBUF};
678static unsigned bufid = AFID_ID; /* buffer id counter */
679static struct ioarg temparg = {0, 0, 0, AFID_NOBUF, 0};
680static struct here *inhere; /* list of hear docs while parsing */
681static struct here *acthere; /* list of active here documents */
682static struct region *areabot; /* bottom of area */
683static struct region *areatop; /* top of area */
684static struct region *areanxt; /* starting point of scan */
685static void * brktop;
686static void * brkaddr;
687
688
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000689#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersen7467c8d2001-07-12 20:26:32 +0000690static char * current_prompt;
Eric Andersenff9eee42001-06-29 04:57:14 +0000691#endif
692
Eric Andersenff9eee42001-06-29 04:57:14 +0000693/* -------- sh.c -------- */
694/*
695 * shell
696 */
697
698
Matt Kraai2d91deb2001-08-01 17:21:35 +0000699extern int msh_main(int argc, char **argv)
Eric Andersenff9eee42001-06-29 04:57:14 +0000700{
701 register int f;
702 register char *s;
703 int cflag;
704 char *name, **ap;
Eric Andersen392947c2002-12-11 07:42:46 +0000705 int (*iof)(struct ioarg *);
Eric Andersenff9eee42001-06-29 04:57:14 +0000706
707 initarea();
708 if ((ap = environ) != NULL) {
709 while (*ap)
710 assign(*ap++, !COPYV);
711 for (ap = environ; *ap;)
712 export(lookup(*ap++));
713 }
714 closeall();
715 areanum = 1;
716
717 shell = lookup("SHELL");
718 if (shell->value == null)
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000719 setval(shell, DEFAULT_SHELL);
Eric Andersenff9eee42001-06-29 04:57:14 +0000720 export(shell);
721
722 homedir = lookup("HOME");
723 if (homedir->value == null)
724 setval(homedir, "/");
725 export(homedir);
726
Eric Andersen737f5fb2003-03-14 16:05:59 +0000727 setval(lookup("$"), putn(getpid()));
Eric Andersenff9eee42001-06-29 04:57:14 +0000728
729 path = lookup("PATH");
Eric Andersen737f5fb2003-03-14 16:05:59 +0000730 if (path->value == null) {
731 if (geteuid() == 0)
732 setval(path, "/sbin:/bin:/usr/sbin:/usr/bin");
733 else
734 setval(path, "/bin:/usr/bin");
735 }
Eric Andersenff9eee42001-06-29 04:57:14 +0000736 export(path);
737
738 ifs = lookup("IFS");
739 if (ifs->value == null)
740 setval(ifs, " \t\n");
741
742 prompt = lookup("PS1");
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000743#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenff9eee42001-06-29 04:57:14 +0000744 if (prompt->value == null)
Eric Andersen1c039232001-07-07 00:05:55 +0000745#endif
Eric Andersenff9eee42001-06-29 04:57:14 +0000746 setval(prompt, "$ ");
747 if (geteuid() == 0) {
748 setval(prompt, "# ");
749 prompt->status &= ~EXPORT;
750 }
751 cprompt = lookup("PS2");
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000752#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenff9eee42001-06-29 04:57:14 +0000753 if (cprompt->value == null)
Eric Andersen1c039232001-07-07 00:05:55 +0000754#endif
Eric Andersenff9eee42001-06-29 04:57:14 +0000755 setval(cprompt, "> ");
756
757 iof = filechar;
758 cflag = 0;
759 name = *argv++;
760 if (--argc >= 1) {
761 if(argv[0][0] == '-' && argv[0][1] != '\0') {
762 for (s = argv[0]+1; *s; s++)
763 switch (*s) {
764 case 'c':
765 prompt->status &= ~EXPORT;
766 cprompt->status &= ~EXPORT;
767 setval(prompt, "");
768 setval(cprompt, "");
769 cflag = 1;
770 if (--argc > 0)
771 PUSHIO(aword, *++argv, iof = nlchar);
772 break;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000773
Eric Andersenff9eee42001-06-29 04:57:14 +0000774 case 'q':
775 qflag = SIG_DFL;
776 break;
777
778 case 's':
779 /* standard input */
780 break;
781
782 case 't':
783 prompt->status &= ~EXPORT;
784 setval(prompt, "");
785 iof = linechar;
786 break;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000787
Eric Andersenff9eee42001-06-29 04:57:14 +0000788 case 'i':
789 interactive++;
790 default:
791 if (*s>='a' && *s<='z')
792 flag[(int)*s]++;
793 }
794 } else {
795 argv--;
796 argc++;
797 }
798 if (iof == filechar && --argc > 0) {
799 setval(prompt, "");
800 setval(cprompt, "");
801 prompt->status &= ~EXPORT;
802 cprompt->status &= ~EXPORT;
803 if (newfile(name = *++argv))
804 exit(1);
805 }
806 }
807 setdash();
808 if (e.iop < iostack) {
809 PUSHIO(afile, 0, iof);
Eric Andersen1c039232001-07-07 00:05:55 +0000810 if (isatty(0) && isatty(1) && !cflag) {
Eric Andersenff9eee42001-06-29 04:57:14 +0000811 interactive++;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000812#ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
Eric Andersen1c039232001-07-07 00:05:55 +0000813 printf( "\n\n" BB_BANNER " Built-in shell (msh)\n");
814 printf( "Enter 'help' for a list of built-in commands.\n\n");
Eric Andersend63dee42001-10-19 00:22:23 +0000815#endif
Eric Andersen1c039232001-07-07 00:05:55 +0000816 }
Eric Andersenff9eee42001-06-29 04:57:14 +0000817 }
818 signal(SIGQUIT, qflag);
819 if (name && name[0] == '-') {
820 interactive++;
821 if ((f = open(".profile", 0)) >= 0)
822 next(remap(f));
823 if ((f = open("/etc/profile", 0)) >= 0)
824 next(remap(f));
825 }
826 if (interactive)
827 signal(SIGTERM, sig);
828 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
829 signal(SIGINT, onintr);
830 dolv = argv;
831 dolc = argc;
832 dolv[0] = name;
833 if (dolc > 1) {
834 for (ap = ++argv; --argc > 0;) {
835 if (assign(*ap = *argv++, !COPYV)) {
836 dolc--; /* keyword */
837 } else {
838 ap++;
839 }
840 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000841 }
Eric Andersenff9eee42001-06-29 04:57:14 +0000842 setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
843
844 for (;;) {
845 if (interactive && e.iop <= iostack) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000846#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersenff9eee42001-06-29 04:57:14 +0000847 current_prompt=prompt->value;
848#else
849 prs(prompt->value);
850#endif
851 }
852 onecommand();
Eric Andersen1c315012002-04-26 23:40:09 +0000853 /* Ensure that getenv("PATH") stays current */
854 setenv("PATH", path->value, 1);
Eric Andersenff9eee42001-06-29 04:57:14 +0000855 }
856}
857
858static void
859setdash()
860{
861 register char *cp;
862 register int c;
863 char m['z'-'a'+1];
864
865 cp = m;
866 for (c='a'; c<='z'; c++)
Eric Andersen737f5fb2003-03-14 16:05:59 +0000867 if (flag[(int)c])
Eric Andersenff9eee42001-06-29 04:57:14 +0000868 *cp++ = c;
869 *cp = 0;
870 setval(lookup("-"), m);
871}
872
873static int
874newfile(s)
875register char *s;
876{
877 register int f;
878
879 if (strcmp(s, "-") != 0) {
880 f = open(s, 0);
881 if (f < 0) {
882 prs(s);
883 err(": cannot open");
884 return(1);
885 }
886 } else
887 f = 0;
888 next(remap(f));
889 return(0);
890}
891
892static void
893onecommand()
894{
895 register int i;
896 jmp_buf m1;
897
898 while (e.oenv)
899 quitenv();
900 areanum = 1;
901 freehere(areanum);
902 freearea(areanum);
903 garbage();
904 wdlist = 0;
905 iolist = 0;
906 e.errpt = 0;
907 e.linep = line;
908 yynerrs = 0;
909 multiline = 0;
910 inparse = 1;
911 intr = 0;
912 execflg = 0;
913 setjmp(failpt = m1); /* Bruce Evans' fix */
914 if (setjmp(failpt = m1) || yyparse() || intr) {
915 while (e.oenv)
916 quitenv();
917 scraphere();
918 if (!interactive && intr)
919 leave();
920 inparse = 0;
921 intr = 0;
922 return;
923 }
924 inparse = 0;
925 brklist = 0;
926 intr = 0;
927 execflg = 0;
928 if (!flag['n'])
929 execute(outtree, NOPIPE, NOPIPE, 0);
930 if (!interactive && intr) {
931 execflg = 0;
932 leave();
933 }
934 if ((i = trapset) != 0) {
935 trapset = 0;
936 runtrap(i);
937 }
938}
939
940static void
941fail()
942{
943 longjmp(failpt, 1);
944 /* NOTREACHED */
945}
946
947static void
948leave()
949{
950 if (execflg)
951 fail();
952 scraphere();
953 freehere(1);
954 runtrap(0);
955 exit(exstat);
956 /* NOTREACHED */
957}
958
959static void
960warn(s)
961register char *s;
962{
963 if(*s) {
964 prs(s);
965 exstat = -1;
966 }
967 prs("\n");
968 if (flag['e'])
969 leave();
970}
971
972static void
973err(s)
974char *s;
975{
976 warn(s);
977 if (flag['n'])
978 return;
979 if (!interactive)
980 leave();
981 if (e.errpt)
982 longjmp(e.errpt, 1);
983 closeall();
984 e.iop = e.iobase = iostack;
985}
986
987static int
988newenv(f)
989int f;
990{
991 register struct env *ep;
992
993 if (f) {
994 quitenv();
995 return(1);
996 }
997 ep = (struct env *) space(sizeof(*ep));
998 if (ep == NULL) {
999 while (e.oenv)
1000 quitenv();
1001 fail();
1002 }
1003 *ep = e;
1004 e.oenv = ep;
1005 e.errpt = errpt;
1006 return(0);
1007}
1008
1009static void
1010quitenv()
1011{
1012 register struct env *ep;
1013 register int fd;
1014
1015 if ((ep = e.oenv) != NULL) {
1016 fd = e.iofd;
1017 e = *ep;
1018 /* should close `'d files */
1019 DELETE(ep);
1020 while (--fd >= e.iofd)
1021 close(fd);
1022 }
1023}
1024
1025/*
1026 * Is any character from s1 in s2?
1027 */
1028static int
1029anys(s1, s2)
1030register char *s1, *s2;
1031{
1032 while (*s1)
1033 if (any(*s1++, s2))
1034 return(1);
1035 return(0);
1036}
1037
1038/*
1039 * Is character c in s?
1040 */
1041static int
1042any(c, s)
1043register int c;
1044register char *s;
1045{
1046 while (*s)
1047 if (*s++ == c)
1048 return(1);
1049 return(0);
1050}
1051
1052static char *
1053putn(n)
1054register int n;
1055{
Eric Andersen737f5fb2003-03-14 16:05:59 +00001056 return(itoa(n));
Eric Andersenff9eee42001-06-29 04:57:14 +00001057}
1058
1059static char *
Eric Andersen737f5fb2003-03-14 16:05:59 +00001060itoa(n)
1061register int n;
Eric Andersenff9eee42001-06-29 04:57:14 +00001062{
Eric Andersenff9eee42001-06-29 04:57:14 +00001063 static char s[20];
Eric Andersen737f5fb2003-03-14 16:05:59 +00001064 snprintf(s, sizeof(s), "%u", n);
1065 return(s);
Eric Andersenff9eee42001-06-29 04:57:14 +00001066}
1067
1068static void
Eric Andersen392947c2002-12-11 07:42:46 +00001069next(int f)
Eric Andersenff9eee42001-06-29 04:57:14 +00001070{
1071 PUSHIO(afile, f, filechar);
1072}
1073
1074static void
1075onintr(s)
1076int s; /* ANSI C requires a parameter */
1077{
1078 signal(SIGINT, onintr);
1079 intr = 1;
1080 if (interactive) {
1081 if (inparse) {
1082 prs("\n");
1083 fail();
1084 }
1085 }
1086 else if (heedint) {
1087 execflg = 0;
1088 leave();
1089 }
1090}
1091
1092static char *
1093space(n)
1094int n;
1095{
1096 register char *cp;
1097
1098 if ((cp = getcell(n)) == 0)
1099 err("out of string space");
1100 return(cp);
1101}
1102
1103static char *
1104strsave(s, a)
1105register char *s;
1106int a;
1107{
1108 register char *cp, *xp;
1109
1110 if ((cp = space(strlen(s)+1)) != NULL) {
1111 setarea((char *)cp, a);
1112 for (xp = cp; (*xp++ = *s++) != '\0';)
1113 ;
1114 return(cp);
1115 }
1116 return("");
1117}
1118
1119/*
1120 * trap handling
1121 */
1122static void
1123sig(i)
1124register int i;
1125{
1126 trapset = i;
1127 signal(i, sig);
1128}
1129
1130static void runtrap(i)
1131int i;
1132{
1133 char *trapstr;
1134
1135 if ((trapstr = trap[i]) == NULL)
1136 return;
1137 if (i == 0)
1138 trap[i] = 0;
1139 RUN(aword, trapstr, nlchar);
1140}
1141
1142/* -------- var.c -------- */
1143
1144/*
1145 * Find the given name in the dictionary
1146 * and return its value. If the name was
1147 * not previously there, enter it now and
1148 * return a null value.
1149 */
1150static struct var *
1151lookup(n)
1152register char *n;
1153{
1154 register struct var *vp;
1155 register char *cp;
1156 register int c;
1157 static struct var dummy;
1158
1159 if (isdigit(*n)) {
1160 dummy.name = n;
1161 for (c = 0; isdigit(*n) && c < 1000; n++)
1162 c = c*10 + *n-'0';
1163 dummy.status = RONLY;
1164 dummy.value = c <= dolc? dolv[c]: null;
1165 return(&dummy);
1166 }
1167 for (vp = vlist; vp; vp = vp->next)
1168 if (eqname(vp->name, n))
1169 return(vp);
1170 cp = findeq(n);
1171 vp = (struct var *)space(sizeof(*vp));
1172 if (vp == 0 || (vp->name = space((int)(cp-n)+2)) == 0) {
1173 dummy.name = dummy.value = "";
1174 return(&dummy);
1175 }
1176 for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++)
1177 ;
1178 if (*cp == 0)
1179 *cp = '=';
1180 *++cp = 0;
1181 setarea((char *)vp, 0);
1182 setarea((char *)vp->name, 0);
1183 vp->value = null;
1184 vp->next = vlist;
1185 vp->status = GETCELL;
1186 vlist = vp;
1187 return(vp);
1188}
1189
1190/*
1191 * give variable at `vp' the value `val'.
1192 */
1193static void
1194setval(vp, val)
1195struct var *vp;
1196char *val;
1197{
1198 nameval(vp, val, (char *)NULL);
1199}
1200
1201/*
1202 * if name is not NULL, it must be
1203 * a prefix of the space `val',
1204 * and end with `='.
1205 * this is all so that exporting
1206 * values is reasonably painless.
1207 */
1208static void
1209nameval(vp, val, name)
1210register struct var *vp;
1211char *val, *name;
1212{
1213 register char *cp, *xp;
1214 char *nv;
1215 int fl;
1216
1217 if (vp->status & RONLY) {
1218 for (xp = vp->name; *xp && *xp != '=';)
1219 putc(*xp++, stderr);
1220 err(" is read-only");
1221 return;
1222 }
1223 fl = 0;
1224 if (name == NULL) {
1225 xp = space(strlen(vp->name)+strlen(val)+2);
1226 if (xp == 0)
1227 return;
1228 /* make string: name=value */
1229 setarea((char *)xp, 0);
1230 name = xp;
1231 for (cp = vp->name; (*xp = *cp++) && *xp!='='; xp++)
1232 ;
1233 if (*xp++ == 0)
1234 xp[-1] = '=';
1235 nv = xp;
1236 for (cp = val; (*xp++ = *cp++) != '\0';)
1237 ;
1238 val = nv;
1239 fl = GETCELL;
1240 }
1241 if (vp->status & GETCELL)
1242 freecell(vp->name); /* form new string `name=value' */
1243 vp->name = name;
1244 vp->value = val;
1245 vp->status |= fl;
1246}
1247
1248static void
1249export(vp)
1250struct var *vp;
1251{
1252 vp->status |= EXPORT;
1253}
1254
1255static void
1256ronly(vp)
1257struct var *vp;
1258{
Matt Kraai69edfec2001-08-06 14:14:18 +00001259 if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */
Eric Andersenff9eee42001-06-29 04:57:14 +00001260 vp->status |= RONLY;
1261}
1262
1263static int
1264isassign(s)
1265register char *s;
1266{
Matt Kraai69edfec2001-08-06 14:14:18 +00001267 if (!isalpha((int)*s) && *s != '_')
Eric Andersenff9eee42001-06-29 04:57:14 +00001268 return(0);
1269 for (; *s != '='; s++)
Matt Kraai69edfec2001-08-06 14:14:18 +00001270 if (*s == 0 || (!isalnum(*s) && *s != '_'))
Eric Andersenff9eee42001-06-29 04:57:14 +00001271 return(0);
1272 return(1);
1273}
1274
1275static int
1276assign(s, cf)
1277register char *s;
1278int cf;
1279{
1280 register char *cp;
1281 struct var *vp;
1282
Matt Kraai69edfec2001-08-06 14:14:18 +00001283 if (!isalpha(*s) && *s != '_')
Eric Andersenff9eee42001-06-29 04:57:14 +00001284 return(0);
1285 for (cp = s; *cp != '='; cp++)
Matt Kraai69edfec2001-08-06 14:14:18 +00001286 if (*cp == 0 || (!isalnum(*cp) && *cp != '_'))
Eric Andersenff9eee42001-06-29 04:57:14 +00001287 return(0);
1288 vp = lookup(s);
1289 nameval(vp, ++cp, cf == COPYV? (char *)NULL: s);
1290 if (cf != COPYV)
1291 vp->status &= ~GETCELL;
1292 return(1);
1293}
1294
1295static int
1296checkname(cp)
1297register char *cp;
1298{
Matt Kraai69edfec2001-08-06 14:14:18 +00001299 if (!isalpha(*cp++) && *(cp-1) != '_')
Eric Andersenff9eee42001-06-29 04:57:14 +00001300 return(0);
1301 while (*cp)
Matt Kraai69edfec2001-08-06 14:14:18 +00001302 if (!isalnum(*cp++) && *(cp-1) != '_')
Eric Andersenff9eee42001-06-29 04:57:14 +00001303 return(0);
1304 return(1);
1305}
1306
1307static void
1308putvlist(f, out)
1309register int f, out;
1310{
1311 register struct var *vp;
1312
1313 for (vp = vlist; vp; vp = vp->next)
Matt Kraai69edfec2001-08-06 14:14:18 +00001314 if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) {
Eric Andersenff9eee42001-06-29 04:57:14 +00001315 if (vp->status & EXPORT)
1316 write(out, "export ", 7);
1317 if (vp->status & RONLY)
1318 write(out, "readonly ", 9);
1319 write(out, vp->name, (int)(findeq(vp->name) - vp->name));
1320 write(out, "\n", 1);
1321 }
1322}
1323
1324static int
1325eqname(n1, n2)
1326register char *n1, *n2;
1327{
1328 for (; *n1 != '=' && *n1 != 0; n1++)
1329 if (*n2++ != *n1)
1330 return(0);
1331 return(*n2 == 0 || *n2 == '=');
1332}
1333
1334static char *
1335findeq(cp)
1336register char *cp;
1337{
1338 while (*cp != '\0' && *cp != '=')
1339 cp++;
1340 return(cp);
1341}
1342
1343/* -------- gmatch.c -------- */
1344/*
1345 * int gmatch(string, pattern)
1346 * char *string, *pattern;
1347 *
1348 * Match a pattern as in sh(1).
1349 */
1350
1351#define CMASK 0377
1352#define QUOTE 0200
1353#define QMASK (CMASK&~QUOTE)
1354#define NOT '!' /* might use ^ */
1355
1356static int
1357gmatch(s, p)
1358register char *s, *p;
1359{
1360 register int sc, pc;
1361
1362 if (s == NULL || p == NULL)
1363 return(0);
1364 while ((pc = *p++ & CMASK) != '\0') {
1365 sc = *s++ & QMASK;
1366 switch (pc) {
1367 case '[':
1368 if ((p = cclass(p, sc)) == NULL)
1369 return(0);
1370 break;
1371
1372 case '?':
1373 if (sc == 0)
1374 return(0);
1375 break;
1376
1377 case '*':
1378 s--;
1379 do {
1380 if (*p == '\0' || gmatch(s, p))
1381 return(1);
1382 } while (*s++ != '\0');
1383 return(0);
1384
1385 default:
1386 if (sc != (pc&~QUOTE))
1387 return(0);
1388 }
1389 }
1390 return(*s == 0);
1391}
1392
1393static char *
1394cclass(p, sub)
1395register char *p;
1396register int sub;
1397{
1398 register int c, d, not, found;
1399
1400 if ((not = *p == NOT) != 0)
1401 p++;
1402 found = not;
1403 do {
1404 if (*p == '\0')
1405 return((char *)NULL);
1406 c = *p & CMASK;
1407 if (p[1] == '-' && p[2] != ']') {
1408 d = p[2] & CMASK;
1409 p++;
1410 } else
1411 d = c;
1412 if (c == sub || (c <= sub && sub <= d))
1413 found = !not;
1414 } while (*++p != ']');
1415 return(found? p+1: (char *)NULL);
1416}
1417
1418
1419/* -------- area.c -------- */
1420
1421/*
1422 * All memory between (char *)areabot and (char *)(areatop+1) is
1423 * exclusively administered by the area management routines.
1424 * It is assumed that sbrk() and brk() manipulate the high end.
1425 */
1426
1427#define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;})
1428
1429static void
1430initarea()
1431{
1432 brkaddr = malloc(65000);
1433 brktop = brkaddr + 65000;
1434
1435 while ((int)sbrk(0) & ALIGN)
1436 sbrk(1);
1437 areabot = (struct region *)sbrk(REGSIZE);
1438
1439 areabot->next = areabot;
1440 areabot->area = BUSY;
1441 areatop = areabot;
1442 areanxt = areabot;
1443}
1444
1445char *
1446getcell(nbytes)
1447unsigned nbytes;
1448{
1449 register int nregio;
1450 register struct region *p, *q;
1451 register int i;
1452
1453 if (nbytes == 0) {
1454 puts("getcell(0)");
1455 abort();
1456 } /* silly and defeats the algorithm */
1457 /*
1458 * round upwards and add administration area
1459 */
1460 nregio = (nbytes+(REGSIZE-1))/REGSIZE + 1;
1461 for (p = areanxt;;) {
1462 if (p->area > areanum) {
1463 /*
1464 * merge free cells
1465 */
1466 while ((q = p->next)->area > areanum && q != areanxt)
1467 p->next = q->next;
1468 /*
1469 * exit loop if cell big enough
1470 */
1471 if (q >= p + nregio)
1472 goto found;
1473 }
1474 p = p->next;
1475 if (p == areanxt)
1476 break;
1477 }
1478 i = nregio >= GROWBY ? nregio : GROWBY;
1479 p = (struct region *)sbrk(i * REGSIZE);
1480 if (p == (struct region *)-1)
1481 return((char *)NULL);
1482 p--;
1483 if (p != areatop) {
1484 puts("not contig");
1485 abort(); /* allocated areas are contiguous */
1486 }
1487 q = p + i;
1488 p->next = q;
1489 p->area = FREE;
1490 q->next = areabot;
1491 q->area = BUSY;
1492 areatop = q;
1493found:
1494 /*
1495 * we found a FREE area big enough, pointed to by 'p', and up to 'q'
1496 */
1497 areanxt = p + nregio;
1498 if (areanxt < q) {
1499 /*
1500 * split into requested area and rest
1501 */
1502 if (areanxt+1 > q) {
1503 puts("OOM");
1504 abort(); /* insufficient space left for admin */
1505 }
1506 areanxt->next = q;
1507 areanxt->area = FREE;
1508 p->next = areanxt;
1509 }
1510 p->area = areanum;
1511 return((char *)(p+1));
1512}
1513
1514static void
1515freecell(cp)
1516char *cp;
1517{
1518 register struct region *p;
1519
1520 if ((p = (struct region *)cp) != NULL) {
1521 p--;
1522 if (p < areanxt)
1523 areanxt = p;
1524 p->area = FREE;
1525 }
1526}
1527
1528static void
1529freearea(a)
1530register int a;
1531{
1532 register struct region *p, *top;
1533
1534 top = areatop;
1535 for (p = areabot; p != top; p = p->next)
1536 if (p->area >= a)
1537 p->area = FREE;
1538}
1539
1540static void
1541setarea(cp,a)
1542char *cp;
1543int a;
1544{
1545 register struct region *p;
1546
1547 if ((p = (struct region *)cp) != NULL)
1548 (p-1)->area = a;
1549}
1550
1551int
1552getarea(cp)
1553char *cp;
1554{
1555 return ((struct region*)cp-1)->area;
1556}
1557
1558static void
1559garbage()
1560{
1561 register struct region *p, *q, *top;
1562
1563 top = areatop;
1564 for (p = areabot; p != top; p = p->next) {
1565 if (p->area > areanum) {
1566 while ((q = p->next)->area > areanum)
1567 p->next = q->next;
1568 areanxt = p;
1569 }
1570 }
1571#ifdef SHRINKBY
1572 if (areatop >= q + SHRINKBY && q->area > areanum) {
1573 brk((char *)(q+1));
1574 q->next = areabot;
1575 q->area = BUSY;
1576 areatop = q;
1577 }
1578#endif
1579}
1580
1581/* -------- csyn.c -------- */
1582/*
1583 * shell: syntax (C version)
1584 */
1585
1586
1587int
1588yyparse()
1589{
1590 startl = 1;
1591 peeksym = 0;
1592 yynerrs = 0;
1593 outtree = c_list();
1594 musthave('\n', 0);
1595 return(yynerrs!=0);
1596}
1597
1598static struct op *
1599pipeline(cf)
1600int cf;
1601{
1602 register struct op *t, *p;
1603 register int c;
1604
1605 t = command(cf);
1606 if (t != NULL) {
1607 while ((c = yylex(0)) == '|') {
1608 if ((p = command(CONTIN)) == NULL)
1609 SYNTAXERR;
1610 if (t->type != TPAREN && t->type != TCOM) {
1611 /* shell statement */
1612 t = block(TPAREN, t, NOBLOCK, NOWORDS);
1613 }
1614 t = block(TPIPE, t, p, NOWORDS);
1615 }
1616 peeksym = c;
1617 }
1618 return(t);
1619}
1620
1621static struct op *
1622andor()
1623{
1624 register struct op *t, *p;
1625 register int c;
1626
1627 t = pipeline(0);
1628 if (t != NULL) {
1629 while ((c = yylex(0)) == LOGAND || c == LOGOR) {
1630 if ((p = pipeline(CONTIN)) == NULL)
1631 SYNTAXERR;
1632 t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS);
1633 }
1634 peeksym = c;
1635 }
1636 return(t);
1637}
1638
1639static struct op *
1640c_list()
1641{
1642 register struct op *t, *p;
1643 register int c;
1644
1645 t = andor();
1646 if (t != NULL) {
1647 if((peeksym = yylex(0)) == '&')
1648 t = block(TASYNC, t, NOBLOCK, NOWORDS);
1649 while ((c = yylex(0)) == ';' || c == '&' || (multiline && c == '\n')) {
1650 if ((p = andor()) == NULL)
1651 return(t);
1652 if((peeksym = yylex(0)) == '&')
1653 p = block(TASYNC, p, NOBLOCK, NOWORDS);
1654 t = list(t, p);
1655 }
1656 peeksym = c;
1657 }
1658 return(t);
1659}
1660
1661
1662static int
1663synio(cf)
1664int cf;
1665{
1666 register struct ioword *iop;
1667 register int i;
1668 register int c;
1669
1670 if ((c = yylex(cf)) != '<' && c != '>') {
1671 peeksym = c;
1672 return(0);
1673 }
1674 i = yylval.i;
1675 musthave(WORD, 0);
1676 iop = io(iounit, i, yylval.cp);
1677 iounit = IODEFAULT;
1678 if (i & IOHERE)
1679 markhere(yylval.cp, iop);
1680 return(1);
1681}
1682
1683static void
1684musthave(c, cf)
1685int c, cf;
1686{
1687 if ((peeksym = yylex(cf)) != c)
1688 SYNTAXERR;
1689 peeksym = 0;
1690}
1691
1692static struct op *
1693simple()
1694{
1695 register struct op *t;
1696
1697 t = NULL;
1698 for (;;) {
1699 switch (peeksym = yylex(0)) {
1700 case '<':
1701 case '>':
1702 (void) synio(0);
1703 break;
1704
1705 case WORD:
1706 if (t == NULL) {
1707 t = newtp();
1708 t->type = TCOM;
1709 }
1710 peeksym = 0;
1711 word(yylval.cp);
1712 break;
1713
1714 default:
1715 return(t);
1716 }
1717 }
1718}
1719
1720static struct op *
1721nested(type, mark)
1722int type, mark;
1723{
1724 register struct op *t;
1725
1726 multiline++;
1727 t = c_list();
1728 musthave(mark, 0);
1729 multiline--;
1730 return(block(type, t, NOBLOCK, NOWORDS));
1731}
1732
1733static struct op *
1734command(cf)
1735int cf;
1736{
1737 register struct op *t;
1738 struct wdblock *iosave;
1739 register int c;
1740
1741 iosave = iolist;
1742 iolist = NULL;
1743 if (multiline)
1744 cf |= CONTIN;
1745 while (synio(cf))
1746 cf = 0;
1747 switch (c = yylex(cf)) {
1748 default:
1749 peeksym = c;
1750 if ((t = simple()) == NULL) {
1751 if (iolist == NULL)
1752 return((struct op *)NULL);
1753 t = newtp();
1754 t->type = TCOM;
1755 }
1756 break;
1757
1758 case '(':
1759 t = nested(TPAREN, ')');
1760 break;
1761
1762 case '{':
1763 t = nested(TBRACE, '}');
1764 break;
1765
1766 case FOR:
1767 t = newtp();
1768 t->type = TFOR;
1769 musthave(WORD, 0);
1770 startl = 1;
1771 t->str = yylval.cp;
1772 multiline++;
1773 t->words = wordlist();
1774 if ((c = yylex(0)) != '\n' && c != ';')
1775 peeksym = c;
1776 t->left = dogroup(0);
1777 multiline--;
1778 break;
1779
1780 case WHILE:
1781 case UNTIL:
1782 multiline++;
1783 t = newtp();
1784 t->type = c == WHILE? TWHILE: TUNTIL;
1785 t->left = c_list();
1786 t->right = dogroup(1);
1787 t->words = NULL;
1788 multiline--;
1789 break;
1790
1791 case CASE:
1792 t = newtp();
1793 t->type = TCASE;
1794 musthave(WORD, 0);
1795 t->str = yylval.cp;
1796 startl++;
1797 multiline++;
1798 musthave(IN, CONTIN);
1799 startl++;
1800 t->left = caselist();
1801 musthave(ESAC, 0);
1802 multiline--;
1803 break;
1804
1805 case IF:
1806 multiline++;
1807 t = newtp();
1808 t->type = TIF;
1809 t->left = c_list();
1810 t->right = thenpart();
1811 musthave(FI, 0);
1812 multiline--;
1813 break;
1814 }
1815 while (synio(0))
1816 ;
1817 t = namelist(t);
1818 iolist = iosave;
1819 return(t);
1820}
1821
1822static struct op *
1823dogroup(onlydone)
1824int onlydone;
1825{
1826 register int c;
1827 register struct op *mylist;
1828
1829 c = yylex(CONTIN);
1830 if (c == DONE && onlydone)
1831 return((struct op *)NULL);
1832 if (c != DO)
1833 SYNTAXERR;
1834 mylist = c_list();
1835 musthave(DONE, 0);
1836 return(mylist);
1837}
1838
1839static struct op *
1840thenpart()
1841{
1842 register int c;
1843 register struct op *t;
1844
1845 if ((c = yylex(0)) != THEN) {
1846 peeksym = c;
1847 return((struct op *)NULL);
1848 }
1849 t = newtp();
1850 t->type = 0;
1851 t->left = c_list();
1852 if (t->left == NULL)
1853 SYNTAXERR;
1854 t->right = elsepart();
1855 return(t);
1856}
1857
1858static struct op *
1859elsepart()
1860{
1861 register int c;
1862 register struct op *t;
1863
1864 switch (c = yylex(0)) {
1865 case ELSE:
1866 if ((t = c_list()) == NULL)
1867 SYNTAXERR;
1868 return(t);
1869
1870 case ELIF:
1871 t = newtp();
1872 t->type = TELIF;
1873 t->left = c_list();
1874 t->right = thenpart();
1875 return(t);
1876
1877 default:
1878 peeksym = c;
1879 return((struct op *)NULL);
1880 }
1881}
1882
1883static struct op *
1884caselist()
1885{
1886 register struct op *t;
1887
1888 t = NULL;
1889 while ((peeksym = yylex(CONTIN)) != ESAC)
1890 t = list(t, casepart());
1891 return(t);
1892}
1893
1894static struct op *
1895casepart()
1896{
1897 register struct op *t;
1898
1899 t = newtp();
1900 t->type = TPAT;
1901 t->words = pattern();
1902 musthave(')', 0);
1903 t->left = c_list();
1904 if ((peeksym = yylex(CONTIN)) != ESAC)
1905 musthave(BREAK, CONTIN);
1906 return(t);
1907}
1908
1909static char **
1910pattern()
1911{
1912 register int c, cf;
1913
1914 cf = CONTIN;
1915 do {
1916 musthave(WORD, cf);
1917 word(yylval.cp);
1918 cf = 0;
1919 } while ((c = yylex(0)) == '|');
1920 peeksym = c;
1921 word(NOWORD);
1922 return(copyw());
1923}
1924
1925static char **
1926wordlist()
1927{
1928 register int c;
1929
1930 if ((c = yylex(0)) != IN) {
1931 peeksym = c;
1932 return((char **)NULL);
1933 }
1934 startl = 0;
1935 while ((c = yylex(0)) == WORD)
1936 word(yylval.cp);
1937 word(NOWORD);
1938 peeksym = c;
1939 return(copyw());
1940}
1941
1942/*
1943 * supporting functions
1944 */
1945static struct op *
1946list(t1, t2)
1947register struct op *t1, *t2;
1948{
1949 if (t1 == NULL)
1950 return(t2);
1951 if (t2 == NULL)
1952 return(t1);
1953 return(block(TLIST, t1, t2, NOWORDS));
1954}
1955
1956static struct op *
1957block(type, t1, t2, wp)
1958int type;
1959struct op *t1, *t2;
1960char **wp;
1961{
1962 register struct op *t;
1963
1964 t = newtp();
1965 t->type = type;
1966 t->left = t1;
1967 t->right = t2;
1968 t->words = wp;
1969 return(t);
1970}
1971
1972static int
1973rlookup(n)
1974register char *n;
1975{
1976 register struct res *rp;
1977
1978 for (rp = restab; rp->r_name; rp++)
1979 if (strcmp(rp->r_name, n) == 0)
1980 return(rp->r_val);
1981 return(0);
1982}
1983
1984static struct op *
1985newtp()
1986{
1987 register struct op *t;
1988
1989 t = (struct op *)tree(sizeof(*t));
1990 t->type = 0;
1991 t->words = NULL;
1992 t->ioact = NULL;
1993 t->left = NULL;
1994 t->right = NULL;
1995 t->str = NULL;
1996 return(t);
1997}
1998
1999static struct op *
2000namelist(t)
2001register struct op *t;
2002{
2003 if (iolist) {
2004 iolist = addword((char *)NULL, iolist);
2005 t->ioact = copyio();
2006 } else
2007 t->ioact = NULL;
2008 if (t->type != TCOM) {
2009 if (t->type != TPAREN && t->ioact != NULL) {
2010 t = block(TPAREN, t, NOBLOCK, NOWORDS);
2011 t->ioact = t->left->ioact;
2012 t->left->ioact = NULL;
2013 }
2014 return(t);
2015 }
2016 word(NOWORD);
2017 t->words = copyw();
2018 return(t);
2019}
2020
2021static char **
2022copyw()
2023{
2024 register char **wd;
2025
2026 wd = getwords(wdlist);
2027 wdlist = 0;
2028 return(wd);
2029}
2030
2031static void
2032word(cp)
2033char *cp;
2034{
2035 wdlist = addword(cp, wdlist);
2036}
2037
2038static struct ioword **
2039copyio()
2040{
2041 register struct ioword **iop;
2042
2043 iop = (struct ioword **) getwords(iolist);
2044 iolist = 0;
2045 return(iop);
2046}
2047
2048static struct ioword *
2049io(u, f, cp)
2050int u;
2051int f;
2052char *cp;
2053{
2054 register struct ioword *iop;
2055
2056 iop = (struct ioword *) tree(sizeof(*iop));
2057 iop->io_unit = u;
2058 iop->io_flag = f;
2059 iop->io_name = cp;
2060 iolist = addword((char *)iop, iolist);
2061 return(iop);
2062}
2063
2064static void
2065zzerr()
2066{
2067 yyerror("syntax error");
2068}
2069
2070static void
2071yyerror(s)
2072char *s;
2073{
2074 yynerrs++;
2075 if (interactive && e.iop <= iostack) {
2076 multiline = 0;
2077 while (eofc() == 0 && yylex(0) != '\n')
2078 ;
2079 }
2080 err(s);
2081 fail();
2082}
2083
2084static int
2085yylex(cf)
2086int cf;
2087{
2088 register int c, c1;
2089 int atstart;
2090
2091 if ((c = peeksym) > 0) {
2092 peeksym = 0;
2093 if (c == '\n')
2094 startl = 1;
2095 return(c);
2096 }
2097 nlseen = 0;
2098 e.linep = line;
2099 atstart = startl;
2100 startl = 0;
2101 yylval.i = 0;
2102
2103loop:
2104 while ((c = my_getc(0)) == ' ' || c == '\t')
2105 ;
2106 switch (c) {
2107 default:
2108 if (any(c, "0123456789")) {
2109 unget(c1 = my_getc(0));
2110 if (c1 == '<' || c1 == '>') {
2111 iounit = c - '0';
2112 goto loop;
2113 }
2114 *e.linep++ = c;
2115 c = c1;
2116 }
2117 break;
2118
2119 case '#':
2120 while ((c = my_getc(0)) != 0 && c != '\n')
2121 ;
2122 unget(c);
2123 goto loop;
2124
2125 case 0:
2126 return(c);
2127
2128 case '$':
2129 *e.linep++ = c;
2130 if ((c = my_getc(0)) == '{') {
2131 if ((c = collect(c, '}')) != '\0')
2132 return(c);
2133 goto pack;
2134 }
2135 break;
2136
2137 case '`':
2138 case '\'':
2139 case '"':
2140 if ((c = collect(c, c)) != '\0')
2141 return(c);
2142 goto pack;
2143
2144 case '|':
2145 case '&':
2146 case ';':
2147 if ((c1 = dual(c)) != '\0') {
2148 startl = 1;
2149 return(c1);
2150 }
2151 startl = 1;
2152 return(c);
2153 case '^':
2154 startl = 1;
2155 return('|');
2156 case '>':
2157 case '<':
2158 diag(c);
2159 return(c);
2160
2161 case '\n':
2162 nlseen++;
2163 gethere();
2164 startl = 1;
2165 if (multiline || cf & CONTIN) {
2166 if (interactive && e.iop <= iostack) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00002167#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersenff9eee42001-06-29 04:57:14 +00002168 current_prompt=cprompt->value;
2169#else
2170 prs(cprompt->value);
2171#endif
2172 }
2173 if (cf & CONTIN)
2174 goto loop;
2175 }
2176 return(c);
2177
2178 case '(':
2179 case ')':
2180 startl = 1;
2181 return(c);
2182 }
2183
2184 unget(c);
2185
2186pack:
2187 while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n"))
2188 if (e.linep >= elinep)
2189 err("word too long");
2190 else
2191 *e.linep++ = c;
2192 unget(c);
2193 if(any(c, "\"'`$"))
2194 goto loop;
2195 *e.linep++ = '\0';
2196 if (atstart && (c = rlookup(line))!=0) {
2197 startl = 1;
2198 return(c);
2199 }
2200 yylval.cp = strsave(line, areanum);
2201 return(WORD);
2202}
2203
2204static int
2205collect(c, c1)
2206register int c, c1;
2207{
2208 char s[2];
2209
2210 *e.linep++ = c;
2211 while ((c = my_getc(c1)) != c1) {
2212 if (c == 0) {
2213 unget(c);
2214 s[0] = c1;
2215 s[1] = 0;
2216 prs("no closing "); yyerror(s);
2217 return(YYERRCODE);
2218 }
2219 if (interactive && c == '\n' && e.iop <= iostack) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00002220#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersenff9eee42001-06-29 04:57:14 +00002221 current_prompt=cprompt->value;
2222#else
2223 prs(cprompt->value);
2224#endif
2225 }
2226 *e.linep++ = c;
2227 }
2228 *e.linep++ = c;
2229 return(0);
2230}
2231
2232static int
2233dual(c)
2234register int c;
2235{
2236 char s[3];
2237 register char *cp = s;
2238
2239 *cp++ = c;
2240 *cp++ = my_getc(0);
2241 *cp = 0;
2242 if ((c = rlookup(s)) == 0)
2243 unget(*--cp);
2244 return(c);
2245}
2246
2247static void
2248diag(ec)
2249register int ec;
2250{
2251 register int c;
2252
2253 c = my_getc(0);
2254 if (c == '>' || c == '<') {
2255 if (c != ec)
2256 zzerr();
2257 yylval.i = ec == '>'? IOWRITE|IOCAT: IOHERE;
2258 c = my_getc(0);
2259 } else
2260 yylval.i = ec == '>'? IOWRITE: IOREAD;
2261 if (c != '&' || yylval.i == IOHERE)
2262 unget(c);
2263 else
2264 yylval.i |= IODUP;
2265}
2266
2267static char *
2268tree(size)
2269unsigned size;
2270{
2271 register char *t;
2272
2273 if ((t = getcell(size)) == NULL) {
2274 prs("command line too complicated\n");
2275 fail();
2276 /* NOTREACHED */
2277 }
2278 return(t);
2279}
2280
2281/* VARARGS1 */
2282/* ARGSUSED */
2283
2284/* -------- exec.c -------- */
2285
2286/*
2287 * execute tree
2288 */
2289
2290
2291static int
2292execute(t, pin, pout, act)
2293register struct op *t;
2294int *pin, *pout;
2295int act;
2296{
2297 register struct op *t1;
2298 volatile int i, rv, a;
2299 char *cp, **wp, **wp2;
2300 struct var *vp;
2301 struct brkcon bc;
2302
2303#if __GNUC__
2304 /* Avoid longjmp clobbering */
2305 (void) &wp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002306#endif
Eric Andersenff9eee42001-06-29 04:57:14 +00002307
2308
2309 if (t == NULL)
2310 return(0);
2311 rv = 0;
2312 a = areanum++;
2313 wp = (wp2 = t->words) != NULL
2314 ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
2315 : NULL;
2316
2317 switch(t->type) {
2318 case TPAREN:
Eric Andersen737f5fb2003-03-14 16:05:59 +00002319 rv = execute(t->left, pin, pout, 0);
2320 break;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002321
Eric Andersenff9eee42001-06-29 04:57:14 +00002322 case TCOM:
Eric Andersen1c039232001-07-07 00:05:55 +00002323 {
2324 int child;
2325 rv = forkexec(t, pin, pout, act, wp, &child);
2326 if (child) {
2327 exstat = rv;
2328 leave();
2329 }
Eric Andersenff9eee42001-06-29 04:57:14 +00002330 }
Eric Andersenff9eee42001-06-29 04:57:14 +00002331 break;
2332
2333 case TPIPE:
2334 {
2335 int pv[2];
2336 if ((rv = openpipe(pv)) < 0)
2337 break;
2338 pv[0] = remap(pv[0]);
2339 pv[1] = remap(pv[1]);
2340 (void) execute(t->left, pin, pv, 0);
2341 rv = execute(t->right, pv, pout, 0);
2342 }
2343 break;
2344
2345 case TLIST:
2346 (void) execute(t->left, pin, pout, 0);
2347 rv = execute(t->right, pin, pout, 0);
2348 break;
2349
2350 case TASYNC:
2351 {
2352 int hinteractive = interactive;
2353
2354 i = vfork();
2355 if (i != 0) {
2356 interactive = hinteractive;
2357 if (i != -1) {
2358 setval(lookup("!"), putn(i));
2359 if (pin != NULL)
2360 closepipe(pin);
2361 if (interactive) {
2362 prs(putn(i));
2363 prs("\n");
2364 }
2365 } else
2366 rv = -1;
2367 setstatus(rv);
2368 } else {
2369 signal(SIGINT, SIG_IGN);
2370 signal(SIGQUIT, SIG_IGN);
2371 if (interactive)
2372 signal(SIGTERM, SIG_DFL);
2373 interactive = 0;
2374 if (pin == NULL) {
2375 close(0);
2376 open("/dev/null", 0);
2377 }
2378 exit(execute(t->left, pin, pout, FEXEC));
2379 }
2380 }
2381 break;
2382
2383 case TOR:
2384 case TAND:
2385 rv = execute(t->left, pin, pout, 0);
2386 if ((t1 = t->right)!=NULL && (rv == 0) == (t->type == TAND))
2387 rv = execute(t1, pin, pout, 0);
2388 break;
2389
2390 case TFOR:
2391 if (wp == NULL) {
2392 wp = dolv+1;
2393 if ((i = dolc) < 0)
2394 i = 0;
2395 } else {
2396 i = -1;
2397 while (*wp++ != NULL)
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002398 ;
Eric Andersenff9eee42001-06-29 04:57:14 +00002399 }
2400 vp = lookup(t->str);
2401 while (setjmp(bc.brkpt))
2402 if (isbreak)
2403 goto broken;
2404 brkset(&bc);
2405 for (t1 = t->left; i-- && *wp != NULL;) {
2406 setval(vp, *wp++);
2407 rv = execute(t1, pin, pout, 0);
2408 }
2409 brklist = brklist->nextlev;
2410 break;
2411
2412 case TWHILE:
2413 case TUNTIL:
2414 while (setjmp(bc.brkpt))
2415 if (isbreak)
2416 goto broken;
2417 brkset(&bc);
2418 t1 = t->left;
2419 while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE))
2420 rv = execute(t->right, pin, pout, 0);
2421 brklist = brklist->nextlev;
2422 break;
2423
2424 case TIF:
2425 case TELIF:
2426 if (t->right != NULL) {
2427 rv = !execute(t->left, pin, pout, 0) ?
2428 execute(t->right->left, pin, pout, 0):
2429 execute(t->right->right, pin, pout, 0);
2430 }
2431 break;
2432
2433 case TCASE:
2434 if ((cp = evalstr(t->str, DOSUB|DOTRIM)) == 0)
2435 cp = "";
2436 if ((t1 = findcase(t->left, cp)) != NULL)
2437 rv = execute(t1, pin, pout, 0);
2438 break;
2439
2440 case TBRACE:
2441/*
2442 if (iopp = t->ioact)
2443 while (*iopp)
2444 if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
2445 rv = -1;
2446 break;
2447 }
2448*/
2449 if (rv >= 0 && (t1 = t->left))
2450 rv = execute(t1, pin, pout, 0);
2451 break;
2452 }
2453
2454broken:
2455 t->words = wp2;
2456 isbreak = 0;
2457 freehere(areanum);
2458 freearea(areanum);
2459 areanum = a;
2460 if (interactive && intr) {
2461 closeall();
2462 fail();
2463 }
2464 if ((i = trapset) != 0) {
2465 trapset = 0;
2466 runtrap(i);
2467 }
2468 return(rv);
2469}
2470
2471static int
2472forkexec( register struct op *t, int *pin, int *pout, int act, char **wp, int *pforked)
2473{
2474 int i, rv;
Eric Andersen392947c2002-12-11 07:42:46 +00002475 int (*shcom)(struct op *) = NULL;
Eric Andersenff9eee42001-06-29 04:57:14 +00002476 register int f;
2477 char *cp = NULL;
2478 struct ioword **iopp;
2479 int resetsig;
2480 char **owp;
2481
2482 int *hpin = pin;
2483 int *hpout = pout;
2484 int hforked;
2485 char *hwp;
2486 int hinteractive;
2487 int hintr;
2488 struct brkcon * hbrklist;
2489 int hexecflg;
2490
2491#if __GNUC__
2492 /* Avoid longjmp clobbering */
2493 (void) &pin;
2494 (void) &pout;
2495 (void) &wp;
2496 (void) &shcom;
2497 (void) &cp;
2498 (void) &resetsig;
2499 (void) &owp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002500#endif
Eric Andersenff9eee42001-06-29 04:57:14 +00002501
2502 owp = wp;
2503 resetsig = 0;
2504 *pforked = 0;
2505 rv = -1; /* system-detected error */
2506 if (t->type == TCOM) {
2507 while ((cp = *wp++) != NULL)
2508 ;
2509 cp = *wp;
2510
2511 /* strip all initial assignments */
2512 /* not correct wrt PATH=yyy command etc */
2513 if (flag['x'])
2514 echo (cp ? wp: owp);
2515 if (cp == NULL && t->ioact == NULL) {
2516 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2517 ;
2518 return(setstatus(0));
2519 }
2520 else if (cp != NULL)
2521 shcom = inbuilt(cp);
2522 }
2523 t->words = wp;
2524 f = act;
2525 if (shcom == NULL && (f & FEXEC) == 0) {
2526
2527 hpin = pin;
2528 hpout = pout;
2529 hforked = *pforked;
2530 hwp = *wp;
2531 hinteractive = interactive;
2532 hintr = intr;
2533 hbrklist = brklist;
2534 hexecflg = execflg;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002535
Eric Andersenff9eee42001-06-29 04:57:14 +00002536 i = vfork();
2537 if (i != 0) {
2538 /* who wrote this crappy non vfork safe shit? */
2539 pin = hpin;
2540 pout = hpout;
2541 *pforked = hforked;
2542 *wp = hwp;
2543 interactive = hinteractive;
2544 intr = hintr;
2545 brklist = hbrklist;
2546 execflg = hexecflg;
2547
2548 *pforked = 0;
2549 if (i == -1)
2550 return(rv);
2551 if (pin != NULL)
2552 closepipe(pin);
2553 return(pout==NULL? setstatus(waitfor(i,0)): 0);
2554 }
2555
2556 if (interactive) {
2557 signal(SIGINT, SIG_IGN);
2558 signal(SIGQUIT, SIG_IGN);
2559 resetsig = 1;
2560 }
2561 interactive = 0;
2562 intr = 0;
2563 (*pforked)++;
2564 brklist = 0;
2565 execflg = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002566 }
Eric Andersenff9eee42001-06-29 04:57:14 +00002567 if (owp != NULL)
2568 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2569 if (shcom == NULL)
2570 export(lookup(cp));
2571#ifdef COMPIPE
2572 if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) {
2573 err("piping to/from shell builtins not yet done");
2574 return(-1);
2575 }
2576#endif
2577 if (pin != NULL) {
2578 dup2(pin[0], 0);
2579 closepipe(pin);
2580 }
2581 if (pout != NULL) {
2582 dup2(pout[1], 1);
2583 closepipe(pout);
2584 }
2585 if ((iopp = t->ioact) != NULL) {
2586 if (shcom != NULL && shcom != doexec) {
2587 prs(cp);
2588 err(": cannot redirect shell command");
2589 return(-1);
2590 }
2591 while (*iopp)
2592 if (iosetup(*iopp++, pin!=NULL, pout!=NULL))
2593 return(rv);
2594 }
2595 if (shcom)
2596 return(setstatus((*shcom)(t)));
2597 /* should use FIOCEXCL */
2598 for (i=FDBASE; i<NOFILE; i++)
2599 close(i);
2600 if (resetsig) {
2601 signal(SIGINT, SIG_DFL);
2602 signal(SIGQUIT, SIG_DFL);
2603 }
2604 if (t->type == TPAREN)
2605 exit(execute(t->left, NOPIPE, NOPIPE, FEXEC));
2606 if (wp[0] == NULL)
2607 exit(0);
2608
2609 cp = rexecve(wp[0], wp, makenv());
2610 prs(wp[0]); prs(": "); warn(cp);
2611 if (!execflg)
2612 trap[0] = NULL;
2613 leave();
2614 /* NOTREACHED */
2615 exit(1);
2616}
2617
2618/*
2619 * 0< 1> are ignored as required
2620 * within pipelines.
2621 */
2622static int
2623iosetup(iop, pipein, pipeout)
2624register struct ioword *iop;
2625int pipein, pipeout;
2626{
2627 register int u = -1;
2628 char *cp=NULL, *msg;
2629
2630 if (iop->io_unit == IODEFAULT) /* take default */
2631 iop->io_unit = iop->io_flag&(IOREAD|IOHERE)? 0: 1;
2632 if (pipein && iop->io_unit == 0)
2633 return(0);
2634 if (pipeout && iop->io_unit == 1)
2635 return(0);
2636 msg = iop->io_flag&(IOREAD|IOHERE)? "open": "create";
2637 if ((iop->io_flag & IOHERE) == 0) {
2638 cp = iop->io_name;
2639 if ((cp = evalstr(cp, DOSUB|DOTRIM)) == NULL)
2640 return(1);
2641 }
2642 if (iop->io_flag & IODUP) {
2643 if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
2644 prs(cp);
2645 err(": illegal >& argument");
2646 return(1);
2647 }
2648 if (*cp == '-')
2649 iop->io_flag = IOCLOSE;
2650 iop->io_flag &= ~(IOREAD|IOWRITE);
2651 }
2652 switch (iop->io_flag) {
2653 case IOREAD:
2654 u = open(cp, 0);
2655 break;
2656
2657 case IOHERE:
2658 case IOHERE|IOXHERE:
2659 u = herein(iop->io_name, iop->io_flag&IOXHERE);
2660 cp = "here file";
2661 break;
2662
2663 case IOWRITE|IOCAT:
2664 if ((u = open(cp, 1)) >= 0) {
2665 lseek(u, (long)0, 2);
2666 break;
2667 }
2668 case IOWRITE:
2669 u = creat(cp, 0666);
2670 break;
2671
2672 case IODUP:
2673 u = dup2(*cp-'0', iop->io_unit);
2674 break;
2675
2676 case IOCLOSE:
2677 close(iop->io_unit);
2678 return(0);
2679 }
2680 if (u < 0) {
2681 prs(cp);
2682 prs(": cannot ");
2683 warn(msg);
2684 return(1);
2685 } else {
2686 if (u != iop->io_unit) {
2687 dup2(u, iop->io_unit);
2688 close(u);
2689 }
2690 }
2691 return(0);
2692}
2693
2694static void
2695echo(wp)
2696register char **wp;
2697{
2698 register int i;
2699
2700 prs("+");
2701 for (i=0; wp[i]; i++) {
2702 if (i)
2703 prs(" ");
2704 prs(wp[i]);
2705 }
2706 prs("\n");
2707}
2708
2709static struct op **
2710find1case(t, w)
2711struct op *t;
2712char *w;
2713{
2714 register struct op *t1;
2715 struct op **tp;
2716 register char **wp, *cp;
2717
2718 if (t == NULL)
2719 return((struct op **)NULL);
2720 if (t->type == TLIST) {
2721 if ((tp = find1case(t->left, w)) != NULL)
2722 return(tp);
2723 t1 = t->right; /* TPAT */
2724 } else
2725 t1 = t;
2726 for (wp = t1->words; *wp;)
2727 if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp))
2728 return(&t1->left);
2729 return((struct op **)NULL);
2730}
2731
2732static struct op *
2733findcase(t, w)
2734struct op *t;
2735char *w;
2736{
2737 register struct op **tp;
2738
2739 return((tp = find1case(t, w)) != NULL? *tp: (struct op *)NULL);
2740}
2741
2742/*
2743 * Enter a new loop level (marked for break/continue).
2744 */
2745static void
2746brkset(bc)
2747struct brkcon *bc;
2748{
2749 bc->nextlev = brklist;
2750 brklist = bc;
2751}
2752
2753/*
2754 * Wait for the last process created.
2755 * Print a message for each process found
2756 * that was killed by a signal.
2757 * Ignore interrupt signals while waiting
2758 * unless `canintr' is true.
2759 */
2760static int
2761waitfor(lastpid, canintr)
2762register int lastpid;
2763int canintr;
2764{
2765 register int pid, rv;
2766 int s;
2767 int oheedint = heedint;
2768
2769 heedint = 0;
2770 rv = 0;
2771 do {
2772 pid = wait(&s);
2773 if (pid == -1) {
2774 if (errno != EINTR || canintr)
2775 break;
2776 } else {
2777 if ((rv = WAITSIG(s)) != 0) {
2778 if (rv < NSIGNAL) {
2779 if (signame[rv] != NULL) {
2780 if (pid != lastpid) {
2781 prn(pid);
2782 prs(": ");
2783 }
2784 prs(signame[rv]);
2785 }
2786 } else {
2787 if (pid != lastpid) {
2788 prn(pid);
2789 prs(": ");
2790 }
2791 prs("Signal "); prn(rv); prs(" ");
2792 }
2793 if (WAITCORE(s))
2794 prs(" - core dumped");
2795 if (rv >= NSIGNAL || signame[rv])
2796 prs("\n");
2797 rv = -1;
2798 } else
2799 rv = WAITVAL(s);
2800 }
2801 } while (pid != lastpid);
2802 heedint = oheedint;
2803 if (intr) {
2804 if (interactive) {
2805 if (canintr)
2806 intr = 0;
2807 } else {
2808 if (exstat == 0) exstat = rv;
2809 onintr(0);
2810 }
2811 }
2812 return(rv);
2813}
2814
2815static int
2816setstatus(s)
2817register int s;
2818{
2819 exstat = s;
2820 setval(lookup("?"), putn(s));
2821 return(s);
2822}
2823
2824/*
2825 * PATH-searching interface to execve.
2826 * If getenv("PATH") were kept up-to-date,
2827 * execvp might be used.
2828 */
2829static char *
2830rexecve(c, v, envp)
2831char *c, **v, **envp;
2832{
2833 register int i;
2834 register char *sp, *tp;
2835 int eacces = 0, asis = 0;
2836
Eric Andersenbdfd0d72001-10-24 05:00:29 +00002837#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
Eric Andersen1c039232001-07-07 00:05:55 +00002838 char *name = c;
Eric Andersen1c039232001-07-07 00:05:55 +00002839 optind = 1;
2840 if (find_applet_by_name(name)) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002841 /* We have to exec here since we vforked. Running
Eric Andersen1c039232001-07-07 00:05:55 +00002842 * run_applet_by_name() won't work and bad things
2843 * will happen. */
2844 execve("/proc/self/exe", v, envp);
2845 execve("busybox", v, envp);
2846 }
2847#endif
2848
Eric Andersenff9eee42001-06-29 04:57:14 +00002849 sp = any('/', c)? "": path->value;
2850 asis = *sp == '\0';
2851 while (asis || *sp != '\0') {
2852 asis = 0;
2853 tp = e.linep;
2854 for (; *sp != '\0'; tp++)
2855 if ((*tp = *sp++) == ':') {
2856 asis = *sp == '\0';
2857 break;
2858 }
2859 if (tp != e.linep)
2860 *tp++ = '/';
2861 for (i = 0; (*tp++ = c[i++]) != '\0';)
2862 ;
Eric Andersen1c039232001-07-07 00:05:55 +00002863
Eric Andersenff9eee42001-06-29 04:57:14 +00002864 execve(e.linep, v, envp);
2865 switch (errno) {
2866 case ENOEXEC:
2867 *v = e.linep;
2868 tp = *--v;
2869 *v = e.linep;
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +00002870 execve(DEFAULT_SHELL, v, envp);
Eric Andersenff9eee42001-06-29 04:57:14 +00002871 *v = tp;
2872 return("no Shell");
2873
2874 case ENOMEM:
Manuel Novoa III cad53642003-03-19 09:13:01 +00002875 return((char*)bb_msg_memory_exhausted);
Eric Andersenff9eee42001-06-29 04:57:14 +00002876
2877 case E2BIG:
2878 return("argument list too long");
2879
2880 case EACCES:
2881 eacces++;
2882 break;
2883 }
2884 }
2885 return(errno==ENOENT ? "not found" : "cannot execute");
2886}
2887
2888/*
2889 * Run the command produced by generator `f'
2890 * applied to stream `arg'.
2891 */
2892static int
Eric Andersen392947c2002-12-11 07:42:46 +00002893run(struct ioarg *argp, int (*f)(struct ioarg *))
Eric Andersenff9eee42001-06-29 04:57:14 +00002894{
2895 struct op *otree;
2896 struct wdblock *swdlist;
2897 struct wdblock *siolist;
2898 jmp_buf ev, rt;
2899 xint *ofail;
2900 int rv;
2901
2902#if __GNUC__
2903 /* Avoid longjmp clobbering */
2904 (void) &rv;
2905#endif
2906
2907 areanum++;
2908 swdlist = wdlist;
2909 siolist = iolist;
2910 otree = outtree;
2911 ofail = failpt;
2912 rv = -1;
2913 if (newenv(setjmp(errpt = ev)) == 0) {
2914 wdlist = 0;
2915 iolist = 0;
2916 pushio(argp, f);
2917 e.iobase = e.iop;
2918 yynerrs = 0;
2919 if (setjmp(failpt = rt) == 0 && yyparse() == 0)
2920 rv = execute(outtree, NOPIPE, NOPIPE, 0);
2921 quitenv();
2922 }
2923 wdlist = swdlist;
2924 iolist = siolist;
2925 failpt = ofail;
2926 outtree = otree;
2927 freearea(areanum--);
2928 return(rv);
2929}
2930
2931/* -------- do.c -------- */
2932
2933/*
2934 * built-in commands: doX
2935 */
2936
Eric Andersen392947c2002-12-11 07:42:46 +00002937static int dohelp(struct op *t )
Eric Andersen1c039232001-07-07 00:05:55 +00002938{
2939 int col;
2940 const struct builtincmd *x;
2941
2942 printf("\nBuilt-in commands:\n");
2943 printf("-------------------\n");
2944
2945 for (col=0, x = builtincmds; x->builtinfunc != NULL; x++) {
2946 if (!x->name)
2947 continue;
2948 col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name);
2949 if (col > 60) {
2950 printf("\n");
2951 col = 0;
2952 }
2953 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00002954#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
Eric Andersen1c039232001-07-07 00:05:55 +00002955 {
2956 int i;
2957 const struct BB_applet *applet;
2958 extern const struct BB_applet applets[];
2959 extern const size_t NUM_APPLETS;
2960
2961 for (i=0, applet = applets; i < NUM_APPLETS; applet++, i++) {
2962 if (!applet->name)
2963 continue;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002964
2965 col += printf("%s%s", ((col == 0) ? "\t" : " "),
Eric Andersen1c039232001-07-07 00:05:55 +00002966 applet->name);
2967 if (col > 60) {
2968 printf("\n");
2969 col = 0;
2970 }
2971 }
2972 }
2973#endif
2974 printf("\n\n");
2975 return EXIT_SUCCESS;
2976}
2977
2978
2979
Eric Andersen392947c2002-12-11 07:42:46 +00002980static int dolabel(struct op *t )
Eric Andersenff9eee42001-06-29 04:57:14 +00002981{
2982 return(0);
2983}
2984
2985static int
2986dochdir(t)
2987register struct op *t;
2988{
2989 register char *cp, *er;
2990
2991 if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL)
2992 er = ": no home directory";
2993 else if(chdir(cp) < 0)
2994 er = ": bad directory";
2995 else
2996 return(0);
2997 prs(cp != NULL? cp: "cd");
2998 err(er);
2999 return(1);
3000}
3001
3002static int
3003doshift(t)
3004register struct op *t;
3005{
3006 register int n;
3007
3008 n = t->words[1]? getn(t->words[1]): 1;
3009 if(dolc < n) {
3010 err("nothing to shift");
3011 return(1);
3012 }
3013 dolv[n] = dolv[0];
3014 dolv += n;
3015 dolc -= n;
3016 setval(lookup("#"), putn(dolc));
3017 return(0);
3018}
3019
3020/*
3021 * execute login and newgrp directly
3022 */
3023static int
3024dologin(t)
3025struct op *t;
3026{
3027 register char *cp;
3028
3029 if (interactive) {
3030 signal(SIGINT, SIG_DFL);
3031 signal(SIGQUIT, SIG_DFL);
3032 }
3033 cp = rexecve(t->words[0], t->words, makenv());
3034 prs(t->words[0]); prs(": "); err(cp);
3035 return(1);
3036}
3037
3038static int
3039doumask(t)
3040register struct op *t;
3041{
3042 register int i, n;
3043 register char *cp;
3044
3045 if ((cp = t->words[1]) == NULL) {
3046 i = umask(0);
3047 umask(i);
3048 for (n=3*4; (n-=3) >= 0;)
3049 putc('0'+((i>>n)&07), stderr);
3050 putc('\n', stderr);
3051 } else {
3052 for (n=0; *cp>='0' && *cp<='9'; cp++)
3053 n = n*8 + (*cp-'0');
3054 umask(n);
3055 }
3056 return(0);
3057}
3058
3059static int
3060doexec(t)
3061register struct op *t;
3062{
3063 register int i;
3064 jmp_buf ex;
3065 xint *ofail;
3066
3067 t->ioact = NULL;
3068 for(i = 0; (t->words[i]=t->words[i+1]) != NULL; i++)
3069 ;
3070 if (i == 0)
3071 return(1);
3072 execflg = 1;
3073 ofail = failpt;
3074 if (setjmp(failpt = ex) == 0)
3075 execute(t, NOPIPE, NOPIPE, FEXEC);
3076 failpt = ofail;
3077 execflg = 0;
3078 return(1);
3079}
3080
3081static int
3082dodot(t)
3083struct op *t;
3084{
3085 register int i;
3086 register char *sp, *tp;
3087 char *cp;
3088
3089 if ((cp = t->words[1]) == NULL)
3090 return(0);
3091 sp = any('/', cp)? ":": path->value;
3092 while (*sp) {
3093 tp = e.linep;
3094 while (*sp && (*tp = *sp++) != ':')
3095 tp++;
3096 if (tp != e.linep)
3097 *tp++ = '/';
3098 for (i = 0; (*tp++ = cp[i++]) != '\0';)
3099 ;
3100 if ((i = open(e.linep, 0)) >= 0) {
3101 exstat = 0;
3102 next(remap(i));
3103 return(exstat);
3104 }
3105 }
3106 prs(cp);
3107 err(": not found");
3108 return(-1);
3109}
3110
3111static int
3112dowait(t)
3113struct op *t;
3114{
3115 register int i;
3116 register char *cp;
3117
3118 if ((cp = t->words[1]) != NULL) {
3119 i = getn(cp);
3120 if (i == 0)
3121 return(0);
3122 } else
3123 i = -1;
3124 setstatus(waitfor(i, 1));
3125 return(0);
3126}
3127
3128static int
3129doread(t)
3130struct op *t;
3131{
3132 register char *cp, **wp;
3133 register int nb = 0;
3134 register int nl = 0;
3135
3136 if (t->words[1] == NULL) {
3137 err("Usage: read name ...");
3138 return(1);
3139 }
3140 for (wp = t->words+1; *wp; wp++) {
3141 for (cp = e.linep; !nl && cp < elinep-1; cp++)
3142 if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) ||
3143 (nl = (*cp == '\n')) ||
3144 (wp[1] && any(*cp, ifs->value)))
3145 break;
3146 *cp = 0;
3147 if (nb <= 0)
3148 break;
3149 setval(lookup(*wp), e.linep);
3150 }
3151 return(nb <= 0);
3152}
3153
3154static int
3155doeval(t)
3156register struct op *t;
3157{
3158 return(RUN(awordlist, t->words+1, wdchar));
3159}
3160
3161static int
3162dotrap(t)
3163register struct op *t;
3164{
3165 register int n, i;
3166 register int resetsig;
3167
3168 if (t->words[1] == NULL) {
3169 for (i=0; i<=_NSIG; i++)
3170 if (trap[i]) {
3171 prn(i);
3172 prs(": ");
3173 prs(trap[i]);
3174 prs("\n");
3175 }
3176 return(0);
3177 }
3178 resetsig = isdigit(*t->words[1]);
3179 for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) {
3180 n = getsig(t->words[i]);
3181 freecell(trap[n]);
3182 trap[n] = 0;
3183 if (!resetsig) {
3184 if (*t->words[1] != '\0') {
3185 trap[n] = strsave(t->words[1], 0);
3186 setsig(n, sig);
3187 } else
3188 setsig(n, SIG_IGN);
3189 } else {
3190 if (interactive)
3191 if (n == SIGINT)
3192 setsig(n, onintr);
3193 else
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003194 setsig(n, n == SIGQUIT ? SIG_IGN
Eric Andersenff9eee42001-06-29 04:57:14 +00003195 : SIG_DFL);
3196 else
3197 setsig(n, SIG_DFL);
3198 }
3199 }
3200 return(0);
3201}
3202
3203static int
3204getsig(s)
3205char *s;
3206{
3207 register int n;
3208
3209 if ((n = getn(s)) < 0 || n > _NSIG) {
3210 err("trap: bad signal number");
3211 n = 0;
3212 }
3213 return(n);
3214}
3215
3216static void
Eric Andersen392947c2002-12-11 07:42:46 +00003217setsig( register int n, sighandler_t f)
Eric Andersenff9eee42001-06-29 04:57:14 +00003218{
3219 if (n == 0)
3220 return;
3221 if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) {
3222 ourtrap[n] = 1;
3223 signal(n, f);
3224 }
3225}
3226
3227static int
3228getn(as)
3229char *as;
3230{
3231 register char *s;
3232 register int n, m;
3233
3234 s = as;
3235 m = 1;
3236 if (*s == '-') {
3237 m = -1;
3238 s++;
3239 }
3240 for (n = 0; isdigit(*s); s++)
3241 n = (n*10) + (*s-'0');
3242 if (*s) {
3243 prs(as);
3244 err(": bad number");
3245 }
3246 return(n*m);
3247}
3248
3249static int
3250dobreak(t)
3251struct op *t;
3252{
3253 return(brkcontin(t->words[1], 1));
3254}
3255
3256static int
3257docontinue(t)
3258struct op *t;
3259{
3260 return(brkcontin(t->words[1], 0));
3261}
3262
3263static int
3264brkcontin(cp, val)
3265register char *cp;
3266int val;
3267{
3268 register struct brkcon *bc;
3269 register int nl;
3270
3271 nl = cp == NULL? 1: getn(cp);
3272 if (nl <= 0)
3273 nl = 999;
3274 do {
3275 if ((bc = brklist) == NULL)
3276 break;
3277 brklist = bc->nextlev;
3278 } while (--nl);
3279 if (nl) {
3280 err("bad break/continue level");
3281 return(1);
3282 }
3283 isbreak = val;
3284 longjmp(bc->brkpt, 1);
3285 /* NOTREACHED */
3286}
3287
3288static int
3289doexit(t)
3290struct op *t;
3291{
3292 register char *cp;
3293
3294 execflg = 0;
3295 if ((cp = t->words[1]) != NULL)
3296 setstatus(getn(cp));
3297 leave();
3298 /* NOTREACHED */
3299 return(0);
3300}
3301
3302static int
3303doexport(t)
3304struct op *t;
3305{
3306 rdexp(t->words+1, export, EXPORT);
3307 return(0);
3308}
3309
3310static int
3311doreadonly(t)
3312struct op *t;
3313{
3314 rdexp(t->words+1, ronly, RONLY);
3315 return(0);
3316}
3317
Eric Andersen392947c2002-12-11 07:42:46 +00003318static void rdexp (char **wp, void (*f)(struct var *), int key)
Eric Andersenff9eee42001-06-29 04:57:14 +00003319{
3320 if (*wp != NULL) {
Matt Kraaif69bfc72001-07-12 19:39:59 +00003321 for (; *wp != NULL; wp++) {
3322 if (isassign(*wp)) {
3323 char *cp;
3324 assign(*wp, COPYV);
3325 for (cp = *wp; *cp != '='; cp++)
3326 ;
3327 *cp = '\0';
3328 }
Eric Andersenff9eee42001-06-29 04:57:14 +00003329 if (checkname(*wp))
3330 (*f)(lookup(*wp));
3331 else
3332 badid(*wp);
Matt Kraaif69bfc72001-07-12 19:39:59 +00003333 }
Eric Andersenff9eee42001-06-29 04:57:14 +00003334 } else
3335 putvlist(key, 1);
3336}
3337
3338static void
3339badid(s)
3340register char *s;
3341{
3342 prs(s);
3343 err(": bad identifier");
3344}
3345
3346static int
3347doset(t)
3348register struct op *t;
3349{
3350 register struct var *vp;
3351 register char *cp;
3352 register int n;
3353
3354 if ((cp = t->words[1]) == NULL) {
3355 for (vp = vlist; vp; vp = vp->next)
3356 varput(vp->name, 1);
3357 return(0);
3358 }
3359 if (*cp == '-') {
3360 /* bad: t->words++; */
3361 for(n = 0; (t->words[n]=t->words[n+1]) != NULL; n++)
3362 ;
3363 if (*++cp == 0)
3364 flag['x'] = flag['v'] = 0;
3365 else
3366 for (; *cp; cp++)
3367 switch (*cp) {
3368 case 'e':
3369 if (!interactive)
3370 flag['e']++;
3371 break;
3372
3373 default:
3374 if (*cp>='a' && *cp<='z')
3375 flag[(int)*cp]++;
3376 break;
3377 }
3378 setdash();
3379 }
3380 if (t->words[1]) {
3381 t->words[0] = dolv[0];
3382 for (n=1; t->words[n]; n++)
3383 setarea((char *)t->words[n], 0);
3384 dolc = n-1;
3385 dolv = t->words;
3386 setval(lookup("#"), putn(dolc));
3387 setarea((char *)(dolv-1), 0);
3388 }
3389 return(0);
3390}
3391
3392static void
3393varput(s, out)
3394register char *s;
3395int out;
3396{
Matt Kraai69edfec2001-08-06 14:14:18 +00003397 if (isalnum(*s) || *s == '_') {
Eric Andersenff9eee42001-06-29 04:57:14 +00003398 write(out, s, strlen(s));
3399 write(out, "\n", 1);
3400 }
3401}
3402
3403
3404/*
3405 * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
3406 * This file contains code for the times builtin.
Eric Andersenff9eee42001-06-29 04:57:14 +00003407 */
Eric Andersen392947c2002-12-11 07:42:46 +00003408static int dotimes(struct op *t )
Eric Andersenff9eee42001-06-29 04:57:14 +00003409{
3410 struct tms buf;
3411 long int clk_tck = sysconf(_SC_CLK_TCK);
3412
3413 times(&buf);
3414 printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
3415 (int) (buf.tms_utime / clk_tck / 60),
3416 ((double) buf.tms_utime) / clk_tck,
3417 (int) (buf.tms_stime / clk_tck / 60),
3418 ((double) buf.tms_stime) / clk_tck,
3419 (int) (buf.tms_cutime / clk_tck / 60),
3420 ((double) buf.tms_cutime) / clk_tck,
3421 (int) (buf.tms_cstime / clk_tck / 60),
3422 ((double) buf.tms_cstime) / clk_tck);
3423 return 0;
3424}
3425
3426
Eric Andersen392947c2002-12-11 07:42:46 +00003427static int(*inbuilt(char *s))(struct op *)
Eric Andersenff9eee42001-06-29 04:57:14 +00003428{
Eric Andersen1c039232001-07-07 00:05:55 +00003429 const struct builtincmd *bp;
Eric Andersenff9eee42001-06-29 04:57:14 +00003430
Eric Andersen1c039232001-07-07 00:05:55 +00003431 for (bp = builtincmds; bp->name != NULL; bp++)
3432 if (strcmp(bp->name, s) == 0)
3433 return(bp->builtinfunc);
3434
Eric Andersen392947c2002-12-11 07:42:46 +00003435 return(NULL);
Eric Andersenff9eee42001-06-29 04:57:14 +00003436}
3437
3438/* -------- eval.c -------- */
3439
3440/*
3441 * ${}
3442 * `command`
3443 * blank interpretation
3444 * quoting
3445 * glob
3446 */
3447
3448static char ** eval( char **ap, int f)
3449{
3450 struct wdblock *wb;
3451 char **wp;
3452 char **wf;
3453 jmp_buf ev;
3454
3455#if __GNUC__
3456 /* Avoid longjmp clobbering */
3457 (void) &wp;
3458 (void) &ap;
3459#endif
3460 wp = NULL;
3461 wb = NULL;
3462 wf = NULL;
3463 if (newenv(setjmp(errpt = ev)) == 0) {
3464 while (*ap && isassign(*ap))
3465 expand(*ap++, &wb, f & ~DOGLOB);
3466 if (flag['k']) {
3467 for (wf = ap; *wf; wf++) {
3468 if (isassign(*wf))
3469 expand(*wf, &wb, f & ~DOGLOB);
3470 }
3471 }
3472 for (wb = addword((char *)0, wb); *ap; ap++) {
3473 if (!flag['k'] || !isassign(*ap))
3474 expand(*ap, &wb, f & ~DOKEY);
3475 }
3476 wb = addword((char *)0, wb);
3477 wp = getwords(wb);
3478 quitenv();
3479 } else
3480 gflg = 1;
3481 return(gflg? (char **)NULL: wp);
3482}
3483
3484/*
3485 * Make the exported environment from the exported
3486 * names in the dictionary. Keyword assignments
3487 * will already have been done.
3488 */
3489static char **
3490makenv()
3491
3492{
3493 register struct wdblock *wb;
3494 register struct var *vp;
3495
3496 wb = NULL;
3497 for (vp = vlist; vp; vp = vp->next)
3498 if (vp->status & EXPORT)
3499 wb = addword(vp->name, wb);
3500 wb = addword((char *)0, wb);
3501 return(getwords(wb));
3502}
3503
3504static char *
3505evalstr(cp, f)
3506register char *cp;
3507int f;
3508{
3509 struct wdblock *wb;
3510
3511 wb = NULL;
3512 if (expand(cp, &wb, f)) {
3513 if (wb == NULL || wb->w_nword == 0 || (cp = wb->w_words[0]) == NULL)
3514 cp = "";
3515 DELETE(wb);
3516 } else
3517 cp = NULL;
3518 return(cp);
3519}
3520
3521static int
3522expand( char *cp, register struct wdblock **wbp, int f)
3523{
3524 jmp_buf ev;
3525
3526#if __GNUC__
3527 /* Avoid longjmp clobbering */
3528 (void) &cp;
3529#endif
3530 gflg = 0;
3531 if (cp == NULL)
3532 return(0);
3533 if (!anys("$`'\"", cp) &&
3534 !anys(ifs->value, cp) &&
3535 ((f&DOGLOB)==0 || !anys("[*?", cp))) {
3536 cp = strsave(cp, areanum);
3537 if (f & DOTRIM)
3538 unquote(cp);
3539 *wbp = addword(cp, *wbp);
3540 return(1);
3541 }
3542 if (newenv(setjmp(errpt = ev)) == 0) {
3543 PUSHIO(aword, cp, strchar);
3544 e.iobase = e.iop;
3545 while ((cp = blank(f)) && gflg == 0) {
3546 e.linep = cp;
3547 cp = strsave(cp, areanum);
3548 if ((f&DOGLOB) == 0) {
3549 if (f & DOTRIM)
3550 unquote(cp);
3551 *wbp = addword(cp, *wbp);
3552 } else
3553 *wbp = glob(cp, *wbp);
3554 }
3555 quitenv();
3556 } else
3557 gflg = 1;
3558 return(gflg == 0);
3559}
3560
3561/*
3562 * Blank interpretation and quoting
3563 */
3564static char *
3565blank(f)
3566int f;
3567{
3568 register int c, c1;
3569 register char *sp;
3570 int scanequals, foundequals;
3571
3572 sp = e.linep;
3573 scanequals = f & DOKEY;
3574 foundequals = 0;
3575
3576loop:
3577 switch (c = subgetc('"', foundequals)) {
3578 case 0:
3579 if (sp == e.linep)
3580 return(0);
3581 *e.linep++ = 0;
3582 return(sp);
3583
3584 default:
3585 if (f & DOBLANK && any(c, ifs->value))
3586 goto loop;
3587 break;
3588
3589 case '"':
3590 case '\'':
3591 scanequals = 0;
3592 if (INSUB())
3593 break;
3594 for (c1 = c; (c = subgetc(c1, 1)) != c1;) {
3595 if (c == 0)
3596 break;
3597 if (c == '\'' || !any(c, "$`\""))
3598 c |= QUOTE;
3599 *e.linep++ = c;
3600 }
3601 c = 0;
3602 }
3603 unget(c);
Matt Kraai69edfec2001-08-06 14:14:18 +00003604 if (!isalpha(c) && c != '_')
Eric Andersenff9eee42001-06-29 04:57:14 +00003605 scanequals = 0;
3606 for (;;) {
3607 c = subgetc('"', foundequals);
3608 if (c == 0 ||
3609 f & (DOBLANK && any(c, ifs->value)) ||
3610 (!INSUB() && any(c, "\"'"))) {
3611 scanequals = 0;
3612 unget(c);
3613 if (any(c, "\"'"))
3614 goto loop;
3615 break;
3616 }
3617 if (scanequals) {
3618 if (c == '=') {
3619 foundequals = 1;
3620 scanequals = 0;
3621 }
Matt Kraai69edfec2001-08-06 14:14:18 +00003622 else if (!isalnum(c) && c != '_')
Eric Andersenff9eee42001-06-29 04:57:14 +00003623 scanequals = 0;
3624 }
3625 *e.linep++ = c;
3626 }
3627 *e.linep++ = 0;
3628 return(sp);
3629}
3630
3631/*
3632 * Get characters, substituting for ` and $
3633 */
3634static int
3635subgetc(ec, quoted)
Eric Andersen737f5fb2003-03-14 16:05:59 +00003636register char ec;
Eric Andersenff9eee42001-06-29 04:57:14 +00003637int quoted;
3638{
3639 register char c;
3640
3641again:
3642 c = my_getc(ec);
3643 if (!INSUB() && ec != '\'') {
3644 if (c == '`') {
3645 if (grave(quoted) == 0)
3646 return(0);
3647 e.iop->task = XGRAVE;
3648 goto again;
3649 }
3650 if (c == '$' && (c = dollar(quoted)) == 0) {
3651 e.iop->task = XDOLL;
3652 goto again;
3653 }
3654 }
3655 return(c);
3656}
3657
3658/*
3659 * Prepare to generate the string returned by ${} substitution.
3660 */
3661static int
3662dollar(quoted)
3663int quoted;
3664{
3665 int otask;
3666 struct io *oiop;
3667 char *dolp;
3668 register char *s, c, *cp=NULL;
3669 struct var *vp;
3670
3671 c = readc();
3672 s = e.linep;
3673 if (c != '{') {
3674 *e.linep++ = c;
Matt Kraai69edfec2001-08-06 14:14:18 +00003675 if (isalpha(c) || c == '_') {
3676 while ((c = readc())!=0 && (isalnum(c) || c == '_'))
Eric Andersenff9eee42001-06-29 04:57:14 +00003677 if (e.linep < elinep)
3678 *e.linep++ = c;
3679 unget(c);
3680 }
3681 c = 0;
3682 } else {
3683 oiop = e.iop;
3684 otask = e.iop->task;
3685 e.iop->task = XOTHER;
3686 while ((c = subgetc('"', 0))!=0 && c!='}' && c!='\n')
3687 if (e.linep < elinep)
3688 *e.linep++ = c;
3689 if (oiop == e.iop)
3690 e.iop->task = otask;
3691 if (c != '}') {
3692 err("unclosed ${");
3693 gflg++;
3694 return(c);
3695 }
3696 }
3697 if (e.linep >= elinep) {
3698 err("string in ${} too long");
3699 gflg++;
3700 e.linep -= 10;
3701 }
3702 *e.linep = 0;
3703 if (*s)
3704 for (cp = s+1; *cp; cp++)
3705 if (any(*cp, "=-+?")) {
3706 c = *cp;
3707 *cp++ = 0;
3708 break;
3709 }
3710 if (s[1] == 0 && (*s == '*' || *s == '@')) {
3711 if (dolc > 1) {
3712 /* currently this does not distinguish $* and $@ */
3713 /* should check dollar */
3714 e.linep = s;
3715 PUSHIO(awordlist, dolv+1, dolchar);
3716 return(0);
3717 } else { /* trap the nasty ${=} */
3718 s[0] = '1';
3719 s[1] = 0;
3720 }
3721 }
3722 vp = lookup(s);
3723 if ((dolp = vp->value) == null) {
3724 switch (c) {
3725 case '=':
3726 if (isdigit(*s)) {
3727 err("cannot use ${...=...} with $n");
3728 gflg++;
3729 break;
3730 }
3731 setval(vp, cp);
3732 dolp = vp->value;
3733 break;
3734
3735 case '-':
3736 dolp = strsave(cp, areanum);
3737 break;
3738
3739 case '?':
3740 if (*cp == 0) {
3741 prs("missing value for ");
3742 err(s);
3743 } else
3744 err(cp);
3745 gflg++;
3746 break;
3747 }
3748 } else if (c == '+')
3749 dolp = strsave(cp, areanum);
3750 if (flag['u'] && dolp == null) {
3751 prs("unset variable: ");
3752 err(s);
3753 gflg++;
3754 }
3755 e.linep = s;
3756 PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
3757 return(0);
3758}
3759
3760/*
3761 * Run the command in `...` and read its output.
3762 */
Eric Andersen737f5fb2003-03-14 16:05:59 +00003763
Eric Andersenff9eee42001-06-29 04:57:14 +00003764static int
3765grave(quoted)
3766int quoted;
3767{
Eric Andersenff9eee42001-06-29 04:57:14 +00003768 char *cp;
Eric Andersen737f5fb2003-03-14 16:05:59 +00003769 register int i;
3770 int j;
Eric Andersenff9eee42001-06-29 04:57:14 +00003771 int pf[2];
Eric Andersen737f5fb2003-03-14 16:05:59 +00003772 static char child_cmd[LINELIM];
3773 char *src;
3774 char *dest;
3775 int count;
3776 int ignore;
3777 int ignore_once;
3778 char *argument_list[4];
Eric Andersenff9eee42001-06-29 04:57:14 +00003779
3780#if __GNUC__
3781 /* Avoid longjmp clobbering */
3782 (void) &cp;
3783#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003784
Eric Andersenff9eee42001-06-29 04:57:14 +00003785 for (cp = e.iop->argp->aword; *cp != '`'; cp++)
3786 if (*cp == 0) {
3787 err("no closing `");
3788 return(0);
3789 }
Eric Andersen737f5fb2003-03-14 16:05:59 +00003790
3791 /* string copy with dollar expansion */
3792 src = e.iop->argp->aword;
3793 dest = child_cmd;
3794 count = 0;
3795 ignore = 0;
3796 ignore_once = 0;
3797 while ((*src != '`') && (count < LINELIM)) {
3798 if (*src == '\'')
3799 ignore = !ignore;
3800 if (*src == '\\')
3801 ignore_once = 1;
3802 if (*src == '$' && !ignore && !ignore_once) {
3803 struct var *vp;
3804 char var_name[LINELIM];
3805 char alt_value[LINELIM];
3806 int var_index = 0;
3807 int alt_index = 0;
3808 char operator = 0;
3809 int braces = 0;
3810 char *value;
3811
3812 src++;
3813 if (*src == '{') {
3814 braces = 1;
3815 src++;
3816 }
3817
3818 var_name[var_index++] = *src++;
3819 while (isalnum(*src))
3820 var_name[var_index++] = *src++;
3821 var_name[var_index] = 0;
3822
3823 if (braces) {
3824 switch (*src) {
3825 case '}':
3826 break;
3827 case '-':
3828 case '=':
3829 case '+':
3830 case '?':
3831 operator = *src;
3832 break;
3833 default:
3834 err("unclosed ${\n");
3835 return(0);
3836 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003837 if (operator) {
Eric Andersen737f5fb2003-03-14 16:05:59 +00003838 src++;
3839 while (*src && (*src != '}')) {
3840 alt_value[alt_index++] = *src++;
3841 }
3842 alt_value[alt_index] = 0;
3843 if (*src != '}') {
3844 err("unclosed ${\n");
3845 return(0);
3846 }
3847 }
3848 src++;
3849 }
3850
3851 vp = lookup(var_name);
3852 if (vp->value != null)
3853 value = (operator == '+')? alt_value : vp->value;
3854 else if (operator == '?') {
3855 err(alt_value);
3856 return(0);
3857 } else if (alt_index && (operator != '+')) {
3858 value = alt_value;
3859 if (operator == '=')
3860 setval(vp, value);
3861 } else
3862 continue;
3863
3864 while (*value && (count < LINELIM)) {
3865 *dest++ = *value++;
3866 count++;
3867 }
3868 } else {
3869 *dest++ = *src++;
3870 count++;
3871 ignore_once = 0;
3872 }
3873 }
3874 *dest = '\0';
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003875
Eric Andersenff9eee42001-06-29 04:57:14 +00003876 if (openpipe(pf) < 0)
3877 return(0);
Eric Andersen737f5fb2003-03-14 16:05:59 +00003878 while ((i = vfork()) == -1 && errno == EAGAIN)
3879 ;
3880 if (i < 0) {
Eric Andersenff9eee42001-06-29 04:57:14 +00003881 closepipe(pf);
Manuel Novoa III cad53642003-03-19 09:13:01 +00003882 err((char*)bb_msg_memory_exhausted);
Eric Andersenff9eee42001-06-29 04:57:14 +00003883 return(0);
3884 }
3885 if (i != 0) {
Eric Andersen737f5fb2003-03-14 16:05:59 +00003886 waitpid(i, NULL, 0);
Eric Andersenff9eee42001-06-29 04:57:14 +00003887 e.iop->argp->aword = ++cp;
3888 close(pf[1]);
Eric Andersen392947c2002-12-11 07:42:46 +00003889 PUSHIO(afile, remap(pf[0]), (int(*)(struct ioarg *))((quoted)? qgravechar: gravechar));
Eric Andersenff9eee42001-06-29 04:57:14 +00003890 return(1);
3891 }
Eric Andersenff9eee42001-06-29 04:57:14 +00003892 /* allow trapped signals */
Eric Andersen737f5fb2003-03-14 16:05:59 +00003893 /* XXX - Maybe this signal stuff should go as well? */
3894 for (j=0; j<=_NSIG; j++)
3895 if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN)
3896 signal(j, SIG_DFL);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003897
Eric Andersenff9eee42001-06-29 04:57:14 +00003898 dup2(pf[1], 1);
3899 closepipe(pf);
Eric Andersen737f5fb2003-03-14 16:05:59 +00003900
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +00003901 argument_list[0] = (char *)DEFAULT_SHELL;
Eric Andersen737f5fb2003-03-14 16:05:59 +00003902 argument_list[1] = "-c";
3903 argument_list[2] = child_cmd;
3904 argument_list[3] = 0;
3905
3906 prs(rexecve(argument_list[0], argument_list, makenv()));
3907 _exit(1);
Eric Andersenff9eee42001-06-29 04:57:14 +00003908}
3909
Eric Andersen737f5fb2003-03-14 16:05:59 +00003910
Eric Andersenff9eee42001-06-29 04:57:14 +00003911static char *
3912unquote(as)
3913register char *as;
3914{
3915 register char *s;
3916
3917 if ((s = as) != NULL)
3918 while (*s)
3919 *s++ &= ~QUOTE;
3920 return(as);
3921}
3922
3923/* -------- glob.c -------- */
3924
3925/*
3926 * glob
3927 */
3928
3929#define scopy(x) strsave((x), areanum)
3930#define BLKSIZ 512
3931#define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
3932
3933static struct wdblock *cl, *nl;
3934static char spcl[] = "[?*";
3935
3936static struct wdblock *
3937glob(cp, wb)
3938char *cp;
3939struct wdblock *wb;
3940{
3941 register int i;
3942 register char *pp;
3943
3944 if (cp == 0)
3945 return(wb);
3946 i = 0;
3947 for (pp = cp; *pp; pp++)
3948 if (any(*pp, spcl))
3949 i++;
3950 else if (!any(*pp & ~QUOTE, spcl))
3951 *pp &= ~QUOTE;
3952 if (i != 0) {
3953 for (cl = addword(scopy(cp), (struct wdblock *)0); anyspcl(cl); cl = nl) {
3954 nl = newword(cl->w_nword*2);
3955 for(i=0; i<cl->w_nword; i++) { /* for each argument */
3956 for (pp = cl->w_words[i]; *pp; pp++)
3957 if (any(*pp, spcl)) {
3958 globname(cl->w_words[i], pp);
3959 break;
3960 }
3961 if (*pp == '\0')
3962 nl = addword(scopy(cl->w_words[i]), nl);
3963 }
3964 for(i=0; i<cl->w_nword; i++)
3965 DELETE(cl->w_words[i]);
3966 DELETE(cl);
3967 }
3968 for(i=0; i<cl->w_nword; i++)
3969 unquote(cl->w_words[i]);
3970 glob0((char *)cl->w_words, cl->w_nword, sizeof(char *), xstrcmp);
3971 if (cl->w_nword) {
3972 for (i=0; i<cl->w_nword; i++)
3973 wb = addword(cl->w_words[i], wb);
3974 DELETE(cl);
3975 return(wb);
3976 }
3977 }
3978 wb = addword(unquote(cp), wb);
3979 return(wb);
3980}
3981
3982static void
3983globname(we, pp)
3984char *we;
3985register char *pp;
3986{
3987 register char *np, *cp;
3988 char *name, *gp, *dp;
3989 int k;
3990 DIR *dirp;
3991 struct dirent *de;
3992 char dname[NAME_MAX+1];
3993 struct stat dbuf;
3994
3995 for (np = we; np != pp; pp--)
3996 if (pp[-1] == '/')
3997 break;
3998 for (dp = cp = space((int)(pp-np)+3); np < pp;)
3999 *cp++ = *np++;
4000 *cp++ = '.';
4001 *cp = '\0';
4002 for (gp = cp = space(strlen(pp)+1); *np && *np != '/';)
4003 *cp++ = *np++;
4004 *cp = '\0';
4005 dirp = opendir(dp);
4006 if (dirp == 0) {
4007 DELETE(dp);
4008 DELETE(gp);
4009 return;
4010 }
4011 dname[NAME_MAX] = '\0';
4012 while ((de=readdir(dirp))!=NULL) {
4013 /* XXX Hmmm... What this could be? (abial) */
4014 /*
4015 if (ent[j].d_ino == 0)
4016 continue;
4017 */
4018 strncpy(dname, de->d_name, NAME_MAX);
4019 if (dname[0] == '.')
4020 if (*gp != '.')
4021 continue;
4022 for(k=0; k<NAME_MAX; k++)
4023 if (any(dname[k], spcl))
4024 dname[k] |= QUOTE;
4025 if (gmatch(dname, gp)) {
4026 name = generate(we, pp, dname, np);
4027 if (*np && !anys(np, spcl)) {
4028 if (stat(name,&dbuf)) {
4029 DELETE(name);
4030 continue;
4031 }
4032 }
4033 nl = addword(name, nl);
4034 }
4035 }
4036 closedir(dirp);
4037 DELETE(dp);
4038 DELETE(gp);
4039}
4040
4041/*
4042 * generate a pathname as below.
4043 * start..end1 / middle end
4044 * the slashes come for free
4045 */
4046static char *
4047generate(start1, end1, middle, end)
4048char *start1;
4049register char *end1;
4050char *middle, *end;
4051{
4052 char *p;
4053 register char *op, *xp;
4054
4055 p = op = space((int)(end1-start1)+strlen(middle)+strlen(end)+2);
4056 for (xp = start1; xp != end1;)
4057 *op++ = *xp++;
4058 for (xp = middle; (*op++ = *xp++) != '\0';)
4059 ;
4060 op--;
4061 for (xp = end; (*op++ = *xp++) != '\0';)
4062 ;
4063 return(p);
4064}
4065
4066static int
4067anyspcl(wb)
4068register struct wdblock *wb;
4069{
4070 register int i;
4071 register char **wd;
4072
4073 wd = wb->w_words;
4074 for (i=0; i<wb->w_nword; i++)
4075 if (anys(spcl, *wd++))
4076 return(1);
4077 return(0);
4078}
4079
4080static int
4081xstrcmp(p1, p2)
4082char *p1, *p2;
4083{
4084 return(strcmp(*(char **)p1, *(char **)p2));
4085}
4086
4087/* -------- word.c -------- */
4088
4089static struct wdblock *
4090newword(nw)
4091register int nw;
4092{
4093 register struct wdblock *wb;
4094
4095 wb = (struct wdblock *) space(sizeof(*wb) + nw*sizeof(char *));
4096 wb->w_bsize = nw;
4097 wb->w_nword = 0;
4098 return(wb);
4099}
4100
4101static struct wdblock *
4102addword(wd, wb)
4103char *wd;
4104register struct wdblock *wb;
4105{
4106 register struct wdblock *wb2;
4107 register int nw;
4108
4109 if (wb == NULL)
4110 wb = newword(NSTART);
4111 if ((nw = wb->w_nword) >= wb->w_bsize) {
4112 wb2 = newword(nw * 2);
4113 memcpy((char *)wb2->w_words, (char *)wb->w_words, nw*sizeof(char *));
4114 wb2->w_nword = nw;
4115 DELETE(wb);
4116 wb = wb2;
4117 }
4118 wb->w_words[wb->w_nword++] = wd;
4119 return(wb);
4120}
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004121static
Eric Andersenff9eee42001-06-29 04:57:14 +00004122char **
4123getwords(wb)
4124register struct wdblock *wb;
4125{
4126 register char **wd;
4127 register int nb;
4128
4129 if (wb == NULL)
4130 return((char **)NULL);
4131 if (wb->w_nword == 0) {
4132 DELETE(wb);
4133 return((char **)NULL);
4134 }
4135 wd = (char **) space(nb = sizeof(*wd) * wb->w_nword);
4136 memcpy((char *)wd, (char *)wb->w_words, nb);
4137 DELETE(wb); /* perhaps should done by caller */
4138 return(wd);
4139}
4140
4141int (*func)(char *, char *);
4142int globv;
4143
4144static void
4145glob0(a0, a1, a2, a3)
4146char *a0;
4147unsigned a1;
4148int a2;
4149int (*a3) (char *, char *);
4150{
4151 func = a3;
4152 globv = a2;
4153 glob1(a0, a0 + a1 * a2);
4154}
4155
4156static void
4157glob1(base, lim)
4158char *base, *lim;
4159{
4160 register char *i, *j;
4161 int v2;
4162 char *lptr, *hptr;
4163 int c;
4164 unsigned n;
4165
4166
4167 v2 = globv;
4168
4169top:
4170 if ((n=(int)(lim-base)) <= v2)
4171 return;
4172 n = v2 * (n / (2*v2));
4173 hptr = lptr = base+n;
4174 i = base;
4175 j = lim-v2;
4176 for(;;) {
4177 if (i < lptr) {
4178 if ((c = (*func)(i, lptr)) == 0) {
4179 glob2(i, lptr -= v2);
4180 continue;
4181 }
4182 if (c < 0) {
4183 i += v2;
4184 continue;
4185 }
4186 }
4187
4188begin:
4189 if (j > hptr) {
4190 if ((c = (*func)(hptr, j)) == 0) {
4191 glob2(hptr += v2, j);
4192 goto begin;
4193 }
4194 if (c > 0) {
4195 if (i == lptr) {
4196 glob3(i, hptr += v2, j);
4197 i = lptr += v2;
4198 goto begin;
4199 }
4200 glob2(i, j);
4201 j -= v2;
4202 i += v2;
4203 continue;
4204 }
4205 j -= v2;
4206 goto begin;
4207 }
4208
4209
4210 if (i == lptr) {
4211 if (lptr-base >= lim-hptr) {
4212 glob1(hptr+v2, lim);
4213 lim = lptr;
4214 } else {
4215 glob1(base, lptr);
4216 base = hptr+v2;
4217 }
4218 goto top;
4219 }
4220
4221
4222 glob3(j, lptr -= v2, i);
4223 j = hptr -= v2;
4224 }
4225}
4226
4227static void
4228glob2(i, j)
4229char *i, *j;
4230{
4231 register char *index1, *index2, c;
4232 int m;
4233
4234 m = globv;
4235 index1 = i;
4236 index2 = j;
4237 do {
4238 c = *index1;
4239 *index1++ = *index2;
4240 *index2++ = c;
4241 } while(--m);
4242}
4243
4244static void
4245glob3(i, j, k)
4246char *i, *j, *k;
4247{
4248 register char *index1, *index2, *index3;
4249 int c;
4250 int m;
4251
4252 m = globv;
4253 index1 = i;
4254 index2 = j;
4255 index3 = k;
4256 do {
4257 c = *index1;
4258 *index1++ = *index3;
4259 *index3++ = *index2;
4260 *index2++ = c;
4261 } while(--m);
4262}
4263
4264/* -------- io.c -------- */
4265
4266/*
4267 * shell IO
4268 */
4269
4270static int my_getc( int ec)
4271{
4272 register int c;
4273
4274 if(e.linep > elinep) {
4275 while((c=readc()) != '\n' && c)
4276 ;
4277 err("input line too long");
4278 gflg++;
4279 return(c);
4280 }
4281 c = readc();
Eric Andersen737f5fb2003-03-14 16:05:59 +00004282 if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) {
Eric Andersenff9eee42001-06-29 04:57:14 +00004283 if(c == '\\') {
4284 c = readc();
4285 if (c == '\n' && ec != '\"')
4286 return(my_getc(ec));
4287 c |= QUOTE;
4288 }
4289 }
4290 return(c);
4291}
4292
4293static void
4294unget(c)
4295int c;
4296{
4297 if (e.iop >= e.iobase)
4298 e.iop->peekc = c;
4299}
4300
4301static int
4302eofc()
4303
4304{
4305 return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0);
4306}
4307
4308static int
4309readc()
4310{
4311 register int c;
4312
4313 for (; e.iop >= e.iobase; e.iop--)
4314 if ((c = e.iop->peekc) != '\0') {
4315 e.iop->peekc = 0;
4316 return(c);
4317 }
4318 else {
4319 if (e.iop->prev != 0) {
4320 if ((c = (*e.iop->iofn)(e.iop->argp, e.iop)) != '\0') {
4321 if (c == -1) {
4322 e.iop++;
4323 continue;
4324 }
4325 if (e.iop == iostack)
4326 ioecho(c);
4327 return(e.iop->prev = c);
4328 }
4329 else if (e.iop->task == XIO && e.iop->prev != '\n') {
4330 e.iop->prev = 0;
4331 if (e.iop == iostack)
4332 ioecho('\n');
4333 return '\n';
4334 }
4335 }
4336 if (e.iop->task == XIO) {
4337 if (multiline)
4338 return e.iop->prev = 0;
4339 if (interactive && e.iop == iostack+1) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00004340#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersenff9eee42001-06-29 04:57:14 +00004341 current_prompt=prompt->value;
4342#else
4343 prs(prompt->value);
4344#endif
4345 }
4346 }
4347 }
4348 if (e.iop >= iostack)
4349 return(0);
4350 leave();
4351 /* NOTREACHED */
4352 return(0);
4353}
4354
4355static void
4356ioecho(c)
4357char c;
4358{
4359 if (flag['v'])
4360 write(2, &c, sizeof c);
4361}
4362
4363static void
Eric Andersen392947c2002-12-11 07:42:46 +00004364pushio(struct ioarg *argp, int (*fn)(struct ioarg *))
Eric Andersenff9eee42001-06-29 04:57:14 +00004365{
4366 if (++e.iop >= &iostack[NPUSH]) {
4367 e.iop--;
4368 err("Shell input nested too deeply");
4369 gflg++;
4370 return;
4371 }
Eric Andersen392947c2002-12-11 07:42:46 +00004372 e.iop->iofn = (int (*)(struct ioarg *, struct io *))fn;
Eric Andersenff9eee42001-06-29 04:57:14 +00004373
4374 if (argp->afid != AFID_NOBUF)
4375 e.iop->argp = argp;
4376 else {
4377 e.iop->argp = ioargstack + (e.iop - iostack);
4378 *e.iop->argp = *argp;
4379 e.iop->argp->afbuf = e.iop == &iostack[0] ? &mainbuf : &sharedbuf;
4380 if (isatty(e.iop->argp->afile) == 0 &&
4381 (e.iop == &iostack[0] ||
4382 lseek(e.iop->argp->afile, 0L, 1) != -1)) {
4383 if (++bufid == AFID_NOBUF)
4384 bufid = AFID_ID;
4385 e.iop->argp->afid = bufid;
4386 }
4387 }
4388
4389 e.iop->prev = ~'\n';
4390 e.iop->peekc = 0;
4391 e.iop->xchar = 0;
4392 e.iop->nlcount = 0;
4393 if (fn == filechar || fn == linechar)
4394 e.iop->task = XIO;
Eric Andersen392947c2002-12-11 07:42:46 +00004395 else if (fn == (int(*)(struct ioarg *))gravechar || fn == (int(*)(struct ioarg *))qgravechar)
Eric Andersenff9eee42001-06-29 04:57:14 +00004396 e.iop->task = XGRAVE;
4397 else
4398 e.iop->task = XOTHER;
4399}
4400
4401static struct io *
4402setbase(ip)
4403struct io *ip;
4404{
4405 register struct io *xp;
4406
4407 xp = e.iobase;
4408 e.iobase = ip;
4409 return(xp);
4410}
4411
4412/*
4413 * Input generating functions
4414 */
4415
4416/*
4417 * Produce the characters of a string, then a newline, then EOF.
4418 */
4419static int
4420nlchar(ap)
4421register struct ioarg *ap;
4422{
4423 register int c;
4424
4425 if (ap->aword == NULL)
4426 return(0);
4427 if ((c = *ap->aword++) == 0) {
4428 ap->aword = NULL;
4429 return('\n');
4430 }
4431 return(c);
4432}
4433
4434/*
4435 * Given a list of words, produce the characters
4436 * in them, with a space after each word.
4437 */
4438static int
4439wdchar(ap)
4440register struct ioarg *ap;
4441{
4442 register char c;
4443 register char **wl;
4444
4445 if ((wl = ap->awordlist) == NULL)
4446 return(0);
4447 if (*wl != NULL) {
4448 if ((c = *(*wl)++) != 0)
4449 return(c & 0177);
4450 ap->awordlist++;
4451 return(' ');
4452 }
4453 ap->awordlist = NULL;
4454 return('\n');
4455}
4456
4457/*
4458 * Return the characters of a list of words,
4459 * producing a space between them.
4460 */
4461static int
4462dolchar(ap)
4463register struct ioarg *ap;
4464{
4465 register char *wp;
4466
4467 if ((wp = *ap->awordlist++) != NULL) {
4468 PUSHIO(aword, wp, *ap->awordlist == NULL? strchar: xxchar);
4469 return(-1);
4470 }
4471 return(0);
4472}
4473
4474static int
4475xxchar(ap)
4476register struct ioarg *ap;
4477{
4478 register int c;
4479
4480 if (ap->aword == NULL)
4481 return(0);
4482 if ((c = *ap->aword++) == '\0') {
4483 ap->aword = NULL;
4484 return(' ');
4485 }
4486 return(c);
4487}
4488
4489/*
4490 * Produce the characters from a single word (string).
4491 */
4492static int
4493strchar(ap)
4494register struct ioarg *ap;
4495{
4496 register int c;
4497
4498 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4499 return(0);
4500 return(c);
4501}
4502
4503/*
4504 * Produce quoted characters from a single word (string).
4505 */
4506static int
4507qstrchar(ap)
4508register struct ioarg *ap;
4509{
4510 register int c;
4511
4512 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4513 return(0);
4514 return(c|QUOTE);
4515}
4516
4517/*
4518 * Return the characters from a file.
4519 */
4520static int
4521filechar(ap)
4522register struct ioarg *ap;
4523{
4524 register int i;
4525 char c;
4526 struct iobuf *bp = ap->afbuf;
4527
4528 if (ap->afid != AFID_NOBUF) {
4529 if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
4530 if (i)
4531 lseek(ap->afile, ap->afpos, 0);
Eric Andersen7467c8d2001-07-12 20:26:32 +00004532 i = safe_read(ap->afile, bp->buf, sizeof(bp->buf));
Eric Andersenff9eee42001-06-29 04:57:14 +00004533 if (i <= 0) {
4534 closef(ap->afile);
4535 return 0;
4536 }
4537 bp->id = ap->afid;
4538 bp->ebufp = (bp->bufp = bp->buf) + i;
4539 }
4540 ap->afpos++;
4541 return *bp->bufp++ & 0177;
4542 }
4543
Eric Andersenbdfd0d72001-10-24 05:00:29 +00004544#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersen737f5fb2003-03-14 16:05:59 +00004545 if (interactive && isatty(ap->afile)) {
Eric Andersenff9eee42001-06-29 04:57:14 +00004546 static char mycommand[BUFSIZ];
4547 static int position = 0, size = 0;
4548
4549 while (size == 0 || position >= size) {
4550 cmdedit_read_input(current_prompt, mycommand);
Eric Andersenff9eee42001-06-29 04:57:14 +00004551 size = strlen(mycommand);
4552 position = 0;
4553 }
4554 c = mycommand[position];
4555 position++;
4556 return(c);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004557 } else
Eric Andersenff9eee42001-06-29 04:57:14 +00004558#endif
4559 {
Eric Andersen7467c8d2001-07-12 20:26:32 +00004560 i = safe_read(ap->afile, &c, sizeof(c));
Eric Andersenff9eee42001-06-29 04:57:14 +00004561 return(i == sizeof(c)? c&0177: (closef(ap->afile), 0));
4562 }
4563}
4564
4565/*
4566 * Return the characters from a here temp file.
4567 */
4568static int
4569herechar(ap)
4570register struct ioarg *ap;
4571{
4572 char c;
4573
4574
4575 if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
4576 close(ap->afile);
4577 c = 0;
4578 }
4579 return (c);
4580
4581}
4582
4583/*
4584 * Return the characters produced by a process (`...`).
4585 * Quote them if required, and remove any trailing newline characters.
4586 */
4587static int
4588gravechar(ap, iop)
4589struct ioarg *ap;
4590struct io *iop;
4591{
4592 register int c;
4593
4594 if ((c = qgravechar(ap, iop)&~QUOTE) == '\n')
4595 c = ' ';
4596 return(c);
4597}
4598
4599static int
4600qgravechar(ap, iop)
4601register struct ioarg *ap;
4602struct io *iop;
4603{
4604 register int c;
4605
4606 if (iop->xchar) {
4607 if (iop->nlcount) {
4608 iop->nlcount--;
4609 return('\n'|QUOTE);
4610 }
4611 c = iop->xchar;
4612 iop->xchar = 0;
4613 } else if ((c = filechar(ap)) == '\n') {
4614 iop->nlcount = 1;
4615 while ((c = filechar(ap)) == '\n')
4616 iop->nlcount++;
4617 iop->xchar = c;
4618 if (c == 0)
4619 return(c);
4620 iop->nlcount--;
4621 c = '\n';
4622 }
4623 return(c!=0? c|QUOTE: 0);
4624}
4625
4626/*
4627 * Return a single command (usually the first line) from a file.
4628 */
4629static int
4630linechar(ap)
4631register struct ioarg *ap;
4632{
4633 register int c;
4634
4635 if ((c = filechar(ap)) == '\n') {
4636 if (!multiline) {
4637 closef(ap->afile);
4638 ap->afile = -1; /* illegal value */
4639 }
4640 }
4641 return(c);
4642}
4643
4644static void
4645prs(s)
4646register char *s;
4647{
4648 if (*s)
4649 write(2, s, strlen(s));
4650}
4651
4652static void
4653prn(u)
4654unsigned u;
4655{
Eric Andersen737f5fb2003-03-14 16:05:59 +00004656 prs(itoa(u));
Eric Andersenff9eee42001-06-29 04:57:14 +00004657}
4658
4659static void
4660closef(i)
4661register int i;
4662{
4663 if (i > 2)
4664 close(i);
4665}
4666
4667static void
4668closeall()
4669{
4670 register int u;
4671
4672 for (u=NUFILE; u<NOFILE;)
4673 close(u++);
4674}
4675
4676/*
4677 * remap fd into Shell's fd space
4678 */
4679static int
4680remap(fd)
4681register int fd;
4682{
4683 register int i;
4684 int map[NOFILE];
4685
4686 if (fd < e.iofd) {
4687 for (i=0; i<NOFILE; i++)
4688 map[i] = 0;
4689 do {
4690 map[fd] = 1;
4691 fd = dup(fd);
4692 } while (fd >= 0 && fd < e.iofd);
4693 for (i=0; i<NOFILE; i++)
4694 if (map[i])
4695 close(i);
4696 if (fd < 0)
4697 err("too many files open in shell");
4698 }
4699 return(fd);
4700}
4701
4702static int
4703openpipe(pv)
4704register int *pv;
4705{
4706 register int i;
4707
4708 if ((i = pipe(pv)) < 0)
4709 err("can't create pipe - try again");
4710 return(i);
4711}
4712
4713static void
4714closepipe(pv)
4715register int *pv;
4716{
4717 if (pv != NULL) {
4718 close(*pv++);
4719 close(*pv);
4720 }
4721}
4722
4723/* -------- here.c -------- */
4724
4725/*
4726 * here documents
4727 */
4728
4729static void
4730markhere(s, iop)
4731register char *s;
4732struct ioword *iop;
4733{
4734 register struct here *h, *lh;
4735
4736 h = (struct here *) space(sizeof(struct here));
4737 if (h == 0)
4738 return;
4739 h->h_tag = evalstr(s, DOSUB);
4740 if (h->h_tag == 0)
4741 return;
4742 h->h_iop = iop;
4743 iop->io_name = 0;
4744 h->h_next = NULL;
4745 if (inhere == 0)
4746 inhere = h;
4747 else
4748 for (lh = inhere; lh!=NULL; lh = lh->h_next)
4749 if (lh->h_next == 0) {
4750 lh->h_next = h;
4751 break;
4752 }
4753 iop->io_flag |= IOHERE|IOXHERE;
4754 for (s = h->h_tag; *s; s++)
4755 if (*s & QUOTE) {
4756 iop->io_flag &= ~ IOXHERE;
4757 *s &= ~ QUOTE;
4758 }
4759 h->h_dosub = iop->io_flag & IOXHERE;
4760}
4761
4762static void
4763gethere()
4764{
4765 register struct here *h, *hp;
4766
4767 /* Scan here files first leaving inhere list in place */
4768 for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
4769 readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub? 0: '\'');
4770
4771 /* Make inhere list active - keep list intact for scraphere */
4772 if (hp != NULL) {
4773 hp->h_next = acthere;
4774 acthere = inhere;
4775 inhere = NULL;
4776 }
4777}
4778
4779static void
4780readhere(name, s, ec)
4781char **name;
4782register char *s;
4783int ec;
4784{
4785 int tf;
4786 char tname[30] = ".msh_XXXXXX";
4787 register int c;
4788 jmp_buf ev;
4789 char myline [LINELIM+1];
4790 char *thenext;
4791
4792 tf = mkstemp(tname);
4793 if (tf < 0)
4794 return;
4795 *name = strsave(tname, areanum);
4796 if (newenv(setjmp(errpt = ev)) != 0)
4797 unlink(tname);
4798 else {
Eric Andersen392947c2002-12-11 07:42:46 +00004799 pushio(e.iop->argp, (int(*)(struct ioarg *))e.iop->iofn);
Eric Andersenff9eee42001-06-29 04:57:14 +00004800 e.iobase = e.iop;
4801 for (;;) {
4802 if (interactive && e.iop <= iostack) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +00004803#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersenff9eee42001-06-29 04:57:14 +00004804 current_prompt=cprompt->value;
4805#else
4806 prs(cprompt->value);
4807#endif
4808 }
4809 thenext = myline;
4810 while ((c = my_getc(ec)) != '\n' && c) {
4811 if (ec == '\'')
4812 c &= ~ QUOTE;
4813 if (thenext >= &myline[LINELIM]) {
4814 c = 0;
4815 break;
4816 }
4817 *thenext++ = c;
4818 }
4819 *thenext = 0;
4820 if (strcmp(s, myline) == 0 || c == 0)
4821 break;
4822 *thenext++ = '\n';
4823 write (tf, myline, (int)(thenext-myline));
4824 }
4825 if (c == 0) {
4826 prs("here document `"); prs(s); err("' unclosed");
4827 }
4828 quitenv();
4829 }
4830 close(tf);
4831}
4832
4833/*
4834 * open here temp file.
4835 * if unquoted here, expand here temp file into second temp file.
4836 */
4837static int
4838herein(hname, xdoll)
4839char *hname;
4840int xdoll;
4841{
4842 register int hf;
4843 int tf;
4844
4845#if __GNUC__
4846 /* Avoid longjmp clobbering */
4847 (void) &tf;
4848#endif
4849 if (hname == 0)
4850 return(-1);
4851 hf = open(hname, 0);
4852 if (hf < 0)
4853 return (-1);
4854 if (xdoll) {
4855 char c;
4856 char tname[30] = ".msh_XXXXXX";
4857 jmp_buf ev;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004858
Eric Andersenff9eee42001-06-29 04:57:14 +00004859 tf = mkstemp(tname);
4860 if (tf < 0)
4861 return (-1);
4862 if (newenv(setjmp(errpt = ev)) == 0) {
4863 PUSHIO(afile, hf, herechar);
4864 setbase(e.iop);
4865 while ((c = subgetc(0, 0)) != 0) {
4866 c &= ~ QUOTE;
4867 write(tf, &c, sizeof c);
4868 }
4869 quitenv();
4870 } else
4871 unlink(tname);
4872 close(tf);
4873 tf = open(tname, 0);
4874 unlink(tname);
4875 return (tf);
4876 } else
4877 return (hf);
4878}
4879
4880static void
4881scraphere()
4882{
4883 register struct here *h;
4884
4885 for (h = inhere; h != NULL; h = h->h_next) {
4886 if (h->h_iop && h->h_iop->io_name)
4887 unlink(h->h_iop->io_name);
4888 }
4889 inhere = NULL;
4890}
4891
4892/* unlink here temp files before a freearea(area) */
4893static void
4894freehere(area)
4895int area;
4896{
4897 register struct here *h, *hl;
4898
4899 hl = NULL;
4900 for (h = acthere; h != NULL; h = h->h_next)
4901 if (getarea((char *) h) >= area) {
4902 if (h->h_iop->io_name != NULL)
4903 unlink(h->h_iop->io_name);
4904 if (hl == NULL)
4905 acthere = h->h_next;
4906 else
4907 hl->h_next = h->h_next;
4908 } else
4909 hl = h;
4910}
4911
4912
4913
4914/*
4915 * Copyright (c) 1987,1997, Prentice Hall
4916 * All rights reserved.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004917 *
Eric Andersenff9eee42001-06-29 04:57:14 +00004918 * Redistribution and use of the MINIX operating system in source and
4919 * binary forms, with or without modification, are permitted provided
4920 * that the following conditions are met:
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004921 *
Eric Andersenff9eee42001-06-29 04:57:14 +00004922 * Redistributions of source code must retain the above copyright
4923 * notice, this list of conditions and the following disclaimer.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004924 *
Eric Andersenff9eee42001-06-29 04:57:14 +00004925 * Redistributions in binary form must reproduce the above
4926 * copyright notice, this list of conditions and the following
4927 * disclaimer in the documentation and/or other materials provided
4928 * with the distribution.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004929 *
Eric Andersenff9eee42001-06-29 04:57:14 +00004930 * Neither the name of Prentice Hall nor the names of the software
4931 * authors or contributors may be used to endorse or promote
4932 * products derived from this software without specific prior
4933 * written permission.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004934 *
Eric Andersenff9eee42001-06-29 04:57:14 +00004935 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
4936 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
4937 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4938 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4939 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
4940 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4941 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4942 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
4943 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4944 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
4945 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
4946 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4947 *
4948 */
4949